:root {
  color-scheme: light;
  --surface-0: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #efeeea;
  --border: #dddcd6;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #77766f;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --grid: #e6e5e0;
  --good: #0ca30c;
  --good-ink: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --warn-bg: #fff4d6;
  --crit-bg: #fde8e6;
  --info-bg: #e6f0fc;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --surface-0: #121211;
    --surface-1: #1a1a19;
    --surface-2: #262624;
    --border: #383835;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #9a9990;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --series-1: #3987e5;
    --series-2: #d95926;
    --grid: #2e2e2b;
    --good-ink: #5fd35f;
    --warn-bg: #3a2f10;
    --crit-bg: #3d1a19;
    --info-bg: #182a40;
  }
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--surface-0); color: var(--text-primary); }
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 24px 0 8px; }
h3 { font-size: 14px; margin: 16px 0 6px; }
p { margin: 4px 0; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
.small { font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.topbar { display: flex; align-items: center; gap: 24px; padding: 10px 24px; background: var(--surface-1); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.brand { font-weight: 700; color: var(--text-primary); font-size: 16px; }
nav { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
nav a { padding: 6px 12px; border-radius: 6px; color: var(--text-secondary); }
nav a.active { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
nav a:hover { text-decoration: none; background: var(--surface-2); }
main { padding: 20px 24px 60px; max-width: 1500px; margin: 0 auto; }

.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 16px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.spread { justify-content: space-between; }
.grow { flex: 1; }
.stack > * + * { margin-top: 8px; }

button, .button { font: inherit; padding: 6px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary); cursor: pointer; display: inline-block; }
button:hover, .button:hover { background: var(--surface-2); text-decoration: none; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--critical); }
button.link { border: none; background: none; color: var(--text-secondary); padding: 6px 8px; }
button:disabled { opacity: .5; cursor: default; }

input, select, textarea { font: inherit; padding: 5px 8px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface-1); color: var(--text-primary); min-width: 0; }
input[type="number"] { width: 110px; text-align: right; }
input.pct { width: 80px; }
textarea { width: 100%; min-height: 60px; resize: vertical; }
label.field { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--text-secondary); }
label.check { display: inline-flex; gap: 6px; align-items: center; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 10px 14px; }

table { border-collapse: collapse; width: 100%; background: var(--surface-1); }
th, td { padding: 6px 8px; border-bottom: 1px solid var(--grid); text-align: left; vertical-align: top; }
th { font-size: 12px; color: var(--text-secondary); font-weight: 600; background: var(--surface-2); position: sticky; top: 0; }
tbody tr:hover { background: var(--surface-2); }
tr.total td { font-weight: 700; border-top: 2px solid var(--border); }
tr.clickable { cursor: pointer; }
tr.sub td { background: var(--surface-0); }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); }
td.name { white-space: nowrap; font-weight: 600; }
td button { white-space: nowrap; }
td.wide { min-width: 280px; width: 40%; }
td input[type="text"] { width: 100%; }
tr.sub:hover { background: none; }

.badge { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 11px; font-weight: 600; background: var(--surface-2); color: var(--text-secondary); white-space: nowrap; }
.badge.final, .badge.decided { background: var(--info-bg); color: var(--accent-ink); }
.badge.zero { background: var(--crit-bg); color: var(--critical); }
.badge.draft, .badge.planned { background: var(--warn-bg); color: var(--text-primary); }
.delta { font-size: 11px; color: var(--text-secondary); display: block; }
.delta::before { content: "≠ "; }
.note { display: block; font-size: 11px; color: var(--text-muted); }
.warn { background: var(--warn-bg); border-radius: 6px; padding: 8px 10px; }
.error { background: var(--crit-bg); border-radius: 6px; padding: 8px 10px; }
.ok { color: var(--good-ink); }

.chips { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0 16px; }
.chip { padding: 4px 10px; border: 1px solid var(--border); border-radius: 14px; background: var(--surface-1); color: var(--text-secondary); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip:hover { text-decoration: none; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 12px; margin: 12px 0; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.tile .label { font-size: 12px; color: var(--text-secondary); }
.tile .value { font-size: 20px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { font-size: 12px; color: var(--text-muted); }

.chart { width: 100%; overflow-x: auto; }
.chart svg { display: block; }
.legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-secondary); margin-bottom: 6px; }
.legend .sw { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }
.sw.s1 { background: var(--series-1); }
.sw.s2 { background: var(--series-2); }
.tooltip { position: fixed; pointer-events: none; background: var(--surface-1); border: 1px solid var(--border); border-radius: 6px; padding: 8px 10px; font-size: 12px; box-shadow: 0 4px 14px rgb(0 0 0 / .15); z-index: 20; min-width: 160px; }
.tooltip .tt-row { display: flex; justify-content: space-between; gap: 16px; }

dialog { border: 1px solid var(--border); border-radius: 10px; padding: 0; background: var(--surface-1); color: var(--text-primary); width: min(980px, 96vw); max-height: 92vh; }
dialog::backdrop { background: rgb(0 0 0 / .45); }
dialog form { padding: 18px 20px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; position: sticky; bottom: -18px; background: var(--surface-1); padding: 10px 0; }

#toast { position: fixed; bottom: 20px; right: 20px; background: var(--text-primary); color: var(--surface-1); padding: 10px 16px; border-radius: 8px; z-index: 30; }
.login { max-width: 360px; margin: 12vh auto; }
.formula-text { white-space: pre-wrap; }
.preview { background: var(--surface-0); border-radius: var(--radius); padding: 10px 12px; }

@media (max-width: 640px) {
  main { padding: 12px 16px 40px; }
  .topbar { padding: 10px 16px; gap: 8px; }
}
