// Primitivas editoriales: trazos a mano, sellos, papel con borde dentado
const useInView = (threshold = 0.35) => {
  const ref = React.useRef(null);
  const [seen, setSeen] = React.useState(false);
  React.useEffect(() => {
    if (!ref.current || seen) return;
    const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) { setSeen(true); io.disconnect(); } }, { threshold });
    io.observe(ref.current);
    return () => io.disconnect();
  }, [seen, threshold]);
  return [ref, seen];
};

const HandUnderline = ({ children, color = 'var(--olive)', show = true }) => (
  <span style={{ position: 'relative', display: 'inline-block', whiteSpace: 'nowrap' }}>
    {children}
    {show && (
      <svg viewBox="0 0 300 24" preserveAspectRatio="none" aria-hidden="true"
        style={{ position: 'absolute', left: '-3%', bottom: '-0.14em', width: '106%', height: '0.3em', overflow: 'visible', pointerEvents: 'none' }}>
        <path className="draw" pathLength="1" d="M4 12 L 296 12" fill="none" stroke={color} strokeWidth="6" strokeLinecap="round"/>
      </svg>
    )}
  </span>
);

const HandCircle = ({ show, color = 'var(--bad)', pad = 6, width = 2.4 }) => show ? (
  <svg viewBox="0 0 100 40" preserveAspectRatio="none" aria-hidden="true" style={{
    position: 'absolute', left: -pad, top: -pad, width: `calc(100% + ${pad * 2}px)`, height: `calc(100% + ${pad * 2}px)`,
    overflow: 'visible', pointerEvents: 'none',
  }}>
    <path className="draw" pathLength="1" d="M10 21 C 7 8, 40 2, 63 4 C 89 6, 98 15, 94 25 C 90 36, 55 39, 31 36 C 11 33, 2 26, 9 15 C 13 9, 22 6, 31 5"
      fill="none" stroke={color} strokeWidth={width} strokeLinecap="round"/>
  </svg>
) : null;

const HandStrike = ({ show = true, color = 'var(--bad)' }) => show ? (
  <svg viewBox="0 0 100 20" preserveAspectRatio="none" aria-hidden="true" style={{
    position: 'absolute', left: '-5%', top: '36%', width: '110%', height: '.3em', overflow: 'visible', pointerEvents: 'none',
  }}>
    <path className="draw" pathLength="1" d="M2 15 C 28 9, 62 12, 98 3" fill="none" stroke={color} strokeWidth="3.2" strokeLinecap="round"/>
  </svg>
) : null;

const HandArrow = ({ color = 'var(--ink)', style }) => (
  <svg width="70" height="40" viewBox="0 0 70 40" fill="none" aria-hidden="true" style={style}>
    <path d="M3 30 C 18 8, 40 4, 62 14" stroke={color} strokeWidth="2" strokeLinecap="round"/>
    <path d="M52 6 L 63 14 L 51 20" stroke={color} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
  </svg>
);

const Stamp = ({ children, color = 'var(--bad)', rotate = -8, size = 1, style }) => (
  <div className="stamp" style={{ color, transform: `rotate(${rotate}deg)`, fontSize: 12 * size, ...style }}>{children}</div>
);

const zigzag = (n = 26, h = 8) => {
  const pts = ['0 0', '100% 0'];
  for (let i = n; i >= 0; i--) pts.push(`${(i / n * 100).toFixed(3)}% ${i % 2 ? `calc(100% - ${h}px)` : '100%'}`);
  return `polygon(${pts.join(', ')})`;
};

const Receipt = ({ children, pad = '28px 28px 38px', bg = 'var(--paper)', teeth = 26, style }) => (
  <div style={{ background: bg, padding: pad, clipPath: zigzag(teeth), WebkitClipPath: zigzag(teeth), ...style }}>{children}</div>
);

const Leader = ({ label, value, size = 40 }) => (
  <div style={{ display: 'flex', alignItems: 'baseline', gap: 14, padding: '18px 0', borderBottom: '1px solid var(--line-strong)' }}>
    <span style={{ fontSize: 17, color: 'var(--ink-soft)', flexShrink: 1, minWidth: 0 }}>{label}</span>
    <span aria-hidden="true" style={{ flex: 1, minWidth: 24, borderBottom: '2px dotted var(--line-strong)', transform: 'translateY(-6px)' }}/>
    <span className="display" style={{ fontSize: size, color: 'var(--primary)', lineHeight: 1, flexShrink: 0 }}>{value}</span>
  </div>
);

// Reemplaza el encabezado de sección compartido (lo usan también Módulos y Sistema)
const EdHead = ({ eyebrow, title, blurb, center, maxWidth, light }) => (
  <header style={{
    marginBottom: 64, textAlign: center ? 'center' : 'left',
    maxWidth: center ? (maxWidth || 920) : 'none', marginLeft: center ? 'auto' : 0, marginRight: center ? 'auto' : 0,
  }}>
    <div className="kicker" style={{ justifyContent: center ? 'center' : 'flex-start', color: light ? 'var(--accent-soft)' : undefined }}><span>{eyebrow}</span></div>
    <h2 className="display" style={{ fontSize: 'clamp(40px, 5.6vw, 82px)', margin: '20px 0 0', maxWidth: center ? 'none' : (maxWidth || 1000) }}>{title}</h2>
    {blurb && <p className="body-lg" style={{
      maxWidth: 640, margin: '26px 0 0', marginLeft: center ? 'auto' : 0, marginRight: center ? 'auto' : 0,
      color: light ? 'rgba(246,241,231,.8)' : undefined,
    }}>{blurb}</p>}
  </header>
);

Object.assign(window, { useInView, HandUnderline, HandCircle, HandStrike, HandArrow, Stamp, Receipt, Leader, SectionHead: EdHead });
