// Story (process steps), Custom (process), Contact — premium luxury sections.

// ─── STORY ───────────────────────────────────────────────────
const Story = ({ c, isMobile, lang }) => {
  const isHe = lang === "he";
  return (
    <section id="story" style={{
      background: "var(--bg)",
      padding: isMobile ? "80px 20px" : "144px 56px",
      direction: c.dir,
      position: "relative",
      overflow: "hidden",
    }}>
      {/* Decorative gold blob */}
      <div aria-hidden style={{
        position: "absolute",
        top: -120, [isHe ? "left" : "right"]: -160,
        width: 460, height: 460,
        borderRadius: "50%",
        background: "radial-gradient(circle, rgba(201,161,74,0.10) 0%, rgba(201,161,74,0) 65%)",
        pointerEvents: "none",
      }} />

      <div style={{
        maxWidth: "var(--laad-max)",
        margin: "0 auto",
        display: "grid",
        gridTemplateColumns: isMobile ? "1fr" : "1.1fr 1fr",
        gap: isMobile ? 48 : 96,
        alignItems: "center",
        position: "relative",
      }}>
        <div>
          <div style={{
            display: "inline-flex", alignItems: "center", gap: 12,
            marginBottom: 22,
          }}>
            <span aria-hidden style={{
              width: 32, height: 1,
              background: "var(--laad-gold)",
            }} />
            <span style={{
              fontFamily: "'Inter',sans-serif",
              fontSize: 11,
              letterSpacing: "0.36em",
              textTransform: "uppercase",
              color: "var(--laad-gold-deep)",
              fontWeight: 500,
            }}>{c.story.kicker}</span>
          </div>

          <h2 style={{
            fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Playfair Display', serif",
            fontSize: isMobile ? "clamp(34px, 8.4vw, 48px)" : "clamp(44px, 4vw, 68px)",
            lineHeight: 1.05,
            fontWeight: 500,
            letterSpacing: "-0.02em",
            margin: 0,
            color: "var(--ink)",
          }}>{c.story.title}</h2>

          <p style={{
            fontFamily: "'Heebo','Inter',sans-serif",
            fontSize: isMobile ? 16 : 17.5,
            lineHeight: 1.8,
            fontWeight: 300,
            color: "var(--ink-2)",
            margin: "32px 0 0",
            maxWidth: 560,
            whiteSpace: "pre-line",
          }}>{c.story.body}</p>
        </div>
      </div>
    </section>
  );
};

// ─── CUSTOM / PROCESS — interactive configurator ─────────────
const SWATCH_TEX = {
  carbon: "repeating-linear-gradient(45deg, #1c1c1c 0 4px, #0a0a0a 4px 8px)",
  metal:  "linear-gradient(135deg, #d9d9d9 0%, #f4f4f4 45%, #9a9a9a 100%)",
  mix:    "linear-gradient(135deg, #f0f0f0 0%, #f0f0f0 49%, #1c1c1c 51%, #1c1c1c 100%)",
};

const Swatch = ({ opt, active, size = 44 }) => {
  const ring = active ? "0 0 0 2px rgba(230,207,149,0.85)" : "0 0 0 1px rgba(255,255,255,0.14)";
  const base = {
    width: size, height: size, borderRadius: "50%",
    boxShadow: ring,
    transition: "box-shadow 0.32s var(--laad-ease), transform 0.32s var(--laad-ease)",
    transform: active ? "scale(1.04)" : "scale(1)",
    flexShrink: 0,
  };
  if (opt.kind === "color") {
    return <span aria-hidden style={{ ...base, background: opt.color }} />;
  }
  if (opt.kind === "tex") {
    return <span aria-hidden style={{ ...base, backgroundImage: SWATCH_TEX[opt.tex] || SWATCH_TEX.metal }} />;
  }
  return (
    <span aria-hidden style={{
      ...base,
      background: "linear-gradient(180deg, rgba(255,255,255,0.04), rgba(255,255,255,0.01))",
      display: "inline-flex", alignItems: "center", justifyContent: "center",
      color: "var(--laad-gold)", fontSize: size * 0.46, lineHeight: 1,
    }}>{opt.glyph}</span>
  );
};

const Custom = ({ c, isMobile, lang }) => {
  const isHe = lang === "he";
  const steps = c.custom.steps;
  const [active, setActive] = React.useState(0);
  const [userTouched, setUserTouched] = React.useState(false);
  const sectionRef = React.useRef(null);

  React.useEffect(() => {
    if (userTouched) return;
    if (typeof IntersectionObserver === "undefined") return;
    let timer = null;
    const obs = new IntersectionObserver((entries) => {
      entries.forEach(e => {
        if (e.isIntersecting && !timer) {
          timer = setInterval(() => setActive(a => (a + 1) % steps.length), 4500);
        } else if (!e.isIntersecting && timer) {
          clearInterval(timer); timer = null;
        }
      });
    }, { threshold: 0.35 });
    if (sectionRef.current) obs.observe(sectionRef.current);
    return () => { if (timer) clearInterval(timer); obs.disconnect(); };
  }, [steps.length, userTouched]);

  const pick = (i) => { setUserTouched(true); setActive(i); };
  const step = steps[active];

  return (
    <section ref={sectionRef} id="custom" style={{
      background: "linear-gradient(180deg, #0a0a0a 0%, #14110d 100%)",
      color: "#f6f3ec",
      padding: isMobile ? "88px 20px" : "160px 56px",
      direction: c.dir,
      position: "relative",
      overflow: "hidden",
    }}>
      <div aria-hidden style={{
        position: "absolute",
        top: "8%", [isHe ? "left" : "right"]: "-14%",
        width: 580, height: 580, borderRadius: "50%",
        border: "1px solid rgba(230,207,149,0.10)",
        pointerEvents: "none",
      }} />
      <div aria-hidden style={{
        position: "absolute",
        bottom: "-18%", [isHe ? "right" : "left"]: "-10%",
        width: 460, height: 460, borderRadius: "50%",
        background: "radial-gradient(circle, rgba(201,161,74,0.10) 0%, rgba(201,161,74,0) 70%)",
        pointerEvents: "none",
      }} />

      <div style={{ maxWidth: "var(--laad-max)", margin: "0 auto", position: "relative" }}>
        <div style={{ maxWidth: 720, marginBottom: isMobile ? 48 : 72 }}>
          <div style={{ display: "inline-flex", alignItems: "center", gap: 12, marginBottom: 22 }}>
            <span aria-hidden style={{ width: 32, height: 1, background: "var(--laad-gold)" }} />
            <span style={{
              fontFamily: "'Inter',sans-serif",
              fontSize: 11, letterSpacing: "0.36em",
              textTransform: "uppercase",
              color: "var(--laad-gold-soft)", fontWeight: 500,
            }}>{c.custom.kicker}</span>
          </div>

          <h2 style={{
            fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Playfair Display', serif",
            fontSize: isMobile ? "clamp(34px, 9vw, 48px)" : "clamp(48px, 4.4vw, 72px)",
            lineHeight: 1.04, fontWeight: 500, letterSpacing: "-0.02em",
            margin: 0, color: "#fff",
          }}>{c.custom.title}</h2>

          <p style={{
            fontFamily: "'Heebo','Inter',sans-serif",
            fontSize: isMobile ? 16 : 18, lineHeight: 1.75, fontWeight: 300,
            color: "rgba(255,255,255,0.7)",
            margin: "28px 0 0", maxWidth: 560,
          }}>{c.custom.body}</p>
        </div>

        <div style={{
          display: "grid",
          gridTemplateColumns: isMobile ? "1fr" : "minmax(260px, 340px) 1fr",
          gap: isMobile ? 32 : 64,
          alignItems: "stretch",
          padding: isMobile ? "28px 0 0" : "8px 0 0",
        }}>
          {/* Step rail */}
          <div style={{ position: "relative" }}>
            <div aria-hidden style={{
              position: "absolute",
              [isHe ? "right" : "left"]: 22,
              top: 22, bottom: 22, width: 1,
              background: "linear-gradient(180deg, rgba(230,207,149,0.35), rgba(230,207,149,0.06))",
            }} />
            <ol style={{ listStyle: "none", margin: 0, padding: 0, display: "grid", gap: isMobile ? 6 : 10 }}>
              {steps.map((s, i) => {
                const on = i === active;
                return (
                  <li key={i}>
                    <button
                      type="button"
                      onClick={() => pick(i)}
                      aria-current={on ? "step" : undefined}
                      style={{
                        all: "unset", cursor: "pointer", display: "flex", alignItems: "center",
                        gap: 18, width: "100%",
                        padding: "14px 14px",
                        borderRadius: 12,
                        background: on ? "rgba(230,207,149,0.06)" : "transparent",
                        transition: "background 0.32s var(--laad-ease)",
                      }}>
                      <span aria-hidden style={{
                        width: 44, height: 44, borderRadius: "50%",
                        display: "inline-flex", alignItems: "center", justifyContent: "center",
                        fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Playfair Display', serif",
                        fontSize: 16, fontVariantNumeric: "tabular-nums",
                        background: on ? "var(--laad-gold-shine)" : "#0a0a0a",
                        color: on ? "#0a0a0a" : "var(--laad-gold-soft)",
                        border: on ? "1px solid transparent" : "1px solid rgba(230,207,149,0.30)",
                        transition: "all 0.32s var(--laad-ease)",
                        flexShrink: 0,
                        boxShadow: on ? "0 0 0 4px rgba(230,207,149,0.10)" : "none",
                      }}>{s.num}</span>
                      <span style={{
                        fontFamily: "'Heebo','Inter',sans-serif",
                        fontSize: isMobile ? 17 : 18,
                        fontWeight: on ? 500 : 400,
                        color: on ? "#fff" : "rgba(255,255,255,0.55)",
                        letterSpacing: "-0.005em",
                        transition: "color 0.32s var(--laad-ease)",
                      }}>{s.title}</span>
                    </button>
                  </li>
                );
              })}
            </ol>
          </div>

          {/* Preview panel */}
          <div style={{
            position: "relative",
            padding: isMobile ? "32px 24px" : "44px 48px",
            background: "linear-gradient(180deg, rgba(255,255,255,0.025), rgba(255,255,255,0.01))",
            border: "1px solid rgba(255,255,255,0.08)",
            borderRadius: 20,
            backdropFilter: "blur(8px)",
            overflow: "hidden",
            minHeight: isMobile ? "auto" : 360,
          }}>
            <div aria-hidden style={{
              position: "absolute", top: 0, left: 24, right: 24, height: 1,
              background: "linear-gradient(90deg, transparent, var(--laad-gold), transparent)",
              opacity: 0.8,
            }} />
            <div key={active} style={{
              animation: "laadFadeUp 0.5s var(--laad-ease-out) both",
            }}>
              <div style={{
                fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Playfair Display', serif",
                fontSize: isMobile ? 56 : 84,
                fontWeight: 400,
                color: "transparent",
                backgroundImage: "var(--laad-gold-shine)",
                WebkitBackgroundClip: "text", backgroundClip: "text",
                fontVariantNumeric: "tabular-nums",
                letterSpacing: "0.02em",
                lineHeight: 1, margin: 0,
              }}>{step.num}</div>

              <h3 style={{
                fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Playfair Display', serif",
                fontSize: isMobile ? 28 : 36,
                fontWeight: 500,
                margin: "12px 0 14px",
                color: "#fff",
                letterSpacing: "-0.01em",
              }}>{step.title}</h3>

              <p style={{
                fontFamily: "'Heebo','Inter',sans-serif",
                fontSize: isMobile ? 15.5 : 16.5,
                lineHeight: 1.75, fontWeight: 300,
                color: "rgba(255,255,255,0.72)",
                margin: 0, maxWidth: 560,
              }}>{step.body}</p>

              <div style={{ marginTop: isMobile ? 28 : 36 }}>
                <div style={{
                  fontFamily: "'Inter',sans-serif",
                  fontSize: 10, letterSpacing: "0.32em",
                  textTransform: "uppercase",
                  color: "var(--laad-gold-soft)",
                  fontWeight: 500,
                  marginBottom: 16,
                }}>{c.custom.optionsLabel}</div>
                <div style={{
                  display: "flex", flexWrap: "wrap",
                  gap: isMobile ? 16 : 22,
                }}>
                  {step.options.map((opt, i) => (
                    <div key={i} style={{
                      display: "flex", flexDirection: "column", alignItems: "center",
                      gap: 10, minWidth: 60,
                    }}>
                      <Swatch opt={opt} active size={isMobile ? 40 : 46} />
                      <span style={{
                        fontFamily: "'Heebo','Inter',sans-serif",
                        fontSize: 12.5,
                        color: "rgba(255,255,255,0.78)",
                        letterSpacing: "0.01em",
                        textAlign: "center",
                      }}>{opt.label}</span>
                    </div>
                  ))}
                </div>
              </div>
            </div>
          </div>
        </div>

        <div style={{ marginTop: isMobile ? 48 : 72, display: "flex", gap: 16, flexWrap: "wrap", alignItems: "center" }}>
          <a href="#contact" className="laad-btn laad-btn--gold">
            <span>{c.custom.cta}</span>
            <span aria-hidden style={{
              display: "inline-block",
              transform: isHe ? "scaleX(-1)" : "none",
              fontSize: 18, lineHeight: 1,
            }}>›</span>
          </a>
          <a href={isHe ? "seo/pages/custom-torah-case.html" : "seo/pages/en/custom-torah-case-israel.html"}
             className="laad-underline"
             style={{
               fontFamily: "'Heebo','Inter',sans-serif",
               fontSize: 14,
               color: "rgba(255,255,255,0.7)",
               letterSpacing: "0.04em",
             }}>
            {isHe ? "קראו עוד על תיק בהתאמה אישית →" : "Read about custom Torah cases →"}
          </a>
        </div>
      </div>
    </section>
  );
};

// ─── CONTACT ─────────────────────────────────────────────────
const Contact = ({ c, isMobile, lang }) => {
  const isHe = lang === "he";
  const [focusKey, setFocusKey] = React.useState(null);

  const inputBase = {
    width: "100%",
    padding: "16px 18px",
    background: "var(--bg-2)",
    border: "1px solid var(--rule)",
    borderRadius: 10,
    fontFamily: "'Heebo','Inter',sans-serif",
    fontSize: 15,
    color: "var(--ink)",
    outline: "none",
    direction: c.dir,
    transition: "border-color 0.32s var(--laad-ease), box-shadow 0.32s var(--laad-ease), background 0.32s var(--laad-ease)",
  };
  const focusStyle = (key) => focusKey === key ? {
    borderColor: "var(--laad-gold)",
    boxShadow: "0 0 0 4px rgba(201,161,74,0.12)",
    background: "#fff",
  } : {};

  return (
    <section id="contact" style={{
      background: "var(--bg)",
      padding: isMobile ? "80px 20px" : "144px 56px",
      direction: c.dir,
    }}>
      <div style={{
        maxWidth: "var(--laad-max)",
        margin: "0 auto",
        display: "grid",
        gridTemplateColumns: isMobile ? "1fr" : "1fr 1.1fr",
        gap: isMobile ? 48 : 96,
      }}>
        <div>
          <div style={{
            display: "inline-flex", alignItems: "center", gap: 12,
            marginBottom: 22,
          }}>
            <span aria-hidden style={{ width: 32, height: 1, background: "var(--laad-gold)" }} />
            <span style={{
              fontFamily: "'Inter',sans-serif",
              fontSize: 11,
              letterSpacing: "0.36em",
              textTransform: "uppercase",
              color: "var(--laad-gold-deep)",
              fontWeight: 500,
            }}>{c.contact.kicker}</span>
          </div>

          <h2 style={{
            fontFamily: isHe ? "'Frank Ruhl Libre', serif" : "'Playfair Display', serif",
            fontSize: isMobile ? "clamp(34px, 9vw, 48px)" : "clamp(44px, 4vw, 64px)",
            lineHeight: 1.05,
            fontWeight: 500,
            letterSpacing: "-0.02em",
            margin: 0,
            whiteSpace: "pre-line",
            color: "var(--ink)",
          }}>{c.contact.title}</h2>

          <p style={{
            fontFamily: "'Heebo','Inter',sans-serif",
            fontSize: isMobile ? 16 : 17.5,
            lineHeight: 1.75,
            fontWeight: 300,
            color: "var(--ink-2)",
            margin: "28px 0 40px",
            maxWidth: 480,
          }}>{c.contact.body}</p>

          <div style={{ display: "flex", flexDirection: "column", gap: 14 }}>
            <a href="https://wa.me/972546267683" target="_blank" rel="noopener"
               className="laad-btn laad-btn--gold"
               style={{ alignSelf: "flex-start" }}>
              <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-.347"/>
              </svg>
              <span>{c.contact.whatsapp}</span>
              <span style={{ direction: "ltr", opacity: 0.85, fontVariantNumeric: "tabular-nums" }}>
                {c.contact.whatsappNum}
              </span>
            </a>

            <a href="tel:0535351629"
               className="laad-btn"
               style={{
                 alignSelf: "flex-start",
                 background: "transparent",
                 color: "var(--ink)",
                 border: "1px solid var(--rule)",
               }}
               onMouseEnter={e => { e.currentTarget.style.borderColor = "var(--laad-gold)"; e.currentTarget.style.color = "var(--laad-gold-deep)"; e.currentTarget.style.transform = "translateY(-2px)"; }}
               onMouseLeave={e => { e.currentTarget.style.borderColor = "var(--rule)"; e.currentTarget.style.color = "var(--ink)"; e.currentTarget.style.transform = "none"; }}>
              <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>
              <span>{c.contact.phone}</span>
              <span style={{ direction: "ltr", opacity: 0.85, fontVariantNumeric: "tabular-nums" }}>
                {c.contact.phoneNum}
              </span>
            </a>
          </div>
        </div>

        <form onSubmit={(e) => { e.preventDefault(); alert(isHe ? "תודה! נחזור אליכם בקרוב." : "Thanks! We'll be in touch."); }}
              style={{
                background: "var(--bg-2)",
                border: "1px solid var(--rule)",
                borderRadius: 18,
                padding: isMobile ? 24 : 40,
                display: "flex",
                flexDirection: "column",
                gap: 16,
                boxShadow: "0 24px 60px rgba(10,10,10,0.06)",
                position: "relative",
              }}>
          {/* Top gold rule */}
          <div aria-hidden style={{
            position: "absolute", top: 0, left: 24, right: 24,
            height: 1,
            background: "linear-gradient(90deg, transparent, var(--laad-gold-soft), transparent)",
          }} />

          <div style={{
            fontFamily: "'Inter',sans-serif",
            fontSize: 11, letterSpacing: "0.32em", textTransform: "uppercase",
            color: "var(--laad-gold-deep)",
            marginBottom: 4,
          }}>{isHe ? "השאירו פרטים" : "Send a message"}</div>

          <div style={{ display: "grid", gridTemplateColumns: isMobile ? "1fr" : "1fr 1fr", gap: 14 }}>
            <input required placeholder={c.contact.formName}
                   onFocus={() => setFocusKey("n")} onBlur={() => setFocusKey(null)}
                   style={{ ...inputBase, ...focusStyle("n") }} />
            <input required placeholder={c.contact.formPhone} type="tel"
                   onFocus={() => setFocusKey("p")} onBlur={() => setFocusKey(null)}
                   style={{ ...inputBase, ...focusStyle("p") }} />
          </div>
          <input placeholder={c.contact.formEmail} type="email"
                 onFocus={() => setFocusKey("e")} onBlur={() => setFocusKey(null)}
                 style={{ ...inputBase, ...focusStyle("e") }} />
          <textarea required placeholder={c.contact.formMsg} rows={5}
                    onFocus={() => setFocusKey("m")} onBlur={() => setFocusKey(null)}
                    style={{ ...inputBase, ...focusStyle("m"), resize: "vertical" }} />
          <button type="submit"
                  className="laad-btn laad-btn--primary"
                  style={{ alignSelf: "flex-start", marginTop: 6 }}>
            <span>{c.contact.formSubmit}</span>
            <span aria-hidden style={{
              display: "inline-block",
              transform: isHe ? "scaleX(-1)" : "none",
              fontSize: 16, lineHeight: 1,
            }}>›</span>
          </button>
        </form>
      </div>
    </section>
  );
};

window.Laad_Story   = Story;
window.Laad_Custom  = Custom;
window.Laad_Contact = Contact;
