:root {
    color-scheme: light;
    --bg: #f4f4f2;
    --surface: #fcfcfb;
    --surface-2: #f0efec;
    --surface-3: #e6e5e1;
    --border: #dddcd7;
    --text: #0b0b0b;
    --text-2: #52514e;
    --text-3: #807f7a;
    --accent: #2a78d6;
    --accent-soft: #e3eefc;
    --neg: #c8302f;
    --pos: #0f7a3c;
    --series-1: #2a78d6;
    --series-2: #eb6834;
    --grid: #e6e5e1;
    --current: #fff6dc;
    --past: #f7f7f5;
}
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        color-scheme: dark;
        --bg: #121211;
        --surface: #1a1a19;
        --surface-2: #232321;
        --surface-3: #2d2d2a;
        --border: #34342f;
        --text: #f0efec;
        --text-2: #c3c2b7;
        --text-3: #8f8e86;
        --accent: #3987e5;
        --accent-soft: #1b2b40;
        --neg: #f07a79;
        --pos: #5ac58a;
        --series-1: #3987e5;
        --series-2: #d95926;
        --grid: #2d2d2a;
        --current: #2e2a1c;
        --past: #1e1e1c;
    }
}
:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --surface-3: #2d2d2a;
    --border: #34342f;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #3987e5;
    --accent-soft: #1b2b40;
    --neg: #f07a79;
    --pos: #5ac58a;
    --series-1: #3987e5;
    --series-2: #d95926;
    --grid: #2d2d2a;
    --current: #2e2a1c;
    --past: #1e1e1c;
}

* { box-sizing: border-box; }
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-variant-numeric: tabular-nums;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; font-weight: 600; }
h2 { font-size: 15px; margin: 0 0 12px; font-weight: 600; }
.muted { color: var(--text-3); }
.neg { color: var(--neg); }
.pos { color: var(--pos); }

.topbar {
    display: flex; align-items: center; gap: 24px;
    padding: 0 24px; height: 52px;
    background: var(--surface); border-bottom: 1px solid var(--border);
    position: sticky; top: 0; z-index: 20;
}
.brand { font-weight: 700; color: var(--text); font-size: 16px; }
.topbar nav { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
.topbar nav a { color: var(--text-2); padding: 6px 12px; border-radius: 6px; white-space: nowrap; }
.topbar nav a:hover { background: var(--surface-2); text-decoration: none; }
.topbar nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.logout { color: var(--text-3); font-size: 13px; }

main { padding: 24px; max-width: 1600px; margin: 0 auto; }
.page-head { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head .spacer { flex: 1; }

.card {
    background: var(--surface); border: 1px solid var(--border);
    border-radius: 10px; padding: 18px;
}
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; margin-bottom: 16px; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; margin-bottom: 16px; }
.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 600; margin-top: 4px; }
.tile .sub { color: var(--text-3); font-size: 12px; margin-top: 4px; }
.progress { height: 6px; background: var(--surface-3); border-radius: 3px; margin-top: 10px; overflow: hidden; }
.progress > span { display: block; height: 100%; background: var(--series-1); border-radius: 3px; }
.chart-box { position: relative; height: 280px; }

button, .btn, select, input[type=text], input[type=number], input[type=password], textarea {
    font: inherit; color: var(--text);
    background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
    padding: 6px 10px;
}
button, .btn { cursor: pointer; background: var(--surface-2); }
button:hover, .btn:hover { background: var(--surface-3); text-decoration: none; }
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.small { padding: 2px 8px; font-size: 12px; }
button.link { background: none; border: none; color: var(--text-3); padding: 0 4px; }
button.link:hover { color: var(--accent); }
.inline-form { display: inline-flex; gap: 8px; align-items: center; flex-wrap: wrap; }

/* Data tables */
table.data { border-collapse: collapse; width: 100%; }
table.data th, table.data td { padding: 6px 10px; border-bottom: 1px solid var(--grid); text-align: right; white-space: nowrap; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data thead th { color: var(--text-2); font-weight: 600; font-size: 12px; background: var(--surface); position: sticky; top: 0; }
table.data tr:hover td { background: var(--surface-2); }
table.data tfoot td { font-weight: 600; border-top: 1px solid var(--border); }
.scroll { overflow: auto; }

/* Year grid */
.sheet-wrap { overflow: auto; max-height: calc(100vh - 170px); border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
table.sheet { border-collapse: separate; border-spacing: 0; min-width: 100%; }
table.sheet th, table.sheet td { border-bottom: 1px solid var(--grid); padding: 0; height: 30px; white-space: nowrap; }
table.sheet thead th {
    position: sticky; top: 0; z-index: 3; background: var(--surface-2);
    font-size: 12px; color: var(--text-2); padding: 0 8px; text-align: right; border-bottom: 1px solid var(--border);
}
table.sheet .label {
    position: sticky; left: 0; z-index: 2; background: var(--surface);
    text-align: left; padding: 0 8px 0 12px; min-width: 230px; border-right: 1px solid var(--border);
}
table.sheet thead th.label { z-index: 4; background: var(--surface-2); }
table.sheet td.num { text-align: right; padding: 0 8px; min-width: 84px; }
table.sheet td.cell { min-width: 84px; }
table.sheet td.cell input {
    width: 100%; height: 29px; border: 0; border-radius: 0; background: transparent;
    text-align: right; padding: 0 8px; font-variant-numeric: tabular-nums;
}
table.sheet td.cell input:focus { outline: 2px solid var(--accent); outline-offset: -2px; background: var(--surface); }
table.sheet td.cell input.neg { color: var(--neg); }
table.sheet td.cell input.saving { color: var(--text-3); }
table.sheet td.cell input.error { outline: 2px solid var(--neg); }
table.sheet .past { background: var(--past); }
table.sheet .current { background: var(--current); }
table.sheet thead th.current { background: var(--current); color: var(--text); }
table.sheet tr.section th, table.sheet tr.section td {
    background: var(--surface-2); font-weight: 700; font-size: 12px; letter-spacing: .03em; text-transform: uppercase;
    color: var(--text-2); height: 32px;
}
table.sheet tr.section .label { background: var(--surface-2); }
table.sheet tr.total td { font-weight: 600; background: var(--surface-2); }
table.sheet tr.total .label { background: var(--surface-2); }
table.sheet tr.key td { font-weight: 700; background: var(--accent-soft); }
table.sheet tr.key .label { background: var(--accent-soft); }
table.sheet tr.gap td { height: 10px; border-bottom: 0; background: var(--bg); }
table.sheet tr.gap .label { background: var(--bg); }
table.sheet .row-tools {
    position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
    display: none; background: inherit; padding-left: 6px; text-transform: none; letter-spacing: 0; font-weight: 400;
}
table.sheet tr:hover .row-tools { display: inline-flex; }
table.sheet .cat-name { cursor: text; }
table.sheet td.avg { color: var(--text-2); border-left: 1px solid var(--border); }

.legend { display: flex; gap: 16px; font-size: 12px; color: var(--text-2); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 6px; vertical-align: -1px; }

/* Heatmap */
table.heat td.h { min-width: 64px; }

/* Reconcile */
.recon { max-width: 720px; }
.recon table.data td input { width: 160px; text-align: right; }
.summary-row { display: flex; justify-content: space-between; padding: 6px 0; }
.summary-row.big { font-size: 18px; font-weight: 700; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; }

/* Login */
.login { max-width: 340px; margin: 12vh auto; }
.login input { width: 100%; margin: 12px 0; }
.flash { padding: 10px 14px; border-radius: 8px; background: var(--accent-soft); margin-bottom: 16px; }
.flash.err { background: color-mix(in srgb, var(--neg) 15%, var(--surface)); }

.toast {
    position: fixed; bottom: 20px; right: 20px; padding: 10px 16px; border-radius: 8px;
    background: var(--text); color: var(--surface); opacity: 0; transition: opacity .2s; pointer-events: none;
}
.toast.show { opacity: .92; }

@media (max-width: 700px) {
    main { padding: 16px; }
    .topbar { padding: 0 16px; gap: 12px; }
    .grid-2 { grid-template-columns: 1fr; }
    table.sheet .label { min-width: 150px; }
}
