// SVG icons + visual elements for CloudEngine Digital
const { useEffect, useState, useRef } = React;

const I = {
  arrow: (p={}) => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none" className="arrow" {...p}>
      <path d="M2 7h10M7.5 2.5 12 7l-4.5 4.5" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  check: () => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
      <path d="M3 7.5 5.5 10l5.5-6" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  spark: () => (
    <svg width="16" height="16" viewBox="0 0 16 16" fill="none">
      <path d="M8 1v4M8 11v4M1 8h4M11 8h4M3 3l2.5 2.5M10.5 10.5 13 13M13 3l-2.5 2.5M5.5 10.5 3 13" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round"/>
    </svg>
  ),
  shield: () => (
    <svg width="18" height="18" viewBox="0 0 18 18" fill="none">
      <path d="M9 1.5 2.5 4v5c0 4 6.5 7.5 6.5 7.5s6.5-3.5 6.5-7.5V4L9 1.5Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
      <path d="m6 9 2.2 2.2L12 7.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  cube: () => (
    <svg width="18" height="18" viewBox="0 0 18 18" fill="none">
      <path d="M9 1.5 2 5v8l7 3.5L16 13V5L9 1.5Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
      <path d="M2 5l7 3.5L16 5M9 8.5V16.5" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
    </svg>
  ),
  loop: () => (
    <svg width="18" height="18" viewBox="0 0 18 18" fill="none">
      <path d="M2 9a3.5 3.5 0 0 1 6-2.5l2 3a3.5 3.5 0 1 0 0 2.5" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round"/>
    </svg>
  ),
  mail: () => (
    <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
      <rect x="2" y="4" width="16" height="12" rx="2" stroke="currentColor" strokeWidth="1.5"/>
      <path d="m2.5 5 7.5 6 7.5-6" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  ),
  pin: () => (
    <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
      <path d="M10 18s6-5.5 6-10a6 6 0 1 0-12 0c0 4.5 6 10 6 10Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
      <circle cx="10" cy="8" r="2.2" stroke="currentColor" strokeWidth="1.5"/>
    </svg>
  ),
  phone: () => (
    <svg width="20" height="20" viewBox="0 0 20 20" fill="none">
      <path d="M3 4.5C3 3.7 3.7 3 4.5 3h2.2c.7 0 1.3.5 1.5 1.2l.6 2.6c.1.6-.1 1.2-.6 1.5L7 9.2c1 1.9 2.5 3.4 4.4 4.4l.9-1.2c.4-.5 1-.7 1.5-.6l2.6.6c.7.2 1.2.8 1.2 1.5v2.2c0 .8-.7 1.5-1.5 1.5C9.6 17.6 3 11 3 4.5Z" stroke="currentColor" strokeWidth="1.5" strokeLinejoin="round"/>
    </svg>
  ),
  close: () => (
    <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
      <path d="M3 3l8 8M11 3l-8 8" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round"/>
    </svg>
  ),
};

// Brand mark — hard-edged "CE" cipher in brand blue.
const BrandMark = ({size=30}) => (
  <svg width={size} height={size} viewBox="0 0 32 32" fill="none">
    <rect x="0.5" y="0.5" width="31" height="31" fill="#1855F2"/>
    <path d="M8 9h11M8 9v14M8 16h9M8 23h11" stroke="#FFFFFF" strokeWidth="2" fill="none" strokeLinecap="square"/>
    <rect x="22" y="9" width="2" height="14" fill="#FFFFFF"/>
  </svg>
);

/* Hero visual — editorial technical schematic.
   Frames the GPU-cluster + control-plane + lifecycle stack as a single
   monochrome diagram. Replaces the floating-orb hero entirely. */
const HeroVisual = () => (
  <figure className="figure" aria-hidden="true">
    <div className="figure-frame">
      <svg viewBox="0 0 480 408" preserveAspectRatio="xMidYMid meet" width="100%" height="100%" style={{display:'block'}}>
        <defs>
          <pattern id="hatch" width="6" height="6" patternUnits="userSpaceOnUse" patternTransform="rotate(45)">
            <line x1="0" y1="0" x2="0" y2="6" stroke="#0A1330" strokeWidth="0.8" strokeOpacity=".18"/>
          </pattern>
          <marker id="dot" viewBox="0 0 6 6" refX="3" refY="3" markerWidth="6" markerHeight="6">
            <circle cx="3" cy="3" r="2" fill="#0A1330"/>
          </marker>
        </defs>

        {/* corner crop marks */}
        <g stroke="#0A1330" strokeOpacity=".5" strokeWidth="1" fill="none">
          <path d="M12 4v12M4 12h12"/>
          <path d="M468 4v12M476 12h-12"/>
          <path d="M12 404v-12M4 396h12"/>
          <path d="M468 404v-12M476 396h-12"/>
        </g>

        {/* figure number, top-left */}
        <text x="24" y="32" fontFamily="JetBrains Mono" fontSize="9" letterSpacing="2" fill="#4A5577">FIG. 01 — STACK</text>
        <text x="24" y="46" fontFamily="JetBrains Mono" fontSize="9" letterSpacing="2" fill="#1855F2">REV 2026.3</text>

        {/* LAYER 1 — Workloads (top row) */}
        <g transform="translate(60 76)">
          <text fontFamily="JetBrains Mono" fontSize="8.5" letterSpacing="1.8" fill="#8893AE">L1 / AI WORKLOADS</text>
          <g transform="translate(0 14)" fontFamily="Geist" fontSize="10" fill="#0A1330">
            <rect x="0"  y="0" width="84" height="34" fill="none" stroke="#0A1330"/>
            <text x="42" y="20" textAnchor="middle">Training</text>
            <rect x="92" y="0" width="84" height="34" fill="none" stroke="#0A1330"/>
            <text x="134" y="20" textAnchor="middle">Inference</text>
            <rect x="184" y="0" width="84" height="34" fill="none" stroke="#0A1330"/>
            <text x="226" y="20" textAnchor="middle">Fine-tune</text>
            <rect x="276" y="0" width="84" height="34" fill="#0A1330"/>
            <text x="318" y="20" textAnchor="middle" fill="#FFFFFF">RAG / Agents</text>
          </g>
        </g>

        {/* vertical tick rails */}
        <g stroke="#0A1330" strokeOpacity=".35" strokeWidth="1">
          <line x1="102" y1="124" x2="102" y2="154"/>
          <line x1="194" y1="124" x2="194" y2="154"/>
          <line x1="286" y1="124" x2="286" y2="154"/>
          <line x1="378" y1="124" x2="378" y2="154"/>
        </g>

        {/* LAYER 2 — Control plane (middle bar) */}
        <g transform="translate(60 154)">
          <rect x="0" y="0" width="360" height="58" fill="url(#hatch)" stroke="#0A1330"/>
          <text x="14" y="18" fontFamily="JetBrains Mono" fontSize="8.5" letterSpacing="1.8" fill="#0A1330">L2 / CONTROL PLANE</text>
          <g fontFamily="Geist" fontSize="10" fill="#0A1330">
            <rect x="14" y="28" width="68" height="20" fill="#FFFFFF" stroke="#0A1330"/>
            <text x="48" y="42" textAnchor="middle">Quota</text>
            <rect x="88" y="28" width="80" height="20" fill="#FFFFFF" stroke="#0A1330"/>
            <text x="128" y="42" textAnchor="middle">Scheduler</text>
            <rect x="174" y="28" width="64" height="20" fill="#FFFFFF" stroke="#0A1330"/>
            <text x="206" y="42" textAnchor="middle">SSO</text>
            <rect x="244" y="28" width="100" height="20" fill="#FFFFFF" stroke="#0A1330"/>
            <text x="294" y="42" textAnchor="middle">AI Security Gateway</text>
          </g>
        </g>

        {/* connecting wire down to L3 */}
        <g stroke="#0A1330" strokeWidth="1" fill="none">
          <path d="M240 212 v18"/>
          <circle cx="240" cy="232" r="2.2" fill="#1855F2"/>
        </g>

        {/* LAYER 3 — GPU rack ledger */}
        <g transform="translate(60 244)">
          <text fontFamily="JetBrains Mono" fontSize="8.5" letterSpacing="1.8" fill="#8893AE">L3 / GPU FABRIC</text>
          <g transform="translate(0 14)">
            {/* rack columns */}
            {Array.from({length:8}).map((_,i)=> (
              <g key={i} transform={`translate(${i*46} 0)`}>
                <rect x="0" y="0" width="40" height="92" fill="none" stroke="#0A1330"/>
                {/* GPU sleds */}
                {[0,1,2,3,4,5,6].map(j => (
                  <rect key={j} x="4" y={6 + j*12} width="32" height="8"
                    fill={(i===3 && j===2) || (i===6 && j===5) ? "#1855F2" : (j%2===0 ? "#0A1330" : "#FFFFFF")}
                    stroke="#0A1330"/>
                ))}
              </g>
            ))}
          </g>
          <text x="0" y="118" fontFamily="JetBrains Mono" fontSize="8" letterSpacing="1.4" fill="#4A5577">
            R01 · R02 · R03 · R04 · R05 · R06 · R07 · R08
          </text>
          <text x="368" y="118" textAnchor="end" fontFamily="JetBrains Mono" fontSize="8" letterSpacing="1.4" fill="#1855F2">
            64× GB200 ONLINE
          </text>
        </g>

        {/* right-margin annotations */}
        <g transform="translate(424 76)" fontFamily="JetBrains Mono" fontSize="8" letterSpacing="1.4" fill="#4A5577">
          <text>↑ L1</text>
          <text y="78">→ L2</text>
          <text y="168">↓ L3</text>
        </g>
        <line x1="420" y1="80" x2="408" y2="92" stroke="#0A1330" strokeWidth=".8"/>
        <line x1="420" y1="158" x2="408" y2="178" stroke="#0A1330" strokeWidth=".8"/>
        <line x1="420" y1="248" x2="408" y2="270" stroke="#0A1330" strokeWidth=".8"/>
      </svg>
    </div>
    <figcaption className="figure-caption">
      <span className="num">FIG. 01</span>
      <span className="label">Reference stack — workloads, control plane, GPU fabric</span>
      <span>02 / 02</span>
    </figcaption>
  </figure>
);

// Service visuals — one per pillar
const StackVisual = () => (
  <div className="stack">
    <div className="stack-layer">
      <div className="head"><span className="title">AI Developer Ecosystem</span><span className="tag">LAYER 01</span></div>
      <div className="stack-grid">
        <span className="stack-chip">PyTorch</span>
        <span className="stack-chip">TensorFlow</span>
        <span className="stack-chip">Ray</span>
        <span className="stack-chip">vLLM</span>
        <span className="stack-chip">Jupyter</span>
        <span className="stack-chip">MLflow</span>
        <span className="stack-chip">W&amp;B</span>
        <span className="stack-chip">Airflow</span>
      </div>
    </div>
    <div className="stack-layer">
      <div className="head"><span className="title">Control Plane</span><span className="tag">LAYER 02</span></div>
      <div className="stack-grid">
        <span className="stack-chip accent">Projects</span>
        <span className="stack-chip accent">Users</span>
        <span className="stack-chip accent">Quota</span>
        <span className="stack-chip accent">SSO/LDAP</span>
        <span className="stack-chip">Multi-GPU</span>
        <span className="stack-chip">Multi-Node</span>
        <span className="stack-chip">Partitioning</span>
        <span className="stack-chip">Batch Jobs</span>
      </div>
    </div>
    <div className="stack-layer">
      <div className="head"><span className="title">Infrastructure Cluster</span><span className="tag">LAYER 03</span></div>
      <div className="stack-grid">
        <span className="stack-chip">NVIDIA</span>
        <span className="stack-chip">AMD</span>
        <span className="stack-chip">BeeGFS</span>
        <span className="stack-chip">Ceph / NFS</span>
      </div>
    </div>
  </div>
);

const LifecycleVisual = () => {
  const steps = ['Plan', 'Data', 'Prompt', 'Eval', 'Deploy', 'Operate', 'Monitor', 'Adapt'];
  return (
    <div className="lifecycle">
      <svg viewBox="0 0 420 260" width="100%" style={{maxWidth: 480}}>
        <defs>
          <path id="infpath" d="M70,130 C70,70 170,70 210,130 C250,190 350,190 350,130 C350,70 250,70 210,130 C170,190 70,190 70,130 Z" />
        </defs>
        <use href="#infpath" stroke="#E2E8F2" strokeWidth="22" fill="none"/>
        <use href="#infpath" stroke="#0A1330" strokeWidth="1.5" fill="none"/>
        <use href="#infpath" stroke="#0A1330" strokeWidth="22" fill="none" strokeLinecap="butt" strokeDasharray="40 760" strokeOpacity=".35">
          <animate attributeName="stroke-dashoffset" from="0" to="-800" dur="14s" repeatCount="indefinite"/>
        </use>
        <circle cx="70" cy="130" r="4" fill="#0A1330"/>
        <circle cx="350" cy="130" r="4" fill="#0A1330"/>
        <circle cx="210" cy="130" r="5" fill="#1855F2"/>
        <text x="210" y="22" textAnchor="middle" fontFamily="JetBrains Mono" fontSize="9" letterSpacing="2" fill="#4A5577">LLM LIFECYCLE</text>
        <text x="70" y="240" textAnchor="middle" fontFamily="Source Serif 4" fontStyle="italic" fontSize="14" fill="#0A1330">Plan</text>
        <text x="350" y="240" textAnchor="middle" fontFamily="Source Serif 4" fontStyle="italic" fontSize="14" fill="#0A1330">Operate</text>
        <text x="210" y="240" textAnchor="middle" fontFamily="Source Serif 4" fontStyle="italic" fontSize="14" fill="#1855F2">Evaluate</text>
      </svg>
      <div style={{display:'flex', justifyContent:'space-between', width:'100%', maxWidth: 460, fontFamily:'JetBrains Mono', fontSize: 9.5, letterSpacing: '.16em', color:'#8893AE', textTransform:'uppercase'}}>
        {['Data','Prompt','Model','Deploy','Monitor','Adapt'].map(s => <span key={s}>{s}</span>)}
      </div>
    </div>
  );
};

const SecurityVisual = () => (
  <div className="shield-stage">
    <div className="shield-row">
      <div className="shield-card"><I.cube/>End Users</div>
      <div className="shield-card center"><I.shield/>AI Gateway</div>
      <div className="shield-card"><I.cube/>AI Providers</div>
    </div>
    <div className="shield-line"/>
    <div style={{display:'grid', gridTemplateColumns:'repeat(2, 1fr)', gap: 8}}>
      <div style={{background:'var(--paper)', border:'1px solid var(--line)', padding:14}}>
        <div style={{fontFamily:'JetBrains Mono', fontSize:10, letterSpacing:'.16em', color:'var(--ink-4)', marginBottom:10, textTransform:'uppercase'}}>Policy</div>
        <div style={{display:'flex', flexDirection:'column', gap:6, fontSize:12.5, color:'var(--ink-2)', fontWeight:500, fontFamily:'Geist, sans-serif'}}>
          <span>— Rate limiting</span>
          <span>— Token authentication</span>
          <span>— Access policy</span>
          <span>— AI security policy</span>
        </div>
      </div>
      <div style={{background:'var(--paper)', border:'1px solid var(--line)', padding:14}}>
        <div style={{fontFamily:'JetBrains Mono', fontSize:10, letterSpacing:'.16em', color:'var(--ink-4)', marginBottom:10, textTransform:'uppercase'}}>Inspection</div>
        <div style={{display:'flex', flexDirection:'column', gap:6, fontSize:12.5, color:'var(--ink-2)', fontWeight:500, fontFamily:'Geist, sans-serif'}}>
          <span>— DLP policy</span>
          <span>— Prompt-injection defense</span>
          <span>— Output sanitization</span>
          <span>— Plugin control</span>
        </div>
      </div>
    </div>
    <div className="shield-row" style={{marginTop:4}}>
      <div className="shield-card"><I.shield/>AISG / GPU</div>
      <div className="shield-card"><I.shield/>DLPoD</div>
      <div className="shield-card"><I.shield/>Audit &amp; Alerts</div>
    </div>
  </div>
);

const DataVisual = () => (
  <div className="stack">
    <div className="stack-layer">
      <div className="head"><span className="title">Activation Layer</span><span className="tag">LAYER 01</span></div>
      <div className="stack-grid">
        <span className="stack-chip accent">AI Agents</span>
        <span className="stack-chip accent">RAG Apps</span>
        <span className="stack-chip">Analytics</span>
        <span className="stack-chip">Dashboards</span>
      </div>
    </div>
    <div className="stack-layer">
      <div className="head"><span className="title">AI-Ready Datasets</span><span className="tag">LAYER 02</span></div>
      <div className="stack-grid">
        <span className="stack-chip">Validated</span>
        <span className="stack-chip">Reconciled</span>
        <span className="stack-chip">Canonical</span>
        <span className="stack-chip">Versioned</span>
      </div>
    </div>
    <div className="stack-layer">
      <div className="head"><span className="title">Quality &amp; Governance</span><span className="tag">LAYER 03</span></div>
      <div className="stack-grid">
        <span className="stack-chip">Catalog</span>
        <span className="stack-chip">Lineage</span>
        <span className="stack-chip">Policy</span>
        <span className="stack-chip">Monitoring</span>
      </div>
    </div>
    <div className="stack-layer">
      <div className="head"><span className="title">Cloud Data Platform</span><span className="tag">LAYER 04</span></div>
      <div className="stack-grid">
        <span className="stack-chip">Lakehouse</span>
        <span className="stack-chip">Warehouse</span>
        <span className="stack-chip">Streaming</span>
        <span className="stack-chip">Object Store</span>
      </div>
    </div>
  </div>
);

Object.assign(window, { I, BrandMark, HeroVisual, StackVisual, LifecycleVisual, SecurityVisual, DataVisual });
