/* Tokens. Series colors are the validated categorical slots 1-3 (light and dark steps). */
:root {
  color-scheme: light;
  --bg: #f2f2f0;
  --panel: #ffffff;
  --panel-2: #f5f5f3;
  --border: #e8e7e3;
  --border-strong: #d4d3ce;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #8a8983;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --select-bg: #f1f0ec;
  --bar-fill: #dddcd6;
  --avg-bg: #fbf8ee;
  --avg-line: #b9b6a9;
  --neutral-dot: #c4c3bd;
  --focus: #2a78d6;
  --error-bg: #fdecec;
  --error-text: #8f1d1d;
  --radius: 10px;
  --topbar-h: 56px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0c0c0b;
    --panel: #151514;
    --panel-2: #1c1c1b;
    --border: #262624;
    --border-strong: #363633;
    --text-primary: #f0efec;
    --text-secondary: #c3c2b7;
    --text-muted: #8c8b83;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --select-bg: #1f1f1d;
    --bar-fill: #34342f;
    --avg-bg: #1e1c15;
    --avg-line: #5b5847;
    --neutral-dot: #55544f;
    --focus: #3987e5;
    --error-bg: #3a1a1a;
    --error-text: #f2b8b8;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text-primary);
  font: 13px/1.5 var(--font);
  font-feature-settings: "cv11", "ss01";
  -webkit-font-smoothing: antialiased;
}

button, select, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.muted { color: var(--text-muted); font-weight: 400; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.eyebrow { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-muted); }
.swatch { display: inline-block; flex: none; width: 8px; height: 8px; margin-right: 7px; border-radius: 50%; background: var(--chip); }
.empty { margin: 0; padding: 20px 0; color: var(--text-secondary); }
.error { grid-column: 1 / -1; margin: 0; padding: 12px 14px; border-radius: var(--radius); background: var(--error-bg); color: var(--error-text); }
.footnote { margin: 0; padding: 10px 14px; font-size: 12px; color: var(--text-muted); border-top: 1px solid var(--border); }

/* Top bar */
.shell { min-height: 100vh; display: flex; flex-direction: column; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 14px;
  min-height: var(--topbar-h);
  padding: 8px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 14px; font-weight: 650; white-space: nowrap; }
.brand-mark {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  background:
    linear-gradient(90deg, transparent 45%, rgb(255 255 255 / 0.85) 45%, rgb(255 255 255 / 0.85) 55%, transparent 55%),
    linear-gradient(135deg, var(--series-1), var(--series-3));
}
.topbar-sep { width: 1px; height: 24px; background: var(--border); }
.topbar-spacer { flex: 1; }
.topbar-source { font-size: 12px; color: var(--text-muted); white-space: nowrap; }

.picker { display: flex; align-items: center; height: 34px; border: 1px solid var(--border-strong); border-radius: 8px; background: var(--panel); }
.picker:hover { border-color: var(--text-muted); }
.picker:focus-within { outline: 2px solid var(--focus); outline-offset: 1px; }
.picker-label { padding: 0 2px 0 10px; font-size: 12px; color: var(--text-muted); }
.picker select {
  height: 100%;
  padding: 0 28px 0 6px;
  border: 0;
  background: transparent
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%238a8983' stroke-width='1.5'/%3E%3C/svg%3E")
    no-repeat right 10px center;
  font-weight: 600;
  appearance: none;
  cursor: pointer;
}
.picker select:focus-visible { outline: none; }

.loading-dot { font-size: 12px; color: var(--text-muted); }
.loading-dot::before {
  content: "";
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--series-1);
  animation: pulse 1s ease-in-out infinite alternate;
}
@keyframes pulse { from { opacity: 0.3; } to { opacity: 1; } }

/* Workspace: grid + inspector */
.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  align-items: start;
  gap: 16px;
  padding: 16px 24px 40px;
}
.grid-panel, .inspector {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
}
.grid-panel { overflow: hidden; transition: opacity 0.15s; }
.grid-panel.is-loading { opacity: 0.5; pointer-events: none; }

.grid-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 16px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
}
.grid-toolbar-right { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; }
.group-tabs { display: flex; gap: 2px; padding: 3px; border: 1px solid var(--border); border-radius: 9px; background: var(--panel-2); }
.group-tab { padding: 5px 12px; border: 0; border-radius: 6px; background: none; color: var(--text-secondary); font-weight: 500; white-space: nowrap; cursor: pointer; }
.group-tab:hover { color: var(--text-primary); }
.group-tab.is-active { background: var(--panel); color: var(--text-primary); font-weight: 600; box-shadow: 0 0 0 1px var(--border), 0 1px 2px rgb(0 0 0 / 0.06); }

.team-chips { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.control-label, .control-hint { font-size: 12px; color: var(--text-muted); }
.chip {
  display: inline-flex;
  align-items: center;
  height: 28px;
  padding: 0 2px 0 9px;
  border: 1px solid var(--border-strong);
  border-left: 3px solid var(--chip);
  border-radius: 7px;
  font-weight: 600;
  white-space: nowrap;
}
.chip-remove { width: 22px; height: 22px; margin-left: 4px; border: 0; border-radius: 5px; background: none; color: var(--text-muted); font-size: 15px; line-height: 1; cursor: pointer; }
.chip-remove:hover { background: var(--panel-2); color: var(--text-primary); }
.link-button { border: 0; background: none; color: var(--text-secondary); font-size: 12px; text-decoration: underline; cursor: pointer; }
.btn { height: 30px; padding: 0 12px; border: 1px solid var(--border-strong); border-radius: 7px; background: var(--panel); font-size: 12px; font-weight: 600; cursor: pointer; }
.btn:hover { background: var(--panel-2); }

/* Data grid */
.grid-scroll { overflow: auto; max-height: calc(100vh - var(--topbar-h) - 140px); }
.grid { width: 100%; border-collapse: separate; border-spacing: 0; font-variant-numeric: tabular-nums; }
.grid th, .grid td { padding: 0 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.grid td { height: 34px; }
.grid thead th {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 10px 12px 8px;
  vertical-align: bottom;
  background: var(--panel);
  border-bottom: 1px solid var(--border-strong);
  text-align: right;
}
.th-button { all: unset; display: grid; justify-items: end; gap: 2px; cursor: pointer; }
.th-button:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.th-label { max-width: 112px; white-space: normal; font-size: 12px; font-weight: 600; line-height: 1.3; color: var(--text-primary); }
.th-arrow { margin-left: 4px; }
.th-unit { font-size: 11px; font-weight: 400; color: var(--text-muted); white-space: nowrap; }
.grid thead th:hover { background: var(--panel-2); }
.grid thead th.is-selected { background: var(--select-bg); box-shadow: inset 0 2px 0 var(--text-primary); }
.grid td.is-selected { position: relative; min-width: 150px; background: var(--select-bg); font-weight: 600; }

/* Bars live left of the number, never under it. */
.cell-bar-track { position: absolute; left: 10px; right: 58px; top: 11px; bottom: 11px; }
.cell-bar { display: block; height: 100%; border-radius: 0 3px 3px 0; background: var(--bar-fill); }
tr.is-compared .cell-bar { background: color-mix(in srgb, var(--chip) 38%, transparent); }
.cell-value { position: relative; }

.sticky-col { position: sticky; left: 0; z-index: 1; background: var(--panel); border-right: 1px solid var(--border); text-align: left; }
.grid thead th.sticky-col { z-index: 3; text-align: left; }
.grid thead th.sticky-col .th-label, .grid thead th.sticky-col .th-unit { display: block; }
.team-col { min-width: 210px; }
.team-label { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.team-label input { width: 15px; height: 15px; margin: 0; accent-color: var(--chip, var(--text-primary)); cursor: pointer; }
.team-label input:disabled { cursor: not-allowed; }
.rank { width: 20px; text-align: right; color: var(--text-muted); font-size: 12px; }
.team-name { font-weight: 500; }

.grid tbody tr:hover td { background: var(--panel-2); }
.grid tbody tr:hover td.is-selected { background: var(--select-bg); }
tr.is-compared td { font-weight: 600; }
tr.is-compared td.sticky-col { box-shadow: inset 3px 0 0 var(--chip); }

.average-row td, .average-row:hover td {
  background: var(--avg-bg);
  border-top: 1px dashed var(--avg-line);
  border-bottom: 1px dashed var(--avg-line);
  color: var(--text-secondary);
  font-weight: 600;
}
.average-row td.is-selected { background: var(--avg-bg); color: var(--text-primary); }
.avg-label { padding-left: 55px; }

/* Inspector */
.inspector {
  position: sticky;
  top: calc(var(--topbar-h) + 16px);
  max-height: calc(100vh - var(--topbar-h) - 32px);
  overflow-y: auto;
  padding: 18px;
}
.inspector-title { margin: 4px 0 6px; font-size: 18px; font-weight: 650; line-height: 1.3; letter-spacing: -0.01em; }
.unit-chip { display: inline-block; padding: 1px 8px; border: 1px solid var(--border-strong); border-radius: 999px; font-size: 12px; color: var(--text-secondary); }
.defs { margin: 16px 0 0; }
.def { margin-bottom: 12px; }
.def dt { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-muted); }
.def dd { margin: 2px 0 0; color: var(--text-primary); }
.def code { font-family: var(--mono); font-size: 12px; padding: 2px 6px; border-radius: 5px; background: var(--panel-2); border: 1px solid var(--border); }
.league-line { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 10px 12px; border-radius: 8px; background: var(--avg-bg); border: 1px dashed var(--avg-line); }
.league-line b { font-size: 18px; font-variant-numeric: tabular-nums; }
.inspector-sub { margin: 20px 0 8px; font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--text-muted); }

.team-summaries { display: grid; gap: 8px; margin: 0; padding: 0; list-style: none; }
.team-summary { padding: 10px 12px; border: 1px solid var(--border); border-left: 3px solid var(--chip); border-radius: 8px; }
.team-summary-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.team-summary-name { display: flex; align-items: center; font-weight: 600; }
.team-summary-value { font-size: 20px; font-weight: 650; font-variant-numeric: tabular-nums; }
.facts { display: grid; gap: 2px; margin: 6px 0 0; font-size: 12px; }
.facts div { display: grid; grid-template-columns: 64px 1fr; gap: 8px; }
.facts dt { color: var(--text-muted); }
.facts dd { margin: 0; color: var(--text-secondary); }

.spread { padding-top: 22px; }
.spread-track { position: relative; height: 22px; margin: 0 6px; }
.spread-track::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px solid var(--border-strong); }
.spread-dot { position: absolute; top: 50%; width: 8px; height: 8px; border-radius: 50%; background: var(--neutral-dot); transform: translate(-50%, -50%); }
.spread-dot.is-compared { z-index: 2; width: 13px; height: 13px; box-shadow: 0 0 0 2px var(--panel); }
.spread-avg { position: absolute; top: 0; bottom: 0; z-index: 1; width: 2px; margin-left: -1px; background: var(--text-primary); }
.spread-avg-label { position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%); font-size: 11px; font-weight: 600; white-space: nowrap; }
.spread-ends { display: flex; justify-content: space-between; gap: 12px; margin-top: 6px; font-size: 12px; color: var(--text-secondary); }
.spread-ends b { color: var(--text-primary); font-variant-numeric: tabular-nums; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .inspector { position: static; max-height: none; }
  .grid-scroll { max-height: none; }
}

@media (max-width: 720px) {
  .topbar { position: static; flex-wrap: wrap; padding: 10px 16px; }
  .topbar-sep, .topbar-spacer, .topbar-source { display: none; }
  .brand { width: 100%; }
  .picker { flex: 1 1 150px; }
  .picker select { flex: 1; min-width: 0; }
  .workspace { padding: 12px 12px 32px; }
  .team-col { min-width: 160px; }
  .group-tabs { overflow-x: auto; max-width: 100%; }
}
