// Small decorative & UI primitives. Exposed on window for sibling scripts.
const { useState, useEffect, useRef, useCallback, useMemo } = React;

// ——— SVG icon — used in many places
function Icon({ d, size = 24, stroke = 1.5, fill = 'none', extras = null, ...props }) {
  return (
    <svg viewBox="0 0 24 24" width={size} height={size} fill={fill} stroke="currentColor"
         strokeWidth={stroke} strokeLinecap="round" strokeLinejoin="round" {...props}>
      {typeof d === 'string' ? <path d={d}></path> : d}
      {extras}
    </svg>
  );
}

// ——— A sparkly editorial star/asterism
function Asterism({ size = 20, color = 'var(--coral)' }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none" stroke={color}
         strokeWidth="1.4" strokeLinecap="round" strokeLinejoin="round">
      <path d="M12 2l1.5 7L21 11l-7.5 2L12 22l-1.5-9L3 11l7.5-2L12 2z"></path>
    </svg>
  );
}

// ——— Three-asterisk ornament
function ThreeAsterisms({ teal = false }) {
  return (
    <div className="divider">
      <span className="dot"></span>
      <Asterism />
      <span className="dot teal"></span>
      <Asterism color="var(--teal)" />
      <span className="dot"></span>
    </div>
  );
}

// ——— Storyboard frame — a tiny filmstrip frame card
function FilmFrame({ number, label, children, accent = 'coral' }) {
  return (
    <div className={`film-frame ${accent}`}>
      <div className="film-frame-tape">
        <span className="frame-num">FRAME {String(number).padStart(2, '0')}</span>
        <span className="frame-label">{label}</span>
      </div>
      <div className="film-frame-body">{children}</div>
    </div>
  );
}

// ——— Hand-marked arrow (used as decoration near callouts)
function MarkerArrow({ rotate = 0, color = 'var(--coral)' }) {
  return (
    <svg width="56" height="36" viewBox="0 0 56 36" fill="none"
         style={{ transform: `rotate(${rotate}deg)` }}>
      <path d="M3 18 Q 18 4, 38 14 T 50 18" stroke={color} strokeWidth="1.6"
            strokeLinecap="round" fill="none" />
      <path d="M44 12 L 50 18 L 44 22" stroke={color} strokeWidth="1.6"
            strokeLinecap="round" strokeLinejoin="round" fill="none" />
    </svg>
  );
}

// ——— Color swatch row — purely decorative artist palette
function PaletteSwatches() {
  const swatches = [
    { c: '#14110C', name: 'Ink' },
    { c: '#B85432', name: 'Persimmon' },
    { c: '#B79049', name: 'Gold' },
    { c: '#F4EFE5', name: 'Ivory' },
    { c: '#5C7561', name: 'Sage' },
    { c: '#1E2C56', name: 'Navy' },
  ];
  return (
    <div className="palette">
      {swatches.map((s, i) => (
        <div key={i} className="swatch" style={{ background: s.c }}>
          <span className="swatch-label">{s.name}</span>
        </div>
      ))}
    </div>
  );
}

// ——— Generic SVG icon paths used inline
const Icons = {
  play: <path d="M10 8l6 4-6 4V8z" fill="currentColor" stroke="none" />,
  mic: <g><rect x="9" y="3" width="6" height="12" rx="3" /><path d="M5 11a7 7 0 0 0 14 0" /><line x1="12" y1="19" x2="12" y2="22" /><line x1="8" y1="22" x2="16" y2="22" /></g>,
  scissors: <g><circle cx="6" cy="6" r="3" /><circle cx="6" cy="18" r="3" /><line x1="20" y1="4" x2="8.12" y2="15.88" /><line x1="14.47" y1="14.48" x2="20" y2="20" /><line x1="8.12" y1="8.12" x2="12" y2="12" /></g>,
  sparkle: <g><path d="M12 3l1.5 4.5L18 9l-4.5 1.5L12 15l-1.5-4.5L6 9l4.5-1.5L12 3z" /><path d="M19 14l.7 2.1L22 17l-2.3.9L19 20l-.7-2.1L16 17l2.3-.9L19 14z" /><path d="M5 14l.7 2.1L8 17l-2.3.9L5 20l-.7-2.1L2 17l2.3-.9L5 14z" /></g>,
  check: <polyline points="4,12 10,18 20,6" />,
  bulb: <g><path d="M9 18h6" /><path d="M10 22h4" /><path d="M12 2a7 7 0 0 0-4 12c.7.7 1 1.5 1 2.5V18h6v-1.5c0-1 .3-1.8 1-2.5a7 7 0 0 0-4-12z" /></g>,
  warn: <g><circle cx="12" cy="12" r="9" /><line x1="12" y1="7" x2="12" y2="13" /><circle cx="12" cy="16.5" r="0.6" fill="currentColor" stroke="none" /></g>,
  film: <g><rect x="3" y="6" width="18" height="13" rx="1.5" /><path d="M10 10l5 2.5L10 15z" fill="currentColor" stroke="none" /></g>,
  question: <g><circle cx="12" cy="12" r="9" /><path d="M9.5 9.5a2.5 2.5 0 0 1 5 0c0 1.5-2.5 2-2.5 3.5" /><circle cx="12" cy="16.5" r="0.7" fill="currentColor" stroke="none" /></g>,
  arrowRight: <g><line x1="5" y1="12" x2="19" y2="12" /><polyline points="13,6 19,12 13,18" /></g>,
  arrowUp: <g><line x1="12" y1="19" x2="12" y2="5" /><polyline points="6,11 12,5 18,11" /></g>,
  copy: <g><rect x="8" y="8" width="12" height="12" rx="2" /><path d="M16 8V6a2 2 0 0 0-2-2H6a2 2 0 0 0-2 2v8a2 2 0 0 0 2 2h2" /></g>,
  menu: <g><line x1="4" y1="7" x2="20" y2="7" /><line x1="4" y1="12" x2="20" y2="12" /><line x1="4" y1="17" x2="20" y2="17" /></g>,
  external: <g><path d="M14 4h6v6" /><line x1="20" y1="4" x2="10" y2="14" /><path d="M19 13v6a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1V6a1 1 0 0 1 1-1h6" /></g>,
  voice: <g><circle cx="12" cy="12" r="9" /><polygon points="14.5,9.5 12,15 9.5,9.5 12,14" fill="currentColor" stroke="none" /></g>,
  face: <g><circle cx="12" cy="12" r="9" /><circle cx="9" cy="10.5" r="0.7" fill="currentColor" stroke="none" /><circle cx="15" cy="10.5" r="0.7" fill="currentColor" stroke="none" /><path d="M9 15.5c.8.8 1.8 1.2 3 1.2s2.2-.4 3-1.2" /></g>,
  building: <g><rect x="4" y="6" width="16" height="14" rx="1" /><line x1="9" y1="6" x2="9" y2="20" /><line x1="15" y1="6" x2="15" y2="20" /><line x1="6.5" y1="10" x2="7.5" y2="10" /><line x1="11" y1="10" x2="13" y2="10" /><line x1="16.5" y1="10" x2="17.5" y2="10" /><line x1="6.5" y1="14" x2="7.5" y2="14" /><line x1="11" y1="14" x2="13" y2="14" /><line x1="16.5" y1="14" x2="17.5" y2="14" /></g>,
  text: <g><line x1="6" y1="8" x2="18" y2="8" /><line x1="6" y1="12" x2="18" y2="12" /><line x1="6" y1="16" x2="13" y2="16" /></g>,
  motion: <g><path d="M3 12s3-6 9-6 9 6 9 6" /><path d="M3 18s3-6 9-6 9 6 9 6" /></g>,
  clock: <g><circle cx="12" cy="12" r="9" /><polyline points="12,7 12,12 15,14" /></g>,
  cost: <g><circle cx="12" cy="12" r="9" /><path d="M15 9.5a2.5 2.5 0 0 0-5 0c0 1.5 1.5 2 3 2.5s3 1 3 2.5a2.5 2.5 0 0 1-5 0" /><line x1="12" y1="6" x2="12" y2="8" /><line x1="12" y1="16" x2="12" y2="18" /></g>,
  brush: <g><path d="M3 21c0-3 3-3 3-6s3-3 3-6 3-3 3-6" /><path d="M15 3l6 6" /><path d="M9 15l6-12 6 6-12 6z" /></g>,
  refresh: <g><polyline points="20,8 16,8 16,4" /><path d="M16 8a8 8 0 1 0 2 7" /></g>,
  send: <g><line x1="22" y1="2" x2="11" y2="13" /><polygon points="22,2 15,22 11,13 2,9" /></g>,
  eye: <g><path d="M2 12s4-7 10-7 10 7 10 7-4 7-10 7S2 12 2 12z" /><circle cx="12" cy="12" r="3" /></g>,
};

Object.assign(window, {
  Icon, Icons, Asterism, ThreeAsterisms, FilmFrame, MarkerArrow, PaletteSwatches,
});
