// Process / About / Contact sections

function Process({ lang }) {
  const L = LANG_COPY[lang];
  return (
    <section id="process" className="section">
      <div className="container">
        <SectionHeader eyebrow={L.process.eyebrow} title={L.process.title} />
        <div style={{ marginTop: 72, display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(240px, 1fr))', gap: 1, background: 'rgba(255,255,255,0.06)', border: '1px solid rgba(255,255,255,0.06)' }}>
          {L.process.steps.map((s, i) => (
            <div key={i} className="reveal" style={{ padding: 36, background: 'var(--bg)', minHeight: 260, display: 'flex', flexDirection: 'column', justifyContent: 'space-between' }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: 12, color: 'var(--accent)', letterSpacing: 1 }}>0{i + 1}</div>
              <div>
                <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 600, letterSpacing: -0.3, margin: 0, lineHeight: 1.15 }}>{s.title}</h3>
                <p style={{ fontSize: 14, color: 'var(--muted)', marginTop: 10, lineHeight: 1.55 }}>{s.desc}</p>
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function About({ lang }) {
  const L = LANG_COPY[lang];
  return (
    <section id="about" className="section" style={{ background: 'var(--bg-alt)' }}>
      <div className="container">
        {/* Header: headline left, body right */}
        <div style={{ display: 'grid', gridTemplateColumns: '1.1fr 1fr', gap: 80, alignItems: 'end' }}>
          <div className="reveal">
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 20, fontFamily: 'var(--font-mono)', fontSize: 12, letterSpacing: 1.5, textTransform: 'uppercase', color: 'var(--muted)' }}>
              <span style={{ width: 24, height: 1, background: 'var(--accent)' }} />
              <span>{L.about.eyebrow}</span>
            </div>
            <h2 style={{ fontFamily: 'var(--font-display)', fontSize: 'clamp(40px, 5.5vw, 76px)', lineHeight: 0.98, letterSpacing: -0.03, fontWeight: 600, margin: 0 }}>
              {L.about.title1} <br /><span style={{ fontFamily: 'var(--font-serif)', fontStyle: 'italic', fontWeight: 400, color: 'var(--accent)' }}>{L.about.title2}</span>
            </h2>
          </div>
          <p className="reveal" style={{ fontSize: 18, color: 'var(--muted)', marginTop: 0, marginBottom: 0, lineHeight: 1.65, maxWidth: 520, textWrap: 'pretty' }}>{L.about.body}</p>
        </div>

        {/* Stats row */}
        <div className="reveal" style={{ marginTop: 72, display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 0, borderTop: '1px solid rgba(255,255,255,0.1)', borderBottom: '1px solid rgba(255,255,255,0.1)' }}>
          {L.about.stats.map((s, i) => (
            <div key={i} style={{ padding: '28px 0', borderLeft: i > 0 ? '1px solid rgba(255,255,255,0.08)' : 'none', paddingLeft: i > 0 ? 32 : 0 }}>
              <div style={{ fontFamily: 'var(--font-display)', fontSize: 'clamp(32px, 3.5vw, 48px)', fontWeight: 500, letterSpacing: -1.5, lineHeight: 1 }}>{s.n}</div>
              <div style={{ fontSize: 12, color: 'var(--muted)', fontFamily: 'var(--font-mono)', letterSpacing: 0.5, marginTop: 8, textTransform: 'uppercase' }}>{s.l}</div>
            </div>
          ))}
        </div>

        {/* Principles — 3 col grid */}
        <div style={{ marginTop: 96, display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(260px, 1fr))', gap: 0 }}>
          {L.about.principles.map((p, i) => (
            <div key={i} className="reveal" style={{ padding: '32px 40px 32px 0', borderRight: i < L.about.principles.length - 1 ? '1px solid rgba(255,255,255,0.08)' : 'none', paddingLeft: i > 0 ? 40 : 0 }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontSize: 12, color: 'var(--accent)', letterSpacing: 1.2, marginBottom: 16 }}>{p.n}</div>
              <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 22, fontWeight: 600, letterSpacing: -0.3, margin: 0, lineHeight: 1.2 }}>{p.title}</h3>
              <p style={{ fontSize: 14.5, color: 'var(--muted)', marginTop: 12, lineHeight: 1.55, textWrap: 'pretty' }}>{p.desc}</p>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

function Contact({ lang }) {
  const L = LANG_COPY[lang];
  const [state, setState] = React.useState({ name: '', email: '', topic: 'meeting', msg: '', website: '' });
  const [sent, setSent] = React.useState(false);
  const [sending, setSending] = React.useState(false);
  const [error, setError] = React.useState(null);
  const tsRef = React.useRef(Math.floor(Date.now() / 1000));

  const submit = async (e) => {
    e.preventDefault();
    if (sending) return;
    setError(null);
    setSending(true);
    try {
      const res = await fetch(window.CYGNUS_FORM_ENDPOINT || '/api/enviar', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          topic: state.topic,
          nombre: state.name,
          email: state.email,
          mensaje: state.msg,
          website: state.website,         // honeypot
          ts: tsRef.current,
          lang,
          origen: 'web',
        }),
      });
      const data = await res.json().catch(() => ({}));
      if (!res.ok || !data.ok) {
        throw new Error(data.error || 'No se pudo enviar el mensaje.');
      }
      try { if (window.gtag) window.gtag('event', 'form_submit', { topic: state.topic }); } catch (err) {}
      setSent(true);
    } catch (err) {
      setError(err.message || 'Error al enviar.');
    } finally {
      setSending(false);
    }
  };

  return (
    <section id="contact" className="section">
      <div className="container">
        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1.1fr', gap: 80 }}>
          <div className="reveal">
            <div style={{ display: 'flex', alignItems: 'center', gap: 10, marginBottom: 20, fontFamily: 'var(--font-mono)', fontSize: 12, letterSpacing: 1.5, textTransform: 'uppercase', color: 'var(--muted)' }}>
              <span style={{ width: 24, height: 1, background: 'var(--accent)' }} />
              <span>{L.contact.eyebrow}</span>
            </div>
            <h2 style={{ fontFamily: 'var(--font-display)', fontSize: 'clamp(44px, 5.5vw, 76px)', lineHeight: 0.98, letterSpacing: -0.03, fontWeight: 600, margin: 0 }}>
              {L.contact.title1} <br />
              <span style={{ fontFamily: 'var(--font-serif)', fontStyle: 'italic', fontWeight: 400, color: 'var(--accent)' }}>{L.contact.title2}</span>
            </h2>
            <p style={{ fontSize: 17, color: 'var(--muted)', marginTop: 28, lineHeight: 1.6, maxWidth: 440 }}>{L.contact.body}</p>

            {/* WhatsApp primary button */}
            <WhatsAppLauncher
              location="contact_section"
              style={{
                marginTop: 40, display: 'inline-flex', alignItems: 'center', gap: 12,
                padding: '16px 24px', background: '#25D366', color: '#0a2a16',
                fontSize: 14, fontWeight: 600, letterSpacing: 0.3, textDecoration: 'none',
                transition: 'all 0.25s cubic-bezier(.2,.7,.3,1)',
                fontFamily: 'var(--font-display)',
                cursor: 'pointer',
              }}
              onMouseEnter={(e) => { e.currentTarget.style.transform = 'translateY(-2px)'; e.currentTarget.style.boxShadow = '0 12px 32px rgba(37, 211, 102, 0.25)'; }}
              onMouseLeave={(e) => { e.currentTarget.style.transform = 'none'; e.currentTarget.style.boxShadow = 'none'; }}
            >
              <svg width="20" height="20" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true">
                <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 0 1-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 0 1-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 0 1 2.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0 0 12.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 0 0 5.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 0 0-3.48-8.413Z"/>
              </svg>
              {L.contact.whatsapp.label}
            </WhatsAppLauncher>

            <div style={{ marginTop: 48, display: 'flex', flexDirection: 'column', gap: 0, borderTop: '1px solid rgba(255,255,255,0.08)' }}>
              {L.contact.info.map((it, i) => {
                const inner = (
                  <>
                    <div style={{ fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: 1.2, textTransform: 'uppercase', color: 'var(--muted)' }}>{it.label}</div>
                    <div style={{ fontSize: 16, marginTop: 6, display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
                      <span>{it.value}</span>
                      {it.href && <span style={{ color: 'var(--accent)', fontSize: 14, opacity: 0.7 }}>→</span>}
                    </div>
                  </>
                );
                const baseStyle = { display: 'block', padding: '18px 0', borderBottom: '1px solid rgba(255,255,255,0.08)', color: 'inherit', textDecoration: 'none', transition: 'padding-left 0.2s' };
                return it.href ? (
                  <a key={i} href={it.href} target={it.href.startsWith('http') ? '_blank' : undefined} rel={it.href.startsWith('http') ? 'noopener' : undefined}
                    style={baseStyle}
                    onMouseEnter={(e) => e.currentTarget.style.paddingLeft = '8px'}
                    onMouseLeave={(e) => e.currentTarget.style.paddingLeft = '0'}
                  >{inner}</a>
                ) : (
                  <div key={i} style={baseStyle}>{inner}</div>
                );
              })}
            </div>
          </div>

          <form className="reveal" onSubmit={submit}
            style={{ padding: 40, border: '1px solid rgba(255,255,255,0.08)', background: 'rgba(255,255,255,0.015)', position: 'relative', overflow: 'hidden' }}>
            {sent ? (
              <div style={{ textAlign: 'center', padding: '80px 0' }}>
                <div style={{ fontSize: 48, marginBottom: 20 }}>✓</div>
                <h3 style={{ fontFamily: 'var(--font-display)', fontSize: 28, margin: 0, fontWeight: 600 }}>{L.contact.thanksTitle}</h3>
                <p style={{ color: 'var(--muted)', marginTop: 12 }}>{L.contact.thanksBody}</p>
                <button onClick={() => { setSent(false); setError(null); setState({ name: '', email: '', topic: 'meeting', msg: '', website: '' }); tsRef.current = Math.floor(Date.now() / 1000); }} className="btn-ghost" style={{ marginTop: 24 }}>{L.contact.sendAnother}</button>
              </div>
            ) : (<>
              <FieldLabel>{L.contact.f.topic}</FieldLabel>
              <div style={{ display: 'flex', gap: 8, flexWrap: 'wrap', marginBottom: 28 }}>
                {L.contact.topics.map((t) => (
                  <button key={t.id} type="button" onClick={() => setState((s) => ({ ...s, topic: t.id }))}
                    className={state.topic === t.id ? 'topic-chip active' : 'topic-chip'}>
                    {t.label}
                  </button>
                ))}
              </div>
              <Field label={L.contact.f.name} value={state.name} onChange={(v) => setState((s) => ({ ...s, name: v }))} />
              <Field label={L.contact.f.email} type="email" value={state.email} onChange={(v) => setState((s) => ({ ...s, email: v }))} />
              <Field label={L.contact.f.msg} value={state.msg} onChange={(v) => setState((s) => ({ ...s, msg: v }))} multiline />

              {/* Honeypot — hidden from users, bots fill it */}
              <input type="text" name="website" tabIndex="-1" autoComplete="off"
                value={state.website} onChange={(e) => setState((s) => ({ ...s, website: e.target.value }))}
                style={{ position: 'absolute', left: '-9999px', width: 1, height: 1, opacity: 0 }}
                aria-hidden="true" />

              {error && (
                <div style={{ marginBottom: 14, padding: '10px 14px', background: 'rgba(204,70,70,0.12)', borderLeft: '3px solid #cc4646', color: '#ffb0b0', fontSize: 13 }}>
                  {error}
                </div>
              )}
              <button type="submit" className="btn-primary" disabled={sending} style={{ marginTop: 16, width: '100%', opacity: sending ? 0.6 : 1, cursor: sending ? 'wait' : 'pointer' }}>
                {sending ? 'Enviando…' : L.contact.send}
                {!sending && (
                  <svg width="14" height="14" viewBox="0 0 14 14" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round">
                    <path d="M3 11L11 3M11 3H5M11 3V9" />
                  </svg>
                )}
              </button>
            </>)}
          </form>
        </div>
      </div>
    </section>
  );
}

function FieldLabel({ children }) {
  return <label style={{ display: 'block', fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: 1.2, textTransform: 'uppercase', color: 'var(--muted)', marginBottom: 10 }}>{children}</label>;
}

function Field({ label, value, onChange, type = 'text', multiline }) {
  const [focus, setFocus] = React.useState(false);
  const Tag = multiline ? 'textarea' : 'input';
  return (
    <div style={{ marginBottom: 24 }}>
      <FieldLabel>{label}</FieldLabel>
      <Tag
        type={type} value={value} onChange={(e) => onChange(e.target.value)}
        onFocus={() => setFocus(true)} onBlur={() => setFocus(false)}
        rows={multiline ? 4 : undefined}
        style={{
          width: '100%', background: 'transparent', color: 'var(--fg)',
          border: 'none', borderBottom: `1px solid ${focus ? 'var(--accent)' : 'rgba(255,255,255,0.15)'}`,
          padding: '10px 0', fontSize: 16, fontFamily: 'inherit', outline: 'none',
          resize: multiline ? 'vertical' : 'none', transition: 'border-color 0.2s',
        }}
      />
    </div>
  );
}

function Footer({ lang }) {
  const L = LANG_COPY[lang];
  return (
    <footer style={{ padding: '60px 40px 40px', borderTop: '1px solid rgba(255,255,255,0.06)' }}>
      <div className="container footer-grid" style={{ display: 'grid', gridTemplateColumns: `1.5fr repeat(${L.footer.cols.length}, 1fr)`, gap: 48 }}>
        <div>
          <CygnusWordmark size={24} />
          <p style={{ fontSize: 13, color: 'var(--muted)', marginTop: 20, lineHeight: 1.5, maxWidth: 320 }}>{L.footer.tagline}</p>
          <div style={{ fontFamily: 'var(--font-serif)', fontStyle: 'italic', fontSize: 15, color: 'var(--accent)', marginTop: 20 }}>
            Inspired by innovation.
          </div>
          <div style={{ fontFamily: 'var(--font-mono)', fontSize: 11, color: 'var(--muted)', marginTop: 24, letterSpacing: 0.5 }}>
            Asunción · Paraguay · LATAM
          </div>
        </div>
        {L.footer.cols.map((col, i) => (
          <div key={i}>
            <div style={{ fontFamily: 'var(--font-mono)', fontSize: 10, letterSpacing: 1.2, textTransform: 'uppercase', color: 'var(--muted)', marginBottom: 16 }}>{col.title}</div>
            {col.links.map((l, j) => (
              <a key={j} href="#" style={{ display: 'block', fontSize: 14, color: 'var(--fg)', textDecoration: 'none', padding: '4px 0', opacity: 0.85 }}>{l}</a>
            ))}
          </div>
        ))}
      </div>
      <div className="container" style={{ marginTop: 60, paddingTop: 24, borderTop: '1px solid rgba(255,255,255,0.06)', display: 'flex', justifyContent: 'space-between', fontFamily: 'var(--font-mono)', fontSize: 11, color: 'var(--muted)' }}>
        <span>© {new Date().getFullYear()} Cygnus — {L.footer.rights}</span>
        <span>cygnus.com.py</span>
      </div>
    </footer>
  );
}

Object.assign(window, { Process, About, Contact, Footer, Field, FieldLabel });
