/* Chrome → window: Banner, Header, CookieBanner, Footer */
(function () {
  const { useState, useEffect, useRef } = React;
  const Icon = window.Icon, Logo = window.Logo;

  // ---- constitution banner ----
  window.TopBanner = function TopBanner({ text, onClose }) {
    return (
      <div style={{ background: "var(--navy-900)", color: "#fff", fontSize: 13.5, letterSpacing: ".01em" }}>
        <div className="wrap" style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 14, padding: "9px 0", position: "relative" }}>
          <span style={{ width: 7, height: 7, borderRadius: 99, background: "var(--gold-400)", flex: "none", boxShadow: "0 0 0 4px rgba(201,162,74,.18)" }}></span>
          <span style={{ opacity: .92, textAlign: "center" }}>{text}</span>
          <button onClick={onClose} aria-label="Fermer" style={{ position: "absolute", right: 0, background: "none", border: 0, color: "rgba(255,255,255,.6)", display: "flex", padding: 6 }}>
            <Icon name="close" size={15} />
          </button>
        </div>
      </div>
    );
  };

  // ---- header + mega menu ----
  const MENU_KEYS = ["alliance", "clubs", "missions", "projects", "membership"];

  // Map each mega sub-item (by index) to a cross-page URL
  const MEGA_HREFS = {
    alliance: ["index.html#about", "index.html#pillars", "index.html#governance", "index.html#bureau", "index.html#council", "index.html#ethics"],
    clubs: ["clubs.html#club-1", "clubs.html#club-2", "clubs.html#club-3", "clubs.html#club-4", "clubs.html#club-5", "clubs.html#club-6", "clubs.html#club-7"],
    missions: ["missions.html", "missions.html#agenda", "missions.html#forum", "missions.html#reports"],
    projects: ["projects.html#projects", "projects.html#pipeline"],
    membership: ["membership.html#why-join", "membership.html#membership-tiers", "membership.html#membership-sov", "membership.html#application", "membership.html#faq"],
  };

  window.Header = function Header({ C, lang, setLang, mark, navStyle, overHero, heroDark, currentPage, onNav }) {
    const [open, setOpen] = useState(null);
    const [scrolled, setScrolled] = useState(false);
    const [mobile, setMobile] = useState(false);
    const closeT = useRef();

    useEffect(() => {
      const f = () => setScrolled(window.scrollY > 40);
      f(); window.addEventListener("scroll", f, { passive: true });
      return () => window.removeEventListener("scroll", f);
    }, []);

    const transparent = navStyle === "transparent" && overHero && !scrolled && !open;
    const onDark = transparent && heroDark;
    const mode = onDark ? "light" : "dark";
    const linkColor = onDark ? "rgba(255,255,255,.9)" : "var(--ink)";

    const enter = (k) => { clearTimeout(closeT.current); setOpen(k); };
    const leave = () => { closeT.current = setTimeout(() => setOpen(null), 120); };

    // Top-level nav: [key, label, href, activePage]
    const navItems = [
      ["alliance", C.nav.alliance, "index.html", "home"],
      ["clubs", C.nav.clubs, "clubs.html", "clubs"],
      ["missions", C.nav.missions, "missions.html", "missions"],
      ["projects", C.nav.projects, "projects.html", "projects"],
      ["membership", C.nav.membership, "membership.html", "membership"],
      ["news", C.nav.news, "news.html", "news"],
      ["contact", C.nav.contact, "contact.html", "contact"],
    ];

    return (
      <header style={{
        position: "sticky", top: 0, zIndex: 100,
        background: transparent ? "transparent" : "color-mix(in srgb, var(--bg) 88%, transparent)",
        backdropFilter: transparent ? "none" : "saturate(140%) blur(12px)",
        WebkitBackdropFilter: transparent ? "none" : "saturate(140%) blur(12px)",
        borderBottom: transparent ? "1px solid transparent" : "1px solid var(--hairline)",
        transition: "background .3s, border-color .3s",
      }}
        onMouseLeave={leave}>
        <div className="ema-header-inner" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", height: 76, gap: 28, padding: "0 clamp(20px, 3vw, 44px)", maxWidth: 1640, marginInline: "auto" }}>
          <Logo mark={mark} mode={mode} compact={scrolled} />

          {/* desktop nav */}
          <nav className="ema-desk-nav" style={{ display: "flex", alignItems: "center", gap: 0, flex: "0 0 auto", flexWrap: "nowrap", minWidth: 0 }}>
            {navItems.map(([k, label, href, activePage]) => {
              const hasMenu = MENU_KEYS.includes(k) && C.mega[k];
              const isActive = currentPage === activePage;
              return (
                <div key={k} onMouseEnter={() => hasMenu ? enter(k) : enter(null)}
                  style={{ position: "relative" }}>
                  <a href={href} onClick={() => { setOpen(null); onNav && onNav(k); }}
                    className="ema-nav-link"
                    data-active={isActive ? "1" : "0"}
                    style={{
                      display: "flex", alignItems: "center", gap: 5, padding: "10px 14px",
                      fontSize: 14, fontWeight: isActive ? 600 : 500,
                      color: isActive ? "var(--accent-ink)" : linkColor,
                      borderRadius: 8, transition: "color .2s, background .2s", position: "relative",
                      whiteSpace: "nowrap",
                    }}>
                    {label}
                    {hasMenu && <span style={{ opacity: .55, display: "flex", transform: open === k ? "rotate(180deg)" : "none", transition: "transform .2s" }}><Icon name="arrowDown" size={12} /></span>}
                    {isActive && <span style={{ position: "absolute", left: 14, right: 22, bottom: 2, height: 2, background: "var(--accent)", borderRadius: 2 }}></span>}
                  </a>
                </div>
              );
            })}
          </nav>

          {/* right cluster */}
          <div className="ema-header-actions" style={{ display: "flex", alignItems: "center", gap: 14, flex: "none" }}>
            <span className="ema-header-lang" style={{ display: "flex" }}><LangToggle lang={lang} setLang={setLang} color={linkColor} /></span>
            <a href="membership.html" className="btn btn-gold ema-header-join" style={{ padding: "11px 22px", fontSize: 14 }}>{C.nav.join}</a>
            <button className="ema-burger" onClick={() => setMobile(true)} aria-label="Menu" style={{ background: "none", border: 0, color: linkColor, display: "none", padding: 4 }}>
              <Icon name="menu" size={24} />
            </button>
          </div>
        </div>

        {/* mega menu */}
        {open && C.mega[open] && (
          <div onMouseEnter={() => enter(open)}
            style={{
              position: "absolute", left: 0, right: 0, top: "100%",
              background: "var(--surface)", borderBottom: "1px solid var(--hairline)",
              boxShadow: "0 24px 48px -24px rgba(14,31,58,.22)",
              animation: "emaFade .22s var(--ease)",
            }}>
            <div className="wrap" style={{ padding: "34px 0 38px", display: "grid", gridTemplateColumns: "1.1fr 1fr", gap: 40 }}>
              <div>
                <div className="eyebrow" style={{ marginBottom: 18 }}>{C.nav[open]}</div>
                <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: "2px 30px" }}>
                  {C.mega[open].map((it, i) => {
                    const href = (MEGA_HREFS[open] && MEGA_HREFS[open][i]) || ("#" + open);
                    return (
                    <a key={i} href={href} onClick={() => {
                        if (open === "clubs") window.dispatchEvent(new CustomEvent("ema-select-club", { detail: i }));
                        setOpen(null);
                      }}
                      style={{ display: "flex", alignItems: "center", gap: 10, padding: "11px 0", fontSize: 15.5, color: "var(--ink)", borderBottom: "1px solid var(--hairline)" }}
                      onMouseEnter={(e) => e.currentTarget.style.color = "var(--accent-ink)"}
                      onMouseLeave={(e) => e.currentTarget.style.color = "var(--ink)"}>
                      <span style={{ color: "var(--accent)", display: "flex" }}><Icon name="arrow" size={15} /></span>
                      {it}
                    </a>
                    );
                  })}
                </div>
              </div>
              <div style={{ background: "var(--navy-800)", borderRadius: 6, padding: "30px 30px", color: "#fff", display: "flex", flexDirection: "column", justifyContent: "space-between", position: "relative", overflow: "hidden" }}>
                <div style={{ position: "absolute", inset: 0, opacity: .5 }}><window.RiverFlow opacity={.5} /></div>
                <div style={{ position: "relative" }}>
                  <div className="eyebrow" style={{ color: "var(--gold-300)" }}>{C.nav.join}</div>
                  <p className="display" style={{ fontSize: 26, margin: "10px 0 0", color: "#fff" }}>{C.cta.title}</p>
                </div>
                <a href="membership.html" onClick={() => setOpen(null)} className="btn btn-gold" style={{ alignSelf: "flex-start", marginTop: 24, position: "relative" }}>{C.membership.cta} <Icon name="arrow" size={16} /></a>
              </div>
            </div>
          </div>
        )}

        {mobile && <MobileMenu C={C} navItems={navItems} lang={lang} setLang={setLang} onClose={() => setMobile(false)} />}
      </header>
    );
  };

  function LangToggle({ lang, setLang, color }) {
    return (
      <div style={{ display: "flex", alignItems: "center", gap: 2, fontSize: 13, fontWeight: 600, fontFamily: "var(--mono)", letterSpacing: ".04em" }}>
        {["fr", "en"].map((l, i) => (
          <React.Fragment key={l}>
            {i === 1 && <span style={{ opacity: .35, color }}>/</span>}
            <button onClick={() => setLang(l)} style={{
              background: "none", border: 0, padding: "4px 5px",
              color: lang === l ? "var(--accent-ink)" : color,
              opacity: lang === l ? 1 : .6, fontWeight: lang === l ? 700 : 500,
            }}>{l.toUpperCase()}</button>
          </React.Fragment>
        ))}
      </div>
    );
  }

  function MobileMenu({ C, navItems, lang, setLang, onClose }) {
    useEffect(() => {
      document.body.style.overflow = "hidden";
      return () => { document.body.style.overflow = ""; };
    }, []);
    return ReactDOM.createPortal(
      <div style={{ position: "fixed", inset: 0, zIndex: 2000, background: "var(--navy-900)", color: "#fff", padding: "20px var(--gutter)", overflowY: "auto", animation: "emaFade .25s var(--ease)" }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 30 }}>
          <span style={{ fontFamily: "var(--display)", fontWeight: 700, fontSize: 22, letterSpacing: ".06em" }}>EMA</span>
          <button onClick={onClose} style={{ background: "none", border: 0, color: "#fff", padding: 6 }}><Icon name="close" size={26} /></button>
        </div>
        <nav style={{ display: "flex", flexDirection: "column" }}>
          {navItems.map(([k, label, href]) => (
            <a key={k} href={href} onClick={onClose} style={{ fontFamily: "var(--display)", fontSize: 30, padding: "14px 0", borderBottom: "1px solid rgba(255,255,255,.12)", color: "#fff" }}>{label}</a>
          ))}
        </nav>
        <div style={{ marginTop: 28, display: "flex", gap: 12, alignItems: "center" }}>
          <LangToggle lang={lang} setLang={setLang} color="#fff" />
          <a href="membership.html" onClick={onClose} className="btn btn-gold" style={{ flex: 1, justifyContent: "center" }}>{C.nav.join}</a>
        </div>
      </div>,
      document.body
    );
  }

  // ---- cookie banner ----
  window.CookieBanner = function CookieBanner({ C, onClose }) {
    return (
      <div style={{ position: "fixed", left: 16, right: 16, bottom: 16, zIndex: 150, maxWidth: 560, margin: "0 auto",
        background: "var(--navy-900)", color: "#fff", borderRadius: 8, padding: "20px 22px",
        boxShadow: "0 24px 60px -20px rgba(0,0,0,.5)", animation: "emaUp .4s var(--ease)" }}>
        <p style={{ margin: "0 0 14px", fontSize: 14, lineHeight: 1.55, color: "rgba(255,255,255,.82)" }}>{C.cookie.text}</p>
        <div style={{ display: "flex", gap: 10, flexWrap: "wrap" }}>
          <button className="btn btn-gold" style={{ padding: "9px 18px", fontSize: 13.5 }} onClick={onClose}>{C.cookie.accept}</button>
          <button className="btn" style={{ padding: "9px 18px", fontSize: 13.5, color: "#fff", border: "1px solid rgba(255,255,255,.3)" }} onClick={onClose}>{C.cookie.refuse}</button>
          <button className="btn-line" style={{ color: "var(--gold-300)", marginLeft: "auto", fontSize: 13.5 }} onClick={onClose}>{C.cookie.settings}</button>
        </div>
      </div>
    );
  };

  // footer column links → cross-page URLs (parallel to C.footer.cols)
  const FOOT_HREFS = [
    ["index.html#about", "index.html#governance", "index.html#bureau", "index.html#ethics"],
    ["clubs.html", "missions.html", "projects.html", "membership.html"],
    ["news.html", "news.html#publications", "news.html#press", "contact.html"],
  ];

  // ---- footer ----
  window.Footer = function Footer({ C, mark }) {
    return (
      <footer id="contact" style={{ background: "var(--navy-900)", color: "#fff", position: "relative", overflow: "hidden", scrollMarginTop: 80 }}>
        <div style={{ position: "absolute", top: 0, left: 0, right: 0, height: 200, opacity: .35 }}><window.RiverFlow opacity={.35} /></div>
        <div className="wrap" style={{ position: "relative", paddingTop: 80, paddingBottom: 36 }}>
          <div style={{ display: "grid", gridTemplateColumns: "1.4fr 1fr 1fr 1fr", gap: 40 }} className="ema-foot-grid">
            <div>
              <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 18 }}>
                <img src="assets/ema_logo_cutout.png" alt="EMA" style={{ width: 52, height: 52 }} />
                <span style={{ fontFamily: "var(--display)", fontWeight: 700, fontSize: 26, letterSpacing: ".06em" }}>EMA</span>
              </div>
              <p style={{ fontFamily: "var(--display)", fontSize: 21, lineHeight: 1.3, maxWidth: 300, margin: "0 0 22px", color: "rgba(255,255,255,.92)" }}>{C.footer.tagline}</p>
              <div style={{ display: "flex", alignItems: "center", gap: 10, color: "rgba(255,255,255,.7)", fontSize: 14, marginBottom: 8 }}><Icon name="pin" size={16} /> {C.footer.address}</div>
              <a href={"mailto:" + C.footer.email} style={{ display: "flex", alignItems: "center", gap: 10, color: "var(--gold-300)", fontSize: 14 }}><Icon name="mail" size={16} /> {C.footer.email}</a>
              <div style={{ display: "flex", gap: 12, marginTop: 22 }}>
                {["linkedin", "youtube", "mail"].map((s) => (
                  <a key={s} href="#" aria-label={s} style={{ width: 40, height: 40, borderRadius: 99, border: "1px solid rgba(255,255,255,.2)", display: "flex", alignItems: "center", justifyContent: "center", color: "#fff", transition: "all .2s" }}
                    onMouseEnter={(e) => { e.currentTarget.style.background = "var(--gold-500)"; e.currentTarget.style.borderColor = "var(--gold-500)"; }}
                    onMouseLeave={(e) => { e.currentTarget.style.background = "transparent"; e.currentTarget.style.borderColor = "rgba(255,255,255,.2)"; }}>
                    <Icon name={s} size={17} /></a>
                ))}
              </div>
            </div>
            {C.footer.cols.map((col, i) => (
              <div key={i}>
                <div className="eyebrow" style={{ color: "var(--gold-300)", marginBottom: 18 }}>{C.footer.colTitles[i]}</div>
                <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 12 }}>
                  {col.map((l, j) => <li key={j}><a href={(FOOT_HREFS[i] && FOOT_HREFS[i][j]) || "#"} style={{ fontSize: 14.5, color: "rgba(255,255,255,.78)", transition: "color .2s" }} onMouseEnter={(e) => e.currentTarget.style.color = "#fff"} onMouseLeave={(e) => e.currentTarget.style.color = "rgba(255,255,255,.78)"}>{l}</a></li>)}
                </ul>
              </div>
            ))}
          </div>

          {/* newsletter */}
          <div style={{ marginTop: 56, padding: "28px 0", borderTop: "1px solid rgba(255,255,255,.14)", borderBottom: "1px solid rgba(255,255,255,.14)", display: "flex", justifyContent: "space-between", alignItems: "center", gap: 24, flexWrap: "wrap" }}>
            <div style={{ fontFamily: "var(--display)", fontSize: 24 }}>{C.footer.newsletter}</div>
            <form onSubmit={(e) => e.preventDefault()} style={{ display: "flex", gap: 8, flex: "1 1 340px", maxWidth: 440 }}>
              <input type="email" placeholder={C.footer.newsletterPh} style={{ flex: 1, padding: "13px 18px", borderRadius: 99, border: "1px solid rgba(255,255,255,.25)", background: "rgba(255,255,255,.06)", color: "#fff", fontSize: 14, fontFamily: "var(--sans)" }} />
              <button className="btn btn-gold" type="submit">{C.footer.subscribe}</button>
            </form>
          </div>

          <div style={{ marginTop: 28, display: "flex", justifyContent: "space-between", alignItems: "center", gap: 18, flexWrap: "wrap" }}>
            <div style={{ display: "flex", flexWrap: "wrap", gap: "8px 22px" }}>
              {C.footer.legal.map((l, i) => <a key={i} href="#" style={{ fontSize: 12.5, color: "rgba(255,255,255,.55)" }}>{l}</a>)}
            </div>
            <div style={{ fontSize: 12.5, color: "rgba(255,255,255,.45)" }}>{C.footer.rights}</div>
          </div>
        </div>
      </footer>
    );
  };
})();
