:root {
  color-scheme: light;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #dde1e7;
  --text: #15181d;
  --text-2: #4d5562;
  --muted: #6b7380;
  --accent: #1d5fae;
  --accent-ink: #ffffff;
  --accent-soft: #e5eefa;
  /* Spreadsheet conditional-format colours (green ≥ 0, red < 0) */
  --good-bg: #d9ead3;
  --good-ink: #1d4d14;
  --bad-bg: #f4cccc;
  --bad-ink: #7a1414;
  /* DATA sheet highlights */
  --missing-bg: #fff2cc;
  --verify-bg: #fce4d6;
  --total-bg: #eef1f5;
  --series-cmp: #2a78d6;
  --series-cur: #eb6834;
  --grid: #e6e8ec;
  --danger: #b42318;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111317;
    --surface: #1a1d22;
    --surface-2: #22262c;
    --border: #2f343c;
    --text: #eef0f3;
    --text-2: #c3c8d0;
    --muted: #9aa2ae;
    --accent: #5b9be6;
    --accent-ink: #0b1220;
    --accent-soft: #1d2a3b;
    --good-bg: #1f3a1c;
    --good-ink: #b9e3ad;
    --bad-bg: #4a1f1f;
    --bad-ink: #f5bcbc;
    --missing-bg: #3d3416;
    --verify-bg: #46291a;
    --total-bg: #23272e;
    --series-cmp: #3987e5;
    --series-cur: #d95926;
    --grid: #2b3038;
    --danger: #f97066;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111317;
  --surface: #1a1d22;
  --surface-2: #22262c;
  --border: #2f343c;
  --text: #eef0f3;
  --text-2: #c3c8d0;
  --muted: #9aa2ae;
  --accent: #5b9be6;
  --accent-ink: #0b1220;
  --accent-soft: #1d2a3b;
  --good-bg: #1f3a1c;
  --good-ink: #b9e3ad;
  --bad-bg: #4a1f1f;
  --bad-ink: #f5bcbc;
  --missing-bg: #3d3416;
  --verify-bg: #46291a;
  --total-bg: #23272e;
  --series-cmp: #3987e5;
  --series-cur: #d95926;
  --grid: #2b3038;
  --danger: #f97066;
  --shadow: none;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.45; -webkit-text-size-adjust: 100%; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
[hidden] { display: none !important; }

/* Top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 10; }
.topbar-inner { max-width: 1200px; margin: 0 auto; padding: 0 16px; display: flex; align-items: center; gap: 24px; min-height: 56px; flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; color: var(--text); text-decoration: none; white-space: nowrap; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs a { padding: 16px 12px; color: var(--text-2); text-decoration: none; font-weight: 550; border-bottom: 3px solid transparent; white-space: nowrap; }
.tabs a:hover { color: var(--text); }
.tabs a[aria-current="page"] { color: var(--accent); border-bottom-color: var(--accent); }
@media (max-width: 760px) {
  .topbar-inner { gap: 0; padding-top: 8px; }
  .tabs { width: 100%; margin: 0 -16px; padding: 0 8px; width: calc(100% + 32px); }
  .tabs a { padding: 12px 10px; }
}

.container { max-width: 1200px; margin: 0 auto; padding: 24px 16px 64px; outline: none; }
.loading { color: var(--muted); }

.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 16px; margin-bottom: 20px; }
.page-head p { margin: 4px 0 0; color: var(--text-2); }

/* Selector bar */
.pickers { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); margin-bottom: 20px; }
.pickers label { display: grid; gap: 4px; font-size: .8rem; font-weight: 600; color: var(--text-2); text-transform: uppercase; letter-spacing: .03em; }
.pickers .vs { padding-bottom: 9px; color: var(--muted); font-weight: 600; }
.swap { margin-left: auto; }
@media (max-width: 560px) { .swap { margin-left: 0; } .pickers label { flex: 1 1 140px; } .pickers select { width: 100%; } .pickers .vs { display: none; } .flag { display: block; margin-left: 0; } }

select, input, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; min-height: 38px;
}
select { padding-right: 28px; }
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { width: 100%; min-height: 80px; resize: vertical; }

.btn { font: inherit; font-weight: 600; border-radius: 8px; padding: 8px 14px; min-height: 38px; border: 1px solid var(--border); background: var(--surface); color: var(--text); cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.link { border: none; background: none; padding: 4px 6px; min-height: 0; color: var(--accent); }
.btn.link.danger { color: var(--danger); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.wide { width: 100%; justify-content: center; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

/* Cards */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 20px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head p { margin: 2px 0 0; color: var(--muted); font-size: .88rem; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 20px; }
@media (max-width: 760px) { .kpis { grid-template-columns: 1fr; } }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.kpi .label { font-size: .8rem; color: var(--text-2); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 1.55rem; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { font-size: .8rem; color: var(--muted); margin-top: 4px; }
.kpi-change { margin-top: 6px; font-size: .9rem; color: var(--text-2); display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.pill { display: inline-block; border-radius: 999px; padding: 1px 8px; font-weight: 650; font-variant-numeric: tabular-nums; }
.pill.good, td.good { background: var(--good-bg); color: var(--good-ink); }
.pill.bad, td.bad { background: var(--bad-bg); color: var(--bad-ink); }

/* Tables */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 8px; -webkit-overflow-scrolling: touch; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 9px 12px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--border); }
th { background: var(--surface-2); font-size: .78rem; font-weight: 650; color: var(--text-2); text-transform: uppercase; letter-spacing: .02em; vertical-align: bottom; }
th:first-child, td:first-child { text-align: left; position: sticky; left: 0; background: inherit; z-index: 1; }
thead th:first-child { background: var(--surface-2); }
tbody tr { background: var(--surface); }
tbody tr:hover { background: var(--surface-2); }
tr.total { background: var(--total-bg); font-weight: 700; }
tr.total td { border-top: 2px solid var(--border); border-bottom: none; }
td.blank { color: var(--muted); }
th .grp { display: block; font-weight: 500; text-transform: none; color: var(--muted); letter-spacing: 0; }
td.text { text-align: left; white-space: normal; min-width: 160px; }
td.actions { text-align: right; }
td.col-source { min-width: 220px; max-width: 260px; }
td.col-notes { min-width: 300px; max-width: 380px; }
.clamp { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
tr.missing td { background: var(--missing-bg); }
td.verify { background: var(--verify-bg) !important; }
.flag { color: var(--muted); font-size: .78rem; margin-left: 6px; font-weight: 500; }

.legend-row { display: flex; flex-wrap: wrap; gap: 16px; margin-top: 10px; font-size: .82rem; color: var(--text-2); }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; vertical-align: -1px; margin-right: 6px; border: 1px solid var(--border); }

.note { background: var(--accent-soft); border-radius: 8px; padding: 12px 14px; color: var(--text); white-space: pre-line; font-size: .9rem; margin-bottom: 20px; }
.note strong { display: block; }

.chart-box { position: relative; height: 340px; }
@media (max-width: 560px) { .chart-box { height: 280px; } }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 900px) { .grid-2 { grid-template-columns: 1fr; } }

/* Data page filters */
.filters { display: flex; flex-wrap: wrap; gap: 10px; align-items: end; margin-bottom: 14px; }
.filters label { display: grid; gap: 4px; font-size: .8rem; font-weight: 600; color: var(--text-2); }
.filters .grow { flex: 1 1 200px; }
.filters .grow input { width: 100%; }
.count { color: var(--muted); font-size: .88rem; margin-left: auto; padding-bottom: 8px; }

/* Dialog */
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--surface); color: var(--text); width: min(820px, calc(100vw - 24px)); max-height: calc(100vh - 32px); box-shadow: 0 20px 50px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(10, 14, 20, .45); }
.dlg-head { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.dlg-body { padding: 16px 20px; overflow: auto; max-height: calc(100vh - 190px); }
.dlg-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.dlg-foot .error { margin-right: auto; }
.field-row { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.field-row label { display: grid; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--text-2); }
.entry-table input { width: 100%; min-width: 110px; text-align: right; }
.entry-table td { padding: 6px 8px; }
.entry-table td:first-child { font-weight: 600; min-width: 150px; }
.entry-table .extra td { padding-top: 0; background: var(--surface); }
.entry-table .extra input { text-align: left; }
.entry-extra { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (max-width: 640px) { .entry-extra { grid-template-columns: 1fr; } }
.input-wrap { position: relative; display: block; }
.input-wrap .unit { position: absolute; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; font-size: .9rem; }
.input-wrap .unit.pre { left: 10px; }
.input-wrap .unit.post { right: 10px; }
.input-wrap input.has-pre { padding-left: 22px; }
.input-wrap input.has-post { padding-right: 26px; }
.hint { font-size: .78rem; color: var(--muted); }
.fill-profit { font-size: .78rem; }
.error { color: var(--danger); font-weight: 600; margin: 0; }

/* Import preview */
.summary-stats { display: flex; gap: 12px; flex-wrap: wrap; margin-bottom: 12px; }
.stat { background: var(--surface-2); border-radius: 8px; padding: 8px 12px; }
.stat b { font-size: 1.2rem; display: block; }
.diff-old { color: var(--muted); text-decoration: line-through; margin-right: 6px; }

/* Settings */
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 14px; }
.chip { display: inline-flex; align-items: center; gap: 4px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 6px 4px 12px; font-size: .9rem; }
.chip .x { border: none; background: none; color: var(--muted); cursor: pointer; font-size: 1rem; line-height: 1; padding: 2px 6px; border-radius: 999px; }
.chip .x:hover { background: var(--bad-bg); color: var(--bad-ink); }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; }
.help h3 { margin: 18px 0 6px; font-size: 1rem; }
.help p, .help li { color: var(--text-2); }
.help ul { padding-left: 20px; }
.history-list td { vertical-align: top; }

/* Toast */
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; font-weight: 600; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: calc(100vw - 32px); }
.toast.bad { background: var(--danger); color: #fff; }

/* Login */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow); padding: 28px; width: min(400px, 100%); text-align: center; }
.login-card h1 { font-size: 1.3rem; margin-top: 12px; }
.login-card form { display: grid; gap: 14px; margin-top: 20px; text-align: left; }
.login-card label { display: grid; gap: 6px; font-weight: 600; font-size: .9rem; }
.login-card label .muted { font-weight: 400; font-size: .8rem; }

@media print {
  .topbar, .pickers .swap, .btn, .filters { display: none !important; }
  body { background: #fff; }
  .card, .kpi { box-shadow: none; break-inside: avoid; }
}
