const { Logo, Button, Mark, Seal, Divider } = window.PYPAAluvaCentreDesignSystem_9d12c1;
const S = window.SITE;
const NAV = [["about", "About"], ["programmes", "Programmes"], ["serve", "Serve"]];
const PAGE_WRAP = "clamp(24px,6vw,96px)";
const MAXW = 1240;

const reduceMotion = () => window.matchMedia && window.matchMedia("(prefers-reduced-motion: reduce)").matches;

// — Scroll-reveal: one fade-and-rise on entry, never repeating.
function Reveal({ children, delay = 0, y = 16, style }) {
  const ref = React.useRef(null);
  const [shown, setShown] = React.useState(false);
  React.useEffect(() => {
    if (reduceMotion()) { setShown(true); return; }
    const el = ref.current; if (!el) return;
    const io = new IntersectionObserver((es) => {
      es.forEach((e) => { if (e.isIntersecting) { setShown(true); io.disconnect(); } });
    }, { threshold: 0.12, rootMargin: "0px 0px -8% 0px" });
    io.observe(el); return () => io.disconnect();
  }, []);
  return (
    <div ref={ref} style={{ opacity: shown ? 1 : 0, transform: shown ? "none" : `translateY(${y}px)`, transition: `opacity var(--t-slow) var(--ease-standard) ${delay}ms, transform var(--t-slow) var(--ease-standard) ${delay}ms`, ...style }}>
      {children}
    </div>
  );
}

function Container({ children, narrow, style }) {
  return <div style={{ maxWidth: narrow ? 760 : MAXW, margin: "0 auto", padding: `0 ${PAGE_WRAP}`, ...style }}>{children}</div>;
}

function Eyebrow({ children, tone, style }) {
  const col = tone === "inverse" ? "rgba(244,242,236,.55)" : "var(--c-ink-faint)";
  return <div style={{ fontFamily: "var(--font-sans)", fontSize: 12, fontWeight: 500, letterSpacing: ".18em", textTransform: "uppercase", color: col, ...style }}>{children}</div>;
}

// — Section: vertical rhythm + tone band. Rhythm is varied deliberately by `pad`.
function Section({ children, tone = "paper", pad = "lg", id, style }) {
  const bg = { paper: "var(--c-paper)", linen: "var(--c-linen)", deep: "var(--c-deep)", raised: "var(--c-paper-raised)" }[tone];
  const py = { sm: "clamp(48px,7vw,80px)", md: "clamp(64px,9vw,112px)", lg: "clamp(80px,12vw,160px)" }[pad];
  return <section id={id} style={{ background: bg, padding: `${py} 0`, color: tone === "deep" ? "var(--text-inverse)" : "var(--c-ink)", ...style }}>{children}</section>;
}

// — Honest photography placeholder. Clearly temporary; never fake-real.
function Photo({ ratio = "16 / 9", label = "Photograph", note, tone = "linen", align = "center", src, alt, style }) {
  const [failed, setFailed] = React.useState(false);
  const bg = tone === "deep" ? "var(--c-deep-soft)" : "var(--c-linen-deep)";
  const fg = tone === "deep" ? "rgba(244,242,236,.5)" : "var(--c-ink-mute)";
  const corner = align === "corner";
  if (src && !failed) {
    return (
      <div style={{ position: "relative", aspectRatio: ratio, background: bg, overflow: "hidden", ...style }}>
        <img src={src} alt={alt || note || label} loading="lazy" onError={() => setFailed(true)} style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
      </div>
    );
  }
  return (
    <div role="img" aria-label={`Placeholder — ${label}${note ? ": " + note : ""}`} style={{ position: "relative", aspectRatio: ratio, background: bg, overflow: "hidden", display: "grid", placeItems: corner ? "end end" : "center", ...style }}>
      <div style={{ position: "absolute", inset: 10, border: `1px solid ${tone === "deep" ? "rgba(244,242,236,.14)" : "var(--c-rule-strong)"}`, pointerEvents: "none" }} />
      <div style={{ textAlign: corner ? "right" : "center", padding: corner ? 24 : 24, maxWidth: "80%" }}>
        {!corner && <Mark size={26} tone={tone === "deep" ? "paper" : "ink"} style={{ margin: "0 auto .7em", opacity: .5 }} />}
        <div style={{ fontFamily: "var(--font-sans)", fontSize: 11, letterSpacing: ".16em", textTransform: "uppercase", color: fg }}>{label}</div>
        {note && <div style={{ fontFamily: "var(--font-sans)", fontSize: 12.5, color: fg, marginTop: 6, lineHeight: 1.5 }}>{note}</div>}
      </div>
    </div>
  );
}

// Display + heading helpers (Newsreader).
function Display({ children, style }) {
  return <h1 style={{ fontFamily: "var(--font-serif)", fontWeight: 300, fontSize: "clamp(38px,6vw,84px)", lineHeight: 1.02, letterSpacing: "-.02em", textWrap: "pretty", margin: 0, ...style }}>{children}</h1>;
}
function Heading({ children, as = "h2", style }) {
  const T = as;
  return <T style={{ fontFamily: "var(--font-serif)", fontWeight: 300, fontSize: "clamp(28px,3.6vw,48px)", lineHeight: 1.08, letterSpacing: "-.02em", textWrap: "pretty", margin: 0, ...style }}>{children}</T>;
}
function Lede({ children, tone, style }) {
  return <p style={{ fontSize: "clamp(17px,1.4vw,20px)", lineHeight: 1.6, color: tone === "inverse" ? "rgba(244,242,236,.78)" : "var(--c-ink-soft)", maxWidth: "38ch", margin: 0, textWrap: "pretty", ...style }}>{children}</p>;
}

// — Header. Transparent over the hero on Home; solid otherwise and on scroll.
function SiteHeader({ route, go, transparentAtTop }) {
  const [solid, setSolid] = React.useState(!transparentAtTop);
  const [open, setOpen] = React.useState(false);
  React.useEffect(() => {
    if (!transparentAtTop) { setSolid(true); return; }
    const onScroll = () => setSolid(window.scrollY > 40);
    onScroll(); window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, [transparentAtTop]);
  React.useEffect(() => { document.body.style.overflow = open ? "hidden" : ""; return () => { document.body.style.overflow = ""; }; }, [open]);
  React.useEffect(() => { setOpen(false); }, [route]);

  const onDark = transparentAtTop && !solid;
  const linkCol = onDark ? "rgba(244,242,236,.82)" : "var(--c-ink-mute)";
  const activeCol = onDark ? "#F4F2EC" : "var(--c-ink)";

  return (
    <header style={{ position: "sticky", top: 0, zIndex: 40, background: solid ? "rgba(252,251,248,.9)" : "transparent", backdropFilter: solid ? "blur(10px)" : "none", borderBottom: solid ? "1px solid var(--c-rule)" : "none", transition: "background var(--t-base), border-color var(--t-base)" }}>
      <div style={{ maxWidth: MAXW, margin: "0 auto", padding: `16px ${PAGE_WRAP}`, display: "flex", alignItems: "center", justifyContent: "space-between", gap: 24 }}>
        <a href="#/" onClick={(e) => { e.preventDefault(); go("home"); }} aria-label="PYPA Aluva Centre — home" style={{ display: "inline-flex", border: 0 }}>
          <BrandLockup onDark={onDark} />
        </a>
        <nav aria-label="Primary" style={{ display: "flex", alignItems: "center", gap: 30 }} className="pypa-desktop-nav">
          {NAV.map(([r, l]) => (
            <a key={r} href={`#/${r}`} onClick={(e) => { e.preventDefault(); go(r); }}
              style={{ fontFamily: "var(--font-sans)", fontSize: 15, color: route === r ? activeCol : linkCol, borderBottom: `1px solid ${route === r ? activeCol : "transparent"}`, paddingBottom: 3, transition: "color var(--t-quick)" }}>{l}</a>
          ))}
          <Button size="sm" variant={onDark ? "inverse" : "primary"} onClick={() => go("get-involved")}>Get involved</Button>
        </nav>
        <button aria-label="Open menu" aria-expanded={open} onClick={() => setOpen(true)} className="pypa-menu-btn" style={{ display: "none", flexDirection: "column", gap: 5, background: "none", border: 0, padding: 10, cursor: "pointer" }}>
          {[0, 1, 2].map((i) => <span key={i} style={{ width: 24, height: 2, background: onDark ? "#F4F2EC" : "var(--c-ink)", display: "block" }} />)}
        </button>
      </div>
      {open && ReactDOM.createPortal(
        <div role="dialog" aria-modal="true" style={{ position: "fixed", inset: 0, zIndex: 100, background: "var(--c-paper)", display: "flex", flexDirection: "column", padding: `20px ${PAGE_WRAP}`, animation: "pypaFade var(--t-base) var(--ease-standard)" }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
            <BrandLockup />
            <button aria-label="Close menu" onClick={() => setOpen(false)} style={{ background: "none", border: 0, fontSize: 30, lineHeight: 1, color: "var(--c-ink)", cursor: "pointer", padding: 6, minWidth: 44, minHeight: 44 }}>×</button>
          </div>
          <nav aria-label="Primary" style={{ display: "flex", flexDirection: "column", gap: 4, marginTop: "8vh" }}>
            {[["home", "Home"], ...NAV].map(([r, l]) => (
              <a key={r} href={`#/${r}`} onClick={(e) => { e.preventDefault(); setOpen(false); go(r); }}
                style={{ fontFamily: "var(--font-serif)", fontWeight: 300, fontSize: 34, letterSpacing: "-.01em", color: route === r ? "var(--c-sage)" : "var(--c-ink)", padding: "12px 0", borderBottom: "1px solid var(--c-rule)", minHeight: 44 }}>{l}</a>
            ))}
          </nav>
          <div style={{ marginTop: "auto", paddingTop: 24 }}>
            <Button fullWidth onClick={() => { setOpen(false); go("get-involved"); }}>Get involved</Button>
          </div>
        </div>, document.body
      )}
    </header>
  );
}

// Header/menu brand — the full parent seal beside the wordmark.
function BrandLockup({ onDark }) {
  const ink = onDark ? "#F4F2EC" : "var(--c-ink)";
  const sub = onDark ? "rgba(244,242,236,.6)" : "var(--c-ink-mute)";
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 13 }}>
      <Seal size={48} tone={onDark ? "paper" : "ink"} />
      <span style={{ display: "flex", flexDirection: "column", gap: 4 }}>
        <span style={{ fontFamily: "var(--font-serif)", fontWeight: 300, fontSize: 26, letterSpacing: ".13em", lineHeight: 1, textIndent: ".13em", color: ink }}>PYPA</span>
        <span style={{ fontFamily: "var(--font-sans)", fontWeight: 500, fontSize: 9.5, letterSpacing: ".2em", textTransform: "uppercase", color: sub, whiteSpace: "nowrap" }}>IPC Aluva Centre</span>
      </span>
    </span>
  );
}

function FootCol({ title, children }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
      <span style={{ fontFamily: "var(--font-sans)", fontSize: 12, letterSpacing: ".16em", textTransform: "uppercase", color: "rgba(244,242,236,.5)", marginBottom: 4 }}>{title}</span>
      {children}
    </div>
  );
}
const footLink = { color: "rgba(244,242,236,.82)", fontSize: 15, border: 0, lineHeight: 1.5 };

function SiteFooter({ go }) {
  return (
    <footer style={{ background: "var(--c-deep)", color: "var(--text-inverse)" }}>
      <Container style={{ padding: `clamp(64px,9vw,112px) ${PAGE_WRAP} 40px`, display: "grid", gap: 48, gridTemplateColumns: "1.5fr 1fr 1fr", alignItems: "start" }} >
        <div>
          <Logo tone="paper" size={30} />
          <p style={{ marginTop: 22, color: "rgba(244,242,236,.66)", fontSize: 15, lineHeight: 1.65, maxWidth: "32ch" }}>
            A Christ-centered community of young people in Aluva. A local Centre of PYPA, within the Indian Pentecostal Church.
          </p>
        </div>
        <FootCol title="Explore">
          {[["home", "Home"], ...NAV].map(([r, l]) => <a key={r} href={`#/${r}`} onClick={(e) => { e.preventDefault(); go(r); }} style={footLink}>{l}</a>)}
        </FootCol>
        <FootCol title="Find us">
          <span style={{ color: "rgba(244,242,236,.66)", fontSize: 15, lineHeight: 1.6 }}>{S.settings.area}</span>
          <a href={`mailto:${S.settings.email}`} style={footLink}>{S.settings.email}</a>
          <a href={S.settings.instagram} style={footLink}>Instagram</a>
          <a href={S.settings.whatsapp} style={footLink}>WhatsApp</a>
        </FootCol>
      </Container>
      <Container style={{ paddingBottom: 44 }}>
        <div style={{ borderTop: "1px solid rgba(244,242,236,.14)", paddingTop: 22, display: "flex", justifyContent: "space-between", alignItems: "center", gap: 16, flexWrap: "wrap" }}>
          <span style={{ fontSize: 13, color: "rgba(244,242,236,.45)" }}>© {new Date().getFullYear()} Pentecostal Young People's Association · Aluva Centre</span>
          <span style={{ display: "inline-flex", alignItems: "center", gap: 12 }}>
            <span style={{ fontFamily: "var(--font-sans)", fontSize: 11, letterSpacing: ".22em", textTransform: "uppercase", color: "rgba(244,242,236,.5)" }}>Saved to serve</span>
            <Mark size={18} tone="paper" />
          </span>
        </div>
      </Container>
    </footer>
  );
}

Object.assign(window, { Reveal, Container, Eyebrow, Section, Photo, Display, Heading, Lede, SiteHeader, SiteFooter, NAV, MAXW, PAGE_WRAP });
