*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body { height: 100%; background: #0d1117; color: #c9d1d9; font-family: system-ui, sans-serif; overflow: hidden; display: flex; flex-direction: column; }

#layout { display: flex; flex: 1; min-height: 0; position: relative; overflow: hidden; }

/* Map */
#map { flex: 1; }

/* Sidebar */
#sidebar {
  width: 280px; min-width: 280px;
  background: #0d1117;
  border-left: 1px solid #21262d;
  display: flex; flex-direction: column;
  overflow: hidden;
}
#sidebar-header {
  padding: 10px 12px;
  border-bottom: 1px solid #21262d;
  display: flex; align-items: center; justify-content: space-between;
}
#sidebar-title { font-size: 12px; font-weight: 700; letter-spacing: 1px; color: #e6edf3; }
#sidebar-collapse { background: none; border: none; color: #8b949e; cursor: pointer; font-size: 16px; padding: 0 4px; }

/* Sidebar controls */
#sidebar-controls {
  padding: 6px 10px;
  border-bottom: 1px solid #21262d;
  display: flex; flex-direction: column; gap: 5px;
}
#display-options-btn {
  width: 100%;
  background: #161b22; border: 1px solid #30363d; border-radius: 4px;
  color: #8b949e; font-size: 10px; padding: 5px 8px;
  cursor: pointer; text-align: center; letter-spacing: 0.3px;
}
#display-options-btn:hover { border-color: #58a6ff; color: #c9d1d9; }
#display-options-btn.active { border-color: #58a6ff; color: #58a6ff; background: rgba(88,166,255,0.06); }
#runner-search {
  width: 100%;
  background: #161b22; border: 1px solid #30363d; border-radius: 4px;
  color: #c9d1d9; font-size: 12px; padding: 5px 8px;
  outline: none;
}
#runner-search:focus { border-color: #58a6ff; }
#runner-search::placeholder { color: #484f58; }
#control-row {
  display: flex; gap: 6px;
}
#color-mode-btn, #follow-btn {
  flex: 1;
  background: #161b22; border: 1px solid #30363d; border-radius: 4px;
  color: #8b949e; font-size: 10px; padding: 4px 6px;
  cursor: pointer; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#color-mode-btn:hover, #follow-btn:hover { border-color: #58a6ff; color: #c9d1d9; }
#color-mode-btn.active, #follow-btn.active { border-color: #3fb950; color: #3fb950; }

#watched-section, #runner-list-wrapper { overflow-y: auto; }
#runner-list { overflow-y: auto; flex: 1; }

.section-label {
  padding: 6px 12px 4px;
  font-size: 10px; color: #8b949e;
  text-transform: uppercase; letter-spacing: 1px;
}
.divider { height: 1px; background: #21262d; margin: 4px 0; }

/* Runner rows */
.runner-row {
  position: relative; overflow: hidden;
  cursor: pointer;
  border-bottom: 1px solid #161b22;
  transition: opacity 0.2s;
}
.runner-row:hover { background: rgba(255,255,255,0.03); }
.runner-row.dnf { opacity: 0.4; }

.runner-progress {
  position: absolute; top: 0; left: 0; height: 100%;
  background: linear-gradient(90deg, rgba(63,185,80,0.18) 0%, rgba(63,185,80,0.04) 100%);
  border-right: 2px solid rgba(63,185,80,0.3);
  pointer-events: none;
  transition: width 0.5s ease;
}
.runner-row.watched .runner-progress { opacity: 0; } /* color handled by inline style */

.runner-content {
  position: relative;
  padding: 7px 10px;
  display: flex; align-items: center; gap: 8px;
}
.runner-rank { font-size: 10px; min-width: 18px; color: #8b949e; }
.runner-rank.top1 { color: #f0883e; font-weight: 700; }
.runner-bib {
  font-size: 10px; font-weight: 700;
  background: #21262d; color: #c9d1d9;
  border-radius: 3px; padding: 1px 5px;
  min-width: 24px; text-align: center;
}
.runner-name { flex: 1; font-size: 12px; }
.runner-flag { height: 11px; width: auto; vertical-align: middle; opacity: 0.85; margin-right: 3px; flex-shrink: 0; }
.runner-name.dnf-name { text-decoration: line-through; }
.runner-stats { text-align: right; }
.runner-dist { font-size: 11px; }
.runner-pace { font-size: 10px; color: #8b949e; }

#sidebar-footer {
  padding: 7px 12px;
  border-top: 1px solid #21262d;
  display: flex; justify-content: space-between; align-items: center;
  font-size: 10px; color: #8b949e;
}
#refresh-btn { background: none; border: none; color: #58a6ff; cursor: pointer; font-size: 12px; }

#sidebar-expand {
  position: absolute; top: 50%; right: 0;
  transform: translateY(-50%);
  background: #161b22; border: 1px solid #21262d; border-right: none;
  color: #8b949e; cursor: pointer; padding: 8px 4px; font-size: 16px;
  border-radius: 4px 0 0 4px; z-index: 1000;
}

/* Scrubber */
#scrubber-bar {
  flex-shrink: 0;
  height: 36px; background: #161b22;
  border-top: 1px solid #21262d;
  display: flex; align-items: center;
  padding: 0 10px; gap: 8px;
  z-index: 999;
}
#layer-toggle-btn {
  background: none; border: 1px solid #30363d; border-radius: 4px;
  color: #8b949e; font-size: 10px; padding: 3px 7px;
  cursor: pointer; white-space: nowrap; flex-shrink: 0;
}
#layer-toggle-btn:hover { border-color: #58a6ff; color: #c9d1d9; }
#race-start-label {
  font-size: 10px; color: #484f58; white-space: nowrap; flex-shrink: 0;
}
#scrubber-track { flex: 1; position: relative; height: 20px; }
#scrubber-ticks { position: absolute; top: 0; left: 0; right: 0; height: 8px; pointer-events: none; }
#scrubber-input { width: 100%; margin: 0; cursor: pointer; accent-color: #4a9eff; }
#scrubber-right { display: flex; align-items: center; gap: 8px; white-space: nowrap; }
#current-time { font-size: 10px; color: #8b949e; margin-left: 3px; }

#live-indicator { font-size: 11px; color: #3fb950; display: flex; align-items: center; gap: 5px; }
.live-dot {
  width: 7px; height: 7px; border-radius: 50%; background: #3fb950;
  animation: pulse 1.5s infinite;
}
@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.3; } }

#back-to-live {
  background: #21262d; border: 1px solid #30363d;
  color: #58a6ff; font-size: 11px; padding: 3px 8px;
  border-radius: 4px; cursor: pointer;
}

/* Layer toggle moved to scrubber bar — no absolute overlay needed */

/* No-data overlay */
#no-data-overlay {
  position: absolute; inset: 0;
  background: rgba(13,17,23,0.85);
  display: flex; align-items: center; justify-content: center;
  z-index: 2000;
}
#no-data-msg { color: #8b949e; font-size: 16px; }

/* Attribution */
#attribution-bar {
  flex-shrink: 0;
  height: 20px;
  background: #0d1117;
  border-top: 1px solid #161b22;
  font-size: 9px; color: #30363d;
  padding: 0 12px;
  display: flex; align-items: center; justify-content: center;
  z-index: 998;
  pointer-events: none;
}
#attribution-bar a { color: #58a6ff; pointer-events: all; text-decoration: none; }
#attribution-bar a:hover { text-decoration: underline; }

.hidden { display: none !important; }

/* ── TV Mode features ───────────────────────────────────────────────────── */

/* Live Event Ticker */
#event-ticker {
  position: fixed;
  bottom: 56px; /* above scrubber (36px) + attribution (20px) */
  left: 0;
  right: 0;
  height: 32px;
  background: #161b22;
  border-top: 1px solid #21262d;
  overflow: hidden;
  z-index: 997;
}
#ticker-track {
  display: flex;
  align-items: center;
  height: 100%;
  white-space: nowrap;
  overflow: visible;
}
#ticker-content {
  display: inline-block;
  font-size: 12px;
  color: #c9d1d9;
  padding-left: 100vw;
  animation: ticker-scroll 60s linear infinite;
}
@keyframes ticker-scroll {
  0%   { transform: translateX(0); }
  100% { transform: translateX(-100%); }
}
#event-ticker:hover #ticker-content { animation-play-state: paused; }

/* Stats HUD */
#stats-hud {
  position: fixed;
  top: 10px;
  right: 290px;
  z-index: 1000;
  background: rgba(13,17,23,0.9);
  border: 1px solid #21262d;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 11px;
  color: #c9d1d9;
  width: 180px;
  line-height: 1.6;
  pointer-events: none;
}
#stats-hud .hud-divider {
  height: 1px;
  background: #21262d;
  margin: 5px 0;
}

/* Milestone overlay */
#milestone-overlay {
  position: fixed;
  inset: 0;
  z-index: 9000;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
}
#milestone-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
#milestone-emoji {
  font-size: 64px;
  line-height: 1;
}
#milestone-text {
  font-size: 28px;
  font-weight: 700;
  color: #fff;
  max-width: 600px;
}
#milestone-sub {
  font-size: 16px;
  color: #8b949e;
}
#milestone-overlay.fade-out {
  animation: milestone-fade 0.8s ease forwards;
}
@keyframes milestone-fade {
  from { opacity: 1; }
  to   { opacity: 0; }
}

/* Night mode */
body.night-mode {
  --bg: #080b0f;
}
body.night-mode #sidebar {
  background: #080b0f;
  border-left-color: #1a2744;
}
body.night-mode #sidebar-header,
body.night-mode #sidebar-controls,
body.night-mode #sidebar-footer {
  border-color: #1a2744;
}
body.night-mode .live-dot {
  animation: pulse-night 3s infinite;
}
@keyframes pulse-night {
  0%,100% { opacity: 0.6; }
  50%      { opacity: 0.15; }
}

/* Stale warning */
.stale-warning { color: #f0883e; }

/* ── TV Mode Panel ──────────────────────────────────────────────────────── */

/* ── TV button (bottom-left of map) ──────────────────────────────────────── */
#tv-mode-btn {
  position: absolute;
  bottom: 58px; /* above attribution bar (36px scrubber + ~22px attr) */
  left: 10px;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(13,17,23,0.88);
  border: 1px solid #30363d;
  border-radius: 4px;
  color: #8b949e;
  font-size: 11px;
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  letter-spacing: 0.5px;
  padding: 5px 10px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, background 0.2s;
  user-select: none;
}
#tv-mode-btn:hover {
  border-color: #58a6ff;
  color: #c9d1d9;
  background: rgba(13,17,23,1);
}
#tv-mode-btn.active {
  border-color: #58a6ff;
  color: #58a6ff;
  background: rgba(88,166,255,0.08);
  box-shadow: 0 0 8px rgba(88,166,255,0.18);
}
.tv-btn-icon {
  font-size: 13px;
  line-height: 1;
}
.tv-btn-label {
  font-weight: 700;
  letter-spacing: 1px;
}

/* ── TV Panel shell ───────────────────────────────────────────────────────── */
#tv-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 56px; /* stop above scrubber (36px) + attribution (20px) */
  width: 300px;
  z-index: 1100;
  background: #0d1117;
  border-left: 1px solid #21262d;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.28s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;

  /* Subtle scanline texture using repeating gradient */
  background-image:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 3px,
      rgba(255,255,255,0.012) 3px,
      rgba(255,255,255,0.012) 4px
    );
}
#tv-panel.open {
  transform: translateX(0);
}

/* ── Panel header ─────────────────────────────────────────────────────────── */
#tv-panel-header {
  padding: 14px 14px 10px;
  border-bottom: 1px solid #21262d;
  flex-shrink: 0;
}
#tv-panel-title-row {
  display: flex;
  align-items: center;
  gap: 8px;
}
#tv-panel-title {
  flex: 1;
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 2.5px;
  color: #e6edf3;
}
#tv-panel-subtitle {
  margin-top: 3px;
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 9px;
  letter-spacing: 2px;
  color: #484f58;
}
#tv-panel-close {
  background: none;
  border: none;
  color: #484f58;
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
  line-height: 1;
  transition: color 0.15s;
  flex-shrink: 0;
}
#tv-panel-close:hover { color: #8b949e; }

/* ── Signal indicator (top-left of header) ────────────────────────────────── */
#tv-panel-signal {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  flex-shrink: 0;
}
.signal-dot {
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: #30363d;
  margin-bottom: 1px;
  flex-shrink: 0;
}
.signal-bar {
  width: 3px;
  border-radius: 1px;
  background: #30363d;
  flex-shrink: 0;
}
.signal-bar.b1 { height: 5px; }
.signal-bar.b2 { height: 8px; }
.signal-bar.b3 { height: 11px; }

/* Active signal — green bars + pulsing dot */
#tv-panel-header.signal-active .signal-dot {
  background: #3fb950;
  animation: signal-pulse 1.2s ease-in-out infinite;
}
#tv-panel-header.signal-active .signal-bar {
  background: #3fb950;
}
#tv-panel-header.signal-active .signal-bar.b1 {
  animation: signal-bar-flicker 1.8s ease-in-out infinite;
}
#tv-panel-header.signal-active .signal-bar.b2 {
  animation: signal-bar-flicker 1.8s ease-in-out 0.3s infinite;
}
#tv-panel-header.signal-active .signal-bar.b3 {
  animation: signal-bar-flicker 1.8s ease-in-out 0.6s infinite;
}

@keyframes signal-pulse {
  0%,100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.75); }
}
@keyframes signal-bar-flicker {
  0%,100% { opacity: 1; }
  40% { opacity: 0.5; }
  60% { opacity: 0.8; }
}

/* ── Master toggle row ────────────────────────────────────────────────────── */
#tv-master-row {
  padding: 12px 14px;
  border-bottom: 1px solid #21262d;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  background: rgba(255,255,255,0.018);
}
#tv-master-info {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
#tv-master-label {
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1.5px;
  color: #c9d1d9;
}
/* Master toggle button */
.tv-master-btn {
  display: flex;
  align-items: center;
  gap: 7px;
  background: none;
  border: 1px solid #30363d;
  border-radius: 5px;
  padding: 6px 10px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  flex-shrink: 0;
}
.tv-master-btn:hover { border-color: #58a6ff; }
.tv-master-btn.is-on {
  border-color: #3fb950;
  background: rgba(63,185,80,0.08);
}

.master-btn-inner {
  position: relative;
  display: inline-flex;
  align-items: center;
}
.master-btn-track {
  display: block;
  width: 28px;
  height: 14px;
  border-radius: 7px;
  background: #21262d;
  transition: background 0.25s;
  border: 1px solid #30363d;
}
.tv-master-btn.is-on .master-btn-track {
  background: #1a4028;
  border-color: #3fb950;
}
.master-btn-knob {
  position: absolute;
  left: 2px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #484f58;
  transition: transform 0.25s cubic-bezier(0.4,0,0.2,1), background 0.25s;
}
.tv-master-btn.is-on .master-btn-knob {
  transform: translateX(14px);
  background: #3fb950;
}

.master-btn-text {
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #484f58;
  transition: color 0.2s;
  min-width: 20px;
}
.tv-master-btn.is-on .master-btn-text { color: #3fb950; }

/* ── Features section ─────────────────────────────────────────────────────── */
#tv-features-section {
  flex: 1;
  overflow-y: auto;
  padding-bottom: 2px;
}
#tv-features-section::-webkit-scrollbar { width: 4px; }
#tv-features-section::-webkit-scrollbar-track { background: transparent; }
#tv-features-section::-webkit-scrollbar-thumb { background: #21262d; border-radius: 2px; }

/* ── Section label ───────────────────────────────────────────────────────── */
.tv-section-label {
  padding: 10px 14px 6px;
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 9px;
  letter-spacing: 2px;
  color: #484f58;
  text-transform: uppercase;
}

/* ── Feature row ─────────────────────────────────────────────────────────── */
.tv-feature-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
  border-bottom: 1px solid #161b22;
  transition: background 0.15s;
  cursor: default;
}
.tv-feature-row:hover { background: rgba(255,255,255,0.025); }
.tv-feature-row.feat-active {
  background: rgba(88,166,255,0.04);
  border-bottom-color: rgba(88,166,255,0.1);
}

.tv-feat-icon {
  font-size: 14px;
  line-height: 1;
  color: #484f58;
  flex-shrink: 0;
  width: 18px;
  text-align: center;
  transition: color 0.2s;
  font-style: normal;
}
.tv-feature-row.feat-active .tv-feat-icon { color: #58a6ff; }

.tv-feat-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.tv-feat-name {
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.8px;
  color: #c9d1d9;
  line-height: 1.3;
}
.tv-feat-desc {
  font-size: 10px;
  color: #484f58;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ── Feature toggle switch ───────────────────────────────────────────────── */
.tv-toggle {
  flex-shrink: 0;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
}
.tv-toggle-track {
  position: relative;
  display: block;
  width: 32px;
  height: 17px;
  border-radius: 9px;
  background: #21262d;
  border: 1px solid #30363d;
  transition: background 0.25s, border-color 0.25s;
}
.tv-toggle.is-on .tv-toggle-track {
  background: rgba(88,166,255,0.22);
  border-color: #58a6ff;
}
.tv-toggle.master-off {
  opacity: 0.38;
  pointer-events: none;
}
.tv-toggle-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: #484f58;
  transition: transform 0.25s cubic-bezier(0.4,0,0.2,1), background 0.25s;
}
.tv-toggle.is-on .tv-toggle-knob {
  transform: translateX(15px);
  background: #58a6ff;
  box-shadow: 0 0 5px rgba(88,166,255,0.5);
}

/* ── Elevation section ───────────────────────────────────────────────────── */
#tv-elevation-section {
  border-top: 1px solid #21262d;
  flex-shrink: 0;
}
.tv-elev-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 14px;
}

/* ── Panel footer ─────────────────────────────────────────────────────────── */
#tv-panel-footer {
  padding: 8px 14px;
  border-top: 1px solid #21262d;
  flex-shrink: 0;
  text-align: center;
  font-family: 'SF Mono', 'Fira Code', 'Fira Mono', 'Consolas', monospace;
  font-size: 9px;
  color: #30363d;
  letter-spacing: 0.5px;
}

/* ── Elevation panel — slides up from above scrubber bar ────────────────── */
#elevation-panel {
  position: fixed;
  left: 0; right: 0;
  bottom: 56px; /* above scrubber (36px) + attribution (20px) */
  height: 140px;
  background: rgba(13,17,23,0.96);
  border-top: 1px solid #30363d;
  z-index: 900;
  transform: translateY(100%);
  transition: transform 0.25s ease;
  pointer-events: none;
  display: flex;
  flex-direction: column;
}
#elevation-panel.visible {
  transform: translateY(0);
  pointer-events: all;
}
#elevation-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 12px;
  flex-shrink: 0;
}
#elevation-panel-title {
  font-size: 9px;
  letter-spacing: 1.5px;
  color: #484f58;
  font-family: 'SF Mono', 'Fira Code', monospace;
}
#elevation-panel-close {
  background: none; border: none; color: #484f58;
  cursor: pointer; font-size: 11px; padding: 0 2px;
}
#elevation-panel-close:hover { color: #8b949e; }
#elevation-canvas { flex: 1; min-height: 0; width: 100%; }

/* ── Sidebar offset when panel is open ───────────────────────────────────── */
/* Panel slides over the map; sidebar is unaffected (stays on right edge) */

/* ── Mobile: bottom sheet ───────────────────────────────────────── */
@media (max-width: 700px) {
  #layout {
    flex-direction: column;
  }

  #map { flex: 1; min-height: 0; }

  #sidebar {
    position: absolute;
    bottom: 0; left: 0; right: 0;
    width: 100%; min-width: unset;
    border-left: none;
    border-top: 2px solid #30363d;
    border-radius: 14px 14px 0 0;
    max-height: 70vh;
    transform: translateY(calc(100% - 52px)); /* collapsed: show header only */
    transition: transform 0.3s ease;
    z-index: 1000;
  }

  #sidebar.sheet-open {
    transform: translateY(0);
  }

  /* Drag handle */
  #sidebar-header::before {
    content: '';
    display: block;
    width: 36px; height: 4px;
    background: #30363d;
    border-radius: 2px;
    margin: 0 auto 8px;
  }

  #sidebar-collapse { display: none; }
  #sidebar-expand { display: none; }
  #race-start-label { display: none; } /* too narrow */

  /* Mobile: layout is flex column on body, scrubber in flow — no overrides needed */
}
