:root {
  color-scheme: light;
  --bg: #f4f5f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e1e0d9;
  --border-strong: #c3c2b7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6f6d68;
  --accent: #1c5cab;
  --accent-ink: #ffffff;
  --accent-soft: #cde2fb;
  --pos: #006300;
  --neg: #b42323;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-1-track: #cde2fb;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --shadow: 0 1px 2px rgba(11, 11, 11, 0.06), 0 4px 16px rgba(11, 11, 11, 0.06);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: #2c2c2a;
    --border-strong: #383835;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --muted: #9b9991;
    --accent: #3987e5;
    --accent-ink: #ffffff;
    --accent-soft: #184f95;
    --pos: #3fbf3f;
    --neg: #f07a7a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-1-track: #184f95;
    --grid: #2c2c2a;
    --axis: #383835;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --border: #2c2c2a; --border-strong: #383835;
  --text: #f0efec; --text-2: #c3c2b7; --muted: #9b9991; --accent: #3987e5; --accent-soft: #184f95;
  --pos: #3fbf3f; --neg: #f07a7a; --series-1: #3987e5; --series-2: #d95926; --series-1-track: #184f95;
  --grid: #2c2c2a; --axis: #383835; --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); -webkit-font-smoothing: antialiased; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; font-weight: 650; }
h2 { font-size: 17px; margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 15px; margin: 16px 0 8px; font-weight: 600; }
p { margin: 0 0 10px; }
small, .small { font-size: 13px; }
.muted { color: var(--muted); }
.secondary { color: var(--text-2); }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.pos { color: var(--pos); }
.neg { color: var(--neg); }
.boot { padding: 40px; color: var(--muted); }
.hidden { display: none !important; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.spacer { flex: 1; }
.stack > * + * { margin-top: 12px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 250px minmax(0, 1fr); min-height: 100vh; }
.shell > div { min-width: 0; }
.side { background: var(--surface); border-right: 1px solid var(--border); padding: 16px 12px; position: sticky; top: 0; height: 100vh; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; padding: 4px 8px 16px; color: var(--text); }
.brand img { width: 28px; height: 28px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 8px; color: var(--text-2); font-weight: 500; }
.nav a:hover { background: var(--surface-2); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--text); }
.nav-section { font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 10px 6px; font-weight: 600; }
.nav .acct { justify-content: space-between; font-size: 14px; padding: 5px 10px; }
.nav .acct span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav .acct .num { font-size: 13px; }
.main { padding: 24px 28px 60px; min-width: 0; max-width: 1400px; }
.topbar { display: none; }
.page-head { display: flex; align-items: flex-end; gap: 12px; flex-wrap: wrap; margin-bottom: 18px; }
.page-head .spacer { flex: 1; }

/* ---------- cards ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); min-width: 0; }
.card.flush { padding: 0; overflow: hidden; }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 650; margin-top: 2px; }
.hero .value { font-size: 44px; font-weight: 650; letter-spacing: -0.01em; }

/* ---------- forms ---------- */
label { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 4px; font-weight: 500; }
input, select, textarea, button { font: inherit; color: inherit; }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=search], input:not([type]), select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--surface); min-height: 38px;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.field { margin-bottom: 12px; }
.check { display: flex; gap: 8px; align-items: center; font-size: 14px; color: var(--text); font-weight: 400; }
.check input { width: 16px; height: 16px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border-strong); background: var(--surface); cursor: pointer; font-weight: 550; min-height: 38px; white-space: nowrap; }
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--critical); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.link { border: none; background: none; color: var(--accent); padding: 4px 6px; min-height: 0; }
.btn[disabled] { opacity: .55; cursor: default; }
.seg { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: none; background: var(--surface); padding: 6px 12px; cursor: pointer; font-size: 14px; }
.seg button + button { border-left: 1px solid var(--border-strong); }
.seg button.on { background: var(--accent-soft); font-weight: 600; }
.error { color: var(--critical); font-size: 14px; margin: 8px 0; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-weight: 600; color: var(--text-2); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; padding: 8px 10px; border-bottom: 1px solid var(--border-strong); background: var(--surface); position: sticky; top: 0; z-index: 1; }
td { padding: 7px 10px; border-bottom: 1px solid var(--border); vertical-align: top; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
tr.selected td { background: var(--accent-soft); }
tr.future td { color: var(--muted); }
tr.subtotal td { font-weight: 600; background: var(--surface-2); }
tr.total td { font-weight: 700; border-top: 2px solid var(--border-strong); }
td.cat { color: var(--text-2); font-size: 13px; }
td .memo { color: var(--muted); font-size: 13px; }
.clr { width: 28px; text-align: center; cursor: pointer; font-weight: 700; color: var(--muted); }
.clr.c { color: var(--accent); }
.clr.R { color: var(--pos); }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); font-size: 12px; color: var(--text-2); }

/* ---------- register entry ---------- */
.entry { display: grid; grid-template-columns: 140px 80px 1.4fr 1.3fr 110px 110px; gap: 8px; align-items: end; }
.entry .span4 { grid-column: span 4; }
.entry .span2 { grid-column: span 2; }
.entry-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; align-items: center; }
.reconcile-bar { position: sticky; bottom: 12px; margin-top: 12px; background: var(--surface); border: 1px solid var(--border-strong); padding: 12px 16px; z-index: 5; display: flex; gap: 20px; flex-wrap: wrap; align-items: center; }

/* ---------- modal ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: flex-start; justify-content: center; padding: 6vh 16px; z-index: 50; overflow-y: auto; }
.modal { background: var(--surface); border-radius: 12px; padding: 22px; width: min(560px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.25); }
.modal.wide { width: min(860px, 100%); }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--surface); padding: 10px 18px; border-radius: 8px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }

/* ---------- auth ---------- */
.auth { max-width: 420px; margin: 8vh auto; padding: 0 16px; }
.auth .card { padding: 28px; }
.auth .brand { justify-content: center; font-size: 20px; padding-bottom: 20px; }
.qr { width: 200px; height: 200px; margin: 8px auto; background: #fff; padding: 6px; border-radius: 8px; }
.qr svg { width: 100%; height: 100%; display: block; }
.secret { font-family: ui-monospace, Menlo, monospace; font-size: 14px; letter-spacing: .08em; word-break: break-all; background: var(--surface-2); padding: 8px; border-radius: 6px; text-align: center; }
.codes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; font-family: ui-monospace, Menlo, monospace; background: var(--surface-2); padding: 12px; border-radius: 8px; }
.code-input { font-size: 24px !important; letter-spacing: .3em; text-align: center; font-family: ui-monospace, Menlo, monospace; }

/* ---------- insights ---------- */
.insight { display: grid; grid-template-columns: 28px 1fr; gap: 10px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.insight:last-child { border-bottom: none; }
.insight .icon { width: 24px; height: 24px; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 13px; font-weight: 800; color: #fff; }
.insight .icon.good { background: var(--good); }
.insight .icon.tip { background: var(--accent); }
.insight .icon.warn { background: var(--warning); color: #0b0b0b; }
.insight .title { font-weight: 600; }
.insight .tag { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 600; }
.chat { display: flex; flex-direction: column; gap: 10px; max-height: 480px; overflow-y: auto; padding: 4px; }
.bubble { padding: 10px 14px; border-radius: 12px; max-width: 85%; white-space: pre-wrap; }
.bubble.user { background: var(--accent-soft); align-self: flex-end; }
.bubble.assistant { background: var(--surface-2); align-self: flex-start; }

/* ---------- charts ---------- */
.chart { position: relative; }
.chart svg { display: block; width: 100%; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; font-family: var(--font); }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.tooltip { position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border-strong); border-radius: 8px; padding: 8px 10px; font-size: 13px; box-shadow: var(--shadow); white-space: nowrap; z-index: 5; display: none; }
.tooltip .tt-title { font-weight: 600; margin-bottom: 2px; }
.tooltip .key { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-2); margin-bottom: 8px; flex-wrap: wrap; }
.legend .key { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -2px; }
.barlist { display: grid; grid-template-columns: minmax(110px, 38%) 1fr auto; gap: 6px 12px; align-items: center; font-size: 14px; }
.barlist .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.barlist .track { height: 14px; position: relative; }
.barlist .fill { height: 100%; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; }
.barlist .fill.over { background: var(--critical); }
.barlist .val { font-variant-numeric: tabular-nums; color: var(--text-2); text-align: right; }
.meter { height: 8px; border-radius: 4px; background: var(--series-1-track); overflow: hidden; }
.meter > div { height: 100%; background: var(--series-1); border-radius: 4px; }
.meter.warn > div { background: var(--warning); }
.meter.over > div { background: var(--critical); }

/* ---------- weekly plan ---------- */
.plan-week { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 0 12px; padding: 4px 6px 8px; }
.plan-week td, .plan-week th { padding: 5px 8px; }
.plan-week input.amt { width: 96px; min-height: 30px; padding: 3px 8px; text-align: right; font-variant-numeric: tabular-nums; }
.plan-week input.amt.paid { background: var(--surface-2); }
.plan-week .line-name { padding: 0; color: var(--text); font-weight: 500; text-align: left; }
.week.current { outline: 2px solid var(--accent); }
tr.warn-row > td { background: color-mix(in srgb, var(--critical) 8%, transparent); }
tr.detail > td { background: var(--surface-2); padding: 4px 16px 8px; }
tr.detail table td { border-bottom: none; padding: 3px 6px; }

/* ---------- reports & print ---------- */
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--border); margin-bottom: 16px; }
.tabs a { padding: 8px 12px; color: var(--text-2); border-bottom: 2px solid transparent; font-weight: 500; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); }
.tabs a:hover { text-decoration: none; color: var(--text); }
.report-title { display: none; }
.dropzone { border: 2px dashed var(--border-strong); border-radius: var(--radius); padding: 30px; text-align: center; color: var(--text-2); }
.dropzone.drag { border-color: var(--accent); background: var(--accent-soft); }
.progress { height: 10px; border-radius: 5px; background: var(--series-1-track); overflow: hidden; }
.progress > div { height: 100%; background: var(--series-1); width: 0; transition: width .2s; }

@media print {
  :root { color-scheme: light; --bg: #fff; --surface: #fff; --text: #000; --text-2: #222; --muted: #444; --border: #ccc; --border-strong: #888; }
  body { font-size: 11pt; background: #fff; }
  .side, .topbar, .no-print, .tabs, .page-head .btn, .page-head select, .page-head input, .filters { display: none !important; }
  .shell { display: block; }
  .main { padding: 0; max-width: none; }
  .card { box-shadow: none; border: none; padding: 0; }
  .report-title { display: block; margin-bottom: 12px; }
  th { position: static; }
  tr { break-inside: avoid; }
  .week { break-inside: avoid; margin-bottom: 10px; outline: none; }
  .plan-week input.amt { border: none; background: none !important; padding: 0; min-height: 0; }
  a { color: inherit; }
}

/* ---------- phones ---------- */
@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .side { position: fixed; inset: 0 30% 0 0; z-index: 40; transform: translateX(-105%); transition: transform .2s; height: 100%; box-shadow: var(--shadow); }
  .side.open { transform: none; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
  .main { padding: 16px 16px 60px; }
  .entry { grid-template-columns: 1fr 1fr; }
  .entry .span4, .entry .span2 { grid-column: 1 / -1; }
  .grid2 { grid-template-columns: minmax(0, 1fr); }
  .hide-sm { display: none; }
  .hero .value { font-size: 36px; }
  .plan-week { grid-template-columns: minmax(0, 1fr); }
  .plan-week input.amt { width: 80px; }
}
