/* JOIN CTA - the open hand */

function JoinCTA() {
  const handRef = useRef(null);
  const [openQuiz, setOpenQuiz] = useState(false);

  useEffect(() => {
    const onMove = (e) => {
      if (!handRef.current) return;
      const r = handRef.current.getBoundingClientRect();
      const cx = r.left + r.width / 2;
      const cy = r.top + r.height / 2;
      const dx = (e.clientX - cx);
      const dy = (e.clientY - cy);
      const dist = Math.min(1, Math.hypot(dx, dy) / 400);
      const reach = (1 - dist) * 60;
      const ax = Math.sign(dx) * reach * 0.5;
      const ay = Math.sign(dy) * reach * 0.3;
      handRef.current.style.transform = `translate(${ax}px, ${ay}px) rotate(${ax * .2}deg)`;
    };
    addEventListener('mousemove', onMove);
    return () => removeEventListener('mousemove', onMove);
  }, []);

  return (
    <section className="join" id="join">
      <div className="light-pool yellow" style={{ width: 600, height: 600, left: '40%', top: '40%', opacity: .35 }}/>
      <div className="light-pool purple drift-b" style={{ width: 700, height: 700, left: '-15%', top: '20%', opacity: .35 }}/>

      <div className="container join-inner">
        <Reveal className="join-eyebrow">
          <span className="eyebrow">- the open hand · join</span>
        </Reveal>

        <Reveal delay={1} className="join-title-wrap">
          <h2 className="h-display join-title">
            ARE <span className="italic">YOU</span><br/>
            <span className="gradient-text">GNG?</span>
          </h2>
        </Reveal>

        <Reveal delay={2} className="join-sub">
          <p>
            Step into the circle. We're not asking - we're <strong>reaching</strong>.
            The hand is out. All you have to do is grab it.
          </p>
        </Reveal>

        {/* Reaching hand silhouette */}
        <div className="join-hand-wrap">
          <div className="join-hand" ref={handRef}>
            <svg viewBox="0 0 320 380" width="100%" height="100%">
              <defs>
                <radialGradient id="handglow" cx="50%" cy="40%" r="55%">
                  <stop offset="0%" stopColor="rgba(255,199,0,.45)"/>
                  <stop offset="50%" stopColor="rgba(139,92,246,.25)"/>
                  <stop offset="100%" stopColor="rgba(0,0,0,0)"/>
                </radialGradient>
                <linearGradient id="handfill" x1="0" y1="0" x2="0" y2="1">
                  <stop offset="0%" stopColor="#0d0a1a"/>
                  <stop offset="100%" stopColor="#060109"/>
                </linearGradient>
                <filter id="handblur"><feGaussianBlur stdDeviation="6"/></filter>
              </defs>
              {/* aura */}
              <ellipse cx="160" cy="170" rx="160" ry="180" fill="url(#handglow)"/>
              {/* sleeve */}
              <path d="M 60 380 L 100 200 Q 110 170 150 165 L 200 165 Q 240 170 250 200 L 280 380 Z"
                    fill="url(#handfill)"
                    stroke="rgba(139,92,246,.25)" strokeWidth="1"/>
              {/* palm */}
              <path d="M 130 165
                       C 125 145, 130 120, 140 100
                       C 145 85, 155 75, 165 75
                       C 175 75, 180 85, 178 100
                       L 172 140
                       L 188 100
                       C 192 90, 198 82, 207 82
                       C 215 82, 218 90, 215 100
                       L 200 145
                       L 220 110
                       C 224 102, 230 96, 238 98
                       C 244 100, 246 108, 242 118
                       L 222 160
                       L 240 130
                       C 245 122, 252 120, 257 124
                       C 261 128, 260 136, 256 144
                       L 235 200
                       Q 220 240, 175 240
                       Q 130 230, 125 195
                       Z"
                    fill="url(#handfill)"
                    stroke="rgba(255,199,0,.35)" strokeWidth="1.2"
                    filter="drop-shadow(0 0 14px rgba(255,199,0,.25))"/>
              {/* finger highlights */}
              <path d="M 165 80 L 173 130" stroke="rgba(255,199,0,.4)" strokeWidth="1" fill="none"/>
              <path d="M 205 88 L 198 138" stroke="rgba(255,199,0,.3)" strokeWidth="1" fill="none"/>
              <path d="M 238 116 L 224 150" stroke="rgba(255,199,0,.25)" strokeWidth="1" fill="none"/>
              {/* particles drifting toward hand */}
              {Array.from({length: 12}).map((_, i) => (
                <circle key={i}
                        cx={20 + (i * 25) % 280}
                        cy={300 - (i * 23) % 260}
                        r={1 + (i % 3)}
                        fill="#FFC700"
                        opacity=".6">
                  <animate attributeName="cy"
                           values={`${300 - (i*23)%260};${150 + (i%30)}`}
                           dur={`${4 + i*0.3}s`}
                           repeatCount="indefinite"/>
                  <animate attributeName="opacity"
                           values=".6;.05;.6"
                           dur={`${4 + i*0.3}s`}
                           repeatCount="indefinite"/>
                </circle>
              ))}
            </svg>
          </div>
        </div>

        <Reveal delay={3} className="join-ctas">
          <Magnetic strength={.5}>
            <button className="btn btn-spark hot big" onClick={() => setOpenQuiz(true)}>
              Step into the circle <Arrow/>
            </button>
          </Magnetic>
          <div className="join-channels">
            <a href="https://x.com/webesgng" className="channel hot" target="_blank" rel="noopener noreferrer">
              <span className="ch-icon">𝕏</span>
              <span>Follow on X</span>
            </a>
            <a href="https://discord.com/invite/NG5jGQTEM" className="channel hot" target="_blank" rel="noopener noreferrer">
              <span className="ch-icon">◆</span>
              <span>Join Discord</span>
            </a>
          </div>
        </Reveal>

        <Reveal delay={4} className="join-footer-line">
          <span className="stamp green">we eating</span>
          <span>·</span>
          <span>no application form. just hands.</span>
          <span>·</span>
          <span className="stamp purple">yktv</span>
        </Reveal>
      </div>

      {openQuiz && <Quiz onClose={() => setOpenQuiz(false)} />}

      <style>{`
        .join {
          position: relative;
          min-height: 100vh;
          padding: 120px 0;
          display: flex; align-items: center;
          overflow: hidden;
        }
        .join-inner {
          position: relative;
          display: flex; flex-direction: column;
          align-items: center; text-align: center;
          gap: 28px;
          z-index: 5;
        }
        .join-title {
          font-size: clamp(80px, 16vw, 240px);
          line-height: .85;
          font-weight: 900;
        }
        .join-sub p {
          max-width: 560px;
          color: var(--muted);
          font-size: clamp(15px, 1.3vw, 18px);
        }
        .join-sub strong {
          color: var(--text);
          font-weight: 700;
          background: linear-gradient(120deg, rgba(255,199,0,.2), rgba(139,92,246,.18));
          padding: 1px 6px;
          border-radius: 4px;
        }

        .join-hand-wrap {
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -40%);
          width: 380px; height: 460px;
          pointer-events: none;
          z-index: 1;
          opacity: .9;
          mix-blend-mode: normal;
        }
        [data-theme="dark"] .join-hand-wrap { mix-blend-mode: screen; }
        .join-hand {
          width: 100%; height: 100%;
          transition: transform 1s var(--ease-out);
        }
        @media (max-width: 720px) {
          .join-hand-wrap { width: 260px; height: 320px; opacity: .4; }
        }

        .join-ctas {
          display: flex; flex-direction: column; align-items: center;
          gap: 24px;
          position: relative; z-index: 5;
        }
        .btn-spark.big {
          padding: 22px 36px;
          font-size: 18px;
          border-radius: 999px;
        }

        .join-channels {
          display: flex; gap: 14px; flex-wrap: wrap; justify-content: center;
        }
        .channel {
          display: inline-flex; align-items: center; gap: 10px;
          padding: 12px 18px;
          border-radius: 999px;
          background: var(--w-04);
          border: 1px solid var(--w-08);
          color: var(--text);
          font-size: 14px;
          font-weight: 500;
          transition: all .3s var(--ease-out);
        }
        .channel:hover {
          background: var(--w-08);
          border-color: rgba(22,224,122,.4);
          transform: translateY(-2px);
        }
        .ch-icon {
          width: 22px; height: 22px; border-radius: 50%;
          background: var(--grad);
          display: grid; place-items: center;
          color: #0a0711;
          font-weight: 800;
          font-size: 12px;
        }

        .join-footer-line {
          display: flex; align-items: center; gap: 14px;
          color: var(--muted);
          font-size: 13px;
          flex-wrap: wrap; justify-content: center;
        }
      `}</style>
    </section>
  );
}

/* Mini "Are you GNG?" quiz modal */
function Quiz({ onClose }) {
  const [step, setStep] = useState(0);
  const [answers, setAnswers] = useState({});

  const qs = [
    {
      q: "What time do you typically ship?",
      opts: ['First thing morning', 'Whenever it\'s done', '2am, the gang\'s up', 'I don\'t ship - yet'],
    },
    {
      q: "Pick one:",
      opts: ['I want to learn Web3', 'I want to make money in Web3', 'I want to build a circle', 'All three (correct)'],
    },
    {
      q: "Your reaction when a friend wins:",
      opts: ['Like the post', 'Slide in their DMs', 'Repost & hype it', 'I want them to teach me'],
    },
  ];

  const choose = (val) => {
    setAnswers(a => ({...a, [step]: val }));
    if (step < qs.length - 1) setStep(step + 1);
    else setStep(qs.length);
  };

  const done = step >= qs.length;

  return (
    <div className="quiz-back" onClick={onClose}>
      <div className="quiz" onClick={e => e.stopPropagation()}>
        <button className="quiz-close hot" onClick={onClose}>✕</button>

        {!done ? (
          <>
            <div className="quiz-progress">
              {qs.map((_, i) => (
                <span key={i} className={`p ${i <= step ? 'on' : ''}`}></span>
              ))}
            </div>
            <div className="quiz-step">{String(step+1).padStart(2,'0')} / {String(qs.length).padStart(2,'0')}</div>
            <h3 className="quiz-q">{qs[step].q}</h3>
            <div className="quiz-opts">
              {qs[step].opts.map((o, i) => (
                <button key={i} className="quiz-opt hot" onClick={() => choose(o)}>
                  <span className="qd">{String.fromCharCode(65+i)}</span>
                  <span>{o}</span>
                  <Arrow/>
                </button>
              ))}
            </div>
          </>
        ) : (
          <div className="quiz-done">
            <div className="quiz-eye"><GlowingEyes size={140}/></div>
            <span className="stamp green">verdict</span>
            <h3 className="quiz-final h-display">YOU'RE <span className="gradient-text italic">GNG.</span></h3>
            <p>Already, lowkey. Pick a door - see you in motion.</p>
            <div className="join-channels">
              <a href="https://x.com/webesgng" className="channel hot" target="_blank" rel="noopener noreferrer"><span className="ch-icon">𝕏</span>X</a>
              <a href="https://discord.com/invite/NG5jGQTEM" className="channel hot" target="_blank" rel="noopener noreferrer"><span className="ch-icon">◆</span>Discord</a>
            </div>
          </div>
        )}
      </div>

      <style>{`
        .quiz-back {
          position: fixed; inset: 0; z-index: 800;
          background: rgba(5,3,12,.7);
          backdrop-filter: blur(14px);
          display: grid; place-items: center;
          padding: 24px;
          animation: backIn .35s var(--ease-out);
        }
        @keyframes backIn { from { opacity: 0; } to { opacity: 1; } }
        .quiz {
          position: relative;
          width: 100%;
          max-width: 520px;
          padding: 40px 36px;
          background: var(--modal-bg);
          border: 1px solid rgba(139,92,246,.25);
          border-radius: var(--r-xl);
          box-shadow: var(--modal-shadow);
          animation: modalIn .55s var(--ease-overshoot);
        }
        @keyframes modalIn {
          from { opacity: 0; transform: scale(.94) translateY(20px); }
          to { opacity: 1; transform: scale(1) translateY(0); }
        }
        .quiz-close {
          position: absolute; top: 18px; right: 18px;
          width: 36px; height: 36px; border-radius: 50%;
          background: var(--w-06);
          color: var(--text); font-size: 14px;
        }
        .quiz-progress {
          display: flex; gap: 6px; margin-bottom: 18px;
        }
        .quiz-progress .p {
          flex: 1; height: 4px;
          background: var(--ln-08);
          border-radius: 4px; overflow: hidden;
          transition: background .3s;
        }
        .quiz-progress .p.on { background: var(--green); box-shadow: 0 0 12px rgba(22,224,122,.5); }
        .quiz-step {
          font-size: 11px; color: var(--muted);
          letter-spacing: .2em; text-transform: uppercase;
          margin-bottom: 6px;
          font-variant-numeric: tabular-nums;
        }
        .quiz-q {
          font-family: var(--font-display);
          font-size: 28px; font-weight: 600;
          letter-spacing: -.01em;
          margin-bottom: 24px;
          color: var(--text);
        }
        .quiz-opts { display: flex; flex-direction: column; gap: 10px; }
        .quiz-opt {
          display: flex; align-items: center; gap: 14px;
          padding: 14px 16px;
          background: var(--w-03);
          border: 1px solid var(--w-07);
          border-radius: var(--r-md);
          color: var(--text);
          font-size: 14px; font-weight: 500;
          text-align: left;
          transition: all .25s var(--ease-out);
        }
        .quiz-opt:hover {
          background: rgba(22,224,122,.08);
          border-color: rgba(22,224,122,.4);
          transform: translateX(4px);
        }
        .quiz-opt .qd {
          width: 24px; height: 24px; border-radius: 6px;
          background: var(--w-05);
          display: grid; place-items: center;
          font-size: 11px; font-weight: 700;
          color: var(--muted);
          flex-shrink: 0;
        }
        .quiz-opt:hover .qd { color: var(--green); }
        .quiz-opt .arrow { margin-left: auto; }

        .quiz-done {
          display: flex; flex-direction: column; align-items: center; gap: 16px; text-align: center;
        }
        .quiz-eye { margin-bottom: 4px; }
        .quiz-final {
          font-size: clamp(40px, 6vw, 64px);
          line-height: 1;
        }
        .quiz-done p { color: var(--muted); }
      `}</style>
    </div>
  );
}

window.JoinCTA = JoinCTA;
