/* Telegram passes its theme as --tg-theme-* variables; the fallbacks are for a plain browser. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --bg2: var(--tg-theme-secondary-bg-color, #f2f3f5);
  --text: var(--tg-theme-text-color, #1c1d1f);
  --muted: var(--tg-theme-hint-color, #8a8f98);
  --link: var(--tg-theme-link-color, #2481cc);
  --accent: var(--tg-theme-button-color, #2481cc);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --plus: #2f9e44;
  --minus: #d9480f;
  --line: color-mix(in srgb, var(--text) 10%, transparent);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-tg]) { --bg: #17181a; --bg2: #0f1011; --text: #ececec; --muted: #8b8f96; --plus: #51cf66; --minus: #ff8a4c; }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg2); color: var(--text);
  font: 15px/1.4 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif; -webkit-tap-highlight-color: transparent; }
body { padding-bottom: calc(64px + env(safe-area-inset-bottom)); }
button, input, select, textarea { font: inherit; color: inherit; }
h1 { font-size: 17px; margin: 0; font-weight: 600; text-align: center; flex: 1; }
h2 { font-size: 15px; margin: 0 0 10px; font-weight: 600; }
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }

.top { position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 8px;
  padding: 10px 12px; background: var(--bg); border-bottom: 1px solid var(--line); }
.icon { border: 0; background: var(--bg2); width: 36px; height: 36px; border-radius: 10px; font-size: 20px; cursor: pointer; }

main { padding: 12px; max-width: 720px; margin: 0 auto; }
.tab { display: none; }
.tab.active { display: block; }

.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-bottom: 10px; }
.kpi { background: var(--bg); border-radius: var(--radius); padding: 10px; min-width: 0; }
.kpi span { display: block; color: var(--muted); font-size: 12px; }
.kpi b { display: block; font-size: clamp(12px, 3.6vw, 16px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.plus { color: var(--plus); } .minus { color: var(--minus); }

.card { background: var(--bg); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; margin-bottom: 6px; }
.card-head h2 { margin: 0; }
.chart { width: 100%; height: 280px; }
.chart.small { height: 180px; }

.banner { width: 100%; text-align: left; border: 0; border-radius: var(--radius); padding: 10px 12px; margin-bottom: 10px;
  background: color-mix(in srgb, var(--accent) 14%, var(--bg)); color: var(--text); cursor: pointer; }

.dir-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.dir-head b { font-size: 16px; }
.dir-line { display: flex; gap: 12px; font-size: 13px; color: var(--muted); margin: 2px 0 8px; flex-wrap: wrap; }
.cat-line { display: flex; justify-content: space-between; gap: 8px; padding: 5px 0; border-top: 1px solid var(--line); font-size: 14px; }
.cat-line span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-line span:last-child { white-space: nowrap; }

.seg { display: inline-flex; background: var(--bg2); border-radius: 10px; padding: 2px; gap: 2px; flex-wrap: wrap; }
.seg.wide { display: flex; width: 100%; margin-bottom: 10px; }
.seg.wide button { flex: 1; }
.seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 8px; font-size: 13px; cursor: pointer; }
.seg button.on { background: var(--bg); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }

.quick { display: flex; gap: 8px; margin-bottom: 10px; }
.quick input { flex: 1; min-width: 0; }
input, select, textarea { width: 100%; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); }
.quick button, .primary { border: 0; border-radius: 10px; padding: 9px 16px; background: var(--accent); color: var(--accent-text); cursor: pointer; font-weight: 600; }
.danger { border: 0; border-radius: 10px; padding: 9px 16px; background: transparent; color: var(--danger); cursor: pointer; }
.row { display: flex; gap: 8px; align-items: center; }
.row > input { flex: 1; min-width: 0; }
.row > button:not(.primary):not(.danger) { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 9px 12px; cursor: pointer; }
.check { display: flex; align-items: center; gap: 6px; white-space: nowrap; font-size: 14px; }
.check input { width: auto; }
.filters { display: grid; gap: 8px; margin-bottom: 10px; }

.day { color: var(--muted); font-size: 13px; margin: 12px 4px 4px; display: flex; justify-content: space-between; }
.op { display: flex; gap: 10px; align-items: center; background: var(--bg); padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--line); }
.op:first-of-type { border-radius: var(--radius) var(--radius) 0 0; }
.ops-group { border-radius: var(--radius); overflow: hidden; }
.op .what { flex: 1; min-width: 0; }
.op .what b { display: block; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op .what span { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.op .sum { white-space: nowrap; font-weight: 600; }
.op.nocat .what b { color: var(--danger); }

.cat-group { margin-bottom: 10px; }
.cat-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); }
.cat-row .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.cat-row.archived .name { color: var(--muted); text-decoration: line-through; }
.cat-row.child .name { padding-left: 14px; }
.cat-row button { border: 0; background: var(--bg2); border-radius: 8px; padding: 4px 8px; cursor: pointer; font-size: 13px; }
.form { display: grid; gap: 8px; }

.tabs { position: fixed; bottom: 0; left: 0; right: 0; z-index: 6; display: flex; background: var(--bg);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom); }
.tabs button { flex: 1; border: 0; background: transparent; padding: 6px 0 8px; font-size: 18px; cursor: pointer; color: var(--muted); }
.tabs button span { display: block; font-size: 11px; }
.tabs button.active { color: var(--accent); }

.sheet-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 10; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; background: var(--bg); border-radius: 18px 18px 0 0;
  padding: 14px 16px calc(16px + env(safe-area-inset-bottom)); max-height: 92vh; overflow: auto; display: grid; gap: 10px;
  max-width: 720px; margin: 0 auto; }
.sheet label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.sheet label.check { display: flex; color: var(--text); }
.sheet-head { display: flex; justify-content: space-between; align-items: center; }
.sheet-head h2 { margin: 0; }
.sheet .row .primary { flex: 1; }

.toast { position: fixed; left: 50%; bottom: calc(76px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; max-width: 90vw; }
.gate { position: fixed; inset: 0; z-index: 30; background: var(--bg); display: grid; place-items: center; text-align: center; padding: 24px; }

/* plan and AI */
.add { border: 0; background: var(--bg2); border-radius: 8px; padding: 5px 10px; font-size: 13px; cursor: pointer; color: var(--link); }
.item { padding: 9px 0; border-top: 1px solid var(--line); cursor: pointer; }
.item:first-child { border-top: 0; }
.item-head { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.item-head b { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-head span { white-space: nowrap; font-size: 14px; }
.item .sub { color: var(--muted); font-size: 13px; margin-top: 3px; display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.bar { height: 6px; background: var(--bg2); border-radius: 3px; overflow: hidden; margin-top: 6px; }
.bar i { display: block; height: 100%; background: var(--plus); border-radius: 3px; }
.bar i.warn { background: #f08c00; } .bar i.over { background: var(--danger); }
.pay { border: 0; background: color-mix(in srgb, var(--accent) 14%, var(--bg)); color: var(--link); border-radius: 8px; padding: 3px 8px; font-size: 12px; cursor: pointer; }
.empty { color: var(--muted); font-size: 14px; padding: 4px 0; }
.form label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
#form-sheet { z-index: 12; }
.md { font-size: 14px; line-height: 1.5; overflow-wrap: anywhere; }
.md h1 { font-size: 17px; margin: 4px 0 8px; } .md h2 { font-size: 15px; margin: 14px 0 6px; } .md h3 { font-size: 14px; margin: 10px 0 4px; }
.md ul, .md ol { padding-left: 20px; margin: 4px 0; } .md p { margin: 6px 0; }
.md table { border-collapse: collapse; font-size: 13px; } .md td, .md th { border: 1px solid var(--line); padding: 3px 6px; }
details.card summary { cursor: pointer; }
.report-item { display: flex; justify-content: space-between; padding: 9px 0; border-top: 1px solid var(--line); cursor: pointer; }
.report-item:first-child { border-top: 0; }
.tabs button span { font-size: 10px; }
