// Hero centerpiece variants for the F1 StratLab landing page

const { useState, useEffect, useRef } = React;

// ==============================================================
// VARIANT A — System architecture diagram (static, informative)
// A clean one-glance map of the whole pipeline: inputs → 6 agents
// → orchestrator → strategy output. No animation, no car — just
// the system laid out.
// ==============================================================
function AgentGraphViz() {
  const W = 1120, H = 520;

  // Three columns: inputs, agents, output
  const inputs = [
    { id: 'telem',   label: 'Telemetry',     sub: '14 MB/s · 1.2M pts',    y: 88 },
    { id: 'tires',   label: 'Tire model',    sub: '8 compounds · wear',    y: 158 },
    { id: 'radio',   label: 'Team radio',    sub: 'Whisper · NLP',         y: 228 },
    { id: 'race',    label: 'Race control',  sub: 'Flags · pit · SC',      y: 298 },
    { id: 'weather', label: 'Weather',       sub: 'Track · rain prob',     y: 368 },
    { id: 'rules',   label: 'FIA rules',     sub: 'RAG · regulations',     y: 438 },
  ];

  const agents = [
    { id: 'pace',  label: 'Pace',            out: 'Lap time prediction',     color: '#a29bfe', y: 108 },
    { id: 'tire',  label: 'Tire degradation', out: 'Stint length limit',     color: '#ff5a3f', y: 178 },
    { id: 'race',  label: 'Race strategy',   out: 'Optimal pit window',      color: '#ffbd33', y: 248 },
    { id: 'pit',   label: 'Pit call',        out: 'Box / stay recommendation', color: '#3385ff', y: 318 },
    { id: 'radio', label: 'Radio intent',    out: 'Driver state · request',  color: '#43d67a', y: 388 },
    { id: 'rules', label: 'Rules lookup',    out: 'Regulation check · RAG',  color: '#b58cff', y: 458 },
  ];

  const inputX = 20;
  const inputW = 190;
  const agentX = 370;
  const agentW = 320;
  const orchX = 830;
  const orchW = 270;

  return (
    <div style={{ position: 'relative', borderRadius: 16, overflow: 'hidden',
                  background: 'linear-gradient(180deg, #0a0b13 0%, #0c0d1a 50%, #10111f 100%)',
                  border: '1px solid rgba(255,255,255,0.08)',
                  boxShadow: '0 20px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(108,92,231,0.15)' }}>
        <svg viewBox={`0 0 ${W} ${H}`} style={{ width: '100%', height: 'auto', display: 'block' }}>
          <defs>
            <pattern id="archGrid" width="60" height="60" patternUnits="userSpaceOnUse">
              <path d="M 60 0 L 0 0 0 60" fill="none" stroke="rgba(255,255,255,0.03)" strokeWidth="1"/>
            </pattern>
            <linearGradient id="archInLine" x1="0" x2="1">
              <stop offset="0%" stopColor="rgba(255,255,255,0.08)"/>
              <stop offset="100%" stopColor="rgba(162,155,254,0.5)"/>
            </linearGradient>
            <linearGradient id="archOutLine" x1="0" x2="1">
              <stop offset="0%" stopColor="rgba(162,155,254,0.5)"/>
              <stop offset="100%" stopColor="rgba(67,214,122,0.7)"/>
            </linearGradient>
            <radialGradient id="archCore">
              <stop offset="0%" stopColor="rgba(162,155,254,0.35)"/>
              <stop offset="70%" stopColor="rgba(108,92,231,0.05)"/>
              <stop offset="100%" stopColor="rgba(108,92,231,0)"/>
            </radialGradient>
            <filter id="archGlow" x="-40%" y="-40%" width="180%" height="180%">
              <feGaussianBlur stdDeviation="6"/>
            </filter>
          </defs>

          {/* grid */}
          <rect width={W} height={H} fill="url(#archGrid)"/>

          {/* column headers */}
          <text x={inputX + inputW/2} y="42" textAnchor="middle"
                fontFamily="var(--font-mono)" fontSize="10"
                fill="rgba(255,255,255,0.45)" letterSpacing="0.2em">INPUTS</text>
          <text x={agentX + agentW/2} y="42" textAnchor="middle"
                fontFamily="var(--font-mono)" fontSize="10"
                fill="rgba(255,255,255,0.45)" letterSpacing="0.2em">SPECIALIST AGENTS · LANGGRAPH</text>
          <text x={orchX + orchW/2} y="42" textAnchor="middle"
                fontFamily="var(--font-mono)" fontSize="10"
                fill="rgba(255,255,255,0.45)" letterSpacing="0.2em">DECISION</text>

          {/* hairline separators under headers */}
          <line x1={inputX} y1="54" x2={inputX + inputW} y2="54"
                stroke="rgba(255,255,255,0.12)" strokeWidth="0.8"/>
          <line x1={agentX} y1="54" x2={agentX + agentW} y2="54"
                stroke="rgba(255,255,255,0.12)" strokeWidth="0.8"/>
          <line x1={orchX} y1="54" x2={orchX + orchW} y2="54"
                stroke="rgba(255,255,255,0.12)" strokeWidth="0.8"/>

          {/* halo behind orchestrator */}
          <ellipse cx={orchX + orchW/2} cy={H/2 + 10} rx="200" ry="200" fill="url(#archCore)"/>

          {/* Input → Agent wires */}
          {inputs.map((inp, i) => {
            const a = agents[i];
            const x1 = inputX + inputW;
            const y1 = inp.y + 24;
            const x2 = agentX;
            const y2 = a.y + 26;
            const mx = (x1 + x2) / 2;
            return (
              <g key={inp.id}>
                <path d={`M ${x1} ${y1} C ${mx} ${y1}, ${mx} ${y2}, ${x2} ${y2}`}
                      fill="none" stroke="url(#archInLine)" strokeWidth="1"/>
                <circle cx={x2} cy={y2} r="2" fill={a.color}/>
              </g>
            );
          })}

          {/* Agent → Orchestrator wires */}
          {agents.map((a) => {
            const x1 = agentX + agentW;
            const y1 = a.y + 26;
            const x2 = orchX + 12;
            const y2 = H/2 + 10;
            const mx = (x1 + x2) / 2;
            return (
              <g key={a.id}>
                <path d={`M ${x1} ${y1} C ${mx} ${y1}, ${mx} ${y2}, ${x2} ${y2}`}
                      fill="none" stroke={a.color} strokeOpacity="0.4" strokeWidth="1"/>
                <circle cx={x1} cy={y1} r="2.5" fill={a.color}/>
              </g>
            );
          })}

          {/* INPUT boxes */}
          {inputs.map((inp) => (
            <g key={inp.id}>
              <rect x={inputX} y={inp.y} width={inputW} height="48" rx="7"
                    fill="rgba(15,16,32,0.7)"
                    stroke="rgba(255,255,255,0.1)" strokeWidth="0.8"/>
              <rect x={inputX} y={inp.y} width="3" height="48" rx="1.5"
                    fill="rgba(255,255,255,0.2)"/>
              <text x={inputX + 14} y={inp.y + 21} fontFamily="var(--font-display)"
                    fontSize="13" fill="#fff" fontWeight="500">{inp.label}</text>
              <text x={inputX + 14} y={inp.y + 37} fontFamily="var(--font-mono)"
                    fontSize="9.5" fill="rgba(255,255,255,0.5)" letterSpacing="0.04em">{inp.sub}</text>
            </g>
          ))}

          {/* AGENT boxes */}
          {agents.map((a) => (
            <g key={a.id}>
              <rect x={agentX} y={a.y} width={agentW} height="54" rx="8"
                    fill="rgba(15,16,32,0.85)"
                    stroke={a.color} strokeOpacity="0.4" strokeWidth="1"
                    style={{ filter: `drop-shadow(0 0 12px ${a.color}22)` }}/>
              <circle cx={agentX + 16} cy={a.y + 27} r="5" fill={a.color}
                      style={{ filter: `drop-shadow(0 0 4px ${a.color})` }}/>
              <text x={agentX + 32} y={a.y + 22} fontFamily="var(--font-display)"
                    fontSize="14" fill="#fff" fontWeight="500">{a.label}</text>
              <text x={agentX + 32} y={a.y + 40} fontFamily="var(--font-mono)"
                    fontSize="10" fill="rgba(255,255,255,0.55)" letterSpacing="0.02em">→ {a.out}</text>
              {/* structured output pill */}
              <rect x={agentX + agentW - 78} y={a.y + 15} width="64" height="16" rx="8"
                    fill="rgba(162,155,254,0.1)" stroke="rgba(162,155,254,0.35)" strokeWidth="0.6"/>
              <text x={agentX + agentW - 46} y={a.y + 26} textAnchor="middle"
                    fontFamily="var(--font-mono)" fontSize="8"
                    fill="#a29bfe" letterSpacing="0.1em">PYDANTIC</text>
              <text x={agentX + agentW - 46} y={a.y + 42} textAnchor="middle"
                    fontFamily="var(--font-mono)" fontSize="8.5"
                    fill="rgba(255,255,255,0.4)" letterSpacing="0.06em">conf 0.{80 + ((a.label.length * 3) % 18)}</text>
            </g>
          ))}

          {/* ORCHESTRATOR box — the big decision */}
          <g>
            <rect x={orchX} y={H/2 - 90} width={orchW} height="200" rx="12"
                  fill="rgba(108,92,231,0.08)"
                  stroke="rgba(162,155,254,0.55)" strokeWidth="1.2"
                  style={{ filter: 'drop-shadow(0 0 24px rgba(108,92,231,0.25))' }}/>
            <rect x={orchX} y={H/2 - 90} width="4" height="200" rx="2" fill="#a29bfe"/>
            <text x={orchX + 20} y={H/2 - 68} fontFamily="var(--font-mono)" fontSize="10"
                  fill="#a29bfe" letterSpacing="0.18em">ORCHESTRATOR</text>
            <text x={orchX + 20} y={H/2 - 46} fontFamily="var(--font-mono)" fontSize="9"
                  fill="rgba(255,255,255,0.5)" letterSpacing="0.1em">SUPERVISOR AGENT</text>

            <line x1={orchX + 20} y1={H/2 - 32} x2={orchX + orchW - 20} y2={H/2 - 32}
                  stroke="rgba(255,255,255,0.12)" strokeWidth="0.8"/>

            <text x={orchX + 20} y={H/2 - 10} fontFamily="var(--font-mono)" fontSize="9"
                  fill="rgba(255,255,255,0.5)" letterSpacing="0.14em">RECOMMENDATION</text>
            <text x={orchX + 20} y={H/2 + 20} fontFamily="var(--font-display)" fontSize="26"
                  fill="#fff" fontWeight="500" letterSpacing="-0.01em">PIT_NOW · MED</text>
            <text x={orchX + 20} y={H/2 + 42} fontFamily="var(--font-mono)" fontSize="10"
                  fill="rgba(255,255,255,0.55)" letterSpacing="0.06em">Undercut window · laps 34–36</text>

            {/* confidence bar */}
            <text x={orchX + 20} y={H/2 + 66} fontFamily="var(--font-mono)" fontSize="9"
                  fill="rgba(255,255,255,0.45)" letterSpacing="0.1em">CONFIDENCE</text>
            <text x={orchX + orchW - 20} y={H/2 + 66} textAnchor="end"
                  fontFamily="var(--font-mono)" fontSize="9"
                  fill="#43d67a" letterSpacing="0.04em">0.87</text>
            <rect x={orchX + 20} y={H/2 + 74} width={orchW - 40} height="4" rx="2"
                  fill="rgba(255,255,255,0.08)"/>
            <rect x={orchX + 20} y={H/2 + 74} width={(orchW - 40) * 0.87} height="4" rx="2"
                  fill="#43d67a"/>
            <text x={orchX + 20} y={H/2 + 94} fontFamily="var(--font-mono)" fontSize="8.5"
                  fill="rgba(255,255,255,0.4)" letterSpacing="0.08em">p(win) +3.2% vs stay-out</text>
          </g>

          {/* Final output arrow — from orchestrator to "STRATEGY" label */}
          <path d={`M ${orchX + orchW} ${H/2 + 10} L ${W - 8} ${H/2 + 10}`}
                stroke="url(#archOutLine)" strokeWidth="1.4" fill="none"/>
          <path d={`M ${W - 16} ${H/2 + 4} L ${W - 8} ${H/2 + 10} L ${W - 16} ${H/2 + 16}`}
                fill="none" stroke="#43d67a" strokeWidth="1.4"/>
        </svg>

      {/* Header chrome */}
      <div className="arch-chrome arch-chrome-left" style={{
        position: 'absolute', top: 14, left: 18,
        fontFamily: 'var(--font-mono)', fontSize: 10,
        color: 'rgba(255,255,255,0.45)', letterSpacing: '0.16em', textTransform: 'uppercase'
      }}>
        system_architecture · f1 strategy manager
      </div>
      <div className="arch-chrome arch-chrome-right" style={{
        position: 'absolute', top: 14, right: 18,
        display: 'flex', gap: 14, alignItems: 'center',
        fontFamily: 'var(--font-mono)', fontSize: 11
      }}>
        <span style={{ color: 'rgba(255,255,255,0.55)' }}>6 agents</span>
        <span style={{ color: 'rgba(255,255,255,0.25)' }}>·</span>
        <span style={{ color: 'rgba(255,255,255,0.55)' }}>7 ML models</span>
        <span style={{ color: 'rgba(255,255,255,0.25)' }}>·</span>
        <span style={{ color: '#43d67a' }}>&lt; 1 s end-to-end</span>
      </div>
      <style>{`
        @media (max-width: 760px) {
          .arch-chrome-left { display: none; }
          .arch-chrome-right { gap: 8px !important; font-size: 10px !important; }
          .hero-viz {
            overflow-x: auto;
            -webkit-overflow-scrolling: touch;
            scrollbar-width: thin;
            scrollbar-color: rgba(108,92,231,0.45) transparent;
            padding-bottom: 8px;
            position: relative;
          }
          .hero-viz > div { min-width: 720px; }
          .hero-viz::-webkit-scrollbar { height: 5px; }
          .hero-viz::-webkit-scrollbar-track { background: transparent; }
          .hero-viz::-webkit-scrollbar-thumb {
            background: rgba(108,92,231,0.5);
            border-radius: 3px;
          }
          .hero-viz::after {
            content: '→';
            position: absolute;
            right: 14px;
            bottom: 18px;
            width: 32px;
            height: 32px;
            line-height: 32px;
            text-align: center;
            font-family: var(--font-display);
            font-size: 16px;
            font-weight: 700;
            color: #fff;
            background: rgba(108,92,231,0.78);
            border-radius: 50%;
            box-shadow: 0 0 18px rgba(108,92,231,0.55);
            pointer-events: none;
            z-index: 5;
            animation:
              heroVizHintNudge 1.4s ease-in-out 2 both,
              heroVizHintFade 0.55s ease 3s forwards;
          }
        }
        @media (max-width: 480px) {
          .arch-chrome-right { gap: 6px !important; font-size: 9px !important; }
        }
        @keyframes heroVizHintNudge {
          0%, 100% { transform: translateX(0); }
          50% { transform: translateX(7px); }
        }
        @keyframes heroVizHintFade {
          to { opacity: 0; transform: translateX(0) scale(0.75); }
        }
        @media (prefers-reduced-motion: reduce) {
          .hero-viz::after { animation: heroVizHintFade 0.3s ease 1.2s forwards; }
        }
      `}</style>
    </div>
  );
}

// ==============================================================
// VARIANT B — Live dashboard mockup (ticking lap chart + rec)
// ==============================================================
function DashboardViz() {
  const [lap, setLap] = useState(34);
  useEffect(() => {
    const i = setInterval(() => setLap(l => (l >= 48 ? 34 : l + 1)), 900);
    return () => clearInterval(i);
  }, []);

  // synthetic lap time data with a stint change at lap 22 and 38
  const laps = Array.from({ length: lap }, (_, i) => {
    const n = i + 1;
    // base + tire age effect
    let t = 87.2;
    if (n <= 21) t += (n - 1) * 0.08;
    else if (n <= 37) t += 0.3 + (n - 22) * 0.07;
    else t += 0.2 + (n - 38) * 0.05;
    t += Math.sin(n * 0.7) * 0.15 + (i === lap - 1 ? 0 : 0);
    return { lap: n, time: t, compound: n <= 21 ? 'soft' : n <= 37 ? 'medium' : 'hard' };
  });

  const W = 1120, H = 520;
  // layout: 3 columns
  return (
    <div style={{
      background: 'linear-gradient(180deg, #0c0d14 0%, #111827 100%)',
      border: '1px solid rgba(255,255,255,0.1)',
      borderRadius: 16,
      overflow: 'hidden',
      boxShadow: '0 20px 60px rgba(0,0,0,0.5), 0 0 0 1px rgba(108,92,231,0.2)'
    }}>
      {/* chrome */}
      <div style={{
        display: 'flex', alignItems: 'center', gap: 10,
        padding: '12px 16px', borderBottom: '1px solid rgba(255,255,255,0.08)',
        background: 'rgba(255,255,255,0.02)'
      }}>
        <div style={{ display: 'flex', gap: 6 }}>
          <span style={{ width: 11, height: 11, borderRadius: '50%', background: 'rgba(255,255,255,0.14)' }}/>
          <span style={{ width: 11, height: 11, borderRadius: '50%', background: 'rgba(255,255,255,0.14)' }}/>
          <span style={{ width: 11, height: 11, borderRadius: '50%', background: 'rgba(255,255,255,0.14)' }}/>
        </div>
        <div style={{
          marginLeft: 12, fontFamily: 'var(--font-mono)', fontSize: 12,
          color: 'rgba(255,255,255,0.5)'
        }}>f1-strat · Spain GP 2023 · #16 Leclerc</div>
        <div style={{ marginLeft: 'auto', display: 'flex', alignItems: 'center', gap: 8 }}>
          <span style={{
            width: 6, height: 6, borderRadius: '50%',
            background: '#43ff64', boxShadow: '0 0 8px #43ff64',
            animation: 'pulse-dot 1.8s infinite'
          }}/>
          <span style={{ fontFamily: 'var(--font-mono)', fontSize: 11, color: '#43ff64' }}>live</span>
        </div>
      </div>

      <style>{`
        .dash-viz-grid {
          display: grid;
          grid-template-columns: 1fr 320px;
          gap: 0;
        }
        .dash-viz-chart {
          padding: 24px;
          border-right: 1px solid rgba(255,255,255,0.06);
        }
        @media (max-width: 780px) {
          .dash-viz-grid {
            grid-template-columns: 1fr;
          }
          .dash-viz-chart {
            border-right: none;
            border-bottom: 1px solid rgba(255,255,255,0.06);
          }
        }
      `}</style>
      <div className="dash-viz-grid">
        {/* chart */}
        <div className="dash-viz-chart">
          <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'baseline', marginBottom: 16 }}>
            <div>
              <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.4)', letterSpacing: '0.12em', textTransform: 'uppercase', fontWeight: 600 }}>Lap time evolution</div>
              <div style={{ fontFamily: 'var(--font-display)', fontSize: 24, fontWeight: 600, letterSpacing: '-0.02em', marginTop: 4 }}>
                {laps[laps.length - 1]?.time.toFixed(3)}<span style={{ fontSize: 14, color: 'rgba(255,255,255,0.5)', marginLeft: 4 }}>s</span>
              </div>
            </div>
            <div style={{ display: 'flex', gap: 12 }}>
              <div style={{ textAlign: 'right' }}>
                <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.4)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>Lap</div>
                <div style={{ fontFamily: 'var(--font-mono)', fontSize: 20, color: '#fff' }}>{lap}<span style={{ color: 'rgba(255,255,255,0.4)' }}>/66</span></div>
              </div>
            </div>
          </div>
          <LapChart laps={laps} />
          <div style={{ display: 'flex', gap: 18, marginTop: 16, fontSize: 11, fontFamily: 'var(--font-mono)', color: 'rgba(255,255,255,0.6)' }}>
            <span><span style={{ display: 'inline-block', width: 10, height: 10, borderRadius: '50%', background: '#ff2d3a', marginRight: 6, verticalAlign: 'middle' }}/>SOFT · stint 1</span>
            <span><span style={{ display: 'inline-block', width: 10, height: 10, borderRadius: '50%', background: '#ffcf2d', marginRight: 6, verticalAlign: 'middle' }}/>MEDIUM · stint 2</span>
            <span><span style={{ display: 'inline-block', width: 10, height: 10, borderRadius: '50%', background: '#e6e6e6', marginRight: 6, verticalAlign: 'middle' }}/>HARD · stint 3</span>
          </div>
        </div>

        {/* right rail: live recommendation */}
        <div style={{ padding: 20, display: 'flex', flexDirection: 'column', gap: 14 }}>
          <div>
            <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.4)', letterSpacing: '0.14em', textTransform: 'uppercase', fontWeight: 600, marginBottom: 8 }}>Orchestrator · next lap</div>
            <div style={{
              border: '1px solid rgba(108,92,231,0.4)',
              borderLeft: '3px solid #6c5ce7',
              borderRadius: 10,
              padding: 14,
              background: 'linear-gradient(135deg, rgba(108,92,231,0.15) 0%, rgba(108,92,231,0.04) 100%)'
            }}>
              <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 6 }}>
                <span style={{ fontFamily: 'var(--font-mono)', fontSize: 10, color: '#a29bfe', letterSpacing: '0.08em' }}>PERFORM_UNDERCUT</span>
                <span style={{ fontFamily: 'var(--font-mono)', fontSize: 10, color: 'rgba(255,255,255,0.5)' }}>conf 0.87</span>
              </div>
              <div style={{ fontFamily: 'var(--font-display)', fontSize: 15, fontWeight: 500, lineHeight: 1.35, marginBottom: 6 }}>
                Pit lap 35 · target +1 position vs SAI
              </div>
              <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.55)', lineHeight: 1.5 }}>
                Medium @ 14 laps. Gap 1.8s, predicted pit delta +19.3s. Clean air on out-lap.
              </div>
            </div>
          </div>

          <MiniStat label="Degradation" value="0.082" unit="s/lap" trend="up"/>
          <MiniStat label="P(safety car)" value="0.18" unit="" trend="flat"/>
          <MiniStat label="Undercut win" value="71%" unit="" trend="up"/>

          <div style={{ marginTop: 'auto' }}>
            <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.4)', letterSpacing: '0.14em', textTransform: 'uppercase', fontWeight: 600, marginBottom: 8 }}>Radio · last 3 laps</div>
            <RadioMessage speaker="LEC" text="Tyres are gone, mate." sentiment="negative" lap={33}/>
            <RadioMessage speaker="BOX" text="Box this lap, box." sentiment="neutral" lap={34}/>
          </div>
        </div>
      </div>
    </div>
  );
}

function LapChart({ laps }) {
  const W = 680, H = 220;
  const pad = { l: 36, r: 16, t: 10, b: 26 };
  const min = 86.5, max = 90.5;
  const xs = (i) => pad.l + (i / 65) * (W - pad.l - pad.r);
  const ys = (t) => pad.t + (1 - (t - min) / (max - min)) * (H - pad.t - pad.b);

  const compoundColor = { soft: '#ff2d3a', medium: '#ffcf2d', hard: '#e6e6e6' };
  // build segments by compound
  const segs = [];
  let cur = null;
  laps.forEach(l => {
    if (!cur || cur.compound !== l.compound) {
      cur = { compound: l.compound, points: [] };
      segs.push(cur);
    }
    cur.points.push(l);
  });

  return (
    <svg viewBox={`0 0 ${W} ${H}`} style={{ width: '100%', height: 220, display: 'block' }}>
      {/* gridlines */}
      {[0, 0.25, 0.5, 0.75, 1].map((p, i) => (
        <line key={i} x1={pad.l} x2={W - pad.r}
              y1={pad.t + p * (H - pad.t - pad.b)} y2={pad.t + p * (H - pad.t - pad.b)}
              stroke="rgba(255,255,255,0.05)" strokeWidth="1"/>
      ))}
      {/* y labels */}
      {[86.5, 88, 89.5].map((v, i) => (
        <text key={i} x={pad.l - 8} y={ys(v) + 3} textAnchor="end"
              fontFamily="var(--font-mono)" fontSize="10" fill="rgba(255,255,255,0.35)">{v.toFixed(1)}</text>
      ))}
      {/* x labels */}
      {[1, 20, 40, 60].map(l => (
        <text key={l} x={xs(l - 1)} y={H - 8} textAnchor="middle"
              fontFamily="var(--font-mono)" fontSize="10" fill="rgba(255,255,255,0.35)">{l}</text>
      ))}
      {/* stint divider vertical lines */}
      {[21, 37].map(lap => laps.length >= lap && (
        <line key={lap} x1={xs(lap - 1)} x2={xs(lap - 1)} y1={pad.t} y2={H - pad.b}
              stroke="rgba(255,255,255,0.18)" strokeDasharray="3 3"/>
      ))}
      {/* data lines per segment */}
      {segs.map((s, i) => {
        if (s.points.length < 2) return null;
        const d = s.points.map((p, j) => {
          const x = xs(p.lap - 1), y = ys(p.time);
          return `${j === 0 ? 'M' : 'L'}${x},${y}`;
        }).join(' ');
        return (
          <path key={i} d={d} fill="none" stroke={compoundColor[s.compound]} strokeWidth="2" strokeLinecap="round"/>
        );
      })}
      {/* latest dot */}
      {laps.length > 0 && (
        <g>
          <circle cx={xs(laps[laps.length - 1].lap - 1)} cy={ys(laps[laps.length - 1].time)}
                  r="5" fill="#fff" stroke={compoundColor[laps[laps.length - 1].compound]} strokeWidth="2"/>
        </g>
      )}
    </svg>
  );
}

function MiniStat({ label, value, unit, trend }) {
  const arrow = trend === 'up' ? '▲' : trend === 'down' ? '▼' : '─';
  const color = trend === 'up' ? '#ff5733' : trend === 'down' ? '#43ff64' : 'rgba(255,255,255,0.4)';
  return (
    <div style={{
      display: 'flex', justifyContent: 'space-between', alignItems: 'center',
      padding: '10px 12px',
      background: 'rgba(255,255,255,0.02)',
      border: '1px solid rgba(255,255,255,0.06)',
      borderRadius: 8
    }}>
      <span style={{ fontSize: 12, color: 'rgba(255,255,255,0.6)' }}>{label}</span>
      <span style={{ display: 'flex', alignItems: 'baseline', gap: 6 }}>
        <span style={{ fontFamily: 'var(--font-mono)', fontSize: 14, color: '#fff' }}>{value}{unit}</span>
        <span style={{ fontSize: 10, color }}>{arrow}</span>
      </span>
    </div>
  );
}

function RadioMessage({ speaker, text, sentiment, lap }) {
  const color = sentiment === 'negative' ? '#ff5733' : sentiment === 'positive' ? '#43ff64' : '#a29bfe';
  return (
    <div style={{
      padding: '8px 10px',
      marginBottom: 6,
      background: 'rgba(255,255,255,0.02)',
      borderLeft: `2px solid ${color}`,
      borderRadius: '0 6px 6px 0',
      fontSize: 12
    }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', color: 'rgba(255,255,255,0.5)', fontFamily: 'var(--font-mono)', fontSize: 10, marginBottom: 2 }}>
        <span>L{lap} · {speaker}</span><span style={{ color }}>{sentiment}</span>
      </div>
      <div style={{ color: 'rgba(255,255,255,0.85)', fontStyle: 'italic' }}>"{text}"</div>
    </div>
  );
}

// ==============================================================
// VARIANT C — Cinematic (banner image + overlay)
// ==============================================================
function CinematicViz() {
  return (
    <div style={{
      position: 'relative',
      borderRadius: 20,
      overflow: 'hidden',
      border: '1px solid rgba(255,255,255,0.1)',
      boxShadow: '0 30px 80px rgba(0,0,0,0.6)',
      aspectRatio: '21 / 9',
      background: '#071022'
    }}>
      <img src="assets/banner.jpeg" alt="F1 StratLab"
           style={{ width: '100%', height: '100%', objectFit: 'cover', display: 'block', opacity: 0.9 }}/>
      <div style={{
        position: 'absolute', inset: 0,
        background: 'linear-gradient(180deg, rgba(8,8,12,0) 30%, rgba(8,8,12,0.6) 80%, rgba(8,8,12,0.95) 100%), radial-gradient(ellipse at 30% 60%, rgba(108,92,231,0.25) 0%, transparent 60%)'
      }}/>
      <style>{`
        .cine-viz-row {
          position: absolute;
          bottom: 0;
          left: 0;
          right: 0;
          padding: 32px 40px;
          display: flex;
          flex-wrap: wrap;
          justify-content: space-between;
          align-items: flex-end;
          gap: 40px;
        }
        .cine-viz-stats {
          display: grid;
          grid-template-columns: repeat(4, auto);
          gap: 40px;
        }
        .cine-viz-replay {
          font-family: var(--font-mono);
          font-size: 11px;
          color: rgba(255,255,255,0.5);
          text-align: right;
        }
        @media (max-width: 640px) {
          .cine-viz-row {
            padding: 20px 20px;
            gap: 16px;
          }
          .cine-viz-stats {
            grid-template-columns: repeat(2, auto);
            gap: 20px;
          }
          .cine-viz-replay {
            text-align: left;
          }
        }
      `}</style>
      <div className="cine-viz-row">
        <div className="cine-viz-stats">
          <InlineStat k="7" label="ML models"/>
          <InlineStat k="6" label="Sub-agents"/>
          <InlineStat k="71" label="Grand Prix"/>
          <InlineStat k="~15 GB" label="First run"/>
        </div>
        <div className="cine-viz-replay">
          <div style={{ color: '#43ff64' }}>● replay · 2023 · Spain</div>
          <div style={{ marginTop: 4 }}>lap 34 / 66 · SC probability 0.18</div>
        </div>
      </div>
    </div>
  );
}

function InlineStat({ k, label }) {
  return (
    <div>
      <div style={{ fontFamily: 'var(--font-display)', fontSize: 32, fontWeight: 600, letterSpacing: '-0.02em', color: '#fff' }}>{k}</div>
      <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.55)', letterSpacing: '0.14em', textTransform: 'uppercase', marginTop: 2 }}>{label}</div>
    </div>
  );
}

Object.assign(window, { AgentGraphViz, DashboardViz, CinematicViz });
