/* Кольори — з теми Telegram (--tg-theme-*, їх ставить telegram-web-app.js);
   запасні значення — для світлої й темної теми поза Telegram. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f1f2f6);
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --text: var(--tg-theme-text-color, #16181d);
  --hint: var(--tg-theme-hint-color, #8a8f98);
  --accent: var(--tg-theme-accent-text-color, var(--tg-theme-link-color, #2481cc));
  --button: var(--tg-theme-button-color, #2481cc);
  --button-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --warn: #f5a524;
  --ok: #30a46c;
  --line: color-mix(in srgb, var(--hint) 28%, transparent);
  --track: color-mix(in srgb, var(--hint) 22%, transparent);
  --radius: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: var(--tg-theme-bg-color, #17191d);
    --bg2: var(--tg-theme-secondary-bg-color, #0f1013);
    --text: var(--tg-theme-text-color, #eceef2);
    --hint: var(--tg-theme-hint-color, #8b9099);
  }
}
:root[data-theme="dark"] { --warn: #ffb224; --ok: #3dd68c; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg2);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-variant-numeric: tabular-nums;
  padding-bottom: calc(24px + env(safe-area-inset-bottom));
  overflow-x: hidden;
}
button { font: inherit; color: inherit; }
h2 { font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--hint); margin: 0 0 10px; }
[hidden] { display: none !important; }

/* ─── Верх: вкладки й місяці ─────────────────────────────────────────────── */
.top { position: sticky; top: 0; z-index: 5; background: var(--bg2); padding: 10px 16px 6px; }
.tabs { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); background: var(--track); border-radius: 10px; padding: 2px; }
.tab { border: 0; background: transparent; padding: 7px 0; border-radius: 8px; font-weight: 600; font-size: 12px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tab[aria-selected="true"] { background: var(--section); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.months { display: flex; gap: 6px; margin-top: 8px; overflow-x: auto; scrollbar-width: none; }
.months:empty { display: none; }
.top-total { display: flex; justify-content: space-between; align-items: baseline; margin-top: 8px; padding: 6px 2px 2px; border-top: 1px solid var(--line); font-size: 14px; }
.top-total b { font-size: 17px; }
.month { border: 1px solid var(--line); background: var(--section); border-radius: 999px; padding: 4px 12px; font-size: 13px; white-space: nowrap; cursor: pointer; }
.month[aria-pressed="true"] { background: var(--button); color: var(--button-text); border-color: var(--button); }

main { padding: 0 16px; }
.card { background: var(--section); border-radius: var(--radius); padding: 14px; margin: 10px 0; }
.hint, .footnote { color: var(--hint); font-size: 13px; margin: 0 0 8px; }
.footnote { margin: 12px 4px; }

/* ─── Підсумок плану ────────────────────────────────────────────────────── */
.summary .label { color: var(--hint); font-size: 13px; }
.summary .total { font-size: 30px; font-weight: 700; letter-spacing: -.01em; line-height: 1.15; }
.summary .delta { font-size: 14px; font-weight: 600; margin-left: 6px; }
.up { color: var(--danger); }
.down { color: var(--ok); }
.compare { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
.compare div { background: var(--bg2); border-radius: 10px; padding: 7px 10px; min-width: 0; }
.compare b { display: block; font-size: 15px; }
.compare span { color: var(--hint); font-size: 12px; }
.money-line { margin-top: 10px; font-size: 14px; }
.money-line b { white-space: nowrap; }
.arrow { color: var(--hint); margin: 0 4px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 6px; }
.chip { border: 1px solid var(--line); background: var(--section); border-radius: 999px; padding: 5px 11px; font-size: 13px; cursor: pointer; }
.chip:active { opacity: .7; }
.chip.accent { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }

.warning { background: color-mix(in srgb, var(--warn) 16%, var(--section)); border-radius: 10px; padding: 8px 12px; margin: 6px 0; font-size: 13px; }

/* ─── Категорії плану ───────────────────────────────────────────────────── */
.group { margin: 14px 0 4px; }
.group-head { display: flex; justify-content: space-between; align-items: baseline; padding: 0 4px 6px; }
.group-head h2 { margin: 0; }
.group-head span { color: var(--hint); font-size: 13px; }
.group-body { background: var(--section); border-radius: var(--radius); overflow: hidden; }
.cat { padding: 12px 14px 10px; border-top: 1px solid var(--line); }
.cat:first-child { border-top: 0; }
.cat.changed { background: color-mix(in srgb, var(--accent) 7%, var(--section)); }
.cat-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; }
.cat-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; border: 0; background: none; padding: 0; text-align: left; cursor: pointer; }
.cat-name::after { content: " ›"; color: var(--hint); font-weight: 400; }
.cat.open .cat-name::after { content: " ⌄"; }
.amount { border: 0; background: none; padding: 2px 0; font-weight: 700; font-size: 17px; cursor: text; white-space: nowrap; border-bottom: 1px dashed var(--line); }
.amount input { width: 7.5em; font: inherit; text-align: right; border: 1px solid var(--accent); border-radius: 8px; padding: 2px 6px; background: var(--bg); color: var(--text); }
.was { color: var(--hint); font-size: 12px; font-weight: 400; margin-right: 6px; text-decoration: line-through; }
.no-plan { color: var(--hint); font-size: 13px; font-weight: 500; }

input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; margin: 4px 0 0; touch-action: pan-y; }
input[type="range"]::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: linear-gradient(to right, var(--button) var(--fill, 0%), var(--track) var(--fill, 0%)); }
input[type="range"]::-moz-range-track { height: 6px; border-radius: 3px; background: var(--track); }
input[type="range"]::-moz-range-progress { height: 6px; border-radius: 3px; background: var(--button); }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 24px; height: 24px; border-radius: 50%; background: #fff; border: 0; box-shadow: 0 1px 4px rgba(0,0,0,.35); margin-top: -9px; }
input[type="range"]::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: #fff; border: 0; box-shadow: 0 1px 4px rgba(0,0,0,.35); }
.scale { position: relative; height: 12px; margin: -2px 12px 0; }
.tick { position: absolute; top: 0; width: 2px; height: 8px; border-radius: 1px; transform: translateX(-1px); }
.tick.spent { background: var(--danger); }
.tick.sugg { background: var(--accent); }
.tick.prev { background: var(--hint); }

.facts { display: flex; gap: 10px; flex-wrap: wrap; font-size: 12px; color: var(--hint); margin-top: 2px; }
.facts b { color: var(--text); font-weight: 600; }
.facts .over { color: var(--danger); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.actions .chip { font-size: 12px; padding: 4px 9px; }

.details { margin-top: 10px; }
.basis { font-size: 12px; color: var(--hint); margin: 6px 0 0; }
.history svg { width: 100%; height: 86px; display: block; }
.history text { font-size: 10px; fill: var(--hint); }

.save-fallback { display: block; width: 100%; margin: 14px 0; border: 0; border-radius: 12px; padding: 13px; background: var(--button); color: var(--button-text); font-weight: 600; font-size: 16px; }

/* ─── Бюджет ────────────────────────────────────────────────────────────── */
.kpis { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin-top: 10px; }
.kpis div { background: var(--bg2); border-radius: 10px; padding: 8px 10px; min-width: 0; }
.kpis b { display: block; font-size: 16px; }
.kpis span { color: var(--hint); font-size: 12px; }
.progress { position: relative; height: 10px; border-radius: 5px; background: var(--track); margin-top: 10px; overflow: visible; }
.progress .fill { height: 100%; border-radius: 5px; background: var(--button); max-width: 100%; }
.progress .fill.over { background: var(--danger); }
.progress .today { position: absolute; top: -4px; width: 2px; height: 18px; background: var(--text); border-radius: 1px; opacity: .6; }

.chart svg { width: 100%; display: block; overflow: visible; }
.chart text { font-size: 10px; fill: var(--hint); }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .fact { fill: none; stroke: var(--button); stroke-width: 2.5; stroke-linejoin: round; stroke-linecap: round; }
.chart .even { fill: none; stroke: var(--hint); stroke-width: 1.5; stroke-dasharray: 5 4; }
.chart .proj { fill: none; stroke: var(--warn); stroke-width: 2; stroke-dasharray: 3 4; }
.chart .dot { fill: var(--button); stroke: var(--section); stroke-width: 2; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12px; color: var(--hint); margin-top: 8px; }
.sw { display: inline-block; width: 14px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 5px; }
.sw-fact { background: var(--button); }
.sw-even { background: var(--hint); }
.sw-proj { background: var(--warn); }

.bar { padding: 9px 0; border-top: 1px solid var(--line); }
.bar:first-child { border-top: 0; padding-top: 0; }
.bar-head { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; }
.bar-head .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.bar-head .nums { white-space: nowrap; }
.bar-head .nums span { color: var(--hint); }
.bar .progress { height: 8px; margin-top: 6px; }
.bar .progress .today { height: 14px; top: -3px; }
.bar .status { font-size: 12px; color: var(--hint); margin-top: 4px; }
.code-over .fill { background: var(--danger); }
.code-over .status { color: var(--danger); }
.code-pace .fill { background: var(--warn); }
.code-pace .status { color: color-mix(in srgb, var(--warn) 75%, var(--text)); }
.code-paid .fill { background: var(--ok); }
.code-noplan .fill { background: var(--hint); }

.flows { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 14px; }
.flows .sub { color: var(--hint); }

/* ─── Операції ─────────────────────────────────────────────────────────── */
.kpis-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.search { width: 100%; font: inherit; border: 1px solid var(--line); border-radius: 12px; padding: 9px 12px; background: var(--section); color: var(--text); margin: 4px 0 8px; }
.chip.on { background: var(--button); color: var(--button-text); border-color: var(--button); }
.chip:disabled { opacity: .5; }
.chip.more { display: block; margin: 12px auto; }
.tx { border-top: 1px solid var(--line); }
.tx:first-child { border-top: 0; }
.tx.open { background: color-mix(in srgb, var(--accent) 6%, var(--section)); }
.tx-main { display: flex; width: 100%; justify-content: space-between; align-items: center; gap: 10px; border: 0; background: none; padding: 10px 14px; text-align: left; cursor: pointer; }
.tx-text { display: flex; flex-direction: column; min-width: 0; }
.tx-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-cat { font-size: 12px; color: var(--hint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx-cat.other { color: color-mix(in srgb, var(--warn) 80%, var(--text)); }
.tx-hint { color: var(--accent); }
.tx-sum { font-weight: 600; white-space: nowrap; }
.tx-sum.in { color: var(--ok); }
.picker { padding: 0 14px 12px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips .chip { font-size: 12px; padding: 5px 10px; }

/* ─── Цілі й капітал ───────────────────────────────────────────────────── */
.goals { display: flex; flex-direction: column; gap: 10px; margin: 10px 0; }
.goal { background: var(--section); border-radius: var(--radius); padding: 14px; }
.goal.reached { box-shadow: inset 0 0 0 2px color-mix(in srgb, var(--ok) 55%, transparent); }
.goal-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.goal-name { font-weight: 700; font-size: 16px; }
.badge { font-size: 11px; border-radius: 999px; padding: 2px 8px; background: var(--bg2); color: var(--hint); }
.badge.warn { background: color-mix(in srgb, var(--danger) 15%, var(--section)); color: var(--danger); }
.goal-nums { margin-top: 6px; font-size: 15px; }
.goal-lines { font-size: 13px; color: var(--hint); margin-top: 8px; display: flex; flex-direction: column; gap: 2px; }
.goal-chart { margin-top: 10px; }
.move-form { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.move-row { display: flex; gap: 8px; margin-top: 8px; }
.move-row input { flex: 1; min-width: 0; font: inherit; border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: var(--bg); color: var(--text); }
.btn { border: 0; border-radius: 10px; padding: 8px 16px; background: var(--button); color: var(--button-text); font-weight: 600; cursor: pointer; }
.btn:disabled { opacity: .6; }
.nw-group { border-top: 1px solid var(--line); }
.nw-group:first-child { border-top: 0; }
.nw-head { display: flex; width: 100%; justify-content: space-between; border: 0; background: none; padding: 9px 0; font-weight: 600; cursor: pointer; text-align: left; }
.nw-head span::after { content: " ›"; color: var(--hint); font-weight: 400; }
.nw-group.open .nw-head span::after { content: " ⌄"; }
.nw-items { padding: 0 0 8px; }
.nw-item { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; padding: 3px 0; }
.nw-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.nw-item .sub, .flows .sub { color: var(--hint); }
.fr-line { font-size: 14px; }

/* ─── Аналітика ─────────────────────────────────────────────────────────── */
.trend-lines { font-size: 13px; margin-top: 8px; display: flex; flex-direction: column; gap: 3px; }
.trend-changes { font-size: 13px; display: flex; flex-direction: column; gap: 3px; }
.sub-row { padding: 7px 0; border-top: 1px solid var(--line); }
.sub-row:first-of-type { border-top: 0; }
.sub-row .status { font-size: 12px; color: var(--hint); margin-top: 2px; }

/* ─── Стан, тост ────────────────────────────────────────────────────────── */
.state { text-align: center; color: var(--hint); padding: 40px 12px; }
.state:empty { display: none; }
.state .big { font-size: 34px; margin-bottom: 8px; }
.skeleton { height: 64px; border-radius: var(--radius); margin: 10px 0; background: linear-gradient(90deg, var(--section) 25%, var(--bg2) 50%, var(--section) 75%); background-size: 200% 100%; animation: shimmer 1.2s infinite linear; }
@keyframes shimmer { to { background-position: -200% 0; } }
.toast { position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 10; background: var(--text); color: var(--bg); border-radius: 12px; padding: 11px 14px; font-size: 14px; text-align: center; box-shadow: 0 6px 20px rgba(0,0,0,.25); }

@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } }
