/* ===== Design Tokens ===== */
:root {
  --bg-primary: #0a0f1a;
  --bg-secondary: #111827;
  --bg-card: #1a2332;
  --bg-card-hover: #1f2b3d;
  --bg-glass: rgba(26, 35, 50, 0.85);
  --surface: #243044;
  --border: rgba(255,255,255,0.08);
  --border-light: rgba(255,255,255,0.12);

  --text-primary: #f0f4f8;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;

  --green-400: #4ade80;
  --green-500: #22c55e;
  --green-600: #16a34a;
  --green-glow: rgba(34, 197, 94, 0.25);
  --gradient-green: linear-gradient(135deg, #22c55e 0%, #059669 50%, #0d9488 100%);
  --gradient-hero: linear-gradient(135deg, #0a2e1a 0%, #0a1628 40%, #1a0a28 100%);

  --red-400: #f87171;
  --red-500: #ef4444;
  --yellow-400: #fbbf24;
  --blue-400: #60a5fa;
  --blue-500: #3b82f6;
  --purple-400: #a78bfa;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;

  --shadow-sm: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.4);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
  --shadow-glow: 0 0 30px var(--green-glow);

  --font-sans: 'Inter', 'Noto Sans TC', system-ui, -apple-system, sans-serif;
  --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { scroll-behavior: smooth; }
body {
  font-family: var(--font-sans);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; cursor: pointer; border: none; }
select { font-family: inherit; }
input { font-family: inherit; }

/* ===== Nickname Modal ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  background: rgba(0,0,0,0.7);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  animation: fadeIn 0.3s ease;
}
.modal-overlay.hidden { display: none; }
.modal-card {
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-xl);
  padding: 40px 36px;
  max-width: 420px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow-lg);
  animation: slideUp 0.4s ease;
}
.modal-icon { font-size: 48px; margin-bottom: 16px; }
.modal-card h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 8px;
}
.modal-card > p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
}
.nickname-input-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.nickname-input-group input {
  background: var(--surface);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  font-size: 16px;
  color: var(--text-primary);
  text-align: center;
  transition: var(--transition);
}
.nickname-input-group input::placeholder { color: var(--text-muted); }
.nickname-input-group input:focus {
  outline: none;
  border-color: var(--green-500);
  box-shadow: 0 0 0 3px var(--green-glow);
}
.modal-hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 16px;
}

/* ===== User Button (header) ===== */
.user-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface) !important;
  border: 1px solid var(--border-light) !important;
  padding: 6px 12px !important;
  border-radius: 99px !important;
  font-size: 13px !important;
  color: var(--text-primary) !important;
  cursor: pointer;
}
.user-btn:hover { border-color: var(--green-500) !important; }
.user-avatar { font-size: 16px; }

/* ===== Animations ===== */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes slideUp {
  from { opacity: 0; transform: translateY(24px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 20px var(--green-glow); }
  50% { box-shadow: 0 0 40px var(--green-glow), 0 0 60px rgba(34,197,94,0.1); }
}
@keyframes spin {
  to { transform: rotate(360deg); }
}
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-6px); }
  75% { transform: translateX(6px); }
}
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); }
}

/* ===== Header ===== */
#header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--bg-glass);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}
.header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}
.logo-icon { font-size: 28px; }
.logo-text {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.02em;
}
.logo-sub {
  display: inline-block;
  margin-left: 8px;
  font-size: 12px;
  font-weight: 500;
  color: var(--green-400);
  background: rgba(34,197,94,0.12);
  padding: 2px 8px;
  border-radius: 99px;
  vertical-align: middle;
}
.header-nav { display: flex; gap: 4px; }
.nav-btn {
  background: transparent;
  color: var(--text-secondary);
  padding: 8px 16px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  transition: var(--transition);
}
.nav-btn:hover { color: var(--text-primary); background: var(--surface); }
.nav-btn.active {
  color: var(--green-400);
  background: rgba(34,197,94,0.1);
}

/* ===== Views ===== */
.view { display: none; }
.view.active { display: block; animation: fadeIn 0.3s ease; }

/* ===== Hero ===== */
.hero {
  background: var(--gradient-hero);
  padding: 60px 24px 48px;
  text-align: center;
  position: relative;
  overflow: hidden;
}
.hero::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle at 30% 40%, rgba(34,197,94,0.08) 0%, transparent 50%),
              radial-gradient(circle at 70% 60%, rgba(59,130,246,0.06) 0%, transparent 40%);
  pointer-events: none;
}
.hero-content { position: relative; z-index: 1; max-width: 800px; margin: 0 auto; }
.hero h1 {
  font-size: clamp(28px, 5vw, 42px);
  font-weight: 800;
  letter-spacing: -0.03em;
  margin-bottom: 12px;
  background: linear-gradient(135deg, #f0f4f8, #94a3b8);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}
.hero-desc {
  font-size: 16px;
  color: var(--text-secondary);
  margin-bottom: 32px;
}
.hero-stats {
  display: flex;
  justify-content: center;
  gap: 24px;
  flex-wrap: wrap;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px 32px;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 120px;
  transition: var(--transition);
}
.stat-card:hover {
  border-color: var(--green-500);
  box-shadow: var(--shadow-glow);
  transform: translateY(-2px);
}
.stat-num {
  font-size: 28px;
  font-weight: 800;
  color: var(--green-400);
}
.stat-label {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ===== Mode Selection ===== */
.mode-selection {
  max-width: 1100px;
  margin: 0 auto;
  padding: 48px 24px 64px;
}
.mode-selection h2 {
  font-size: 22px;
  font-weight: 700;
  margin-bottom: 24px;
  color: var(--text-primary);
}
.mode-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 20px;
}
.mode-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  transition: var(--transition);
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.mode-card:hover {
  border-color: var(--border-light);
  transform: translateY(-3px);
  box-shadow: var(--shadow-md);
}
.mode-icon { font-size: 36px; }
.mode-card h3 { font-size: 18px; font-weight: 700; }
.mode-card p { font-size: 14px; color: var(--text-secondary); line-height: 1.5; }
.mode-options {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 4px;
}

/* Subject toggles */
.subject-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
}
.subject-toggle input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--green-500);
  cursor: pointer;
}

/* Select */
select {
  background: var(--surface);
  color: var(--text-primary);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  font-size: 13px;
}
select:focus { outline: 2px solid var(--green-500); outline-offset: 2px; }

.wrong-count {
  font-size: 14px;
  color: var(--text-secondary);
}
.wrong-count span {
  font-weight: 700;
  color: var(--yellow-400);
}

/* ===== Buttons ===== */
.btn-primary {
  background: var(--gradient-green);
  color: #fff;
  padding: 12px 24px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 600;
  transition: var(--transition);
  margin-top: auto;
}
.btn-primary:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-glow);
  filter: brightness(1.1);
}
.btn-primary:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
  filter: none;
  box-shadow: none;
}
.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-weight: 500;
  border: 1px solid var(--border-light);
  transition: var(--transition);
}
.btn-ghost:hover {
  background: var(--surface);
  color: var(--text-primary);
}
.btn-ghost.danger:hover {
  background: rgba(239,68,68,0.1);
  color: var(--red-400);
  border-color: var(--red-400);
}

/* ===== Quiz View ===== */
.quiz-progress {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px 24px 0;
}
.progress-bar {
  height: 6px;
  background: var(--surface);
  border-radius: 99px;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  background: var(--gradient-green);
  border-radius: 99px;
  transition: width 0.4s ease;
  width: 0%;
}
.progress-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 10px;
  font-size: 13px;
  color: var(--text-muted);
}
.timer {
  font-weight: 700;
  font-size: 16px;
  color: var(--yellow-400);
  font-variant-numeric: tabular-nums;
}
.timer.danger { color: var(--red-400); animation: pulse-glow 1s infinite; }

/* ===== Question Card ===== */
.question-card {
  max-width: 800px;
  margin: 24px auto 0;
  padding: 0 24px;
  animation: slideUp 0.35s ease;
}
.question-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}
.badge {
  display: inline-block;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 99px;
  background: rgba(34,197,94,0.12);
  color: var(--green-400);
}
.badge-outline {
  background: transparent;
  border: 1px solid var(--border-light);
  color: var(--text-muted);
}
.q-number {
  margin-left: auto;
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}
.question-text {
  font-size: 18px;
  font-weight: 600;
  line-height: 1.7;
  margin-bottom: 24px;
  color: var(--text-primary);
}

/* Options */
.options-list { display: flex; flex-direction: column; gap: 10px; }
.option-btn {
  width: 100%;
  text-align: left;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 20px;
  font-size: 15px;
  color: var(--text-primary);
  transition: var(--transition);
  display: flex;
  align-items: flex-start;
  gap: 12px;
  line-height: 1.5;
}
.option-btn:hover:not(.disabled) {
  border-color: var(--green-500);
  background: var(--bg-card-hover);
  transform: translateX(4px);
}
.option-key {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-secondary);
  transition: var(--transition);
}
.option-btn:hover:not(.disabled) .option-key {
  background: var(--green-500);
  color: #fff;
}
.option-btn.correct {
  border-color: var(--green-500);
  background: rgba(34,197,94,0.08);
  animation: pop 0.3s ease;
}
.option-btn.correct .option-key {
  background: var(--green-500);
  color: #fff;
}
.option-btn.wrong {
  border-color: var(--red-500);
  background: rgba(239,68,68,0.08);
  animation: shake 0.4s ease;
}
.option-btn.wrong .option-key {
  background: var(--red-500);
  color: #fff;
}
.option-btn.disabled { pointer-events: none; opacity: 0.7; }
.option-btn.disabled.neutral { opacity: 0.5; }

/* ===== Feedback Panel ===== */
.feedback-panel {
  max-width: 800px;
  margin: 20px auto 40px;
  padding: 0 24px;
  animation: slideUp 0.35s ease;
}
.feedback-header {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 700;
  margin-bottom: 12px;
}
.feedback-icon { font-size: 24px; }
.feedback-correct {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 16px;
}
.feedback-correct strong { color: var(--green-400); }

.explanation {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 20px;
  margin-bottom: 16px;
}
.explanation h4 {
  font-size: 14px;
  margin-bottom: 8px;
  color: var(--text-secondary);
}
.explanation p {
  font-size: 14px;
  line-height: 1.7;
  color: var(--text-primary);
}

.ai-analysis {
  background: linear-gradient(135deg, rgba(99,102,241,0.08), rgba(34,197,94,0.06));
  border: 1px solid rgba(99,102,241,0.2);
  border-radius: var(--radius-md);
  padding: 20px;
  margin-bottom: 16px;
}
.ai-analysis h4 {
  font-size: 14px;
  margin-bottom: 10px;
  color: var(--purple-400);
}
.ai-text {
  font-size: 14px;
  line-height: 1.8;
  color: var(--text-primary);
  white-space: pre-wrap;
}
.ai-loading {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text-muted);
}
.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--purple-400);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.feedback-actions {
  display: flex;
  gap: 12px;
  margin-top: 8px;
}

/* ===== Exam Result ===== */
.exam-result {
  max-width: 700px;
  margin: 40px auto;
  padding: 0 24px;
  animation: slideUp 0.4s ease;
}
.result-header {
  text-align: center;
  margin-bottom: 32px;
}
.result-header h2 {
  font-size: 24px;
  margin-bottom: 20px;
}
.result-score {
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 4px;
}
.score-num {
  font-size: 64px;
  font-weight: 800;
  color: var(--green-400);
  line-height: 1;
}
.score-total {
  font-size: 24px;
  color: var(--text-muted);
}
.result-percent {
  font-size: 20px;
  font-weight: 700;
  margin-top: 8px;
  color: var(--text-secondary);
}
.result-status {
  font-size: 16px;
  color: var(--text-muted);
  margin-top: 8px;
}
.result-details {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: 400px;
  overflow-y: auto;
  margin-bottom: 24px;
}
.result-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 13px;
}
.result-item.correct-item { border-left: 3px solid var(--green-500); }
.result-item.wrong-item { border-left: 3px solid var(--red-500); }
.result-item .ri-num { color: var(--text-muted); min-width: 30px; }
.result-item .ri-text { flex: 1; color: var(--text-primary); }
.result-item .ri-answer { font-weight: 600; }
.result-item .ri-answer.ri-correct { color: var(--green-400); }
.result-item .ri-answer.ri-wrong { color: var(--red-400); }
.result-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
}

/* ===== Stats ===== */
.stats-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 40px 24px 64px;
}
.stats-container h2 {
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 24px;
}
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.stats-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 24px;
}
.stats-card.full-width { grid-column: 1 / -1; }
.stats-card h3 {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
  margin-bottom: 8px;
}
.stats-value {
  font-size: 32px;
  font-weight: 800;
  color: var(--green-400);
}
.wrong-list {
  max-height: 400px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}
.wrong-item {
  background: var(--surface);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 13px;
  line-height: 1.5;
  color: var(--text-primary);
  border-left: 3px solid var(--red-400);
}
.wrong-item .wi-meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 4px;
}
.empty-state {
  color: var(--text-muted);
  font-size: 14px;
  text-align: center;
  padding: 20px;
}

/* ===== Utilities ===== */
.hidden { display: none !important; }

/* ===== Responsive ===== */
@media (max-width: 640px) {
  .header-inner { padding: 0 16px; }
  .logo-text { font-size: 15px; }
  .logo-sub { display: none; }
  .nav-btn { padding: 6px 10px; font-size: 13px; }
  .hero { padding: 40px 16px 32px; }
  .hero-stats { gap: 12px; }
  .stat-card { padding: 14px 20px; min-width: 90px; }
  .stat-num { font-size: 22px; }
  .mode-selection { padding: 32px 16px 48px; }
  .question-card, .feedback-panel { padding: 0 16px; }
  .question-text { font-size: 16px; }
  .option-btn { padding: 14px 16px; font-size: 14px; }
  .feedback-actions { flex-direction: column; }
  .score-num { font-size: 48px; }
}
