// car.jsx — three side-view car silhouettes
// Each is drawn pointing RIGHT (+X) inside a viewBox; the path follower
// rotates the wrapper to match the path tangent.


function SportsCar({
  accent = "#E63946",
  body = "#07080d",
  glass = "#111827",
  headlights = false,
}) {
  // Top-down sports car — vector artwork (SVG → background-removed PNG), points RIGHT.
  // `headlights` swaps to a variant with bright headlights + long beams baked in for
  // dark mode. Both PNGs share canvas dims so toggling doesn't shift position.
  //
  // The PNG canvas is wider than the car silhouette (extra space to the right holds
  // the headlight beams). Body width is ~38% of the canvas, biased to the LEFT.
  // We compensate the off-center body via an inner translate so the body CENTER —
  // not the canvas center — sits at the path-follower's anchor point.
  const src = headlights ? "assets/car-svg-lights.png" : "assets/car-svg.png";
  const CANVAS_W = 3449;
  const CANVAS_H = 670;
  const BODY_W = 1319;
  const BODY_LEFT = 20;          // x in canvas where car body starts
  const BODY_CENTER_X = BODY_LEFT + BODY_W / 2;
  const bodyCenterFrac = BODY_CENTER_X / CANVAS_W; // ~0.197 — fraction of canvas where body center sits
  const W = 460;                                   // overall image width on page
  const H = Math.round(W / (CANVAS_W / CANVAS_H)); // preserve canvas aspect
  return (
    <div
      style={{
        width: W,
        height: H,
        position: "relative",
        // Anchor on the BODY CENTER, not the canvas center, so rotation & path-following
        // pivot around the visual car body rather than empty beam space.
        transform: `translateX(${(0.5 - bodyCenterFrac) * 100}%)`,
      }}
    >
      {/* Blurred PNG duplicate as drop shadow. CSS `blur()` on an <img>
          respects the alpha channel (unlike `drop-shadow()` on <img>, which
          iOS Safari can draw against the bounding box). The .car parent is
          position:fixed and only its X + rotation change per scroll frame, so
          the blurred layer's bitmap is cached after the first paint and not
          re-rasterised on every frame. */}
      <img
        src={src}
        alt=""
        aria-hidden="true"
        draggable="false"
        style={{
          position: "absolute",
          top: 0,
          left: 0,
          width: "100%",
          height: "100%",
          filter: headlights ? "blur(7px) brightness(0)" : "blur(5px) brightness(0)",
          opacity: headlights ? 0.55 : 0.4,
          transform: headlights ? "translateY(4px)" : "translateY(6px)",
          pointerEvents: "none",
          userSelect: "none",
        }}
      />
      <img
        src={src}
        alt=""
        draggable="false"
        style={{
          position: "relative",
          width: "100%",
          height: "100%",
          display: "block",
          userSelect: "none",
          pointerEvents: "none",
        }}
      />
    </div>
  );
}

// Legacy SVG sports car, kept as a fallback variant in case someone wants it.
function SportsCarSVG({
  accent = "#E63946",
  body = "#07080d",
  glass = "#111827",
  headlights = false,
}) {
  const paint = body;
  const stripe = accent;

  return (
    <svg
      viewBox="0 0 340 140"
      width="170"
      height="70"
      xmlns="http://www.w3.org/2000/svg"
      style={{ overflow: "visible" }}
    >
      <defs>
        <linearGradient id="sc-body-gloss" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0" stopColor="#ffffff" stopOpacity="0.28" />
          <stop offset="0.22" stopColor="#ffffff" stopOpacity="0.08" />
          <stop offset="0.55" stopColor="#000000" stopOpacity="0" />
          <stop offset="1" stopColor="#000000" stopOpacity="0.45" />
        </linearGradient>

        <linearGradient id="sc-glass" x1="0" y1="0" x2="1" y2="0">
          <stop offset="0" stopColor="#05070a" />
          <stop offset="0.45" stopColor={glass} />
          <stop offset="1" stopColor="#05070a" />
        </linearGradient>

        <radialGradient id="sc-headlight" cx="0.5" cy="0.5" r="0.5">
          <stop offset="0" stopColor="#fffef2" />
          <stop offset="0.45" stopColor="#ffe79a" />
          <stop offset="1" stopColor="#ffcc44" stopOpacity="0.1" />
        </radialGradient>

        <radialGradient id="sc-beam" cx="0" cy="0.5" r="1">
          <stop offset="0" stopColor="#fff4b8" stopOpacity="0.7" />
          <stop offset="0.45" stopColor="#ffe08a" stopOpacity="0.25" />
          <stop offset="1" stopColor="#ffd66b" stopOpacity="0" />
        </radialGradient>
      </defs>

      {/* headlight beams */}
      {headlights && (
        <g opacity="0.75">
          <ellipse cx="380" cy="47" rx="100" ry="20" fill="url(#sc-beam)" />
          <ellipse cx="380" cy="93" rx="100" ry="20" fill="url(#sc-beam)" />
        </g>
      )}

      {/* shadow */}
      <ellipse cx="170" cy="127" rx="145" ry="7" fill="rgba(0,0,0,.28)" />

      {/* main body */}
      <path
        d="
          M 20 70
          C 20 38, 36 24, 68 20
          C 92 17, 116 18, 138 22
          C 154 12, 196 12, 218 22
          C 252 23, 286 31, 307 48
          C 319 58, 324 65, 324 70
          C 324 75, 319 82, 307 92
          C 286 109, 252 117, 218 118
          C 196 128, 154 128, 138 118
          C 116 122, 92 123, 68 120
          C 36 116, 20 102, 20 70
          Z
        "
        fill={paint}
      />

      {/* glossy overlay */}
      <path
        d="
          M 20 70
          C 20 38, 36 24, 68 20
          C 92 17, 116 18, 138 22
          C 154 12, 196 12, 218 22
          C 252 23, 286 31, 307 48
          C 319 58, 324 65, 324 70
          C 324 75, 319 82, 307 92
          C 286 109, 252 117, 218 118
          C 196 128, 154 128, 138 118
          C 116 122, 92 123, 68 120
          C 36 116, 20 102, 20 70
          Z
        "
        fill="url(#sc-body-gloss)"
      />

      {/* red racing stripe */}
      <rect x="28" y="61" width="282" height="18" rx="3" fill={stripe} />
      <rect x="28" y="57" width="282" height="3" rx="1.5" fill="#f4f4f5" opacity="0.9" />
      <rect x="28" y="80" width="282" height="3" rx="1.5" fill="#f4f4f5" opacity="0.9" />

      {/* center dark line through stripe */}
      <line x1="30" y1="70" x2="310" y2="70" stroke="#000" strokeOpacity="0.35" strokeWidth="1" />

      {/* cabin glass */}
      <path
        d="
          M 116 34
          C 139 27, 196 27, 226 34
          C 237 43, 242 56, 242 70
          C 242 84, 237 97, 226 106
          C 196 113, 139 113, 116 106
          C 107 94, 104 82, 104 70
          C 104 58, 107 46, 116 34
          Z
        "
        fill="url(#sc-glass)"
      />

      {/* windshield/rear glass separators */}
      <path d="M 151 31 Q 145 70 151 109" stroke="#000" strokeOpacity="0.45" strokeWidth="2" />
      <path d="M 215 32 Q 224 70 215 108" stroke="#000" strokeOpacity="0.45" strokeWidth="2" />

      {/* roof highlights */}
      <path d="M 125 38 Q 170 29 216 38" stroke="#fff" strokeOpacity="0.18" strokeWidth="2" fill="none" />
      <path d="M 125 102 Q 170 111 216 102" stroke="#fff" strokeOpacity="0.1" strokeWidth="1.5" fill="none" />

      {/* seats visible through glass */}
      <ellipse cx="137" cy="55" rx="11" ry="7" fill="#3a3f47" opacity="0.55" />
      <ellipse cx="137" cy="85" rx="11" ry="7" fill="#3a3f47" opacity="0.55" />
      <ellipse cx="198" cy="55" rx="12" ry="7" fill="#3a3f47" opacity="0.45" />
      <ellipse cx="198" cy="85" rx="12" ry="7" fill="#3a3f47" opacity="0.45" />

      {/* rear engine vent */}
      <path
        d="M 44 45 C 58 39, 82 38, 96 44 L 96 96 C 82 102, 58 101, 44 95 C 38 82, 38 58, 44 45 Z"
        fill="#050609"
        opacity="0.8"
      />
      <g stroke="#59606b" strokeOpacity="0.75" strokeWidth="1">
        <line x1="50" y1="53" x2="90" y2="51" />
        <line x1="48" y1="60" x2="92" y2="59" />
        <line x1="47" y1="67" x2="93" y2="67" />
        <line x1="47" y1="74" x2="93" y2="74" />
        <line x1="48" y1="81" x2="92" y2="82" />
        <line x1="50" y1="88" x2="90" y2="90" />
      </g>

      {/* front hood contour */}
      <path
        d="M 244 42 C 270 45, 294 54, 306 70 C 294 86, 270 95, 244 98"
        stroke="#000"
        strokeOpacity="0.28"
        strokeWidth="1.2"
        fill="none"
      />
      <path
        d="M 252 53 C 273 57, 289 62, 297 70 C 289 78, 273 83, 252 87"
        stroke="#fff"
        strokeOpacity="0.11"
        strokeWidth="1"
        fill="none"
      />

      {/* side sculpting lines */}
      <path d="M 72 31 C 130 19, 220 20, 286 46" stroke="#fff" strokeOpacity="0.13" strokeWidth="1.2" fill="none" />
      <path d="M 72 109 C 130 121, 220 120, 286 94" stroke="#fff" strokeOpacity="0.13" strokeWidth="1.2" fill="none" />

      {/* side mirrors */}
      <path d="M 150 22 L 166 9 L 174 14 L 166 25 Z" fill={paint} />
      <path d="M 150 118 L 166 131 L 174 126 L 166 115 Z" fill={paint} />
      <path d="M 160 13 L 171 14" stroke="#fff" strokeOpacity="0.18" strokeWidth="1" />
      <path d="M 160 127 L 171 126" stroke="#fff" strokeOpacity="0.18" strokeWidth="1" />

      {/* wheels tucked under body */}
      <ellipse cx="92" cy="22" rx="25" ry="7" fill="#050505" opacity="0.9" />
      <ellipse cx="92" cy="118" rx="25" ry="7" fill="#050505" opacity="0.9" />
      <ellipse cx="263" cy="23" rx="25" ry="7" fill="#050505" opacity="0.9" />
      <ellipse cx="263" cy="117" rx="25" ry="7" fill="#050505" opacity="0.9" />
      <ellipse cx="92" cy="22" rx="13" ry="3" fill="#3a3f47" opacity="0.7" />
      <ellipse cx="92" cy="118" rx="13" ry="3" fill="#3a3f47" opacity="0.7" />
      <ellipse cx="263" cy="23" rx="13" ry="3" fill="#3a3f47" opacity="0.7" />
      <ellipse cx="263" cy="117" rx="13" ry="3" fill="#3a3f47" opacity="0.7" />

      {/* rear taillights */}
      <path d="M 23 50 C 16 62, 16 78, 23 90" stroke="#ff2734" strokeWidth="4" strokeLinecap="round" fill="none" />
      <path d="M 28 52 C 23 63, 23 77, 28 88" stroke="#ff9aa2" strokeWidth="1.5" strokeLinecap="round" fill="none" opacity="0.8" />

      {/* front headlights */}
      <ellipse cx="307" cy="49" rx="10" ry="7" fill="#0a0c10" />
      <ellipse cx="307" cy="91" rx="10" ry="7" fill="#0a0c10" />
      <ellipse cx="309" cy="49" rx="6" ry="4.5" fill={headlights ? "url(#sc-headlight)" : "#d8dde5"} />
      <ellipse cx="309" cy="91" rx="6" ry="4.5" fill={headlights ? "url(#sc-headlight)" : "#d8dde5"} />

      {/* front bumper intake */}
      <path
        d="M 302 61 C 315 64, 320 67, 321 70 C 320 73, 315 76, 302 79 Z"
        fill="#050609"
        opacity="0.8"
      />

      {/* tiny badge */}
      <ellipse cx="276" cy="70" rx="3.5" ry="4.5" fill="#e5b93c" stroke="#000" strokeOpacity="0.5" strokeWidth="0.5" />
      <line x1="276" y1="66" x2="276" y2="74" stroke="#c9242f" strokeWidth="1" />
    </svg>
  );
}


function F1Car({ accent = "#E63946", body = "#1b1d23", headlights = false }) {
  // Top-down F1 silhouette pointing RIGHT.
  return (
    <svg viewBox="0 0 220 90" width="130" height="53" xmlns="http://www.w3.org/2000/svg">
      <ellipse cx="110" cy="80" rx="100" ry="5" fill="rgba(0,0,0,.18)" />
      {/* rear wing */}
      <rect x="6" y="20" width="22" height="50" rx="2" fill={body} />
      <rect x="6" y="40" width="22" height="10" fill={accent} />
      {/* engine/airbox */}
      <rect x="30" y="34" width="48" height="22" rx="3" fill={body} />
      {/* sidepods */}
      <path d="M 78 30 L 150 30 Q 156 36 156 45 Q 156 54 150 60 L 78 60 Q 72 54 72 45 Q 72 36 78 30 Z" fill={body} />
      {/* nose */}
      <path d="M 150 38 L 198 42 L 198 48 L 150 52 Z" fill={body} />
      {/* front wing */}
      <rect x="192" y="22" width="18" height="46" rx="2" fill={body} />
      <rect x="192" y="40" width="18" height="10" fill={accent} />
      {/* cockpit */}
      <ellipse cx="100" cy="45" rx="14" ry="8" fill="#2a2e36" />
      <rect x="92" y="42" width="16" height="6" rx="2" fill="#9aa3ad" />
      {/* halo */}
      <path d="M 88 30 Q 100 24 112 30" stroke="#0e0f12" strokeWidth="2" fill="none" />
      {/* tyres */}
      <rect x="38" y="6" width="18" height="14" rx="3" fill="#0a0a0a" />
      <rect x="38" y="70" width="18" height="14" rx="3" fill="#0a0a0a" />
      <rect x="158" y="14" width="16" height="10" rx="2" fill="#0a0a0a" />
      <rect x="158" y="66" width="16" height="10" rx="2" fill="#0a0a0a" />
      {/* livery stripe */}
      <rect x="32" y="42" width="118" height="6" fill={accent} opacity=".9" />
    </svg>
  );
}

function GoKart({ accent = "#E63946", body = "#1b1d23", headlights = false }) {
  // Top-down kart silhouette pointing RIGHT.
  return (
    <svg viewBox="0 0 160 100" width="100" height="63" xmlns="http://www.w3.org/2000/svg">
      <ellipse cx="80" cy="90" rx="68" ry="5" fill="rgba(0,0,0,.18)" />
      {/* chassis frame */}
      <rect x="18" y="34" width="124" height="32" rx="4" fill={body} />
      {/* seat */}
      <path d="M 36 40 Q 30 50 36 60 L 60 60 L 60 40 Z" fill={accent} />
      {/* driver helmet circle */}
      <circle cx="80" cy="50" r="12" fill="#0e0f12" />
      <path d="M 70 50 Q 80 56 90 50" stroke="#fff" strokeWidth="1.4" fill="none" />
      <rect x="74" y="44" width="12" height="5" rx="1" fill="#3a4150" />
      {/* steering wheel */}
      <circle cx="106" cy="50" r="6" fill="none" stroke={accent} strokeWidth="2" />
      {/* number plate */}
      <rect x="18" y="44" width="14" height="12" rx="2" fill="#fff" stroke={body} strokeWidth="1" />
      <text x="25" y="54" fontSize="10" fontFamily="ui-monospace, monospace" fontWeight="700" textAnchor="middle" fill={body}>7</text>
      {/* tires — at all four corners */}
      <rect x="24" y="14" width="18" height="14" rx="3" fill="#0a0a0a" />
      <rect x="24" y="72" width="18" height="14" rx="3" fill="#0a0a0a" />
      <rect x="118" y="14" width="18" height="14" rx="3" fill="#0a0a0a" />
      <rect x="118" y="72" width="18" height="14" rx="3" fill="#0a0a0a" />
    </svg>
  );
}

window.SportsCar = React.memo(SportsCar);
window.F1Car = React.memo(F1Car);
window.GoKart = React.memo(GoKart);

window.CAR_VARIANTS = {
  sports: window.SportsCar,
  f1: window.F1Car,
  kart: window.GoKart,
};
