// V2 — Live Assessment
// Animated hero showing the product working: evidence files drop in,
// get scanned, controls light up with maturity scores. Confident voice.

(function () {
const { Icon, Logo, TopNav, Footer, CustomerWall, FRAMEWORK_GROUPS, SAMPLE_CONTROLS } = window.LP;
const { useState: useStateLA, useEffect: useEffectLA, useRef: useRefLA } = React;

// Cycling animation states for the demo
const DEMO_FILES = [
  { name: "AWS-IAM-policy-export.json", type: "json", color: "#F4C28A" },
  { name: "Q4-access-review.csv", type: "csv", color: "#A8D8A8" },
  { name: "incident-runbook.pdf", type: "pdf", color: "#E07A5F" },
  { name: "vendor-SOC2-datadog.pdf", type: "pdf", color: "#D4A8D4" },
  { name: "training-roster.xlsx", type: "xlsx", color: "#93D2CC" },
];

function HeroLive({ headline, kicker }) {
  const [phase, setPhase] = useStateLA("idle"); // idle | dropping | scanning | scoring | done
  const [activeFile, setActiveFile] = useStateLA(0);
  const [scoredRows, setScoredRows] = useStateLA({});
  const [progress, setProgress] = useStateLA(0);

  // Master cycle
  useEffectLA(() => {
    let timers = [];
    const cycle = () => {
      timers.forEach(clearTimeout);
      setScoredRows({});
      setProgress(0);
      setPhase("dropping");
      timers.push(setTimeout(() => setPhase("scanning"), 900));
      // Progress bar
      let p = 0;
      const progInt = setInterval(() => {
        p += 1.5 + Math.random() * 2;
        if (p >= 100) { p = 100; clearInterval(progInt); }
        setProgress(p);
      }, 40);
      timers.push({ clear: () => clearInterval(progInt) });
      // Score rows progressively
      SAMPLE_CONTROLS.slice(0, 6).forEach((c, i) => {
        timers.push(setTimeout(() => {
          setScoredRows((r) => ({ ...r, [c.id]: c.score }));
        }, 1400 + i * 280));
      });
      timers.push(setTimeout(() => setPhase("done"), 3300));
      timers.push(setTimeout(() => {
        setActiveFile((f) => (f + 1) % DEMO_FILES.length);
      }, 4400));
    };
    cycle();
    const rep = setInterval(cycle, 5500);
    return () => {
      clearInterval(rep);
      timers.forEach((t) => t.clear ? t.clear() : clearTimeout(t));
    };
  }, []);

  return (
    <section className="relative bg-gradient-to-b from-[#4F5B6B] via-[#404C5C] to-[#2e3744] text-white overflow-hidden">
      {/* Subtle radial mesh */}
      <div className="absolute inset-0 opacity-50 pointer-events-none" style={{
        background: "radial-gradient(800px circle at 20% 20%, rgba(147,210,204,0.15), transparent 50%), radial-gradient(600px circle at 80% 80%, rgba(147,210,204,0.08), transparent 50%)",
      }} />
      {/* Grid pattern */}
      <div className="absolute inset-0 opacity-[0.04] pointer-events-none" style={{
        backgroundImage: "linear-gradient(rgba(255,255,255,0.5) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,0.5) 1px, transparent 1px)",
        backgroundSize: "32px 32px",
      }} />

      <div className="relative mx-auto max-w-6xl px-6 pt-14 pb-20 md:pt-20 md:pb-28">
        <div className="grid md:grid-cols-12 gap-12 md:gap-10 items-center">
          {/* Copy */}
          <div className="md:col-span-5">
            <span className="inline-flex items-center gap-2 px-3 py-1 rounded-full border border-[#93D2CC]/30 bg-[#93D2CC]/10 text-[11px] font-semibold uppercase tracking-[0.16em] text-[#93D2CC]">
              <span className="h-1.5 w-1.5 rounded-full bg-[#93D2CC] animate-pulse" />
              {kicker}
            </span>
            <h1 className="mt-5 text-[44px] md:text-[58px] lg:text-[64px] leading-[0.98] tracking-[-0.02em] font-semibold" style={{ fontFamily: "Geist, system-ui, sans-serif" }}>
              {headline.split("|").map((part, i) => (
                <span key={i} className={i === 1 ? "text-[#93D2CC]" : ""}>{part}</span>
              ))}
            </h1>
            <p className="mt-5 text-[16.5px] md:text-[18px] leading-relaxed text-white/75 max-w-md">
              Drop in a policy, an export, a screenshot. Control+S maps it to every framework you have, scores maturity, and shows its work.
            </p>
            <div className="mt-8 flex flex-wrap items-center gap-4">
              <button className="inline-flex items-center gap-2 px-6 py-3 rounded-full bg-[#93D2CC] text-[#404C5C] text-[14px] font-bold shadow-[0_14px_40px_-12px_rgba(147,210,204,0.7)] hover:bg-white transition-all hover:scale-[1.02]">
                Access platform
                <Icon name="arrowRight" className="h-4 w-4" strokeWidth={2.4} />
              </button>
              <a href="#how" className="text-[14px] font-medium text-white/65 hover:text-white inline-flex items-center gap-1.5 underline-offset-4 hover:underline">
                <Icon name="play" className="h-3.5 w-3.5 fill-current" /> Watch the 90-sec tour
              </a>
            </div>
            <div className="mt-7 flex items-center gap-5 text-[12px] text-white/55">
              <span className="inline-flex items-center gap-1.5"><Icon name="check" className="h-3.5 w-3.5 text-[#93D2CC]" strokeWidth={3} /> Free credits to start</span>
              <span className="inline-flex items-center gap-1.5"><Icon name="check" className="h-3.5 w-3.5 text-[#93D2CC]" strokeWidth={3} /> No card required</span>
            </div>
          </div>

          {/* Animated demo */}
          <div className="md:col-span-7">
            <LiveDemo phase={phase} progress={progress} scoredRows={scoredRows} activeFile={activeFile} />
          </div>
        </div>
      </div>
    </section>
  );
}

function LiveDemo({ phase, progress, scoredRows, activeFile }) {
  const file = DEMO_FILES[activeFile];
  const scoreColor = (s) => {
    if (s == null) return { bg: "rgba(255,255,255,0.08)", fg: "rgba(255,255,255,0.5)", ring: "rgba(255,255,255,0.15)" };
    if (s >= 4) return { bg: "#93D2CC", fg: "#404C5C", ring: "#93D2CC" };
    if (s >= 2) return { bg: "#F4C28A", fg: "#404C5C", ring: "#F4C28A" };
    return { bg: "#E07A5F", fg: "#FFFFFF", ring: "#E07A5F" };
  };

  return (
    <div className="relative">
      {/* Glow halo */}
      <div className="absolute -inset-8 rounded-[40px] bg-gradient-to-br from-[#93D2CC]/20 via-transparent to-[#93D2CC]/5 blur-2xl" />

      {/* App-window chrome */}
      <div className="relative rounded-2xl border border-white/10 bg-gradient-to-b from-white/[0.07] to-white/[0.02] shadow-[0_30px_80px_-20px_rgba(0,0,0,0.6)] backdrop-blur-xl overflow-hidden">
        {/* Title bar */}
        <div className="flex items-center justify-between px-4 py-3 border-b border-white/8">
          <div className="flex items-center gap-1.5">
            <span className="h-2.5 w-2.5 rounded-full bg-white/15" />
            <span className="h-2.5 w-2.5 rounded-full bg-white/15" />
            <span className="h-2.5 w-2.5 rounded-full bg-white/15" />
          </div>
          <span className="text-[10.5px] font-mono uppercase tracking-[0.2em] text-white/40">control+s · live assessment</span>
          <div className="flex items-center gap-1.5">
            <span className="h-1.5 w-1.5 rounded-full bg-[#93D2CC] animate-pulse" />
            <span className="text-[10px] uppercase tracking-wider text-white/45 font-semibold">live</span>
          </div>
        </div>

        {/* Drop zone */}
        <div className="relative px-4 pt-4">
          <div className={`relative rounded-xl border-2 border-dashed transition-all duration-500 ${phase === "scanning" || phase === "scoring" ? "border-[#93D2CC] bg-[#93D2CC]/8" : "border-white/15 bg-white/[0.02]"}`}>
            <div className="px-4 py-3 flex items-center gap-3">
              {/* File icon */}
              <div
                className={`relative h-10 w-10 rounded-lg flex items-center justify-center font-mono text-[10px] font-bold flex-none transition-all duration-700 ${phase === "dropping" ? "translate-y-[-12px] scale-110" : "translate-y-0 scale-100"}`}
                style={{ backgroundColor: file.color, color: "#404C5C" }}
              >
                {file.type.toUpperCase()}
                {phase === "scanning" && (
                  <span className="absolute inset-0 rounded-lg ring-2 ring-[#93D2CC] animate-ping" />
                )}
              </div>
              <div className="flex-1 min-w-0">
                <p className="text-[13.5px] font-semibold text-white/95 truncate font-mono">{file.name}</p>
                <p className="text-[11px] text-white/50 mt-0.5">
                  {phase === "dropping" && "Receiving file…"}
                  {phase === "scanning" && "Extracting controls…"}
                  {phase === "scoring" && "Mapping to frameworks…"}
                  {phase === "done" && `Mapped to ${Object.keys(scoredRows).length} controls across 4 frameworks`}
                  {phase === "idle" && "Drop here to assess"}
                </p>
              </div>
              <span className="text-[11px] font-mono text-[#93D2CC] tabular-nums font-bold">
                {Math.round(progress)}%
              </span>
            </div>
            {/* Progress bar */}
            <div className="h-0.5 bg-white/5 overflow-hidden rounded-b-xl">
              <div className="h-full bg-gradient-to-r from-[#93D2CC] to-white transition-all duration-100" style={{ width: `${progress}%` }} />
            </div>
          </div>
        </div>

        {/* Controls being scored */}
        <div className="p-4">
          <div className="flex items-center justify-between mb-2.5 px-1">
            <span className="text-[10.5px] uppercase tracking-[0.18em] text-white/40 font-semibold">Controls scoring · CIS v8.1.2</span>
            <span className="text-[10.5px] text-white/40 font-mono">{Object.keys(scoredRows).length}/{SAMPLE_CONTROLS.slice(0, 6).length}</span>
          </div>
          <div className="space-y-1.5">
            {SAMPLE_CONTROLS.slice(0, 6).map((c) => {
              const score = scoredRows[c.id];
              const isScored = score !== undefined;
              const colors = scoreColor(score);
              return (
                <div
                  key={c.id}
                  className={`relative flex items-center gap-3 rounded-lg px-3 py-2 border transition-all duration-500 ${isScored ? "bg-white/[0.04] border-white/10" : "bg-white/[0.01] border-white/5"}`}
                  style={{
                    transform: isScored ? "translateY(0)" : "translateY(2px)",
                    opacity: isScored ? 1 : 0.55,
                  }}
                >
                  <span className="min-w-[3rem] text-[11px] font-mono font-semibold text-white/45">{c.id}</span>
                  <span className="flex-1 truncate text-[13px] text-white/85">{c.title}</span>
                  {isScored && (
                    <span className="hidden sm:inline-flex items-center gap-1 px-1.5 py-0.5 rounded text-[9.5px] uppercase tracking-wider font-bold bg-[#93D2CC]/15 text-[#93D2CC]">
                      <Icon name="sparkles" className="h-2.5 w-2.5" />AI
                    </span>
                  )}
                  <span
                    className={`flex h-7 w-7 flex-none items-center justify-center rounded-md text-[12px] font-bold tabular-nums transition-all duration-300 ${isScored ? "scale-100 opacity-100" : "scale-75 opacity-30"}`}
                    style={{ backgroundColor: colors.bg, color: colors.fg, boxShadow: isScored ? `0 0 0 2px ${colors.ring}33` : "none" }}
                  >
                    {score == null ? (isScored ? "–" : "?") : score}
                  </span>
                </div>
              );
            })}
          </div>

          {/* Footer summary */}
          <div className="mt-4 pt-3 border-t border-white/8 flex items-center justify-between">
            <div className="flex items-center gap-3">
              <span className="text-[10.5px] uppercase tracking-[0.18em] text-white/40 font-semibold">Overall maturity</span>
              <div className="flex items-center gap-0.5">
                {[1,2,3,4,5].map(n => (
                  <div key={n} className="h-1.5 w-5 rounded-sm transition-colors duration-500" style={{ background: phase === "done" ? (n <= 4 ? "#93D2CC" : "rgba(255,255,255,0.1)") : "rgba(255,255,255,0.08)" }} />
                ))}
              </div>
            </div>
            <span className="text-[14px] font-bold text-[#93D2CC] tabular-nums">{phase === "done" ? "4.0 / 5" : "— / 5"}</span>
          </div>
        </div>
      </div>

      {/* Floating "explanation" tooltip */}
      <div
        className={`absolute -right-2 md:-right-6 top-[55%] w-[220px] rounded-xl bg-[#404C5C] border border-[#93D2CC]/40 shadow-2xl px-3.5 py-3 transition-all duration-700 ${phase === "done" ? "opacity-100 translate-y-0" : "opacity-0 translate-y-2"}`}
        style={{ pointerEvents: "none" }}
      >
        <div className="flex items-start gap-2">
          <span className="h-5 w-5 rounded-md bg-[#93D2CC]/20 flex items-center justify-center text-[#93D2CC] flex-none mt-0.5"><Icon name="sparkles" className="h-3 w-3" /></span>
          <div className="min-w-0">
            <p className="text-[10px] uppercase tracking-wider font-bold text-[#93D2CC]">Reasoning</p>
            <p className="mt-1 text-[11.5px] leading-snug text-white/85">
              MFA enrolled for 96% of admin users (Okta export, line 412). Lacks step-up auth for billing. <span className="text-[#93D2CC]">Maturity 4.</span>
            </p>
          </div>
        </div>
      </div>
    </div>
  );
}

function MetricStrip() {
  const metrics = [
    { v: "11→1", l: "Weeks of audit prep, condensed" },
    { v: "3,200+", l: "Controls in our pre-loaded library" },
    { v: "<60s", l: "From upload to maturity score" },
    { v: "16", l: "Frameworks supported" },
  ];
  return (
    <section className="bg-[#FAF7F4]">
      <div className="mx-auto max-w-6xl px-6 py-12">
        <div className="grid grid-cols-2 md:grid-cols-4 divide-y md:divide-y-0 md:divide-x divide-[#404C5C]/15">
          {metrics.map((m, i) => (
            <div key={i} className="px-2 py-5 md:px-6 md:py-2">
              <p className="text-[36px] md:text-[42px] font-bold tracking-tight text-[#404C5C] tabular-nums leading-none">{m.v}</p>
              <p className="mt-2 text-[12px] uppercase tracking-wider text-[#404C5C]/60 font-semibold">{m.l}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function HowItWorksLive() {
  const steps = [
    { n: "01", title: "Connect or upload", body: "Pipe in evidence from Okta, AWS, GitHub, KnowBe4, or just drop a PDF. We don't care where it came from.", icon: "upload" },
    { n: "02", title: "We do the mapping", body: "Every artifact gets parsed, then matched against every control in every framework you have enabled. Reasoning attached.", icon: "sparkles" },
    { n: "03", title: "You review and publish", body: "Accept the score, edit the rationale, or override. Your auditor gets a defensible trail. You get back to work.", icon: "shieldCheck" },
  ];
  return (
    <section id="how" className="bg-[#FAF7F4]">
      <div className="mx-auto max-w-6xl px-6 py-20 md:py-24">
        <div className="text-center max-w-2xl mx-auto mb-14">
          <p className="text-[11px] uppercase tracking-[0.22em] text-[#3a8a82] font-bold mb-3">How it works</p>
          <h2 className="text-[36px] md:text-[48px] leading-[1.05] tracking-tight font-semibold text-[#404C5C]" style={{ fontFamily: "Geist, system-ui, sans-serif" }}>
            Drop. Score. Done.
          </h2>
        </div>
        <div className="grid gap-4 md:grid-cols-3">
          {steps.map((s, i) => (
            <div key={s.n} className="relative group">
              {/* Connector line */}
              {i < 2 && <div className="hidden md:block absolute top-12 left-[calc(100%-12px)] right-[-12px] h-px bg-gradient-to-r from-[#93D2CC] to-transparent z-0" />}
              <div className="relative rounded-2xl border border-[#404C5C]/12 bg-white p-6 hover:border-[#93D2CC] hover:shadow-[0_12px_36px_-12px_rgba(147,210,204,0.4)] transition-all">
                <div className="flex items-center gap-3 mb-4">
                  <span className="h-12 w-12 rounded-xl bg-gradient-to-br from-[#93D2CC] to-[#7cc5be] text-[#404C5C] flex items-center justify-center shadow-[0_8px_20px_-6px_rgba(147,210,204,0.55)]">
                    <Icon name={s.icon} className="h-5 w-5" strokeWidth={2.2} />
                  </span>
                  <span className="text-[11px] uppercase tracking-[0.22em] text-[#404C5C]/45 font-bold font-mono">{s.n}</span>
                </div>
                <h3 className="text-[20px] font-semibold tracking-tight text-[#404C5C] mb-2">{s.title}</h3>
                <p className="text-[14px] leading-relaxed text-[#404C5C]/70">{s.body}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function FrameworksLive() {
  return (
    <section id="frameworks" className="bg-[#404C5C] text-white relative overflow-hidden">
      <div className="absolute inset-0 opacity-[0.04] pointer-events-none" style={{
        backgroundImage: "linear-gradient(rgba(255,255,255,0.5) 1px, transparent 1px), linear-gradient(90deg, rgba(255,255,255,0.5) 1px, transparent 1px)",
        backgroundSize: "40px 40px",
      }} />
      <div className="relative mx-auto max-w-6xl px-6 py-20 md:py-24">
        <div className="text-center max-w-2xl mx-auto mb-12">
          <p className="text-[11px] uppercase tracking-[0.22em] text-[#93D2CC] font-bold mb-3">Pre-loaded</p>
          <h2 className="text-[36px] md:text-[48px] leading-[1.05] tracking-tight font-semibold" style={{ fontFamily: "Geist, system-ui, sans-serif" }}>
            16 frameworks. Zero setup.
          </h2>
          <p className="mt-3 text-white/65 text-[15px]">Enable as many as you need. Map evidence once, score against all.</p>
        </div>
        <div className="grid gap-3 sm:grid-cols-2 lg:grid-cols-3">
          {FRAMEWORK_GROUPS.flatMap((g) => g.frameworks.map((f) => ({ ...f, category: g.category }))).map((f) => (
            <div key={f.name} className="group flex items-center gap-3 rounded-xl border border-white/10 bg-white/[0.04] p-3.5 hover:border-[#93D2CC]/40 hover:bg-white/[0.07] transition-all">
              <span className="h-8 w-8 rounded-lg bg-[#93D2CC]/15 text-[#93D2CC] flex items-center justify-center flex-none group-hover:bg-[#93D2CC] group-hover:text-[#404C5C] transition-colors">
                <Icon name="check" className="h-4 w-4" strokeWidth={3} />
              </span>
              <div className="min-w-0">
                <p className="text-[13.5px] font-semibold truncate">{f.name}</p>
                <p className="text-[11px] text-white/45">{f.publisher}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function ClosingLive() {
  return (
    <section className="bg-[#FAF7F4]">
      <div className="mx-auto max-w-4xl px-6 py-20 md:py-24 text-center">
        <h2 className="text-[36px] md:text-[52px] leading-[1.02] tracking-tight font-semibold text-[#404C5C]" style={{ fontFamily: "Geist, system-ui, sans-serif" }}>
          See it work on <span className="text-[#3a8a82]">your</span> evidence.
        </h2>
        <p className="mt-4 text-[16px] text-[#404C5C]/70 max-w-xl mx-auto">
          Free credits to start. Pay only for what you use.
        </p>
        <div className="mt-8 flex flex-wrap items-center justify-center gap-3">
          <button className="inline-flex items-center gap-2 px-7 py-3.5 rounded-full bg-[#404C5C] text-white text-[14px] font-bold hover:bg-[#2e3744] transition-all">
            Access platform
            <Icon name="arrowRight" className="h-4 w-4" strokeWidth={2.4} />
          </button>
          <a href="mailto:anouar@shadesec.com" className="inline-flex items-center gap-2 px-7 py-3.5 rounded-full border border-[#404C5C]/20 text-[#404C5C] text-[14px] font-semibold hover:bg-[#404C5C]/5 transition-colors">
            Talk to founder
          </a>
        </div>
      </div>
    </section>
  );
}

window.LP_Live = function ({ headline, kicker, sectionOrder }) {
  const sections = {
    proof: <CustomerWall key="proof" tone="light" title="Trusted by security teams at" />,
    metrics: <MetricStrip key="metrics" />,
    how: <HowItWorksLive key="how" />,
    frameworks: <FrameworksLive key="frameworks" />,
  };
  return (
    <div className="bg-[#FAF7F4]">
      <TopNav variant="dark" />
      <HeroLive headline={headline} kicker={kicker} />
      {sectionOrder.map((k) => sections[k])}
      <ClosingLive />
      <Footer tone="dark" />
    </div>
  );
};
})();
