
const Nav = ({ tweaks, currentSection, navigateTo, isVisible = true, isHeroAttached = false }) => {
  const [open, setOpen] = React.useState(false);
  const [scrolled, setScrolled] = React.useState(false);
  const [isMobile, setIsMobile] = React.useState(() => window.innerWidth <= 768);

  const navItems = [
    { id: "projects",   label: "Projects",   num: "01" },
    { id: "about",      label: "About",      num: "02" },
    { id: "skills",     label: "Skills",     num: "03" },
    { id: "experience", label: "Experience", num: "04" },
    { id: "contact",    label: "Contact",    num: "05" },
  ];

  React.useEffect(() => {
    setScrolled(currentSection !== "hero");
  }, [currentSection]);

  React.useEffect(() => {
    const mediaQuery = window.matchMedia("(max-width: 768px)");
    const syncMobile = () => {
      const mobile = mediaQuery.matches;
      setIsMobile(mobile);
      if (!mobile) setOpen(false);
    };
    syncMobile();
    mediaQuery.addEventListener("change", syncMobile);
    return () => mediaQuery.removeEventListener("change", syncMobile);
  }, []);

  const navStyle = tweaks?.navStyle || "pill";
  const handleNavigate = (id) => { navigateTo(id); setOpen(false); };
  const isHeroSection = currentSection === "hero";
  const useHeroIntegratedStyle = isHeroSection && isHeroAttached;

  if (!isVisible) return null;

  const LogoBlock = ({ size = "desktop" }) => (
    <button onClick={() => handleNavigate("hero")} style={{
      display: "flex", alignItems: "center",
      gap: size === "mobile" ? 10 : 14,
      background: "none", border: "none", cursor: "pointer",
      minWidth: 0, flexShrink: 0,
    }}>
      <img src="assets/logo.png" alt="logo"
        style={{ width: size === "mobile" ? 40 : 52, height: size === "mobile" ? 40 : 52, objectFit: "contain", flexShrink: 0 }}
      />
      <div style={{ display: "flex", alignItems: "center" }}>
        <span style={{ fontFamily: "Roboto Mono, monospace", fontWeight: 800, fontSize: size === "mobile" ? 12 : 15, letterSpacing: size === "mobile" ? "0.12em" : "0.14em", lineHeight: 1, whiteSpace: "nowrap" }}>
          <span style={{ color: "var(--teal)", textShadow: "0 0 12px oklch(0.65 0.16 185 / 0.85), 0 0 28px oklch(0.65 0.16 185 / 0.4)", animation: "tealGlow 2.5s ease-in-out infinite" }}>FRANCIS</span>
          <span style={{ color: "rgba(255,255,255,0.78)", textShadow: "0 2px 10px rgba(0,0,0,0.55)" }}> JOSEPH</span>
        </span>
      </div>
    </button>
  );

  const HireMeBtn = () => (
    <button onClick={() => handleNavigate("contact")} style={{
      background: "var(--teal)", color: "#030d0e",
      border: "none", cursor: "pointer", borderRadius: 999,
      padding: "7px 20px",
      fontFamily: "Montserrat, sans-serif", fontSize: 12, fontWeight: 800,
      letterSpacing: "0.04em", flexShrink: 0,
      transition: "all 0.2s",
      boxShadow: "inset 0 1px 0 rgba(255,255,255,0.35), 0 2px 8px rgba(20,184,166,0.25)",
    }}
    onMouseEnter={e => {
      e.currentTarget.style.boxShadow = "inset 0 1px 0 rgba(255,255,255,0.35), 0 0 22px rgba(20,184,166,0.5), 0 4px 14px rgba(0,0,0,0.3)";
      e.currentTarget.style.opacity = "0.92";
    }}
    onMouseLeave={e => {
      e.currentTarget.style.boxShadow = "inset 0 1px 0 rgba(255,255,255,0.35), 0 2px 8px rgba(20,184,166,0.25)";
      e.currentTarget.style.opacity = "1";
    }}>
      Hire Me
    </button>
  );

  const ResumeBtn = ({ pill }) => (
    <a href="Francis_Joseph_Danao_Resume.pdf" download="Francis_Joseph_Danao_Resume.pdf" style={{
      display: "flex", alignItems: "center", gap: 6,
      border: "1px solid rgba(255,255,255,0.14)",
      borderRadius: 999,
      padding: pill ? "6px 14px" : "7px 16px",
      fontFamily: "Roboto Mono, monospace", fontSize: 10, fontWeight: 600,
      color: "rgba(255,255,255,0.5)", textDecoration: "none",
      letterSpacing: "0.12em", textTransform: "uppercase",
      transition: "all 0.2s",
      background: "rgba(255,255,255,0.04)",
      boxShadow: "inset 0 1px 0 rgba(255,255,255,0.1)",
    }}
    onMouseEnter={e => {
      e.currentTarget.style.background = "rgba(20,184,166,0.1)";
      e.currentTarget.style.borderColor = "rgba(20,184,166,0.5)";
      e.currentTarget.style.color = "var(--teal)";
      e.currentTarget.style.boxShadow = "inset 0 1px 0 rgba(255,255,255,0.1), 0 0 16px rgba(20,184,166,0.2)";
    }}
    onMouseLeave={e => {
      e.currentTarget.style.background = "rgba(255,255,255,0.04)";
      e.currentTarget.style.borderColor = "rgba(255,255,255,0.14)";
      e.currentTarget.style.color = "rgba(255,255,255,0.5)";
      e.currentTarget.style.boxShadow = "inset 0 1px 0 rgba(255,255,255,0.1)";
    }}>
      <svg width="10" height="10" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5"><path d="M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4M7 10l5 5 5-5M12 15V3"/></svg>
      Resume
    </a>
  );

  const MobileMenuButton = (
    <button
      aria-label={open ? "Close navigation menu" : "Open navigation menu"}
      aria-expanded={open}
      onClick={() => setOpen(v => !v)}
      style={{
        width: 42, height: 42, borderRadius: 10,
        border: "1px solid rgba(255,255,255,0.18)",
        background: open ? "rgba(9,16,18,0.96)" : "rgba(9,16,18,0.94)",
        backdropFilter: "blur(28px) saturate(1.08)", WebkitBackdropFilter: "blur(28px) saturate(1.08)",
        color: open ? "rgba(126,230,219,0.95)" : "rgba(255,255,255,0.88)",
        display: "flex", alignItems: "center", justifyContent: "center",
        cursor: "pointer", flexShrink: 0,
        boxShadow: "inset 0 1px 0 rgba(255,255,255,0.1), 0 14px 32px rgba(0,0,0,0.34)",
        transition: "all 0.2s",
      }}
    >
      <svg width="17" height="17" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2">
        {open ? <path d="M6 6l12 12M18 6L6 18" /> : <path d="M4 7h16M4 12h16M4 17h16" />}
      </svg>
    </button>
  );

  const MobileMenu = ({ compact }) => open ? (
    <div style={{
      position: "absolute",
      top: compact ? "calc(100% + 12px)" : "calc(100% + 10px)",
      right: 0,
      width: "min(286px, calc(100vw - 24px))",
      padding: 14, borderRadius: 20,
      border: "1px solid rgba(255,255,255,0.14)",
      background: "rgba(8,13,15,0.95)",
      backdropFilter: "blur(38px) saturate(1.05)",
      WebkitBackdropFilter: "blur(38px) saturate(1.05)",
      boxShadow: "inset 0 1px 0 rgba(255,255,255,0.1), 0 28px 68px rgba(0,0,0,0.58)",
      display: "flex", flexDirection: "column", gap: 8,
    }}>
      <p style={{
        fontFamily: "Roboto Mono, monospace", fontSize: 9,
        color: "rgba(255,255,255,0.42)", letterSpacing: "0.2em",
        textTransform: "uppercase", padding: "2px 6px 8px",
        borderBottom: "1px solid rgba(255,255,255,0.1)", margin: 0,
      }}>Navigation</p>

      <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 8 }}>
        {navItems.map((item, index) => {
          const isActive = currentSection === item.id;
          return (
            <button key={`${item.id}-${index}`} onClick={() => handleNavigate(item.id)} style={{
              minHeight: 52, borderRadius: 12,
              border: isActive ? "1px solid rgba(20,184,166,0.34)" : "1px solid rgba(255,255,255,0.12)",
              background: isActive ? "rgba(20,184,166,0.12)" : "rgba(255,255,255,0.04)",
              boxShadow: isActive
                ? "inset 0 1px 0 rgba(255,255,255,0.14), 0 10px 24px rgba(0,0,0,0.22)"
                : "inset 0 1px 0 rgba(255,255,255,0.08)",
              color: isActive ? "rgba(126,230,219,0.98)" : "rgba(255,255,255,0.86)",
              fontFamily: "Roboto Mono, monospace", fontSize: 10,
              fontWeight: 600, letterSpacing: "0.1em", textTransform: "uppercase",
              cursor: "pointer", transition: "all 0.15s",
              display: "flex", flexDirection: "column", alignItems: "center", justifyContent: "center", gap: 3,
            }}>
              <span style={{ fontSize: 8, opacity: 0.4 }}>{item.num}</span>
              {item.label}
            </button>
          );
        })}
      </div>

      <div style={{ display: "flex", flexDirection: "column", gap: 6 }}>
        <ResumeBtn pill={false} />
        <button onClick={() => handleNavigate("contact")} style={{
          minHeight: 44, borderRadius: 10,
          background: "var(--teal)", color: "#030d0e",
          border: "none", cursor: "pointer",
          fontFamily: "Montserrat, sans-serif", fontSize: 12, fontWeight: 800,
          letterSpacing: "0.04em",
          boxShadow: "inset 0 1px 0 rgba(255,255,255,0.3)",
        }}>Hire Me</button>
      </div>
    </div>
  ) : null;

  // ─── Minimal nav ────────────────────────────────────────────────────────────
  if (navStyle === "minimal") {
    return (
      <nav style={{
        position: useHeroIntegratedStyle ? "absolute" : "fixed", top: 0, left: 0, right: 0, zIndex: 100,
        display: "flex", alignItems: "center", justifyContent: "space-between",
        padding: isMobile ? "14px 20px" : "16px 48px",
        minHeight: isMobile ? 64 : "auto",
        background: useHeroIntegratedStyle ? "transparent" : scrolled ? "rgba(5,12,14,0.55)" : "rgba(5,12,14,0.18)",
        backdropFilter: useHeroIntegratedStyle ? "none" : "blur(28px) saturate(1.5)",
        WebkitBackdropFilter: useHeroIntegratedStyle ? "none" : "blur(28px) saturate(1.5)",
        borderBottom: useHeroIntegratedStyle ? "none" : "1px solid rgba(255,255,255,0.07)",
        boxShadow: useHeroIntegratedStyle
          ? "none"
          : scrolled
          ? "inset 0 1px 0 rgba(255,255,255,0.06), 0 16px 48px rgba(0,0,0,0.35)"
          : "inset 0 1px 0 rgba(255,255,255,0.04)",
        transition: "background 0.35s ease, box-shadow 0.35s ease",
      }}>
        <LogoBlock size={isMobile ? "mobile" : "desktop"} />

        {isMobile ? (
          <div style={{ position: "relative", display: "flex", alignItems: "center", justifyContent: "flex-end", flexShrink: 0 }}>
            {MobileMenuButton}
            <MobileMenu compact={true} />
          </div>
        ) : (
          <div style={{ display: "flex", alignItems: "center", gap: 6 }}>
            {navItems.map((item, index) => {
              const isActive = currentSection === item.id;
              return (
                <button key={`${item.id}-${index}`} onClick={() => handleNavigate(item.id)}
                  style={{
                    background: isActive ? (useHeroIntegratedStyle ? "rgba(3,10,12,0.38)" : "rgba(255,255,255,0.08)") : "none",
                    border: "none", cursor: "pointer", borderRadius: 8,
                    padding: "6px 14px",
                    fontFamily: "Roboto Mono, monospace", fontSize: 10, fontWeight: 600,
                    color: isActive ? "rgba(181,255,247,0.98)" : "rgba(255,255,255,0.82)",
                    letterSpacing: "0.14em", textTransform: "uppercase",
                    transition: "all 0.2s",
                    boxShadow: isActive ? "inset 0 1px 0 rgba(255,255,255,0.15)" : "none",
                    textShadow: isActive
                      ? "0 2px 14px rgba(0,0,0,0.7), 0 0 10px rgba(20,184,166,0.45)"
                      : "0 2px 10px rgba(0,0,0,0.68)",
                  }}
                  onMouseEnter={e => {
                    if (!isActive) {
                      e.currentTarget.style.color = "rgba(255,255,255,0.96)";
                      e.currentTarget.style.background = useHeroIntegratedStyle ? "rgba(3,10,12,0.28)" : "rgba(255,255,255,0.06)";
                      e.currentTarget.style.textShadow = "0 2px 12px rgba(0,0,0,0.72)";
                    }
                  }}
                  onMouseLeave={e => {
                    if (!isActive) {
                      e.currentTarget.style.color = "rgba(255,255,255,0.82)";
                      e.currentTarget.style.background = "none";
                      e.currentTarget.style.textShadow = "0 2px 10px rgba(0,0,0,0.68)";
                    }
                  }}
                >{item.label}</button>
              );
            })}

            <div style={{ width: 1, height: 20, background: "rgba(255,255,255,0.1)", margin: "0 8px" }} />
            <ResumeBtn pill={false} />
            <HireMeBtn />
          </div>
        )}
      </nav>
    );
  }

  // ─── Pill nav (default) ──────────────────────────────────────────────────────
  return (
    <div style={{ position: useHeroIntegratedStyle ? "absolute" : "fixed", top: 0, left: 0, right: 0, zIndex: 100, display: "flex", justifyContent: "center", padding: isMobile ? "12px 16px" : "16px 24px" }}>
      <nav style={{
        width: "100%", maxWidth: 880,
        display: "flex", alignItems: "center", justifyContent: "space-between",
        padding: isMobile ? "6px 8px 6px 12px" : "6px 8px 6px 18px",
        borderRadius: 999,
        border: useHeroIntegratedStyle ? "none" : "1px solid rgba(255,255,255,0.14)",
        background: useHeroIntegratedStyle ? "transparent" : isMobile ? "rgba(8,13,15,0.94)" : "rgba(255,255,255,0.05)",
        backdropFilter: useHeroIntegratedStyle ? "none" : isMobile ? "blur(30px) saturate(1.05)" : "blur(28px) saturate(1.5)",
        WebkitBackdropFilter: useHeroIntegratedStyle ? "none" : isMobile ? "blur(30px) saturate(1.05)" : "blur(28px) saturate(1.5)",
        boxShadow: useHeroIntegratedStyle
          ? "none"
          : scrolled
          ? "inset 0 1px 0 rgba(255,255,255,0.14), inset 0 -1px 0 rgba(0,0,0,0.1), 0 16px 48px rgba(0,0,0,0.45)"
          : isMobile
            ? "inset 0 1px 0 rgba(255,255,255,0.1), 0 16px 36px rgba(0,0,0,0.34)"
            : "inset 0 1px 0 rgba(255,255,255,0.15), 0 4px 24px rgba(0,0,0,0.25)",
        transition: "box-shadow 0.4s ease",
        position: "relative",
      }}>
        {/* White glass shimmer on top edge */}
        <div style={{
          position: "absolute", top: 0, left: "15%", right: "15%", height: 1,
          background: "linear-gradient(90deg, transparent, rgba(255,255,255,0.25), transparent)",
          borderRadius: 999, pointerEvents: "none",
        }} />

        <LogoBlock size={isMobile ? "mobile" : "desktop"} />

        {!isMobile && (
          <div className="nav-desktop" style={{ display: "flex", gap: 2 }}>
            {navItems.map((item, index) => {
              const isActive = currentSection === item.id;
              return (
                <button key={`${item.id}-${index}`} onClick={() => handleNavigate(item.id)} style={{
                  background: isActive ? (useHeroIntegratedStyle ? "rgba(3,10,12,0.4)" : "rgba(255,255,255,0.1)") : "none",
                  border: "none", cursor: "pointer", borderRadius: 999,
                  padding: "5px 14px 6px",
                  fontFamily: "Roboto Mono, monospace", fontSize: 10, fontWeight: 600,
                  color: isActive ? "rgba(181,255,247,0.98)" : "rgba(255,255,255,0.82)",
                  letterSpacing: "0.14em", textTransform: "uppercase",
                  transition: "all 0.15s",
                  boxShadow: isActive
                    ? "inset 0 1px 0 rgba(255,255,255,0.2), inset 0 -1px 0 rgba(0,0,0,0.1)"
                    : "none",
                  textShadow: isActive
                    ? "0 2px 14px rgba(0,0,0,0.7), 0 0 12px rgba(20,184,166,0.45)"
                    : "0 2px 10px rgba(0,0,0,0.68)",
                }}
                onMouseEnter={e => { if (!isActive) { e.currentTarget.style.color = "rgba(255,255,255,0.96)"; e.currentTarget.style.background = useHeroIntegratedStyle ? "rgba(3,10,12,0.28)" : "rgba(255,255,255,0.07)"; e.currentTarget.style.textShadow = "0 2px 12px rgba(0,0,0,0.72)"; } }}
                onMouseLeave={e => { if (!isActive) { e.currentTarget.style.color = "rgba(255,255,255,0.82)"; e.currentTarget.style.background = "none"; e.currentTarget.style.textShadow = "0 2px 10px rgba(0,0,0,0.68)"; } }}
                >{item.label}</button>
              );
            })}
          </div>
        )}

        {!isMobile && (
          <div style={{ display: "flex", alignItems: "center", gap: 8, flexShrink: 0 }}>
            <ResumeBtn pill={true} />
            <HireMeBtn />
          </div>
        )}

        {isMobile && (
          <div style={{ position: "relative", marginLeft: "auto" }}>
            {MobileMenuButton}
            <MobileMenu compact={false} />
          </div>
        )}
      </nav>
    </div>
  );
};

Object.assign(window, { Nav });
