:root {
  --bg: #0d0d0f;
  --surface: #16171b;
  --surface2: #1e1f24;
  --border: #2a2b31;
  --text: #e4e4e8;
  --text-dim: #8b8c94;
  --accent: #00d4aa;
  --accent2: #00b4ff;
  --purple: #b44dff;
  --red: #ff4d5a;
  --yellow: #ffc94d;
  --green: #00d46a;
  --orange: #ff8c42;
  --car3: #00d4aa;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
}

/* ── Header ─────────────────────────── */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 100;
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 12px; }
.header-left h1 { font-size: 18px; font-weight: 700; letter-spacing: 1px; }
.car-badge {
  background: var(--car3);
  color: #000;
  padding: 2px 8px;
  border-radius: 4px;
  font-weight: 800;
  font-size: 16px;
}
.team-name { font-size: 12px; color: var(--text-dim); }
.header-center { display: flex; align-items: center; gap: 12px; }
.session-info { font-size: 13px; color: var(--text-dim); }
.flag-indicator {
  display: inline-block;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--green);
}
.flag-indicator { transition: background 0.5s ease; }
.flag-indicator.yellow { background: var(--yellow); }
.flag-indicator.red { background: var(--red); }
.flag-indicator.sc { background: var(--orange); }
.header-right { display: flex; gap: 8px; align-items: center; }
.conn-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--red);
  transition: background 0.3s;
}
.conn-dot.connected { background: var(--green); }
.conn-dot.disconnected { background: var(--red); }

/* ── Tab Navigation ──────────────────── */
#tab-nav {
  display: flex;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab-btn {
  flex: 1;
  padding: 10px 20px;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.2s, border-color 0.2s;
  text-decoration: none;
  text-align: center;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active {
  color: var(--car3);
  border-bottom-color: var(--car3);
}

/* ── Tab Content ─────────────────────── */
.tab-content { display: none; flex: 1; overflow: hidden; }
.tab-content.active { display: flex; flex-direction: column; }

/* ── Tab 1: Map ──────────────────────── */
#map-container {
  position: relative;
  flex: 1;
  background: #1a1a1e;
  overflow: hidden;
}
#leaflet-map {
  width: 100%;
  height: 100%;
  background: #393939;
}
#map-hud {
  position: absolute;
  top: 12px;
  right: 12px;
  pointer-events: none;
  z-index: 10;
}
.hud-card {
  background: rgba(13, 13, 15, 0.85);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 16px;
  backdrop-filter: blur(8px);
  min-width: 140px;
}
.hud-pos {
  font-size: 36px;
  font-weight: 800;
  color: var(--car3);
  line-height: 1;
  font-variant-numeric: tabular-nums;
}
.hud-driver {
  font-size: 14px;
  font-weight: 600;
  margin-top: 4px;
  white-space: nowrap;
}
.hud-stats {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}
.hud-stats span { white-space: nowrap; }

#map-legend {
  position: absolute;
  bottom: 12px;
  left: 12px;
  display: flex;
  gap: 14px;
  font-size: 11px;
  color: var(--text-dim);
  z-index: 10;
}
.legend-item { display: flex; align-items: center; gap: 5px; }
.legend-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
}
.legend-dot.car3-dot { background: var(--car3); }
.legend-dot.other-dot { background: rgba(60,160,255,0.7); }

/* ── Tab 2: Data ─────────────────────── */
#tab-data { overflow-y: auto; }
#hero-section { padding: 16px 20px; }
.hero-card {
  display: flex;
  gap: 20px;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--car3);
  border-radius: 8px;
  padding: 16px 20px;
}
.hero-pos {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 60px;
}
.pos-label { font-size: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 1px; }
.pos-value { font-size: 42px; font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.pos-class { font-size: 11px; color: var(--accent2); margin-top: 2px; }
.hero-main { flex: 1; min-width: 0; }
.hero-driver { font-size: 20px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hero-team { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.hero-car { font-size: 11px; color: var(--text-dim); }
.hero-stats {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}
.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 52px;
}
.stat-label { font-size: 9px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.stat-value { font-size: 15px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.stat-value.best { color: var(--purple); }
.state-run { color: var(--green); }
.state-pit { color: var(--yellow); }
.state-out { color: var(--accent2); }
.state-stop { color: var(--red); }

.panels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  padding: 0 20px 20px;
}
.panels section, .panels > div { min-width: 0; }
.panels h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-dim);
  margin-bottom: 10px;
}

#class-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  max-height: 500px;
  overflow: auto;
}
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 12px; }
th {
  text-align: left;
  padding: 6px 8px;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
}
td {
  padding: 5px 8px;
  border-bottom: 1px solid #1a1b20;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
tr.car3-row {
  background: rgba(0,212,170,0.08);
  font-weight: 600;
}
tr.car3-row td { border-color: rgba(0,212,170,0.2); }

#right-panels { display: flex; flex-direction: column; gap: 16px; }
#lap-chart-section, #driver-section, #messages-section, #race-stats-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
}
#race-stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
  font-size: 12px;
}
.race-stat-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.race-stat-item .rs-label {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}
.race-stat-item .rs-value {
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.race-stat-item .rs-value.accent { color: var(--car3); }
.race-stat-item .rs-value.purple { color: var(--purple); }
#lap-chart {
  width: 100%;
  height: 140px;
  display: block;
}
#driver-list, #message-list {
  font-size: 12px;
  max-height: 160px;
  overflow-y: auto;
}
.driver-stint {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}
.driver-stint .name { font-weight: 600; }
.driver-stint .time { color: var(--text-dim); font-size: 11px; }
.driver-stint.active .name { color: var(--car3); }
.msg-entry {
  padding: 4px 0;
  border-bottom: 1px solid #1a1b20;
  line-height: 1.4;
}
.msg-entry .msg-time { color: var(--text-dim); font-size: 10px; margin-right: 6px; }
.msg-entry .msg-cat { color: var(--accent2); font-size: 10px; margin-right: 6px; }

/* ── Tab 3: Video ────────────────────── */
#tab-video { overflow-y: auto; }
#video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 12px;
  padding: 16px;
}
.video-cell {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.video-cell .video-title {
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}
.video-cell .video-frame {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: #000;
}
.video-cell .video-frame img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.video-cell .video-frame iframe {
  width: 100%;
  height: 100%;
  border: none;
}
.video-cell .video-frame .cam-offline {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  color: var(--text-dim);
  font-size: 12px;
}

/* ── Leaflet Overrides ────────────────── */
@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 2px 8px rgba(0,212,170,0.4); }
  50% { box-shadow: 0 2px 16px rgba(0,212,170,0.8); }
}
.car3-tooltip {
  background: rgba(0,212,170,0.9);
  color: #000;
  font-weight: 800;
  font-size: 11px;
  border: none;
  border-radius: 3px;
  padding: 2px 6px;
  animation: pulse-glow 2s ease-in-out infinite;
}
.car3-tooltip::before {
  border-top-color: rgba(0,212,170,0.9) !important;
}
.car-label {
  background: rgba(13, 13, 15, 0.75);
  color: #e4e4e8;
  font-weight: 700;
  font-size: 10px;
  border: none;
  border-radius: 3px;
  padding: 1px 4px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.5);
  white-space: nowrap;
}
.car-label::before {
  border-top-color: rgba(13, 13, 15, 0.75) !important;
}
.leaflet-control-layers {
  background: rgba(22,23,27,0.9) !important;
  border: 1px solid var(--border) !important;
  border-radius: 6px !important;
  color: var(--text) !important;
}
.leaflet-control-layers label {
  color: var(--text) !important;
}
.leaflet-control-zoom a {
  background: rgba(22,23,27,0.9) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover {
  background: var(--surface2) !important;
}

/* ── Device Picker Modal ────────────── */
.device-picker {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.device-picker.hidden { display: none; }
.picker-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  max-width: 360px;
}
.picker-content h3 { margin-bottom: 8px; }
.picker-content p { color: var(--text-dim); font-size: 13px; margin-bottom: 16px; }
.picker-content button {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 8px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
.picker-content button:hover { background: var(--border); }

/* ── Telemetry Tab ──────────────────── */
#tab-telemetry { padding: 16px; }
#telem-header {
  text-align: center;
  padding: 8px 0 16px;
  color: var(--text-dim);
  font-size: 12px;
  font-family: var(--mono);
}
/* ── Car #3 Deep Dive ──────────────────────────────────────────────── */
#dd-wrap {
  height: calc(100vh - 90px);
  overflow-y: auto;
  padding: 0 12px 24px;
}
#dd-status {
  display: flex;
  gap: 4px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.dd-stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  text-align: center;
  flex: 1;
  min-width: 70px;
}
.dd-label {
  display: block;
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
  margin-bottom: 4px;
}
.dd-val {
  display: block;
  font-size: 18px;
  font-weight: 700;
  font-family: var(--mono);
  color: var(--text);
}
.dd-val.dd-pos { color: #FFD700; }
.dd-val.dd-best { color: #B877D9; }
.dd-val.dd-driver { font-size: 14px; font-family: inherit; }

/* Sector breakdown table */
#dd-sectors { margin-bottom: 20px; }
#dd-sectors h3 { font-size: 13px; color: var(--text-dim); margin-bottom: 8px; }
#dd-sec-scroll {
  max-height: 280px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
}
#dd-sec-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-family: var(--mono);
  white-space: nowrap;
}
#dd-sec-table thead {
  position: sticky;
  top: 0;
  z-index: 1;
}
#dd-sec-table th {
  background: #1e1f24;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 6px 8px;
  text-align: center;
  border-bottom: 2px solid var(--border);
}
#dd-sec-table td {
  padding: 4px 8px;
  text-align: center;
  border-bottom: 1px solid #1e1f24;
  color: var(--text);
}
#dd-sec-table tbody tr { background: #121318; }
#dd-sec-table tbody tr:nth-child(even) { background: #16171b; }
#dd-sec-table td.sec-purple { color: #A855F7; font-weight: 600; }
#dd-sec-table td.sec-green { color: #22C55E; font-weight: 600; }
#dd-sec-table td.sec-yellow { color: #FFC94D; }
#dd-sec-table td.dd-laptime { color: #e0e0e0; font-weight: 600; }
#dd-sec-table td.dd-driver-cell { font-family: inherit; color: var(--text-dim); }

/* Charts */
#dd-charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 20px;
}
.dd-chart-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
}
.dd-chart-box h3 {
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 8px;
}
.dd-chart-box canvas {
  width: 100%;
  height: 180px;
}

/* Stints + Pits */
#dd-bottom {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
#dd-stints h3, #dd-pit-log h3 {
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 10px;
}
#dd-stint-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.stint-block {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 12px;
  border-left: 3px solid var(--accent);
}
.stint-block .stint-name {
  font-weight: 600;
  color: var(--text);
  min-width: 120px;
}
.stint-block .stint-time {
  font-size: 11px;
  color: var(--text-dim);
  font-family: var(--mono);
}
.stint-block .stint-duration {
  font-size: 11px;
  color: var(--accent);
  margin-left: auto;
  font-family: var(--mono);
}
.stint-block:nth-child(2n) { border-left-color: #f0c040; }
.stint-block:nth-child(3n) { border-left-color: #e84040; }

#dd-pit-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
#dd-pit-table th {
  background: #1e1f24;
  color: var(--text-dim);
  font-weight: 600;
  font-size: 10px;
  text-transform: uppercase;
  padding: 6px 8px;
  text-align: center;
  border-bottom: 2px solid var(--border);
}
#dd-pit-table td {
  padding: 5px 8px;
  text-align: center;
  border-bottom: 1px solid #1e1f24;
  color: var(--text);
  font-family: var(--mono);
  font-size: 12px;
}
#dd-pit-table tbody tr { background: #121318; }
#dd-pit-table tbody tr:nth-child(even) { background: #16171b; }
.dd-empty {
  text-align: center;
  color: var(--text-dim);
  font-size: 12px;
  padding: 16px;
}

@media (max-width: 900px) {
  #dd-charts { grid-template-columns: 1fr; }
  #dd-bottom { grid-template-columns: 1fr; }
}

/* ── Responsive ─────────────────────── */
@media (max-width: 768px) {
  .panels { grid-template-columns: 1fr; }
  .hero-card { flex-direction: column; align-items: flex-start; }
  .hero-stats { gap: 10px; }
  .team-name { display: none; }
  #video-grid { grid-template-columns: 1fr; }
}

/* ── Live Timing Tab ───────────────────────────────────────────────── */
#lt-wrap {
  height: calc(100vh - 90px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#lt-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 12px;
  background: #16171b;
  border-bottom: 1px solid #2a2b30;
  font-size: 13px;
  flex-shrink: 0;
}
#lt-session {
  font-weight: 600;
  color: #e0e0e0;
  font-size: 14px;
}
#lt-car-count { color: #8b8c94; }
#lt-show-all {
  margin-left: auto;
  background: #2a2b30;
  color: #8b8c94;
  border: 1px solid #3a3b40;
  border-radius: 4px;
  padding: 2px 10px;
  font-size: 11px;
  cursor: pointer;
  display: none;
}
#lt-show-all:hover { background: #3a3b40; color: #e0e0e0; }
#lt-show-all.visible { display: inline-block; }
#lt-hidden-hint {
  font-size: 10px;
  color: #555;
  display: none;
}
#lt-hidden-hint.visible { display: inline; }
#lt-scroll {
  flex: 1;
  overflow: auto;
}
#lt-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  font-family: 'SF Mono', 'Consolas', 'Monaco', monospace;
  white-space: nowrap;
}
#lt-table thead {
  position: sticky;
  top: 0;
  z-index: 2;
}
#lt-table th {
  background: #1e1f24;
  color: #8b8c94;
  font-weight: 600;
  text-transform: uppercase;
  font-size: 10px;
  letter-spacing: 0.5px;
  padding: 6px 6px;
  text-align: center;
  border-bottom: 2px solid #2e2f36;
  cursor: pointer;
  user-select: none;
  position: relative;
}
#lt-table th:hover { color: #ff6b6b; }
#lt-table th:hover::after {
  content: "×";
  position: absolute;
  top: 1px;
  right: 2px;
  font-size: 9px;
  color: #ff6b6b;
  opacity: 0.7;
}
#lt-table th.lt-driver,
#lt-table th.lt-car,
#lt-table th.lt-team { text-align: left; }
#lt-table td {
  padding: 4px 6px;
  text-align: center;
  border-bottom: 1px solid #1e1f24;
  color: #d0d0d4;
}
#lt-table td.lt-driver,
#lt-table td.lt-car,
#lt-table td.lt-team { text-align: left; }
#lt-table tbody tr {
  background: #121318;
  transition: background 0.15s;
}
#lt-table tbody tr:nth-child(even) { background: #16171b; }
#lt-table tbody tr:hover { background: #1f2028; }
#lt-table tbody tr.lt-car3 {
  background: #1a2a20 !important;
  box-shadow: inset 3px 0 0 #00d4aa;
}
#lt-table tbody tr.lt-car3:hover { background: #1e3428 !important; }

/* Position cell */
td.lt-pos { font-weight: 700; width: 30px; }
td.lt-pos.pos-1 { color: #FFD700; }
td.lt-pos.pos-2 { color: #C0C0C0; }
td.lt-pos.pos-3 { color: #CD7F32; }
td.lt-pos.pos-top10 { color: #73BF69; }

/* Car number */
td.lt-nr { font-weight: 700; width: 40px; color: #fff; }

/* Column widths */
td.lt-driver { max-width: 110px; overflow: hidden; text-overflow: ellipsis; }
td.lt-car { max-width: 130px; overflow: hidden; text-overflow: ellipsis; color: #8b8c94; }
td.lt-team { max-width: 160px; overflow: hidden; text-overflow: ellipsis; color: #8b8c94; }
td.lt-cls { font-size: 10px; color: #8b8c94; }
td.lt-clsr { width: 30px; }
td.lt-laps { width: 36px; }
td.lt-lap { font-variant-numeric: tabular-nums; }
td.lt-gap { color: #8b8c94; font-variant-numeric: tabular-nums; }
td.lt-pits { width: 30px; }
td.lt-spd { width: 36px; color: #8b8c94; }

/* F1-style sector colors */
td.lt-sec { font-variant-numeric: tabular-nums; width: 54px; color: #FFC94D; }
td.lt-sec.sec-purple { color: #A855F7; font-weight: 600; }
td.lt-sec.sec-green { color: #22C55E; font-weight: 600; }
td.lt-sec:empty { color: inherit; }

/* Remaining time */
.lt-remaining {
  font-weight: 700;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: #FFD700;
  letter-spacing: 0.5px;
}
.lt-remaining.lt-finished { color: #ff4d5a; }
.lt-remaining.lt-not-started { color: #8b8c94; }

/* Class filter dropdown */
#lt-class-filter {
  background: #2a2b30;
  color: #e0e0e0;
  border: 1px solid #3a3b40;
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 11px;
  cursor: pointer;
  outline: none;
}
#lt-class-filter:hover { background: #3a3b40; }
#lt-class-filter option { background: #1e1f24; color: #e0e0e0; }

/* Best lap in purple */
td.lt-best { color: #B877D9; }
