:root {
  color-scheme: light dark;
  --bg: #fff;
  --bg-elevated: #fff;
  --bg-muted: #fafafa;
  --bg-code: #f4f4f4;
  --text: #222;
  --text-muted: #444;
  --text-dim: #666;
  --text-faint: #888;
  --border: #eee;
  --border-strong: #ddd;
  --border-mark: #444;
  --toast-bg: #222;
  --toast-text: #fff;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1a1a1a;
    --bg-elevated: #242424;
    --bg-muted: #2a2a2a;
    --bg-code: #2e2e2e;
    --text: #eee;
    --text-muted: #ccc;
    --text-dim: #aaa;
    --text-faint: #888;
    --border: #333;
    --border-strong: #444;
    --border-mark: #888;
    --toast-bg: #eee;
    --toast-text: #222;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; font-family: ui-sans-serif, system-ui, sans-serif; }
body { display: grid; grid-template-rows: auto 1fr auto; height: 100vh; height: 100dvh; background: var(--bg); color: var(--text); }

.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}

header {
  border-bottom: 1px solid var(--border); background: var(--bg-elevated);
}
.header-bar {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 8px 16px;
}
header h1 { font-size: 16px; margin: 0; font-weight: 600; flex: 1; }
#pane-toggle {
  background: transparent; border: 1px solid var(--border-strong);
  color: var(--text); font-size: 16px; line-height: 1;
  padding: 4px 8px; border-radius: 4px; cursor: pointer;
}
#race-selector-wrap { flex: 0 1 auto; }
#race-selector { font-size: 13px; padding: 4px 6px; background: var(--bg-elevated); color: var(--text); border: 1px solid var(--border-strong); }

#race-info {
  font-size: 12px; color: var(--text-dim);
  padding: 4px 16px 8px;
  border-top: 1px solid var(--border);
  background: var(--bg-elevated);
  display: flex; gap: 6px; flex-wrap: wrap; align-items: baseline;
}
#race-info[hidden] { display: none; }
#race-info .race-info-sep { color: var(--text-faint); }
#race-info .race-info-label { color: var(--text-faint); text-transform: uppercase; font-size: 10px; letter-spacing: 0.05em; }

#header-expand {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 16px;
  align-items: center;
  padding: 8px 16px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-muted);
}
body[data-header-expand="closed"] #header-expand { display: none; }

#model-picker {
  border: 0; padding: 0; margin: 0;
  display: flex; flex-wrap: wrap; gap: 4px 8px;
}
#model-picker label {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 14px; cursor: pointer;
  padding: 4px 10px; border-radius: 4px;
  border: 1px solid var(--border-strong); background: var(--bg-elevated);
}
#model-picker label:hover { background: var(--bg); }
#model-picker input[type="radio"] { margin: 0; }
#model-picker label:has(input:checked) {
  background: var(--text); color: var(--bg);
  border-color: var(--text);
}

.use-current-row {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 12px;
  margin-top: 4px;
}
.use-current-label {
  opacity: 0.85;
}

#model-status {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; line-height: 1.4; color: var(--text-muted);
  text-align: right;
}
#status-run { color: var(--text); }
#status-frame { color: var(--text-dim); }

#main-panes { display: grid; grid-template-columns: 1fr; min-height: 0; }
body[data-mode="race"] #main-panes { grid-template-columns: 1fr 1fr; }
body[data-mode="race-past"] #main-panes { grid-template-columns: 1fr; }
body[data-mode="race-past"] #chart-pane { display: none; }

#chart-pane { display: grid; grid-template-rows: 1fr auto; min-height: 0; }
#analysis-pane { overflow-y: auto; padding: 12px 16px; border-left: 1px solid var(--border); min-height: 0; }
body[data-mode="race-past"] #analysis-pane { border-left: none; }

#map { width: 100%; height: 100%; background: var(--bg); }
#map.glyph-hover, #map.glyph-hover .leaflet-interactive { cursor: pointer !important; }
.leaflet-container { background: var(--bg) !important; }

#chart-footer {
  padding: 8px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-muted);
}
#slider-row {
  display: flex; align-items: center; gap: 8px;
}
#slider { flex: 1; width: 100%; }
#play-btn {
  flex: 0 0 auto;
  width: 32px; height: 32px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 4px;
  font-size: 14px; line-height: 1;
  cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
}
#play-btn:disabled { opacity: 0.4; cursor: default; }
#play-btn[aria-pressed="true"] { background: var(--accent, #c0392b); color: #fff; border-color: var(--accent, #c0392b); }
#slider-labels {
  display: flex; justify-content: space-between;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px; color: var(--text-muted); margin-top: 4px;
}
#slider-current { color: var(--text); font-weight: 600; }

/* Subtle loading hint while an uncached frame is being fetched. */
body[data-frame-loading="true"] #slider-current {
  animation: frame-loading-pulse 1.1s ease-in-out infinite;
}
@keyframes frame-loading-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}

.glyph-popup-wrap .leaflet-popup-content-wrapper {
  background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 6px;
}
.glyph-popup-wrap .leaflet-popup-tip { background: var(--bg-elevated); }
.glyph-popup { font-size: 12px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.glyph-popup-title { font-weight: 600; margin-bottom: 2px; }
.glyph-popup-loc { color: var(--text-dim); font-weight: 400; margin-left: 6px; }
.glyph-popup-now { color: var(--text-muted); margin-bottom: 6px; }
.glyph-popup-chart { color: var(--text-faint); overflow: hidden; }
.glyph-sparkline .spark-grid { stroke: var(--border); stroke-width: 0.5; }
.glyph-sparkline .spark-day { stroke: var(--text-dim); stroke-width: 0.75; stroke-dasharray: 3 3; opacity: 0.55; }
.glyph-sparkline .spark-zero { stroke: var(--border-strong, var(--border)); stroke-width: 0.75; }
.glyph-sparkline .spark-label { fill: var(--text-dim); font-size: 10px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

#glyph-tooltip {
  position: fixed; z-index: 9000; pointer-events: none;
  background: var(--bg-elevated); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: 4px;
  padding: 4px 8px; font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap; box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

#toast {
  position: fixed; top: 60px; right: 16px;
  background: var(--toast-bg); color: var(--toast-text); padding: 8px 12px; border-radius: 4px;
  font-size: 13px; opacity: 0.95; z-index: 9999;
}

.analysis-card { border: 1px solid var(--border-strong); border-radius: 6px; padding: 12px 14px; margin-bottom: 12px; background: var(--bg-elevated); }
.analysis-card h2 { font-size: 14px; margin: 0 0 4px 0; }
.analysis-card .meta { color: var(--text-dim); font-size: 12px; margin-bottom: 8px; }
.analysis-section { border-top: 1px solid var(--border); padding-top: 8px; margin-top: 8px; }
.analysis-section h3 { font-size: 13px; margin: 0 0 4px 0; color: var(--text-muted); }
.analysis-section .body p { margin: 4px 0; font-size: 14px; line-height: 1.5; }
.analysis-section .body code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; background: var(--bg-code); padding: 1px 4px; border-radius: 3px; }
.analysis-card .analysis-section:first-of-type { border-top: none; padding-top: 0; margin-top: 0; }
.analysis-section .image-link { margin: 6px 0 0 0; font-size: 13px; }
.analysis-section .image-link a { color: var(--text); text-decoration: underline; }
.analysis-placeholder { color: var(--text-faint); font-style: italic; }

.wind-model-block { margin: 8px 0; }

.wind-model-header {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-code, #f5f5f5);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 3px 8px;
  font: inherit;
  font-weight: 600;
  font-size: 13px;
  margin: 4px 0;
  color: var(--text);
}

.wind-model-header.chart-active {
  background: rgba(120, 180, 230, 0.18);
  border-color: rgba(120, 180, 230, 0.5);
}

.wind-model-block > .body { margin-top: 4px; }

.analysis-synthesis {
  border-left: 3px solid #1f77b4;
  padding: 0.75em 1em;
  background: var(--bg-muted);
  margin-bottom: 1em;
}
.analysis-synthesis dl { margin: 0; display: grid; grid-template-columns: max-content 1fr; gap: 0.3em 0.8em; }
.analysis-synthesis dt { font-weight: 600; color: var(--text-muted); }
.analysis-synthesis dd { margin: 0; }
.model-chip {
  display: inline-block; padding: 0.1em 0.5em; border-radius: 1em;
  background: #1f77b4; color: white; font-size: 0.85em; font-weight: 600;
}

.analysis-routings { margin-bottom: 1em; border: 1px solid var(--border-strong); border-radius: 4px; padding: 0.5em 1em; background: var(--bg-elevated); }
.analysis-routings summary { font-weight: 600; cursor: pointer; }
.analysis-routings table { width: 100%; border-collapse: collapse; margin-top: 0.5em; }
.analysis-routings th, .analysis-routings td { padding: 0.25em 0.5em; text-align: left; border-bottom: 1px solid var(--border); }
.routings-meta { margin-top: 0.4em; font-size: 0.9em; color: var(--text-dim); }
.anchor-pill { background: var(--bg-muted); border: 1px solid var(--border); padding: 0.1em 0.4em; border-radius: 3px; }

.per-route-briefs { display: flex; flex-direction: column; gap: 0.4em; margin-bottom: 1em; }
.per-route-card { border: 1px solid var(--border-strong); border-radius: 4px; padding: 0.5em 0.8em; background: var(--bg-elevated); }
.per-route-card[open] { background: var(--bg-muted); }
.per-route-card summary { font-weight: 500; cursor: pointer; }
.per-route-card .per-route-brief p { margin: 0.4em 0; font-size: 14px; line-height: 1.5; }
.per-route-card .lightbox-link { display: inline-block; margin-top: 0.3em; font-size: 13px; color: var(--text); text-decoration: underline; }

.lightbox-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 9999; cursor: zoom-out;
}
.lightbox-overlay img { max-width: 95vw; max-height: 95vh; box-shadow: 0 0 50px rgba(0,0,0,0.5); }

.course-mark-label { background: var(--bg-elevated); color: var(--text); border: 1px solid var(--border-mark); padding: 1px 4px; font-size: 11px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.ais-track-tooltip { background: var(--bg-elevated); color: var(--text); border: 1px solid #9b59b6; padding: 2px 6px; font-size: 11px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

#mobile-tabs { display: none; }
.tab { flex: 1; padding: 8px 0; font-size: 14px; background: var(--bg-muted); color: var(--text); border: 0; border-top: 1px solid var(--border); }
.tab.tab-active { background: var(--bg-elevated); font-weight: 600; }

@media (max-width: 767px) {
  body[data-mode="race"] #main-panes { grid-template-columns: 1fr; }
  body[data-mode="race"] #mobile-tabs { display: flex; }
  body[data-mode="race"] #analysis-pane { border-left: none; }
  body[data-mode="race"] #chart-pane[data-active="false"] { display: none; }
  body[data-mode="race"] #analysis-pane[data-active="false"] { display: none; }
  body[data-mode="race"] #analysis-pane[data-active="true"] { display: block; }

  body[data-mode="race-past"] #mobile-tabs { display: none; }

  body { font-size: 15px; }

  #header-expand { grid-template-columns: 1fr; }
  #model-status { text-align: left; }
}

.route-timing { color: #888; font-size: 0.85em; margin-top: 0.5em; }
.routes-pending { color: #888; font-size: 0.9em; font-style: italic; margin-bottom: 0.5em; }
.route-failed { color: #888; font-size: 0.9em; margin: 0.25em 0; }

.geolocate-control a { font-size: 18px; line-height: 26px; text-align: center; }
.geolocate-control a.active { background: #2980b9; color: #fff; }
