/* THE LINE — paper-collage F1 race strip */

:root {
  --paper: #ece3d0;
  --paper-2: #e2d7bf;
  --ink: #1d1a16;
  --ink-soft: #4a443a;
  --road: #2b2926;
  --accent: #e10600;
  --card: #f7f1e3;
  --shadow: rgba(30, 20, 5, 0.35);
  --grain: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.8' numOctaves='2'/><feColorMatrix type='matrix' values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.05 0'/></filter><rect width='180' height='180' filter='url(%23n)'/></svg>");
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Space Grotesk", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
  background-image: var(--grain);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  user-select: none;
}

/* ---------- top bar ---------- */
#topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  padding: 10px 18px 14px;
  background: var(--ink);
  background-image: var(--grain);
  color: var(--card);
  clip-path: polygon(0 0, 100% 0, 100% 88%, 97% 100%, 93% 90%, 88% 99%, 83% 91%, 77% 100%, 71% 92%, 66% 99%, 60% 90%, 54% 100%, 48% 91%, 42% 99%, 36% 90%, 30% 100%, 24% 92%, 18% 99%, 12% 90%, 6% 100%, 2% 91%, 0 97%);
  padding-bottom: 22px;
  z-index: 30;
}

.brand { display: flex; align-items: baseline; gap: 12px; min-width: 0; }

.brand h1 {
  font-family: "Archivo Black", sans-serif;
  font-size: 26px;
  letter-spacing: 1px;
  margin: 0;
  color: var(--card);
  text-shadow: 3px 3px 0 var(--accent);
  white-space: nowrap;
}

.brand .sub {
  font-family: "Permanent Marker", cursive;
  font-size: 13px;
  color: #cfc4ac;
  white-space: nowrap;
}

.race-info { display: flex; align-items: center; gap: 10px; font-weight: 600; flex-wrap: wrap; justify-content: flex-end; }
#race-name { font-size: 14px; letter-spacing: 0.4px; }

.chip {
  display: inline-block;
  padding: 3px 10px;
  background: var(--card);
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.6px;
  border-radius: 3px;
  transform: rotate(-1.2deg);
  box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.5);
}

.chip.mode { background: var(--accent); color: #fff; transform: rotate(1.4deg); }
.chip.flag-green { background: #2e9e44; color: #fff; }
.chip.flag-yellow { background: #f3c623; color: var(--ink); }
.chip.flag-sc { background: #f57f17; color: #fff; }
.chip.flag-red { background: #d32f2f; color: #fff; }
.chip.flag-chequered { background: repeating-conic-gradient(#1d1a16 0 90deg, #fff 0 180deg) 0 0/12px 12px; color: transparent; }

/* ---------- stage ---------- */
#stage { flex: 1; display: flex; min-height: 0; }

#viewport {
  position: relative;
  flex: 1;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
}
#viewport.dragging { cursor: grabbing; }

#track-strip {
  position: absolute;
  left: 0; right: 0;
  top: calc(var(--track-y, 62%));
  height: 34px;
  background: var(--road);
  background-image: var(--grain);
  clip-path: polygon(0 6%, 3% 0, 9% 5%, 17% 1%, 26% 6%, 35% 0, 44% 5%, 53% 1%, 62% 6%, 71% 0, 80% 5%, 89% 1%, 100% 5%, 100% 94%, 96% 100%, 88% 95%, 79% 100%, 70% 94%, 61% 99%, 52% 94%, 43% 100%, 34% 95%, 25% 100%, 16% 94%, 8% 99%, 0 95%);
}

#track-strip::after {
  content: "";
  position: absolute;
  left: 0; right: 0; top: 50%;
  height: 3px;
  background: repeating-linear-gradient(90deg, #d9cfb8 0 26px, transparent 26px 52px);
  opacity: 0.85;
}

/* gap gridlines */
#gridlines { position: absolute; inset: 0; pointer-events: none; }
.gridline {
  position: absolute;
  top: 8%;
  bottom: 18%;
  width: 2px;
  background: rgba(60, 50, 30, 0.12);
}
.gridline .gl-label {
  position: absolute;
  bottom: -22px;
  left: -30px;
  width: 60px;
  text-align: center;
  font-size: 11px;
  font-weight: 700;
  color: rgba(60, 50, 30, 0.55);
  font-variant-numeric: tabular-nums;
}

/* ---------- car sprites ---------- */
.car {
  position: absolute;
  bottom: calc(100% - var(--track-y, 62%));
  left: 0;
  width: 0;
  cursor: pointer;
  transition: opacity 0.4s;
}

.car-inner {
  position: absolute;
  bottom: -6px;
  left: 0;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  transform-origin: bottom center;
}

.car.selected .car-inner { z-index: 25 !important; }
.car.out { opacity: 0.25; filter: grayscale(1); }

/* the comically large head */
.head-wrap {
  position: relative;
  z-index: 2;
  margin-bottom: -14px;
  filter:
    drop-shadow(2px 0 0 #fff) drop-shadow(-2px 0 0 #fff)
    drop-shadow(0 2px 0 #fff) drop-shadow(0 -2px 0 #fff)
    drop-shadow(4px 6px 5px var(--shadow));
  animation: bob var(--bob-dur, 2.3s) ease-in-out var(--bob-delay, 0s) infinite alternate;
  transform-origin: 50% 95%;
}

.head-wrap img {
  display: block;
  width: var(--head-size, 92px);
  height: auto;
  clip-path: polygon(2% 38%, 9% 12%, 22% 4%, 38% 0%, 57% 2%, 73% 7%, 86% 15%, 95% 31%, 98% 52%, 93% 71%, 97% 86%, 84% 95%, 65% 99%, 42% 97%, 24% 100%, 10% 91%, 4% 74%, 0% 55%);
  pointer-events: none;
}

/* placeholder when a driver has no usable headshot */
.head-fallback {
  width: var(--head-size, 92px);
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Archivo Black", sans-serif;
  font-size: 26px;
  color: #fff;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.4);
  clip-path: polygon(2% 38%, 9% 12%, 22% 4%, 38% 0%, 57% 2%, 73% 7%, 86% 15%, 95% 31%, 98% 52%, 93% 71%, 97% 86%, 84% 95%, 65% 99%, 42% 97%, 24% 100%, 10% 91%, 4% 74%, 0% 55%);
}

@keyframes bob {
  from { transform: rotate(var(--tilt, -3deg)) translateY(0); }
  to   { transform: rotate(calc(var(--tilt, -3deg) * -1)) translateY(-3px); }
}

body.paused .head-wrap { animation-play-state: paused; }

/* car body (svg) */
.car-body {
  position: relative;
  z-index: 1;
  width: var(--car-w, 120px);
  filter:
    drop-shadow(2px 0 0 #fff) drop-shadow(-2px 0 0 #fff)
    drop-shadow(0 2px 0 #fff) drop-shadow(0 -2px 0 #fff)
    drop-shadow(3px 5px 4px var(--shadow));
}
.car-body svg { display: block; width: 100%; height: auto; }

.wheel-spin { transform-box: fill-box; transform-origin: center; }
body.playing .wheel-spin { animation: spin 0.5s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* tag under the car */
.tag {
  margin-top: 10px;
  font-size: 11px;
  font-weight: 700;
  background: var(--card);
  border-left: 4px solid var(--team, #888);
  padding: 2px 7px 2px 5px;
  border-radius: 2px;
  white-space: nowrap;
  box-shadow: 2px 2px 0 var(--shadow);
  transform: rotate(var(--tag-tilt, -1.5deg));
  font-variant-numeric: tabular-nums;
}
.tag .pos { font-family: "Archivo Black", sans-serif; font-size: 11px; margin-right: 4px; color: var(--ink); }
.tag .gap { color: var(--ink-soft); margin-left: 4px; }
.tag .lapped { color: #b3382c; }

.car.alt .car-inner .tag { margin-top: 26px; }

/* ---------- detail card ---------- */
#detail-card {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 5000;
  width: 270px;
  background: var(--card);
  background-image: var(--grain);
  padding: 12px 14px;
  transform: rotate(-1deg);
  box-shadow: 5px 6px 0 var(--shadow);
  clip-path: polygon(1% 3%, 99% 0%, 100% 97%, 2% 100%);
}

#detail-card .dc-head { display: flex; gap: 10px; align-items: center; margin-bottom: 8px; }
#detail-card .dc-head img { width: 64px; clip-path: polygon(2% 38%, 9% 12%, 22% 4%, 38% 0%, 57% 2%, 73% 7%, 86% 15%, 95% 31%, 98% 52%, 93% 71%, 97% 86%, 84% 95%, 65% 99%, 42% 97%, 24% 100%, 10% 91%, 4% 74%, 0% 55%); }
#detail-card .dc-name { font-family: "Archivo Black", sans-serif; font-size: 16px; line-height: 1.1; }
#detail-card .dc-team { font-size: 12px; color: var(--ink-soft); font-weight: 600; }
#detail-card .dc-close { position: absolute; top: 6px; right: 10px; cursor: pointer; font-weight: 700; font-size: 14px; background: none; border: none; color: var(--ink-soft); }

#detail-card table { width: 100%; border-collapse: collapse; font-size: 12.5px; font-variant-numeric: tabular-nums; }
#detail-card td { padding: 3px 0; border-top: 1px dashed rgba(0,0,0,0.15); }
#detail-card td:last-child { text-align: right; font-weight: 700; }

.hint {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%) rotate(-0.6deg);
  font-family: "Permanent Marker", cursive;
  font-size: 12px;
  color: rgba(60, 50, 30, 0.5);
  pointer-events: none;
}

/* ---------- leaderboard ---------- */
#leaderboard {
  width: 295px;
  flex-shrink: 0;
  background: var(--paper-2);
  background-image: var(--grain);
  border-left: 3px solid var(--ink);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.board-head {
  font-family: "Archivo Black", sans-serif;
  font-size: 13px;
  letter-spacing: 1.5px;
  padding: 10px 14px 6px;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}
#board-note { font-family: "Space Grotesk"; font-size: 10px; color: var(--ink-soft); }

#board-rows { overflow-y: auto; padding: 4px 10px 14px; display: flex; flex-direction: column; gap: 4px; }

.brow {
  display: grid;
  grid-template-columns: 24px 30px 1fr 62px 58px;
  align-items: center;
  gap: 6px;
  background: var(--card);
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  border-left: 4px solid var(--team, #888);
  box-shadow: 2px 2px 0 rgba(30, 20, 5, 0.18);
  cursor: pointer;
  order: var(--ord, 99);
}
.brow:hover, .brow.selected { outline: 2px solid var(--ink); }
.brow.out { opacity: 0.45; filter: grayscale(0.8); }

.brow .bpos { font-family: "Archivo Black", sans-serif; font-size: 12px; }
.brow .bhead { width: 26px; height: 26px; object-fit: cover; object-position: top; border-radius: 50%; background: #d8cdb4; }
.brow .bacr { letter-spacing: 0.5px; }
.brow .btyre { font-size: 9px; font-weight: 800; margin-left: 4px; padding: 1px 4px; border-radius: 8px; border: 1.5px solid; }
.brow .bgap { text-align: right; color: var(--ink-soft); }
.brow .blap { text-align: right; }
.brow .blap.best { color: #7b1fa2; font-weight: 800; }

.tyre-SOFT { color: #d32f2f; border-color: #d32f2f; }
.tyre-MEDIUM { color: #b58900; border-color: #b58900; }
.tyre-HARD { color: #555; border-color: #555; }
.tyre-INTERMEDIATE { color: #2e7d32; border-color: #2e7d32; }
.tyre-WET { color: #1565c0; border-color: #1565c0; }

/* ---------- controls ---------- */
#controls {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 16px 14px;
  background: var(--ink);
  background-image: var(--grain);
  color: var(--card);
  z-index: 30;
}

#controls .btn,
#controls .group button {
  font-family: "Space Grotesk", sans-serif;
  font-weight: 700;
  font-size: 12px;
  background: var(--card);
  color: var(--ink);
  border: none;
  padding: 6px 10px;
  cursor: pointer;
  box-shadow: 2px 2px 0 rgba(0, 0, 0, 0.55);
}

#controls .btn.big { font-size: 16px; padding: 6px 16px; transform: rotate(-1deg); }
#controls .group { display: flex; gap: 4px; }
#controls .group button.on { background: var(--accent); color: #fff; }
#controls .group button:active, #controls .btn:active { transform: translate(2px, 2px); box-shadow: none; }

.scrub-wrap { flex: 1; display: flex; align-items: center; gap: 12px; min-width: 100px; }
.clock { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 13px; min-width: 80px; }

#scrubber { flex: 1; accent-color: var(--accent); height: 6px; cursor: pointer; }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  #stage { flex-direction: column; }
  #leaderboard { width: 100%; max-height: 30%; border-left: none; border-top: 3px solid var(--ink); }
  .brand .sub { display: none; }
  #controls { flex-wrap: wrap; }
  .scrub-wrap { order: 10; width: 100%; flex-basis: 100%; }
}
