/* ============================================================
   Reynard Marketing — shared primitives
   Glass panels, buttons, wordmark, eyebrow, icons, reveal-on-scroll.
   Forked from the design-system UI kit. Exports to window.
   ============================================================ */

const rk = {
  // amber CTA — dark ink text, the one warm accent
  amberBtn: {
    fontFamily: "var(--font-display)", fontWeight: 600, fontSize: "1rem",
    color: "var(--text-on-amber)", background: "var(--amber-500)",
    border: "1px solid var(--amber-400)", borderRadius: "var(--r-sm)",
    padding: "14px 26px", cursor: "pointer", letterSpacing: "-0.01em",
    boxShadow: "var(--sh-amber), inset 0 1px 0 rgba(255,255,255,0.35)",
    transition: "transform var(--dur) var(--ease-out), background var(--dur), box-shadow var(--dur)",
    display: "inline-flex", alignItems: "center", gap: "10px", whiteSpace: "nowrap",
  },
  ghostBtn: {
    fontFamily: "var(--font-display)", fontWeight: 500, fontSize: "1rem",
    color: "var(--text-on-dark)", background: "rgba(255,255,255,0.06)",
    border: "1px solid var(--glass-dark-border)", borderRadius: "var(--r-sm)",
    padding: "13px 22px", cursor: "pointer",
    backdropFilter: "blur(10px)", WebkitBackdropFilter: "blur(10px)",
    transition: "background var(--dur) var(--ease-out), transform var(--dur)",
    display: "inline-flex", alignItems: "center", gap: "8px", whiteSpace: "nowrap",
  },
};

// ---- Wordmark (img, recolored variants) ----
function Wordmark({ variant = "light", height = 30, style }) {
  const src = `assets/reynard-wordmark-${variant}.svg`;
  return <img src={src} alt="Reynard Marketing" style={{ height, width: "auto", display: "block", ...style }} />;
}

// ---- Amber CTA button with hover/press ----
function AmberButton({ children, onClick, style }) {
  const [h, setH] = React.useState(false);
  const [p, setP] = React.useState(false);
  return (
    <button onClick={onClick}
      onMouseEnter={() => setH(true)} onMouseLeave={() => { setH(false); setP(false); }}
      onMouseDown={() => setP(true)} onMouseUp={() => setP(false)}
      style={{ ...rk.amberBtn,
        background: h ? "var(--amber-400)" : "var(--amber-500)",
        transform: p ? "scale(0.97)" : h ? "translateY(-1px)" : "none",
        ...style }}>
      {children}
    </button>
  );
}

function GhostButton({ children, onClick, style }) {
  const [h, setH] = React.useState(false);
  return (
    <button onClick={onClick} onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
      style={{ ...rk.ghostBtn, background: h ? "rgba(255,255,255,0.12)" : "rgba(255,255,255,0.06)", ...style }}>
      {children}
    </button>
  );
}

// ---- Glass panel ----
const Glass = React.forwardRef(function Glass({ children, light = false, style, strong = false, className = "", onMouseEnter, onMouseLeave, id }, ref) {
  const base = {
    background: light ? "var(--glass-light-bg)" : (strong ? "var(--glass-dark-bg-strong)" : "var(--glass-dark-bg)"),
    backdropFilter: "blur(var(--glass-blur)) saturate(115%)",
    WebkitBackdropFilter: "blur(var(--glass-blur)) saturate(115%)",
    border: `1px solid ${light ? "var(--glass-light-border)" : "var(--glass-dark-border)"}`,
    borderRadius: "var(--r-lg)",
    boxShadow: `var(--sh-glass-md), ${light ? "var(--inset-light-hi)" : "var(--inset-glass-hi)"}`,
    color: light ? "var(--text-on-light)" : "var(--text-on-dark)",
  };
  return <div ref={ref} id={id} className={className} style={{ ...base, ...style }} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave}>{children}</div>;
});

// ---- Eyebrow (mono, tracked) ----
function Eyebrow({ children, color = "var(--amber-400)", style }) {
  return <div className="eyebrow" style={{ color, ...style }}>{children}</div>;
}

// ---- Reveal on scroll (restrained fade + lift) ----
function Reveal({ children, delay = 0, style, enabled = true }) {
  const ref = React.useRef(null);
  const [shown, setShown] = React.useState(false);
  React.useEffect(() => {
    if (!enabled) { setShown(true); return; }
    const el = ref.current;
    if (!el) { setShown(true); return; }
    // If already in view on mount, show right away (IO's initial callback can
    // be unreliable for in-viewport elements after a reload).
    const root = document.querySelector("#site-scroll");
    const inView = () => {
      const r = el.getBoundingClientRect();
      const h = (root ? root.clientHeight : window.innerHeight) || window.innerHeight;
      return r.top < h * 0.92 && r.bottom > 0;
    };
    if (inView()) { setShown(true); return; }
    let io;
    try {
      io = new IntersectionObserver((entries) => {
        entries.forEach((e) => { if (e.isIntersecting) { setShown(true); io.disconnect(); } });
      }, { threshold: 0.12, root, rootMargin: "0px 0px -8% 0px" });
      io.observe(el);
    } catch (err) { setShown(true); }
    // Safety net: never let content stay hidden if the observer misfires.
    const t = setTimeout(() => setShown(true), 1400);
    return () => { io && io.disconnect(); clearTimeout(t); };
  }, [enabled]);
  return (
    <div ref={ref} style={{
      opacity: shown ? 1 : 0,
      transform: shown ? "none" : "translateY(22px)",
      transition: `opacity 560ms var(--ease-out) ${delay}ms, transform 560ms var(--ease-out) ${delay}ms`,
      ...style,
    }}>{children}</div>
  );
}

// ---- Minimal stroke icons (Lucide-style, 1.7 stroke) ----
const Icon = {
  phone: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><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.72c.13.96.36 1.9.7 2.81a2 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-.45c.91.34 1.85.57 2.81.7A2 2 0 0 1 22 16.92Z"/></svg>,
  target: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9"/><circle cx="12" cy="12" r="5"/><circle cx="12" cy="12" r="1.4" fill="currentColor"/></svg>,
  search: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></svg>,
  layout: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><path d="M3 9h18M9 21V9"/></svg>,
  funnel: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M3 4h18l-7 8v7l-4 2v-9L3 4Z"/></svg>,
  trending: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="m3 17 6-6 4 4 8-8"/><path d="M17 7h4v4"/></svg>,
  arrow: (p) => <svg viewBox="0 0 24 24" width={p.s||20} height={p.s||20} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"><path d="M5 12h14M13 6l6 6-6 6"/></svg>,
  check: (p) => <svg viewBox="0 0 24 24" width={p.s||20} height={p.s||20} fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"><path d="M20 6 9 17l-5-5"/></svg>,
  menu: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"><path d="M4 7h16M4 12h16M4 17h16"/></svg>,
  x: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round"><path d="M6 6l12 12M18 6 6 18"/></svg>,
  waveform: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round"><path d="M3 12h2M7 8v8M11 4v16M15 7v10M19 10v4M21 12h.01"/></svg>,
  route: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><circle cx="6" cy="19" r="2.4"/><circle cx="18" cy="5" r="2.4"/><path d="M8.4 19H14a3.5 3.5 0 0 0 0-7H9.5a3.5 3.5 0 0 1 0-7h6.1"/></svg>,
  receipt: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M4 3.5v17l2.5-1.5L9 20.5 11.5 19 14 20.5 16.5 19 19 20.5v-17L16.5 5 14 3.5 11.5 5 9 3.5 6.5 5 4 3.5Z"/><path d="M8 9h8M8 13h5"/></svg>,
  shield: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M12 3 5 6v5c0 4.5 3 8 7 10 4-2 7-5.5 7-10V6l-7-3Z"/><path d="m9 12 2 2 4-4"/></svg>,
  eyeOff: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M9.9 4.24A9.1 9.1 0 0 1 12 4c5.5 0 9 6 9 6a16 16 0 0 1-2.2 3M6.6 6.6A16 16 0 0 0 3 10s3.5 6 9 6a9 9 0 0 0 3.4-.66"/><path d="m2 2 20 20"/><path d="M9.9 9.9a3 3 0 0 0 4.2 4.2"/></svg>,
  loop: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M3 11a9 9 0 0 1 15-6.7L21 7"/><path d="M21 4v3h-3"/><path d="M21 13a9 9 0 0 1-15 6.7L3 17"/><path d="M3 20v-3h3"/></svg>,
  form: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M8 4H6a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V6a2 2 0 0 0-2-2h-2"/><rect x="8" y="2" width="8" height="4" rx="1"/><path d="M8 12h8M8 16h5"/></svg>,
  globe: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><circle cx="12" cy="12" r="9"/><path d="M3.2 9h17.6M3.2 15h17.6"/><path d="M12 3a14 14 0 0 1 0 18 14 14 0 0 1 0-18Z"/></svg>,
  chat: (p) => <svg viewBox="0 0 24 24" width={p.s||22} height={p.s||22} fill="none" stroke="currentColor" strokeWidth="1.7" strokeLinecap="round" strokeLinejoin="round"><path d="M21 11.5a8 8 0 0 1-11.6 7.1L4 20l1.4-4.4A8 8 0 1 1 21 11.5Z"/></svg>,
};

Object.assign(window, { rk, Wordmark, AmberButton, GhostButton, Glass, Eyebrow, Reveal, Icon });
