// Top navigation with bilingual toggle
function Nav({ lang, setLang, t }) {
  const [scrolled, setScrolled] = React.useState(false);
  const [open, setOpen] = React.useState(false);

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

  React.useEffect(() => {
    if (!open) return;
    const prev = document.body.style.overflow;
    document.body.style.overflow = "hidden";
    const onKey = (e) => { if (e.key === "Escape") setOpen(false); };
    window.addEventListener("keydown", onKey);
    return () => {
      document.body.style.overflow = prev;
      window.removeEventListener("keydown", onKey);
    };
  }, [open]);

  const navItems = [
    { id: "about", label: t.nav.about },
    { id: "services", label: t.nav.services },
    { id: "projects", label: t.nav.projects },
    { id: "contact", label: t.nav.contact },
  ];

  const surfaceActive = open || scrolled;

  return (
    <React.Fragment>
      <nav style={{
        position: "fixed", top: 0, left: 0, right: 0,
        zIndex: open ? 70 : 50,
        background: surfaceActive ? "rgba(245,241,235,0.92)" : "transparent",
        backdropFilter: surfaceActive ? "blur(12px)" : "none",
        WebkitBackdropFilter: surfaceActive ? "blur(12px)" : "none",
        borderBottom: surfaceActive ? "1px solid var(--border)" : "1px solid transparent",
        transition: "background 200ms var(--ease), border-color 200ms var(--ease)",
      }}>
        <div className="shell" style={{
          display: "flex", alignItems: "center", justifyContent: "space-between",
          height: 72,
        }}>
          <a href="#top" onClick={() => setOpen(false)} style={{
            display: "flex", alignItems: "center", gap: 10,
            color: "var(--ink)",
          }}>
            <img src="assets/rework-wordmark.png" alt="Rework" style={{
              height: 28, width: "auto", display: "block",
              mixBlendMode: "multiply",
            }} />
          </a>

          <div style={{ display: "flex", alignItems: "center", gap: 4 }} className="nav-links">
            {navItems.map(it => (
              <a key={it.id} href={`#${it.id}`} style={{
                padding: "8px 14px",
                fontSize: 14,
                color: "var(--ink-soft)",
                borderRadius: 8,
                transition: "background 150ms var(--ease), color 150ms var(--ease)",
              }}
              onMouseEnter={e => e.currentTarget.style.background = "rgba(26,23,20,0.05)"}
              onMouseLeave={e => e.currentTarget.style.background = "transparent"}
              >{it.label}</a>
            ))}
          </div>

          <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
            <div className="lang-toggle-desktop" style={{
              display: "inline-flex", alignItems: "center",
              border: "1px solid var(--border-strong)",
              borderRadius: 999,
              padding: 3,
              background: "var(--surface)",
            }}>
              {["no", "en"].map(l => (
                <button key={l} onClick={() => setLang(l)} style={{
                  padding: "11px 14px",
                  borderRadius: 999,
                  fontSize: 11,
                  fontWeight: 600,
                  letterSpacing: "0.1em",
                  textTransform: "uppercase",
                  color: lang === l ? "var(--canvas)" : "var(--muted)",
                  background: lang === l ? "var(--ink)" : "transparent",
                  transition: "all 180ms var(--ease)",
                }}>{l}</button>
              ))}
            </div>

            <button
              className="hamburger"
              aria-label={open ? "Lukk meny" : "Åpne meny"}
              aria-expanded={open}
              onClick={() => setOpen(o => !o)}
              style={{
                width: 44, height: 44,
                display: "none",
                alignItems: "center", justifyContent: "center",
                color: "var(--ink)",
                borderRadius: 8,
              }}
            >
              <svg width="22" height="22" viewBox="0 0 22 22" fill="none" aria-hidden="true">
                {open ? (
                  <React.Fragment>
                    <line x1="5" y1="5" x2="17" y2="17" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
                    <line x1="17" y1="5" x2="5" y2="17" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
                  </React.Fragment>
                ) : (
                  <React.Fragment>
                    <line x1="4" y1="7" x2="18" y2="7" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
                    <line x1="4" y1="11" x2="18" y2="11" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
                    <line x1="4" y1="15" x2="18" y2="15" stroke="currentColor" strokeWidth="1.6" strokeLinecap="round" />
                  </React.Fragment>
                )}
              </svg>
            </button>
          </div>
        </div>

        <style>{`
          @media (max-width: 880px) {
            .nav-links { display: none !important; }
            .lang-toggle-desktop { display: none !important; }
            .hamburger { display: inline-flex !important; }
          }
        `}</style>
      </nav>

      {open && (
        <div
          role="dialog"
          aria-modal="true"
          aria-label="Hovedmeny"
          style={{
            position: "fixed", inset: 0, zIndex: 60,
            background: "var(--canvas)",
            display: "flex", flexDirection: "column",
            paddingTop: 72,
            animation: "fadeIn 200ms var(--ease)",
          }}
        >
          <div style={{
            flex: 1,
            display: "flex", flexDirection: "column",
            justifyContent: "center", alignItems: "stretch",
            padding: "0 32px",
            gap: 4,
          }}>
            {navItems.map((it, i) => (
              <a
                key={it.id}
                href={`#${it.id}`}
                onClick={() => setOpen(false)}
                style={{
                  display: "block",
                  padding: "18px 8px",
                  fontSize: 32,
                  fontWeight: 500,
                  letterSpacing: "-0.02em",
                  color: "var(--ink)",
                  borderBottom: i < navItems.length - 1 ? "1px solid var(--border)" : "none",
                  fontFamily: "var(--font-display)",
                }}
              >{it.label}</a>
            ))}
          </div>

          <div style={{
            padding: "24px 32px 40px",
            borderTop: "1px solid var(--border)",
            display: "flex", justifyContent: "center",
          }}>
            <div style={{
              display: "inline-flex", alignItems: "center",
              border: "1px solid var(--border-strong)",
              borderRadius: 999,
              padding: 4,
              background: "var(--surface)",
            }}>
              {["no", "en"].map(l => (
                <button key={l} onClick={() => setLang(l)} style={{
                  padding: "12px 22px",
                  borderRadius: 999,
                  fontSize: 12,
                  fontWeight: 600,
                  letterSpacing: "0.12em",
                  textTransform: "uppercase",
                  color: lang === l ? "var(--canvas)" : "var(--muted)",
                  background: lang === l ? "var(--ink)" : "transparent",
                  transition: "all 180ms var(--ease)",
                }}>{l}</button>
              ))}
            </div>
          </div>

          <style>{`
            @keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
          `}</style>
        </div>
      )}
    </React.Fragment>
  );
}

window.Nav = Nav;
