// Inline SVG icon library (lucide-style strokes, optimized to brand)
// usage: <Icon name="check" size={16} />
const ICON_PATHS = {
  // Nav
  'dashboard': <><rect x="3" y="3" width="7" height="9" rx="1" /><rect x="14" y="3" width="7" height="5" rx="1" /><rect x="14" y="12" width="7" height="9" rx="1" /><rect x="3" y="16" width="7" height="5" rx="1" /></>,
  'jobs': <><path d="M16 4h2a2 2 0 0 1 2 2v14a2 2 0 0 1-2 2H6a2 2 0 0 1-2-2V6a2 2 0 0 1 2-2h2" /><rect x="8" y="2" width="8" height="4" rx="1" /><path d="M9 12h6" /><path d="M9 16h6" /></>,
  'archive': <><rect x="3" y="3" width="18" height="4" rx="1" /><path d="M5 7v12a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V7" /><path d="M10 12h4" /></>,
  'admin': <><circle cx="12" cy="12" r="3" /><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 1 1-2.83 2.83l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 1 1-4 0v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 1 1-2.83-2.83l.06-.06a1.65 1.65 0 0 0 .33-1.82 1.65 1.65 0 0 0-1.51-1H3a2 2 0 1 1 0-4h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 1 1 2.83-2.83l.06.06a1.65 1.65 0 0 0 1.82.33H9a1.65 1.65 0 0 0 1-1.51V3a2 2 0 1 1 4 0v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 1 1 2.83 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 1 1 0 4h-.09a1.65 1.65 0 0 0-1.51 1z" /></>,
  'equipment': <><path d="M3 9l9-6 9 6v9a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z" style={{ display: 'none' }} /><circle cx="12" cy="12" r="3" /><path d="M12 1v3M12 20v3M4.22 4.22l2.12 2.12M17.66 17.66l2.12 2.12M1 12h3M20 12h3M4.22 19.78l2.12-2.12M17.66 6.34l2.12-2.12" /></>,
  'users': <><path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2" /><circle cx="9" cy="7" r="4" /><path d="M23 21v-2a4 4 0 0 0-3-3.87" /><path d="M16 3.13a4 4 0 0 1 0 7.75" /></>,
  'snippet': <><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" /><path d="M14 2v6h6" /><path d="M9 13h6M9 17h6" /></>,
  'sliders': <><line x1="4" y1="21" x2="4" y2="14" /><line x1="4" y1="10" x2="4" y2="3" /><line x1="12" y1="21" x2="12" y2="12" /><line x1="12" y1="8" x2="12" y2="3" /><line x1="20" y1="21" x2="20" y2="16" /><line x1="20" y1="12" x2="20" y2="3" /><line x1="1" y1="14" x2="7" y2="14" /><line x1="9" y1="8" x2="15" y2="8" /><line x1="17" y1="16" x2="23" y2="16" /></>,
  'layers': <><polygon points="12 2 2 7 12 12 22 7 12 2" /><polyline points="2 17 12 22 22 17" /><polyline points="2 12 12 17 22 12" /></>,
  'audit': <><circle cx="12" cy="12" r="10" /><polyline points="12 6 12 12 16 14" /></>,
  // Actions
  'plus': <><line x1="12" y1="5" x2="12" y2="19" /><line x1="5" y1="12" x2="19" y2="12" /></>,
  'search': <><circle cx="11" cy="11" r="7" /><line x1="21" y1="21" x2="16.65" y2="16.65" /></>,
  'filter': <><polygon points="22 3 2 3 10 12.46 10 19 14 21 14 12.46 22 3" /></>,
  'check': <><polyline points="20 6 9 17 4 12" /></>,
  'check-circle': <><circle cx="12" cy="12" r="10" /><polyline points="9 12 11 14 15 10" /></>,
  'x': <><line x1="18" y1="6" x2="6" y2="18" /><line x1="6" y1="6" x2="18" y2="18" /></>,
  'x-circle': <><circle cx="12" cy="12" r="10" /><line x1="15" y1="9" x2="9" y2="15" /><line x1="9" y1="9" x2="15" y2="15" /></>,
  'send': <><line x1="22" y1="2" x2="11" y2="13" /><polygon points="22 2 15 22 11 13 2 9 22 2" /></>,
  'download': <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /><polyline points="7 10 12 15 17 10" /><line x1="12" y1="15" x2="12" y2="3" /></>,
  'upload': <><path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" /><polyline points="17 8 12 3 7 8" /><line x1="12" y1="3" x2="12" y2="15" /></>,
  'eye': <><path d="M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z" /><circle cx="12" cy="12" r="3" /></>,
  'edit': <><path d="M12 20h9" /><path d="M16.5 3.5a2.121 2.121 0 0 1 3 3L7 19l-4 1 1-4z" /></>,
  'trash': <><polyline points="3 6 5 6 21 6" /><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2" /></>,
  'arrow-right': <><line x1="5" y1="12" x2="19" y2="12" /><polyline points="12 5 19 12 12 19" /></>,
  'arrow-left': <><line x1="19" y1="12" x2="5" y2="12" /><polyline points="12 19 5 12 12 5" /></>,
  'chevron-down': <><polyline points="6 9 12 15 18 9" /></>,
  'chevron-right': <><polyline points="9 18 15 12 9 6" /></>,
  'more': <><circle cx="12" cy="12" r="1" /><circle cx="19" cy="12" r="1" /><circle cx="5" cy="12" r="1" /></>,
  'bell': <><path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9" /><path d="M13.73 21a2 2 0 0 1-3.46 0" /></>,
  'help': <><circle cx="12" cy="12" r="10" /><path d="M9.09 9a3 3 0 0 1 5.83 1c0 2-3 3-3 3" /><line x1="12" y1="17" x2="12.01" y2="17" /></>,
  'pdf': <><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z" /><path d="M14 2v6h6" /><text x="6" y="18" fontSize="6" fill="currentColor" stroke="none" fontWeight="700">PDF</text></>,
  'image': <><rect x="3" y="3" width="18" height="18" rx="2" ry="2" /><circle cx="8.5" cy="8.5" r="1.5" /><polyline points="21 15 16 10 5 21" /></>,
  'paperclip': <><path d="M21.44 11.05l-9.19 9.19a6 6 0 0 1-8.49-8.49l9.19-9.19a4 4 0 0 1 5.66 5.66l-9.2 9.19a2 2 0 0 1-2.83-2.83l8.49-8.48" /></>,
  'lock': <><rect x="3" y="11" width="18" height="11" rx="2" ry="2" /><path d="M7 11V7a5 5 0 0 1 10 0v4" /></>,
  'alert': <><path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z" /><line x1="12" y1="9" x2="12" y2="13" /><line x1="12" y1="17" x2="12.01" y2="17" /></>,
  'info': <><circle cx="12" cy="12" r="10" /><line x1="12" y1="16" x2="12" y2="12" /><line x1="12" y1="8" x2="12.01" y2="8" /></>,
  'flag': <><path d="M4 15s1-1 4-1 5 2 8 2 4-1 4-1V3s-1 1-4 1-5-2-8-2-4 1-4 1z" /><line x1="4" y1="22" x2="4" y2="15" /></>,
  'reject': <><circle cx="12" cy="12" r="10" /><line x1="4.93" y1="4.93" x2="19.07" y2="19.07" /></>,
  'thumb-up': <><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.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3" /></>,
  'calendar': <><rect x="3" y="4" width="18" height="18" rx="2" ry="2" /><line x1="16" y1="2" x2="16" y2="6" /><line x1="8" y1="2" x2="8" y2="6" /><line x1="3" y1="10" x2="21" y2="10" /></>,
  'clock': <><circle cx="12" cy="12" r="10" /><polyline points="12 6 12 12 16 14" /></>,
  'package': <><line x1="16.5" y1="9.4" x2="7.5" y2="4.21" /><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z" /><polyline points="3.27 6.96 12 12.01 20.73 6.96" /><line x1="12" y1="22.08" x2="12" y2="12" /></>,
  'building': <><rect x="4" y="2" width="16" height="20" rx="1" /><path d="M9 22v-4h6v4" /><path d="M8 6h.01M16 6h.01M12 6h.01M12 10h.01M12 14h.01M16 10h.01M16 14h.01M8 10h.01M8 14h.01" /></>,
  'logout': <><path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4" /><polyline points="16 17 21 12 16 7" /><line x1="21" y1="12" x2="9" y2="12" /></>,
  'switch': <><polyline points="17 1 21 5 17 9" /><path d="M3 11V9a4 4 0 0 1 4-4h14" /><polyline points="7 23 3 19 7 15" /><path d="M21 13v2a4 4 0 0 1-4 4H3" /></>,
  'flask': <><path d="M9 2v6L4 18a2 2 0 0 0 2 3h12a2 2 0 0 0 2-3l-5-10V2" /><line x1="8" y1="2" x2="16" y2="2" /><line x1="6" y1="14" x2="18" y2="14" /></>,
  'play': <><polygon points="5 3 19 12 5 21 5 3" /></>,
  'pause': <><rect x="6" y="4" width="4" height="16" /><rect x="14" y="4" width="4" height="16" /></>,
  'spark': <><path d="M12 2L13.5 9.5 21 11 13.5 12.5 12 20 10.5 12.5 3 11 10.5 9.5z" /></>,
  'shield': <><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z" /></>,
  'circle': <><circle cx="12" cy="12" r="10" /></>,
  'rotate': <><polyline points="23 4 23 10 17 10" /><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10" /></>,
  'mail': <><path d="M4 4h16c1.1 0 2 .9 2 2v12c0 1.1-.9 2-2 2H4c-1.1 0-2-.9-2-2V6c0-1.1.9-2 2-2z" /><polyline points="22,6 12,13 2,6" /></>
};

function Icon({ name, size = 16, color, style, strokeWidth = 1.75, ...rest }) {
  const path = ICON_PATHS[name];
  if (!path) return <span style={{ display: 'inline-block', width: size, height: size }} />;
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
    stroke={color || 'currentColor'} strokeWidth={strokeWidth} strokeLinecap="round" strokeLinejoin="round"
    style={{ display: 'inline-block', flexShrink: 0, ...style }} {...rest}>
      {path}
    </svg>);

}

// Status pill
function StatusPill({ status }) {
  const map = {
    'Planned': { cls: 'planned' },
    'In Progress': { cls: 'inprogress' },
    'For Approval': { cls: 'forapproval' },
    'Rejected': { cls: 'rejected' },
    'Approved': { cls: 'approved' },
    'Complete': { cls: 'complete' }
  };
  const m = map[status] || { cls: 'planned' };
  return <span className={`pill ${m.cls}`}><span className="dot" />{status}</span>;
}

function OutcomePill({ outcome }) {
  if (!outcome) return <span className="outcome-pill notavailable">—</span>;
  const cls = outcome.toLowerCase().replace(/\s+/g, '');
  return <span className={`outcome-pill ${cls}`}>{outcome}</span>;
}

// Avatar
function Avatar({ user, size = 28 }) {
  if (!user) return null;
  return (
    <div style={{
      width: size, height: size, borderRadius: '50%',
      background: 'var(--light-blue)', color: 'var(--dark-blue)',
      display: 'grid', placeItems: 'center',
      fontWeight: 700, fontSize: Math.round(size * 0.38),
      flexShrink: 0
    }}>{user.initials}</div>);

}

// Synthetic inspection image — picks a "scene" by leading digit of label (group number)
// or by seed if no label. Scenes mimic the kind of imagery an AS6171A lab would produce.
function pickScene(label, seed) {
  const k = String(label || '').trim()[0];
  if (k === '1') return 'visual';      // External visual inspection — IC top-down marking
  if (k === '2') return 'xrf';          // XRF — spectrum
  if (k === '3') return 'reagent';      // Resistance to solvents — tray / close-up
  if (k === '4') return 'xray';         // X-ray — internals
  if (k === '5') return 'csam';         // CSAM — acoustic delam map
  if (k === '6') return 'decap';        // Decap — die surface
  if (k === '7') return 'electrical';   // Electrical — scope trace
  const scenes = ['visual','xray','csam','decap','xrf','electrical','reagent'];
  return scenes[seed % scenes.length];
}

function SceneVisual({ seed, label }) {
  // IC top-down with marking and leads
  const lead = (x, y) => <rect x={x} y={y} width="3.5" height="1.4" rx="0.3" fill="#cdd2d8" />;
  const leads = [];
  for (let i = 0; i < 8; i++) {
    leads.push(<g key={'l'+i}>{lead(11 + i * 9.3, 14.5)}{lead(11 + i * 9.3, 56.5)}</g>);
  }
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={`bg-v-${seed}`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#a8aeb5" /><stop offset="100%" stopColor="#787f87" />
        </linearGradient>
        <linearGradient id={`pkg-${seed}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#222428" /><stop offset="60%" stopColor="#0e0f12" /><stop offset="100%" stopColor="#1c1e22" />
        </linearGradient>
      </defs>
      <rect width="100" height="75" fill={`url(#bg-v-${seed})`} />
      {leads}
      <rect x="8" y="16" width="84" height="42" rx="2" fill={`url(#pkg-${seed})`} stroke="#0a0b0d" strokeWidth="0.3" />
      <circle cx="14" cy="22" r="1.4" fill="#3a3d44" />
      <text x="50" y="34" fontFamily="ui-monospace, Menlo, Consolas, monospace" fontSize="4.5" fontWeight="700" fill="#dfe2e7" textAnchor="middle">XLR{`${1240 + seed * 7}`}A</text>
      <text x="50" y="40" fontFamily="ui-monospace, Menlo, Consolas, monospace" fontSize="2.6" fill="#a9adb4" textAnchor="middle">DC{`${2244 + seed}`}  LOT {`${seed % 9}${(seed * 3) % 10}${seed % 7}A`}</text>
      <text x="50" y="46" fontFamily="ui-monospace, Menlo, Consolas, monospace" fontSize="2.4" fill="#888e95" textAnchor="middle">{`${seed % 5 + 1}H${(seed * 11) % 99}`}</text>
      <rect x="0" y="0" width="100" height="75" fill="none" stroke="rgba(255,255,255,0.06)" strokeWidth="0.4" />
    </svg>
  );
}

function SceneXray({ seed }) {
  const wires = [];
  for (let i = 0; i < 14; i++) {
    const a = (i / 14) * Math.PI * 2;
    const x1 = 50 + Math.cos(a) * 6, y1 = 37.5 + Math.sin(a) * 5;
    const x2 = 50 + Math.cos(a) * 22, y2 = 37.5 + Math.sin(a) * 18;
    wires.push(<line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke="rgba(220,230,255,0.55)" strokeWidth="0.35" />);
  }
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <defs>
        <radialGradient id={`xr-${seed}`} cx="50%" cy="50%" r="65%">
          <stop offset="0%" stopColor="#27313e" /><stop offset="100%" stopColor="#0a0f17" />
        </radialGradient>
      </defs>
      <rect width="100" height="75" fill={`url(#xr-${seed})`} />
      <rect x="20" y="14" width="60" height="47" rx="0.5" fill="none" stroke="rgba(180,200,230,0.25)" strokeWidth="0.4" />
      <rect x="38" y="28" width="24" height="19" rx="0.5" fill="rgba(255,255,255,0.05)" stroke="rgba(220,235,255,0.45)" strokeWidth="0.35" />
      <rect x="44" y="33" width="12" height="9" fill="rgba(255,255,255,0.10)" />
      {wires}
      {Array.from({ length: 28 }).map((_, i) => (
        <rect key={i} x={22 + (i % 14) * 4} y={i < 14 ? 14 : 59} width="2.6" height="2" fill="rgba(220,230,255,0.35)" />
      ))}
      <text x="3" y="72" fontFamily="ui-monospace" fontSize="2.2" fill="rgba(255,255,255,0.45)">120 kV · 65 µA · 8.4×</text>
    </svg>
  );
}

function SceneCsam({ seed }) {
  // Acoustic heatmap with random delam blobs
  const blobs = Array.from({ length: 6 }, (_, i) => ({
    cx: 18 + ((seed * 13 + i * 17) % 64),
    cy: 12 + ((seed * 7 + i * 11) % 50),
    r: 3 + ((seed + i) % 5),
    o: 0.45 + ((i * 0.13) % 0.4),
  }));
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={`csb-${seed}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#10243a" /><stop offset="100%" stopColor="#06101a" />
        </linearGradient>
      </defs>
      <rect width="100" height="75" fill={`url(#csb-${seed})`} />
      <rect x="6" y="6" width="88" height="63" fill="none" stroke="rgba(120,200,255,0.25)" strokeWidth="0.3" />
      {blobs.map((b, i) => (
        <g key={i}>
          <circle cx={b.cx} cy={b.cy} r={b.r * 1.6} fill={i % 2 ? `rgba(255,120,80,${b.o * 0.45})` : `rgba(120,200,255,${b.o * 0.45})`} />
          <circle cx={b.cx} cy={b.cy} r={b.r} fill={i % 2 ? `rgba(255,200,80,${b.o})` : `rgba(160,220,255,${b.o})`} />
        </g>
      ))}
      {Array.from({ length: 10 }).map((_, i) => (
        <line key={i} x1="6" y1={6 + i * 6.3} x2="94" y2={6 + i * 6.3} stroke="rgba(255,255,255,0.04)" strokeWidth="0.2" />
      ))}
      <text x="3" y="72" fontFamily="ui-monospace" fontSize="2.2" fill="rgba(160,220,255,0.65)">230 MHz · die attach</text>
    </svg>
  );
}

function SceneDecap({ seed }) {
  // Decapped die — bond pads around edge, interior circuit blocks
  const pads = [];
  for (let i = 0; i < 14; i++) pads.push(<rect key={'a'+i} x={18 + i * 4.7} y="14" width="2" height="2.6" fill="#d8c27a" />, <rect key={'b'+i} x={18 + i * 4.7} y="58.5" width="2" height="2.6" fill="#d8c27a" />);
  for (let i = 0; i < 10; i++) pads.push(<rect key={'c'+i} x="14.5" y={20 + i * 4} width="2.6" height="2" fill="#d8c27a" />, <rect key={'d'+i} x="82.7" y={20 + i * 4} width="2.6" height="2" fill="#d8c27a" />);
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={`die-${seed}`} x1="0" y1="0" x2="1" y2="1">
          <stop offset="0%" stopColor="#5c6478" /><stop offset="100%" stopColor="#383f50" />
        </linearGradient>
      </defs>
      <rect width="100" height="75" fill="#1a1d24" />
      <rect x="12" y="12" width="76" height="51" rx="0.5" fill={`url(#die-${seed})`} stroke="#9aa1b0" strokeWidth="0.3" />
      {pads}
      {Array.from({ length: 6 }).map((_, i) => (
        <rect key={i} x={22 + (i % 3) * 22} y={22 + Math.floor(i / 3) * 18} width="18" height="14" fill="rgba(255,255,255,0.06)" stroke="rgba(255,255,255,0.18)" strokeWidth="0.2" />
      ))}
      {Array.from({ length: 30 }).map((_, i) => (
        <line key={i} x1={22 + (i % 10) * 5.5} y1="22" x2={22 + (i % 10) * 5.5} y2="36" stroke="rgba(255,255,255,0.06)" strokeWidth="0.15" />
      ))}
      <text x="50" y="50" fontFamily="ui-monospace" fontSize="2.4" fill="rgba(255,255,255,0.55)" textAnchor="middle">M{seed % 9}{(seed * 3) % 10}{seed % 7}-A</text>
    </svg>
  );
}

function SceneXrf({ seed }) {
  // Spectrum chart
  const peaks = [
    { x: 15, h: 38 + (seed * 3) % 12, l: 'Sn' },
    { x: 32, h: 22 + (seed * 5) % 10, l: 'Cu' },
    { x: 48, h: 48 + (seed * 7) % 8, l: 'Pb' },
    { x: 64, h: 14 + (seed * 11) % 10, l: 'Ni' },
    { x: 80, h: 28 + (seed * 13) % 10, l: 'Ag' },
  ];
  let path = 'M5,62';
  for (let x = 5; x <= 95; x += 1.5) {
    let y = 62;
    peaks.forEach((p) => { const d = (x - p.x); y -= p.h * Math.exp(-(d * d) / 4); });
    y += (Math.sin(x * 1.7 + seed) * 0.8);
    path += ` L${x.toFixed(1)},${y.toFixed(1)}`;
  }
  path += ' L95,62 Z';
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <rect width="100" height="75" fill="#fafbfd" />
      {Array.from({ length: 8 }).map((_, i) => (
        <line key={i} x1="5" y1={10 + i * 6.5} x2="95" y2={10 + i * 6.5} stroke="#e3e8ec" strokeWidth="0.2" />
      ))}
      <path d={path} fill="rgba(0,173,239,0.18)" stroke="#00ADEF" strokeWidth="0.5" strokeLinejoin="round" />
      {peaks.map((p, i) => (
        <g key={i}>
          <line x1={p.x} y1="62" x2={p.x} y2={62 - p.h - 2} stroke="rgba(35,38,86,0.25)" strokeWidth="0.2" strokeDasharray="0.5 0.5" />
          <text x={p.x} y={62 - p.h - 4} fontFamily="ui-monospace" fontSize="2.6" fill="#232656" textAnchor="middle" fontWeight="700">{p.l}</text>
        </g>
      ))}
      <line x1="5" y1="62" x2="95" y2="62" stroke="#232656" strokeWidth="0.4" />
      <text x="5" y="71" fontFamily="ui-monospace" fontSize="2.4" fill="#5F727F">0 keV</text>
      <text x="95" y="71" fontFamily="ui-monospace" fontSize="2.4" fill="#5F727F" textAnchor="end">25 keV</text>
      <text x="50" y="9" fontFamily="ui-monospace" fontSize="2.6" fill="#232656" textAnchor="middle" fontWeight="700">XRF SPECTRUM · 60 s · 50 kV</text>
    </svg>
  );
}

function SceneElectrical({ seed }) {
  // Oscilloscope-style trace
  let p1 = 'M5,40', p2 = 'M5,52';
  for (let x = 5; x <= 95; x += 1) {
    const y1 = 40 + Math.sin((x + seed * 5) * 0.35) * 14 + Math.sin(x * 0.9) * 2;
    const y2 = 52 + Math.cos((x + seed * 7) * 0.22) * 6;
    p1 += ` L${x},${y1.toFixed(1)}`;
    p2 += ` L${x},${y2.toFixed(1)}`;
  }
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <rect width="100" height="75" fill="#070b10" />
      {Array.from({ length: 10 }).map((_, i) => (
        <line key={'h'+i} x1="5" y1={8 + i * 6.5} x2="95" y2={8 + i * 6.5} stroke="rgba(80,200,120,0.10)" strokeWidth="0.2" />
      ))}
      {Array.from({ length: 12 }).map((_, i) => (
        <line key={'v'+i} x1={5 + i * 7.5} y1="5" x2={5 + i * 7.5} y2="70" stroke="rgba(80,200,120,0.10)" strokeWidth="0.2" />
      ))}
      <path d={p1} fill="none" stroke="#7CFFB2" strokeWidth="0.5" />
      <path d={p2} fill="none" stroke="#FFD37A" strokeWidth="0.5" />
      <text x="6" y="11" fontFamily="ui-monospace" fontSize="2.4" fill="#7CFFB2">CH1 2.00V</text>
      <text x="6" y="71" fontFamily="ui-monospace" fontSize="2.4" fill="#FFD37A">CH2 1.00V  TB 200ns</text>
    </svg>
  );
}

function SceneReagent({ seed }) {
  // Lab bench: vials + tray of devices
  const vials = [
    { x: 14, c: '#bcd9c9' }, { x: 28, c: '#e7c98a' }, { x: 42, c: '#bcd3ea' },
  ];
  const devices = [];
  for (let i = 0; i < 10; i++) {
    const col = i % 5, row = Math.floor(i / 5);
    devices.push(<g key={i} transform={`translate(${58 + col * 7},${22 + row * 18})`}>
      <rect x="0" y="0" width="5.5" height="11" rx="0.5" fill="#222428" />
      <rect x="0.5" y="3" width="4.5" height="0.6" fill="#dfe2e7" />
      <rect x="0.5" y="5" width="3" height="0.4" fill="#a9adb4" />
    </g>);
  }
  return (
    <svg viewBox="0 0 100 75" preserveAspectRatio="xMidYMid slice" style={{ position: 'absolute', inset: 0, width: '100%', height: '100%' }}>
      <defs>
        <linearGradient id={`bench-${seed}`} x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#e8ecef" /><stop offset="100%" stopColor="#c4cad0" />
        </linearGradient>
      </defs>
      <rect width="100" height="75" fill={`url(#bench-${seed})`} />
      {/* tray */}
      <rect x="55" y="16" width="40" height="46" rx="1" fill="#3a3f48" />
      <rect x="56.5" y="17.5" width="37" height="43" rx="0.5" fill="#1f232a" />
      {devices}
      {/* vials */}
      {vials.map((v, i) => (
        <g key={i} transform={`translate(${v.x}, 22)`}>
          <rect x="-4" y="0" width="8" height="3" fill="#9aa1b0" />
          <rect x="-3.5" y="3" width="7" height="22" rx="0.5" fill="rgba(255,255,255,0.85)" stroke="#888e95" strokeWidth="0.2" />
          <rect x="-3" y="11" width="6" height="13" fill={v.c} opacity="0.85" />
          <text x="0" y="32" fontFamily="ui-monospace" fontSize="2.4" textAnchor="middle" fill="#232656" fontWeight="700">R{i + 1}</text>
        </g>
      ))}
    </svg>
  );
}

function SyntheticImage({ seed = 0, label, children, style, className }) {
  const scene = pickScene(label, seed);
  const Scene = ({
    visual: SceneVisual, xray: SceneXray, csam: SceneCsam, decap: SceneDecap,
    xrf: SceneXrf, electrical: SceneElectrical, reagent: SceneReagent,
  })[scene];
  return (
    <div className={className} style={{ position: 'relative', background: '#0e1014', overflow: 'hidden', ...style }}>
      <Scene seed={seed} label={label} />
      {/* subtle vignette */}
      <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none', background: 'radial-gradient(ellipse at center, transparent 55%, rgba(0,0,0,0.35) 100%)' }} />
      {/* corner label badge */}
      {label && (
        <div style={{
          position: 'absolute', left: 8, bottom: 8,
          fontFamily: 'ui-monospace, Menlo, Consolas, monospace',
          fontSize: 10, fontWeight: 700, letterSpacing: '0.04em',
          padding: '2px 7px',
          background: 'rgba(0,0,0,0.55)',
          color: '#fff',
          borderRadius: 3,
          border: '1px solid rgba(255,255,255,0.15)',
          backdropFilter: 'blur(2px)',
        }}>Fig. {label}</div>
      )}
      {children}
    </div>);

}

window.Icon = Icon;
window.StatusPill = StatusPill;
window.OutcomePill = OutcomePill;
window.Avatar = Avatar;
window.SyntheticImage = SyntheticImage;