:root {
  color-scheme: light;
  --bg: #f7f4ef;
  --surface: #fffdf9;
  --surface-2: #f3eee6;
  --ink: #1f1a16;
  --ink-2: #5b524a;
  --muted: #857c72;
  --line: #e7e0d6;
  --accent: #6b4226;
  --accent-2: #8a5a36;
  --accent-ink: #fff;
  --accent-soft: #f1e4d6;
  --good: #146c2e;
  --good-soft: #e3f3e6;
  --bad: #b3261e;
  --bad-soft: #fbe6e4;
  --warn: #8a5a00;
  --warn-soft: #fdf1d8;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --grid: #e1e0d9;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(31, 26, 22, .05), 0 4px 16px rgba(31, 26, 22, .04);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #12100e; --surface: #1c1916; --surface-2: #26221e; --ink: #f5efe8; --ink-2: #cfc5b9; --muted: #968c80;
    --line: #322c26; --accent: #d39a6a; --accent-2: #e2b088; --accent-ink: #1c1208; --accent-soft: #3a2a1d;
    --good: #4cc46c; --good-soft: #17301e; --bad: #f07a6f; --bad-soft: #3a1c19; --warn: #e8b44c; --warn-soft: #362a12;
    --series-1: #3987e5; --series-2: #d95926; --grid: #2c2c2a; --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #12100e; --surface: #1c1916; --surface-2: #26221e; --ink: #f5efe8; --ink-2: #cfc5b9; --muted: #968c80;
  --line: #322c26; --accent: #d39a6a; --accent-2: #e2b088; --accent-ink: #1c1208; --accent-soft: #3a2a1d;
  --good: #4cc46c; --good-soft: #17301e; --bad: #f07a6f; --bad-soft: #3a1c19; --warn: #e8b44c; --warn-soft: #362a12;
  --series-1: #3987e5; --series-2: #d95926; --grid: #2c2c2a; --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.45; -webkit-font-smoothing: antialiased; }
h1, h2, h3 { margin: 0 0 .6rem; line-height: 1.2; letter-spacing: -.01em; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .6rem; }
a { color: var(--accent); }
.muted { color: var(--muted); font-size: .88rem; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.boot { padding: 2rem; color: var(--muted); }
[hidden] { display: none !important; }

/* ---------- Layout ---------- */
.shell { display: grid; grid-template-columns: 220px minmax(0, 1fr); min-height: 100vh; }
.side { background: var(--surface); border-right: 1px solid var(--line); padding: 1.1rem .8rem; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; gap: .2rem; }
.brand { display: flex; align-items: center; gap: .55rem; font-weight: 700; font-size: 1.1rem; padding: 0 .5rem .9rem; }
.brand img { width: 30px; height: 30px; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a { display: flex; align-items: center; gap: .6rem; padding: .55rem .7rem; border-radius: 10px; color: var(--ink-2); text-decoration: none; font-weight: 500; white-space: nowrap; }
.nav a svg { width: 18px; height: 18px; flex: none; }
.nav a:hover { background: var(--surface-2); color: var(--ink); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 650; }
.main { padding: 1.4rem clamp(16px, 3vw, 2rem) 4rem; max-width: 1180px; width: 100%; min-width: 0; }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: .75rem; margin-bottom: 1rem; }
.page-head p { margin: 0; }

@media (max-width: 860px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
  .side { position: sticky; top: 0; z-index: 10; height: auto; min-width: 0; flex-direction: row; align-items: center; padding: .5rem 0 .5rem 12px; border-right: 0; border-bottom: 1px solid var(--line); gap: .4rem; }
  .brand { padding: 0 .3rem; font-size: 1rem; }
  .brand span { display: none; }
  .nav { flex-direction: row; overflow-x: auto; scrollbar-width: none; padding-right: 12px; }
  .nav::-webkit-scrollbar { display: none; }
  .nav a { padding: .45rem .7rem; font-size: .9rem; }
  .nav a svg { display: none; }
  .main { padding: 1rem 16px 4rem; }
}

/* ---------- Cards & grids ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.2rem; margin-bottom: 1rem; box-shadow: var(--shadow); min-width: 0; }
.grid { display: grid; gap: 1rem; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(min(100%, 380px), 1fr)); }
.grid > .card { margin-bottom: 0; }
.stack > * + * { margin-top: 1rem; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: .85rem 1rem; box-shadow: var(--shadow); }
.kpi .label { color: var(--muted); font-size: .8rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.kpi .value { font-size: 1.45rem; font-weight: 700; margin-top: .2rem; }
.kpi .sub { color: var(--ink-2); font-size: .82rem; margin-top: .15rem; }
.pos { color: var(--good); }
.neg { color: var(--bad); }

/* ---------- Break-even ---------- */
.be-top { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 1rem; align-items: start; }
.be-figure { font-size: 2rem; font-weight: 750; letter-spacing: -.02em; }
.be-figure small { font-size: .95rem; color: var(--muted); font-weight: 500; }
.progress { height: 14px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin: .9rem 0 .4rem; border: 1px solid var(--line); }
.progress > div { height: 100%; background: var(--accent); border-radius: 99px; transition: width .6s ease; }
.progress.done > div { background: var(--good); }
.progress-labels { display: flex; justify-content: space-between; font-size: .85rem; color: var(--ink-2); gap: .5rem; }
.facts { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .6rem; margin-top: 1rem; }
.fact { background: var(--surface-2); border-radius: 10px; padding: .65rem .8rem; }
.fact b { display: block; font-size: 1.1rem; }
.fact span { font-size: .8rem; color: var(--muted); }
.callout { border-radius: 10px; padding: .7rem .9rem; margin-top: .9rem; font-weight: 500; display: flex; gap: .5rem; align-items: start; }
.callout.good { background: var(--good-soft); color: var(--good); }
.callout.bad { background: var(--bad-soft); color: var(--bad); }
.callout.warn { background: var(--warn-soft); color: var(--warn); }
.callout.info { background: var(--accent-soft); color: var(--accent); }

/* Spend breakdown bars */
.bars { display: flex; flex-direction: column; gap: .6rem; }
.bar-row { display: grid; grid-template-columns: 110px 1fr auto; align-items: center; gap: .6rem; font-size: .9rem; }
.bar-track { height: 10px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--series-2); border-radius: 99px; }
.bar-row .amt { font-variant-numeric: tabular-nums; font-weight: 600; min-width: 90px; text-align: right; }

.chart-box { position: relative; height: 260px; }
.legend { display: flex; gap: 1rem; font-size: .85rem; color: var(--ink-2); margin-bottom: .4rem; flex-wrap: wrap; }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: .35rem; }

/* ---------- Controls ---------- */
.btn { font: inherit; font-weight: 600; border-radius: 10px; padding: .6rem 1rem; border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer; min-height: 40px; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--ink-2); }
.btn.ghost:hover { background: var(--surface-2); color: var(--ink); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: .3rem .6rem; min-height: 32px; font-size: .85rem; }
.btn:disabled { opacity: .6; cursor: progress; }
.btn:focus-visible, input:focus-visible, select:focus-visible, .nav a:focus-visible, .seg button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.seg { display: inline-flex; background: var(--surface-2); border-radius: 11px; padding: 3px; gap: 2px; flex-wrap: wrap; }
.seg button { font: inherit; font-size: .88rem; font-weight: 600; border: 0; background: transparent; color: var(--ink-2); padding: .4rem .75rem; border-radius: 8px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.range { display: flex; gap: .5rem; flex-wrap: wrap; align-items: center; margin-top: .5rem; }

.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 160px), 1fr)); gap: .75rem; align-items: end; }
.field { display: flex; flex-direction: column; gap: .3rem; font-size: .85rem; font-weight: 600; color: var(--ink-2); min-width: 0; }
.field.wide { grid-column: 1 / -1; }
.field small { font-weight: 400; color: var(--muted); }
.field.check { flex-direction: row; align-items: center; gap: .5rem; min-height: 42px; }
.field.check input { width: 20px; height: 20px; accent-color: var(--accent); }
input, select { font: inherit; font-size: 1rem; color: var(--ink); background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: .55rem .7rem; min-height: 42px; width: 100%; min-width: 0; }
input:focus, select:focus { border-color: var(--accent); }
label.inline { display: inline-flex; align-items: center; gap: .4rem; font-size: .85rem; color: var(--ink-2); font-weight: 600; }
label.inline input { width: auto; min-height: 36px; padding: .3rem .5rem; }
.form-actions { display: flex; gap: .5rem; grid-column: 1 / -1; flex-wrap: wrap; }
.chips { display: flex; gap: .4rem; flex-wrap: wrap; grid-column: 1 / -1; }
.chip { font: inherit; font-size: .85rem; border: 1px solid var(--line); background: var(--surface-2); color: var(--ink-2); padding: .3rem .7rem; border-radius: 99px; cursor: pointer; }
.chip:hover { border-color: var(--accent); color: var(--accent); }

.list-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; flex-wrap: wrap; margin-bottom: .6rem; }
.list-head h3 { margin: 0; }
.summary:empty { display: none; }
.summary { display: flex; flex-wrap: wrap; gap: .5rem 1.25rem; margin-bottom: .8rem; font-size: .9rem; color: var(--ink-2); }
.summary b { color: var(--ink); }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th { text-align: left; font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 650; padding: .5rem .6rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: .55rem .6rem; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.actions, th.actions { text-align: right; white-space: nowrap; width: 1%; }
tr.low td { background: var(--warn-soft); }
tr.selected td { background: var(--accent-soft); }
tr.clickable { cursor: pointer; }
.empty { color: var(--muted); padding: .8rem 0; margin: 0; }
.badge { display: inline-block; font-size: .75rem; font-weight: 700; padding: .12rem .5rem; border-radius: 99px; white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.neutral { background: var(--surface-2); color: var(--ink-2); }

/* Mobile: tables become stacked cards */
@media (max-width: 640px) {
  .table-wrap table, .table-wrap thead, .table-wrap tbody, .table-wrap tr, .table-wrap td { display: block; }
  .table-wrap thead { display: none; }
  .table-wrap tr { border: 1px solid var(--line); border-radius: 12px; padding: .4rem .7rem; margin-bottom: .6rem; }
  .table-wrap td { border: 0; padding: .25rem 0; display: flex; justify-content: space-between; gap: 1rem; text-align: right; }
  .table-wrap td::before { content: attr(data-label); color: var(--muted); font-size: .8rem; font-weight: 600; text-align: left; }
  .table-wrap td.actions { justify-content: flex-end; width: auto; padding-top: .4rem; }
  .table-wrap td.actions::before { content: none; }
  .bar-row { grid-template-columns: 90px 1fr auto; }
  .be-figure { font-size: 1.6rem; }
}

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 340px; text-align: center; }
.login img { width: 60px; height: 60px; margin-bottom: .6rem; }
.login input { text-align: center; font-size: 1.4rem; letter-spacing: .4em; margin: .8rem 0; }
.login .btn { width: 100%; }
.login .error { color: var(--bad); min-height: 1.3em; font-size: .9rem; margin-top: .6rem; }

/* ---------- Toast ---------- */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); opacity: 0; background: var(--ink); color: var(--bg); padding: .65rem 1.1rem; border-radius: 10px; font-weight: 600; z-index: 100; transition: .25s; max-width: calc(100% - 32px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { background: var(--bad); color: #fff; }

.steps { margin: 0; padding-left: 1.2rem; }
.steps li { margin: .3rem 0; }
.steps li.done { color: var(--muted); text-decoration: line-through; }
