// Cook Street Sourcing v2 — Sections 1: Hero, StatsBar, About

function useReveal(threshold = 0.1) {
  const ref = React.useRef(null);
  React.useEffect(() => {
    const el = ref.current; if (!el) return;
    const obs = new IntersectionObserver((entries) => {
      if (entries[0].isIntersecting) {
        el.querySelectorAll(".fu").forEach((c, i) => {
          setTimeout(() => { c.style.opacity="1"; c.style.transform="translateY(0)"; }, i*120);
        });
        obs.disconnect();
      }
    }, { threshold });
    obs.observe(el);
    return () => obs.disconnect();
  }, []);
  return ref;
}
const fu = { opacity:0, transform:"translateY(24px)", transition:"opacity 0.7s ease, transform 0.7s ease" };

// ─── Hero: Bleed Split ───────────────────────────────────────────────────────
function HeroBleed({ t, in_, anim }) {
  const h = t.hero;
  return (
    <section style={{ position:"relative", minHeight:"100svh", backgroundColor:"#1E1B18", overflow:"hidden" }}>
      <div className="hero-bleed-grid">
        {/* Left text */}
        <div style={{
          display:"flex", flexDirection:"column", justifyContent:"center",
          paddingTop:130, paddingBottom:100,
          paddingLeft:"max(40px, calc((100vw - 1280px)/2 + 56px))",
          paddingRight:48, position:"relative", zIndex:2
        }}>
          <div style={{ display:"flex", alignItems:"center", gap:12, marginBottom:52, ...anim(150) }}>
            <div style={{ width:40, height:1, backgroundColor:"#9B7B52" }} />
            <span style={{ fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:600, letterSpacing:"0.2em", textTransform:"uppercase", color:"#9B7B52" }}>{h.label}</span>
          </div>
          <h1 style={{ fontFamily:"'Playfair Display',serif", fontWeight:400, marginBottom:52, ...anim(250) }}>
            <span style={{ display:"block", fontSize:"clamp(4rem,7vw,7.5rem)", fontStyle:"italic", color:"rgba(255,255,255,0.25)", lineHeight:0.88, letterSpacing:"-0.02em" }}>{h.line1}</span>
            <span style={{ display:"block", fontSize:"clamp(4rem,7vw,7.5rem)", fontStyle:"italic", color:"#FAFAF7", lineHeight:0.88, letterSpacing:"-0.02em" }}>{h.line2}</span>
            <span style={{ display:"block", fontSize:"clamp(4rem,7vw,7.5rem)", fontStyle:"normal", color:"#9B7B52", lineHeight:0.88, letterSpacing:"-0.02em" }}>{h.line3}</span>
          </h1>
          <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:16, fontWeight:300, color:"rgba(255,255,255,0.72)", lineHeight:1.9, maxWidth:380, marginBottom:60, ...anim(400) }}>{h.sub}</p>
          <div style={{ display:"flex", flexWrap:"wrap", gap:14, ...anim(550) }}>
            <a href="#contact" style={{ display:"inline-flex", alignItems:"center", padding:"14px 36px", backgroundColor:"#4A5240", color:"#fff", fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:500, letterSpacing:"0.14em", textTransform:"uppercase", textDecoration:"none", transition:"background 0.3s" }}
              onMouseEnter={e=>e.currentTarget.style.backgroundColor="#5a6450"} onMouseLeave={e=>e.currentTarget.style.backgroundColor="#4A5240"}>{h.cta1}</a>
            <a href="#services" style={{ display:"inline-flex", alignItems:"center", padding:"14px 36px", border:"1px solid rgba(255,255,255,0.18)", color:"rgba(255,255,255,0.65)", fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:400, letterSpacing:"0.14em", textTransform:"uppercase", textDecoration:"none", transition:"all 0.3s" }}
              onMouseEnter={e=>{e.currentTarget.style.borderColor="rgba(255,255,255,0.5)";e.currentTarget.style.color="#fff";}}
              onMouseLeave={e=>{e.currentTarget.style.borderColor="rgba(255,255,255,0.18)";e.currentTarget.style.color="rgba(255,255,255,0.65)";}}>{h.cta2}</a>
          </div>
        </div>
        {/* Right image — bleeds to viewport edge */}
        <div className="hero-bleed-img" style={{ overflow:"hidden", position:"relative", minHeight:320 }}>
          <img src="hero.jpg" alt="Premium leather handbag"
            style={{ width:"100%", height:"100%", objectFit:"cover", objectPosition:"center", display:"block", transition:"transform 1.2s ease" }}
            onMouseEnter={e=>e.currentTarget.style.transform="scale(1.04)"}
            onMouseLeave={e=>e.currentTarget.style.transform="scale(1)"} />
          <div style={{ position:"absolute", inset:0, background:"linear-gradient(to right, #1E1B18 0%, transparent 20%)", pointerEvents:"none" }} />
        </div>
      </div>
      {/* Scroll indicator */}
      <div style={{ position:"absolute", bottom:32, left:"50%", transform:"translateX(-50%)", display:"flex", flexDirection:"column", alignItems:"center", opacity:in_?0.4:0, transition:"opacity 1s 1.5s" }}>
        <div style={{ width:1, height:48, backgroundColor:"rgba(255,255,255,0.25)", position:"relative", overflow:"hidden" }}>
          <div style={{ position:"absolute", top:0, width:"100%", height:"45%", backgroundColor:"rgba(255,255,255,0.6)", animation:"scrollLine 2.2s ease-in-out infinite" }} />
        </div>
      </div>
    </section>
  );
}

// ─── Hero: Full Photo Overlay ───────────────────────────────────────────────
function HeroOverlay({ t, in_, anim }) {
  const h = t.hero;
  return (
    <section style={{ position:"relative", minHeight:"100svh", overflow:"hidden", display:"flex", alignItems:"center" }}>
      <img src="hero.jpg" alt="" style={{ position:"absolute", inset:0, width:"100%", height:"100%", objectFit:"cover", objectPosition:"center top", transform:in_?"scale(1)":"scale(1.06)", transition:"transform 2.5s ease" }} />
      <div style={{ position:"absolute", inset:0, background:"linear-gradient(105deg, rgba(18,15,12,0.95) 0%, rgba(18,15,12,0.78) 50%, rgba(18,15,12,0.25) 100%)" }} />
      <div className="cs-container" style={{ position:"relative", zIndex:2, paddingTop:130, paddingBottom:100, width:"100%" }}>
        <div style={{ maxWidth:580 }}>
          <div style={{ display:"flex", alignItems:"center", gap:12, marginBottom:52, ...anim(150) }}>
            <div style={{ width:40, height:1, backgroundColor:"#9B7B52" }} />
            <span style={{ fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:600, letterSpacing:"0.2em", textTransform:"uppercase", color:"#9B7B52" }}>{h.label}</span>
          </div>
          <h1 style={{ fontFamily:"'Playfair Display',serif", fontWeight:400, marginBottom:52, ...anim(250) }}>
            <span style={{ display:"block", fontSize:"clamp(4.5rem,9vw,9rem)", fontStyle:"italic", color:"rgba(255,255,255,0.22)", lineHeight:0.86, letterSpacing:"-0.025em" }}>{h.line1}</span>
            <span style={{ display:"block", fontSize:"clamp(4.5rem,9vw,9rem)", fontStyle:"italic", color:"#FAFAF7", lineHeight:0.86, letterSpacing:"-0.025em" }}>{h.line2}</span>
            <span style={{ display:"block", fontSize:"clamp(4.5rem,9vw,9rem)", fontStyle:"normal", color:"#9B7B52", lineHeight:0.86, letterSpacing:"-0.025em" }}>{h.line3}</span>
          </h1>
          <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:16, fontWeight:300, color:"rgba(255,255,255,0.72)", lineHeight:1.9, maxWidth:420, marginBottom:60, ...anim(400) }}>{h.sub}</p>
          <div style={{ display:"flex", flexWrap:"wrap", gap:14, ...anim(550) }}>
            <a href="#contact" style={{ display:"inline-flex", alignItems:"center", padding:"14px 36px", backgroundColor:"#4A5240", color:"#fff", fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:500, letterSpacing:"0.14em", textTransform:"uppercase", textDecoration:"none", transition:"background 0.3s" }}
              onMouseEnter={e=>e.currentTarget.style.backgroundColor="#5a6450"} onMouseLeave={e=>e.currentTarget.style.backgroundColor="#4A5240"}>{h.cta1}</a>
            <a href="#services" style={{ display:"inline-flex", alignItems:"center", padding:"14px 36px", border:"1px solid rgba(255,255,255,0.22)", color:"rgba(255,255,255,0.65)", fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:400, letterSpacing:"0.14em", textTransform:"uppercase", textDecoration:"none", transition:"all 0.3s" }}
              onMouseEnter={e=>{e.currentTarget.style.borderColor="rgba(255,255,255,0.5)";e.currentTarget.style.color="#fff";}}
              onMouseLeave={e=>{e.currentTarget.style.borderColor="rgba(255,255,255,0.22)";e.currentTarget.style.color="rgba(255,255,255,0.65)";}}>{h.cta2}</a>
          </div>
        </div>
      </div>
    </section>
  );
}

// ─── Hero: Typographic (Giant Type) ─────────────────────────────────────────
function HeroTypographic({ t, in_, anim }) {
  const h = t.hero;
  return (
    <section style={{ position:"relative", minHeight:"100svh", backgroundColor:"#1E1B18", overflow:"hidden", display:"flex", alignItems:"center" }}>
      {/* Grid texture */}
      <div style={{ position:"absolute", inset:0, backgroundImage:"linear-gradient(rgba(255,255,255,0.018) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.018) 1px,transparent 1px)", backgroundSize:"110px 110px", pointerEvents:"none" }} />
      {/* Accent image — right edge strip */}
      <div style={{ position:"absolute", right:0, top:0, bottom:0, width:"22%", overflow:"hidden", opacity:in_?1:0, transition:"opacity 1.4s ease 0.6s" }}>
        <img src="hero.jpg" alt="" style={{ width:"100%", height:"100%", objectFit:"cover", objectPosition:"left center" }} />
        <div style={{ position:"absolute", inset:0, background:"linear-gradient(to right, #1E1B18 0%, transparent 60%)" }} />
        <div style={{ position:"absolute", inset:0, background:"linear-gradient(to bottom, rgba(30,27,24,0.4) 0%, transparent 30%, rgba(30,27,24,0.4) 100%)" }} />
      </div>
      <div className="cs-container" style={{ position:"relative", zIndex:2, paddingTop:130, paddingBottom:100, width:"100%" }}>
        <div style={{ display:"flex", alignItems:"center", gap:12, marginBottom:44, ...anim(150) }}>
          <div style={{ width:40, height:1, backgroundColor:"#9B7B52" }} />
          <span style={{ fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:600, letterSpacing:"0.2em", textTransform:"uppercase", color:"#9B7B52" }}>{h.label}</span>
        </div>
        <h1 style={{ fontFamily:"'Playfair Display',serif", fontWeight:400, lineHeight:0.85, marginBottom:60, ...anim(200) }}>
          <span style={{ display:"block", fontSize:"clamp(5rem,12vw,11rem)", fontStyle:"italic", color:"rgba(255,255,255,0.12)", letterSpacing:"-0.03em" }}>{h.line1}</span>
          <span style={{ display:"block", fontSize:"clamp(5rem,12vw,11rem)", fontStyle:"italic", color:"#FAFAF7", letterSpacing:"-0.03em" }}>{h.line2}</span>
          <span style={{ display:"block", fontSize:"clamp(5rem,12vw,11rem)", fontStyle:"normal", color:"#9B7B52", letterSpacing:"-0.03em" }}>{h.line3}</span>
        </h1>
        <div style={{ display:"flex", alignItems:"flex-start", gap:80, flexWrap:"wrap" }}>
          <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:16, fontWeight:300, color:"rgba(255,255,255,0.72)", lineHeight:1.9, maxWidth:340, ...anim(380) }}>{h.sub}</p>
          <div style={{ display:"flex", flexWrap:"wrap", gap:14, paddingTop:4, ...anim(520) }}>
            <a href="#contact" style={{ display:"inline-flex", alignItems:"center", padding:"14px 36px", backgroundColor:"#4A5240", color:"#fff", fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:500, letterSpacing:"0.14em", textTransform:"uppercase", textDecoration:"none", transition:"background 0.3s" }}
              onMouseEnter={e=>e.currentTarget.style.backgroundColor="#5a6450"} onMouseLeave={e=>e.currentTarget.style.backgroundColor="#4A5240"}>{h.cta1}</a>
            <a href="#services" style={{ display:"inline-flex", alignItems:"center", padding:"14px 36px", border:"1px solid rgba(255,255,255,0.18)", color:"rgba(255,255,255,0.6)", fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:400, letterSpacing:"0.14em", textTransform:"uppercase", textDecoration:"none", transition:"all 0.3s" }}
              onMouseEnter={e=>{e.currentTarget.style.borderColor="rgba(255,255,255,0.5)";e.currentTarget.style.color="#fff";}}
              onMouseLeave={e=>{e.currentTarget.style.borderColor="rgba(255,255,255,0.18)";e.currentTarget.style.color="rgba(255,255,255,0.6)";}}>{h.cta2}</a>
          </div>
        </div>
      </div>
    </section>
  );
}

// ─── Hero (dispatcher) ───────────────────────────────────────────────────────
function Hero() {
  const [in_, setIn] = React.useState(false);
  const t = useT();
  const tweaks = React.useContext(window.TweakCtx || React.createContext({}));
  const heroLayout = (tweaks && tweaks.heroLayout) || "bleed";
  React.useEffect(() => { const tm = setTimeout(()=>setIn(true),80); return ()=>clearTimeout(tm); },[]);
  const anim = d => ({ transition:`opacity 0.9s ease ${d}ms, transform 0.9s ease ${d}ms`, opacity:in_?1:0, transform:in_?"none":"translateY(24px)" });
  if (heroLayout === "overlay") return <HeroOverlay t={t} in_={in_} anim={anim} />;
  if (heroLayout === "type")    return <HeroTypographic t={t} in_={in_} anim={anim} />;
  return <HeroBleed t={t} in_={in_} anim={anim} />;
}

// ─── Stats Bar ────────────────────────────────────────────────────────────────
function CountUp({ to, suffix }) {
  const [n, setN] = React.useState(0);
  const [started, setStarted] = React.useState(false);
  const ref = React.useRef(null);
  React.useEffect(() => {
    const obs = new IntersectionObserver(e=>{if(e[0].isIntersecting){setStarted(true);obs.disconnect();}},{threshold:0.5});
    obs.observe(ref.current);
    return ()=>obs.disconnect();
  },[]);
  React.useEffect(() => {
    if(!started)return;
    let start=null; const dur=1600;
    const step=ts=>{if(!start)start=ts;const p=Math.min((ts-start)/dur,1);setN(Math.floor(to*(1-Math.pow(1-p,3))));if(p<1)requestAnimationFrame(step);};
    requestAnimationFrame(step);
  },[started,to]);
  return <span ref={ref}>{n}{suffix}</span>;
}

function StatsBar() {
  const t = useT();
  const nums = [{ to:25, suffix:"+" },{ to:315, suffix:"K+" },{ to:19, suffix:"" },{ to:3, suffix:"" },{ to:18, suffix:"" }];
  return (
    <section style={{ backgroundColor:"#FAFAF7", borderBottom:"1px solid #E8DFD0" }}>
      <div className="cs-container">
        <div className="stats-row">
          {nums.map((s,i) => (
            <div key={i} style={{ padding:"56px 0", textAlign:"center", borderRight:"1px solid #E8DFD0", flex:1 }}>
              <div style={{ fontFamily:"'Playfair Display',serif", fontSize:"clamp(3rem,5.5vw,5.5rem)", fontWeight:400, color:"#1E1B18", lineHeight:1, letterSpacing:"-0.02em" }}>
                <CountUp to={s.to} suffix={s.suffix} />
              </div>
              <div style={{ fontFamily:"'Raleway',sans-serif", fontSize:11, fontWeight:500, letterSpacing:"0.14em", textTransform:"uppercase", color:"#9B7B52", marginTop:12 }}>
                {t.stats[i].label}
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ─── About ───────────────────────────────────────────────────────────────────
function About() {
  const ref = useReveal(0.05);
  const t = useT(); const a = t.about;
  return (
    <section id="about" ref={ref} style={{ backgroundColor:"#FAFAF7", padding:"140px 0 0" }}>
      <div className="cs-container">

        {/* Label */}
        <div className="fu" style={{ ...fu, display:"flex", alignItems:"center", gap:12, marginBottom:24 }}>
          <div style={{ width:40, height:1, backgroundColor:"#9B7B52" }} />
          <span className="cs-label">{a.label}</span>
        </div>

        {/* About layout */}
        <div className="about-layout">
          <div>
            <h2 className="fu" style={{ ...fu, fontFamily:"'Playfair Display',serif", fontSize:"clamp(3rem,5.5vw,5rem)", fontWeight:400, color:"#1E1B18", lineHeight:1.05, letterSpacing:"-0.01em" }}>
              {a.headline1}<br /><em style={{ fontStyle:"italic" }}>{a.headline2}</em>
            </h2>
          </div>
          <div>
            <div className="fu" style={{ ...fu }}>
              {/* Show just 2 core paragraphs — breathe */}
              <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:16, fontWeight:300, color:"#6B6560", lineHeight:1.9, marginBottom:24 }}>{a.p1}</p>
              <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:16, fontWeight:300, color:"#6B6560", lineHeight:1.9, marginBottom:36 }}>{a.p3}</p>
              <p style={{ fontFamily:"'Playfair Display',serif", fontSize:19, fontStyle:"italic", color:"#4A5240", lineHeight:1.65 }}>{a.tagline}</p>
            </div>


          </div>
        </div>

        {/* Divider */}
        <div className="fu" style={{ ...fu, margin:"120px 0 0", borderTop:"1px solid #E8DFD0" }} />

        {/* Founding Expertise */}
        <div style={{ backgroundColor:"#E8DFD0" }} className="expertise-inner">
          <div className="fu" style={{ ...fu, display:"flex", alignItems:"center", gap:12, marginBottom:56 }}>
            <div style={{ width:40, height:1, backgroundColor:"#9B7B52" }} />
            <span className="cs-label">{a.expertiseLabel}</span>
          </div>

          {/* Top row: headline left, stats right */}
          <div className="expertise-layout" style={{ marginTop:0, marginBottom:80 }}>
            <div style={{ alignSelf:"flex-start" }}>
              <h2 className="fu" style={{ ...fu, fontFamily:"'Playfair Display',serif", fontSize:"clamp(3rem,5.5vw,5rem)", fontWeight:400, color:"#1E1B18", lineHeight:1.05, letterSpacing:"-0.01em" }}>
                {a.expertiseH1}<br /><em style={{ fontStyle:"italic" }}>{a.expertiseH2}</em>
              </h2>
            </div>
            <div className="fu" style={{ ...fu, display:"flex", flexDirection:"column", alignSelf:"flex-start", borderBottom:"1px solid rgba(155,123,82,0.25)" }}>
              {a.stats.map((s,i) => (
                <div key={i} style={{ borderTop:"1px solid rgba(155,123,82,0.25)", paddingTop:20, paddingBottom:20, display:"flex", alignItems:"baseline", gap:20 }}>
                  <div style={{ fontFamily:"'Playfair Display',serif", fontSize:"clamp(2.5rem,4vw,3.5rem)", fontWeight:400, color:"rgba(155,123,82,0.7)", lineHeight:1, letterSpacing:"-0.02em", flexShrink:0 }}>{s.value}</div>
                  <div style={{ fontFamily:"'Raleway',sans-serif", fontSize:12, fontWeight:500, letterSpacing:"0.1em", textTransform:"uppercase", color:"#6B6560", lineHeight:1.4 }}>{s.label}</div>
                </div>
              ))}
            </div>
          </div>

          {/* Points — Process style */}
          <div style={{ marginTop:80 }}>
            <div className="process-desktop-v2">
              <div style={{ display:"grid", gridTemplateColumns:"repeat(4,1fr)", gap:0, borderTop:"1px solid rgba(155,123,82,0.2)" }}>
                {a.points.map((p,i) => (
                  <div key={i} className="fu" style={{ ...fu, padding:"40px 32px 0 0", position:"relative" }}>
                    <div style={{ fontFamily:"'Playfair Display',serif", fontSize:"clamp(4rem,6vw,6rem)", color:"rgba(155,123,82,0.22)", fontWeight:400, lineHeight:1, marginBottom:16, letterSpacing:"-0.04em" }}>0{i+1}</div>
                    {i < a.points.length-1 && <div style={{ position:"absolute", right:0, top:56, height:1, width:32, backgroundColor:"rgba(155,123,82,0.2)" }} />}
                    <h3 style={{ fontFamily:"'Playfair Display',serif", fontSize:18, color:"#1E1B18", fontWeight:500, marginBottom:12, lineHeight:1.3 }}>{p.title}</h3>
                    <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:13, color:"#6B6560", lineHeight:1.8, fontWeight:300 }}>{p.desc}</p>
                  </div>
                ))}
              </div>
            </div>
            <div className="process-mobile-v2">
              {a.points.map((p,i) => (
                <div key={i} className="fu" style={{ ...fu, display:"flex", gap:0, borderTop:"1px solid rgba(155,123,82,0.2)", paddingTop:28, paddingBottom:28 }}>
                  <div style={{ flexShrink:0, width:64 }}>
                    <span style={{ fontFamily:"'Playfair Display',serif", fontSize:"clamp(2.5rem,7vw,3.5rem)", color:"rgba(155,123,82,0.22)", fontWeight:400, lineHeight:1, letterSpacing:"-0.03em" }}>0{i+1}</span>
                  </div>
                  <div style={{ paddingTop:6 }}>
                    <h3 style={{ fontFamily:"'Playfair Display',serif", fontSize:20, color:"#1E1B18", fontWeight:500, marginBottom:10 }}>{p.title}</h3>
                    <p style={{ fontFamily:"'Raleway',sans-serif", fontSize:14, color:"#6B6560", lineHeight:1.85, fontWeight:300 }}>{p.desc}</p>
                  </div>
                </div>
              ))}
            </div>
          </div>
        </div>

      </div>
    </section>
  );
}

function Expertise() { return null; }

Object.assign(window, { Hero, StatsBar, About, Expertise, useReveal, fu });
