    fb.innerHTML = `<span class="${detail.is_correct ? 'fb-correct' : 'fb-wrong'}">${detail.is_correct ? '✓ Correct!' : '✗ Incorrect'}</span> ${escHtml(detail.explanation)}`;
  });

  // Show results panel
  const pct = res.percentage;
  const circle = document.getElementById('scoreCircle');
  circle.textContent = res.score + '/' + res.total;
  circle.className = 'quiz-score-circle ' + (pct >= 80 ? 'score-gold' : pct >= 60 ? 'score-silver' : 'score-bronze');

  const label = document.getElementById('resultLabel');
  const msg   = document.getElementById('resultMsg');
  if (pct === 100) {
    label.textContent = '🏆 Perfect Score!';
    msg.textContent   = 'Outstanding! You have mastered this event completely.';
  } else if (pct >= 80) {
    label.textContent = '🌟 Excellent Work!';
    msg.textContent   = 'Great understanding of this significant event.';
  } else if (pct >= 60) {
    label.textContent = '👍 Good Effort!';
    msg.textContent   = 'You have a solid grasp — review the reading to improve further.';
  } else {
    label.textContent = '📖 Keep Studying!';
    msg.textContent   = 'Review the reading material and try again to strengthen your knowledge.';
  }

  // Scroll to results
  const resultsPanel = document.getElementById('quizResults');
  resultsPanel.style.display = 'block';
  document.getElementById('quizForm').querySelector('.quiz-submit-row').style.display = 'none';
  setTimeout(() => resultsPanel.scrollIntoView({ behavior: 'smooth', block: 'start' }), 100);
}

function retryQuiz() {
  location.href = '/events.php?id=' + QUIZ_ID + '&view=quiz';
}

function escHtml(str) {
  return str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/"/g,'"');
}
</script>
<?php endif; ?>

<?php require_once __DIR__ . '/../src/includes/layout_end.php'; ?>