/* Small reusable primitives — Eyebrow, Pill, Section, WaitlistForm, etc. */
const { useState, useEffect, useRef, useCallback } = React;

/* ---------- Eyebrow ---------- */
function Eyebrow({ children, accent, style }) {
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 10,
      fontFamily: PP.mono, fontSize: 11, letterSpacing: '1.6px',
      color: accent ? PP.accentHi : 'rgba(14,14,12,0.5)',
      textTransform: 'uppercase', fontWeight: 500,
      whiteSpace: 'nowrap',
      ...style,
    }}>
      <span style={{
        width: 18, height: 1, background: 'currentColor', opacity: 0.45,
      }} />
      {children}
    </div>
  );
}

/* ---------- Pulse pill (used in hero + final CTA) ---------- */
function PulsePill({ children }) {
  return (
    <div style={{
      display: 'inline-flex', alignItems: 'center', gap: 9,
      padding: '7px 14px 7px 12px',
      background: PP.accentDim,
      border: '1px solid rgba(229,83,60,0.28)',
      borderRadius: 999,
      fontFamily: PP.mono, fontSize: 11.5, letterSpacing: '0.8px',
      color: PP.accentHi, textTransform: 'uppercase', fontWeight: 500,
      whiteSpace: 'nowrap',
    }}>
      <span className="pp-dot" style={{
        width: 7, height: 7, borderRadius: 999, background: PP.green,
        boxShadow: '0 0 0 0 rgba(31,138,91,0.6)',
      }} />
      {children}
    </div>
  );
}

/* ---------- Section wrapper ---------- */
function Section({ id, children, style, full }) {
  return (
    <section id={id} data-reveal={id} style={{
      position: 'relative',
      padding: '120px 32px',
      maxWidth: full ? 'none' : 1180,
      margin: '0 auto',
      ...style,
    }}>
      {children}
    </section>
  );
}

/* ---------- Section header (eyebrow + headline + body) ---------- */
function SectionHeader({ eyebrow, children, body, align = 'center', maxBody = 560 }) {
  const ta = align;
  return (
    <header style={{
      textAlign: ta, display: 'flex', flexDirection: 'column',
      alignItems: ta === 'center' ? 'center' : 'flex-start',
      gap: 48, marginBottom: 64,
    }}>
      {eyebrow && <Eyebrow>{eyebrow}</Eyebrow>}
      <h2 style={{
        margin: 0, fontFamily: PP.serif, fontStyle: 'italic',
        fontWeight: 'var(--pp-serif-weight, 400)',
        fontSize: 'clamp(32px, 4.4vw, 52px)',
        lineHeight: 1.28, letterSpacing: '-0.8px',
        color: PP.text, maxWidth: 820, textWrap: 'balance',
        paddingBottom: '0.12em',
      }}>{children}</h2>
      {body && <p style={{
        margin: 0, fontFamily: PP.sans, fontSize: 16.5, lineHeight: 1.7,
        color: 'rgba(14,14,12,0.5)', maxWidth: maxBody, textWrap: 'pretty',
      }}>{body}</p>}
    </header>
  );
}

/* ---------- Primary button ---------- */
function PrimaryButton({ children, onClick, type = 'button', disabled, style }) {
  return (
    <button type={type} onClick={onClick} disabled={disabled}
      className="pp-btn-primary"
      style={{
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
        gap: 8, padding: '13px 22px', borderRadius: 10,
        background: PP.accent, color: '#fff',
        fontFamily: PP.sans, fontSize: 14.5, fontWeight: 500, letterSpacing: '0.1px',
        boxShadow: '0 1px 0 rgba(14,14,12,0.13) inset, 0 8px 24px -10px rgba(229,83,60,0.6)',
        opacity: disabled ? 0.7 : 1, cursor: disabled ? 'not-allowed' : 'pointer',
        transition: 'transform .2s ease, box-shadow .2s ease, background .2s ease',
        ...style,
      }}>
      {children}
    </button>
  );
}

/* ---------- Ghost button ---------- */
function GhostButton({ children, onClick, style }) {
  return (
    <button onClick={onClick} className="pp-btn-ghost" style={{
      display: 'inline-flex', alignItems: 'center', gap: 8,
      padding: '12px 18px', borderRadius: 10,
      background: 'transparent', color: PP.text,
      border: '1px solid ' + PP.border,
      fontFamily: PP.sans, fontSize: 14, fontWeight: 500,
      transition: 'background .2s ease, border-color .2s ease',
      ...style,
    }}>{children}</button>
  );
}

/* ---------- Waitlist form ---------- */
function WaitlistForm({ shared, onJoined, compact, idSuffix = '' }) {
  const [email, setEmail] = useState('');
  const [err, setErr] = useState('');
  const [loading, setLoading] = useState(false);
  const success = shared.success;

  const submit = async (e) => {
    e.preventDefault();
    if (loading) return;
    const v = email.trim();
    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v)) {
      setErr('Please enter a valid email address');
      return;
    }
    setErr(''); setLoading(true);
    await new Promise(r => setTimeout(r, 800));
    try {
      const next = await window.ppSaveEmail(v);
      onJoined(next);
    } catch {
      onJoined(shared.count + 1);
    }
    setLoading(false);
  };

  if (success) {
    return (
      <div className="pp-success-card" style={{
        width: '100%', maxWidth: 520,
        padding: '22px 24px', borderRadius: 14,
        background: 'rgba(31,138,91,0.07)',
        border: '1px solid rgba(31,138,91,0.32)',
        display: 'flex', alignItems: 'flex-start', gap: 14,
      }}>
        <div style={{
          flex: '0 0 36px',
          width: 36, height: 36, borderRadius: 10,
          background: 'rgba(31,138,91,0.18)',
          border: '1px solid rgba(31,138,91,0.4)',
          display: 'grid', placeItems: 'center',
          color: PP.green, fontSize: 18, fontWeight: 600,
        }}>✓</div>
        <div style={{ textAlign: 'left' }}>
          <div style={{ fontFamily: PP.sans, fontWeight: 500, color: PP.green, fontSize: 14.5 }}>
            You’re on the list.
          </div>
          <div style={{ fontFamily: PP.sans, fontSize: 13.5, color: 'rgba(14,14,12,0.66)', marginTop: 4, lineHeight: 1.55 }}>
            We’ll be in touch when early access opens — usually within a few weeks of joining. Watch for an email from <span style={{ color: PP.text, fontWeight: 500 }}>team@proposalpulse.co</span>.
          </div>
        </div>
      </div>
    );
  }

  return (
    <form onSubmit={submit} noValidate className="pp-waitlist-form" style={{ width: '100%', maxWidth: 460 }}>
      <div style={{
        display: 'flex', gap: 6, padding: 5, borderRadius: 13,
        background: 'rgba(14,14,12,0.04)',
        border: '1px solid ' + (err ? 'rgba(248,113,113,0.5)' : PP.border),
        transition: 'border-color .15s ease, background .15s ease',
      }} className="pp-form-shell">
        <input
          type="email"
          aria-label="Email address"
          placeholder="you@agency.com"
          value={email}
          onChange={e => { setEmail(e.target.value); if (err) setErr(''); }}
          style={{
            flex: 1, minWidth: 0, background: 'transparent', border: 0, outline: 'none',
            padding: '10px 12px', color: PP.text,
            fontFamily: PP.sans, fontSize: 15,
          }}
        />
        <button type="submit" disabled={loading}
          className="pp-btn-primary"
          style={{
            display: 'inline-flex', alignItems: 'center', gap: 6,
            padding: '10px 18px', borderRadius: 9,
            background: PP.accent, color: '#fff',
            fontFamily: PP.sans, fontSize: 14, fontWeight: 500,
            opacity: loading ? 0.7 : 1, cursor: loading ? 'wait' : 'pointer',
            transition: 'transform .2s ease, box-shadow .2s ease',
            boxShadow: '0 6px 18px -8px rgba(229,83,60,0.7)',
            whiteSpace: 'nowrap',
          }}>
          {loading ? 'Joining…' : (<>Join Waitlist <span aria-hidden style={{ transform: 'translateY(-0.5px)' }}>→</span></>)}
        </button>
      </div>
      {err && (
        <div style={{
          marginTop: 10, fontFamily: PP.sans, fontSize: 12.5,
          color: '#DC2626', textAlign: 'left', paddingLeft: 6,
        }}>{err}</div>
      )}
    </form>
  );
}

Object.assign(window, {
  Eyebrow, PulsePill, Section, SectionHeader,
  PrimaryButton, GhostButton, WaitlistForm,
});
