// interface.jsx — conjunto de ícones + primitivos compartilhados de O Casal Legendário.
// Ícones de linha estilo Lucide (sóbrios, sem emoji). Exporta para window.
const { useState, useEffect, useRef, useCallback, createContext, useContext } = React;

const ICON_PATHS = {
  home:    'M3 11.5l9-8 9 8M5.5 9.8V21h4.5v-6h4v6h4.5V9.8',
  book:    'M4 4.5A1.5 1.5 0 0 1 5.5 3H20v15H6a2 2 0 0 0-2 2zM20 18v3H6',
  hands:   'M7 13.5V6.2a1.5 1.5 0 0 1 3 0v4.8m0-1.2V4.6a1.5 1.5 0 0 1 3 0v6.2m0-1.4V6.4a1.5 1.5 0 0 1 3 0v7.6a6 6 0 0 1-6 6 6 6 0 0 1-6-6v-1.8a1.5 1.5 0 0 1 3 0V13',
  bookmark:'M6 3.5h12V21l-6-4.2L6 21z',
  user:    'M12 12.2a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM4.5 20.5c0-3.7 3.4-5.6 7.5-5.6s7.5 1.9 7.5 5.6',
  flame:   'M12 3c.6 3.2-2 4.2-2 7 0 1.6-1 2.2-1 2.2S7 11 7 9c0 0-2.2 2.2-2.2 5.3A7.2 7.2 0 0 0 12 21.5a7.2 7.2 0 0 0 7.2-7.2c0-4.2-2.6-6.3-3.7-8.4C14.2 6 13.2 4.4 12 3z',
  play:    'M8 5.2v13.6l11-6.8z',
  lock:    'M6.5 10.5V8a5.5 5.5 0 0 1 11 0v2.5M5.5 10.5h13v9.5h-13z',
  unlock:  'M6.5 10.5V8a5.5 5.5 0 0 1 10.8-1.3M5.5 10.5h13v9.5h-13z',
  check:   'M4.5 12.5l5 5 10.5-10.5',
  chevron: 'M9 5.5l6.5 6.5L9 18.5',
  back:    'M14.5 5.5L8 12l6.5 6.5',
  down:    'M5.5 9l6.5 6.5L18.5 9',
  bell:    'M6 9.5a6 6 0 0 1 12 0c0 4.5 1.8 5.8 1.8 5.8H4.2S6 14 6 9.5M10 20a2 2 0 0 0 4 0',
  star:    'M12 3l2.7 5.8 6.3.7-4.7 4.3 1.3 6.2L12 17.1l-5.6 2.9 1.3-6.2L3 9.5l6.3-.7z',
  store:   'M4 9l1.6-5h12.8L20 9M4.5 9c0 1.6 1 2.6 2.4 2.6S9.3 10.6 9.3 9c0 1.6 1 2.6 2.7 2.6S14.7 10.6 14.7 9c0 1.6 1.1 2.6 2.6 2.6S20 10.6 20 9M5.5 11.4V20h13v-8.6M10 20v-4.5h4V20',
  users:   'M9 11a3.4 3.4 0 1 0 0-6.8A3.4 3.4 0 0 0 9 11zM2.5 20c0-3.5 3-5.4 6.5-5.4M16 11a3 3 0 1 0 0-6M21.5 19.5c0-3.1-2.3-4.8-5.5-5.2',
  arrow:   'M5 12h13.5M13 6l6 6-6 6',
  search:  'M11 18.5a7.5 7.5 0 1 0 0-15 7.5 7.5 0 0 0 0 15zM20.5 20.5l-4.3-4.3',
  heart:   'M12 20.5S3 15 3 8.8C3 5.6 5.4 4 7.6 4c1.7 0 3.4.9 4.4 2.6C13 4.9 14.7 4 16.4 4 18.6 4 21 5.6 21 8.8c0 6.2-9 11.7-9 11.7z',
  heartf:  'M12 20.5S3 15 3 8.8C3 5.6 5.4 4 7.6 4c1.7 0 3.4.9 4.4 2.6C13 4.9 14.7 4 16.4 4 18.6 4 21 5.6 21 8.8c0 6.2-9 11.7-9 11.7z',
  share:   'M16 6l-4-4-4 4M12 2v14M5 12v7.5a1.5 1.5 0 0 0 1.5 1.5h11a1.5 1.5 0 0 0 1.5-1.5V12',
  settings:'M12 15.5a3.5 3.5 0 1 0 0-7 3.5 3.5 0 0 0 0 7zM19.4 13a7.6 7.6 0 0 0 0-2l1.8-1.4-1.8-3.1-2.1.9a7.5 7.5 0 0 0-1.7-1l-.3-2.3H9l-.3 2.3a7.5 7.5 0 0 0-1.7 1l-2.1-.9-1.8 3.1L4.9 11a7.6 7.6 0 0 0 0 2l-1.8 1.4 1.8 3.1 2.1-.9c.5.4 1.1.8 1.7 1l.3 2.3h3.8l.3-2.3c.6-.2 1.2-.6 1.7-1l2.1.9 1.8-3.1z',
  close:   'M6 6l12 12M18 6L6 18',
  plus:    'M12 5v14M5 12h14',
  whatsapp:'M3 21l1.7-5A8 8 0 1 1 8 19.3zM9 8.5c-.3 0-.6.1-.8.4-.3.3-.9.9-.9 2.1s.9 2.4 1 2.6c.1.2 1.8 2.9 4.5 3.9 2.2.8 2.7.7 3.2.6.6-.1 1.7-.7 2-1.4.2-.7.2-1.2.2-1.4-.1-.1-.3-.2-.6-.4-.3-.2-1.7-.9-2-1-.3-.1-.5-.1-.6.1l-.9 1.1c-.2.2-.3.2-.6.1-.3-.1-1.2-.5-2.2-1.4-.8-.7-1.4-1.6-1.5-1.9-.2-.3 0-.4.1-.6l.4-.5c.1-.2.2-.3.3-.5 0-.2 0-.4 0-.5l-.8-2c-.2-.5-.4-.4-.6-.5z',
  pencil:  'M4 20h4l10.5-10.5a2 2 0 0 0 0-2.8l-1.2-1.2a2 2 0 0 0-2.8 0L4 16z',
  calendar:'M5 5.5h14V20H5zM5 9.5h14M9 3v4M15 3v4',
  shield:  'M12 3l7 3v5c0 4.5-3 7.8-7 9.5-4-1.7-7-5-7-9.5V6z',
  shieldcheck:'M12 3l7 3v5c0 4.5-3 7.8-7 9.5-4-1.7-7-5-7-9.5V6zM9 11.5l2 2 4-4',
  brain:   'M9 4.5A2.5 2.5 0 0 0 6.5 7 2.5 2.5 0 0 0 5 12a2.5 2.5 0 0 0 1.5 4.5A2.5 2.5 0 0 0 9 19.5c1.4 0 2.5-.8 3-1.5V5.5C11.5 5 10.4 4.5 9 4.5zM15 4.5A2.5 2.5 0 0 1 17.5 7 2.5 2.5 0 0 1 19 12a2.5 2.5 0 0 1-1.5 4.5A2.5 2.5 0 0 1 15 19.5c-1.4 0-2.5-.8-3-1.5',
  rings:   'M9 14.5a4 4 0 1 0 0-8 4 4 0 0 0 0 8zM15 17.5a4 4 0 1 0 0-8 4 4 0 0 0 0 8z',
  sun:     'M12 7.5a4.5 4.5 0 1 0 0 9 4.5 4.5 0 0 0 0-9zM12 2v2.5M12 19.5V22M4.2 4.2l1.8 1.8M18 18l1.8 1.8M2 12h2.5M19.5 12H22M4.2 19.8L6 18M18 6l1.8-1.8',
  moon:    'M20 14.5A8 8 0 0 1 9.5 4 8 8 0 1 0 20 14.5z',
  wifi:    'M2.5 9.5a14 14 0 0 1 19 0M5.5 13a9 9 0 0 1 13 0M8.5 16.5a4.5 4.5 0 0 1 7 0M12 20h.01',
  wifioff: 'M2 3l19 19M8.5 16.5a4.5 4.5 0 0 1 7 0M2.5 9.5a14 14 0 0 1 5-3.2M16 6c2 .6 3.8 1.7 5.4 3.5M5.5 13a9 9 0 0 1 4-2.6M12 20h.01',
  external:'M14 4h6v6M20 4l-9 9M18 14v4.5a1.5 1.5 0 0 1-1.5 1.5h-11A1.5 1.5 0 0 1 4 18.5v-11A1.5 1.5 0 0 1 5.5 6H10',
  quote:   'M9 7H5.5A1.5 1.5 0 0 0 4 8.5V13h5V7zM9 7c0 4-1 5.5-3 6.5M20 7h-3.5A1.5 1.5 0 0 0 15 8.5V13h5V7zM20 7c0 4-1 5.5-3 6.5',
  message: 'M4 5.5h16V17H9l-5 4z',
  clock:   'M12 21a9 9 0 1 0 0-18 9 9 0 0 0 0 18zM12 7.5V12l3 2',
  sparkle: 'M12 3l1.8 5.4L19 10.2l-5.2 1.8L12 17.4l-1.8-5.4L5 10.2l5.2-1.8z',
  gift:    'M5 11.5h14V20H5zM3.5 7.5h17v4h-17zM12 7.5V20M12 7.5C12 5 10.5 3.5 8.8 3.5S6 4.6 6 6s1.3 1.5 2.8 1.5M12 7.5C12 5 13.5 3.5 15.2 3.5S18 4.6 18 6s-1.3 1.5-2.8 1.5',
  leaf:    'M5 19c0-8 5-13 14-13 0 9-5 14-13 14 0-4 2.5-7 6.5-8',
  refresh: 'M4 12a8 8 0 0 1 13.5-5.8L20 8M20 4v4h-4M20 12a8 8 0 0 1-13.5 5.8L4 16M4 20v-4h4',
};

function Icon({ name, size = 22, sw = 1.7, fill, color, style, className }) {
  const d = ICON_PATHS[name] || ICON_PATHS.home;
  return (
    React.createElement('svg', {
      width: size, height: size, viewBox: '0 0 24 24',
      fill: fill ? 'currentColor' : 'none', stroke: 'currentColor', strokeWidth: sw,
      strokeLinecap: 'round', strokeLinejoin: 'round',
      style: { color: color || 'currentColor', flex: '0 0 auto', ...style }, className,
    }, React.createElement('path', { d }))
  );
}

// brand logo — interlocking rings (uses the real asset)
function Logo({ size = 38, radius = 10 }) {
  // Marca vetorial: dois anéis da aliança entrelaçados (consistente com o splash).
  return (
    <span aria-label="O Casal Legendário"
      style={{ width: size, height: size, borderRadius: radius, flex: '0 0 auto',
        display: 'inline-grid', placeItems: 'center', background: 'var(--bg-2)',
        border: '1px solid var(--accent-line)' }}>
      <svg width={size * 0.6} height={size * 0.4} viewBox="0 0 46 26" aria-hidden="true">
        <g fill="none" stroke="var(--accent)" strokeWidth="2.3" strokeLinecap="round">
          <circle cx="18" cy="13" r="9" /><circle cx="28" cy="13" r="9" />
        </g>
      </svg>
    </span>
  );
}

// progress ring
function Ring({ pct = 60, size = 48, sw = 5, children, color = 'var(--accent)', track = 'var(--surface-2)' }) {
  const r = (size - sw) / 2, c = 2 * Math.PI * r;
  return (
    <div className="ring-wrap" style={{ width: size, height: size }}>
      <svg width={size} height={size} style={{ transform: 'rotate(-90deg)' }}>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke={track} strokeWidth={sw} />
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke={color} strokeWidth={sw}
          strokeDasharray={c} strokeDashoffset={c * (1 - pct/100)} strokeLinecap="round"
          style={{ transition: 'stroke-dashoffset .6s var(--ease)' }} />
      </svg>
      {children && <div className="ctr">{children}</div>}
    </div>
  );
}

function Eyebrow({ children, className = '', style }) {
  return <div className={'eyebrow ' + className} style={style}>{children}</div>;
}
function Stars({ n = 5, size = 13 }) {
  return <span className="row" style={{ gap: 2, color: 'var(--accent)' }}>
    {Array.from({ length: n }).map((_, i) => <Icon key={i} name="star" size={size} fill />)}
  </span>;
}

// image: real asset with graceful placeholder fallback
function Picture({ src, label = 'imagem', ratio, h, w, r = 16, className = '', style, children }) {
  const [err, setErr] = useState(false);
  const box = { borderRadius: r, ...(ratio ? { aspectRatio: ratio } : {}), ...(h ? { height: h } : {}), ...(w ? { width: w } : {}), ...style };
  if (err || !src) {
    return <div className={'imgph ' + className} style={box}><span>{label}</span>{children}</div>;
  }
  return <div className={'imgph cover ' + className} style={{ ...box, backgroundImage: `url("${src}")` }}>
    <img src={src} alt={label} loading="lazy" decoding="async" onError={() => setErr(true)} style={{ opacity: 0, width: '100%', height: '100%' }} />
    {children}
  </div>;
}

// bottom sheet / modal
function Sheet({ open, onClose, children, modal = false, maxW }) {
  const onCloseRef = useRef(onClose);
  onCloseRef.current = onClose;
  useEffect(() => {
    if (!open) return;
    // empurra uma entrada de history ao abrir: o "Voltar" do sistema (Android standalone) FECHA o modal
    // em vez de navegar/sair. pushState sem mexer no hash NÃO dispara o router (que ouve só hashchange).
    let pushed = false;
    try { history.pushState({ sheet: true }, ''); pushed = true; } catch (e) {}
    const onPop = () => { pushed = false; onCloseRef.current(); };
    const onKey = (e) => { if (e.key === 'Escape') onCloseRef.current(); };
    window.addEventListener('popstate', onPop);
    document.addEventListener('keydown', onKey);
    // trava o scroll do container real (.scroll), não só o body — na moldura mobile o
    // scroll vive no .scroll/wrapper, então travar só o body não impede o fundo de rolar.
    const scroller = document.querySelector('.scroll');
    const prevBodyOverflow = document.body.style.overflow;
    const prevScrollerOverflow = scroller ? scroller.style.overflow : '';
    document.body.style.overflow = 'hidden';
    if (scroller) scroller.style.overflow = 'hidden';
    return () => {
      window.removeEventListener('popstate', onPop);
      document.removeEventListener('keydown', onKey);
      document.body.style.overflow = prevBodyOverflow;
      if (scroller) scroller.style.overflow = prevScrollerOverflow;
      // fechamento por scrim/X/Escape (não pelo Voltar): consome a entrada extra para não poluir o history
      if (pushed && history.state && history.state.sheet) history.back();
    };
  }, [open]);
  if (!open) return null;
  const overlay = (
    <div className={'scrim' + (modal ? ' center' : '')} onClick={onClose}>
      <div className={'sheet' + (modal ? ' modal' : '')} style={maxW ? { maxWidth: maxW } : null}
        onClick={(e) => e.stopPropagation()}>
        {!modal && <div className="sheet-grab" />}
        {children}
      </div>
    </div>
  );
  // Portal no .app: tira o overlay de dentro do .screen-anim (que tem transform e por isso
  // capturava o position:absolute/fixed do scrim, jogando-o "lá embaixo"). Ancora no shell do app
  // → funciona igual no fullscreen real e dentro da moldura iPhone. Fallback inline se não houver portal.
  const root = (typeof document !== 'undefined') && document.querySelector('.app');
  return (root && window.ReactDOM && window.ReactDOM.createPortal)
    ? window.ReactDOM.createPortal(overlay, root)
    : overlay;
}

// little favorite heart toggle
function FavBtn({ on, onToggle, size = 20 }) {
  return <button className="iconbtn" onClick={(e) => { e.stopPropagation(); onToggle(); }}
    aria-label="favoritar" style={{ color: on ? 'var(--accent)' : 'var(--muted)' }}>
    <Icon name="heart" size={size} fill={on} />
  </button>;
}

Object.assign(window, { Icon, Logo, Ring, Eyebrow, Stars, Picture, Sheet, FavBtn,
  useState, useEffect, useRef, useCallback, createContext, useContext });
