:root {
  font-family: 'Space Grotesk', system-ui, -apple-system, blinkmacsystemfont, 'Segoe UI', sans-serif;
  color: #0f172a;
  background: linear-gradient(135deg, #f9fafb 0%, #e0f2fe 100%);
  min-height: 100%;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
}

.app-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem 1.5rem;
}

.brand {
  font-weight: 600;
}

.app-shell {
  padding: 1rem;
  display: flex;
  justify-content: center;
}

.card {
  background: white;
  border-radius: 18px;
  padding: 1.5rem;
  box-shadow: 0 12px 40px rgba(15, 23, 42, 0.12);
  width: min(900px, 100%);
}

.card.narrow {
  width: min(420px, 100%);
}

.card h1 {
  margin-top: 0;
}

label {
  display: block;
  font-size: 0.9rem;
  margin-bottom: 0.4rem;
  color: #475569;
}

input[type='password'],
textarea,
select {
  width: 100%;
  padding: 0.8rem;
  border-radius: 12px;
  border: 1px solid #cbd5f5;
  font-family: inherit;
  font-size: 1rem;
}

button {
  border: none;
  border-radius: 999px;
  background: #2563eb;
  color: white;
  padding: 0.8rem 1.5rem;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
}

button.ghost {
  background: transparent;
  color: #2563eb;
}

.start-button {
  background: #10b981;
  font-size: 1.1rem;
  padding: 1rem 2rem;
  margin-top: 0.5rem;
}

.start-button:hover:not(:disabled) {
  background: #059669;
}

.start-button:disabled {
  background: #9ca3af;
  cursor: not-allowed;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1rem;
}

.chat-log {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  max-height: 60vh;
  overflow-y: auto;
}

.chat-log li {
  display: flex;
}

.chat-log li.user {
  justify-content: flex-end;
}

.bubble {
  padding: 0.9rem 1.1rem;
  border-radius: 20px;
  max-width: 80%;
  line-height: 1.4;
}

.chat-log li.assistant .bubble {
  background: #eef2ff;
  border-bottom-left-radius: 4px;
}

#options-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}

.option-button {
  flex: 1 1 calc(50% - 0.5rem);
  min-width: 140px;
  padding: 0.75rem;
  border-radius: 12px;
  border: 1px solid #c7d2fe;
  background: #eef2ff;
  color: #1e293b;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s ease;
}

.option-button:hover:not(:disabled) {
  background: #ddd6fe;
  border-color: #a78bfa;
  transform: translateY(-1px);
}

.option-button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.options-panel.hidden {
  display: none;
}

.chat-log li.user .bubble {
  background: #2563eb;
  color: white;
  border-bottom-right-radius: 4px;
}

#chat-form {
  margin-top: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.form-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
}

.meta {
  font-size: 0.9rem;
  color: #475569;
}

.error {
  background: #fee2e2;
  color: #b91c1c;
  padding: 0.6rem 0.9rem;
  border-radius: 12px;
  margin-bottom: 0.8rem;
}

.status {
  font-size: 0.9rem;
  color: #0f172a;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

@media (max-width: 640px) {
  .card.chat {
    padding: 1rem;
  }

  .bubble {
    max-width: 100%;
  }
}

.feedback-separator {
  border: none;
  border-top: 2px solid #cbd5e1;
  margin: 1rem 0;
}

/* Session Setup Modal */
.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
}

.modal.show {
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-content {
  background: white;
  border-radius: 18px;
  padding: 2rem;
  max-width: 800px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

.modal-content h2 {
  margin-top: 0;
  color: #1e40af;
}

.setup-section {
  margin: 2rem 0;
}

.setup-section h3 {
  margin-bottom: 0.5rem;
  color: #334155;
}

.help-text {
  color: #64748b;
  font-size: 0.9rem;
  margin-bottom: 1rem;
}

.topics-grid {
  display: grid;
  gap: 1rem;
  margin-bottom: 1rem;
}

.subject-group {
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 1rem;
}

.subject-group h4 {
  margin: 0 0 0.75rem 0;
  color: #1e40af;
  font-size: 1rem;
  text-transform: capitalize;
}

.topic-checkboxes {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.5rem;
}

.topic-checkbox {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.topic-checkbox input[type="checkbox"] {
  width: 18px;
  height: 18px;
  cursor: pointer;
}

.topic-checkbox label {
  cursor: pointer;
  font-size: 0.9rem;
}

.btn-secondary {
  background: #e2e8f0;
  color: #334155;
  border: none;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  margin-right: 0.5rem;
}

.btn-secondary:hover {
  background: #cbd5e1;
}

.mode-options {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 1rem;
}

.mode-option {
  cursor: pointer;
}

.mode-option input[type="radio"] {
  display: none;
}

.mode-card {
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  padding: 1rem;
  transition: all 0.2s;
}

.mode-option input[type="radio"]:checked + .mode-card {
  border-color: #3b82f6;
  background: #eff6ff;
}

.mode-card:hover {
  border-color: #93c5fd;
}

.mode-card strong {
  display: block;
  margin-bottom: 0.5rem;
  color: #1e40af;
}

.mode-card p {
  margin: 0;
  font-size: 0.85rem;
  color: #64748b;
}

.progress-summary {
  background: #f1f5f9;
  border-radius: 12px;
  padding: 1rem;
  margin: 1.5rem 0;
}

.progress-summary h4 {
  margin-top: 0;
  color: #334155;
}

.progress-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 1rem;
  margin-top: 1rem;
}

.stat {
  text-align: center;
}

.stat-value {
  font-size: 2rem;
  font-weight: 700;
  color: #1e40af;
}

.stat-label {
  font-size: 0.85rem;
  color: #64748b;
  margin-top: 0.25rem;
}

/* ============================================
   PHASE 2: Gamification & Enhanced UI
   ============================================ */

/* Stats Display in Header */
.stats-display {
  display: flex;
  gap: 1.5rem;
  align-items: center;
  padding: 0.5rem 1rem;
  background: linear-gradient(135deg, #f0f9ff 0%, #e0f2fe 100%);
  border-radius: 12px;
  border: 2px solid #7dd3fc;
}

.stat-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.25rem;
}

.stat-label {
  font-size: 0.75rem;
  color: #475569;
  text-transform: uppercase;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  color: #0c4a6e;
}

/* XP Progress Animation */
@keyframes xpGain {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); color: #10b981; }
  100% { transform: scale(1); }
}

.stat-value.xp-gain {
  animation: xpGain 0.5s ease-out;
}

/* Level Up Animation */
@keyframes levelUp {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.stat-value.level-up {
  animation: levelUp 0.6s ease-in-out;
  color: #f59e0b !important;
}

/* Toggle Option Styling */
.toggle-option {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
  padding: 1rem;
  border: 2px solid #e2e8f0;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.toggle-option:hover {
  border-color: #94a3b8;
  background: #f8fafc;
}

.toggle-option input[type="checkbox"] {
  width: 48px;
  height: 24px;
  margin: 0;
  cursor: pointer;
  appearance: none;
  background: #cbd5e1;
  border-radius: 12px;
  position: relative;
  transition: background 0.3s ease;
}

.toggle-option input[type="checkbox"]::before {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  top: 2px;
  left: 2px;
  background: white;
  transition: transform 0.3s ease;
}

.toggle-option input[type="checkbox"]:checked {
  background: #10b981;
}

.toggle-option input[type="checkbox"]:checked::before {
  transform: translateX(24px);
}

.toggle-label {
  flex: 1;
}

.toggle-label strong {
  display: block;
  margin-bottom: 0.25rem;
  color: #0f172a;
}

.toggle-label .help-text {
  margin: 0;
  font-size: 0.875rem;
}

/* Celebration Confetti */
.confetti-container {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 9999;
  overflow: hidden;
}

.confetti {
  position: absolute;
  width: 10px;
  height: 10px;
  background: #f59e0b;
  animation: confettiFall 3s linear forwards;
}

@keyframes confettiFall {
  0% {
    transform: translateY(-100vh) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(100vh) rotate(720deg);
    opacity: 0;
  }
}

/* Topic Mastery Progress Bars */
.progress-bar-container {
  width: 100%;
  height: 24px;
  background: #e2e8f0;
  border-radius: 12px;
  overflow: hidden;
  position: relative;
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
  border-radius: 12px;
  transition: width 0.5s ease-out;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 0.5rem;
  color: white;
  font-size: 0.75rem;
  font-weight: 700;
}

.progress-bar.in-progress {
  background: linear-gradient(90deg, #f59e0b 0%, #d97706 100%);
}

.progress-bar.not-started {
  background: linear-gradient(90deg, #ef4444 0%, #dc2626 100%);
  width: 5% !important;
}

/* Mastery Badge */
.mastery-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.25rem 0.75rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.mastery-badge.mastered {
  background: #d1fae5;
  color: #065f46;
}

.mastery-badge.in-progress {
  background: #fef3c7;
  color: #92400e;
}

.mastery-badge.not-started {
  background: #fee2e2;
  color: #991b1b;
}

/* Mobile Responsive */
@media (max-width: 640px) {
  .stats-display {
    gap: 0.75rem;
    padding: 0.5rem;
  }

  .stat-label {
    font-size: 0.65rem;
  }

  .stat-value {
    font-size: 1rem;
  }
}

/* Topic Selection Progress Bars */
.topic-label-wrapper {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.topic-checkbox {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.5rem;
}

.topic-checkbox input[type="checkbox"] {
  margin-top: 0.25rem;
  flex-shrink: 0;
}

.topic-checkbox label {
  font-weight: 500;
  color: #0f172a;
  margin: 0;
}

.progress-bar-container {
  margin-top: 0.25rem;
}

/* Open-ended test mode */
.topic-group { margin: 0.5rem 0 1rem; }
.topic-group h4 { margin: 0.25rem 0; text-transform: capitalize; }
.topic-group ul { list-style: none; padding: 0; margin: 0; }
.topic-group li { padding: 0.15rem 0; }

.commissioned-list { list-style: none; padding: 0; }
.commissioned-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.5rem;
  border: 1px solid rgba(0,0,0,0.08);
  border-radius: 6px;
  margin-bottom: 0.5rem;
  flex-wrap: wrap;
}
.commissioned-list .meta { font-size: 0.85em; opacity: 0.75; }

.results-list { list-style: none; padding: 0; }
.results-list li {
  padding: 0.75rem;
  border: 1px solid rgba(0,0,0,0.08);
  border-radius: 6px;
  margin-bottom: 0.75rem;
}
.result-question, .result-answer, .result-mark { margin: 0.25rem 0; }
.result-mark { font-weight: 600; }

/* Recent marks panel */
.recent-marks-list { list-style: none; padding: 0; margin: 0; }
.recent-mark {
  padding: 0.5rem;
  border-left: 3px solid #ccc;
  margin-bottom: 0.4rem;
  background: rgba(0,0,0,0.02);
}
.recent-mark-correct { border-left-color: #4caf50; }
.recent-mark-partial { border-left-color: #ff9800; }
.recent-mark-incorrect { border-left-color: #e53935; }
.recent-mark-line {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.recent-mark-q { flex: 1; min-width: 0; }
.recent-mark-detail {
  margin-top: 0.4rem;
  padding-top: 0.4rem;
  border-top: 1px solid rgba(0,0,0,0.1);
  font-size: 0.92em;
}
.recent-mark-detail > div { margin: 0.25rem 0; }
.recent-mark .badge { font-weight: 600; white-space: nowrap; }

/* Test-mode picker (setup) */
.mode-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.4rem;
}
.mode-picker label {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem 0.7rem;
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 6px;
  cursor: pointer;
}

/* MCQ option buttons */
#mcq-options {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin: 0.75rem 0;
}
.mcq-option {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  text-align: left;
  padding: 0.7rem 0.9rem;
  border: 1px solid rgba(0,0,0,0.12);
  background: #fff;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}
.mcq-option:hover { background: rgba(0,0,0,0.04); }
.mcq-letter {
  font-weight: 700;
  background: rgba(0,0,0,0.06);
  padding: 0.05rem 0.45rem;
  border-radius: 4px;
  flex: 0 0 auto;
}
.mcq-text { flex: 1; }
