// how-it-works.jsx — Dark "How it works" section (Gladia-style scroll-driven)

function HowItWorksSection() {
  const steps = [
    {
      num: 1,
      title: 'Conecta tu número',
      desc: 'Vinculas tu WhatsApp y tu teléfono en 2 minutos. Sin instalar nada, sin tocar código.',
      bullets: [
        'Tu mismo número de WhatsApp y llamadas, sin cambiar nada',
        'Calendario, agenda de citas y herramientas que ya usas',
        'Todo en un solo lugar, desde el celular o la computadora',
        'En menos de 10 minutos lo tienes andando',
      ],
      illustration: 'channels',
    },
    {
      num: 2,
      title: 'Cuéntale tu negocio',
      desc: 'Le dices a qué te dedicas, qué responder, cómo cobrar, cuándo pasarte una conversación. Él entiende y se adapta.',
      bullets: [
        'Le subes tu menú, lista de precios o lo que vendas',
        'Eliges cómo habla: formal, cercano, con tu nombre o el de tu marca',
        'Pruebas todo antes de soltarlo con tus clientes',
        'Si responde algo raro, lo corriges en un clic',
      ],
      illustration: 'train',
    },
    {
      num: 3,
      title: 'Y se pone a trabajar',
      desc: 'Atiende cada llamada y cada mensaje al instante, agenda citas, recupera ventas y te avisa cuando hace falta un humano. Sin descansar, sin pedirte permiso.',
      bullets: [
        'Responde en menos de 2 segundos, a cualquier hora',
        'Agenda citas en tu calendario y manda recordatorios',
        'Sabe quién quiere comprar de verdad y quién solo pregunta',
        'Tú ves todo lo que pasa, en vivo, desde tu celular',
      ],
      illustration: 'automate',
    },
  ];

  return (
    <section data-dark-section style={{
      background: '#0A0A0F',
      position: 'relative',
      overflow: 'hidden',
    }}>
      {/* Smooth gradient transition from light to dark */}
      <div style={{
        position: 'absolute', top: 0, left: 0, right: 0, height: 'clamp(200px, 25vw, 400px)',
        background: 'linear-gradient(to bottom, #fff, #0A0A0F)',
        pointerEvents: 'none',
        zIndex: 1,
      }} />

      {/* Intro statement (Gladia-style) — bridge between hero and HowItWorks header */}
      <div style={{
        position: 'relative',
        padding: 'clamp(140px, 16vw, 240px) 24px clamp(80px, 10vw, 140px)',
        textAlign: 'center',
        zIndex: 2,
      }}>
        <h2 className="heading-display" style={{
          fontSize: 'clamp(40px, 6.5vw, 78px)',
          color: '#fff',
          textWrap: 'balance',
          maxWidth: 980, margin: '0 auto',
          lineHeight: 1.06,
          letterSpacing: '-0.025em',
        }}>
          La diferencia entre cerrar la venta o perderla está en quién responde primero
        </h2>
        <p style={{
          fontSize: 'clamp(16px, 1.6vw, 20px)',
          color: 'rgba(255,255,255,0.55)',
          lineHeight: 1.6, marginTop: 32,
          maxWidth: 640, marginLeft: 'auto', marginRight: 'auto',
          textWrap: 'pretty',
        }}>
          Cada minuto que un cliente espera, sube la probabilidad de que se vaya con otro. Tu agente responde antes de que termine de escribir la pregunta
        </p>
      </div>

      {/* Section header */}
      <div style={{
        position: 'relative',
        padding: 'clamp(100px, 14vw, 200px) 24px clamp(60px, 8vw, 100px)',
        textAlign: 'center',
      }}>
        <h2 className="heading-display" style={{
          fontSize: 'clamp(30px, 5vw, 58px)',
          color: '#fff',
          textWrap: 'balance',
          maxWidth: 800, margin: '0 auto',
        }}>
          Listo en una tarde. En tres pasos
        </h2>
        <p style={{
          fontSize: 'clamp(15px, 1.6vw, 18px)',
          color: 'rgba(255,255,255,0.40)',
          lineHeight: 1.65, marginTop: 24,
          maxWidth: 580, marginLeft: 'auto', marginRight: 'auto',
          textWrap: 'pretty',
        }}>
          No necesitas saber nada de tecnología. Lo conectas, le enseñas tu negocio, y empieza a trabajar. Tú haces lo de siempre
        </p>
      </div>

      {/* Steps — scroll-driven */}
      <div style={{ position: 'relative', maxWidth: 1200, margin: '0 auto' }}>
        {steps.map((step, i) => (
          <HIWStep key={step.num} step={step} isLast={i === steps.length - 1} />
        ))}
      </div>
    </section>
  );
}

function HIWStep({ step, isLast }) {
  const ref = React.useRef(null);
  const [visible, setVisible] = React.useState(false);

  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    const observer = new IntersectionObserver(([entry]) => {
      if (entry.isIntersecting) {
        setVisible(true);
      }
    }, { threshold: 0.15, rootMargin: '0px 0px -60px 0px' });
    observer.observe(el);
    return () => observer.disconnect();
  }, []);

  return (
    <div ref={ref} style={{
      display: 'grid',
      minHeight: '80vh',
      padding: '0 24px',
      paddingBottom: isLast ? 'clamp(80px, 10vw, 140px)' : 0,
    }} className="hiw-step-row">

      {/* Left — text */}
      <div style={{
        paddingTop: 'clamp(64px, 8vw, 100px)',
        paddingBottom: 'clamp(48px, 6vw, 80px)',
        opacity: visible ? 1 : 0,
        transform: visible ? 'translateY(0)' : 'translateY(28px)',
        transition: 'opacity 500ms var(--ease-out), transform 500ms var(--ease-out)',
      }} className="hiw-step-text">
        <div className="hiw-step-badge">Paso {step.num}</div>

        <h3 className="heading-display" style={{
          fontSize: 'clamp(36px, 5vw, 56px)',
          color: '#fff', marginBottom: 18, lineHeight: 1.05,
        }}>
          {step.title}
        </h3>

        <p style={{
          fontSize: 'clamp(15px, 1.4vw, 17px)',
          color: 'rgba(255,255,255,0.42)',
          lineHeight: 1.65, marginBottom: 32,
          maxWidth: 480,
        }}>
          {step.desc}
        </p>

        <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
          {step.bullets.map((b, j) => (
            <div key={j} style={{
              display: 'flex', alignItems: 'flex-start', gap: 14,
              opacity: visible ? 1 : 0,
              transform: visible ? 'translateY(0)' : 'translateY(12px)',
              transition: `opacity 420ms var(--ease-out) ${300 + j * 60}ms, transform 420ms var(--ease-out) ${300 + j * 60}ms`,
            }}>
              <svg width="18" height="18" viewBox="0 0 18 18" fill="none" style={{ flexShrink: 0, marginTop: 2 }}>
                <path d="M5.5 9.2L7.8 11.5L12.5 6.5" stroke="var(--accent)" strokeWidth="1.5" strokeLinecap="round" strokeLinejoin="round" />
              </svg>
              <span style={{
                fontSize: 'clamp(13.5px, 1.2vw, 15px)',
                color: 'rgba(255,255,255,0.52)',
                lineHeight: 1.55,
              }}>{b}</span>
            </div>
          ))}
        </div>
      </div>

      {/* Vertical divider (visible on desktop) */}
      <div className="hiw-divider" style={{
        display: 'none',
        background: 'linear-gradient(to bottom, transparent, rgba(255,255,255,0.06) 20%, rgba(255,255,255,0.06) 80%, transparent)',
      }}></div>

      {/* Right — illustration */}
      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        paddingTop: 'clamp(0px, 2vw, 40px)',
        paddingBottom: 'clamp(48px, 6vw, 80px)',
        opacity: visible ? 1 : 0,
        transform: visible ? 'translateY(0)' : 'translateY(20px)',
        transition: 'opacity 540ms var(--ease-out) 180ms, transform 540ms var(--ease-out) 180ms',
      }} className="hiw-step-illus">
        <StepIllustration type={step.illustration} />
      </div>
    </div>
  );
}

// iOS status bar reusable
function IPhoneStatusBar() {
  return (
    <div className="iphone-statusbar">
      <span>9:41</span>
      <span className="iphone-statusbar-icons">
        <svg width="15" height="11" viewBox="0 0 15 11" fill="currentColor"><path d="M1 8h2v2H1zM4.5 6h2v4h-2zM8 4h2v6H8zM11.5 1h2v9h-2z"/></svg>
        <svg width="15" height="11" viewBox="0 0 15 11" fill="currentColor"><path d="M7.5 2c2.2 0 4.3.9 5.8 2.4l-.7.7c-1.4-1.4-3.2-2.1-5.1-2.1S3.8 3.7 2.4 5.1l-.7-.7C3.2 2.9 5.3 2 7.5 2zm0 2.4c1.5 0 2.9.6 4 1.7l-.7.7c-.9-.9-2.1-1.4-3.3-1.4s-2.4.5-3.3 1.4l-.7-.7c1.1-1.1 2.5-1.7 4-1.7zm0 2.4c.9 0 1.7.3 2.3.9l-.7.7c-.5-.5-1-.7-1.6-.7s-1.2.3-1.6.7l-.7-.7c.6-.6 1.4-.9 2.3-.9zm0 2.4c.3 0 .6.1.8.3l-.8.8-.8-.8c.2-.2.5-.3.8-.3z"/></svg>
        <svg width="24" height="11" viewBox="0 0 24 11" fill="none">
          <rect x="0.5" y="0.5" width="20" height="10" rx="2.5" stroke="currentColor" fill="none"/>
          <rect x="2" y="2" width="14" height="7" rx="1" fill="currentColor"/>
          <rect x="21" y="3.5" width="2" height="4" rx="1" fill="currentColor"/>
        </svg>
      </span>
    </div>
  );
}

// Gladia-style floating cards + connectors
function StepIllustration({ type }) {
  // Inline lucide-style icons
  const IconMic = () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <rect x="9" y="2" width="6" height="11" rx="3"/><path d="M19 10v2a7 7 0 0 1-14 0v-2"/><line x1="12" y1="19" x2="12" y2="22"/>
    </svg>
  );
  const IconPhone = () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M22 16.92v3a2 2 0 0 1-2.18 2 19.79 19.79 0 0 1-8.63-3.07 19.5 19.5 0 0 1-6-6 19.79 19.79 0 0 1-3.07-8.67A2 2 0 0 1 4.11 2h3a2 2 0 0 1 2 1.72 12.84 12.84 0 0 0 .7 2.81 2 2 0 0 1-.45 2.11L8.09 9.91a16 16 0 0 0 6 6l1.27-1.27a2 2 0 0 1 2.11-.45 12.84 12.84 0 0 0 2.81.7A2 2 0 0 1 22 16.92z"/>
    </svg>
  );
  const IconChat = () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>
    </svg>
  );
  const IconWhatsapp = () => (
    <svg viewBox="0 0 24 24" fill="currentColor">
      <path d="M.057 24l1.687-6.163c-1.041-1.804-1.588-3.849-1.587-5.946.003-6.556 5.338-11.891 11.893-11.891 3.181.001 6.167 1.24 8.413 3.488 2.245 2.248 3.481 5.236 3.48 8.414-.003 6.557-5.338 11.892-11.893 11.892-1.99-.001-3.951-.5-5.688-1.448l-6.305 1.654zm6.597-3.807c1.676.995 3.276 1.591 5.392 1.592 5.448 0 9.886-4.434 9.889-9.885.002-5.462-4.415-9.89-9.881-9.892-5.452 0-9.887 4.434-9.889 9.884-.001 2.225.651 3.891 1.746 5.634l-.999 3.648 3.742-.981z"/>
    </svg>
  );
  const IconCheck = () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
      <polyline points="20 6 9 17 4 12"/>
    </svg>
  );
  const IconCalendar = () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <rect x="3" y="4" width="18" height="18" rx="2" ry="2"/><line x1="16" y1="2" x2="16" y2="6"/><line x1="8" y1="2" x2="8" y2="6"/><line x1="3" y1="10" x2="21" y2="10"/>
    </svg>
  );
  const IconWebhook = () => (
    <svg viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
      <path d="M18 16.98h-5.99c-1.1 0-1.95.94-2.48 1.9A4 4 0 0 1 2 17c.01-.7.2-1.4.57-2"/>
      <path d="m6 17 3.13-5.78c.53-.97.1-2.18-.5-3.1a4 4 0 1 1 6.89-4.06"/>
      <path d="m12 6 3.13 5.73C15.66 12.7 16.9 13 18 13a4 4 0 0 1 0 8"/>
    </svg>
  );
  const IconBolt = () => (
    <svg viewBox="0 0 24 24" fill="currentColor">
      <path d="M13 2L3 14h7l-1 8 10-12h-7z"/>
    </svg>
  );

  // === Step 1: Conecta tu número — 3 channels funnel into 1 unified output
  if (type === 'channels') {
    return (
      <div className="hiw-scene">
        <div className="hiw-pill-row">
          <span className="hiw-pill"><IconWhatsapp /> WhatsApp</span>
          <span className="hiw-pill"><IconPhone /> Llamadas</span>
          <span className="hiw-pill"><IconChat /> Web Chat</span>
        </div>

        <div className="hiw-connector-tree">
          <span className="hiw-connector-tree-leg left"></span>
          <span className="hiw-connector-tree-leg mid"></span>
          <span className="hiw-connector-tree-leg right"></span>
        </div>

        <span className="hiw-pill hiw-pill-light">
          <IconBolt /> Tu número activo
        </span>

        <div className="hiw-bottom-status">
          <IconCheck /> 3 canales conectados
        </div>
      </div>
    );
  }

  // === Step 2: Cuéntale tu negocio — config card + test results
  if (type === 'train') {
    return (
      <div className="hiw-scene" style={{ gap: 14 }}>
        <div className="hiw-card">
          <div className="hiw-card-header">
            <span className="hiw-card-title">
              <span className="hiw-card-title-dot" style={{ background: '#7C5CFF' }}></span>
              Asistente ALVEX
            </span>
            <span className="hiw-card-status">LISTO</span>
          </div>
          <div className="hiw-stat-row">
            <span className="hiw-stat-key">Nombre</span>
            <span className="hiw-stat-val">Asistente ALVEX</span>
          </div>
          <div className="hiw-stat-row">
            <span className="hiw-stat-key">Tono</span>
            <span className="hiw-stat-val">Profesional · cercano</span>
          </div>
          <div className="hiw-stat-row">
            <span className="hiw-stat-key">Idiomas</span>
            <span className="hiw-stat-val">ES · EN · PT</span>
          </div>
          <div className="hiw-stat-row">
            <span className="hiw-stat-key">Conocimiento</span>
            <span className="hiw-stat-val">14 docs · 2.4 MB</span>
          </div>
        </div>

        <div className="hiw-bottom-status" style={{ marginTop: 6 }}>
          <IconCheck /> Probado · 23 conversaciones
        </div>
      </div>
    );
  }

  // === Step 3: Y se pone a trabajar — input fans out to 3 actions
  if (type === 'automate') {
    return (
      <div className="hiw-scene">
        <div className="hiw-card" style={{ maxWidth: 320 }}>
          <div className="hiw-card-header" style={{ marginBottom: 8 }}>
            <span className="hiw-card-title">
              <span className="hiw-card-title-dot" style={{ background: 'var(--accent)' }}></span>
              Mensaje entrante
            </span>
            <span style={{
              fontFamily: 'var(--font-mono)', fontSize: 10.5,
              color: 'rgba(255,255,255,0.35)', letterSpacing: '0.04em',
            }}>4.2s</span>
          </div>
          <div style={{
            fontSize: 12.5, color: 'rgba(255,255,255,0.72)',
            lineHeight: 1.45, fontStyle: 'italic',
          }}>
            "Hola, ¿tienen mesa para 4 mañana a las 21:00?"
          </div>
        </div>

        <div className="hiw-connector-tree">
          <span className="hiw-connector-tree-leg left"></span>
          <span className="hiw-connector-tree-leg mid"></span>
          <span className="hiw-connector-tree-leg right"></span>
        </div>

        <div className="hiw-pill-row">
          <span className="hiw-pill hiw-pill-success">
            <IconWhatsapp /> Respondido
          </span>
          <span className="hiw-pill hiw-pill-success">
            <IconCalendar /> Cita agendada
          </span>
          <span className="hiw-pill hiw-pill-success">
            <IconWebhook /> CRM actualizado
          </span>
        </div>

        <div className="hiw-bottom-status">
          <IconCheck /> Resuelto · sin tu equipo
        </div>
      </div>
    );
  }

  return null;
}


Object.assign(window, { HowItWorksSection });
