/* Chameleon Space — site chrome (header + footer) + shared helpers. */
const CS = window.ChameleonSpaceDesignSystem_1abbe1;
const { Button, Eyebrow, Marquee } = CS;

/* ---- shared: scroll-reveal wrapper ---- */
function Reveal({ children, delay = 0, tag = "div", className = "", style }) {
  const ref = React.useRef(null);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const show = () => { if (ref.current) ref.current.classList.add("in"); };
    let io;
    if ("IntersectionObserver" in window) {
      io = new IntersectionObserver((es) => {
        es.forEach((e) => { if (e.isIntersecting) { show(); io.unobserve(e.target); } });
      }, { threshold: 0.1, rootMargin: "0px 0px -6% 0px" });
      io.observe(el);
    }
    // Reliable fallback: reveal right after mount regardless of viewport/visibility state.
    const t = setTimeout(show, 80 + delay);
    return () => { if (io) io.disconnect(); clearTimeout(t); };
  }, [delay]);
  const Tag = tag;
  return <Tag ref={ref} className={("reveal " + className).trim()} style={{ transitionDelay: delay + "ms", ...style }}>{children}</Tag>;
}

/* ---- shared: section header (eyebrow + big Anton headline) ---- */
function SectionHead({ kicker, title, note, invert, max = 900, align = "left" }) {
  return (
    <div style={{ display: "flex", alignItems: "flex-end", justifyContent: "space-between", gap: 24, flexWrap: "wrap" }}>
      <div style={{ maxWidth: max, textAlign: align }}>
        <Eyebrow>{kicker}</Eyebrow>
        <h2 style={{
          fontFamily: "var(--font-display)", textTransform: "uppercase",
          fontSize: "clamp(34px, 5vw, 62px)", lineHeight: .92, letterSpacing: "-.015em",
          margin: "14px 0 0", color: invert ? "var(--paper)" : "var(--ink)", textWrap: "balance"
        }}>{title}</h2>
      </div>
      {note ? <span style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 12, textTransform: "uppercase", letterSpacing: ".14em", color: "var(--text-dim)", whiteSpace: "nowrap", paddingBottom: 6 }}>{note}</span> : null}
    </div>
  );
}

/* ---- header ---- */
const NAV = [
  ["Our mission", "mission"],
  ["What we’re about", "about"],
  ["Opening soon", "opening"],
  ["The scholarship", "scholarship"],
  ["Apply", "apply"],
];
function SiteHeader() {
  const [open, setOpen] = React.useState(false);
  const go = (id) => (e) => { e.preventDefault(); setOpen(false); const t = document.getElementById(id); if (t) t.scrollIntoView({ behavior: "smooth" }); };
  return (
    <header style={{
      position: "sticky", top: 0, zIndex: 60,
      display: "flex", alignItems: "center", justifyContent: "space-between",
      padding: "12px clamp(16px, 4vw, 36px)", background: "var(--paper)",
      borderBottom: "3px solid var(--ink)"
    }}>
      <a href="#top" onClick={go("top")} style={{ display: "flex", alignItems: "center", gap: 12, textDecoration: "none", flexShrink: 0 }}>
        <img src="assets/logo/chameleon-mark.png" alt="" style={{ height: 40 }} />
        <span style={{ fontFamily: "var(--font-display)", textTransform: "uppercase", fontSize: 22, color: "var(--ink)", letterSpacing: "-.01em", lineHeight: .9 }}>
          Chameleon<br />Space
        </span>
        <span style={{ fontFamily: "var(--font-mono)", fontSize: 10, fontWeight: 700, letterSpacing: ".14em", textTransform: "uppercase", color: "var(--text-dim)", borderLeft: "2px solid var(--paper-line)", paddingLeft: 10, marginLeft: 2 }}>Gaines&shy;ville</span>
      </a>
      <nav className="cs-desktop-nav" style={{ display: "flex", alignItems: "center", gap: 2 }}>
        {NAV.slice(0, 4).map(([label, id]) => (
          <a key={id} href={"#" + id} onClick={go(id)} style={{
            fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 12,
            textTransform: "uppercase", letterSpacing: ".08em", color: "var(--ink)",
            textDecoration: "none", padding: "8px 11px"
          }}>{label}</a>
        ))}
      </nav>
      <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
        <Button size="sm" onClick={go("apply")}>Apply to join</Button>
      </div>
      <style>{`
        @media (max-width: 880px){ .cs-desktop-nav{ display:none !important; } }
      `}</style>
    </header>
  );
}

/* ---- footer ---- */
function SiteFooter() {
  return (
    <footer id="footer" data-ground="ink" style={{ borderTop: "3px solid var(--ink)" }}>
      <Marquee items={["Come as you are", "Founding rate open now", "Full bloom by fall", "We’ll make room"]} accent speed={22} size={30} />
      <div style={{ maxWidth: 1180, margin: "0 auto", padding: "clamp(40px,6vw,64px) clamp(16px,4vw,36px) 36px", display: "grid", gridTemplateColumns: "1.5fr 1fr 1fr", gap: 40 }} className="cs-footer-grid">
        <div>
          <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 16 }}>
            <img src="assets/logo/chameleon-mark.png" alt="" style={{ height: 46 }} />
            <span style={{ fontFamily: "var(--font-display)", textTransform: "uppercase", fontSize: 26, color: "var(--paper)", lineHeight: .9 }}>Chameleon<br />Space</span>
          </div>
          <p style={{ fontFamily: "var(--font-display)", textTransform: "uppercase", fontSize: 28, lineHeight: .95, color: "var(--accent)", margin: "0 0 8px", maxWidth: 360, letterSpacing: "-.01em" }}>
            Come as you are. We’ll make room.
          </p>
        </div>
        <div>
          <div style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 11, textTransform: "uppercase", letterSpacing: ".14em", color: "var(--accent)", marginBottom: 14 }}>// Where we are</div>
          <p style={{ fontFamily: "var(--font-text)", fontSize: 14, lineHeight: 1.55, color: "var(--paper)", opacity: .9, margin: 0, maxWidth: 260 }}>
            Founding memberships open now at our lowest rate, by application. Wider memberships soon. Full bloom by fall.
          </p>
        </div>
        <div>
          <div style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 11, textTransform: "uppercase", letterSpacing: ".14em", color: "var(--accent)", marginBottom: 14 }}>// Find us</div>
          <p style={{ fontFamily: "var(--font-mono)", fontSize: 13, lineHeight: 1.7, color: "var(--paper)", margin: "0 0 18px" }}>
            Gainesville, Florida<br /><span style={{ opacity: .6 }}>[add your street address]</span>
          </p>
          <div style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 11, textTransform: "uppercase", letterSpacing: ".14em", color: "var(--accent)", marginBottom: 10 }}>// Say hi</div>
          <div style={{ display: "flex", flexDirection: "column", gap: 8, alignItems: "flex-start" }}>
            <a href="mailto:hello@chameleon.space" style={{ fontFamily: "var(--font-mono)", fontSize: 13, color: "var(--paper)", textDecoration: "none" }}>hello@chameleon.space</a>
            <a href="#apply" onClick={(e) => { e.preventDefault(); document.getElementById("apply").scrollIntoView({ behavior: "smooth" }); }} style={{ fontFamily: "var(--font-mono)", fontWeight: 700, fontSize: 13, color: "var(--accent)", textDecoration: "none" }}>Apply to join →</a>
          </div>
        </div>
      </div>
      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", padding: "16px clamp(16px,4vw,36px)", borderTop: "1px solid var(--ink-line)", fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--text-dim)", letterSpacing: ".04em", gap: 12, flexWrap: "wrap" }}>
        <span>© 2026 Chameleon Space · Gainesville, FL</span>
        <span>// this place wears a new skin every season</span>
      </div>
      <style>{`@media (max-width: 760px){ .cs-footer-grid{ grid-template-columns: 1fr !important; } }`}</style>
    </footer>
  );
}

Object.assign(window, { Reveal, SectionHead, SiteHeader, SiteFooter });
