/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --bg: #f4f4f1;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --line: #e1e0d9;
  --line-2: #c3c2b7;
  --accent: #2a78d6;
  --accent-soft: #e8f0fb;
  --on-accent: #ffffff;
  --good: #006300;
  --good-soft: #e6f4e6;
  --bad: #c0392b;
  --bad-soft: #fbe9e7;
  --warn: #9a5b00;
  --warn-soft: #fff3dc;
  --info-soft: #eef1f5;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --gray-series: #c3c2b7;
  --grid: #e1e0d9;
  --shadow: 0 1px 2px rgba(11, 11, 11, .06), 0 8px 24px rgba(11, 11, 11, .06);
  --radius: 16px;
  --tab-h: 58px;
  --top-h: 52px;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232322;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --line: #2c2c2a;
    --line-2: #383835;
    --accent: #3987e5;
    --accent-soft: #1b2b40;
    --good: #0ca30c;
    --good-soft: #142914;
    --bad: #e66767;
    --bad-soft: #3a1c1c;
    --warn: #e0a030;
    --warn-soft: #34290f;
    --info-soft: #20242a;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --gray-series: #4a4a47;
    --grid: #2c2c2a;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  }
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; -webkit-tap-highlight-color: transparent; }
input, select, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 20px; }
h3 { font-size: 15px; color: var(--ink-2); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
p { margin: 0; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.good { color: var(--good); }
.bad { color: var(--bad); }
[hidden] { display: none !important; }

/* ---------- login ---------- */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 360px; background: var(--surface); border-radius: 24px; padding: 32px 24px; box-shadow: var(--shadow); display: grid; gap: 12px; text-align: center; }
.login-card h1 { font-size: 24px; }
.login-sub { color: var(--ink-2); margin-bottom: 8px; }
.login-logo { width: 56px; height: 56px; border-radius: 14px; background: var(--accent); margin: 0 auto 8px; display: flex; align-items: flex-end; justify-content: center; gap: 5px; padding: 10px 11px; }
.login-logo span { width: 9px; background: #fff; border-radius: 2px 2px 0 0; }
.login-logo span:nth-child(1) { height: 32%; } .login-logo span:nth-child(2) { height: 55%; } .login-logo span:nth-child(3) { height: 80%; }
.login-error { color: var(--bad); font-size: 14px; }

/* ---------- shell ---------- */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  height: calc(var(--top-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex; align-items: center; justify-content: center; gap: 4px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 12px; color: var(--ink-2); }
.icon-btn:active { background: var(--surface-2); }
.month-btn { min-width: 160px; height: 40px; padding: 0 14px; border-radius: 12px; font-weight: 650; font-size: 17px; }
.month-btn:active { background: var(--surface-2); }
.view {
  padding: calc(var(--top-h) + env(safe-area-inset-top) + 12px) 16px calc(var(--tab-h) + env(safe-area-inset-bottom) + 96px);
  max-width: 720px; margin: 0 auto;
  display: grid; gap: 14px;
}
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; justify-content: space-around;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tab { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; color: var(--muted); font-size: 11px; font-weight: 500; }
.tab.active { color: var(--accent); }
.tab svg { width: 24px; height: 24px; }
.fab {
  position: fixed; right: 18px; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px); z-index: 21;
  width: 56px; height: 56px; border-radius: 18px; background: var(--accent); color: var(--on-accent);
  display: grid; place-items: center; box-shadow: 0 8px 20px rgba(42, 120, 214, .35);
}
.fab svg { width: 26px; height: 26px; stroke-width: 2.4; }
.fab:active { transform: scale(.96); }

/* ---------- components ---------- */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); display: grid; gap: 12px; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.hero { display: grid; gap: 4px; }
.hero-label { font-size: 13px; color: var(--ink-2); font-weight: 500; }
.hero-value { font-size: 36px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; }
.hero-delta { font-size: 14px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.kpi { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; display: grid; gap: 2px; min-width: 0; }
.kpi-label { font-size: 12px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-value { font-size: 16px; font-weight: 650; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-sub { font-size: 12px; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill-good { background: var(--good-soft); color: var(--good); }
.pill-bad { background: var(--bad-soft); color: var(--bad); }
.pill-neutral { background: var(--surface-2); color: var(--ink-2); }

.insights { display: grid; gap: 8px; }
.insight { display: flex; gap: 10px; padding: 10px 12px; border-radius: 12px; background: var(--info-soft); font-size: 14.5px; line-height: 1.45; }
.insight-good { background: var(--good-soft); }
.insight-bad { background: var(--bad-soft); }
.insight-neutral { background: var(--surface-2); }
.insight .dot { flex: 0 0 8px; width: 8px; height: 8px; border-radius: 50%; margin-top: 7px; background: var(--muted); }
.insight-good .dot { background: var(--good); }
.insight-bad .dot { background: var(--bad); }
.insight-info .dot { background: var(--accent); }

.chart-wrap { position: relative; width: 100%; }
.chart-wrap canvas { width: 100% !important; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 12.5px; color: var(--ink-2); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

.list { display: grid; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 12px 0; border-top: 1px solid var(--line); min-height: 48px; }
.row:first-child { border-top: 0; }
.row-main { min-width: 0; display: grid; gap: 1px; }
.row-title { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-amt { font-weight: 600; white-space: nowrap; }
.row-amt.zero { color: var(--muted); font-weight: 400; }
.row.tappable { cursor: pointer; }
.row.tappable:active { background: var(--surface-2); margin: 0 -16px; padding-left: 16px; padding-right: 16px; }

.cat-card { padding: 0; }
.cat-head { display: flex; align-items: center; gap: 10px; padding: 14px 16px; cursor: pointer; }
.cat-emoji { font-size: 22px; width: 32px; text-align: center; }
.cat-name { font-weight: 650; flex: 1; }
.cat-total { font-weight: 650; }
.cat-delta { font-size: 12px; }
.cat-body { padding: 0 16px 6px; }
.cat-body .row { padding: 10px 0; }
.chev { color: var(--muted); transition: transform .15s; }
.collapsed .chev { transform: rotate(-90deg); }
.collapsed .cat-body { display: none; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 46px; padding: 0 18px; border-radius: 13px; font-weight: 600; font-size: 16px; background: var(--surface-2); color: var(--ink); }
.btn:active { opacity: .8; }
.btn-primary { background: var(--accent); color: var(--on-accent); }
.btn-danger { background: var(--bad-soft); color: var(--bad); }
.btn-ghost { background: transparent; color: var(--accent); }
.btn-block { width: 100%; }
.btn-sm { height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
.input, select.input, textarea.input {
  width: 100%; height: 48px; padding: 0 14px; border-radius: 12px; border: 1px solid var(--line-2); background: var(--surface);
  font-size: 16px; outline: none; -webkit-appearance: none; appearance: none;
}
textarea.input { height: auto; padding: 12px 14px; resize: vertical; }
.input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.input-lg { height: 54px; font-size: 18px; text-align: center; }
.input-inline { height: 40px; width: 130px; text-align: right; padding: 0 10px; font-variant-numeric: tabular-nums; }
select.input { background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: calc(100% - 20px) 21px, calc(100% - 15px) 21px; background-size: 5px 5px; background-repeat: no-repeat; padding-right: 36px; }
.field { display: grid; gap: 6px; }
.field label { font-size: 13px; font-weight: 600; color: var(--ink-2); }
.form { display: grid; gap: 14px; }
.seg { display: flex; background: var(--surface-2); border-radius: 12px; padding: 3px; }
.seg button { flex: 1; height: 36px; border-radius: 10px; font-weight: 600; font-size: 14px; color: var(--ink-2); }
.seg button.active { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { height: 34px; padding: 0 12px; border-radius: 999px; background: var(--surface-2); font-size: 14px; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; }
.chip.active { background: var(--accent); color: var(--on-accent); }

.table-scroll { overflow-x: auto; -webkit-overflow-scrolling: touch; margin: 0 -16px; padding: 0 16px; }
table.grid { border-collapse: collapse; min-width: 100%; font-size: 14px; }
table.grid th, table.grid td { padding: 8px 6px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.grid th { font-size: 12px; color: var(--ink-2); font-weight: 600; }
table.grid th:first-child, table.grid td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); font-weight: 500; }
table.grid tfoot td { font-weight: 650; border-bottom: 0; }
table.grid tr.is-current td { background: var(--accent-soft); font-weight: 600; }
table.grid input { width: 96px; height: 36px; text-align: right; }

.empty { text-align: center; color: var(--muted); padding: 24px 8px; font-size: 14.5px; }
.divider { height: 1px; background: var(--line); margin: 4px 0; }
.inline-actions { display: flex; gap: 6px; }
.inline-actions button { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; color: var(--ink-2); background: var(--surface-2); }
.inline-actions button svg { width: 18px; height: 18px; }
.warn-box { background: var(--warn-soft); color: var(--warn); border-radius: 12px; padding: 10px 12px; font-size: 14px; }
.ok-box { background: var(--good-soft); color: var(--good); border-radius: 12px; padding: 10px 12px; font-size: 14px; }
.month-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.month-grid button { height: 44px; border-radius: 12px; background: var(--surface-2); font-weight: 500; }
.month-grid button.active { background: var(--accent); color: var(--on-accent); }
.month-grid button.has-data { box-shadow: inset 0 -3px 0 var(--accent); }
.year-nav { display: flex; align-items: center; justify-content: space-between; }
.year-nav b { font-size: 18px; }

/* ---------- sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0, 0, 0, .45); animation: fade .18s ease-out; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 92dvh; overflow: auto;
  background: var(--surface); border-radius: 22px 22px 0 0; padding: 8px 18px calc(24px + env(safe-area-inset-bottom));
  animation: up .22s cubic-bezier(.2, .8, .2, 1);
  max-width: 720px; margin: 0 auto;
}
.sheet-handle { width: 40px; height: 5px; border-radius: 3px; background: var(--line-2); margin: 4px auto 14px; }
.sheet-body { display: grid; gap: 14px; }
.sheet-title { display: flex; align-items: center; justify-content: space-between; }
@keyframes up { from { transform: translateY(40px); opacity: .6; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.toast { position: fixed; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 20px); transform: translateX(-50%); z-index: 60; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 500; box-shadow: var(--shadow); max-width: calc(100% - 32px); }
.toast.error { background: var(--bad); color: #fff; }

@media (max-width: 400px) { .kpi-value { font-size: 14.5px; } .hero-value { font-size: 32px; } }
@media (min-width: 720px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
}
