/* ============ NAVIGATION ============ */
const NAV_MAP = {
  Home: "hero", About: "about", Rooms: "rooms", Cafe: "cafe",
  Gallery: "gallery", Experiences: "experiences", Reviews: "reviews", Contact: "contact"
};

function ThemeToggle({ theme, onToggle }) {
  const dark = theme === "dark";
  return (
    <button
      className="theme-toggle"
      onClick={onToggle}
      aria-label={`Switch to ${dark ? "light" : "dark"} mode`}
      title={dark ? "Daytime / forest mode" : "Night / neon mode"}
    >
      <span className={`tt-track ${dark ? "on" : ""}`}>
        <span className="tt-thumb">
          <Icon name={dark ? "moon" : "sun"} />
        </span>
        <Icon name="mountain-sun" className="tt-ic tt-sun" />
        <Icon name="star" className="tt-ic tt-moon" />
      </span>
    </button>
  );
}

function Nav({ theme, onToggle }) {
  const [scrolled, setScrolled] = useState(false);
  const [open, setOpen] = useState(false);
  const [active, setActive] = useState("hero");
  const S = window.SITE;

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

  useEffect(() => {
    const ids = Object.values(NAV_MAP);
    const onScroll = () => {
      const pos = window.scrollY + window.innerHeight * 0.35;
      let current = ids[0];
      for (const id of ids) {
        const el = document.getElementById(id);
        if (el && el.offsetTop <= pos) current = id;
      }
      setActive(current);
    };
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const go = (id) => (e) => {
    e.preventDefault();
    setOpen(false);
    const el = document.getElementById(id);
    if (el) window.scrollTo({ top: el.getBoundingClientRect().top + window.scrollY - 76, behavior: "smooth" });
  };

  return (
    <header className={`nav ${scrolled ? "scrolled" : ""}`}>
      <div className="nav-inner container">
        <a href="#hero" onClick={go("hero")} className="brand serif">
          The Last Stop<sup>®</sup>
        </a>

        <nav className="nav-links">
          {S.nav.map((item) => {
            const id = NAV_MAP[item];
            return (
              <a key={item} href={`#${id}`} onClick={go(id)} className={active === id ? "active" : ""}>
                {item}
              </a>
            );
          })}
        </nav>

        <div className="nav-right">
          <ThemeToggle theme={theme} onToggle={onToggle} />
          <a href="#contact" onClick={go("contact")} className="btn btn-primary nav-cta">Book Now</a>
          <button className="hamburger" aria-label="Menu" onClick={() => setOpen((o) => !o)}>
            <Icon name={open ? "xmark" : "bars"} />
          </button>
        </div>
      </div>

      <div className={`mobile-menu ${open ? "open" : ""}`}>
        {S.nav.map((item) => (
          <a key={item} href={`#${NAV_MAP[item]}`} onClick={go(NAV_MAP[item])} className="serif">{item}</a>
        ))}
        <a href="#contact" onClick={go("contact")} className="btn btn-primary" style={{ marginTop: 12 }}>Book Now</a>
      </div>

      <style>{`
        .nav {
          position: fixed; top: 0; left: 0; right: 0; z-index: 100;
          transition: background .4s var(--ease), box-shadow .4s, border-color .4s, backdrop-filter .4s;
          border-bottom: 1px solid transparent;
        }
        .nav.scrolled {
          background: var(--nav-glass);
          backdrop-filter: blur(18px) saturate(150%); -webkit-backdrop-filter: blur(18px) saturate(150%);
          border-bottom: 1px solid var(--border);
        }
        .nav-inner { display: flex; align-items: center; justify-content: space-between; height: 72px; gap: 20px; }
        .brand { font-size: 1.6rem; color: var(--text); letter-spacing: -.02em; white-space: nowrap; }
        .brand sup { font-size: .5em; font-family: var(--sans); color: var(--primary); top: -.9em; }
        html[data-theme="dark"] .brand { text-shadow: 0 0 26px rgba(54,230,236,.25); }

        .nav-links { display: flex; gap: 30px; align-items: center; }
        .nav-links a {
          font-size: 14px; font-weight: 500; color: var(--text-2); position: relative; padding: 4px 0;
          transition: color .25s;
        }
        .nav-links a:hover { color: var(--text); }
        .nav-links a.active { color: var(--text); }
        .nav-links a.active::after {
          content: ""; position: absolute; left: 0; right: 0; bottom: -2px; height: 2px;
          background: var(--primary); border-radius: 2px;
        }
        html[data-theme="dark"] .nav-links a.active::after { box-shadow: 0 0 10px var(--primary); }

        .nav-right { display: flex; align-items: center; gap: 16px; }

        /* theme toggle */
        .theme-toggle { padding: 4px; border-radius: 999px; }
        .tt-track {
          position: relative; width: 60px; height: 30px; border-radius: 999px; display: block;
          background: linear-gradient(120deg, #8FB7E8, #cfe3f5); border: 1px solid var(--border);
          transition: background .5s var(--ease), box-shadow .5s; overflow: hidden;
        }
        .tt-track.on { background: linear-gradient(120deg, #1a1340, #3a1f63); box-shadow: inset 0 0 14px rgba(54,230,236,.4); }
        .tt-thumb {
          position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 999px;
          background: #FFD27D; display: flex; align-items: center; justify-content: center; color: #7a4a00;
          font-size: 11px; transition: left .5s var(--ease), background .5s, color .5s; z-index: 2;
          box-shadow: 0 2px 8px rgba(0,0,0,.25);
        }
        .tt-track.on .tt-thumb { left: 33px; background: #36E6EC; color: #06121A; box-shadow: 0 0 14px var(--primary); }
        .tt-ic { position: absolute; top: 50%; transform: translateY(-50%); font-size: 10px; transition: opacity .4s; }
        .tt-sun { right: 8px; color: #fff; opacity: 1; }
        .tt-moon { left: 9px; color: #FFB15A; opacity: 0; }
        .tt-track.on .tt-sun { opacity: 0; }
        .tt-track.on .tt-moon { opacity: 1; }

        .hamburger { display: none; font-size: 22px; color: var(--text); width: 40px; height: 40px; }

        .mobile-menu {
          display: none; flex-direction: column; gap: 6px; padding: 0 28px;
          max-height: 0; overflow: hidden; transition: max-height .5s var(--ease), padding .4s;
          background: var(--nav-glass); backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px);
          border-bottom: 1px solid transparent;
        }
        .mobile-menu.open { max-height: 520px; padding: 14px 28px 26px; border-bottom: 1px solid var(--border); }
        .mobile-menu a { font-size: 1.5rem; color: var(--text); padding: 8px 0; }
        .mobile-menu a.btn { font-size: 15px; font-family: var(--sans); justify-content: center; }

        @media (max-width: 940px) {
          .nav-links { display: none; }
          .nav-cta { display: none; }
          .hamburger { display: flex; align-items: center; justify-content: center; }
          .mobile-menu { display: flex; }
        }
      `}</style>
    </header>
  );
}

Object.assign(window, { Nav });
