// screens.jsx — wallet onboarding screens for Paisa wallet

const { useState, useEffect, useRef } = React;

// ─── 1. Welcome / start screen ─────────────────────────────
function WelcomeScreen({ onStart, tier, returningUser }) {
  return (
    <div style={{
      padding: '92px 28px 32px', height: '100%',
      display: 'flex', flexDirection: 'column',
      background: C.bg,
    }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        <PaisaMark size={32} />
        <span style={{ fontSize: 22, fontWeight: 700, letterSpacing: -0.5 }}>paisa</span>
      </div>

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
        <div style={{ fontSize: 13, fontWeight: 600, color: C.accentInk,
                      letterSpacing: 1.2, textTransform: 'uppercase', marginBottom: 12 }}>
          Wallet · Nepal
        </div>
        <h1 style={{
          fontSize: 38, lineHeight: 1.05, fontWeight: 700, margin: 0,
          letterSpacing: -1.5,
        }}>
          Money you<br/>can move<br/>in <span style={{ fontStyle: 'italic',
            fontFamily: 'Tiro Devanagari Hindi, serif', fontWeight: 400 }}>seconds</span>.
        </h1>
        <p style={{ marginTop: 18, fontSize: 16, lineHeight: 1.45, color: C.ink2, maxWidth: 320 }}>
          Sign up with your National ID. No paperwork, no branch visits — verified by the
          Government of Nepal.
        </p>

        <div style={{ marginTop: 28, display: 'flex', gap: 10, flexWrap: 'wrap' }}>
          <TierBadge tier={tier} />
          {returningUser && (
            <span style={{
              padding: '4px 10px', borderRadius: 999,
              background: C.goodSoft, color: C.good,
              fontSize: 11, fontWeight: 600, letterSpacing: 0.2,
            }}>↺ Returning · CKYCR match</span>
          )}
        </div>
      </div>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        <PrimaryBtn onClick={onStart}>
          Sign up with National ID →
        </PrimaryBtn>
        <button style={{
          height: 48, color: C.ink2, fontSize: 14, fontWeight: 500,
        }}>I already have an account</button>
      </div>
    </div>
  );
}

// ─── 2. NID + DOB entry ────────────────────────────────────
function NIDEntryScreen({ onNext, onBack }) {
  const [nid, setNid] = useState('');
  const [dob, setDob] = useState('');

  // Format NID as XXX-XXX-XXX-XX (11 digits)
  const fmt = (raw) => {
    const d = raw.replace(/\D/g, '').slice(0, 11);
    const a = d.slice(0,3), b = d.slice(3,6), c = d.slice(6,9), e = d.slice(9,11);
    return [a, b, c, e].filter(Boolean).join('-');
  };
  const fmtDob = (raw) => {
    const d = raw.replace(/\D/g, '').slice(0, 8);
    const y = d.slice(0,4), m = d.slice(4,6), day = d.slice(6,8);
    return [y, m, day].filter(Boolean).join('/');
  };

  const valid = nid.replace(/\D/g, '').length === 11 && dob.length === 10;

  return (
    <div style={{ padding: '70px 24px 24px', height: '100%', display: 'flex', flexDirection: 'column' }}>
      <button onClick={onBack} style={{ width: 40, height: 40,
        borderRadius: 12, background: C.surface, border: `1px solid ${C.line}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
      }}>
        <svg width="16" height="16" viewBox="0 0 16 16">
          <path d="M10 3l-5 5 5 5" fill="none" stroke={C.ink} strokeWidth="1.8"
                strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      </button>

      <div style={{ marginTop: 24 }}>
        <StepDots count={5} current={0} />
      </div>

      <h2 style={{ fontSize: 26, fontWeight: 700, letterSpacing: -0.8,
                   margin: '24px 0 6px' }}>
        Your National ID
      </h2>
      <p style={{ fontSize: 14, color: C.ink2, margin: 0, lineHeight: 1.45 }}>
        We'll verify with the NID Authority. Your raw ID number never leaves their server.
      </p>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 16, marginTop: 28 }}>
        <TextField
          label="NID Number" mono
          value={nid} onChange={v => setNid(fmt(v))}
          placeholder="000-000-000-00"
          hint="11 digits, printed on the front of your card"
        />
        <TextField
          label="Date of Birth" mono
          value={dob} onChange={v => setDob(fmtDob(v))}
          placeholder="YYYY/MM/DD"
        />
      </div>

      <div style={{
        marginTop: 20, padding: 14, borderRadius: 12,
        background: C.govSoft, display: 'flex', gap: 12, alignItems: 'flex-start',
      }}>
        <div style={{ marginTop: 1 }}>
          <svg width="18" height="18" viewBox="0 0 18 18">
            <path d="M9 1L2 4v5c0 4 3 7 7 8 4-1 7-4 7-8V4L9 1z"
                  fill="none" stroke={C.gov} strokeWidth="1.5"/>
            <path d="M6 9l2 2 4-4" fill="none" stroke={C.gov} strokeWidth="1.5"
                  strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        </div>
        <div style={{ fontSize: 12.5, color: C.gov, lineHeight: 1.5 }}>
          <strong>Tokenized.</strong> Paisa stores a Virtual ID — useless if our database is
          ever breached. Only the NID Authority sees your real number.
        </div>
      </div>

      <div style={{ flex: 1 }} />
      <PrimaryBtn onClick={onNext} disabled={!valid}>Continue</PrimaryBtn>
    </div>
  );
}

// ─── 3. Send-to-NID + OTP ──────────────────────────────────
function OTPScreen({ onNext, onBack, otpFail }) {
  const [code, setCode] = useState(['', '', '', '', '', '']);
  const [resending, setResending] = useState(false);
  const [phase, setPhase] = useState('sending'); // sending → enter
  const [seconds, setSeconds] = useState(58);

  useEffect(() => {
    const t = setTimeout(() => setPhase('enter'), 1400);
    return () => clearTimeout(t);
  }, []);

  useEffect(() => {
    if (phase !== 'enter') return;
    const i = setInterval(() => setSeconds(s => Math.max(0, s - 1)), 1000);
    return () => clearInterval(i);
  }, [phase]);

  const setDigit = (i, v) => {
    const d = v.replace(/\D/g, '').slice(-1);
    const next = [...code]; next[i] = d; setCode(next);
    if (d && i < 5) {
      const el = document.getElementById('otp-' + (i + 1));
      if (el) el.focus();
    }
    if (next.every(x => x) && !otpFail) {
      setTimeout(() => onNext(), 500);
    }
  };

  if (phase === 'sending') {
    return (
      <div style={{ padding: 32, height: '100%', display: 'flex',
                    flexDirection: 'column', alignItems: 'center', justifyContent: 'center' }}>
        <div style={{ position: 'relative', width: 96, height: 96 }}>
          <div style={{ position: 'absolute', inset: 0, borderRadius: '50%',
                        border: `2px solid ${C.gov}`, animation: 'pulse-ring 1.4s ease-out infinite' }} />
          <div style={{ position: 'absolute', inset: 12, borderRadius: '50%',
                        background: C.govSoft, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
            <NIDSeal size={50} />
          </div>
        </div>
        <div style={{ marginTop: 28, fontSize: 17, fontWeight: 600 }}>
          Calling NID Authority…
        </div>
        <div className="mono" style={{ marginTop: 8, fontSize: 11, color: C.ink3, letterSpacing: 0.5 }}>
          POST /v2/authenticate
        </div>
      </div>
    );
  }

  return (
    <div style={{ padding: '70px 24px 24px', height: '100%', display: 'flex', flexDirection: 'column' }}>
      <button onClick={onBack} style={{ width: 40, height: 40,
        borderRadius: 12, background: C.surface, border: `1px solid ${C.line}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
      }}>
        <svg width="16" height="16" viewBox="0 0 16 16">
          <path d="M10 3l-5 5 5 5" fill="none" stroke={C.ink} strokeWidth="1.8"
                strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      </button>
      <div style={{ marginTop: 24 }}><StepDots count={5} current={1} /></div>

      <h2 style={{ fontSize: 26, fontWeight: 700, letterSpacing: -0.8, margin: '24px 0 6px' }}>
        Enter the 6-digit code
      </h2>
      <p style={{ fontSize: 14, color: C.ink2, margin: 0, lineHeight: 1.45 }}>
        Sent to your NID-registered mobile <span className="mono" style={{ color: C.ink }}>+977 98•••••42</span>.
      </p>

      <div style={{ display: 'flex', gap: 10, marginTop: 32, justifyContent: 'space-between' }}>
        {code.map((d, i) => (
          <input
            key={i} id={'otp-' + i}
            value={d} onChange={e => setDigit(i, e.target.value)}
            inputMode="numeric" maxLength={1}
            style={{
              width: 48, height: 60, borderRadius: 12,
              border: `1.5px solid ${otpFail ? C.bad : d ? C.ink : C.line}`,
              background: C.surface,
              textAlign: 'center', fontSize: 24, fontWeight: 600,
              fontFamily: 'JetBrains Mono, monospace',
              color: C.ink, outline: 'none',
            }}
          />
        ))}
      </div>

      {otpFail && (
        <div style={{
          marginTop: 14, padding: 12, borderRadius: 10,
          background: 'oklch(0.96 0.03 25)', color: C.bad,
          fontSize: 13, lineHeight: 1.4,
        }}>
          <strong>Code not received?</strong> Try the fallback options — Paisa supports
          alternative authentication so no one is locked out.
        </div>
      )}

      <div style={{ marginTop: 24, display: 'flex', justifyContent: 'space-between',
                    fontSize: 13, color: C.ink2 }}>
        <span>Resend in <span className="mono">{String(seconds).padStart(2,'0')}s</span></span>
        <button onClick={() => { setSeconds(58); setResending(true); setTimeout(() => setResending(false), 600);} }
                style={{ color: C.accentInk, fontWeight: 600 }}>
          {resending ? 'Sending…' : 'Need a fallback?'}
        </button>
      </div>

      <div style={{ flex: 1 }} />
      <div style={{ fontSize: 11, color: C.ink3, textAlign: 'center', marginBottom: 12,
                    fontFamily: 'JetBrains Mono, monospace', letterSpacing: 0.4 }}>
        ✓ TLS · pinned to nid.gov.np
      </div>
    </div>
  );
}

// ─── 4. Government Consent Screen (handoff to NID app) ────
function ConsentScreen({ onNext, onBack, tier }) {
  const fields = [
    { k: 'Full name', v: 'Aanya Sharma' },
    { k: 'Photo', v: 'Profile photograph' },
    { k: 'Date of birth', v: '1996 / 04 / 12' },
    { k: 'Permanent address', v: 'Lalitpur · Bagmati' },
    { k: 'Gender', v: 'Female' },
  ];

  return (
    <div style={{
      height: '100%', display: 'flex', flexDirection: 'column',
      background: '#0d1220', color: '#fff',
    }}>
      {/* Top NID bar */}
      <div style={{
        padding: '70px 22px 18px',
        background: 'linear-gradient(180deg, #0d1220 0%, #1a2340 100%)',
        borderBottom: '1px solid rgba(255,255,255,0.08)',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <NIDSeal size={34} />
          <div>
            <div style={{ fontSize: 14, fontWeight: 700, letterSpacing: 0.3 }}>
              National ID Authority
            </div>
            <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.55)',
                          fontFamily: 'JetBrains Mono, monospace', letterSpacing: 0.4 }}>
              gov.np · secure
            </div>
          </div>
          <div style={{ flex: 1 }} />
          <div style={{ fontSize: 10, padding: '4px 8px', borderRadius: 999,
                        background: 'rgba(255,255,255,0.08)', color: 'rgba(255,255,255,0.7)' }}>
            ↗ handoff
          </div>
        </div>
      </div>

      <div style={{ padding: '24px 22px', flex: 1, overflowY: 'auto' }}>
        <div style={{ fontSize: 12, color: 'rgba(255,255,255,0.55)',
                      letterSpacing: 1, textTransform: 'uppercase', fontWeight: 600 }}>
          Consent request
        </div>
        <h2 style={{ fontSize: 22, fontWeight: 700, letterSpacing: -0.4,
                     lineHeight: 1.25, margin: '8px 0 0' }}>
          <span style={{ color: 'oklch(0.78 0.13 60)' }}>Paisa Wallet</span> wants to verify
          your identity for <span style={{ opacity: 0.8 }}>account opening</span>.
        </h2>

        <div style={{
          marginTop: 22, padding: 16, borderRadius: 14,
          background: 'rgba(255,255,255,0.05)',
          border: '1px solid rgba(255,255,255,0.08)',
        }}>
          <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.5)',
                        textTransform: 'uppercase', letterSpacing: 0.8, fontWeight: 600 }}>
            Will be shared
          </div>
          <div style={{ marginTop: 10 }}>
            {fields.map(f => (
              <div key={f.k} style={{
                padding: '10px 0', borderBottom: '1px solid rgba(255,255,255,0.06)',
                display: 'flex', justifyContent: 'space-between', alignItems: 'baseline',
                fontSize: 13.5,
              }}>
                <span style={{ color: 'rgba(255,255,255,0.6)' }}>{f.k}</span>
                <span style={{ fontWeight: 500 }}>{f.v}</span>
              </div>
            ))}
          </div>
          <div style={{ marginTop: 12, fontSize: 12, color: 'rgba(255,255,255,0.5)',
                        lineHeight: 1.5 }}>
            <strong style={{ color: '#fff' }}>Not shared:</strong> raw NID number, biometric
            templates, family records, passport / migration data.
          </div>
        </div>

        <div style={{
          marginTop: 14, padding: 14, borderRadius: 12,
          background: 'rgba(120,180,255,0.08)',
          border: '1px solid rgba(120,180,255,0.15)',
          display: 'flex', gap: 12, alignItems: 'flex-start',
        }}>
          <div style={{ fontSize: 18 }}>↺</div>
          <div style={{ fontSize: 12, color: 'rgba(255,255,255,0.75)', lineHeight: 1.5 }}>
            You can <strong style={{ color: '#fff' }}>revoke</strong> this consent any time
            from <em>mNID app → Authentications</em>. Paisa will be required to delete the
            bundle within 7 days.
          </div>
        </div>

        <div className="mono" style={{ marginTop: 14, fontSize: 10.5,
                                       color: 'rgba(255,255,255,0.4)', letterSpacing: 0.4,
                                       lineHeight: 1.6 }}>
          Purpose: ACCT_OPEN · Tier-{tier} · Token TTL 24h<br/>
          Auth ID: NID-AUTH-2026-{('0000' + Math.floor(Math.random()*99999)).slice(-5)}
        </div>
      </div>

      <div style={{ padding: '12px 22px 28px',
                    borderTop: '1px solid rgba(255,255,255,0.08)',
                    background: '#0d1220',
                    display: 'flex', flexDirection: 'column', gap: 10 }}>
        <button onClick={onNext} style={{
          height: 54, borderRadius: 14, background: 'oklch(0.78 0.13 60)',
          color: '#0d1220', fontSize: 16, fontWeight: 700,
        }}>
          Approve & continue to biometric
        </button>
        <button onClick={onBack} style={{
          height: 44, color: 'rgba(255,255,255,0.7)', fontSize: 14, fontWeight: 500,
        }}>Deny</button>
      </div>
    </div>
  );
}

// ─── 5. Biometric / face scan (Tier 1+) ────────────────────
function BiometricScreen({ onNext, onBack, fail, tier }) {
  const [phase, setPhase] = useState('frame'); // frame → scan → match → done|fail
  const [progress, setProgress] = useState(0);

  useEffect(() => {
    const t1 = setTimeout(() => setPhase('scan'), 1200);
    return () => clearTimeout(t1);
  }, []);

  useEffect(() => {
    if (phase !== 'scan') return;
    let p = 0;
    const i = setInterval(() => {
      p += 4;
      setProgress(Math.min(100, p));
      if (p >= 100) {
        clearInterval(i);
        setTimeout(() => setPhase('match'), 400);
      }
    }, 60);
    return () => clearInterval(i);
  }, [phase]);

  useEffect(() => {
    if (phase !== 'match') return;
    const t = setTimeout(() => {
      setPhase(fail ? 'fail' : 'done');
      if (!fail) setTimeout(onNext, 800);
    }, 1100);
    return () => clearTimeout(t);
  }, [phase, fail]);

  const ringColor = phase === 'fail' ? C.bad
                  : phase === 'done' ? C.good
                  : C.accent;

  return (
    <div style={{
      height: '100%', display: 'flex', flexDirection: 'column',
      background: '#0d1220', color: '#fff',
    }}>
      <div style={{ padding: '70px 22px 12px', display: 'flex', alignItems: 'center', gap: 12 }}>
        <button onClick={onBack} style={{
          width: 36, height: 36, borderRadius: 10,
          background: 'rgba(255,255,255,0.08)',
          display: 'flex', alignItems: 'center', justifyContent: 'center',
        }}>
          <svg width="14" height="14" viewBox="0 0 16 16">
            <path d="M10 3l-5 5 5 5" fill="none" stroke="#fff" strokeWidth="1.8"
                  strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        </button>
        <div style={{ fontSize: 13, fontWeight: 600 }}>Biometric · liveness check</div>
        <div style={{ flex: 1 }} />
        <NIDSeal size={26} />
      </div>

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column',
                    alignItems: 'center', justifyContent: 'center', padding: 24 }}>
        {/* Face frame */}
        <div style={{ position: 'relative', width: 240, height: 280 }}>
          {/* outer scan ring */}
          <svg width="240" height="280" viewBox="0 0 240 280" style={{ position: 'absolute', inset: 0 }}>
            <ellipse cx="120" cy="140" rx="110" ry="135"
                     fill="none" stroke="rgba(255,255,255,0.12)" strokeWidth="2"/>
            {phase === 'scan' && (
              <ellipse cx="120" cy="140" rx="110" ry="135"
                       fill="none" stroke={ringColor} strokeWidth="3"
                       strokeDasharray={`${progress * 7.5} 1000`}
                       transform="rotate(-90 120 140)"
                       style={{ transition: 'stroke-dasharray .1s linear' }}/>
            )}
            {phase === 'match' && (
              <ellipse cx="120" cy="140" rx="110" ry="135"
                       fill="none" stroke={ringColor} strokeWidth="3"
                       style={{ animation: 'pulse-ring 1.2s ease-in-out infinite' }}/>
            )}
            {(phase === 'done' || phase === 'fail') && (
              <ellipse cx="120" cy="140" rx="110" ry="135"
                       fill="none" stroke={ringColor} strokeWidth="3"/>
            )}
          </svg>

          {/* corner brackets */}
          {[
            { top: 30, left: 30, rot: 0 },
            { top: 30, right: 30, rot: 90 },
            { bottom: 30, right: 30, rot: 180 },
            { bottom: 30, left: 30, rot: 270 },
          ].map((p, i) => (
            <div key={i} style={{
              position: 'absolute', ...p,
              width: 18, height: 18,
              borderTop: `2px solid ${ringColor}`,
              borderLeft: `2px solid ${ringColor}`,
              transform: `rotate(${p.rot}deg)`,
              transition: 'border-color .3s',
            }}/>
          ))}

          {/* placeholder face */}
          <div style={{
            position: 'absolute', top: 60, left: 70, width: 100, height: 130,
            display: 'flex', alignItems: 'center', justifyContent: 'center',
            background: `radial-gradient(ellipse at 50% 35%,
              rgba(255,255,255,0.12) 0%, rgba(255,255,255,0.02) 70%)`,
            borderRadius: '50% 50% 50% 50% / 55% 55% 45% 45%',
          }}>
            <div style={{
              fontFamily: 'JetBrains Mono, monospace',
              fontSize: 9, color: 'rgba(255,255,255,0.5)', letterSpacing: 0.5,
              textAlign: 'center',
            }}>[face_capture]<br/>liveness ✓</div>
          </div>

          {phase === 'scan' && (
            <div style={{
              position: 'absolute', left: 30, right: 30,
              top: `${30 + (220 * progress / 100)}px`,
              height: 2, background: ringColor,
              boxShadow: `0 0 20px ${ringColor}`,
              transition: 'top .1s linear',
            }}/>
          )}
        </div>

        <div style={{ marginTop: 32, textAlign: 'center', minHeight: 80 }}>
          {phase === 'frame' && <>
            <div style={{ fontSize: 18, fontWeight: 600 }}>Center your face in the frame</div>
            <div style={{ fontSize: 13, color: 'rgba(255,255,255,0.55)', marginTop: 6 }}>
              Look straight ahead. We'll detect movement to confirm liveness.
            </div>
          </>}
          {phase === 'scan' && <>
            <div style={{ fontSize: 18, fontWeight: 600 }}>Scanning…</div>
            <div className="mono" style={{ fontSize: 12, color: 'rgba(255,255,255,0.55)', marginTop: 6, letterSpacing: 0.5 }}>
              {progress}% · liveness · 1:N match
            </div>
          </>}
          {phase === 'match' && <>
            <div style={{ fontSize: 18, fontWeight: 600 }}>Matching against NID database</div>
            <div className="mono" style={{ fontSize: 12, color: 'rgba(255,255,255,0.55)', marginTop: 6, letterSpacing: 0.5 }}>
              encrypted · template-only
            </div>
          </>}
          {phase === 'done' && <>
            <div style={{ fontSize: 20, fontWeight: 700, color: ringColor }}>Match · 99.6%</div>
            <div style={{ fontSize: 13, color: 'rgba(255,255,255,0.55)', marginTop: 6 }}>
              Identity verified by NID Authority.
            </div>
          </>}
          {phase === 'fail' && <>
            <div style={{ fontSize: 18, fontWeight: 700, color: ringColor }}>Couldn't match</div>
            <div style={{ fontSize: 13, color: 'rgba(255,255,255,0.55)', marginTop: 6,
                          maxWidth: 280, marginInline: 'auto' }}>
              No problem — try a fallback. You can verify with fingerprint at any
              registered agent point, or proceed with Tier 0 access.
            </div>
          </>}
        </div>
      </div>

      {phase === 'fail' && (
        <div style={{ padding: '0 22px 28px', display: 'flex', flexDirection: 'column', gap: 10 }}>
          <button style={{
            height: 50, borderRadius: 12, background: '#fff', color: '#0d1220',
            fontSize: 15, fontWeight: 600,
          }}>Try fingerprint at agent</button>
          <button onClick={onNext} style={{
            height: 50, borderRadius: 12, background: 'transparent',
            border: '1px solid rgba(255,255,255,0.2)', color: '#fff',
            fontSize: 15, fontWeight: 500,
          }}>Continue with Tier 0 →</button>
        </div>
      )}
    </div>
  );
}

// ─── 6. Signed eKYC bundle reveal ──────────────────────────
function BundleScreen({ onNext, onBack, tier }) {
  const [revealed, setRevealed] = useState(false);
  useEffect(() => { const t = setTimeout(() => setRevealed(true), 200); return () => clearTimeout(t); }, []);

  return (
    <div style={{ padding: '70px 24px 24px', height: '100%', display: 'flex', flexDirection: 'column',
                  background: C.bg }}>
      <button onClick={onBack} style={{ width: 40, height: 40,
        borderRadius: 12, background: C.surface, border: `1px solid ${C.line}`,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
      }}>
        <svg width="16" height="16" viewBox="0 0 16 16">
          <path d="M10 3l-5 5 5 5" fill="none" stroke={C.ink} strokeWidth="1.8"
                strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      </button>
      <div style={{ marginTop: 24 }}><StepDots count={5} current={3} /></div>

      <h2 style={{ fontSize: 26, fontWeight: 700, letterSpacing: -0.8, margin: '24px 0 6px' }}>
        Signed by the<br/>NID Authority
      </h2>
      <p style={{ fontSize: 14, color: C.ink2, margin: 0, lineHeight: 1.45 }}>
        This is the eKYC bundle Paisa now stores. No manual review needed.
      </p>

      <div style={{
        marginTop: 24, padding: 20, borderRadius: 18,
        background: C.surface, border: `1px solid ${C.line}`,
        boxShadow: '0 4px 24px rgba(24,21,19,0.06)',
        opacity: revealed ? 1 : 0, transform: revealed ? 'translateY(0)' : 'translateY(8px)',
        transition: 'all .5s ease',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 12, marginBottom: 4 }}>
          <PhotoPlaceholder size={64} name="AS"/>
          <div>
            <div style={{ fontSize: 17, fontWeight: 700, letterSpacing: -0.3 }}>
              Aanya Sharma
            </div>
            <div className="mono" style={{ fontSize: 11, color: C.ink3, marginTop: 4, letterSpacing: 0.4 }}>
              VID · 9842•••••3076
            </div>
          </div>
          <div style={{ flex: 1 }} />
          <NIDSeal size={32}/>
        </div>

        <div style={{ marginTop: 12 }}>
          <BundleRow k="DOB" v="12 Apr 1996"/>
          <BundleRow k="Gender" v="Female"/>
          <BundleRow k="Address" v="Lalitpur, Bagmati"/>
          <BundleRow k="Tier" v={`Tier ${tier}`}/>
          <BundleRow k="Issued" v="6 May 2026 · 14:32 NPT" mono/>
        </div>

        <div style={{
          marginTop: 14, padding: 12, borderRadius: 10,
          background: C.goodSoft, display: 'flex', alignItems: 'center', gap: 10,
        }}>
          <svg width="18" height="18" viewBox="0 0 20 20">
            <path d="M10 2l7 3v5c0 4-3 7-7 8-4-1-7-4-7-8V5l7-3z" fill="none" stroke={C.good} strokeWidth="1.5"/>
            <path d="M7 10l2 2 4-4" fill="none" stroke={C.good} strokeWidth="1.5"
                  strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
          <div style={{ fontSize: 12, color: C.good, fontWeight: 500, lineHeight: 1.4 }}>
            <strong>Signed</strong> · ed25519 · verified against NID public key
          </div>
        </div>

        <div className="mono" style={{ fontSize: 9.5, color: C.ink3, marginTop: 10,
                                       letterSpacing: 0.3, lineHeight: 1.5,
                                       wordBreak: 'break-all' }}>
          sig: ed25519:7f3a…b29e · key: nid-auth-2026-01<br/>
          ckyc-id: CKYC-NP-7HQ4J2-K88B
        </div>
      </div>

      <div style={{ flex: 1 }} />
      <PrimaryBtn onClick={onNext}>Activate my wallet</PrimaryBtn>
    </div>
  );
}

// ─── 7. CKYCR registration / success ───────────────────────
function SuccessScreen({ onDone, tier, returningUser }) {
  const [step, setStep] = useState(0); // 0 ckyc → 1 done
  useEffect(() => {
    const t = setTimeout(() => setStep(1), 1600);
    return () => clearTimeout(t);
  }, []);

  return (
    <div style={{ padding: '90px 28px 32px', height: '100%',
                  display: 'flex', flexDirection: 'column', background: C.bg }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        <PaisaMark size={28} />
        <span style={{ fontSize: 18, fontWeight: 700, letterSpacing: -0.5 }}>paisa</span>
        <div style={{ flex: 1 }} />
        <StepDots count={5} current={4} />
      </div>

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
        {step === 0 ? (
          <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start' }}>
            <Spinner size={28}/>
            <h2 style={{ fontSize: 26, fontWeight: 700, letterSpacing: -0.6,
                         margin: '20px 0 6px', lineHeight: 1.15 }}>
              Registering with the<br/>Central KYC Registry…
            </h2>
            <p style={{ fontSize: 14, color: C.ink2, margin: 0, lineHeight: 1.5 }}>
              So next time you open a wallet or bank account, you skip this whole flow.
            </p>
            <div className="mono" style={{ marginTop: 18, fontSize: 11, color: C.ink3, letterSpacing: 0.4 }}>
              POST ckycr.gov.np/v1/onboard
            </div>
          </div>
        ) : (
          <div style={{ animation: 'fade-up .5s ease' }}>
            <AnimatedCheck size={72}/>
            <h1 style={{ fontSize: 38, fontWeight: 700, letterSpacing: -1.2,
                         margin: '24px 0 6px', lineHeight: 1.05 }}>
              You're in,<br/>Aanya.
            </h1>
            <p style={{ fontSize: 16, color: C.ink2, margin: 0, lineHeight: 1.45, maxWidth: 320 }}>
              Wallet activated. Verified by the Government of Nepal in {returningUser ? '6' : '52'} seconds.
            </p>

            <div style={{ marginTop: 28, padding: 16, borderRadius: 14,
                          background: C.surface, border: `1px solid ${C.line}` }}>
              <div style={{ fontSize: 11, color: C.ink3, fontWeight: 600,
                            textTransform: 'uppercase', letterSpacing: 0.6 }}>
                Your CKYC ID
              </div>
              <div className="mono" style={{ fontSize: 18, fontWeight: 600, marginTop: 4, letterSpacing: 0.4 }}>
                CKYC-NP-7HQ4J2-K88B
              </div>
              <div style={{ marginTop: 10, fontSize: 12, color: C.ink2, lineHeight: 1.5 }}>
                Reusable across all Nepali wallets, banks & fintechs — with your consent.
              </div>
            </div>

            <div style={{ marginTop: 16, display: 'flex', gap: 10 }}>
              <TierBadge tier={tier} size="lg"/>
              <span style={{
                padding: '8px 14px', borderRadius: 999,
                background: C.line2, color: C.ink2,
                fontSize: 13, fontWeight: 500,
              }}>NPR {tier === 0 ? '10K' : tier === 1 ? '1L' : '5L+'} balance</span>
            </div>
          </div>
        )}
      </div>

      {step === 1 && (
        <PrimaryBtn onClick={onDone}>Open my wallet</PrimaryBtn>
      )}
    </div>
  );
}

// ─── 8. Returning user / CKYCR hit ─────────────────────────
function ReturningScreen({ onUseExisting, onStartFresh }) {
  return (
    <div style={{ padding: '90px 28px 32px', height: '100%',
                  display: 'flex', flexDirection: 'column', background: C.bg }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
        <PaisaMark size={28} />
        <span style={{ fontSize: 18, fontWeight: 700 }}>paisa</span>
      </div>

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center' }}>
        <div style={{ fontSize: 12, color: C.good, fontWeight: 700, letterSpacing: 1.2,
                      textTransform: 'uppercase' }}>
          ↺ Found in CKYCR
        </div>
        <h2 style={{ fontSize: 30, fontWeight: 700, letterSpacing: -1, lineHeight: 1.1,
                     margin: '10px 0 8px' }}>
          We already know<br/>you, Aanya.
        </h2>
        <p style={{ fontSize: 15, color: C.ink2, lineHeight: 1.5, margin: 0 }}>
          You completed KYC with eSewa on 18 Feb 2026. With your consent, we'll pull
          that record — no biometric, no OTP, no waiting.
        </p>

        <div style={{ marginTop: 24, padding: 18, borderRadius: 16,
                      background: C.surface, border: `1px solid ${C.line}` }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
            <PhotoPlaceholder size={56} name="AS"/>
            <div>
              <div style={{ fontSize: 16, fontWeight: 600 }}>Aanya Sharma</div>
              <div className="mono" style={{ fontSize: 11, color: C.ink3, marginTop: 4, letterSpacing: 0.4 }}>
                CKYC-NP-7HQ4J2-K88B
              </div>
            </div>
          </div>
          <div style={{
            marginTop: 14, padding: 10, borderRadius: 10, background: C.line2,
            fontSize: 12, color: C.ink2, lineHeight: 1.5,
          }}>
            <strong>Re-using saves ~50 seconds.</strong> Your data never left the registry —
            Paisa requests, you approve, registry releases.
          </div>
        </div>

        <div style={{ marginTop: 16, fontSize: 11, color: C.ink3, lineHeight: 1.5 }}>
          You can review every CKYCR pull in your <strong>mNID app</strong> at any time.
        </div>
      </div>

      <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
        <PrimaryBtn onClick={onUseExisting}>
          Use my CKYCR record →
        </PrimaryBtn>
        <button onClick={onStartFresh} style={{
          height: 48, color: C.ink2, fontSize: 14, fontWeight: 500,
        }}>Start fresh instead</button>
      </div>
    </div>
  );
}

// ─── 9. Wallet home (after onboarding) ─────────────────────
function WalletHome({ tier, onOpenAudit, onUpgrade }) {
  const txs = [
    { t: 'Top-up · IME Bank', a: '+ 5,000', when: 'Just now', cat: 'in' },
    { t: 'Bhatbhateni Megastore', a: '− 1,840', when: '2h ago', cat: 'out' },
    { t: 'From Manish K. (P2P)', a: '+ 750', when: 'Yesterday', cat: 'in' },
  ];

  return (
    <div style={{ height: '100%', background: C.bg, display: 'flex', flexDirection: 'column' }}>
      <div style={{
        background: C.ink, color: '#fffdf8',
        padding: '70px 22px 28px',
        borderRadius: '0 0 28px 28px',
      }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
          <PaisaMark size={26} color="#fffdf8"/>
          <span style={{ fontSize: 16, fontWeight: 700 }}>paisa</span>
          <div style={{ flex: 1 }}/>
          <button onClick={onOpenAudit} style={{
            display: 'flex', alignItems: 'center', gap: 6,
            padding: '6px 10px', borderRadius: 999,
            background: 'rgba(255,253,248,0.08)', fontSize: 11, fontWeight: 500,
          }}>
            <NIDSeal size={14}/> mNID
          </button>
        </div>

        <div style={{ marginTop: 26 }}>
          <div style={{ fontSize: 11, opacity: 0.6, fontWeight: 600,
                        textTransform: 'uppercase', letterSpacing: 0.6 }}>
            Available balance
          </div>
          <div style={{ display: 'flex', alignItems: 'baseline', gap: 6, marginTop: 4 }}>
            <span style={{ fontSize: 12, opacity: 0.7 }}>NPR</span>
            <span style={{ fontSize: 38, fontWeight: 700, letterSpacing: -1 }}>5,910</span>
            <span style={{ fontSize: 18, opacity: 0.55 }}>.42</span>
          </div>
          <div style={{ marginTop: 10, display: 'flex', gap: 8, alignItems: 'center' }}>
            <TierBadge tier={tier}/>
            {tier < 2 && (
              <button onClick={onUpgrade} style={{
                fontSize: 11, color: 'oklch(0.85 0.13 60)', fontWeight: 600,
              }}>↑ Upgrade tier</button>
            )}
          </div>
        </div>

        <div style={{ marginTop: 22, display: 'grid', gridTemplateColumns: 'repeat(4, 1fr)', gap: 8 }}>
          {[
            { l: 'Send', i: '→' },
            { l: 'Receive', i: '↓' },
            { l: 'Top-up', i: '+' },
            { l: 'Pay', i: '⌗' },
          ].map(a => (
            <div key={a.l} style={{
              padding: '10px 0', borderRadius: 14,
              background: 'rgba(255,253,248,0.08)',
              display: 'flex', flexDirection: 'column', alignItems: 'center', gap: 6,
            }}>
              <span style={{ fontSize: 18 }}>{a.i}</span>
              <span style={{ fontSize: 11, fontWeight: 500, opacity: 0.85 }}>{a.l}</span>
            </div>
          ))}
        </div>
      </div>

      <div style={{ flex: 1, padding: '20px 22px', overflowY: 'auto' }}>
        <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between' }}>
          <span style={{ fontSize: 14, fontWeight: 600 }}>Recent activity</span>
          <span style={{ fontSize: 12, color: C.ink3 }}>See all</span>
        </div>
        <div style={{ marginTop: 12, background: C.surface, borderRadius: 14,
                      border: `1px solid ${C.line}`, padding: '4px 14px' }}>
          {txs.map((t, i) => (
            <div key={i} style={{
              padding: '12px 0',
              borderBottom: i < txs.length - 1 ? `1px solid ${C.line2}` : 'none',
              display: 'flex', alignItems: 'center', gap: 12,
            }}>
              <div style={{
                width: 32, height: 32, borderRadius: 10,
                background: t.cat === 'in' ? C.goodSoft : C.line2,
                color: t.cat === 'in' ? C.good : C.ink2,
                display: 'flex', alignItems: 'center', justifyContent: 'center',
                fontSize: 14, fontWeight: 700,
              }}>{t.cat === 'in' ? '↓' : '↑'}</div>
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 14, fontWeight: 500 }}>{t.t}</div>
                <div style={{ fontSize: 11, color: C.ink3 }}>{t.when}</div>
              </div>
              <div className="mono" style={{
                fontSize: 14, fontWeight: 600,
                color: t.cat === 'in' ? C.good : C.ink,
              }}>{t.a}</div>
            </div>
          ))}
        </div>

        <div style={{
          marginTop: 16, padding: 14, borderRadius: 14,
          background: C.govSoft, border: `1px solid ${C.gov}30`,
          display: 'flex', gap: 12, alignItems: 'flex-start',
        }}>
          <NIDSeal size={28}/>
          <div>
            <div style={{ fontSize: 13, fontWeight: 600, color: C.gov }}>
              Verified by NID Authority
            </div>
            <div style={{ fontSize: 12, color: C.ink2, marginTop: 4, lineHeight: 1.45 }}>
              View or revoke this consent in <strong>mNID app → Authentications</strong>.
            </div>
            <button onClick={onOpenAudit} style={{
              marginTop: 8, fontSize: 12, fontWeight: 600, color: C.gov,
            }}>Open audit log →</button>
          </div>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, {
  WelcomeScreen, NIDEntryScreen, OTPScreen, ConsentScreen,
  BiometricScreen, BundleScreen, SuccessScreen,
  ReturningScreen, WalletHome,
});
