// Custom cursor + scroll reveal observer
function useCustomCursor() {
  React.useEffect(() => {
    if (window.matchMedia('(max-width: 900px)').matches) return;
    const dot = document.getElementById('cursor-dot');
    const ring = document.getElementById('cursor-ring');
    if (!dot || !ring) return;

    let mx = window.innerWidth / 2, my = window.innerHeight / 2;
    let rx = mx, ry = my;
    let raf;

    const onMove = (e) => {
      mx = e.clientX; my = e.clientY;
      dot.style.transform = `translate(${mx}px, ${my}px) translate(-50%, -50%)`;
    };

    const tick = () => {
      rx += (mx - rx) * 0.18;
      ry += (my - ry) * 0.18;
      ring.style.transform = `translate(${rx}px, ${ry}px) translate(-50%, -50%)`;
      raf = requestAnimationFrame(tick);
    };

    const hoverables = 'a, button, [data-hover], .servicio__row, .caso, .fuerza-card, .chip, .metodologia__step';
    const onOver = (e) => {
      if (e.target.closest(hoverables)) document.body.classList.add('cursor-hover');
      else document.body.classList.remove('cursor-hover');
      if (e.target.closest('input, textarea')) document.body.classList.add('cursor-text');
      else document.body.classList.remove('cursor-text');
    };

    window.addEventListener('mousemove', onMove);
    window.addEventListener('mouseover', onOver);
    raf = requestAnimationFrame(tick);
    return () => {
      window.removeEventListener('mousemove', onMove);
      window.removeEventListener('mouseover', onOver);
      cancelAnimationFrame(raf);
    };
  }, []);
}

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

Object.assign(window, { useCustomCursor, useReveal });
