:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #242836;
  --border: #2e3348;
  --text: #e8eaef;
  --muted: #8b92a8;
  --accent: #e10600;
  --accent-dim: #a80400;
  --best: #22c55e;
  --gold: #f59e0b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

header {
  background: linear-gradient(135deg, var(--accent-dim), var(--accent));
  padding: 1.25rem 1.5rem;
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 1rem;
  justify-content: space-between;
  box-shadow: 0 4px 24px rgba(225, 6, 0, 0.25);
}

.header-left {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.view-tabs {
  display: flex;
  gap: 0.35rem;
}

.tab-btn {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.25);
  color: rgba(255, 255, 255, 0.85);
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.8rem;
}

.tab-btn:hover {
  background: rgba(0, 0, 0, 0.4);
}

.tab-btn.active {
  background: rgba(255, 255, 255, 0.95);
  color: var(--accent-dim);
  border-color: transparent;
  font-weight: 600;
}

header h1 {
  margin: 0;
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

header .sub {
  opacity: 0.9;
  font-size: 0.85rem;
  font-weight: 400;
}

.date-controls {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.date-controls input[type="date"],
.date-controls input[type="number"] {
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: white;
  padding: 0.4rem 0.6rem;
  border-radius: 6px;
  font-size: 0.9rem;
}

.date-controls input[type="number"] {
  width: 4.2rem;
}

.days-control {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: rgba(255, 255, 255, 0.9);
  font-size: 0.85rem;
}

.days-control[hidden] {
  display: none;
}

.date-controls button {
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(255, 255, 255, 0.35);
  color: white;
  padding: 0.45rem 0.9rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 0.85rem;
}

.date-controls button:hover {
  background: rgba(0, 0, 0, 0.5);
}

.layout {
  display: none;
  grid-template-columns: minmax(280px, 340px) 1fr;
  min-height: calc(100vh - 72px);
}

.layout.view-active {
  display: grid;
}

@media (max-width: 800px) {
  .layout {
    grid-template-columns: 1fr;
  }
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow-y: auto;
  max-height: calc(100vh - 72px);
}

.races-section-label {
  padding: 0.65rem 0.75rem 0.25rem;
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--gold);
  list-style: none;
}

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--gold);
  cursor: pointer;
  font: inherit;
  text-decoration: underline;
}

.link-btn:hover {
  color: var(--text);
}

.sidebar h2 {
  margin: 0;
  padding: 1rem 1.25rem 0.5rem;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.run-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem;
}

.run-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.75rem 1rem;
  color: var(--text);
  cursor: pointer;
  margin-bottom: 0.35rem;
  transition: background 0.15s, border-color 0.15s;
}

.run-item:hover {
  background: var(--surface2);
}

.run-item.active {
  background: var(--surface2);
  border-color: var(--accent);
}

.run-item .time {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--gold);
}

.run-item .name {
  font-weight: 600;
  margin-top: 0.15rem;
}

.run-item .meta {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 0.2rem;
}

.main {
  padding: 1.25rem 1.5rem 2rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.main-single {
  grid-column: 1 / -1;
}

.pilots-day {
  border-top: 1px solid var(--border);
  padding-top: 1.25rem;
}

.pilots-day-title {
  margin: 0 0 0.75rem;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
}

.pilots-day-loading {
  margin: 0;
  font-size: 0.85rem;
  color: var(--muted);
}

.pilots-day-empty {
  color: var(--muted);
  font-size: 0.9rem;
}

.pilots-summary-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.pilots-summary-table th,
.pilots-summary-table td {
  padding: 0.6rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.pilots-summary-table th {
  background: var(--surface2);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.pilots-summary-table tr:last-child td {
  border-bottom: none;
}

.pilots-summary-table .pilot-name {
  font-weight: 600;
}

.pilots-summary-table .num-cell {
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.race-groups,
.race-all-participants {
  margin-bottom: 1.5rem;
}

.race-groups-title {
  margin: 0 0 0.75rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
}

.groups-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 1rem;
}

.group-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem 0.85rem;
}

.group-card-title {
  margin: 0;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--gold);
  line-height: 1.3;
}

.group-card-count {
  margin: 0.2rem 0 0.6rem;
  font-size: 0.75rem;
  color: var(--muted);
}

.group-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.group-table th,
.group-table td {
  padding: 0.35rem 0.4rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.group-table th {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 600;
}

.group-table tr:last-child td {
  border-bottom: none;
}

.group-table .pilot-name {
  font-weight: 500;
}

.group-table .points-cell {
  font-variant-numeric: tabular-nums;
  text-align: right;
  color: var(--gold);
  font-weight: 600;
}

.empty-state,
.loading {
  color: var(--muted);
  text-align: center;
  padding: 3rem 1rem;
}

.error {
  background: rgba(225, 6, 0, 0.15);
  border: 1px solid var(--accent);
  border-radius: 8px;
  padding: 1rem;
  color: #fca5a5;
}

.run-header {
  margin-bottom: 1.25rem;
}

.run-header h2 {
  margin: 0 0 0.35rem;
  font-size: 1.6rem;
}

.run-header .tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.tag {
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.25rem 0.6rem;
  border-radius: 999px;
  font-size: 0.8rem;
  color: var(--muted);
}

.tag.finished {
  color: var(--best);
  border-color: rgba(34, 197, 94, 0.4);
}

.tag a {
  color: var(--gold);
  text-decoration: none;
}

.tag a:hover {
  text-decoration: underline;
}

.results-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.results-table th,
.results-table td {
  padding: 0.65rem 0.75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

.results-table th {
  background: var(--surface2);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  font-weight: 600;
}

.results-table tr:last-child td {
  border-bottom: none;
}

.results-table tr:hover td {
  background: rgba(255, 255, 255, 0.03);
}

.results-table .pos {
  font-weight: 700;
  width: 2.5rem;
}

.results-table .kart {
  font-weight: 700;
  color: var(--gold);
}

.results-table .best {
  color: var(--best);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

.results-table .session-best td {
  background: rgba(34, 197, 94, 0.08);
}

.results-table .sector {
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.85rem;
}

.results-table tr.pilot-row {
  cursor: pointer;
}

.results-table tr.pilot-row:hover td {
  background: rgba(225, 6, 0, 0.08);
}

.results-table tr.pilot-row-selected td {
  background: rgba(225, 6, 0, 0.14);
}

.laps-hint {
  color: var(--muted);
  font-size: 0.75rem;
}

.pilot-laps-panel {
  margin-top: 1.25rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 1rem;
}

.pilot-laps-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 0.75rem;
}

.pilot-laps-header h3 {
  margin: 0 0 0.25rem;
  font-size: 1rem;
}

.pilot-laps-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.pilot-laps-close {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--muted);
  width: 2rem;
  height: 2rem;
  border-radius: 6px;
  cursor: pointer;
  font-size: 1.25rem;
  line-height: 1;
}

.pilot-laps-close:hover {
  color: var(--text);
  border-color: var(--muted);
}

.laps-table .lap-time,
.laps-table .sector,
.laps-table .lap-pos,
.laps-table .lap-num {
  font-variant-numeric: tabular-nums;
}

.laps-table .personal-best {
  color: var(--best);
  font-weight: 600;
}

.laps-table .total-best {
  color: var(--gold);
  font-weight: 700;
}

.laps-table tr.deleted-lap td,
.laps-table tr.rejected-lap td {
  opacity: 0.45;
  text-decoration: line-through;
}

.laps-table tr.warmup-lap .lap-num,
.laps-table tr.green-lap .lap-time {
  color: var(--muted);
}

.hint {
  margin-top: 1.5rem;
  font-size: 0.8rem;
  color: var(--muted);
}

.hint code {
  background: var(--surface2);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.hint.offline {
  color: var(--best);
}

.hint a {
  color: var(--gold);
}

.kart-rating-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 1rem;
}

.kart-rating-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.85rem;
}

.kart-rating-card h3 {
  margin: 0 0 0.7rem;
  font-size: 0.95rem;
  color: var(--gold);
}

.kart-matrix-section {
  margin-bottom: 1.5rem;
}

.kart-matrix-title {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
  color: var(--gold);
}

.kart-matrix-hint {
  margin: 0 0 0.75rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.kart-matrix-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
}

.kart-matrix-table {
  width: max-content;
  min-width: 100%;
  border-collapse: collapse;
  font-size: 0.82rem;
}

.kart-matrix-table th,
.kart-matrix-table td {
  padding: 0.45rem 0.55rem;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  text-align: center;
  white-space: nowrap;
}

.kart-matrix-table th:last-child,
.kart-matrix-table td:last-child {
  border-right: none;
}

.kart-matrix-table tr:last-child th,
.kart-matrix-table tr:last-child td {
  border-bottom: none;
}

.kart-matrix-table thead th {
  background: rgba(255, 193, 7, 0.08);
  color: var(--gold);
  font-weight: 600;
}

.kart-matrix-pilot {
  position: sticky;
  left: 0;
  z-index: 1;
  text-align: left;
  background: var(--surface);
  min-width: 9rem;
  max-width: 14rem;
  white-space: normal;
}

.kart-matrix-table thead .kart-matrix-pilot {
  background: #1a1a1a;
}

.kart-matrix-kart {
  min-width: 4.5rem;
}

.kart-matrix-cell.best {
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.kart-matrix-cell.empty {
  color: var(--muted);
}

.kart-median-section {
  margin-bottom: 1.5rem;
}

.kart-median-summary {
  margin: 0;
}

.kart-median-kart {
  font-weight: 600;
  color: var(--gold);
}

.kart-median-delta {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}

.kart-median-faster {
  color: #7dcea0;
}

.kart-median-slower {
  color: #f1948a;
}

.kart-median-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 0.85rem;
  margin-top: 1rem;
}

.kart-median-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.75rem;
}

.kart-median-card h4 {
  margin: 0 0 0.6rem;
  font-size: 0.9rem;
  color: var(--gold);
}

.kart-median-delta-inline {
  margin-left: 0.35rem;
  font-weight: 600;
  color: var(--text);
}

.kart-sectors-section {
  margin-bottom: 1.5rem;
}

.kart-sectors-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 1rem;
}

.kart-sector-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.85rem;
}

.kart-sector-card h4 {
  margin: 0 0 0.65rem;
  font-size: 0.95rem;
  color: var(--gold);
}

.kart-sector-meta {
  font-size: 0.78rem;
  color: var(--muted);
  white-space: nowrap;
}
