:root {
  color-scheme: light;
  --bg: #f2f3f5;
  --card: #ffffff;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --ink-3: #8a8984;
  --line: #e8e8e5;
  --fill: #f2f3f5;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --accent-soft: #e8f1fc;
  --bar: #9ec5f4;
  --bar-strong: #2a78d6;
  --income: #0b7a32;
  --good: #0ca30c;
  --warn: #fab219;
  --crit: #d03b3b;
  --crit-ink: #b42b2b;
  --good-ink: #0b7a32;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 6px 20px rgba(16, 24, 40, .05);
  --sheet-shadow: 0 -10px 40px rgba(0, 0, 0, .18);
  --radius: 20px;
  --nav-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f0f0e;
    --card: #1a1a19;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --ink-3: #8f8e86;
    --line: #2c2c2a;
    --fill: #242422;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --accent-soft: #1a2a3e;
    --bar: #1c5cab;
    --bar-strong: #5598e7;
    --income: #4cc76a;
    --crit-ink: #ef7c7c;
    --good-ink: #4cc76a;
    --shadow: none;
    --sheet-shadow: 0 -10px 40px rgba(0, 0, 0, .6);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f0f0e;
  --card: #1a1a19;
  --ink: #ffffff;
  --ink-2: #c3c2b7;
  --ink-3: #8f8e86;
  --line: #2c2c2a;
  --fill: #242422;
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --accent-soft: #1a2a3e;
  --bar: #1c5cab;
  --bar-strong: #5598e7;
  --income: #4cc76a;
  --crit-ink: #ef7c7c;
  --good-ink: #4cc76a;
  --shadow: none;
  --sheet-shadow: 0 -10px 40px rgba(0, 0, 0, .6);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
  min-height: 100dvh;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; padding: 0; }
a { color: var(--accent-ink); text-decoration: none; }
.hidden { display: none !important; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- каркас ---------- */
#app { max-width: 560px; margin: 0 auto; padding: calc(var(--safe-t) + 8px) 16px calc(var(--nav-h) + var(--safe-b) + 96px); }

.topbar {
  position: sticky; top: 0; z-index: 5;
  margin: calc(-8px - var(--safe-t)) -16px 8px; padding: calc(var(--safe-t) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px); -webkit-backdrop-filter: saturate(1.6) blur(14px);
}
.seg { display: flex; background: var(--fill); border-radius: 12px; padding: 3px; gap: 2px; }
.seg button { flex: 1; height: 34px; border-radius: 9px; font-size: 14px; font-weight: 500; color: var(--ink-2); }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .1); }
.period { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.period .label { font-size: 20px; font-weight: 650; letter-spacing: -.01em; text-align: center; flex: 1; }
.iconbtn { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; color: var(--ink-2); }
.iconbtn:active { background: var(--fill); }
.iconbtn:disabled { opacity: .3; }
.iconbtn svg { width: 22px; height: 22px; }

.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 12px; }
.card h3 { margin: 0 0 12px; font-size: 15px; font-weight: 600; color: var(--ink-2); display: flex; justify-content: space-between; align-items: baseline; }
.card h3 small { font-weight: 400; color: var(--ink-3); font-size: 13px; }

/* ---------- главная цифра ---------- */
.hero .label { font-size: 15px; color: var(--ink-2); }
.hero .value { font-size: 46px; font-weight: 700; letter-spacing: -.025em; line-height: 1.1; margin: 4px 0 8px; }
.hero .value .cur { font-size: 30px; font-weight: 600; color: var(--ink-3); margin-left: 4px; }
.delta { display: inline-flex; align-items: center; gap: 4px; font-size: 14px; font-weight: 500; padding: 4px 10px; border-radius: 999px; background: var(--fill); color: var(--ink-2); }
.delta.up { color: var(--crit-ink); }
.delta.down { color: var(--good-ink); }
.delta svg { width: 14px; height: 14px; }
.hero .row { display: flex; gap: 10px; margin-top: 14px; }
.hero .mini { flex: 1; background: var(--fill); border-radius: 14px; padding: 10px 12px; }
.hero .mini .k { font-size: 13px; color: var(--ink-3); }
.hero .mini .v { font-size: 17px; font-weight: 600; margin-top: 2px; }
.pos { color: var(--income); }

/* ---------- входящие ---------- */
.banner { display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; background: var(--accent-soft); color: var(--ink); border-radius: 16px; padding: 12px 14px; margin-bottom: 12px; }
.banner .ic { font-size: 22px; }
.banner b { display: block; font-weight: 600; }
.banner span { font-size: 13px; color: var(--ink-2); }
.banner .chev { margin-left: auto; color: var(--ink-3); }

/* ---------- график по дням ---------- */
.chart { position: relative; height: 180px; margin: 4px -4px 0; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .grid { stroke: var(--line); stroke-width: 1; }
.chart .tick { fill: var(--ink-3); font-size: 11px; }
.chart .bar { fill: var(--bar); }
.chart .bar.sel { fill: var(--bar-strong); }
.chart .hit { fill: transparent; }
.tip { position: absolute; top: 0; transform: translateX(-50%); background: var(--ink); color: var(--card); font-size: 13px; padding: 6px 10px; border-radius: 10px; white-space: nowrap; pointer-events: none; font-weight: 500; }
.tip small { opacity: .7; font-weight: 400; margin-right: 4px; }
.chart-foot { display: flex; justify-content: space-between; font-size: 13px; color: var(--ink-3); margin-top: 8px; }

/* ---------- статьи ---------- */
.cat-row { display: grid; grid-template-columns: 40px 1fr auto; gap: 4px 12px; align-items: center; padding: 10px 0; width: 100%; text-align: left; }
.cat-row + .cat-row { border-top: 1px solid var(--line); }
.emo { width: 40px; height: 40px; border-radius: 12px; background: var(--fill); display: grid; place-items: center; font-size: 20px; grid-row: span 2; }
.cat-row .name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-row .sum { font-weight: 600; text-align: right; }
.cat-row .sub { font-size: 13px; color: var(--ink-3); grid-column: 2 / 4; display: flex; align-items: center; gap: 8px; }
.track { flex: 1; height: 6px; border-radius: 3px; background: var(--fill); overflow: hidden; }
.track i { display: block; height: 100%; border-radius: 3px; background: var(--bar-strong); }
.track.warn i { background: var(--warn); }
.track.over i { background: var(--crit); }
.cat-row .share { min-width: 36px; text-align: right; }
.lim { font-size: 12px; display: inline-flex; align-items: center; gap: 3px; white-space: nowrap; }
.lim.warn { color: var(--ink-2); }
.lim.over { color: var(--crit-ink); font-weight: 600; }

.empty { text-align: center; color: var(--ink-3); padding: 28px 12px; }
.empty .big { font-size: 40px; margin-bottom: 8px; }
.empty p { margin: 6px 0; }

/* ---------- записи ---------- */
.search { display: flex; align-items: center; gap: 8px; background: var(--card); border-radius: 14px; padding: 0 12px; height: 44px; box-shadow: var(--shadow); margin-bottom: 10px; }
.search svg { width: 18px; height: 18px; color: var(--ink-3); flex: none; }
.search input { flex: 1; border: 0; outline: 0; background: none; font-size: 16px; min-width: 0; }
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; height: 34px; padding: 0 14px; border-radius: 999px; background: var(--card); box-shadow: var(--shadow); font-size: 14px; color: var(--ink-2); display: inline-flex; align-items: center; gap: 6px; }
.chip.on { background: var(--ink); color: var(--card); }
.day { display: flex; justify-content: space-between; font-size: 13px; font-weight: 600; color: var(--ink-3); padding: 14px 4px 6px; text-transform: uppercase; letter-spacing: .03em; }
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 0 14px; }
.item { display: grid; grid-template-columns: 40px 1fr auto; gap: 0 12px; align-items: center; padding: 10px 0; width: 100%; text-align: left; min-height: 60px; }
.item + .item { border-top: 1px solid var(--line); }
.item .note { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .meta { font-size: 13px; color: var(--ink-3); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .amt { font-weight: 600; text-align: right; white-space: nowrap; }

/* ---------- нижняя панель ---------- */
.nav {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  height: calc(var(--nav-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  display: flex; justify-content: center;
  background: color-mix(in srgb, var(--card) 92%, transparent);
  backdrop-filter: saturate(1.6) blur(16px); -webkit-backdrop-filter: saturate(1.6) blur(16px);
  border-top: 1px solid var(--line);
}
.nav .in { display: flex; width: 100%; max-width: 560px; }
.nav button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; font-size: 11px; font-weight: 500; color: var(--ink-3); }
.nav button svg { width: 24px; height: 24px; }
.nav button.on { color: var(--accent); }

.fab {
  position: fixed; z-index: 11; right: max(16px, calc(50vw - 280px + 16px)); bottom: calc(var(--nav-h) + var(--safe-b) + 16px);
  width: 58px; height: 58px; border-radius: 50%; background: var(--accent); color: #fff;
  display: grid; place-items: center; box-shadow: 0 8px 24px rgba(42, 120, 214, .4);
}
.fab svg { width: 28px; height: 28px; }
.fab:active { transform: scale(.94); }

/* ---------- нижний лист ---------- */
.backdrop { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .35); opacity: 0; transition: opacity .2s; }
.backdrop.show { opacity: 1; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; margin: 0 auto; max-width: 560px;
  max-height: calc(100dvh - var(--safe-t) - 24px); overflow-y: auto; overscroll-behavior: contain;
  background: var(--card); border-radius: 24px 24px 0 0; box-shadow: var(--sheet-shadow);
  padding: 8px 18px calc(18px + var(--safe-b)); transform: translateY(100%); transition: transform .26s cubic-bezier(.2, .8, .2, 1);
}
.sheet.show { transform: none; }
.grab { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet h2 { font-size: 20px; margin: 4px 0 14px; display: flex; justify-content: space-between; align-items: center; }
.field { margin-bottom: 14px; }
.field > label { display: block; font-size: 13px; color: var(--ink-3); margin-bottom: 6px; }
.input { width: 100%; height: 48px; border-radius: 14px; border: 1px solid var(--line); background: var(--bg); padding: 0 14px; font-size: 16px; outline: none; }
.input:focus { border-color: var(--accent); }
textarea.input { height: 84px; padding: 12px 14px; resize: none; }
.amount-in { display: flex; align-items: baseline; gap: 6px; border-bottom: 2px solid var(--line); padding-bottom: 6px; }
.amount-in:focus-within { border-color: var(--accent); }
.amount-in input { flex: none; min-width: 1.4ch; max-width: 100%; font-variant-numeric: tabular-nums; border: 0; outline: 0; background: none; font-size: 40px; font-weight: 700; letter-spacing: -.02em; }
.amount-in span { font-size: 28px; color: var(--ink-3); font-weight: 600; }
.catgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 8px; }
.catgrid button { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 4px; border-radius: 14px; background: var(--bg); font-size: 12px; line-height: 1.2; min-height: 72px; border: 2px solid transparent; }
.catgrid button .e { font-size: 22px; }
.catgrid button.on { border-color: var(--accent); background: var(--accent-soft); }
.datechips { display: flex; gap: 8px; align-items: center; }
.datechips .chip { box-shadow: none; background: var(--bg); }
.datechips .chip.on { background: var(--ink); color: var(--card); }
.datechips input { flex: 1; height: 34px; border: 0; background: var(--bg); border-radius: 999px; padding: 0 10px; min-width: 0; font-size: 14px; }
.btn { width: 100%; height: 52px; border-radius: 16px; background: var(--accent); color: #fff; font-size: 17px; font-weight: 600; }
.btn:active { opacity: .85; }
.btn.ghost { background: var(--fill); color: var(--ink); }
.btn.danger { background: none; color: var(--crit-ink); height: 44px; font-weight: 500; }
.btns { display: grid; gap: 8px; margin-top: 6px; }
.voice { background: var(--bg); border-radius: 14px; padding: 10px 12px; font-size: 14px; color: var(--ink-2); margin-bottom: 14px; }
.voice i { color: var(--ink); }
.voice audio { width: 100%; height: 36px; margin-top: 8px; }
.voice img { max-width: 100%; border-radius: 10px; margin-top: 8px; }
.emoji-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.emoji-row button { width: 40px; height: 40px; border-radius: 10px; background: var(--bg); font-size: 20px; }
.hint { font-size: 13px; color: var(--ink-3); margin-top: 6px; }

/* ---------- «Ещё» ---------- */
.menu-row { display: flex; align-items: center; gap: 12px; padding: 12px 0; width: 100%; text-align: left; min-height: 56px; }
.menu-row + .menu-row { border-top: 1px solid var(--line); }
.menu-row .t { flex: 1; }
.menu-row .t b { display: block; font-weight: 500; color: var(--ink); }
.menu-row .t span { font-size: 13px; color: var(--ink-3); }
.menu-row .chev { color: var(--ink-3); }
.code { font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace; background: var(--bg); border-radius: 12px; padding: 10px 12px; word-break: break-all; margin: 8px 0; }
.steps { margin: 8px 0 0; padding-left: 20px; color: var(--ink-2); font-size: 14px; }
.steps li { margin-bottom: 4px; }

/* ---------- вход ---------- */
.login { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; gap: 12px; }
.login .logo { width: 88px; height: 88px; border-radius: 24px; box-shadow: 0 12px 30px rgba(42, 120, 214, .35); }
.login h1 { margin: 8px 0 0; font-size: 28px; letter-spacing: -.02em; }
.login p { margin: 0; color: var(--ink-2); max-width: 320px; }
.login .btn { max-width: 320px; margin-top: 12px; display: flex; align-items: center; justify-content: center; gap: 8px; }
.spin { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: sp .8s linear infinite; display: inline-block; }
@keyframes sp { to { transform: rotate(360deg); } }

/* ---------- уведомление ---------- */
.toast {
  position: fixed; z-index: 30; left: 50%; bottom: calc(var(--nav-h) + var(--safe-b) + 88px); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--card); border-radius: 14px; padding: 12px 16px; font-size: 15px;
  display: flex; gap: 16px; align-items: center; opacity: 0; transition: .2s; max-width: calc(100vw - 32px); pointer-events: none;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
.toast button { color: var(--accent-ink); font-weight: 600; }
:root:not([data-theme="dark"]) .toast button { color: #86b6ef; }

.skeleton { background: linear-gradient(90deg, var(--fill), var(--line), var(--fill)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 12px; }
@keyframes sk { to { background-position: -200% 0; } }

@media (min-width: 600px) {
  #app { padding-top: 20px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
