:root {
  color-scheme: light;
  --page: #f6f6f3;
  --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;
  --accent-ink: #ffffff;
  --accent-soft: #cde2fb;
  --planned: #86b6ef;
  --pos: #006300;
  --neg: #b42d2d;
  --warn: #fab219;
  --crit: #d03b3b;
  --good: #0ca30c;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 24px rgba(0,0,0,.06);
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #252523;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-soft: #1c3656;
    --planned: #184f95;
    --pos: #0ca30c;
    --neg: #e66767;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #252523; --ink: #ffffff; --ink-2: #c3c2b7;
  --muted: #898781; --grid: #2c2c2a; --axis: #383835; --border: rgba(255,255,255,.10);
  --accent: #3987e5; --accent-soft: #1c3656; --planned: #184f95; --pos: #0ca30c; --neg: #e66767; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font); font-size: 15px; line-height: 1.4;
  background: var(--page); color: var(--ink);
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
.num, td.num, .amount { font-variant-numeric: tabular-nums; white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }

/* ---------- Layout ---------- */
#app { display: grid; grid-template-columns: minmax(0, 1fr); min-height: 100vh; }
main, .card, .stack > * { min-width: 0; }
header.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
header.top .title { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sync-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); border: 0; cursor: pointer;
}
.sync-pill .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); }
.sync-pill.offline .dot { background: var(--muted); }
.sync-pill.pending .dot { background: var(--warn); }
.sync-pill.error .dot { background: var(--crit); }
.sync-pill.syncing .dot { background: var(--accent); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .3; } }

main { padding: 16px; padding-bottom: 96px; max-width: 1100px; width: 100%; margin: 0 auto; }

nav.tabs {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
nav.tabs a {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 4px; font-size: 11px; color: var(--muted); text-decoration: none;
}
nav.tabs a svg { width: 22px; height: 22px; }
nav.tabs a.active { color: var(--accent); }

@media (min-width: 900px) {
  #app { grid-template-columns: 220px minmax(0, 1fr); }
  #app > header.top { grid-column: 1 / -1; }
  nav.tabs {
    position: sticky; top: 53px; height: calc(100vh - 53px); align-self: start;
    display: flex; flex-direction: column; gap: 2px; padding: 12px 8px;
    border-top: 0; border-right: 1px solid var(--border);
  }
  nav.tabs a { flex-direction: row; gap: 12px; font-size: 14px; padding: 10px 12px; border-radius: 8px; }
  nav.tabs a.active { background: var(--accent-soft); }
  main { padding: 24px; padding-bottom: 40px; }
}

.fab {
  position: fixed; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 25;
  width: 56px; height: 56px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-ink); font-size: 28px; line-height: 1;
  box-shadow: 0 4px 14px rgba(0,0,0,.25); cursor: pointer;
}
@media (min-width: 900px) { .fab { bottom: 24px; right: 24px; } }

/* ---------- Components ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; box-shadow: var(--shadow);
}
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid { display: grid; gap: 12px; }
.grid.tiles { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.grid.two { grid-template-columns: 1fr; }
@media (min-width: 760px) { .grid.two { grid-template-columns: 1fr 1fr; } }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 14px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 500; text-decoration: none; color: var(--ink);
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger { color: var(--neg); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn { border: 0; background: transparent; padding: 6px 10px; cursor: pointer; font-size: 18px; color: var(--ink-2); border-radius: 8px; }
.icon-btn:hover { background: var(--surface-2); }

label.field { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--ink-2); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=search], select, textarea {
  width: 100%; padding: 10px 12px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface); color: var(--ink); font-size: 16px;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); }

.seg { display: inline-grid; grid-auto-flow: column; background: var(--surface-2); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; cursor: pointer; font-size: 14px; color: var(--ink-2); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); font-weight: 600; }

.chip { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); }
.chip.planned { background: var(--accent-soft); color: var(--ink); }
.chip.warn { background: #fdefc8; color: #5c4200; }
:root[data-theme="dark"] .chip.warn { background: #4a3900; color: #ffe29a; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .chip.warn { background: #4a3900; color: #ffe29a; } }

/* Month navigation */
.monthnav { display: flex; align-items: center; gap: 4px; }
.monthnav .label { min-width: 130px; text-align: center; font-weight: 600; text-transform: capitalize; }

/* ---------- Transactions list ---------- */
.filters { display: flex; gap: 8px; flex-wrap: wrap; }
.filters > * { flex: 1 1 140px; }
.daygroup h4 { margin: 14px 4px 6px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: capitalize; }
.txlist { list-style: none; margin: 0; padding: 0; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.tx { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; padding: 10px 14px; border-top: 1px solid var(--border); cursor: pointer; }
.tx:first-child { border-top: 0; }
.tx:hover { background: var(--surface-2); }
.tx .lbl { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tx .meta { font-size: 12px; color: var(--muted); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.tx .amt { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; }
.tx.planned .lbl { font-style: italic; color: var(--ink-2); }
.tx.planned { background: repeating-linear-gradient(135deg, transparent 0 10px, color-mix(in srgb, var(--accent-soft) 35%, transparent) 10px 11px); }
.tx .side { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.summary .k { font-size: 12px; color: var(--muted); }
.summary .v { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: 14px; }
table.data th { text-align: left; font-weight: 600; font-size: 12px; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; }
table.data td { padding: 8px 10px; border-bottom: 1px solid var(--grid); vertical-align: middle; }
table.data th.num, table.data td.num { text-align: right; }
table.data tr.total td { font-weight: 700; border-top: 2px solid var(--axis); }
table.data input[type=number], table.data input[type=text] { max-width: 130px; min-width: 90px; padding: 6px 8px; font-size: 14px; text-align: right; }

.meter { height: 8px; border-radius: 4px; background: var(--accent-soft); overflow: hidden; min-width: 80px; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 4px; }
.meter.over > span { background: var(--crit); }
.meter.near > span { background: var(--warn); }

/* ---------- Accounts ---------- */
.acc-card { display: flex; flex-direction: column; gap: 8px; cursor: pointer; text-decoration: none; color: inherit; }
.acc-card:hover { border-color: var(--accent); }
.acc-card .bal { font-size: 24px; font-weight: 600; }
.kv { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; font-size: 13px; }
.kv .k { color: var(--muted); }
.kv .v { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Dashboard ---------- */
.hero .label { font-size: 13px; color: var(--ink-2); }
.hero .value { font-size: 44px; font-weight: 600; line-height: 1.1; letter-spacing: -0.5px; }
.tile .label { font-size: 12px; color: var(--ink-2); }
.tile .value { font-size: 22px; font-weight: 600; margin-top: 2px; }
.tile .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart text.lbl { fill: var(--ink-2); font-size: 12px; }
.chart text.val { fill: var(--ink); font-size: 12px; font-weight: 600; }
.chart .bar { fill: var(--accent); }
.chart .bar.planned { fill: var(--planned); }
.chart .hit { fill: transparent; cursor: pointer; }
.chart .hit:hover + .bar, .chart .hit:focus + .bar { opacity: .8; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-2); flex-wrap: wrap; }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.tooltip {
  position: fixed; z-index: 100; pointer-events: none;
  background: var(--surface); color: var(--ink); border: 1px solid var(--border); box-shadow: 0 4px 16px rgba(0,0,0,.18);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; min-width: 120px;
}
.tooltip .tv { font-weight: 700; font-size: 14px; font-variant-numeric: tabular-nums; }
.tooltip .tl { color: var(--ink-2); }
.tooltip .key { display: inline-block; width: 12px; height: 2px; margin-right: 6px; vertical-align: middle; }

/* ---------- Dialog ---------- */
dialog.sheet {
  border: 0; padding: 0; border-radius: 16px; width: min(560px, calc(100vw - 24px));
  max-height: calc(100vh - 24px); background: var(--surface); color: var(--ink);
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
dialog.sheet::backdrop { background: rgba(0,0,0,.4); }
dialog.sheet form { display: flex; flex-direction: column; max-height: calc(100vh - 24px); }
dialog.sheet .sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--border); }
dialog.sheet .sheet-body { padding: 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
dialog.sheet .sheet-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--border); flex-wrap: wrap; }
dialog.sheet .sheet-foot .spacer { flex: 1; }
@media (max-width: 600px) {
  dialog.sheet { width: 100vw; max-width: 100vw; margin: auto 0 0 0; border-radius: 16px 16px 0 0; max-height: 92vh; }
  dialog.sheet form { max-height: 92vh; }
}
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.amount-input { display: grid; grid-template-columns: 1fr 90px; gap: 8px; }
.hint { font-size: 12px; color: var(--muted); }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login .card { width: min(420px, 100%); display: flex; flex-direction: column; gap: 12px; }
.login .brand { display: flex; align-items: center; gap: 10px; }
.login .brand img { width: 40px; height: 40px; border-radius: 10px; }
.error { color: var(--neg); font-size: 14px; }
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(90px + env(safe-area-inset-bottom)); z-index: 200;
  background: var(--ink); color: var(--page); padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow);
}
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
details.section > summary { cursor: pointer; font-weight: 600; padding: 4px 0; }
.list-plain { list-style: none; margin: 0; padding: 0; }
.list-plain li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--grid); }
.list-plain li:first-child { border-top: 0; }
.list-plain li .grow { flex: 1; min-width: 0; }
