/* ============================================================
   EBTR — Shared chrome: masthead / nav + footer
   ============================================================ */
const { ThreeBar: EBThreeBarC } = window.EBTHubDesignSystem_ccdc9d;

const EBTR_NAV_SHORT = { "lab-outputs": "Labs", "voices": "Voices", "tools": "Tools", "field-notes": "Field", "agent-stories": "Agents", "briefings": "Briefs" };

function EBTRMasthead({ route, onHome, onCategory, activeCategory, loggedIn, viewer, onSignIn, onSignOut, onApply, onSearch }) {
  const cats = window.EBTR_DATA.categories;
  const [scrolled, setScrolled] = React.useState(false);
  const [acctOpen, setAcctOpen] = React.useState(false);
  const [burgerOpen, setBurgerOpen] = React.useState(false);
  React.useEffect(() => {
    const f = () => setScrolled(window.scrollY > 8);
    f(); window.addEventListener("scroll", f, { passive: true });
    return () => window.removeEventListener("scroll", f);
  }, []);
  React.useEffect(() => {
    if (!acctOpen) return;
    const c = () => setAcctOpen(false);
    window.addEventListener("click", c); return () => window.removeEventListener("click", c);
  }, [acctOpen]);

  return (
    <header style={{ position: "sticky", top: 0, zIndex: 50, background: scrolled ? "rgba(20,18,18,0.86)" : "var(--ebt-ink)", backdropFilter: scrolled ? "blur(10px)" : "none", borderBottom: "1px solid var(--border-hairline)", transition: "background var(--dur-base)" }}>
      <div style={{ maxWidth: 1600, margin: "0 auto", padding: "0 var(--space-7)", display: "flex", alignItems: "center", gap: 32, height: 72 }}>
        {/* Wordmark — full EBT REVIEW lockup in Michroma */}
        <button onClick={onHome} style={{ display: "flex", alignItems: "center", gap: 14, background: "none", border: "none", padding: 0, cursor: "pointer", flex: "none" }}>
          <EBThreeBarC orientation="vertical" thickness={10} />
          <div style={{ textAlign: "left", lineHeight: 1.1 }}>
            <div style={{ fontFamily: "var(--font-ui)", fontSize: 19, color: "var(--white)", letterSpacing: "0.12em" }}>
              EBT<span style={{ color: "var(--ebt-stone)", margin: "0 1px" }}>·</span>HUB
            </div>
            <div style={{ fontFamily: "var(--font-ui)", fontSize: 9.5, letterSpacing: "0.42em", color: "var(--ebt-orange)", marginTop: 5, textTransform: "uppercase" }}>Review</div>
          </div>
        </button>

        {/* Category nav — centered between logo and search */}
        <nav style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 22, flex: 1, overflow: "hidden" }} className="ebtr-nav">
          {Object.keys(cats).map((k) => {
            const active = route.name === "home" && activeCategory === k;
            return (
              <button key={k} onClick={() => onCategory(k)} title={cats[k].label} style={{ background: "none", border: "none", padding: "6px 0", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.1em", textTransform: "uppercase", color: active ? "var(--white)" : "var(--text-secondary)", borderBottom: active ? "2px solid var(--ebt-orange)" : "2px solid transparent", whiteSpace: "nowrap" }}>
                {EBTR_NAV_SHORT[k]}
              </button>
            );
          })}
        </nav>

        {/* Actions — search · (signed out: sign in + apply / signed in: name + sign out) */}
        <div style={{ display: "flex", alignItems: "center", gap: 18, flex: "none" }} className="ebtr-actions">
          <button onClick={onSearch} title="Search" aria-label="Search" className="ebtr-search" style={{ display: "inline-flex", alignItems: "center", justifyContent: "center", width: 34, height: 34, background: "none", border: "1px solid var(--border-hairline)", borderRadius: "50%", cursor: "pointer", color: "var(--text-secondary)", flex: "none" }}>
            <svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>
          </button>
          <span className="ebtr-hdr-div" style={{ width: 1, height: 22, background: "var(--border-hairline)" }}></span>
          <button onClick={() => setBurgerOpen(true)} className="ebtr-burger" aria-label="Menu" style={{ display: "none", alignItems: "center", justifyContent: "center", width: 34, height: 34, background: "none", border: "1px solid var(--border-hairline)", borderRadius: "50%", cursor: "pointer", color: "var(--white)", flex: "none" }}><svg width="17" height="17" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M3 6h18M3 12h18M3 18h18" /></svg></button>
          {loggedIn ? (
            <div style={{ position: "relative" }} onClick={(e) => e.stopPropagation()}>
              <button onClick={() => setAcctOpen((v) => !v)} className="ebtr-signin" style={{ display: "inline-flex", alignItems: "center", gap: 7, background: "none", border: "none", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.06em", color: "var(--text-secondary)" }}>
                <span className="ebtr-vname">{viewer.name}</span>
                <span style={{ fontSize: 9, opacity: 0.7 }}>{acctOpen ? "▲" : "▼"}</span>
              </button>
              {acctOpen && (
                <div style={{ position: "absolute", top: "calc(100% + 12px)", right: 0, width: 260, background: "var(--surface-raised)", border: "1px solid var(--border-hairline)", borderTop: "3px solid var(--ebt-orange)", padding: "18px 18px 16px", zIndex: 60, boxShadow: "var(--shadow-md)" }}>
                  <div style={{ fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 15, color: "var(--white)" }}>{viewer.name}</div>
                  <div style={{ fontFamily: "var(--font-ui)", fontSize: 11, color: "var(--text-muted)", marginTop: 3, wordBreak: "break-all" }}>{viewer.email || ""}</div>
                  <div style={{ display: "inline-block", marginTop: 12, fontFamily: "var(--font-ui)", fontSize: 9.5, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--neutral-1000)", background: viewer.isStaff ? "var(--ebt-blue)" : "var(--ebt-orange)", padding: "4px 9px" }}>{viewer.isStaff ? "EBTHub staff" : "Partner access"}</div>
                  <div style={{ fontFamily: "var(--font-body)", fontSize: 12.5, lineHeight: 1.5, color: "var(--text-secondary)", margin: "12px 0 14px" }}>{viewer.isStaff ? "Full access. Manage your account in Google." : "Reader access to EBTR. Premium papers may need a one-time unlock."}</div>
                  <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
                    {viewer.isStaff && <a href="https://myaccount.google.com" target="_blank" rel="noopener" style={{ fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.06em", color: "var(--blue-400)", textDecoration: "none" }}>Manage Google account →</a>}
                    <a href="mailto:office@ebthub.com" style={{ fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.06em", color: "var(--blue-400)", textDecoration: "none" }}>Contact membership →</a>
                  </div>
                  <button onClick={() => { setAcctOpen(false); onSignOut(); }} style={{ width: "100%", marginTop: 16, background: "none", border: "1px solid var(--border-strong)", padding: "9px", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--white)" }}>Sign out</button>
                </div>
              )}
            </div>
          ) : (
            <React.Fragment>
              <button onClick={onSignIn} className="ebtr-signin" style={{ background: "none", border: "none", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--text-secondary)" }}>Sign in</button>
              <button onClick={onApply} className="ebtr-apply" style={{ display: "inline-flex", alignItems: "center", gap: 9, fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--neutral-1000)", background: "var(--ebt-orange)", border: "none", padding: "11px 20px", cursor: "pointer", whiteSpace: "nowrap" }}>Apply <span aria-hidden="true">→</span></button>
            </React.Fragment>
          )}
        </div>
      </div>
      {burgerOpen && (
        <div onClick={() => setBurgerOpen(false)} style={{ position: "fixed", inset: 0, zIndex: 90, background: "rgba(8,8,10,0.6)" }}>
          <div onClick={(e) => e.stopPropagation()} style={{ position: "absolute", top: 0, right: 0, bottom: 0, width: "82%", maxWidth: 320, background: "var(--ebt-ink)", borderLeft: "1px solid var(--border-hairline)", padding: "20px 22px 32px", overflowY: "auto" }}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginBottom: 22 }}>
              <span style={{ fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--text-muted)" }}>Menu</span>
              <button onClick={() => setBurgerOpen(false)} style={{ background: "none", border: "none", color: "var(--text-muted)", fontSize: 22, cursor: "pointer", fontFamily: "var(--font-ui)" }}>✕</button>
            </div>
            {loggedIn && <div style={{ fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 15, color: "var(--white)", marginBottom: 18 }}>{viewer.name}</div>}
            <div style={{ fontFamily: "var(--font-ui)", fontSize: 10, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--text-muted)", marginBottom: 6 }}>Topics</div>
            <div style={{ display: "flex", flexDirection: "column" }}>
              {Object.keys(cats).map((k) => (
                <button key={k} onClick={() => { setBurgerOpen(false); onCategory(k); }} style={{ textAlign: "left", background: "none", border: "none", borderBottom: "1px solid var(--border-hairline)", padding: "13px 0", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 13, letterSpacing: "0.05em", textTransform: "uppercase", color: "var(--white)" }}>{cats[k].label}</button>
              ))}
            </div>
            <div style={{ display: "flex", flexDirection: "column", gap: 12, marginTop: 24 }}>
              {!loggedIn && <button onClick={() => { setBurgerOpen(false); onApply(); }} style={{ width: "100%", background: "var(--ebt-orange)", border: "none", padding: "14px", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 12, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--neutral-1000)" }}>Apply →</button>}
              {!loggedIn && <button onClick={() => { setBurgerOpen(false); onSignIn(); }} style={{ width: "100%", background: "none", border: "1px solid var(--border-strong)", padding: "13px", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 12, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--white)" }}>Sign in</button>}
              {loggedIn && <button onClick={() => { setBurgerOpen(false); onSignOut(); }} style={{ width: "100%", background: "none", border: "1px solid var(--border-strong)", padding: "13px", cursor: "pointer", fontFamily: "var(--font-ui)", fontSize: 12, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--white)" }}>Sign out</button>}
            </div>
          </div>
        </div>
      )}
    </header>
  );
}

function EBTRFooter({ onCategory }) {
  const cats = window.EBTR_DATA.categories;
  const Col = ({ title, items }) => (
    <div>
      <div style={{ fontFamily: "var(--font-ui)", fontSize: 10, letterSpacing: "0.16em", textTransform: "uppercase", color: "var(--text-muted)", marginBottom: 18 }}>{title}</div>
      <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
        {items.map((it, i) => (
          <button key={i} onClick={it.onClick || null} className="ebtr-flink" style={{ textAlign: "left", background: "none", border: "none", padding: 0, cursor: "pointer", fontFamily: "var(--font-body)", fontSize: 14.5, color: "var(--text-secondary)", transition: "color var(--dur-base)" }}>{it.label}</button>
        ))}
      </div>
    </div>
  );
  return (
    <footer style={{ borderTop: "1px solid var(--border-hairline)", background: "var(--neutral-1000)", marginTop: 96 }}>
      <div style={{ maxWidth: 1600, margin: "0 auto", padding: "72px var(--space-7) 36px" }}>
        <div style={{ display: "grid", gridTemplateColumns: "1.9fr 1fr 1fr 1fr", gap: 44, alignItems: "start" }} className="ebtr-foot-grid">
          <div style={{ maxWidth: 360 }}>
            <div style={{ display: "flex", alignItems: "center", gap: 14, marginBottom: 20 }}>
              <EBThreeBarC orientation="vertical" thickness={11} />
              <div style={{ lineHeight: 1.1 }}>
                <div style={{ fontFamily: "var(--font-ui)", fontSize: 22, color: "var(--white)", letterSpacing: "0.12em", whiteSpace: "nowrap" }}>EBT<span style={{ color: "var(--ebt-stone)", margin: "0 1px" }}>·</span>HUB</div>
                <div style={{ fontFamily: "var(--font-ui)", fontSize: 10.5, letterSpacing: "0.46em", color: "var(--ebt-orange)", marginTop: 5, textTransform: "uppercase" }}>Review</div>
              </div>
            </div>
            <p style={{ fontFamily: "var(--font-body)", fontSize: 15, lineHeight: 1.65, color: "var(--text-secondary)", margin: 0 }}>
              The publication from European Business Transformation Hub. Hard data, field-tested tools, and blunt reality checks on the roles, processes, and data gaps that make or break real transformation.
            </p>
          </div>
          <Col title="The Review" items={[{ label: "About EBTR", onClick: () => { window.location.href = "EBTR About.html#about"; } }, { label: "Editorial standards", onClick: () => { window.location.href = "EBTR About.html#standards"; } }, { label: "Peer review", onClick: () => { window.location.href = "EBTR About.html#peer"; } }]} />
          <Col title="Membership" items={[{ label: "Partner access", onClick: () => { window.open("https://docs.google.com/forms/d/e/1FAIpQLSf7kn_amYFM9mIDtQR3UoxzkEXyadnJIMzzd6O_cYhfKBj8sw/viewform", "_blank", "noopener"); } }, { label: "For authors", onClick: () => { window.location.href = "EBTR Authors.html"; } }, { label: "Contact", onClick: () => { window.location.href = "mailto:office@ebthub.com"; } }]} />
          <Col title="Association" items={[{ label: "EBTHub.com", onClick: () => { window.open("https://www.ebthub.com", "_blank", "noopener"); } }, { label: "LinkedIn", onClick: () => { window.open("https://www.linkedin.com/company/ebthub/", "_blank", "noopener"); } }, { label: "Imprint", onClick: () => { window.open("https://www.ebthub.com/imprint", "_blank", "noopener"); } }]} />
        </div>

        <div style={{ marginTop: 56 }}><EBThreeBarC variant="divider" thickness={3} /></div>

        <div style={{ marginTop: 24, display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 14 }}>
          <span style={{ fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.05em", color: "var(--text-muted)" }}>© 2026 EBTHub — European Business Transformation Hub · Vienna, AT · ZVR 1201089427</span>
          <div style={{ display: "flex", alignItems: "center", gap: 22, fontFamily: "var(--font-ui)", fontSize: 11, letterSpacing: "0.06em", color: "var(--text-muted)" }}>
            <a className="ebtr-flink" href="https://www.ebthub.com/privacy-policy" target="_blank" rel="noopener" style={{ color: "inherit", textDecoration: "none", letterSpacing: "inherit" }}>Privacy &amp; GDPR</a>
            <a className="ebtr-flink" href="https://www.ebthub.com/terms-conditions" target="_blank" rel="noopener" style={{ color: "inherit", textDecoration: "none", letterSpacing: "inherit" }}>Terms</a>
            <a className="ebtr-flink" href="https://www.ebthub.com/imprint" target="_blank" rel="noopener" style={{ color: "inherit", textDecoration: "none", letterSpacing: "inherit" }}>Imprint</a>
          </div>
        </div>
      </div>
    </footer>
  );
}

/* ---- Search overlay ---- */
function EBTRSearch({ articles, onOpen, onClose }) {
  const [q, setQ] = React.useState("");
  const cats = window.EBTR_DATA.categories;
  React.useEffect(() => {
    const k = (e) => e.key === "Escape" && onClose();
    window.addEventListener("keydown", k); return () => window.removeEventListener("keydown", k);
  }, [onClose]);
  const term = q.trim().toLowerCase();
  const results = !term ? articles.slice(0, 6) : articles.filter((a) => {
    const hay = (a.title + " " + a.dek + " " + (cats[a.category] ? cats[a.category].label : "") + " " + (a.tags || []).join(" ")).toLowerCase();
    return hay.includes(term);
  });
  return (
    <div onClick={onClose} style={{ position: "fixed", inset: 0, zIndex: 100, background: "rgba(8,8,10,0.78)", backdropFilter: "blur(6px)", display: "flex", alignItems: "flex-start", justifyContent: "center", padding: "12vh 24px 24px", animation: "ebtrFade 180ms ease-out" }}>
      <div onClick={(e) => e.stopPropagation()} style={{ width: "100%", maxWidth: 620, background: "var(--surface-raised)", border: "1px solid var(--border-hairline)", borderTop: "3px solid var(--ebt-orange)" }}>
        <div style={{ display: "flex", alignItems: "center", gap: 12, padding: "16px 20px", borderBottom: "1px solid var(--border-hairline)" }}>
          <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="var(--text-muted)" strokeWidth="2"><circle cx="11" cy="11" r="7" /><path d="m21 21-4.3-4.3" /></svg>
          <input autoFocus value={q} onChange={(e) => setQ(e.target.value)} placeholder="Search articles, topics, tags…" style={{ flex: 1, background: "none", border: "none", outline: "none", color: "var(--white)", fontFamily: "var(--font-body)", fontSize: 17 }} />
          <button onClick={onClose} style={{ background: "none", border: "none", color: "var(--text-muted)", cursor: "pointer", fontSize: 20, fontFamily: "var(--font-ui)" }}>✕</button>
        </div>
        <div style={{ maxHeight: "52vh", overflowY: "auto" }}>
          {!term && <div style={{ fontFamily: "var(--font-ui)", fontSize: 10, letterSpacing: "0.12em", textTransform: "uppercase", color: "var(--text-muted)", padding: "14px 20px 4px" }}>Latest</div>}
          {results.length === 0 && <div style={{ fontFamily: "var(--font-body)", fontSize: 15, color: "var(--text-muted)", padding: "26px 20px" }}>No matches for “{q}”.</div>}
          {results.map((a) => {
            const c = cats[a.category];
            return (
              <button key={a.id} onClick={(e) => { e.stopPropagation(); onOpen(a.id); }} className="ebtr-sresult" style={{ display: "block", width: "100%", textAlign: "left", background: "none", border: "none", borderBottom: "1px solid var(--border-hairline)", padding: "14px 20px", cursor: "pointer" }}>
                <div style={{ display: "flex", alignItems: "center", gap: 9, marginBottom: 5 }}>
                  <span style={{ width: 8, height: 8, background: ebtrAccentVar(c.color) }}></span>
                  <span style={{ fontFamily: "var(--font-ui)", fontSize: 9.5, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--text-muted)" }}>{c.label}</span>
                  {a.premium && <span style={{ fontFamily: "var(--font-ui)", fontSize: 9, letterSpacing: "0.1em", textTransform: "uppercase", color: "var(--ebt-orange)" }}>Premium</span>}
                </div>
                <div style={{ fontFamily: "var(--font-body)", fontWeight: 700, fontSize: 16, lineHeight: 1.25, color: "var(--text-primary)" }}>{a.title}</div>
              </button>
            );
          })}
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { EBTRMasthead, EBTRFooter, EBTRSearch });
