// ReservAI Landing — Contacto + Footer responsive
const { useState: useStateC } = React;

function useIsMobile() {
  const [m, setM] = React.useState(window.innerWidth < 768);
  React.useEffect(() => {
    const h = () => setM(window.innerWidth < 768);
    window.addEventListener('resize', h);
    return () => window.removeEventListener('resize', h);
  }, []);
  return m;
}

const WA_LINK = 'https://wa.me/523314627189?text=Hola%2C%20me%20interesa%20ReservAI';
const IG_LINK = 'https://www.instagram.com/reservai.mx/';

function Contacto() {
  const [form, setForm] = useStateC({ nombre: '', negocio: '', telefono: '', plan: '', terminos: false });
  const [sent, setSent] = useStateC(false);
  const [focused, setFocused] = useStateC(null);
  const [loading, setLoading] = useStateC(false);
  const [error, setError] = useStateC(null);
  const isMobile = useIsMobile();

  if (sent) {
    return (
      <section id="contacto" style={{ padding: '90px 0', position: 'relative' }}>
        <div style={{ maxWidth: 600, margin: '0 auto', padding: '0 20px', textAlign: 'center' }}>
          <div style={{
            background: 'rgba(9,131,214,.08)', border: '1px solid #0983d6', borderRadius: 20, padding: 56,
            backdropFilter: 'blur(10px)', boxShadow: '0 30px 80px rgba(9,131,214,.15)',
          }}>
            <div style={{
              width: 72, height: 72, borderRadius: '50%', background: '#0983d6',
              display: 'flex', alignItems: 'center', justifyContent: 'center', margin: '0 auto 20px',
              boxShadow: '0 0 0 8px rgba(9,131,214,.15)',
            }}>
              <svg width="36" height="36" viewBox="0 0 24 24" fill="none" stroke="#fff" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
                <polyline points="20 6 9 17 4 12" />
              </svg>
            </div>
            <h2 style={{ color: '#fff', fontSize: '1.8rem', fontWeight: 700, marginBottom: 12 }}>¡Mensaje enviado!</h2>
            <p style={{ color: '#A0A0A0', lineHeight: 1.6 }}>Nos comunicaremos contigo pronto para iniciar con el proceso.</p>
          </div>
        </div>
      </section>
    );
  }

  const handleSubmit = async () => {
    if (!form.nombre || !form.negocio || !form.telefono || !form.plan) {
      setError('Por favor completa todos los campos.');
      return;
    }
    if (!form.terminos) {
      setError('Debes aceptar los términos y condiciones.');
      return;
    }
    setError(null);
    setLoading(true);
    try {
      const res = await fetch('/api/webhook', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', 'X-Source': 'ReservAI-Landing' },
        body: JSON.stringify({ nombre: form.nombre, negocio: form.negocio, telefono: form.telefono, plan: form.plan }),
      });
      if (!res.ok) throw new Error('Error al enviar');
      setSent(true);
    } catch {
      setError('Hubo un error al enviar. Intenta de nuevo o escríbenos por WhatsApp.');
    } finally {
      setLoading(false);
    }
  };

  const fields = [
    { key: 'nombre',   label: 'Nombre completo',   placeholder: 'Tu nombre',   type: 'text' },
    { key: 'negocio',  label: 'Nombre del negocio', placeholder: 'Mi negocio',  type: 'text' },
    { key: 'telefono', label: 'Teléfono',           placeholder: '10 dígitos',  type: 'tel' },
  ];

  return (
    <section id="contacto" style={{ padding: isMobile ? '56px 0' : '90px 0', position: 'relative' }}>
      <div style={{ maxWidth: 1100, margin: '0 auto', padding: '0 20px' }}>
        <div style={{ textAlign: 'center', marginBottom: 48 }}>
          <div style={{
            display: 'inline-block', background: 'rgba(9,131,214,.12)', border: '1px solid rgba(9,131,214,.25)',
            borderRadius: 40, padding: '5px 14px', fontSize: '.75rem', fontWeight: 600,
            color: '#0983d6', marginBottom: 16, letterSpacing: '.06em', textTransform: 'uppercase',
          }}>Empieza hoy</div>
          <h2 style={{ fontSize: isMobile ? '1.9rem' : '2.5rem', fontWeight: 700, color: '#fff', marginBottom: 12, letterSpacing: '-.02em' }}>
            ¿Te interesa <span style={{ color: '#0983d6' }}>ReservAI?</span>
          </h2>
          <p style={{ color: '#A0A0A0', fontSize: '1rem', maxWidth: 560, margin: '0 auto', lineHeight: 1.6 }}>
            Cuéntanos de tu negocio y nos pondremos en contacto. O escríbenos directo por nuestras redes.
          </p>
        </div>

        <div style={{ display: 'grid', gridTemplateColumns: isMobile ? '1fr' : '1.2fr 1fr', gap: isMobile ? 20 : 32, alignItems: 'stretch' }}>
          {/* Form */}
          <div style={{
            background: 'rgba(255,255,255,.04)', border: '1px solid rgba(255,255,255,.1)',
            borderRadius: 20, padding: isMobile ? 24 : 36, backdropFilter: 'blur(10px)',
          }}>
            {fields.map(f => (
              <div key={f.key} style={{ marginBottom: 18 }}>
                <label style={{ display: 'block', marginBottom: 8, fontWeight: 600, color: '#fff', fontSize: '.85rem' }}>
                  {f.label} <span style={{ color: '#0983d6' }}>*</span>
                </label>
                <input value={form[f.key]} type={f.type}
                  onChange={e => setForm(p => ({ ...p, [f.key]: e.target.value }))}
                  onFocus={() => setFocused(f.key)}
                  onBlur={() => setFocused(null)}
                  placeholder={f.placeholder}
                  style={{
                    width: '100%', padding: '13px 14px',
                    background: 'rgba(0,0,0,.4)',
                    border: `1px solid ${focused === f.key ? '#0983d6' : 'rgba(255,255,255,.12)'}`,
                    borderRadius: 10, color: '#fff', fontSize: '.95rem', outline: 'none', fontFamily: 'inherit',
                    transition: 'all .25s ease',
                    boxShadow: focused === f.key ? '0 0 0 3px rgba(9,131,214,.15)' : 'none',
                    boxSizing: 'border-box',
                  }} />
              </div>
            ))}
            <div style={{ marginBottom: 20 }}>
              <label style={{ display: 'block', marginBottom: 8, fontWeight: 600, color: '#fff', fontSize: '.85rem' }}>
                Plan a solicitar <span style={{ color: '#0983d6' }}>*</span>
              </label>
              <select value={form.plan} onChange={e => setForm(p => ({ ...p, plan: e.target.value }))}
                onFocus={() => setFocused('plan')} onBlur={() => setFocused(null)}
                style={{
                  width: '100%', padding: '13px 14px',
                  background: 'rgba(0,0,0,.4)',
                  border: `1px solid ${focused === 'plan' ? '#0983d6' : 'rgba(255,255,255,.12)'}`,
                  borderRadius: 10, color: form.plan ? '#fff' : '#666', fontSize: '.95rem',
                  outline: 'none', fontFamily: 'inherit', cursor: 'pointer',
                  transition: 'all .25s ease',
                  boxShadow: focused === 'plan' ? '0 0 0 3px rgba(9,131,214,.15)' : 'none',
                  appearance: 'none',
                  backgroundImage: 'url("data:image/svg+xml;utf8,<svg xmlns=\'http://www.w3.org/2000/svg\' width=\'12\' height=\'8\' viewBox=\'0 0 12 8\'><path d=\'M1 1l5 5 5-5\' stroke=\'%230983d6\' stroke-width=\'2\' fill=\'none\' stroke-linecap=\'round\' stroke-linejoin=\'round\'/></svg>")',
                  backgroundRepeat: 'no-repeat',
                  backgroundPosition: 'right 16px center',
                  paddingRight: 40,
                  boxSizing: 'border-box',
                }}>
                <option value="" style={{ background: '#1a1a1a' }}>Selecciona un plan</option>
                <option value="basico" style={{ background: '#1a1a1a' }}>Básico</option>
                <option value="pro" style={{ background: '#1a1a1a' }}>Pro</option>
                <option value="otro" style={{ background: '#1a1a1a' }}>Otro plan</option>
              </select>
            </div>
            <label style={{ display: 'flex', alignItems: 'flex-start', gap: 10, cursor: 'pointer', marginBottom: 24, color: '#A0A0A0', fontSize: '.85rem', lineHeight: 1.5 }}>
              <input type="checkbox" checked={form.terminos} onChange={e => setForm(p => ({ ...p, terminos: e.target.checked }))}
                style={{ width: 18, height: 18, accentColor: '#0983d6', marginTop: 1, flexShrink: 0 }} />
              Acepto los{' '}
              <a href="/terms.html" style={{ color: '#0983d6' }}>términos y condiciones</a>
              {' '}y la{' '}
              <a href="/privacy.html" style={{ color: '#0983d6' }}>política de privacidad</a>
            </label>
            {error && (
              <div style={{ background: 'rgba(244,67,54,.1)', border: '1px solid rgba(244,67,54,.35)', borderRadius: 10, padding: '12px 14px', marginBottom: 16, color: '#ff6b6b', fontSize: '.85rem', lineHeight: 1.5 }}>
                {error}
              </div>
            )}
            <button onClick={handleSubmit} disabled={loading} style={{
              width: '100%', background: loading ? 'rgba(9,131,214,.6)' : '#0983d6', color: '#fff', border: 'none', padding: '15px 24px',
              borderRadius: 10, fontWeight: 700, fontSize: '.98rem', cursor: loading ? 'not-allowed' : 'pointer', fontFamily: 'inherit',
              transition: 'all .25s ease',
              boxShadow: '0 8px 20px rgba(9,131,214,.35)',
            }}
              onMouseEnter={e => { if (!loading) { e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.boxShadow = '0 14px 28px rgba(9,131,214,.45)'; } }}
              onMouseLeave={e => { e.currentTarget.style.transform = 'none'; e.currentTarget.style.boxShadow = '0 8px 20px rgba(9,131,214,.35)'; }}
            >{loading ? 'Enviando...' : 'Quiero ReservAI →'}</button>
          </div>

          {/* Sidebar — Redes */}
          <div style={{ display: 'flex', flexDirection: 'column', gap: 16 }}>
            <div style={{
              background: 'rgba(255,255,255,.04)', border: '1px solid rgba(255,255,255,.1)',
              borderRadius: 20, padding: isMobile ? 20 : 28, backdropFilter: 'blur(10px)',
            }}>
              <h3 style={{ color: '#fff', fontSize: '1.1rem', fontWeight: 700, marginBottom: 8 }}>
                ¿Tienes dudas?
              </h3>
              <p style={{ color: '#A0A0A0', fontSize: '.9rem', lineHeight: 1.5, marginBottom: 20 }}>
                Escríbenos directamente, te respondemos en minutos.
              </p>

              {/* WhatsApp card */}
              <a href={WA_LINK} target="_blank" rel="noopener noreferrer"
                style={{
                  display: 'flex', alignItems: 'center', gap: 14,
                  background: 'rgba(37,211,102,.08)',
                  border: '1px solid rgba(37,211,102,.3)',
                  borderRadius: 14, padding: '14px 16px', marginBottom: 12,
                  textDecoration: 'none', transition: 'all .25s ease',
                }}
                onMouseEnter={e => { e.currentTarget.style.background = 'rgba(37,211,102,.15)'; e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.borderColor = '#25D366'; }}
                onMouseLeave={e => { e.currentTarget.style.background = 'rgba(37,211,102,.08)'; e.currentTarget.style.transform = 'none'; e.currentTarget.style.borderColor = 'rgba(37,211,102,.3)'; }}
              >
                <div style={{
                  width: 44, height: 44, borderRadius: 12, background: '#25D366',
                  display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
                }}>
                  <svg width="22" height="22" viewBox="0 0 24 24" fill="#fff">
                    <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0020.885 3.488"/>
                  </svg>
                </div>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ color: '#fff', fontWeight: 700, fontSize: '.95rem', marginBottom: 2 }}>WhatsApp</div>
                  <div style={{ color: '#A0A0A0', fontSize: '.8rem' }}>+52 33 1462 7189</div>
                </div>
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#25D366" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0 }}>
                  <line x1="5" y1="12" x2="19" y2="12" />
                  <polyline points="12 5 19 12 12 19" />
                </svg>
              </a>

              {/* Instagram card */}
              <a href={IG_LINK} target="_blank" rel="noopener noreferrer"
                style={{
                  display: 'flex', alignItems: 'center', gap: 14,
                  background: 'rgba(225,48,108,.06)',
                  border: '1px solid rgba(225,48,108,.25)',
                  borderRadius: 14, padding: '14px 16px',
                  textDecoration: 'none', transition: 'all .25s ease',
                }}
                onMouseEnter={e => { e.currentTarget.style.background = 'rgba(225,48,108,.13)'; e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.borderColor = '#E1306C'; }}
                onMouseLeave={e => { e.currentTarget.style.background = 'rgba(225,48,108,.06)'; e.currentTarget.style.transform = 'none'; e.currentTarget.style.borderColor = 'rgba(225,48,108,.25)'; }}
              >
                <div style={{
                  width: 44, height: 44, borderRadius: 12,
                  background: 'linear-gradient(135deg, #FFDC80 0%, #F77737 25%, #F56040 50%, #E1306C 75%, #833AB4 100%)',
                  display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
                }}>
                  <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#fff" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                    <rect x="2" y="2" width="20" height="20" rx="5" ry="5" />
                    <path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z" />
                    <line x1="17.5" y1="6.5" x2="17.51" y2="6.5" />
                  </svg>
                </div>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ color: '#fff', fontWeight: 700, fontSize: '.95rem', marginBottom: 2 }}>Instagram</div>
                  <div style={{ color: '#A0A0A0', fontSize: '.8rem' }}>@reservai.mx</div>
                </div>
                <svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="#E1306C" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round" style={{ flexShrink: 0 }}>
                  <line x1="5" y1="12" x2="19" y2="12" />
                  <polyline points="12 5 19 12 12 19" />
                </svg>
              </a>
            </div>

            {/* Quick badge */}
            <div style={{
              background: 'rgba(255,255,255,.04)', border: '1px solid rgba(255,255,255,.1)',
              borderRadius: 20, padding: 24, backdropFilter: 'blur(10px)',
              display: 'flex', alignItems: 'center', gap: 14,
            }}>
              <div style={{
                width: 44, height: 44, borderRadius: 12, background: 'rgba(9,131,214,.15)',
                display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
              }}>
                <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="#0983d6" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
                  <circle cx="12" cy="12" r="10" />
                  <polyline points="12 6 12 12 16 14" />
                </svg>
              </div>
              <div>
                <div style={{ color: '#fff', fontWeight: 700, fontSize: '.95rem', marginBottom: 2 }}>Respuesta rápida</div>
                <div style={{ color: '#A0A0A0', fontSize: '.82rem', lineHeight: 1.4 }}>Te contactamos en menos de 24 hrs.</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </section>
  );
}

function Footer() {
  const isMobile = useIsMobile();
  return (
    <footer style={{ padding: '40px 0 16px', borderTop: '1px solid rgba(255,255,255,.08)', position: 'relative' }}>
      <div style={{ maxWidth: 1200, margin: '0 auto', padding: '0 20px' }}>
        <div style={{
          display: 'flex',
          flexDirection: isMobile ? 'column' : 'row',
          justifyContent: isMobile ? 'center' : 'space-between',
          alignItems: 'center',
          marginBottom: 24,
          gap: 20,
          textAlign: isMobile ? 'center' : 'left',
        }}>
          <img src="assets/ReservAI-wordmark.png" alt="ReservAI" style={{ height: 36 }} />
          <div style={{ display: 'flex', gap: isMobile ? 16 : 28, alignItems: 'center', flexWrap: 'wrap', justifyContent: 'center' }}>
            {[
              { label: 'Términos y Condiciones', href: '/terms.html' },
              { label: 'Política de Privacidad', href: '/privacy.html' },
              { label: 'Contacto',               href: '#contacto' },
            ].map(({ label, href }) => (
              <a key={label} href={href} style={{ color: '#A0A0A0', textDecoration: 'none', fontSize: '.9rem', transition: 'color .3s' }}
                onMouseEnter={e => e.target.style.color = '#0983d6'}
                onMouseLeave={e => e.target.style.color = '#A0A0A0'}>{label}</a>
            ))}
            <div style={{ display: 'flex', gap: 10 }}>
              <a href={WA_LINK} target="_blank" rel="noopener noreferrer"
                style={{
                  width: 36, height: 36, borderRadius: 10, background: 'rgba(37,211,102,.12)',
                  border: '1px solid rgba(37,211,102,.3)',
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  transition: 'all .25s ease',
                }}
                onMouseEnter={e => { e.currentTarget.style.background = '#25D366'; }}
                onMouseLeave={e => { e.currentTarget.style.background = 'rgba(37,211,102,.12)'; }}
              >
                <svg width="16" height="16" viewBox="0 0 24 24" fill="#25D366"><path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893A11.821 11.821 0 0020.885 3.488"/></svg>
              </a>
              <a href={IG_LINK} target="_blank" rel="noopener noreferrer"
                style={{
                  width: 36, height: 36, borderRadius: 10, background: 'rgba(225,48,108,.1)',
                  border: '1px solid rgba(225,48,108,.3)',
                  display: 'flex', alignItems: 'center', justifyContent: 'center',
                  transition: 'all .25s ease',
                }}
                onMouseEnter={e => { e.currentTarget.style.background = 'linear-gradient(135deg, #F77737, #E1306C, #833AB4)'; }}
                onMouseLeave={e => { e.currentTarget.style.background = 'rgba(225,48,108,.1)'; }}
              >
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="#E1306C" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                  <rect x="2" y="2" width="20" height="20" rx="5" ry="5" />
                  <path d="M16 11.37A4 4 0 1 1 12.63 8 4 4 0 0 1 16 11.37z" />
                  <line x1="17.5" y1="6.5" x2="17.51" y2="6.5" />
                </svg>
              </a>
            </div>
          </div>
        </div>
        <div style={{ borderTop: '1px solid rgba(255,255,255,.08)', paddingTop: 16, textAlign: 'center' }}>
          <p style={{ color: '#666', fontSize: '.85rem' }}>© ReservAI 2026. Todos los derechos reservados.</p>
        </div>
      </div>
    </footer>
  );
}

Object.assign(window, { Contacto, Footer });
