/* Router + Nav + Footer */

const { useState, useEffect, useRef, useCallback, useMemo } = React;

/* ───── hash-based router ───── */
const useRoute = () => {
  const [route, setRoute] = useState(() => window.location.hash.replace("#/", "") || "home");
  useEffect(() => {
    const onHash = () => {
      setRoute(window.location.hash.replace("#/", "") || "home");
      window.scrollTo({ top: 0, behavior: "instant" });
    };
    window.addEventListener("hashchange", onHash);
    return () => window.removeEventListener("hashchange", onHash);
  }, []);
  return route;
};

const navigate = (r) => {window.location.hash = "/" + r;};

/* ───── reveal on scroll ───── */
const useReveal = () => {
  useEffect(() => {
    const io = new IntersectionObserver(
      (entries) => entries.forEach((e) => e.isIntersecting && e.target.classList.add("in")),
      { threshold: 0.12, rootMargin: "0px 0px -10% 0px" }
    );
    document.querySelectorAll(".reveal:not(.in)").forEach((el) => io.observe(el));
    return () => io.disconnect();
  });
};

/* ───── Nav ───── */
const NAV_ITEMS = [
{ id: "home", label: "Home" },
{ id: "services", label: "Services" },
{ id: "work", label: "Work" },
{ id: "pricing", label: "Pricing" },
{ id: "about", label: "About" },
{ id: "partners", label: "Partners" },
{ id: "contact", label: "Contact" }];


const Nav = ({ route }) => {
  const [drawer, setDrawer] = useState(false);
  useEffect(() => {setDrawer(false);}, [route]);

  return (
    <>
      <nav className="nav">
        <div className="wrap nav-inner">
          <a onClick={(e) => {e.preventDefault();navigate("home");}} href="#/home" style={{ display: "flex", alignItems: "center", gap: 10, flexShrink: 0 }}>
            <M2Logo size={36} variant="dark" />
            <span style={{ fontWeight: 700, fontSize: 18, letterSpacing: "0.02em", whiteSpace: "nowrap", fontFamily: "Roboto" }}>M2 TECH</span>
          </a>

          <div className="nav-links">
            {NAV_ITEMS.map((it) =>
            <a
              key={it.id}
              href={"#/" + it.id}
              onClick={(e) => {e.preventDefault();navigate(it.id);}}
              className={"nav-link" + (route === it.id ? " active" : "")}>
              
                {it.label}
              </a>
            )}
          </div>

          <a href="#/contact"
          onClick={(e) => {e.preventDefault();navigate("contact");}}
          className="nav-cta nav-cta-desktop">
            Start a project
            <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 5l7 7-7 7" /></svg>
          </a>

          <button className="nav-burger" onClick={() => setDrawer(true)} aria-label="Open menu">
            <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M3 6h18M3 12h18M3 18h18" /></svg>
          </button>
        </div>
      </nav>

      <div className={"drawer" + (drawer ? " open" : "")}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
          <M2Logo size={36} variant="light" />
          <button onClick={() => setDrawer(false)} style={{ color: "var(--paper)", padding: 10 }} aria-label="Close">
            <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M18 6 6 18M6 6l12 12" /></svg>
          </button>
        </div>
        <div className="drawer-links">
          {NAV_ITEMS.map((it) =>
          <a
            key={it.id}
            href={"#/" + it.id}
            onClick={(e) => {e.preventDefault();navigate(it.id);setDrawer(false);}}
            className="drawer-link">
            
              {it.label} <span style={{ color: "var(--accent)" }}>↗</span>
            </a>
          )}
        </div>
        <div style={{ marginTop: "auto", paddingTop: 32, fontFamily: "var(--f-mono)", fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase", color: "rgba(246,244,239,0.5)" }}>
          support@m2-tech.co.za
        </div>
      </div>
    </>);

};

/* ───── Footer ───── */
const Footer = () =>
<footer className="footer">
    <div className="wrap" style={{ fontFamily: "Roboto" }}>
      <div className="eyebrow" style={{ color: "rgba(246,244,239,0.5)", marginBottom: 24 }}>
        ▸ Let's build something
      </div>
      <h3 style={{ fontFamily: "Roboto" }}>
        You bring the<br />idea. We<br /><em>ship it</em>.
      </h3>

      <div style={{ display: "grid", gridTemplateColumns: "1.3fr 1fr 1fr 1fr", gap: 40, alignItems: "start", borderTop: "1px solid rgba(246,244,239,0.15)", paddingTop: 40 }}
    className="footer-grid">
        <div>
          <div style={{ display: "flex", alignItems: "center", gap: 10, marginBottom: 20 }}>
            <M2Logo size={40} variant="light" />
            <span style={{ fontFamily: "var(--f-display)", fontWeight: 700, fontSize: 22 }}>M2 TECH</span>
          </div>
          <p style={{ color: "rgba(246,244,239,0.6)", fontSize: 14, maxWidth: "36ch", margin: 0 }}>
            Apps, websites and games —<br />engineered for the people who<br />actually use them.
          </p>
        </div>
        <div>
          <div className="eyebrow" style={{ color: "rgba(246,244,239,0.4)", marginBottom: 14 }}>Sitemap</div>
          <div style={{ display: "flex", flexDirection: "column", gap: 8 }}>
            {NAV_ITEMS.map((it) =>
          <a key={it.id} href={"#/" + it.id} onClick={(e) => {e.preventDefault();navigate(it.id);}} style={{ color: "var(--paper)", fontSize: 14 }}>
                {it.label}
              </a>
          )}
          </div>
        </div>
        <div>
          <div className="eyebrow" style={{ color: "rgba(246,244,239,0.4)", marginBottom: 14 }}>Services</div>
          <div style={{ display: "flex", flexDirection: "column", gap: 8, fontSize: 14, color: "rgba(246,244,239,0.75)" }}>
            <span>Web Development</span>
            <span>App Development</span>
            <span>Game Development</span>
            <span>Design & UX</span>
          </div>
        </div>
        <div>
          <div className="eyebrow" style={{ color: "rgba(246,244,239,0.4)", marginBottom: 14 }}>Contact</div>
          <a href="mailto:support@m2-tech.co.za" style={{ color: "var(--accent)", fontSize: 14, display: "block", marginBottom: 10 }}>support@m2-tech.co.za</a>
          <a href="https://m2-tech.co.za" target="_blank" rel="noreferrer" style={{ color: "rgba(246,244,239,0.75)", fontSize: 14 }}>m2-tech.co.za ↗</a>
        </div>
      </div>

      <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", borderTop: "1px solid rgba(246,244,239,0.15)", marginTop: 48, paddingTop: 24, fontFamily: "var(--f-mono)", fontSize: 11, letterSpacing: "0.12em", textTransform: "uppercase", color: "rgba(246,244,239,0.4)" }}>
        <span>© {new Date().getFullYear()} M2 Tech — All rights reserved</span>
        <span>Est. 2025 · Built with care</span>
      </div>
    </div>
    <style>{`
      @media (max-width: 900px) {
        .footer-grid { grid-template-columns: 1fr 1fr !important; gap: 32px !important; }
      }
      @media (max-width: 560px) {
        .footer-grid { grid-template-columns: 1fr !important; }
      }
    `}</style>
  </footer>;


Object.assign(window, { useRoute, navigate, useReveal, Nav, Footer, NAV_ITEMS });