// ============================================================
// DEMIURGE — LANDING HERO
// "Deploy Your Digital Workforce."
// ============================================================

const HeroOrbital = () => (
  <div className="lp-orbital">
    <svg viewBox="0 0 600 600" xmlns="http://www.w3.org/2000/svg">
      <defs>
        <radialGradient id="brassFill" cx="35%" cy="35%">
          <stop offset="0%" stopColor="#f5d99c"/>
          <stop offset="35%" stopColor="#c9a674"/>
          <stop offset="75%" stopColor="#8a6a2e"/>
          <stop offset="100%" stopColor="#3d2e12"/>
        </radialGradient>
        <linearGradient id="brassRing" x1="0%" y1="0%" x2="100%" y2="100%">
          <stop offset="0%" stopColor="#e8c98a"/>
          <stop offset="50%" stopColor="#8a6a2e"/>
          <stop offset="100%" stopColor="#d4af6a"/>
        </linearGradient>
        <linearGradient id="chromeRing" x1="0%" y1="0%" x2="100%" y2="0%">
          <stop offset="0%" stopColor="#6b7180"/>
          <stop offset="50%" stopColor="#f0f3f8"/>
          <stop offset="100%" stopColor="#6b7180"/>
        </linearGradient>
        <radialGradient id="coreOrb" cx="40%" cy="40%">
          <stop offset="0%" stopColor="#ffffff"/>
          <stop offset="20%" stopColor="#c8a8ff"/>
          <stop offset="55%" stopColor="#8b6bff"/>
          <stop offset="100%" stopColor="#1e1455"/>
        </radialGradient>
        <linearGradient id="iri" x1="0%" y1="0%" x2="100%" y2="100%">
          <stop offset="0%" stopColor="#4be8ff"/>
          <stop offset="50%" stopColor="#8b6bff"/>
          <stop offset="100%" stopColor="#ff7ad9"/>
        </linearGradient>
        <filter id="orbGlow" x="-50%" y="-50%" width="200%" height="200%">
          <feGaussianBlur stdDeviation="14"/>
        </filter>
      </defs>

      {/* Outermost iridescent halo glow */}
      <circle cx="300" cy="300" r="240" fill="url(#iri)" opacity="0.08" filter="url(#orbGlow)"/>

      {/* OUTER BRASS RING — slow rotation */}
      <g className="spin-1">
        <circle cx="300" cy="300" r="270" fill="none" stroke="url(#brassRing)" strokeWidth="0.5" opacity="0.4"/>
        <circle cx="300" cy="300" r="262" fill="none" stroke="url(#brassRing)" strokeWidth="1.5"/>
        <circle cx="300" cy="300" r="254" fill="none" stroke="url(#brassRing)" strokeWidth="0.5" opacity="0.5"/>
        {Array.from({length: 60}).map((_, i) => {
          const a = (i * 6) * Math.PI / 180;
          const r1 = i % 5 === 0 ? 248 : 256;
          const r2 = 262;
          const x1 = 300 + Math.cos(a) * r1, y1 = 300 + Math.sin(a) * r1;
          const x2 = 300 + Math.cos(a) * r2, y2 = 300 + Math.sin(a) * r2;
          return <line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke="#c9a674" strokeWidth={i % 5 === 0 ? 1.2 : 0.5} opacity={i % 5 === 0 ? 0.8 : 0.45}/>;
        })}
        {['N', 'E', 'S', 'W'].map((label, i) => {
          const a = (i * 90 - 90) * Math.PI / 180;
          const x = 300 + Math.cos(a) * 238, y = 300 + Math.sin(a) * 238;
          return <text key={label} x={x} y={y} fill="#e8c98a" fontSize="10" fontFamily="Geist Mono" textAnchor="middle" dominantBaseline="middle" letterSpacing="2">{label}</text>;
        })}
        {[['00°', 0], ['072°', 72], ['144°', 144], ['216°', 216], ['288°', 288]].map(([t, deg], i) => {
          const a = (deg - 90) * Math.PI / 180;
          const x = 300 + Math.cos(a) * 280, y = 300 + Math.sin(a) * 280;
          return <text key={i} x={x} y={y} fill="#8a6a2e" fontSize="7" fontFamily="Geist Mono" textAnchor="middle" dominantBaseline="middle" letterSpacing="1.5">{t}</text>;
        })}
      </g>

      {/* CHROME MIDDLE RING — counter-rotation */}
      <g className="spin-2">
        <circle cx="300" cy="300" r="200" fill="none" stroke="url(#chromeRing)" strokeWidth="2" opacity="0.9"/>
        <circle cx="300" cy="300" r="196" fill="none" stroke="#d4dae2" strokeWidth="0.5" opacity="0.5"/>
        {Array.from({length: 8}).map((_, i) => {
          const startAngle = (i * 45) * Math.PI / 180;
          const endAngle = ((i * 45) + 38) * Math.PI / 180;
          const r = 192;
          const x1 = 300 + Math.cos(startAngle) * r, y1 = 300 + Math.sin(startAngle) * r;
          const x2 = 300 + Math.cos(endAngle) * r, y2 = 300 + Math.sin(endAngle) * r;
          return <path key={i} d={`M ${x1} ${y1} A ${r} ${r} 0 0 1 ${x2} ${y2}`} fill="none" stroke="#d4dae2" strokeWidth="3" opacity={i % 2 === 0 ? 0.7 : 0.3}/>;
        })}
        {[0, 60, 120, 180, 240, 300].map((deg, i) => {
          const a = (deg - 90) * Math.PI / 180;
          const x = 300 + Math.cos(a) * 200, y = 300 + Math.sin(a) * 200;
          return (
            <g key={i}>
              <circle cx={x} cy={y} r="5" fill="#03040c" stroke="#e8c98a" strokeWidth="1.2"/>
              <circle cx={x} cy={y} r="2" fill="#4be8ff" opacity="0.9"/>
              <circle cx={x} cy={y} r="8" fill="none" stroke="#4be8ff" strokeWidth="0.5" opacity="0.4"/>
            </g>
          );
        })}
      </g>

      {/* INNER BRASS RING with text */}
      <g className="spin-3">
        <circle cx="300" cy="300" r="158" fill="none" stroke="#c9a674" strokeWidth="0.5" opacity="0.6"/>
        <circle cx="300" cy="300" r="150" fill="none" stroke="url(#brassRing)" strokeWidth="1"/>
        <path id="ringText" d="M 300,300 m -150,0 a 150,150 0 1,1 300,0 a 150,150 0 1,1 -300,0" fill="none"/>
        <text fill="#c9a674" fontSize="9" fontFamily="Geist Mono" letterSpacing="6">
          <textPath href="#ringText">
            DEMIURGE · SYSTEMS · v.II ··· DIGITAL · WORKFORCE · DEPLOYED ··· INTAKE · COHORT · 014 ··· 
          </textPath>
        </text>
        {Array.from({length: 24}).map((_, i) => {
          const a = (i * 15) * Math.PI / 180;
          const x1 = 300 + Math.cos(a) * 140, y1 = 300 + Math.sin(a) * 140;
          const x2 = 300 + Math.cos(a) * 148, y2 = 300 + Math.sin(a) * 148;
          return <line key={i} x1={x1} y1={y1} x2={x2} y2={y2} stroke="#8a6a2e" strokeWidth="0.5" opacity="0.6"/>;
        })}
      </g>

      {/* Stationary inner crosshair */}
      <circle cx="300" cy="300" r="110" fill="none" stroke="#3d2e12" strokeWidth="0.5"/>
      <line x1="300" y1="180" x2="300" y2="420" stroke="#c9a674" strokeWidth="0.3" opacity="0.3"/>
      <line x1="180" y1="300" x2="420" y2="300" stroke="#c9a674" strokeWidth="0.3" opacity="0.3"/>

      {/* Glowing core orb */}
      <circle cx="300" cy="300" r="120" fill="url(#iri)" opacity="0.15" filter="url(#orbGlow)"/>
      <circle cx="300" cy="300" r="92" fill="url(#coreOrb)"/>
      <ellipse cx="278" cy="278" rx="32" ry="22" fill="rgba(255,255,255,0.5)" opacity="0.55"/>
      <ellipse cx="278" cy="278" rx="16" ry="10" fill="rgba(255,255,255,0.9)" opacity="0.7"/>
      <circle cx="300" cy="300" r="92" fill="none" stroke="url(#iri)" strokeWidth="1.5" opacity="0.9"/>
      <circle cx="300" cy="300" r="96" fill="none" stroke="#4be8ff" strokeWidth="0.5" opacity="0.5"/>
      <g transform="translate(300 300)" opacity="0.65">
        <circle r="44" fill="none" stroke="rgba(255,255,255,0.4)" strokeWidth="0.5"/>
        <path d="M -22 -28 L -22 28 L 0 28 A 28 28 0 0 0 28 0 A 28 28 0 0 0 0 -28 L -22 -28 Z" fill="rgba(255,255,255,0.18)" stroke="rgba(255,255,255,0.7)" strokeWidth="0.8"/>
      </g>

      {/* Floating operator labels around the orbit — represents the live agents */}
      {[
        { a: 28,  label: 'ATLAS · 12' },
        { a: 92,  label: 'HERMES · 04' },
        { a: 162, label: 'CALLIOPE · 07' },
        { a: 232, label: 'VESTA · 09' },
        { a: 302, label: 'ORION · 03' }
      ].map(({ a, label }, i) => {
        const rad = (a - 90) * Math.PI / 180;
        const x = 300 + Math.cos(rad) * 230, y = 300 + Math.sin(rad) * 230;
        return (
          <g key={i}>
            <line x1={x} y1={y} x2={300 + Math.cos(rad) * 202} y2={300 + Math.sin(rad) * 202} stroke="#4be8ff" strokeWidth="0.5" opacity="0.35" strokeDasharray="2 3"/>
            <circle cx={x} cy={y} r="3" fill="#4be8ff" opacity="0.9"/>
            <text x={x + (Math.cos(rad) > 0 ? 8 : -8)} y={y + 3} fill="#4be8ff" fontSize="7.5" fontFamily="Geist Mono" letterSpacing="1.4" opacity="0.85" textAnchor={Math.cos(rad) > 0 ? 'start' : 'end'}>{label}</text>
          </g>
        );
      })}
    </svg>
  </div>
);

const HeroReadouts = () => {
  const [time, setTime] = React.useState(() => new Date());
  React.useEffect(() => {
    const t = setInterval(() => setTime(new Date()), 1000);
    return () => clearInterval(t);
  }, []);
  const fmt = (n) => String(n).padStart(2, '0');
  return (
    <div className="lp-hero-readouts">
      <div className="rd"><span className="lbl">UTC</span><span className="val">{fmt(time.getUTCHours())}:{fmt(time.getUTCMinutes())}:{fmt(time.getUTCSeconds())}</span></div>
      <div className="rd"><span className="lbl">Latency</span><span className="val cy">38 ms</span></div>
      <div className="rd"><span className="lbl">Throughput</span><span className="val cy">14,802 /s</span></div>
      <div className="rd"><span className="lbl">Confidence</span><span className="val ok">99.42 %</span></div>
      <div className="rd"><span className="lbl">Cohort</span><span className="val">014 / Q2-26</span></div>
    </div>
  );
};

// Live-ticking number — gives the metric capsules a heartbeat
const TickingNumber = ({ base, range, decimals = 0, prefix = '', suffix = '' }) => {
  const [val, setVal] = React.useState(base);
  React.useEffect(() => {
    const t = setInterval(() => {
      setVal(b => {
        const delta = (Math.random() - 0.3) * range;
        const next = Math.max(base * 0.95, b + delta);
        return next;
      });
    }, 2200);
    return () => clearInterval(t);
  }, [base, range]);
  const formatted = decimals === 0
    ? Math.round(val).toLocaleString()
    : val.toFixed(decimals);
  return <span>{prefix}{formatted}{suffix}</span>;
};

const HeroMetricCapsules = () => {
  const caps = [
    { lbl: 'Meetings booked / 30d',   v: <TickingNumber base={241} range={2}/>,                       hue: 'cy', sub: 'across all operators' },
    { lbl: 'Influenced pipeline',     v: <span>£<TickingNumber base={1.84} range={0.04} decimals={2}/>M</span>, hue: 'brass', sub: 'live, 90-day window' },
    { lbl: 'Trial → client',          v: <span>87%</span>,                                            hue: 'ok', sub: 'measured at 14 days' },
    { lbl: 'Autonomous operators',    v: <span>8 <span style={{opacity:0.5, fontSize:'0.7em'}}>/ live</span></span>, hue: 'cy', sub: 'tuned per deployment' }
  ];
  return (
    <div className="lp-hero-caps">
      {caps.map((c, i) => (
        <div key={i} className={`lp-hero-cap hue-${c.hue}`}>
          <div className="lp-hero-cap-pip"></div>
          <div className="lp-hero-cap-lbl">{c.lbl}</div>
          <div className="lp-hero-cap-val">{c.v}</div>
          <div className="lp-hero-cap-sub">{c.sub}</div>
        </div>
      ))}
    </div>
  );
};

const HeroTicker = () => {
  const items = [
    { sym: 'ATLAS',    d: '41 meetings booked today',                  dir: 'up' },
    { sym: 'HERMES',   d: '2,847 calls handled · avg 22.4s pickup',    dir: 'up' },
    { sym: 'CALLIOPE', d: '184 assets shipped · 4 channels',           dir: 'up' },
    { sym: 'VESTA',    d: '12,402 leads scored · ICP fit 92%',         dir: 'up' },
    { sym: 'ORION',    d: 'ROAS 6.2x · CPM -34% · auto-scaling',       dir: 'up' },
    { sym: 'IRIS',     d: 'CSAT 4.91 / 5 · deflection 96%',            dir: 'up' },
    { sym: 'NET',      d: '£487M processed · 30d · 99.97% SLA',        dir: 'up' },
    { sym: 'INTAKE',   d: 'Cohort 014 · 47 of 200 seats remain',       dir: 'dn' }
  ];
  return (
    <div className="lp-hero-tickr">
      <div className="track">
        {[...items, ...items, ...items].map((it, i) => (
          <span key={i} className="it">
            <span className="sym">{it.sym}</span>
            <span style={{color: 'rgba(255,255,255,0.15)'}}>·</span>
            <span>{it.d}</span>
            <span className={it.dir === 'up' ? 'up' : 'dn'}>{it.dir === 'up' ? '▲' : '▼'}</span>
          </span>
        ))}
      </div>
    </div>
  );
};

const LandingHero = () => (
  <section className="lp-hero">
    <div className="lp">
      <div className="lp-hero-grid">
        <div className="lp-hero-l">
          <div className="lp-hero-coords">
            <span>DMG-OS · v.II</span>
            <span className="pipe">/</span>
            <span>Intake · Cohort 014</span>
            <span className="pipe">/</span>
            <span className="live">System Online</span>
          </div>
          <h1 className="lp-hero-h1">
            <span className="line">Deploy your</span>
            <span className="line">
              <em className="lp-brass underline">digital</em>{" "}
              <em className="lp-brass underline">workforce</em>.
            </span>
          </h1>
          <p className="lp-lede">
            Autonomous operators that acquire customers, book meetings, run campaigns, answer calls,
            and scale your business — without adding headcount.
          </p>
          <div className="lp-hero-cta-row">
            <a href="#pricing" className="lp-btn primary lg">
              Deploy your operators
              <svg className="arrow" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 6l6 6-6 6"/></svg>
            </a>
            <a href="#live" className="lp-btn lg">
              <svg viewBox="0 0 24 24" fill="currentColor"><path d="M7 5l12 7-12 7V5Z"/></svg>
              See Demiurge live
            </a>
          </div>
          <div className="lp-hero-trustline">
            Trusted operator systems for <span>clinics</span> · <span>law firms</span> · <span>property</span> · <span>trades</span> · <span>recruitment</span> · <span>agencies</span>
          </div>
          <div className="lp-hero-scarcity">
            <span className="dot"></span>
            <span>Limited onboarding slots each month — <strong>Cohort 014 · 47 of 200 seats remaining</strong></span>
          </div>
        </div>

        <div className="lp-mech">
          <div className="lp-mech-frame">
            <div className="corner tl"></div>
            <div className="corner tr"></div>
            <div className="corner bl"></div>
            <div className="corner br"></div>
          </div>
          <div className="lp-mech-label tl">OPERATOR · NEXUS</div>
          <div className="lp-mech-label tr">LIVE TELEMETRY</div>
          <div className="lp-mech-label bl">5 OPERATORS · ORBIT-SYNC</div>
          <div className="lp-mech-label br">RENDER · 60fps</div>
          <HeroOrbital/>
          <HeroReadouts/>
        </div>
      </div>

      <HeroMetricCapsules/>
    </div>
    <HeroTicker/>
  </section>
);

window.LandingHero = LandingHero;
