/* Tennis match analysis UI.
   Palette roles: two categorical slots for the two players (identity), one
   sequential blue ramp for occupancy heatmaps (magnitude). Both modes selected,
   declared under the OS query and the [data-theme] toggle scope. */

:root {
  color-scheme: light;
  --page:            #f9f9f7;
  --surface-1:       #fcfcfb;
  --surface-2:       #f2f1ed;
  --text-primary:    #0b0b0b;
  --text-secondary:  #52514e;
  --text-muted:      #898781;
  --hairline:        rgba(11, 11, 11, 0.10);
  --gridline:        #e1e0d9;
  --baseline:        #c3c2b7;

  --player-1:        #2a78d6;   /* categorical slot 1 - near side */
  --player-2:        #eb6834;   /* categorical slot 2 - far side */
  --accent:          #2a78d6;
  --accent-strong:   #1c5cab;
  --wash:            rgba(42, 120, 214, 0.08);
  --track:           #cde2fb;

  --good:            #0ca30c;
  --warning:         #fab219;
  --critical:        #d03b3b;

  --court-line:      #898781;
  --court-fill:      #eceae2;

  --radius:          12px;
  --radius-sm:       8px;
  --sans: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "Cascadia Mono", Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --page: #0d0d0d; --surface-1: #1a1a19; --surface-2: #232322;
    --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #898781;
    --hairline: rgba(255, 255, 255, 0.10); --gridline: #2c2c2a; --baseline: #383835;
    --player-1: #3987e5; --player-2: #d95926;
    --accent: #3987e5; --accent-strong: #86b6ef;
    --wash: rgba(57, 135, 229, 0.14); --track: #184f95;
    --court-line: #898781; --court-fill: #232322;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface-1: #1a1a19; --surface-2: #232322;
  --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #898781;
  --hairline: rgba(255, 255, 255, 0.10); --gridline: #2c2c2a; --baseline: #383835;
  --player-1: #3987e5; --player-2: #d95926;
  --accent: #3987e5; --accent-strong: #86b6ef;
  --wash: rgba(57, 135, 229, 0.14); --track: #184f95;
  --court-line: #898781; --court-fill: #232322;
}

* { box-sizing: border-box; }
body {
  margin: 0; font-family: var(--sans); background: var(--page);
  color: var(--text-primary); line-height: 1.5; -webkit-font-smoothing: antialiased;
}
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* Top bar ---------------------------------------------------------------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  flex-wrap: wrap; padding: 0.75rem clamp(1rem, 4vw, 2rem);
  border-bottom: 1px solid var(--hairline); background: var(--surface-1);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 0.6rem; }
.brand-mark { width: 10px; height: 22px; border-radius: 0 4px 4px 0; background: var(--accent); }
.topbar h1 { font-size: 1rem; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.topbar-actions { display: flex; align-items: center; gap: 0.5rem; }

.pill {
  display: inline-flex; align-items: center; gap: 0.45rem; font: inherit;
  font-size: 0.8125rem; color: var(--text-secondary); background: transparent;
  border: 1px solid var(--hairline); border-radius: 999px;
  padding: 0.3rem 0.7rem; cursor: pointer;
}
.pill:hover { background: var(--wash); }
.pill-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.pill[data-state="ok"] .pill-dot { background: var(--good); }
.pill[data-state="error"] .pill-dot { background: var(--critical); }
.pill[data-state="busy"] .pill-dot { background: var(--warning); animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.25; } }
@media (prefers-reduced-motion: reduce) { .pill[data-state="busy"] .pill-dot { animation: none; } }

.icon-btn {
  font: inherit; font-size: 1rem; line-height: 1; color: var(--text-secondary);
  background: transparent; border: 1px solid var(--hairline);
  border-radius: var(--radius-sm); padding: 0.4rem 0.55rem; cursor: pointer;
}
.icon-btn:hover { background: var(--wash); color: var(--text-primary); }

/* Steps ------------------------------------------------------------------ */
.steps {
  display: flex; gap: 0.25rem; padding: 0.5rem clamp(1rem, 4vw, 2rem) 0;
  max-width: 1240px; margin: 0 auto; overflow-x: auto;
}
.step-tab {
  font: inherit; font-size: 0.875rem; color: var(--text-secondary);
  background: transparent; border: 0; border-bottom: 2px solid transparent;
  padding: 0.6rem 0.85rem; cursor: pointer; white-space: nowrap;
  display: flex; align-items: center; gap: 0.45rem;
}
.step-tab[aria-selected="true"] { color: var(--text-primary); border-bottom-color: var(--accent); font-weight: 600; }
.step-tab:disabled { opacity: 0.4; cursor: not-allowed; }
.step-num {
  display: grid; place-items: center; width: 18px; height: 18px; flex: none;
  border-radius: 50%; background: var(--surface-2); color: var(--text-secondary);
  font-size: 0.6875rem; font-weight: 600;
}
.step-tab[aria-selected="true"] .step-num { background: var(--accent); color: #fff; }
.step-tab[data-done="true"] .step-num { background: var(--good); color: #fff; }

main { max-width: 1240px; margin: 0 auto; padding: clamp(1rem, 3vw, 1.75rem); }
.panel[hidden] { display: none; }
.grid { display: grid; gap: clamp(1rem, 2vw, 1.25rem); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); align-items: start; }
.card {
  background: var(--surface-1); border: 1px solid var(--hairline);
  border-radius: var(--radius); padding: clamp(0.875rem, 2vw, 1.25rem); min-width: 0;
}
.card-title {
  margin: 0 0 0.75rem; font-size: 0.75rem; font-weight: 600; letter-spacing: 0.06em;
  text-transform: uppercase; color: var(--text-muted);
}
.card-note { margin: 0 0 0.75rem; font-size: 0.8125rem; color: var(--text-secondary); }
.hint { font-size: 0.8125rem; color: var(--text-muted); margin: 0.5rem 0 0; }

/* Forms & buttons -------------------------------------------------------- */
.row { display: flex; gap: 0.6rem; align-items: flex-end; flex-wrap: wrap; }
.field { display: block; min-width: 0; }
.field > span { display: block; font-size: 0.8125rem; font-weight: 500; margin-bottom: 0.3rem; }
.field small { display: block; margin-top: 0.3rem; font-size: 0.75rem; color: var(--text-muted); }
.field input, .field select {
  font: inherit; font-size: 0.875rem; width: 100%; color: var(--text-primary);
  background: var(--page); border: 1px solid var(--hairline);
  border-radius: var(--radius-sm); padding: 0.45rem 0.6rem;
}
.field-sm { width: 6.5rem; flex: none; }
.field-wide { width: 11rem; flex: none; }
/* Checkboxes read as "box then label", not label-above-box. */
.field-check { display: flex; align-items: center; gap: 0.4rem; flex: none; }
.field-check > span { margin: 0; order: 2; }
.field-check input { width: auto; }
.field input:focus-visible, .field select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent);
}
.btn {
  font: inherit; font-size: 0.875rem; font-weight: 500; border-radius: var(--radius-sm);
  padding: 0.5rem 0.9rem; border: 1px solid transparent; cursor: pointer;
}
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-strong); }
.btn-ghost { background: transparent; border-color: var(--hairline); color: var(--text-secondary); }
.btn-ghost:hover:not(:disabled) { background: var(--wash); color: var(--text-primary); }
.btn-danger { background: transparent; border-color: var(--critical); color: var(--critical); }
.btn-sm { font-size: 0.8125rem; padding: 0.3rem 0.6rem; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.notice {
  border: 1px solid var(--hairline); border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm); padding: 0.7rem 0.85rem; font-size: 0.8125rem;
  color: var(--text-secondary); margin: 0 0 0.875rem;
}
.notice strong { color: var(--text-primary); }
.notice[data-kind="warn"] { border-left-color: var(--warning); }
.notice[data-kind="error"] { border-left-color: var(--critical); }
.notice[data-kind="good"] { border-left-color: var(--good); }
.notice p { margin: 0.35rem 0 0; }

/* Lists ------------------------------------------------------------------ */
.list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.4rem; max-height: 320px; overflow-y: auto; }
.list-item {
  display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem 0.6rem;
  border: 1px solid var(--hairline); border-radius: var(--radius-sm);
  background: var(--page); font-size: 0.8125rem; cursor: pointer; text-align: left;
  width: 100%; font-family: inherit; color: var(--text-primary);
}
.list-item:hover { background: var(--wash); }
.list-item[aria-current="true"] { border-color: var(--accent); background: var(--wash); }
.list-item .grow { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .meta { color: var(--text-muted); font-size: 0.75rem; flex: none; }
.tag {
  font-size: 0.6875rem; padding: 0.1rem 0.35rem; border-radius: 4px;
  background: var(--surface-2); color: var(--text-secondary); flex: none;
}

/* Calibration ------------------------------------------------------------ */
.stage {
  position: relative; background: var(--surface-2); border-radius: var(--radius-sm);
  overflow: hidden; line-height: 0;
}
.stage img { width: 100%; height: auto; display: block; }
.stage svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.stage.is-picking { cursor: crosshair; }
.stage-empty { padding: 3rem 1rem; text-align: center; color: var(--text-muted); font-size: 0.875rem; line-height: 1.5; }
.mark { fill: none; stroke: var(--accent); stroke-width: 2; vector-effect: non-scaling-stroke; }
.mark-dot { fill: var(--accent); stroke: var(--surface-1); stroke-width: 2; vector-effect: non-scaling-stroke; }
.mark-label { fill: var(--text-primary); font-size: 11px; font-family: var(--sans); paint-order: stroke; stroke: var(--surface-1); stroke-width: 3px; }
.mark-seg { stroke: var(--player-2); stroke-width: 1.5; opacity: 0.5; vector-effect: non-scaling-stroke; }
.mark-poly { stroke: var(--player-2); stroke-width: 2; fill: none; vector-effect: non-scaling-stroke; }

.landmark-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 0.3rem; }
.landmark-btn {
  font: inherit; font-size: 0.75rem; text-align: left; padding: 0.35rem 0.5rem;
  border: 1px solid var(--hairline); border-radius: 6px; background: var(--page);
  color: var(--text-secondary); cursor: pointer; display: flex; justify-content: space-between; gap: 0.3rem;
}
.landmark-btn:hover { background: var(--wash); }
.landmark-btn[aria-pressed="true"] { border-color: var(--accent); color: var(--text-primary); background: var(--wash); }
.landmark-btn[data-set="true"] { border-color: var(--good); }
.landmark-btn[data-set="true"]::after { content: "✓"; color: var(--good); }

/* Stats ------------------------------------------------------------------ */
.stat-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(118px, 1fr)); gap: 0.75rem 1rem; margin: 0; }
.stat dt { font-size: 0.75rem; color: var(--text-muted); margin-bottom: 0.1rem; }
.stat dd { margin: 0; font-size: 1.0625rem; font-weight: 600; overflow-wrap: anywhere; }
.stat dd small { font-size: 0.75rem; font-weight: 400; color: var(--text-secondary); }

.legend { display: flex; gap: 1rem; flex-wrap: wrap; margin: 0 0 0.6rem; font-size: 0.8125rem; color: var(--text-secondary); }
.legend-item { display: inline-flex; align-items: center; gap: 0.4rem; }
.legend-key { width: 14px; height: 3px; border-radius: 2px; flex: none; }

table.data { border-collapse: collapse; width: 100%; font-size: 0.8125rem; }
table.data th, table.data td {
  text-align: left; padding: 0.4rem 0.6rem; border-bottom: 1px solid var(--gridline); white-space: nowrap;
}
table.data th { color: var(--text-muted); font-weight: 500; }
table.data td { color: var(--text-secondary); font-variant-numeric: tabular-nums; }
table.data td.name { color: var(--text-primary); }
table.data tbody tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

.progress-track { height: 8px; border-radius: 0 4px 4px 0; background: var(--track); overflow: hidden; margin: 0.5rem 0; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); border-radius: 0 4px 4px 0; transition: width 300ms ease; }
@media (prefers-reduced-motion: reduce) { .progress-fill { transition: none; } }

.timeline { display: block; width: 100%; height: 42px; }
.foot { max-width: 1240px; margin: 0 auto; padding: 0 clamp(1rem, 3vw, 2rem) 2rem; font-size: 0.75rem; color: var(--text-muted); }

/* Dialog ----------------------------------------------------------------- */
.dialog {
  border: 1px solid var(--hairline); border-radius: var(--radius);
  background: var(--surface-1); color: var(--text-primary);
  padding: clamp(1rem, 3vw, 1.5rem); max-width: 460px; width: calc(100% - 2rem);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
.dialog-title { margin: 0 0 0.35rem; font-size: 1.0625rem; }
.dialog-note { margin: 0 0 1rem; font-size: 0.8125rem; color: var(--text-muted); }
.dialog .field { margin-bottom: 0.875rem; }
.dialog code { font-family: var(--mono); font-size: 0.95em; background: var(--page); border-radius: 4px; padding: 0.05em 0.3em; }
.dialog-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 1.25rem; }
.dialog-actions .btn-ghost:first-child { margin-right: auto; }
.probe { margin: 0; font-size: 0.8125rem; overflow-wrap: anywhere; }
.probe[data-state="ok"] { color: var(--good); }
.probe[data-state="error"] { color: var(--critical); }

/* Preview player ---------------------------------------------------------- */
.video-wrap {
  position: relative; border-radius: var(--radius); overflow: hidden;
  background: #000; border: 1px solid var(--hairline);
  aspect-ratio: 16 / 9; display: grid; place-items: center;
}
.video-wrap video {
  display: block; width: 100%; height: 100%; object-fit: contain; background: #000;
}
/* The <video> starts with no src; hiding it stops browsers painting a black
   box over the "run the analysis" hint before there is anything to play. */
.video-wrap video:not([src]) { display: none; }
.video-wrap .hint { margin: 0; padding: 1rem; text-align: center; }
