/* NIA — shared chrome: navbar, footer, i18n */

const { useState, useEffect, useRef } = React;

/* ---------- i18n (global pub/sub so every useLang instance stays in sync) ---------- */
const LANG_KEY = "nia.lang";
const _langListeners = new Set();
let _langValue = (typeof window !== "undefined" && localStorage.getItem(LANG_KEY)) || "fr";
function _setLangGlobal(v) {
  _langValue = v;
  try { localStorage.setItem(LANG_KEY, v); document.documentElement.setAttribute("lang", v); } catch(e){}
  _langListeners.forEach(fn => fn(v));
}
function useLang() {
  const [lang, setLocal] = useState(_langValue);
  useEffect(() => {
    _langListeners.add(setLocal);
    return () => { _langListeners.delete(setLocal); };
  }, []);
  return [lang, _setLangGlobal];
}
function t(lang, fr, en) { return lang === "en" ? en : fr; }
window.useLang = useLang;
window.t = t;

/* ---------- Monogram ---------- */
function Monogram({ size = 32 }) {
  return (
    <div style={{
      width: size, height: size, background: "var(--ink)", color: "var(--paper)",
      display: "inline-flex", alignItems: "center", justifyContent: "center",
      fontFamily: "var(--ff-mono)", fontSize: size * 0.44, fontWeight: 500,
      position: "relative", borderRadius: 2, letterSpacing: 0,
    }}>
      N
      <span style={{
        position: "absolute", top: size * 0.18, right: size * 0.18,
        width: 4, height: 4, borderRadius: "50%", background: "var(--acid)",
      }} />
    </div>
  );
}
window.Monogram = Monogram;

/* ---------- Navbar ---------- */
function Navbar({ current = "home" }) {
  const [lang, setLang] = useLang();
  const [scrolled, setScrolled] = useState(false);
  const [menu, setMenu] = useState(false);
  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 8);
    window.addEventListener("scroll", onScroll); onScroll();
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const links = [
    { key: "labels", fr: "Labels", en: "Labels", href: "labels.html" },
    { key: "marques", fr: "Marques", en: "Brands", href: "marques.html" },
    { key: "agences", fr: "Agences", en: "Agencies", href: "agences.html" },
    { key: "createurs", fr: "Créateurs", en: "Creators", href: "createurs.html" },
  ];

  return (
    <>
    <nav className="nia-nav" style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 100,
      height: 64, display: "flex", alignItems: "center",
      background: scrolled ? "rgba(242, 238, 227, 0.78)" : "transparent",
      backdropFilter: scrolled ? "blur(24px) saturate(1.2)" : "none",
      WebkitBackdropFilter: scrolled ? "blur(24px) saturate(1.2)" : "none",
      borderBottom: scrolled ? "1px solid var(--rule)" : "1px solid transparent",
      transition: "background 0.3s ease, border-color 0.3s ease",
    }}>
      <div className="container" style={{ display: "flex", alignItems: "center", gap: 40, width: "100%" }}>
        <a href="/" style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <Monogram size={32} />
        </a>
        <div style={{ display: "flex", gap: 28, flex: 1 }} className="nav-links">
          {links.map(l => (
            <a key={l.key} href={l.href} className="mono"
              style={{
                color: current === l.key ? "var(--ink)" : "var(--graphite)",
                fontSize: 12, padding: "4px 0", position: "relative",
                borderBottom: current === l.key ? "1px solid var(--ink)" : "1px solid transparent",
                transition: "color 0.15s",
              }}
              onMouseEnter={e => e.currentTarget.style.color = "var(--ink)"}
              onMouseLeave={e => e.currentTarget.style.color = current === l.key ? "var(--ink)" : "var(--graphite)"}
            >{t(lang, l.fr, l.en)}</a>
          ))}
        </div>
        <div className="nav-actions" style={{ display: "flex", alignItems: "center", gap: 20, marginLeft: "auto" }}>
          <LangToggle lang={lang} setLang={setLang} />
          <a href="/connexion" className="mono nav-signin" style={{ color: "var(--graphite)", fontSize: 12 }}>
            {t(lang, "Se connecter", "Sign in")}
          </a>
          <a href="/demander-une-demo" className="btn btn--ink nav-demo">
            {t(lang, "Demander une démo", "Book a demo")}
          </a>
        </div>
        <button className="nav-burger" aria-label="Menu" onClick={() => setMenu(!menu)} style={{
          display: "none", width: 40, height: 40, padding: 0,
          alignItems: "center", justifyContent: "center", background: "transparent",
          border: "1px solid var(--rule)", borderRadius: 2, marginLeft: "auto",
        }}>
          <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
            <span style={{ width: 18, height: 1.5, background: "var(--ink)", transform: menu ? "translateY(3px) rotate(45deg)" : "none", transition: "transform 0.2s" }} />
            <span style={{ width: 18, height: 1.5, background: "var(--ink)", opacity: menu ? 0 : 1, transition: "opacity 0.2s" }} />
            <span style={{ width: 18, height: 1.5, background: "var(--ink)", transform: menu ? "translateY(-3px) rotate(-45deg)" : "none", transition: "transform 0.2s" }} />
          </div>
        </button>
      </div>
    </nav>

      {/* Mobile menu overlay */}
    {menu && (
      <div className="nav-menu-overlay" style={{
        position: "fixed", inset: 0, zIndex: 99, background: "var(--paper)",
        paddingTop: 88, padding: "88px 24px 32px", display: "flex", flexDirection: "column", gap: 0,
      }} onClick={() => setMenu(false)}>
        {links.map(l => (
          <a key={l.key} href={l.href} className="display" style={{
            fontSize: 40, color: current === l.key ? "var(--ink)" : "var(--graphite)",
            padding: "20px 0", borderBottom: "1px solid var(--rule)", letterSpacing: "-0.02em",
          }}>{t(lang, l.fr, l.en)}</a>
        ))}
        <div style={{ padding: "24px 0", display: "flex", alignItems: "center", gap: 20 }}>
          <LangToggle lang={lang} setLang={setLang} />
          <a href="/connexion" className="mono" style={{ fontSize: 12, color: "var(--graphite)" }}>
            {t(lang, "Se connecter", "Sign in")} →
          </a>
        </div>
        <a href="/demander-une-demo" className="btn btn--ink" style={{
          marginTop: 8, alignSelf: "flex-start",
        }}>{t(lang, "Demander une démo", "Book a demo")} →</a>
      </div>
    )}
    </>
  );
}
function LangToggle({ lang, setLang }) {
  return (
    <div className="lang-toggle" style={{
      display: "inline-flex", border: "1px solid var(--rule)", borderRadius: 999,
      padding: 3, fontFamily: "var(--ff-mono)", fontSize: 11, letterSpacing: "0.08em",
      textTransform: "uppercase", flexShrink: 0, whiteSpace: "nowrap",
    }}>
      {["fr", "en"].map(l => (
        <button key={l} onClick={() => setLang(l)} style={{
          padding: "5px 12px", borderRadius: 999, border: "none",
          background: lang === l ? "var(--ink)" : "transparent",
          color: lang === l ? "var(--paper)" : "var(--graphite)",
          cursor: "pointer", fontFamily: "inherit", fontSize: "inherit",
          letterSpacing: "inherit", textTransform: "inherit",
          whiteSpace: "nowrap", flexShrink: 0,
          transition: "all 0.15s",
        }}>{l}</button>
      ))}
    </div>
  );
}
window.Navbar = Navbar;

/* ---------- Footer ---------- */
function Footer() {
  const [lang] = useLang();
  const cols = [
    { title: t(lang, "Produit", "Product"), links: ["Labels", t(lang, "Marques", "Brands"), t(lang, "Agences", "Agencies"), t(lang, "Créateurs", "Creators"), t(lang, "Sécurité", "Security")] },
    { title: t(lang, "Ressources", "Resources"), links: [t(lang, "Études de cas", "Case studies"), "Blog", t(lang, "Presse", "Press")] },
    { title: t(lang, "Entreprise", "Company"), links: [t(lang, "À propos", "About"), t(lang, "Équipe", "Team"), t(lang, "Carrières", "Careers"), "Contact"] },
    { title: t(lang, "Légal", "Legal"), links: ["CGU", t(lang, "Confidentialité", "Privacy"), "DPA", "Cookies", t(lang, "Mentions", "Imprint")] },
  ];
  return (
    <footer style={{
      position: "relative", padding: "80px 0 40px", overflow: "hidden",
      background: "radial-gradient(ellipse 80% 60% at 20% 0%, rgba(197, 238, 63, 0.14), transparent 60%), radial-gradient(ellipse 60% 50% at 90% 100%, rgba(197, 238, 63, 0.08), transparent 55%), linear-gradient(180deg, #0d0f0a 0%, #16190f 40%, #0a0b07 100%)",
      color: "var(--paper)",
    }}>
      {/* animated grain overlay */}
      <div style={{
        position: "absolute", inset: 0,
        backgroundImage: "url(\"data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='200' height='200'><filter id='n'><feTurbulence baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.77  0 0 0 0 0.93  0 0 0 0 0.25  0 0 0 0.55 0'/></filter><rect width='200' height='200' filter='url(%23n)' opacity='0.35'/></svg>\")",
        mixBlendMode: "overlay", opacity: 0.25, pointerEvents: "none",
      }} />

      {/* vertical acid rule on left */}
      <div style={{
        position: "absolute", top: 0, bottom: 0, left: "max(24px, 4vw)",
        width: 1, background: "linear-gradient(180deg, transparent, var(--acid) 30%, var(--acid) 70%, transparent)",
        opacity: 0.35, pointerEvents: "none",
      }} />

      <div className="container" style={{ position: "relative", zIndex: 2 }}>
        {/* Top row: monogram + status */}
        <div style={{ display: "flex", alignItems: "center", gap: 20, marginBottom: 72 }}>
          <div style={{
            width: 48, height: 48, background: "var(--acid)", color: "#0a0b07",
            display: "inline-flex", alignItems: "center", justifyContent: "center",
            fontFamily: "var(--ff-mono)", fontSize: 22, fontWeight: 500,
            borderRadius: 2, position: "relative",
            boxShadow: "0 0 40px rgba(197, 238, 63, 0.4), 0 0 80px rgba(197, 238, 63, 0.2)",
          }}>
            N
            <span style={{
              position: "absolute", top: 9, right: 9, width: 5, height: 5,
              borderRadius: "50%", background: "#0a0b07",
            }} />
          </div>
          <div className="mono" style={{ fontSize: 11, color: "rgba(242,238,227,0.55)", letterSpacing: "0.14em", display: "flex", gap: 16, alignItems: "center" }}>
            <span style={{ color: "var(--acid)" }}>● {t(lang, "OPÉRATIONNEL", "OPERATIONAL")}</span>
            <span>NIA · PARIS · {t(lang, "DEPUIS 2024", "SINCE 2024")}</span>
          </div>
        </div>

        {/* Columns */}
        <div style={{
          display: "grid", gridTemplateColumns: "1fr 1fr 1fr 1fr 1.6fr", gap: 48,
          paddingTop: 48, borderTop: "1px solid rgba(197, 238, 63, 0.18)",
          marginBottom: 80,
        }}>
          {cols.map(c => (
            <div key={c.title}>
              <div className="mono" style={{ color: "var(--acid)", fontSize: 10, marginBottom: 20, letterSpacing: "0.14em" }}>— {c.title.toUpperCase()}</div>
              <ul style={{ listStyle: "none", display: "flex", flexDirection: "column", gap: 12, padding: 0, margin: 0 }}>
                {c.links.map(l => (
                  <li key={l}>
                    <a href="#" style={{
                      color: "rgba(242,238,227,0.8)", fontSize: 14, textDecoration: "none",
                      transition: "color 0.15s", display: "inline-block",
                    }}
                    onMouseEnter={e => { e.currentTarget.style.color = "var(--acid)"; }}
                    onMouseLeave={e => { e.currentTarget.style.color = "rgba(242,238,227,0.8)"; }}
                    >{l}</a>
                  </li>
                ))}
              </ul>
            </div>
          ))}
          <div>
            <div className="mono" style={{ color: "var(--acid)", fontSize: 10, marginBottom: 20, letterSpacing: "0.14em" }}>
              — {t(lang, "NEWSLETTER", "NEWSLETTER")}
            </div>
            <div style={{ color: "rgba(242,238,227,0.65)", fontSize: 13, lineHeight: 1.5, marginBottom: 20 }}>
              {t(lang, "Les signaux culturels du mois, sans bruit. 1× / mois.", "Monthly cultural signals, no noise. 1× / month.")}
            </div>
            <form onSubmit={e => e.preventDefault()} style={{
              display: "flex", alignItems: "center",
              border: "1px solid rgba(197, 238, 63, 0.3)",
              borderRadius: 999, padding: "4px 4px 4px 18px",
              background: "rgba(197, 238, 63, 0.05)",
              transition: "border-color 0.2s",
            }}
            onFocus={e => e.currentTarget.style.borderColor = "var(--acid)"}
            >
              <input placeholder={t(lang, "votre@email.com", "your@email.com")} style={{
                flex: 1, background: "transparent", border: "none", color: "var(--paper)",
                fontSize: 13, padding: "10px 0", outline: "none", fontFamily: "inherit",
              }} />
              <button style={{
                background: "var(--acid)", color: "#0a0b07", border: "none",
                borderRadius: 999, padding: "8px 16px", fontFamily: "var(--ff-mono)",
                fontSize: 11, letterSpacing: "0.08em", cursor: "pointer",
                textTransform: "uppercase",
              }}>OK →</button>
            </form>
            <div style={{ display: "flex", gap: 20, marginTop: 28 }}>
              {["LinkedIn", "Instagram", "X"].map(s => (
                <a key={s} href="#" className="mono" style={{
                  color: "rgba(242,238,227,0.55)", fontSize: 11, letterSpacing: "0.08em",
                  textDecoration: "none", transition: "color 0.15s",
                }}
                onMouseEnter={e => e.currentTarget.style.color = "var(--acid)"}
                onMouseLeave={e => e.currentTarget.style.color = "rgba(242,238,227,0.55)"}
                >{s} ↗</a>
              ))}
            </div>
          </div>
        </div>

        {/* Bottom bar */}
        <div style={{
          paddingTop: 32, display: "flex", justifyContent: "space-between",
          alignItems: "center", flexWrap: "wrap", gap: 20,
          borderTop: "1px solid rgba(242,238,227,0.1)",
        }}>
          <div className="mono" style={{ color: "rgba(242,238,227,0.45)", fontSize: 11, letterSpacing: "0.08em" }}>
            © 2026 NIA — {t(lang, "Tous droits réservés", "All rights reserved")}
          </div>
          <div className="mono" style={{ color: "rgba(242,238,227,0.45)", fontSize: 11, letterSpacing: "0.08em", display: "flex", gap: 20, alignItems: "center" }}>
            <span style={{ color: "var(--acid)" }}>●</span>
            {t(lang, "Paris, France", "Paris, France")}
            <span>·</span>
            <span>{t(lang, "Made by AMRAOUI YANIS.", "Made by AMRAOUI YANIS.")}</span>
          </div>
        </div>

        {/* Giant wordmark with gradient */}
        <div style={{
          position: "absolute", left: 0, right: 0, bottom: -60,
          fontFamily: "var(--ff-display)", fontSize: "clamp(140px, 22vw, 340px)",
          fontWeight: 400, letterSpacing: "-0.06em", lineHeight: 0.8,
          textAlign: "center", pointerEvents: "none", userSelect: "none",
          background: "linear-gradient(180deg, rgba(197, 238, 63, 0.06) 0%, rgba(197, 238, 63, 0.015) 50%, transparent 100%)",
          WebkitBackgroundClip: "text", WebkitTextFillColor: "transparent",
          backgroundClip: "text",
        }}>NIA</div>
      </div>
    </footer>
  );
}
window.Footer = Footer;
