:root {
  color-scheme: light;
  --bg: #f6f5f1;
  --surface: #fcfcfb;
  --surface-2: #f1efe9;
  --line: #e4e1d8;
  --line-strong: #cfcabd;
  --ink: #16150f;
  --ink-2: #52514e;
  --ink-3: #898781;
  --gold: #a87a1e;
  --gold-soft: #f4ead2;
  --side-bg: #15140f;
  --side-ink: #d9d5c8;
  --side-ink-2: #8d897b;
  --side-active: #262419;
  --pos: #006300;
  --neg: #c02f2f;
  --warn-bg: #fff4d9;
  --warn-ink: #7a5300;
  --s-ka: #2a78d6;
  --s-kl: #eb6834;
  --s-ap: #1baf7a;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --shadow: 0 1px 2px rgba(20, 18, 10, .06), 0 4px 16px rgba(20, 18, 10, .04);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f0f0d; --surface: #1a1a19; --surface-2: #222220; --line: #2c2c2a; --line-strong: #3d3c38;
    --ink: #f0efec; --ink-2: #c3c2b7; --ink-3: #898781; --gold: #d9ac4f; --gold-soft: #2e2716;
    --side-bg: #0b0b0a; --side-active: #1f1d16; --pos: #0ca30c; --neg: #e66767;
    --warn-bg: #2f2610; --warn-ink: #f0c35c;
    --s-ka: #3987e5; --s-kl: #d95926; --s-ap: #199e70; --grid: #2c2c2a; --axis: #383835;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f0f0d; --surface: #1a1a19; --surface-2: #222220; --line: #2c2c2a; --line-strong: #3d3c38;
  --ink: #f0efec; --ink-2: #c3c2b7; --ink-3: #898781; --gold: #d9ac4f; --gold-soft: #2e2716;
  --side-bg: #0b0b0a; --side-active: #1f1d16; --pos: #0ca30c; --neg: #e66767;
  --warn-bg: #2f2610; --warn-ink: #f0c35c;
  --s-ka: #3987e5; --s-kl: #d95926; --s-ap: #199e70; --grid: #2c2c2a; --axis: #383835;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif; }
button, select, input { font: inherit; color: inherit; }
a { color: inherit; }

.shell { display: grid; grid-template-columns: 236px 1fr; min-height: 100vh; }

/* Sidebar */
.side { background: var(--side-bg); color: var(--side-ink); position: sticky; top: 0; height: 100vh; overflow-y: auto; display: flex; flex-direction: column; }
.brand { display: flex; gap: 10px; align-items: center; padding: 18px 18px 14px; }
.mark { width: 34px; height: 34px; border-radius: 8px; background: linear-gradient(135deg, #e3c06a, #a87a1e); color: #1b1608; font-weight: 800; display: grid; place-items: center; letter-spacing: .5px; }
.brand-name { font-weight: 700; color: #fff; }
.brand-sub { font-size: 12px; color: var(--side-ink-2); }
nav { padding: 4px 10px 16px; display: flex; flex-direction: column; gap: 1px; }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--side-ink-2); padding: 14px 10px 6px; }
nav a { display: flex; align-items: center; gap: 10px; padding: 7px 10px; border-radius: 7px; text-decoration: none; font-size: 13.5px; }
nav a:hover { background: var(--side-active); color: #fff; }
nav a.active { background: var(--side-active); color: #fff; box-shadow: inset 2px 0 0 #d9ac4f; }
.ico { width: 18px; text-align: center; color: var(--side-ink-2); font-size: 13px; }
nav a.active .ico { color: #d9ac4f; }
.ico.dot::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.ico.dot.ka { color: #3987e5; } .ico.dot.kl { color: #eb6834; } .ico.dot.ap { color: #1baf7a; }
.side-foot { margin-top: auto; padding: 14px 18px; font-size: 12px; color: var(--side-ink-2); border-top: 1px solid #23221b; }
.side-foot b { color: var(--side-ink); font-weight: 600; }

/* Top bar */
.main { min-width: 0; display: flex; flex-direction: column; }
.top { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 14px 28px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.top h1 { font-size: 19px; margin: 0; letter-spacing: -.01em; }
.top-sub { color: var(--ink-3); font-size: 12.5px; }
.top-title { flex: 1; min-width: 0; }
.top-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ctl { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-3); }
select, input[type=search], input[type=text] { background: var(--surface); border: 1px solid var(--line-strong); border-radius: 7px; padding: 6px 9px; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 10px; border-radius: 6px; cursor: pointer; font-size: 12.5px; color: var(--ink-2); }
.seg button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); font-weight: 600; }
.icon-btn { border: 1px solid var(--line); background: var(--surface); border-radius: 7px; width: 32px; height: 32px; cursor: pointer; }
.menu { display: none; }
.btn { border: 1px solid var(--line-strong); background: var(--surface); border-radius: 7px; padding: 7px 12px; cursor: pointer; font-size: 13px; }
.btn.primary { background: var(--gold); border-color: var(--gold); color: #fff; font-weight: 600; }
.btn:hover { filter: brightness(.97); }

.page { padding: 22px 28px 60px; display: flex; flex-direction: column; gap: 18px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; gap: 12px; padding: 14px 18px 0; flex-wrap: wrap; }
.card-head h2 { font-size: 14.5px; margin: 0; flex: 1; }
.card-body { padding: 14px 18px 18px; }
.muted { color: var(--ink-3); }
.small { font-size: 12px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.kpi { padding: 14px 16px; cursor: pointer; }
.kpi:hover { border-color: var(--line-strong); }
.kpi-label { font-size: 12px; color: var(--ink-2); display: flex; justify-content: space-between; gap: 8px; }
.kpi-value { font-size: 24px; font-weight: 650; letter-spacing: -.02em; margin: 6px 0 4px; }
.kpi-value small { font-size: 13px; font-weight: 500; color: var(--ink-3); margin-left: 3px; }
.kpi-foot { font-size: 12px; color: var(--ink-3); display: flex; gap: 8px; flex-wrap: wrap; }
.delta { font-weight: 600; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--pos); } .delta.down { color: var(--neg); } .delta.flat { color: var(--ink-3); }
.pill { font-size: 11.5px; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); border: 1px solid var(--line); }
.pill.gold { background: var(--gold-soft); color: var(--gold); border-color: transparent; }

/* Tables */
.table-wrap { overflow: auto; max-height: calc(100vh - 210px); border-top: 1px solid var(--line); }
table.t { border-collapse: separate; border-spacing: 0; width: 100%; font-variant-numeric: tabular-nums; font-size: 13px; }
.t th, .t td { padding: 6px 12px; white-space: nowrap; border-bottom: 1px solid var(--line); text-align: right; }
.t th { position: sticky; top: 0; background: var(--surface-2); z-index: 2; font-size: 11.5px; font-weight: 600; color: var(--ink-2); text-transform: uppercase; letter-spacing: .04em; }
.t th:first-child, .t td:first-child { text-align: left; position: sticky; left: 0; background: var(--surface); z-index: 1; min-width: 260px; }
.t th:first-child { z-index: 3; background: var(--surface-2); }
.t tr.section td { background: var(--surface-2); font-weight: 700; font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--gold); cursor: pointer; }
.t tr.section td:first-child { background: var(--surface-2); }
.t tr.total td { font-weight: 650; }
.t tr.total td:first-child { box-shadow: inset 3px 0 0 var(--gold); }
.t tr.pct td { color: var(--ink-2); font-style: italic; font-size: 12.5px; }
.t tr.sub td:first-child { padding-left: 28px; color: var(--ink-2); }
.t tr.memo td { color: var(--ink-3); font-size: 12.5px; }
.t tr.memo td:first-child { padding-left: 40px; }
.t td.num { cursor: pointer; }
.t td.num:hover { background: var(--gold-soft); }
.t td.sel { outline: 2px solid var(--gold); outline-offset: -2px; }
.t th.focus, .t td.focus { background: color-mix(in srgb, var(--gold-soft) 55%, var(--surface)); }
.t th.focus { background: var(--gold-soft); color: var(--gold); }
.t th.grp, .t td.grp { border-left: 1px solid var(--line-strong); }
.neg { color: var(--neg); }
.t td.st-bad { background: color-mix(in srgb, var(--neg) 16%, var(--surface)); color: var(--neg); font-weight: 650; }
.t td.st-warn { background: color-mix(in srgb, #e0a100 18%, var(--surface)); color: var(--warn-ink); font-weight: 600; }
.st-mark { display: inline-grid; place-items: center; width: 15px; height: 15px; border-radius: 50%; font-size: 10.5px; font-weight: 800; font-style: normal; margin-right: 6px; vertical-align: 1px; color: #fff; }
.st-bad .st-mark { background: var(--neg); }
.st-warn .st-mark { background: #c98500; }

/* Strategic read */
.insights { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.ins { display: flex; gap: 10px; padding: 11px 13px; border: 1px solid var(--line); border-left: 3px solid var(--line-strong); background: var(--surface); cursor: default; }
.ins:hover, .ins:focus { border-color: var(--line-strong); outline: none; }
.ins-bad { border-left-color: var(--neg); } .ins-warn { border-left-color: #c98500; } .ins-good { border-left-color: var(--pos); } .ins-info { border-left-color: var(--ink-3); }
.ins-ico { flex: none; width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; font-size: 12px; font-weight: 800; color: #fff; background: var(--ink-3); }
.ins-bad .ins-ico { background: var(--neg); } .ins-warn .ins-ico { background: #c98500; } .ins-good .ins-ico { background: var(--pos); }
.ins-meta { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--ink-3); }
.ins-title { font-weight: 650; margin: 2px 0 3px; }
.ins-body { font-size: 12.5px; color: var(--ink-2); }
.ins-act { font-size: 12.5px; margin-top: 6px; padding-top: 6px; border-top: 1px dashed var(--line); }
.ins-act b { color: var(--gold); font-weight: 650; }
@media (max-width: 1100px) { .insights { grid-template-columns: 1fr; } }
.flag { display: inline-block; margin-left: 6px; color: var(--warn-ink); background: var(--warn-bg); border-radius: 4px; padding: 0 5px; font-size: 11px; font-style: normal; font-weight: 600; }
.t tfoot td { font-weight: 700; background: var(--surface-2); position: sticky; bottom: 0; }
.t tfoot td:first-child { background: var(--surface-2); }
.t input.cell { width: 110px; text-align: right; border: 1px solid transparent; background: transparent; padding: 3px 6px; border-radius: 5px; font-variant-numeric: tabular-nums; }
.t input.cell:hover { border-color: var(--line-strong); }
.t input.cell:focus { border-color: var(--gold); background: var(--surface); outline: none; }
.t input.cell.over { background: var(--warn-bg); color: var(--warn-ink); font-weight: 600; }
.t td.left, .t th.left { text-align: left; }

/* Tabs */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); padding: 0 18px; }
.tabs button { border: 0; background: none; padding: 10px 12px; cursor: pointer; color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px; font-weight: 500; }
.tabs button.on { color: var(--ink); border-color: var(--gold); font-weight: 650; }

/* Charts */
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis { stroke: var(--axis); stroke-width: 1; }
.chart text { fill: var(--ink-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .lbl { fill: var(--ink-2); font-weight: 600; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--ink-2); padding-bottom: 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.tip { position: fixed; z-index: 60; pointer-events: none; background: var(--surface); border: 1px solid var(--line-strong); border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: var(--shadow); display: none; min-width: 160px; font-variant-numeric: tabular-nums; }
.tip .tr { display: flex; justify-content: space-between; gap: 16px; }
.tip .th { font-weight: 650; margin-bottom: 4px; }
.tip i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: 6px; }

/* Drawer */
.drawer { position: fixed; top: 0; right: 0; width: min(560px, 100vw); height: 100vh; background: var(--surface); border-left: 1px solid var(--line); box-shadow: -10px 0 30px rgba(0,0,0,.12); transform: translateX(100%); transition: transform .18s ease; z-index: 50; display: flex; flex-direction: column; }
.drawer.open { transform: none; }
.drawer-head { display: flex; align-items: flex-start; gap: 10px; padding: 16px 18px; border-bottom: 1px solid var(--line); }
.drawer-head > div { flex: 1; }
.drawer-body { overflow: auto; padding: 16px 18px 40px; display: flex; flex-direction: column; gap: 14px; }
.dt { font-size: 12px; color: var(--ink-3); }
.dv { font-size: 26px; font-weight: 650; letter-spacing: -.02em; }
.rule-box { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; white-space: pre-wrap; }
.note { background: var(--warn-bg); color: var(--warn-ink); border-radius: 8px; padding: 9px 12px; font-size: 12.5px; }
.linkish { color: var(--gold); cursor: pointer; text-decoration: underline; text-underline-offset: 2px; }

/* Import */
.drop { display: block; border: 2px dashed var(--line-strong); border-radius: 12px; padding: 40px 20px; text-align: center; background: var(--surface); cursor: pointer; }
.drop.over { border-color: var(--gold); background: var(--gold-soft); }
.drop h3 { margin: 0 0 6px; }
.log { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; }
.log div { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; font-size: 12.5px; }
.ok { color: var(--pos); } .bad { color: var(--neg); }

/* Upload Center */
.ds-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 12px; }
.ds-card { padding: 14px 16px; text-decoration: none; display: block; }
.ds-card:hover { border-color: var(--gold); }
.ds-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; margin-bottom: 4px; }
.warn-pill { background: var(--warn-bg); color: var(--warn-ink); border-color: transparent; }
.warn { color: var(--warn-ink); }

/* Sign in */
.login { position: fixed; inset: 0; z-index: 100; display: grid; place-items: center; background: var(--bg); padding: 16px; }
.login[hidden] { display: none; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 26px 26px 20px; display: flex; flex-direction: column; gap: 12px; }
.login-card h2 { margin: 0; font-size: 18px; }
.login-card label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--ink-2); }
.login-card input { padding: 9px 11px; }
.login-card .btn.primary { padding: 10px; }
.login-links { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.login-links a { color: var(--gold); }
#lgMsg { min-height: 18px; color: var(--ink-2); }

.stat-row { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; }
.stat { padding: 14px 16px; }
.stat b { display: block; font-size: 22px; letter-spacing: -.02em; }
.empty { padding: 48px; text-align: center; color: var(--ink-3); }

@media (max-width: 1100px) { .kpis, .stat-row { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid-2 { grid-template-columns: 1fr; } }
.backdrop { display: none; }
html { -webkit-text-size-adjust: 100%; }
button, a, .t td.num, .ins, .kpi { touch-action: manipulation; -webkit-tap-highlight-color: transparent; }
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; left: 0; top: 0; bottom: 0; height: 100dvh; z-index: 70; width: min(280px, 85vw); transform: translateX(-100%); transition: transform .18s;
    padding-top: env(safe-area-inset-top); padding-bottom: env(safe-area-inset-bottom); }
  .side.open { transform: none; }
  .side.open + .main .backdrop, .backdrop.on { display: block; position: fixed; inset: 0; z-index: 65; background: rgba(0,0,0,.45); }
  .menu { display: inline-grid; place-items: center; }
  .top { padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top)); flex-wrap: wrap; gap: 10px; }
  .top-title { flex: 1 1 calc(100% - 50px); }
  .top h1 { font-size: 17px; }
  .top-controls { width: 100%; justify-content: space-between; }
  .top-controls .ctl > span { display: none; }
  .top-controls select { max-width: 52vw; }
  .page { padding: 14px 16px calc(50px + env(safe-area-inset-bottom)); gap: 14px; }
  .kpis, .stat-row { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .kpi { padding: 12px; }
  .kpi-value { font-size: 20px; }
  .card-head { padding: 12px 14px 0; }
  .card-body { padding: 12px 14px 14px; }
  .t th, .t td { padding: 7px 10px; }
  .t th:first-child, .t td:first-child { min-width: 150px; max-width: 46vw; white-space: normal; }
  .table-wrap { max-height: none; -webkit-overflow-scrolling: touch; }
  .seg button { padding: 7px 10px; }
  /* iOS zooms into inputs under 16px */
  input, select, textarea { font-size: 16px; }
  .t input.cell { font-size: 16px; width: 96px; }
  .drawer { width: 100vw; padding-bottom: env(safe-area-inset-bottom); }
  .drawer-head { padding-top: max(16px, env(safe-area-inset-top)); }
  .insights { grid-template-columns: 1fr; }
  .icon-btn { width: 38px; height: 38px; }
}
@media (max-width: 420px) {
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 18px; }
  .top-controls { flex-wrap: wrap; }
}
