:root {
  color-scheme: light;
  --ink: #0f172a; --muted: #64748b; --line: #e2e8f0; --bg: #f1f5f9; --card: #ffffff;
  --accent: #2563eb; --good: #16a34a; --bad: #dc2626;
  --c1: #2563eb; --c2: #f97316; --c3: #14b8a6; --c4: #a855f7; --c5: #e87ba4;
  --track: #e2e8f0; --hover: #eff6ff; --rowline: #f1f5f9; --dot: #ffffff; --seg: #e2e8f0;
  --shadow: rgba(15, 23, 42, .12); --shade: rgba(15, 23, 42, .35); --glow: rgba(37, 99, 235, .15);
  --good-bg: #dcfce7; --good-ink: #166534; --bad-bg: #fee2e2; --bad-ink: #991b1b; --flat-bg: #e2e8f0; --flat-ink: #475569;
  --tipbg: #0f172a; --tipink: #ffffff;
}
/* Dark palette: lifted accents and softer chips so contrast holds on a dark ground. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #e2e8f0; --muted: #94a3b8; --line: #334155; --bg: #0b1220; --card: #111c30;
  --accent: #60a5fa; --good: #4ade80; --bad: #f87171;
  --c1: #60a5fa; --c2: #fb923c; --c3: #2dd4bf; --c4: #c084fc; --c5: #f9a8d4;
  --track: #263449; --hover: #1a2a45; --rowline: #1e293b; --dot: #111c30; --seg: #1e293b;
  --shadow: rgba(0, 0, 0, .5); --shade: rgba(0, 0, 0, .6); --glow: rgba(96, 165, 250, .25);
  --good-bg: #14532d; --good-ink: #bbf7d0; --bad-bg: #7f1d1d; --bad-ink: #fecaca; --flat-bg: #263449; --flat-ink: #cbd5e1;
  --tipbg: #e2e8f0; --tipink: #0f172a;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body { margin: 0; font: 13px/1.4 system-ui, -apple-system, 'Segoe UI', sans-serif; color: var(--ink); background: var(--bg); }
button { font: inherit; color: inherit; cursor: pointer; }
.boot, .error, .empty { padding: 2rem; color: var(--muted); }
.error { color: var(--bad); }

.app { height: 100vh; padding: 14px 20px 16px; display: flex; flex-direction: column; overflow: hidden; }
.top { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 10px; flex: none; }
.titleblock { min-width: 0; }
h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; }
.sub { margin: 2px 0 8px; color: var(--muted); }
.filters { display: flex; gap: 14px; flex-wrap: wrap; align-items: flex-end; }
.corner { display: flex; flex-direction: column; align-items: flex-end; gap: 8px; flex: none; }
.byline { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 12px; white-space: nowrap; }
.links { display: inline-flex; gap: 4px; }
.out { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; border-radius: 8px; color: var(--muted); text-decoration: none; }
.out:hover, .out:focus-visible { color: var(--accent); background: var(--hover); outline: none; }
.linkshade { position: fixed; inset: 0; z-index: 40; background: var(--shade); display: flex; align-items: center; justify-content: center; padding: 16px; }
.linkbox { background: var(--card); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; width: min(460px, 100%); box-shadow: 0 10px 40px var(--shadow); }
.linkbox h2 { margin: 0 0 4px; font-size: 15px; }
.link-field { width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); font: inherit; }
.linkrow { display: flex; justify-content: flex-end; margin-top: 10px; }
.seg { display: inline-flex; background: var(--seg); border-radius: 8px; padding: 2px; }
.seg button { border: 0; background: transparent; padding: 5px 11px; border-radius: 6px; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 2px var(--shadow); font-weight: 600; }
.theme { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 5px 12px; color: var(--ink); }
.theme:hover, .theme:focus-visible { border-color: var(--accent); outline: none; }
.flabel { display: block; font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 3px; }

.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 10px; margin-bottom: 10px; flex: none; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 14px 16px; }
.kpi { text-align: left; position: relative; transition: box-shadow .15s, transform .15s, border-color .15s; }
.kpi:hover, .kpi:focus-visible { border-color: var(--accent); box-shadow: 0 4px 14px var(--glow); transform: translateY(-1px); outline: none; }
.kpi .label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.kpi .value { display: block; font-size: 24px; font-weight: 700; letter-spacing: -0.02em; margin: 0; }
.kpi .foot { color: var(--muted); font-size: 12px; }
.kpi svg { display: block; margin-top: 4px; width: 100%; height: 26px; }
.chip { display: inline-block; padding: 0 6px; border-radius: 999px; font-weight: 600; font-size: 11px; margin-right: 4px; }
.chip.good { background: var(--good-bg); color: var(--good-ink); }
.chip.bad { background: var(--bad-bg); color: var(--bad-ink); }
.chip.flat { background: var(--flat-bg); color: var(--flat-ink); }

/* The chart area takes whatever height the header and KPI row leave, so nothing scrolls. Each card
   scrolls inside itself only if its list is longer than the space it was given. */
.grid { flex: 1 1 0; min-height: 0; display: grid; gap: 10px;
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr) minmax(0, 1fr);
  grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
  grid-template-areas: 'trend trend cat' 'sub prod reg'; }
.grid > .card { min-height: 0; min-width: 0; display: flex; flex-direction: column; overflow: hidden; padding: 10px 14px; }
.grid > .card .fill { flex: 1 1 0; min-height: 0; overflow-y: auto; }
.grid .a-trend { grid-area: trend; } .grid .a-cat { grid-area: cat; } .grid .a-reg { grid-area: reg; }
.grid .a-sub { grid-area: sub; } .grid .a-prod { grid-area: prod; }
.chartbox { flex: 1 1 0; min-height: 0; position: relative; }
.chartbox svg.chart { position: absolute; inset: 0; width: 100%; height: 100%; }
.card h2 { margin: 0 0 2px; font-size: 14px; }
.card .cap { margin: 0 0 6px; color: var(--muted); font-size: 11px; }
.card svg.chart { width: 100%; display: block; }
.hint { color: var(--muted); font-size: 11px; margin-top: 6px; }

.rows { list-style: none; margin: 0; padding: 0; }
.rows li { margin: 0; }
.row { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 2fr) 74px; gap: 10px; align-items: center; width: 100%;
  border: 0; background: transparent; text-align: left; padding: 3px 6px; border-radius: 6px; }
.row:hover, .row:focus-visible { background: var(--hover); outline: none; }
.row .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .bar { height: 10px; background: var(--track); border-radius: 5px; overflow: hidden; }
.row .bar i { display: block; height: 100%; border-radius: 5px; }
.row .num { text-align: right; font-variant-numeric: tabular-nums; }
.neg { color: var(--bad); }

.tabs { margin-bottom: 8px; }

.shade { position: fixed; inset: 0; background: var(--shade); z-index: 10; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(640px, 94vw); background: var(--bg); z-index: 11; overflow-y: auto;
  padding: 18px 20px 28px; box-shadow: -8px 0 30px var(--shadow); animation: slide .18s ease-out; }
@keyframes slide { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.drawer h2.title { font-size: 22px; margin: 2px 0 2px; letter-spacing: -0.02em; }
.eyebrow { margin: 0; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.dtop { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; gap: 8px; }
.pill { border: 1px solid var(--line); background: var(--card); border-radius: 999px; padding: 3px 11px; }
.pill:hover, .pill:focus-visible { border-color: var(--accent); outline: none; }
.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; margin: 10px 0 12px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; }
.stat b { display: block; font-size: 17px; }
.stat span { color: var(--muted); font-size: 11px; }
.drawer .card { margin-bottom: 10px; }

table.t { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
table.t th { text-align: left; font-size: 10px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 4px 6px; border-bottom: 1px solid var(--line); font-weight: 600; }
table.t th.r, table.t td.r { text-align: right; }
table.t td { padding: 5px 6px; border-bottom: 1px solid var(--rowline); }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover { background: var(--hover); }

#tip { position: fixed; z-index: 30; pointer-events: none; background: var(--tipbg); color: var(--tipink); padding: 5px 8px; border-radius: 6px; font-size: 12px; white-space: nowrap; }

/* Export to Excel: the button, and the dialog that hands the file over. */
.xl-btn { font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.xl-btn svg { flex: none; }
/* Web copy only: "Learn how I did this", glowing, its arrow pointing at Export to Excel. */
.xl-cluster { display: flex; align-items: center; gap: 8px; }
.learn-how { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px 4px 12px; border-radius: 999px; white-space: nowrap;
  font-weight: 600; text-decoration: none; background: var(--accent); color: var(--card); animation: learn-glow 2.4s ease-in-out infinite; }
.learn-how:hover, .learn-how:focus-visible { outline: none; filter: brightness(1.08); }
.learn-how:focus-visible { box-shadow: 0 0 0 3px var(--card), 0 0 0 5px var(--accent); animation: none; }
.learn-how svg { flex: none; animation: learn-nudge 2.4s ease-in-out infinite; }
@keyframes learn-glow {
  0%, 100% { box-shadow: 0 0 4px 0 color-mix(in srgb, var(--accent) 35%, transparent); }
  50% { box-shadow: 0 0 16px 3px color-mix(in srgb, var(--accent) 60%, transparent); }
}
@keyframes learn-nudge { 0%, 100% { transform: translateX(0); } 50% { transform: translateX(3px); } }
@media (prefers-reduced-motion: reduce) {
  .learn-how { animation: none; box-shadow: 0 0 10px 2px color-mix(in srgb, var(--accent) 45%, transparent); }
  .learn-how svg { animation: none; }
}
.xl-row { margin: 8px 0 4px; }
.xl-shade { position: fixed; inset: 0; z-index: 50; background: var(--shade); display: flex; align-items: center; justify-content: center; padding: 16px; }
.xl-box { background: var(--card); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; padding: 16px 18px; width: min(520px, 100%); max-height: 100%; overflow-y: auto; box-shadow: 0 10px 40px var(--shadow); }
.xl-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.xl-top h2 { margin: 0; font-size: 16px; }
.xl-box .cap { margin: 2px 0 12px; color: var(--muted); font-size: 12px; }
.xl-box h3 { margin: 14px 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.xl-link { display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 14px 16px; border-radius: 10px; background: var(--accent); color: #fff; text-decoration: none; text-align: center; word-break: break-word; }
.xl-link:hover, .xl-link:focus-visible { filter: brightness(1.08); outline: 2px solid var(--ink); outline-offset: 2px; }
.xl-link b { font-size: 14px; }
.xl-link span { font-size: 12px; opacity: .95; }
.xl-hint { margin: 8px 0 0; color: var(--muted); font-size: 12px; border-radius: 6px; padding: 2px 4px; }
.xl-hint.flash { animation: xlflash 1.2s ease-out; }
@keyframes xlflash { 0%, 40% { background: var(--hover); color: var(--ink); } 100% { background: transparent; } }
.xl-warn, .xl-note { margin: 8px 0 0; font-size: 12px; }
.xl-warn { color: var(--bad); }
.xl-note { color: var(--muted); }
.xl-tabs { margin: 0; padding-left: 18px; font-size: 12px; color: var(--muted); }
.xl-tabs li { margin: 2px 0; }
.xl-tabs b { color: var(--ink); }

/* Below this width there is no room for the grid to fit, so it stacks and the page scrolls instead. */
@media (max-width: 980px), (max-height: 520px) {
  .app { height: auto; min-height: 100vh; overflow: visible; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid { display: block; }
  .grid > .card { margin-bottom: 10px; min-height: 220px; }
  .chartbox { height: 200px; flex: none; }
}
