:root {
  --text-xs: clamp(0.75rem, 0.7rem + 0.25vw, 0.875rem);
  --text-sm: clamp(0.875rem, 0.8rem + 0.35vw, 1rem);
  --text-base: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
  --text-lg: clamp(1.125rem, 1rem + 0.75vw, 1.5rem);
  --text-xl: clamp(1.5rem, 1.2rem + 1.25vw, 2.25rem);
  --space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem; --space-5: 1.25rem;
  --space-6: 1.5rem; --space-8: 2rem; --space-10: 2.5rem; --space-12: 3rem; --space-16: 4rem;
  --font-body: 'Instrument Sans', 'Segoe UI', system-ui, sans-serif;
  --radius: 10px; --radius-sm: 6px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);

  /* Light: warm ivory paper, ink, champagne gold */
  --bg: oklch(97% 0.008 80);
  --surface: oklch(99.5% 0.004 80);
  --surface-2: oklch(95% 0.01 80);
  --border: oklch(88% 0.012 80);
  --text: oklch(20% 0.01 60);
  --muted: oklch(48% 0.015 60);
  --accent: oklch(66% 0.10 78);
  --accent-ink: oklch(45% 0.09 78);
  --accent-soft: oklch(66% 0.10 78 / 0.14);
  --up: oklch(55% 0.13 150);
  --up-soft: oklch(55% 0.13 150 / 0.12);
  --down: oklch(55% 0.16 25);
  --down-soft: oklch(55% 0.16 25 / 0.12);
  --shadow: 0 1px 2px oklch(20% 0.01 60 / 0.06), 0 8px 24px -12px oklch(20% 0.01 60 / 0.12);
  color-scheme: light;
}
[data-theme="dark"] {
  --bg: oklch(15% 0.01 260);
  --surface: oklch(19% 0.012 260);
  --surface-2: oklch(23% 0.012 260);
  --border: oklch(30% 0.012 260);
  --text: oklch(94% 0.01 80);
  --muted: oklch(68% 0.015 80);
  --accent: oklch(78% 0.10 82);
  --accent-ink: oklch(84% 0.09 82);
  --accent-soft: oklch(78% 0.10 82 / 0.16);
  --up: oklch(74% 0.14 152);
  --up-soft: oklch(74% 0.14 152 / 0.14);
  --down: oklch(70% 0.16 25);
  --down-soft: oklch(70% 0.16 25 / 0.14);
  --shadow: 0 1px 2px oklch(0% 0 0 / 0.4), 0 8px 24px -12px oklch(0% 0 0 / 0.6);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  font-family: var(--font-body); font-size: var(--text-base); line-height: 1.5;
  color: var(--text); background: var(--bg);
  -webkit-font-smoothing: antialiased; text-rendering: optimizeLegibility;
}
a { color: var(--accent-ink); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, p { margin: 0; }
h2 { font-size: var(--text-lg); font-weight: 600; letter-spacing: -0.01em; }
button, input, select { font: inherit; color: inherit; }
.num, td.num, .kpi-value, .tabular { font-variant-numeric: tabular-nums lining-nums; }

.dashboard { display: grid; grid-template-rows: auto 1fr; height: 100dvh; }
.header {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: var(--space-4);
  padding: var(--space-4) var(--space-8);
  background: color-mix(in oklab, var(--surface) 88%, transparent);
  backdrop-filter: blur(12px); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: var(--space-4); min-width: 0; }
.logo { flex: none; color: var(--text); }
.brand h1 { font-size: var(--text-lg); font-weight: 600; letter-spacing: -0.015em; line-height: 1.2; }
.brand .sub { font-size: var(--text-xs); color: var(--muted); margin-top: 2px; }
.header-actions { display: flex; gap: var(--space-2); flex: none; }

.btn {
  display: inline-flex; align-items: center; gap: var(--space-2);
  padding: var(--space-2) var(--space-4); border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font-size: var(--text-sm); font-weight: 500; cursor: pointer;
  transition: background .2s var(--ease), border-color .2s var(--ease), transform .15s var(--ease);
}
.btn:hover { background: var(--surface-2); border-color: var(--accent); }
.btn:active { transform: translateY(1px); }
.btn.icon { padding: var(--space-2); width: 38px; height: 38px; justify-content: center; }
.btn.ghost { background: transparent; }
.btn:focus-visible, input:focus-visible, select:focus-visible, .chip:focus-visible, th:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
[data-theme="dark"] .sun { display: none; }
:root:not([data-theme="dark"]) .moon { display: none; }

.main {
  overflow-y: auto; overscroll-behavior: contain;
  padding: var(--space-6) var(--space-8) var(--space-16);
  display: flex; flex-direction: column; gap: var(--space-6);
}

/* KPIs */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: var(--space-4); }
.kpi {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--space-4) var(--space-5); box-shadow: var(--shadow);
  animation: rise .5s var(--ease) both;
}
.kpi:nth-child(2) { animation-delay: .05s } .kpi:nth-child(3) { animation-delay: .1s }
.kpi:nth-child(4) { animation-delay: .15s } .kpi:nth-child(5) { animation-delay: .2s } .kpi:nth-child(6) { animation-delay: .25s }
.kpi-label { font-size: var(--text-xs); color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.kpi-value { font-size: var(--text-xl); font-weight: 600; letter-spacing: -0.02em; line-height: 1.15; margin-top: var(--space-1); }
.kpi-value.small { font-size: var(--text-lg); }
.kpi-note { font-size: var(--text-xs); color: var(--muted); margin-top: var(--space-1); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.kpi-value.pos { color: var(--up); } .kpi-value.neg { color: var(--down); }

/* Filters */
.filters {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: var(--space-4) var(--space-5); display: grid; gap: var(--space-3); box-shadow: var(--shadow);
}
.filter-row { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: end; }
.chips { display: flex; flex-wrap: wrap; gap: var(--space-2); }
.chip {
  display: inline-flex; align-items: center; gap: var(--space-2);
  padding: var(--space-1) var(--space-3); border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: transparent; color: var(--muted);
  font-size: var(--text-sm); font-weight: 500; transition: all .2s var(--ease);
}
.chip .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--c, var(--accent)); opacity: .55; }
.chip:hover { border-color: var(--accent); color: var(--text); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.chip[aria-pressed="true"] .dot { opacity: 1; }
.chip .n { font-size: var(--text-xs); color: var(--muted); }
.field { display: grid; gap: var(--space-1); font-size: var(--text-xs); color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.field input, .field select {
  padding: var(--space-2) var(--space-3); border-radius: var(--radius-sm); border: 1px solid var(--border);
  background: var(--bg); color: var(--text); font-size: var(--text-sm); font-weight: 400; text-transform: none; letter-spacing: 0;
  min-width: 170px; height: 38px;
}
.field input:hover, .field select:hover { border-color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: var(--space-2); font-size: var(--text-sm); height: 38px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.applied { font-size: var(--text-xs); color: var(--muted); }

/* Cards */
.chart-card, .table-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-head { display: flex; justify-content: space-between; gap: var(--space-4); padding: var(--space-5) var(--space-5) var(--space-3); flex-wrap: wrap; align-items: start; }
.hint { font-size: var(--text-sm); color: var(--muted); margin-top: var(--space-1); max-width: 70ch; }
.count { color: var(--muted); font-weight: 500; font-size: var(--text-sm); margin-left: var(--space-2); }
.legend { display: flex; flex-wrap: wrap; gap: var(--space-2) var(--space-3); font-size: var(--text-xs); color: var(--muted); align-items: center; max-width: 380px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.chart-wrap { position: relative; height: clamp(340px, 52vh, 560px); padding: 0 var(--space-4) var(--space-4); }

/* Table */
.table-scroll { overflow-x: auto; overscroll-behavior: contain; border-radius: 0 0 var(--radius) var(--radius); }
table { width: 100%; border-collapse: separate; border-spacing: 0; font-size: var(--text-xs); min-width: 960px; }
thead th {
  position: sticky; top: 0; z-index: 1; background: var(--surface-2); color: var(--muted);
  font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .05em; font-weight: 600;
  text-align: left; padding: var(--space-3) var(--space-2); border-bottom: 1px solid var(--border);
  cursor: pointer; user-select: none; white-space: nowrap;
}
thead th.num { text-align: right; }
thead th[data-k]:hover { color: var(--text); }
thead th.sorted { color: var(--text); }
thead th.sorted::after { content: ' ↑'; color: var(--accent); }
thead th.sorted.desc::after { content: ' ↓'; }
tbody td { padding: var(--space-2) var(--space-2); border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
tbody td.num { text-align: right; }
tbody tr:hover { background: var(--surface-2); }
tbody tr.flash { animation: flash 1.6s var(--ease); }
td.model { color: var(--muted); }
td.model span { display: block; max-width: 140px; overflow: hidden; text-overflow: ellipsis; }
td.mat span { display: block; max-width: 96px; overflow: hidden; text-overflow: ellipsis; }
td.ref { font-weight: 600; }
td.ref .tag { display: inline-block; margin-left: var(--space-2); font-size: 10px; text-transform: uppercase; letter-spacing: .05em; padding: 1px 6px; border-radius: 999px; background: var(--down-soft); color: var(--down); vertical-align: middle; font-weight: 600; }
.coll-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: var(--space-2); vertical-align: middle; }
.delta { display: inline-block; min-width: 64px; text-align: right; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.delta.pos { color: var(--up); background: var(--up-soft); }
.delta.neg { color: var(--down); background: var(--down-soft); }
.delta.na { color: var(--muted); background: transparent; }
td.links a { margin-right: var(--space-2); font-weight: 600; }
.empty { padding: var(--space-8); text-align: center; color: var(--muted); }

.notes { color: var(--muted); font-size: var(--text-sm); max-width: 90ch; }
.notes h2 { color: var(--text); margin-bottom: var(--space-2); }
.notes ul { padding-left: var(--space-5); margin: 0; display: grid; gap: var(--space-2); }

.tooltip {
  position: fixed; z-index: 50; pointer-events: none;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow); padding: var(--space-3) var(--space-4); font-size: var(--text-xs); line-height: 1.45; min-width: 220px;
}
.tooltip b { font-size: var(--text-sm); display: block; }
.tooltip .row { display: flex; justify-content: space-between; gap: var(--space-4); }
.tooltip .row span:last-child { font-variant-numeric: tabular-nums; }
.tip-grid { display: flex; gap: var(--space-3); align-items: flex-start; }
.tip-img { flex: 0 0 112px; width: 112px; height: 154px; border-radius: 6px; background: #f2f0ea; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.tip-img img { width: 100%; height: 100%; object-fit: contain; display: block; }
.tip-body { min-width: 190px; }
[data-theme="dark"] .tip-img { background: #2a2a2e; }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes flash { 0% { background: var(--accent-soft); } 100% { background: transparent; } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

@media (max-width: 720px) {
  .header { padding: var(--space-3) var(--space-4); }
  .brand .sub { display: none; }
  .main { padding: var(--space-4) var(--space-4) var(--space-12); gap: var(--space-4); }
  .field input, .field select { min-width: 140px; flex: 1; }
  .field { flex: 1 1 45%; }
  .chart-wrap { height: 360px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--space-3); }
  .kpi { padding: var(--space-3) var(--space-4); }
  .kpi-value { font-size: var(--text-lg); }
  .legend { display: none; }
  #exportBtn { display: none; }
}

/* Thumbnails and lightbox */
th.thumb-cell, td.thumb-cell { width: 44px; padding-left: var(--space-3); padding-right: 0; }
.thumb { display: block; width: 36px; height: 48px; padding: 0; border: 1px solid var(--border); border-radius: 4px; background: #f2f0ea; cursor: zoom-in; overflow: hidden; transition: transform 120ms ease, box-shadow 120ms ease; }
.thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.thumb:hover, .thumb:focus-visible { transform: scale(1.08); box-shadow: var(--shadow); outline: none; border-color: var(--accent); }
[data-theme="dark"] .thumb { background: #2a2a2e; }
.lightbox[hidden] { display: none; }
.lightbox { position: fixed; inset: 0; z-index: 100; background: rgba(20, 18, 14, 0.72); display: flex; align-items: center; justify-content: center; padding: var(--space-6); backdrop-filter: blur(4px); animation: lb-in 160ms ease; }
@keyframes lb-in { from { opacity: 0; } to { opacity: 1; } }
.lb-panel { position: relative; background: var(--surface); color: var(--text); border-radius: var(--radius); box-shadow: 0 24px 64px rgba(0,0,0,0.35); padding: var(--space-5); max-width: min(92vw, 560px); max-height: 92vh; display: flex; flex-direction: column; align-items: center; gap: var(--space-3); }
.lb-panel img { max-width: 100%; max-height: calc(92vh - 150px); height: auto; object-fit: contain; border-radius: 6px; background: #f2f0ea; }
[data-theme="dark"] .lb-panel img { background: #2a2a2e; }
.lb-cap { text-align: center; font-size: var(--text-sm); display: flex; flex-direction: column; gap: 2px; }
.lb-cap b { font-size: var(--text-lg); }
.lb-cap span { color: var(--muted); font-size: var(--text-xs); }
.lb-cap em { font-style: normal; font-weight: 600; }
.lb-cap a { color: var(--accent-ink); font-size: var(--text-xs); margin-top: var(--space-2); }
.lb-close { position: absolute; top: 8px; right: 8px; width: 32px; height: 32px; border-radius: 50%; border: 1px solid var(--border); background: var(--surface); color: var(--text); font-size: 20px; line-height: 1; cursor: pointer; }
.lb-close:hover { border-color: var(--accent); }
