/* Цвета: светлая тема по умолчанию, тёмная — по настройке системы. */
:root {
  color-scheme: light dark;
  --bg: #f3f5f7;
  --surface: #ffffff;
  --surface-2: #f0f3f5;
  --text: #17202a;
  --muted: #5d6b7a;
  --border: #dde3e8;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --accent-soft: #d9f2ee;
  --pos: #12733b;
  --neg: #b3261e;
  --warn-bg: #fff4d6;
  --warn-text: #7a4b00;
  --ok-bg: #e2f5e9;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 16px rgb(16 24 40 / 6%);
  --radius: 14px;
  --nav-h: 0px;
  --target: 36px;
  accent-color: var(--accent);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #101418;
    --surface: #191e24;
    --surface-2: #222930;
    --text: #e8ecf0;
    --muted: #9aa7b4;
    --border: #2d353e;
    --accent: #2cc5b3;
    --accent-text: #06231f;
    --accent-soft: #143732;
    --pos: #5bd68a;
    --neg: #ff8a80;
    --warn-bg: #3d3113;
    --warn-text: #ffd98a;
    --ok-bg: #163524;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%);
  }
}
@media (pointer: coarse) {
  :root { --target: 44px; }
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
html { scroll-padding-bottom: calc(var(--nav-h) + 16px); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  overflow-wrap: break-word;
}
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.4rem; line-height: 1.2; text-wrap: balance; }
h2 { font-size: 1.05rem; }
h3 { font-size: .85rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.neutral { color: var(--muted); }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }
.sr-only { position: absolute; inline-size: 1px; block-size: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- элементы управления ---------- */
input, select, button, textarea { font: inherit; color: inherit; }
input:not([type='checkbox'], [type='radio'], [type='file']), select {
  inline-size: 100%;
  min-block-size: var(--target);
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}
input[type='file'] { inline-size: 100%; min-block-size: var(--target); }
input[type='checkbox'] { inline-size: 18px; block-size: 18px; flex: none; }
select.compact { inline-size: auto; }
.amount-input { font-size: 1.25rem; font-weight: 600; font-variant-numeric: tabular-nums; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-block-size: var(--target);
  padding: 8px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font-weight: 600;
  text-decoration: none;
  color: var(--text);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.income { color: var(--pos); }
.btn.expense { color: var(--neg); }
.btn.danger { background: var(--neg); border-color: var(--neg); color: var(--surface); }
.btn.danger-text { color: var(--neg); }
.btn.small { padding: 4px 10px; font-size: .875rem; }
.btn.block { inline-size: 100%; }
.icon-btn {
  position: relative;
  inline-size: 36px; block-size: 36px;
  display: inline-grid; place-items: center;
  background: none; border: 0; border-radius: 50%;
  font-size: 1.2rem; cursor: pointer;
}
.icon-btn::after { content: ''; position: absolute; inset: -4px; }
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.link { padding: 0; background: none; border: 0; color: var(--accent); font-weight: 600; text-decoration: underline; cursor: pointer; }

.field { display: flex; flex-direction: column; gap: 4px; min-inline-size: 0; }
.field-label { font-size: .875rem; font-weight: 600; color: var(--muted); }
.hint { font-size: .8rem; color: var(--muted); }
.check { display: flex; align-items: center; gap: 10px; min-block-size: var(--target); }
.row2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.grid-auto { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.actions, .toolbar { display: flex; flex-wrap: wrap; gap: 8px; }

.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; padding: 3px; background: var(--surface-2); border-radius: 12px; }
.segmented label { display: block; text-align: center; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 8px; border-radius: 9px; font-weight: 600; color: var(--muted); }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }
form[data-kind='expense'] .segmented input:checked + span { color: var(--neg); }
form[data-kind='income'] .segmented input:checked + span { color: var(--pos); }

/* ---------- каркас ---------- */
.topbar {
  position: sticky; inset-block-start: 0; z-index: 5;
  display: flex; align-items: center; gap: 24px;
  padding: 8px max(16px, env(safe-area-inset-right, 0px)) 8px max(16px, env(safe-area-inset-left, 0px));
  background: var(--surface);
  border-block-end: 1px solid var(--border);
}
.brand { font-weight: 800; font-size: 1.1rem; color: var(--text); text-decoration: none; }
.nav { display: flex; gap: 4px; }
.nav a {
  display: inline-flex; align-items: center; gap: 8px;
  min-block-size: var(--target);
  padding: 6px 12px;
  border-radius: 10px;
  color: var(--muted); text-decoration: none; font-weight: 600;
}
.nav a[aria-current='page'] { background: var(--accent-soft); color: var(--accent); }
.nav svg { inline-size: 20px; block-size: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.view { max-inline-size: 1080px; margin-inline: auto; padding: 16px; padding-block-end: calc(var(--nav-h) + 32px); }
.view-inner { display: grid; gap: 16px; }
.view-inner > * { min-inline-size: 0; }

@media (max-width: 700px) {
  :root { --nav-h: calc(58px + env(safe-area-inset-bottom, 0px)); }
  .topbar { position: static; justify-content: center; }
  .nav {
    position: fixed; inset-inline: 0; inset-block-end: 0; z-index: 5;
    justify-content: space-around;
    padding: 4px max(8px, env(safe-area-inset-right, 0px)) max(4px, env(safe-area-inset-bottom, 0px)) max(8px, env(safe-area-inset-left, 0px));
    background: var(--surface);
    border-block-start: 1px solid var(--border);
  }
  .nav a { flex: 1; flex-direction: column; gap: 2px; padding: 4px; font-size: .75rem; justify-content: center; }
  .view { padding: 12px; padding-block-end: calc(var(--nav-h) + 24px); }
}

.card { display: grid; gap: 12px; padding: 16px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.card > * { min-inline-size: 0; }
.card-head, .page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; }
.card.empty { justify-items: start; padding: 28px 20px; }
.big { font-size: clamp(1.7rem, 7vw, 2.4rem); font-weight: 800; line-height: 1.1; white-space: normal; }
.month-nav { display: flex; gap: 4px; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 12px; }
.stats > div { display: grid; gap: 2px; padding: 10px 12px; background: var(--surface-2); border-radius: 10px; }
.stats strong { font-size: 1.1rem; }
.bars { display: grid; gap: 10px; }
.bar-top { display: flex; justify-content: space-between; gap: 12px; font-size: .95rem; }
.bar-track { block-size: 6px; margin-block-start: 4px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar-fill { block-size: 100%; background: var(--accent); border-radius: 3px; }

.list { list-style: none; margin: 0; padding: 0; }
.list > li + li { border-block-start: 1px solid var(--border); }
.list-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  inline-size: 100%; min-block-size: 48px;
  padding: 8px 4px;
  background: none; border: 0;
  color: inherit; text-align: start; text-decoration: none; cursor: pointer;
}
.list-row:hover { background: var(--surface-2); }
.list-main { display: grid; min-inline-size: 0; }
.list.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-block-start: 8px; }
.list.chips > li + li { border: 0; }
.chip { min-block-size: var(--target); padding: 4px 12px; background: var(--surface-2); border: 1px solid transparent; border-radius: 999px; cursor: pointer; }
.chip:hover { border-color: var(--accent); }
.chip.add { background: none; border: 1px dashed var(--border); color: var(--accent); font-weight: 600; }
details summary { min-block-size: var(--target); display: flex; align-items: center; cursor: pointer; color: var(--accent); font-weight: 600; }

.notice { padding: 10px 14px; background: var(--accent-soft); border-radius: 10px; }
.notice.warn { background: var(--warn-bg); color: var(--warn-text); }
.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: .8rem; font-weight: 600; white-space: nowrap; }
.badge.warn { background: var(--warn-bg); color: var(--warn-text); }
.badge.ok { background: var(--ok-bg); color: var(--pos); }

/* ---------- таблица операций ---------- */
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 8px; align-items: center; }
.filters .f-search { grid-column: 1 / -1; }
.table-wrap { overflow: auto; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.table-wrap.ops { container: ops / inline-size; }
.table { inline-size: 100%; border-collapse: collapse; }
.table th { padding: 10px 12px; font-size: .8rem; font-weight: 600; color: var(--muted); text-align: start; white-space: nowrap; border-block-end: 1px solid var(--border); }
.table td { padding: 8px 12px; border-block-end: 1px solid var(--border); vertical-align: middle; }
.table th.num, .table td.num { text-align: end; }
.table tbody tr:last-child td { border-block-end: 0; }
.ops-table tbody tr { cursor: pointer; }
.ops-table tbody tr:hover { background: var(--surface-2); }
.table tr.selected { background: var(--accent-soft); }
.table tr.dup { background: var(--warn-bg); }
.c-check { inline-size: 1%; padding-inline-end: 0 !important; }
.check-cell { display: grid; place-items: center; min-inline-size: 32px; min-block-size: 32px; cursor: pointer; }
.c-amount { font-weight: 600; }
.c-desc { color: var(--muted); max-inline-size: 360px; }
.row-open { padding: 0; background: none; border: 0; font-weight: 600; text-align: start; cursor: pointer; }
.preview-table { min-inline-size: 640px; font-size: .92rem; }
.import-preview .table-wrap { max-block-size: 45vh; box-shadow: none; border: 1px solid var(--border); }
.import-preview .table thead th { position: sticky; inset-block-start: 0; background: var(--surface); }

/* На узком экране строка таблицы превращается в карточку. */
@container ops (width < 640px) {
  .ops-table, .ops-table tbody { display: block; }
  .ops-table thead { display: none; }
  .ops-table tr {
    display: grid;
    grid-template-columns: auto auto 1fr auto;
    grid-template-areas: 'check cat cat amount' 'check desc desc desc' 'check date account account';
    gap: 0 8px;
    align-items: baseline;
    padding: 10px 12px 10px 4px;
    border-block-end: 1px solid var(--border);
  }
  .ops-table tr:last-child { border-block-end: 0; }
  .ops-table td { display: block; padding: 0; border: 0; }
  .ops-table td:empty { display: none; }
  .ops-table .c-check { grid-area: check; inline-size: auto; align-self: center; }
  .ops-table .c-cat { grid-area: cat; }
  .ops-table .c-amount { grid-area: amount; white-space: normal; text-align: end; }
  .ops-table .c-desc { grid-area: desc; max-inline-size: none; font-size: .9rem; }
  .ops-table .c-date { grid-area: date; }
  .ops-table .c-account { grid-area: account; }
  .ops-table .c-date, .ops-table .c-account { font-size: .8rem; color: var(--muted); }
  .check-cell { min-inline-size: 44px; min-block-size: 44px; }
}

.bulkbar {
  position: sticky; inset-block-end: calc(var(--nav-h) + 8px); z-index: 4;
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- диалоги ---------- */
dialog.dlg {
  inline-size: min(480px, calc(100vw - 24px));
  max-block-size: calc(100dvh - 24px);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  border: 0;
  border-radius: var(--radius);
  box-shadow: 0 16px 48px rgb(0 0 0 / 30%);
}
dialog.dlg.wide { inline-size: min(860px, calc(100vw - 24px)); }
dialog:modal { overscroll-behavior: contain; }
dialog::backdrop { background: rgb(10 16 22 / 55%); overflow: hidden; overscroll-behavior: contain; }
.dlg-head {
  position: sticky; inset-block-start: 0; z-index: 1;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 12px 8px 16px;
  background: var(--surface);
}
.dlg-head h1 { font-size: 1.15rem; }
.dlg-body { display: grid; gap: 12px; padding: 4px 16px 16px; }
.dlg-body > * { min-inline-size: 0; }
.dlg-body h2 { margin-block-start: 4px; }
.dlg-foot { display: flex; flex-flow: row-reverse wrap; gap: 8px; margin-block-start: 4px; }
.rate-line { padding: 8px 12px; background: var(--surface-2); border-radius: 10px; font-size: .9rem; font-variant-numeric: tabular-nums; }
@media (max-width: 600px) {
  dialog.dlg, dialog.dlg.wide {
    inline-size: 100vw; max-inline-size: 100vw;
    margin-block-end: 0;
    max-block-size: calc(100dvh - 16px);
    border-radius: var(--radius) var(--radius) 0 0;
  }
  .dlg-body { padding-block-end: max(16px, env(safe-area-inset-bottom, 0px)); }
  .dlg-foot .btn { flex: 1 1 auto; }
  .dlg-foot .spacer { display: none; }
}

.import-mapping, .import-preview { display: grid; gap: 10px; }
.import-mapping > *, .import-preview > * { min-inline-size: 0; }
.pairs { display: grid; gap: 12px; }
.pair { display: grid; gap: 8px; padding: 12px; border: 1px solid var(--border); border-radius: 12px; }
.pair-top { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 4px 12px; }
.pair-comment { font-size: .92rem; }
.pair-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

/* ---------- вход, уведомления ---------- */
.auth { min-block-size: 100dvh; display: grid; place-items: center; padding: 16px; }
.auth-card { display: grid; gap: 14px; inline-size: min(380px, 100%); padding: 24px; background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); }
.form-error { color: var(--neg); font-weight: 600; min-block-size: 1.4em; }
#toasts {
  position: fixed; inset-inline: 0; inset-block-start: max(12px, env(safe-area-inset-top, 0px)); z-index: 2147483647;
  display: grid; justify-items: center; gap: 8px;
  padding-inline: 12px;
  pointer-events: none;
}
.toast { max-inline-size: 480px; padding: 10px 16px; background: var(--text); color: var(--bg); border-radius: 10px; box-shadow: var(--shadow); font-weight: 600; }
.toast.error { background: var(--neg); color: #fff; }
@media (prefers-color-scheme: dark) { .toast.error { color: #2b0906; } }
