/* global React */

// ─────────────────────────────────────────────────────────────────
// Hero machine alternatives — 5 visuals that better fit the claim
// "Research that closes the questions that matter."
// All in the locked Codex × Comic spec (2.6/1.8/0.5–0.9 strokes, 35°
// hatching, copper accents, ochre starburst, mono-set annotation).
//
//   1. LENS        — Spectroscope. RESEARCH / OBSERVATION.
//   2. CIPHER      — Codex with decoded sigils + key. INTELLIGENCE.
//   3. CONSTELLATION — Star chart with connecting plot lines. ANALYTICS.
//   4. LOOM        — Threads from many sources woven into one fabric. SYNTHESIS.
//   5. CITADEL     — Glyph-stamped block with telemetry rings. TECHNICAL EDGE.
// ─────────────────────────────────────────────────────────────────

const HMDefs = ({ id }) => (
  <defs>
    <pattern id={`hm-hatch-${id}`} width="3" height="3" patternUnits="userSpaceOnUse" patternTransform="rotate(35)">
      <line x1="0" y1="0" x2="0" y2="3" stroke="#2A1810" strokeWidth="0.6" opacity="0.85" />
    </pattern>
    <pattern id={`hm-hatch-fine-${id}`} width="2" height="2" patternUnits="userSpaceOnUse" patternTransform="rotate(35)">
      <line x1="0" y1="0" x2="0" y2="2" stroke="#2A1810" strokeWidth="0.4" opacity="0.6" />
    </pattern>
    <radialGradient id={`hm-glow-${id}`} cx="50%" cy="50%" r="50%">
      <stop offset="0%" stopColor="#FFC47C" stopOpacity="1"/>
      <stop offset="55%" stopColor="#D97757" stopOpacity="0.85"/>
      <stop offset="100%" stopColor="#8B3A1F" stopOpacity="0.4"/>
    </radialGradient>
  </defs>
);

// ── Comic burst (matches Machines.jsx ComicBurst signature) ──
const HMBurst = ({ x, y, r, text, rot=-12, points=12, size=12 }) => {
  const path = [];
  for (let i = 0; i < points*2; i++) {
    const ang = (i / (points*2)) * Math.PI * 2;
    const rr = i % 2 === 0 ? r : r * 0.66;
    path.push(`${i===0?'M':'L'} ${Math.cos(ang)*rr} ${Math.sin(ang)*rr}`);
  }
  path.push("Z");
  return (
    <g transform={`translate(${x}, ${y}) rotate(${rot})`} className="machine-burst">
      <path d={path.join(" ")} fill="#FFC47C"/>
      <path d={path.join(" ")} stroke="#2A1810" strokeWidth="2" fill="none" strokeLinejoin="round"/>
      <text textAnchor="middle" y="4" fontFamily="GT Sectra, Source Serif 4, serif" fontStyle="italic" fontWeight="700" fontSize={size} fill="#2A1810">{text}</text>
    </g>
  );
};

// Mono callout (label) helper
const HMLabel = ({ x, y, ax, ay, text, anchor='start' }) => (
  <g>
    <line x1={ax} y1={ay} x2={x} y2={y} stroke="#8B3A1F" strokeWidth="1.6" strokeLinecap="round"/>
    <circle cx={ax} cy={ay} r="2.2" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.5"/>
    <text x={x + (anchor==='end' ? -4 : 4)} y={y+2.5} fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" textAnchor={anchor} letterSpacing="0.1em">{text}</text>
  </g>
);

// ─────────────────────────────────────────────────────
// 1. THE LENS — Spectroscope on tripod, splitting one beam into spectrum.
//    Metaphor: research = decomposing a question into evidence.
// ─────────────────────────────────────────────────────
const LensMachine = () => (
  <svg viewBox="0 0 400 320" preserveAspectRatio="xMidYMid meet" width="100%" style={{maxHeight:340}}>
    <HMDefs id="lens"/>
    {/* action lines */}
    <g stroke="#4A3520" strokeWidth="0.9" opacity="0.45" strokeLinecap="round">
      {[10,25,40,160,175,200,330,345,355].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={200 + Math.cos(rad)*100} y1={150 + Math.sin(rad)*100} x2={200 + Math.cos(rad)*150} y2={150 + Math.sin(rad)*150}/>;
      })}
    </g>
    {/* tripod */}
    <line x1="170" y1="225" x2="120" y2="285" stroke="#1A0F08" strokeWidth="2.6" strokeLinecap="round"/>
    <line x1="200" y1="225" x2="200" y2="285" stroke="#1A0F08" strokeWidth="2.6" strokeLinecap="round"/>
    <line x1="230" y1="225" x2="280" y2="285" stroke="#1A0F08" strokeWidth="2.6" strokeLinecap="round"/>
    {[120,200,280].map(x => (
      <g key={x}>
        <rect x={x-8} y="285" width="16" height="6" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2.6"/>
        <circle cx={x} cy="291" r="2" fill="#2A1810"/>
      </g>
    ))}
    {/* tripod head */}
    <ellipse cx="200" cy="225" rx="50" ry="9" fill="#D8C8A8"/>
    <ellipse cx="200" cy="225" rx="50" ry="9" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    <ellipse cx="200" cy="225" rx="50" ry="9" fill="url(#hm-hatch-lens)" opacity="0.55"/>
    {[160,180,200,220,240].map(x => <circle key={x} cx={x} cy="225" r="2" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.5"/>)}
    {/* barrel */}
    <rect x="135" y="125" width="210" height="42" fill="#E8DCC0"/>
    <rect x="135" y="125" width="210" height="42" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    <rect x="135" y="155" width="210" height="12" fill="url(#hm-hatch-lens)" opacity="0.7"/>
    {/* flange rings */}
    {[155,175,235,255,275].map(x => (
      <g key={x}>
        <line x1={x} y1="123" x2={x} y2="169" stroke="#2A1810" strokeWidth="1.8"/>
        <circle cx={x} cy="146" r="2.4" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.5"/>
      </g>
    ))}
    {/* objective lens (left) */}
    <ellipse cx="135" cy="146" rx="9" ry="22" fill="url(#hm-glow-lens)"/>
    <ellipse cx="135" cy="146" rx="9" ry="22" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    <ellipse cx="131" cy="142" rx="3" ry="6" fill="#FFC47C" opacity="0.85"/>
    {/* prism block (mid-right) */}
    <polygon points="290,138 320,128 320,164 290,154" fill="#FAF3E0"/>
    <polygon points="290,138 320,128 320,164 290,154" stroke="#1A0F08" strokeWidth="2.6" fill="none" strokeLinejoin="round"/>
    <polygon points="290,138 320,128 320,164 290,154" fill="url(#hm-hatch-fine-lens)" opacity="0.4"/>
    {/* focus knob top */}
    <rect x="220" y="106" width="36" height="20" fill="#D8C8A8"/>
    <rect x="220" y="106" width="36" height="20" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {[226,234,242,250].map(x => <line key={x} x1={x} y1="106" x2={x} y2="126" stroke="#2A1810" strokeWidth="1.5"/>)}
    <circle cx="238" cy="116" r="3" fill="#B85F40"/>
    {/* eyepiece (right) */}
    <rect x="345" y="138" width="22" height="16" fill="#D8C8A8"/>
    <rect x="345" y="138" width="22" height="16" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {/* collected ray + spectrum split */}
    <line x1="40" y1="146" x2="135" y2="146" stroke="#FFC47C" strokeWidth="3"/>
    <line x1="40" y1="146" x2="135" y2="146" stroke="#1A0F08" strokeWidth="2.6" strokeDasharray="3 2"/>
    {/* spectrum out the right */}
    {[
      ['#D97757', -20], ['#FFC47C', -10], ['#7C9A5B', 0], ['#5B7BAA', 10], ['#8B3A1F', 20]
    ].map(([c, dy], i) => (
      <line key={i} x1="367" y1="146" x2="394" y2={146+dy} stroke={c} strokeWidth="2.4"/>
    ))}
    {/* parchment panel (data readout) */}
    <rect x="48" y="200" width="84" height="44" fill="#FAF3E0"/>
    <rect x="48" y="200" width="84" height="44" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    <line x1="56" y1="210" x2="124" y2="210" stroke="#2A1810" strokeWidth="1.8"/>
    <line x1="56" y1="218" x2="116" y2="218" stroke="#4A3520" strokeWidth="0.9"/>
    <line x1="56" y1="225" x2="120" y2="225" stroke="#4A3520" strokeWidth="0.9"/>
    <line x1="56" y1="232" x2="100" y2="232" stroke="#4A3520" strokeWidth="0.9"/>
    {/* burst */}
    <HMBurst x={70} y={108} r={26} text="OBSERVE!" rot={-15} points={11} size={11}/>
    {/* labels */}
    <HMLabel x={388} y={88} ax={332} ay={140} text="PRISM · 7°" anchor="end"/>
    <HMLabel x={32} y={252} ax={88} ay={222} text="SIGNAL · LOG" anchor="start"/>
  </svg>
);

// ─────────────────────────────────────────────────────
// 2. THE CIPHER — Codex book + key + decoded sigils.
//    Metaphor: intelligence = decoding what others can't.
// ─────────────────────────────────────────────────────
const CipherMachine = () => (
  <svg viewBox="0 0 400 320" preserveAspectRatio="xMidYMid meet" width="100%" style={{maxHeight:340}}>
    <HMDefs id="cipher"/>
    {/* action lines */}
    <g stroke="#4A3520" strokeWidth="0.9" opacity="0.4" strokeLinecap="round">
      {[20,35,165,180,335,350].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={200 + Math.cos(rad)*120} y1={160 + Math.sin(rad)*120} x2={200 + Math.cos(rad)*170} y2={160 + Math.sin(rad)*170}/>;
      })}
    </g>
    {/* book — back cover */}
    <rect x="65" y="80" width="270" height="180" fill="#8B3A1F"/>
    <rect x="65" y="80" width="270" height="180" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {/* book spine binding */}
    <rect x="195" y="80" width="10" height="180" fill="#2A1810"/>
    <rect x="195" y="80" width="10" height="180" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {[100,130,160,190,220,240].map(y => <line key={y} x1="195" y1={y} x2="205" y2={y} stroke="#FFC47C" strokeWidth="1.4"/>)}
    {/* left page */}
    <rect x="75" y="92" width="115" height="156" fill="#FAF3E0"/>
    <rect x="75" y="92" width="115" height="156" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {/* left text lines */}
    {[105,113,121,129,137,145,153,161,169,177,185].map(y => (
      <line key={y} x1="83" y1={y} x2={85 + Math.random()*98} y2={y} stroke="#4A3520" strokeWidth="0.9"/>
    ))}
    {/* sigils on left page */}
    <g fontFamily="GT Sectra, Source Serif 4, serif" fontSize="14" fill="#2A1810">
      <text x="92" y="210" fontWeight="700">⊕</text>
      <text x="110" y="210" fontWeight="700">⊗</text>
      <text x="128" y="210" fontWeight="700">⊙</text>
      <text x="146" y="210" fontWeight="700">⊘</text>
      <text x="164" y="210" fontWeight="700">⊜</text>
    </g>
    <line x1="83" y1="220" x2="183" y2="220" stroke="#8B3A1F" strokeWidth="1.6" strokeDasharray="2 2"/>
    {/* right page (decoded) */}
    <rect x="210" y="92" width="115" height="156" fill="#FAF3E0"/>
    <rect x="210" y="92" width="115" height="156" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {/* mono "decoded" lines */}
    {[105,113,121,129,137,145,153].map((y,i) => (
      <line key={y} x1="218" y1={y} x2={222 + (i%3===0?92:i%3===1?72:60)} y2={y} stroke="#1A0F08" strokeWidth={1.2} />
    ))}
    {/* highlighted finding */}
    <rect x="218" y="160" width="100" height="14" fill="#FFC47C" opacity="0.45"/>
    <line x1="218" y1="167" x2="316" y2="167" stroke="#1A0F08" strokeWidth="1.4"/>
    <line x1="218" y1="184" x2="290" y2="184" stroke="#4A3520" strokeWidth="0.9"/>
    <line x1="218" y1="192" x2="305" y2="192" stroke="#4A3520" strokeWidth="0.9"/>
    {/* findings sigil/glyph */}
    <g transform="translate(266, 218)">
      <circle r="13" fill="url(#hm-glow-cipher)"/>
      <circle r="13" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
      <text textAnchor="middle" y="5" fontFamily="GT Sectra" fontSize="14" fontWeight="700" fill="#1A0F08">≡</text>
    </g>
    {/* key floating left */}
    <g transform="translate(38, 130) rotate(-22)">
      <circle r="14" stroke="#1A0F08" strokeWidth="2.6" fill="#D97757"/>
      <circle r="8" stroke="#1A0F08" strokeWidth="1.8" fill="#FAF3E0"/>
      <rect x="14" y="-3" width="36" height="6" fill="#D97757" stroke="#1A0F08" strokeWidth="2.6"/>
      <rect x="44" y="3" width="4" height="8" fill="#D97757" stroke="#1A0F08" strokeWidth="1.8"/>
      <rect x="36" y="3" width="4" height="6" fill="#D97757" stroke="#1A0F08" strokeWidth="1.8"/>
    </g>
    {/* decode arrow across spine */}
    <path d="M 188 168 Q 200 158 212 168" stroke="#8B3A1F" strokeWidth="2.2" fill="none" strokeLinecap="round" strokeDasharray="3 3"/>
    <polygon points="208,165 218,170 208,174" fill="#8B3A1F"/>
    {/* ribbon */}
    <path d="M 280 80 L 280 270 L 290 260 L 300 270 L 300 80 Z" fill="#B85F40" stroke="#1A0F08" strokeWidth="2.2" strokeLinejoin="round"/>
    {/* burst */}
    <HMBurst x={335} y={70} r={26} text="DECODED!" rot={-10} points={11} size={11}/>
    {/* labels */}
    <HMLabel x={38} y={70} ax={120} ay={92} text="CIPHER · IN" anchor="start"/>
    <HMLabel x={388} y={250} ax={310} ay={222} text="FINDING · OUT" anchor="end"/>
  </svg>
);

// ─────────────────────────────────────────────────────
// 3. THE CONSTELLATION — Star chart with plotted nodes + lines.
//    Metaphor: analytics = patterns drawn from scattered points.
// ─────────────────────────────────────────────────────
const ConstellationMachine = () => {
  // Pre-computed star nodes — looks intentional, not random
  const nodes = [
    [70, 90, 4], [110, 130, 3], [160, 80, 5], [205, 145, 6], [255, 95, 4],
    [300, 145, 5], [340, 100, 4], [85, 220, 4], [140, 235, 3], [195, 215, 5],
    [250, 250, 4], [305, 230, 4], [355, 250, 3], [180, 175, 3], [275, 175, 3],
  ];
  const edges = [
    [0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[3,13],[13,9],[9,10],[10,11],[11,5],[7,8],[8,9],[11,12],[14,5],[14,3]
  ];
  return (
    <svg viewBox="0 0 400 320" preserveAspectRatio="xMidYMid meet" width="100%" style={{maxHeight:340}}>
      <HMDefs id="con"/>
      {/* parchment frame */}
      <rect x="24" y="44" width="352" height="232" fill="#FAF3E0"/>
      <rect x="24" y="44" width="352" height="232" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
      <rect x="32" y="52" width="336" height="216" stroke="#2A1810" strokeWidth="1.4" fill="none"/>
      {/* corner cogs */}
      {[[32,52],[368,52],[32,268],[368,268]].map(([x,y],i) => (
        <g key={i}>
          <circle cx={x} cy={y} r="5" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.8"/>
          <circle cx={x-1} cy={y-1} r="1.4" fill="#FFC47C"/>
        </g>
      ))}
      {/* gridded ticks along edges */}
      {[80,120,160,200,240,280,320].map(x => <line key={`tx${x}`} x1={x} y1="52" x2={x} y2="58" stroke="#4A3520" strokeWidth="0.9"/>)}
      {[80,120,160,200,240].map(y => <line key={`ty${y}`} x1="32" y1={y} x2="38" y2={y} stroke="#4A3520" strokeWidth="0.9"/>)}
      {/* edges (fitted lines) */}
      {edges.map(([a,b],i) => {
        const [x1,y1] = nodes[a], [x2,y2] = nodes[b];
        return <line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke="#8B3A1F" strokeWidth="1.4" strokeLinecap="round" strokeDasharray="2 2"/>;
      })}
      {/* main fitted curve through some nodes */}
      <path d="M 70 90 Q 130 110 205 145 Q 270 175 340 100" stroke="#1A0F08" strokeWidth="2.6" fill="none" strokeLinecap="round"/>
      {/* nodes */}
      {nodes.map(([x,y,r],i) => (
        <g key={i}>
          <circle cx={x} cy={y} r={r+2} fill="#FFC47C" opacity="0.55"/>
          <circle cx={x} cy={y} r={r} fill="#D97757" stroke="#1A0F08" strokeWidth="2.2"/>
        </g>
      ))}
      {/* highlight one cluster — annotation circle */}
      <circle cx="225" cy="170" r="50" stroke="#8B3A1F" strokeWidth="1.6" strokeDasharray="3 3" fill="none"/>
      <line x1="265" y1="200" x2="320" y2="240" stroke="#8B3A1F" strokeWidth="1.8" strokeLinecap="round"/>
      <text x="324" y="246" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" letterSpacing="0.1em">CLUSTER · 04</text>
      {/* axis labels */}
      <text x="32" y="40" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#2A1810" letterSpacing="0.1em">FIG · 0.1</text>
      <text x="368" y="40" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#2A1810" letterSpacing="0.1em" textAnchor="end">N · 15 NODES</text>
      <text x="200" y="296" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" letterSpacing="0.15em" textAnchor="middle">SIGNAL × TIME — DRAWN, NOT GENERATED</text>
      {/* burst */}
      <HMBurst x={345} y={70} r={22} text="POW!" rot={-8} points={12} size={11}/>
      {/* center sigil */}
      <g transform="translate(200, 145)">
        <circle r="10" fill="url(#hm-glow-con)" stroke="#1A0F08" strokeWidth="2.2"/>
      </g>
    </svg>
  );
};

// ─────────────────────────────────────────────────────
// 4. THE SYNTHESIZER (Loom Mk II) — Complex multi-source machine on the
//    left, a single clean ‘insight card’ output on the right. Maximum
//    mechanical complexity in → one actionable line out.
// ─────────────────────────────────────────────────────
const LoomMachine = () => (
  <svg viewBox="0 0 400 320" preserveAspectRatio="xMidYMid meet" width="100%" style={{maxHeight:340}}>
    <HMDefs id="loom"/>
    {/* action lines (left side, around the machine) */}
    <g stroke="#4A3520" strokeWidth="0.9" opacity="0.4" strokeLinecap="round">
      {[20,40,160,175,335,350].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={140 + Math.cos(rad)*100} y1={160 + Math.sin(rad)*100} x2={140 + Math.cos(rad)*138} y2={160 + Math.sin(rad)*138}/>;
      })}
    </g>

    {/* ── MACHINE CASING (left ~70%) ── */}
    <rect x="30" y="54" width="230" height="212" fill="#E8DCC0"/>
    <rect x="30" y="54" width="230" height="212" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    <rect x="30" y="54" width="230" height="212" fill="url(#hm-hatch-loom)" opacity="0.35"/>
    {/* inner panel */}
    <rect x="40" y="64" width="210" height="192" stroke="#2A1810" strokeWidth="1.8" fill="none"/>
    {/* corner rivets */}
    {[[42,66],[248,66],[248,254],[42,254]].map(([x,y],i) => (
      <g key={i}>
        <circle cx={x} cy={y} r="3.2" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.8"/>
        <circle cx={x-1} cy={y-1} r="1.2" fill="#FFC47C"/>
      </g>
    ))}
    {/* nameplate */}
    <text x="145" y="78" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#2A1810" textAnchor="middle" letterSpacing="0.18em">SYNTHESIZER · MK II</text>

    {/* ── 5 source intakes (top edge) ── */}
    {[
      [60, 'ROIS', '#D97757'],
      [95, 'ACAD', '#7C9A5B'],
      [130, 'REGS', '#5B7BAA'],
      [165, 'PATS', '#B85F40'],
      [200, 'PRSS', '#8B3A1F'],
    ].map(([x, label, c], i) => (
      <g key={i}>
        {/* tag */}
        <rect x={x-13} y="34" width="26" height="12" fill={c} stroke="#1A0F08" strokeWidth="2"/>
        <text x={x} y="43" fontFamily="JetBrains Mono, monospace" fontSize="6" fontWeight="700" fill="#FAF3E0" textAnchor="middle">{label}</text>
        {/* feed pipe down through casing */}
        <line x1={x} y1="46" x2={x} y2="94" stroke={c} strokeWidth="2.4"/>
        <line x1={x} y1="46" x2={x} y2="94" stroke="#1A0F08" strokeWidth="0.8" strokeDasharray="2 2" opacity="0.7"/>
      </g>
    ))}
    {/* manifold bar collecting all five inputs */}
    <rect x="50" y="94" width="170" height="10" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="2.2"/>
    {[60,95,130,165,200].map(x => <circle key={x} cx={x} cy="99" r="2" fill="#2A1810"/>)}
    {/* manifold output (one channel down) */}
    <line x1="135" y1="104" x2="135" y2="118" stroke="#2A1810" strokeWidth="3"/>

    {/* ── COMPLEX MIDDLE: gears + cam + governor + gauge ── */}
    {/* primary gear */}
    <g transform="translate(95, 158)">
      {Array.from({length:12}).map((_,i) => {
        const a = (i / 12) * Math.PI * 2;
        const r1 = 24, r2 = 32, ang = 0.24;
        const x1 = Math.cos(a-ang) * r1, y1 = Math.sin(a-ang) * r1;
        const x2 = Math.cos(a-ang*0.65) * r2, y2 = Math.sin(a-ang*0.65) * r2;
        const x3 = Math.cos(a+ang*0.65) * r2, y3 = Math.sin(a+ang*0.65) * r2;
        const x4 = Math.cos(a+ang) * r1, y4 = Math.sin(a+ang) * r1;
        return <path key={i} d={`M${x1},${y1} L${x2},${y2} L${x3},${y3} L${x4},${y4} Z`} stroke="#1A0F08" strokeWidth="2.2" fill="#D97757" strokeLinejoin="round"/>;
      })}
      <circle r="24" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2.6"/>
      <circle r="24" fill="url(#hm-hatch-loom)" opacity="0.5"/>
      <circle r="15" fill="#D8C8A8" stroke="#2A1810" strokeWidth="1.8"/>
      {[0,72,144,216,288].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={Math.cos(rad)*5} y1={Math.sin(rad)*5} x2={Math.cos(rad)*14} y2={Math.sin(rad)*14} stroke="#1A0F08" strokeWidth="2" strokeLinecap="round"/>;
      })}
      <circle r="6" fill="url(#hm-glow-loom)" stroke="#1A0F08" strokeWidth="2.2"/>
      <circle r="2" fill="#2A1810"/>
    </g>
    {/* secondary gear (interlocking) */}
    <g transform="translate(160, 195)">
      {Array.from({length:9}).map((_,i) => {
        const a = (i / 9) * Math.PI * 2 + 0.18;
        const r1 = 16, r2 = 23, ang = 0.28;
        const x1 = Math.cos(a-ang) * r1, y1 = Math.sin(a-ang) * r1;
        const x2 = Math.cos(a-ang*0.65) * r2, y2 = Math.sin(a-ang*0.65) * r2;
        const x3 = Math.cos(a+ang*0.65) * r2, y3 = Math.sin(a+ang*0.65) * r2;
        const x4 = Math.cos(a+ang) * r1, y4 = Math.sin(a+ang) * r1;
        return <path key={i} d={`M${x1},${y1} L${x2},${y2} L${x3},${y3} L${x4},${y4} Z`} stroke="#1A0F08" strokeWidth="2" fill="#B85F40" strokeLinejoin="round"/>;
      })}
      <circle r="16" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2.2"/>
      <circle r="9" fill="#D8C8A8" stroke="#2A1810" strokeWidth="1.6"/>
      <circle r="3" fill="#2A1810"/>
    </g>
    {/* tertiary tiny cog */}
    <g transform="translate(218, 158)">
      {Array.from({length:8}).map((_,i) => {
        const a = (i / 8) * Math.PI * 2;
        const r1 = 10, r2 = 15, ang = 0.32;
        const x1 = Math.cos(a-ang) * r1, y1 = Math.sin(a-ang) * r1;
        const x2 = Math.cos(a-ang*0.65) * r2, y2 = Math.sin(a-ang*0.65) * r2;
        const x3 = Math.cos(a+ang*0.65) * r2, y3 = Math.sin(a+ang*0.65) * r2;
        const x4 = Math.cos(a+ang) * r1, y4 = Math.sin(a+ang) * r1;
        return <path key={i} d={`M${x1},${y1} L${x2},${y2} L${x3},${y3} L${x4},${y4} Z`} stroke="#1A0F08" strokeWidth="1.8" fill="#8B3A1F" strokeLinejoin="round"/>;
      })}
      <circle r="10" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2"/>
      <circle r="3" fill="#2A1810"/>
    </g>
    {/* governor (top-right) — spinning balls */}
    <g transform="translate(208, 110)">
      <line x1="0" y1="-10" x2="0" y2="22" stroke="#1A0F08" strokeWidth="2.2" strokeLinecap="round"/>
      <line x1="0" y1="-10" x2="-14" y2="6" stroke="#1A0F08" strokeWidth="1.8" strokeLinecap="round"/>
      <line x1="0" y1="-10" x2="14" y2="6" stroke="#1A0F08" strokeWidth="1.8" strokeLinecap="round"/>
      <circle cx="-14" cy="6" r="3.5" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.5"/>
      <circle cx="14" cy="6" r="3.5" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.5"/>
      <circle r="3" fill="#FFC47C" stroke="#1A0F08" strokeWidth="1.5"/>
    </g>
    {/* pressure gauge bottom-left */}
    <g transform="translate(64, 220)">
      <circle r="18" fill="#FAF3E0" stroke="#1A0F08" strokeWidth="2.2"/>
      <circle r="15" stroke="#8B3A1F" strokeWidth="1.4" fill="none"/>
      {[-90,-45,0,45].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={Math.cos(rad)*12} y1={Math.sin(rad)*12} x2={Math.cos(rad)*15} y2={Math.sin(rad)*15} stroke="#2A1810" strokeWidth="1.4"/>;
      })}
      <line x1="0" y1="0" x2="10" y2="-7" stroke="#8B3A1F" strokeWidth="1.8" strokeLinecap="round"/>
      <circle r="2.4" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.4"/>
    </g>
    {/* valves bottom-right */}
    <g transform="translate(220, 232)">
      <rect x="-10" y="-6" width="20" height="12" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="2"/>
      <line x1="0" y1="-12" x2="0" y2="-6" stroke="#1A0F08" strokeWidth="2"/>
      <circle cx="0" cy="-14" r="4" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.6"/>
      <line x1="-3" y1="-14" x2="3" y2="-14" stroke="#FAF3E0" strokeWidth="1.2"/>
    </g>
    {/* pipework connecting components */}
    <path d="M 121 158 Q 138 178 144 195" stroke="#1A0F08" strokeWidth="2.4" fill="none"/>
    <path d="M 176 195 Q 200 180 208 158" stroke="#1A0F08" strokeWidth="2.4" fill="none"/>
    <path d="M 95 184 Q 80 200 82 218" stroke="#1A0F08" strokeWidth="2.4" fill="none"/>
    <path d="M 160 211 Q 188 224 210 232" stroke="#1A0F08" strokeWidth="2.4" fill="none"/>

    {/* output funnel exiting right side */}
    <polygon points="230,150 252,150 246,170 236,170" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="2.4" strokeLinejoin="round"/>
    <line x1="260" y1="160" x2="294" y2="160" stroke="#1A0F08" strokeWidth="3"/>
    {/* arrow head */}
    <polygon points="294,154 304,160 294,166" fill="#1A0F08"/>

    {/* ── CLEAN OUTPUT CARD (right ~25%) ── */}
    <rect x="304" y="108" width="86" height="104" fill="#FAF3E0"/>
    <rect x="304" y="108" width="86" height="104" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {/* card header */}
    <text x="312" y="122" fontFamily="JetBrains Mono, monospace" fontSize="6" fontWeight="700" fill="#B85F40" letterSpacing="0.18em">FINDING · 01</text>
    <line x1="312" y1="126" x2="382" y2="126" stroke="#2A1810" strokeWidth="1"/>
    {/* the one verdict line */}
    <text x="312" y="146" fontFamily="GT Sectra, Source Serif 4, serif" fontStyle="italic" fontWeight="700" fontSize="13" fill="#1A0F08">Investigate</text>
    <text x="312" y="161" fontFamily="GT Sectra, Source Serif 4, serif" fontStyle="italic" fontSize="9.5" fill="#4A3520">EU/AI capacity</text>
    {/* confidence dot */}
    <text x="312" y="184" fontFamily="JetBrains Mono, monospace" fontSize="6" fontWeight="700" fill="#4A3520" letterSpacing="0.12em">CONFIDENCE</text>
    <g transform="translate(312, 192)">
      {[0,1,2,3,4].map(i => <circle key={i} cx={i*7+3} cy="4" r="2.4" fill={i<4 ? '#D97757' : '#FAF3E0'} stroke="#1A0F08" strokeWidth="1.4"/>)}
      <text x="45" y="7" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#1A0F08">0.84</text>
    </g>
    {/* sources stamp */}
    <line x1="312" y1="203" x2="382" y2="203" stroke="#C8B89A" strokeWidth="1" strokeDasharray="2 2"/>
    <text x="312" y="212" fontFamily="JetBrains Mono, monospace" fontSize="5.5" fontWeight="700" fill="#8B3A1F" letterSpacing="0.1em">5 SRC · 30+ DOCS · 48H</text>

    {/* burst (over the machine) */}
    <HMBurst x={130} y={88} r={20} text="DISTILL!" rot={-12} points={11} size={10}/>

    {/* mono framing label below */}
    <text x="145" y="288" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" textAnchor="middle" letterSpacing="0.18em">COMPLEX IN</text>
    <text x="347" y="288" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" textAnchor="middle" letterSpacing="0.18em">SIMPLE OUT</text>
  </svg>
);

// ─────────────────────────────────────────────────────
// 5. THE VERDICT ENGINE (Citadel Mk II) — Babbage-style stacked
//    difference engine on the left, a single stamped verdict card
//    on the right. Hand-crank → ledger feed → tiered gears → seal.
// ─────────────────────────────────────────────────────
const CitadelMachine = () => (
  <svg viewBox="0 0 400 320" preserveAspectRatio="xMidYMid meet" width="100%" style={{maxHeight:340}}>
    <HMDefs id="cit"/>

    {/* action lines (around the engine) */}
    <g stroke="#4A3520" strokeWidth="0.9" opacity="0.4" strokeLinecap="round">
      {[10,30,160,180,330,350].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={130 + Math.cos(rad)*100} y1={160 + Math.sin(rad)*100} x2={130 + Math.cos(rad)*138} y2={160 + Math.sin(rad)*138}/>;
      })}
    </g>

    {/* ── ENGINE FRAME (left side, ~58%) ── */}
    {/* base plinth */}
    <rect x="24" y="240" width="222" height="22" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="2.6"/>
    <rect x="24" y="254" width="222" height="8" fill="url(#hm-hatch-cit)"/>
    <rect x="40" y="262" width="22" height="10" stroke="#1A0F08" strokeWidth="2.6" fill="#E8DCC0"/>
    <rect x="208" y="262" width="22" height="10" stroke="#1A0F08" strokeWidth="2.6" fill="#E8DCC0"/>

    {/* engine body (3-tier) */}
    <rect x="38" y="66" width="196" height="178" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2.6"/>
    <rect x="38" y="66" width="196" height="178" fill="url(#hm-hatch-cit)" opacity="0.35"/>
    {/* nameplate */}
    <rect x="50" y="74" width="172" height="14" fill="#FAF3E0" stroke="#2A1810" strokeWidth="1.4"/>
    <text x="136" y="84" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#2A1810" textAnchor="middle" letterSpacing="0.22em">VERDICT ENGINE · MK II</text>
    {/* corner rivets */}
    {[[44,72],[228,72],[228,238],[44,238]].map(([x,y],i) => (
      <g key={i}>
        <circle cx={x} cy={y} r="3.2" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.8"/>
        <circle cx={x-1} cy={y-1} r="1.2" fill="#FFC47C"/>
      </g>
    ))}
    {/* divider rails between tiers */}
    <line x1="38" y1="134" x2="234" y2="134" stroke="#2A1810" strokeWidth="1.8"/>
    <line x1="38" y1="190" x2="234" y2="190" stroke="#2A1810" strokeWidth="1.8"/>

    {/* TIER 1 — three small gears + ledger feed */}
    {[80, 130, 180].map((cx, i) => (
      <g key={i} transform={`translate(${cx}, 110)`}>
        {Array.from({length:8}).map((_,j) => {
          const a = (j / 8) * Math.PI * 2 + (i*0.25);
          const r1 = 9, r2 = 13, ang = 0.32;
          const x1 = Math.cos(a-ang) * r1, y1 = Math.sin(a-ang) * r1;
          const x2 = Math.cos(a-ang*0.6) * r2, y2 = Math.sin(a-ang*0.6) * r2;
          const x3 = Math.cos(a+ang*0.6) * r2, y3 = Math.sin(a+ang*0.6) * r2;
          const x4 = Math.cos(a+ang) * r1, y4 = Math.sin(a+ang) * r1;
          return <path key={j} d={`M${x1},${y1} L${x2},${y2} L${x3},${y3} L${x4},${y4} Z`} stroke="#1A0F08" strokeWidth="1.8" fill={['#D97757','#B85F40','#8B3A1F'][i]} strokeLinejoin="round"/>;
        })}
        <circle r="9" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="1.8"/>
        <circle r="2" fill="#2A1810"/>
      </g>
    ))}
    {/* drive shaft tier 1 */}
    <line x1="68" y1="110" x2="192" y2="110" stroke="#1A0F08" strokeWidth="1.4" strokeDasharray="3 2" opacity="0.6"/>
    {/* ledger card stack on tier 1 right edge */}
    <g transform="translate(212, 100)">
      <rect x="-3" y="3" width="22" height="18" fill="#FAF3E0" stroke="#1A0F08" strokeWidth="1.4"/>
      <rect x="-1" y="1" width="22" height="18" fill="#FAF3E0" stroke="#1A0F08" strokeWidth="1.4"/>
      <rect x="1" y="-1" width="22" height="18" fill="#FAF3E0" stroke="#1A0F08" strokeWidth="1.6"/>
      <line x1="4" y1="4" x2="20" y2="4" stroke="#2A1810" strokeWidth="0.9"/>
      <line x1="4" y1="8" x2="18" y2="8" stroke="#4A3520" strokeWidth="0.7"/>
      <line x1="4" y1="12" x2="19" y2="12" stroke="#4A3520" strokeWidth="0.7"/>
    </g>

    {/* TIER 2 — one big gear + cam wheel + governor */}
    <g transform="translate(96, 162)">
      {Array.from({length:14}).map((_,i) => {
        const a = (i / 14) * Math.PI * 2;
        const r1 = 18, r2 = 24, ang = 0.22;
        const x1 = Math.cos(a-ang) * r1, y1 = Math.sin(a-ang) * r1;
        const x2 = Math.cos(a-ang*0.65) * r2, y2 = Math.sin(a-ang*0.65) * r2;
        const x3 = Math.cos(a+ang*0.65) * r2, y3 = Math.sin(a+ang*0.65) * r2;
        const x4 = Math.cos(a+ang) * r1, y4 = Math.sin(a+ang) * r1;
        return <path key={i} d={`M${x1},${y1} L${x2},${y2} L${x3},${y3} L${x4},${y4} Z`} stroke="#1A0F08" strokeWidth="2.2" fill="#D97757" strokeLinejoin="round"/>;
      })}
      <circle r="18" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2.2"/>
      <circle r="18" fill="url(#hm-hatch-cit)" opacity="0.5"/>
      <circle r="10" fill="#D8C8A8" stroke="#2A1810" strokeWidth="1.6"/>
      {[0,90,180,270].map(a => {
        const rad = a * Math.PI / 180;
        return <line key={a} x1={Math.cos(rad)*4} y1={Math.sin(rad)*4} x2={Math.cos(rad)*9} y2={Math.sin(rad)*9} stroke="#1A0F08" strokeWidth="1.6"/>;
      })}
      <circle r="4" fill="url(#hm-glow-cit)" stroke="#1A0F08" strokeWidth="1.8"/>
    </g>
    {/* cam wheel */}
    <g transform="translate(160, 162)">
      <circle r="15" fill="#E8DCC0" stroke="#1A0F08" strokeWidth="2.2"/>
      <circle r="15" fill="url(#hm-hatch-cit)" opacity="0.5"/>
      <ellipse cx="6" cy="-2" rx="6" ry="3" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.6"/>
      <circle r="3" fill="#2A1810"/>
    </g>
    {/* tappet rod from cam upward */}
    <line x1="166" y1="152" x2="166" y2="138" stroke="#1A0F08" strokeWidth="2.4" strokeLinecap="round"/>
    <rect x="162" y="136" width="8" height="4" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="1.4"/>
    {/* governor (right of cam) */}
    <g transform="translate(204, 158)">
      <line x1="0" y1="-12" x2="0" y2="22" stroke="#1A0F08" strokeWidth="2" strokeLinecap="round"/>
      <line x1="0" y1="-12" x2="-10" y2="4" stroke="#1A0F08" strokeWidth="1.6" strokeLinecap="round"/>
      <line x1="0" y1="-12" x2="10" y2="4" stroke="#1A0F08" strokeWidth="1.6" strokeLinecap="round"/>
      <circle cx="-10" cy="4" r="3" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.4"/>
      <circle cx="10" cy="4" r="3" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.4"/>
    </g>

    {/* TIER 3 — pistons + pressure gauge */}
    {[68, 100, 132, 164].map((cx, i) => (
      <g key={i}>
        <rect x={cx-7} y="196" width="14" height="22" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="1.8"/>
        <rect x={cx-7} y="196" width="14" height="22" fill="url(#hm-hatch-cit)" opacity="0.6"/>
        <rect x={cx-4} y={[200,210,206,214][i]} width="8" height="4" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.4"/>
        <line x1={cx} y1={[204,214,210,218][i]} x2={cx} y2="222" stroke="#1A0F08" strokeWidth="2"/>
      </g>
    ))}
    {/* gauge tier 3 right */}
    <g transform="translate(208, 210)">
      <circle r="14" fill="#FAF3E0" stroke="#1A0F08" strokeWidth="2"/>
      <circle r="11" stroke="#8B3A1F" strokeWidth="1.2" fill="none"/>
      <line x1="0" y1="0" x2="7" y2="-5" stroke="#8B3A1F" strokeWidth="1.6" strokeLinecap="round"/>
      <circle r="2" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.2"/>
    </g>

    {/* steam vents top */}
    <g stroke="#2A1810" strokeWidth="1.6" opacity="0.6" strokeLinecap="round" fill="none" strokeDasharray="2 1.5">
      <path d="M 70 60 Q 64 50 70 42 Q 74 36 70 28"/>
      <path d="M 200 60 Q 206 50 200 42 Q 196 36 200 28"/>
    </g>

    {/* CRANK on right side of engine */}
    <line x1="234" y1="160" x2="258" y2="160" stroke="#1A0F08" strokeWidth="2.6"/>
    <circle cx="258" cy="160" r="10" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="2.6"/>
    <line x1="258" y1="160" x2="258" y2="180" stroke="#1A0F08" strokeWidth="2.4"/>
    <circle cx="258" cy="184" r="4" fill="#B85F40" stroke="#1A0F08" strokeWidth="1.8"/>
    {/* swooshes around crank */}
    <path d="M 270 152 Q 278 152 282 158" stroke="#2A1810" strokeWidth="1.4" fill="none" strokeLinecap="round"/>
    <path d="M 272 168 Q 280 170 284 164" stroke="#2A1810" strokeWidth="1.4" fill="none" strokeLinecap="round"/>

    {/* output channel exiting to verdict card */}
    <line x1="248" y1="160" x2="248" y2="110" stroke="#1A0F08" strokeWidth="0" />
    {/* horizontal pipe to card */}
    <rect x="246" y="196" width="50" height="10" fill="#D8C8A8" stroke="#1A0F08" strokeWidth="2.2"/>
    {/* arrow head */}
    <polygon points="296,194 308,201 296,208" fill="#1A0F08"/>

    {/* ── VERDICT CARD (right ~30%) ── */}
    <rect x="302" y="94" width="86" height="126" fill="#FAF3E0"/>
    <rect x="302" y="94" width="86" height="126" stroke="#1A0F08" strokeWidth="2.6" fill="none"/>
    {/* card eyebrow */}
    <text x="310" y="108" fontFamily="JetBrains Mono, monospace" fontSize="6" fontWeight="700" fill="#B85F40" letterSpacing="0.2em">VERDICT</text>
    <line x1="310" y1="112" x2="380" y2="112" stroke="#2A1810" strokeWidth="1"/>
    {/* the single takeaway */}
    <text x="345" y="144" fontFamily="GT Sectra, Source Serif 4, serif" fontStyle="italic" fontWeight="700" fontSize="22" fill="#1A0F08" textAnchor="middle">Acquire</text>
    <text x="345" y="160" fontFamily="GT Sectra, Source Serif 4, serif" fontStyle="italic" fontSize="10" fill="#4A3520" textAnchor="middle">within 6 months</text>
    {/* wax seal stamp */}
    <g transform="translate(345, 188)">
      <circle r="18" fill="url(#hm-glow-cit)" stroke="#1A0F08" strokeWidth="2.2"/>
      <circle r="14" stroke="#1A0F08" strokeWidth="1" strokeDasharray="2 2" fill="none"/>
      <text textAnchor="middle" y="6" fontFamily="GT Sectra, Source Serif 4, serif" fontStyle="italic" fontWeight="700" fontSize="18" fill="#1A0F08">R</text>
      <circle cx="5" cy="7" r="2.4" fill="#D97757" stroke="#1A0F08" strokeWidth="1.2"/>
    </g>
    <text x="345" y="214" fontFamily="JetBrains Mono, monospace" fontSize="5.5" fontWeight="700" fill="#8B3A1F" textAnchor="middle" letterSpacing="0.1em">SIGNED · WK19 · READIX</text>

    {/* burst over the engine */}
    <HMBurst x={88} y={50} r={22} text="COMPUTE!" rot={-12} points={11} size={9}/>

    {/* mono framing labels */}
    <text x="136" y="294" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" textAnchor="middle" letterSpacing="0.18em">THE HARD PART</text>
    <text x="345" y="294" fontFamily="JetBrains Mono, monospace" fontSize="7" fontWeight="700" fill="#B85F40" textAnchor="middle" letterSpacing="0.18em">YOU GET THIS</text>
  </svg>
);

const HERO_MACHINES = {
  lens: { Comp: LensMachine, name: 'Lens', tagline: 'Decompose the question.', meta: 'RESEARCH · OBSERVATION' },
  cipher: { Comp: CipherMachine, name: 'Cipher', tagline: 'Decode what others can\u2019t.', meta: 'INTELLIGENCE · DECODING' },
  constellation: { Comp: ConstellationMachine, name: 'Constellation', tagline: 'Patterns from scattered points.', meta: 'ANALYTICS · PATTERN' },
  loom: { Comp: LoomMachine, name: 'Synthesizer', tagline: 'Complex in, simple out.', meta: 'SYNTHESIS · ACTIONABLE INSIGHT' },
  citadel: { Comp: CitadelMachine, name: 'Verdict Engine', tagline: 'The hard part. You get this.', meta: 'TECHNICAL EDGE · ONE TAKEAWAY' },
};

Object.assign(window, {
  LensMachine, CipherMachine, ConstellationMachine, LoomMachine, CitadelMachine,
  HERO_MACHINES,
});
