/* sections.jsx — all page sections for Sukatukang */

const WHATSAPP = "60123456789"; // placeholder — swap for Sukatukang's real number before launch

// ===================== HEADER =====================
function Header({ lang }) {
  const [scrolled, setScrolled] = React.useState(false);
  const mobile = useIsMobile();
  React.useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 24);
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);
  return (
    <header style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 50,
      transition: "background .3s ease, box-shadow .3s ease, border-color .3s ease",
      background: scrolled ? "rgba(245,239,228,0.86)" : "transparent",
      backdropFilter: scrolled ? "saturate(140%) blur(10px)" : "none",
      WebkitBackdropFilter: scrolled ? "saturate(140%) blur(10px)" : "none",
      borderBottom: "1px solid " + (scrolled ? "var(--line)" : "transparent"),
    }}>
      <div className="wrap" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", height: 72 }}>
        <a href="#top" style={{ display: "flex", alignItems: "baseline", gap: 10 }}>
          <span style={{ fontFamily: "var(--font-display)", fontSize: 25, fontWeight: 500, letterSpacing: "-0.01em" }}>Sukatukang</span>
          <span style={{ width: 7, height: 7, borderRadius: "50%", background: "var(--accent)", transform: "translateY(-2px)" }}></span>
        </a>
        <nav style={{ display: mobile ? "none" : "flex", alignItems: "center", gap: 30 }} className="hdr-nav">
          {COPY.nav.map((n) => (
            <a key={n.id} href={"#" + n.id} style={{ fontSize: 14.5, color: "var(--ink-soft)", fontWeight: 500 }}
               onMouseEnter={(e) => (e.currentTarget.style.color = "var(--accent-deep)")}
               onMouseLeave={(e) => (e.currentTarget.style.color = "var(--ink-soft)")}>
              {pri(n, lang)}
            </a>
          ))}
        </nav>
        <a href="#tempah" className="btn btn--primary" style={{ padding: "11px 18px", fontSize: 13.5 }}>
          {pri(COPY.hero.ctaPrimary, lang)} <span className="arrow">→</span>
        </a>
      </div>
    </header>
  );
}

// ===================== HERO =====================
function Hero({ lang, headlineIdx }) {
  const h = COPY.hero;
  const headline = h.headlines[headlineIdx] || h.headlines[0];
  return (
    <section id="top" style={{ position: "relative", paddingTop: 72 }}>
      {/* full-bleed image */}
      <div style={{ position: "relative", height: "min(92vh, 880px)", minHeight: 600 }}>
        <img src="hero.jpg" alt="Booth mudah alih terbesar tersuai oleh Sukatukang" fetchpriority="high"
             style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }} />
        {/* legibility scrim */}
        <div style={{ position: "absolute", inset: 0,
          background: "linear-gradient(90deg, rgba(28,20,12,0.82) 0%, rgba(28,20,12,0.55) 38%, rgba(28,20,12,0.15) 70%, rgba(28,20,12,0.05) 100%)" }}></div>
        <div style={{ position: "absolute", inset: 0,
          background: "linear-gradient(0deg, rgba(28,20,12,0.45) 0%, transparent 30%)" }}></div>

        {/* content */}
        <div className="wrap" style={{ position: "absolute", inset: 0, display: "flex", flexDirection: "column", justifyContent: "center" }}>
          <div style={{ maxWidth: 760 }}>
            <div className="reveal in" style={{ marginBottom: 22 }}>
              <span style={{
                fontFamily: "var(--font-mono)", fontSize: 12, letterSpacing: "0.16em", textTransform: "uppercase",
                color: "#f0c89a", display: "inline-block",
              }}>{pri(h.eyebrow, lang)}</span>
              <span style={{ display: "block", fontFamily: "var(--font-mono)", fontSize: 11, letterSpacing: "0.12em",
                textTransform: "uppercase", color: "rgba(245,239,228,0.55)", marginTop: 6 }}>{sec(h.eyebrow, lang)}</span>
            </div>

            <h1 style={{ color: "var(--bone)", fontSize: "clamp(40px, 6vw, 82px)", lineHeight: 1.02, letterSpacing: "-0.02em", fontWeight: 500 }}>
              {pri(headline, lang).split("\n").map((ln, i) => <span key={i} style={{ display: "block" }}>{ln}</span>)}
            </h1>
            <div style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontSize: "clamp(18px,2vw,24px)",
              color: "rgba(245,239,228,0.7)", marginTop: 14 }}>{sec(headline, lang)}</div>

            <p style={{ color: "rgba(245,239,228,0.86)", fontSize: "clamp(16px,1.3vw,19px)", lineHeight: 1.6, marginTop: 26, maxWidth: 560 }}>
              {pri(h.sub, lang)}
            </p>

            <div style={{ display: "flex", flexWrap: "wrap", gap: 14, marginTop: 36 }}>
              <a href="#tempah" className="btn btn--primary">{pri(h.ctaPrimary, lang)} <span className="arrow">→</span></a>
              <a href="#kerja" className="btn btn--ghost" style={{ color: "var(--bone)", borderColor: "rgba(245,239,228,0.4)" }}
                 onMouseEnter={(e) => { e.currentTarget.style.background = "rgba(245,239,228,0.1)"; e.currentTarget.style.borderColor = "var(--bone)"; }}
                 onMouseLeave={(e) => { e.currentTarget.style.background = "transparent"; e.currentTarget.style.borderColor = "rgba(245,239,228,0.4)"; }}>
                {pri(h.ctaSecondary, lang)}
              </a>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

// ===================== TRUST STRIP =====================
function Trust({ lang }) {
  const t = COPY.trust;
  return (
    <section style={{ background: "var(--cream)", borderBottom: "1px solid var(--line)" }}>
      <div className="wrap" style={{ paddingBlock: 38, display: "flex", flexWrap: "wrap", alignItems: "center", justifyContent: "space-between", gap: 30 }}>
        <div style={{ maxWidth: 360 }}>
          <div style={{ fontSize: 16.5, fontWeight: 600, color: "var(--ink)", lineHeight: 1.4 }}>{pri(t.line, lang)}</div>
          <div style={{ fontSize: 13, color: "var(--muted)", marginTop: 4 }}>{sec(t.line, lang)}</div>
        </div>

        {/* logo placeholders — real client logos only after permission (D'Herbs etc.) */}
        <div style={{ display: "flex", gap: 14, flexWrap: "wrap" }}>
          {[0,1,2,3].map((i) => (
            <div key={i} style={{
              width: 116, height: 48, borderRadius: 6,
              border: "1px dashed var(--line)", display: "flex", alignItems: "center", justifyContent: "center",
              background: "repeating-linear-gradient(45deg, transparent 0 7px, rgba(135,122,104,0.06) 7px 14px)",
            }}>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: 10, color: "var(--muted)", letterSpacing: "0.05em" }}>{pri(t.logosNote, lang)}</span>
            </div>
          ))}
        </div>

        {/* stats */}
        <div style={{ display: "flex", gap: 28 }}>
          {t.stats.map((s, i) => (
            <div key={i} style={{ textAlign: "right" }}>
              <div style={{ fontFamily: "var(--font-display)", fontSize: 30, lineHeight: 1, color: "var(--accent-deep)", fontWeight: 500 }}>{s.value}</div>
              <div style={{ fontSize: 12.5, color: "var(--ink-soft)", marginTop: 5 }}>{pri(s.label, lang)}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ===================== SECTION HEADER =====================
function SectionHead({ obj, title, intro, lang, align = "left", light }) {
  return (
    <div style={{ maxWidth: 680, textAlign: align, marginInline: align === "center" ? "auto" : 0 }}>
      <Eyebrow obj={obj} lang={lang} />
      <h2 style={{ fontSize: "clamp(30px, 3.6vw, 48px)", lineHeight: 1.08, letterSpacing: "-0.018em", marginTop: 16,
        color: light ? "var(--bone)" : "var(--ink)", fontWeight: 500 }}>
        {pri(title, lang).split("\n").map((ln, i) => <span key={i} style={{ display: "block" }}>{ln}</span>)}
      </h2>
      {intro ? <p style={{ fontSize: 17, color: light ? "rgba(245,239,228,0.75)" : "var(--ink-soft)", marginTop: 16, lineHeight: 1.6 }}>{pri(intro, lang)}</p> : null}
    </div>
  );
}

// ===================== PORTFOLIO =====================
function Portfolio({ lang, onOpen }) {
  const w = COPY.work;
  const [filter, setFilter] = React.useState("all");
  const ref = useReveal();
  const mobile = useIsMobile();
  const shown = BUILDS.filter((b) => filter === "all" || b.cat === filter);

  return (
    <section id="kerja" ref={ref} style={{ background: "var(--bone)", paddingBlock: "clamp(70px,9vw,120px)" }}>
      <div className="wrap">
        <div className="reveal" style={{ display: "flex", flexWrap: "wrap", alignItems: "flex-end", justifyContent: "space-between", gap: 24 }}>
          <SectionHead obj={w.eyebrow} title={w.title} intro={w.intro} lang={lang} />
        </div>

        {/* filter chips */}
        <div className="reveal noscroll" style={{ display: "flex", gap: 10, marginTop: 36, overflowX: "auto", paddingBottom: 4 }}>
          {w.filters.map((f) => {
            const active = filter === f.id;
            return (
              <button key={f.id} onClick={() => setFilter(f.id)} style={{
                flex: "0 0 auto",
                padding: "10px 18px", borderRadius: 999, fontSize: 14, fontWeight: 600,
                border: "1px solid " + (active ? "var(--walnut)" : "var(--line)"),
                background: active ? "var(--walnut)" : "transparent",
                color: active ? "var(--bone)" : "var(--ink-soft)",
                transition: "all .18s ease", whiteSpace: "nowrap",
              }}>
                {pri(f, lang)}
              </button>
            );
          })}
        </div>

        {/* grid */}
        <div style={{ display: "grid", gridTemplateColumns: "repeat(12, 1fr)", gap: 22, marginTop: 32 }}>
          {shown.map((b, i) => {
            const big = b.feature && filter === "all";
            const span = big ? 12 : (mobile ? 6 : 4);
            return (
              <article key={b.id} className="reveal build-card"
                style={{ gridColumn: "span " + span, transitionDelay: (i * 40) + "ms" }}
                onClick={() => onOpen(b)}>
                <BuildCard b={b} lang={lang} big={big && !mobile} />
              </article>
            );
          })}
        </div>
      </div>
    </section>
  );
}

function BuildCard({ b, lang, big }) {
  const [hover, setHover] = React.useState(false);
  return (
    <div onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{ cursor: "pointer", display: big ? "grid" : "block", gridTemplateColumns: big ? "1.4fr 1fr" : undefined,
        gap: big ? 0 : undefined, border: "1px solid var(--line)", borderRadius: "var(--r-lg)", overflow: "hidden",
        background: "var(--cream)", transition: "transform .25s ease, box-shadow .25s ease, border-color .25s ease",
        transform: hover ? "translateY(-4px)" : "none",
        boxShadow: hover ? "0 18px 40px -22px rgba(39,28,18,0.5)" : "0 1px 0 rgba(0,0,0,0.02)",
        borderColor: hover ? "var(--oak)" : "var(--line)" }}>
      <div style={{ position: "relative", overflow: "hidden", aspectRatio: big ? "auto" : "4 / 3", height: big ? "100%" : undefined, minHeight: big ? 380 : undefined }}>
        <Slot id={"build-" + b.id} placeholder={pri(b.name, lang).toLowerCase()}
              style={{ position: "absolute", inset: 0, width: "100%", height: "100%",
                transform: hover ? "scale(1.04)" : "scale(1)", transition: "transform .5s ease" }} />
        <span style={{ position: "absolute", top: 14, left: 14, fontFamily: "var(--font-mono)", fontSize: 10.5,
          textTransform: "uppercase", letterSpacing: "0.08em", color: "var(--walnut)", background: "rgba(245,239,228,0.9)",
          padding: "5px 9px", borderRadius: 5 }}>{pri(b.use, lang)}</span>
      </div>
      <div style={{ padding: big ? "40px 42px" : "20px 22px 24px", display: "flex", flexDirection: "column", justifyContent: big ? "center" : "flex-start" }}>
        {big ? <div className="eyebrow" style={{ marginBottom: 12 }}>★ {lang === "en" ? "Signature" : "Binaan utama"}</div> : null}
        <h3 style={{ fontSize: big ? "clamp(26px,3vw,40px)" : 20, lineHeight: 1.1, letterSpacing: "-0.01em", fontWeight: 500 }}>{pri(b.name, lang)}</h3>
        <p style={{ fontSize: big ? 17 : 14, color: "var(--ink-soft)", marginTop: big ? 12 : 7 }}>{pri(b.cap, lang)}</p>
        <div style={{ fontSize: 12.5, color: "var(--muted)", marginTop: big ? 8 : 5 }}>{sec(b.name, lang)}</div>
        <div style={{ marginTop: big ? 28 : 14, display: "flex", alignItems: "center", gap: 8, color: "var(--accent-deep)", fontWeight: 600, fontSize: 14 }}>
          {lang === "en" ? "View build" : "Lihat binaan"} <span style={{ transition: "transform .2s ease", transform: hover ? "translateX(4px)" : "none" }}>→</span>
        </div>
      </div>
    </div>
  );
}

// ===================== BUILD DETAIL MODAL =====================
function BuildModal({ build, lang, onClose }) {
  React.useEffect(() => {
    const onKey = (e) => { if (e.key === "Escape") onClose(); };
    document.addEventListener("keydown", onKey);
    document.body.style.overflow = "hidden";
    return () => { document.removeEventListener("keydown", onKey); document.body.style.overflow = ""; };
  }, [onClose]);
  if (!build) return null;
  const b = build;
  return (
    <div onClick={onClose} style={{ position: "fixed", inset: 0, zIndex: 100, background: "rgba(28,20,12,0.6)",
      backdropFilter: "blur(4px)", WebkitBackdropFilter: "blur(4px)", display: "flex", alignItems: "flex-start",
      justifyContent: "center", padding: "clamp(16px,5vh,64px) 16px", overflowY: "auto" }}>
      <div onClick={(e) => e.stopPropagation()} style={{ background: "var(--bone)", borderRadius: "var(--r-lg)",
        maxWidth: 960, width: "100%", overflow: "hidden", boxShadow: "0 40px 90px -30px rgba(0,0,0,0.6)",
        animation: "modalIn .3s ease" }}>
        {/* hero image */}
        <div style={{ position: "relative", aspectRatio: "16 / 9" }}>
          <Slot id={"modal-" + b.id} placeholder={pri(b.name, lang).toLowerCase() + " — main shot"}
                style={{ position: "absolute", inset: 0, width: "100%", height: "100%" }} />
          <button onClick={onClose} aria-label="Close" style={{ position: "absolute", top: 16, right: 16,
            width: 40, height: 40, borderRadius: "50%", border: "none", background: "rgba(245,239,228,0.92)",
            fontSize: 20, color: "var(--ink)", display: "flex", alignItems: "center", justifyContent: "center" }}>✕</button>
          <span style={{ position: "absolute", bottom: 16, left: 16, fontFamily: "var(--font-mono)", fontSize: 11,
            textTransform: "uppercase", letterSpacing: "0.08em", color: "var(--walnut)", background: "rgba(245,239,228,0.92)",
            padding: "6px 11px", borderRadius: 6 }}>{pri(b.use, lang)}</span>
        </div>

        <div style={{ padding: "clamp(24px,4vw,44px)" }}>
          <h3 style={{ fontSize: "clamp(28px,3.4vw,42px)", lineHeight: 1.08, letterSpacing: "-0.015em", fontWeight: 500 }}>{pri(b.name, lang)}</h3>
          <div style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontSize: 19, color: "var(--muted)", marginTop: 6 }}>{sec(b.name, lang)}</div>
          <p style={{ fontSize: 17.5, color: "var(--ink-soft)", marginTop: 18, maxWidth: 620, lineHeight: 1.6 }}>{pri(b.cap, lang)}.</p>

          {/* thumbnail strip */}
          <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: 12, marginTop: 26 }}>
            {[1,2,3].map((n) => (
              <Slot key={n} id={"modal-" + b.id + "-" + n} placeholder={"detail " + n} aspect="1 / 1" radius={8} />
            ))}
          </div>

          <div style={{ display: "flex", flexWrap: "wrap", gap: 24, marginTop: 28, paddingTop: 24, borderTop: "1px solid var(--line)" }}>
            <div>
              <div className="eyebrow">{lang === "en" ? "Use case" : "Kegunaan"}</div>
              <div style={{ fontSize: 16, fontWeight: 600, marginTop: 6 }}>{pri(b.use, lang)}</div>
            </div>
            <div>
              <div className="eyebrow">{lang === "en" ? "Category" : "Kategori"}</div>
              <div style={{ fontSize: 16, fontWeight: 600, marginTop: 6 }}>{pri(COPY.work.filters.find((f) => f.id === b.cat), lang)}</div>
            </div>
          </div>

          <div style={{ marginTop: 30 }}>
            <a href="#tempah" onClick={onClose} className="btn btn--primary">
              {lang === "en" ? "Build something like this" : "Bina yang seperti ini"} <span className="arrow">→</span>
            </a>
          </div>
        </div>
      </div>
    </div>
  );
}

// ===================== PROCESS =====================
function Process({ lang }) {
  const p = COPY.process;
  const ref = useReveal();
  return (
    <section id="proses" ref={ref} style={{ background: "var(--espresso)", color: "var(--bone)", paddingBlock: "clamp(70px,9vw,120px)" }}>
      <div className="wrap">
        <div className="reveal">
          <SectionHead obj={p.eyebrow} title={p.title} lang={lang} light />
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(220px, 1fr))", gap: 0, marginTop: 56,
          borderTop: "1px solid rgba(245,239,228,0.16)" }}>
          {p.steps.map((s, i) => (
            <div key={i} className="reveal" style={{ padding: "32px 28px 32px 0", borderBottom: "1px solid rgba(245,239,228,0.12)",
              borderRight: i < p.steps.length - 1 ? "1px solid rgba(245,239,228,0.12)" : "none", paddingLeft: i === 0 ? 0 : 28,
              transitionDelay: (i * 70) + "ms" }}>
              <div style={{ fontFamily: "var(--font-mono)", fontSize: 13, color: "var(--accent)", letterSpacing: "0.1em" }}>{s.n}</div>
              <h3 style={{ fontSize: 22, lineHeight: 1.2, marginTop: 18, fontWeight: 500, letterSpacing: "-0.01em" }}>{pri(s, lang)}</h3>
              <div style={{ fontSize: 13, color: "rgba(245,239,228,0.5)", marginTop: 6, fontStyle: "italic", fontFamily: "var(--font-display)" }}>{sec(s, lang)}</div>
              <p style={{ fontSize: 14.5, color: "rgba(245,239,228,0.72)", marginTop: 14, lineHeight: 1.55 }}>{pri(s.body, lang)}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ===================== WHY =====================
function Why({ lang }) {
  const w = COPY.why;
  const ref = useReveal();
  const mobile = useIsMobile();
  return (
    <section id="kenapa" ref={ref} style={{ background: "var(--cream)", paddingBlock: "clamp(70px,9vw,120px)" }}>
      <div className="wrap" style={{ display: "grid", gridTemplateColumns: mobile ? "1fr" : "minmax(280px, 0.85fr) 1.15fr", gap: "clamp(40px,6vw,90px)", alignItems: "start" }}>
        <div className="reveal" style={{ position: mobile ? "static" : "sticky", top: 100 }}>
          <SectionHead obj={w.eyebrow} title={w.title} lang={lang} />
          <div style={{ marginTop: 28 }}>
            <Slot id="why" placeholder="craftsman / workshop detail (portrait)" aspect="4 / 5" radius={14} />
          </div>
        </div>
        <div style={{ display: "grid", gap: 0 }}>
          {w.pillars.map((p, i) => (
            <div key={i} className="reveal" style={{ display: "flex", gap: 22, padding: "26px 0",
              borderBottom: i < w.pillars.length - 1 ? "1px solid var(--line)" : "none", transitionDelay: (i * 60) + "ms" }}>
              <div style={{ fontFamily: "var(--font-display)", fontSize: 34, color: "var(--oak)", lineHeight: 1, width: 44, flexShrink: 0, fontWeight: 500 }}>0{i + 1}</div>
              <div>
                <h3 style={{ fontSize: 22, lineHeight: 1.25, fontWeight: 500, letterSpacing: "-0.01em" }}>{pri(p, lang)}</h3>
                <div style={{ fontSize: 13.5, color: "var(--muted)", marginTop: 4, fontStyle: "italic", fontFamily: "var(--font-display)" }}>{sec(p, lang)}</div>
                <p style={{ fontSize: 16, color: "var(--ink-soft)", marginTop: 12, lineHeight: 1.6, maxWidth: 520 }}>{pri(p.body, lang)}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ===================== SOUVENIR TEASER =====================
function Souvenir({ lang }) {
  const s = COPY.souvenir;
  const ref = useReveal();
  return (
    <section id="cenderahati" ref={ref} style={{ background: "var(--walnut)", color: "var(--bone)", paddingBlock: "clamp(70px,9vw,120px)" }}>
      <div className="wrap">
        <div className="reveal" style={{ display: "flex", flexWrap: "wrap", alignItems: "flex-end", justifyContent: "space-between", gap: 24 }}>
          <SectionHead obj={s.eyebrow} title={s.title} intro={s.body} lang={lang} light />
          <div style={{ display: "flex", flexDirection: "column", gap: 12, alignItems: "flex-start" }}>
            <a href="#tempah" className="btn btn--primary">{pri(s.cta, lang)} <span className="arrow">→</span></a>
            <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "rgba(245,239,228,0.55)", letterSpacing: "0.04em" }}>{pri(s.phase, lang)}</span>
          </div>
        </div>
        <div style={{ display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(190px, 1fr))", gap: 18, marginTop: 48 }}>
          {s.items.map((it, i) => (
            <div key={i} className="reveal" style={{ transitionDelay: (i * 50) + "ms" }}>
              <Slot id={"souv-" + i} placeholder={pri(it, lang).toLowerCase()} aspect="1 / 1" radius={10} />
              <div style={{ fontSize: 15, fontWeight: 600, marginTop: 12 }}>{pri(it, lang)}</div>
              <div style={{ fontSize: 12.5, color: "rgba(245,239,228,0.55)", marginTop: 2 }}>{sec(it, lang)}</div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { WHATSAPP, Header, Hero, Trust, SectionHead, Portfolio, BuildCard, BuildModal, Process, Why, Souvenir });
