function QuizPage({ onNavigate, toast }) {
  const questions = [
    {
      q: "You receive an email from 'chief.radiology@hosp1tal.com' asking you to urgently share a patient's MRI. What do you do?",
      options: [
        { letter: 'A', text: 'Reply with the file attached — the chief is in a hurry.' },
        { letter: 'B', text: 'Forward to a colleague to verify before sending.' },
        { letter: 'C', text: 'Report as phishing and contact the chief by phone using a known number.' },
        { letter: 'D', text: 'Click the attached verification link to check the request.' },
      ],
      correct: 2,
      explain: "Correct. The domain 'hosp1tal.com' swaps an 'i' for a '1' — a classic typosquat. Always verify urgent requests by an out-of-band channel like a known phone number.",
      wrongExplain: {
        0: "Never send patient data based on an unverified email — especially one with a suspicious domain.",
        1: "Forwarding spreads the phish. Report it, then verify via a known channel.",
        3: "The link itself could harvest credentials or install malware. Don't click — report."
      }
    },
    {
      q: "A visitor asks to charge their phone via the USB port on a nursing workstation. What is the safest action?",
      options: [
        { letter: 'A', text: 'Allow it briefly — it\'s just charging.' },
        { letter: 'B', text: 'Redirect them to a public outlet — never USB on medical workstations.' },
        { letter: 'C', text: 'Ask a colleague what they think.' },
        { letter: 'D', text: 'Lock the screen and allow charging.' },
      ],
      correct: 1,
      explain: "Correct. 'Juice jacking' devices can exfiltrate data or install malware through USB. Medical workstations must never accept unknown USB peripherals, even for charging.",
      wrongExplain: {
        0: "USB ports transmit data, not just power. This is a known attack vector (BadUSB, juice jacking).",
        2: "Policy is already clear — no unknown USB devices on clinical workstations. Don't wait.",
        3: "Locking the screen doesn't prevent USB-level attacks, which operate below the OS login."
      }
    },
    {
      q: "You see a colleague's credentials written on a sticky note under their keyboard. Best first step?",
      options: [
        { letter: 'A', text: 'Ignore — it\'s their problem.' },
        { letter: 'B', text: 'Take the note and throw it away.' },
        { letter: 'C', text: 'Privately remind the colleague and report to compliance.' },
        { letter: 'D', text: 'Post about it in the staff chat as a warning.' },
      ],
      correct: 2,
      explain: "Correct. A private reminder preserves the working relationship; reporting to compliance triggers the required password rotation and creates the audit trail.",
      wrongExplain: {
        0: "Shared credentials risk breach of every patient record that account can access — not just theirs.",
        1: "Removing evidence without telling them leaves them locked out and removes the audit trail.",
        3: "Public shaming damages trust. Private resolution + official reporting is the right workflow."
      }
    }
  ];

  const [idx, setIdx] = useState(0);
  const [selected, setSelected] = useState(null);
  const [locked, setLocked] = useState(false);
  const [score, setScore] = useState(0);
  const [done, setDone] = useState(false);
  const [introOpen, setIntroOpen] = useState(() => {
    try { return !localStorage.getItem('medishield_quiz_intro_seen'); } catch { return true; }
  });
  const dismissIntro = () => {
    setIntroOpen(false);
    try { localStorage.setItem('medishield_quiz_intro_seen', '1'); } catch {}
  };

  const q = questions[idx];

  const choose = (i) => {
    if (locked) return;
    setSelected(i);
    setLocked(true);
    if (i === q.correct) setScore(s => s + 1);
  };

  const next = () => {
    if (idx + 1 < questions.length) {
      setIdx(idx + 1);
      setSelected(null);
      setLocked(false);
    } else {
      setDone(true);
    }
  };

  if (done) {
    const pct = Math.round((score / questions.length) * 100);
    const passed = pct >= 80;
    return (
      <div className="page-enter" style={{maxWidth: 640, margin: '40px auto'}}>
        <div className="card" style={{textAlign: 'center'}}>
          <div className="card-body" style={{padding: 48}}>
            <div style={{width: 88, height: 88, borderRadius: '50%', background: passed ? 'var(--green-100)' : 'var(--coral-100)', color: passed ? 'var(--green)' : 'var(--coral)', display: 'grid', placeItems: 'center', margin: '0 auto 20px'}}>
              {passed ? <I.Award size={44}/> : <I.AlertTriangle size={40}/>}
            </div>
            <div className="muted" style={{fontSize: 12, textTransform: 'uppercase', letterSpacing: '0.08em', fontWeight: 600}}>Quiz complete</div>
            <h1 style={{fontSize: 28, marginTop: 6, marginBottom: 6}}>{passed ? 'Badge earned!' : 'Nearly there'}</h1>
            <p className="muted" style={{fontSize: 14.5}}>Recognizing phishing in clinical email</p>

            <div style={{display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 12, marginTop: 32, marginBottom: 24}}>
              <div style={{padding: 16, background: 'var(--bg)', borderRadius: 10}}>
                <div className="kpi-label">Score</div>
                <div style={{fontSize: 26, fontWeight: 700, color: 'var(--navy)'}}>{score}/{questions.length}</div>
              </div>
              <div style={{padding: 16, background: 'var(--bg)', borderRadius: 10}}>
                <div className="kpi-label">Result</div>
                <div style={{fontSize: 26, fontWeight: 700, color: passed ? 'var(--green)' : 'var(--coral)'}}>{pct}%</div>
              </div>
              <div style={{padding: 16, background: 'var(--bg)', borderRadius: 10}}>
                <div className="kpi-label">Threshold</div>
                <div style={{fontSize: 26, fontWeight: 700, color: 'var(--navy)'}}>80%</div>
              </div>
            </div>

            {passed && (
              <div style={{display: 'inline-flex', gap: 10, alignItems: 'center', padding: '10px 16px', background: 'var(--teal-100)', borderRadius: 100, border: '1px solid #BFE3DF'}}>
                <I.Award size={16} color="var(--teal)"/>
                <span style={{fontSize: 13, fontWeight: 600, color: 'var(--navy)'}}>Phishing Awareness — Level 2</span>
              </div>
            )}

            <div style={{display: 'flex', gap: 10, justifyContent: 'center', marginTop: 32}}>
              <button className="btn btn-outline" onClick={() => {setIdx(0); setSelected(null); setLocked(false); setScore(0); setDone(false);}}>Retake quiz</button>
              <button className="btn btn-primary" onClick={() => {toast('Progress saved','success'); onNavigate('dashboard');}}>Back to dashboard <I.ArrowRight size={14}/></button>
            </div>
          </div>
        </div>
      </div>
    );
  }

  return (
    <div className="page-enter" style={{maxWidth: 720, margin: '0 auto'}}>
      <div className="breadcrumbs">
        <a href="#" onClick={e => {e.preventDefault(); onNavigate('lesson');}}>Lesson</a>
        <I.ChevronRight size={12}/>
        <span style={{color: 'var(--navy)'}}>Knowledge check</span>
      </div>

      <div className="row-between" style={{marginBottom: 16, alignItems: 'flex-start'}}>
        <div>
          <div className="muted" style={{fontSize: 10.5, textTransform: 'uppercase', letterSpacing: '0.05em', fontWeight: 600}}>Module PHI-201 · Knowledge check</div>
          <h1 style={{fontSize: 20, marginTop: 4}}>Question {idx + 1} of {questions.length}</h1>
        </div>
        <div style={{textAlign: 'right'}}>
          <div className="muted" style={{fontSize: 10.5, textTransform: 'uppercase', letterSpacing: '0.05em', fontWeight: 600, marginBottom: 6}}>Pass threshold · 80%</div>
          <div style={{display: 'flex', gap: 3}}>
            {questions.map((_, i) => (
              <div key={i} style={{width: 32, height: 4, background: i < idx ? 'var(--teal)' : i === idx ? 'var(--navy)' : 'var(--border)'}}/>
            ))}
          </div>
        </div>
      </div>

      <div className="card">
        <div className="card-body" style={{padding: 28}}>
          {introOpen && idx === 0 && !locked && (
            <div className="callout callout-teal" style={{marginBottom: 20, alignItems: 'flex-start'}}>
              <I.Info size={18} style={{flexShrink: 0, marginTop: 2}}/>
              <div style={{flex: 1}}>
                <div className="callout-title">Before you start</div>
                This quiz has <b>{questions.length} questions</b>. You'll see the correct answer and a short explanation after each one.
                You need <b>80%</b> to pass. There is no time limit.
                <div style={{marginTop: 10}}>
                  <button type="button" className="btn btn-sm btn-outline" onClick={dismissIntro}>Got it</button>
                </div>
              </div>
            </div>
          )}
          <p style={{fontSize: 17, fontWeight: 500, color: 'var(--navy)', lineHeight: 1.5, marginBottom: 20}}>{q.q}</p>
          <div className="stack-sm" role="radiogroup">
            {q.options.map((opt, i) => {
              const isSelected = selected === i;
              const isCorrect = locked && i === q.correct;
              const isWrong = locked && isSelected && i !== q.correct;
              return (
                <button
                  key={i}
                  className={`quiz-option ${isSelected && !locked ? 'selected' : ''} ${isCorrect ? 'correct' : ''} ${isWrong ? 'incorrect' : ''} ${locked ? 'locked' : ''}`}
                  onClick={() => choose(i)}
                  role="radio"
                  aria-checked={isSelected}
                  disabled={locked}
                >
                  <span className="quiz-option-letter">{opt.letter}</span>
                  <span style={{flex: 1, color: 'var(--ink)', fontSize: 14}}>{opt.text}</span>
                  {isCorrect && <I.CheckCircle size={18} color="var(--green)"/>}
                  {isWrong && <I.XCircle size={18} color="var(--coral)"/>}
                </button>
              );
            })}
          </div>

          {locked && (
            <div className={`callout ${selected === q.correct ? 'callout-green' : 'callout-red'}`} style={{marginTop: 20}}>
              {selected === q.correct ? <I.CheckCircle size={20} style={{flexShrink: 0, marginTop: 2}}/> : <I.AlertTriangle size={20} style={{flexShrink: 0, marginTop: 2}}/>}
              <div>
                <div className="callout-title">{selected === q.correct ? 'Correct' : 'Not quite'}</div>
                {selected === q.correct ? q.explain : q.wrongExplain[selected]}
              </div>
            </div>
          )}

          <div className="row-between" style={{marginTop: 24, paddingTop: 20, borderTop: '1px solid var(--border-2)'}}>
            <button className="btn btn-ghost" onClick={() => onNavigate('lesson')}>Back to lesson</button>
            <button className="btn btn-primary" onClick={next} disabled={!locked}>
              {idx + 1 === questions.length ? 'See results' : 'Next question'} <I.ArrowRight size={14}/>
            </button>
          </div>
        </div>
      </div>
    </div>
  );
}

window.QuizPage = QuizPage;
