// ---- SÉRÈNE — shared components ---------------------------------------
const { useState, useEffect, useRef } = React;

/* ---------- Icons (simple stroke UI glyphs) ---------- */
const Ico = ({ d, size = 19, fill = "none", sw = 1.4, children, vb = 24 }) => (
  <svg width={size} height={size} viewBox={`0 0 ${vb} ${vb}`} fill={fill}
       stroke="currentColor" strokeWidth={sw} strokeLinecap="round" strokeLinejoin="round">
    {d ? <path d={d} /> : children}
  </svg>
);
const IconSearch = (p) => <Ico {...p}><circle cx="11" cy="11" r="7" /><path d="M21 21l-4.3-4.3" /></Ico>;
const IconBag = (p) => <Ico {...p}><path d="M6 8h12l-1 12H7L6 8z" /><path d="M9 8V6a3 3 0 0 1 6 0v2" /></Ico>;
const IconUser = (p) => <Ico {...p}><circle cx="12" cy="8" r="3.4" /><path d="M5.5 20a6.5 6.5 0 0 1 13 0" /></Ico>;
const IconMenu = (p) => <Ico {...p}><path d="M3 6h18M3 12h18M3 18h18" /></Ico>;
const IconClose = (p) => <Ico {...p}><path d="M6 6l12 12M18 6L6 18" /></Ico>;
const IconArrow = (p) => <Ico {...p}><path d="M5 12h14M13 6l6 6-6 6" /></Ico>;
const IconChevron = (p) => <Ico {...p}><path d="M9 6l6 6-6 6" /></Ico>;
const IconMinus = (p) => <Ico {...p}><path d="M5 12h14" /></Ico>;
const IconPlus = (p) => <Ico {...p}><path d="M12 5v14M5 12h14" /></Ico>;
const IconHeart = (p) => <Ico {...p}><path d="M12 20s-7-4.4-7-9.3A3.7 3.7 0 0 1 12 7a3.7 3.7 0 0 1 7 3.7C19 15.6 12 20 12 20z" /></Ico>;
const IconCheck = (p) => <Ico {...p}><path d="M4 12l5 5L20 6" /></Ico>;
const IconTruck = (p) => <Ico {...p}><path d="M3 7h11v8H3zM14 10h4l3 3v2h-7z" /><circle cx="7" cy="18" r="1.6" /><circle cx="17" cy="18" r="1.6" /></Ico>;
const IconLeaf = (p) => <Ico {...p}><path d="M5 19C5 11 11 6 19 5c1 8-4 14-12 14z" /><path d="M9 15c2-2 5-4 8-5" /></Ico>;
const IconGift = (p) => <Ico {...p}><path d="M4 11h16v9H4zM4 8h16v3H4zM12 8v12M12 8S10 4 8 5.2 9 8 12 8zM12 8s2-4 4-2.8S15 8 12 8z" /></Ico>;

/* ---------- Image placeholder ---------- */
const Ph = ({ label, tone, ratio = "1 / 1", style = {}, className = "" }) => (
  <div className={`ph ${tone ? "ph--" + tone : ""} ${className}`}
       style={{ aspectRatio: ratio, ...style }}>
    <span className="ph__label">{label}</span>
  </div>
);

/* ---------- Color swatch ---------- */
const Swatch = ({ tone, active, onClick, title }) => (
  <button onClick={onClick} title={title} aria-label={title}
    style={{
      width: 26, height: 26, borderRadius: "50%", padding: 0,
      background: TONE_LABEL[tone] || "var(--surface)",
      border: active ? "1.5px solid var(--ink)" : "1px solid var(--line)",
      outline: active ? "2px solid var(--cream)" : "none",
      outlineOffset: -4,
      boxShadow: active ? "0 0 0 1px var(--ink)" : "none",
      transition: "all 0.25s var(--ease)",
    }} />
);

/* ---------- Qty stepper ---------- */
const Stepper = ({ value, onChange, small }) => (
  <div style={{
    display: "inline-flex", alignItems: "center",
    border: "1px solid var(--line)", borderRadius: "var(--radius)",
  }}>
    <button onClick={() => onChange(Math.max(1, value - 1))}
      style={{ background: "none", border: "none", padding: small ? "7px 10px" : "11px 13px", color: "var(--ink)", display: "flex" }}>
      <IconMinus size={14} />
    </button>
    <span style={{ minWidth: 26, textAlign: "center", fontSize: 14, fontVariantNumeric: "tabular-nums" }}>{value}</span>
    <button onClick={() => onChange(value + 1)}
      style={{ background: "none", border: "none", padding: small ? "7px 10px" : "11px 13px", color: "var(--ink)", display: "flex" }}>
      <IconPlus size={14} />
    </button>
  </div>
);

/* ---------- Product card ---------- */
const ProductCard = ({ p, onOpen, onAdd }) => {
  const [hover, setHover] = useState(false);
  return (
    <article
      onMouseEnter={() => setHover(true)} onMouseLeave={() => setHover(false)}
      style={{ cursor: "pointer" }} onClick={() => onOpen(p.id)}>
      <div style={{ position: "relative", overflow: "hidden", borderRadius: "var(--radius-lg)" }}>
        <Ph label={p.name.toLowerCase() + " — produit"} tone={p.tone} ratio="4 / 5"
            style={{ transform: hover ? "scale(1.04)" : "scale(1)", transition: "transform 0.7s var(--ease)" }} />
        {p.tag && (
          <span style={{
            position: "absolute", top: 14, left: 14, background: "var(--paper)",
            fontSize: 10, letterSpacing: "0.16em", textTransform: "uppercase",
            padding: "6px 11px", borderRadius: 100, color: "var(--ink)",
          }}>{p.tag}</span>
        )}
        <button onClick={(e) => { e.stopPropagation(); }}
          style={{
            position: "absolute", top: 12, right: 12, width: 36, height: 36,
            borderRadius: "50%", background: "var(--paper)", border: "none",
            display: "flex", alignItems: "center", justifyContent: "center",
            color: "var(--ink-soft)", opacity: hover ? 1 : 0,
            transform: hover ? "none" : "translateY(-4px)", transition: "all 0.3s var(--ease)",
          }}>
          <IconHeart size={16} />
        </button>
        <button onClick={(e) => { e.stopPropagation(); onAdd(p, 1); }}
          style={{
            position: "absolute", left: 12, right: 12, bottom: 12,
            background: "var(--ink)", color: "var(--cream)", border: "none",
            padding: "13px", fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase",
            borderRadius: "var(--radius)", opacity: hover ? 1 : 0,
            transform: hover ? "none" : "translateY(8px)", transition: "all 0.35s var(--ease)",
          }}>
          Ajouter au panier
        </button>
      </div>
      <div style={{ paddingTop: 16 }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", gap: 10 }}>
          <h3 style={{ fontSize: 21, fontWeight: 500 }}>{p.name}</h3>
          <span style={{ fontSize: 15, fontVariantNumeric: "tabular-nums", whiteSpace: "nowrap" }}>{fmt(p.price)}</span>
        </div>
        <p style={{ margin: "4px 0 0", fontSize: 13, color: "var(--ink-faint)", lineHeight: 1.5 }}>{p.catName}</p>
      </div>
    </article>
  );
};

/* ---------- Section heading ---------- */
const SectionHead = ({ eyebrow, title, action, onAction }) => (
  <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", marginBottom: 36, flexWrap: "wrap", gap: 16 }}>
    <div>
      {eyebrow && <div className="eyebrow" style={{ marginBottom: 14 }}>{eyebrow}</div>}
      <h2 style={{ fontSize: "clamp(30px, 4vw, 46px)" }}>{title}</h2>
    </div>
    {action && <button onClick={onAction} className="link-underline" style={{ background: "none", border: "none", borderBottom: "1px solid var(--ink)" }}>{action}</button>}
  </div>
);

Object.assign(window, {
  Ph, Swatch, Stepper, ProductCard, SectionHead,
  IconSearch, IconBag, IconUser, IconMenu, IconClose, IconArrow, IconChevron,
  IconMinus, IconPlus, IconHeart, IconCheck, IconTruck, IconLeaf, IconGift,
});
