/* Shared intro block — ONLY uses copy from CONTENT.md */
function HeroCopy({ navigate, dense, dark }) {
  const muted = dark ? "rgba(239,235,227,0.55)" : "var(--muted)";
  return (
    <div style={{
      maxWidth: 820,
      marginTop: dense ? 40 : 72,
      fontFamily: "var(--display)",
      fontStyle: "italic",
      fontWeight: 300,
      fontSize: "clamp(22px, 2.2vw, 32px)",
      lineHeight: 1.3,
      letterSpacing: "-0.01em"
    }}>
      <p style={{marginBottom: 22}}>
        A full service creative agency. <span style={{color: muted}}>We handle social strategy, content production, influencer marketing, and represent the creative talent that brings it to life.</span>
      </p>
      <p style={{color: muted, fontSize: "clamp(16px, 1.4vw, 19px)", lineHeight: 1.55, fontFamily: "var(--sans)", fontStyle:"normal"}}>
        Our approach combines real human creativity and strategy with the latest in AI and data analytics, so every decision is as informed as it is inspired.
      </p>
      <div style={{marginTop: 56, display: "flex", gap: 40, flexWrap: "wrap", alignItems: "baseline"}}>
        <button className="link-action" onClick={()=>navigate("contact")} data-cursor="hover" data-cursor-label="CONTACT" style={{color: dark ? "#EFEBE3":"var(--fg)"}}>
          Contact Us <span className="glyph">↗</span>
        </button>
        <button className="link-action small" onClick={()=>navigate("social")} data-cursor="hover" data-cursor-label="SERVICES" style={{color: dark ? "rgba(239,235,227,0.7)":"var(--muted)"}}>
          Our Services <span className="glyph">→</span>
        </button>
      </div>
    </div>
  );
}

/* ---------- Variant: Showreel — cycling real videos ---------- */
function HeroReel({ navigate }) {
  const videos = window.VIDEOS || [];
  const [i, setI] = React.useState(0);
  const next = () => setI(n => (n + 1) % videos.length);
  const prev = () => setI(n => (n - 1 + videos.length) % videos.length);

  React.useEffect(() => {
    const id = setInterval(next, 6000);
    return () => clearInterval(id);
  }, [videos.length]);

  return (
    <section style={{minHeight: "100vh", padding: "120px var(--pad-x) 80px", position: "relative"}}>
      <div style={{display:"flex", justifyContent:"space-between", marginBottom: 24}}>
        <div className="mono" style={{color:"var(--muted)"}}>— Reel {String(i+1).padStart(2,"0")} ⁄ {String(videos.length).padStart(2,"0")}</div>
        <div className="mono" style={{color:"var(--muted)"}}><span style={{color:"var(--accent)"}}>●</span> Live</div>
      </div>

      <div style={{position: "relative", width: "100%", height: "min(72vh, 720px)", background: "#0a0a0a", overflow: "hidden"}} data-cursor="hover" data-cursor-label="NEXT" onClick={next}>
        <video
          key={videos[i]} src={videos[i]}
          autoPlay muted loop playsInline preload="auto"
          style={{position:"absolute", inset: 0, width:"100%", height:"100%", objectFit:"cover", animation: "reelFade 800ms var(--ease-out)"}}
        />
        <div style={{position:"absolute", inset: 0, background:"linear-gradient(180deg, rgba(0,0,0,0.35) 0%, transparent 30%, transparent 55%, rgba(0,0,0,0.6) 100%)", pointerEvents:"none"}}/>
        <div style={{position: "absolute", inset: 0, display: "flex", flexDirection: "column", justifyContent: "space-between", padding: "28px 36px", color: "#EFEBE3", pointerEvents: "none"}}>
          <div style={{display:"flex", justifyContent:"space-between"}}>
            <div className="mono" style={{opacity:0.75}}>212 ⁄ Reel</div>
            <div className="mono" style={{opacity:0.75}}>NYC · 2026</div>
          </div>
        </div>
        <div style={{position:"absolute", left: 0, right: 0, bottom: 0, height: 2, display:"flex", gap: 2, pointerEvents: "none"}}>
          {videos.map((_,k) => (
            <div key={k} style={{flex: 1, background: k === i ? "var(--accent)" : "rgba(239,235,227,0.18)", transition: "background 300ms var(--ease)"}}/>
          ))}
        </div>
      </div>

      <div style={{display:"flex", justifyContent:"space-between", alignItems:"center", marginTop: 14, fontFamily:"var(--mono)", fontSize: 10, letterSpacing:"0.2em", textTransform:"uppercase", color:"var(--muted)"}}>
        <button onClick={prev} data-cursor="hover" style={{color:"inherit"}}>← Prev</button>
        <span>{String(i+1).padStart(2,"0")} / {String(videos.length).padStart(2,"0")}</span>
        <button onClick={next} data-cursor="hover" style={{color:"inherit"}}>Next →</button>
      </div>

      <HeroCopy navigate={navigate} />
      <style>{`@keyframes reelFade { 0%{opacity:0; transform:scale(1.04);} 100%{opacity:1; transform:scale(1);} }`}</style>
    </section>
  );
}

/* ---------- Variant 3: Editorial ---------- */
function HeroEditorial({ navigate }) {
  return (
    <section style={{minHeight: "100vh", padding: "140px var(--pad-x) 80px", display: "grid", gridTemplateColumns: "1fr 2fr", gap: 48, alignItems: "start"}}>
      <div style={{position:"sticky", top: 140}}>
        <div className="eyebrow" style={{marginBottom: 20}}>— 212 / NYC / 2026</div>
        <div className="mono" style={{color:"var(--muted)", lineHeight: 1.9}}>
          A full service<br/>creative agency.<br/><br/>New York City.<br/>Working globally.
        </div>
      </div>

      <div>
        <h1 style={{
          fontFamily:"var(--display)",
          fontSize:"clamp(64px, 13vw, 220px)",
          lineHeight: 0.88,
          letterSpacing:"-0.03em",
          marginBottom: 40,
          fontWeight: 300
        }}>
          Social<br/>
          <em style={{fontStyle:"italic"}}>strategy.</em><br/>
          Content.<br/>
          <em style={{fontStyle:"italic"}}>Influence.</em><br/>
          <span style={{WebkitTextStroke: "1px var(--fg)", color: "transparent"}}>Talent.</span>
        </h1>
        <HeroCopy navigate={navigate} dense />
      </div>
    </section>
  );
}

/* ---------- Variant 4: Grid assembly ---------- */
function AssemblyTile({ vsrc, label, assembled, tx, transition, mountDelay }) {
  const wrapRef = React.useRef(null);
  const videoRef = React.useRef(null);

  // Create video via DOM API (bypasses React muted bug) with staggered load.
  React.useEffect(() => {
    const wrap = wrapRef.current;
    if (!wrap || !vsrc || !assembled) return;
    let video = null;
    let disposed = false;
    let staggerTimer = null;

    const io = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting && !video && !disposed) {
        io.disconnect();
        staggerTimer = setTimeout(() => {
          if (disposed) return;
          video = _createVideo(wrap, vsrc);
          videoRef.current = video;
          // Play/pause on visibility.
          const vis = new IntersectionObserver((ents) => {
            ents.forEach(e => {
              if (disposed) return;
              if (e.isIntersecting) {
                if (_activeVideos.size < _maxConcurrent || _activeVideos.has(video)) {
                  _activeVideos.add(video);
                  video.muted = true;
                  video.play().catch(() => {});
                }
              } else {
                video.pause();
                _activeVideos.delete(video);
              }
            });
          }, { threshold: 0.1 });
          vis.observe(video);
          video.addEventListener("canplay", function() {
            if (!disposed && video.paused) { video.muted = true; video.play().catch(() => {}); }
          });
        }, mountDelay || 0);
      }
    }, { rootMargin: "200px" });
    io.observe(wrap);

    return () => {
      disposed = true;
      io.disconnect();
      if (staggerTimer) clearTimeout(staggerTimer);
      if (video) {
        video.pause();
        _activeVideos.delete(video);
        video.removeAttribute("src");
        video.load();
        video.remove();
        videoRef.current = null;
      }
    };
  }, [vsrc, assembled, mountDelay]);

  return (
    <div ref={wrapRef} data-cursor="hover" onClick={() => {
      const v = videoRef.current;
      if (v && v.paused) { v.muted = true; v.play().catch(()=>{}); }
    }} style={{
      background: "#111", position:"relative", overflow:"hidden",
      transform: tx, opacity: assembled ? 1 : 0,
      transition
    }}>
      <div className="mono" style={{position:"absolute", bottom: 10, left: 10, right: 10, color:"rgba(239,235,227,0.9)", fontSize: 9, textShadow:"0 1px 6px rgba(0,0,0,0.7)", zIndex: 3, pointerEvents:"none"}}>
        {label}
      </div>
    </div>
  );
}

function HeroAssembly({ navigate }) {
  const [assembled, setAssembled] = React.useState(false);
  React.useEffect(() => {
    const t = setTimeout(() => setAssembled(true), 400);
    return () => clearTimeout(t);
  }, []);

  const videos = window.VIDEOS || [];
  const labels = window.VIDEO_LABELS || [];
  const tiles = Array.from({length: 12});
  return (
    <section style={{minHeight:"100vh", padding:"84px var(--pad-x) 44px", position:"relative"}}>
      <div style={{display:"flex", justifyContent:"flex-start", marginBottom: 14}}>
        <div className="mono" style={{color:"var(--muted)"}}>— New York City based, working globally</div>
      </div>

      <div className="assembly-grid" style={{
        display:"grid", gridTemplateColumns: "repeat(4, 1fr)", gridTemplateRows: "repeat(3, 1fr)",
        gap: 6, aspectRatio: "16 / 9", marginBottom: 28, position:"relative"
      }}>
        {tiles.map((_,i) => {
          const row = Math.floor(i/4), col = i%4;
          const dx = (col - 1.5) * 400;
          const dy = (row - 1) * 300;
          const rot = (i % 2 ? 1 : -1) * (12 + i);
          const tx = assembled ? "translate(0,0) rotate(0)" : `translate(${dx}px, ${dy}px) rotate(${rot}deg)`;
          return (
            <AssemblyTile
              key={i}
              vsrc={videos[i]}
              label={labels[i]}
              assembled={assembled}
              tx={tx}
              transition={`transform 1400ms cubic-bezier(0.16, 1, 0.3, 1) ${i*60}ms, opacity 600ms ${i*60}ms`}
              mountDelay={i * 300}
            />
          );
        })}
        <div className="assembly-overlay" style={{
          position:"absolute", inset:0, display:"flex", alignItems:"center", justifyContent:"center",
          fontFamily:'"EB Garamond", Georgia, serif', fontStyle:"italic", fontSize: "min(28vw, 400px)",
          color:"var(--paper)", mixBlendMode:"difference", pointerEvents:"none",
          letterSpacing:"-0.04em", fontWeight: 400,
          opacity: assembled ? 1 : 0, transition: "opacity 700ms 1200ms"
        }}>212</div>
      </div>

      <HeroCopy navigate={navigate} dense />
    </section>
  );
}

/* ---------- Variant 5: Manifesto — uses ONLY doc language ---------- */
function HeroManifesto({ navigate }) {
  const [scrolled, setScrolled] = React.useState(0);
  React.useEffect(() => {
    let pending = false;
    const onScroll = () => {
      if (pending) return;
      pending = true;
      requestAnimationFrame(() => {
        setScrolled(window.scrollY);
        pending = false;
      });
    };
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const manifesto = "A full service creative agency. We handle social strategy, content production, influencer marketing, and represent the creative talent that brings it to life.";
  const words = manifesto.split(" ");

  return (
    <section style={{padding:"120px var(--pad-x) 80px", minHeight:"140vh", position:"relative"}}>
      <div style={{display:"flex", justifyContent:"space-between", marginBottom: 80}}>
        <div className="eyebrow">— New York City · Working globally</div>
        <div className="mono" style={{color:"var(--muted)"}}>↓ Scroll</div>
      </div>
      <div style={{
        fontFamily:"var(--display)",
        fontSize:"clamp(40px, 6vw, 96px)",
        lineHeight: 1.1,
        letterSpacing:"-0.025em",
        maxWidth: 1500,
        fontWeight: 300
      }}>
        {words.map((w,i) => {
          const threshold = i * 24;
          const active = scrolled > threshold;
          return (
            <span key={i} style={{
              opacity: active ? 1 : 0.14,
              transition: "opacity 500ms var(--ease)",
              marginRight: 14, display: "inline-block",
              fontStyle: (i % 6 === 2) ? "italic" : "normal"
            }}>{w}</span>
          );
        })}
      </div>
      <div style={{marginTop: 80}}>
        <HeroCopy navigate={navigate} dense />
      </div>
    </section>
  );
}

/* ---------- Variant 6: NYC Live ---------- */
function HeroNYC({ navigate }) {
  const [now, setNow] = React.useState(new Date());
  React.useEffect(() => {
    const id = setInterval(() => setNow(new Date()), 1000);
    return () => clearInterval(id);
  }, []);
  const nyTime = now.toLocaleTimeString("en-US", { timeZone: "America/New_York", hour12: false });

  return (
    <section style={{minHeight:"100vh", padding:"120px var(--pad-x) 80px", display:"grid", gridTemplateRows:"auto 1fr auto", gap: 40}}>
      <div style={{display:"flex", justifyContent:"space-between"}}>
        <div className="mono" style={{color:"var(--muted)"}}>— Manhattan</div>
        <div className="mono" style={{color:"var(--muted)"}}>40.7580° N · 73.9855° W</div>
      </div>

      <div style={{display:"flex", flexDirection:"column", justifyContent:"center"}}>
        <div className="eyebrow" style={{marginBottom: 20}}>— Right now</div>
        <div style={{fontFamily:"var(--mono)", fontSize:"clamp(80px, 18vw, 280px)", lineHeight: 0.9, letterSpacing:"-0.04em", fontWeight: 400}}>
          {nyTime}
        </div>
        <div style={{
          fontFamily:"var(--display)", fontStyle:"italic",
          fontSize:"clamp(32px, 5vw, 72px)", lineHeight: 1.1, letterSpacing:"-0.02em", marginTop: 24, maxWidth: 1400, fontWeight: 300
        }}>
          New York City based. <span style={{color:"var(--muted)"}}>Working globally.</span>
        </div>
      </div>

      <HeroCopy navigate={navigate} dense />
    </section>
  );
}

Object.assign(window, { HeroReel, HeroEditorial, HeroAssembly, HeroManifesto, HeroNYC });
