// Laad — Torah scroll case workshop. Premium luxury redesign.

const { useState, useMemo, useEffect } = React;

const LaadSite = ({ device = "desktop", lang = "he", theme = "light", accent = "gold", density = "comfortable" }) => {
  const c = window.SITE_CONTENT[lang];
  const isMobile = device === "mobile";

  const [activeFilter, setActiveFilter] = useState(c.filters[0]);
  const [navOpen, setNavOpen] = useState(false);

  // Reset filter when language changes so the localized "All" stays selected.
  useEffect(() => {
    setActiveFilter(c.filters[0]);
  }, [lang]);

  const filteredProducts = useMemo(() => {
    if (activeFilter === c.filters[0]) return c.products;
    return c.products.filter(p => p.family === activeFilter);
  }, [activeFilter, c]);

  // Premium accent palette — black/white/gold/silver/rose-gold
  const accentMap = {
    gold:   { ink: "#a78232", soft: "#e6cf95", deep: "#8c6a23" },
    silver: { ink: "#7a7e85", soft: "#d8dcdf", deep: "#5a5e65" },
    rose:   { ink: "#a87968", soft: "#e8c8b4", deep: "#7e5546" },
    black:  { ink: "#0a0a0a", soft: "#3a3a3a", deep: "#000000" },
    green:  { ink: "#1f5d3a", soft: "#7fae8e", deep: "#0e3a22" }, // legacy
  };
  const a = accentMap[accent] || accentMap.gold;

  const wrapperStyle = {
    "--bg":         "var(--laad-ivory)",
    "--bg-2":       "var(--laad-pearl)",
    "--bg-3":       "var(--laad-mist)",
    "--ink":        "var(--laad-ink)",
    "--ink-2":      "var(--laad-ink-2)",
    "--ink-3":      "var(--laad-ink-3)",
    "--rule":       "var(--laad-rule)",
    "--rule-soft":  "var(--laad-rule-soft)",
    "--accent":     a.ink,
    "--accent-soft":a.soft,
    "--accent-deep":a.deep,
    "--accent-ink": "#fff",
    direction:      c.dir,
    fontFamily:     lang === "he"
                      ? "'Heebo', 'Frank Ruhl Libre', Georgia, serif"
                      : "'Inter', 'Cormorant Garamond', Georgia, serif",
    background:     "var(--laad-ivory)",
    color:          "var(--laad-ink)",
    width:          "100%",
    minHeight:      "100%",
    WebkitFontSmoothing: "antialiased",
  };

  return (
    <div className={`laad laad-${device}`} style={wrapperStyle} dir={c.dir} lang={c.lang}>
      <TopBar c={c} lang={lang} isMobile={isMobile} navOpen={navOpen} setNavOpen={setNavOpen} />
      {isMobile && navOpen && <MobileMenu c={c} lang={lang} onClose={() => setNavOpen(false)} />}
      <main id="main">
        <window.Laad_Hero c={c} isMobile={isMobile} lang={lang} />
        <window.Laad_Collection
          c={c} isMobile={isMobile} lang={lang}
          activeFilter={activeFilter} setActiveFilter={setActiveFilter}
          products={filteredProducts}
        />
        <window.Laad_Story c={c} isMobile={isMobile} lang={lang} />
        <window.Laad_Custom c={c} isMobile={isMobile} lang={lang} />
        <window.Laad_Contact c={c} isMobile={isMobile} lang={lang} />
      </main>
      <window.Laad_Footer c={c} isMobile={isMobile} lang={lang} />
    </div>
  );
};

// ─── TOP BAR ─────────────────────────────────────────────────
const TopBar = ({ c, lang, isMobile, navOpen, setNavOpen, alwaysSolid = false }) => {
  const isHe = lang === "he";
  const [scrolled, setScrolled] = useState(false);

  useEffect(() => {
    const onScroll = () => setScrolled(window.scrollY > 32);
    onScroll();
    window.addEventListener("scroll", onScroll, { passive: true });
    return () => window.removeEventListener("scroll", onScroll);
  }, []);

  const isSolid = alwaysSolid || scrolled;
  const padV = isMobile ? (isSolid ? 10 : 14) : (isSolid ? 14 : 22);
  const padH = isMobile ? 16 : 36;
  const logoH = isMobile ? (isSolid ? 51 : 58) : (isSolid ? 58 : 74);

  const navItems = [
    { href: "#products", label: isHe ? "הקולקציה"    : "Collection" },
    { href: "#story",    label: isHe ? "הסיפור"      : "Story"      },
    { href: "#custom",   label: isHe ? "תהליך ההזמנה" : "Process"    },
    { href: "#contact",  label: isHe ? "צור קשר"     : "Contact"    },
  ];

  return (
    <header style={{
      position: "fixed", top: 0, left: 0, right: 0, zIndex: 50,
      background: isSolid
        ? "rgba(10,10,10,0.82)"
        : "linear-gradient(180deg, rgba(10,10,10,0.55) 0%, rgba(10,10,10,0.0) 100%)",
      backdropFilter: isSolid ? "blur(18px) saturate(160%)" : "blur(2px)",
      WebkitBackdropFilter: isSolid ? "blur(18px) saturate(160%)" : "blur(2px)",
      borderBottom: isSolid ? "1px solid rgba(230,207,149,0.18)" : "1px solid transparent",
      padding: `${padV}px ${padH}px`,
      display: "grid",
      gridTemplateColumns: "1fr auto 1fr",
      alignItems: "center",
      gap: 16,
      direction: c.dir,
      transition: "all 0.45s var(--laad-ease)",
    }}>
      {/* RIGHT in RTL: menu (mobile) or nav (desktop) */}
      <div style={{ display: "flex", justifyContent: "flex-start", alignItems: "center", gap: isMobile ? 10 : 32 }}>
        {isMobile ? (
          <button onClick={() => setNavOpen(!navOpen)}
                  aria-label={isHe ? "תפריט" : "Menu"}
                  aria-expanded={navOpen}
                  style={{
                    display: "inline-flex", alignItems: "center", gap: 10,
                    background: "none", border: "none", cursor: "pointer",
                    fontFamily: "'Heebo',sans-serif",
                    fontSize: 14, color: "#fff",
                    padding: "8px 4px",
                    letterSpacing: "0.06em",
                  }}>
            <span aria-hidden style={{
              position: "relative", width: 22, height: 14,
              display: "inline-block",
            }}>
              <span style={{
                position: "absolute", left: 0, right: 0, top: navOpen ? 6 : 0,
                height: 1.5, background: "currentColor", borderRadius: 1,
                transform: navOpen ? "rotate(45deg)" : "none",
                transition: "all 0.32s var(--laad-ease)",
              }} />
              <span style={{
                position: "absolute", left: 0, right: 0, top: 6,
                height: 1.5, background: "currentColor", borderRadius: 1,
                opacity: navOpen ? 0 : 1,
                transition: "opacity 0.2s var(--laad-ease)",
              }} />
              <span style={{
                position: "absolute", left: 0, right: navOpen ? 0 : 8, top: navOpen ? 6 : 12,
                height: 1.5, background: "currentColor", borderRadius: 1,
                transform: navOpen ? "rotate(-45deg)" : "none",
                transition: "all 0.32s var(--laad-ease)",
              }} />
            </span>
          </button>
        ) : (
          <nav style={{ display: "flex", alignItems: "center", gap: 30 }}>
            {navItems.map(item => (
              <a key={item.href} href={item.href}
                 className="laad-underline"
                 style={{
                   fontFamily: "'Heebo','Inter',sans-serif",
                   fontSize: 13.5,
                   fontWeight: 400,
                   letterSpacing: isHe ? "0.04em" : "0.14em",
                   textTransform: isHe ? "none" : "uppercase",
                   color: "#fff",
                   padding: "6px 0",
                 }}>{item.label}</a>
            ))}
          </nav>
        )}
      </div>

      {/* CENTER: gold logo */}
      <a href="#/" aria-label={isHe ? "לעד — דף הבית" : "Laad — Home"}
         style={{
           display: "flex", justifyContent: "center", alignItems: "center",
           height: logoH,
           transition: "height 0.45s var(--laad-ease)",
         }}>
        <img src="assets/laad-logo.png" alt={isHe ? "לעד" : "Laad"}
             decoding="async" fetchpriority="high"
             style={{
               height: logoH,
               width: "auto",
               objectFit: "contain",
               display: "block",
               filter: isSolid
                 ? "drop-shadow(0 4px 14px rgba(201,161,74,0.25))"
                 : "drop-shadow(0 6px 18px rgba(0,0,0,0.45))",
               transition: "all 0.45s var(--laad-ease)",
             }} />
      </a>

      {/* LEFT in RTL: contact */}
      <div style={{
        display: "flex", alignItems: "center", gap: isMobile ? 10 : 18,
        justifyContent: "flex-end",
      }}>
        {!isMobile ? (
          <a href="tel:0535351629"
             className="laad-underline"
             style={{
               display: "inline-flex", alignItems: "center", gap: 8,
               color: "#fff", textDecoration: "none",
               fontFamily: "'Inter','Heebo',sans-serif", fontSize: 13.5,
               fontVariantNumeric: "tabular-nums",
               letterSpacing: "0.06em",
               direction: "ltr",
             }}>
            <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                 strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
              <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z" />
            </svg>
            <span>053-535-1629</span>
          </a>
        ) : (
          <a href="tel:0535351629"
             aria-label={isHe ? "התקשר 053-535-1629" : "Call 053-535-1629"}
             style={{
               width: 38, height: 38, borderRadius: "50%",
               background: "rgba(255,255,255,0.08)",
               border: "1px solid rgba(230,207,149,0.45)",
               color: "#e6cf95",
               display: "inline-flex", alignItems: "center", justifyContent: "center",
               flexShrink: 0,
               textDecoration: "none",
               transition: "all 0.32s var(--laad-ease)",
             }}>
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor"
                 strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" aria-hidden="true">
              <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z" />
            </svg>
          </a>
        )}
        <a href="https://wa.me/972546267683" target="_blank" rel="noopener"
           aria-label="WhatsApp"
           style={{
             width: 38, height: 38, borderRadius: "50%",
             background: "var(--laad-gold-shine)",
             backgroundSize: "200% 100%",
             color: "#1a1410",
             display: "inline-flex", alignItems: "center", justifyContent: "center",
             flexShrink: 0,
             boxShadow: "0 6px 18px rgba(201,161,74,0.42)",
             transition: "all 0.32s var(--laad-ease)",
           }}
           onMouseEnter={e => { e.currentTarget.style.transform = "translateY(-2px) scale(1.06)"; e.currentTarget.style.backgroundPosition = "100% 50%"; }}
           onMouseLeave={e => { e.currentTarget.style.transform = "none"; e.currentTarget.style.backgroundPosition = "0% 50%"; }}
           >
          <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
            <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 0 1-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 0 1-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 0 1 2.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0 0 12.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 0 0 5.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0 0 20.464 3.488"/>
          </svg>
        </a>
      </div>
    </header>
  );
};

const MobileMenu = ({ c, lang, onClose }) => {
  const isHe = lang === "he";
  const items = [
    { href: "#products", label: isHe ? "הקולקציה"    : "Collection" },
    { href: "#story",    label: isHe ? "הסיפור"      : "Story"      },
    { href: "#custom",   label: isHe ? "תהליך ההזמנה" : "Process"    },
    { href: "#contact",  label: isHe ? "צור קשר"     : "Contact"    },
  ];

  return (
    <div style={{
      position: "fixed", inset: 0, zIndex: 45,
      background: "linear-gradient(180deg, #0a0a0a 0%, #1a1410 100%)",
      padding: "92px 24px 32px",
      display: "flex", flexDirection: "column", gap: 4,
      animation: "laadFadeIn 0.32s var(--laad-ease)",
      direction: c.dir,
    }}>
      {items.map((item, i) => (
        <a key={i} href={item.href} onClick={onClose}
           style={{
             fontSize: 32,
             fontWeight: 400,
             color: "#fff",
             textDecoration: "none",
             padding: "20px 0",
             borderBottom: "1px solid rgba(230,207,149,0.14)",
             fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Cormorant Garamond', serif",
             opacity: 0,
             animation: `laadFadeUp 0.5s var(--laad-ease-out) ${0.08 + i * 0.07}s forwards`,
             letterSpacing: "-0.01em",
           }}>{item.label}</a>
      ))}

      <div style={{ marginTop: 32, display: "flex", flexDirection: "column", gap: 14, opacity: 0, animation: "laadFadeUp 0.5s var(--laad-ease-out) 0.45s forwards" }}>
        <a href="https://wa.me/972546267683" target="_blank" rel="noopener" onClick={onClose}
           className="laad-btn laad-btn--gold"
           style={{ alignSelf: "flex-start" }}>
          <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
            <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347"/>
          </svg>
          <span>{isHe ? "וואטסאפ — 054-626-7683" : "WhatsApp"}</span>
        </a>
        <a href="tel:0535351629" onClick={onClose}
           className="laad-btn laad-btn--ghost"
           style={{ alignSelf: "flex-start", direction: "ltr" }}>
          <span>053-535-1629</span>
        </a>
      </div>
    </div>
  );
};

window.LaadSite = LaadSite;
window.LaadSite_TopBar = TopBar;
window.LaadSite_MobileMenu = MobileMenu;
