// screens-onboarding.jsx — Onboarding flow: pace / distance / level / neighborhood

const APP_VERSION = 'v1.0.5';

const ONBOARD_STEPS = [
  { id: 'welcome', title: 'Small local runs,\nmatched by pace.', sub: `Create or join 2–8 person runs near you. Verified hosts and meeting points you can trust. ${APP_VERSION}` },
  { id: 'phone',   title: 'Verify your phone',     sub: 'Used to confirm who you are. Never shown to other runners.' },
  { id: 'profile', title: 'About you',             sub: 'Just the basics. You can keep your last name private.' },
  { id: 'pace',     title: 'Your usual pace',           sub: 'We\u2019ll match you to runs in your range so nobody gets dropped.' },
  { id: 'distance', title: 'Preferred distance',          sub: 'We\u2019ll highlight runs that match what you like.' },
  { id: 'area',     title: 'Where do you like to run?',   sub: 'We\u2019ll show runs starting nearby first.' },
];

function ProgressDots({ idx, total }) {
  return (
    <div style={{ display: 'flex', gap: 6 }}>
      {Array.from({ length: total }).map((_, i) => (
        <div key={i} style={{
          height: 3, width: i === idx ? 22 : 14,
          background: i <= idx ? PACER.lime : 'rgba(255,255,255,0.12)',
          borderRadius: 99, transition: 'all .25s',
        }}/>
      ))}
    </div>
  );
}

function PaceStep({ pace, setPace, onSkip }) {
  // pace stored as tick index 0..14 (8:00 → 4:30, 15s steps)
  const TICKS = [
    '8:00','7:45','7:30','7:15','7:00',
    '6:45','6:30','6:15','6:00','5:45',
    '5:30','5:15','5:00','4:45','4:30',
  ];
  const MAJOR = [0, 2, 4, 6, 8, 10, 12, 14];
  const LABEL = [0, 4, 8, 12, 14];
  const idx = typeof pace === 'number' ? pace : 8;
  const setIdx = (v) => setPace(v);
  const value = TICKS[idx];
  const pct = (idx / (TICKS.length - 1)) * 100;
  return (
    <div>
      {/* Big pace readout */}
      <div style={{ textAlign: 'center', marginBottom: 28 }}>
        <div style={{
          fontFamily: PACER.mono, fontSize: 84, fontWeight: 500,
          color: PACER.lime, letterSpacing: -3, lineHeight: 1,
        }}>{value}</div>
        <div style={{
          fontFamily: PACER.mono, fontSize: 13, color: PACER.textMute, marginTop: 10, letterSpacing: 1.4,
        }}>MIN / KM</div>
        <div style={{
          fontFamily: PACER.font, fontSize: 12.5, color: PACER.textDim, marginTop: 10,
        }}>
          5K in ~{Math.round((parseInt(value.split(':')[0]) * 60 + parseInt(value.split(':')[1])) * 5 / 60)} min
        </div>
      </div>

      {/* Slider card */}
      <div style={{
        background: PACER.card, border: `1px solid ${PACER.border}`,
        borderRadius: 18, padding: '24px 22px 20px',
      }}>
        <div style={{ position: 'relative', height: 36 }}>
          <div style={{
            position: 'absolute', top: 17, left: 0, right: 0, height: 2,
            background: 'rgba(255,255,255,0.06)', borderRadius: 99,
          }}/>
          <div style={{
            position: 'absolute', top: 17, left: 0, width: `${pct}%`,
            height: 2, background: PACER.lime, borderRadius: 99,
          }}/>
          <div style={{
            position: 'absolute', top: 8, left: `${pct}%`, transform: 'translateX(-50%)',
            width: 22, height: 22, borderRadius: '50%',
            background: PACER.lime, border: `3px solid ${PACER.bg}`,
            boxShadow: `0 0 0 1px ${PACER.lime}, 0 4px 14px rgba(212,255,58,0.35)`,
            pointerEvents: 'none',
          }}/>
          <input type="range" min="0" max={TICKS.length - 1} value={idx}
            onChange={e => setIdx(parseInt(e.target.value))}
            style={{
              position: 'absolute', inset: 0, width: '100%', height: 36,
              opacity: 0, cursor: 'pointer', appearance: 'none',
            }}/>
        </div>
        <div style={{ position: 'relative', height: 14, marginTop: 8 }}>
          {LABEL.map(i => (
            <span key={i} style={{
              position: 'absolute', left: `${(i / (TICKS.length - 1)) * 100}%`,
              transform: i === 0 ? 'translateX(0)' : i === 14 ? 'translateX(-100%)' : 'translateX(-50%)',
              fontFamily: PACER.mono, fontSize: 10, color: PACER.textSubtle,
            }}>{TICKS[i]}</span>
          ))}
        </div>
      </div>

      {/* Skip link */}
      <button onClick={onSkip} style={{
        marginTop: 16, width: '100%',
        background: 'transparent', border: 'none',
        padding: '12px 14px',
        color: PACER.textDim, cursor: 'pointer',
        fontFamily: PACER.font, fontSize: 13.5, fontWeight: 500,
        textAlign: 'center', textDecoration: 'underline',
        textDecorationColor: PACER.borderStrong, textUnderlineOffset: 4,
      }}>
        I don't know my pace
      </button>
    </div>
  );
}

function PhoneStep({ onNext }) {
  const [phone, setPhone] = React.useState('7700 900 184');
  return (
    <div>
      <div style={{
        fontSize: 11, fontWeight: 600, color: PACER.textMute,
        letterSpacing: 1.4, textTransform: 'uppercase', marginBottom: 10,
      }}>Phone number</div>

      <div style={{
        background: PACER.card, border: `1px solid ${PACER.border}`,
        borderRadius: 14, padding: '14px 16px',
        display: 'flex', alignItems: 'center', gap: 12,
      }}>
        <div style={{
          display: 'flex', alignItems: 'center', gap: 6,
          paddingRight: 12, borderRight: `1px solid ${PACER.border}`,
        }}>
          <span style={{ fontSize: 16 }}>🇬🇧</span>
          <span style={{ fontFamily: PACER.mono, fontSize: 15, color: PACER.text, fontWeight: 500 }}>+44</span>
        </div>
        <input
          value={phone}
          onChange={e => setPhone(e.target.value)}
          placeholder="7700 900 184"
          inputMode="tel"
          style={{
            flex: 1, background: 'transparent', border: 'none',
            color: PACER.text, fontFamily: PACER.mono, fontSize: 17, fontWeight: 500,
            outline: 'none', letterSpacing: 1, padding: 0,
          }}
        />
      </div>

      <div style={{
        marginTop: 14, padding: '12px 14px', borderRadius: 12,
        background: 'rgba(255,255,255,0.02)', border: `1px solid ${PACER.border}`,
        display: 'flex', alignItems: 'flex-start', gap: 10,
      }}>
        <Icon name="shield" size={14} color={PACER.textDim} style={{ marginTop: 1, flexShrink: 0 }}/>
        <div style={{ fontSize: 11.5, color: PACER.textMute, lineHeight: 1.5 }}>
          Your number is used for verification only. Never shared with other runners.
        </div>
      </div>
    </div>
  );
}

function OtpStep({ onNext }) {
  const code = ['7', '3', '9', '', '', ''];
  return (
    <div>
      <div style={{ display: 'flex', gap: 8, marginBottom: 16 }}>
        {code.map((d, i) => {
          const empty = !d;
          const isCursor = i === 3;
          return (
            <div key={i} style={{
              flex: 1, height: 64, borderRadius: 14,
              background: empty ? PACER.inset : PACER.card,
              border: `1px solid ${isCursor ? PACER.lime : empty ? PACER.border : PACER.limeBorder}`,
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              fontFamily: PACER.mono, fontSize: 26, fontWeight: 500,
              color: empty ? PACER.textSubtle : PACER.text,
              position: 'relative',
            }}>
              {d}
              {isCursor && (
                <div style={{
                  position: 'absolute', width: 2, height: 30,
                  background: PACER.lime, borderRadius: 1,
                }}/>
              )}
            </div>
          );
        })}
      </div>

      <div style={{
        display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        fontSize: 12.5, color: PACER.textMute,
      }}>
        <span>Didn't get it?</span>
        <span style={{ color: PACER.textDim, fontFamily: PACER.mono }}>Resend in 0:24</span>
      </div>
    </div>
  );
}

function ProfileStep({ name, setName, gender, setGender, age, setAge, area, setArea }) {
  const ageBuckets = ['18–24', '25–34', '35–44', '45–54', '55+'];
  const genders = ['Woman', 'Man', 'Non-binary', 'Prefer not to say'];
  return (
    <div>
      <div style={{ display: 'flex', alignItems: 'center', gap: 16, marginBottom: 22 }}>
        <Avatar name={name || 'Sam'} size={64} photo/>
        <div>
          <button style={{
            padding: '10px 14px', borderRadius: 12,
            background: PACER.card, border: `1px solid ${PACER.border}`,
            color: PACER.text, fontSize: 13, fontWeight: 500, cursor: 'pointer',
          }}>Add photo</button>
          <div style={{ fontSize: 11.5, color: PACER.textMute, marginTop: 6, lineHeight: 1.4 }}>
            Must be your face so others can recognise you when you show up.
          </div>
        </div>
      </div>

      {/* Name */}
      <div style={{ marginBottom: 18 }}>
        <div style={{ fontSize: 11, fontWeight: 600, color: PACER.textMute, letterSpacing: 1.4, textTransform: 'uppercase', marginBottom: 8 }}>First name</div>
        <input value={name} onChange={e => setName(e.target.value)} style={{
          width: '100%', boxSizing: 'border-box', height: 48,
          background: PACER.inset, border: `1px solid ${PACER.border}`,
          borderRadius: 14, padding: '0 16px',
          color: PACER.text, fontFamily: PACER.font, fontSize: 16, outline: 'none',
        }}/>
      </div>

      {/* Gender */}
      <div style={{ marginBottom: 18 }}>
        <div style={{
          display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 8,
        }}>
          <div style={{ fontSize: 11, fontWeight: 600, color: PACER.textMute, letterSpacing: 1.4, textTransform: 'uppercase' }}>Gender</div>
          <span style={{ fontSize: 10.5, color: PACER.textSubtle }}>Used for women-only runs</span>
        </div>
        <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6 }}>
          {genders.map(g => {
            const sel = gender === g;
            return (
              <button key={g} onClick={() => setGender(g)} style={{
                padding: '9px 13px', borderRadius: 99,
                background: sel ? PACER.lime : 'transparent',
                border: `1px solid ${sel ? PACER.lime : PACER.borderStrong}`,
                color: sel ? PACER.limeInk : PACER.text,
                fontSize: 13, fontWeight: sel ? 600 : 500, cursor: 'pointer',
              }}>{g}</button>
            );
          })}
        </div>
      </div>

      {/* Age */}
      <div style={{ marginBottom: 16 }}>
        <div style={{ fontSize: 11, fontWeight: 600, color: PACER.textMute, letterSpacing: 1.4, textTransform: 'uppercase', marginBottom: 8 }}>Age</div>
        <div style={{
          display: 'flex', background: PACER.inset, border: `1px solid ${PACER.border}`,
          borderRadius: 12, padding: 4, gap: 4,
        }}>
          {ageBuckets.map(a => {
            const sel = age === a;
            return (
              <button key={a} onClick={() => setAge(a)} style={{
                flex: 1, height: 36, borderRadius: 9, border: 'none',
                background: sel ? PACER.card : 'transparent',
                color: sel ? PACER.text : PACER.textMute,
                fontFamily: PACER.mono, fontSize: 12.5,
                fontWeight: sel ? 600 : 500, cursor: 'pointer',
              }}>{a}</button>
            );
          })}
        </div>
      </div>

      <div style={{
        display: 'flex', alignItems: 'center', gap: 10, padding: '10px 12px',
        background: 'rgba(255,255,255,0.02)', border: `1px solid ${PACER.border}`,
        borderRadius: 10, fontSize: 12, color: PACER.textMute,
      }}>
        <Icon name="eye" size={14} color={PACER.textMute}/>
        Only your first name and an age range show on runs.
      </div>
    </div>
  );
}

function DistanceStep({ dist, setDist }) {
  return (
    <div>
      <div style={{ fontSize: 11, fontWeight: 600, color: PACER.textMute, letterSpacing: 1.4, textTransform: 'uppercase', marginBottom: 10 }}>
        Preferred distance
      </div>
      <SegmentedControl
        options={['3K', '5K', '10K', '10K+']}
        value={dist} onChange={setDist} mono
      />
    </div>
  );
}

function AreaStep({ area, setArea }) {
  const areas = ['Bristol', 'Bath', 'Other'];
  return (
    <div>
      <div style={{
        background: PACER.card, border: `1px solid ${PACER.border}`,
        borderRadius: 14, padding: '14px 16px',
        display: 'flex', alignItems: 'center', gap: 10, marginBottom: 16,
      }}>
        <Icon name="search" size={16} color={PACER.textMute}/>
        <span style={{ flex: 1, fontSize: 14, color: PACER.textDim }}>Search neighborhoods…</span>
      </div>
      <div style={{ display: 'flex', flexWrap: 'wrap', gap: 8 }}>
        {areas.map(a => {
          const sel = a === area;
          return (
            <button key={a} onClick={() => setArea(a)} style={{
              padding: '10px 14px', borderRadius: 99,
              background: sel ? PACER.lime : 'transparent',
              border: `1px solid ${sel ? PACER.lime : PACER.borderStrong}`,
              color: sel ? PACER.limeInk : PACER.text,
              fontFamily: PACER.font, fontSize: 13.5, fontWeight: sel ? 600 : 500,
              cursor: 'pointer', transition: 'all .15s',
            }}>{a}</button>
          );
        })}
      </div>
    </div>
  );
}

function WelcomeStep() {
  return (
    <div>
      <div style={{
        background: PACER.card, border: `1px solid ${PACER.border}`,
        borderRadius: 18, padding: 18, marginBottom: 14,
      }}>
        <div style={{
          fontFamily: PACER.font, fontSize: 11, fontWeight: 600,
          color: PACER.lime, letterSpacing: 1.4, textTransform: 'uppercase', marginBottom: 8,
        }}>How Run Crew works</div>
        <div style={{ display: 'grid', gap: 14 }}>
          {[
            ['Browse runs near you — or create your own', 'pace'],
            ['Request to join, or approve who joins yours', 'check'],
            ['Meeting point + group chat unlocks', 'unlock'],
            ['Show up and run together', 'run'],
          ].map(([t, icon], i) => (
            <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 12 }}>
              <div style={{
                width: 32, height: 32, borderRadius: 9,
                background: 'rgba(212,255,58,0.08)', border: `1px solid ${PACER.limeBorder}`,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                fontFamily: PACER.mono, fontSize: 12, color: PACER.lime, fontWeight: 600,
              }}>{i + 1}</div>
              <div style={{ fontSize: 14, color: PACER.text, fontWeight: 500 }}>{t}</div>
            </div>
          ))}
        </div>
      </div>
      <div style={{
        background: 'rgba(255,255,255,0.02)', border: `1px solid ${PACER.border}`,
        borderRadius: 14, padding: 14,
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 6 }}>
          <Icon name="shield" size={14} color={PACER.textDim}/>
          <span style={{ fontSize: 11.5, fontWeight: 600, color: PACER.text, letterSpacing: 1, textTransform: 'uppercase' }}>
            Safety first
          </span>
        </div>
        <div style={{ fontSize: 12.5, color: PACER.textMute, lineHeight: 1.55 }}>
          Phone verified · public meeting points · group chat per run · women-only runs supported.
        </div>
      </div>
    </div>
  );
}

function OnboardingScreen({ onDone, initialStep = 0, __initialPhoneSubStep = 'phone' }) {
  const [step, setStep] = React.useState(initialStep);
  const [phoneSubStep, setPhoneSubStep] = React.useState(__initialPhoneSubStep);
  const [signingIn, setSigningIn] = React.useState(false);
  const [pace, setPace] = React.useState(8); // tick index 0..14 (8 = 6:00)
  const [name, setName] = React.useState('Sam');
  const [gender, setGender] = React.useState('Prefer not to say');
  const [age, setAge] = React.useState('25–34');
  const [dist, setDist] = React.useState('5K');
  const [level, setLevel] = React.useState('Beginner');
  const [area, setArea] = React.useState('Bristol');

  const s = ONBOARD_STEPS[step];
  const isLast = step === ONBOARD_STEPS.length; // 6 -> welcome screen
  const TOTAL = ONBOARD_STEPS.length + 1;

  // Sign-in overlay
  if (signingIn) {
    return <SignInScreen onClose={() => setSigningIn(false)} onSignedIn={onDone}/>;
  }

  // Step 0 — Variation A: full-bleed video, centered "How it works" card, pinned CTA
  if (step === 0) {
    const howItWorks = [
      ['Browse runs near you — or create your own', 'pace'],
      ['Request to join, or approve who joins yours', 'check'],
      ['Meeting point + group chat unlocks',          'unlock'],
      ['Show up and run together',                   'run'],
    ];
    return (
      <div style={{
        position: 'relative', height: '100%', color: PACER.text, fontFamily: PACER.font,
        display: 'flex', flexDirection: 'column', overflow: 'hidden',
      }}>
        <video autoPlay muted loop playsInline style={{
          position: 'absolute', inset: 0, width: '100%', height: '100%',
          objectFit: 'cover', zIndex: 0,
        }} src="onboarding_vid.mp4"/>
        <div style={{
          position: 'absolute', inset: 0, zIndex: 1,
          background: 'linear-gradient(180deg, rgba(10,12,10,0.7) 0%, rgba(10,12,10,0.45) 25%, rgba(10,12,10,0.45) 55%, rgba(10,12,10,0.92) 100%)',
        }}/>

        {/* Logo */}
        <div style={{
          position: 'relative', zIndex: 2, flexShrink: 0,
          padding: `${SCREEN_TOP}px ${SCREEN_SIDE}px 0`,
          display: 'flex', alignItems: 'center', justifyContent: 'space-between',
        }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 9 }}>
            <div style={{
              width: 32, height: 32, borderRadius: 9, background: PACER.lime,
              display: 'flex', alignItems: 'center', justifyContent: 'center',
            }}>
              <Icon name="run" size={18} color={PACER.limeInk}/>
            </div>
            <div style={{ fontSize: 20, fontWeight: 700, letterSpacing: -0.5, textShadow: '0 2px 8px rgba(0,0,0,0.6)' }}>Run Crew</div>
          </div>
          <div style={{
            background: 'rgba(212,255,58,0.15)',
            border: '1px solid rgba(212,255,58,0.4)',
            borderRadius: 99,
            padding: '4px 10px',
            backdropFilter: 'blur(12px)', WebkitBackdropFilter: 'blur(12px)',
          }}>
            <div style={{ fontSize: 10, fontWeight: 700, color: PACER.lime, letterSpacing: 1.2, textTransform: 'uppercase' }}>
              Design Preview
            </div>
          </div>
        </div>

        {/* Prototype notice banner */}
        <div style={{
          position: 'relative', zIndex: 2, margin: `10px ${SCREEN_SIDE}px 0`,
          background: 'rgba(0,0,0,0.55)',
          border: '1px solid rgba(255,255,255,0.12)',
          borderRadius: 12, padding: '10px 14px',
          backdropFilter: 'blur(20px)', WebkitBackdropFilter: 'blur(20px)',
          display: 'flex', alignItems: 'center', gap: 10,
        }}>
          <div style={{
            width: 8, height: 8, borderRadius: '50%', flexShrink: 0,
            background: PACER.lime, boxShadow: '0 0 8px rgba(212,255,58,0.8)',
          }}/>
          <div style={{ fontSize: 12.5, color: 'rgba(255,255,255,0.75)', lineHeight: 1.4 }}>
            <span style={{ color: PACER.text, fontWeight: 600 }}>Interactive design prototype</span>
            {' '}&mdash; not the real app. Tap around to explore the concept.
          </div>
        </div>

        {/* Centered content — headline + how it works */}
        <div style={{
          flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center',
          padding: `0 ${SCREEN_SIDE}px`, position: 'relative', zIndex: 2,
        }}>
          <div style={{
            fontSize: 34, fontWeight: 600, letterSpacing: -1.3, lineHeight: 1.05,
            whiteSpace: 'pre-line', marginBottom: 8,
            textShadow: '0 2px 12px rgba(0,0,0,0.5)',
          }}>{'Small local runs,\nmatched by pace.'}</div>
          <div style={{ fontSize: 14, color: PACER.textDim, lineHeight: 1.6, marginBottom: 24 }}>
            Run clubs are loud and full of strangers. Running alone gets old. Run Crew finds the two or three people near you running your pace — and brings you all together, one run at a time.
          </div>
          <div style={{
            background: 'rgba(10,12,10,0.45)', border: `1px solid rgba(255,255,255,0.09)`,
            borderRadius: 18, padding: '16px 16px',
            backdropFilter: 'blur(24px)', WebkitBackdropFilter: 'blur(24px)',
          }}>
            <div style={{
              fontSize: 10, fontWeight: 600, color: PACER.lime,
              letterSpacing: 1.4, textTransform: 'uppercase', marginBottom: 12,
            }}>How it works</div>
            <div style={{ display: 'grid', gap: 11 }}>
              {howItWorks.map(([label, icon], i) => (
                <div key={i} style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                  <div style={{
                    width: 28, height: 28, borderRadius: 8, flexShrink: 0,
                    background: 'rgba(212,255,58,0.07)', border: `1px solid ${PACER.limeBorder}`,
                    display: 'flex', alignItems: 'center', justifyContent: 'center',
                  }}>
                    <Icon name={icon} size={14} color={PACER.lime}/>
                  </div>
                  <span style={{ fontSize: 13.5, color: PACER.text, fontWeight: 500 }}>{label}</span>
                </div>
              ))}
            </div>
          </div>
        </div>

        {/* Pinned bottom CTA */}
        <div style={{
          position: 'relative', zIndex: 2, flexShrink: 0,
          padding: `0 ${SCREEN_SIDE}px ${16 + SCREEN_BOTTOM}px`,
        }}>
          <Btn variant="primary" size="lg" style={{ width: '100%' }}
            onClick={() => setStep(step + 1)}
            trailing={<Icon name="arrow" size={18} color={PACER.limeInk}/>}>
            Get started
          </Btn>
          <div style={{ textAlign: 'center', fontSize: 12.5, color: PACER.textMute, marginTop: 14 }}>
            Have an account? <span onClick={() => setSigningIn(true)} style={{ color: PACER.lime, fontWeight: 600, cursor: 'pointer' }}>Sign in</span>
          </div>
        </div>
      </div>
    );
  }

  if (isLast) {
    // Final welcome step
    return (
      <div style={{
        background: PACER.bg, height: '100%', color: PACER.text, fontFamily: PACER.font,
        display: 'flex', flexDirection: 'column', overflow: 'hidden',
      }}>
        <div style={{ flex: 1, minHeight: 0, overflowY: 'auto', padding: `${SCREEN_TOP}px ${SCREEN_SIDE}px 0` }}>
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 32 }}>
            <button onClick={() => setStep(step - 1)} style={{
              width: 40, height: 40, borderRadius: 12,
              background: 'rgba(255,255,255,0.04)', border: `1px solid ${PACER.border}`,
              color: PACER.text, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
            }}>
              <Icon name="chevL" size={20} color={PACER.text}/>
            </button>
            <ProgressDots idx={TOTAL - 1} total={TOTAL}/>
            <div style={{ width: 40 }}/>
          </div>
          <div style={{
            fontSize: 30, fontWeight: 600, letterSpacing: -1.1, lineHeight: 1.1,
            marginBottom: 10,
          }}>You're set, {name || 'Sam'}.</div>
          <div style={{ fontSize: 14.5, color: PACER.textDim, marginBottom: 28 }}>
            Here's the rhythm.
          </div>
          <WelcomeStep/>
        </div>
        <div style={{ flexShrink: 0, padding: `16px ${SCREEN_SIDE}px ${16 + SCREEN_BOTTOM}px`, background: `linear-gradient(to bottom, rgba(10,12,10,0), ${PACER.bg} 30%)` }}>
          <Btn variant="primary" size="lg" style={{ width: '100%' }} onClick={onDone}
            trailing={<Icon name="arrow" size={18} color={PACER.limeInk}/>}>
            See runs near you
          </Btn>
        </div>
      </div>
    );
  }

  return (
    <div style={{
      background: PACER.bg, height: '100%', color: PACER.text, fontFamily: PACER.font,
      display: 'flex', flexDirection: 'column', overflow: 'hidden',
    }}>
      <div style={{ flex: 1, minHeight: 0, overflowY: 'auto', padding: `${SCREEN_TOP}px ${SCREEN_SIDE}px 0` }}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 28 }}>
          <button onClick={() => {
            if (s.id === 'phone' && phoneSubStep === 'otp') { setPhoneSubStep('phone'); return; }
            setStep(Math.max(0, step - 1));
          }} style={{
            width: 40, height: 40, borderRadius: 12,
            background: 'rgba(255,255,255,0.04)', border: `1px solid ${PACER.border}`,
            color: PACER.text, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
          }}>
            <Icon name="chevL" size={20} color={PACER.text}/>
          </button>
          <ProgressDots idx={step - 1} total={TOTAL}/>
          <div style={{ width: 40 }}/>
        </div>
        <div style={{
          fontSize: 28, fontWeight: 600, letterSpacing: -1, lineHeight: 1.1,
          marginBottom: 8, whiteSpace: 'pre-line',
        }}>{s.id === 'phone' && phoneSubStep === 'otp' ? 'Enter the code.' : s.title}</div>
        <div style={{ fontSize: 14, color: PACER.textDim, lineHeight: 1.5, marginBottom: 28 }}>
          {s.id === 'phone' && phoneSubStep === 'otp'
            ? <span>We sent a 6-digit code to <span style={{ color: PACER.text, fontFamily: PACER.mono }}>+44 7700 900 184</span>.</span>
            : s.sub}
        </div>
        {s.id === 'phone' && phoneSubStep === 'phone' && <PhoneStep/>}
        {s.id === 'phone' && phoneSubStep === 'otp'   && <OtpStep/>}
        {s.id === 'profile' && <ProfileStep name={name} setName={setName} gender={gender} setGender={setGender} age={age} setAge={setAge} area={area} setArea={setArea}/>}
        {s.id === 'pace' && <PaceStep pace={pace} setPace={setPace} onSkip={() => setStep(step + 1)}/>}
        {s.id === 'distance' && <DistanceStep dist={dist} setDist={setDist}/>}
        {s.id === 'area' && <AreaStep area={area} setArea={setArea}/>}
      </div>
      <div style={{ flexShrink: 0, padding: `16px ${SCREEN_SIDE}px ${16 + SCREEN_BOTTOM}px`, background: `linear-gradient(to bottom, rgba(10,12,10,0), ${PACER.bg} 30%)` }}>
        <Btn variant="primary" size="lg" style={{ width: '100%' }}
          onClick={() => {
            if (s.id === 'phone' && phoneSubStep === 'phone') { setPhoneSubStep('otp'); return; }
            if (s.id === 'phone' && phoneSubStep === 'otp')   { setPhoneSubStep('phone'); setStep(step + 1); return; }
            setStep(step + 1);
          }}
          trailing={<Icon name="arrow" size={18} color={PACER.limeInk}/>}>
          {s.id === 'phone' && phoneSubStep === 'phone' ? 'Send code' : s.id === 'phone' ? 'Verify' : 'Continue'}
        </Btn>
      </div>
    </div>
  );
}

Object.assign(window, { OnboardingScreen });

// ─────────────────────────────────────────────────────────────
// Pace step · slider variant (3 buckets + 15s-step slider)
// Static screen used only on the canvas as a side-by-side option.
// ─────────────────────────────────────────────────────────────
function OnboardingPaceSliderScreen() {
  // Ticks 8:00 → 4:30 at 15s increments (15 stops). Slow on the left.
  const TICKS = [
    '8:00','7:45','7:30','7:15','7:00',
    '6:45','6:30','6:15','6:00','5:45',
    '5:30','5:15','5:00','4:45','4:30',
  ];
  const MAJOR = [0, 2, 4, 6, 8, 10, 12, 14];
  const LABEL = new Set([0, 4, 8, 12, 14]); // 8:00, 7:00, 6:00, 5:00, 4:30
  const [idx, setIdx] = React.useState(8); // 6:00 default
  const [done, setDone] = React.useState(false);
  if (done) return <div style={{ background: PACER.bg, height: '100%', display: 'flex', alignItems: 'center', justifyContent: 'center', color: PACER.textDim, fontFamily: PACER.font, fontSize: 14 }}>Next step…</div>;

  const pace = TICKS[idx];
  const pct = (idx / (TICKS.length - 1)) * 100;

  return (
    <div style={{
      background: PACER.bg, minHeight: '100%', color: PACER.text, fontFamily: PACER.font,
      display: 'flex', flexDirection: 'column', position: 'relative',
    }}>
      <div style={{ padding: `${SCREEN_TOP}px ${SCREEN_SIDE}px 0`, flex: 1 }}>
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginBottom: 28 }}>
          <button style={{
            width: 40, height: 40, borderRadius: 12,
            background: 'rgba(255,255,255,0.04)', border: `1px solid ${PACER.border}`,
            color: PACER.text, cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center',
          }}>
            <Icon name="chevL" size={20} color={PACER.text}/>
          </button>
          <ProgressDots idx={3} total={7}/>
          <div style={{ width: 40 }}/>
        </div>
        <div style={{
          fontSize: 28, fontWeight: 600, letterSpacing: -1, lineHeight: 1.1,
          marginBottom: 8, whiteSpace: 'pre-line',
        }}>Your usual pace</div>
        <div style={{ fontSize: 14, color: PACER.textDim, lineHeight: 1.5, marginBottom: 40 }}>
          We'll match you to runs in your range so nobody gets dropped.
        </div>

        {/* Big pace readout */}
        <div style={{ textAlign: 'center', marginBottom: 28 }}>
          <div style={{
            fontFamily: PACER.mono, fontSize: 84, fontWeight: 500,
            color: PACER.lime, letterSpacing: -3, lineHeight: 1,
          }}>{pace}</div>
          <div style={{
            fontFamily: PACER.mono, fontSize: 13, color: PACER.textMute, marginTop: 10, letterSpacing: 1.4,
          }}>MIN / KM</div>
          <div style={{
            fontFamily: PACER.font, fontSize: 12.5, color: PACER.textDim, marginTop: 10,
          }}>
            5K in ~{Math.round((parseInt(pace.split(':')[0]) * 60 + parseInt(pace.split(':')[1])) * 5 / 60)} min
          </div>
        </div>

        {/* Slider */}
        <div style={{
          background: PACER.card, border: `1px solid ${PACER.border}`,
          borderRadius: 18, padding: '24px 22px 20px',
        }}>
          <div style={{ position: 'relative', height: 36 }}>
            <div style={{
              position: 'absolute', top: 14, left: 0, right: 0, height: 12,
              display: 'flex', justifyContent: 'space-between', alignItems: 'center',
            }}>
              {TICKS.map((_, i) => (
                <div key={i} style={{
                  width: 1, height: MAJOR.includes(i) ? 10 : 6,
                  background: i <= idx ? PACER.lime : PACER.borderStrong,
                  opacity: i <= idx ? 1 : 0.6,
                }}/>
              ))}
            </div>
            <div style={{
              position: 'absolute', top: 17, left: 0, right: 0, height: 2,
              background: 'rgba(255,255,255,0.06)', borderRadius: 99,
            }}/>
            <div style={{
              position: 'absolute', top: 17, left: 0, width: `${pct}%`,
              height: 2, background: PACER.lime, borderRadius: 99,
            }}/>
            <div style={{
              position: 'absolute', top: 8, left: `${pct}%`, transform: 'translateX(-50%)',
              width: 22, height: 22, borderRadius: '50%',
              background: PACER.lime, border: `3px solid ${PACER.bg}`,
              boxShadow: `0 0 0 1px ${PACER.lime}, 0 4px 14px rgba(212,255,58,0.35)`,
              pointerEvents: 'none',
            }}/>
            <input type="range" min="0" max={TICKS.length - 1} value={idx}
              onChange={e => setIdx(parseInt(e.target.value))}
              style={{
                position: 'absolute', inset: 0, width: '100%', height: 36,
                opacity: 0, cursor: 'pointer', appearance: 'none',
              }}/>
          </div>
          <div style={{ position: 'relative', height: 14, marginTop: 8 }}>
            {[...LABEL].map(i => (
              <span key={i} style={{
                position: 'absolute', left: `${(i / (TICKS.length - 1)) * 100}%`,
                transform: i === 0 ? 'translateX(0)' : i === 14 ? 'translateX(-100%)' : 'translateX(-50%)',
                fontFamily: PACER.mono, fontSize: 10, color: PACER.textSubtle,
              }}>{TICKS[i]}</span>
            ))}
          </div>
        </div>

        {/* Skip link */}
        <button onClick={() => setDone(true)} style={{
          marginTop: 16, width: '100%',
          background: 'transparent', border: 'none',
          padding: '12px 14px',
          color: PACER.textDim, cursor: 'pointer',
          fontFamily: PACER.font, fontSize: 13.5, fontWeight: 500,
          textAlign: 'center', textDecoration: 'underline',
          textDecorationColor: PACER.borderStrong, textUnderlineOffset: 4,
        }}>
          I don't know my pace
        </button>
      </div>
      <div style={{ padding: `20px ${SCREEN_SIDE}px ${20 + SCREEN_BOTTOM}px` }}>
        <Btn variant="primary" size="lg" style={{ width: '100%' }} onClick={() => setDone(true)}
          trailing={<Icon name="arrow" size={18} color={PACER.limeInk}/>}>
          Continue
        </Btn>
      </div>
    </div>
  );
}

Object.assign(window, { OnboardingPaceSliderScreen });
