/* ---------- tokens ----------
   Sunny theme. Light is the default; dark applies when <html data-theme="dark">. js/theme.js sets that
   attribute before the first paint from the user's choice (Системная / Светлая / Тёмная) and follows the
   OS setting (iOS and Windows both report it via prefers-color-scheme) while «Системная» is selected.
   All colours were checked for text contrast; chart slots were validated against both card surfaces. */
:root {
  --bg: #fdf8ea;            /* warm cream page */
  --surface: #fffdf6;       /* cards */
  --surface-2: #f8efd6;     /* tiles, tracks */
  --text: #2b2210;
  --muted: #76653f;
  --border: #ecdfba;
  --accent: #f5b301;        /* sunny yellow: fills, primary buttons */
  --accent-text: #2b2000;   /* text on yellow */
  --accent-soft: #fdeeb5;
  --accent-strong: #8a5a00; /* yellow can't be text on cream: links, active tab, chips use this */
  --chart-accent: #c47f00;  /* the "spent" line: ≥ 3:1 on the card */
  --hero-grad: linear-gradient(135deg, #ffe58a 0%, #ffd24a 55%, #ffbf3c 100%);
  --hero-text: #2b2000;
  --ok: #15803d;
  --warn: #b45309;
  --warn-soft: #fdebd3;
  --danger: #c2410c;
  --danger-soft: #fde2d4;
  --radius: 16px;
  --shadow: 0 1px 2px rgb(90 60 0 / 6%), 0 4px 14px rgb(120 80 0 / 6%);
  --nav-w: 210px;
  /* chart palette: 8 categorical slots in a fixed, colour-blind-checked order. Categories own a slot;
     everything else is drawn as grey «Другое». */
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --s-other: #c4bba3;
  --plan-line: #9a8f74;
  --grid: #f1e7cc;
  --axis: #d9cba3;
  --tick: #8a7a55;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #15120a;
  --surface: #1f1a0f;
  --surface-2: #2a2415;
  --text: #f5ecd6;
  --muted: #b3a47e;
  --border: #3a321d;
  --accent: #fcc419;
  --accent-text: #241a00;
  --accent-soft: #3a2f10;
  --accent-strong: #fcd34d;
  --chart-accent: #fcc419;
  --hero-grad: linear-gradient(135deg, #3b2f0d 0%, #4a3a0c 60%, #5a440a 100%);
  --hero-text: #fff3cf;
  --ok: #4ade80;
  --warn: #fbbf24;
  --warn-soft: #3a2d12;
  --danger: #fb8a5b;
  --danger-soft: #3d2217;
  --shadow: none;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #0ca30c; --s7: #9085e9; --s8: #e66767;
  --s-other: #5f5745;
  --plan-line: #8f8466;
  --grid: #2c2616;
  --axis: #463c22;
  --tick: #a39570;
  color-scheme: dark;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
h1 { font-size: 22px; margin: 4px 0 16px; }
h2 { font-size: 17px; margin: 0 0 12px; }
a { color: var(--accent-strong); }
button, input, select { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 14px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--ok); }
.neg { color: var(--danger); }
.hidden { display: none !important; }

/* ---------- layout: bottom tabs in portrait, side rail on wide screens / landscape ---------- */
.shell { min-height: 100dvh; }
main {
  padding: calc(12px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right))
           calc(88px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  max-width: 1180px;
}
nav.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; justify-content: space-around;
  background: var(--surface); border-top: 1px solid var(--border);
  padding: 6px env(safe-area-inset-right) calc(6px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
}
nav.tabs a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 11px; color: var(--muted); text-decoration: none; padding: 4px 0;
}
nav.tabs a svg { width: 24px; height: 24px; }
nav.tabs a.active { color: var(--accent-strong); }
nav.tabs .brand { display: none; }

@media (min-width: 720px) {
  /* minmax(0, 1fr): the content column may shrink below its content's width instead of overflowing */
  .shell { display: grid; grid-template-columns: calc(var(--nav-w) + env(safe-area-inset-left)) minmax(0, 1fr); }
  nav.tabs {
    position: sticky; top: 0; height: 100dvh; flex-direction: column; justify-content: flex-start; gap: 2px;
    border-top: 0; border-right: 1px solid var(--border);
    padding: calc(16px + env(safe-area-inset-top)) 10px 16px calc(10px + env(safe-area-inset-left));
    overflow-y: auto;
  }
  nav.tabs .brand { display: block; font-weight: 700; font-size: 18px; padding: 0 10px 14px; }
  nav.tabs a { flex: none; flex-direction: row; gap: 10px; font-size: 15px; padding: 9px 10px; border-radius: 10px; }
  nav.tabs a svg { width: 20px; height: 20px; }
  nav.tabs a.active { background: var(--accent-soft); }
  main { padding: calc(20px + env(safe-area-inset-top)) calc(24px + env(safe-area-inset-right))
                  calc(24px + env(safe-area-inset-bottom)) 24px; }
}
/* phone in landscape: little height — narrower, tighter rail */
@media (min-width: 720px) and (max-height: 500px) {
  :root { --nav-w: 170px; }
  nav.tabs { padding-top: calc(8px + env(safe-area-inset-top)); }
  nav.tabs .brand { padding-bottom: 6px; }
  nav.tabs a { padding: 6px 10px; }
}

.cols { display: grid; gap: 16px; align-items: start; }
@media (min-width: 720px) { .cols { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); } }
.stack { display: grid; gap: 16px; align-content: start; grid-template-columns: minmax(0, 1fr); }
.cols > *, .stack > * { min-width: 0; }   /* a wide chart or long name must never push the page sideways */

/* ---------- components ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); }
.card.tight { padding: 4px 0; }
.hero .label { color: var(--muted); font-size: 14px; }
.hero .big { font-size: 44px; font-weight: 700; line-height: 1.1; margin: 4px 0 6px; letter-spacing: -0.5px; }
.hero .sub { color: var(--muted); }
@media (max-height: 500px) { .hero .big { font-size: 36px; } }

.stats { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-top: 14px; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat .v { font-weight: 600; }
.stat .l { color: var(--muted); font-size: 13px; }

.bar { height: 8px; border-radius: 4px; background: var(--surface-2); overflow: hidden; }
.bar > i { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.bar.over > i { background: var(--danger); }
.bar.warn > i { background: var(--warn); }

.banner { border-radius: var(--radius); padding: 12px 14px; background: var(--warn-soft); color: var(--text); display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.banner.info { background: var(--accent-soft); }

.row { display: flex; align-items: center; gap: 12px; padding: 10px 16px; min-height: 52px; }
.row + .row { border-top: 1px solid var(--border); }
.row .grow { flex: 1; min-width: 0; }
.row .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .meta { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .ico { width: 34px; height: 34px; border-radius: 10px; background: var(--surface-2); display: grid; place-items: center; flex: none; font-size: 18px; }
button.row { width: 100%; border: 0; background: none; text-align: left; cursor: pointer; }
button.row:active { background: var(--surface-2); }
.day-head { padding: 14px 4px 6px; color: var(--muted); font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .3px; display: flex; justify-content: space-between; }

.cat-row { padding: 8px 0; }
a.cat-row { display: block; color: inherit; text-decoration: none; }
a.cat-row + a.cat-row { border-top: 1px solid var(--border); }
.cat-name { display: inline-flex; align-items: center; gap: 6px; min-width: 0; }
.cat-row .top { display: flex; justify-content: space-between; gap: 8px; margin-bottom: 6px; }

.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 12px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.chip.ok { background: var(--accent-soft); color: var(--accent-strong); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.bad { background: var(--danger-soft); color: var(--danger); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 9px 14px; cursor: pointer; text-decoration: none; color: var(--text); min-height: 40px; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.btn.danger { color: var(--danger); }
.btn.small { padding: 5px 10px; min-height: 32px; font-size: 14px; }
.btn:disabled { opacity: .5; }
.btns { display: flex; gap: 8px; flex-wrap: wrap; }

label.field { display: grid; gap: 4px; font-size: 14px; color: var(--muted); }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=search], input[inputmode], select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); min-height: 44px;
}
input:focus, select:focus, .btn:focus-visible { outline: 2px solid var(--accent-strong); outline-offset: 1px; }
.form { display: grid; gap: 12px; }
.form .two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 15px; }

.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar .grow { flex: 1; min-width: 160px; }
.period-switch { display: flex; align-items: center; gap: 6px; }
.presets { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.presets button { border: 1px solid var(--border); background: var(--surface); color: var(--text); border-radius: 999px; padding: 6px 12px; cursor: pointer; font-size: 14px; white-space: nowrap; }
.presets button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.date-range { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.date-range label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; }
.date-range input[type=date] { width: auto; min-width: 150px; }

.drop { border: 2px dashed var(--border); border-radius: var(--radius); padding: 28px 16px; text-align: center; background: var(--surface); }
.drop.over { border-color: var(--accent-strong); background: var(--accent-soft); }
.counts { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0 12px; }

table.plan { width: 100%; border-collapse: collapse; }
table.plan th, table.plan td { padding: 8px 6px; border-top: 1px solid var(--border); text-align: right; }
table.plan th:first-child, table.plan td:first-child { text-align: left; }
table.plan thead th { border-top: 0; color: var(--muted); font-weight: 500; font-size: 13px; }
table.plan input { max-width: 120px; text-align: right; min-height: 36px; padding: 6px 8px; }
@media (max-width: 719px) { table.plan .wide { display: none; } }

/* dialog: bottom sheet on phones, centered on wide screens */
dialog.sheet { border: 0; padding: 0; background: var(--surface); color: var(--text); width: 100%; max-width: 560px; border-radius: 18px 18px 0 0; margin: auto auto 0; max-height: 90dvh; }
dialog.sheet::backdrop { background: rgb(0 0 0 / 40%); }
dialog.sheet .sheet-body { padding: 18px calc(18px + env(safe-area-inset-right)) calc(18px + env(safe-area-inset-bottom)) calc(18px + env(safe-area-inset-left)); overflow-y: auto; max-height: 90dvh; }
@media (min-width: 720px), (max-height: 500px) { dialog.sheet { border-radius: 18px; margin: auto; } }

.toast { position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 100; max-width: calc(100% - 32px); }
@media (min-width: 720px) { .toast { bottom: 24px; } }

.center-page { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.center-page .card { width: 100%; max-width: 380px; }
.empty { padding: 24px 16px; text-align: center; color: var(--muted); }
.spinner { width: 22px; height: 22px; border: 3px solid var(--surface-2); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; display: inline-block; vertical-align: middle; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- sunny hero card ---------- */
.card.hero { background: var(--hero-grad); color: var(--hero-text); border-color: transparent; }
.card.hero .label, .card.hero .sub, .card.hero .muted { color: inherit; opacity: 0.78; }
.card.hero .stat { background: rgb(255 255 255 / 42%); }
.card.hero .stat .l { color: inherit; opacity: 0.75; }
.card.hero .bar { background: rgb(255 255 255 / 45%); }
.card.hero .bar > i { background: var(--hero-text); }
.card.hero .bar.warn > i { background: var(--warn); }
.card.hero .bar.over > i { background: var(--danger); }
/* dark: a "sun at night" — the card stays dark, a warm glow rises from the top-right corner,
   a thin golden edge, and the headline number itself is the sunny yellow */
:root[data-theme="dark"] .card.hero {
  background:
    radial-gradient(120% 130% at 100% 0%, rgb(252 196 25 / 30%) 0%, rgb(245 140 20 / 12%) 38%, transparent 68%),
    linear-gradient(160deg, #262011 0%, #1c170c 100%);
  border-color: rgb(252 196 25 / 32%);
  box-shadow: 0 0 0 1px rgb(252 196 25 / 6%), 0 10px 30px rgb(252 160 20 / 8%);
}
:root[data-theme="dark"] .card.hero .big { color: var(--accent); }
:root[data-theme="dark"] .card.hero .big.neg { color: var(--danger); }
:root[data-theme="dark"] .card.hero .stat { background: rgb(255 240 200 / 6%); border: 1px solid rgb(252 196 25 / 10%); }
:root[data-theme="dark"] .card.hero .bar { background: rgb(255 240 200 / 10%); }
:root[data-theme="dark"] .card.hero .bar > i { background: linear-gradient(90deg, #f59f0b, #fcc419); }
.card.hero .big.neg { color: var(--danger); }
nav.tabs .brand::before { content: "☀️ "; }

.card.tight + .card { margin-top: 16px; }

/* ---------- theme switch ---------- */
.theme-switch { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.theme-switch button { border: 0; background: none; border-radius: 8px; padding: 6px 10px; cursor: pointer; color: var(--muted); font-size: 14px; white-space: nowrap; }
.theme-switch button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
nav.tabs .rail-foot { display: none; }
@media (min-width: 720px) {
  nav.tabs .rail-foot { display: block; margin-top: auto; padding: 12px 4px 0; }
  nav.tabs .rail-foot .theme-switch button { padding: 6px 8px; }
}

/* ---------- donut ---------- */
.donut-layout { display: grid; gap: 16px; justify-items: center; container-type: inline-size; }
.donut-layout > .donut-list { width: 100%; }
@container (min-width: 440px) {
  .donut-layout > .donut-inner { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 20px; align-items: center; width: 100%; }
}
.donut-inner { width: 100%; display: grid; gap: 14px; justify-items: center; }
.donut-wrap { position: relative; }
.donut-wrap svg { display: block; }
.donut-wrap .seg { cursor: pointer; outline: none; transition: opacity .15s; }
.donut-wrap.hovering .seg:not(.hot) { opacity: 0.35; }
.donut-center { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; }
.donut-center .v { font-size: 22px; font-weight: 700; }
.donut-center .l { color: var(--muted); font-size: 12px; }
.share { color: var(--muted); font-size: 13px; margin-left: 6px; }

/* ---------- navigation variants: 5 tabs at the bottom, full list in the side rail ---------- */
nav.tabs a.rail-only { display: none; }
@media (min-width: 720px) {
  nav.tabs a.rail-only { display: flex; }
  nav.tabs a.bottom-only { display: none; }
}

/* ---------- charts ---------- */
.chart-card { overflow: hidden; }
.chart-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; margin-bottom: 10px; flex-wrap: wrap; }
.segmented button { white-space: nowrap; }
.chart-head h2 { margin-bottom: 2px; }
.chart-body { width: 100%; min-height: 40px; }
.chart-wrap { position: relative; width: 100%; touch-action: pan-y; }
.chart-wrap svg { display: block; overflow: visible; }
.chart-wrap .grid { stroke: var(--grid); stroke-width: 1; }
.chart-wrap .axis { stroke: var(--axis); stroke-width: 1; }
.chart-wrap .crosshair { stroke: var(--axis); stroke-width: 1; }
.chart-wrap .tick { fill: var(--tick); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart-wrap .cap-label, .chart-wrap .end-label { fill: var(--muted); font-size: 11px; font-weight: 600; }
.chart-wrap .end-label { fill: var(--text); font-size: 12px; }
.chart-wrap .ring { stroke: var(--surface); stroke-width: 2; }
.chart-wrap .mark { cursor: pointer; outline: none; }
.chart-wrap .mark:focus { stroke: var(--text); stroke-width: 1.5; }
.chart-wrap .hidden { display: none; }
.tooltip { position: absolute; z-index: 5; pointer-events: none; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; box-shadow: 0 4px 16px rgb(0 0 0 / 14%); font-size: 13px; min-width: 150px; max-width: 240px; }
.tip-title { font-weight: 600; margin-bottom: 4px; }
.tip-row { display: flex; align-items: center; gap: 6px; }
.tip-row .grow { flex: 1; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; font-size: 13px; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: 6px; }
.swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; display: inline-block; }
.key-line { width: 14px; height: 2px; border-radius: 1px; flex: none; display: inline-block; }

.segbar { display: flex; gap: 2px; height: 8px; border-radius: 4px; overflow: hidden; margin: 4px 0 12px; }
.segbar i { display: block; min-width: 2px; }

.ranked { display: grid; gap: 10px; }
.ranked-row { display: grid; grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.1fr); gap: 10px; align-items: center; }
.ranked-label { display: grid; min-width: 0; }
.ranked-label .title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ranked-track { display: flex; align-items: center; gap: 8px; min-width: 0; }
.ranked-track i { display: block; height: 10px; border-radius: 0 4px 4px 0; flex: none; max-width: calc(100% - 76px); }
@media (max-width: 480px) { .ranked-row { grid-template-columns: 1fr; gap: 4px; } }

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
@media (min-width: 960px) { .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
.tile .v { font-size: 22px; font-weight: 650; }
.tile .l { color: var(--muted); font-size: 13px; }
.tile .d { font-size: 13px; margin-top: 2px; }

.table-wrap { overflow-x: auto; }
.btn.ghost { border-color: transparent; background: var(--surface-2); }

/* ---------- segmented control, category rows, colour picker ---------- */
.segmented { display: inline-flex; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.segmented a, .segmented button { border: 0; background: none; padding: 6px 14px; border-radius: 8px; color: var(--muted); text-decoration: none; cursor: pointer; font-size: 14px; }
.segmented .on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.cat-dot { width: 12px; height: 12px; border-radius: 4px; flex: none; }
.colors { display: flex; flex-wrap: wrap; gap: 8px; }
.colors button { width: 34px; height: 34px; border-radius: 10px; border: 2px solid transparent; cursor: pointer; display: grid; place-items: center; color: #fff; font-size: 12px; }
.colors button.on { border-color: var(--text); }
.colors button.none { background: var(--surface-2); color: var(--muted); width: auto; padding: 0 10px; }
.emoji-row { display: flex; flex-wrap: wrap; gap: 6px; }
.emoji-row button { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; width: 38px; height: 38px; font-size: 18px; cursor: pointer; }
.quick-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: -6px 0 14px; }
.link-list a.row { color: inherit; text-decoration: none; }
.chev { color: var(--muted); }

/* ---------- quick cash page ---------- */
body.quick main { max-width: 520px; margin: 0 auto; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.amount-input { font-size: 40px; font-weight: 700; text-align: center; border: 0; border-bottom: 2px solid var(--accent); border-radius: 0; background: transparent; width: 100%; padding: 8px 0; color: var(--text); }
.amount-input:focus { outline: none; }
.cats { display: flex; flex-wrap: wrap; gap: 8px; }
.cats button { border: 1px solid var(--border); background: var(--surface); border-radius: 999px; padding: 8px 12px; cursor: pointer; }
.cats button.on { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
/* landscape phone: amount on the left, categories on the right so the keyboard does not hide the button */
@media (orientation: landscape) and (max-height: 500px) {
  body.quick main { max-width: 900px; }
  .quick-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; align-items: start; }
}
