:root {
  --bg: var(--tg-theme-secondary-bg-color, #f2f2f5);
  --card: var(--tg-theme-section-bg-color, var(--tg-theme-bg-color, #ffffff));
  --text: var(--tg-theme-text-color, #111214);
  --hint: var(--tg-theme-hint-color, #8d9096);
  --accent: var(--tg-theme-button-color, #3478f6);
  --accent-text: var(--tg-theme-button-text-color, #ffffff);
  --link: var(--tg-theme-link-color, #3478f6);
  --danger: var(--tg-theme-destructive-text-color, #e5484d);
  --income: #1e9e5a;
  --line: color-mix(in srgb, var(--hint) 18%, transparent);
  --soft: color-mix(in srgb, var(--hint) 10%, transparent);
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not(.tg) { --bg: #0e0f12; --card: #191b20; --text: #eceef1; --hint: #8a8f99; }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; }
body {
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
  min-height: 100vh; -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; text-align: inherit; }
input, select {
  font: inherit; color: var(--text); background: var(--soft);
  border: 0; border-radius: 10px; padding: 11px 12px; width: 100%; outline: none;
}
input:focus, select:focus { box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 50%, transparent); }
svg { flex: none; }

/* header */
.top { position: sticky; top: 0; z-index: 5; background: var(--bg); padding: 14px 16px 10px; display: flex; align-items: flex-end; justify-content: space-between; gap: 12px; }
.ledger-btn { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.ledger-kind { font-size: 12px; color: var(--hint); }
.ledger-name { display: flex; align-items: center; gap: 6px; font-size: 20px; font-weight: 700; letter-spacing: -.01em; min-width: 0; }
.ledger-name span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ledger-name svg { color: var(--hint); }
.month { display: flex; align-items: center; background: var(--card); border-radius: 10px; padding: 2px; font-size: 13px; font-weight: 600; flex: none; }
.month button { width: 30px; height: 30px; display: grid; place-items: center; color: var(--hint); }
.month span { min-width: 70px; text-align: center; }

main { padding: 2px 16px 24px; display: flex; flex-direction: column; gap: 10px; }
.card { background: var(--card); border-radius: var(--radius); padding: 14px 16px; }
.card-title { display: flex; justify-content: space-between; align-items: baseline; margin: 0 0 10px; font-size: 13px; font-weight: 600; color: var(--hint); }
.card-title button { color: var(--link); font-weight: 600; }
.section-label { font-size: 13px; color: var(--hint); font-weight: 600; margin: 8px 4px -2px; display: flex; justify-content: space-between; }
.muted { color: var(--hint); }
.small { font-size: 13px; }
.row { display: flex; align-items: center; gap: 10px; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--income); }
.neg { color: var(--danger); }

/* hero */
.hero .label { font-size: 13px; color: var(--hint); }
.hero .big { font-size: 32px; font-weight: 750; letter-spacing: -.02em; margin: 2px 0 12px; }
.hero .split { display: flex; gap: 24px; font-size: 14px; }
.hero .split div span { display: block; font-size: 12px; color: var(--hint); }

/* accounts strip */
.accounts { display: flex; gap: 8px; overflow-x: auto; margin: 0 -16px; padding: 0 16px; scrollbar-width: none; }
.accounts::-webkit-scrollbar { display: none; }
.acc { flex: none; min-width: 128px; background: var(--card); border-radius: 12px; padding: 10px 12px; }
.acc .t { font-size: 12px; color: var(--hint); }
.acc .n { font-size: 14px; font-weight: 600; margin-top: 1px; }
.acc .b { font-size: 16px; font-weight: 700; margin-top: 6px; }
.acc.add { display: grid; place-items: center; min-width: 96px; color: var(--link); font-weight: 600; font-size: 14px; }

/* lists */
.list { display: flex; flex-direction: column; }
.item { display: flex; align-items: center; gap: 12px; padding: 10px 0; width: 100%; }
.item + .item { border-top: 1px solid var(--line); }
.ico { width: 36px; height: 36px; border-radius: 10px; background: var(--soft); display: grid; place-items: center; font-size: 17px; flex: none; }
.ico svg { width: 18px; height: 18px; stroke: var(--hint); fill: none; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sub { font-size: 13px; color: var(--hint); margin-top: 1px; }
.chev { color: var(--hint); }

.catbar { height: 4px; border-radius: 2px; background: var(--soft); margin-top: 6px; overflow: hidden; }
.catbar > div { height: 100%; background: var(--accent); border-radius: 2px; }
.catbar.warn > div { background: #f0a020; }
.catbar.over > div { background: var(--danger); }

.bars { display: flex; align-items: flex-end; gap: 2px; height: 72px; }
.bars div { flex: 1; background: var(--accent); border-radius: 2px 2px 0 0; min-height: 2px; }
.bars div.zero { background: var(--soft); }

.seg { display: flex; background: var(--soft); border-radius: 10px; padding: 2px; }
.seg button { flex: 1; padding: 7px 4px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--hint); text-align: center; }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.08); }

.btn { display: block; width: 100%; padding: 13px; border-radius: 12px; background: var(--accent); color: var(--accent-text); font-weight: 600; text-align: center; }
.btn.secondary { background: var(--soft); color: var(--link); }
.btn.danger { background: none; color: var(--danger); }
.btn.inline { display: inline-block; width: auto; padding: 7px 12px; font-size: 14px; }
.link { color: var(--link); font-weight: 600; font-size: 14px; }
.stack { display: flex; flex-direction: column; gap: 8px; margin-top: 12px; }

.switch { position: relative; width: 46px; height: 28px; border-radius: 14px; background: var(--soft); flex: none; transition: background .2s; }
.switch::after { content: ""; position: absolute; top: 2px; left: 2px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .2s; }
.switch.on { background: var(--income); }
.switch.on::after { transform: translateX(18px); }

.fab {
  position: fixed; right: 16px; bottom: calc(80px + env(safe-area-inset-bottom)); z-index: 6;
  width: 54px; height: 54px; border-radius: 50%; background: var(--accent); color: var(--accent-text);
  display: grid; place-items: center; box-shadow: 0 4px 14px rgba(0,0,0,.18);
}
.fab[hidden] { display: none; }
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6; display: flex;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
}
.tabs button { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; font-size: 10.5px; color: var(--hint); padding: 3px 0; text-align: center; }
.tabs svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs button.active { color: var(--accent); }
.tabs button[hidden] { display: none; }

/* sheet */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 10; }
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-height: 92vh; overflow-y: auto;
  background: var(--card); border-radius: 16px 16px 0 0; padding: 8px 16px calc(18px + env(safe-area-inset-bottom));
  transform: translateY(105%); transition: transform .22s ease;
}
.sheet.open { transform: none; }
.sheet .grip { width: 36px; height: 4px; border-radius: 2px; background: var(--line); margin: 0 auto 14px; }
.sheet h2 { margin: 0 0 14px; font-size: 17px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > label { font-size: 13px; color: var(--hint); }
.amount-input { font-size: 30px; font-weight: 700; text-align: center; padding: 12px; }
.cats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.cat { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 2px; border-radius: 10px; font-size: 11px; line-height: 1.2; text-align: center; background: var(--soft); border: 1.5px solid transparent; }
.cat span { font-size: 20px; }
.cat.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 7px 12px; border-radius: 9px; background: var(--soft); font-size: 14px; border: 1.5px solid transparent; }
.chip.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 10%, transparent); }

/* дела */
.top-seg { margin-bottom: 2px; }
.check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--line); flex: none; position: relative; }
.check.on { background: var(--income); border-color: var(--income); }
.check.on::after { content: ""; position: absolute; left: 7px; top: 3px; width: 6px; height: 11px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg); }
.item .grow { text-align: left; }
.struck { text-decoration: line-through; color: var(--hint); }
.time-chip { font-size: 13px; font-weight: 600; color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, transparent); border-radius: 8px; padding: 4px 7px; flex: none; font-variant-numeric: tabular-nums; }
.chips.days .chip { min-width: 40px; text-align: center; padding: 7px 0; flex: 1; }
.card-title span { color: var(--text); font-size: 15px; }
.card-title span::first-letter { text-transform: uppercase; }
[hidden] { display: none !important; }
code { background: var(--soft); padding: 1px 6px; border-radius: 6px; font-size: 13px; }

/* шапка: шестерёнка и экраны настроек */
.top-right { display: flex; align-items: center; gap: 8px; flex: none; }
.gear { width: 36px; height: 36px; border-radius: 10px; background: var(--card); color: var(--hint); display: grid; place-items: center; }
.gear:active { color: var(--accent); }
.page-head { display: flex; align-items: center; gap: 10px; margin: 0 0 2px; }
.page-head h2 { margin: 0; font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.page-head .back { color: var(--link); font-weight: 600; font-size: 15px; flex: none; }
html.tg .page-head .back { display: none; }  /* в Telegram есть своя кнопка «Назад» */
.card.menu { padding: 2px 16px; }
.menu-row { text-align: left; }

/* подсказка «С чего начать» */
.intro-row { display: grid; grid-template-columns: 96px 1fr; gap: 10px; padding: 8px 0; font-size: 14px; line-height: 1.45; }
.intro-row + .intro-row { border-top: 1px solid var(--line); }
.intro-row b { font-weight: 600; }
.intro-row span { color: var(--hint); }
.intro-row code { color: var(--text); }
@media (max-width: 360px) { .intro-row { grid-template-columns: 1fr; gap: 2px; } }

/* оформление */
select option { background: var(--card); color: var(--text); }
.swatches { display: grid; grid-template-columns: repeat(5, 1fr); gap: 10px 6px; }
.swatch { display: flex; flex-direction: column; align-items: center; gap: 5px; font-size: 11px; color: var(--hint); text-align: center; }
.swatch .dot { width: 34px; height: 34px; border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0,0,0,.08); transition: transform .15s; }
.swatch.on .dot { box-shadow: 0 0 0 2px var(--card), 0 0 0 4px var(--accent); transform: scale(1.05); }
.swatch.on .nm { color: var(--text); font-weight: 600; }
.preview { margin-top: 16px; padding: 14px; border-radius: 12px; background: var(--bg); }
.pv-big { font-size: 24px; font-weight: 750; letter-spacing: -.02em; margin: 2px 0 8px; }
.preview .btn.inline { padding: 7px 14px; }

/* страница вне Telegram */
body.landing { padding-bottom: 0; }
body.landing .top, body.landing .tabs, body.landing .fab { display: none; }
body.landing main { min-height: 100vh; justify-content: center; align-items: center; padding: 24px 16px; }
.landing-card { background: var(--card); border-radius: 18px; padding: 28px 22px; max-width: 420px; width: 100%; }
.landing-card h1 { margin: 0 0 8px; font-size: 26px; letter-spacing: -.02em; }
.landing-card p { margin: 0 0 14px; color: var(--hint); }
.landing-card ul { margin: 0 0 6px; padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.landing-card code { background: var(--soft); padding: 1px 6px; border-radius: 6px; font-size: 14px; }
.landing-card a.btn { text-decoration: none; }
.landing-card .stack + p { margin: 14px 0 0; }

.empty { text-align: center; padding: 28px 10px; color: var(--hint); font-size: 14px; }
.toast {
  position: fixed; left: 50%; bottom: calc(146px + env(safe-area-inset-bottom)); transform: translateX(-50%) translateY(16px);
  background: rgba(20,20,22,.92); color: #fff; padding: 9px 14px; border-radius: 10px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 20; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
