/* On The Limit — Find Your Fit enquiry flow modal */
function FindYourFit({ onClose }) {
  const { Button } = window.DesignSystem_494066;
  const go = window.OTL_go;
  const isMobile = window.OTL_useIsMobile();
  const [step, setStep] = React.useState(0);
  const [answers, setAnswers] = React.useState({});
  const [result, setResult] = React.useState(null);
  const [animDir, setAnimDir] = React.useState('forward');

  const questions = [
    {
      key: 'goal',
      q: 'What do you want to get out of your gym experience?',
      options: [
        { id: 'fitter',    label: 'Get fitter & lose weight',          icon: '🏃' },
        { id: 'strength',  label: 'Build strength & muscle',            icon: '💪' },
        { id: 'event',     label: 'Train for an event (HYROX, race…)',  icon: '🏆' },
        { id: 'wellbeing', label: 'Improve my health & wellbeing',      icon: '🧘' },
        { id: 'community', label: 'Find my people & stay motivated',    icon: '🤝' },
      ],
    },
    {
      key: 'level',
      q: 'How would you describe yourself right now?',
      options: [
        { id: 'beginner',    label: "Just starting out, haven't trained much" },
        { id: 'returning',   label: 'Getting back into it after a break' },
        { id: 'active',      label: 'Already active, want more structure' },
        { id: 'experienced', label: 'Training regularly, ready to push harder' },
      ],
    },
    {
      key: 'style',
      q: 'How do you prefer to train?',
      options: [
        { id: 'group',    label: 'In a group, I love the energy' },
        { id: 'one2one',  label: 'One-to-one with a coach' },
        { id: 'solo',     label: 'On my own, at my own pace' },
        { id: 'mix',      label: 'Mix of everything, variety keeps me going' },
      ],
    },
    {
      key: 'frequency',
      q: 'How often are you hoping to train each week?',
      options: [
        { id: '1-2', label: '1–2 times, steady start' },
        { id: '3-4', label: '3–4 times, consistent routine' },
        { id: '5+',  label: '5+ times, I mean business' },
      ],
    },
  ];

  const APP_STORE_URL = 'https://apps.apple.com/app/on-the-limit'; // update with real App Store link
  const PLAY_STORE_URL = 'https://play.google.com/store/apps/details?id=com.onthelimit'; // update with real Play Store link

  function getResult(a) {
    // Elevate: only studio-focused, wellbeing goal, lightest commitment
    if (a.style === 'group' && a.goal === 'wellbeing' && a.frequency === '1-2') return 'elevate';
    // Everything else → Accelerate X
    return 'accelerate';
  }

  function getAccelerateBody(a) {
    if (a.goal === 'event')      return "You're training with a target in mind, Accelerate X gives you full HYROX sessions, GRIT, the gym floor and Studio Classes all under one roof. Everything you need to hit that start line in the best shape of your life.";
    if (a.goal === 'strength')   return "Building real strength takes variety, heavy lifting on the gym floor, GRIT sessions that push your limits, and coaches who keep your technique honest. Accelerate X gives you all of that, every single day.";
    if (a.goal === 'fitter')     return "Getting fitter happens fastest when you've got options, GRIT to build conditioning, Studio Classes to keep it fresh, and the gym floor for the days you want to go at your own pace. Accelerate X covers it all.";
    if (a.goal === 'community')  return "The best results come when you're part of something. With Accelerate X you're in every GRIT session, every studio class, every HYROX session, training alongside the same people, week in, week out.";
    if (a.style === 'one2one')   return "Accelerate X gives you full access to every session and the gym floor, and you can add Personal Training on top whenever you're ready for dedicated one-to-one coaching. The best of both worlds.";
    return "Accelerate X is our flagship membership and the one the vast majority of our members choose. GRIT, Studio Classes, HYROX, the full gym floor, everything included, no tie-ins, coaches alongside you every step.";
  }

  const RESULTS = {
    accelerate: (a) => ({
      tag: 'Accelerate X',
      body: getAccelerateBody(a),
      whatsIncluded: [
        'GRIT sessions (group PT)',
        'Studio Classes & RIDE (150+ per month)',
        'HYROX Training',
        'Full gym floor access, 7 days a week',
        'Coached every session',
        'No tie-in contracts, cancel any time',
      ],
      membershipNote: 'Everything included. No tie-in contracts.',
    }),
    elevate: () => ({
      tag: 'Elevate',
      body: "Elevate gives you unlimited Studio Classes, RIDE and full gym floor access, perfect if classes and your own training sessions are your thing. Over 150 group classes a month, coached every time, with a rotating timetable that never gets stale.",
      whatsIncluded: [
        'Unlimited Studio Classes & RIDE',
        '150+ classes per month',
        'Full gym floor access, 7 days a week',
        'Coached every session',
        'No tie-in contracts, cancel any time',
      ],
      membershipNote: 'Studio & gym floor access. No tie-in contracts.',
    }),
  };

  function buildResult(a) {
    const key = getResult(a);
    const builder = RESULTS[key];
    return { key, ...builder(a) };
  }

  function pick(key, id) {
    const newAnswers = { ...answers, [key]: id };
    setAnswers(newAnswers);
    setAnimDir('forward');
    if (step < questions.length - 1) {
      setStep(s => s + 1);
    } else {
      setResult(buildResult(newAnswers));
    }
  }

  function back() {
    setAnimDir('back');
    if (result) { setResult(null); return; }
    if (step > 0) setStep(s => s - 1);
  }

  const progress = result ? 1 : step / questions.length;

  const modal = (
    <div
      onClick={e => e.target === e.currentTarget && onClose()}
      style={{
        position: 'fixed', inset: 0, zIndex: 10000,
        background: 'rgba(0,0,0,0.78)',
        backdropFilter: 'blur(6px)',
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        padding: '20px',
      }}
    >
      <div style={{
        background: 'var(--surface-card)',
        border: '1px solid var(--border-subtle)',
        borderRadius: 24,
        width: '100%', maxWidth: 560,
        padding: isMobile ? '28px 20px 24px' : '40px 36px 36px',
        maxHeight: isMobile ? '90vh' : 'none',
        overflowY: isMobile ? 'auto' : 'visible',
        position: 'relative',
        boxShadow: '0 32px 80px rgba(0,0,0,0.6)',
        animation: 'otl-page-in 0.3s var(--ease-out) both',
      }}>

        {/* Close */}
        <button onClick={onClose} style={{
          position: 'absolute', top: 16, right: 16,
          width: 36, height: 36, borderRadius: 99,
          background: 'rgba(255,255,255,0.07)',
          border: '1px solid rgba(255,255,255,0.12)',
          color: 'var(--fog-300)', fontSize: 18, cursor: 'pointer',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          lineHeight: 1,
        }}>×</button>

        {/* Progress bar */}
        <div style={{ marginBottom: 32 }}>
          <div style={{
            height: 3, background: 'rgba(255,255,255,0.08)',
            borderRadius: 99, overflow: 'hidden',
          }}>
            <div style={{
              height: '100%',
              width: `${Math.round(progress * 100)}%`,
              background: 'var(--accent)',
              borderRadius: 99,
              transition: 'width 0.4s var(--ease-out)',
            }} />
          </div>
          <div style={{
            marginTop: 8,
            fontFamily: 'var(--font-body)', fontSize: 12,
            color: 'var(--text-muted)',
          }}>
            {result ? 'Your result' : `Step ${step + 1} of ${questions.length}`}
          </div>
        </div>

        {result ? (
          /* RESULT SCREEN */
          <div style={{ display: 'flex', flexDirection: 'column', gap: 18 }}>

            {/* Label */}
            <div style={{
              fontFamily: 'var(--font-body)', fontSize: 13,
              letterSpacing: '0.06em', textTransform: 'uppercase',
              color: 'var(--text-muted)',
            }}>Your best membership is</div>

            {/* Membership name */}
            <h2 style={{
              margin: 0, fontFamily: 'var(--font-display)', fontWeight: 800,
              textTransform: 'uppercase', fontSize: isMobile ? 24 : 32, lineHeight: 1,
              color: 'var(--accent)', letterSpacing: '-0.02em',
            }}>{result.tag}</h2>

            {/* Why */}
            <p style={{
              margin: 0, fontFamily: 'var(--font-body)', fontSize: 15,
              lineHeight: 1.6, color: 'var(--fog-300)',
            }}>{result.body}</p>

            {/* Bullet points */}
            <div style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
              {result.whatsIncluded.map(item => (
                <div key={item} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
                  <span style={{
                    width: 20, height: 20, borderRadius: 99,
                    background: 'var(--mint-tint)', border: '1px solid rgba(114,221,158,0.3)',
                    display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
                  }}>
                    <span style={{ width: 6, height: 6, borderRadius: 99, background: 'var(--accent)' }} />
                  </span>
                  <span style={{ fontFamily: 'var(--font-body)', fontSize: 14.5, color: 'var(--fog-200)' }}>{item}</span>
                </div>
              ))}
            </div>

            <div style={{ height: 1, background: 'var(--border-subtle)' }} />

            {/* Download label */}
            <div style={{
              fontFamily: 'var(--font-body)', fontSize: 13,
              color: 'var(--text-muted)',
            }}>Join via the On The Limit app:</div>

            {/* Store badges */}
            <div style={{ display: 'flex', flexDirection: isMobile ? 'column' : 'row', gap: 10 }}>

              {/* App Store */}
              <a href={APP_STORE_URL} target="_blank" rel="noopener" style={{
                display: 'inline-flex', alignItems: 'center', gap: 10,
                background: '#000', border: '1px solid rgba(255,255,255,0.2)',
                borderRadius: 10, padding: '9px 18px', textDecoration: 'none',
                transition: 'border-color .2s, transform .15s',
              }}
                onMouseEnter={e => { e.currentTarget.style.borderColor='rgba(255,255,255,0.5)'; e.currentTarget.style.transform='translateY(-1px)'; }}
                onMouseLeave={e => { e.currentTarget.style.borderColor='rgba(255,255,255,0.2)'; e.currentTarget.style.transform='none'; }}
              >
                <svg width="18" height="18" viewBox="0 0 24 24" fill="white" xmlns="http://www.w3.org/2000/svg">
                  <path d="M18.71 19.5c-.83 1.24-1.71 2.45-3.05 2.47-1.34.03-1.77-.79-3.29-.79-1.53 0-2 .77-3.27.82-1.31.05-2.3-1.32-3.14-2.53C4.25 17 2.94 12.45 4.7 9.39c.87-1.52 2.43-2.48 4.12-2.51 1.28-.02 2.5.87 3.29.87.78 0 2.26-1.07 3.8-.91.65.03 2.47.26 3.64 1.98-.09.06-2.17 1.28-2.15 3.81.03 3.02 2.65 4.03 2.68 4.04-.03.07-.42 1.44-1.38 2.83M13 3.5c.73-.83 1.94-1.46 2.94-1.5.13 1.17-.34 2.35-1.04 3.19-.69.85-1.83 1.51-2.95 1.42-.15-1.15.41-2.35 1.05-3.11z"/>
                </svg>
                <div style={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
                  <span style={{ fontFamily: 'var(--font-body)', fontSize: 9.5, color: 'rgba(255,255,255,0.6)', lineHeight: 1, letterSpacing: '0.02em' }}>Download on the</span>
                  <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 16, color: '#fff', lineHeight: 1, letterSpacing: '-0.01em' }}>App Store</span>
                </div>
              </a>

              {/* Google Play */}
              <a href={PLAY_STORE_URL} target="_blank" rel="noopener" style={{
                display: 'inline-flex', alignItems: 'center', gap: 10,
                background: '#000', border: '1px solid rgba(255,255,255,0.2)',
                borderRadius: 10, padding: '9px 18px', textDecoration: 'none',
                transition: 'border-color .2s, transform .15s',
              }}
                onMouseEnter={e => { e.currentTarget.style.borderColor='rgba(255,255,255,0.5)'; e.currentTarget.style.transform='translateY(-1px)'; }}
                onMouseLeave={e => { e.currentTarget.style.borderColor='rgba(255,255,255,0.2)'; e.currentTarget.style.transform='none'; }}
              >
                <svg width="22" height="24" viewBox="0 0 22 24" xmlns="http://www.w3.org/2000/svg">
                  <path d="M.538.812C.202 1.17 0 1.73 0 2.46v19.082c0 .73.202 1.29.538 1.646l.087.085 10.7-10.7v-.252L.625.727.538.812z" fill="#4FC3F7"/>
                  <path d="M14.893 14.929l-3.568-3.567v-.252l3.569-3.569.08.046 4.228 2.402c1.207.686 1.207 1.808 0 2.494l-4.228 2.402-.081.044z" fill="#FFCA28"/>
                  <path d="M14.974 14.883L11.325 11.23.538 22.018c.398.42 1.055.473 1.796.054l12.64-7.189" fill="#F44336"/>
                  <path d="M14.974 9.579L2.334 2.39C1.593 1.97.936 2.024.538 2.444l10.787 10.787 3.649-3.652z" fill="#4CAF50"/>
                </svg>
                <div style={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
                  <span style={{ fontFamily: 'var(--font-body)', fontSize: 9.5, color: 'rgba(255,255,255,0.6)', lineHeight: 1, letterSpacing: '0.02em' }}>Get it on</span>
                  <span style={{ fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 16, color: '#fff', lineHeight: 1, letterSpacing: '-0.01em' }}>Google Play</span>
                </div>
              </a>
            </div>

            <button onClick={back} style={{
              background: 'none', border: 'none', cursor: 'pointer',
              fontFamily: 'var(--font-body)', fontSize: 13,
              color: 'var(--text-muted)', textAlign: 'left', padding: 0,
            }}>← Start over</button>
          </div>
        ) : (
          /* QUESTION SCREEN */
          <div style={{ display: 'flex', flexDirection: 'column', gap: 24 }}>
            <h2 style={{
              margin: 0, fontFamily: 'var(--font-display)', fontWeight: 800,
              textTransform: 'uppercase', fontSize: 22, lineHeight: 1.1,
              color: '#fff', letterSpacing: '-0.01em', maxWidth: 440,
            }}>{questions[step].q}</h2>

            <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
              {questions[step].options.map(opt => (
                <button
                  key={opt.id}
                  onClick={() => pick(questions[step].key, opt.id)}
                  style={{
                    background: 'rgba(255,255,255,0.04)',
                    border: '1px solid rgba(255,255,255,0.1)',
                    borderRadius: 12, padding: '15px 18px',
                    display: 'flex', alignItems: 'center', gap: 14,
                    cursor: 'pointer', textAlign: 'left',
                    transition: 'background .15s, border-color .15s',
                    width: '100%',
                  }}
                  onMouseEnter={e => {
                    e.currentTarget.style.background = 'rgba(114,221,158,0.08)';
                    e.currentTarget.style.borderColor = 'rgba(114,221,158,0.4)';
                  }}
                  onMouseLeave={e => {
                    e.currentTarget.style.background = 'rgba(255,255,255,0.04)';
                    e.currentTarget.style.borderColor = 'rgba(255,255,255,0.1)';
                  }}
                >
                  {opt.icon && (
                    <span style={{ fontSize: 22, lineHeight: 1, flexShrink: 0 }}>{opt.icon}</span>
                  )}
                  <span style={{
                    fontFamily: 'var(--font-body)', fontSize: 15,
                    color: 'var(--fog-200)', lineHeight: 1.4,
                  }}>{opt.label}</span>
                  <span style={{
                    marginLeft: 'auto', color: 'var(--accent)',
                    fontSize: 18, flexShrink: 0, opacity: 0.6,
                  }}>›</span>
                </button>
              ))}
            </div>

            {step > 0 && (
              <button onClick={back} style={{
                background: 'none', border: 'none', cursor: 'pointer',
                fontFamily: 'var(--font-body)', fontSize: 13,
                color: 'var(--text-muted)', textAlign: 'left', padding: 0,
              }}>← Back</button>
            )}
          </div>
        )}
      </div>
    </div>
  );
  return ReactDOM.createPortal(modal, document.body);
}
window.OTL_FindYourFit = FindYourFit;
