function Nav() {
  const [scrolled, setScrolled] = React.useState(false);
  const [open, setOpen] = React.useState(false);

  const openBookingModal = () => {
    let modal = document.getElementById('booking-modal');
    if (!modal) {
      modal = document.createElement('div');
      modal.id = 'booking-modal';
      modal.style.cssText = `
        position: fixed; top: 0; left: 0; right: 0; bottom: 0;
        background: rgba(0,0,0,0.5); display: flex; align-items: center;
        justify-content: center; z-index: 9999;
      `;
      modal.innerHTML = `
        <div style="background: white; border-radius: 12px;
          width: 90vw; height: 90vh; max-width: 1200px; max-height: 800px;
          overflow: hidden; position: relative;">
          <button id="modal-close" style="position: absolute; top: 20px; right: 20px;
            background: white; border: none; font-size: 24px; cursor: pointer;
            color: #666; z-index: 10000; width: 36px; height: 36px;
            border-radius: 50%; display: flex; align-items: center;
            justify-content: center;">&times;</button>
          <iframe id="calendar-iframe"
            src="https://calendar.google.com/calendar/appointments/schedules/AcZssZ1reaYeikSmVMJn2-PxYgQ0PiwKIqdmx6K8NZHazWXDjkiNuK9MG6PTyF6utsUZU7rFxn8tJtSF?gv=true"
            style="width: 100%; height: 100%; border: none; border-radius: 12px;">
          </iframe>
        </div>
      `;
      document.body.appendChild(modal);

      document.getElementById('modal-close').addEventListener('click', () => {
        modal.remove();
      });

      modal.addEventListener('click', (e) => {
        if (e.target === modal) modal.remove();
      });
    } else {
      modal.style.display = 'flex';
    }
  };

  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 20);
    window.addEventListener("scroll", onScroll);
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const links = [
    { href: "#about", label: "About" },
    { href: "#how", label: "How it works" },
    { href: "#pricing", label: "Pricing" },
    { href: "#faq", label: "FAQ" },
  ];

  return (
    <nav style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 50,
      transition: "all .3s ease",
      background: scrolled ? "rgba(251, 248, 241, 0.88)" : "transparent",
      backdropFilter: scrolled ? "blur(14px) saturate(140%)" : "none",
      WebkitBackdropFilter: scrolled ? "blur(14px) saturate(140%)" : "none",
      borderBottom: scrolled ? "1px solid var(--line)" : "1px solid transparent",
    }}>
      <div className="wrap" style={{
        display: "flex", alignItems: "center", justifyContent: "space-between",
        height: 76,
      }}>
        {/* Logo */}
        <a href="#top" style={{
          display: "flex", alignItems: "center", gap: 10,
          textDecoration: "none", color: "var(--forest)",
        }}>
          <span style={{
            display: "grid", placeItems: "center",
            width: 36, height: 36, borderRadius: "50%",
            background: "var(--forest)", color: "var(--cream)",
          }}>
            <IconSprout size={20} stroke="currentColor" />
          </span>
          <span style={{
            fontFamily: "var(--serif)", fontSize: 26, fontWeight: 500,
            letterSpacing: "-0.01em",
          }}>Zeezoo</span>
        </a>

        {/* Desktop nav */}
        <ul style={{
          display: "flex", gap: 36, listStyle: "none",
          alignItems: "center",
        }} className="desktop-only">
          {links.map(l => (
            <li key={l.href}>
              <a href={l.href} style={{
                color: "var(--forest)", textDecoration: "none",
                fontSize: 14.5, fontWeight: 500,
              }}>{l.label}</a>
            </li>
          ))}
        </ul>

        {/* CTA */}
        <div style={{ display: "flex", alignItems: "center", gap: 12 }}>
          <a href="tel:+15104626609" style={{
            color: "var(--forest)", textDecoration: "none",
            fontSize: 14.5, fontWeight: 500,
            display: "flex", alignItems: "center", gap: 8,
          }} className="desktop-only">
            <IconPhone size={16} />
            (510) 462-6609
          </a>
          <button onClick={openBookingModal} className="btn btn-primary" style={{ padding: "12px 22px", fontSize: 14 }}>
            Book consult
          </button>
          <button onClick={() => setOpen(!open)} className="mobile-only" aria-label="Menu" style={{
            width: 40, height: 40, display: "grid", placeItems: "center",
            border: "1px solid var(--line)", borderRadius: 999, color: "var(--forest)",
          }}>
            <svg width="18" height="14" viewBox="0 0 18 14" stroke="currentColor" strokeWidth="1.6">
              <line x1="0" y1="2" x2="18" y2="2" />
              <line x1="0" y1="7" x2="18" y2="7" />
              <line x1="0" y1="12" x2="18" y2="12" />
            </svg>
          </button>
        </div>
      </div>

      {/* Mobile sheet */}
      {open && (
        <div className="mobile-only" style={{
          background: "var(--paper)", borderTop: "1px solid var(--line)",
          padding: "16px 20px 24px",
        }}>
          <ul style={{ listStyle: "none", display: "flex", flexDirection: "column", gap: 4 }}>
            {links.map(l => (
              <li key={l.href}>
                <a href={l.href} onClick={() => setOpen(false)} style={{
                  display: "block", padding: "12px 4px", color: "var(--forest)",
                  textDecoration: "none", fontWeight: 500,
                  borderBottom: "1px solid var(--line)",
                }}>{l.label}</a>
              </li>
            ))}
          </ul>
        </div>
      )}

      <style>{`
        @media (max-width: 880px) {
          .desktop-only { display: none !important; }
        }
        @media (min-width: 881px) {
          .mobile-only { display: none !important; }
        }
      `}</style>
    </nav>
  );
}

window.Nav = Nav;
