function SiteSignup() {
  return (
    <section id="signup" style={{
      marginTop: 160, position: "relative", paddingBottom: 40,
    }}>
      {/* Aurora wash */}
      <div aria-hidden style={{
        position: "absolute", inset: -60, zIndex: 0, pointerEvents: "none",
        background:
          "radial-gradient(ellipse 700px 500px at 18% 20%, rgba(0,212,255,0.18), transparent 60%)," +
          "radial-gradient(ellipse 800px 600px at 82% 80%, rgba(196,77,255,0.18), transparent 60%)",
        filter: "blur(8px)",
        animation: "ps-aurora-drift 18s ease-in-out infinite",
      }}/>

      <div className="ps-section" style={{ position: "relative", zIndex: 1 }}>
        <div style={{ textAlign: "center", marginBottom: 56 }}>
          <div className="ps-eyebrow" style={{ marginBottom: 14 }}>— Two doors in —</div>
          <h2 style={{
            margin: 0, fontFamily: "var(--font-display)",
            fontSize: 64, fontWeight: 500,
            letterSpacing: "-0.025em", color: "var(--fg-1)",
            lineHeight: 1.04, maxWidth: 840, marginInline: "auto",
            textWrap: "balance",
          }}>
            Shaping the future of <span style={{
              background: "var(--ps-gradient)",
              WebkitBackgroundClip: "text", backgroundClip: "text",
              WebkitTextFillColor: "transparent", color: "transparent",
            }}>immersive play.</span>
          </h2>
          <p style={{
            margin: "20px auto 0", maxWidth: 580, color: "var(--fg-2)",
            fontSize: 17, lineHeight: 1.55,
          }}>
            Whether you play or you make games — we'd love you in early.
            Pick your door below. Invites ship in cohorts.
          </p>
        </div>

        <div className="ps-signup-grid" style={{
          display: "grid", gridTemplateColumns: "1fr 1fr", gap: 24,
        }}>
          <BetaCard />
          <StudioCard />
        </div>

        {/* Reassurance row */}
        <div style={{
          marginTop: 40, display: "flex", flexWrap: "wrap",
          justifyContent: "center", gap: 36,
          fontFamily: "var(--font-display)", fontSize: 13,
          color: "var(--fg-3)", letterSpacing: "0.04em",
        }}>
          <ReassureItem>No spam · we email once a cohort</ReassureItem>
          <ReassureItem>Beta is free · headset required</ReassureItem>
          <ReassureItem>Developer enquiries reviewed weekly</ReassureItem>
        </div>
      </div>
    </section>
  );
}

function ReassureItem({ children }) {
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
      <svg width="14" height="14" viewBox="0 0 24 24" fill="none"
           stroke="url(#rg)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
        <defs>
          <linearGradient id="rg" x1="0" y1="0" x2="1" y2="1">
            <stop offset="0" stopColor="#00D4FF"/>
            <stop offset="1" stopColor="#C44DFF"/>
          </linearGradient>
        </defs>
        <circle cx="12" cy="12" r="9"/>
        <path d="M8 12l3 3 5-6"/>
      </svg>
      {children}
    </span>
  );
}

/* ──────────────────────────────────────────────────────────
   BETA CARD — players · cyan accent
   ────────────────────────────────────────────────────────── */
function BetaCard() {
  const [email, setEmail] = React.useState("");
  const [headset, setHeadset] = React.useState("Quest 3");
  const [referrer, setReferrer] = React.useState("");
  const [submitted, setSubmitted] = React.useState(false);
  const [sending, setSending] = React.useState(false);
  const [error, setError] = React.useState("");

  const handle = async (e) => {
    e.preventDefault();
    if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) {
      setError("That doesn't look like an email.");
      return;
    }
    setError("");
    setSending(true);
    try {
      await window.PSForms.send("beta", {
        email: email,
        headset: headset,
        heard_about_us: referrer || "—",
      });
      setSubmitted(true);
    } catch (err) {
      setError(err.message || "Couldn't send just now — please try again in a moment.");
    }
    setSending(false);
  };

  return (
    <div id="signup-beta" className="ps-card" style={{
      padding: 32, display: "grid", gap: 18,
      borderColor: "rgba(0,212,255,0.30)",
      boxShadow: "0 0 56px rgba(0,212,255,0.12), inset 0 0 0 1px rgba(0,212,255,0.06)",
    }}>
      <div aria-hidden style={{
        position: "absolute", left: -80, top: -80, width: 240, height: 240,
        borderRadius: 999, background: "var(--ps-cyan)",
        opacity: 0.10, filter: "blur(60px)", pointerEvents: "none",
      }}/>

      <div style={{
        display: "flex", justifyContent: "space-between", alignItems: "center",
        flexWrap: "wrap", gap: 10,
      }}>
        <span style={{
          fontFamily: "var(--font-mono)", fontSize: 10,
          letterSpacing: "0.18em", color: "var(--ps-cyan)",
          padding: "5px 10px", borderRadius: 4,
          border: "1px solid rgba(0,212,255,0.30)",
          background: "rgba(0,212,255,0.06)",
          whiteSpace: "nowrap",
        }}>// 01 · PLAYERS</span>
        <span style={{
          fontFamily: "var(--font-mono)", fontSize: 10,
          letterSpacing: "0.18em", color: "var(--fg-3)",
          whiteSpace: "nowrap",
        }}>BATA TEST COHORT 2026</span>
      </div>

      <h3 style={{
        margin: "8px 0 0", fontFamily: "var(--font-display)",
        fontSize: 36, fontWeight: 500,
        letterSpacing: "-0.02em", color: "var(--fg-1)", lineHeight: 1.05,
      }}>
        Play it first.<br/>
        <span style={{ color: "var(--ps-cyan)" }}>Beta tester.</span>
      </h3>

      <p style={{
        margin: 0, color: "var(--fg-2)", fontSize: 15,
        lineHeight: 1.55, textWrap: "pretty",
      }}>
        Bring your headset, your friends, and your couch. We ship
        invites monthly and listen to every bug report like it's
        a love letter.
      </p>

      {!submitted ? (
        <form onSubmit={handle} style={{ display: "grid", gap: 12, marginTop: 8 }}>
          <Field label="Email">
            <input className="ps-input" type="email"
                   placeholder="you@somewhere.xyz"
                   value={email}
                   onChange={(e)=>{setEmail(e.target.value); setError("");}} />
          </Field>

          <Field label="Headset">
            <SelectChips
              options={["Quest 3", "Quest 3S", "Vision Pro", "Pico 4 Ultra"]}
              value={headset} onChange={setHeadset}
              accent="cyan"
            />
          </Field>

          <Field label="How'd you hear about us? (optional)">
            <input className="ps-input" type="text"
                   placeholder="A friend, a tweet, a vision in a dream"
                   value={referrer}
                   onChange={(e)=>setReferrer(e.target.value)} />
          </Field>

          {error && (
            <div style={{
              fontSize: 12, color: "#FF9090", fontFamily: "var(--font-mono)",
              letterSpacing: "0.04em",
            }}>// {error}</div>
          )}

          <button type="submit" disabled={sending} style={{
            marginTop: 6,
            fontFamily: "var(--font-display)", fontSize: 15, fontWeight: 500,
            letterSpacing: "0.04em", padding: "14px 22px", borderRadius: 999,
            border: 0, color: "#0A0C14", cursor: sending ? "wait" : "pointer",
            opacity: sending ? 0.7 : 1,
            background: "linear-gradient(90deg, #00D4FF, #4AA6BF, #8A3FFC)",
            animation: "ps-glow-pulse-cyan 3.6s var(--ease-in-out) infinite",
            transition: "transform 200ms",
          }}
            onMouseDown={(e)=>e.currentTarget.style.transform="scale(0.97)"}
            onMouseUp={(e)=>e.currentTarget.style.transform="scale(1)"}
            onMouseLeave={(e)=>e.currentTarget.style.transform="scale(1)"}
          >{sending ? "Sending…" : "Join the waitlist →"}</button>
        </form>
      ) : (
        <SuccessState
          accent="cyan"
          title="You're in queue."
          body={`Look out for an email from us when Cohort 24.3 opens. Position #${1000 + Math.floor(Math.random()*450)} on the waitlist.`}
          next={[
            "Check your inbox for a verification email.",
            "We'll send a setup guide 24 h before your slot opens.",
            "Want to skip the line? Refer a friend from your dashboard.",
          ]}
        />
      )}
    </div>
  );
}

/* ──────────────────────────────────────────────────────────
   STUDIO CARD — designers/producers · purple accent
   ────────────────────────────────────────────────────────── */
function StudioCard() {
  const [email, setEmail] = React.useState("");
  const [studio, setStudio] = React.useState("");
  const [project, setProject] = React.useState("");
  const [stage, setStage] = React.useState("In print");
  const [submitted, setSubmitted] = React.useState(false);
  const [sending, setSending] = React.useState(false);
  const [error, setError] = React.useState("");

  const handle = async (e) => {
    e.preventDefault();
    if (!/^[^@\s]+@[^@\s]+\.[^@\s]+$/.test(email)) {
      setError("That doesn't look like an email.");
      return;
    }
    if (studio.trim().length < 2) {
      setError("Tell us who you are — even a working name is fine.");
      return;
    }
    setError("");
    setSending(true);
    try {
      await window.PSForms.send("developer", {
        email: email,
        studio_or_publisher: studio,
        game: project || "—",
        stage: stage,
      });
      setSubmitted(true);
    } catch (err) {
      setError(err.message || "Couldn't send just now — please try again in a moment.");
    }
    setSending(false);
  };

  return (
    <div id="signup-studio" className="ps-card" style={{
      padding: 32, display: "grid", gap: 18,
      borderColor: "rgba(138,63,252,0.30)",
      boxShadow: "0 0 56px rgba(138,63,252,0.16), inset 0 0 0 1px rgba(138,63,252,0.08)",
    }}>
      <div aria-hidden style={{
        position: "absolute", right: -80, bottom: -80, width: 260, height: 260,
        borderRadius: 999, background: "var(--ps-purple)",
        opacity: 0.14, filter: "blur(60px)", pointerEvents: "none",
      }}/>

      <div style={{
        display: "flex", justifyContent: "space-between", alignItems: "center",
        flexWrap: "wrap", gap: 10,
      }}>
        <span style={{
          fontFamily: "var(--font-mono)", fontSize: 10,
          letterSpacing: "0.18em", color: "var(--ps-magenta)",
          padding: "5px 10px", borderRadius: 4,
          border: "1px solid rgba(196,77,255,0.30)",
          background: "rgba(196,77,255,0.06)",
          whiteSpace: "nowrap",
        }}>// 02 · DEVELOPERS</span>
        <span style={{
          fontFamily: "var(--font-mono)", fontSize: 10,
          letterSpacing: "0.18em", color: "var(--fg-3)",
          whiteSpace: "nowrap",
        }}>STUDIOS · PUBLISHERS · DESIGNERS</span>
      </div>

      <h3 style={{
        margin: "8px 0 0", fontFamily: "var(--font-display)",
        fontSize: 36, fontWeight: 500,
        letterSpacing: "-0.02em", color: "var(--fg-1)", lineHeight: 1.05,
      }}>
        Bring your game.<br/>
        <span style={{ color: "var(--ps-magenta)" }}>To Playseer.</span>
      </h3>

      <p style={{
        margin: 0, color: "var(--fg-2)", fontSize: 15,
        lineHeight: 1.55, textWrap: "pretty",
      }}>
        Want your game on Playseer? Tell us what you make and who
        plays it. White-glove onboarding, direct line to engineering —
        we build it with you.
      </p>

      {!submitted ? (
        <form onSubmit={handle} style={{ display: "grid", gap: 12, marginTop: 8 }}>
          <Field label="Work email">
            <input className="ps-input" type="email"
                   placeholder="you@studio.com"
                   value={email}
                   onChange={(e)=>{setEmail(e.target.value); setError("");}} />
          </Field>

          <div className="ps-field-row" style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 12 }}>
            <Field label="Studio / publisher">
              <input className="ps-input" type="text"
                     placeholder="Northgate Games"
                     value={studio}
                     onChange={(e)=>{setStudio(e.target.value); setError("");}} />
            </Field>
            <Field label="Your game">
              <input className="ps-input" type="text"
                     placeholder="Title or working name"
                     value={project}
                     onChange={(e)=>setProject(e.target.value)} />
            </Field>
          </div>

          <Field label="Where is the game today?">
            <SelectChips
              options={["Concept", "In print", "Established", "Classic"]}
              value={stage} onChange={setStage}
              accent="purple"
            />
          </Field>

          {error && (
            <div style={{
              fontSize: 12, color: "#FF9090", fontFamily: "var(--font-mono)",
              letterSpacing: "0.04em",
            }}>// {error}</div>
          )}

          <button type="submit" disabled={sending} style={{
            marginTop: 6,
            fontFamily: "var(--font-display)", fontSize: 15, fontWeight: 500,
            letterSpacing: "0.04em", padding: "14px 22px", borderRadius: 999,
            border: 0, color: "#0A0C14", cursor: sending ? "wait" : "pointer",
            opacity: sending ? 0.7 : 1,
            background: "linear-gradient(90deg, #8A3FFC, #C44DFF, #FF6FD8)",
            animation: "ps-glow-pulse 3.6s var(--ease-in-out) infinite",
            transition: "transform 200ms",
          }}
            onMouseDown={(e)=>e.currentTarget.style.transform="scale(0.97)"}
            onMouseUp={(e)=>e.currentTarget.style.transform="scale(1)"}
            onMouseLeave={(e)=>e.currentTarget.style.transform="scale(1)"}
          >{sending ? "Sending…" : "Send enquiry →"}</button>
        </form>
      ) : (
        <SuccessState
          accent="purple"
          title="Enquiry received."
          body={`Thanks${studio ? `, ${studio}` : ""} — we review developer enquiries weekly. We'll be in touch within 7 days.`}
          next={[
            "We'll send a short async call link if it's a fit.",
            "Bring rules, models, or photos of the game — anything helps.",
            "Onboarding is white-glove from day one.",
          ]}
        />
      )}
    </div>
  );
}

/* ── Small subcomponents ── */
function Field({ label, children }) {
  return (
    <label style={{ display: "grid", gap: 6 }}>
      <span style={{
        fontFamily: "var(--font-display)", fontSize: 11, fontWeight: 500,
        letterSpacing: "0.16em", textTransform: "uppercase",
        color: "var(--fg-3)",
      }}>{label}</span>
      {children}
    </label>
  );
}

function SelectChips({ options, value, onChange, accent }) {
  const accentBorder = accent === "cyan"
    ? "rgba(0,212,255,0.55)" : "rgba(196,77,255,0.55)";
  const accentBg = accent === "cyan"
    ? "rgba(0,212,255,0.08)" : "rgba(196,77,255,0.08)";
  const accentColor = accent === "cyan"
    ? "var(--ps-cyan)" : "var(--ps-magenta)";
  return (
    <div style={{ display: "flex", flexWrap: "wrap", gap: 6 }}>
      {options.map((opt) => {
        const on = opt === value;
        return (
          <button key={opt} type="button"
                  onClick={()=>onChange(opt)}
                  style={{
                    fontFamily: "var(--font-display)", fontSize: 13,
                    padding: "9px 14px", borderRadius: 999, cursor: "pointer",
                    background: on ? accentBg : "rgba(10,12,20,0.55)",
                    color: on ? accentColor : "var(--fg-2)",
                    border: `1px solid ${on ? accentBorder : "rgba(255,255,255,0.10)"}`,
                    transition: "all 180ms var(--ease-out)",
                  }}>
            {opt}
          </button>
        );
      })}
    </div>
  );
}

function SuccessState({ accent, title, body, next }) {
  const accentColor = accent === "cyan" ? "var(--ps-cyan)" : "var(--ps-magenta)";
  return (
    <div style={{
      display: "grid", gap: 14,
      animation: "ps-reveal 500ms var(--ease-out) both",
      padding: "20px 22px",
      borderRadius: 14,
      background: "rgba(10,12,20,0.55)",
      border: `1px solid ${accent === "cyan" ? "rgba(0,212,255,0.30)" : "rgba(196,77,255,0.30)"}`,
    }}>
      <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
        <div style={{
          width: 36, height: 36, borderRadius: 999,
          background: accent === "cyan"
            ? "rgba(0,212,255,0.14)" : "rgba(196,77,255,0.14)",
          display: "grid", placeItems: "center",
          border: `1px solid ${accent === "cyan" ? "rgba(0,212,255,0.40)" : "rgba(196,77,255,0.40)"}`,
        }}>
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none"
               stroke={accent === "cyan" ? "#00D4FF" : "#C44DFF"}
               strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
            <path d="M5 12l4 4L20 6"/>
          </svg>
        </div>
        <div>
          <div style={{
            fontFamily: "var(--font-display)", fontSize: 18, fontWeight: 600,
            color: "var(--fg-1)",
          }}>{title}</div>
          <div style={{
            fontFamily: "var(--font-mono)", fontSize: 10,
            letterSpacing: "0.16em", color: accentColor,
            marginTop: 3,
          }}>// CONFIRMED</div>
        </div>
      </div>
      <p style={{
        margin: 0, color: "var(--fg-2)", fontSize: 14, lineHeight: 1.55,
      }}>{body}</p>

      <div style={{ display: "grid", gap: 8, marginTop: 4 }}>
        {next.map((n, i) => (
          <div key={i} style={{
            display: "flex", gap: 10, alignItems: "flex-start",
            fontSize: 13, color: "var(--fg-2)", lineHeight: 1.5,
          }}>
            <span style={{
              fontFamily: "var(--font-mono)", fontSize: 10,
              color: accentColor, letterSpacing: "0.14em",
              minWidth: 22, marginTop: 2,
            }}>{`0${i+1}`}</span>
            <span>{n}</span>
          </div>
        ))}
      </div>
    </div>
  );
}

window.SiteSignup = SiteSignup;
