// import LogoChanry from "./assets/clanry-01.svg";
/* global React, ReactDOM */
const { useState, useEffect, useRef, useMemo } = React;

/* ---------- Reveal hook ---------- */
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll(
      ".reveal, .reveal-scale, .reveal-left",
    );
    const io = new IntersectionObserver(
      (entries) => {
        entries.forEach((e) => {
          if (e.isIntersecting) {
            e.target.classList.add("in");
            io.unobserve(e.target);
          }
        });
      },
      { threshold: 0.14, rootMargin: "0px 0px -40px 0px" },
    );
    els.forEach((el) => io.observe(el));
    return () => io.disconnect();
  }, []);
}

/* ---------- Tiny atoms ---------- */
const AcidDot = ({ className = "" }) => (
  <span
    className={
      "inline-block w-1.5 h-1.5 rounded-full align-middle " + className
    }
    style={{ background: "var(--acid)" }}
  />
);

const SectionLabel = ({ children, n }) => (
  <div
    className="flex items-center gap-3 text-[11px] uppercase tracking-[0.22em]"
    style={{ color: "var(--muted)" }}
  >
    <span className="mono-feat" style={{ color: "var(--acid)" }}>
      {n}
    </span>
    <span className="h-px w-8" style={{ background: "var(--border)" }} />
    <span>{children}</span>
  </div>
);

/* ---------- Placeholder card ---------- */
function Placeholder({
  w = 220,
  h = 290,
  label = "PORTRAIT",
  price = "$60",
  persona = "",
  className = "",
  style = {},
  hero = false,
  img = null,
}) {
  return (
    <div
      className={
        "ph-card rounded-[18px] overflow-hidden relative " +
        (hero ? "hero-card " : "") +
        className
      }
      style={{ width: w, height: h, ...style }}
    >
      {img && (
        <div className="ph-img" style={{ backgroundImage: `url('${img}')` }} />
      )}
      <div
        className="absolute inset-0"
        style={{
          background:
            "linear-gradient(180deg, rgba(10,10,10,0.55) 0%, rgba(10,10,10,0.05) 30%, rgba(10,10,10,0.05) 60%, rgba(10,10,10,0.75) 100%)",
        }}
      />
      <div className="ph-scan absolute inset-0 scan opacity-40" />
      <div className="ph-acid-glow" />
      <div
        className="ph-label-row absolute top-3 left-3 right-3 flex items-center justify-between text-[10px] mono-feat uppercase tracking-[0.18em]"
        style={{ color: "rgba(242,242,242,0.8)" }}
      >
        <span>{label}</span>
        <span style={{ color: "var(--acid)" }}>{price}</span>
      </div>
      <div
        className="ph-foot-row absolute bottom-3 left-3 right-3 flex items-center justify-between text-[10px] mono-feat uppercase tracking-[0.18em]"
        style={{ color: "rgba(242,242,242,0.7)" }}
      >
        <span>{persona}</span>
        <span
          className="w-2 h-2 rounded-full"
          style={{ background: "var(--acid)" }}
        />
      </div>
      {!img && (
        <div
          className="ph-slot absolute inset-x-6 top-1/2 -translate-y-1/2 text-center text-[9px] mono-feat uppercase tracking-[0.3em]"
          style={{ color: "rgba(242,242,242,0.25)" }}
        >
          image slot
        </div>
      )}
      {hero && <span className="ph-price-chip">View →</span>}
    </div>
  );
}

/* ---------- Nav ---------- */
function Nav() {
  return (
    <header className="relative z-40">
      <div className="max-w-[1360px] mx-auto px-8 h-[110px] flex items-center justify-between relative">
        <a href="#top" className="flex items-center group">
          <img src="assets/MainLogo.svg" alt="Clanry" className="w-24 h-24" />
        </a>

        <nav
          className="absolute left-1/2 -translate-x-1/2 hidden md:flex items-center gap-9 text-[13.5px]"
          style={{ color: "var(--muted)" }}
        >
          <a className="nav-link" href="#how">
            How it works
          </a>
          <a className="nav-link" href="#pricing">
            Pricing
          </a>
          <a className="nav-link" href="#founding">
            Founding 20
          </a>
          <a className="nav-link" href="#faq">
            FAQ
          </a>
        </nav>
        <div className="flex items-center gap-2.5">
          <a
            href="#claim"
            className="inline-flex px-4 h-9 items-center rounded-full text-[16px] font-semibold cta-acid"
          >
            Claim handle →
          </a>
        </div>
      </div>
    </header>
  );
}

/* ---------- Spots counter (shared across Hero + Founding + Claim) ---------- */
/* Single source of truth: one useSpots() call in App, value passed as prop to every counter. */
function useSpots() {
  const [spots, setSpots] = useState(() => {
    /* Plausible Founding 20 scarcity range: 9–17 out of 20.
       Avoids fake-panic numbers (1–3) and "nobody signed up" numbers (18–19). */
    return 9 + Math.floor(Math.random() * 9);
  });
  useEffect(() => {
    const id = setInterval(() => {
      setSpots((s) => Math.max(3, s - (Math.random() < 0.35 ? 1 : 0)));
    }, 18000);
    return () => clearInterval(id);
  }, []);
  return spots;
}

/* ---------- Hero ---------- */
function Hero({ spots }) {
  const layouts = [
    {
      cls: "float-a",
      style: { top: "12%", left: "4%", "--rot": "-8deg" },
      label: "PERSONA",
      price: "$80",
      persona: "",
      w: 210,
      h: 270,
      img: "assets/imgOne.jpeg",
    },
    {
      cls: "float-b",
      style: { top: "7%", right: "6%", "--rot": "7deg" },
      label: "LoRA PACK",
      price: "$60",
      persona: "",
      w: 200,
      h: 260,
      img: "assets/ImgTwo.avif",
    },
    {
      cls: "float-c",
      style: { bottom: "14%", left: "7%", "--rot": "-4deg" },
      label: "WORKFLOW",
      price: "$30",
      persona: "",
      w: 190,
      h: 240,
      img: "assets/imgThree.avif",
    },
    {
      cls: "float-d",
      style: { bottom: "9%", right: "4%", "--rot": "6deg" },
      label: "SUB TIER",
      price: "$20",
      persona: "",
      w: 210,
      h: 270,
      img: "assets/imgFour.avif",
    },
    {
      cls: "float-a",
      style: { top: "40%", left: "-3%", "--rot": "-12deg" },
      label: "AGENCY",
      price: "$60",
      persona: "",
      w: 180,
      h: 230,
      img: "assets/imgFive.avif",
    },
    {
      cls: "float-b",
      style: { top: "46%", right: "-2%", "--rot": "10deg" },
      label: "DROP",
      price: "$30",
      persona: "",
      w: 180,
      h: 230,
      img: "assets/imgSix.avif",
    },
  ];
  return (
    <section
      id="top"
      className="relative pt-[40px] pb-[60px] overflow-hidden"
    >
      <div className="mesh" />
      <div className="relative max-w-[1360px] mx-auto px-8 min-h-[760px]">
        {/* floating cards */}
        {layouts.map((l, i) => (
          <div
            key={i}
            className={"hero-card-wrap absolute " + l.cls}
            style={l.style}
          >
            <Placeholder
              hero
              img={l.img}
              w={l.w}
              h={l.h}
              label={l.label}
              price={l.price}
              persona={l.persona}
              className="card-shadow"
            />
          </div>
        ))}

        {/* center scrim */}
        <div
          aria-hidden="true"
          className="absolute left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 w-[920px] h-[640px] pointer-events-none"
          style={{
            background:
              "radial-gradient(closest-side, rgba(10,10,10,0.98), rgba(10,10,10,0.85) 50%, rgba(10,10,10,0.4) 75%, transparent 95%)",
          }}
        />

        {/* center content — pointer-events:none so hero cards underneath stay
            hoverable even where text overlaps. CTAs re-enable pointer-events below. */}
        <div
          className="relative z-10 flex flex-col items-center text-center pt-8"
          style={{ pointerEvents: "none" }}
        >
          <div
            className="inline-flex items-center gap-2 px-3 h-8 rounded-full mb-8 mono-feat text-[11px] uppercase tracking-[0.22em]"
            style={{
              background: "rgba(185,255,58,0.08)",
              border: "1px solid rgba(185,255,58,0.35)",
              color: "var(--acid)",
            }}
          >
            <AcidDot className="ring-pulse rounded-full" />
            Founding 20 ·{" "}
            <span className="tick" key={spots}>
              {spots}
            </span>{" "}
            spots left
          </div>

          <h1
            className="font-bold tight w-full max-w-[770px]"
            style={{
              fontSize: "clamp(44px, 6.2vw, 92px)",
              lineHeight: 0.96,
              letterSpacing: "-0.035em",
            }}
          >
            <span className="block">
              <span className="rise" data-d="1">
                <span>
                  Get paid for <br /> your AI work. <br /> Without the{" "}
                  <span className="text-[#B9FF3A]">bans.</span>
                </span>
              </span>
            </span>
            <span className="block mt-1">
              {/* <span className="rise" data-d="2"><span>Keep </span></span>
              <span className="rise" data-d="2"><span className="hl-under" style={{color:'var(--acid)'}}>97%</span></span>
              <span className="rise" data-d="2"><span>. No bans.</span></span> */}
            </span>
          </h1>

          <p
            className="mt-6 max-w-[640px] text-[17px] leading-[1.55] reveal"
            style={{ color: "var(--muted)" }}
          >
            Clanry is the creator economy for AI artists, storytellers, and
            builders. Sell personas, LoRAs, workflows, and subscriptions —
            payouts that won't vanish overnight.
          </p>

          <div
            className="mt-7 flex flex-wrap items-center justify-center gap-3 reveal"
            data-d="1"
            style={{ pointerEvents: "auto" }}
          >
            <a
              href="#claim"
              className="inline-flex items-center h-12 px-6 rounded-full text-[16px] font-semibold cta-acid gap-2"
            >
              Claim your handle <span aria-hidden>→</span>
            </a>
            <a
              href="#how"
              className="inline-flex items-center h-12 px-6 rounded-full text-[16px] cta-outline"
            >
              See how it works
            </a>
          </div>

          <div
            className="mt-14 md:mt-10 flex flex-wrap items-center justify-center gap-x-7 gap-y-4 text-[12px] mono-feat uppercase tracking-[0.18em] reveal"
            data-d="2"
            style={{ color: "var(--muted)" }}
          >
            <span className="inline-flex items-center gap-2">
              <AcidDot />
              3% Founding fee
            </span>
            <span className="inline-flex items-center gap-2">
              <AcidDot />
              Non-custodial USDC
            </span>
            <span className="inline-flex items-center gap-2">
              <AcidDot />
              NSFW-friendly policies
            </span>
          </div>
        </div>
      </div>

      {/* marquee */}
      <div
        className="mt-8 border-y overflow-hidden"
        style={{ borderColor: "var(--border)" }}
      >
        <div
          className="marq py-4 text-[13px] uppercase mono-feat tracking-[0.22em]"
          style={{ color: "var(--muted)" }}
        >
          {Array.from({ length: 2 }).flatMap((_, k) =>
            [
              "Personas",
              "LoRA Packs",
              "Workflows",
              "Subscriptions",
              "Agency Bundles",
              "One-off Drops",
              "Custom Requests",
              "Fan Tiers",
              "Creator Collabs",
              "API Access",
            ].map((t, i) => (
              <span
                key={k + "-" + i}
                className="inline-flex items-center gap-3"
              >
                <AcidDot /> {t}
              </span>
            )),
          )}
        </div>
      </div>
    </section>
  );
}

/* ---------- Problem (THE REALITY FOR AI CREATORS) ---------- */
function Problem() {
  const cards = [
    {
      name: "STRIPE",
      lines: [
        "Bans AI content without warning.",
        "Freezes funds for 90+ days.",
        "No human appeal process.",
      ],
      stat: "3 of 5 AI creators banned in 2025–2026",
    },
    {
      name: "FANVUE / PATREON",
      lines: [
        "Takes 12–20% of every dollar.",
        "Restricts NSFW-adjacent work.",
        "Can delete your account overnight.",
      ],
      stat: "$18K/mo lost on a $100K business",
    },
    {
      name: "GUMROAD",
      lines: [
        "10% fees + payment processor fees.",
        "AI content in gray zone.",
        "Zero creator ownership.",
      ],
      stat: "Built for ecommerce, not creators",
    },
  ];
  return (
    <section
      id="reality"
      className="relative py-32 border-t"
      style={{ borderColor: "var(--border)" }}
    >
      <div className="max-w-[1360px] mx-auto px-8">
        <div className="reveal">
          <SectionLabel n="01">The reality for AI creators</SectionLabel>
        </div>
        <h2
          className="mt-6 font-semibold tight reveal"
          data-d="1"
          style={{
            fontSize: "clamp(40px, 5vw, 72px)",
            lineHeight: 0.98,
            maxWidth: "16ch",
          }}
        >
          Every payment platform is built to{" "}
          <span style={{ color: "var(--danger)" }}>betray you</span>.
        </h2>

        <div
          className="mt-14 grid grid-cols-1 md:grid-cols-3 gap-px"
          style={{
            background: "var(--border)",
            border: "1px solid var(--border)",
          }}
        >
          {cards.map((c, i) => (
            <div
              key={i}
              className="betray-card reveal-scale relative p-8 scan"
              data-d={i + 1}
              style={{ background: "var(--surface)" }}
            >
              <div
                className="absolute top-0 left-0 px-2 py-1 mono-feat text-[10px] tracking-[0.2em]"
                style={{ color: "var(--dim)" }}
              >
                0{i + 1}
              </div>
              <div className="flex items-center justify-between">
                <div className="font-semibold text-[13px] tracking-[0.22em]">
                  {c.name}
                </div>
                <div
                  className="betray-chip mono-feat text-[10px] tracking-[0.22em] px-2 py-1 rounded-sm"
                  style={{
                    background: "rgba(255,59,59,0.1)",
                    color: "var(--danger)",
                    border: "1px solid rgba(255,59,59,0.3)",
                  }}
                >
                  BANS YOU
                </div>
              </div>
              <div className="mt-8 mono-feat text-[13.5px] leading-[1.9]">
                {c.lines.map((l, j) => (
                  <div key={j} className="flex gap-3">
                    <span
                      style={{ color: "var(--danger)" }}
                      className="select-none"
                    >
                      ×
                    </span>
                    <span>{l}</span>
                  </div>
                ))}
              </div>
              <div
                className="betray-stat mt-10 pt-5 border-t mono-feat text-[12px]"
                style={{ borderColor: "var(--border)", color: "var(--muted)" }}
              >
                <span style={{ color: "var(--acid)" }}>→</span> {c.stat}
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------- Flips (Clanry flips the model) ---------- */
function Flips() {
  const items = [
    {
      title: "Unbannable by design",
      body: "Policies written by creators who’ve been banned. Real appeals, time-boxed reviews, no silent freezes.",
    },
    {
      title: "Instant, non-custodial.",
      body: "USDC straight to your wallet. Founding 20 gets it the second a buyer pays — new creators weekly after 72h fraud review. We never hold your money.",
    },
    {
      title: "Built by banned creators",
      body: "We’ve been deplatformed everywhere too. Clanry is the platform we wished existed.",
    },
  ];
  return (
    <section
      className="relative py-32 border-t"
      style={{ borderColor: "var(--border)" }}
    >
      <div className="max-w-[1360px] mx-auto px-8">
        <div className="flex flex-col md:flex-row md:items-end md:justify-between gap-6">
          <h2
            className="reveal font-semibold tight"
            style={{
              fontSize: "clamp(40px, 5vw, 72px)",
              lineHeight: 0.98,
              maxWidth: "16ch",
            }}
          >
            Clanry{" "}
            <span style={{ color: "var(--acid)" }} className="hl-under">
              flips
            </span>{" "}
            <br />
            the model.
          </h2>
          <div className="reveal">
            <SectionLabel n="02">The fix</SectionLabel>
          </div>
        </div>
        <div className="mt-16 grid md:grid-cols-3 gap-5">
          {items.map((it, i) => (
            <div key={i} className="flip-card reveal" data-d={i + 1}>
              <div className="flex items-center gap-3">
                <svg
                  className="flip-icon"
                  viewBox="0 0 32 32"
                  width="30"
                  height="30"
                  fill="none"
                  stroke="var(--acid)"
                  strokeWidth="1.4"
                >
                  <polygon points="16,4 28,26 4,26" />
                  {i === 0 && (
                    <>
                      <line x1="16" y1="12" x2="16" y2="20" />
                      <circle cx="16" cy="23" r="0.8" fill="var(--acid)" />
                    </>
                  )}
                  {i === 1 && (
                    <polyline
                      points="17,12 13.5,19.5 16.5,19.5 14,25"
                      stroke="var(--acid)"
                      strokeWidth="1.6"
                      strokeLinecap="round"
                      strokeLinejoin="round"
                      fill="none"
                    />
                  )}
                  {i === 2 && (
                    <>
                      <line x1="10" y1="20" x2="22" y2="20" />
                      <line x1="13" y1="17" x2="19" y2="17" />
                    </>
                  )}
                </svg>
                <div
                  className="mono-feat text-[11px] tracking-[0.22em]"
                  style={{ color: "var(--dim)" }}
                >
                  0{i + 1}
                </div>
              </div>
              <h3 className="mt-6 text-[24px] md:text-[26px] font-semibold tight">
                {it.title}
              </h3>
              <p
                className="mt-3 text-[14.5px] leading-[1.6]"
                style={{ color: "var(--muted)" }}
              >
                {it.body}
              </p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

/* ---------- How it works ---------- */
function How() {
  const steps = [
    {
      n: "01",
      t: "Claim your handle",
      b: "clanry.com/yourname. Yours forever. Takes 12 seconds.",
    },
    {
      n: "02",
      t: "Set up your shop",
      b: "Personas, LoRAs, workflows, subs, requests. Price anything.",
    },
    {
      n: "03",
      t: "Get paid",
      b: "USDC straight to your wallet. Founding 20: instant. New creators: weekly after 72h fraud review.",
    },
    {
      n: "04",
      t: "Keep building",
      b: "Ban-proof policies. Real disputes. We back AI creators.",
    },
  ];
  return (
    <section
      id="how"
      className="relative py-32 border-t"
      style={{ borderColor: "var(--border)" }}
    >
      <div className="max-w-[1360px] mx-auto px-8">
        <div className="flex items-end justify-between gap-8 flex-wrap">
          <div>
            <div className="reveal">
              <SectionLabel n="03">How it works</SectionLabel>
            </div>
            <h2
              className="mt-6 font-semibold tight reveal"
              data-d="1"
              style={{ fontSize: "clamp(40px, 5vw, 72px)", lineHeight: 0.96 }}
            >
              Four steps. <br />
              Zero gatekeepers.
            </h2>
          </div>
          <a
            href="#claim"
            className="inline-flex items-center h-11 px-5 rounded-full text-[16px] cta-outline reveal font-semibold"
            data-d="2"
          >
            Skip ahead — claim handle →
          </a>
        </div>

        <div className="mt-16 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
          {steps.map((s, i) => (
            <div
              key={i}
              className="how-card reveal relative rounded-[18px] p-7 h-[300px] flex flex-col justify-between overflow-hidden group"
              data-d={i + 1}
              style={{
                background: "var(--surface)",
                border: "1px solid var(--border)",
              }}
            >
              <div className="how-glow" />
              <div className="flex items-start justify-between relative">
                <span
                  className="how-num mono-feat text-[48px] leading-none"
                  style={{ color: "var(--acid)", letterSpacing: "-0.05em" }}
                >
                  {s.n}
                </span>
                <span
                  className="how-arrow w-9 h-9 rounded-full flex items-center justify-center"
                  style={{ border: "1px solid var(--border)" }}
                >
                  <svg width="14" height="14" viewBox="0 0 14 14" fill="none">
                    <path
                      d="M1 7h12M8 2l5 5-5 5"
                      stroke="currentColor"
                      strokeWidth="1.4"
                      strokeLinecap="round"
                      strokeLinejoin="round"
                    />
                  </svg>
                </span>
              </div>
              <div className="relative">
                <h3 className="text-[22px] font-semibold tight leading-tight">
                  {s.t}
                </h3>
                <p
                  className="mt-3 text-[14px] leading-[1.55]"
                  style={{ color: "var(--muted)" }}
                >
                  {s.b}
                </p>
              </div>
              <div
                className="absolute bottom-0 left-0 h-[2px] w-0 transition-all duration-700 group-hover:w-full"
                style={{ background: "var(--acid)" }}
              />
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

window.Nav = Nav;
window.Hero = Hero;
window.Problem = Problem;
window.Flips = Flips;
window.How = How;
window.useSpots = useSpots;
window.useReveal = useReveal;
window.Placeholder = Placeholder;
window.SectionLabel = SectionLabel;
window.AcidDot = AcidDot;
