/* global React, ReactDOM */
const { useState, useEffect, useRef, useMemo } = React;

// ============= i18n labels for SVG/animations =============
const VIZ_LABELS = {
  en: {
    driver: "DRIVER",
    speed: "SPEED",
    rec: "● REC",
    cam: "CAM-04 · CABIN",
    eyes: "EYES · OPEN",
    yaw: "YAW · 4°",
    attn: "ATTN · 92%",
    face: "FACE · 0.97",
    ped: "PED",
    hazard: "HAZARD",
    hub: "HUB · MVD",
    hubMvd04: "HUB · MVD-04",
    monitorTitle: "DRIVER MONITORING · 3 ACTIVE",
    eventsTitle: "AI · EVENTS FEED",
    events: [
      { t: "14:47:02", k: "DETECT", label: "Pedestrian crossing · vehicle 04", tone: "ok" },
      { t: "14:39:11", k: "ALERT", label: "Hard brake · vehicle 09", tone: "warn" },
      { t: "14:31:47", k: "OPTIMIZE", label: "Re-routed 12 stops · −38% km", tone: "ok" },
      { t: "14:26:22", k: "CAMERA", label: "POD captured · stop #84", tone: "ok" },
      { t: "14:17:58", k: "FATIGUE", label: "Suggest break · driver 07", tone: "warn" },
    ],
    heroEvents: [
      { t: "0:42", k: "DETECT", label: "Stop sign · driver compliant", tone: "ok" },
      { t: "0:38", k: "RE-PLAN", label: "Traffic +6 min · re-routed 4 stops", tone: "alert" },
      { t: "0:31", k: "CAMERA", label: "Cargo door · sealed", tone: "ok" },
    ],
    daiAttn: "ATTN",
    daiDriver: "DRIVER · 0.96",
    daiScore: "SCORE",
    daiBars: ["BRK", "COR", "SPD", "FOL"],
    daiPdf: "WEEKLY · PDF",
    daiHardBrk: "HARD-BRK",
    daiTimeline: "TIMELINE",
    fatigueLabel: "FATIGUE",
    fatigueLow: "LOW",
    aiEvents: "AI · EVENTS",
  },
  es: {
    driver: "CHOFER",
    speed: "VEL",
    rec: "● REC",
    cam: "CAM-04 · CABINA",
    eyes: "OJOS · ABIERTOS",
    yaw: "CABEZA · 4°",
    attn: "ATEN · 92%",
    face: "ROSTRO · 0,97",
    ped: "PEATÓN",
    hazard: "RIESGO",
    hub: "BASE · MVD",
    hubMvd04: "BASE · MVD-04",
    monitorTitle: "CHOFERES · 3 ACTIVOS",
    eventsTitle: "IA · FEED DE EVENTOS",
    events: [
      { t: "14:47:02", k: "DETECTA", label: "Peatón cruzando · vehículo 04", tone: "ok" },
      { t: "14:39:11", k: "ALERTA", label: "Frenada brusca · vehículo 09", tone: "warn" },
      { t: "14:31:47", k: "OPTIMIZA", label: "Re-ruteó 12 paradas · −38% km", tone: "ok" },
      { t: "14:26:22", k: "CÁMARA", label: "Prueba de entrega · parada #84", tone: "ok" },
      { t: "14:17:58", k: "FATIGA", label: "Sugiere descanso · chofer 07", tone: "warn" },
    ],
    heroEvents: [
      { t: "0:42", k: "DETECTA", label: "Pare · chofer cumple", tone: "ok" },
      { t: "0:38", k: "RE-PLAN", label: "Tráfico +6 min · 4 paradas re-ruteadas", tone: "alert" },
      { t: "0:31", k: "CÁMARA", label: "Puerta de carga · sellada", tone: "ok" },
    ],
    daiAttn: "ATEN",
    daiDriver: "CHOFER · 0,96",
    daiScore: "SCORE",
    daiBars: ["FRN", "CUR", "VEL", "DST"],
    daiPdf: "SEMANAL · PDF",
    daiHardBrk: "FRENADA",
    daiTimeline: "LÍNEA TIEMPO",
    fatigueLabel: "FATIGA",
    fatigueLow: "BAJA",
    aiEvents: "IA · EVENTOS",
  },
};
const getLabels = (lang) => VIZ_LABELS[lang] || VIZ_LABELS.en;

// ============= Brand Mark =============
const BrandMark = () => (
  <svg viewBox="0 0 32 32" fill="none">
    <circle cx="16" cy="16" r="15" stroke="currentColor" strokeWidth="1.5" />
    <path d="M5 20 Q 12 8, 18 14 T 28 12" stroke="var(--accent)" strokeWidth="2" strokeLinecap="round" fill="none" />
    <circle cx="5" cy="20" r="2" fill="var(--accent)" />
    <circle cx="28" cy="12" r="2" fill="currentColor" />
  </svg>
);

// ============= Marquee Icons =============
const MarqueeIcon = ({ name }) => {
  const common = { width: 20, height: 20, viewBox: "0 0 24 24", fill: "none", stroke: "currentColor", strokeWidth: 1.8, strokeLinecap: "round", strokeLinejoin: "round" };
  switch (name) {
    case "sparkle":
      return (<svg {...common}><path d="M12 3v6M12 15v6M3 12h6M15 12h6M5.6 5.6l4.2 4.2M14.2 14.2l4.2 4.2M5.6 18.4l4.2-4.2M14.2 9.8l4.2-4.2"/></svg>);
    case "bolt":
      return (<svg {...common}><path d="M13 2L4 14h7l-1 8 9-12h-7l1-8z"/></svg>);
    case "steering":
      return (<svg {...common}><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="2.5"/><path d="M12 14.5V21M3.5 11h6M14.5 11h6"/></svg>);
    case "layers":
      return (<svg {...common}><path d="M12 3l9 5-9 5-9-5 9-5z"/><path d="M3 13l9 5 9-5"/><path d="M3 17l9 5 9-5"/></svg>);
    case "brain":
      return (<svg {...common}><path d="M9 4a3 3 0 0 0-3 3v1a3 3 0 0 0-2 5.5A3 3 0 0 0 6 19a3 3 0 0 0 3 1V4z"/><path d="M15 4a3 3 0 0 1 3 3v1a3 3 0 0 1 2 5.5A3 3 0 0 1 18 19a3 3 0 0 1-3 1V4z"/></svg>);
    case "shield":
      return (<svg {...common}><path d="M12 3l8 3v6c0 5-3.5 8-8 9-4.5-1-8-4-8-9V6l8-3z"/><path d="M9 12l2.2 2.2L15 10.5"/></svg>);
    default:
      return null;
  }
};

// ============= Map Background (streets / blocks / water / parks) =============
const MapBackground = ({ id = "mapbg", w = 600, h = 600 }) => (
  <g>
    <defs>
      <pattern id={`${id}-blocks`} width="56" height="56" patternUnits="userSpaceOnUse">
        <rect width="56" height="56" fill="var(--ink-3)" opacity="0.35" />
        <rect x="2" y="2" width="22" height="22" rx="1" fill="var(--bone)" />
        <rect x="30" y="2" width="24" height="18" rx="1" fill="var(--bone)" />
        <rect x="2" y="30" width="18" height="24" rx="1" fill="var(--bone)" />
        <rect x="26" y="26" width="28" height="28" rx="1" fill="var(--bone)" />
      </pattern>
      <pattern id={`${id}-water`} width="20" height="20" patternUnits="userSpaceOnUse">
        <rect width="20" height="20" fill="oklch(0.92 0.04 230)" />
        <path d="M 0 10 Q 5 7, 10 10 T 20 10" stroke="oklch(0.85 0.06 230)" strokeWidth="0.8" fill="none" />
      </pattern>
    </defs>

    {/* Base — block grid */}
    <rect width={w} height={h} fill={`url(#${id}-blocks)`} />

    {/* Water body (river / coast) */}
    <path
      d={`M ${w * 0.62} ${h} L ${w * 0.78} ${h * 0.78} Q ${w * 0.86} ${h * 0.65}, ${w} ${h * 0.62} L ${w} ${h} Z`}
      fill={`url(#${id}-water)`}
    />
    <path
      d={`M ${w * 0.62} ${h} L ${w * 0.78} ${h * 0.78} Q ${w * 0.86} ${h * 0.65}, ${w} ${h * 0.62}`}
      stroke="oklch(0.7 0.08 230)" strokeWidth="1" fill="none" opacity="0.6"
    />

    {/* Park */}
    <rect x={w * 0.08} y={h * 0.18} width={w * 0.16} height={h * 0.14} rx="2" fill="oklch(0.9 0.08 140)" opacity="0.7" />
    <rect x={w * 0.08} y={h * 0.18} width={w * 0.16} height={h * 0.14} rx="2" fill="none" stroke="oklch(0.7 0.1 140)" strokeWidth="0.5" opacity="0.6" />

    {/* Major arterial roads */}
    <g stroke="var(--bone)" strokeWidth="6" fill="none" opacity="0.95">
      <path d={`M 0 ${h * 0.45} L ${w} ${h * 0.42}`} />
      <path d={`M ${w * 0.4} 0 L ${w * 0.42} ${h}`} />
      <path d={`M 0 ${h * 0.78} Q ${w * 0.5} ${h * 0.72}, ${w} ${h * 0.74}`} />
    </g>
    <g stroke="oklch(0.85 0.02 240)" strokeWidth="6.5" fill="none" opacity="0.4">
      <path d={`M 0 ${h * 0.45} L ${w} ${h * 0.42}`} />
      <path d={`M ${w * 0.4} 0 L ${w * 0.42} ${h}`} />
      <path d={`M 0 ${h * 0.78} Q ${w * 0.5} ${h * 0.72}, ${w} ${h * 0.74}`} />
    </g>
    {/* Centerlines */}
    <g stroke="oklch(0.78 0.03 60)" strokeWidth="0.6" strokeDasharray="4 4" fill="none">
      <path d={`M 0 ${h * 0.45} L ${w} ${h * 0.42}`} />
      <path d={`M ${w * 0.4} 0 L ${w * 0.42} ${h}`} />
    </g>

    {/* Minor streets */}
    <g stroke="var(--bone)" strokeWidth="2.5" fill="none" opacity="0.8">
      <path d={`M 0 ${h * 0.2} L ${w * 0.6} ${h * 0.22}`} />
      <path d={`M 0 ${h * 0.62} L ${w} ${h * 0.6}`} />
      <path d={`M ${w * 0.18} 0 L ${w * 0.18} ${h * 0.5}`} />
      <path d={`M ${w * 0.7} 0 L ${w * 0.72} ${h * 0.6}`} />
      <path d={`M ${w * 0.55} ${h * 0.5} L ${w * 0.6} ${h}`} />
    </g>
  </g>
);

// ============= Hero Map =============
const HeroMap = ({ copy, lang = "en" }) => {
  const L = getLabels(lang);
  const [feedIdx, setFeedIdx] = useState(0);
  useEffect(() => {
    const id = setInterval(() => setFeedIdx((i) => (i + 1) % 3), 2400);
    return () => clearInterval(id);
  }, []);
  const events = L.heroEvents;
  return (
    <div className="map-card">
      <div className="corners"><span></span></div>
      <div className="map-hud mono">
        <div className="live">{copy.live}</div>
        <div style={{ marginTop: 4 }}>{copy.session}</div>
      </div>

      <svg className="routes" viewBox="0 0 600 600" preserveAspectRatio="none">
        <MapBackground id="hero" w={600} h={600} />

        {/* Legacy route (ghost) */}
        <path className="route-line legacy" d="M 80 480 L 200 380 L 280 420 L 380 280 L 460 360 L 520 220" />

        {/* Optimized route */}
        <path className="route-line optimized" d="M 80 480 Q 150 380, 230 360 T 380 280 Q 440 250, 520 220" />

        {/* Secondary route */}
        <path className="route-line secondary" d="M 80 480 Q 180 500, 280 460 T 480 440 Q 540 420, 560 360" />

        {/* AI detection cones from cameras */}
        <g opacity="0.65">
          <path d="M 230 360 L 195 332 L 215 318 Z" fill="var(--accent)" opacity="0.18" />
          <path d="M 230 360 L 195 332 L 215 318 Z" stroke="var(--accent)" strokeWidth="0.8" fill="none" />
        </g>

        {/* Hub */}
        <circle cx="80" cy="480" r="9" fill="var(--accent)" />
        <circle cx="80" cy="480" r="14" fill="none" stroke="var(--accent)" strokeWidth="1.2" opacity="0.5" />
        <text x="96" y="484" fontSize="10" fontFamily="var(--font-mono)" fill="var(--ink)">{L.hubMvd04}</text>

        {/* Stops */}
        {[[230,360],[380,280],[520,220],[280,460],[480,440],[560,360]].map(([cx,cy], i) => (
          <g key={i}>
            <circle cx={cx} cy={cy} r="4" className="pin" />
            <text x={cx + 7} y={cy - 4} fontSize="8" fontFamily="var(--font-mono)" fill="var(--ink)" opacity="0.7">#{String(i+12).padStart(2,'0')}</text>
          </g>
        ))}

        {/* Live ping at active stop */}
        <g className="dot-ping">
          <circle cx="380" cy="280" r="3" fill="var(--accent)" />
          <circle cx="380" cy="280" className="ping" />
        </g>

        {/* Vehicle traveling */}
        <g>
          <rect x="-7" y="-5" width="14" height="10" rx="2" fill="var(--accent)" stroke="var(--bone)" strokeWidth="1.2">
            <animateMotion dur="9s" repeatCount="indefinite" path="M 80 480 Q 150 380, 230 360 T 380 280 Q 440 250, 520 220" rotate="auto" />
          </rect>
        </g>

        {/* AI bounding box on a detected pedestrian crossing */}
        <g>
          <rect x="296" y="318" width="22" height="14" fill="none" stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 2">
            <animate attributeName="opacity" values="1;0.3;1" dur="1.5s" repeatCount="indefinite" />
          </rect>
          <text x="296" y="313" fontSize="7" fontFamily="var(--font-mono)" fill="var(--accent)">{L.ped} · 0.94</text>
        </g>
      </svg>

      {/* Driver telemetry overlay (top-right) */}
      <div className="map-overlay tr mono">
        <div className="ovl-row">
          <span className="ovl-dot ok"></span>
          <span className="ovl-label">{L.driver} · J. PÉREZ</span>
        </div>
        <div className="ovl-row">
          <span className="ovl-label">{L.fatigueLabel}</span>
          <span className="ovl-bar"><span style={{ width: "18%" }}></span></span>
          <span className="ovl-val">{L.fatigueLow}</span>
        </div>
        <div className="ovl-row">
          <span className="ovl-label">{L.speed}</span>
          <span className="ovl-val">42 km/h</span>
        </div>
      </div>

      {/* AI events feed (mid-right) */}
      <div className="map-overlay mr mono">
        <div className="ovl-title">{L.aiEvents}</div>
        {events.map((e, i) => (
          <div key={i} className={`ovl-event ${i === feedIdx ? "on" : ""}`}>
            <span className="ovl-time">{e.t}</span>
            <span className={`ovl-tag ${e.tone}`}>{e.k}</span>
            <span className="ovl-evlabel">{e.label}</span>
          </div>
        ))}
      </div>

      <div className="map-stats">
        {copy.stats.map((s, i) => (
          <div key={i}>
            <div className="map-stat-label">{s.label}</div>
            <div className="map-stat-value" dangerouslySetInnerHTML={{ __html: s.value }} />
          </div>
        ))}
      </div>
    </div>
  );
};

// ============= Demo Map =============
const DemoMap = ({ chips, lang = "en" }) => {
  const L = getLabels(lang);
  const [time, setTime] = useState(0);
  const [events, setEvents] = useState(L.events);

  useEffect(() => {
    const id = setInterval(() => setTime((t) => t + 1), 240);
    return () => clearInterval(id);
  }, []);

  const timecode = useMemo(() => {
    const h = String(14 + Math.floor(time / 60) % 4).padStart(2, "0");
    const m = String(time % 60).padStart(2, "0");
    const s = String((time * 7) % 60).padStart(2, "0");
    return `${h}:${m}:${s} UTC-3`;
  }, [time]);

  return (
    <div className="demo-map">
      <div className="timecode mono">{timecode} · {chips.length} {lang === "es" ? "señales" : "signals"}</div>

      <svg className="routes" viewBox="0 0 800 600" preserveAspectRatio="xMidYMid slice">
        <MapBackground id="demo" w={800} h={600} />

        {/* Legacy ghost routes */}
        <path className="route-line legacy" d="M 100 100 L 200 220 L 320 180 L 440 280 L 560 220 L 680 320" />
        <path className="route-line legacy" d="M 100 500 L 220 440 L 320 480 L 460 400 L 600 460 L 700 380" />

        {/* Optimized live routes */}
        <path className="route-line optimized" d="M 100 100 Q 200 180, 320 180 T 560 220 Q 620 250, 680 320" />
        <path className="route-line secondary" d="M 100 500 Q 220 440, 340 470 T 600 420 Q 660 400, 700 380" />
        <path className="route-line optimized" d="M 100 300 Q 200 260, 280 320 T 460 360 Q 540 340, 620 280" style={{ animationDelay: "0.5s" }} />

        {/* Hub */}
        <circle cx="100" cy="300" r="9" fill="var(--accent)" />
        <circle cx="100" cy="300" r="16" fill="none" stroke="var(--accent)" strokeWidth="1" opacity="0.5" />
        <text x="118" y="304" fill="var(--ink)" fontSize="11" fontFamily="var(--font-mono)">{L.hub}</text>

        {/* Stops */}
        {[[200,220],[320,180],[440,280],[560,220],[680,320],[220,440],[320,480],[460,400],[600,460],[700,380],[280,320],[460,360],[620,280]].map(([cx, cy], i) => (
          <circle key={i} cx={cx} cy={cy} r="3.5" className="pin" />
        ))}

        {/* AI detection bounding boxes */}
        <g>
          <rect x="430" y="270" width="24" height="16" fill="none" stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 2">
            <animate attributeName="opacity" values="1;0.3;1" dur="1.4s" repeatCount="indefinite" />
          </rect>
          <text x="430" y="265" fontSize="9" fontFamily="var(--font-mono)" fill="var(--accent)">{L.ped} · 0.92</text>
        </g>
        <g>
          <rect x="595" y="455" width="22" height="14" fill="none" stroke="oklch(0.7 0.18 30)" strokeWidth="1.2" strokeDasharray="3 2">
            <animate attributeName="opacity" values="1;0.3;1" dur="1.4s" repeatCount="indefinite" begin="0.6s" />
          </rect>
          <text x="595" y="450" fontSize="9" fontFamily="var(--font-mono)" fill="oklch(0.6 0.18 30)">{L.hazard} · 0.81</text>
        </g>

        {/* Camera detection cones */}
        <path d="M 320 480 L 280 510 L 280 470 Z" fill="var(--accent)" opacity="0.12" />
        <path d="M 320 480 L 280 510 L 280 470 Z" stroke="var(--accent)" strokeWidth="0.8" fill="none" opacity="0.6" />
        <path d="M 600 460 L 640 490 L 640 450 Z" fill="oklch(0.7 0.18 30)" opacity="0.12" />
        <path d="M 600 460 L 640 490 L 640 450 Z" stroke="oklch(0.7 0.18 30)" strokeWidth="0.8" fill="none" opacity="0.6" />

        {/* Live vehicles */}
        <g>
          <rect x="-8" y="-6" width="16" height="12" rx="2" fill="var(--accent)" stroke="var(--bone)" strokeWidth="1.4">
            <animateMotion dur="8s" repeatCount="indefinite" path="M 100 100 Q 200 180, 320 180 T 560 220 Q 620 250, 680 320" rotate="auto" />
          </rect>
          <rect x="-8" y="-6" width="16" height="12" rx="2" fill="var(--accent-2)" stroke="var(--bone)" strokeWidth="1.4">
            <animateMotion dur="11s" repeatCount="indefinite" path="M 100 500 Q 220 440, 340 470 T 600 420 Q 660 400, 700 380" rotate="auto" />
          </rect>
          <rect x="-8" y="-6" width="16" height="12" rx="2" fill="var(--accent)" stroke="var(--bone)" strokeWidth="1.4">
            <animateMotion dur="9.5s" repeatCount="indefinite" begin="1s" path="M 100 300 Q 200 260, 280 320 T 460 360 Q 540 340, 620 280" rotate="auto" />
          </rect>
        </g>

        {/* Pings */}
        <g className="dot-ping"><circle cx="440" cy="280" r="3" fill="var(--accent)" /><circle cx="440" cy="280" className="ping" /></g>
        <g className="dot-ping"><circle cx="320" cy="480" r="3" fill="var(--accent-2)" /><circle cx="320" cy="480" className="ping" /></g>
      </svg>

      {/* Driver monitoring panel */}
      <div className="dm-panel mono">
        <div className="dm-title">{L.monitorTitle}</div>
        {[
          { id: "04 · J. Pérez", fatigue: 18, speed: "42 km/h", state: "ok" },
          { id: "07 · M. Silva", fatigue: 64, speed: "38 km/h", state: "warn" },
          { id: "09 · A. Cabrera", fatigue: 31, speed: "51 km/h", state: "ok" },
        ].map((d, i) => (
          <div key={i} className="dm-row">
            <div className="dm-driver">
              <span className={`dm-dot ${d.state}`}></span>
              <span className="dm-id">VH-{d.id}</span>
            </div>
            <div className="dm-meta">
              <span className="dm-bar"><span style={{ width: `${d.fatigue}%` }} className={d.state}></span></span>
              <span className="dm-spd">{d.speed}</span>
            </div>
          </div>
        ))}
      </div>

      {/* AI events feed */}
      <div className="ev-panel mono">
        <div className="ev-title">{L.eventsTitle}</div>
        {events.map((e, i) => (
          <div key={i} className="ev-row">
            <span className="ev-time">{e.t}</span>
            <span className={`ev-tag ${e.tone}`}>{e.k}</span>
            <span className="ev-label">{e.label}</span>
          </div>
        ))}
      </div>

      {/* Camera tile */}
      <div className="cam-tile">
        <div className="cam-header mono">
          <span className="cam-rec">{L.rec}</span>
          <span>{L.cam}</span>
        </div>
        <svg viewBox="0 0 200 120" preserveAspectRatio="xMidYMid slice" className="cam-feed">
          <defs>
            <linearGradient id="camg-overlay" x1="0" y1="0" x2="0" y2="1">
              <stop offset="60%" stopColor="transparent" />
              <stop offset="100%" stopColor="oklch(0.14 0.02 240)" />
            </linearGradient>
            <filter id="cam-desat">
              <feColorMatrix type="saturate" values="0.95" />
              <feComponentTransfer>
                <feFuncR type="linear" slope="1.05" />
                <feFuncG type="linear" slope="1.05" />
                <feFuncB type="linear" slope="1.08" />
              </feComponentTransfer>
            </filter>
          </defs>
          {/* Real driver photo */}
          <image href="assets/driver-portrait.jpg?v=2" x="-20" y="-10" width="240" height="140" preserveAspectRatio="xMidYMid slice" filter="url(#cam-desat)" />
          {/* Dark overlay at bottom for HUD legibility */}
          <rect width="200" height="120" fill="url(#camg-overlay)" />
          {/* Bounding box on face */}
          <rect x="68" y="10" width="64" height="78" fill="none" stroke="var(--accent)" strokeWidth="1.5" strokeDasharray="3 2">
            <animate attributeName="opacity" values="1;0.4;1" dur="1.6s" repeatCount="indefinite" />
          </rect>
          {/* corner accents */}
          {[[68,10,1,1],[132,10,-1,1],[68,88,1,-1],[132,88,-1,-1]].map(([cx,cy,dx,dy],i) => (
            <g key={i} stroke="var(--accent)" strokeWidth="2.2" fill="none">
              <line x1={cx} y1={cy} x2={cx+8*dx} y2={cy} />
              <line x1={cx} y1={cy} x2={cx} y2={cy+8*dy} />
            </g>
          ))}
          <text x="68" y="7" fontSize="7" fontFamily="var(--font-mono)" fill="var(--accent)">{L.face}</text>
          {/* Eye-detection dots */}
          <circle cx="94" cy="44" r="1.2" fill="var(--accent)" />
          <circle cx="106" cy="44" r="1.2" fill="var(--accent)" />
          {/* Scanline */}
          <rect x="0" y="0" width="200" height="2" fill="var(--accent)" opacity="0.4">
            <animate attributeName="y" from="0" to="120" dur="3s" repeatCount="indefinite" />
          </rect>
        </svg>
        <div className="cam-foot mono">
          <span>{L.eyes.split(" · ")[0]} · <span style={{color:"oklch(0.65 0.18 145)"}}>{ L.eyes.split(" · ")[1]}</span></span>
          <span>{L.yaw}</span>
          <span>{L.attn.split(" · ")[0]} · <span style={{color:"oklch(0.65 0.18 145)"}}>{L.attn.split(" · ")[1]}</span></span>
        </div>
      </div>
    </div>
  );
};

// ============= Demo Panel =============
const DemoPanel = ({ copy }) => {
  const [activeChips, setActiveChips] = useState(new Set([0, 1, 2, 3]));
  const [progress, setProgress] = useState(20);

  useEffect(() => {
    const id = setInterval(() => setProgress((p) => (p >= 100 ? 28 : p + 6)), 800);
    return () => clearInterval(id);
  }, []);

  const toggleChip = (i) => {
    setActiveChips((prev) => {
      const next = new Set(prev);
      if (next.has(i)) next.delete(i); else next.add(i);
      return next;
    });
  };

  return (
    <div className="demo-panel">
      <div>
        <div className="eyebrow" style={{ marginBottom: 12 }}>{copy.progress_label}</div>
        <h3>{copy.panel_title}</h3>
      </div>
      <div className="progress"><span style={{ width: `${progress}%` }}></span></div>

      <div style={{ display: "flex", flexDirection: "column" }}>
        {copy.rows.map((r, i) => (
          <div className="demo-row" key={i}>
            <div className="label">{r.label}</div>
            <div>
              <span className="value">{r.value}</span>
              {r.delta && <span className={`delta ${r.delta.startsWith("−") ? "down" : "up"}`}>{r.delta}</span>}
            </div>
          </div>
        ))}
      </div>

      <div className="demo-controls">
        {copy.chips.map((c, i) => (
          <button key={i} className={`chip ${activeChips.has(i) ? "on" : ""}`} onClick={() => toggleChip(i)}>{c}</button>
        ))}
      </div>
    </div>
  );
};

// ============= Feature Visuals =============
const FeatureViz = ({ kind, lang = "en" }) => {
  if (kind === "routing") {
    const isEs = lang === "es";
    const RoutingViz = () => {
      const [t, setT] = useState(0);
      useEffect(() => {
        const id = setInterval(() => setT(x => x + 1), 60);
        return () => clearInterval(id);
      }, []);

      // Phase: 0-60 show old route, 60-120 animate re-plan, 120-240 show new route with vehicle
      const phase = Math.floor(t / 80) % 3; // 0=old, 1=replan, 2=optimized
      const phaseT = (t % 80) / 80;

      // Stops
      const stops = [
        { x:20,  y:85, label:"HUB" },
        { x:80,  y:55, label:"#2" },
        { x:130, y:72, label:"#3" },
        { x:175, y:38, label:"#4" },
        { x:220, y:60, label:"#5" },
        { x:270, y:28, label:"#6" },
      ];

      // Old inefficient route (zigzag)
      const oldPath = "M 20 85 L 130 72 L 80 55 L 220 60 L 175 38 L 270 28";
      // New optimized route (smooth)
      const newPath = "M 20 85 Q 50 70, 80 55 T 175 38 Q 220 32, 270 28";

      // Vehicle position on new route
      const vehicleT = phase === 2 ? phaseT : (phase === 1 ? 0 : 0);
      const vPoints = [
        {x:20,y:85},{x:80,y:55},{x:130,y:62},{x:175,y:38},{x:220,y:44},{x:270,y:28}
      ];
      const vIdx = Math.floor(vehicleT * (vPoints.length - 1));
      const vFrac = (vehicleT * (vPoints.length - 1)) - vIdx;
      const vp = vPoints[Math.min(vIdx, vPoints.length-2)];
      const vp2 = vPoints[Math.min(vIdx+1, vPoints.length-1)];
      const vx = vp.x + (vp2.x - vp.x) * vFrac;
      const vy = vp.y + (vp2.y - vp.y) * vFrac;

      const showOld = phase === 0;
      const showNew = phase === 2;
      const replanning = phase === 1;

      return (
        <svg viewBox="0 0 300 115" style={{ width:"100%", height:"100%" }}>
          {/* Map background grid */}
          {Array.from({length:6}).map((_,i) => (
            <line key={`h${i}`} x1="0" y1={i*22} x2="300" y2={i*22} stroke="var(--line-soft)" strokeWidth="0.4" />
          ))}
          {Array.from({length:8}).map((_,i) => (
            <line key={`v${i}`} x1={i*44} y1="0" x2={i*44} y2="115" stroke="var(--line-soft)" strokeWidth="0.4" />
          ))}

          {/* Old inefficient route */}
          <path
            d={oldPath}
            stroke="var(--warn)" strokeWidth="1.8" fill="none"
            strokeDasharray="4 3"
            opacity={showOld ? 0.7 : replanning ? 0.3 * (1 - phaseT) : 0}
            style={{ transition:"opacity 0.5s" }}
          />

          {/* New optimized route */}
          <path
            d={newPath}
            stroke="var(--accent)" strokeWidth="2.2" fill="none"
            strokeDasharray="200"
            strokeDashoffset={showNew ? 0 : replanning ? 200 * (1 - phaseT) : 200}
            opacity={showOld ? 0 : 1}
            style={{ transition:"stroke-dashoffset 1.2s ease, opacity 0.3s" }}
          />

          {/* Animated flow on new route */}
          {showNew && (
            <path
              d={newPath}
              stroke="var(--accent)" strokeWidth="2.2" fill="none"
              strokeDasharray="12 30"
              opacity="0.45"
            >
              <animate attributeName="stroke-dashoffset" from="0" to="-42" dur="1.4s" repeatCount="indefinite" />
            </path>
          )}

          {/* Stops */}
          {stops.map((s, i) => {
            const done = showNew && vehicleT > i / (stops.length - 1);
            return (
              <g key={i}>
                <circle
                  cx={s.x} cy={s.y} r={i === 0 ? 5 : 3.5}
                  fill={i === 0 ? "var(--accent)" : done ? "oklch(0.62 0.18 145)" : "var(--bone)"}
                  stroke={i === 0 ? "var(--accent)" : done ? "oklch(0.62 0.18 145)" : "var(--line)"}
                  strokeWidth="1.5"
                />
                {i === 0 && <text x={s.x} y={s.y+3} textAnchor="middle" fontSize="5" fontFamily="var(--font-mono)" fill="white">H</text>}
                {i > 0 && (
                  <text x={s.x} y={s.y - 6} textAnchor="middle" fontSize="6.5" fontFamily="var(--font-mono)"
                    fill={done ? "oklch(0.58 0.18 145)" : "var(--mist)"}>{s.label}</text>
                )}
                {done && i > 0 && (
                  <text x={s.x} y={s.y + 3} textAnchor="middle" fontSize="5.5" fill="oklch(0.62 0.18 145)">✓</text>
                )}
              </g>
            );
          })}

          {/* Vehicle */}
          {showNew && (
            <g>
              <circle cx={vx} cy={vy} r={9} fill="var(--accent)" opacity={0.15} />
              <circle cx={vx} cy={vy} r={5} fill="var(--accent)" />
              <circle cx={vx} cy={vy} r={2.5} fill="white" />
            </g>
          )}

          {/* Re-planning badge */}
          {replanning && (
            <g>
              <rect x="80" y="42" width="140" height="26" rx="4" fill="var(--ink)" opacity="0.92" />
              <text x="150" y="53" textAnchor="middle" fontSize="7.5" fontFamily="var(--font-mono)" fill="var(--accent)" letterSpacing="0.1em">
                {isEs ? "RE-OPTIMIZANDO" : "RE-OPTIMIZING"}
              </text>
              <text x="150" y="63" textAnchor="middle" fontSize="7" fontFamily="var(--font-mono)" fill="oklch(0.65 0.03 240)">
                {isEs ? "▸ ahorrando 38% km" : "▸ saving 38% km"}
              </text>
            </g>
          )}

          {/* Stats strip */}
          <rect x="0" y="97" width="300" height="18" fill="var(--ink-3)" opacity="0.7" />
          <text x="10" y="108" fontSize="7" fontFamily="var(--font-mono)" fill="var(--mist)" letterSpacing="0.06em">
            {showOld ? (isEs ? "RUTA ORIGINAL · 8.430 km" : "ORIGINAL ROUTE · 8,430 km") :
             replanning ? (isEs ? "SOLVER · convergiendo…" : "SOLVER · converging…") :
             (isEs ? "RUTA OPTIMIZADA · 5.212 km · −38%" : "OPTIMIZED · 5,212 km · −38%")}
          </text>
          {showNew && (
            <text x="290" y="108" textAnchor="end" fontSize="7" fontFamily="var(--font-mono)" fill="oklch(0.62 0.18 145)" letterSpacing="0.06em">
              {isEs ? "EN VIVO" : "LIVE"}
            </text>
          )}
        </svg>
      );
    };
    return <RoutingViz />;
  }
  if (kind === "eta") {
    const isEs = lang === "es";
    const EtaViz = () => {
      const [t, setT] = useState(0);
      useEffect(() => {
        const id = setInterval(() => setT(x => (x + 1) % 140), 50);
        return () => clearInterval(id);
      }, []);
      const progress = t / 139;

      const stops = [
        { x: 22,  label: isEs ? "BASE" : "HUB" },
        { x: 85,  label: "#2" },
        { x: 148, label: "#3" },
        { x: 211, label: "#4" },
        { x: 270, label: isEs ? "FIN" : "END" },
      ];
      const vx = 22 + progress * 248;
      const spread = 20 * (1 - progress * 0.88);
      const etaMin = Math.round(18 - progress * 16);
      const accuracy = Math.round(88 + progress * 10);
      const stopIdx = Math.min(Math.floor(progress * 5), 4);

      return (
        <svg viewBox="0 0 300 140" style={{ width:"100%", height:"100%" }}>
          <line x1="22" y1="55" x2="278" y2="55" stroke="var(--line)" strokeWidth="1.5" strokeLinecap="round" />

          {/* Prediction cone */}
          {vx < 265 && (
            <polygon
              points={`${vx},55 ${Math.min(vx+70,270)},${55-spread} ${Math.min(vx+70,270)},${55+spread}`}
              fill="var(--accent)" opacity="0.10"
            />
          )}

          {/* Stops */}
          {stops.map((s, i) => {
            const done = vx > s.x + 5;
            const active = !done && Math.abs(vx - s.x) < 18;
            return (
              <g key={i}>
                <circle cx={s.x} cy={55} r={i===0?6:4}
                  fill={done ? "oklch(0.62 0.18 145)" : active ? "var(--accent)" : "var(--bone)"}
                  stroke={done ? "oklch(0.62 0.18 145)" : active ? "var(--accent)" : "var(--line)"}
                  strokeWidth="1.5"
                />
                {done && i > 0 && <text x={s.x} y={58} textAnchor="middle" fontSize="5" fill="white">✓</text>}
                <text x={s.x} y={44} textAnchor="middle" fontSize="6.5" fontFamily="var(--font-mono)"
                  fill={done ? "oklch(0.58 0.18 145)" : active ? "var(--accent)" : "var(--mist)"}>{s.label}</text>
              </g>
            );
          })}

          {/* Vehicle */}
          <circle cx={vx} cy={55} r={9} fill="var(--accent)" opacity={0.18} />
          <circle cx={vx} cy={55} r={5} fill="var(--accent)" />
          <circle cx={vx} cy={55} r={2.2} fill="white" />

          {/* ETA card */}
          <rect x="8" y="74" width="136" height="56" rx="4" fill="var(--ink)" opacity="0.05" />
          <text x="16" y="88" fontSize="7" fontFamily="var(--font-mono)" fill="var(--mist)" letterSpacing="0.08em">
            {isEs ? "ETA ESTIMADA" : "PREDICTED ETA"}
          </text>
          <text x="16" y="108" fontSize="16" fontFamily="var(--font-mono)" fontWeight="600" fill="var(--ink)">
            {`14:${String(Math.max(0,etaMin)).padStart(2,"0")}`}
          </text>
          <text x="16" y="122" fontSize="8" fontFamily="var(--font-mono)" fill="var(--mist)">
            {isEs ? `±${Math.round(spread/1.4)} min ventana` : `±${Math.round(spread/1.4)} min window`}
          </text>

          {/* Accuracy card */}
          <rect x="156" y="74" width="136" height="56" rx="4" fill="var(--ink)" opacity="0.05" />
          <text x="164" y="88" fontSize="7" fontFamily="var(--font-mono)" fill="var(--mist)" letterSpacing="0.08em">
            {isEs ? "PRECISIÓN" : "ACCURACY"}
          </text>
          <rect x="164" y="96" width="120" height="7" rx="2" fill="var(--ink-3)" />
          <rect x="164" y="96" width={120 * accuracy / 100} height="7" rx="2" fill="oklch(0.62 0.18 145)" />
          <text x="164" y="116" fontSize="15" fontFamily="var(--font-mono)" fontWeight="600" fill="oklch(0.58 0.18 145)">{accuracy}%</text>
          <text x="288" y="122" textAnchor="end" fontSize="7.5" fontFamily="var(--font-mono)" fill="var(--mist)">
            {isEs ? `parada ${stopIdx+1}/5` : `stop ${stopIdx+1}/5`}
          </text>
        </svg>
      );
    };
    return <EtaViz />;
  }
  if (kind === "copilot") {
    const isEs = lang === "es";
    const messages = isEs
      ? [
          { who: "dispatch", txt: "Parada #18 omitida — cliente AVL no estaba." },
          { who: "flow",     txt: "Entendido. Propongo reagendar mañana 09:30 y reordenar 4 paradas. ¿Aprobás?" },
          { who: "dispatch", txt: "Sí, aprobado." },
          { who: "flow",     txt: "✓ Hecho. ETAs actualizados. Chofer notificado." },
        ]
      : [
          { who: "dispatch", txt: "Stop #18 missed — client AVL wasn't home." },
          { who: "flow",     txt: "Got it. I suggest rescheduling tomorrow 09:30 and reordering 4 stops. Approve?" },
          { who: "dispatch", txt: "Yes, approved." },
          { who: "flow",     txt: "✓ Done. ETAs updated. Driver notified." },
        ];
    return (
      <div style={{ padding: "6px 8px", display: "flex", flexDirection: "column", gap: 4, background: "oklch(0.95 0.008 240)", height: "100%", boxSizing: "border-box", overflow: "hidden" }}>
        {/* chat header */}
        <div style={{ display: "flex", alignItems: "center", gap: 5, paddingBottom: 4, borderBottom: "1px solid var(--line-soft)", flexShrink: 0 }}>
          <div style={{ width: 15, height: 15, borderRadius: "50%", background: "var(--accent)", display: "flex", alignItems: "center", justifyContent: "center", fontSize: 7, color: "white", fontWeight: 600 }}>G</div>
          <div style={{ fontSize: 8.5, fontWeight: 600, color: "var(--ink)" }}>Gropp Flow</div>
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 7, color: "oklch(0.62 0.18 145)", marginLeft: 2 }}>● {isEs ? "en línea" : "online"}</div>
        </div>
        <div style={{ display: "flex", justifyContent: "flex-end", flexShrink: 0 }}>
          <div style={{ maxWidth: "85%", background: "oklch(0.58 0.17 145)", color: "white", borderRadius: "10px 10px 3px 10px", padding: "5px 9px", fontSize: 9.5, lineHeight: 1.35 }}>
            {isEs ? "Parada #18 omitida — cliente no estaba." : "Stop #18 missed — client not home."}
            <div style={{ fontFamily: "var(--font-mono)", fontSize: 6.5, color: "rgba(255,255,255,0.6)", textAlign: "right", marginTop: 1 }}>14:02 ✓✓</div>
          </div>
        </div>
        <div style={{ display: "flex", justifyContent: "flex-start", flexShrink: 0 }}>
          <div style={{ maxWidth: "92%", background: "white", color: "var(--ink)", borderRadius: "10px 10px 10px 3px", padding: "5px 9px", fontSize: 9.5, lineHeight: 1.35, boxShadow: "0 1px 2px oklch(0.50 0.04 240 / 0.10)" }}>
            <div style={{ fontFamily: "var(--font-mono)", fontSize: 6.5, color: "var(--accent)", marginBottom: 2, letterSpacing: "0.06em" }}>GROPP AI</div>
            {isEs ? "Reagendo mañana 09:30 y reordeno 4 paradas. ¿Aprobás?" : "Rescheduling tomorrow 09:30, reordering 4 stops. Approve?"}
            <div style={{ fontFamily: "var(--font-mono)", fontSize: 6.5, color: "var(--mist)", textAlign: "right", marginTop: 1 }}>14:02</div>
          </div>
        </div>
      </div>
    );
  }
  if (kind === "driver") {
    const isEs = lang === "es";
    return (
      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 10, padding: 14, alignItems: "stretch", height: "100%" }}>
        {/* Left: signed invoice */}
        <div style={{ background: "var(--bone)", border: "1px solid var(--line)", borderRadius: 6, padding: "8px 10px", display: "flex", flexDirection: "column", gap: 4, position: "relative", overflow: "hidden" }}>
          {/* invoice header */}
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 7.5, color: "var(--accent)", letterSpacing: "0.1em", marginBottom: 2, marginTop: 14 }}>REMITO #0042</div>
          <div style={{ height: 1, background: "var(--line)" }} />
          {/* line items */}
          {[
            { label: isEs ? "Caja 18 kg" : "Box 18 kg", qty: "×1", code: "PKG-18" },
            { label: isEs ? "Destino" : "Destination", qty: "", code: "Av. Italia 4321" },
            { label: isEs ? "Cliente" : "Client", qty: "", code: "G. Pereyra" },
          ].map((r, i) => (
            <div key={i} style={{ display: "flex", justifyContent: "space-between", fontFamily: "var(--font-mono)", fontSize: 7.5, color: "var(--fog)" }}>
              <span style={{ color: "var(--ink)", fontSize: 7 }}>{r.label}</span>
              <span style={{ color: "var(--mist)", fontSize: 7 }}>{r.code}</span>
            </div>
          ))}
          <div style={{ height: 1, background: "var(--line)", marginTop: 2 }} />
          {/* signature area */}
          <div style={{ marginTop: 2, fontSize: 7, fontFamily: "var(--font-mono)", color: "var(--mist)", letterSpacing: "0.08em" }}>{isEs ? "FIRMA RECEPTOR:" : "SIGNATURE:"}</div>
          <svg viewBox="0 0 90 22" style={{ width: "100%", height: 22 }}>
            <path d="M 6 16 Q 18 4, 26 14 T 44 10 Q 56 4, 66 12 T 84 9" fill="none" stroke="var(--ink)" strokeWidth="1.4" strokeLinecap="round" />
          </svg>
          {/* Gropp logo top right */}
          <div style={{ position: "absolute", top: 7, right: 8, display: "flex", alignItems: "center", gap: 3 }}>
            <img src="assets/gropp-logo.png" alt="Gropp" style={{ height: 12, opacity: 0.7 }} />
          </div>
        </div>
        {/* Right: stop info */}
        <div style={{ display: "flex", flexDirection: "column", gap: 6, fontFamily: "var(--font-mono)", fontSize: 10, color: "var(--fog)" }}>
          <div style={{ color: "var(--accent)" }}>{isEs ? "PRÓXIMA · 12 min" : "NEXT · 12 min"}</div>
          <div>Av. Italia 4321</div>
          <div>↳ {isEs ? "Caja 18kg · Firma" : "Box 18kg · Signature"}</div>
          <div style={{ marginTop: "auto", padding: "4px 8px", background: "var(--accent)", color: "var(--accent-ink)", borderRadius: 3, textAlign: "center" }}>{isEs ? "ENTREGADO" : "DELIVERED"}</div>
        </div>
      </div>
    );
  }
  return null;
};

// ============= Vehicle Diagram (truck photo + sensor callouts) =============
const VehicleDiagram = ({ sensors }) => {
  const [hovered, setHovered] = useState(null);

  // Icons per sensor id
  const icons = {
    gps:   <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><circle cx="12" cy="12" r="3"/><path d="M12 2v3M12 19v3M2 12h3M19 12h3"/><circle cx="12" cy="12" r="9" strokeDasharray="3 2"/></svg>,
    cabin: <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><circle cx="12" cy="12" r="3"/><path d="M3 9a2 2 0 0 1 2-2h1l2-3h8l2 3h1a2 2 0 0 1 2 2v8a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/></svg>,
    road:  <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M5 20L12 4l7 16M9 14h6"/></svg>,
    lte:   <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M2 20h.01M7 20v-4M12 20v-8M17 20V8M22 4v16"/></svg>,
    imu:   <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/></svg>,
    can:   <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round"><ellipse cx="12" cy="7" rx="9" ry="4"/><path d="M3 7v10a9 4 0 0 0 18 0V7"/><path d="M3 12a9 4 0 0 0 18 0"/></svg>,
  };

  const leftIds  = ["gps", "cabin", "road"];
  const rightIds = ["lte", "imu", "can"];

  const SensorCard = ({ s }) => (
    <div
      className={`vd-card ${hovered === s.id ? "on" : ""}`}
      onMouseEnter={() => setHovered(s.id)}
      onMouseLeave={() => setHovered(null)}
    >
      <div className="vd-card-head">
        <span className="vd-card-icon">{icons[s.id]}</span>
        <span className="vd-card-title">{s.label}</span>
      </div>
      <p className="vd-card-body">{s.body}</p>
    </div>
  );

  return (
    <div className="vehicle">
      <style>{`
        .vehicle { display: grid; grid-template-columns: 1fr auto 1fr; gap: 40px; align-items: center; }
        .vd-col { display: flex; flex-direction: column; gap: 14px; justify-content: center; }
        .vd-col--right .vd-card { border-left: 1px solid var(--line); border-right: 3px solid var(--line); }
        .vd-col--right .vd-card:hover, .vd-col--right .vd-card.on { border-left-color: var(--line); border-right-color: var(--accent); }
        .vd-card {
          background: var(--bone); border: 1px solid var(--line);
          border-left: 3px solid var(--line);
          border-radius: 8px; padding: 16px 16px;
          cursor: pointer; transition: all .2s;
          max-width: 220px;
        }
        .vd-card:hover, .vd-card.on {
          background: var(--ink-2);
          border-left-color: var(--accent);
        }
        .vd-card-head { display: flex; align-items: center; gap: 9px; margin-bottom: 8px; }
        .vd-card-icon { color: var(--accent); flex-shrink: 0; }
        .vd-card-title { font-size: 13px; font-weight: 500; color: var(--ink); }
        .vd-card-body { font-size: 12px; color: var(--fog); line-height: 1.5; }
        .vd-truck { width: 520px; flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
        .vd-truck img { width: 100%; display: block; }
        @media (max-width: 1100px) {
          .vehicle { grid-template-columns: 1fr; }
          .vd-truck { width: 100%; }
          .vd-col { display: grid; grid-template-columns: 1fr 1fr; }
        }
      `}</style>

      {/* Left column */}
      <div className="vd-col" style={{ alignItems:"flex-end" }}>
        {leftIds.map(id => {
          const s = sensors.find(x => x.id === id);
          return s ? <SensorCard key={id} s={s} /> : null;
        })}
      </div>

      {/* Truck */}
      <div className="vd-truck">
        <img src="assets/truck.png" alt="delivery truck" />
      </div>

      {/* Right column */}
      <div className="vd-col vd-col--right" style={{ alignItems:"flex-start" }}>
        {rightIds.map(id => {
          const s = sensors.find(x => x.id === id);
          return s ? <SensorCard key={id} s={s} /> : null;
        })}
      </div>
    </div>
  );
};

// ============= Driver AI Animations (4 cards) =============
const DriverAiViz = ({ kind, lang = "en" }) => {
  const L = getLabels(lang);
  if (kind === "fatigue") {
    // 3-camera grid: cabin front, side/surroundings, forward road
    return (
      <div className="dai-viz dai-3cams">
        <style>{`
          .dai-3cams { display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr; gap: 2px; background: oklch(0.14 0.03 240); }
          .dai-cam-cell { position: relative; overflow: hidden; }
          .dai-cam-cell img { width: 100%; height: 100%; object-fit: cover; display: block; filter: contrast(1.05) brightness(0.9); }
          .dai-cam-cell.main { grid-row: 1 / 3; }
          .dai-cam-label { position: absolute; top: 4px; left: 5px; font-family: var(--font-mono); font-size: 7px; color: var(--bone); letter-spacing: 0.08em; background: oklch(0.12 0.03 240 / 0.75); padding: 2px 5px; border-radius: 2px; }
          .dai-cam-label-rec { color: oklch(0.7 0.2 25); animation: pulse 1.4s infinite; }
          .dai-cam-bb { position: absolute; inset: 0; pointer-events: none; }
          .dai-cam-foot { position: absolute; bottom: 0; left: 0; right: 0; display: flex; justify-content: space-between; padding: 3px 5px; font-family: var(--font-mono); font-size: 7px; background: oklch(0.10 0.03 240 / 0.75); color: var(--bone); }
          .dai-cam-foot-ok { color: oklch(0.72 0.18 145); }
        `}</style>

        {/* MAIN: cabin front */}
        <div className="dai-cam-cell main">
          <img src="assets/driver-portrait.png" alt="Cabin" />
          <div className="dai-cam-label"><span className="dai-cam-label-rec">● </span>CAB-01</div>
          <svg className="dai-cam-bb" viewBox="0 0 150 140" preserveAspectRatio="none">
            <rect x="38" y="14" width="74" height="90" fill="none" stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="4 2">
              <animate attributeName="opacity" values="1;0.4;1" dur="1.7s" repeatCount="indefinite" />
            </rect>
            {[[38,14,1,1],[112,14,-1,1],[38,104,1,-1],[112,104,-1,-1]].map(([cx,cy,dx,dy],i) => (
              <g key={i} stroke="var(--accent)" strokeWidth="1.8" fill="none">
                <line x1={cx} y1={cy} x2={cx+7*dx} y2={cy} />
                <line x1={cx} y1={cy} x2={cx} y2={cy+7*dy} />
              </g>
            ))}
            <text x="38" y="11" fontSize="6.5" fontFamily="var(--font-mono)" fill="var(--accent)">{L.daiDriver}</text>
            <circle cx="63" cy="52" r="1.4" fill="var(--accent)"><animate attributeName="opacity" values="1;1;0.1;1" keyTimes="0;0.43;0.5;1" dur="3s" repeatCount="indefinite" /></circle>
            <circle cx="87" cy="52" r="1.4" fill="var(--accent)"><animate attributeName="opacity" values="1;1;0.1;1" keyTimes="0;0.43;0.5;1" dur="3s" repeatCount="indefinite" /></circle>
            <rect x="38" y="14" width="74" height="1.2" fill="var(--accent)" opacity="0.5">
              <animate attributeName="y" values="14;104;14" dur="3.8s" repeatCount="indefinite" />
            </rect>
          </svg>
          <div className="dai-cam-foot">
            <span>{L.daiAttn} <span className="dai-cam-foot-ok">94%</span></span>
            <span>{lang === "es" ? "FATIGA" : "FATIGUE"}: <span className="dai-cam-foot-ok">{lang === "es" ? "BAJA" : "LOW"}</span></span>
          </div>
        </div>

        {/* TOP RIGHT: side surroundings */}
        <div className="dai-cam-cell">
          <img src="assets/street-cam.png" alt="Exterior cam" style={{width:"100%",height:"100%",objectFit:"cover",display:"block",filter:"contrast(1.05) brightness(0.88)"}} />
          {/* AI detection overlay */}
          <svg className="dai-cam-bb" viewBox="0 0 150 69" preserveAspectRatio="none">
            {/* pedestrian 1 */}
            <rect x="28" y="22" width="16" height="32" fill="none" stroke="oklch(0.80 0.18 50)" strokeWidth="1.2" strokeDasharray="3 2">
              <animate attributeName="opacity" values="1;0.3;1" dur="1.5s" repeatCount="indefinite" />
            </rect>
            <text x="28" y="20" fontSize="5.5" fontFamily="var(--font-mono)" fill="oklch(0.80 0.18 50)">{lang === "es" ? "PEATÓN" : "PED"} 0.94</text>
            {/* pedestrian 2 */}
            <rect x="100" y="26" width="14" height="28" fill="none" stroke="oklch(0.80 0.18 50)" strokeWidth="1.2" strokeDasharray="3 2">
              <animate attributeName="opacity" values="1;0.4;1" dur="1.8s" repeatCount="indefinite" begin="0.4s" />
            </rect>
            <text x="100" y="24" fontSize="5.5" fontFamily="var(--font-mono)" fill="oklch(0.80 0.18 50)">{lang === "es" ? "PEATÓN" : "PED"} 0.89</text>
          </svg>
          <div className="dai-cam-label"><span className="dai-cam-label-rec">● </span>EXT-02</div>
          <div className="dai-cam-foot">
            <span>{lang === "es" ? "ENTORNO" : "SURROUND"}</span>
            <span className="dai-cam-foot-ok">2 {lang === "es" ? "PEAT" : "PED"}</span>
          </div>
        </div>

        {/* BOTTOM RIGHT: forward road — dashcam photo */}
        <div className="dai-cam-cell">
          <img src="assets/dashcam.gif" alt="Forward cam" style={{width:"100%",height:"100%",objectFit:"cover",display:"block",filter:"contrast(1.05) brightness(0.9)"}} />
          <svg viewBox="0 0 150 69" style={{position:"absolute",inset:0,width:"100%",height:"100%"}}>
            {/* vehicle detection box */}
            <rect x="60" y="28" width="35" height="26" fill="none" stroke="var(--accent)" strokeWidth="1.2" strokeDasharray="3 2">
              <animate attributeName="opacity" values="1;0.4;1" dur="1.9s" repeatCount="indefinite" />
            </rect>
            <text x="60" y="26" fontSize="6" fontFamily="var(--font-mono)" fill="var(--accent)">VEH 0.96</text>
            {/* traffic light box */}
            <rect x="118" y="8" width="20" height="28" fill="none" stroke="oklch(0.82 0.18 130)" strokeWidth="1" strokeDasharray="2 2">
              <animate attributeName="opacity" values="1;0.5;1" dur="2.5s" repeatCount="indefinite" />
            </rect>
            <text x="118" y="6" fontSize="6" fontFamily="var(--font-mono)" fill="oklch(0.82 0.18 130)">{lang === "es" ? "SEMÁF" : "LIGHT"}</text>
            {/* distance indicator */}
            <rect x="5" y="58" width="140" height="9" fill="oklch(0.10 0.03 240 / 0.6)" />
            <text x="10" y="65" fontSize="6" fontFamily="var(--font-mono)" fill="var(--bone)">{lang === "es" ? "DISTANCIA" : "DISTANCE"}: 18m · OK</text>
          </svg>
          <div className="dai-cam-label"><span className="dai-cam-label-rec">● </span>FWD-03</div>
        </div>
      </div>
    );
  }
  if (kind === "score") {
    const isEs = lang === "es";
    const EVENTS = [
      { type: "good",    delta: "+3", label: isEs ? "Frenado suave"             : "Smooth braking"          },
      { type: "warn",    delta: "−4", label: isEs ? "Giro brusco"               : "Hard turn"               },
      { type: "good",    delta: "+2", label: isEs ? "Llegada a tiempo"           : "On-time arrival"         },
      { type: "warn",    delta: "−6", label: isEs ? "Exceso de velocidad"        : "Speeding detected"       },
      { type: "good",    delta: "+3", label: isEs ? "Distancia segura"           : "Safe following distance" },
      { type: "warn",    delta: "−5", label: isEs ? "Uso de teléfono"            : "Phone usage detected"    },
      { type: "good",    delta: "+2", label: isEs ? "Adherencia a la ruta"       : "Route adherence"        },
      { type: "neutral", delta: "○",  label: isEs ? "Parada registrada"          : "Stop registered"        },
      { type: "good",    delta: "+4", label: isEs ? "Velocidad correcta"         : "Correct speed"          },
      { type: "warn",    delta: "−3", label: isEs ? "Frenado tardío"             : "Late braking"           },
    ];
    const ScoreEventFeed = () => {
      const [visible, setVisible] = useState(3);
      const [score, setScore] = useState(87);
      useEffect(() => {
        if (visible >= EVENTS.length) {
          const rst = setTimeout(() => { setVisible(3); setScore(87); }, 1800);
          return () => clearTimeout(rst);
        }
        const ev = EVENTS[visible];
        const id = setTimeout(() => {
          setScore(s => Math.min(100, Math.max(0, s + (ev.type === "good" ? parseInt(ev.delta) : ev.type === "warn" ? parseInt(ev.delta.replace("−","-")) : 0))));
          setVisible(v => v + 1);
        }, 950);
        return () => clearTimeout(id);
      }, [visible]);
      const shown = EVENTS.slice(0, visible).reverse();
      const typeColor = { good: "oklch(0.62 0.18 145)", warn: "oklch(0.65 0.18 35)", neutral: "oklch(0.55 0.06 240)" };
      const typeBg   = { good: "oklch(0.94 0.06 145)", warn: "oklch(0.96 0.06 35)",  neutral: "oklch(0.93 0.02 240)" };
      const gaugeArc = 188;
      const fillRatio = score / 100;
      const dashOffset = gaugeArc - fillRatio * gaugeArc;
      const gaugeColor = score >= 80 ? "oklch(0.62 0.18 145)" : score >= 60 ? "oklch(0.75 0.18 75)" : "oklch(0.65 0.18 35)";
      return (
        <div style={{ display:"grid", gridTemplateColumns:"1fr 1fr", height:"100%", background:"var(--viz-bg, oklch(0.96 0.008 240))", borderRadius:6, overflow:"hidden" }}>
          <style>{`
            .dai-score-gauge { display:flex; flex-direction:column; align-items:center; justify-content:center; padding:12px 8px; gap:10px; border-right:1px solid var(--line-soft); }
            .dai-score-bars  { display:flex; align-items:flex-end; gap:3px; height:44px; }
            .dai-score-bar-wrap { display:flex; flex-direction:column; align-items:center; gap:3px; }
            .dai-score-bar-bg { background:var(--ink-3); border-radius:2px; position:relative; overflow:hidden; }
            .dai-score-bar-fill { position:absolute; bottom:0; left:0; right:0; border-radius:2px; transition: height .5s cubic-bezier(.4,0,.2,1); }
            .dai-score-bar-lbl { font-family:var(--font-mono); font-size:6px; color:var(--mist); letter-spacing:.04em; }
            .dai-ev-feed { display:flex; flex-direction:column; gap:0; padding:8px 6px; overflow:hidden; }
            .dai-ev-head { font-family:var(--font-mono); font-size:7px; letter-spacing:.12em; color:var(--mist); padding-bottom:5px; border-bottom:1px solid var(--line-soft); margin-bottom:5px; display:flex; justify-content:space-between; }
            .dai-ev-row { display:flex; align-items:center; gap:5px; padding:3px 0; font-size:11px; animation: daiFadeIn .3s ease; }
            @keyframes daiFadeIn { from { opacity:0; transform:translateY(-6px); } to { opacity:1; transform:translateY(0); } }
            .dai-ev-delta { font-family:var(--font-mono); font-size:9px; font-weight:600; padding:1px 5px; border-radius:999px; flex-shrink:0; min-width:26px; text-align:center; }
            .dai-ev-label { font-size:10px; color:var(--ink); line-height:1.2; flex:1; }
            .dai-ev-dot { width:5px; height:5px; border-radius:50%; flex-shrink:0; }
          `}</style>
          {/* LEFT: gauge */}
          <div className="dai-score-gauge">
            <svg viewBox="0 0 120 70" style={{ width:110, overflow:"visible" }}>
              <path d="M 10 65 A 50 50 0 0 1 110 65" fill="none" stroke="var(--ink-3)" strokeWidth="7" strokeLinecap="round" />
              <path d="M 10 65 A 50 50 0 0 1 110 65" fill="none" stroke={gaugeColor} strokeWidth="7" strokeLinecap="round"
                strokeDasharray={gaugeArc} strokeDashoffset={dashOffset}
                style={{ transition:"stroke-dashoffset .6s cubic-bezier(.4,0,.2,1), stroke .4s" }} />
              <text x="60" y="56" textAnchor="middle" fontSize="22" fontFamily="var(--font-display)" fontWeight="500" fill="var(--ink)"
                style={{ transition:"all .4s" }}>{score}</text>
              <text x="60" y="69" textAnchor="middle" fontSize="7.5" fontFamily="var(--font-mono)" fill="var(--mist)">{L.daiScore}</text>
            </svg>
            {/* mini bars */}
            <div className="dai-score-bars">
              {[
                { label: L.daiBars[0], h: 44, fill: 78 },
                { label: L.daiBars[1], h: 44, fill: 55 },
                { label: L.daiBars[2], h: 44, fill: 90 },
                { label: L.daiBars[3], h: 44, fill: 66 },
              ].map((b, i) => (
                <div key={i} className="dai-score-bar-wrap">
                  <div className="dai-score-bar-bg" style={{ width:12, height:b.h }}>
                    <div className="dai-score-bar-fill" style={{ height:`${b.fill}%`, background:"var(--accent)" }} />
                  </div>
                  <div className="dai-score-bar-lbl">{b.label}</div>
                </div>
              ))}
            </div>
          </div>
          {/* RIGHT: indicator stats — auto-scroll through all */}
          <div style={{ overflow:"hidden", height:"100%", position:"relative" }}>
            <style>{`
              @keyframes daiScroll {
                0%   { transform: translateY(0); }
                100% { transform: translateY(-50%); }
              }
              .dai-indicators-track {
                display: flex; flex-direction: column; gap: 6px;
                padding: 10px 10px;
                animation: daiScroll 6s linear infinite;
              }
              .dai-ind-row {
                display: flex; align-items: center; gap: 7px;
                background: var(--ink-2); border-radius: 5px; padding: 5px 8px;
                flex-shrink: 0;
              }
            `}</style>
            <div className="dai-indicators-track">
              {[
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>, label: isEs?"Score global":"Global score", value:`${score}`, unit:"/100", color: gaugeColor },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>, label: isEs?"Horas conducidas":"Hours driven", value:"38.4", unit:"h", color:"var(--accent)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M3 12h18M3 6h18M3 18h18"/></svg>, label: isEs?"Km conducidos":"Km driven", value:"412", unit:"km", color:"var(--accent)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/></svg>, label: isEs?"Eventos bruscos":"Harsh events", value:"3", unit:"", color:"oklch(0.65 0.18 35)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>, label: isEs?"Paradas completadas":"Stops completed", value:"142", unit:"", color:"oklch(0.58 0.18 145)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3H14z"/></svg>, label: isEs?"CSAT clientes":"Customer CSAT", value:"4.8", unit:"/5", color:"oklch(0.58 0.18 145)" },
              ].map((ind, i) => (
                <div key={i} className="dai-ind-row">
                  <span style={{ color: ind.color, flexShrink:0 }}>{ind.icon}</span>
                  <div style={{ flex:1, minWidth:0 }}>
                    <div style={{ fontFamily:"var(--font-mono)", fontSize:6.5, color:"var(--mist)", letterSpacing:".08em", textTransform:"uppercase", marginBottom:1 }}>{ind.label}</div>
                    <div style={{ fontFamily:"var(--font-mono)", fontSize:12, fontWeight:600, color: ind.color, lineHeight:1 }}>
                      {ind.value}<span style={{ fontSize:8, fontWeight:400, color:"var(--mist)", marginLeft:1 }}>{ind.unit}</span>
                    </div>
                  </div>
                </div>
              ))}
              {/* duplicate for seamless loop */}
              {[
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/></svg>, label: isEs?"Score global":"Global score", value:`${score}`, unit:"/100", color: gaugeColor },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><circle cx="12" cy="12" r="10"/><polyline points="12 6 12 12 16 14"/></svg>, label: isEs?"Horas conducidas":"Hours driven", value:"38.4", unit:"h", color:"var(--accent)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M3 12h18M3 6h18M3 18h18"/></svg>, label: isEs?"Km conducidos":"Km driven", value:"412", unit:"km", color:"var(--accent)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M13 2L3 14h9l-1 8 10-12h-9l1-8z"/></svg>, label: isEs?"Eventos bruscos":"Harsh events", value:"3", unit:"", color:"oklch(0.65 0.18 35)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/><polyline points="22 4 12 14.01 9 11.01"/></svg>, label: isEs?"Paradas completadas":"Stops completed", value:"142", unit:"", color:"oklch(0.58 0.18 145)" },
                { icon:<svg width="11" height="11" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round"><path d="M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3H14z"/></svg>, label: isEs?"CSAT clientes":"Customer CSAT", value:"4.8", unit:"/5", color:"oklch(0.58 0.18 145)" },
              ].map((ind, i) => (
                <div key={`d-${i}`} className="dai-ind-row">
                  <span style={{ color: ind.color, flexShrink:0 }}>{ind.icon}</span>
                  <div style={{ flex:1, minWidth:0 }}>
                    <div style={{ fontFamily:"var(--font-mono)", fontSize:6.5, color:"var(--mist)", letterSpacing:".08em", textTransform:"uppercase", marginBottom:1 }}>{ind.label}</div>
                    <div style={{ fontFamily:"var(--font-mono)", fontSize:12, fontWeight:600, color: ind.color, lineHeight:1 }}>
                      {ind.value}<span style={{ fontSize:8, fontWeight:400, color:"var(--mist)", marginLeft:1 }}>{ind.unit}</span>
                    </div>
                  </div>
                </div>
              ))}
            </div>
          </div>
        </div>
      );
    };
    return <div className="dai-viz" style={{ padding:0 }}><ScoreEventFeed /></div>;
  }
  if (kind === "coaching") {
    const isEs = lang === "es";
    return (
      <div className="dai-viz" style={{ padding:0, display:"grid", gridTemplateColumns:"1fr 1fr", height:"100%", background:"var(--viz-bg, oklch(0.96 0.008 240))", overflow:"hidden" }}>
        {/* LEFT: animated PDF sliding in */}
        <div style={{ position:"relative", borderRight:"1px solid var(--line-soft)" }}>
          <svg viewBox="0 0 150 140" style={{ width:"100%", height:"100%" }}>
            <rect width="150" height="140" fill="var(--viz-bg, oklch(0.96 0.008 240))" />
            <text x="10" y="14" fontSize="7" fontFamily="var(--font-mono)" fill="var(--mist)">{L.daiPdf}</text>
            {/* PDF doc animating in */}
            <g>
              <rect x="20" y="22" width="110" height="110" fill="var(--bone)" stroke="var(--line)" strokeWidth="1">
                <animateTransform attributeName="transform" type="translate" values="0 18;0 0;0 0;0 18" keyTimes="0;0.2;0.85;1" dur="5s" repeatCount="indefinite" />
                <animate attributeName="opacity" values="0;1;1;0" keyTimes="0;0.2;0.85;1" dur="5s" repeatCount="indefinite" />
              </rect>
              {/* header bar */}
              <rect x="30" y="30" width="50" height="5" fill="var(--accent)">
                <animate attributeName="width" values="0;50" keyTimes="0;0.3" dur="5s" repeatCount="indefinite" />
              </rect>
              {/* text lines */}
              {[42, 52, 62, 74, 84, 96, 108].map((y, i) => (
                <rect key={i} x="30" y={y} width={i % 2 ? 75 : 90} height="3" fill="var(--ink-3)">
                  <animate attributeName="width" values={`0;${i % 2 ? 75 : 90}`} keyTimes={`0;${0.3 + i*0.07}`} dur="5s" repeatCount="indefinite" />
                </rect>
              ))}
              {/* chart area */}
              <polyline points="30,118 48,110 66,113 84,104 102,106 120,98" fill="none" stroke="var(--accent)" strokeWidth="1.4" strokeLinecap="round">
                <animate attributeName="opacity" values="0;0;1;1;0" keyTimes="0;0.5;0.65;0.85;1" dur="5s" repeatCount="indefinite" />
              </polyline>
              {/* CTA bar */}
              <rect x="30" y="124" width="40" height="6" rx="1" fill="var(--accent)" opacity="0.3">
                <animate attributeName="opacity" values="0;0.3" keyTimes="0;0.75" dur="5s" repeatCount="indefinite" />
              </rect>
            </g>
          </svg>
        </div>

        {/* RIGHT: what goes in the PDF */}
        <div style={{ display:"flex", flexDirection:"column", gap:5, padding:"8px 10px", overflowY:"hidden" }}>
          <div style={{ fontFamily:"var(--font-mono)", fontSize:6.5, color:"var(--mist)", letterSpacing:".10em", marginBottom:2 }}>
            {isEs ? "CONTENIDO DEL INFORME" : "REPORT CONTENTS"}
          </div>
          {[
            { icon:"◆", label: isEs?"Score semanal":"Weekly score",      value:"87/100", color:"var(--accent)" },
            { icon:"▲", label: isEs?"Top 3 eventos":"Top 3 events",      value:"3 clips", color:"oklch(0.65 0.18 35)" },
            { icon:"→", label: isEs?"Km conducidos":"Km driven",          value:"412 km", color:"var(--fog)" },
            { icon:"◎", label: isEs?"Frenadas bruscas":"Hard braking",    value:"2×", color:"oklch(0.65 0.18 35)" },
            { icon:"◉", label: isEs?"Objetivo personal":"Personal goal",  value:"↑ foco", color:"oklch(0.58 0.18 145)" },
            { icon:"✦", label: isEs?"Tendencia 4 sem":"4-week trend",     value:"+5 pts", color:"oklch(0.58 0.18 145)" },
          ].map((item, i) => (
            <div key={i} style={{ display:"flex", alignItems:"center", gap:6, background:"var(--ink-2)", borderRadius:4, padding:"4px 7px" }}>
              <span style={{ fontFamily:"var(--font-mono)", fontSize:9, color: item.color, flexShrink:0, width:10, textAlign:"center" }}>{item.icon}</span>
              <div style={{ flex:1, minWidth:0 }}>
                <div style={{ fontFamily:"var(--font-mono)", fontSize:6.5, color:"var(--mist)", letterSpacing:".06em", marginBottom:1 }}>{item.label}</div>
                <div style={{ fontFamily:"var(--font-mono)", fontSize:10, fontWeight:600, color: item.color }}>{item.value}</div>
              </div>
            </div>
          ))}
        </div>
      </div>
    );
  }
  if (kind === "evidence") {
    // Real dashcam GIF with HUD overlay
    return (
      <div className="dai-viz" style={{ padding:0, position:"relative", overflow:"hidden", background:"#000" }}>
        <img src="assets/dashcam.gif" alt="dashcam" style={{ width:"100%", height:"115%", objectFit:"cover", display:"block", opacity:0.88, marginTop:"-7.5%" }} />
        {/* HUD overlay */}
        <svg style={{ position:"absolute", inset:0, width:"100%", height:"100%", pointerEvents:"none" }} viewBox="0 0 300 140" preserveAspectRatio="none">
          {/* REC indicator */}
          <circle cx="30" cy="14" r="3.5" fill="oklch(0.68 0.22 25)">
            <animate attributeName="opacity" values="1;0.2;1" dur="1s" repeatCount="indefinite" />
          </circle>
          <text x="37" y="18" fontSize="7.5" fontFamily="var(--font-mono)" fill="var(--bone)">{L.rec}</text>
          {/* timecode */}
          <rect x="0" y="124" width="300" height="16" fill="oklch(0.08 0.03 240 / 0.75)" />
          <text x="8" y="135" fontSize="7.5" fontFamily="var(--font-mono)" fill="var(--bone)">14:02:11.483</text>
          <text x="140" y="135" fontSize="7.5" fontFamily="var(--font-mono)" fill="oklch(0.7 0.18 30)">52→18 km/h · −0.82g</text>
          {/* hash badge */}
          <rect x="232" y="125" width="60" height="14" rx="2" fill="oklch(0.58 0.18 145 / 0.85)" />
          <text x="262" y="135" textAnchor="middle" fontSize="6.5" fontFamily="var(--font-mono)" fill="white">✓ SIGNED</text>
          {/* scanline */}
          <rect x="0" y="0" width="300" height="1.5" fill="var(--accent)" opacity="0.35">
            <animate attributeName="y" values="0;138;0" dur="4s" repeatCount="indefinite" />
          </rect>
        </svg>
      </div>
    );
  }
  return null;
};

window.GroppFlowComponents = { BrandMark, HeroMap, DemoMap, DemoPanel, FeatureViz, VehicleDiagram, DriverAiViz, MarqueeIcon };
