*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #eef2f7;
  --card:      #ffffff;
  --primary:   #4f6cf7;
  --primary-h: #3a55e0;
  --success:   #10b981;
  --danger:    #ef4444;
  --text:      #1e293b;
  --muted:     #64748b;
  --border:    #e2e8f0;
  --radius:    12px;
  --shadow:    0 4px 24px rgba(0,0,0,.10);
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 16px 48px;
}

[x-cloak] { display: none !important; }

.card { opacity: 0; animation: fadeIn .2s .05s forwards; }
@keyframes fadeIn { to { opacity: 1; } }

/* ── Card ─────────────────────────────────────────────── */
.card {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* ── Header ───────────────────────────────────────────── */
.header {
  background: linear-gradient(135deg, #4f6cf7, #7c3aed);
  color: #fff;
  padding: 18px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.header__title {
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: .01em;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.date-chip {
  background: rgba(255,255,255,.2);
  border: none;
  color: #fff;
  border-radius: 20px;
  padding: 5px 12px;
  font-size: .85rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: background .15s;
  white-space: nowrap;
}
.date-chip:hover { background: rgba(255,255,255,.3); }

.user-chip {
  background: rgba(255,255,255,.15);
  border: none;
  color: #fff;
  border-radius: 20px;
  padding: 5px 12px;
  font-size: .82rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 5px;
  transition: background .15s;
  white-space: nowrap;
  max-width: 130px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-chip:hover { background: rgba(255,255,255,.28); }

.reset-chip {
  background: rgba(255,255,255,.15);
  border: none;
  color: #fff;
  border-radius: 20px;
  width: 30px;
  height: 30px;
  font-size: .9rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
  flex-shrink: 0;
}
.reset-chip:hover { background: rgba(255, 80, 80, .45); }

/* ── Progress bar ─────────────────────────────────────── */
.progress-bar {
  display: flex;
  padding: 12px 20px 0;
  gap: 5px;
}
.progress-bar__dot {
  flex: 1;
  height: 4px;
  border-radius: 2px;
  background: var(--border);
  transition: background .3s;
}
.progress-bar__dot.done   { background: var(--success); }
.progress-bar__dot.active { background: var(--primary); }

/* ── Summary ──────────────────────────────────────────── */
.summary {
  padding: 14px 20px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.summary-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: .85rem;
}
.summary-row__label { color: var(--muted); }
.summary-row__value { font-weight: 500; text-align: right; max-width: 65%; word-break: break-word; }

.divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 14px 0 0;
}

/* ── Step content ─────────────────────────────────────── */
.step {
  padding: 20px;
}

/* Step header: back button + question on one line */
.step-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 14px;
}

.btn-back {
  flex-shrink: 0;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color .15s, color .15s, transform .1s;
}
.btn-back:hover  { border-color: var(--primary); color: var(--primary); }
.btn-back:active { transform: scale(.95); }

.question {
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.4;
  margin-bottom: 0;
}

/* Amount input */
.amount-row {
  display: flex;
  gap: 8px;
}
.input-amount {
  flex: 1;
  font-size: 1.4rem;
  font-weight: 500;
  padding: 10px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  transition: border-color .15s;
}
.input-amount:focus { border-color: var(--primary); }

/* Text / comment input */
.input-text {
  width: 100%;
  font-size: 1rem;
  padding: 10px 14px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  margin-bottom: 10px;
  transition: border-color .15s;
}
.input-text:focus { border-color: var(--primary); }

/* Options grid */
.options-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.options-grid--3col {
  grid-template-columns: 1fr 1fr 1fr;
}
.btn-option {
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: .88rem;
  font-weight: 500;
  cursor: pointer;
  text-align: center;
  transition: border-color .15s, background .15s, transform .1s;
  line-height: 1.3;
  word-break: break-word;
}
.btn-option:hover             { border-color: var(--primary); background: #f0f4ff; }
.btn-option:active            { transform: scale(.97); }
.btn-option--selected         { border-color: var(--primary); background: #eef2ff; color: var(--primary); }

/* ── Buttons ──────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: .92rem;
  font-weight: 500;
  cursor: pointer;
  border: none;
  transition: background .15s, transform .1s, opacity .15s;
}
.btn:active   { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn--primary { background: var(--primary); color: #fff; }
.btn--primary:hover:not(:disabled) { background: var(--primary-h); }

.btn--secondary { background: var(--bg); color: var(--text); border: 2px solid var(--border); }
.btn--secondary:hover { border-color: var(--primary); }

.btn--ghost { background: transparent; color: var(--muted); padding: 8px 12px; }
.btn--ghost:hover { color: var(--text); }

.btn--full { width: 100%; }

/* ── Comment actions ──────────────────────────────────── */
.comment-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── Done / error states ──────────────────────────────── */
.done-block {
  padding: 32px 20px;
  text-align: center;
}
.done-icon  { font-size: 3rem; margin-bottom: 12px; }
.done-title { font-size: 1.2rem; font-weight: 600; margin-bottom: 6px; }
.done-sub   { color: var(--muted); font-size: .9rem; margin-bottom: 8px; }

/* Остаток после операции */
.balance-info {
  display: inline-block;
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: .88rem;
  color: #166534;
  margin-bottom: 4px;
  text-align: left;
  line-height: 1.6;
}

.error-msg {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--danger);
  border-radius: 8px;
  padding: 10px 14px;
  font-size: .88rem;
  margin-top: 8px;
}

/* ── Date picker overlay ──────────────────────────────── */
.overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 16px;          /* отступ от краёв на мобильном */
  overflow-y: auto;       /* прокрутка если не влезает */
}
.picker-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px;
  width: 100%;            /* адаптивная ширина */
  max-width: 300px;       /* но не шире 300px */
  box-shadow: var(--shadow);
  flex-shrink: 0;
}
.picker-card h3 { margin-bottom: 14px; font-size: 1rem; }
.input-date {
  width: 100%;
  padding: 10px 12px;
  border: 2px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px;        /* минимум 16px — иначе iOS зумит страницу */
  margin-bottom: 14px;
  outline: none;
  box-sizing: border-box;
}
.input-date:focus { border-color: var(--primary); }
.picker-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

/* ── Auth form ────────────────────────────────────────── */
.auth-form {
  padding: 28px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 0;
}

.auth-title {
  font-size: 1.1rem;
  font-weight: 700;
  margin-bottom: 20px;
  color: var(--text);
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 14px;
}

.auth-label {
  font-size: .82rem;
  font-weight: 500;
  color: var(--muted);
}

.pwd-wrap {
  position: relative;
  display: flex;
  align-items: center;
}
.pwd-wrap .input-text {
  width: 100%;
  padding-right: 42px;
  margin-bottom: 0;
}
.pwd-toggle {
  position: absolute;
  right: 10px;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  padding: 2px 4px;
  color: var(--muted);
  transition: color .15s;
  user-select: none;
}
.pwd-toggle:hover { color: var(--text); }

.auth-switch {
  margin-top: 18px;
  text-align: center;
  font-size: .85rem;
  color: var(--muted);
}

.auth-link {
  background: none;
  border: none;
  color: var(--primary);
  font-size: .85rem;
  font-weight: 500;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 2px;
}
.auth-link:hover { color: var(--primary-h); }

/* ── Tab bar ──────────────────────────────────────────── */
.tab-bar {
  display: flex;
  border-bottom: 1px solid var(--border);
}
.tab-btn {
  flex: 1;
  padding: 10px 4px;
  font-size: .82rem;
  font-weight: 500;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--muted);
  cursor: pointer;
  transition: color .15s, border-color .15s;
  white-space: nowrap;
}
.tab-btn:hover          { color: var(--text); }
.tab-btn--active        { color: var(--primary); border-bottom-color: var(--primary); }

/* ── History ──────────────────────────────────────────── */
.hist-balance {
  background: #f0fdf4;
  border-bottom: 1px solid #bbf7d0;
  color: #166534;
  font-size: .88rem;
  font-weight: 500;
  padding: 10px 20px;
}

.hist-empty {
  color: var(--muted);
  font-size: .9rem;
  text-align: center;
  padding: 24px 0 8px;
}

.hist-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 55vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 4px;
}

.hist-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  border-left-width: 3px;
}
.hist-item--expense { border-left-color: var(--danger); }
.hist-item--income  { border-left-color: var(--success); }

.hist-item__top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}
.hist-item__date {
  font-size: .78rem;
  color: var(--muted);
}
.hist-item__mode {
  font-size: .78rem;
  font-weight: 500;
  color: var(--muted);
}
.hist-item__bottom {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
.hist-item__category {
  font-size: .85rem;
  color: var(--text);
  max-width: 60%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.hist-item__amount {
  font-size: .95rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.hist-item--income  .hist-item__amount { color: var(--success); }
.hist-item--expense .hist-item__amount { color: var(--danger); }
.hist-item__comment {
  font-size: .78rem;
  color: var(--muted);
  margin-top: 4px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ── Balances ─────────────────────────────────────────── */
.bal-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 4px;
}

/* Обёртка: кнопка-строка + раскрывающаяся история */
.bal-entry {
  display: flex;
  flex-direction: column;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

/* Базовая строка остатка */
.bal-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 14px;
  background: var(--bg);
  font-family: inherit;
  font-size: inherit;
  text-align: left;
}

/* Кнопка-строка (внутри .bal-entry) */
.bal-item--clickable {
  cursor: pointer;
  border: none;
  width: 100%;
  transition: background .15s;
}
.bal-item--clickable:hover { background: #e8edf7; }
.bal-item--clickable:active { background: #dce3f5; }

/* Строка «Итого» — самостоятельный элемент (не внутри .bal-entry) */
.bal-item--total {
  background: #f0fdf4;
  border: 1px solid #bbf7d0;
  border-radius: 10px;
  margin-top: 4px;
}

/* Правая часть кнопки: сумма + шеврон */
.bal-item__right {
  display: flex;
  align-items: center;
  gap: 8px;
}
.bal-chevron {
  font-size: .7rem;
  color: var(--muted);
  flex-shrink: 0;
}

.bal-item__name   { font-size: .88rem; color: var(--muted); flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.bal-item__amount { font-size: 1rem; font-weight: 600; color: var(--text); white-space: nowrap; }
.bal-item--total .bal-item__name   { font-weight: 600; color: var(--text); }
.bal-item--total .bal-item__amount { color: var(--success); }

/* Раскрывающаяся история внутри bal-entry */
.bal-hist {
  background: #fff;
  border-top: 1px solid var(--border);
  padding: 8px 12px 12px;
}
.bal-hist__loading {
  color: var(--muted);
  font-size: .88rem;
  padding: 10px 0 4px;
  text-align: center;
}
/* История внутри аккордеона — ограниченная высота */
.bal-hist .hist-list {
  max-height: 40vh;
}
.bal-hist .hist-empty {
  padding: 12px 0 4px;
}

/* ── Done-page history ────────────────────────────────────── */
.done-hist {
  margin-top: 24px;
  text-align: left;
  border-top: 1px solid var(--border);
  padding-top: 16px;
}
.done-hist__title {
  font-size: .88rem;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 10px;
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
}
.done-hist__sub {
  font-size: .78rem;
  font-weight: 400;
  color: var(--muted);
}
.done-hist .hist-list { max-height: 45vh; }

/* ── Refresh button (balances header) ─────────────────────── */
.btn-refresh {
  margin-left: auto;
  flex-shrink: 0;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 8px;
  width: 34px;
  height: 34px;
  font-size: .95rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: border-color .15s, transform .3s;
}
.btn-refresh:hover:not(:disabled)  { border-color: var(--primary); }
.btn-refresh:active:not(:disabled) { transform: rotate(180deg); }
.btn-refresh:disabled              { opacity: .4; cursor: not-allowed; }

/* ── Admin button ─────────────────────────────────────── */
.admin-row {
  padding: 0 20px 16px;
  display: flex;
  justify-content: flex-end;
}
.btn--admin {
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: .78rem;
  cursor: pointer;
  padding: 4px 6px;
}
.btn--admin:hover       { color: var(--text); }
.btn--admin:disabled    { opacity: .4; cursor: not-allowed; }
