/* global React */
const { useState, useEffect, useRef, useCallback } = React;

/* ----------------------------------------------------------
   Icon set — clean line icons (stroke = currentColor)
---------------------------------------------------------- */
function Icon({ name, size = 24, stroke = 1.7, style }) {
  const p = {
    fill: "none",
    stroke: "currentColor",
    strokeWidth: stroke,
    strokeLinecap: "round",
    strokeLinejoin: "round",
  };
  const paths = {
    chat: <><path {...p} d="M21 11.5a8.38 8.38 0 0 1-8.5 8.5 9 9 0 0 1-4-1L3 20l1.3-4.5A8.38 8.38 0 0 1 3.5 11 8.5 8.5 0 0 1 12 3a8.38 8.38 0 0 1 9 8.5z"/><circle cx="8.5" cy="11.5" r="1" fill="currentColor" stroke="none"/><circle cx="12" cy="11.5" r="1" fill="currentColor" stroke="none"/><circle cx="15.5" cy="11.5" r="1" fill="currentColor" stroke="none"/></>,
    calendar: <><rect {...p} x="3" y="4.5" width="18" height="16" rx="2.5"/><path {...p} d="M3 9h18M8 2.5v4M16 2.5v4"/><path {...p} d="M8.5 14l2 2 4-4.5"/></>,
    doc: <><path {...p} d="M14 3H7a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h10a2 2 0 0 0 2-2V8z"/><path {...p} d="M14 3v5h5M8.5 13h7M8.5 16.5h5M8.5 9.5h2"/></>,
    track: <><path {...p} d="M3 7.5l9-4.5 9 4.5v9L12 21 3 16.5z"/><path {...p} d="M3 7.5l9 4.5 9-4.5M12 12v9"/></>,
    layout: <><rect {...p} x="3" y="3.5" width="18" height="17" rx="2.5"/><path {...p} d="M3 9h18M9 9v11.5"/></>,
    cog: <><circle {...p} cx="12" cy="12" r="3.2"/><path {...p} d="M12 2.5v2.4M12 19.1v2.4M21.5 12h-2.4M4.9 12H2.5M18.7 5.3l-1.7 1.7M7 17l-1.7 1.7M18.7 18.7L17 17M7 7L5.3 5.3"/></>,
    clock: <><circle {...p} cx="12" cy="12" r="9"/><path {...p} d="M12 7.5V12l3 2"/></>,
    bolt: <><path {...p} d="M13 2L4.5 13.5H11l-1 8.5L18.5 10H12z"/></>,
    inbox: <><path {...p} d="M3 13l3-8.5a2 2 0 0 1 1.9-1.3h8.2A2 2 0 0 1 18 4.5L21 13v5a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2z"/><path {...p} d="M3 13h5a2 2 0 0 1 2 2 2 2 0 0 0 4 0 2 2 0 0 1 2-2h5"/></>,
    users: <><circle {...p} cx="9" cy="8" r="3.4"/><path {...p} d="M2.5 20a6.5 6.5 0 0 1 13 0M17 5.2a3.4 3.4 0 0 1 0 5.6M21.5 20a6.4 6.4 0 0 0-4-5.9"/></>,
    pulse: <><path {...p} d="M3 12h4l2.5-6 4 13L17 12h4"/></>,
    check: <><path {...p} d="M4 12.5l5 5L20 6"/></>,
    arrow: <><path {...p} d="M5 12h14M13 6l6 6-6 6"/></>,
    spark: <><path {...p} d="M12 3v5M12 16v5M3 12h5M16 12h5M6 6l3 3M15 15l3 3M18 6l-3 3M9 15l-3 3"/></>,
    shield: <><path {...p} d="M12 2.5l8 3v6c0 5-3.5 8.4-8 10-4.5-1.6-8-5-8-10v-6z"/><path {...p} d="M8.5 12l2.2 2.2L15.5 9.5"/></>,
    building: <><rect {...p} x="4" y="3" width="16" height="18" rx="2"/><path {...p} d="M9 7h2M13 7h2M9 11h2M13 11h2M9 15h2M13 15h2M9 21v-3h6v3"/></>,
    bed: <><path {...p} d="M3 7v11M3 13h18v5M21 13v-2a3 3 0 0 0-3-3H9v5"/><circle {...p} cx="6.5" cy="11" r="1.6"/></>,
    truck: <><path {...p} d="M3 5.5h11v9H3zM14 8.5h4l3 3v3h-7z"/><circle {...p} cx="7" cy="17.5" r="2"/><circle {...p} cx="17.5" cy="17.5" r="2"/></>,
    cart: <><circle {...p} cx="9" cy="20" r="1.6"/><circle {...p} cx="17" cy="20" r="1.6"/><path {...p} d="M2.5 3.5h2.3l2.2 11.2a1.5 1.5 0 0 0 1.5 1.2h8.3a1.5 1.5 0 0 0 1.5-1.2L21 7H6"/></>,
    briefcase: <><rect {...p} x="3" y="7.5" width="18" height="12" rx="2.5"/><path {...p} d="M8.5 7.5V6a2 2 0 0 1 2-2h3a2 2 0 0 1 2 2v1.5M3 13h18"/></>,
    plane: <><path {...p} d="M10.5 2.5l1 7L21 13.5v2l-9.5-2 .5 5L15 20v1.5L12 21l-3 .5V20l3-1.5.5-5L3 15.5v-2L11.5 9.5l-1-7z"/></>,
    whatsapp: <><path d="M12 2a10 10 0 0 0-8.6 15l-1.3 4.7L7 20.4A10 10 0 1 0 12 2z" fill="none" stroke="currentColor" strokeWidth={stroke}/><path d="M9 7.6c-.2-.5-.4-.5-.6-.5h-.5a1 1 0 0 0-.7.35A2.9 2.9 0 0 0 6.3 9.6c0 1.3.95 2.55 1.08 2.72.13.18 1.86 2.95 4.6 4 2.27.9 2.73.72 3.22.67.5-.04 1.6-.65 1.82-1.28.22-.63.22-1.17.16-1.28-.07-.11-.25-.18-.52-.31-.27-.14-1.6-.79-1.84-.88-.25-.09-.43-.13-.6.13-.18.27-.7.88-.85 1.06-.16.18-.31.2-.58.07a7.4 7.4 0 0 1-2.17-1.34 8.2 8.2 0 0 1-1.5-1.87c-.16-.27-.02-.42.12-.55.12-.12.27-.31.4-.47.14-.16.18-.27.27-.45.09-.18.04-.34-.02-.47C9.5 9 9.18 8.06 9 7.6z" fill="currentColor" stroke="none"/></>,
    menu: <><path {...p} d="M3 6h18M3 12h18M3 18h18"/></>,
    close: <><path {...p} d="M6 6l12 12M18 6L6 18"/></>,
    send: <><path {...p} d="M21 3L3 10.5l7 2.5 2.5 7z"/><path {...p} d="M21 3l-11 10"/></>,
    mail: <><rect {...p} x="3" y="5" width="18" height="14" rx="2.5"/><path {...p} d="M3.5 7l8.5 6 8.5-6"/></>,
    location: <><path {...p} d="M12 21c4-4.5 7-7.6 7-11a7 7 0 1 0-14 0c0 3.4 3 6.5 7 11z"/><circle {...p} cx="12" cy="10" r="2.5"/></>,
    bell: <><path {...p} d="M18 8.5a6 6 0 1 0-12 0c0 6-2.5 7.5-2.5 7.5h17S18 14.5 18 8.5z"/><path {...p} d="M10 20a2.2 2.2 0 0 0 4 0"/></>,
    trend: <><path {...p} d="M3 17l5.5-5.5 3.5 3.5L21 6"/><path {...p} d="M16 6h5v5"/></>,
    instagram: <><rect {...p} x="3" y="3" width="18" height="18" rx="5"/><circle {...p} cx="12" cy="12" r="4"/><circle cx="17.3" cy="6.7" r="1.1" fill="currentColor" stroke="none"/></>,
    telegram: <><path {...p} d="M21 4L3 11l5 2 2 6 3-4 5 4z"/><path {...p} d="M8 13l9-6-6 7"/></>,
    messenger: <><path {...p} d="M12 3c-5 0-9 3.7-9 8.3 0 2.6 1.3 4.9 3.3 6.4V22l3-1.7c.9.2 1.8.4 2.7.4 5 0 9-3.7 9-8.3S17 3 12 3z"/><path {...p} d="M7 13l3-3 2.2 2L16 9l-3 3-2.2-2z"/></>,
    headset: <><path {...p} d="M4 13v-1a8 8 0 0 1 16 0v1"/><rect {...p} x="2.5" y="13" width="4" height="7" rx="1.6"/><rect {...p} x="17.5" y="13" width="4" height="7" rx="1.6"/><path {...p} d="M20 19.5a3 3 0 0 1-3 2.5h-3"/></>,
    crown: <><path {...p} d="M3 17l1.5-9 4.5 5 3-7 3 7 4.5-5L21 17z"/><path {...p} d="M3 17h18v3H3z"/></>,
    bars: <><path {...p} d="M4 20V10M9.5 20V4M15 20v-7M20.5 20V8"/></>,
    dollar: <><circle {...p} cx="12" cy="12" r="9"/><path {...p} d="M14.8 9c-.6-1-1.7-1.4-2.8-1.4-1.5 0-2.6.8-2.6 2 0 1.4 1.4 1.8 2.8 2.1 1.4.3 2.8.7 2.8 2.1 0 1.2-1.1 2-2.6 2-1.2 0-2.3-.5-2.8-1.5M12 6v12"/></>,
    play: <><circle {...p} cx="12" cy="12" r="9"/><path d="M10 8.5l5.5 3.5L10 15.5z" fill="currentColor" stroke="none"/></>,
    bot: <><rect {...p} x="4" y="8" width="16" height="11" rx="3.5"/><path {...p} d="M12 4.5v3.5M12 3.2v.1"/><circle cx="9" cy="13.5" r="1.4" fill="currentColor" stroke="none"/><circle cx="15" cy="13.5" r="1.4" fill="currentColor" stroke="none"/><circle {...p} cx="12" cy="4" r="1.2"/></>,
    grid: <><rect {...p} x="3.5" y="3.5" width="7" height="7" rx="1.8"/><rect {...p} x="13.5" y="3.5" width="7" height="7" rx="1.8"/><rect {...p} x="3.5" y="13.5" width="7" height="7" rx="1.8"/><rect {...p} x="13.5" y="13.5" width="7" height="7" rx="1.8"/></>,
    card: <><rect {...p} x="2.5" y="5" width="19" height="14" rx="2.5"/><path {...p} d="M2.5 9.5h19M6 15h4"/></>,
    book: <><path {...p} d="M4 4.5A2 2 0 0 1 6 3h13v15H6a2 2 0 0 0-2 2z"/><path {...p} d="M4 19.5A2 2 0 0 1 6 18h13v3H6a2 2 0 0 1-2-2z"/></>,
    sparkle2: <><path {...p} d="M12 3l1.8 5.2L19 10l-5.2 1.8L12 17l-1.8-5.2L5 10l5.2-1.8z"/><path {...p} d="M18 15l.7 2 2 .7-2 .7-.7 2-.7-2-2-.7 2-.7z"/></>,
    globe: <><circle {...p} cx="12" cy="12" r="9"/><path {...p} d="M3 12h18M12 3c2.5 2.5 2.5 15 0 18M12 3c-2.5 2.5-2.5 15 0 18"/></>,
    code: <><path {...p} d="M8.5 8.5L4 12l4.5 3.5M15.5 8.5L20 12l-4.5 3.5M13.5 5l-3 14"/></>,
    database: <><ellipse {...p} cx="12" cy="5.5" rx="7.5" ry="3"/><path {...p} d="M4.5 5.5v6c0 1.66 3.36 3 7.5 3s7.5-1.34 7.5-3v-6M4.5 11.5v6c0 1.66 3.36 3 7.5 3s7.5-1.34 7.5-3v-6"/></>,
    mobile: <><rect {...p} x="6.5" y="2.5" width="11" height="19" rx="2.6"/><path {...p} d="M10.5 5.5h3M10.5 18.5h3"/></>,
    server: <><rect {...p} x="3" y="4" width="18" height="7" rx="2"/><rect {...p} x="3" y="13" width="18" height="7" rx="2"/><path {...p} d="M7 7.5h.01M7 16.5h.01"/></>,
    rocket: <><path {...p} d="M5 15c-1.5 1-2 4-2 4s3-.5 4-2c.6-.9.5-2-.3-2.7-.8-.8-1.8-.9-1.7.7z"/><path {...p} d="M9 13l-2-2c1-4 4-7.5 9-8 .5 5-3.5 8-8 9l-2-2"/><circle {...p} cx="14.5" cy="9.5" r="1.4"/></>,
    lock: <><rect {...p} x="4.5" y="10.5" width="15" height="10" rx="2.5"/><path {...p} d="M8 10.5V7.5a4 4 0 0 1 8 0v3"/></>,
    cloud: <><path {...p} d="M7 18a4 4 0 0 1-.5-7.97A5.5 5.5 0 0 1 17 9.5a3.75 3.75 0 0 1 .5 8.5z"/></>,
    api: <><path {...p} d="M12 3l8 4.5v9L12 21l-8-4.5v-9z"/><path {...p} d="M12 12l8-4.5M12 12v9M12 12L4 7.5"/></>,
  };
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" style={style} aria-hidden="true">
      {paths[name] || null}
    </svg>
  );
}

/* ----------------------------------------------------------
   Logo — official STAMINEX hexagon mark (warm-recolored) + wordmark
---------------------------------------------------------- */
function Logo({ light = false, size = 30 }) {
  const txt = light ? "#ffffff" : "#251f17";
  const sub = light ? "rgba(231,221,202,.78)" : "rgba(108,99,84,.85)";
  return (
    <span style={{ display: "inline-flex", alignItems: "center", gap: 11 }}>
      <span style={{
        width: size + 12, height: size + 12, borderRadius: 11, flex: "none",
        background: "linear-gradient(155deg, #1d2a3d, #0d1726)",
        display: "inline-flex", alignItems: "center", justifyContent: "center",
        boxShadow: "0 6px 16px rgba(15,30,55,.32)", border: "1px solid rgba(255,255,255,.1)",
      }}>
        <img src="assets/staminex-mark.png" alt="STAMINEX" style={{ height: size * 0.74, width: "auto", display: "block" }} />
      </span>
      <span style={{ display: "inline-flex", flexDirection: "column", lineHeight: 1, gap: 3 }}>
        <span style={{
          fontFamily: "var(--font-display)", fontWeight: 800, fontSize: size * 0.82,
          letterSpacing: "-0.01em", color: txt,
        }}>
          STAMINEX
        </span>
        <span style={{
          fontFamily: "var(--font-body)", fontWeight: 700, fontSize: size * 0.225,
          letterSpacing: "0.3em", color: sub,
        }}>
          SOLUCIONES DIGITALES
        </span>
      </span>
    </span>
  );
}

/* ----------------------------------------------------------
   Animated counter — counts up when scrolled into view
---------------------------------------------------------- */
function Counter({ end, duration = 1400, suffix = "", prefix = "", decimals = 0 }) {
  const ref = useRef(null);
  const [val, setVal] = useState(0);
  const done = useRef(false);
  useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const io = new IntersectionObserver((entries) => {
      entries.forEach((e) => {
        if (e.isIntersecting && !done.current) {
          done.current = true;
          const start = performance.now();
          const tick = (now) => {
            const t = Math.min((now - start) / duration, 1);
            const eased = 1 - Math.pow(1 - t, 3);
            setVal(end * eased);
            if (t < 1) requestAnimationFrame(tick);
            else setVal(end);
          };
          requestAnimationFrame(tick);
        }
      });
    }, { threshold: 0.4 });
    io.observe(el);
    return () => io.disconnect();
  }, [end, duration]);
  const display = decimals > 0 ? val.toFixed(decimals) : Math.round(val).toLocaleString("es");
  return <span ref={ref}>{prefix}{display}{suffix}</span>;
}

Object.assign(window, { Icon, Logo, Counter });
