// sections.jsx — The Pit. Sections built to exact brief spec.
const { useState, useEffect, useRef } = React;

function useReveal() {
  const ref = useRef(null);
  useEffect(() => {
    const els = ref.current ? ref.current.querySelectorAll(".reveal") : [];
    const io = new IntersectionObserver((ents) => {
      ents.forEach((e) => { if (e.isIntersecting) { e.target.classList.add("in"); io.unobserve(e.target); } });
    }, { threshold: 0.14 });
    els.forEach((el) => io.observe(el));
    return () => io.disconnect();
  });
  return ref;
}

// ---- 1. NAV ----
function Nav({ onBook }) {
  const [solid, setSolid] = useState(false);
  const [menuOpen, setMenuOpen] = useState(false);
  useEffect(() => {
    const on = () => setSolid(window.scrollY > 40);
    window.addEventListener("scroll", on); on();
    return () => window.removeEventListener("scroll", on);
  }, []);
  const links = [["CUTS", "#cuts"], ["NEXT AVAILABLE", "#next"], ["VISIT", "#visit"]];
  return (
    <header style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 70,
      transition: "background .3s ease, border-color .3s ease",
      background: (solid || menuOpen) ? "rgba(14,14,14,0.96)" : "transparent",
      backdropFilter: (solid || menuOpen) ? "blur(10px)" : "none",
      borderBottom: (solid || menuOpen) ? "1px solid var(--ash)" : "1px solid transparent",
    }}>
      <div className="wrap" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", height: 74 }}>
        <a href="#top" onClick={() => setMenuOpen(false)} style={{ textDecoration: "none", display: "flex", flexDirection: "column", lineHeight: 1 }}>
          <span className="display" style={{ fontSize: 23, letterSpacing: "-0.01em" }}>THE PIT</span>
          <span className="label" style={{ color: "var(--dim)", fontSize: 9.5, letterSpacing: "0.26em", marginTop: 4 }}>{SHOP.sub}</span>
        </a>
        <nav style={{ display: "flex", alignItems: "center", gap: 32 }}>
          <div className="nav-links" style={{ display: "flex", gap: 28 }}>
            {links.map(([t, h]) => (
              <a key={t} href={h} className="label" style={{ fontSize: 12, color: "var(--dim)", textDecoration: "none", transition: "color .15s ease" }}
                onMouseEnter={(e) => e.target.style.color = "var(--cream)"} onMouseLeave={(e) => e.target.style.color = "var(--dim)"}>{t}</a>
            ))}
          </div>
          <button className="btn btn-yellow nav-book" style={{ padding: "14px 24px", fontSize: 13 }} onClick={onBook}>BOOK <span className="arr">→</span></button>
          <button className="nav-burger" aria-label="Menu" aria-expanded={menuOpen} onClick={() => setMenuOpen((v) => !v)}
            style={{ display: "none", width: 46, height: 46, background: "transparent", border: "1.5px solid var(--ash)", color: "var(--cream)", cursor: "pointer", borderRadius: 0, flexDirection: "column", alignItems: "center", justifyContent: "center", gap: 5 }}>
            <span style={{ width: 20, height: 2, background: "var(--cream)", transition: "transform .2s ease", transform: menuOpen ? "translateY(7px) rotate(45deg)" : "none" }}></span>
            <span style={{ width: 20, height: 2, background: "var(--cream)", opacity: menuOpen ? 0 : 1, transition: "opacity .2s ease" }}></span>
            <span style={{ width: 20, height: 2, background: "var(--cream)", transition: "transform .2s ease", transform: menuOpen ? "translateY(-7px) rotate(-45deg)" : "none" }}></span>
          </button>
        </nav>
      </div>
      {/* mobile dropdown */}
      <div className="nav-drawer" style={{ display: "none", overflow: "hidden", maxHeight: menuOpen ? 320 : 0, transition: "max-height .3s ease", borderTop: menuOpen ? "1px solid var(--ash)" : "1px solid transparent" }}>
        <div className="wrap" style={{ display: "flex", flexDirection: "column", paddingTop: 8, paddingBottom: 20 }}>
          {links.map(([t, h]) => (
            <a key={t} href={h} onClick={() => setMenuOpen(false)} className="label" style={{ fontSize: 15, color: "var(--cream)", textDecoration: "none", minHeight: 52, display: "flex", alignItems: "center", borderBottom: "1px solid var(--ash)" }}>{t}</a>
          ))}
          <button className="btn btn-yellow" style={{ marginTop: 18, justifyContent: "center", minHeight: 50 }} onClick={() => { setMenuOpen(false); onBook(); }}>BOOK ON FRESHA <span className="arr">→</span></button>
        </div>
      </div>
      <style>{`
        @media (max-width: 820px){
          .nav-links{ display:none !important; }
          .nav-book{ display:none !important; }
          .nav-burger{ display:flex !important; }
          .nav-drawer{ display:block !important; }
        }
      `}</style>
    </header>
  );
}

// ---- 2. HERO ----
function Hero({ onBook, layout, showMarquee }) {
  const stacked = layout === "stacked";
  return (
    <section id="top" style={{ position: "relative", minHeight: "100vh", display: "flex", flexDirection: "column", justifyContent: "center", paddingTop: 96, paddingBottom: 64, overflow: "hidden" }}>
      {/* marquee bg — THE PIT · THE PIT · */}
      {showMarquee && (
        <div className="marq" aria-hidden="true" style={{ position: "absolute", top: "50%", left: 0, right: 0, transform: "translateY(-50%)", zIndex: 0 }}>
          <div className="marq-track">
            {[0, 1].map((k) => (
              <span key={k} className="display" style={{ fontSize: "clamp(120px, 22vw, 340px)", color: "var(--cream)", opacity: 0.045, paddingRight: "0.3em", whiteSpace: "nowrap" }}>
                THE PIT&nbsp;·&nbsp;THE PIT&nbsp;·&nbsp;THE PIT&nbsp;·&nbsp;
              </span>
            ))}
          </div>
        </div>
      )}
      <div className="wrap" style={{ position: "relative", zIndex: 2, width: "100%" }}>
        <div className="hero-grid" style={{
          display: "grid",
          gridTemplateColumns: stacked ? "1fr" : "minmax(0,1.12fr) minmax(0,0.88fr)",
          gap: stacked ? 0 : "clamp(40px, 4vw, 56px)", alignItems: "center",
          textAlign: stacked ? "center" : "left",
          justifyItems: stacked ? "center" : "stretch",
        }}>
          <div style={{ minWidth: 0 }}>
            <div className="eyebrow" style={{ marginBottom: 28 }}>{SHOP.eyebrow}</div>
            <h1 className="display" style={{ fontSize: "clamp(48px, 8vw, 112px)", margin: 0 }}>
              <span style={{ display: "block" }}>BOOKED.</span>
              <span style={{ display: "block", color: "var(--yellow)" }}>WALK-IN<br />BACKUP.</span>
            </h1>
            <p style={{ maxWidth: "62ch", margin: stacked ? "30px auto 0" : "30px 0 0", fontSize: 19, color: "var(--dim)", lineHeight: 1.6 }}>{LEDE}</p>
            <div style={{ display: "flex", gap: 14, marginTop: 40, flexWrap: "wrap", justifyContent: stacked ? "center" : "flex-start" }}>
              <button className="btn btn-yellow" onClick={onBook}>BOOK ON FRESHA <span className="arr">→</span></button>
              <a className="btn btn-ghost" href={"tel:" + SHOP.phoneTel}>CALL {SHOP.phone}</a>
            </div>
          </div>
          {!stacked && (
            <div style={{ position: "relative" }}>
              <image-slot
                id="hero-clipper"
                shape="rect"
                src="https://images.pexels.com/photos/1570806/pexels-photo-1570806.jpeg?auto=compress&cs=tinysrgb&w=1600"
                placeholder="clipper close-up on a tight fade · black bg · no face"
                style={{ width: "100%", height: "clamp(460px, 62vh, 640px)", border: "1px solid var(--ash)" }}
              ></image-slot>
              <div className="label" style={{ position: "absolute", top: 16, left: 16, background: "var(--yellow)", color: "#000", fontSize: 11, fontWeight: 700, padding: "8px 13px", letterSpacing: "0.14em" }}>NO. 3168 OAK</div>
            </div>
          )}
        </div>
      </div>
      <div className="scroll-hint label" style={{ position: "absolute", bottom: 26, left: "50%", transform: "translateX(-50%)", fontSize: 10, color: "var(--faint)", letterSpacing: "0.26em", zIndex: 3 }}>SCROLL</div>
    </section>
  );
}

// ---- 3. THE CUTS ----
function Cuts({ onBook }) {
  const ref = useReveal();
  return (
    <section id="cuts" ref={ref} style={{ padding: "clamp(80px,11vw,150px) 0", borderTop: "1px solid var(--ash)" }}>
      <div className="wrap">
        <div style={{ display: "flex", alignItems: "flex-end", justifyContent: "space-between", gap: 30, flexWrap: "wrap", marginBottom: 52 }}>
          <div className="reveal">
            <div className="eyebrow" style={{ marginBottom: 18 }}>THE CUTS</div>
            <h2 className="display" style={{ fontSize: "clamp(40px,6.5vw,90px)" }}>WHAT YOU<br />SIT FOR</h2>
          </div>
          <p className="reveal" style={{ maxWidth: 320, color: "var(--dim)", fontSize: 16 }}>Clipper cut, taper fade, beard work. No menus of upsells — just the cut, done right.</p>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(260px, 1fr))", gap: 0, border: "1px solid var(--yellow)" }}>
          {CUTS.map((c, i) => (
            <button key={c.no} className="reveal" onClick={onBook} style={{
              textAlign: "left", background: "#000", color: "var(--cream)", border: "none",
              borderRight: i < CUTS.length - 1 ? "1px solid var(--yellow)" : "none",
              padding: "clamp(28px,3.5vw,44px)", cursor: "pointer", transitionDelay: (i * 60) + "ms",
              display: "flex", flexDirection: "column", gap: 0, minHeight: 320, fontFamily: "var(--font-body)",
              transition: "background .18s ease",
            }}
            onMouseEnter={(e) => e.currentTarget.style.background = "#141414"}
            onMouseLeave={(e) => e.currentTarget.style.background = "#000"}>
              <span className="display" style={{ color: "var(--yellow)", fontSize: 18, letterSpacing: 0 }}>{c.no}</span>
              <span className="display" style={{ fontSize: "clamp(30px,3.6vw,44px)", marginTop: 20 }}>{c.name}</span>
              <span style={{ color: "var(--dim)", fontSize: 16, marginTop: 14, flex: 1 }}>{c.desc}</span>
              <span style={{ display: "flex", alignItems: "baseline", justifyContent: "space-between", marginTop: 28, borderTop: "1px solid var(--ash)", paddingTop: 18 }}>
                <span className="display" style={{ color: "var(--yellow)", fontSize: "clamp(26px,3vw,36px)" }}>{c.price}</span>
                <span className="label" style={{ color: "var(--faint)", fontSize: 11 }}>BOOK →</span>
              </span>
            </button>
          ))}
        </div>
      </div>
    </section>
  );
}

// ---- 4. THE NUMBERS ----
function Numbers() {
  const ref = useReveal();
  return (
    <section ref={ref} style={{ borderTop: "1px solid var(--ash)", borderBottom: "1px solid var(--ash)", background: "#0a0a0a" }}>
      <div className="wrap" style={{ padding: "clamp(40px,5vw,64px) 0" }}>
        <div className="reveal" style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(150px,1fr))", gap: 24 }}>
          {NUMBERS.map((n, i) => (
            <div key={i} style={{ textAlign: "center", borderRight: (i < NUMBERS.length - 1) ? "1px solid var(--ash)" : "none", padding: "0 12px" }}>
              <div className="display" style={{ fontSize: "clamp(38px,5vw,68px)", color: "var(--cream)" }}>{n.big}</div>
              <div className="label" style={{ color: "var(--yellow)", fontSize: 11.5, marginTop: 10, letterSpacing: "0.18em" }}>{n.small}</div>
            </div>
          ))}
        </div>
        <div className="reveal label" style={{ textAlign: "center", color: "var(--dim)", fontSize: 12, marginTop: 32, letterSpacing: "0.16em", lineHeight: 1.8 }}>
          BARBERSHOP · 3168 OAK ST · VANCOUVER · WALK-INS WELCOME · BOOK AHEAD
        </div>
      </div>
    </section>
  );
}

// ---- 5. NEXT AVAILABLE ----
function NextAvailable({ onBook }) {
  const ref = useReveal();
  return (
    <section id="next" ref={ref} style={{ padding: "clamp(80px,11vw,150px) 0" }}>
      <div className="wrap">
        <div className="reveal" style={{ background: "var(--yellow)", color: "#000", padding: "clamp(40px,6vw,80px)", display: "grid", gridTemplateColumns: "repeat(auto-fit,minmax(280px,1fr))", gap: 40, alignItems: "center" }}>
          <div>
            <div className="label" style={{ letterSpacing: "0.2em", fontSize: 12, marginBottom: 22 }}>NEXT AVAILABLE</div>
            <h2 className="display" style={{ fontSize: "clamp(44px,6vw,92px)", color: "#000" }}>NEXT CHAIR<br />FREE.</h2>
          </div>
          <div>
            <p style={{ fontSize: 19, lineHeight: 1.6, color: "#000", maxWidth: 420 }}>Booking through Fresha. If we don't have a slot today, tomorrow's better. Or call — sometimes a chair opens up.</p>
            <div style={{ display: "flex", gap: 13, marginTop: 30, flexWrap: "wrap" }}>
              <button className="btn btn-black" onClick={onBook}>VIEW NEXT AVAILABLE <span className="arr">→</span></button>
              <a className="btn" href={"tel:" + SHOP.phoneTel} style={{ background: "transparent", color: "#000", border: "1.5px solid #000" }}>CALL {SHOP.phone}</a>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ---- 6. VISIT ----
function Visit({ onBook }) {
  const ref = useReveal();
  return (
    <section id="visit" ref={ref} style={{ padding: "clamp(80px,11vw,150px) 0", borderTop: "1px solid var(--ash)" }}>
      <div className="wrap">
        <div style={{ background: "#000", border: "1px solid var(--ash)", display: "grid", gridTemplateColumns: "repeat(auto-fit,minmax(300px,1fr))" }}>
          <div className="reveal" style={{ padding: "clamp(36px,5vw,64px)" }}>
            <div className="eyebrow" style={{ marginBottom: 22 }}>VISIT</div>
            <h2 className="display" style={{ fontSize: "clamp(36px,4.6vw,72px)", marginBottom: 14 }}>{SHOP.addrLine1}<br /><span style={{ color: "var(--yellow)" }}>{SHOP.addrLine2}.</span></h2>
            <div style={{ color: "var(--dim)", fontSize: 15, marginBottom: 28, fontFamily: "var(--font-mono)" }}>{SHOP.addr}</div>
            <a href={"tel:" + SHOP.phoneTel} className="display" style={{ fontSize: "clamp(28px,3.4vw,42px)", color: "var(--cream)", textDecoration: "none", display: "inline-block" }}>{SHOP.phone}</a>

            <table style={{ width: "100%", marginTop: 34, borderCollapse: "collapse", fontFamily: "var(--font-mono)" }}>
              <tbody>
                {HOURS.map((h) => (
                  <tr key={h.d} style={{ borderBottom: "1px solid var(--ash)" }}>
                    <td style={{ padding: "12px 0", textTransform: "uppercase", letterSpacing: "0.14em", fontSize: 13, color: h.closed ? "var(--faint)" : "var(--cream)" }}>{h.d}</td>
                    <td style={{ padding: "12px 0", textAlign: "right", color: h.closed ? "var(--faint)" : "var(--dim)", fontSize: 14, letterSpacing: "0.06em" }}>{h.h}</td>
                  </tr>
                ))}
              </tbody>
            </table>
            <div className="label" style={{ color: "var(--faint)", fontSize: 10.5, marginTop: 14, letterSpacing: "0.14em" }}>HOURS TO CONFIRM WITH SHOP</div>
            <button className="btn btn-yellow" style={{ marginTop: 30 }} onClick={onBook}>BOOK ON FRESHA <span className="arr">→</span></button>
          </div>
          <div className="reveal" style={{ transitionDelay: "100ms", borderLeft: "1px solid var(--ash)", minHeight: 380, position: "relative" }}>
            <iframe
              title="Map — 3168 Oak St, Vancouver"
              src="https://maps.google.com/maps?q=3168+Oak+St+Vancouver+BC&output=embed&z=15"
              loading="lazy"
              allowFullScreen
              referrerPolicy="no-referrer-when-downgrade"
              style={{ display: "block", width: "100%", height: "100%", minHeight: 380, border: 0, filter: "invert(0.92) hue-rotate(180deg) brightness(0.92) contrast(0.95) grayscale(0.25)" }}
            ></iframe>
            <div className="label" style={{ position: "absolute", top: 16, left: 16, background: "var(--yellow)", color: "#000", fontSize: 11, fontWeight: 700, padding: "8px 13px", letterSpacing: "0.14em", pointerEvents: "none" }}>3168 OAK ST</div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ---- 7. FOOTER ----
function Footer({ onBook }) {
  return (
    <footer style={{ borderTop: "1px solid var(--ash)", background: "#0a0a0a" }}>
      <div className="marq" aria-hidden="true" style={{ padding: "30px 0", borderBottom: "1px solid var(--ash)" }}>
        <div className="ticker-track">
          {[0, 1].map((k) => (
            <span key={k} style={{ display: "inline-flex", alignItems: "center" }}>
              {["CLIPPER CUT", "TAPER FADE", "BEARD TRIM", "WALK-INS WELCOME", "BOOK ON FRESHA"].map((t) => (
                <span key={t} style={{ display: "inline-flex", alignItems: "center" }}>
                  <span className="display" style={{ fontSize: 26, color: "var(--cream)", padding: "0 24px" }}>{t}</span>
                  <span style={{ width: 8, height: 8, background: "var(--yellow)" }}></span>
                </span>
              ))}
            </span>
          ))}
        </div>
      </div>
      <div className="wrap" style={{ padding: "clamp(56px,8vw,100px) 0 40px" }}>
        <div style={{ textAlign: "center", marginBottom: 56 }}>
          <h2 className="display" style={{ fontSize: "clamp(44px,9vw,128px)", lineHeight: 0.9 }}>
            GET IN<br /><span style={{ color: "var(--yellow)" }}>THE BOOK.</span>
          </h2>
          <div style={{ marginTop: 34 }}>
            <button className="btn btn-yellow" style={{ padding: "20px 40px", fontSize: 15 }} onClick={onBook}>BOOK ON FRESHA <span className="arr">→</span></button>
          </div>
        </div>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", borderTop: "1px solid var(--ash)", paddingTop: 28, flexWrap: "wrap", gap: 18 }}>
          <div>
            <span className="display" style={{ fontSize: 22 }}>THE PIT</span>
            <div className="label" style={{ color: "var(--faint)", fontSize: 11, marginTop: 8, letterSpacing: "0.14em" }}>{SHOP.addr}</div>
          </div>
          <div style={{ display: "flex", gap: 22 }}>
            <a href={SHOP.fresha} target="_blank" className="label" style={{ color: "var(--dim)", textDecoration: "none", fontSize: 12 }}>FRESHA</a>
            <a href={"tel:" + SHOP.phoneTel} className="label" style={{ color: "var(--dim)", textDecoration: "none", fontSize: 12 }}>CALL</a>
          </div>
        </div>
        <div className="label" style={{ color: "var(--faint)", fontSize: 10.5, marginTop: 26, letterSpacing: "0.12em" }}>
          © THE PIT BARBERSHOP · OAK ST, VANCOUVER BC · CONCEPT · X9 LAB MEDIA · 2026
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { useReveal, Nav, Hero, Cuts, Numbers, NextAvailable, Visit, Footer });
