// Gaussia — shared UI components & helpers
const { useEffect, useRef, useState } = React;

// Logo mark — recreated as SVG (compass + diamond) to scale crisply on dark bg
function GaussiaMark({ size = 28, color = 'var(--accent)' }) {
  return (
    <svg width={size} height={size * 1.05} viewBox="0 0 100 105" fill="none" aria-hidden="true">
      {/* compass top circle */}
      <circle cx="50" cy="14" r="6" stroke={color} strokeWidth="5" fill="none" />
      {/* left leg */}
      <path d="M50 20 L18 92" stroke={color} strokeWidth="6" strokeLinecap="round" />
      {/* right leg */}
      <path d="M50 20 L82 92" stroke={color} strokeWidth="6" strokeLinecap="round" />
      {/* diamond inside */}
      <path d="M50 38 L66 60 L50 82 L34 60 Z" stroke={color} strokeWidth="5" fill="none" strokeLinejoin="round" />
    </svg>
  );
}

function Logo({ size = 24 }) {
  return (
    <a href="#top" style={{ display: 'inline-flex', alignItems: 'center', gap: 10 }}>
      <GaussiaMark size={size} />
      <span style={{
        fontFamily: 'var(--sans)',
        fontWeight: 500,
        letterSpacing: '0.04em',
        fontSize: size * 0.62,
        textTransform: 'uppercase',
      }}>Gaussia</span>
    </a>
  );
}

// Reveal-on-scroll observer
function useReveal() {
  useEffect(() => {
    document.body.classList.add('js-reveal-on');
    const els = document.querySelectorAll('.reveal');
    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 -60px 0px' });
    els.forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);
}

// Arrow icon
function Arrow({ size = 14 }) {
  return (
    <svg className="arrow" width={size} height={size} viewBox="0 0 14 14" fill="none">
      <path d="M3 7H11M11 7L7 3M11 7L7 11" stroke="currentColor" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
    </svg>
  );
}

// Plus icon for FAQ
function Plus({ open }) {
  return (
    <svg width="20" height="20" viewBox="0 0 20 20" fill="none" style={{
      transition: 'transform 0.4s cubic-bezier(0.2,0.8,0.2,1)',
      transform: open ? 'rotate(45deg)' : 'rotate(0deg)',
    }}>
      <path d="M10 4V16M4 10H16" stroke="currentColor" strokeWidth="1.4" strokeLinecap="round" />
    </svg>
  );
}

// Numbered counter
function StepNum({ n }) {
  return (
    <span style={{
      fontFamily: 'var(--mono)',
      fontSize: 11,
      color: 'var(--text-faint)',
      letterSpacing: '0.1em',
    }}>{String(n).padStart(2, '0')}</span>
  );
}

// Responsive hooks
function useMediaQuery(breakpoint = 768) {
  const [matches, setMatches] = useState(() => window.innerWidth <= breakpoint);
  useEffect(() => {
    const mq = window.matchMedia(`(max-width: ${breakpoint}px)`);
    const handler = (e) => setMatches(e.matches);
    mq.addEventListener('change', handler);
    return () => mq.removeEventListener('change', handler);
  }, [breakpoint]);
  return matches;
}

function useResponsive() {
  const isMobile = useMediaQuery(480);
  const isTablet = useMediaQuery(768);
  const isSmallDesktop = useMediaQuery(1024);
  return { isMobile, isTablet, isSmallDesktop };
}

// Mobile menu overlay
function MobileMenu({ open, onClose, links }) {
  // Prevent body scroll when open
  useEffect(() => {
    if (open) {
      document.body.style.overflow = 'hidden';
    } else {
      document.body.style.overflow = '';
    }
    return () => { document.body.style.overflow = ''; };
  }, [open]);

  return (
    <div className={`mobile-menu ${open ? 'open' : ''}`}>
      {links.map(l => (
        <a key={l.href} href={l.href} onClick={onClose}>{l.label}</a>
      ))}
      <a href="https://cal.eu/gaussia" target="_blank" rel="noopener noreferrer"
        className="btn btn-primary" style={{ marginTop: 16 }}
        onClick={onClose}>
        Prendre RDV <Arrow />
      </a>
    </div>
  );
}

Object.assign(window, { GaussiaMark, Logo, useReveal, Arrow, Plus, StepNum, useMediaQuery, useResponsive, MobileMenu });
