/* ── Reset & base ───────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: system-ui, -apple-system, sans-serif; background: #f0f2f5; color: #222; font-size: 14px; }

/* ── Header ─────────────────────────────────────────────────────────────── */
header {
  background: #1a3a5c; color: #fff; padding: .75rem 1.25rem;
  display: flex; align-items: center; gap: .75rem; position: sticky; top: 0; z-index: 50;
}
header h1 { font-size: 1.15rem; font-weight: 700; flex: 1; }
.ws-status { display: flex; align-items: center; gap: .4rem; font-size: .75rem; }
.status-dot { width: 11px; height: 11px; border-radius: 50%; background: #b0b0b0; border: 1px solid #fff; flex-shrink: 0; }
.status-dot.connected { background: #4caf50; }

/* ── Main ────────────────────────────────────────────────────────────────── */
main { max-width: 1500px; margin: 1rem auto; padding: 0 1rem; display: flex; flex-direction: column; gap: .75rem; }

/* ── Toolbar ─────────────────────────────────────────────────────────────── */
.toolbar {
  display: flex; justify-content: space-between; align-items: center;
  flex-wrap: wrap; gap: .5rem; background: #fff; padding: .6rem .75rem;
  border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,.1);
}
.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: .4rem; flex-wrap: wrap; }
.inline-label { font-weight: 600; white-space: nowrap; }
#race-select { padding: .35rem .5rem; border: 1px solid #ccc; border-radius: 4px; min-width: 220px; }

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
  padding: .38rem .75rem; border: none; border-radius: 4px; cursor: pointer;
  font-size: .85rem; font-weight: 500; white-space: nowrap; transition: filter .15s;
}
.btn:hover { filter: brightness(1.1); }
.btn-primary   { background: #1a3a5c; color: #fff; }
.btn-secondary { background: #5a6a7a; color: #fff; }
.btn-success   { background: #2e7d32; color: #fff; }
.btn-danger    { background: #c62828; color: #fff; }
.btn-sm  { padding: .28rem .55rem; font-size: .8rem; }
.btn-xs  { padding: .18rem .5rem;  font-size: .8rem; min-width: 40px; min-height: 40px; }
.now-btn { margin-top: .3rem; min-height: 40px; }

/* ── Panel ───────────────────────────────────────────────────────────────── */
.panel { background: #fff; border-radius: 6px; box-shadow: 0 1px 3px rgba(0,0,0,.1); overflow: hidden; }
.panel-title {
  background: #1a3a5c; color: #fff; padding: .5rem .75rem;
  display: flex; justify-content: space-between; align-items: center;
  font-weight: 600; font-size: .88rem;
}

/* ── Totals ──────────────────────────────────────────────────────────────── */
.totals { padding: .6rem .75rem; display: flex; gap: 2rem; flex-wrap: wrap; font-size: .9rem; }
.totals strong { font-weight: 700; }

/* ── Error banner ────────────────────────────────────────────────────────── */
#error-banner {
  display: none; background: #ffebee; color: #c62828;
  padding: .5rem .75rem; border-radius: 4px; border-left: 4px solid #c62828;
}

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .82rem; }
th {
  background: #e8eef5; padding: .45rem .5rem; text-align: left;
  white-space: nowrap; border-bottom: 2px solid #c0cfe0; font-weight: 600;
}
td { padding: .38rem .5rem; border-bottom: 1px solid #eee; vertical-align: middle; }
tr:hover td { background: #f4f8ff; }
tr.done td { opacity: .65; }
tr.in-progress td { background: #fff8e1; }
tr.in-progress td:first-child { font-weight: 700; }

/* ── Diff colours ────────────────────────────────────────────────────────── */
.ahead  { color: #2e7d32; font-weight: 700; }
.behind { color: #c62828; font-weight: 700; }

/* ── Badges ──────────────────────────────────────────────────────────────── */
.badge {
  display: inline-block; padding: .1rem .38rem; border-radius: 3px;
  font-size: .73rem; font-weight: 600; white-space: nowrap;
}
.src-computed { background: #e3f2fd; color: #1565c0; }
.src-manual   { background: #f3e5f5; color: #6a1b9a; }
.status-badge { display: inline-block; font-size: .75rem; white-space: nowrap; }
.s-done        { color: #555; }
.s-in_progress { color: #e65100; font-weight: 700; }
.s-pending     { color: #5f6368; }

/* ── Runner select (inline in legs table) ────────────────────────────────── */
.runner-select {
  max-width: 160px; padding: .18rem .3rem; font-size: .8rem;
  border: 1px solid #ccc; border-radius: 3px; background: #fff;
}

/* ── Handover cell ───────────────────────────────────────────────────────── */
.handover-cell { display: flex; align-items: center; gap: .25rem; }
.handover-cell input[type="time"] {
  width: 96px; padding: .2rem .3rem; font-size: .8rem;
  border: 1px solid #ccc; border-radius: 3px;
}
.real-label { font-size: .72rem; color: #555; margin-top: .15rem; }

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.45); z-index: 200;
  justify-content: center; align-items: center;
}
.modal-overlay.open { display: flex; }
.modal {
  background: #fff; border-radius: 8px; padding: 1.5rem;
  width: 90%; max-width: 440px; max-height: 90vh; overflow-y: auto;
  box-shadow: 0 4px 24px rgba(0,0,0,.2);
}
.modal-wide { max-width: 640px; }
.modal h2 { font-size: 1.1rem; margin-bottom: 1rem; color: #1a3a5c; }
.modal label {
  display: block; font-size: .85rem; font-weight: 500;
  margin-bottom: .65rem; color: #333;
}
.modal label input, .modal label select {
  display: block; width: 100%; margin-top: .2rem;
  padding: .38rem .5rem; border: 1px solid #ccc; border-radius: 4px;
  font-size: .9rem;
}
.modal label input:focus, .modal label select:focus {
  outline: 2px solid #1a3a5c; border-color: #1a3a5c;
}
.modal-actions { display: flex; justify-content: flex-end; gap: .5rem; margin-top: 1.25rem; }
.form-row { display: flex; gap: .75rem; flex-wrap: wrap; }
.form-row label { flex: 1; min-width: 120px; }
.field-narrow { flex: 0 0 90px !important; min-width: 0 !important; }

/* ── Leg type toggle ─────────────────────────────────────────────────────── */
.leg-type-group {
  display: flex; gap: 1.5rem; margin: .6rem 0 .9rem;
  padding: .5rem .75rem; background: #f5f7fa; border-radius: 4px;
}
.radio-label { display: flex !important; align-items: center; gap: .4rem; font-weight: 400 !important; cursor: pointer; }
.radio-label input { display: inline !important; width: auto !important; margin: 0 !important; }

/* ── Banners ─────────────────────────────────────────────────────────────── */
#offline-banner {
  background: #fff3cd; color: #664d03; padding: .5rem .75rem;
  border-radius: 4px; border-left: 4px solid #e0a800;
}
#offline-banner[hidden] { display: none; }
#last-updated { margin-left: .4rem; }

/* ── Sticky first column of legs table ───────────────────────────────────── */
#legs-table th:first-child, #legs-table td:first-child {
  position: sticky; left: 0; z-index: 1; background: #fff;
}
#legs-table th:first-child { background: #e8eef5; }
#legs-table tr.in-progress td:first-child { background: #fff8e1; }

/* ── Dark mode ───────────────────────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
  body { background: #121820; color: #e4e8ec; }
  .toolbar, .panel, .modal { background: #1c2430; color: #e4e8ec; }
  .modal h2 { color: #9cc3ee; }
  .modal label { color: #cfd6de; }
  th { background: #26324a; border-bottom-color: #3a4a66; }
  td { border-bottom-color: #2a3442; }
  tr:hover td { background: #243044; }
  tr.in-progress td, #legs-table tr.in-progress td:first-child { background: #3a3218; }
  #legs-table td:first-child { background: #1c2430; }
  #legs-table th:first-child { background: #26324a; }
  .modal label input, .modal label select, #race-select, .runner-select,
  .handover-cell input[type="time"], #vr-team-select {
    background: #121820; color: #e4e8ec; border-color: #3a4a66;
  }
  .leg-type-group { background: #26324a; }
  .ahead { color: #66bb6a; } .behind { color: #ef7b7b; }
  .s-done { color: #aab2bc; } .s-pending { color: #aab2bc; } .s-in_progress { color: #ffa65c; }
  .real-label { color: #aab2bc; }
  .src-computed { background: #1b3552; color: #90caf9; }
  .src-manual { background: #3a2548; color: #ce93d8; }
  #error-banner { background: #3b1c1f; color: #ffb4b4; }
  #offline-banner { background: #3a3218; color: #ffe08a; }
  .btn-primary { background: #2d5a8c; }
  .panel-title { background: #26324a; }
  #vr-loading { color: #aab2bc !important; }
}

/* Now panel */
#now-panel { padding: .75rem 1rem; }
.now-grid { display: flex; flex-wrap: wrap; gap: .5rem 2rem; align-items: baseline; }
.now-cd { font-size: 1.8rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.now-muted { color: #666; font-size: .9rem; }
.ahead { color: #2e7d32; } .behind { color: #c62828; }
#toast { position: fixed; bottom: 1rem; left: 50%; transform: translateX(-50%); background: #222; color: #fff;
  padding: .5rem 1rem; border-radius: 4px; display: none; z-index: 1000; }

/* Spectator mode (?view=1) */
body.view-mode .toolbar, body.view-mode #runners-panel, body.view-mode #summary-panel,
body.view-mode .handover-cell, body.view-mode .now-btn,
body.view-mode .panel-title .btn, body.view-mode #legs-table th:last-child, body.view-mode #legs-table td:last-child
  { display: none !important; }
body.view-mode .runner-select { appearance: none; border: 0; background: none; pointer-events: none; padding: 0; }
