// APEX SERIES — fictional motorsport championship
// Real Laguna Seca track. Original teams, drivers, sponsors.

const RACE_META = {
  series: "APEX SERIES",
  round: "ROUND 04",
  event: "GRAND PRIX OF MONTEREY",
  circuit: "WEATHERTECH RACEWAY LAGUNA SECA",
  location: "MONTEREY, CALIFORNIA",
  length_km: 3.602,
  length_mi: 2.238,
  turns: 11,
  laps_total: 95,
  date: "MAY 17, 2026",
  weather: { air_c: 19, track_c: 31, wind_kph: 14, condition: "DRY / SUNNY", humidity: 58 },
};

// Each turn on Laguna Seca, with the famous ones called out
const TURNS = [
  { n: 1, name: "ANDRETTI HAIRPIN", note: "Heavy braking from 280→90 km/h" },
  { n: 2, name: "T2", note: "" },
  { n: 3, name: "T3", note: "" },
  { n: 4, name: "T4", note: "" },
  { n: 5, name: "T5", note: "" },
  { n: 6, name: "RAHAL STRAIGHT EXIT", note: "" },
  { n: 7, name: "T7", note: "" },
  { n: 8, name: "THE CORKSCREW", note: "5.5-storey blind drop. Brake on the dirt patch." },
  { n: 9, name: "RAINEY CURVE", note: "" },
  { n: 10, name: "T10", note: "" },
  { n: 11, name: "T11", note: "Last apex — onto the front straight" },
];

// 12 cars — original liveries, teams, drivers
const CARS = [
  {
    no: 7, pos: 1, name: "MAREN VOSS", country: "SE", age: 28,
    team: "ARC NORDIC RACING", short: "ARC",
    livery: { primary: "#E8E4DC", secondary: "#0B0B0B", accent: "#C8102E", pattern: "stripe-diag" },
    chassis: "Dallara DW13", engine: "Honda HI26 2.2L V6 Twin-Turbo (760 hp)",
    crew_chief: "TOMASZ BERG", race_engineer: "PIA HALSTEAD",
    sponsors: { title: "SHELL V-POWER", primary: ["MOBIL 1", "BREMBO", "BOSCH"], support: ["SPARCO", "RAY-BAN", "TAG HEUER", "PIRELLI", "STANLEY", "TOTAL ENERGIES"] },
    bio: "Stockholm-born, karted in the Nordic championship by 11. Won APEX Rookie of the Year in 2023. Cool under pressure — known on the radio as 'The Glacier'. 3rd-generation racer; her grandfather Jan Voss won Le Mans in '68.",
    helmet: { primary: "#E8E4DC", secondary: "#C8102E", visor: "#0B0B0B" },
    crew_chief_bio: "20 years in the pit lane. Came up through Le Mans prototypes before crossing to APEX. Runs the cleanest sub-2.4s stops in the paddock.",
    race_engineer_bio: "Aerodynamicist by training (KTH Royal Institute). Joined ARC after four years at the Honda HI engine programme.",
    last_lap: "1:09.842", best_lap: "1:09.214", gap: "LEADER",
    tire: "S", tire_age: 14, fuel: 38.4,
    sectors: [{ s: "S1", t: "21.104", state: "purple" }, { s: "S2", t: "26.918", state: "green" }, { s: "S3", t: "21.820", state: "yellow" }],
  },
  {
    no: 22, pos: 2, name: "RAFA MORENO", country: "BR", age: 31,
    team: "VELOCITÀ MILANO", short: "VLM",
    livery: { primary: "#0B1F3A", secondary: "#C9A24A", accent: "#E8E4DC", pattern: "stripe-vert" },
    chassis: "Dallara DW13", engine: "Ferrari 066/12 2.2L V6 Twin-Turbo (758 hp)",
    crew_chief: "ENZO BARRA", race_engineer: "SOFIA REI",
    sponsors: { title: "PIRELLI", primary: ["MARTINI RACING", "TAG HEUER", "MAGNETI MARELLI"], support: ["OMP", "BREMBO", "ENI", "ALPINESTARS", "MOMO", "SABELT"] },
    bio: "São Paulo native who climbed the European single-seater ladder. Twice runner-up in F2 before APEX picked him up. Aggressive on cold tyres, brilliant in the wet. Plays bossa nova guitar between sessions.",
    helmet: { primary: "#0B1F3A", secondary: "#C9A24A", visor: "#0B0B0B" },
    crew_chief_bio: "Maranello veteran. Ran the #44 Ferrari WEC car for three seasons. Demands a 1.8s fuel-only stop or shorter.",
    race_engineer_bio: "First female lead engineer in Velocità's 40-year history. PhD in tyre dynamics from Politecnico di Milano.",
    last_lap: "1:09.901", best_lap: "1:09.342", gap: "+0.842",
    tire: "S", tire_age: 14, fuel: 38.1,
    sectors: [{ s: "S1", t: "21.218", state: "green" }, { s: "S2", t: "27.014", state: "yellow" }, { s: "S3", t: "21.669", state: "purple" }],
  },
  {
    no: 11, pos: 3, name: "OKONKWO ADEYEMI", country: "NG", age: 26,
    team: "MERIDIAN GP", short: "MRD",
    livery: { primary: "#0E5E3A", secondary: "#0B0B0B", accent: "#FFFFFF", pattern: "stripe-horiz" },
    chassis: "Dallara DW13", engine: "Honda HI26 2.2L V6 Twin-Turbo (760 hp)",
    crew_chief: "AKIN WALSH", race_engineer: "JUNE OKAFOR",
    sponsors: { title: "PUMA", primary: ["CASTROL EDGE", "DHL", "MICHELIN"], support: ["GULF OIL", "MOTUL", "AKEBONO", "LIQUI MOLY", "OAKLEY", "STILO"] },
    bio: "Lagos-born. Came through the African karting circuit, then Formula Regional Asia. Famous for his late-braking move on Parrish at Spa. First Nigerian to score an APEX podium.",
    helmet: { primary: "#0E5E3A", secondary: "#FFFFFF", visor: "#0B0B0B" },
    crew_chief_bio: "Ex-McLaren mechanic. Built Meridian's pit crew from scratch when the team joined APEX in 2024.",
    race_engineer_bio: "Strategy specialist. Called the one-stop pivot at Long Beach that vaulted the team from P9 to P3.",
    last_lap: "1:10.012", best_lap: "1:09.488", gap: "+1.221",
    tire: "M", tire_age: 28, fuel: 41.7,
    sectors: [{ s: "S1", t: "21.402", state: "yellow" }, { s: "S2", t: "27.118", state: "yellow" }, { s: "S3", t: "21.492", state: "green" }],
  },
  {
    no: 4, pos: 4, name: "JUNE TANAKA", country: "JP", age: 24,
    team: "KINTSUGI MOTORSPORT", short: "KIN",
    livery: { primary: "#0B0B0B", secondary: "#D4AF37", accent: "#C8102E", pattern: "kintsugi" },
    chassis: "Dallara DW13", engine: "Honda HI26 2.2L V6 Twin-Turbo (760 hp)",
    crew_chief: "HARUTO MIZUKI", race_engineer: "REN TAKEDA",
    sponsors: { title: "BRIDGESTONE", primary: ["ENEOS", "SEIKO", "DENSO"], support: ["MOTUL", "ARAI", "RECARO", "ALPINESTARS", "NGK", "YOKOHAMA"] },
    bio: "Suzuka-trained, Super Formula champion in 2024. Smooth and analytical — engineers say her data is 'eerily repeatable'. APEX rookie this season; already on the podium.",
    helmet: { primary: "#0B0B0B", secondary: "#D4AF37", visor: "#C8102E" },
    crew_chief_bio: "Three-time Super GT champion crew chief. First season outside Japan. Brings a meticulous, low-noise pit philosophy.",
    race_engineer_bio: "Tokyo Tech grad. Wrote the regen-braking model that APEX teams now reference.",
    last_lap: "1:10.118", best_lap: "1:09.611", gap: "+1.918",
    tire: "M", tire_age: 28, fuel: 41.4,
    sectors: [{ s: "S1", t: "21.488", state: "yellow" }, { s: "S2", t: "27.244", state: "yellow" }, { s: "S3", t: "21.601", state: "green" }],
  },
  {
    no: 19, pos: 5, name: "ELLIS PARRISH", country: "GB", age: 33,
    team: "ALBION HOUSE", short: "ALB",
    livery: { primary: "#1D3F6E", secondary: "#E8E4DC", accent: "#C8102E", pattern: "stripe-diag" },
    chassis: "Dallara DW13", engine: "Mercury M-26 2.2L V6 Twin-Turbo (752 hp)",
    crew_chief: "RHYS HEWITT", race_engineer: "ANNA BOULT",
    sponsors: { title: "ROLEX", primary: ["BRITISH PETROLEUM", "VODAFONE", "SANTANDER"], support: ["MICHELIN", "HUGO BOSS", "CHANDON", "BREMBO", "OMP", "MAHLE"] },
    bio: "Silverstone karter at 8, F3 champion at 19. Veteran — this is his 11th season at the top tier. Calm radio voice; mentor to half the grid's rookies.",
    helmet: { primary: "#1D3F6E", secondary: "#E8E4DC", visor: "#0B0B0B" },
    crew_chief_bio: "Welsh, ex-RAF logistics. Runs Albion's pit wall like a military operation. 90% stops under 2.5s in 2025.",
    race_engineer_bio: "Cambridge engineering. Led the wet-weather setup programme — Albion is undefeated in rain since 2024.",
    last_lap: "1:10.214", best_lap: "1:09.722", gap: "+2.402",
    tire: "M", tire_age: 28, fuel: 41.0,
    sectors: [{ s: "S1", t: "21.514", state: "yellow" }, { s: "S2", t: "27.318", state: "yellow" }, { s: "S3", t: "21.704", state: "yellow" }],
  },
  {
    no: 33, pos: 6, name: "CASIMIR DIOR", country: "FR", age: 29,
    team: "ÉCURIE BLEU", short: "EBL",
    livery: { primary: "#0E2D5C", secondary: "#E8E4DC", accent: "#D4AF37", pattern: "stripe-vert" },
    chassis: "Dallara DW13", engine: "Mercury M-26 2.2L V6 Twin-Turbo (752 hp)",
    crew_chief: "LUC FERRAND", race_engineer: "MARIE HERVÉ",
    sponsors: { title: "TOTAL ENERGIES", primary: ["MICHELIN", "RICHARD MILLE", "ELF"], support: ["SPARCO", "MOTUL", "MAHLE", "BREMBO", "RECARO", "PEUGEOT"] },
    bio: "Le Mans-born, son of a Le Mans 24 Hours winner. Fastest qualifier in last year's championship. Quiet, intense — keeps a notebook on every corner he's ever driven.",
    helmet: { primary: "#0E2D5C", secondary: "#D4AF37", visor: "#C8102E" },
    crew_chief_bio: "30 years at Peugeot Sport. Ran the LMP1 programme. Speaks four languages on the radio.",
    race_engineer_bio: "ENSAM Paris. Moved from Formula E to APEX in 2023; brought regen-mapping expertise.",
    last_lap: "1:10.402", best_lap: "1:09.811", gap: "+3.118",
    tire: "M", tire_age: 28, fuel: 40.6,
    sectors: [{ s: "S1", t: "21.612", state: "yellow" }, { s: "S2", t: "27.488", state: "yellow" }, { s: "S3", t: "21.802", state: "yellow" }],
  },
  {
    no: 88, pos: 7, name: "LIV SCHRADER", country: "DE", age: 27,
    team: "EISEN-HAUS", short: "EIS",
    livery: { primary: "#1A1A1A", secondary: "#9D9DA0", accent: "#C8102E", pattern: "stripe-horiz" },
    chassis: "Dallara DW13", engine: "Honda HI26 2.2L V6 Twin-Turbo (760 hp)",
    crew_chief: "FRIEDRICH KOLB", race_engineer: "MARLA WIRTH",
    sponsors: { title: "BOSCH", primary: ["MOBIL 1", "ALLIANZ", "BMW"], support: ["RECARO", "SACHS", "ZF", "MAHLE", "CONTINENTAL", "PUMA"] },
    bio: "Bavarian, came up through ADAC GT. Engineering-minded driver — has a master's in mechatronics from TU Munich. Famously methodical setup process.",
    helmet: { primary: "#1A1A1A", secondary: "#9D9DA0", visor: "#C8102E" },
    crew_chief_bio: "From the Mercedes DTM programme. Brought a six-step pit-stop checklist that's now standard across Eisen-Haus.",
    race_engineer_bio: "Aerodynamics PhD, ex-Audi Sport. Rebuilt EIS's CFD pipeline in her first six months.",
    last_lap: "1:10.488", best_lap: "1:09.902", gap: "+3.611",
    tire: "M", tire_age: 28, fuel: 40.3,
    sectors: [{ s: "S1", t: "21.702", state: "yellow" }, { s: "S2", t: "27.502", state: "yellow" }, { s: "S3", t: "21.918", state: "yellow" }],
  },
  {
    no: 51, pos: 8, name: "DESMOND CHAU", country: "HK", age: 30,
    team: "PACIFIC CREST", short: "PAC",
    livery: { primary: "#E8E4DC", secondary: "#0E5E3A", accent: "#0B0B0B", pattern: "stripe-diag" },
    chassis: "Dallara DW13", engine: "Mercury M-26 2.2L V6 Twin-Turbo (752 hp)",
    crew_chief: "WEI LAU", race_engineer: "MAX GOLDEN",
    sponsors: { title: "CATHAY PACIFIC", primary: ["HSBC", "TUDOR", "SHELL"], support: ["YOKOHAMA", "RAY-BAN", "TAG HEUER", "OAKLEY", "MONSTER ENERGY", "STILO"] },
    bio: "Hong Kong native, came up through Asian F3. Veteran of GT3 endurance racing before crossing to single-seaters. Strategist on track — saves tyres better than anyone.",
    helmet: { primary: "#E8E4DC", secondary: "#0E5E3A", visor: "#0B0B0B" },
    crew_chief_bio: "Decade at Toyota Gazoo Racing's WEC programme. Specializes in long-stint strategy.",
    race_engineer_bio: "Stanford ME. Joined Pacific Crest as their first US-based engineer; now leads vehicle dynamics.",
    last_lap: "1:10.602", best_lap: "1:10.018", gap: "+4.288",
    tire: "H", tire_age: 42, fuel: 44.2,
    sectors: [{ s: "S1", t: "21.804", state: "yellow" }, { s: "S2", t: "27.612", state: "yellow" }, { s: "S3", t: "22.018", state: "yellow" }],
  },
  {
    no: 6, pos: 9, name: "TRACE HOLLOWAY", country: "US", age: 35,
    team: "BAJA CASTLE", short: "BJC",
    livery: { primary: "#C8102E", secondary: "#0B0B0B", accent: "#E8E4DC", pattern: "stripe-vert" },
    chassis: "Dallara DW13", engine: "Honda HI26 2.2L V6 Twin-Turbo (760 hp)",
    crew_chief: "JESSE DUNCAN", race_engineer: "PERRY CASTILLO",
    sponsors: { title: "MONSTER ENERGY", primary: ["JIM BEAM", "GULF OIL", "GOODYEAR"], support: ["OAKLEY", "ALPINESTARS", "PIRELLI", "MOTUL", "STANLEY", "RAY-BAN"] },
    bio: "Texas-born, Nashville-raised. Ex-NASCAR Cup driver who crossed to open-wheel at 32. Cult following — wears a cowboy hat to the podium. Wins in the rain, loses in the qualifying simulator.",
    helmet: { primary: "#C8102E", secondary: "#0B0B0B", visor: "#E8E4DC" },
    crew_chief_bio: "Lifelong NASCAR mechanic. Crossed over with Trace. Famously fast on no-stop calls.",
    race_engineer_bio: "Rice University, started in IndyCar. Maintains the team's setup database — over 4,000 entries.",
    last_lap: "1:10.711", best_lap: "1:10.118", gap: "+5.014",
    tire: "H", tire_age: 42, fuel: 44.0,
    sectors: [{ s: "S1", t: "21.918", state: "yellow" }, { s: "S2", t: "27.704", state: "yellow" }, { s: "S3", t: "22.089", state: "yellow" }],
  },
  {
    no: 14, pos: 10, name: "SILJE HAUG", country: "NO", age: 25,
    team: "FJORD DYNAMICS", short: "FJD",
    livery: { primary: "#0B3D5C", secondary: "#E8E4DC", accent: "#5BB7E2", pattern: "stripe-horiz" },
    chassis: "Dallara DW13", engine: "Mercury M-26 2.2L V6 Twin-Turbo (752 hp)",
    crew_chief: "OVE LARSSEN", race_engineer: "INGRID HOLM",
    sponsors: { title: "EQUINOR", primary: ["VOLVO", "DNB", "TELENOR"], support: ["ALPINESTARS", "MICHELIN", "PIRELLI", "OMP", "MOTUL", "ATLAS COPCO"] },
    bio: "From Bergen. Junior World Karting champion at 14. Quiet, surgical driver — one fast lap per session is enough for her. Lives with two huskies.",
    helmet: { primary: "#0B3D5C", secondary: "#5BB7E2", visor: "#E8E4DC" },
    crew_chief_bio: "Ex-Volvo S60 STCC programme. Cold-weather setup expert. Two clean stops under 2.40s this season.",
    race_engineer_bio: "NTNU Trondheim. Led Fjord's data team before promotion. Calm under fire.",
    last_lap: "1:10.802", best_lap: "1:10.244", gap: "+5.788",
    tire: "H", tire_age: 42, fuel: 43.8,
    sectors: [{ s: "S1", t: "21.988", state: "yellow" }, { s: "S2", t: "27.808", state: "yellow" }, { s: "S3", t: "22.118", state: "yellow" }],
  },
  {
    no: 9, pos: 11, name: "MATEO REYES", country: "MX", age: 22,
    team: "SOL ROJO", short: "SOL",
    livery: { primary: "#E8E4DC", secondary: "#C8102E", accent: "#D4AF37", pattern: "stripe-diag" },
    chassis: "Dallara DW13", engine: "Honda HI26 2.2L V6 Twin-Turbo (760 hp)",
    crew_chief: "OSCAR MENDEZ", race_engineer: "ANA QUINTANA",
    sponsors: { title: "TELMEX", primary: ["CORONA", "INFINITI", "HERDEZ"], support: ["MOBIL 1", "PEMEX", "ARAI", "PIRELLI", "OMP", "RAY-BAN"] },
    bio: "Mexico City prodigy. Karted in Europe from 13. Youngest APEX driver this season — already qualifying inside the top 8. Backed by Telmex from his rookie year.",
    helmet: { primary: "#E8E4DC", secondary: "#C8102E", visor: "#D4AF37" },
    crew_chief_bio: "From the Pato O'Ward IndyCar programme. Mentor to the team's young pit crew.",
    race_engineer_bio: "ITESM Monterrey. First Mexican female lead engineer in APEX history.",
    last_lap: "1:10.918", best_lap: "1:10.402", gap: "+6.502",
    tire: "S", tire_age: 4, fuel: 36.2,
    sectors: [{ s: "S1", t: "21.488", state: "green" }, { s: "S2", t: "27.218", state: "green" }, { s: "S3", t: "22.212", state: "yellow" }],
  },
  {
    no: 27, pos: 12, name: "VEDA RAMACHANDRAN", country: "IN", age: 24,
    team: "MONSOON RACING", short: "MON",
    livery: { primary: "#5C1F4D", secondary: "#D4AF37", accent: "#E8E4DC", pattern: "stripe-vert" },
    chassis: "Dallara DW13", engine: "Mercury M-26 2.2L V6 Twin-Turbo (752 hp)",
    crew_chief: "ARJUN KAPOOR", race_engineer: "PRIYA NAIR",
    sponsors: { title: "TATA CONSULTANCY", primary: ["JK TYRE", "MAHINDRA", "RELIANCE"], support: ["INDIAN OIL", "BHARAT PETROLEUM", "MICHELIN", "SPARCO", "OMP", "GODREJ"] },
    bio: "Bangalore-born. Came up through MRF Challenge and Asian F3. First Indian woman in APEX. Engineering background — owns three patents on cooling-duct geometry.",
    helmet: { primary: "#5C1F4D", secondary: "#D4AF37", visor: "#0B0B0B" },
    crew_chief_bio: "20 years across MRF, Mahindra Racing, and Formula E. Brought a six-person pit crew from Mahindra.",
    race_engineer_bio: "IIT Bombay. Tyre-degradation modelling specialist. Known for precise long-stint calls.",
    last_lap: "1:11.014", best_lap: "1:10.502", gap: "+7.214",
    tire: "H", tire_age: 42, fuel: 43.5,
    sectors: [{ s: "S1", t: "22.018", state: "yellow" }, { s: "S2", t: "27.918", state: "yellow" }, { s: "S3", t: "22.288", state: "yellow" }],
  },
];

// Telemetry trace points — smooth lap pattern, monotonic in i.
function generateTelemetry(seed) {
  const out = [];
  // Corner sample positions around a 100-sample lap (rough). At each, brake high, throttle low.
  const corners = [12, 22, 32, 42, 52, 62, 72, 82, 92];
  for (let i = 0; i < 100; i++) {
    let nearestDist = 999;
    for (const c of corners) {
      const d = Math.abs(i - c);
      if (d < nearestDist) nearestDist = d;
    }
    let throttle, brake;
    if (nearestDist <= 1) { brake = 95; throttle = 5; }
    else if (nearestDist <= 3) { brake = 60 - (nearestDist - 1) * 25; throttle = 20 + (nearestDist - 1) * 10; }
    else if (nearestDist <= 5) { brake = 0; throttle = 55 + (nearestDist - 3) * 18; }
    else { brake = 0; throttle = 100; }
    // gentle jitter only — no flipping
    const j = Math.sin(i * 0.7 + seed * 0.3) * 1.5;
    throttle = Math.max(0, Math.min(100, throttle + j));
    brake = Math.max(0, Math.min(100, brake + j * 0.4));
    // speed correlated with throttle/brake state
    const speed = brake > 30 ? 110 + (100 - brake) * 1.4 : 200 + throttle * 1.05;
    out.push({ i, throttle, brake, speed: Math.max(80, Math.min(310, speed)) });
  }
  return out;
}

// Pit stop log — last few stops in the race
const PIT_LOG = [
  { lap: 38, no: 7, team: "ARC", duration: 2.41, action: "S→S", notes: "CLEAN. UNDERCUT MORENO." },
  { lap: 39, no: 22, team: "VLM", duration: 2.62, action: "S→S", notes: "WHEELGUN HESITATION L/R." },
  { lap: 41, no: 11, team: "MRD", duration: 2.38, action: "S→M", notes: "STRATEGY: ONE-STOP PIVOT." },
  { lap: 42, no: 4, team: "KIN", duration: 2.51, action: "S→M", notes: "FRONT WING ADJ." },
  { lap: 44, no: 19, team: "ALB", duration: 2.48, action: "S→M", notes: "" },
  { lap: 45, no: 33, team: "EBL", duration: 2.44, action: "S→M", notes: "" },
];

// Sector heat map data — fastest car per sector per lap (for the strip chart)
const SECTOR_LEADERS = {
  S1: { car: 7, time: "21.104", driver: "VOSS" },
  S2: { car: 11, time: "26.842", driver: "ADEYEMI" },
  S3: { car: 22, time: "21.669", driver: "MORENO" },
};

const RACE_STATE = {
  lap: 47,
  laps_remaining: 48,
  flag: "GREEN",
  leader_lap_time: "1:09.214",
  fastest_lap: { car: 7, driver: "VOSS", time: "1:09.214", lap: 31 },
  next_pit_window: "L52-L58",
};

window.APEX_DATA = { RACE_META, TURNS, CARS, PIT_LOG, SECTOR_LEADERS, RACE_STATE, generateTelemetry };
