:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --series-1: #2a78d6;  /* net worth */
  --series-2: #eb6834;  /* debt */
  --series-3: #1baf7a;
  --series-4: #eda100;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --bad-text: #b42323;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d; --surface: #1a1a19; --surface-2: #262624;
    --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
    --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --good-text: #0ca30c; --bad-text: #e66767; --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #262624;
  --ink: #ffffff; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5; --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --good-text: #0ca30c; --bad-text: #e66767; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-2); margin: 22px 4px 8px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; }
.pos { color: var(--good-text); }
.neg { color: var(--bad-text); }
.error { color: var(--bad-text); font-size: 14px; }

main { max-width: 640px; margin: 0 auto; padding: 16px 16px calc(84px + env(safe-area-inset-bottom)); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 10px; }

/* ---------- hero ---------- */
.hero { padding: 20px 18px; }
.hero .label { color: var(--ink-2); font-size: 14px; display: flex; justify-content: space-between; gap: 8px; }
.hero .value { font-size: 40px; font-weight: 700; letter-spacing: -0.02em; line-height: 1.1; margin: 6px 0 4px; word-break: break-word; }
.hero .split { display: flex; gap: 18px; margin-top: 12px; flex-wrap: wrap; }
.hero .split div { min-width: 0; }
.hero .split .k { font-size: 12px; color: var(--muted); }
.hero .split .v { font-size: 16px; font-weight: 600; }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 99px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--ink-2); }

/* ---------- milestone tiles ---------- */
.tiles { display: grid; grid-template-columns: 1fr; gap: 10px; margin-top: 10px; }
.tile { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: baseline; }
.tile .t-title { font-weight: 600; }
.tile .t-when { font-size: 20px; font-weight: 700; text-align: right; white-space: nowrap; }
.tile .t-sub { color: var(--muted); font-size: 13px; }
.tile .t-date { color: var(--ink-2); font-size: 13px; text-align: right; white-space: nowrap; }
.tile.done .t-when { color: var(--good-text); }
.tile.never .t-when { color: var(--bad-text); }

/* ---------- charts ---------- */
.chart-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.chart-head h3 { font-size: 15px; }
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 2px; }
.seg button { border: 0; background: transparent; color: var(--ink-2); font: inherit; font-size: 13px; padding: 4px 10px; border-radius: 7px; cursor: pointer; }
.seg button.active { background: var(--surface); color: var(--ink); font-weight: 600; box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.legend { display: flex; gap: 14px; font-size: 13px; color: var(--ink-2); margin: 2px 0 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 6px; }
.chart { position: relative; width: 100%; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .zero { stroke: var(--axis); stroke-width: 1.5; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .s1 { stroke: var(--series-1); }
.chart .s2 { stroke: var(--series-2); }
.chart .dot1 { fill: var(--series-1); stroke: var(--surface); stroke-width: 2; }
.chart .dot2 { fill: var(--series-2); stroke: var(--surface); stroke-width: 2; }
.chart .cross { stroke: var(--axis); stroke-width: 1; }
.chart .mark { fill: var(--ink); stroke: var(--surface); stroke-width: 2; }
.chart .mark-label { fill: var(--ink); font-weight: 600; font-size: 11px; }
.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border);
  border-radius: 10px; padding: 8px 10px; font-size: 13px; box-shadow: 0 4px 16px rgba(0,0,0,.14);
  min-width: 150px;
}
.tooltip .tt-date { color: var(--muted); font-size: 12px; margin-bottom: 4px; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 12px; }
.tooltip .tt-row i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; }

/* ---------- cash flow bar ---------- */
.flow { display: flex; height: 14px; border-radius: 7px; overflow: hidden; gap: 2px; background: var(--surface); margin: 10px 0 12px; }
.flow span { display: block; height: 100%; min-width: 2px; }
.flow-legend { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; font-size: 14px; }
.flow-legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; vertical-align: -1px; }
.flow-legend .total { border-top: 1px solid var(--border); padding-top: 6px; font-weight: 650; }

/* ---------- health ---------- */
.health { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.health .h { background: var(--surface-2); border-radius: 10px; padding: 10px; min-width: 0; }
.health .hk { font-size: 12px; color: var(--ink-2); }
.health .hv { font-size: 18px; font-weight: 700; margin-top: 2px; }
.health .hs { font-size: 12px; margin-top: 2px; display: flex; align-items: center; gap: 4px; color: var(--ink-2); }
.dotc { width: 8px; height: 8px; border-radius: 50%; display: inline-block; flex: none; }

.insights { margin: 0; padding: 0; list-style: none; }
.insights li { padding: 10px 0; border-top: 1px solid var(--border); font-size: 14px; display: flex; gap: 10px; }
.insights li:first-child { border-top: 0; padding-top: 0; }
.insights .ic { flex: none; }

/* ---------- lists ---------- */
.list { padding: 4px 0; }
.row { display: flex; align-items: center; gap: 12px; padding: 12px 16px; cursor: pointer; border-top: 1px solid var(--border); }
.row:first-child { border-top: 0; }
.row:active { background: var(--surface-2); }
.row .main { flex: 1; min-width: 0; }
.row .title { font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .sub { font-size: 13px; color: var(--muted); }
.row .amt { text-align: right; font-weight: 600; white-space: nowrap; }
.row .amt .sub { font-weight: 400; }
.section-total { display: flex; justify-content: space-between; align-items: baseline; margin: 22px 4px 8px; }
.section-total h2 { margin: 0; }
.section-total .v { font-weight: 650; }
.add { display: block; width: 100%; padding: 12px 16px; border: 0; border-top: 1px solid var(--border); background: transparent; color: var(--accent); font: inherit; font-weight: 600; text-align: left; cursor: pointer; }
.empty { padding: 12px 16px; color: var(--muted); font-size: 14px; }

.loan .top { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.loan .name { font-weight: 650; }
.loan .out { font-size: 22px; font-weight: 700; }
.progress { height: 8px; background: var(--surface-2); border-radius: 4px; overflow: hidden; margin: 10px 0; }
.progress span { display: block; height: 100%; background: var(--series-1); border-radius: 4px; }
.kv { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; font-size: 13px; }
.kv .k { color: var(--muted); font-size: 12px; }
.kv .v { font-weight: 600; }
.loan-actions { display: flex; gap: 8px; margin-top: 12px; }
.schedule { width: 100%; border-collapse: collapse; font-size: 13px; margin-top: 10px; font-variant-numeric: tabular-nums; }
.schedule th, .schedule td { padding: 6px 4px; text-align: right; border-top: 1px solid var(--border); }
.schedule th:first-child, .schedule td:first-child { text-align: left; }
.schedule th { color: var(--muted); font-weight: 500; border-top: 0; }
.table-wrap { max-height: 320px; overflow: auto; }

/* ---------- buttons & forms ---------- */
.btn { appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--ink); font: inherit; font-weight: 600; padding: 10px 14px; border-radius: 10px; cursor: pointer; }
.btn.primary { background: var(--accent); color: #fff; border-color: transparent; }
.btn.danger { color: var(--bad-text); background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; }

.field { display: block; margin-bottom: 12px; }
.field > span { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 4px; }
.field small { display: block; font-size: 12px; color: var(--muted); margin-top: 3px; }
input, select, textarea {
  width: 100%; font: inherit; font-size: 16px; color: var(--ink); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
.check { display: flex; gap: 10px; align-items: center; margin-bottom: 12px; font-size: 14px; }
.check input { width: 20px; height: 20px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 12px; }

/* ---------- tabs ---------- */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; justify-content: center;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabs button { flex: 1; max-width: 128px; border: 0; background: transparent; color: var(--muted); font: inherit; font-size: 11px; padding: 8px 0 10px; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer; }
.tabs .ti { font-size: 18px; line-height: 1.2; }
.tabs button.active { color: var(--accent); font-weight: 600; }

/* ---------- sheet ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.4); z-index: 30; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; max-height: 92vh; overflow: auto;
  background: var(--surface); border-radius: 18px 18px 0 0; padding: 18px 16px calc(18px + env(safe-area-inset-bottom));
  max-width: 640px; margin: 0 auto; animation: up .18s ease-out;
}
@keyframes up { from { transform: translateY(30px); } to { transform: none; } }
.sheet h3 { font-size: 18px; margin-bottom: 14px; }
.sheet .actions { display: flex; gap: 8px; margin-top: 6px; }
.sheet .actions .btn.primary { flex: 1; }

.toast { position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60; background: var(--ink); color: var(--page); padding: 8px 14px; border-radius: 10px; font-size: 14px; }

@media (min-width: 520px) {
  .tiles { grid-template-columns: 1fr; }
  .hero .value { font-size: 46px; }
}
@media (max-width: 380px) {
  .health { grid-template-columns: 1fr 1fr; }
  .kv { grid-template-columns: 1fr 1fr; }
}

/* ---------- loan status, quick add ---------- */
.due-line { font-size: 13px; background: var(--surface-2); border-radius: 10px; padding: 8px 10px; margin: 10px 0 0; line-height: 1.4; }
.due-line.ok { color: var(--muted); }
.qa .seg { display: flex; margin: 0 auto 6px; max-width: 280px; }
.qa .seg button { flex: 1; }
.qa-amt { font-size: 44px; font-weight: 700; text-align: center; padding: 14px 0 8px; font-variant-numeric: tabular-nums; min-height: 76px; overflow: hidden; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.chips button { border: 1px solid var(--border); background: var(--surface-2); color: var(--ink); border-radius: 999px; padding: 6px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.chips button.active { background: var(--accent); color: #fff; border-color: transparent; }
.numpad { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 4px; }
.numpad button { font: inherit; font-size: 26px; font-weight: 600; padding: 16px 0; border: 1px solid var(--border); border-radius: 14px; background: var(--surface); color: var(--ink); cursor: pointer; touch-action: manipulation; }
.numpad button:active { background: var(--surface-2); }
