// components.jsx — small UI primitives for Lumera

const Eyebrow = ({ children, style, color }) => (
  <div style={{
    fontFamily: "var(--font-sans)",
    fontSize: "11px",
    letterSpacing: "0.22em",
    textTransform: "uppercase",
    color: color || "var(--ink-3)",
    fontWeight: 400,
    ...style,
  }}>{children}</div>
);

const Rule = ({ tick = true, margin = "var(--s-7) auto", width = "100%", maxWidth = "100%", color = "var(--line)" }) => (
  <div style={{
    position: "relative",
    height: "1px",
    background: color,
    margin,
    width,
    maxWidth,
  }}>
    {tick && <span style={{
      position: "absolute",
      left: "50%",
      top: "-2.5px",
      width: "6px",
      height: "6px",
      background: "var(--champagne)",
      transform: "translateX(-50%) rotate(45deg)",
    }} />}
  </div>
);

const Button = ({ children, variant = "primary", onClick, style, full, type = "button" }) => {
  const base = {
    fontFamily: "var(--font-sans)",
    fontSize: "12px",
    fontWeight: 500,
    letterSpacing: "0.18em",
    textTransform: "uppercase",
    padding: "16px 28px",
    border: "1px solid var(--ink)",
    background: "var(--ink)",
    color: "var(--paper)",
    cursor: "pointer",
    transition: "opacity 180ms var(--ease-quiet), background 180ms var(--ease-quiet)",
    width: full ? "100%" : "auto",
  };
  const ghost = { ...base, background: "transparent", color: "var(--ink)" };
  const quiet = {
    fontFamily: "var(--font-sans)",
    fontSize: "11px",
    fontWeight: 400,
    letterSpacing: "0.18em",
    textTransform: "uppercase",
    padding: "6px 0",
    border: 0,
    borderBottom: "1px solid var(--ink)",
    background: "transparent",
    color: "var(--ink)",
    cursor: "pointer",
  };
  const onDark = { ...base, background: "var(--paper)", color: "var(--ink)", border: "1px solid var(--paper)" };
  const ghostOnDark = { ...ghost, color: "var(--paper)", border: "1px solid var(--paper)" };
  const quietOnDark = { ...quiet, color: "var(--paper)", borderBottomColor: "var(--paper)" };
  const map = { primary: base, ghost, quiet, onDark, ghostOnDark, quietOnDark };
  const s = { ...map[variant], ...style };
  return (
    <button type={type} style={s}
      onClick={onClick}
      onMouseOver={e => e.currentTarget.style.opacity = "0.72"}
      onMouseOut={e => e.currentTarget.style.opacity = "1"}>
      {children}
    </button>
  );
};

const ArrowLink = ({ children, onClick, style, color }) => (
  <button onClick={onClick} className="lumera-arrow"
    style={{
      fontFamily: "var(--font-sans)",
      fontSize: "12px",
      fontWeight: 400,
      letterSpacing: "0.18em",
      textTransform: "uppercase",
      padding: "6px 0",
      border: 0,
      background: "transparent",
      color: color || "var(--ink)",
      cursor: "pointer",
      display: "inline-flex",
      alignItems: "baseline",
      gap: "12px",
      ...style,
    }}>
    {children}
    <span style={{ transition: "transform 320ms var(--ease-quiet)", display: "inline-block" }}
      className="lumera-arrow-glyph">→</span>
  </button>
);

// inject hover style once
if (typeof document !== "undefined" && !document.getElementById("lumera-arrow-style")) {
  const s = document.createElement("style");
  s.id = "lumera-arrow-style";
  s.textContent = ".lumera-arrow:hover .lumera-arrow-glyph { transform: translateX(6px); }";
  document.head.appendChild(s);
}

const Nav = ({ onNav, onOpenBag, bagCount = 0, scrolled = false, current = "home" }) => {
  const linkStyle = (active) => ({
    fontFamily: "var(--font-sans)",
    fontSize: "11px",
    letterSpacing: "0.22em",
    textTransform: "uppercase",
    color: active ? "var(--champagne-d)" : "var(--ink)",
    cursor: "pointer",
    background: "transparent",
    border: 0,
    padding: "8px 0",
    transition: "color 180ms var(--ease-quiet)",
  });
  const hover = e => e.currentTarget.style.color = "var(--champagne-d)";
  const out   = (active) => e => e.currentTarget.style.color = active ? "var(--champagne-d)" : "var(--ink)";

  return (
    <header style={{
      position: "sticky", top: 0, zIndex: 50,
      background: scrolled ? "rgba(242, 235, 221, 0.78)" : "var(--bone)",
      backdropFilter: scrolled ? "blur(16px)" : "none",
      WebkitBackdropFilter: scrolled ? "blur(16px)" : "none",
      borderBottom: "0.5px solid var(--line)",
      transition: "background 320ms var(--ease-quiet)",
    }}>
      <div style={{
        display: "grid",
        gridTemplateColumns: "1fr auto 1fr",
        alignItems: "center",
        padding: "20px 32px",
        maxWidth: "1440px",
        margin: "0 auto",
      }}>
        <nav style={{ display: "flex", gap: "32px", alignItems: "center" }}>
          <button style={linkStyle(current === "shop")} onMouseOver={hover} onMouseOut={out(current === "shop")} onClick={() => onNav("shop")}>Shop</button>
          <button style={linkStyle(false)} onMouseOver={hover} onMouseOut={out(false)} onClick={() => onNav("journal")}>Journal</button>
          <button style={linkStyle(false)} onMouseOver={hover} onMouseOut={out(false)} onClick={() => onNav("house")}>The House</button>
        </nav>
        <button onClick={() => onNav("home")}
          style={{
            fontFamily: "var(--font-display)",
            fontWeight: 400,
            fontSize: "26px",
            letterSpacing: "0.42em",
            color: "var(--ink)",
            textAlign: "center",
            paddingLeft: "0.42em",
            background: "transparent",
            border: 0,
            cursor: "pointer",
          }}>LUMERA</button>
        <div style={{ display: "flex", gap: "28px", alignItems: "center", justifyContent: "flex-end" }}>
          <button style={linkStyle(false)} onMouseOver={hover} onMouseOut={out(false)}>
            <i data-lucide="search" style={{ width: 16, height: 16, strokeWidth: 1.25 }}></i>
          </button>
          <button style={linkStyle(false)} onMouseOver={hover} onMouseOut={out(false)}>Account</button>
          <button style={{ ...linkStyle(false), display: "inline-flex", alignItems: "center", gap: 10 }} onMouseOver={hover} onMouseOut={out(false)} onClick={onOpenBag}>
            <i data-lucide="shopping-bag" style={{ width: 16, height: 16, strokeWidth: 1.25 }}></i>
            Bag <span style={{ color: "var(--ink-3)" }}>({bagCount})</span>
          </button>
        </div>
      </div>
    </header>
  );
};

const Footer = ({ onNav }) => {
  const colTitle = { fontFamily: "var(--font-sans)", fontSize: "10px", fontWeight: 400, letterSpacing: "0.22em", textTransform: "uppercase", color: "#BFB5A3", margin: "0 0 22px" };
  const liStyle  = { fontFamily: "var(--font-sans)", fontSize: "13px", letterSpacing: "0.02em", color: "var(--paper)", padding: "6px 0", cursor: "pointer", listStyle: "none", opacity: 0.92 };

  return (
    <footer style={{ background: "var(--noir)", color: "var(--paper)", padding: "var(--s-10) var(--s-7) var(--s-6)" }}>
      <div style={{ maxWidth: "1280px", margin: "0 auto" }}>
        {/* trust strip */}
        <div style={{
          display: "grid", gridTemplateColumns: "repeat(4, 1fr)", gap: "var(--s-6)",
          paddingBottom: "var(--s-9)", borderBottom: "0.5px solid #3A332A", marginBottom: "var(--s-9)",
        }}>
          {[
            ["Complimentary delivery", "Worldwide, on orders over $200. Carbon-neutral."],
            ["Thirty-day return", "No questions. Refilled, recycled, or returned."],
            ["Concierge", "A real person, within twenty-four hours."],
            ["Every concentration disclosed", "Independent clinical studies on request."],
          ].map(([t, d]) => (
            <div key={t}>
              <div style={{ fontFamily: "var(--font-display)", fontWeight: 400, fontSize: "20px", color: "var(--paper)", letterSpacing: "-0.005em" }}>{t}</div>
              <p style={{ fontFamily: "var(--font-sans)", fontSize: "12px", lineHeight: 1.6, color: "#BFB5A3", margin: "10px 0 0", maxWidth: "26ch" }}>{d}</p>
            </div>
          ))}
        </div>

        <div style={{ display: "grid", gridTemplateColumns: "1.6fr 1fr 1fr 1fr 1.4fr", gap: "48px", marginBottom: "var(--s-9)" }}>
          <div>
            <div style={{ fontFamily: "var(--font-display)", fontSize: "32px", letterSpacing: "0.42em", paddingLeft: "0.42em", color: "var(--paper)", marginBottom: "22px" }}>LUMERA</div>
            <p style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300, fontSize: "17px", color: "#D9C49A", maxWidth: "26ch", lineHeight: 1.5, margin: 0 }}>
              Skin, returned to light. The atelier, Geneva.
            </p>
            <div style={{ display: "flex", gap: 18, marginTop: "var(--s-6)", color: "#BFB5A3", fontFamily: "var(--font-sans)", fontSize: "10px", letterSpacing: "0.32em", textTransform: "uppercase" }}>
              <span style={{ cursor: "pointer" }}>Instagram</span>
              <span style={{ color: "#3A332A" }}>·</span>
              <span style={{ cursor: "pointer" }}>Pinterest</span>
            </div>
          </div>
          <div>
            <h6 style={colTitle}>The House</h6>
            <ul style={{ padding: 0, margin: 0 }}>
              <li style={liStyle}>The Philosophy</li>
              <li style={liStyle}>The Science</li>
              <li style={liStyle}>Sourcing</li>
              <li style={liStyle}>Sustainability</li>
              <li style={liStyle}>Press</li>
            </ul>
          </div>
          <div>
            <h6 style={colTitle}>The Collection</h6>
            <ul style={{ padding: 0, margin: 0 }}>
              <li style={liStyle} onClick={() => onNav && onNav("shop")}>All Products</li>
              <li style={liStyle}>The Ritual Set</li>
              <li style={liStyle}>Gift Cards</li>
              <li style={liStyle}>Concierge</li>
            </ul>
          </div>
          <div>
            <h6 style={colTitle}>Support</h6>
            <ul style={{ padding: 0, margin: 0 }}>
              <li style={liStyle}>Shipping & Returns</li>
              <li style={liStyle}>Track an Order</li>
              <li style={liStyle}>Contact</li>
              <li style={liStyle}>FAQ</li>
            </ul>
          </div>
          <div>
            <h6 style={colTitle}>The Lumera Letter</h6>
            <p style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300, fontSize: "14px", color: "#BFB5A3", margin: "0 0 18px", lineHeight: 1.5, maxWidth: "30ch" }}>
              A monthly correspondence on skin, ritual, and stillness. Sent once a month.
            </p>
            <div style={{ display: "flex", alignItems: "flex-end", borderBottom: "1px solid #6B6258", paddingBottom: 6 }}>
              <input placeholder="Your email address" style={{
                flex: 1, fontFamily: "var(--font-sans)", fontSize: "13px",
                background: "transparent", color: "var(--paper)", border: 0, outline: "none",
                padding: "8px 0",
              }} />
              <button style={{
                fontFamily: "var(--font-sans)", fontSize: "10px", fontWeight: 500,
                letterSpacing: "0.22em", textTransform: "uppercase",
                background: "transparent", color: "var(--paper)", border: 0,
                cursor: "pointer", padding: "8px 0 8px 16px",
              }}>Subscribe →</button>
            </div>
          </div>
        </div>

        {/* certifications strip */}
        <div style={{ paddingBottom: "var(--s-6)", borderBottom: "0.5px solid #3A332A", display: "flex", gap: "var(--s-7)", flexWrap: "wrap", alignItems: "center", justifyContent: "center" }}>
          {CERTIFICATIONS.map(c => (
            <span key={c} style={{ fontFamily: "var(--font-sans)", fontSize: "10px", letterSpacing: "0.22em", textTransform: "uppercase", color: "#9A9082" }}>{c}</span>
          ))}
        </div>

        <div style={{
          paddingTop: "var(--s-5)",
          display: "flex", justifyContent: "space-between", flexWrap: "wrap", gap: 16,
          fontFamily: "var(--font-sans)", fontSize: "10px",
          letterSpacing: "0.22em", textTransform: "uppercase", color: "#9A9082",
        }}>
          <span>© 2026 Lumera Geneva. All rights reserved.</span>
          <span>Privacy &nbsp;·&nbsp; Terms &nbsp;·&nbsp; Cookies &nbsp;·&nbsp; Accessibility</span>
        </div>

        <div style={{
          paddingTop: "var(--s-4)",
          fontFamily: "var(--font-sans)", fontSize: "10px",
          letterSpacing: "0.16em", textTransform: "uppercase",
          color: "#6B6258", textAlign: "center", lineHeight: 1.8,
        }}>
          This website is a demo. Lumera is a fictional brand — all products, claims, results, endorsements, testimonials and press mentions are entirely fictional.
        </div>
      </div>
    </footer>
  );
};

const ProductCard = ({ product, onClick, onAdd, compact = false }) => {
  const [hover, setHover] = React.useState(false);
  return (
    <div
      style={{ cursor: "pointer", display: "flex", flexDirection: "column" }}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      onClick={() => onClick && onClick(product)}>
      <div style={{
        position: "relative",
        aspectRatio: "4 / 5",
        background: "var(--linen)",
        borderRadius: "2px",
        overflow: "hidden",
        marginBottom: "var(--s-5)",
      }}>
        <img src={product.image} alt={product.name}
          style={{ width: "100%", height: "100%", objectFit: "cover", display: "block",
            transition: "transform 1200ms var(--ease-quiet), opacity 600ms var(--ease-quiet)",
            transform: hover ? "scale(1.025)" : "scale(1)" }} />
        {onAdd && (
          <button onClick={e => { e.stopPropagation(); onAdd(product); }}
            style={{
              position: "absolute",
              left: "50%", bottom: "20px",
              transform: `translateX(-50%) translateY(${hover ? "0" : "12px"})`,
              opacity: hover ? 1 : 0,
              transition: "opacity 320ms var(--ease-quiet), transform 320ms var(--ease-quiet)",
              fontFamily: "var(--font-sans)", fontSize: "10px",
              letterSpacing: "0.22em", textTransform: "uppercase",
              padding: "12px 22px",
              background: "var(--ink)", color: "var(--paper)",
              border: 0, cursor: "pointer",
              whiteSpace: "nowrap",
            }}>Add to ritual</button>
        )}
      </div>
      <Eyebrow style={{ marginBottom: 10 }}>{product.concern}</Eyebrow>
      <h3 style={{
        fontFamily: "var(--font-display)",
        fontWeight: 300, fontSize: compact ? "22px" : "28px",
        letterSpacing: "-0.015em",
        color: "var(--ink)", margin: 0, lineHeight: 1.15,
      }}>{product.name}</h3>
      <p style={{
        fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300,
        fontSize: "15px", color: "var(--ink-3)",
        margin: "10px 0 0", lineHeight: 1.5, maxWidth: "32ch",
      }}>{product.descriptor}</p>
      <div style={{
        display: "flex", justifyContent: "space-between",
        marginTop: "var(--s-5)", paddingTop: "var(--s-3)",
        borderTop: "0.5px solid var(--line)",
      }}>
        <span style={{ fontFamily: "var(--font-sans)", fontSize: "13px", letterSpacing: "0.06em", color: "var(--ink-2)" }}>${product.price}</span>
        <span style={{ fontFamily: "var(--font-sans)", fontSize: "10px", letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--ink-4)" }}>{product.size}</span>
      </div>
    </div>
  );
};

const JournalCard = ({ entry }) => (
  <article style={{ display: "flex", flexDirection: "column", gap: "var(--s-4)", cursor: "pointer" }}>
    <div style={{
      aspectRatio: "4 / 5",
      background: "var(--linen)",
      borderRadius: "2px",
      overflow: "hidden",
    }}>
      <img src={entry.image} alt="" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
    </div>
    <div>
      <Eyebrow>{entry.eyebrow}</Eyebrow>
      <h3 style={{
        fontFamily: "var(--font-display)", fontWeight: 300,
        fontSize: "30px", letterSpacing: "-0.015em",
        color: "var(--ink)", margin: "12px 0 10px", lineHeight: 1.15,
        textWrap: "balance",
      }}>{entry.title}</h3>
      <p style={{
        fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300,
        fontSize: "16px", color: "var(--ink-3)", margin: 0, lineHeight: 1.5, maxWidth: "36ch",
      }}>{entry.dek}</p>
      <div style={{
        fontFamily: "var(--font-sans)", fontSize: "11px",
        letterSpacing: "0.18em", textTransform: "uppercase",
        color: "var(--ink-4)", marginTop: 16,
      }}>The Lumera Letter &middot; {entry.date}</div>
    </div>
  </article>
);

const BagDrawer = ({ open, items, onClose, onRemove }) => {
  const subtotal = items.reduce((s, it) => s + it.product.price * it.qty, 0);
  return (
    <React.Fragment>
      <div onClick={onClose} style={{
        position: "fixed", inset: 0, zIndex: 100,
        background: "rgba(26, 22, 18, 0.42)",
        opacity: open ? 1 : 0,
        pointerEvents: open ? "auto" : "none",
        transition: "opacity 320ms var(--ease-quiet)",
      }} />
      <aside style={{
        position: "fixed", top: 0, right: 0, bottom: 0,
        width: "min(480px, 92vw)",
        background: "var(--noir)", color: "var(--paper)",
        zIndex: 101,
        transform: open ? "translateX(0)" : "translateX(100%)",
        transition: "transform 600ms var(--ease-quiet)",
        display: "flex", flexDirection: "column",
        boxShadow: "var(--shadow-modal)",
      }}>
        <div style={{
          display: "flex", justifyContent: "space-between", alignItems: "center",
          padding: "var(--s-6) var(--s-6) var(--s-5)",
          borderBottom: "0.5px solid #3A332A",
        }}>
          <div style={{ fontFamily: "var(--font-sans)", fontSize: "11px", letterSpacing: "0.22em", textTransform: "uppercase", color: "#D9C49A" }}>
            Your ritual
          </div>
          <button onClick={onClose} style={{ background: "transparent", border: 0, color: "var(--paper)", cursor: "pointer", padding: 4 }}>
            <i data-lucide="x" style={{ width: 18, height: 18, strokeWidth: 1.25 }}></i>
          </button>
        </div>
        <div style={{ flex: 1, overflowY: "auto", padding: "var(--s-6)" }}>
          {items.length === 0 ? (
            <div style={{ display: "flex", flexDirection: "column", alignItems: "flex-start", gap: 16, marginTop: "var(--s-6)" }}>
              <div style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300, fontSize: "22px", color: "#D9C49A", lineHeight: 1.4 }}>
                Your ritual is empty. Begin with a single product,<br/>or explore the full collection.
              </div>
            </div>
          ) : (
            <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: "var(--s-5)" }}>
              {items.map(({ product, qty }) => (
                <li key={product.id} style={{ display: "grid", gridTemplateColumns: "84px 1fr auto", gap: "var(--s-4)", alignItems: "flex-start", paddingBottom: "var(--s-5)", borderBottom: "0.5px solid #3A332A" }}>
                  <div style={{ aspectRatio: "1 / 1", background: "var(--noir-2)", borderRadius: 2, overflow: "hidden" }}>
                    <img src={product.image} alt="" style={{ width: "100%", height: "100%", objectFit: "cover", display: "block", opacity: 0.92 }} />
                  </div>
                  <div>
                    <div style={{ fontFamily: "var(--font-display)", fontWeight: 300, fontSize: "18px", color: "var(--paper)", lineHeight: 1.2 }}>{product.name}</div>
                    <div style={{ fontFamily: "var(--font-sans)", fontSize: "11px", letterSpacing: "0.12em", textTransform: "uppercase", color: "#BFB5A3", marginTop: 6 }}>{product.size} &middot; Qty {qty}</div>
                    <button onClick={() => onRemove(product.id)} style={{
                      marginTop: 10,
                      fontFamily: "var(--font-sans)", fontSize: "10px",
                      letterSpacing: "0.18em", textTransform: "uppercase",
                      background: "transparent", border: 0, borderBottom: "0.5px solid #BFB5A3",
                      color: "#D9C49A", padding: "2px 0", cursor: "pointer",
                    }}>Remove</button>
                  </div>
                  <div style={{ fontFamily: "var(--font-sans)", fontSize: "13px", color: "var(--paper)" }}>${product.price * qty}</div>
                </li>
              ))}
            </ul>
          )}
        </div>
        <div style={{ padding: "var(--s-5) var(--s-6) var(--s-6)", borderTop: "0.5px solid #3A332A" }}>
          <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 6 }}>
            <span style={{ fontFamily: "var(--font-sans)", fontSize: "11px", letterSpacing: "0.18em", textTransform: "uppercase", color: "#D9C49A" }}>Subtotal</span>
            <span style={{ fontFamily: "var(--font-display)", fontSize: "24px", color: "var(--paper)" }}>${subtotal}</span>
          </div>
          <p style={{ fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300, fontSize: "13px", color: "#BFB5A3", margin: "0 0 var(--s-4)" }}>
            Complimentary global delivery on orders over $200. Thirty-day return, no questions.
          </p>
          <button disabled={items.length === 0} style={{
            width: "100%", padding: "18px 0",
            background: "var(--paper)", color: "var(--ink)",
            border: 0,
            fontFamily: "var(--font-sans)", fontSize: "12px", fontWeight: 500,
            letterSpacing: "0.22em", textTransform: "uppercase",
            cursor: items.length === 0 ? "not-allowed" : "pointer",
            opacity: items.length === 0 ? 0.5 : 1,
          }}>Proceed to checkout</button>
        </div>
      </aside>
    </React.Fragment>
  );
};

// Reveal — fades in element when scrolled into view.
// Uses React state (not classList) so it survives App re-renders.
const Reveal = ({ children, style, as: As = "div", delay = 0 }) => {
  const ref = React.useRef(null);
  const [shown, setShown] = React.useState(false);
  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    // Failsafe — reveal within 700ms even if IO never fires (iframe sandboxes
    // sometimes swallow it). The slow CSS transition still gives a graceful fade.
    const failsafe = setTimeout(() => setShown(true), 700);
    if (typeof IntersectionObserver === "undefined") { setShown(true); return () => clearTimeout(failsafe); }
    const io = new IntersectionObserver((entries) => {
      entries.forEach(en => {
        if (en.isIntersecting) {
          setShown(true);
          io.unobserve(el);
        }
      });
    }, { threshold: 0.12, rootMargin: "0px 0px -40px 0px" });
    io.observe(el);
    return () => { io.disconnect(); clearTimeout(failsafe); };
  }, []);
  return (
    <As ref={ref}
      className={"reveal" + (shown ? " in" : "")}
      style={{ transitionDelay: `${delay}ms`, ...style }}>
      {children}
    </As>
  );
};

window.Eyebrow = Eyebrow;
window.Rule = Rule;
window.Button = Button;
window.ArrowLink = ArrowLink;
window.Nav = Nav;
window.Footer = Footer;
window.ProductCard = ProductCard;
window.JournalCard = JournalCard;
window.BagDrawer = BagDrawer;
window.Reveal = Reveal;
