:root {
  --bg: #0b0d10;
  --panel: #14171c;
  --text: #e8ebef;
  --text-dim: #8a93a1;
  --accent: #ff5c3d;
  --border: #23272e;
  --you: #3d8bff;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.app-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}

.app-header h1 {
  font-size: 20px;
  margin: 0;
  font-weight: 600;
}

.app-header .accent {
  color: var(--accent);
}

.event-selector {
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
  max-width: 100%;
}

.error-banner {
  background: #3a1f12;
  color: #ffb08a;
  border: 1px solid #6b3a1f;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
}

.search-wrap {
  position: relative;
  flex: 1 1 220px;
  max-width: 320px;
}

.racer-search {
  width: 100%;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  font-size: 14px;
}

.racer-search::placeholder {
  color: var(--text-dim);
}

.racer-search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 20;
  margin: 0;
  padding: 4px 0;
  list-style: none;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  max-height: 260px;
  overflow-y: auto;
}

.racer-search-results li {
  padding: 8px 12px;
  font-size: 13px;
  cursor: pointer;
}

.racer-search-results li:hover {
  background: var(--border);
}

.racer-search-results .bib-tag {
  color: var(--accent);
  font-weight: 700;
  margin-right: 6px;
}

.racer-search-results .class-tag {
  color: var(--text-dim);
  font-size: 11px;
}

.stop-follow-btn {
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--accent);
  background: transparent;
  color: var(--accent);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}

.stop-follow-btn:hover {
  background: var(--accent);
  color: #000;
}

.app-main {
  display: flex;
  height: calc(100vh - 65px);
}

.map-panel {
  flex: 1 1 70%;
  min-width: 0;
}

.leaderboard-panel {
  flex: 1 1 30%;
  min-width: 260px;
  max-width: 380px;
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.leaderboard-header {
  padding: 14px 16px;
  font-weight: 600;
  font-size: 15px;
  border-bottom: 1px solid var(--border);
  color: var(--accent);
}

.class-selector {
  display: block;
  width: 100%;
  margin-bottom: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
}

.nearby-callout {
  padding: 8px 16px;
  font-size: 12px;
  color: var(--accent);
  border-bottom: 1px solid var(--border);
}

.leaderboard-list {
  list-style: none;
  margin: 0;
  padding: 8px 0;
  overflow-y: auto;
}

.leaderboard-list li {
  display: grid;
  grid-template-columns: 24px 44px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.leaderboard-list .rank {
  color: var(--text-dim);
}

.leaderboard-list .bib,
.racer-panel .bib {
  border-radius: 4px;
  padding: 2px 6px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  color: #000;
}

.leaderboard-list .mile {
  color: var(--text-dim);
  font-size: 12px;
  white-space: nowrap;
}

/* Mapbox GL JS popups (racer + checkpoint) live in the page body and
   inherit our light --text color by default against Mapbox's own white
   popup background — nearly invisible light-gray-on-white. Style them
   explicitly to match the dark dashboard theme. */
.mapboxgl-popup-content {
  background: var(--panel);
  color: var(--text);
  border-radius: 6px;
  padding: 12px 14px;
}

.mapboxgl-popup-tip {
  border-top-color: var(--panel);
  border-bottom-color: var(--panel);
}

.mapboxgl-popup-close-button {
  color: var(--text);
}

.racer-marker {
  min-width: 20px;
  height: 20px;
  padding: 0 3px;
  border-radius: 10px;
  border: 2px solid #fff;
  box-shadow: 0 0 4px rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  color: #000;
  line-height: 1;
  white-space: nowrap;
}

@media (max-width: 700px) {
  .app-main {
    flex-direction: column;
    height: auto;
  }

  .map-panel {
    /* Override the desktop flex-basis (70%) — in a column flex container,
       flex-basis governs the main-axis (vertical) size and wins over
       height unless flex-basis is auto. With .app-main's height also auto
       on mobile, that leftover 70% resolved against nothing and collapsed
       the map to zero height. */
    flex: none;
    height: 55vh;
  }

  .leaderboard-panel {
    flex: none;
    max-width: none;
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: 40vh;
  }
}

@keyframes nearby-pulse {
  0%, 100% {
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.6), 0 0 0 0 rgba(255, 225, 77, 0.7);
  }
  50% {
    box-shadow: 0 0 4px rgba(0, 0, 0, 0.6), 0 0 0 6px rgba(255, 225, 77, 0);
  }
}

.racer-marker.nearby-pulse {
  animation: nearby-pulse 1.2s ease-in-out infinite;
  border-color: #ffe14d;
}

.locate-btn,
.style-btn {
  font-size: 15px;
  line-height: 1;
}

.locate-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.user-marker {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--you);
  border: 3px solid #fff;
  box-shadow: 0 0 0 4px rgba(61, 139, 255, 0.35);
}

.locate-status {
  padding: 8px 16px;
  font-size: 12px;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}

.locate-status-action {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--accent);
  text-decoration: underline;
  cursor: pointer;
}

/* The racer panel replaces the leaderboard while open. */
.leaderboard-panel.showing-racer .sidebar-tabs,
.leaderboard-panel.showing-racer .leaderboard-header,
.leaderboard-panel.showing-racer .nearby-callout,
.leaderboard-panel.showing-racer .leaderboard-list {
  display: none;
}

.racer-panel {
  padding: 12px 16px;
  overflow-y: auto;
  font-size: 14px;
}

.racer-panel-back,
.racer-panel-share {
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  padding: 4px 10px;
  cursor: pointer;
}

.racer-panel-back {
  margin-bottom: 12px;
  color: var(--text-dim);
}

.racer-panel-title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.racer-panel-name {
  margin: 0;
  font-size: 18px;
}

.racer-panel-class {
  color: var(--text-dim);
  margin: 4px 0 12px;
}

.racer-panel-row {
  display: grid;
  grid-template-columns: 90px 1fr;
  gap: 8px;
  padding: 6px 0;
  border-bottom: 1px solid var(--border);
}

.racer-panel-label {
  color: var(--text-dim);
}

.racer-panel-gone {
  color: var(--text-dim);
}

.racer-panel-actions {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-top: 14px;
}

.racer-panel-follow-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
}

.racer-panel-status {
  min-height: 1.4em;
  margin-top: 6px;
  font-size: 12px;
  color: var(--accent);
}

.leaderboard-list li[data-racer-key] {
  cursor: pointer;
}

.leaderboard-list li[data-racer-key]:hover {
  background: rgba(255, 255, 255, 0.04);
}

.racer-marker {
  cursor: pointer;
}

/* outline, not border/box-shadow: .nearby-pulse animates those. */
.racer-marker.racer-selected {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* Position predicted from the last report's speed (see map.js step()).
   filter, not opacity: with terrain on, Mapbox rewrites each marker's
   inline opacity whenever it moves (occlusion fade). */
.racer-marker.racer-predicted {
  filter: opacity(0.75);
}

.racer-marker.racer-leader::after {
  content: "👑";
  position: absolute;
  top: -15px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  line-height: 1;
  pointer-events: none;
}

/* Sidebar tabs: Leaderboard | Coming to you. The tab-coming class on
   .leaderboard-panel swaps which list shows (the class dropdown stays). */
.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}

.sidebar-tabs[hidden] {
  display: none;
}

.sidebar-tabs button {
  flex: 1;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  color: var(--text-dim);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 10px 8px;
  cursor: pointer;
}

.sidebar-tabs button[aria-selected="true"] {
  color: var(--text);
  border-bottom-color: var(--accent);
}

.coming-list,
.leaderboard-panel.tab-coming #leaderboard-category,
.leaderboard-panel.tab-coming .nearby-callout,
.leaderboard-panel.tab-coming #leaderboard-list {
  display: none;
}

.leaderboard-panel.tab-coming .coming-list {
  display: block;
}

.leaderboard-panel.showing-racer .coming-list {
  display: none;
}

.coming-list li {
  grid-template-columns: minmax(64px, auto) 44px 1fr auto;
}

.coming-when {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}

.coming-when strong {
  white-space: nowrap;
}

.coming-when small {
  color: var(--text-dim);
  font-size: 11px;
}

.coming-list li.coming-stopped {
  opacity: 0.6;
}

.coming-list li.coming-divider,
.coming-list li.coming-empty {
  display: block;
  color: var(--text-dim);
  font-size: 12px;
}
