// pages.jsx — Shop & Product detail pages

const Shop = ({ onOpenProduct, onAdd }) => {
  const [filter, setFilter] = useState("All");
  const filtered = filter === "All" ? PRODUCTS : PRODUCTS.filter(p => p.concern === filter);
  const filters = ["All", "Hydration", "Renewal", "Calm", "Brightening"];

  return (
    <main>
      <section style={{ padding: "var(--s-10) var(--s-7) var(--s-7)", background: "var(--bone)" }}>
        <div style={{ maxWidth: 1280, margin: "0 auto" }}>
          <Eyebrow style={{ marginBottom: "var(--s-5)" }}>The Collection</Eyebrow>
          <h1 style={{
            fontFamily: "var(--font-display)", fontWeight: 300,
            fontSize: "clamp(56px, 7vw, 104px)",
            letterSpacing: "-0.02em", lineHeight: 0.98,
            color: "var(--ink)", margin: 0, textWrap: "balance",
            maxWidth: "14ch",
          }}>
            The Collection.
          </h1>
          <p style={{
            fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300,
            fontSize: "22px", lineHeight: 1.5, color: "var(--ink-2)",
            margin: "var(--s-5) 0 var(--s-8)", maxWidth: "44ch",
          }}>
            Six essentials. A complete ritual. Each formulation has a single purpose. Together, they restore the skin to its quietest, most luminous state.
          </p>

          <div style={{ display: "flex", gap: "var(--s-3)", flexWrap: "wrap", padding: "var(--s-5) 0", borderTop: "0.5px solid var(--line)", borderBottom: "0.5px solid var(--line)" }}>
            {filters.map(f => (
              <button key={f} onClick={() => setFilter(f)} style={{
                fontFamily: "var(--font-sans)", fontSize: "11px",
                letterSpacing: "0.22em", textTransform: "uppercase",
                padding: "10px 20px",
                border: f === filter ? "1px solid var(--ink)" : "1px solid var(--line)",
                background: f === filter ? "var(--ink)" : "transparent",
                color: f === filter ? "var(--paper)" : "var(--ink-2)",
                borderRadius: "999px",
                cursor: "pointer",
              }}>{f}</button>
            ))}
            <div style={{ flex: 1 }}></div>
            <div style={{ fontFamily: "var(--font-sans)", fontSize: "11px", letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--ink-3)", alignSelf: "center" }}>
              {filtered.length} {filtered.length === 1 ? "product" : "products"}
            </div>
          </div>
        </div>
      </section>

      <section style={{ padding: "var(--s-9) var(--s-7) var(--s-11)", background: "var(--bone)" }}>
        <div style={{ maxWidth: 1280, margin: "0 auto", display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: "var(--s-9) var(--s-7)" }}>
          {filtered.map(p => <ProductCard key={p.id} product={p} onClick={onOpenProduct} onAdd={onAdd} />)}
        </div>
      </section>
    </main>
  );
};

const Product = ({ product, onAdd, onOpenProduct }) => {
  const [openTab, setOpenTab] = useState("formulation");
  if (!product) return null;
  const others = PRODUCTS.filter(p => p.id !== product.id).slice(0, 3);
  const tabs = [
    ["formulation", "The Formulation", product.actives],
    ["ritual",      "The Ritual",      product.ritual],
    ["result",      "The Result",      product.result],
    ["provenance",  "The Provenance",  product.provenance],
  ];
  return (
    <main>
      <section style={{ padding: 0, background: "var(--bone)" }}>
        <div style={{ display: "grid", gridTemplateColumns: "1.05fr 1fr", minHeight: "min(92vh, 900px)" }}>
          <div style={{ background: "var(--linen)", overflow: "hidden", position: "relative" }}>
            <img src={product.image} alt={product.name}
              style={{ width: "100%", height: "100%", objectFit: "cover", display: "block" }} />
          </div>
          <div style={{ padding: "var(--s-10) var(--s-9)", display: "flex", flexDirection: "column", justifyContent: "center" }}>
            <Eyebrow style={{ marginBottom: "var(--s-5)" }}>{product.concern}</Eyebrow>
            <h1 style={{
              fontFamily: "var(--font-display)", fontWeight: 300,
              fontSize: "clamp(48px, 5.6vw, 84px)",
              letterSpacing: "-0.02em", lineHeight: 1, color: "var(--ink)", margin: 0, textWrap: "balance",
              maxWidth: "14ch",
            }}>{product.name}</h1>
            <p style={{
              fontFamily: "var(--font-display)", fontStyle: "italic", fontWeight: 300,
              fontSize: "22px", lineHeight: 1.45, color: "var(--ink-2)",
              margin: "var(--s-5) 0 var(--s-5)", maxWidth: "30ch",
            }}>{product.descriptor}</p>
            <p style={{ fontFamily: "var(--font-sans)", fontSize: "15px", color: "var(--ink-2)", lineHeight: 1.75, margin: 0, maxWidth: "44ch" }}>
              {product.longDescriptor}
            </p>
            <div style={{ display: "flex", gap: "var(--s-5)", alignItems: "baseline", marginTop: "var(--s-7)" }}>
              <span style={{ fontFamily: "var(--font-display)", fontWeight: 300, fontSize: "32px", color: "var(--ink)" }}>${product.price}</span>
              <span style={{ fontFamily: "var(--font-sans)", fontSize: "11px", letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--ink-4)" }}>{product.size}</span>
            </div>
            <div style={{ display: "flex", gap: "var(--s-4)", marginTop: "var(--s-6)", flexWrap: "wrap" }}>
              <Button onClick={() => onAdd(product)}>Add to ritual</Button>
              <Button variant="quiet">Add to gift list</Button>
            </div>

            {/* Micro trust strip */}
            <div style={{
              marginTop: "var(--s-8)", paddingTop: "var(--s-5)",
              borderTop: "0.5px solid var(--line)",
              display: "grid", gridTemplateColumns: "1fr 1fr 1fr", gap: "var(--s-4)",
              fontFamily: "var(--font-sans)", fontSize: "11px", letterSpacing: "0.16em", textTransform: "uppercase", color: "var(--ink-3)",
            }}>
              <span>Free shipping &gt;$200</span>
              <span>30-day return</span>
              <span>Concierge ‹24h</span>
            </div>
          </div>
        </div>
      </section>

      {/* Accordion sections */}
      <section style={{ padding: "var(--s-10) var(--s-7)", background: "var(--cream)" }}>
        <div style={{ maxWidth: 960, margin: "0 auto" }}>
          {tabs.map(([key, label, content]) => (
            <div key={key} style={{ borderTop: "0.5px solid var(--line)" }}>
              <button onClick={() => setOpenTab(openTab === key ? "" : key)} style={{
                width: "100%", display: "flex", justifyContent: "space-between", alignItems: "center",
                padding: "var(--s-6) 0",
                background: "transparent", border: 0, cursor: "pointer", textAlign: "left",
              }}>
                <span style={{ fontFamily: "var(--font-display)", fontWeight: 300, fontSize: "32px", color: "var(--ink)", letterSpacing: "-0.015em" }}>{label}</span>
                <span style={{ fontFamily: "var(--font-sans)", fontSize: "18px", color: "var(--ink-3)" }}>{openTab === key ? "−" : "+"}</span>
              </button>
              {openTab === key && (
                <div style={{ paddingBottom: "var(--s-7)", maxWidth: "60ch" }}>
                  {Array.isArray(content) ? (
                    <ul style={{ margin: 0, padding: 0, listStyle: "none", display: "flex", flexDirection: "column", gap: 12 }}>
                      {content.map(c => (
                        <li key={c} style={{ fontFamily: "var(--font-sans)", fontSize: "15px", color: "var(--ink-2)", lineHeight: 1.65, paddingLeft: 16, position: "relative" }}>
                          <span style={{ position: "absolute", left: 0, top: 9, width: 6, height: 1, background: "var(--champagne)" }}></span>
                          {c}
                        </li>
                      ))}
                    </ul>
                  ) : (
                    <p style={{ fontFamily: "var(--font-sans)", fontSize: "16px", color: "var(--ink-2)", lineHeight: 1.75, margin: 0 }}>{content}</p>
                  )}
                </div>
              )}
            </div>
          ))}
          <div style={{ borderTop: "0.5px solid var(--line)" }}></div>
        </div>
      </section>

      {/* Pairs well with */}
      <section style={{ padding: "var(--s-10) var(--s-7) var(--s-11)", background: "var(--bone)" }}>
        <div style={{ maxWidth: 1280, margin: "0 auto" }}>
          <Eyebrow style={{ marginBottom: "var(--s-5)" }}>Pairs well with</Eyebrow>
          <h2 style={{
            fontFamily: "var(--font-display)", fontWeight: 300,
            fontSize: "clamp(36px, 4vw, 56px)",
            letterSpacing: "-0.015em", lineHeight: 1.05,
            color: "var(--ink)", margin: "0 0 var(--s-8)", textWrap: "balance",
          }}>
            Complete the ritual.
          </h2>
          <div style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)", gap: "var(--s-7)" }}>
            {others.map(p => <ProductCard key={p.id} product={p} onClick={onOpenProduct} onAdd={onAdd} />)}
          </div>
        </div>
      </section>
    </main>
  );
};

window.Shop = Shop;
window.Product = Product;
