/* Top-of-page: announcement, nav, hero */

function Announcement({ t, lang, setLang }) {
  return (
    <div style={{
      background: "var(--ink)",
      color: "var(--paper)",
      fontFamily: "var(--ff-mono)",
      fontSize: 11,
      letterSpacing: "0.18em",
      textTransform: "uppercase",
      padding: "10px 40px",
      display: "flex",
      justifyContent: "space-between",
      alignItems: "center",
    }}>
      <span style={{ display: "flex", alignItems: "center", gap: 14 }}>
        <Star8 size={10} color="var(--gold-soft)" />
        <span style={{ color: "var(--gold-soft)" }}>{lang === "en" ? "Spring · 2026" : "ربيع ٢٠٢٦"}</span>
        <span style={{ opacity: 0.4 }}>·</span>
        <span>{lang === "en" ? "Sham El-Nessim collection live · 6 pieces remaining" : "مجموعة شمّ النسيم متاحة · ٦ قطع متبقّية"}</span>
      </span>
      <span style={{ display: "flex", alignItems: "center", gap: 18 }}>
        <span style={{ opacity: 0.6 }}>EGP · USD · AED</span>
        <span style={{ opacity: 0.3 }}>/</span>
        <button
          onClick={() => setLang(lang === "en" ? "ar" : "en")}
          style={{
            background: "transparent", border: "1px solid rgba(255,255,255,0.3)",
            color: "var(--paper)", padding: "4px 12px", borderRadius: 999,
            fontFamily: "var(--ff-mono)", fontSize: 10, letterSpacing: "0.18em",
            textTransform: "uppercase", cursor: "pointer",
          }}
        >
          {lang === "en" ? "عربي" : "English"}
        </button>
      </span>
    </div>
  );
}

function Nav({ t, cart, setCartOpen }) {
  return (
    <header style={{
      borderBottom: "1px solid var(--line-2)",
      background: "var(--paper)",
      position: "sticky", top: 0, zIndex: 40,
    }}>
      <div className="container-wide" style={{
        display: "grid",
        gridTemplateColumns: "1fr auto 1fr",
        alignItems: "center",
        padding: "22px 40px",
        gap: 40,
      }}>
        <nav style={{ display: "flex", gap: 32, fontSize: 13, letterSpacing: "0.04em" }}>
          <a href="#collections" style={linkStyle}>{t.nav.collections}</a>
          <a href="#makers" style={linkStyle}>{t.nav.makers}</a>
          <a href="#journal" style={linkStyle}>{t.nav.journal}</a>
          <a href="#atelier" style={linkStyle}>{t.nav.atelier}</a>
        </nav>
        <Wordmark size={28} />
        <div style={{ display: "flex", gap: 22, justifyContent: "flex-end", alignItems: "center", fontSize: 13 }}>
          <a href="#contact" style={linkStyle}>{t.nav.contact}</a>
          <button style={iconBtn} aria-label="search">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4">
              <circle cx="11" cy="11" r="7" /><path d="M20 20l-3.5-3.5" />
            </svg>
          </button>
          <button style={iconBtn} aria-label="account">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4">
              <circle cx="12" cy="8" r="4" /><path d="M4 21c0-4 3.6-7 8-7s8 3 8 7" />
            </svg>
          </button>
          <button onClick={() => setCartOpen(true)} style={{ ...iconBtn, display: "flex", alignItems: "center", gap: 8, fontSize: 13 }} aria-label="bag">
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.4">
              <path d="M6 8h12l-1 12H7L6 8Z" /><path d="M9 8a3 3 0 1 1 6 0" />
            </svg>
            <span>{t.nav.bag} <span style={{ color: "var(--terracotta)" }}>({cart})</span></span>
          </button>
        </div>
      </div>
    </header>
  );
}
const linkStyle = { textDecoration: "none", color: "var(--ink)", fontFamily: "var(--ff-sans)", fontWeight: 400 };
const iconBtn = { background: "transparent", border: 0, padding: 8, color: "var(--ink)", cursor: "pointer", minWidth: 44, minHeight: 44, display: "inline-flex", alignItems: "center", justifyContent: "center" };

function Hero({ t, lang }) {
  return (
    <section style={{ position: "relative", padding: "60px 0 40px", overflow: "hidden" }}>
      <div className="container-wide">
        <div className="hero-grid">
          {/* Left text */}
          <div>
            <div className="eyebrow" style={{ marginBottom: 40 }}>
              <span className="dot"></span>{t.hero_eyebrow}
            </div>
            <h1 style={{
              fontFamily: "var(--ff-display)",
              fontSize: "clamp(38px, 9vw, 116px)",
              lineHeight: 1.0,
              letterSpacing: "-0.02em",
              color: "var(--ink)",
              margin: 0,
              fontWeight: 400,
            }}>
              {t.hero_title_1}<br />
              <span style={{ fontStyle: "italic", color: "var(--terracotta)" }}>{t.hero_title_2}</span><br />
              {t.hero_title_3}
            </h1>

            <div className="hero-meta" style={{ marginTop: 40 }}>
              <div style={{ borderTop: "1px solid var(--ink)", paddingTop: 14, fontFamily: "var(--ff-mono)", fontSize: 10, letterSpacing: "0.2em", textTransform: "uppercase" }}>
                № 001 — Manifesto
              </div>
              <p style={{ fontSize: 17, lineHeight: 1.55, color: "var(--olive)", maxWidth: 520, margin: 0 }}>
                {t.hero_body}
              </p>
            </div>

            <div className="hero-cta-row" style={{ marginTop: 36, display: "flex", gap: 14, flexWrap: "wrap" }}>
              <a href="#/shop" className="btn">
                {t.hero_cta_1}
                <span className="arrow">→</span>
              </a>
              <a href="#/makers" className="btn ghost">
                {t.hero_cta_2}
              </a>
            </div>
          </div>

          {/* Right arch image */}
          <div className="hero-img-wrap">
            <Placeholder
              tone="terracotta"
              arch
              topLabel="Hero · 001"
              label="hands shaping a macramé knot, warm daylight"
              meta="Atelier · Cairo"
            />
            {/* Decorative side caption */}
            <div className="hero-side-caption" style={{
              position: "absolute", bottom: -20, left: -90,
              transform: "rotate(-90deg)", transformOrigin: "left bottom",
              fontFamily: "var(--ff-mono)", fontSize: 10, letterSpacing: "0.3em",
              textTransform: "uppercase", color: "var(--muted)",
              whiteSpace: "nowrap",
            }}>
              ◆ Photographed in the atelier · natural light
            </div>
            {/* Floating info card */}
            <div className="hero-floater" style={{
              position: "absolute", right: -20, top: 60,
              background: "var(--paper)", padding: "18px 22px",
              border: "1px solid var(--line)", borderRadius: 999,
              boxShadow: "0 20px 40px -20px rgba(28,26,21,0.25)",
              display: "flex", alignItems: "center", gap: 14,
            }}>
              <div style={{ width: 42, height: 42, borderRadius: "50%", background: "var(--gold)", display: "grid", placeItems: "center", color: "var(--ink)", flexShrink: 0 }}>
                <Star8 size={18} />
              </div>
              <div style={{ lineHeight: 1.2 }}>
                <div style={{ fontFamily: "var(--ff-mono)", fontSize: 9, letterSpacing: "0.2em", textTransform: "uppercase", color: "var(--muted)" }}>{lang === "en" ? "Pieces this season" : "قطع هذا الموسم"}</div>
                <div style={{ fontFamily: "var(--ff-display)", fontSize: 22 }}>{lang === "en" ? "48 numbered" : "٤٨ مرقّمة"}</div>
              </div>
            </div>
            {/* Arabic descender */}
            <div className="ar hero-arabic-descender" style={{
              position: "absolute", bottom: -42, right: 20,
              fontSize: 54, color: "var(--olive)", opacity: 0.16,
              fontFamily: "var(--ff-ar)",
            }}>
              باليد
            </div>
          </div>
        </div>
      </div>

      {/* Bottom stats strip */}
      <div style={{ marginTop: 80 }}>
        <div style={{ borderTop: "1px solid var(--line)", borderBottom: "1px solid var(--line)" }}>
          <div className="container-wide hero-stats" style={{ padding: "28px var(--gutter)" }}>
            {[
              { k: "Artisans", v: "12", ar: "حرفيًّا" },
              { k: "Governorates", v: "13", ar: "محافظة" },
              { k: "Pieces released", v: "48 / season", ar: "قطعة / موسم" },
              { k: "Packed in", v: "Zamalek, Cairo", ar: "الزمالك" },
            ].map((s, i) => (
              <div key={i} style={{ borderRight: i < 3 ? "1px solid var(--line-2)" : "none" }}>
                <div style={{ fontFamily: "var(--ff-mono)", fontSize: 10, letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--muted)", marginBottom: 8 }}>
                  {lang === "en" ? s.k : s.ar}
                </div>
                <div style={{ fontFamily: "var(--ff-display)", fontSize: 28, letterSpacing: "-0.01em" }}>
                  {s.v}
                </div>
              </div>
            ))}
          </div>
        </div>

        {/* Marquee */}
        <Marquee items={t.marquee} lang={lang} />
      </div>
    </section>
  );
}

// Self-contained, language-agnostic marquee.
// Uses JS to read the natural width of one set of items, then duplicates
// only as many copies as are needed to fill 2× the container, and translates
// by the first-set width per cycle — this guarantees a seamless loop in any
// direction or language without depending on percentage math.
function Marquee({ items, lang, speed = 60 /* px/sec */ }) {
  const containerRef = React.useRef(null);
  const setRef = React.useRef(null);
  const trackRef = React.useRef(null);
  const [setWidth, setSetWidth] = React.useState(0);
  const [copies, setCopies] = React.useState(2);

  // Measure one set's width and decide how many copies to render
  React.useEffect(() => {
    if (!setRef.current || !containerRef.current) return;
    const measure = () => {
      const w = setRef.current.scrollWidth;
      const cw = containerRef.current.clientWidth;
      if (!w || !cw) return;
      setSetWidth(w);
      // Need at least enough copies so total > 2 × container
      const needed = Math.max(2, Math.ceil((cw * 2) / w) + 1);
      setCopies(needed);
    };
    measure();
    const ro = new ResizeObserver(measure);
    ro.observe(setRef.current);
    ro.observe(containerRef.current);
    return () => ro.disconnect();
  }, [items]);

  // Drive the translateX loop via rAF
  React.useEffect(() => {
    if (!setWidth || !trackRef.current) return;
    let raf, last = performance.now(), x = 0;
    const tick = (now) => {
      const dt = (now - last) / 1000; last = now;
      x -= speed * dt;
      // wrap when one set has fully scrolled past
      if (-x >= setWidth) x += setWidth;
      if (trackRef.current) trackRef.current.style.transform = `translate3d(${x}px,0,0)`;
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [setWidth, speed]);

  // One "set" of items rendered; we render `copies` of it (first one is the
  // measured reference; the rest are aria-hidden duplicates).
  const renderSet = (k, isMeasure = false) => (
    <div
      key={k}
      ref={isMeasure ? setRef : null}
      aria-hidden={!isMeasure}
      style={{ display: "flex", gap: 56, flexShrink: 0, paddingRight: 56 }}
    >
      {items.map((w, i) => (
        <span key={i} style={{ display: "inline-flex", alignItems: "center", gap: 40, whiteSpace: "nowrap" }}>
          <span style={{ fontFamily: "var(--ff-display)", fontStyle: "italic", fontSize: 28, color: "var(--olive)" }}>{w}</span>
          <Star8 size={14} color="var(--terracotta)" />
        </span>
      ))}
    </div>
  );

  return (
    <div
      ref={containerRef}
      className="marquee"
      style={{ padding: "18px 0", borderBottom: "1px solid var(--line-2)", background: "var(--paper-2)", overflow: "hidden", direction: "ltr" }}
    >
      <div
        ref={trackRef}
        style={{ display: "flex", width: "max-content", willChange: "transform" }}
      >
        {Array.from({ length: copies }, (_, i) => renderSet(i, i === 0))}
      </div>
    </div>
  );
}

Object.assign(window, { Announcement, Nav, Hero });
