:root {
  --bg: #f6f7f9; --panel: #ffffff; --panel-2: #f1f3f6; --line: #e3e6eb;
  --text: #14171c; --muted: #5f6b7a; --dim: #8a94a3;
  --accent: #2f6df6; --accent-soft: #e8efff;
  --good: #12805c; --good-soft: #e3f5ee; --bad: #c4352b; --bad-soft: #fcebea;
  --warn: #a86200; --warn-soft: #fff3df;
  --s1: #2f6df6; --s2: #12a37a; --s3: #e0663a; --s4: #8b5cf6; --s5: #c4352b; --s6: #0891b2;
  --radius: 10px; --shadow: 0 1px 2px rgba(16, 24, 40, .05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216; --panel: #171b21; --panel-2: #1d232b; --line: #2a313b;
    --text: #e8ebf0; --muted: #a0aab7; --dim: #6f7a88;
    --accent: #6d9bff; --accent-soft: #1d2a45;
    --good: #3ccf97; --good-soft: #12302a; --bad: #ff7a70; --bad-soft: #3a1d1c;
    --warn: #f0b155; --warn-soft: #3a2c14;
    --s1: #6d9bff; --s2: #3ccf97; --s3: #ff9a6b; --s4: #b597ff; --s5: #ff7a70; --s6: #4fd1e8;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1216; --panel: #171b21; --panel-2: #1d232b; --line: #2a313b;
  --text: #e8ebf0; --muted: #a0aab7; --dim: #6f7a88;
  --accent: #6d9bff; --accent-soft: #1d2a45;
  --good: #3ccf97; --good-soft: #12302a; --bad: #ff7a70; --bad-soft: #3a1d1c;
  --warn: #f0b155; --warn-soft: #3a2c14;
  --s1: #6d9bff; --s2: #3ccf97; --s3: #ff9a6b; --s4: #b597ff; --s5: #ff7a70; --s6: #4fd1e8;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
[x-cloak] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, select, input { font: inherit; color: inherit; }
.num { font-variant-numeric: tabular-nums; }

/* ── Shell ── */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side {
  background: var(--panel); border-right: 1px solid var(--line);
  padding: 18px 12px; position: sticky; top: 0; height: 100vh;
  display: flex; flex-direction: column; gap: 2px;
}
.logo { display: flex; align-items: center; gap: 10px; padding: 4px 10px 18px; font-weight: 650; font-size: 16px; }
.logo-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 700; font-size: 15px; }
.nav-btn {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 8px 10px;
  border: 0; border-radius: 8px; background: none; cursor: pointer; text-align: left; color: var(--muted);
}
.nav-btn:hover { background: var(--panel-2); color: var(--text); }
.nav-btn.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-btn svg { width: 17px; height: 17px; flex: none; }
.nav-group { margin: 14px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); }
.side-foot { margin-top: auto; padding: 10px; color: var(--dim); font-size: 12px; display: grid; gap: 6px; }
.side-foot button { background: none; border: 0; padding: 0; color: var(--muted); cursor: pointer; text-align: left; }

.main { min-width: 0; }
.bar {
  position: sticky; top: 0; z-index: 5; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
  padding: 12px 24px; display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--panel); border-radius: 999px;
  padding: 5px 12px; cursor: pointer; font-size: 13px; color: var(--muted);
}
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--panel); }
.seg button { border: 0; background: none; padding: 5px 10px; cursor: pointer; font-size: 13px; color: var(--muted); }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.ctl { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 8px; font-size: 13px; }
.spacer { flex: 1; }
.fresh { font-size: 12px; color: var(--dim); display: flex; align-items: center; gap: 6px; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--dim); }
.dot.ok { background: var(--good); } .dot.warn { background: var(--warn); } .dot.bad { background: var(--bad); }

.page { padding: 20px 24px 60px; }
.h1 { font-size: 20px; font-weight: 650; margin: 0 0 4px; letter-spacing: -.01em; }
.sub { color: var(--muted); font-size: 13px; margin: 0 0 18px; }

/* ── Cards ── */
.grid { display: grid; gap: 12px; }
.g6 { grid-template-columns: repeat(6, minmax(0, 1fr)); }
.g7 { grid-template-columns: repeat(7, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.g8 { grid-template-columns: repeat(8, minmax(0, 1fr)); }
.g2 { grid-template-columns: 3fr 2fr; }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.pad { padding: 16px; }
.kpi { padding: 14px 16px; }
.kpi .lbl { color: var(--muted); font-size: 12px; display: flex; gap: 6px; align-items: center; }
.kpi .val { font-size: 22px; font-weight: 650; margin-top: 4px; letter-spacing: -.01em; }
.kpi.hero .val { font-size: 28px; }
.kpi .meta { font-size: 12px; color: var(--dim); margin-top: 2px; display: flex; gap: 8px; flex-wrap: wrap; }
.kpi.small .val { font-size: 17px; }
.delta { font-size: 12px; font-weight: 600; }
.up { color: var(--good); } .down { color: var(--bad); } .flat { color: var(--dim); }
.neg { color: var(--bad); }
.card-h { display: flex; align-items: center; gap: 10px; padding: 14px 16px 0; }
.card-h h3 { font-size: 14px; margin: 0; font-weight: 600; }
.card-h .spacer + * { font-size: 12px; }
.chart-box { position: relative; height: 280px; padding: 8px 12px 12px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.flags { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 14px; }
.flag { font-size: 12px; padding: 5px 10px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); }
.flag.info { background: var(--panel-2); color: var(--muted); }

/* waterfall */
.wf { display: grid; gap: 6px; padding: 12px 16px 16px; }
.wf-row { display: grid; grid-template-columns: minmax(100px, 140px) minmax(60px, 1fr) 92px; gap: 10px; align-items: center; font-size: 13px; }
.wf-row .lbl { color: var(--muted); }
.wf-row.total .lbl, .wf-row.total .v { font-weight: 650; color: var(--text); }
.wf-track { height: 14px; background: var(--panel-2); border-radius: 4px; position: relative; overflow: hidden; }
.wf-bar { position: absolute; top: 0; bottom: 0; border-radius: 4px; }
.wf-row .v { text-align: right; }

/* tables */
.tbl-wrap { overflow-x: auto; overscroll-behavior-x: contain; scrollbar-gutter: stable; }
/* macOS hides overlay scrollbars until you scroll, so a wide table just looks
   cut off. Draw the horizontal bar permanently and pin the first column. */
.tbl-wrap::-webkit-scrollbar { height: 10px; }
.tbl-wrap::-webkit-scrollbar-track { background: var(--panel-2); border-radius: 5px; }
.tbl-wrap::-webkit-scrollbar-thumb { background: var(--dim); border-radius: 5px; border: 2px solid var(--panel-2); }
.tbl-wrap th:first-child, .tbl-wrap td:first-child { position: sticky; left: 0; background: var(--panel); z-index: 2; }
.tbl-wrap th:first-child { z-index: 3; }
.tbl-wrap tbody tr:hover td:first-child { background: var(--panel-2); }
.tbl-wrap tr.tot td:first-child { background: var(--panel-2); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 9px 12px;
  border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--panel); position: sticky; top: 0; }
th.r, td.r { text-align: right; }
th.sort { cursor: pointer; user-select: none; }
th.sort:hover { color: var(--text); }
td { padding: 8px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--panel-2); }
tr.tot td { font-weight: 650; background: var(--panel-2); }
td.title { max-width: 340px; overflow: hidden; text-overflow: ellipsis; }
.pill { font-size: 11px; padding: 2px 7px; border-radius: 999px; background: var(--panel-2); color: var(--muted); }
.pill.good { background: var(--good-soft); color: var(--good); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.stmt td:first-child, .stmt th:first-child { position: sticky; left: 0; background: var(--panel); z-index: 1; }
.stmt tr.major td { font-weight: 650; }
.stmt tr.sub td:first-child { padding-left: 26px; color: var(--muted); }
.stmt tr.pct td { color: var(--dim); font-size: 12px; }

.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; padding: 10px 16px; color: var(--muted); font-size: 13px; }
.btn { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 5px 12px; cursor: pointer; font-size: 13px; }
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .4; cursor: default; }
.empty { padding: 40px; text-align: center; color: var(--dim); }
.err { margin: 0 0 14px; padding: 10px 14px; border-radius: 8px; background: var(--bad-soft); color: var(--bad); font-size: 13px; }
.loading { opacity: .55; transition: opacity .15s; }
.cell-good { background: var(--good-soft); color: var(--good); }
.cell-bad { color: var(--bad); }
.note { font-size: 12px; color: var(--dim); padding: 0 16px 14px; }
ul.limits { margin: 0; padding: 4px 16px 16px 32px; color: var(--muted); font-size: 13px; display: grid; gap: 6px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: 100%; max-width: 340px; display: grid; gap: 12px; }
.login input { width: 100%; padding: 10px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); }
.login button { padding: 10px; border: 0; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 600; cursor: pointer; }

/* ── v2.0: sortable headers, totals, groups, sparklines, drawer ── */
th.sort::after { content: ''; display: inline-block; margin-left: 4px; font-size: 9px; opacity: .55; }
th.sort:hover::after { content: '⇅'; opacity: .45; }
th.sort.asc::after { content: '▲'; opacity: 1; }
th.sort.desc::after { content: '▼'; opacity: 1; }
th.sort.asc, th.sort.desc { color: var(--text); }
tfoot td { font-weight: 650; background: var(--panel-2); border-top: 1px solid var(--line); border-bottom: 0; }
tfoot td:first-child { color: var(--muted); font-weight: 600; }
tbody tr.row-link { cursor: pointer; }
tbody tr.sel td { background: var(--accent-soft); }
.tbl-wrap tbody tr.sel td:first-child { background: var(--accent-soft); }
.grp { margin: 16px 0 6px 2px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); }
.kpi .spark { height: 28px; margin-top: 10px; }
.kpi .spark svg { width: 100%; height: 28px; display: block; overflow: visible; }
.spl { fill: none; stroke: var(--accent); stroke-width: 1.6; vector-effect: non-scaling-stroke; stroke-linejoin: round; }
.sp0 { stroke: var(--dim); stroke-width: 1; vector-effect: non-scaling-stroke; stroke-dasharray: 2 3; opacity: .7; }
.spd { fill: var(--accent); }
.kpi.hero .spl { stroke: var(--s2); } .kpi.hero .spd { fill: var(--s2); }
.ver { font-size: 10px; font-weight: 600; color: var(--accent); background: var(--accent-soft); border-radius: 999px; padding: 1px 7px; margin-left: 4px; }
.btn.xs { padding: 3px 9px; font-size: 12px; }
a.ext { color: var(--dim); margin-left: 6px; font-size: 12px; text-decoration: none; }
a.ext:hover { color: var(--accent); text-decoration: none; }
td.delta { font-weight: 600; font-size: 12px; }
.drawer-wrap { position: fixed; inset: 0; z-index: 30; background: rgba(10, 14, 20, .35); }
.drawer {
  position: absolute; top: 0; right: 0; bottom: 0; width: min(600px, 100vw);
  background: var(--panel); border-left: 1px solid var(--line); box-shadow: -12px 0 32px rgba(0, 0, 0, .18);
  overflow-y: auto; padding: 16px 18px 32px; animation: slide-in .16s ease-out;
}
@keyframes slide-in { from { transform: translateX(24px); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer-h { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 14px; }
.drawer-h h3 { margin: 0; font-size: 17px; font-weight: 650; }
.drawer h4 { margin: 18px 0 8px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--dim); }
.drawer .tbl-wrap { border: 1px solid var(--line); border-radius: 8px; }
.drawer .tbl-wrap th:first-child, .drawer .tbl-wrap td:first-child { position: static; }
.drawer td.title { max-width: 260px; }
.kv { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.kv > div { background: var(--panel-2); border-radius: 8px; padding: 8px 10px; display: grid; gap: 2px; min-width: 0; }
.kv span { font-size: 11px; color: var(--dim); }
.kv b { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.kv2 { display: grid; gap: 3px; }
.kv2 > div { display: flex; justify-content: space-between; gap: 12px; padding: 6px 10px; border-radius: 6px; font-size: 13px; color: var(--muted); }
.kv2 > div.tot { background: var(--panel-2); color: var(--text); font-weight: 650; }

/* ── Responsive ── */
@media (max-width: 1200px) { .g3 { grid-template-columns: 1fr; } .g6 { grid-template-columns: repeat(3, minmax(0, 1fr)); } .g7 { grid-template-columns: repeat(4, minmax(0, 1fr)); } .g8 { grid-template-columns: repeat(4, minmax(0, 1fr)); } }
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; align-items: center; overflow-x: auto; padding: 8px 16px; border-right: 0; border-bottom: 1px solid var(--line); }
  .side .nav-group { display: none; }
  .logo { padding: 4px 8px 4px 0; }
  .logo span { display: none; }
  .nav-btn { width: auto; white-space: nowrap; }
  .nav-btn svg { display: none; }
  .side-foot { display: none; }
  .bar { padding: 10px 16px; position: static; }
  .page { padding: 16px 16px 48px; }
  .g2, .g4 { grid-template-columns: 1fr; }
  .g6, .g7, .g8 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .wf-row { grid-template-columns: 110px 1fr 90px; }
  .kv { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .drawer { padding: 12px 14px 28px; }
  /* Today page on a phone (Paul 10-20): shorter charts, smaller tiles, legends wrap */
  .chart-box, .chart-box.tall, .chart-box.mid { height: 240px; padding: 6px 6px 8px; }
  .kpi .val { font-size: 19px; } .kpi.hero .val { font-size: 22px; }
  .kpi .meta { font-size: 11px; white-space: normal; }
  .legend { flex-wrap: wrap; row-gap: 2px; }
  .card-h { flex-wrap: wrap; }
}

/* Dashboard: Watch panel + month projection table */
.watch { list-style: none; margin: 0; display: flex; flex-direction: column; gap: 6px; }
.watch li { font-size: 13px; padding: 7px 10px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); line-height: 1.35; display: flex; align-items: center; gap: 8px; }
.watch li span:first-child { flex: 1; }
.watch li .ack { border: 1px solid currentColor; background: transparent; color: inherit; border-radius: 6px; width: 24px; height: 22px; cursor: pointer; opacity: .6; font-size: 13px; line-height: 1; }
.watch li .ack:hover { opacity: 1; }
.watch li.done { background: var(--panel-2); color: var(--muted); text-decoration: line-through; }
.watch li.done .who { text-decoration: none; font-size: 11px; white-space: nowrap; }
.watch li.cogs, .watch li.fees, .watch li.fulfilment, .watch li.fixed, .watch li.fixcosts, .watch li.renewal { background: var(--panel-2); color: var(--muted); }
table.tight td, table.tight th { padding: 5px 8px; }
tr.strong td { font-weight: 600; }
tr.muted td { color: var(--muted); }

/* Today page: progress bar in the hero tiles (today vs a normal full day; the tick = where a normal day is by now) */
.kpi .pace-bar { position: relative; height: 6px; border-radius: 3px; background: var(--panel-2); margin: 6px 0 4px; overflow: visible; }
.kpi .pace-bar i { display: block; height: 100%; border-radius: 3px; background: var(--s1); }
.kpi .pace-bar b { position: absolute; top: -3px; width: 2px; height: 12px; background: var(--dim); }
.chart-box.tall { height: 400px; }
.chart-box.mid { height: 260px; }
.pill.ok { background: var(--good-soft); color: var(--good); }
.pill.info { background: var(--panel-2); color: var(--muted); }

/* Product analysis: the table scrolls inside its own box so the two header rows stay pinned (Paul 10-20) */
.an-scroll { max-height: calc(100vh - 150px); overflow: auto; }
.an-scroll thead th { position: sticky; top: 0; background: var(--panel); z-index: 3; white-space: normal; line-height: 1.2; vertical-align: middle; text-align: center; min-width: 96px; padding: 8px 6px; }
.an-scroll thead tr:nth-child(2) th:first-child { text-align: left; }
.an-scroll thead tr:nth-child(2) th { top: 33px; box-shadow: 0 1px 0 var(--line); }
.an-scroll thead tr:nth-child(2) th:first-child { left: 0; z-index: 4; }

/* clickable hero KPI tiles that open a chart below (Paul 11-06) */
.kpi.click { cursor: pointer; transition: border-color .15s; }
.kpi.click:hover { border-color: var(--dim); }
.kpi.sel { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.kpi .kx-chev { margin-left: auto; color: var(--dim); font-size: 11px; }
