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

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242836;
  --border: #2e3345;
  --text: #e4e6ed;
  --text-dim: #8b8fa3;
  --accent: #f59e0b;
  --green: #22c55e;
  --red: #ef4444;
  --blue: #3b82f6;
  --yellow: #eab308;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  padding-bottom: 40px;
}

header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}

h1 {
  font-size: 1.5rem;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), #f97316);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.race-info { font-size: 0.9rem; color: var(--text-dim); }

.race-status {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.85rem;
}

.flag-indicator {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  border-radius: 20px;
  font-weight: 600;
  font-size: 0.8rem;
}
.flag-green { background: rgba(34,197,94,0.15); color: var(--green); }
.flag-yellow { background: rgba(234,179,8,0.15); color: var(--yellow); }
.flag-red { background: rgba(239,68,68,0.15); color: var(--red); }
.flag-white { background: rgba(255,255,255,0.15); color: #fff; }
.flag-checkered { background: rgba(255,255,255,0.1); color: #fff; }
.flag-pre { background: rgba(59,130,246,0.15); color: var(--blue); }

.lap-counter { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; }

.stage-badge {
  padding: 4px 12px;
  border-radius: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  font-size: 0.8rem;
}
.stage-badge.active { border-color: var(--accent); background: rgba(245,158,11,0.1); }
.stage-badge .label { color: var(--text-dim); margin-right: 4px; }
.stage-badge .value { font-weight: 600; }

main { padding: 24px; max-width: 1000px; margin: 0 auto; }
.section { margin-bottom: 32px; }

h2 {
  font-size: 1.1rem;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ── Pylon (shared by race + season) ── */
.pylon {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  font-size: 0.88rem;
}

.pylon-header {
  display: flex;
  padding: 6px 12px;
  background: var(--surface2);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  font-weight: 600;
}

.pylon-row {
  display: flex;
  padding: 9px 12px;
  border-top: 1px solid var(--border);
  align-items: center;
  border-left: 3px solid transparent;
  transition: background 0.12s;
}
.pylon-row:hover { background: var(--surface2); }
.pylon-row.field-row { border-left-style: dashed; }

/* Column widths via flex */
.ph-rank, .pr-rank { width: 32px; flex-shrink: 0; text-align: center; }
.ph-player, .pr-player { flex: 1; min-width: 0; }
.ph-driver, .pr-driver { flex: 1; min-width: 0; }
.ph-pos, .pr-pos { width: 48px; flex-shrink: 0; text-align: right; }
.ph-right { width: 68px; flex-shrink: 0; text-align: right; }
.ph-stage { width: 36px; }

.pr-rank { font-weight: 700; color: var(--text-dim); font-size: 0.8rem; }
.pylon-row:nth-child(2) .pr-rank { color: var(--accent); }

.pr-player {
  font-weight: 600;
  padding: 0 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pr-driver {
  color: var(--text);
  padding: 0 6px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 0.85rem;
}

.field-tag { font-size: 0.65rem; opacity: 0.6; }

.pr-driver-sub {
  display: none;
}

.pr-pos {
  width: 48px;
  flex-shrink: 0;
  font-weight: 700;
  text-align: right;
  padding: 0 4px;
  font-variant-numeric: tabular-nums;
}

.pr-racepts {
  width: 68px;
  flex-shrink: 0;
  text-align: right;
  padding: 0 4px;
  color: var(--green);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.pr-stage {
  width: 36px;
  flex-shrink: 0;
  text-align: right;
  padding: 0 4px;
  color: var(--text-dim);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

.pr-total {
  width: 68px;
  flex-shrink: 0;
  text-align: right;
  padding: 0 4px;
  font-weight: 800;
  color: var(--accent);
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
}

.pr-gap {
  width: 68px;
  flex-shrink: 0;
  text-align: right;
  padding: 0 4px;
  color: var(--text-dim);
  font-size: 0.8rem;
  font-variant-numeric: tabular-nums;
}

/* ── Standings table (no-race fallback) ── */
.standings-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
}
.standings-table th {
  background: var(--surface2);
  padding: 10px 16px;
  text-align: left;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
}
.standings-table td {
  padding: 10px 16px;
  border-top: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.standings-table tr:hover td { background: var(--surface2); }
.rank-col { width: 50px; text-align: center; }
.pts-col, .gap-col { width: 80px; text-align: right; }
.pts-col { font-weight: 700; color: var(--accent); }
.gap-col { color: var(--text-dim); font-size: 0.85rem; }
.name-col { font-weight: 600; }
.standings-table td.rank-col { text-align: center; color: var(--text-dim); }
.standings-table td.pts-col { text-align: right; }
.standings-table td.gap-col { text-align: right; }
.rank-1 { color: var(--accent) !important; font-weight: 700 !important; }

/* ── History table ── */
.table-scroll {
  overflow-x: auto;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.history-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  font-size: 0.82rem;
}
.history-table th {
  background: var(--surface2);
  padding: 8px 12px;
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-dim);
  white-space: nowrap;
  position: sticky;
  top: 0;
}
.history-table th:first-child,
.history-table td:first-child {
  position: sticky;
  left: 0;
  z-index: 2;
  background: var(--surface2);
}
.history-table td:first-child { background: var(--surface); font-weight: 600; }
.history-table tr:hover td:first-child { background: var(--surface2); }
.history-table td {
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  white-space: nowrap;
}
.history-table tr:hover td { background: var(--surface2); }
.pick-cell { display: flex; justify-content: space-between; gap: 8px; }
.pick-driver { max-width: 140px; overflow: hidden; text-overflow: ellipsis; }
.pick-pts { font-weight: 600; color: var(--accent); min-width: 24px; text-align: right; }
.pick-empty { color: var(--border); }

/* ── Footer ── */
footer {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 8px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.75rem;
  color: var(--text-dim);
  z-index: 10;
}
.refresh-indicator {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--green);
  transition: background 0.2s;
}
.refresh-indicator.fetching {
  background: var(--accent);
  animation: pulse 0.3s ease-in-out infinite;
}
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.3} }
#refresh-countdown { font-variant-numeric: tabular-nums; }

.no-race {
  text-align: center;
  padding: 40px;
  color: var(--text-dim);
  background: var(--surface);
  border-radius: 10px;
  border: 1px solid var(--border);
}
.no-race h3 { font-size: 1.2rem; color: var(--text); margin-bottom: 8px; }

/* ── Responsive ── */
@media (max-width: 700px) {
  header { padding: 12px 16px; }
  main { padding: 12px; }
  h1 { font-size: 1.2rem; }
  .ph-driver, .pr-driver { display: none; }
  .pr-driver-sub {
    display: block;
    font-weight: 400;
    font-size: 0.75rem;
    color: var(--text-dim);
    margin-top: 2px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .ph-stage, .pr-stage { display: none; }
  .pylon-row { font-size: 0.82rem; padding: 7px 8px; }
}
