/* ===================================================
   ICONS — small, mono-line, consistent
   =================================================== */
const Icon = {
  Search: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="11" cy="11" r="7"></circle>
      <path d="m20 20-3.5-3.5"></path>
    </svg>
  ),
  ArrowRight: () => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M5 12h14"></path><path d="m13 5 7 7-7 7"></path>
    </svg>
  ),
  ArrowUpRight: () => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M7 17 17 7"></path><path d="M7 7h10v10"></path>
    </svg>
  ),
  Close: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M18 6 6 18"></path><path d="m6 6 12 12"></path>
    </svg>
  ),
  Github: () => (
    <svg width="18" height="18" viewBox="0 0 24 24" fill="currentColor">
      <path d="M12 .5C5.65.5.5 5.65.5 12c0 5.08 3.29 9.39 7.86 10.91.58.11.79-.25.79-.56 0-.27-.01-1.16-.02-2.1-3.2.7-3.87-1.36-3.87-1.36-.52-1.32-1.27-1.67-1.27-1.67-1.04-.71.08-.7.08-.7 1.15.08 1.76 1.18 1.76 1.18 1.02 1.75 2.69 1.25 3.35.96.1-.74.4-1.25.73-1.54-2.55-.29-5.24-1.28-5.24-5.69 0-1.26.45-2.29 1.18-3.1-.12-.29-.51-1.46.11-3.04 0 0 .97-.31 3.18 1.18a11 11 0 0 1 5.79 0c2.21-1.49 3.18-1.18 3.18-1.18.62 1.58.23 2.75.11 3.04.74.81 1.18 1.84 1.18 3.1 0 4.42-2.69 5.39-5.25 5.68.41.36.78 1.06.78 2.13 0 1.54-.01 2.78-.01 3.16 0 .31.21.67.8.56C20.21 21.39 23.5 17.08 23.5 12 23.5 5.65 18.35.5 12 .5"/>
    </svg>
  ),
  Twitter: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
      <path d="M18.244 2H21l-6.52 7.45L22.5 22h-6.84l-4.79-6.27L4.96 22H2.2l6.99-7.99L1.5 2h7l4.33 5.74L18.244 2Zm-1.2 18.34h1.6L7.05 3.57H5.34l11.7 16.77Z"/>
    </svg>
  ),
  Linkedin: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
      <path d="M20.45 20.45h-3.55v-5.57c0-1.33-.03-3.04-1.85-3.04-1.86 0-2.14 1.45-2.14 2.95v5.66H9.36V9h3.41v1.56h.05a3.74 3.74 0 0 1 3.36-1.85c3.6 0 4.27 2.37 4.27 5.45v6.29ZM5.34 7.43a2.06 2.06 0 1 1 0-4.12 2.06 2.06 0 0 1 0 4.12Zm1.78 13.02H3.56V9h3.56v11.45ZM22.22 0H1.77C.79 0 0 .77 0 1.72V22.28C0 23.23.79 24 1.77 24h20.45c.98 0 1.78-.77 1.78-1.72V1.72C24 .77 23.2 0 22.22 0Z"/>
    </svg>
  ),
  Instagram: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="3" width="18" height="18" rx="5"></rect>
      <circle cx="12" cy="12" r="4"></circle>
      <circle cx="17.5" cy="6.5" r="1" fill="currentColor"></circle>
    </svg>
  ),
  Facebook: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="currentColor">
      <path d="M24 12.07C24 5.41 18.63 0 12 0S0 5.4 0 12.07C0 18.1 4.39 23.1 10.13 24v-8.44H7.08v-3.49h3.05V9.41c0-3.02 1.79-4.69 4.53-4.69 1.31 0 2.69.24 2.69.24v2.97h-1.51c-1.49 0-1.96.93-1.96 1.89v2.26h3.33l-.53 3.49h-2.8V24C19.6 23.1 24 18.1 24 12.07Z"/>
    </svg>
  ),
  Mail: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <rect x="2" y="4" width="20" height="16" rx="3"></rect>
      <path d="m3 7 9 6 9-6"></path>
    </svg>
  ),
  Globe: () => (
    <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <circle cx="12" cy="12" r="9"></circle>
      <path d="M3 12h18"></path>
      <path d="M12 3a14 14 0 0 1 0 18a14 14 0 0 1 0-18"></path>
    </svg>
  ),
  ExternalLink: () => (
    <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M15 3h6v6"></path>
      <path d="M10 14 21 3"></path>
      <path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"></path>
    </svg>
  ),
};

/* ===================================================
   NAV — floating glass pill, sticky on scroll
   =================================================== */
function navigateWithTransition(href) {
  if (!href || href.startsWith('#')) return false;
  // External / absolute → real navigation
  if (/^https?:/.test(href)) { window.location.href = href; return true; }
  // SPA: tell the router
  window.dispatchEvent(new CustomEvent('spa:navigate', { detail: { href } }));
  return true;
}

function TransitionLink({ href, className, children, ...rest }) {
  const onClick = (e) => {
    if (e.metaKey || e.ctrlKey || e.shiftKey || e.button === 1) return;
    if (!href || href.startsWith('#') || /^https?:/.test(href)) return;
    e.preventDefault();
    navigateWithTransition(href);
  };
  return (
    <a href={href} className={className} onClick={onClick} {...rest}>
      {children}
    </a>
  );
}

function Nav({ active }) {
  const [condensed, setCondensed] = React.useState(false);
  const navRef    = React.useRef(null);
  const pillRef   = React.useRef(null);
  const linksRef  = React.useRef(null);

  React.useEffect(() => {
    const onScroll = () => setCondensed(window.scrollY > 40);
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  // ---- sliding glass pill behind the links ----
  React.useEffect(() => {
    const linksWrap = linksRef.current, pill = pillRef.current, nav = navRef.current;
    if (!linksWrap || !pill || !nav) return;

    const moveTo = (el) => {
      if (!el) return;
      const wrapRect = linksWrap.getBoundingClientRect();
      const r = el.getBoundingClientRect();
      pill.style.width  = r.width  + 'px';
      pill.style.height = r.height + 'px';
      pill.style.transform = `translate3d(${r.left - wrapRect.left}px, ${r.top - wrapRect.top}px, 0)`;
      pill.classList.add('is-visible');
    };
    const hide = () => pill.classList.remove('is-visible');

    const getActive = () => linksWrap.querySelector('.nav__link.is-active');

    const settle = () => {
      const el = getActive();
      if (el) moveTo(el);
      else hide();
    };
    const t = setTimeout(settle, 0);
    window.addEventListener('resize', settle);

    const links = linksWrap.querySelectorAll('.nav__link');
    const onEnter = (e) => moveTo(e.currentTarget);
    const onLeaveWrap = () => settle();

    links.forEach(l => l.addEventListener('mouseenter', onEnter));
    linksWrap.addEventListener('mouseleave', onLeaveWrap);

    return () => {
      clearTimeout(t);
      window.removeEventListener('resize', settle);
      links.forEach(l => l.removeEventListener('mouseenter', onEnter));
      linksWrap.removeEventListener('mouseleave', onLeaveWrap);
    };
  }, [active]);

  return (
    <nav ref={navRef} className={"nav" + (condensed ? " is-condensed" : "")}>
      <TransitionLink href="/" className="nav__brand">
        <img src="/assets/brand/Horum-Labs sin fondo.png" alt="Horum Labs Logo" className="nav__brand-logo" />
        <span>Horum Labs</span>
      </TransitionLink>

      <div className="nav__links" ref={linksRef}>
        <span ref={pillRef} className="nav__pill" aria-hidden="true"></span>
        <TransitionLink href="/" className={"nav__link" + (active === 'home' ? ' is-active' : '')}>Inicio</TransitionLink>
        <TransitionLink href="/repo"  className={"nav__link" + (active === 'repo' ? ' is-active' : '')}>Repositorio</TransitionLink>
      </div>
    </nav>
  );
}

/* ===================================================
   FOOTER
   =================================================== */
function Footer() {
  return (
    <footer className="footer">
      <div className="footer__inner">
        <div>
          <img src="/assets/brand/Horum-Labs sin fondo.png" alt="Horum Labs Logo" className="footer__brand-logo" />
          <h3 className="footer__name">Horum Labs</h3>
          <p className="footer__tagline" style={{ marginTop: '8px' }}>Laboratorio de ideas y software de alto impacto. Construimos con propósito y detalle.</p>
        </div>
        <div className="footer__col">
          <h4>Navegación</h4>
          <ul>
            <li><a href="/">Inicio</a></li>
            <li><a href="/repo">Repositorio</a></li>
            <li><a href="#about">Sobre nosotros</a></li>
            <li><a href="#contact">Contacto</a></li>
          </ul>
        </div>
        <div className="footer__col">
          <h4>Contacto</h4>
          <ul>
            <li><a href="mailto:hola@horumlabs.com">hola@horumlabs.com</a></li>
            <li><a href="mailto:work@horumlabs.com">work@horumlabs.com</a></li>
          </ul>
        </div>
        <div className="footer__col">
          <h4>Social</h4>
          <ul>
            <li><a href="#">Instagram</a></li>
            <li><a href="#">Facebook</a></li>
            <li><a href="#">GitHub</a></li>
            <li><a href="#">LinkedIn</a></li>
          </ul>
        </div>
      </div>
      <div className="footer__bottom">
        <span>© 2026 Horum Labs — todos los derechos reservados</span>
        <span>v0.1.0 · build {new Date().toISOString().slice(0,10)}</span>
      </div>
    </footer>
  );
}

/* ===================================================
   DATA HOOK — fetch JSON helper
   =================================================== */
function useJson(path) {
  const [data, setData] = React.useState(null);
  const [error, setError] = React.useState(null);
  React.useEffect(() => {
    let alive = true;
    fetch(path)
      .then(r => r.json())
      .then(d => { if (alive) setData(d); })
      .catch(e => { if (alive) setError(e); });
    return () => { alive = false; };
  }, [path]);
  return { data, error };
}

/* ===================================================
   FLASHLIGHT GLOW — soft background illumination
   =================================================== */
function FlashlightGlow() {
  const glowRef = React.useRef(null);

  React.useEffect(() => {
    const onMove = (e) => {
      if (glowRef.current) {
        // We set CSS variables for the mouse position
        glowRef.current.style.setProperty('--x', e.clientX + 'px');
        glowRef.current.style.setProperty('--y', e.clientY + 'px');
      }
    };
    window.addEventListener('mousemove', onMove, { passive: true });

    return () => {
      window.removeEventListener('mousemove', onMove);
    };
  }, []);

  return <div ref={glowRef} className="flashlight-glow"></div>;
}

/* expose to window for cross-script access */
Object.assign(window, { Icon, Nav, Footer, useJson, FlashlightGlow, TransitionLink, navigateWithTransition });
