:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #0f766e;
  --accent-ink: #ffffff;
  --accent-soft: #e6f4f1;
  --income: #047857;
  --expense: #b91c1c;
  --invest: #4f46e5;
  --asset: #b45309;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.05);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1117;
    --surface: #121a23;
    --surface-2: #17212c;
    --text: #e6ebf1;
    --muted: #8b98a8;
    --line: #243140;
    --accent: #2dd4bf;
    --accent-ink: #042f2e;
    --accent-soft: #10302e;
    --income: #34d399;
    --expense: #f87171;
    --invest: #a5b4fc;
    --asset: #fbbf24;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.pos { color: var(--income); }
.neg { color: var(--expense); }
.spacer { flex: 1; }
.hidden { display: none !important; }

/* ── Shell ── */
.shell { min-height: 100dvh; display: flex; flex-direction: column; }
.nav {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.nav .brand { display: none; }
.nav .nav-foot { display: none; }
.nav button {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; background: none; border: 0; color: var(--muted); font-size: 11px;
}
.nav button[aria-current="page"] { color: var(--accent); font-weight: 600; }
.nav svg { width: 22px; height: 22px; }

.main { width: 100%; max-width: 980px; margin: 0 auto; padding: 16px 16px 112px; }
.topbar { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.topbar h1 { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; }
.topbar .actions { margin-left: auto; display: flex; gap: 8px; align-items: center; }

@media (min-width: 900px) {
  .shell { flex-direction: row; }
  .nav {
    position: sticky; top: 0; align-self: flex-start; height: 100dvh; width: 244px; flex: none;
    flex-direction: column; border-top: 0; border-right: 1px solid var(--line); padding: 20px 12px; gap: 4px;
  }
  .nav .brand { white-space: nowrap; display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 4px 12px 18px; }
  .nav .brand i { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; }
  .nav .brand svg { width: 18px; height: 18px; color: var(--accent-ink); }
  .nav button { flex: none; flex-direction: row; gap: 12px; font-size: 14.5px; padding: 10px 12px; border-radius: 10px; text-align: left; }
  .nav button:hover { background: var(--surface-2); }
  .nav button[aria-current="page"] { background: var(--accent-soft); }
  .nav .nav-foot { display: block; margin-top: auto; }
  .main { padding: 28px 32px 56px; }
  .topbar .logout-mobile { display: none; }
}
@media (max-width: 899px) {
  .topbar .logout-desktop { display: none; }
}

/* ── Cards & layout ── */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.card + .card, .stack > * + * { margin-top: 14px; }
.card h2 { font-size: 14px; font-weight: 600; color: var(--muted); margin-bottom: 12px; }
.grid { display: grid; gap: 14px; }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
@media (max-width: 640px) {
  .grid.cols-3 { grid-template-columns: 1fr; }
  .grid.cols-3.keep { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid.cols-2.keep { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (min-width: 900px) {
  .grid.split { grid-template-columns: 1.3fr 1fr; align-items: start; }
}
.grid > .card, .grid + .card, .card + .grid, .grid + .grid { margin-top: 0; }
.stack > .grid { margin-top: 14px; }

.hero { background: var(--accent); color: var(--accent-ink); border: 0; }
.hero .label { font-size: 13px; opacity: 0.85; }
.hero .big { font-size: 34px; font-weight: 700; letter-spacing: -0.02em; margin: 2px 0 14px; }
.hero .parts { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.hero .part { background: rgba(255, 255, 255, 0.14); border-radius: 10px; padding: 8px 10px; min-width: 0; }
@media (prefers-color-scheme: dark) { .hero .part { background: rgba(4, 47, 46, 0.14); } }
.hero .part span { display: block; font-size: 12px; opacity: 0.85; }
.hero .part b { font-size: 14px; font-weight: 600; white-space: nowrap; }

.stat .label { font-size: 13px; color: var(--muted); }
.stat .value { font-size: 22px; font-weight: 700; letter-spacing: -0.01em; margin-top: 2px; white-space: nowrap; }
.stat .delta { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
@media (max-width: 640px) {
  .grid.keep .stat { padding: 12px 8px; }
  .grid.keep .stat .value { font-size: 12.5px; letter-spacing: -0.02em; }
  .grid.keep .stat .label { font-size: 12px; }
  .hero .part { padding: 8px 8px; }
  .hero .part b { font-size: 12px; letter-spacing: -0.02em; }
  .hero .part span { font-size: 11.5px; }
}

.monthbar { display: flex; align-items: center; gap: 4px; }
.monthbar .label { min-width: 132px; text-align: center; font-weight: 600; }
.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--line); color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }

/* ── Buttons & inputs ── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 14px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
  font-weight: 600; font-size: 14px;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.6; cursor: default; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-ghost { background: transparent; }
.btn-danger { color: var(--expense); }
.btn-sm { padding: 6px 10px; font-size: 13px; }
.link { background: none; border: 0; padding: 0; color: var(--accent); font-weight: 600; font-size: 13.5px; }

input[type="text"], input[type="password"], input[type="date"], input[type="search"], select {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); min-height: 42px;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); }

.fab {
  position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 15;
  width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  box-shadow: 0 6px 18px rgba(15, 118, 110, 0.35); display: grid; place-items: center;
}
.fab svg { width: 26px; height: 26px; }
@media (min-width: 900px) { .fab { bottom: 32px; right: 32px; } }

.filters { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; align-items: center; }
.filters .grow { flex: 1; min-width: 160px; }
.filters select { width: auto; min-width: 140px; }

/* ── Lists ── */
.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; padding: 12px 4px;
  background: none; border: 0; border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }
button.row:hover { background: var(--surface-2); }
.row .main-col { flex: 1; min-width: 0; }
.row .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .amt { font-weight: 600; white-space: nowrap; text-align: right; }
.row .amt small { display: block; font-weight: 400; font-size: 12.5px; color: var(--muted); }
.group-head { font-size: 12.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; padding: 14px 4px 4px; }
.group-head:first-child { padding-top: 0; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.income { background: var(--income); } .dot.expense { background: var(--expense); }
.dot.invest { background: var(--invest); } .dot.transfer, .dot.adjust { background: var(--muted); }
.badge { display: inline-block; font-size: 11.5px; padding: 1px 8px; border-radius: 99px; background: var(--accent-soft); color: var(--accent); font-weight: 600; margin-left: 6px; vertical-align: 1px; }
.badge.gray { background: var(--surface-2); color: var(--muted); border: 1px solid var(--line); }
.empty { text-align: center; padding: 28px 12px; color: var(--muted); }
.empty b { display: block; color: var(--text); margin-bottom: 4px; }
.row.archived { opacity: 0.55; }

.bars > div + div { margin-top: 12px; }
.bars .line { display: flex; justify-content: space-between; gap: 10px; font-size: 14px; margin-bottom: 5px; }
.bars .line span:first-child { font-weight: 500; }
.bar { height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; border: 1px solid var(--line); }
.bar > span { display: block; height: 100%; border-radius: 99px; background: var(--accent); }
.bar.expense > span { background: var(--expense); }
.stacked { display: flex; height: 12px; border-radius: 99px; overflow: hidden; gap: 2px; margin: 4px 0 14px; }
.stacked span { display: block; height: 100%; }
.legend { display: flex; gap: 14px; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-top: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ── Charts (SVG) ── */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 14px; font-family: inherit; }
.chart .axis { stroke: var(--line); stroke-width: 1; }
.chart .bar-income { fill: var(--income); }
.chart .bar-expense { fill: var(--expense); }
.chart .line { fill: none; stroke: var(--accent); stroke-width: 2.2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { fill: var(--accent); opacity: 0.1; }
.chart .val { fill: var(--text); font-weight: 600; }

/* ── Modal ── */
dialog {
  border: 0; padding: 0; border-radius: 18px; width: min(480px, calc(100vw - 24px)); max-height: calc(100dvh - 24px);
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
dialog::backdrop { background: rgba(15, 23, 42, 0.5); }
@media (max-width: 640px) {
  dialog { width: 100vw; max-width: 100vw; margin: auto 0 0; border-radius: 18px 18px 0 0; max-height: 92dvh; }
}
.form { padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-head h2 { font-size: 18px; }
.field label, .field .lab { display: block; font-size: 13px; font-weight: 600; margin-bottom: 5px; }
.field .hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.field.check { display: flex; gap: 10px; align-items: flex-start; }
.field.check label { margin: 0; font-weight: 500; }
.form-error { color: var(--expense); font-size: 13.5px; min-height: 0; }
.form-error:empty { display: none; }
.modal-actions { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; padding-top: 4px; }

#toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 100;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 99px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; }

/* ── Login ── */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 20px; }
.login .card { width: min(380px, 100%); padding: 26px; }
.login h1 { font-size: 22px; margin: 12px 0 4px; }
.login .logo { width: 44px; height: 44px; border-radius: 12px; background: var(--accent); display: grid; place-items: center; color: var(--accent-ink); }
.login .logo svg { width: 26px; height: 26px; }
.login form { margin-top: 18px; display: flex; flex-direction: column; gap: 12px; }

.skeleton { height: 120px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.5; } }
@media (prefers-reduced-motion: reduce) { .skeleton { animation: none; } #toast { transition: none; } }
