/* global React, CTA */
const { useState: useStateB2, useRef: useRefB2, useEffect: useEffectB2 } = React;

// ====== BLOCO 4 — Antes / Depois (slider) ======
function BeforeAfter() {
  const [pos, setPos] = useStateB2(50);
  const wrapRef = useRefB2(null);
  const dragging = useRefB2(false);

  const setFromX = (clientX) => {
    const r = wrapRef.current.getBoundingClientRect();
    const p = Math.max(0, Math.min(100, ((clientX - r.left) / r.width) * 100));
    setPos(p);
  };
  useEffectB2(() => {
    const move = (e) => {
      if (!dragging.current) return;
      const x = e.touches ? e.touches[0].clientX : e.clientX;
      setFromX(x);
    };
    const up = () => { dragging.current = false; };
    window.addEventListener("mousemove", move);
    window.addEventListener("touchmove", move, { passive: true });
    window.addEventListener("mouseup", up);
    window.addEventListener("touchend", up);
    return () => {
      window.removeEventListener("mousemove", move);
      window.removeEventListener("touchmove", move);
      window.removeEventListener("mouseup", up);
      window.removeEventListener("touchend", up);
    };
  }, []);

  const rows = [
    { i: "confused", a: '"Não sei o que quero fazer"', b: "Ranking de 3–5 carreiras com vantagem natural" },
    { i: "spiral",   a: "Escolha por exclusão / pressão social", b: "Decisão fundamentada em interesse + perfil comportamental" },
    { i: "money",    a: "Risco de R$ 8–25k/ano em curso abandonado", b: "R$ 300 — investimento único e preventivo" },
    { i: "shrug",    a: '"Vou tentar e ver no que dá"', b: "Plano com áreas de destaque e demanda de mercado" },
    { i: "anxious",  a: "Ansiedade pré-vestibular", b: "Clareza e direção" },
  ];

  return (
    <section id="b4" className="grain" style={{ background: "var(--ipgs-paper)" }}>
      <style>{`
        .ba-head { display: flex; justify-content: space-between; align-items: end; flex-wrap: wrap; gap: 20px; margin-bottom: 36px; }
        .ba-slider {
          position: relative;
          aspect-ratio: 16 / 10;
          max-width: 880px;
          margin: 0 auto 32px;
          border-radius: 18px;
          overflow: hidden;
          border: 1px solid rgba(6,41,71,.10);
          box-shadow: var(--shadow-md);
          touch-action: none;
          user-select: none;
          background: var(--paper-100);
        }
        .ba-side {
          position: absolute; inset: 0;
          display: flex; align-items: center; justify-content: center;
        }
        .ba-side .ph {
          position: absolute; inset: 0;
          border: none; border-radius: 0;
          font-size: 10px;
        }
        .ba-side.before .ph {
          background:
            radial-gradient(60% 60% at 30% 30%, rgba(6,41,71,.10), transparent 60%),
            repeating-linear-gradient(135deg, rgba(6,41,71,.06) 0 1px, transparent 1px 14px),
            linear-gradient(180deg, #d6d6d3, #b8b8b4);
        }
        .ba-side.after .ph {
          background:
            radial-gradient(50% 50% at 60% 35%, rgba(171,249,198,.35), transparent 60%),
            radial-gradient(60% 60% at 30% 80%, rgba(198,156,80,.20), transparent 60%),
            linear-gradient(180deg, #f3eedc, #e9e3ce);
        }
        .ba-side.after { clip-path: inset(0 0 0 var(--p, 50%)); }
        .ba-label {
          position: absolute; top: 16px;
          font-family: var(--font-mono);
          font-size: 10px;
          letter-spacing: 0.18em;
          text-transform: uppercase;
          padding: 6px 12px;
          border-radius: 999px;
          z-index: 2;
        }
        .ba-side.before .ba-label { left: 16px; background: rgba(6,41,71,.85); color: var(--ipgs-paper); }
        .ba-side.after  .ba-label { right: 16px; background: var(--ipgs-gold); color: var(--ipgs-deep); }
        .ba-handle {
          position: absolute; top: 0; bottom: 0;
          left: var(--p, 50%);
          width: 2px;
          background: var(--ipgs-gold);
          z-index: 3;
          transform: translateX(-50%);
        }
        .ba-knob {
          position: absolute;
          top: 50%; left: 50%;
          transform: translate(-50%, -50%);
          width: 48px; height: 48px;
          border-radius: 50%;
          background: var(--ipgs-gold);
          color: var(--ipgs-deep);
          display: grid; place-items: center;
          cursor: ew-resize;
          box-shadow: 0 8px 22px -4px rgba(6,41,71,.4);
        }
        .ba-knob svg { display: block; }
        .ba-rows { display: grid; gap: 0; max-width: 880px; margin: 0 auto; }
        .ba-row {
          display: grid;
          grid-template-columns: 32px 1fr 1fr;
          gap: 14px;
          align-items: center;
          padding: 14px 0;
          border-bottom: 1px dashed rgba(6,41,71,.16);
          font-size: 14.5px;
        }
        @media (min-width: 720px) {
          .ba-row { grid-template-columns: 44px 1fr 1fr; gap: 24px; padding: 18px 0; font-size: 16px; }
        }
        .ba-row .icn {
          width: 28px; height: 28px;
          border-radius: 50%;
          display: grid; place-items: center;
          color: var(--gold-700);
          background: var(--paper-100);
          border: 1px solid rgba(6,41,71,.10);
        }
        .ba-row .a { color: var(--deep-700); text-decoration: line-through; text-decoration-color: rgba(6,41,71,.25); }
        .ba-row .b { color: var(--ipgs-deep); font-weight: 600; display: inline-flex; gap: 8px; align-items: start; }
        .ba-row .b svg { color: var(--mint-700); flex-shrink: 0; margin-top: 4px; }
        .ba-cta { text-align: center; margin-top: 36px; }
      `}</style>

      <div className="container-x" style={{ position: "relative", zIndex: 2 }}>
        <div className="ba-head">
          <div>
            <span className="eyebrow">Antes & depois</span>
            <h2 className="display reveal" style={{ marginTop: 14 }}>
              Como muda a conversa <em>em casa.</em>
            </h2>
          </div>
          <p className="lede" style={{ maxWidth: "32ch", margin: 0 }}>
            Arraste o controle para comparar o antes do mapeamento e o depois do laudo IPGS.
          </p>
        </div>

        <div
          className="ba-slider reveal"
          ref={wrapRef}
          style={{ "--p": `${pos}%` }}
          onMouseDown={(e) => { dragging.current = true; setFromX(e.clientX); }}
          onTouchStart={(e) => { dragging.current = true; setFromX(e.touches[0].clientX); }}
        >
          <div className="ba-side before">
            <span className="ba-label">Antes</span>
            <div className="ph">[ TODO-MEDIA · jovem confuso ]</div>
          </div>
          <div className="ba-side after">
            <span className="ba-label">Depois</span>
            <div className="ph">[ TODO-MEDIA · jovem decidido ]</div>
          </div>
          <div className="ba-handle">
            <span className="ba-knob" aria-label="Arraste para comparar">
              <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2">
                <path d="M9 6l-4 6 4 6M15 6l4 6-4 6"/>
              </svg>
            </span>
          </div>
        </div>

        <div className="ba-rows">
          {rows.map((r) => (
            <div className="ba-row" key={r.i}>
              <span className="icn">
                <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2"><path d="M5 12h14M13 5l7 7-7 7"/></svg>
              </span>
              <span className="a">{r.a}</span>
              <span className="b">
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2"><path d="M5 12.5l4 4L19 7"/></svg>
                {r.b}
              </span>
            </div>
          ))}
        </div>

        <div className="ba-cta">
          <CTA size="lg" position="b4_after">Quero esse depois — começar agora</CTA>
        </div>
      </div>
    </section>
  );
}

// ====== BLOCO 5 — Solução em 1 frase ======
function OneLine() {
  return (
    <section className="dark grain" id="b5">
      <style>{`
        .ol {
          font-family: var(--font-sans);
          font-size: clamp(48px, 9vw, 120px);
          line-height: 1.0;
          letter-spacing: -0.04em;
          margin: 0;
          color: var(--ipgs-paper);
        }
        .ol .l1 { font-weight: 700; }
        .ol .l2 { font-weight: 300; opacity: 0.86; display: block; }
        .ol .l3 { font-family: var(--font-display); font-style: italic; color: var(--ipgs-gold); font-weight: 400; display: block; }
      `}</style>
      <div className="container-x" style={{ position: "relative", zIndex: 2 }}>
        <span className="eyebrow">A solução</span>
        <h2 className="ol reveal" style={{ marginTop: 18 }}>
          <span className="l1">20 minutos.</span><br/>
          <span className="l2">Laudo científico.</span>
          <span className="l3">Direção certa.</span>
        </h2>
      </div>
    </section>
  );
}

// ====== BLOCO 6 — Como funciona (4 passos) ======
function HowItWorks() {
  const steps = [
    { n: "01", t: "Preencha cadastro e pague R$ 300", time: "1 min", icon: (<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="M3 7h18v12H3z"/><path d="M3 11h18"/></svg>) },
    { n: "02", t: "Responda 40 perguntas RIASEC + DISC", time: "15–20 min", icon: (<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6"><rect x="4" y="3" width="16" height="18" rx="2"/><path d="M9 7h6M9 11h6M9 15h4"/></svg>) },
    { n: "03", t: "Equipe IPGS analisa o laudo", time: "até 48h", icon: (<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6"><circle cx="9" cy="9" r="3"/><circle cx="17" cy="11" r="2.5"/><path d="M3 20a6 6 0 0 1 12 0"/></svg>) },
    { n: "04", t: "Receba PDF + página de resultados online", time: "imediato", icon: (<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.6"><path d="M5 3h10l4 4v14H5z"/><path d="M15 3v4h4"/><path d="M8 13l2 2 4-5"/></svg>) },
  ];
  return (
    <section id="b6" className="grain" style={{ background: "var(--paper-100)" }}>
      <style>{`
        .hw-head { margin-bottom: 40px; }
        .hw-grid { display: grid; gap: 18px; }
        @media (min-width: 720px) { .hw-grid { grid-template-columns: repeat(4, 1fr); gap: 14px; position: relative; } }
        .hw-grid::before {
          content: "";
          display: none;
        }
        @media (min-width: 720px) {
          .hw-grid::before {
            display: block;
            position: absolute;
            top: 36px;
            left: 8%; right: 8%;
            height: 1px;
            background: repeating-linear-gradient(90deg, var(--ipgs-gold) 0 6px, transparent 6px 14px);
          }
        }
        .hw-card {
          background: white;
          border: 1px solid rgba(6,41,71,.08);
          border-radius: var(--r-lg);
          padding: 24px 22px;
          position: relative;
          z-index: 1;
        }
        .hw-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
        .hw-num {
          font-family: var(--font-display);
          font-style: italic;
          font-size: 56px;
          line-height: 0.9;
          color: var(--ipgs-gold);
          font-weight: 400;
        }
        .hw-icon {
          width: 40px; height: 40px;
          border-radius: 12px;
          background: var(--paper-100);
          border: 1px solid rgba(6,41,71,.10);
          display: grid; place-items: center;
          color: var(--deep-700);
        }
        .hw-time {
          display: inline-block;
          font-family: var(--font-mono);
          font-size: 10px;
          letter-spacing: 0.16em;
          text-transform: uppercase;
          color: var(--gold-700);
          padding: 4px 10px;
          border: 1px solid rgba(198,156,80,.4);
          border-radius: 999px;
          margin-bottom: 10px;
        }
        .hw-t { font-size: 16px; line-height: 1.4; color: var(--ipgs-deep); margin: 0; font-weight: 500; }
        .hw-cta { text-align: center; margin-top: 36px; }
      `}</style>
      <div className="container-x" style={{ position: "relative", zIndex: 2 }}>
        <div className="hw-head">
          <span className="eyebrow">Como funciona</span>
          <h2 className="display reveal" style={{ marginTop: 14, maxWidth: "16ch" }}>
            Quatro passos. <em>Nenhum palpite.</em>
          </h2>
          <p className="reveal" style={{ marginTop: 18, maxWidth: "56ch", color: "var(--deep-700)", fontSize: 16, lineHeight: 1.55 }}>
            <strong style={{ color: "var(--ipgs-deep)" }}>RIASEC</strong> mede o <em style={{ fontStyle: "normal", color: "var(--gold-700)" }}>interesse vocacional</em> (6 perfis).
            {" "}<strong style={{ color: "var(--ipgs-deep)" }}>DISC</strong> mede o <em style={{ fontStyle: "normal", color: "var(--gold-700)" }}>comportamento</em> (4 dimensões).
            {" "}Duas metodologias consolidadas internacionalmente, cruzadas em um único laudo.
          </p>
        </div>
        <div className="hw-grid">
          {steps.map((s) => (
            <article className="hw-card reveal" key={s.n}>
              <div className="hw-row">
                <span className="hw-num">{s.n}</span>
                <span className="hw-icon">{s.icon}</span>
              </div>
              <span className="hw-time">{s.time}</span>
              <p className="hw-t">{s.t}</p>
            </article>
          ))}
        </div>
        <div className="hw-cta">
          <CTA size="lg" position="b6">Comprar mapeamento — R$ 300</CTA>
        </div>
      </div>
    </section>
  );
}

window.BeforeAfter = BeforeAfter;
window.OneLine = OneLine;
window.HowItWorks = HowItWorks;
