/* eslint-disable */
const { useState, useEffect, useRef, useMemo } = React;

/* ============================================================
   TrustFortress — nav + feed + Money + Footer
   Hero / Problem / Cube / HotPath / Seams / ProofStack live in sections.jsx
   Cube primitives + T palette live in cube.jsx (loaded first; T is on window).
   `T` is declared by sections.jsx, which loads before us.
   ============================================================ */

const go = (id) => {
  const el = document.getElementById(id);
  if (el) el.scrollIntoView({ behavior: "smooth", block: "start" });
};

function useReveal() {
  const ref = useRef(null);
  const [v, setV] = useState(false);
  useEffect(() => {
    if (!ref.current) return;
    const fallback = setTimeout(() => setV(true), 350);
    if (typeof IntersectionObserver === "undefined") {
      setV(true);
      return () => clearTimeout(fallback);
    }
    const o = new IntersectionObserver(
      ([e]) => { if (e.isIntersecting) { setV(true); clearTimeout(fallback); } },
      { threshold: 0.07 }
    );
    o.observe(ref.current);
    return () => { clearTimeout(fallback); o.disconnect(); };
  }, []);
  return [ref, v];
}
function R({ children, delay = 0, as: As = "div", style }) {
  const [ref, vis] = useReveal();
  return (
    <As
      ref={ref}
      style={{
        opacity: vis ? 1 : 0,
        transform: vis ? "translateY(0)" : "translateY(14px)",
        transition: `opacity 0.7s ease ${delay}s, transform 0.7s ease ${delay}s`,
        ...(style || {}),
      }}
    >
      {children}
    </As>
  );
}

function Eyebrow({ children, color = T.violet, dot = true }) {
  return (
    <div style={{
      display: "inline-flex", alignItems: "center", gap: 8,
      fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.18em",
      textTransform: "uppercase", color,
    }}>
      {dot && <span style={{ width: 6, height: 6, borderRadius: 1, background: color, boxShadow: `0 0 0 3px ${color}22` }} />}
      <span>{children}</span>
    </div>
  );
}

function Logomark({ size = 22 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
      <defs>
        <linearGradient id="lg-v" x1="0" y1="0" x2="1" y2="1">
          <stop offset="0" stopColor="#AA3BFF" />
          <stop offset="1" stopColor="#7E14FF" />
        </linearGradient>
      </defs>
      <path d="M13 1.5 L4.5 13 H10 L8.5 22.5 L19 11 H13.5 Z" fill="url(#lg-v)"/>
      <path d="M13 1.5 L4.5 13 H10 L8.5 22.5 L19 11 H13.5 Z" stroke="rgba(255,255,255,0.12)" strokeWidth="0.5" fill="none"/>
    </svg>
  );
}

/* expose so sections.jsx can use them (Babel scopes per script) */
Object.assign(window, { T, go, useReveal, R, Eyebrow, Logomark });

/* ---------- Live feed ---------- */
const ACTIONS = [
  { req: "GET /v1/chat/completions",          v: "lease", code: "TFL-7c9a", reason: "deploy △ pass · compute △ pass · pool=A100-x8 · ttl=5s" },
  { req: "POST /v1/admin/db/drop",            v: "deny",  code: "DENY_POLICY",         reason: "policy.B.action_class · ⊢ Ramen V1.19.1 #14" },
  { req: "POST /v1/embed (24K tokens)",       v: "deny",  code: "DENY_OR_SHAPE_GPU",   reason: "context.C.loop_ok · retry #4 in 5min · ⊢ V1.40 #5" },
  { req: "GET  /v1/models",                   v: "lease", code: "TFL-2f10", reason: "read-only · all 6 faces solved · 240µs" },
  { req: "POST /v1/files/transfer",           v: "deny",  code: "DENY_TOPOLOGY",       reason: "runtime.C.host_alive · target mismatch · ⊢ V1.30 #3" },
  { req: "POST /v1/chat (function calling)",  v: "lease", code: "TFL-8b21", reason: "tools_hash matched · attn fits · 312µs" },
  { req: "DELETE /v1/customers/all",          v: "deny",  code: "DENY_BYPASS",         reason: "missing lease at host · ⊢ V1.30 host verifier" },
  { req: "POST /v1/chat/completions",         v: "deny",  code: "DENY_EPOCH_STALE",    reason: "telemetry_epoch=11923 < min=11929 · ⊢ V2.0-C" },
];

function LiveFeed() {
  const [entries, setEntries] = useState([]);
  const [stats, setStats] = useState({ t: 0, p: 0, b: 0 });
  const idx = useRef(0);
  useEffect(() => {
    const add = () => {
      const a = ACTIONS[idx.current % ACTIONS.length];
      idx.current++;
      setEntries((prev) => [{ ...a, id: idx.current, time: new Date().toLocaleTimeString() }, ...prev].slice(0, 6));
      setStats((prev) => ({ t: prev.t + 1, p: prev.p + (a.v === "lease" ? 1 : 0), b: prev.b + (a.v === "deny" ? 1 : 0) }));
    };
    add();
    const iv = setInterval(add, 2400);
    return () => clearInterval(iv);
  }, []);
  return (
    <div style={{
      background: T.panel,
      border: `1px solid ${T.hairline}`,
      borderRadius: 4,
      overflow: "hidden",
    }}>
      <div style={{
        padding: "14px 18px",
        borderBottom: `1px solid ${T.hairline}`,
        display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12,
      }}>
        <Eyebrow color={T.violetSoft}>Admission feed · sub-1ms</Eyebrow>
        <div style={{ display: "flex", gap: 18, fontFamily: "var(--mono)", fontSize: 11 }}>
          <span style={{ color: T.fgMute }}>checked <b style={{ color: T.fg, marginLeft: 4 }}>{String(stats.t).padStart(3, "0")}</b></span>
          <span style={{ color: T.fgMute }}>leased  <b style={{ color: T.mint, marginLeft: 4 }}>{String(stats.p).padStart(3, "0")}</b></span>
          <span style={{ color: T.fgMute }}>denied  <b style={{ color: T.red, marginLeft: 4 }}>{String(stats.b).padStart(3, "0")}</b></span>
        </div>
      </div>
      <div>
        {entries.map((e, i) => (
          <div key={e.id} style={{
            display: "grid",
            gridTemplateColumns: "20px 1fr auto",
            alignItems: "flex-start",
            gap: 12,
            padding: "13px 18px",
            borderBottom: i === entries.length - 1 ? "none" : `1px solid ${T.hairline}`,
            opacity: i === 0 ? 1 : Math.max(0.32, 1 - i * 0.13),
          }}>
            <span style={{
              marginTop: 4,
              width: 10, height: 10, borderRadius: 1,
              background: e.v === "lease" ? T.mint : T.red,
              boxShadow: `0 0 0 3px ${(e.v === "lease" ? T.mint : T.red)}1f`,
            }} />
            <div>
              <div style={{ fontFamily: "var(--mono)", fontSize: 12, color: T.fg, fontWeight: 500, marginBottom: 4, letterSpacing: "0.01em" }}>
                {e.req}
              </div>
              <div style={{
                fontSize: 11,
                color: e.v === "lease" ? `${T.mint}cc` : `${T.red}cc`,
                lineHeight: 1.55,
                fontFamily: "var(--mono)",
              }}>
                <b style={{ marginRight: 6 }}>{e.code}</b>· {e.reason}
              </div>
            </div>
            <span style={{ fontSize: 10, color: T.fgMute, fontFamily: "var(--mono)" }}>{e.time}</span>
          </div>
        ))}
      </div>
    </div>
  );
}
Object.assign(window, { LiveFeed });

/* ---------- Nav ---------- */
function Nav() {
  const [s, setS] = useState(false);
  useEffect(() => {
    const fn = () => setS(window.scrollY > 30);
    window.addEventListener("scroll", fn);
    return () => window.removeEventListener("scroll", fn);
  }, []);
  const links = [
    { id: "problem",    l: "Incidents" },
    { id: "cube",       l: "The Cube" },
    { id: "hotpath",    l: "Hot Path" },
    { id: "seams",      l: "Seams" },
    { id: "proofstack", l: "Proofs" },
    { id: "money",      l: "Savings" },
  ];
  return (
    <nav style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 100,
      background: s ? "rgba(10,14,26,0.78)" : "transparent",
      backdropFilter: s ? "blur(20px) saturate(140%)" : "none",
      WebkitBackdropFilter: s ? "blur(20px) saturate(140%)" : "none",
      borderBottom: s ? `1px solid ${T.hairline}` : "1px solid transparent",
      transition: "all 0.4s ease",
    }}>
      <div style={{
        maxWidth: 1180, margin: "0 auto",
        padding: "0 28px",
        display: "flex", alignItems: "center", justifyContent: "space-between",
        height: 64,
      }}>
        <button onClick={() => go("hero")} style={{
          display: "flex", alignItems: "center", gap: 10,
          background: "none", border: "none", cursor: "pointer", padding: 0, color: T.fg,
        }}>
          <Logomark size={22} />
          <span style={{ fontSize: 14, fontWeight: 600, letterSpacing: "-0.01em" }}>Trust<span style={{ color: T.violetSoft }}>Fortress</span></span>
        </button>
        <div className="nav-links" style={{ display: "flex", gap: 4, alignItems: "center" }}>
          {links.map((n) => (
            <button
              key={n.id}
              onClick={() => go(n.id)}
              style={{
                padding: "8px 12px",
                fontSize: 12,
                color: T.fgSoft,
                background: "none",
                border: "none",
                cursor: "pointer",
                borderRadius: 4,
                fontFamily: "var(--mono)",
                letterSpacing: "0.02em",
              }}
              onMouseEnter={(e) => (e.currentTarget.style.color = T.fg)}
              onMouseLeave={(e) => (e.currentTarget.style.color = T.fgSoft)}
            >
              {n.l}
            </button>
          ))}
          <button
            onClick={() => go("signup")}
            className="tf-cta"
            style={{
              marginLeft: 10,
              padding: "9px 16px",
              background: T.violet,
              color: T.fg,
              borderRadius: 3,
              fontSize: 12,
              fontWeight: 600,
              border: "none",
              cursor: "pointer",
              letterSpacing: "0.01em",
              boxShadow: `0 0 0 1px ${T.violet}, 0 8px 24px -10px ${T.violet}`,
            }}
          >
            Get access ↗
          </button>
        </div>
      </div>
    </nav>
  );
}

/* ---------- MONEY ---------- */
function Money() {
  const [fleet, setFleet] = useState(1000);
  const [rate, setRate] = useState(3.29);
  const waste = fleet * 0.6685 * 8760 * 0.65 * rate;
  const recovered = waste * 0.6185;

  const stats = [
    { v: "25,000×", t: "Cheaper to evaluate the cube than to burn a wasted prefill", b: "1.75 J for the bytecode VM vs 43,750 J for a context-bomb prefill that produces nothing.", c: T.mint },
    { v: "$0",      t: "No new hardware — runs on chips already in your rack",      b: "Rust on Intel: AMX for tiny classifiers, QAT for crypto, DSA for data movement, TPM/TDX for measured boot.", c: T.amber },
    { v: "7",       t: "Outcomes, not just allow / deny",                            b: "Allow, four DENY codes, throttle, reroute, async queue, quarantine. Each carries a typed certificate.", c: T.violetSoft },
  ];

  return (
    <section id="money" data-screen-label="07 Money" style={{ background: T.ink, padding: "140px 28px 120px" }}>
      <div style={{ maxWidth: 1180, margin: "0 auto" }}>
        <div style={{ maxWidth: 780, marginBottom: 56 }}>
          <R><Eyebrow>07 · The Compute corner pays for itself</Eyebrow></R>
          <R delay={0.06}>
            <h2 className="tf-h2" style={{
              fontSize: "clamp(32px,4.6vw,56px)", fontWeight: 400, color: T.fg,
              lineHeight: 1.04, letterSpacing: "-0.03em", margin: "20px 0 20px",
            }}>
              The same seams that stop attacks<br/>
              <span style={{ fontStyle: "italic", color: T.violetSoft, fontWeight: 300 }}>also stop waste.</span>
            </h2>
          </R>
          <R delay={0.12}>
            <p style={{ fontSize: 16, lineHeight: 1.7, color: T.fgSoft, maxWidth: 720 }}>
              The Compute corner asks one question: <i style={{ color: T.fg }}>is this request worth a GPU cycle?</i>
              {" "}Context bombs, retry storms, prefix-cache thrash, single-token aborts — they pass face checks but fail the goodput seam.
              {" "}Failed seam ⇒ no lease ⇒ no prefill ⇒ no burned watt.
            </p>
          </R>
        </div>

        <R delay={0.18}>
          <div style={{
            display: "grid", gridTemplateColumns: "minmax(0, 1fr) minmax(0, 1fr)",
            gap: 24, alignItems: "stretch",
          }} className="tf-money-grid">
            <div style={{
              padding: 36, background: T.panel, color: T.fg,
              borderRadius: 3, border: `1px solid ${T.hairline}`,
              display: "flex", flexDirection: "column",
            }}>
              <Eyebrow color={T.violetSoft}>Calculator</Eyebrow>
              <h3 style={{ fontSize: 22, fontWeight: 400, color: T.fg, marginTop: 12, marginBottom: 32, letterSpacing: "-0.015em" }}>
                Recovered goodput · annual.
              </h3>

              <div style={{ marginBottom: 24 }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 8 }}>
                  <label style={{ fontFamily: "var(--mono)", fontSize: 11, color: T.fgMute, letterSpacing: "0.1em" }}>GPU FLEET</label>
                  <span style={{ fontFamily: "var(--mono)", fontSize: 18, color: T.fg, fontWeight: 600 }}>{fleet.toLocaleString()}</span>
                </div>
                <input type="range" min={100} max={10000} step={100} value={fleet}
                  onChange={(e) => setFleet(+e.target.value)} style={{ width: "100%" }} />
                <div style={{ display: "flex", justifyContent: "space-between", marginTop: 4, fontFamily: "var(--mono)", fontSize: 9, color: T.fgMute }}>
                  <span>100</span><span>10K</span>
                </div>
              </div>

              <div style={{ marginBottom: 28 }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 8 }}>
                  <label style={{ fontFamily: "var(--mono)", fontSize: 11, color: T.fgMute, letterSpacing: "0.1em" }}>$ / GPU-HOUR</label>
                  <span style={{ fontFamily: "var(--mono)", fontSize: 18, color: T.fg, fontWeight: 600 }}>${rate.toFixed(2)}</span>
                </div>
                <input type="range" min={1} max={8} step={0.01} value={rate}
                  onChange={(e) => setRate(+e.target.value)} style={{ width: "100%" }} />
                <div style={{ display: "flex", justifyContent: "space-between", marginTop: 4, fontFamily: "var(--mono)", fontSize: 9, color: T.fgMute }}>
                  <span>$1.00</span><span>$8.00</span>
                </div>
              </div>

              <div style={{ borderTop: `1px solid ${T.hairline}`, paddingTop: 24, marginTop: "auto" }}>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 14 }}>
                  <span style={{ fontFamily: "var(--mono)", fontSize: 11, color: T.fgMute, letterSpacing: "0.08em" }}>WASTE / YEAR</span>
                  <span style={{ fontFamily: "var(--mono)", fontSize: 20, color: T.red }}>${(waste / 1e6).toFixed(1)}M</span>
                </div>
                <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline" }}>
                  <span style={{ fontFamily: "var(--mono)", fontSize: 11, color: T.fgMute, letterSpacing: "0.08em" }}>RECOVERED / YEAR</span>
                  <span style={{ fontFamily: "var(--mono)", fontSize: 36, color: T.mint, fontWeight: 600, letterSpacing: "-0.02em" }}>
                    ${(recovered / 1e6).toFixed(1)}M
                  </span>
                </div>
                <div style={{ fontFamily: "var(--mono)", fontSize: 9, color: T.fgMute, marginTop: 12, letterSpacing: "0.02em", lineHeight: 1.55 }}>
                  Based on Centillion measurements: 66.85% blast radius on TP deadlock cascades · 61.85% of waste blocked at goodput seam.
                </div>
              </div>
            </div>

            <div style={{ display: "flex", flexDirection: "column", gap: 16 }}>
              {stats.map((s, i) => (
                <div key={i} style={{
                  padding: 28, background: T.panel,
                  border: `1px solid ${T.hairline}`,
                  borderLeft: `3px solid ${s.c}`,
                  borderRadius: 2, flex: 1,
                  display: "flex", flexDirection: "column", justifyContent: "center",
                }}>
                  <div style={{ fontFamily: "var(--mono)", fontSize: 36, fontWeight: 500, color: T.fg, letterSpacing: "-0.02em", marginBottom: 8 }}>
                    {s.v}
                  </div>
                  <div style={{ fontSize: 15, fontWeight: 500, color: T.fg, marginBottom: 6, letterSpacing: "-0.005em" }}>
                    {s.t}
                  </div>
                  <div style={{ fontSize: 13, color: T.fgSoft, lineHeight: 1.6 }}>
                    {s.b}
                  </div>
                </div>
              ))}
            </div>
          </div>
        </R>
      </div>
    </section>
  );
}

/* ---------- FOOTER ---------- */
function Footer() {
  const [email, setEmail] = useState("");
  const [done, setDone] = useState(false);
  return (
    <footer id="signup" data-screen-label="08 Signup" style={{ background: T.ink, padding: "120px 28px 56px", borderTop: `1px solid ${T.hairline}`, position: "relative", overflow: "hidden" }}>
      <div aria-hidden style={{ position: "absolute", inset: 0,
        background: `radial-gradient(ellipse 60% 60% at 50% 100%, ${T.violet}28 0%, transparent 60%)`}} />
      <div style={{ maxWidth: 720, margin: "0 auto", textAlign: "center", marginBottom: 80, position: "relative" }}>
        <R><Eyebrow>Design partner walkthrough</Eyebrow></R>
        <R delay={0.06}>
          <h2 className="tf-h2" style={{
            fontSize: "clamp(36px,5.2vw,68px)", fontWeight: 400, color: T.fg,
            lineHeight: 1, letterSpacing: "-0.035em", margin: "20px 0 18px",
          }}>
            Watch one solve.<br/>
            <span style={{ fontStyle: "italic", color: T.violetSoft, fontWeight: 300 }}>Watch one deny.</span>
          </h2>
        </R>
        <R delay={0.12}>
          <p style={{ fontSize: 15, lineHeight: 1.7, color: T.fgSoft, marginBottom: 28, maxWidth: 460, margin: "0 auto 28px" }}>
            We are onboarding design partners. Bring a slice of your traffic; we replay it through the cube and show you what your dashboard misses.
          </p>
        </R>
        <R delay={0.18}>
          {!done ? (
            <form onSubmit={(e) => { e.preventDefault(); if (email.includes("@")) setDone(true); }}
              style={{ display: "flex", gap: 8, maxWidth: 460, margin: "0 auto", padding: 4, background: T.panel, border: `1px solid ${T.hairlineSt}`, borderRadius: 2 }}>
              <input
                type="email" value={email} onChange={(e) => setEmail(e.target.value)}
                placeholder="you@company.com"
                style={{
                  flex: 1, padding: "12px 14px",
                  background: "transparent",
                  border: "none", color: T.fg, fontSize: 14, outline: "none",
                  fontFamily: "inherit",
                }}
              />
              <button type="submit" style={{
                padding: "12px 22px", background: T.violet, color: T.fg,
                border: "none", borderRadius: 2, fontSize: 13, fontWeight: 600, cursor: "pointer", letterSpacing: "0.01em",
              }}>
                Sign up →
              </button>
            </form>
          ) : (
            <div style={{
              display: "inline-flex", alignItems: "center", gap: 10,
              padding: "12px 22px", border: `1px solid ${T.mint}55`, borderRadius: 2,
              color: T.mint, fontSize: 14, fontWeight: 500,
            }}>
              <span style={{ width: 8, height: 8, background: T.mint, borderRadius: "50%" }}/>
              Thank you. We will be in touch.
            </div>
          )}
        </R>
      </div>

      <div style={{
        maxWidth: 1180, margin: "0 auto",
        borderTop: `1px solid ${T.hairline}`, paddingTop: 28,
        display: "grid", gridTemplateColumns: "1fr auto 1fr", gap: 16,
        alignItems: "center", position: "relative",
      }} className="tf-foot-grid">
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <Logomark size={20} />
          <span style={{ fontSize: 13, fontWeight: 600, color: T.fg, letterSpacing: "-0.01em" }}>
            Trust<span style={{ color: T.violetSoft }}>Fortress</span>
          </span>
          <span style={{ fontFamily: "var(--mono)", fontSize: 10, color: T.fgMute, marginLeft: 6, letterSpacing: "0.04em" }}>
            © 2026 Centillion AI Inc.
          </span>
        </div>
        <span style={{ fontFamily: "var(--mono)", fontSize: 10, color: T.fgMute, letterSpacing: "0.1em", textAlign: "center" }}>
          BUILT IN CALIFORNIA
        </span>
        <span style={{ fontFamily: "var(--mono)", fontSize: 10, color: T.fgMute, letterSpacing: "0.04em", textAlign: "right" }}>
          3 US patents · 163 theorems · 7 deliverables
        </span>
      </div>
    </footer>
  );
}

/* ---------- ROOT ---------- */
function App() {
  return (
    <div>
      <Nav />
      <Hero />
      <Problem />
      <CubeSection />
      <HotPath />
      <SeamSection />
      <ProofStack />
      <Money />
      <Footer />
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
