/* ── Boot preloader styles ──────────────────────────────────────────────────
   Loaded via a render-blocking <link> in index.html so the splash paints on
   the first frame, before the JS bundle parses. Kept in /public (served
   verbatim) rather than an inline <style> so Vite's html-inline-proxy never
   touches it.

   The Patrol Racers wordmark floats in the upper area and NEVER moves; a small road
   band sits near the bottom with the "Loading…" label riding on it, and a
   premium 3D SVG tyre spins and ROLLS left→right along the road's top edge,
   throwing speed trails + smoke. There is no progress bar — the tyre's
   horizontal position across the road IS the load progress (the --p custom
   property, 0 → 1, set by window.__preloader.setProgress). */

#preloader {
  position: fixed;
  inset: 0;
  z-index: 9999;
  --p: 0; /* load progress 0 → 1, drives the tyre's position across the road */
  --road-h: clamp(54px, 7.5vh, 84px); /* bottom road band thickness (holds Loading…) */
  --road-b: 0px;
  --tyre-lift: clamp(22px, 4vh, 40px);
  --tyre-size: clamp(80px, 14vmin, 120px);
  background: radial-gradient(120% 90% at 50% 35%, #0d131c 0%, #080b10 70%);
  overflow: hidden;
  opacity: 1;
  transition: opacity 460ms ease;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}
#preloader.pl-hide {
  opacity: 0;
  pointer-events: none;
}

/* Positioning context filling the whole splash. The road band is pinned to the
   bottom; the tyre rolls along its top edge; the logo floats above. */
.pl-stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
}

/* ── Road band — a slim strip floated just off the bottom (--road-b), holding
   the Loading… label; the tyre rolls along its top edge. ── */
.pl-road {
  position: absolute;
  left: -10vw;
  right: -10vw;
  bottom: var(--road-b);
  height: var(--road-h);
  background: linear-gradient(180deg, #1b2230 0%, #131922 45%, #0c1119 100%);
  border-top: 2px solid rgba(120, 200, 255, 0.18);
  box-shadow: 0 -14px 40px rgba(0, 0, 0, 0.5);
}
/* Dashed lane line — sits at the vertical centre of the road strip and scrolls
   to sell forward motion. Dash period (92px) matches the
   scroll distance exactly so the loop is seamless (no jump). */
.pl-road::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  top: 50%;
  height: 4px;
  transform: translateY(-50%);
  background: repeating-linear-gradient(
    90deg,
    rgba(255, 209, 102, 0.9) 0,
    rgba(255, 209, 102, 0.9) 42px,
    transparent 42px,
    transparent 92px
  );
  animation: pl-road-scroll 0.55s linear infinite;
}
@keyframes pl-road-scroll {
  from { background-position: 0 0; }
  to   { background-position: -92px 0; }
}

/* ── Spinning 3D tyre — ROLLS along the road strip's midline; its position is
   the progress. `left` is driven by --p (6% → 94% of the stage width), so the
   tyre drives from the start line to the finish. No CSS transition: the
   index.html rAF loop rate-limits --p itself, so motion is already smooth. */
.pl-tyre {
  position: absolute;
  left: calc(6% + 88% * var(--p));
  bottom: calc(var(--road-b) + var(--road-h) / 2 - var(--tyre-size) / 2 + var(--tyre-lift));
  width: var(--tyre-size);
  height: var(--tyre-size);
  transform: translateX(-50%);
  z-index: 2;
  filter: drop-shadow(0 10px 14px rgba(0, 0, 0, 0.6));
}
/* The SVG itself is tilted into a 3/4 perspective so the flat artwork reads as
   a real wheel raked toward the camera (foreshortened, left edge nearer). The
   internal rim spin happens in the SVG's own 2D plane, so it still rolls
   correctly under the tilt. */
.pl-tyre svg {
  display: block;
  width: 100%;
  height: 100%;
  overflow: visible;
  transform: perspective(680px) rotateX(13deg) rotateY(-12deg);
  transform-origin: 50% 58%;
}
/* The wheel assembly spins hard about its own centre. Tyre body, glow + sheen
   stay put — only this group rotates. */
.pl-wheel {
  transform-box: fill-box;
  transform-origin: center;
  animation: pl-spin 0.8s linear infinite;
}
@keyframes pl-spin {
  to { transform: rotate(360deg); }
}
/* The rubber/tread rotates in the wheel's own SVG plane (so the 3/4 perspective
   still applies and it reads as rolling, not tumbling) — the same motion as the
   rim, just slower. Reuses pl-spin at a longer duration (rim is 0.8s). */
.pl-tread {
  transform-box: fill-box;
  transform-origin: center;
  animation: pl-spin 2.2s linear infinite;
}

/* Contact shadow puddle on the road under the tyre (rides along with it). */
.pl-tyre::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -4%;
  width: 88%;
  height: 14px;
  transform: translateX(-50%);
  background: radial-gradient(50% 50% at 50% 50%, rgba(0, 0, 0, 0.55) 0%, transparent 72%);
  z-index: -1;
}

/* ── Logo — dead centre, fixed. Only the glow revs; it never translates. The
   weeWizzy logo is a WIDE wordmark (~3.4:1), so it's sized by viewport WIDTH
   (vw) rather than vmin — a vmin width would shrink it on the short axis in
   landscape and leave it floating off-centre. Vertically centred (top 50%)
   with translate(-50%, -50%); the short height clears the road + tyre below. ── */
.pl-logo {
  position: absolute;
  left: 50%;
  top: 50%;
  width: clamp(270px, 58vw, 684px);
  height: auto;
  transform: translate(-50%, -50%);
  z-index: 3;
  filter:
    drop-shadow(0 0 9px rgba(60, 200, 255, 0.16))
    drop-shadow(0 0 20px rgba(60, 200, 255, 0.09))
    drop-shadow(0 6px 14px rgba(0, 0, 0, 0.55));
  animation: pl-rev 4.8s ease-in-out infinite;
  user-select: none;
  -webkit-user-drag: none;
}
/* Periodic rev — the glow briefly swells, ported from the menu's rr-logo-rev. */
@keyframes pl-rev {
  0%, 78%, 100% {
    filter:
      drop-shadow(0 0 9px rgba(60, 200, 255, 0.16))
      drop-shadow(0 0 20px rgba(60, 200, 255, 0.09))
      drop-shadow(0 6px 14px rgba(0, 0, 0, 0.55));
  }
  84% {
    filter:
      drop-shadow(0 0 13px rgba(60, 200, 255, 0.3))
      drop-shadow(0 0 26px rgba(60, 200, 255, 0.16))
      drop-shadow(0 6px 14px rgba(0, 0, 0, 0.55));
  }
}
/* Landscape: more horizontal room and a shorter screen, so dial the wide
   wordmark back to a smaller width fraction — keeps it comfortably above the
   road + tyre without dominating the splash. */
@media (orientation: landscape) {
  .pl-logo { width: clamp(252px, 41vw, 576px); }
}

/* ── Speed trails — stream backward (left) off the rolling tyre. They share the
   tyre's --p-driven x position (+ matching transition) so they move WITH the
   tyre, and trail to its left via the keyframe translateX. Just two, kept
   subtle. transform-origin:right keeps the "head" pinned at the tyre. ── */
.pl-streak {
  position: absolute;
  top: calc(100% - var(--road-b) - var(--road-h) / 2 - var(--tyre-lift) - 8px);
  left: calc(6% + 88% * var(--p));
  width: clamp(70px, 15vw, 150px);
  height: 2px;
  pointer-events: none;
  z-index: 1;
  opacity: 0;
  transform-origin: right center;
  will-change: transform, opacity;
  animation: pl-streak 0.66s cubic-bezier(0.4, 0, 0.18, 1) infinite;
}
.pl-streak.s1 { margin-top: -9px; background: linear-gradient(90deg, transparent 5%, rgba(255, 150, 70, 0.7) 95%); filter: blur(0.5px) drop-shadow(0 0 4px rgba(255, 110, 50, 0.5)); animation-delay: 0s; }
.pl-streak.s2 { margin-top: 8px; width: clamp(54px, 12vw, 118px); background: linear-gradient(90deg, transparent 5%, rgba(255, 198, 95, 0.62) 95%); filter: blur(0.45px) drop-shadow(0 0 3px rgba(255, 160, 60, 0.42)); animation-delay: 0.33s; }
@keyframes pl-streak {
  0%   { transform: translateX(-80%) scaleX(0.5);   opacity: 0; }
  30%  { opacity: 0.7; }
  100% { transform: translateX(-210%) scaleX(1.15); opacity: 0; }
}

/* Loading… label — rides low on the road band, below the centred lane dashes
   and tyre roll line. */
.pl-text {
  position: absolute;
  left: 0;
  right: 0;
  top: 75%;
  transform: translateY(-50%);
  text-align: center;
  font-weight: 700;
  letter-spacing: 0.32em;
  text-transform: uppercase;
  font-size: clamp(13px, 2.4vw, 18px);
  color: rgba(210, 235, 255, 0.82);
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
  animation: pl-text-pulse 1.4s ease-in-out infinite;
}
@keyframes pl-text-pulse {
  0%, 100% { opacity: 0.45; }
  50%      { opacity: 0.95; }
}

@media (prefers-reduced-motion: reduce) {
  .pl-logo, .pl-streak, .pl-road::after, .pl-text, .pl-wheel, .pl-tread { animation: none; }
  .pl-smoke { display: none; }
}

/* ── Boot problem / prolonged load ─────────────────────────────────────────
   Set by window.__preloader.fail()/stall(): the game cannot start yet (no WebGL,
   a constructor that threw, or a boot that exceeded 35 s). The splash STAYS so
   the player gets one plain sentence instead of the black
   screen that stripping it used to reveal. The rolling tyre and its speed
   trails are hidden so the sentence stays readable; a late successful done()
   call can still complete the handoff after the 35 s stall state.
   The label leaves the road strip and re-centres under the logo, restyled from
   the wide-tracked "LOADING…" treatment into readable sentence text. */
#preloader.pl-failed .pl-tyre,
#preloader.pl-failed .pl-streak,
#preloader.pl-failed .pl-road::after {
  display: none;
}
#preloader.pl-failed .pl-logo {
  animation: none;
}
#preloader.pl-failed .pl-text {
  /* Offset parent is .pl-road (bottom-pinned, ~54-84px tall), so bottom:100%
     lifts the message off the band into the empty space under the logo. */
  top: auto;
  bottom: calc(100% + clamp(14px, 4vh, 40px));
  transform: none;
  left: 0;
  right: 0;
  max-width: min(86vw, 40rem);
  margin: 0 auto;
  animation: none;
  opacity: 1;
  letter-spacing: 0.01em;
  text-transform: none;
  font-weight: 600;
  line-height: 1.45;
  font-size: clamp(14px, 2.6vw, 18px);
  color: rgba(226, 240, 252, 0.92);
}
