:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f3f2ef;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-soft: rgba(42, 120, 214, 0.10);
  --good: #0ca30c;
  --good-text: #006300;
  --critical: #d03b3b;
  --warning-bg: #fff6df;
  --warning-border: #f0d48a;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 8px 24px rgba(0, 0, 0, 0.06);
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --accent-soft: rgba(57, 135, 229, 0.16);
    --good-text: #0ca30c;
    --warning-bg: #2a2412;
    --warning-border: #5c4a17;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --accent: #3987e5;
  --accent-soft: rgba(57, 135, 229, 0.16);
  --good-text: #0ca30c;
  --warning-bg: #2a2412;
  --warning-border: #5c4a17;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.45;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button, select, input {
  font: inherit;
  color: inherit;
}

/* ---------------- Top bar ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 24px;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 650;
  font-size: 15px;
  cursor: pointer;
  white-space: nowrap;
}
.brand-mark {
  width: 22px; height: 22px;
  border-radius: 6px;
  background: linear-gradient(135deg, #2a78d6, #6250d6);
  display: grid; place-items: center;
  color: white; font-size: 13px;
}
.brand .scenario {
  font-weight: 500;
  color: var(--text-secondary);
  font-size: 13px;
}

.data-source {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  min-width: 0;
}
.data-source label { color: var(--text-muted); font-size: 12px; }
.data-source select,
.data-source input {
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  max-width: 420px;
  min-width: 0;
}
.data-source input { width: 300px; }

.btn {
  height: 32px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: white; border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { height: 26px; padding: 0 8px; font-size: 12px; }

/* ---------------- Layout ---------------- */

main {
  max-width: 1480px;
  margin: 0 auto;
  padding: 20px 24px 64px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px 18px;
  min-width: 0;
}
.card h3 {
  margin: 0 0 4px;
  font-size: 14px;
  font-weight: 650;
}
.card .sub {
  color: var(--text-muted);
  font-size: 12px;
  margin: 0 0 10px;
}

.grid { display: grid; gap: 16px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.kpis { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }
@media (max-width: 1000px) {
  .grid.cols-2, .grid.cols-3 { grid-template-columns: minmax(0, 1fr); }
}

.section-title {
  margin: 28px 0 10px;
  font-size: 16px;
  font-weight: 650;
  display: flex;
  align-items: baseline;
  gap: 10px;
}
.section-title .hint { font-size: 12px; font-weight: 400; color: var(--text-muted); }

.banner {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  background: var(--warning-bg);
  border: 1px solid var(--warning-border);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin-bottom: 16px;
  font-size: 13px;
  color: var(--text-secondary);
}
.banner .icon { font-size: 15px; line-height: 1.3; }
.banner .close { margin-left: auto; }

.loading, .error {
  padding: 60px 0;
  text-align: center;
  color: var(--text-muted);
}
.error { color: var(--critical); }

/* ---------------- Controls ---------------- */

.controls {
  display: flex;
  align-items: center;
  gap: 18px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.control-label {
  font-size: 12px;
  color: var(--text-muted);
  margin-right: 8px;
}
.segmented {
  display: inline-flex;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 2px;
}
.segmented button {
  border: 0;
  background: transparent;
  padding: 5px 14px;
  border-radius: 7px;
  cursor: pointer;
  color: var(--text-secondary);
}
.segmented button[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text-primary);
  font-weight: 600;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
}
.toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
  user-select: none;
}
.toggle input { accent-color: var(--accent); margin: 0; }

/* ---------------- Overview ---------------- */

.overview-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 16px;
}
@media (max-width: 1100px) {
  .overview-layout { grid-template-columns: minmax(0, 1fr); }
}
.scatter-card { padding: 8px 8px 4px; }
#scatter { width: 100%; height: min(72vh, 760px); min-height: 460px; }

.legend { display: flex; flex-direction: column; gap: 2px; }
.legend-title {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: flex;
  justify-content: space-between;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
  border: 0;
  background: transparent;
  text-align: left;
  width: 100%;
}
.legend-item:hover { background: var(--surface-2); }
.legend-item.off { opacity: 0.4; }
.legend-item .swatch { flex: none; }
.legend-item .name { flex: 1; font-size: 13px; color: var(--text-primary); }
.legend-item .count { font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.legend-note {
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.legend-note .row { display: flex; gap: 8px; align-items: center; }

.best-card {
  display: flex;
  gap: 14px;
  align-items: center;
  cursor: pointer;
}
.best-card:hover { border-color: var(--accent); }
.best-card .star { font-size: 28px; color: #eda100; }

/* ---------------- Tooltip ---------------- */

.tooltip {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
  padding: 12px 14px;
  width: 330px;
  font-size: 12px;
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.12s, transform 0.12s;
}
.tooltip.show { opacity: 1; transform: none; }
.tt-title { font-weight: 650; font-size: 13px; margin-bottom: 4px; }
.tt-params { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 10px; }
.tt-metrics { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 10px; }
.tt-metric .v { font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; }
.tt-metric .l { color: var(--text-muted); font-size: 11px; }
.tt-section { color: var(--text-muted); font-size: 11px; margin: 4px 0 6px; text-transform: uppercase; letter-spacing: 0.04em; }
.tt-rows { display: grid; grid-template-columns: 14px 1fr auto auto; gap: 3px 8px; align-items: center; font-variant-numeric: tabular-nums; }
.tt-rows .val { font-weight: 600; text-align: right; }
.tt-rows .pct { color: var(--text-muted); text-align: right; min-width: 34px; }
.tt-foot { margin-top: 10px; color: var(--text-muted); font-size: 11px; display: flex; justify-content: space-between; }
.line-key { width: 12px; height: 3px; border-radius: 2px; display: inline-block; }

/* ---------------- Chips & badges ---------------- */

.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}
.chip b { color: var(--text-primary); font-weight: 600; }
.chip .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.badge.good { color: var(--good-text); background: color-mix(in srgb, var(--good) 14%, transparent); }
.badge.bad { color: var(--critical); background: color-mix(in srgb, var(--critical) 13%, transparent); }
.badge.neutral { color: var(--text-secondary); background: var(--surface-2); }
.badge.accent { color: var(--accent); background: var(--accent-soft); }
.badge.gold { color: #9a6a00; background: rgba(237, 161, 0, 0.16); }
:root[data-theme="dark"] .badge.gold { color: #f2c04d; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .badge.gold { color: #f2c04d; } }

.mode-tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

/* ---------------- Details page ---------------- */

.crumbs {
  display: flex;
  gap: 8px;
  align-items: center;
  color: var(--text-muted);
  font-size: 13px;
  margin-bottom: 12px;
}

.detail-header {
  display: flex;
  justify-content: space-between;
  gap: 24px;
  flex-wrap: wrap;
  margin-bottom: 18px;
}
.detail-header h1 {
  margin: 0 0 8px;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: -0.01em;
}
.detail-header .params { display: flex; gap: 8px; flex-wrap: wrap; }
.detail-header .task-text {
  margin-top: 10px;
  color: var(--text-secondary);
  font-style: italic;
  max-width: 760px;
}
.nav-arrows { display: flex; gap: 6px; align-items: flex-start; }

.kpi .label { font-size: 12px; color: var(--text-muted); }
.kpi .value {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
  margin: 2px 0;
}
.kpi .value .unit { font-size: 15px; font-weight: 500; color: var(--text-secondary); margin-left: 2px; }
.kpi .foot { font-size: 12px; color: var(--text-secondary); }
.meter {
  height: 6px;
  background: var(--surface-2);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 8px;
}
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 3px; }

.pipeline {
  display: flex;
  align-items: stretch;
  gap: 8px;
  flex-wrap: wrap;
}
.pipeline .step {
  flex: 1 1 180px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--surface-2);
  font-size: 13px;
}
.pipeline .step .n { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em; }
.pipeline .arrow { align-self: center; color: var(--text-muted); }

.chart { width: 100%; height: 240px; }
.chart.tall { height: 300px; }

/* Matrix (paradigm x task x horizon) */
.matrix { border-collapse: separate; border-spacing: 3px; width: 100%; font-size: 12px; }
.matrix th { color: var(--text-muted); font-weight: 500; padding: 4px; text-align: center; }
.matrix th.row { text-align: right; }
.matrix td {
  text-align: center;
  padding: 8px 4px;
  border-radius: 6px;
  cursor: pointer;
  font-variant-numeric: tabular-nums;
  border: 2px solid transparent;
  transition: transform 0.08s;
}
.matrix td:hover { transform: scale(1.04); }
.matrix td.current { border-color: var(--text-primary); }
.matrix td .t { font-size: 11px; opacity: 0.8; }

/* ---------------- Tables ---------------- */

.table-wrap { overflow-x: auto; }
table.data {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}
table.data th {
  text-align: left;
  font-weight: 600;
  font-size: 12px;
  color: var(--text-muted);
  padding: 8px 10px;
  border-bottom: 1px solid var(--axis);
  white-space: nowrap;
  user-select: none;
}
table.data th.sortable { cursor: pointer; }
table.data th.sortable:hover { color: var(--text-primary); }
table.data th .arrow { opacity: 0.7; margin-left: 4px; }
table.data td {
  padding: 9px 10px;
  border-bottom: 1px solid var(--grid);
  vertical-align: top;
}
table.data td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
table.data tr.clickable { cursor: pointer; }
table.data tr.clickable:hover td { background: var(--surface-2); }
table.data tr.open td { background: var(--accent-soft); }
table.data tr.expand > td { padding: 0; background: var(--surface-2); }

.clamp {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  color: var(--text-secondary);
  max-width: 520px;
}

.stackbar {
  display: flex;
  height: 10px;
  gap: 2px;
  border-radius: 4px;
  overflow: hidden;
  min-width: 40px;
}
.stackbar > span { height: 100%; }
.stackbar > span:first-child { border-radius: 4px 0 0 4px; }
.stackbar > span:last-child { border-radius: 0 4px 4px 0; }
.stackbar > span:only-child { border-radius: 4px; }
.stackcell { display: flex; align-items: center; gap: 10px; min-width: 200px; }
.stackcell .track { flex: 1; }

/* Expanded batch panel */
.batch-panel { padding: 16px 18px 20px; display: flex; flex-direction: column; gap: 16px; }
.batch-panel .toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
}
@media (max-width: 900px) { .mode-grid { grid-template-columns: minmax(0, 1fr); } }
.mode-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.mode-card.selected { box-shadow: 0 0 0 2px var(--accent); }
.mode-card video {
  width: 100%;
  aspect-ratio: 1600 / 600;
  background: #000;
  display: block;
}
.mode-card .meta { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.mode-card .meta .top { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.mode-card .narration { color: var(--text-secondary); font-size: 13px; }
.mode-card .sim { display: flex; align-items: center; gap: 8px; font-size: 12px; font-variant-numeric: tabular-nums; }
.mode-card .sim .bar { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.mode-card .sim .bar span { display: block; height: 100%; border-radius: 3px; }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; font-size: 13px; }
.kv dt { color: var(--text-muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.kv dd { margin: 0; color: var(--text-secondary); }

.quote {
  border-left: 3px solid var(--accent);
  padding: 4px 0 4px 12px;
  color: var(--text-secondary);
}

/* ---------------- Modal ---------------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 24px;
}
.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: min(900px, 100%);
  max-height: 86vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.35);
}
.modal header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.modal header h3 { margin: 0; font-size: 15px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.modal header .spacer { flex: 1; }
.modal pre {
  margin: 0;
  padding: 16px 18px;
  overflow: auto;
  white-space: pre-wrap;
  font-size: 12.5px;
  line-height: 1.55;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.placeholder {
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 4px;
  padding: 0 3px;
}
.filled {
  background: rgba(27, 175, 122, 0.16);
  border-radius: 4px;
  padding: 0 3px;
}

.prompt-list { display: flex; gap: 8px; flex-wrap: wrap; }

.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.nowrap { white-space: nowrap; }
.spacer { flex: 1; }

details.table-view summary {
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 13px;
  padding: 6px 0;
}

@media (max-width: 640px) {
  .topbar { padding: 10px 16px; }
  main { padding: 16px 16px 48px; }
  .data-source { margin-left: 0; width: 100%; }
  .data-source input { width: 100%; }
}
