// ui.jsx - shared building blocks: Logo, Icon set, Placeholder, Reveal, primitives
const { useState, useEffect, useRef, useCallback } = React;

/* ---------- Wordmark / Logo ---------- */
function Logo({ size = 22, mono = false, forceLight = false }) {
  if (window.LOGO_IMG) {
    const hgt = Math.round(size * 1.42);
    const common = { height: hgt, width: 'auto' };
    if (forceLight) {
      return <img src={window.LOGO_IMG.light} alt="RealtyMarketing.App" style={{ ...common, display: 'block' }} />;
    }
    return (
      <span className="logo-img" style={{ display: 'inline-flex', alignItems: 'center', height: hgt }}>
        <img className="logo-on-light" src={window.LOGO_IMG.color} alt="RealtyMarketing.App" style={common} />
        <img className="logo-on-dark" src={window.LOGO_IMG.light} alt="RealtyMarketing.App" style={common} />
      </span>
    );
  }
  return (
    <span className="serif" style={{ fontSize: size, fontWeight: 600, letterSpacing: '-0.01em', whiteSpace: 'nowrap', display: 'inline-flex', alignItems: 'baseline' }}>
      <span style={{ color: forceLight ? 'oklch(0.97 0.006 83)' : 'var(--ink)' }}>RealtyMarketing</span>
      <span style={{ color: mono ? (forceLight ? 'oklch(0.97 0.006 83)' : 'var(--ink)') : 'var(--gold)' }}>.App</span>
    </span>
  );
}

/* ---------- Icons (functional UI line icons) ---------- */
const Icon = ({ name, size = 22, stroke = 1.6, color = 'currentColor' }) => {
  const p = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: color, strokeWidth: stroke, strokeLinecap: 'round', strokeLinejoin: 'round' };
  const paths = {
    link: <><path d="M9 15l6-6" /><path d="M11 6l1-1a4 4 0 0 1 6 6l-1 1" /><path d="M13 18l-1 1a4 4 0 0 1-6-6l1-1" /></>,
    radar: <><circle cx="12" cy="12" r="9" /><path d="M12 12l5-3" /><path d="M12 3a9 9 0 0 1 5 1.6" opacity="0.5" /><circle cx="12" cy="12" r="1.4" fill={color} stroke="none" /></>,
    sparkle: <><path d="M12 3l1.8 5.2L19 10l-5.2 1.8L12 17l-1.8-5.2L5 10l5.2-1.8z" /><path d="M19 15l.7 2 .7-2 .7 2" opacity="0.6" /></>,
    share: <><circle cx="18" cy="5" r="3" /><circle cx="6" cy="12" r="3" /><circle cx="18" cy="19" r="3" /><path d="M8.6 10.6l6.8-4M8.6 13.4l6.8 4" /></>,
    play: <><circle cx="12" cy="12" r="9" /><path d="M10 9l5 3-5 3z" fill={color} stroke="none" /></>,
    image: <><rect x="3" y="4" width="18" height="16" rx="2.5" /><circle cx="8.5" cy="9.5" r="1.5" /><path d="M4 17l4.5-4.5 4 4L16 12l4 4" /></>,
    bolt: <><path d="M13 3L5 13h6l-1 8 8-10h-6z" /></>,
    globe: <><circle cx="12" cy="12" r="9" /><path d="M3 12h18M12 3c2.5 2.5 2.5 15 0 18M12 3c-2.5 2.5-2.5 15 0 18" /></>,
    phone: <><rect x="6" y="2.5" width="12" height="19" rx="3" /><path d="M10.5 18.5h3" /></>,
    monitor: <><rect x="2.5" y="4" width="19" height="13" rx="2.5" /><path d="M9 21h6M12 17v4" /></>,
    check: <><path d="M5 12.5l4.5 4.5L19 7" /></>,
    arrow: <><path d="M5 12h14M13 6l6 6-6 6" /></>,
    chevron: <><path d="M6 9l6 6 6-6" /></>,
    shield: <><path d="M12 3l7 3v5c0 4.5-3 7.5-7 9-4-1.5-7-4.5-7-9V6z" /><path d="M9 12l2 2 4-4" /></>,
    palette: <><path d="M12 3a9 9 0 1 0 0 18c1.7 0 2-1.3 1.2-2.2-.8-.9-.5-2.2 1-2.2H17a4 4 0 0 0 4-4c0-5-4-9.6-9-9.6z" /><circle cx="7.5" cy="11" r="1" fill={color} stroke="none" /><circle cx="11" cy="7.5" r="1" fill={color} stroke="none" /><circle cx="15" cy="8.5" r="1" fill={color} stroke="none" /></>,
    clock: <><circle cx="12" cy="12" r="9" /><path d="M12 7v5l3.5 2" /></>,
    layers: <><path d="M12 3l9 5-9 5-9-5z" /><path d="M3 13l9 5 9-5" opacity="0.6" /></>,
    tag: <><path d="M3 12V5a2 2 0 0 1 2-2h7l9 9-9 9z" /><circle cx="8" cy="8" r="1.4" /></>,
    quote: <><path d="M9 7c-2.5 1-4 3.2-4 6.5V18h5v-5H7c0-2 .8-3.4 2.5-4zM19 7c-2.5 1-4 3.2-4 6.5V18h5v-5h-3c0-2 .8-3.4 2.5-4z" fill={color} stroke="none" /></>,
  };
  return <svg {...p}>{paths[name] || null}</svg>;
};

/* ---------- Brand glyph icons for socials/stores (simple) ---------- */
const Brand = ({ name, size = 18, color = 'currentColor' }) => {
  const p = { width: size, height: size, viewBox: '0 0 24 24', fill: 'none', stroke: color, strokeWidth: 1.6, strokeLinecap: 'round', strokeLinejoin: 'round' };
  const g = {
    instagram: <><rect x="3" y="3" width="18" height="18" rx="5" /><circle cx="12" cy="12" r="4" /><circle cx="17.2" cy="6.8" r="1" fill={color} stroke="none" /></>,
    facebook: <><path d="M14 8h2.5V4.5H14c-2.2 0-3.5 1.4-3.5 3.7V10H8v3.4h2.5V21H14v-7.6h2.6L17 10h-3V8.5c0-.4.3-.5.6-.5z" fill={color} stroke="none" /></>,
    tiktok: <><path d="M14 4v9.5a3.5 3.5 0 1 1-3-3.46" /><path d="M14 4c.4 2.2 1.8 3.6 4 3.9" /></>,
    youtube: <><rect x="3" y="6" width="18" height="12" rx="3.5" /><path d="M11 9.5l4 2.5-4 2.5z" fill={color} stroke="none" /></>,
    linkedin: <><rect x="3" y="3" width="18" height="18" rx="3" /><path d="M7 10v7M7 7v.01M11 17v-4a2 2 0 0 1 4 0v4M11 10v7" /></>,
    x: <><path d="M5 4l14 16M19 4L5 20" /></>,
    apple: <><path d="M16.5 13c0-1.9 1.4-2.9 1.5-3-.8-1.2-2.1-1.4-2.6-1.4-1.1-.1-2.1.6-2.7.6-.6 0-1.4-.6-2.3-.6-1.2 0-2.3.7-2.9 1.8-1.2 2.1-.3 5.3.9 7 .6.8 1.2 1.7 2.1 1.7.8 0 1.1-.5 2.1-.5s1.3.5 2.1.5 1.5-.8 2-1.6c.4-.6.6-1.2.7-1.3-1.2-.5-2-1.6-2-2.7z" fill={color} stroke="none" /><path d="M13.6 6c.5-.6.8-1.4.7-2.3-.7.1-1.6.5-2.1 1.1-.5.5-.8 1.3-.7 2.1.8.1 1.6-.4 2.1-.9z" fill={color} stroke="none" /></>,
    android: <><path d="M5 10v6a1.5 1.5 0 0 0 1.5 1.5H17.5A1.5 1.5 0 0 0 19 16v-6z" /><path d="M5 10a7 7 0 0 1 14 0z" /><path d="M8 7L6.5 5M16 7l1.5-2" /><circle cx="9.5" cy="8" r=".6" fill={color} stroke="none" /><circle cx="14.5" cy="8" r=".6" fill={color} stroke="none" /></>,
  };
  return <svg {...p}>{g[name] || null}</svg>;
};

/* ---------- Striped placeholder imagery ---------- */
function Placeholder({ label, style, className = '', children, radius }) {
  return (
    <div className={`ph ${className}`} style={{ borderRadius: radius, ...style }}>
      {children || <span className="ph-label">{label}</span>}
    </div>
  );
}

/* ---------- Scroll reveal ---------- */
function useReveal() {
  useEffect(() => {
    const els = document.querySelectorAll('.reveal:not(.in)');
    if (!('IntersectionObserver' in window)) { els.forEach(e => e.classList.add('in')); return; }
    const io = new IntersectionObserver((entries) => {
      entries.forEach(en => { if (en.isIntersecting) { en.target.classList.add('in'); io.unobserve(en.target); } });
    }, { threshold: 0.12, rootMargin: '0px 0px -8% 0px' });
    els.forEach(e => io.observe(e));
    return () => io.disconnect();
  });
}

/* ---------- Reveal wrapper ---------- */
function R({ children, delay = 0, as = 'div', className = '', style, ...rest }) {
  const Tag = as;
  return <Tag className={`reveal ${className}`} style={{ transitionDelay: delay + 'ms', ...style }} {...rest}>{children}</Tag>;
}

Object.assign(window, { Logo, Icon, Brand, Placeholder, useReveal, R });
