const { useState: useS, useEffect: useE, useRef: useR } = React;
const { Reveal, ChapterHead, SectionWorld, Parallax } = window;

/* ---------- Problem ---------- */
function Problem({ copy }) {
  const p = copy.problem;
  return (
    <section id="problem" className="problem">
      <SectionWorld tone="amber" intensity={0.7}/>
      <div className="container" style={{position:'relative', zIndex:1}}>
        <ChapterHead
          chapter="01"
          label={p.eyebrow}
          title={<>{p.title[0]}<em>{p.title[1]}</em>{p.title[2]}</>}
          lede={p.lede}
        />

        <div className="prob-grid">
          {p.cards.map((c, i) => (
            <Reveal key={c.t} delay={120 + i*70} className="prob-card">
              <div className="prob-card-num mono">0{i+1}</div>
              <div>
                <h3 className="prob-card-title">{c.t}</h3>
                <p className="prob-card-desc">{c.d}</p>
              </div>
              <div className="prob-card-bar"><span></span></div>
            </Reveal>
          ))}
        </div>

        <div className="prob-stats">
          {p.stats.map((s, i) => (
            <Reveal key={s.d} delay={i*120} className="prob-stat">
              <div className="prob-stat-n serif">{s.n}</div>
              <div className="prob-stat-d">{s.d}</div>
            </Reveal>
          ))}
        </div>
      </div>

      <style>{`
        .prob-grid {
          margin-top: 72px;
          display: grid;
          grid-template-columns: repeat(4, 1fr);
          border-top: 1px solid var(--line);
          border-left: 1px solid var(--line);
        }
        @media (max-width: 900px) { .prob-grid { grid-template-columns: repeat(2, 1fr); } }
        @media (max-width: 520px) { .prob-grid { grid-template-columns: 1fr; } }

        .prob-card {
          padding: 32px 26px 28px;
          border-right: 1px solid var(--line);
          border-bottom: 1px solid var(--line);
          position: relative;
          transition: background 0.3s;
          display: flex; flex-direction: column; gap: 18px;
          min-height: 220px;
        }
        .prob-card:hover { background: rgba(255,255,255,0.018); }
        .prob-card:hover .prob-card-bar span { width: 100%; }
        .prob-card-num { font-size: 11px; color: var(--text-3); letter-spacing: 0.16em; }
        .prob-card-title { font-family: var(--serif); font-size: 26px; line-height: 1.15; letter-spacing: -0.02em; color: var(--text); }
        .prob-card-desc { color: var(--text-2); font-size: 14px; line-height: 1.55; margin: 8px 0 0; }
        .prob-card-bar { height: 1px; background: var(--line); margin-top: auto; position: relative; overflow: hidden; }
        .prob-card-bar span { display: block; height: 100%; width: 0; background: var(--accent); box-shadow: 0 0 12px var(--accent); transition: width 0.8s cubic-bezier(0.2,0.7,0.2,1); }

        .prob-stats {
          margin-top: 64px;
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 32px;
        }
        @media (max-width: 720px) { .prob-stats { grid-template-columns: 1fr; } }
        .prob-stat { padding: 28px 0; border-top: 1px solid var(--line); display: grid; grid-template-columns: auto 1fr; gap: 32px; align-items: end; }
        .prob-stat-n { font-size: clamp(56px, 7vw, 96px); line-height: 1; letter-spacing: -0.035em; color: var(--text); }
        .prob-stat-d { color: var(--text-2); font-size: 15px; max-width: 36ch; padding-bottom: 8px; }
      `}</style>
    </section>
  );
}

/* ---------- Solution: animated module switcher ---------- */
function Solution({ copy }) {
  const s = copy.solution;
  const [active, setActive] = useS(0);
  const [auto, setAuto] = useS(true);

  // auto-rotate
  useE(() => {
    if (!auto) return;
    const id = setInterval(() => setActive(a => (a + 1) % s.modules.length), 5200);
    return () => clearInterval(id);
  }, [auto, s.modules.length]);

  const mod = s.modules[active];

  return (
    <section id="solution" className="solution">
      <SectionWorld tone="cyan" intensity={0.8}/>
      <div className="container" style={{position:'relative', zIndex:1}}>
        <ChapterHead
          chapter="02"
          label={s.eyebrow}
          title={<>{s.title[0]}<em>{s.title[1]}</em></>}
          lede={s.lede}
        />

        <Reveal delay={120}>
          <div className="sol-tabs" role="tablist">
            {s.modules.map((m, i) => (
              <button
                key={m.id}
                role="tab"
                className={`sol-tab ${i === active ? 'active' : ''}`}
                onClick={() => { setActive(i); setAuto(false); }}
                onMouseEnter={() => setAuto(false)}
              >
                <span className="mono sol-tab-num">{m.id}</span>
                <span className="sol-tab-t">{m.t}</span>
                <span className="sol-tab-bar"><span style={{ animationPlayState: (i === active && auto) ? 'running' : 'paused', width: (i === active && auto) ? null : (i === active ? '100%' : '0%')}}></span></span>
              </button>
            ))}
          </div>
        </Reveal>

        <Reveal delay={200}>
          <div className="sol-panel" key={active}>
            <div className="sol-panel-left">
              <div className="mono" style={{fontSize:11, color:'var(--text-3)', letterSpacing:'0.16em'}}>MODUL_{mod.id}</div>
              <h3 className="sol-mod-title serif">{mod.t}</h3>
              <p className="sol-mod-desc">{mod.d}</p>
              <ul className="sol-feat">
                {mod.features.map((f, i) => (
                  <li key={f}><Check/> {f}</li>
                ))}
              </ul>
            </div>
            <div className="sol-panel-right">
              <ModuleVisual active={active}/>
            </div>
          </div>
        </Reveal>
      </div>

      <style>{`
        .sol-head { display: grid; grid-template-columns: 1.4fr 1fr; gap: 64px; align-items: end; margin-bottom: 56px; }
        @media (max-width: 900px) { .sol-head { grid-template-columns: 1fr; gap: 20px; } }

        .sol-tabs { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
        @media (max-width: 720px) { .sol-tabs { grid-template-columns: 1fr; } }
        .sol-tab {
          appearance: none;
          background: rgba(255,255,255,0.015);
          border: 1px solid var(--line);
          border-radius: 12px;
          padding: 18px 20px;
          color: var(--text-2);
          font-family: var(--sans);
          font-size: 14px;
          text-align: left;
          cursor: pointer;
          display: grid; grid-template-columns: auto 1fr; gap: 12px;
          align-items: center;
          position: relative;
          overflow: hidden;
          transition: all 0.3s;
        }
        .sol-tab:hover { color: var(--text); border-color: var(--line-2); }
        .sol-tab.active { background: rgba(255,255,255,0.04); border-color: var(--line-2); color: var(--text); }
        .sol-tab-num { font-size: 11px; color: var(--text-3); }
        .sol-tab.active .sol-tab-num { color: var(--accent); }
        .sol-tab-t { font-size: 15px; }
        .sol-tab-bar { position: absolute; left: 0; right: 0; bottom: 0; height: 2px; background: var(--line); grid-column: 1 / -1; }
        .sol-tab-bar span { display: block; height: 100%; width: 0; background: var(--accent); box-shadow: 0 0 8px var(--accent); }
        .sol-tab.active .sol-tab-bar span { animation: fillBar 5.2s linear forwards; }
        @keyframes fillBar { from { width: 0; } to { width: 100%; } }

        .sol-panel {
          margin-top: 28px;
          border: 1px solid var(--line);
          border-radius: 18px;
          background: linear-gradient(180deg, rgba(255,255,255,0.025), rgba(255,255,255,0.005));
          overflow: hidden;
          display: grid;
          grid-template-columns: 1fr 1.1fr;
          min-height: 460px;
          animation: panelFade 0.6s cubic-bezier(0.2,0.7,0.2,1);
        }
        @keyframes panelFade {
          from { opacity: 0; transform: translateY(8px); }
          to { opacity: 1; transform: none; }
        }
        @media (max-width: 900px) { .sol-panel { grid-template-columns: 1fr; } }

        .sol-panel-left { padding: 44px; display: flex; flex-direction: column; gap: 14px; border-right: 1px solid var(--line); }
        @media (max-width: 900px) { .sol-panel-left { border-right: none; border-bottom: 1px solid var(--line); padding: 32px; } }
        .sol-mod-title { font-size: clamp(32px, 4vw, 48px); line-height: 1.05; letter-spacing: -0.025em; margin-top: 8px; }
        .sol-mod-desc { color: var(--text-2); font-size: 16px; line-height: 1.6; max-width: 44ch; }
        .sol-feat { list-style: none; padding: 0; margin: 12px 0 0; display: flex; flex-direction: column; gap: 12px; }
        .sol-feat li { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--text); padding: 12px 0; border-top: 1px dashed rgba(255,255,255,0.06); }
        .sol-feat li:first-child { border-top: 1px solid var(--line); }

        .sol-panel-right { padding: 32px; display: flex; align-items: center; justify-content: center; background: radial-gradient(ellipse 80% 60% at 70% 50%, oklch(0.6 0.18 230 / 0.1), transparent 60%); position: relative; overflow: hidden; min-height: 380px; }
      `}</style>
    </section>
  );
}

/* ---------- Module visuals (one per tab) ---------- */
function ModuleVisual({ active }) {
  if (active === 0) return <ChatVisual/>;
  if (active === 1) return <EmailVisual/>;
  return <AnalyzerVisual/>;
}

function ChatVisual() {
  return (
    <div className="mv chat-mv">
      <div className="mv-head mono">internal_chat.session_42</div>
      <div className="chat-msg user">
        <div className="bub">Welche Compliance-Auflagen gelten für unseren neuen DACH-Kunden?</div>
      </div>
      <div className="chat-msg ai">
        <div className="bub">
          Basierend auf der internen Wissensbasis (Policy v3.4): <strong>DSGVO Art. 28</strong> Auftragsverarbeitung, ISO 27001 Klausel A.5.34 und das interne <em>Data Residency Memo</em> vom 14.03.2026.
          <div className="srcs">
            <span className="mono">↳ wissensbasis/policy_v3.4.pdf</span>
            <span className="mono">↳ wissensbasis/data_residency.md</span>
          </div>
        </div>
      </div>
      <div className="chat-input">
        <span className="mono">▍ frag etwas internes…</span>
        <span className="chat-tag mono">PRIVAT</span>
      </div>

      <style>{`
        .mv { width: 100%; max-width: 460px; border: 1px solid var(--line); border-radius: 14px; background: rgba(0,0,0,0.3); padding: 18px; display: flex; flex-direction: column; gap: 14px; backdrop-filter: blur(6px); }
        .mv-head { font-size: 10px; color: var(--text-3); letter-spacing: 0.14em; padding-bottom: 12px; border-bottom: 1px solid var(--line); }
        .chat-msg { display: flex; }
        .chat-msg.user { justify-content: flex-end; }
        .chat-msg .bub { max-width: 80%; padding: 12px 14px; border-radius: 14px; font-size: 13px; line-height: 1.5; }
        .chat-msg.user .bub { background: rgba(255,255,255,0.06); color: var(--text); border-top-right-radius: 4px; }
        .chat-msg.ai .bub { background: rgba(0,0,0,0.4); border: 1px solid var(--line); color: var(--text-2); border-top-left-radius: 4px; }
        .chat-msg.ai .bub strong { color: var(--text); font-weight: 500; }
        .srcs { display: flex; flex-direction: column; gap: 4px; margin-top: 10px; padding-top: 10px; border-top: 1px dashed rgba(255,255,255,0.08); font-size: 10px; color: var(--text-3); }
        .chat-input { margin-top: 4px; display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border: 1px solid var(--line); border-radius: 10px; font-size: 12px; color: var(--text-3); }
        .chat-tag { font-size: 9px; padding: 3px 8px; background: var(--accent-soft); color: var(--accent); border-radius: 999px; border: 1px solid var(--accent-line); }
      `}</style>
    </div>
  );
}

function EmailVisual() {
  return (
    <div className="mv email-mv">
      <div className="mv-head mono"><span style={{color:'var(--accent)'}}>●</span> inbox · 12 threads triaged</div>
      {[
        { from: "Marc L.", subj: "Re: Q3 Roadmap", tag: "REPLIED", time: "vor 2 min" },
        { from: "Finance", subj: "INV-2026-0412", tag: "DRAFTED", time: "vor 8 min" },
        { from: "Sarah K.", subj: "Onboarding-Workshop", tag: "REPLIED", time: "vor 14 min" },
        { from: "Support", subj: "Ticket #8821", tag: "TRIAGED", time: "vor 21 min" }
      ].map((m, i) => (
        <div className="emrow" key={i}>
          <div className="emrow-l">
            <div className="emrow-from">{m.from}</div>
            <div className="emrow-subj">{m.subj}</div>
          </div>
          <div className="emrow-r">
            <span className={`emtag mono ${m.tag.toLowerCase()}`}>{m.tag}</span>
            <span className="mono emrow-time">{m.time}</span>
          </div>
        </div>
      ))}

      <style>{`
        .email-mv { width: 100%; max-width: 460px; }
        .emrow { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-top: 1px dashed rgba(255,255,255,0.06); }
        .emrow:first-of-type { border-top: 1px solid var(--line); }
        .emrow-from { font-size: 12px; color: var(--text); font-weight: 500; }
        .emrow-subj { font-size: 12px; color: var(--text-2); margin-top: 2px; }
        .emrow-r { display: flex; align-items: center; gap: 10px; }
        .emtag { font-size: 9px; padding: 3px 7px; border-radius: 999px; letter-spacing: 0.1em; }
        .emtag.replied { background: oklch(0.85 0.15 145 / 0.12); color: oklch(0.85 0.15 145); border: 1px solid oklch(0.85 0.15 145 / 0.3); }
        .emtag.drafted { background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent-line); }
        .emtag.triaged { background: oklch(0.85 0.12 75 / 0.12); color: oklch(0.85 0.12 75); border: 1px solid oklch(0.85 0.12 75 / 0.3); }
        .emrow-time { font-size: 10px; color: var(--text-3); }
      `}</style>
    </div>
  );
}

function AnalyzerVisual() {
  const tasks = [
    { name: "Rechnungs-Erinnerung", freq: "92×/Monat", roi: 88, status: "auto" },
    { name: "Status-Update an PM", freq: "64×/Monat", roi: 72, status: "auto" },
    { name: "CRM-Hygiene", freq: "48×/Monat", roi: 55, status: "review" },
    { name: "Spesen-Freigabe", freq: "31×/Monat", roi: 41, status: "manual" }
  ];
  return (
    <div className="mv analyzer-mv">
      <div className="mv-head mono">analyzer · workflow_mining</div>
      {tasks.map((t, i) => (
        <div className="anrow" key={t.name} style={{animationDelay: `${i*80}ms`}}>
          <div className="anrow-top">
            <div>
              <div className="anrow-name">{t.name}</div>
              <div className="anrow-freq mono">{t.freq}</div>
            </div>
            <div className={`anrow-tag mono ${t.status}`}>
              {t.status === 'auto' && '◇ AUTOMATISIERBAR'}
              {t.status === 'review' && '◇ REVIEW'}
              {t.status === 'manual' && '◇ MANUELL'}
            </div>
          </div>
          <div className="anbar">
            <div className="anbar-fill" style={{width: `${t.roi}%`}}></div>
          </div>
          <div className="anrow-roi mono">ROI {t.roi}/100</div>
        </div>
      ))}

      <style>{`
        .analyzer-mv { width: 100%; max-width: 460px; }
        .anrow { padding: 14px 0; border-top: 1px dashed rgba(255,255,255,0.06); animation: slideIn 0.5s both; }
        .anrow:first-of-type { border-top: 1px solid var(--line); }
        @keyframes slideIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
        .anrow-top { display: flex; justify-content: space-between; align-items: start; gap: 12px; }
        .anrow-name { font-size: 13px; color: var(--text); }
        .anrow-freq { font-size: 10px; color: var(--text-3); margin-top: 2px; }
        .anrow-tag { font-size: 9px; padding: 3px 7px; border-radius: 4px; letter-spacing: 0.1em; }
        .anrow-tag.auto { background: var(--accent-soft); color: var(--accent); border: 1px solid var(--accent-line); }
        .anrow-tag.review { background: oklch(0.85 0.12 75 / 0.1); color: oklch(0.85 0.12 75); border: 1px solid oklch(0.85 0.12 75 / 0.3); }
        .anrow-tag.manual { background: rgba(255,255,255,0.04); color: var(--text-3); border: 1px solid var(--line); }
        .anbar { height: 3px; background: rgba(255,255,255,0.05); border-radius: 2px; margin-top: 8px; overflow: hidden; }
        .anbar-fill { height: 100%; background: linear-gradient(90deg, var(--accent), oklch(0.8 0.16 200)); animation: barGrow 0.9s cubic-bezier(0.2,0.7,0.2,1); }
        @keyframes barGrow { from { width: 0 !important; } }
        .anrow-roi { font-size: 10px; color: var(--text-3); margin-top: 4px; }
      `}</style>
    </div>
  );
}

/* ---------- How ---------- */
function How({ copy }) {
  const h = copy.how;
  return (
    <section id="how" className="how">
      <SectionWorld tone="violet" intensity={0.7}/>
      <div className="container" style={{position:'relative', zIndex:1}}>
        <ChapterHead
          chapter="03"
          label={h.eyebrow}
          title={<>{h.title[0]}<em>{h.title[1]}</em></>}
        />

        <div className="how-rail">
          <div className="how-line"><span></span></div>
          {h.steps.map((s, i) => (
            <Reveal key={s.n} delay={i*100} className="how-step">
              <div className="how-step-num mono">
                <span className="how-step-dot"></span>
                {s.n}
              </div>
              <h3 className="how-step-t serif">{s.t}</h3>
              <p className="how-step-d">{s.d}</p>
            </Reveal>
          ))}
        </div>
      </div>

      <style>{`
        .how-rail {
          margin-top: 72px;
          display: grid;
          grid-template-columns: repeat(4, 1fr);
          gap: 24px;
          position: relative;
          padding-top: 28px;
        }
        @media (max-width: 900px) { .how-rail { grid-template-columns: repeat(2, 1fr); } }
        @media (max-width: 520px) { .how-rail { grid-template-columns: 1fr; } }
        .how-line {
          position: absolute;
          top: 6px; left: 0; right: 0;
          height: 1px;
          background: var(--line);
          overflow: hidden;
        }
        .how-line span {
          position: absolute; left: 0; top: 0; bottom: 0; width: 60px;
          background: linear-gradient(90deg, transparent, var(--accent), transparent);
          animation: rail 3.6s linear infinite;
        }
        @keyframes rail { from { transform: translateX(-60px); } to { transform: translateX(calc(100vw + 60px)); } }

        .how-step { padding: 0; position: relative; }
        .how-step-num { font-size: 11px; color: var(--text-3); letter-spacing: 0.16em; display: flex; align-items: center; gap: 8px; }
        .how-step-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--bg); border: 1px solid var(--accent); box-shadow: 0 0 12px var(--accent-line); margin-top: -34px; }
        .how-step-t { font-size: 24px; line-height: 1.15; letter-spacing: -0.02em; margin-top: 24px; max-width: 18ch; }
        .how-step-d { color: var(--text-2); font-size: 14px; line-height: 1.55; margin-top: 12px; }
      `}</style>
    </section>
  );
}

/* ---------- Security ---------- */
function Security({ copy }) {
  const s = copy.security;
  return (
    <section id="security" className="security">
      <SectionWorld tone="azure" intensity={0.7}/>
      <div className="container" style={{position:'relative', zIndex:1}}>
        <ChapterHead
          chapter="04"
          label={s.eyebrow}
          title={<>{s.title[0]}<em>{s.title[1]}</em></>}
          lede={s.lede}
        />

        <div className="sec-grid">
          <div className="sec-left">
            <div className="sec-items">
              {s.items.map((it, i) => (
                <Reveal key={it.t} delay={i*80} className="sec-item">
                  <div className="sec-item-icon"><LockIcon i={i}/></div>
                  <div>
                    <div className="sec-item-t">{it.t}</div>
                    <div className="sec-item-d">{it.d}</div>
                  </div>
                </Reveal>
              ))}
            </div>
          </div>

          <div className="sec-right">
            <Reveal delay={120}>
              <EnclaveVisual label={s.enclave} env={s.env}/>
            </Reveal>
          </div>
        </div>
      </div>

      <style>{`
        .sec-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 64px; align-items: start; }
        @media (max-width: 900px) { .sec-grid { grid-template-columns: 1fr; gap: 48px; } }

        .sec-items {
          display: grid;
          grid-template-columns: 1fr 1fr;
          gap: 0;
          border-top: 1px solid var(--line);
          border-left: 1px solid var(--line);
        }
        @media (max-width: 600px) { .sec-items { grid-template-columns: 1fr; } }
        .sec-item {
          display: grid; grid-template-columns: 36px 1fr; gap: 16px;
          padding: 28px 24px;
          border-right: 1px solid var(--line);
          border-bottom: 1px solid var(--line);
          transition: background 0.3s;
          min-height: 168px;
        }
        .sec-item:hover { background: rgba(255,255,255,0.018); }
        .sec-item-icon {
          width: 36px; height: 36px;
          border: 1px solid var(--line);
          border-radius: 8px;
          display: flex; align-items: center; justify-content: center;
          color: var(--accent);
          background: rgba(0,0,0,0.3);
        }
        .sec-item-t { font-size: 17px; color: var(--text); line-height: 1.25; }
        .sec-item-d { color: var(--text-2); font-size: 13px; line-height: 1.5; margin-top: 6px; }

        .sec-right {
          position: sticky;
          top: 100px;
        }
        @media (max-width: 900px) { .sec-right { position: static; } }
      `}</style>
    </section>
  );
}

function LockIcon({ i }) {
  const paths = [
    <g key="0"><rect x="4" y="8" width="12" height="9" rx="2" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M6 8V6a4 4 0 018 0v2" stroke="currentColor" strokeWidth="1.3" fill="none"/></g>,
    <g key="1"><rect x="3" y="5" width="14" height="10" rx="2" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M3 9h14" stroke="currentColor" strokeWidth="1.3"/></g>,
    <g key="2"><circle cx="10" cy="7" r="3" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M4 17c0-3 3-5 6-5s6 2 6 5" stroke="currentColor" strokeWidth="1.3" fill="none"/></g>,
    <g key="3"><path d="M10 2l7 4v5c0 4-3 7-7 8-4-1-7-4-7-8V6l7-4z" stroke="currentColor" strokeWidth="1.3" fill="none"/><path d="M7 10l2 2 4-4" stroke="currentColor" strokeWidth="1.3" fill="none" strokeLinecap="round" strokeLinejoin="round"/></g>,
  ];
  return <svg width="20" height="20" viewBox="0 0 20 20">{paths[i]}</svg>;
}

function EnclaveVisual({ label, env }) {
  return (
    <div className="enclave">
      <div className="enclave-bg"></div>
      <div className="enclave-ring r1"></div>
      <div className="enclave-ring r2"></div>
      <div className="enclave-ring r3"></div>
      <div className="enclave-core">
        <div className="enclave-core-inner">
          <svg width="40" height="40" viewBox="0 0 24 24" fill="none">
            <rect x="6" y="11" width="12" height="9" rx="2" stroke="var(--accent)" strokeWidth="1.4"/>
            <path d="M8 11V8a4 4 0 118 0v3" stroke="var(--accent)" strokeWidth="1.4"/>
            <circle cx="12" cy="15.5" r="1.2" fill="var(--accent)"/>
          </svg>
          <div className="mono" style={{fontSize:10, color:'var(--accent)', letterSpacing:'0.16em', marginTop:8}}>{label}</div>
        </div>
      </div>
      <div className="enclave-env mono">{env}</div>
      <div className="enclave-grid"></div>
      {Array.from({length: 8}).map((_, i) => (
        <div key={i} className="enclave-particle" style={{
          left: `${20 + (i*9) % 70}%`,
          top: `${15 + (i*13) % 70}%`,
          animationDelay: `${i*0.4}s`
        }}></div>
      ))}

      <style>{`
        .enclave {
          position: relative;
          aspect-ratio: 1 / 1;
          max-width: 460px;
          margin: 0 auto;
          border: 1px solid var(--line);
          border-radius: 18px;
          overflow: hidden;
          background: radial-gradient(ellipse 60% 60% at 50% 50%, oklch(0.6 0.18 230 / 0.12), transparent 70%);
        }
        .enclave-grid {
          position: absolute; inset: 0;
          background-image:
            linear-gradient(rgba(255,255,255,0.04) 1px, transparent 1px),
            linear-gradient(90deg, rgba(255,255,255,0.04) 1px, transparent 1px);
          background-size: 32px 32px;
          mask-image: radial-gradient(circle at 50% 50%, transparent 30%, #000 90%);
          -webkit-mask-image: radial-gradient(circle at 50% 50%, transparent 30%, #000 90%);
        }
        .enclave-ring {
          position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
          border: 1px solid var(--accent-line);
          border-radius: 50%;
          opacity: 0.5;
        }
        .enclave-ring.r1 { width: 40%; height: 40%; animation: spin 22s linear infinite; }
        .enclave-ring.r1::after { content: ""; position: absolute; top: -4px; left: 50%; width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 12px var(--accent); }
        .enclave-ring.r2 { width: 60%; height: 60%; animation: spin 36s linear infinite reverse; border-style: dashed; }
        .enclave-ring.r3 { width: 80%; height: 80%; animation: spin 50s linear infinite; opacity: 0.25; }
        .enclave-ring.r3::after { content: ""; position: absolute; bottom: -3px; right: 30%; width: 6px; height: 6px; border-radius: 50%; background: oklch(0.85 0.12 75); box-shadow: 0 0 10px oklch(0.85 0.12 75); }

        .enclave-core {
          position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
          width: 28%; height: 28%;
          border-radius: 50%;
          background: radial-gradient(circle, oklch(0.4 0.1 230 / 0.4), transparent);
          display: flex; align-items: center; justify-content: center;
        }
        .enclave-core-inner {
          width: 100%; height: 100%;
          border: 1px solid var(--accent-line);
          background: rgba(7,8,10,0.8);
          border-radius: 50%;
          display: flex; flex-direction: column; align-items: center; justify-content: center;
          box-shadow: 0 0 40px oklch(0.6 0.18 230 / 0.3);
          backdrop-filter: blur(6px);
        }
        .enclave-env { position: absolute; top: 16px; left: 16px; font-size: 10px; color: var(--text-3); letter-spacing: 0.14em; }
        .enclave-particle {
          position: absolute;
          width: 4px; height: 4px;
          border-radius: 50%;
          background: var(--accent);
          box-shadow: 0 0 8px var(--accent);
          opacity: 0;
          animation: particle 3.6s ease-in-out infinite;
        }
        @keyframes particle {
          0%, 100% { opacity: 0; transform: scale(0.5); }
          40%, 60% { opacity: 1; transform: scale(1); }
        }
      `}</style>
    </div>
  );
}

/* ---------- Results: animated counters ---------- */
function Counter({ to, suffix, decimals = 0, duration = 1400 }) {
  const [val, setVal] = useS(0);
  const ref = useR(null);
  const started = useR(false);
  useE(() => {
    if (!ref.current) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting && !started.current) {
          started.current = true;
          const start = performance.now();
          const tick = (now) => {
            const t = Math.min((now - start) / duration, 1);
            const eased = 1 - Math.pow(1 - t, 3);
            setVal(to * eased);
            if (t < 1) requestAnimationFrame(tick);
          };
          requestAnimationFrame(tick);
        }
      });
    }, { threshold: 0.3 });
    io.observe(ref.current);
    return () => io.disconnect();
  }, [to, duration]);
  const display = decimals ? val.toFixed(decimals) : Math.round(val);
  return <span ref={ref}>{display}{suffix}</span>;
}

function Results({ copy }) {
  const r = copy.results;
  const lang = copy === window.COPY?.en ? 'en' : 'de';
  const methodology = lang === 'en'
    ? "Aggregate across active pilots · Q2 2026"
    : "Aggregat aus aktiven Piloten · Q2 2026";
  return (
    <section id="results" className="results">
      <SectionWorld tone="lime" intensity={0.6}/>
      <div className="container" style={{position:'relative', zIndex:1}}>
        <ChapterHead
          chapter="05"
          label={r.eyebrow}
          title={<>{r.title[0]}<em>{r.title[1]}</em></>}
          lede={methodology}
        />

        <div className="res-grid">
          {r.stats.map((s, i) => (
            <Reveal key={s.t} delay={i*100} className="res-card">
              <div className="res-n serif">
                <Counter to={s.n} suffix={s.suffix} decimals={String(s.n).includes('.') ? 1 : 0}/>
              </div>
              <div className="res-t">{s.t}</div>
              <div className="res-d mono">{s.d}</div>
            </Reveal>
          ))}
        </div>
      </div>

      <style>{`
        .res-grid {
          margin-top: 72px;
          display: grid;
          grid-template-columns: repeat(4, 1fr);
          border-top: 1px solid var(--line);
          border-left: 1px solid var(--line);
        }
        @media (max-width: 900px) { .res-grid { grid-template-columns: repeat(2, 1fr); } }
        @media (max-width: 520px) { .res-grid { grid-template-columns: 1fr; } }
        .res-card {
          padding: 36px 28px;
          border-right: 1px solid var(--line);
          border-bottom: 1px solid var(--line);
          min-height: 220px;
          display: flex; flex-direction: column;
          transition: background 0.3s;
        }
        .res-card:hover { background: rgba(255,255,255,0.018); }
        .res-n { font-size: clamp(56px, 7vw, 88px); line-height: 1; letter-spacing: -0.035em; color: var(--text); }
        .res-t { font-size: 17px; color: var(--text); margin-top: auto; padding-top: 24px; }
        .res-d { font-size: 11px; color: var(--text-3); margin-top: 6px; letter-spacing: 0.06em; }
      `}</style>
    </section>
  );
}

/* ---------- Projects ---------- */
function Projects({ copy }) {
  const p = copy.projects;
  return (
    <section id="projects" className="projects">
      <SectionWorld tone="gold" intensity={0.5}/>
      <div className="container" style={{position:'relative', zIndex:1}}>
        <ChapterHead
          chapter="06"
          label={p.eyebrow}
          title={<>{p.title[0]}<em>{p.title[1]}</em></>}
        />

        <Reveal delay={140}>
          <div className="proj-card">
            <div className="proj-logo">
              <div className="proj-logo-mark serif">B&amp;J</div>
              <div className="proj-pulse"></div>
            </div>
            <div className="proj-body">
              <div className="mono" style={{fontSize:11, color:'var(--accent)', letterSpacing:'0.14em'}}>● {p.pilot.toUpperCase()}</div>
              <h3 className="proj-name serif">{p.name}</h3>
              <p className="proj-desc">{p.desc}</p>
              <div className="proj-tags">
                <span className="proj-tag mono">Internal ChatGPT</span>
                <span className="proj-tag mono">Email Agent</span>
                <span className="proj-tag mono">Workflow Automation</span>
              </div>
            </div>
            <div className="proj-meta">
              <div className="proj-meta-row">
                <span className="mono">START</span>
                <span>Q1 2026</span>
              </div>
              <div className="proj-meta-row">
                <span className="mono">PHASE</span>
                <span>Live</span>
              </div>
              <div className="proj-meta-row">
                <span className="mono">HOURS/MO</span>
                <span>1,240+</span>
              </div>
            </div>
          </div>
        </Reveal>

        <Reveal delay={220}>
          <div className="proj-more">
            <div className="proj-more-line"></div>
            <span className="mono">{p.more}</span>
            <div className="proj-placeholders">
              {Array.from({length: 3}).map((_, i) => (
                <div key={i} className="proj-placeholder mono">[ Q3 / 2026 ]</div>
              ))}
            </div>
          </div>
        </Reveal>
      </div>

      <style>{`
        .proj-card {
          margin-top: 64px;
          display: grid;
          grid-template-columns: 220px 1fr 220px;
          gap: 0;
          border: 1px solid var(--line);
          border-radius: 18px;
          overflow: hidden;
          background: linear-gradient(180deg, rgba(255,255,255,0.025), rgba(255,255,255,0.005));
        }
        @media (max-width: 900px) { .proj-card { grid-template-columns: 1fr; } }
        .proj-logo {
          padding: 44px 32px;
          border-right: 1px solid var(--line);
          display: flex; align-items: center; justify-content: center;
          background: rgba(0,0,0,0.2);
          position: relative;
        }
        @media (max-width: 900px) { .proj-logo { border-right: none; border-bottom: 1px solid var(--line); } }
        .proj-logo-mark {
          font-size: 52px;
          letter-spacing: -0.04em;
          color: var(--text);
        }
        .proj-pulse {
          position: absolute;
          top: 14px; right: 14px;
          width: 8px; height: 8px;
          border-radius: 50%;
          background: oklch(0.85 0.15 145);
          box-shadow: 0 0 12px oklch(0.85 0.15 145);
          animation: pulse 1.8s ease-in-out infinite;
        }
        .proj-body { padding: 36px 32px; display: flex; flex-direction: column; gap: 14px; }
        .proj-name { font-size: clamp(32px, 4vw, 44px); line-height: 1.05; letter-spacing: -0.025em; }
        .proj-desc { color: var(--text-2); font-size: 15px; line-height: 1.6; max-width: 56ch; margin: 0; }
        .proj-tags { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
        .proj-tag { font-size: 10px; padding: 5px 10px; background: rgba(255,255,255,0.04); border: 1px solid var(--line); border-radius: 999px; color: var(--text-2); letter-spacing: 0.06em; }
        .proj-meta { padding: 36px 32px; border-left: 1px solid var(--line); display: flex; flex-direction: column; gap: 18px; justify-content: center; background: rgba(0,0,0,0.2); }
        @media (max-width: 900px) { .proj-meta { border-left: none; border-top: 1px solid var(--line); } }
        .proj-meta-row { display: flex; justify-content: space-between; align-items: center; font-size: 13px; color: var(--text); padding-bottom: 14px; border-bottom: 1px dashed rgba(255,255,255,0.06); }
        .proj-meta-row:last-child { border-bottom: none; padding-bottom: 0; }
        .proj-meta-row .mono { font-size: 10px; color: var(--text-3); letter-spacing: 0.14em; }

        .proj-more { margin-top: 56px; display: grid; grid-template-columns: auto 1fr; gap: 24px; align-items: center; }
        .proj-more-line { display: none; }
        .proj-more > span { font-size: 11px; color: var(--text-3); letter-spacing: 0.14em; text-transform: uppercase; }
        .proj-placeholders { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
        @media (max-width: 600px) { .proj-placeholders { grid-template-columns: 1fr; } .proj-more { grid-template-columns: 1fr; } }
        .proj-placeholder {
          padding: 28px;
          border: 1px dashed var(--line);
          border-radius: 12px;
          text-align: center;
          color: var(--text-3);
          font-size: 11px;
          letter-spacing: 0.14em;
        }
      `}</style>
    </section>
  );
}

/* ---------- CTA ---------- */
function CTA({ copy }) {
  const c = copy.cta;
  return (
    <section id="cta" className="cta">
      <div className="container cta-inner">
        <Reveal>
          <div className="eyebrow">{c.eyebrow}</div>
        </Reveal>
        <Reveal delay={80}>
          <h2 className="cta-title serif">
            {c.title[0]}<em>{c.title[1]}</em>
          </h2>
        </Reveal>
        <Reveal delay={160}>
          <p className="cta-lede">{c.lede}</p>
        </Reveal>
        <Reveal delay={220}>
          <div className="cta-btns">
            <a href="mailto:hello@business-ai.com" className="btn btn-primary cta-btn">
              {c.btn} <span className="arrow">↗</span>
            </a>
          </div>
        </Reveal>
      </div>

      <style>{`
        .cta { position: relative; overflow: hidden; padding: calc(160px * var(--density)) 0; text-align: center; border-top: 1px solid var(--line); }
        .cta-inner { position: relative; z-index: 2; }
        .cta-title { font-size: clamp(48px, 7vw, 96px); line-height: 1.0; letter-spacing: -0.03em; margin: 24px auto 0; max-width: 18ch; text-wrap: balance; color: var(--text); }
        .cta-title em { font-style: italic; color: var(--text-2); }
        .cta-lede { max-width: 56ch; margin: 28px auto 0; color: var(--text-2); font-size: 18px; line-height: 1.6; }
        .cta-btns { margin-top: 36px; display: flex; gap: 12px; justify-content: center; }
        .cta-btn { padding: 18px 28px; font-size: 15px; }
      `}</style>
    </section>
  );
}

Object.assign(window, { Reveal, Problem, Solution, How, Security, Results, Projects, CTA, Counter });
