// screens-auth.jsx — premium onboarding/auth flow
// Replaces the basic OnboardingScreen exported from screens-teams.jsx.
// Loaded AFTER screens-teams.jsx so the global is overridden.
//
// Flow:
//   intro (3 slides) → welcome
//   welcome → login | register
//   login: 'login' (smart unified field) → 'login-pwd' → biometric → empty
//          forgot → forgot-otp → forgot-new
//   register: 'reg-method' → 'reg-id' → 'reg-pwd' → 'reg-otp' →
//             'reg-success' → 'reg-name' → 'reg-role' → empty
//   plus: google one-tap (skips OTP)

const { useState: useStateA, useEffect: useEffectA } = React;

function GoogleGlyphA({ size = 18 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" style={{ flexShrink: 0 }}>
      <path fill="#4285F4" d="M22.56 12.25c0-.78-.07-1.53-.2-2.25H12v4.26h5.92a5.06 5.06 0 0 1-2.2 3.32v2.76h3.56c2.08-1.92 3.28-4.74 3.28-8.09z"/>
      <path fill="#34A853" d="M12 23c2.97 0 5.46-.98 7.28-2.66l-3.56-2.76c-.99.66-2.25 1.06-3.72 1.06-2.86 0-5.29-1.93-6.16-4.53H2.18v2.84A11 11 0 0 0 12 23z"/>
      <path fill="#FBBC05" d="M5.84 14.11A6.6 6.6 0 0 1 5.5 12c0-.73.13-1.44.34-2.11V7.05H2.18A11 11 0 0 0 1 12c0 1.78.43 3.46 1.18 4.95l3.66-2.84z"/>
      <path fill="#EA4335" d="M12 5.38c1.62 0 3.06.56 4.21 1.64l3.15-3.15C17.45 2.09 14.97 1 12 1A11 11 0 0 0 2.18 7.05l3.66 2.84C6.71 7.31 9.14 5.38 12 5.38z"/>
    </svg>
  );
}

// Decorative blob background — subtle, premium ornament
function AuthBg() {
  return (
    <div style={{ position: 'absolute', inset: 0, overflow: 'hidden', pointerEvents: 'none', zIndex: 0 }}>
      <div style={{ position: 'absolute', top: -120, right: -100, width: 300, height: 300, borderRadius: '50%', background: 'radial-gradient(circle, var(--p100), transparent 70%)', opacity: .8 }}></div>
      <div style={{ position: 'absolute', top: 200, left: -80, width: 240, height: 240, borderRadius: '50%', background: 'radial-gradient(circle, var(--c50), transparent 70%)', opacity: .7 }}></div>
    </div>
  );
}

function OnboardingScreen({ nav }) {
  const [step, setStep] = useStateA('intro');
  const [introIdx, setIntroIdx] = useStateA(0);
  const [mode, setMode] = useStateA('register'); // 'register' | 'login' | 'forgot'
  const [identifier, setIdentifier] = useStateA('');
  const [idType, setIdType] = useStateA('email');
  const [password, setPassword] = useStateA('');
  const [showPwd, setShowPwd] = useStateA(false);
  const [name, setName] = useStateA('');
  const [otp, setOtp] = useStateA(['', '', '', '', '', '']);
  const [agreed, setAgreed] = useStateA(true);
  const [resend, setResend] = useStateA(24);
  const [loading, setLoading] = useStateA(false);
  const [pickedRole, setPickedRole] = useStateA(null);

  useEffectA(() => {
    if (step !== 'reg-otp' && step !== 'forgot-otp') return;
    setResend(24);
    const t = setInterval(() => setResend(r => (r > 0 ? r - 1 : 0)), 1000);
    return () => clearInterval(t);
  }, [step]);

  // Smart auto-detect: email if contains @ or letters, phone if all digits
  const isEmailLike = (s) => /[a-zA-Z@]/.test(s);
  const detected = isEmailLike(identifier) ? 'email' : 'phone';
  const idValid = detected === 'email'
    ? /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(identifier)
    : identifier.replace(/\D/g, '').length === 10;
  const pwdStrength = (() => {
    let s = 0;
    if (password.length >= 8) s++;
    if (/[A-Z]/.test(password)) s++;
    if (/[0-9]/.test(password)) s++;
    if (/[^A-Za-z0-9]/.test(password)) s++;
    return s;
  })();

  const fakeSubmit = (next, ms = 800) => {
    setLoading(true);
    setTimeout(() => { setLoading(false); setStep(next); }, ms);
  };

  // ─── Reusable bits ────────────────────────────────────────
  const BackChip = ({ to }) => (
    <button onClick={() => setStep(to)} style={{ width: 38, height: 38, borderRadius: 12, background: '#fff', display: 'grid', placeItems: 'center', border: '1px solid var(--n100)', alignSelf: 'flex-start', flexShrink: 0, boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}><Icon name="chev-l" size={18}/></button>
  );

  const stepNames = {
    'reg-method': 'Method',
    'reg-id': 'Identity',
    'reg-pwd': 'Password',
    'reg-otp': 'Verify',
    'reg-name': 'Profile',
    'reg-role': 'Role',
  };
  const stepIdx = { 'reg-method': 1, 'reg-id': 2, 'reg-pwd': 3, 'reg-otp': 4, 'reg-name': 5, 'reg-role': 6 };

  const Stepper = ({ current, total = 6 }) => (
    <div style={{ marginTop: 16 }}>
      <div className="row" style={{ gap: 4 }}>
        {Array.from({ length: total }).map((_, i) => (
          <div key={i} style={{ flex: 1, height: 3, borderRadius: 99, background: i < current ? 'var(--p600)' : 'var(--n100)', transition: 'background .2s' }}></div>
        ))}
      </div>
      <div className="spread" style={{ marginTop: 10 }}>
        <span style={{ fontSize: 10, color: 'var(--n500)', fontFamily: 'var(--mono)', fontWeight: 700, letterSpacing: '.08em' }}>STEP {current} OF {total}</span>
        <span style={{ fontSize: 10, color: 'var(--p700)', fontFamily: 'var(--mono)', fontWeight: 700, letterSpacing: '.08em' }}>{stepNames[step]?.toUpperCase()}</span>
      </div>
    </div>
  );

  // ─── INTRO CAROUSEL ─────────────────────────────────────────
  const slides = [
    {
      bg: 'linear-gradient(160deg,#EEF2FF 0%,#fff 60%)',
      eyebrow: 'ALL-IN-ONE',
      title: 'Your school life,\nin one app.',
      sub: 'Fees, attendance, transport, homework — track everything for every child you care for.',
      visual: (
        <svg width="220" height="180" viewBox="0 0 220 180" style={{ display: 'block' }}>
          <rect x="20" y="20" width="180" height="140" rx="20" fill="#fff" stroke="#E0E7FF" strokeWidth="2"/>
          <rect x="38" y="40" width="60" height="60" rx="14" fill="#EEF2FF"/>
          <rect x="110" y="40" width="60" height="28" rx="10" fill="#FFF1EE"/>
          <rect x="110" y="74" width="60" height="26" rx="10" fill="#F0FDF4"/>
          <rect x="38" y="112" width="132" height="34" rx="12" fill="#4F46E5"/>
          <text x="104" y="134" textAnchor="middle" fill="#fff" fontSize="11" fontWeight="700" fontFamily="system-ui">Pay fees</text>
          <text x="68" y="76" textAnchor="middle" fontSize="22" fontWeight="800" fill="#4F46E5">92%</text>
          <text x="68" y="92" textAnchor="middle" fontSize="8" fill="#6366F1" letterSpacing="1">PRESENT</text>
        </svg>
      ),
    },
    {
      bg: 'linear-gradient(160deg,#FFF1EE 0%,#fff 60%)',
      eyebrow: 'INSTANT PAYMENTS',
      title: 'UPI fees, no\nlate-night panics.',
      sub: 'Pay term fees in seconds. Get e-receipts and an annual statement — DPDPA-grade secure.',
      visual: (
        <svg width="220" height="180" viewBox="0 0 220 180">
          <rect x="30" y="30" width="160" height="120" rx="18" fill="#fff" stroke="#FED7AA" strokeWidth="2"/>
          <text x="50" y="62" fontSize="9" fontWeight="700" fill="#9A3412" letterSpacing="1">FEES DUE</text>
          <text x="50" y="92" fontSize="28" fontWeight="800" fill="#0F172A" letterSpacing="-1">₹18,500</text>
          <rect x="50" y="106" width="120" height="28" rx="10" fill="#10B981"/>
          <text x="110" y="124" textAnchor="middle" fill="#fff" fontSize="11" fontWeight="700">Pay with UPI</text>
          <circle cx="170" cy="55" r="14" fill="#10B981"/>
          <path d="M163 55l5 5 9-9" stroke="#fff" strokeWidth="2.4" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      ),
    },
    {
      bg: 'linear-gradient(160deg,#F0FDF4 0%,#fff 60%)',
      eyebrow: 'STAY CONNECTED',
      title: 'Live bus tracking\n& teacher chat.',
      sub: 'Know exactly when the bus is 8 minutes away. Message teachers without losing receipts in WhatsApp.',
      visual: (
        <svg width="220" height="180" viewBox="0 0 220 180">
          <rect x="20" y="20" width="180" height="140" rx="20" fill="#0F172A"/>
          <path d="M30 100 Q 70 60, 110 90 T 190 80" stroke="#10B981" strokeWidth="3" fill="none" strokeDasharray="4 4"/>
          <circle cx="40" cy="100" r="6" fill="#10B981"/>
          <circle cx="110" cy="90" r="8" fill="#FBBF24" stroke="#fff" strokeWidth="2"/>
          <rect x="155" y="72" width="32" height="22" rx="4" fill="#4F46E5"/>
          <rect x="160" y="76" width="6" height="6" fill="#fff"/>
          <rect x="170" y="76" width="6" height="6" fill="#fff"/>
          <text x="110" y="135" textAnchor="middle" fill="#fff" fontSize="13" fontWeight="700">Bus 8 min away</text>
          <text x="110" y="148" textAnchor="middle" fill="#94A3B8" fontSize="9">Route 14 · On time</text>
        </svg>
      ),
    },
  ];

  if (step === 'intro') {
    const s = slides[introIdx];
    return (
      <div className="app screen-in" style={{ background: s.bg, transition: 'background .3s' }}>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 24px', display: 'flex', flexDirection: 'column' }}>
          <div className="spread">
            <div style={{ width: 38, height: 38, borderRadius: 11, background: 'var(--p600)', display: 'grid', placeItems: 'center', color: '#fff', fontWeight: 800, fontSize: 17, letterSpacing: '-.02em' }}>S</div>
            <button onClick={() => setStep('welcome')} style={{ fontSize: 12, fontWeight: 600, color: 'var(--n500)', padding: '6px 10px' }}>Skip</button>
          </div>

          <div style={{ flex: 1, display: 'grid', placeItems: 'center', marginTop: 24 }}>
            {s.visual}
          </div>

          <div style={{ minHeight: 200 }}>
            <div style={{ fontSize: 10, fontWeight: 700, color: 'var(--p700)', fontFamily: 'var(--mono)', letterSpacing: '.12em' }}>{s.eyebrow}</div>
            <div style={{ marginTop: 10, fontSize: 30, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em', lineHeight: 1.1, whiteSpace: 'pre-line' }}>{s.title}</div>
            <div style={{ fontSize: 14, color: 'var(--n600)', marginTop: 12, lineHeight: 1.55 }}>{s.sub}</div>
          </div>

          {/* Pagination dots */}
          <div className="row" style={{ justifyContent: 'center', gap: 8, marginTop: 16, marginBottom: 16 }}>
            {slides.map((_, i) => (
              <div key={i} onClick={() => setIntroIdx(i)} style={{
                width: i === introIdx ? 22 : 6, height: 6, borderRadius: 99,
                background: i === introIdx ? 'var(--p600)' : 'var(--n200)',
                transition: 'all .2s', cursor: 'pointer',
              }}></div>
            ))}
          </div>

          <button onClick={() => introIdx < slides.length - 1 ? setIntroIdx(introIdx + 1) : setStep('welcome')} className="btn">
            {introIdx < slides.length - 1 ? 'Next' : 'Get started'} <Icon name="arrow-right" size={16}/>
          </button>
        </div>
      </div>
    );
  }

  // ─── WELCOME ────────────────────────────────────────────────
  if (step === 'welcome') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <div className="row" style={{ marginTop: 24, gap: 10 }}>
            <div style={{ width: 44, height: 44, borderRadius: 12, background: 'var(--p600)', display: 'grid', placeItems: 'center', color: '#fff', fontWeight: 800, fontSize: 20, letterSpacing: '-.02em', boxShadow: '0 6px 16px rgba(79,70,229,.32)' }}>S</div>
            <div style={{ fontSize: 12, color: 'var(--n500)', fontFamily: 'var(--mono)', fontWeight: 700, letterSpacing: '.1em' }}>SCHOLIQ</div>
          </div>

          <div style={{ marginTop: 32, fontSize: 32, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em', lineHeight: 1.05 }}>Sign in to keep<br/>school life simple.</div>
          <div style={{ fontSize: 14, color: 'var(--n600)', marginTop: 12, lineHeight: 1.55, maxWidth: 280 }}>One identity for parents, students, teachers, drivers — across every school you belong to.</div>

          {/* Trust strip */}
          <div className="row" style={{ marginTop: 22, gap: 10, padding: '10px 14px', background: 'rgba(255,255,255,.6)', backdropFilter: 'blur(6px)', borderRadius: 12, border: '1px solid var(--n100)' }}>
            <div style={{ display: 'flex' }}>
              {['var(--p600)', 'var(--c500)', 'var(--success)', 'var(--warning)'].map((c, i) => (
                <div key={i} style={{ width: 22, height: 22, borderRadius: 99, background: c, border: '2px solid #fff', marginLeft: i === 0 ? 0 : -7, fontSize: 9, color: '#fff', fontWeight: 800, display: 'grid', placeItems: 'center' }}>{['P', 'A', 'S', 'V'][i]}</div>
              ))}
            </div>
            <div style={{ flex: 1, fontSize: 11, color: 'var(--n700)', fontWeight: 600, lineHeight: 1.3 }}>Trusted by <b style={{ color: 'var(--n900)' }}>1,200+ schools</b> across India</div>
          </div>

          <div style={{ flex: 1 }}></div>

          {/* Primary — Google */}
          <button onClick={() => fakeSubmit('empty')} className="row" disabled={loading} style={{ width: '100%', height: 54, padding: '0 18px', background: '#fff', border: '1.5px solid var(--n200)', borderRadius: 14, fontWeight: 700, color: 'var(--n900)', fontSize: 14, justifyContent: 'center', gap: 12, boxShadow: '0 4px 12px rgba(15,23,42,.06)', opacity: loading ? .6 : 1 }}>
            <GoogleGlyphA size={20}/> Continue with Google
          </button>

          {/* Divider */}
          <div className="row" style={{ gap: 10, margin: '14px 0' }}>
            <div style={{ flex: 1, height: 1, background: 'var(--n200)' }}></div>
            <span style={{ fontSize: 10, fontWeight: 700, color: 'var(--n400)', letterSpacing: '.12em', fontFamily: 'var(--mono)' }}>OR</span>
            <div style={{ flex: 1, height: 1, background: 'var(--n200)' }}></div>
          </div>

          <button onClick={() => { setMode('register'); setStep('reg-method'); }} className="btn" style={{ marginBottom: 10 }}>Create free account <Icon name="arrow-right" size={16}/></button>
          <button onClick={() => { setMode('login'); setIdentifier(''); setStep('login'); }} className="btn btn-outline">I already have an account</button>

          <div style={{ fontSize: 10.5, color: 'var(--n500)', textAlign: 'center', marginTop: 18, lineHeight: 1.5 }}>By continuing you agree to Scholiq's <b style={{ color: 'var(--n800)' }}>Terms</b> and <b style={{ color: 'var(--n800)' }}>Privacy Policy</b>. <br/>🇮🇳 DPDPA-compliant · data stays in India.</div>
        </div>
      </div>
    );
  }

  // ─── LOGIN · smart unified field ────────────────────────────
  if (step === 'login') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="welcome"/>
          <div style={{ marginTop: 28, fontSize: 28, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>Welcome back 👋</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6 }}>Use the email or phone you signed up with</div>

          <div style={{ marginTop: 28 }}>
            <div style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.06em', textTransform: 'uppercase', marginBottom: 8, fontFamily: 'var(--mono)' }}>Email or phone</div>
            <div className="row" style={{ gap: 8, padding: '14px 16px', background: '#fff', borderRadius: 14, border: '1.5px solid var(--n200)', boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}>
              <Icon name={detected === 'email' ? 'mail' : 'phone'} size={18} style={{ color: identifier ? 'var(--p600)' : 'var(--n400)' }}/>
              <input value={identifier} onChange={e => setIdentifier(e.target.value)} placeholder="you@school.in or 98765 43210" style={{ flex: 1, border: 'none', outline: 'none', fontSize: 15, fontWeight: 600, fontFamily: 'inherit' }}/>
              {idValid && <Icon name="check" size={18} style={{ color: 'var(--success)' }}/>}
            </div>
            {identifier && !idValid && (
              <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 6, marginLeft: 4 }}>
                {detected === 'email' ? 'Looks like an email — finish typing it' : `${identifier.replace(/\D/g, '').length}/10 digits`}
              </div>
            )}
          </div>

          <div style={{ flex: 1, minHeight: 24 }}></div>

          <button disabled={!idValid || loading} onClick={() => fakeSubmit('login-pwd')} className="btn" style={{ opacity: idValid ? 1 : .5 }}>
            {loading ? 'Checking…' : <>Continue <Icon name="arrow-right" size={16}/></>}
          </button>

          <div className="row" style={{ gap: 10, margin: '16px 0' }}>
            <div style={{ flex: 1, height: 1, background: 'var(--n200)' }}></div>
            <span style={{ fontSize: 10, fontWeight: 700, color: 'var(--n400)', letterSpacing: '.12em', fontFamily: 'var(--mono)' }}>OR</span>
            <div style={{ flex: 1, height: 1, background: 'var(--n200)' }}></div>
          </div>
          <button onClick={() => fakeSubmit('empty')} className="row" style={{ width: '100%', height: 48, padding: '0 18px', background: '#fff', border: '1.5px solid var(--n200)', borderRadius: 12, fontWeight: 700, color: 'var(--n900)', fontSize: 13, justifyContent: 'center', gap: 12 }}>
            <GoogleGlyphA size={18}/> Continue with Google
          </button>

          <div style={{ fontSize: 12, color: 'var(--n500)', textAlign: 'center', marginTop: 18 }}>New to Scholiq? <button onClick={() => { setMode('register'); setStep('reg-method'); }} style={{ color: 'var(--p600)', fontWeight: 700 }}>Create an account</button></div>
        </div>
      </div>
    );
  }

  // ─── LOGIN · password ────────────────────────────────────────
  if (step === 'login-pwd') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="login"/>

          <div style={{ marginTop: 24, display: 'flex', alignItems: 'center', gap: 12 }}>
            <div className="av av-44" style={{ background: 'var(--p100)', color: 'var(--p700)' }}>{(identifier[0] || 'P').toUpperCase()}</div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 600 }}>Signing in as</div>
              <div style={{ fontSize: 14, fontWeight: 700, color: 'var(--n900)', overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>{detected === 'phone' ? `+91 ${identifier.replace(/(\d{5})(\d{5})/, '$1 $2')}` : identifier}</div>
            </div>
          </div>

          <div style={{ marginTop: 28, fontSize: 24, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.02em' }}>Enter your password</div>

          <div style={{ marginTop: 22 }}>
            <div className="spread" style={{ marginBottom: 8 }}>
              <span style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.06em', textTransform: 'uppercase', fontFamily: 'var(--mono)' }}>Password</span>
              <button onClick={() => setStep('forgot')} style={{ fontSize: 11, fontWeight: 700, color: 'var(--p600)' }}>Forgot password?</button>
            </div>
            <div className="row" style={{ gap: 8, padding: '14px 16px', background: '#fff', borderRadius: 14, border: '1.5px solid var(--n200)', boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}>
              <Icon name="lock" size={18} style={{ color: password ? 'var(--p600)' : 'var(--n400)' }}/>
              <input autoFocus type={showPwd ? 'text' : 'password'} value={password} onChange={e => setPassword(e.target.value)} placeholder="Enter your password" style={{ flex: 1, border: 'none', outline: 'none', fontSize: 15, fontWeight: 600, fontFamily: 'inherit' }}/>
              <button onClick={() => setShowPwd(!showPwd)} style={{ color: 'var(--n500)' }}><Icon name={showPwd ? 'eye-off' : 'eye'} size={18}/></button>
            </div>
          </div>

          <div style={{ flex: 1 }}></div>

          <button disabled={password.length < 6 || loading} onClick={() => fakeSubmit('biometric', 1000)} className="btn" style={{ opacity: password.length >= 6 ? 1 : .5 }}>
            {loading ? 'Signing in…' : <>Sign in <Icon name="arrow-right" size={16}/></>}
          </button>

          <button onClick={() => fakeSubmit('biometric', 800)} className="row" style={{ width: '100%', marginTop: 12, padding: '12px', background: 'transparent', borderRadius: 12, justifyContent: 'center', gap: 10, color: 'var(--n700)' }}>
            <Icon name="shield" size={16}/> <span style={{ fontSize: 13, fontWeight: 700 }}>Use biometrics instead</span>
          </button>
        </div>
      </div>
    );
  }

  // ─── BIOMETRIC opt-in (post-login) ──────────────────────────
  if (step === 'biometric') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)' }}>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column' }}>
          <div style={{ flex: 1 }}></div>
          <div style={{ textAlign: 'center' }}>
            <div style={{ width: 96, height: 96, borderRadius: 28, background: 'var(--p600)', color: '#fff', display: 'inline-grid', placeItems: 'center', marginBottom: 24, boxShadow: '0 12px 32px rgba(79,70,229,.32)', position: 'relative' }}>
              <svg width="48" height="48" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                <path d="M12 11v3a1 1 0 0 0 1 1"/>
                <path d="M14 4a8 8 0 0 1 4.94 7"/>
                <path d="M5.06 11A8 8 0 0 1 9 4"/>
                <path d="M14 18.5a4 4 0 0 1-3.5-2.5"/>
                <path d="M19.5 14a8 8 0 0 0-1-3"/>
                <path d="M2 14a8 8 0 0 0 .94 5"/>
                <path d="M9 14v.5a3 3 0 0 0 6 0"/>
              </svg>
            </div>
            <div style={{ fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.02em' }}>Sign in with Face ID</div>
            <div style={{ fontSize: 13, color: 'var(--n600)', marginTop: 10, lineHeight: 1.55, padding: '0 20px' }}>Skip the password next time. Your credentials stay locked on this device.</div>
          </div>
          <div style={{ flex: 1 }}></div>
          <button onClick={() => fakeSubmit('empty', 600)} className="btn">Enable Face ID <Icon name="check" size={16}/></button>
          <button onClick={() => setStep('empty')} className="btn-ghost" style={{ marginTop: 10, padding: 14, fontSize: 13, fontWeight: 600 }}>Skip for now</button>
        </div>
      </div>
    );
  }

  // ─── FORGOT password ─────────────────────────────────────────
  if (step === 'forgot') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)' }}>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column' }}>
          <BackChip to="login-pwd"/>
          <div style={{ marginTop: 28, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.02em' }}>Reset your password</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 8, lineHeight: 1.55 }}>We'll send a 6-digit code to <b style={{ color: 'var(--n800)' }}>{detected === 'phone' ? `+91 ${identifier.replace(/(\d{5})(\d{5})/, '$1 $2')}` : identifier}</b> so you can set a new one.</div>

          <div className="card-flat" style={{ marginTop: 24, padding: 14 }}>
            <div className="row" style={{ gap: 10 }}>
              <Icon name="info" size={16} style={{ color: 'var(--p600)', flexShrink: 0 }}/>
              <div style={{ fontSize: 11, color: 'var(--n600)', lineHeight: 1.5 }}>If you signed up with Google, use "Continue with Google" instead — your school account doesn't have a Scholiq password.</div>
            </div>
          </div>

          <div style={{ flex: 1 }}></div>
          <button onClick={() => fakeSubmit('forgot-otp')} className="btn">Send code <Icon name="arrow-right" size={16}/></button>
        </div>
      </div>
    );
  }

  // ─── REGISTER · choose method ───────────────────────────────
  if (step === 'reg-method') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="welcome"/>
          <div style={{ marginTop: 24, fontSize: 28, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>Let's set you up</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6 }}>Pick how you'd like to sign up — takes under a minute.</div>
          <Stepper current={1}/>

          <div style={{ marginTop: 24, display: 'flex', flexDirection: 'column', gap: 12 }}>
            <button onClick={() => fakeSubmit('reg-name')} className="row" style={{ padding: 16, background: '#fff', border: '2px solid var(--p600)', borderRadius: 14, gap: 14, textAlign: 'left', position: 'relative', boxShadow: '0 4px 12px rgba(79,70,229,.1)' }}>
              <div style={{ width: 44, height: 44, borderRadius: 12, background: 'var(--n50)', display: 'grid', placeItems: 'center', flexShrink: 0 }}><GoogleGlyphA size={22}/></div>
              <div style={{ flex: 1 }}>
                <div className="row" style={{ gap: 6 }}>
                  <span style={{ fontSize: 14, fontWeight: 700, color: 'var(--n900)' }}>Sign up with Google</span>
                  <span className="pill pill-p" style={{ fontSize: 9 }}>FASTEST</span>
                </div>
                <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 3 }}>Email pre-verified · 2 taps · no OTP needed</div>
              </div>
              <Icon name="arrow-right" size={16} style={{ color: 'var(--p600)' }}/>
            </button>

            <button onClick={() => { setIdType('email'); setIdentifier(''); setStep('reg-id'); }} className="row" style={{ padding: 16, background: '#fff', border: '1.5px solid var(--n200)', borderRadius: 14, gap: 14, textAlign: 'left' }}>
              <div style={{ width: 44, height: 44, borderRadius: 12, background: 'var(--p50)', color: 'var(--p600)', display: 'grid', placeItems: 'center', flexShrink: 0 }}><Icon name="mail" size={20}/></div>
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 14, fontWeight: 700, color: 'var(--n900)' }}>Sign up with email</div>
                <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 3 }}>Email + password · 6-digit OTP to verify</div>
              </div>
              <Icon name="chev-r" size={16} style={{ color: 'var(--n400)' }}/>
            </button>

            <button onClick={() => { setIdType('phone'); setIdentifier(''); setStep('reg-id'); }} className="row" style={{ padding: 16, background: '#fff', border: '1.5px solid var(--n200)', borderRadius: 14, gap: 14, textAlign: 'left' }}>
              <div style={{ width: 44, height: 44, borderRadius: 12, background: 'var(--c50)', color: 'var(--c600)', display: 'grid', placeItems: 'center', flexShrink: 0 }}><Icon name="phone" size={20}/></div>
              <div style={{ flex: 1 }}>
                <div className="row" style={{ gap: 6 }}>
                  <span style={{ fontSize: 14, fontWeight: 700, color: 'var(--n900)' }}>Sign up with mobile</span>
                  <span className="pill" style={{ background: 'var(--c50)', color: 'var(--c700)', border: '1px solid var(--c100)', fontSize: 9 }}>POPULAR</span>
                </div>
                <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 3 }}>Mobile + password · SMS OTP to verify</div>
              </div>
              <Icon name="chev-r" size={16} style={{ color: 'var(--n400)' }}/>
            </button>
          </div>

          <div style={{ flex: 1 }}></div>
          <div style={{ fontSize: 12, color: 'var(--n500)', textAlign: 'center' }}>Already have an account? <button onClick={() => { setMode('login'); setStep('login'); }} style={{ color: 'var(--p600)', fontWeight: 700 }}>Sign in</button></div>
        </div>
      </div>
    );
  }

  // ─── REGISTER · enter id ────────────────────────────────────
  if (step === 'reg-id') {
    const usingEmail = idType === 'email';
    const idValidNow = usingEmail
      ? /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(identifier)
      : identifier.replace(/\D/g, '').length === 10;
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="reg-method"/>
          <div style={{ marginTop: 24, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>What's your {usingEmail ? 'email' : 'mobile number'}?</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6 }}>We'll send a 6-digit code {usingEmail ? 'to your inbox' : 'by SMS'} to confirm it's you.</div>
          <Stepper current={2}/>

          <div style={{ marginTop: 28 }}>
            <div style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.06em', textTransform: 'uppercase', marginBottom: 8, fontFamily: 'var(--mono)' }}>{usingEmail ? 'Email address' : 'Mobile number'}</div>
            {usingEmail ? (
              <div className="row" style={{ gap: 8, padding: '14px 16px', background: '#fff', borderRadius: 14, border: `1.5px solid ${idValidNow ? 'var(--success)' : 'var(--n200)'}`, boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}>
                <Icon name="mail" size={18} style={{ color: identifier ? 'var(--p600)' : 'var(--n400)' }}/>
                <input autoFocus type="email" value={identifier} onChange={e => setIdentifier(e.target.value)} placeholder="you@school.in" style={{ flex: 1, border: 'none', outline: 'none', fontSize: 15, fontWeight: 600, fontFamily: 'inherit' }}/>
                {idValidNow && <Icon name="check" size={18} style={{ color: 'var(--success)' }}/>}
              </div>
            ) : (
              <div className="row" style={{ gap: 8, padding: '14px 16px', background: '#fff', borderRadius: 14, border: `1.5px solid ${idValidNow ? 'var(--success)' : 'var(--n200)'}`, boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}>
                <span style={{ fontSize: 15, fontWeight: 700, color: 'var(--n700)' }}>🇮🇳 +91</span>
                <input autoFocus inputMode="numeric" value={identifier} onChange={e => setIdentifier(e.target.value.replace(/\D/g, '').slice(0, 10))} placeholder="98765 43210" style={{ flex: 1, border: 'none', outline: 'none', fontSize: 15, fontWeight: 600, fontFamily: 'inherit', letterSpacing: '.05em' }}/>
                {idValidNow && <Icon name="check" size={18} style={{ color: 'var(--success)' }}/>}
              </div>
            )}
            <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 8, marginLeft: 4 }}>
              {usingEmail
                ? 'We\'ll never spam you. Used only for sign-in and important alerts.'
                : `${identifier.replace(/\D/g, '').length}/10 · standard SMS rates may apply`}
            </div>
          </div>

          <div style={{ flex: 1 }}></div>
          <button disabled={!idValidNow} onClick={() => setStep('reg-pwd')} className="btn" style={{ opacity: idValidNow ? 1 : .5 }}>Continue <Icon name="arrow-right" size={16}/></button>
          <button onClick={() => { setIdType(usingEmail ? 'phone' : 'email'); setIdentifier(''); }} className="btn-ghost" style={{ marginTop: 10, padding: 12, fontSize: 12, fontWeight: 600 }}>
            Use {usingEmail ? 'mobile number' : 'email'} instead
          </button>
        </div>
      </div>
    );
  }

  // ─── REGISTER · password ────────────────────────────────────
  if (step === 'reg-pwd') {
    const strengthLabel = ['Too weak', 'Weak', 'Okay', 'Strong', 'Very strong'][pwdStrength];
    const strengthColor = pwdStrength <= 1 ? 'var(--c500)' : pwdStrength === 2 ? 'var(--warning)' : pwdStrength === 3 ? 'var(--p500)' : 'var(--success)';
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="reg-id"/>
          <div style={{ marginTop: 24, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>Create a password</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6 }}>Pick something memorable — you'll use this with your {idType} to sign in.</div>
          <Stepper current={3}/>

          <div style={{ marginTop: 24 }}>
            <div className="spread" style={{ marginBottom: 8 }}>
              <span style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.06em', textTransform: 'uppercase', fontFamily: 'var(--mono)' }}>Password</span>
              {password && <span style={{ fontSize: 10, color: strengthColor, fontWeight: 700, letterSpacing: '.04em', fontFamily: 'var(--mono)' }}>{strengthLabel.toUpperCase()}</span>}
            </div>
            <div className="row" style={{ gap: 8, padding: '14px 16px', background: '#fff', borderRadius: 14, border: '1.5px solid var(--n200)', boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}>
              <Icon name="lock" size={18} style={{ color: password ? 'var(--p600)' : 'var(--n400)' }}/>
              <input autoFocus type={showPwd ? 'text' : 'password'} value={password} onChange={e => setPassword(e.target.value)} placeholder="At least 8 characters" style={{ flex: 1, border: 'none', outline: 'none', fontSize: 15, fontWeight: 600, fontFamily: 'inherit' }}/>
              <button onClick={() => setShowPwd(!showPwd)} style={{ color: 'var(--n500)' }}><Icon name={showPwd ? 'eye-off' : 'eye'} size={18}/></button>
            </div>

            {/* Strength meter */}
            <div className="row" style={{ gap: 4, marginTop: 12 }}>
              {[0, 1, 2, 3].map(i => (
                <div key={i} style={{
                  flex: 1, height: 4, borderRadius: 99,
                  background: pwdStrength > i ? strengthColor : 'var(--n100)',
                  transition: 'background .2s',
                }}></div>
              ))}
            </div>

            {/* Checklist as tighter chips */}
            <div style={{ marginTop: 14, display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
              {[
                { ok: password.length >= 8, lab: '8+ characters' },
                { ok: /[A-Z]/.test(password), lab: 'Uppercase letter' },
                { ok: /[0-9]/.test(password), lab: 'A number' },
                { ok: /[^A-Za-z0-9]/.test(password), lab: 'A symbol' },
              ].map((r, i) => (
                <div key={i} className="row" style={{ gap: 6, padding: '6px 10px', background: r.ok ? 'var(--success-50)' : 'var(--n50)', borderRadius: 8 }}>
                  <Icon name={r.ok ? 'check' : 'circle'} size={12} style={{ color: r.ok ? 'var(--success)' : 'var(--n400)' }}/>
                  <span style={{ fontSize: 11, fontWeight: 600, color: r.ok ? '#065F46' : 'var(--n600)' }}>{r.lab}</span>
                </div>
              ))}
            </div>
          </div>

          <div style={{ flex: 1 }}></div>
          <label className="row" style={{ gap: 10, marginBottom: 14, padding: '12px 14px', background: '#fff', borderRadius: 12, border: '1px solid var(--n100)', cursor: 'pointer' }}>
            <input type="checkbox" checked={agreed} onChange={e => setAgreed(e.target.checked)} style={{ width: 18, height: 18, accentColor: 'var(--p600)', flexShrink: 0 }}/>
            <span style={{ fontSize: 11, color: 'var(--n600)', lineHeight: 1.5, flex: 1 }}>I agree to Scholiq's <b style={{ color: 'var(--n800)' }}>Terms</b>, <b style={{ color: 'var(--n800)' }}>Privacy Policy</b>, and DPDPA-compliant data handling.</span>
          </label>
          <button disabled={pwdStrength < 2 || !agreed || loading} onClick={() => fakeSubmit('reg-otp', 700)} className="btn" style={{ opacity: pwdStrength >= 2 && agreed ? 1 : .5 }}>
            {loading ? 'Sending code…' : <>Send verification code <Icon name="arrow-right" size={16}/></>}
          </button>
        </div>
      </div>
    );
  }

  // ─── REGISTER · OTP gate ────────────────────────────────────
  if (step === 'reg-otp') {
    const allFilled = otp.every(d => d);
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="reg-pwd"/>
          <div style={{ marginTop: 24, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>Verify it's you</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6, lineHeight: 1.55 }}>
            Enter the 6-digit code we sent to{' '}
            <b style={{ color: 'var(--n800)' }}>{idType === 'phone' ? `+91 ${identifier.replace(/(\d{5})(\d{5})/, '$1 $2')}` : identifier}</b>
            {' · '}
            <button onClick={() => setStep('reg-id')} style={{ color: 'var(--p600)', fontWeight: 700 }}>Change</button>
          </div>
          <Stepper current={4}/>

          <div className="row" style={{ gap: 8, marginTop: 28, justifyContent: 'space-between' }}>
            {otp.map((d, i) => (
              <input
                key={i} value={d} maxLength={1} inputMode="numeric"
                onChange={e => {
                  const v = e.target.value.replace(/\D/g, '');
                  const newOtp = [...otp]; newOtp[i] = v; setOtp(newOtp);
                  if (v && i < 5) document.getElementById(`otp-${i + 1}`)?.focus();
                }}
                onPaste={e => {
                  const v = (e.clipboardData?.getData('text') || '').replace(/\D/g, '').slice(0, 6).split('');
                  if (v.length === 6) { e.preventDefault(); setOtp([...v]); document.getElementById('otp-5')?.focus(); }
                }}
                onKeyDown={e => {
                  if (e.key === 'Backspace' && !d && i > 0) document.getElementById(`otp-${i - 1}`)?.focus();
                }}
                id={`otp-${i}`} autoFocus={i === 0}
                style={{
                  flex: 1, height: 60, fontSize: 26, fontWeight: 800, textAlign: 'center', fontFamily: 'var(--mono)',
                  background: '#fff', border: `2px solid ${d ? 'var(--p500)' : 'var(--n200)'}`,
                  borderRadius: 12, color: 'var(--n900)', boxShadow: d ? '0 0 0 4px var(--p100)' : 'none',
                  transition: 'all .2s',
                }}
              />
            ))}
          </div>

          {/* Auto-detect SMS hint */}
          {idType === 'phone' && (
            <div className="row" style={{ marginTop: 14, gap: 8, padding: '8px 12px', background: 'var(--info-50)', borderRadius: 10 }}>
              <Icon name="message" size={14} style={{ color: '#1E40AF' }}/>
              <span style={{ fontSize: 11, color: '#1E40AF', fontWeight: 600 }}>We'll auto-fill the code when SMS arrives</span>
            </div>
          )}

          <div className="card-flat" style={{ marginTop: 14, padding: 12 }}>
            <div className="row" style={{ gap: 10 }}>
              <Icon name="shield" size={16} style={{ color: 'var(--success)', flexShrink: 0 }}/>
              <div style={{ fontSize: 11, color: 'var(--n600)', lineHeight: 1.5, flex: 1 }}>One-time only. We won't ask again unless you sign in from a new device.</div>
            </div>
          </div>

          <div style={{ fontSize: 12, color: 'var(--n500)', marginTop: 16, textAlign: 'center', lineHeight: 1.6 }}>
            {resend > 0
              ? <>Didn't get it? <b style={{ color: 'var(--n400)' }}>Resend in 0:{String(resend).padStart(2, '0')}</b></>
              : <button onClick={() => setResend(24)} style={{ color: 'var(--p600)', fontWeight: 700 }}>Resend code</button>}
            {idType === 'phone' && resend === 0 && <> · <button style={{ color: 'var(--p600)', fontWeight: 700 }}>Get a call instead</button></>}
          </div>

          <div style={{ flex: 1 }}></div>
          <button disabled={!allFilled || loading} onClick={() => fakeSubmit('reg-success', 1000)} className="btn" style={{ opacity: allFilled ? 1 : .5 }}>
            {loading ? 'Verifying…' : <>Verify code <Icon name="check" size={16}/></>}
          </button>
        </div>
      </div>
    );
  }

  // ─── REGISTER · success state ────────────────────────────────
  if (step === 'reg-success') {
    setTimeout(() => step === 'reg-success' && setStep('reg-name'), 1600);
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,var(--success-50) 0%,#fff 60%)' }}>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
          <div style={{ width: 120, height: 120, borderRadius: '50%', background: 'var(--success)', display: 'grid', placeItems: 'center', boxShadow: '0 16px 40px rgba(16,185,129,.32)', position: 'relative' }}>
            <svg width="64" height="64" viewBox="0 0 24 24" fill="none" stroke="#fff" strokeWidth="3" strokeLinecap="round" strokeLinejoin="round">
              <polyline points="20 6 9 17 4 12"/>
            </svg>
            {/* Pulse ring */}
            <div style={{ position: 'absolute', inset: -20, borderRadius: '50%', border: '4px solid var(--success)', opacity: .2, animation: 'pulse 1.4s ease-out infinite' }}></div>
          </div>
          <div style={{ marginTop: 32, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.02em', textAlign: 'center' }}>Verified ✨</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 8, textAlign: 'center', lineHeight: 1.55 }}>Account created. Just a few more details.</div>
          <style>{`@keyframes pulse{0%{transform:scale(1);opacity:.4}100%{transform:scale(1.4);opacity:0}}`}</style>
        </div>
      </div>
    );
  }

  // ─── REGISTER · name + photo ────────────────────────────────
  if (step === 'reg-name') {
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="reg-otp"/>
          <div style={{ marginTop: 24, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>What should we call you?</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6 }}>Schools use this name to find you on invites and rosters.</div>
          <Stepper current={5}/>

          {/* Avatar picker */}
          <div style={{ marginTop: 28, textAlign: 'center' }}>
            <button style={{ position: 'relative', display: 'inline-block' }}>
              <div className="av av-56" style={{ width: 88, height: 88, fontSize: 32, background: 'var(--p100)', color: 'var(--p700)', border: '3px solid #fff', boxShadow: '0 6px 20px rgba(15,23,42,.1)' }}>{(name[0] || '?').toUpperCase()}</div>
              <div style={{ position: 'absolute', bottom: 0, right: 0, width: 30, height: 30, borderRadius: 99, background: 'var(--p600)', color: '#fff', display: 'grid', placeItems: 'center', border: '3px solid #fff' }}><Icon name="camera" size={14}/></div>
            </button>
            <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 10, fontWeight: 600 }}>Tap to add a photo (optional)</div>
          </div>

          <div style={{ marginTop: 24 }}>
            <div style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.06em', textTransform: 'uppercase', marginBottom: 8, fontFamily: 'var(--mono)' }}>Full name</div>
            <div className="row" style={{ gap: 8, padding: '14px 16px', background: '#fff', borderRadius: 14, border: '1.5px solid var(--n200)', boxShadow: '0 1px 2px rgba(0,0,0,.04)' }}>
              <Icon name="user" size={18} style={{ color: name ? 'var(--p600)' : 'var(--n400)' }}/>
              <input autoFocus value={name} onChange={e => setName(e.target.value)} placeholder="e.g. Priya Reddy" style={{ flex: 1, border: 'none', outline: 'none', fontSize: 15, fontWeight: 600, fontFamily: 'inherit' }}/>
            </div>
          </div>

          <div className="card-flat" style={{ marginTop: 14, padding: 12 }}>
            <div className="spread">
              <div style={{ fontSize: 10, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.08em', fontFamily: 'var(--mono)' }}>VERIFIED</div>
              <Icon name="check" size={14} style={{ color: 'var(--success)' }}/>
            </div>
            <div style={{ fontSize: 12, color: 'var(--n800)', fontWeight: 600, marginTop: 4 }}>{idType === 'phone' ? `+91 ${identifier.replace(/(\d{5})(\d{5})/, '$1 $2')}` : identifier}</div>
          </div>

          <div style={{ flex: 1 }}></div>
          <button disabled={name.trim().length < 2} onClick={() => setStep('reg-role')} className="btn" style={{ opacity: name.trim().length >= 2 ? 1 : .5 }}>Continue <Icon name="arrow-right" size={16}/></button>
        </div>
      </div>
    );
  }

  // ─── REGISTER · pick role hint ──────────────────────────────
  if (step === 'reg-role') {
    const roles = [
      { id: 'parent', emoji: '👨‍👩‍👧', lab: "I'm a parent", sub: 'Track my children at school' },
      { id: 'student', emoji: '🎓', lab: "I'm a student", sub: 'See my homework, attendance, results' },
      { id: 'teacher', emoji: '👩‍🏫', lab: "I'm a teacher", sub: 'Manage classes & grade work' },
      { id: 'driver', emoji: '🚌', lab: "I'm a driver", sub: 'Run my school bus route' },
    ];
    return (
      <div className="app screen-in" style={{ background: 'linear-gradient(180deg,#fff 0%,var(--p50) 100%)', position: 'relative' }}>
        <AuthBg/>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 24px 28px', display: 'flex', flexDirection: 'column', position: 'relative', zIndex: 1 }}>
          <BackChip to="reg-name"/>
          <div style={{ marginTop: 24, fontSize: 26, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.025em' }}>How will you use Scholiq?</div>
          <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6 }}>Helps us tailor your home screen. You can have multiple roles later.</div>
          <Stepper current={6}/>

          <div style={{ marginTop: 24, display: 'flex', flexDirection: 'column', gap: 10 }}>
            {roles.map(r => (
              <button key={r.id} onClick={() => setPickedRole(r.id)} className="row" style={{
                padding: 14, borderRadius: 14, gap: 14, textAlign: 'left',
                background: pickedRole === r.id ? 'var(--p50)' : '#fff',
                border: pickedRole === r.id ? '2px solid var(--p600)' : '1.5px solid var(--n200)',
              }}>
                <div style={{ fontSize: 28, width: 44, height: 44, display: 'grid', placeItems: 'center', background: pickedRole === r.id ? '#fff' : 'var(--n50)', borderRadius: 12, flexShrink: 0 }}>{r.emoji}</div>
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 14, fontWeight: 700, color: 'var(--n900)' }}>{r.lab}</div>
                  <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 2 }}>{r.sub}</div>
                </div>
                <div style={{ width: 22, height: 22, borderRadius: 99, border: pickedRole === r.id ? 'none' : '2px solid var(--n200)', background: pickedRole === r.id ? 'var(--p600)' : 'transparent', display: 'grid', placeItems: 'center' }}>
                  {pickedRole === r.id && <Icon name="check" size={12} style={{ color: '#fff' }}/>}
                </div>
              </button>
            ))}
          </div>

          <div style={{ flex: 1 }}></div>
          <button disabled={!pickedRole} onClick={() => fakeSubmit('empty', 800)} className="btn" style={{ opacity: pickedRole ? 1 : .5 }}>
            {loading ? 'Setting up…' : <>Finish setup <Icon name="check" size={16}/></>}
          </button>
        </div>
      </div>
    );
  }

  // ─── EMPTY · invitations ────────────────────────────────────
  if (step === 'empty') {
    return (
      <div className="app screen-in" style={{ background: '#fff' }}>
        <StatusBar/>
        <div className="app-body no-pad" style={{ padding: '12px 20px 28px', display: 'flex', flexDirection: 'column' }}>
          {/* Header with greeting */}
          <div className="row" style={{ marginTop: 24, gap: 12 }}>
            <div className="av av-44" style={{ background: 'var(--p100)', color: 'var(--p700)' }}>{(name[0] || identifier[0] || 'U').toUpperCase()}</div>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 11, color: 'var(--n500)', fontWeight: 600 }}>Welcome to Scholiq</div>
              <div style={{ fontSize: 16, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.01em' }}>{name || 'Hello there'} 👋</div>
            </div>
          </div>

          <div style={{ marginTop: 24 }}>
            <div style={{ fontSize: 22, fontWeight: 800, color: 'var(--n900)', letterSpacing: '-.02em' }}>Add your first team</div>
            <div style={{ fontSize: 13, color: 'var(--n500)', marginTop: 6, lineHeight: 1.55 }}>Schools, tuition centres, and transport operators will invite you. Accept below or use a code.</div>
          </div>

          {/* Pending invitations */}
          <div style={{ marginTop: 20 }}>
            <div className="spread" style={{ marginBottom: 10 }}>
              <span style={{ fontSize: 10, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.08em', fontFamily: 'var(--mono)' }}>PENDING INVITATIONS · 2</span>
              <span style={{ fontSize: 10, color: 'var(--n400)', fontWeight: 600 }}>Last 7 days</span>
            </div>

            <div className="card" style={{ padding: 14, marginBottom: 10 }}>
              <div className="row" style={{ gap: 12 }}>
                <div style={{ width: 44, height: 44, borderRadius: 12, background: 'var(--p50)', color: 'var(--p600)', display: 'grid', placeItems: 'center', flexShrink: 0, fontSize: 22 }}>🏫</div>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ fontSize: 13, fontWeight: 700, color: 'var(--n900)' }}>Delhi Public School, Hyderabad</div>
                  <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 2 }}>Invited as <b style={{ color: 'var(--n800)' }}>Parent</b> · for Aarav Reddy, Class 8-B</div>
                  <div style={{ fontSize: 10, color: 'var(--n400)', marginTop: 2 }}>From <b>Mrs. Reddy (Class teacher)</b> · 2 days ago</div>
                </div>
                <span className="pill" style={{ background: 'var(--success-50)', color: '#065F46', border: '1px solid #A7F3D0', fontSize: 9 }}>NEW</span>
              </div>
              <div className="row" style={{ gap: 8, marginTop: 12 }}>
                <button className="btn-sm" style={{ flex: 1, height: 36, background: 'var(--p600)', color: '#fff', fontSize: 12, fontWeight: 700, borderRadius: 9 }}>Accept</button>
                <button className="btn-sm btn-ghost" style={{ height: 36, padding: '0 14px', fontSize: 12 }}>Decline</button>
              </div>
            </div>

            <div className="card" style={{ padding: 14 }}>
              <div className="row" style={{ gap: 12 }}>
                <div style={{ width: 44, height: 44, borderRadius: 12, background: 'var(--c50)', color: 'var(--c600)', display: 'grid', placeItems: 'center', flexShrink: 0, fontSize: 22 }}>📚</div>
                <div style={{ flex: 1, minWidth: 0 }}>
                  <div style={{ fontSize: 13, fontWeight: 700, color: 'var(--n900)' }}>Aakash Tuitions</div>
                  <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 2 }}>Invited as <b style={{ color: 'var(--n800)' }}>Parent</b> · for Aarav · Maths batch</div>
                  <div style={{ fontSize: 10, color: 'var(--n400)', marginTop: 2 }}>5 days ago</div>
                </div>
              </div>
              <div className="row" style={{ gap: 8, marginTop: 12 }}>
                <button className="btn-sm" style={{ flex: 1, height: 36, background: 'var(--n900)', color: '#fff', fontSize: 12, fontWeight: 700, borderRadius: 9 }}>Accept</button>
                <button className="btn-sm btn-ghost" style={{ height: 36, padding: '0 14px', fontSize: 12 }}>Decline</button>
              </div>
            </div>
          </div>

          {/* No invitation? */}
          <div style={{ marginTop: 24 }}>
            <div style={{ fontSize: 10, color: 'var(--n500)', fontWeight: 700, letterSpacing: '.08em', fontFamily: 'var(--mono)', marginBottom: 10 }}>NO INVITATION YET?</div>
            <div className="card" style={{ padding: 0, overflow: 'hidden' }}>
              <button className="row" style={{ width: '100%', padding: '14px', gap: 12, textAlign: 'left', borderBottom: '1px solid var(--n100)' }}>
                <div style={{ width: 36, height: 36, borderRadius: 10, background: 'var(--p50)', color: 'var(--p600)', display: 'grid', placeItems: 'center' }}><Icon name="key" size={16}/></div>
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 13, fontWeight: 700, color: 'var(--n900)' }}>Enter invitation code</div>
                  <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 1 }}>6-digit code from your school</div>
                </div>
                <Icon name="chev-r" size={16} style={{ color: 'var(--n400)' }}/>
              </button>
              <button className="row" style={{ width: '100%', padding: '14px', gap: 12, textAlign: 'left' }}>
                <div style={{ width: 36, height: 36, borderRadius: 10, background: 'var(--c50)', color: 'var(--c600)', display: 'grid', placeItems: 'center' }}><Icon name="qr" size={16}/></div>
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 13, fontWeight: 700, color: 'var(--n900)' }}>Scan QR from your school</div>
                  <div style={{ fontSize: 11, color: 'var(--n500)', marginTop: 1 }}>Posted on noticeboard or shared on chat</div>
                </div>
                <Icon name="chev-r" size={16} style={{ color: 'var(--n400)' }}/>
              </button>
            </div>
          </div>

          <div style={{ flex: 1, minHeight: 16 }}></div>
          <div style={{ textAlign: 'center', fontSize: 11, color: 'var(--n500)', lineHeight: 1.6, marginTop: 16 }}>
            Want Scholiq for your school?<br/>
            <button style={{ color: 'var(--p600)', fontWeight: 700 }}>Talk to onboarding team →</button>
          </div>
        </div>
      </div>
    );
  }

  return null;
}

window.OnboardingScreen = OnboardingScreen;
