/* ---------------------------------- токены ---------------------------------- */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6ec;
  --text: #16181d;
  --text-dim: #6b7280;
  --text-faint: #9aa1ad;
  --accent: #4f46e5;
  --accent-soft: #eef0ff;
  --income: #0f9d58;
  --expense: #e03131;
  --transfer: #6b7280;
  --warn: #f08c00;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px rgba(16, 24, 40, .06);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
}
html[data-theme="dark"] {
  --bg: #0e1013;
  --surface: #171a1f;
  --surface-2: #1f232a;
  --border: #272c34;
  --text: #e8eaed;
  --text-dim: #9aa1ad;
  --text-faint: #6b7280;
  --accent: #818cf8;
  --accent-soft: #1e2130;
  --income: #4ade80;
  --expense: #f87171;
  --transfer: #9aa1ad;
  --warn: #fbbf24;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
}
@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] {
    --bg: #0e1013; --surface: #171a1f; --surface-2: #1f232a; --border: #272c34;
    --text: #e8eaed; --text-dim: #9aa1ad; --text-faint: #6b7280;
    --accent: #818cf8; --accent-soft: #1e2130;
    --income: #4ade80; --expense: #f87171; --transfer: #9aa1ad; --warn: #fbbf24;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 8px 24px rgba(0,0,0,.25);
  }
}

* { box-sizing: border-box; }
html {
  -webkit-text-size-adjust: 100%;          /* iOS не раздувает шрифт в альбомной ориентации */
  -webkit-tap-highlight-color: transparent; /* без серой вспышки по тапу */
}
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 var(--sans); -webkit-font-smoothing: antialiased;
}
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 22px; }
h2 { font-size: 16px; }
a { color: inherit; text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
::selection { background: var(--accent); color: #fff; }

/* ---------------------------------- каркас ---------------------------------- */
.app { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  display: flex; flex-direction: column; gap: 22px; padding: 20px 14px;
  background: var(--surface); border-right: 1px solid var(--border);
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px; }
.brand-mark {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px;
  background: var(--accent); color: #fff; font-weight: 700;
}
.brand-text { font-weight: 650; font-size: 16px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 9px 11px;
  border-radius: var(--radius-sm); color: var(--text-dim); font-weight: 500;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: var(--accent-soft); color: var(--accent); }
.nav-ico { width: 18px; text-align: center; opacity: .9; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 10px; }
.hint { color: var(--text-faint); font-size: 12px; padding: 0 4px; }

.main { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 18px 26px;
  border-bottom: 1px solid var(--border); background: var(--surface);
  position: sticky; top: 0; z-index: 5;
}
.topbar-actions { margin-left: auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.content { padding: 22px 26px 60px; max-width: 1280px; width: 100%; }

/* ---------------------------------- кнопки ---------------------------------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--surface); cursor: pointer; font-weight: 550;
  transition: background .12s, border-color .12s, transform .06s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-sm { padding: 5px 10px; font-size: 13px; }
.btn-danger { color: var(--expense); border-color: var(--border); }
.btn-danger:hover { background: color-mix(in srgb, var(--expense) 12%, transparent); }
.btn-icon { padding: 5px 8px; }

/* ----------------------------- поля и переключатели -------------------------- */
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 12.5px; color: var(--text-dim); font-weight: 550; }
.input, .select, textarea.input {
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--bg); width: 100%; outline: none;
}
.input:focus, .select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input-xl { font-size: 28px; font-weight: 650; font-variant-numeric: tabular-nums; padding: 12px; text-align: right; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > * { flex: 1 1 140px; min-width: 0; }

.segmented { display: inline-flex; background: var(--surface-2); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.segmented button {
  border: 0; background: transparent; padding: 7px 14px; border-radius: 7px;
  cursor: pointer; color: var(--text-dim); font-weight: 550;
}
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented.kind button.active[data-kind="expense"] { color: var(--expense); }
.segmented.kind button.active[data-kind="income"] { color: var(--income); }

/* ---------------------------------- карточки -------------------------------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 14px; }
.card-head h2 { margin-right: auto; }
.grid { display: grid; gap: 14px; }
.grid-kpi { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid-2 { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); }
.grid-cols-2 { grid-template-columns: repeat(auto-fit, minmax(290px, 1fr)); }
.stack { display: flex; flex-direction: column; gap: 14px; }

.kpi-label { color: var(--text-dim); font-size: 13px; display: flex; align-items: center; gap: 6px; }
.kpi-value { font-size: 25px; font-weight: 680; letter-spacing: -.02em; font-variant-numeric: tabular-nums; margin-top: 6px; }
.kpi-sub { color: var(--text-faint); font-size: 12.5px; margin-top: 4px; }
.pos { color: var(--income); } .neg { color: var(--expense); } .dim { color: var(--text-dim); }
.num { font-variant-numeric: tabular-nums; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px;
  border-radius: 999px; font-size: 12px; background: var(--surface-2); color: var(--text-dim);
}
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }

/* ---------------------------------- таблицы --------------------------------- */
.table { width: 100%; border-collapse: collapse; }
.table th {
  text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-faint); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--border);
}
.table td { padding: 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:hover { background: var(--surface-2); }
.table tr.day-row td { background: var(--surface-2); font-size: 12.5px; color: var(--text-dim); font-weight: 600; padding: 6px 10px; }
.table .right { text-align: right; }
.table .amount { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.tx-cat { display: flex; align-items: center; gap: 9px; }
.tx-ico {
  display: grid; place-items: center; width: 30px; height: 30px; border-radius: 9px;
  background: var(--surface-2); font-size: 15px; flex: none;
}
.tx-note { color: var(--text-dim); font-size: 13px; }
.actions-cell { white-space: nowrap; opacity: 0; transition: opacity .12s; }
tr:hover .actions-cell { opacity: 1; }

/* -------------------------------- список категорий -------------------------- */
.cat-list { display: flex; flex-direction: column; gap: 10px; }
.cat-row { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center; }
.cat-name { display: flex; align-items: center; gap: 8px; min-width: 0; }
.cat-name span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar { grid-column: 1 / -1; height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; border-radius: 99px; }

.sms-body {
  font-size: 13.5px; line-height: 1.45; max-width: 640px;
  overflow-wrap: anywhere; white-space: pre-wrap;
}

/* -------------------------------- бюджеты ----------------------------------- */
.budget { padding: 14px 0; border-bottom: 1px solid var(--border); }
.budget:last-child { border-bottom: 0; }
.budget-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.budget-head .cat-name { margin-right: auto; }
.budget-nums { font-variant-numeric: tabular-nums; font-size: 13.5px; color: var(--text-dim); }

/* ---------------------------------- модалка ---------------------------------- */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(10, 12, 16, .45); backdrop-filter: blur(3px);
  display: grid; place-items: center; padding: 20px; z-index: 50;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 18px;
  width: min(520px, 100%); max-height: 90vh; overflow: auto; box-shadow: 0 24px 64px rgba(0, 0, 0, .3);
}
.modal-head { display: flex; align-items: center; gap: 10px; padding: 18px 20px 0; }
.modal-head h2 { margin-right: auto; }
.modal-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 14px; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-end; padding: 0 20px 20px; }
.error-box {
  background: color-mix(in srgb, var(--expense) 12%, transparent);
  color: var(--expense); padding: 9px 12px; border-radius: var(--radius-sm); font-size: 13.5px;
}

/* --------------------------------- вход ------------------------------------- */
.auth-screen {
  position: fixed; inset: 0; z-index: 90; background: var(--bg);
  display: grid; place-items: center; padding: 20px;
}
.auth-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; text-align: center; }
.auth-card h2 { font-size: 18px; }
.auth-mark {
  width: 46px; height: 46px; border-radius: 13px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-size: 22px; font-weight: 700; margin: 0 auto;
}

/* ---------------------------------- тосты ----------------------------------- */
.toasts { position: fixed; bottom: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 80; }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  padding: 10px 14px; border-radius: var(--radius-sm); box-shadow: var(--shadow);
  animation: slide .18s ease-out; max-width: 340px;
}
.toast.err { border-left-color: var(--expense); }
@keyframes slide { from { opacity: 0; transform: translateY(8px); } }

/* ---------------------------------- прочее ---------------------------------- */
/* новая строка подсвечивается на пару секунд, чтобы её было видно */
@keyframes rowNew {
  from { background: color-mix(in srgb, var(--accent) 22%, transparent); }
  to   { background: transparent; }
}
.table tr.row-new { animation: rowNew 2.4s ease-out; }
.live-dot .dot { animation: livePulse 2s infinite; }
@keyframes livePulse { 50% { opacity: .35; } }

.only-mobile { display: none; }

.empty { text-align: center; padding: 40px 20px; color: var(--text-faint); }
.empty-ico { font-size: 34px; display: block; margin-bottom: 10px; opacity: .6; }
.skeleton { height: 120px; border-radius: var(--radius); background: var(--surface-2); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .55; } }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--text-dim); font-size: 11px; font-family: var(--sans); }
.donut-center-value { fill: var(--text); font-size: 17px; font-weight: 650; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: flex-end; }
.filters .field { flex: 0 1 165px; }

/* --------------------------------- телефон ----------------------------------- */
/* Телефон: навигация уезжает в нижнюю панель, таблицы превращаются в карточки,
   учитываются безопасные зоны (Dynamic Island сверху, полоса «Домой» снизу). */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }

  /* нижняя панель вкладок вместо боковой колонки */
  .sidebar {
    position: fixed; inset: auto 0 0 0; height: auto; z-index: 30;
    flex-direction: row; gap: 0;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    border-right: 0; border-top: 1px solid var(--border);
    background: color-mix(in srgb, var(--surface) 88%, transparent);
    backdrop-filter: saturate(180%) blur(14px);
    -webkit-backdrop-filter: saturate(180%) blur(14px);
  }
  .brand, .sidebar-foot { display: none; }
  .nav { flex-direction: row; flex: 1; gap: 0; }
  .nav-item {
    flex: 1; flex-direction: column; justify-content: center; gap: 3px;
    min-height: 50px; padding: 6px 2px; border-radius: 12px;
    font-size: 11px; font-weight: 500; text-align: center;
  }
  .nav-item.active { background: transparent; }
  .nav-ico { width: auto; font-size: 18px; line-height: 1; }

  /* шапка: заголовок и кнопка в строку, переключатель периода прокручивается */
  .topbar {
    flex-wrap: wrap; gap: 8px 10px;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  }
  .topbar h1 {
    font-size: 17px; flex: 1 1 auto; min-width: 0;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  }
  .topbar-actions {
    order: 3; flex-basis: 100%; margin-left: 0; flex-wrap: nowrap;
    overflow-x: auto; scrollbar-width: none; padding-bottom: 2px;
  }
  .topbar-actions::-webkit-scrollbar { display: none; }
  .topbar-actions > * { flex: none; }
  #add-tx { padding: 8px 12px; }

  /* контент не прячется под нижнюю панель */
  .content { padding: 14px 16px calc(84px + env(safe-area-inset-bottom)); }

  .grid { gap: 10px; }
  .grid-2, .grid-cols-2 { grid-template-columns: 1fr; }
  .grid-kpi { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .card { padding: 14px; border-radius: 16px; }
  .kpi-value { font-size: 21px; }

  /* поля: 16px, иначе Safari зумит страницу при фокусе */
  .input, .select, textarea.input { font-size: 16px; padding: 11px 12px; }
  .input-xl { font-size: 30px; }
  .btn { min-height: 40px; }
  .btn-sm { min-height: 34px; }
  .filters .field { flex: 1 1 calc(50% - 4px); }
  .segmented button { padding: 8px 12px; }

  /* таблицы -> карточки: шапка не нужна, строка переносится по словам */
  .table thead { display: none; }
  .table, .table tbody, .table tr { display: block; width: 100%; }
  .table td { display: block; width: auto; }   /* иначе каждая ячейка занимает всю строку */
  .table tr {
    display: flex; flex-wrap: wrap; align-items: center; gap: 2px 10px;
    padding: 11px 2px; border-bottom: 1px solid var(--border);
  }
  .table td { border: 0; padding: 0; }
  .table td:first-child { flex: 1 1 auto; min-width: 0; }
  .table td.right { margin-left: auto; text-align: right; }
  .table tr.day-row { padding: 7px 2px; }
  .table tr.day-row td { flex: 1 1 100%; }
  .table .col-hide-mobile { display: none; }
  .only-mobile { display: inline; }
  .sms-body { flex: 1 1 100%; margin-top: 4px; max-width: 100%; }

  /* действия всегда видны: наводить мышью на телефоне нечем */
  .actions-cell { opacity: 1; }

  /* модалка как шторка снизу */
  .modal-backdrop { align-items: end; justify-items: stretch; padding: 0; }
  .modal {
    width: 100%; max-width: none; max-height: 92dvh;
    border-radius: 20px 20px 0 0; border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .modal-foot { position: sticky; bottom: 0; background: var(--surface); padding-top: 8px; }

  /* широкие графики прокручиваются, а не ломают вёрстку */
  .chart { overflow-x: auto; scrollbar-width: none; }
  .chart::-webkit-scrollbar { display: none; }

  /* бюджет: название на своей строке, цифры и кнопки — на следующей */
  .budget-head { flex-wrap: wrap; gap: 6px 8px; }
  .budget-head .cat-name { flex: 1 1 100%; margin-right: 0; }
  .budget-head .budget-nums { margin-right: auto; }
  .toasts { left: 16px; right: 16px; bottom: calc(80px + env(safe-area-inset-bottom)); }
  .toast { max-width: none; }
  .auth-screen { padding: 16px; align-content: center; }
}

@media (hover: none) {
  .actions-cell { opacity: 1; }
}
