:root {
  --bg: #f5f8f4;
  --surface: #ffffff;
  --surface-alt: #edf4ef;
  --ink: #172620;
  --ink-muted: #5c6d64;
  --border: #dbe6dd;

  --teal: #108474;
  --teal-deep: #0b6a5d;
  --teal-tint: #e3f3ef;
  --orange: #f97316;
  --orange-deep: #c2570f;
  --orange-tint: #fef1e6;

  --success: #2f9e64;
  --success-tint: #e8f7ee;
  --danger: #c0392b;
  --danger-tint: #fbeceb;

  --shadow: 0 1px 2px rgba(23, 38, 32, 0.04), 0 8px 24px -8px rgba(23, 38, 32, 0.12);
  --radius: 14px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #10170f;
    --surface: #182219;
    --surface-alt: #1d2a1f;
    --ink: #e9f2ea;
    --ink-muted: #93a89b;
    --border: #2b3a2e;

    --teal: #3bb6a1;
    --teal-deep: #108474;
    --teal-tint: #16332c;
    --orange: #fb923c;
    --orange-deep: #f97316;
    --orange-tint: #362413;

    --success: #45c17f;
    --success-tint: #163325;
    --danger: #e8746a;
    --danger-tint: #3a1e1c;

    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 10px 28px -10px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: 'Nunito', system-ui, -apple-system, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

h1, h2 {
  font-family: 'Fraunces', Georgia, 'Times New Roman', serif;
  text-wrap: balance;
  line-height: 1.2;
}

.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: inset 0 -3px 0 var(--teal);
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 14px;
  position: sticky;
  top: 0;
  z-index: 10;
}

.app-header img {
  height: 36px;
  display: block;
}

.back-nav-btn {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--teal-deep);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.back-nav-btn:hover { background: var(--teal-tint); border-color: var(--teal); }
.back-nav-btn[hidden] { display: none; }

.app-header .school-name {
  margin-left: auto;
  font-weight: 700;
  color: var(--teal-deep);
  font-size: 0.95rem;
  text-align: right;
  overflow-wrap: break-word;
}

@media (max-width: 480px) {
  .app-header {
    flex-wrap: wrap;
  }
  .app-header .school-name {
    margin-left: 0;
    width: 100%;
    text-align: left;
    font-size: 0.88rem;
  }
}

main {
  max-width: 640px;
  margin: 0 auto;
  padding: 28px 16px 64px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 26px 24px;
  box-shadow: var(--shadow);
}

h1 {
  font-size: 1.6rem;
  font-weight: 600;
  color: var(--teal-deep);
  margin: 0 0 6px;
}

h2 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--ink);
  margin: 0 0 6px;
}

p { color: var(--ink-muted); line-height: 1.55; margin: 0 0 12px; }
p:last-child { margin-bottom: 0; }

.topic-list {
  display: grid;
  gap: 10px;
  margin: 4px 0 20px;
}
.topic-list:last-child { margin-bottom: 0; }

.grade-group + .grade-group { margin-top: 4px; }

.grade-select-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  gap: 10px;
  margin: 18px 0 4px;
}

.grade-select-btn {
  padding: 20px 12px;
  border-radius: 12px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  font-family: 'Fraunces', Georgia, serif;
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--teal-deep);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}
.grade-select-btn:hover { border-color: var(--orange); background: var(--orange-tint); }
.grade-select-btn:active { transform: scale(0.97); }
.grade-select-btn:focus-visible { outline: 2px solid var(--teal); outline-offset: 2px; }

.grade-badge {
  display: inline-block;
  font-family: 'Fraunces', Georgia, serif;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--teal-deep);
  background: var(--teal-tint);
  padding: 4px 14px;
  border-radius: 999px;
}

.topic-btn {
  display: block;
  width: 100%;
  text-align: left;
  padding: 15px 18px;
  border-radius: 10px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  color: var(--ink);
  cursor: pointer;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}

.topic-btn:hover { border-color: var(--orange); background: var(--orange-tint); }
.topic-btn:active { transform: scale(0.99); }

label {
  display: block;
  font-weight: 700;
  margin: 16px 0 6px;
  font-size: 0.9rem;
  color: var(--ink);
}
label:first-of-type { margin-top: 4px; }

input[type="text"], input[type="number"] {
  width: 100%;
  padding: 11px 13px;
  border-radius: 9px;
  border: 1.5px solid var(--border);
  background: var(--surface);
  color: var(--ink);
  font-family: inherit;
  font-size: 1rem;
}
input[type="text"]:focus, input[type="number"]:focus {
  outline: 2px solid var(--teal);
  outline-offset: 1px;
  border-color: var(--teal);
}

button { font-family: inherit; }
button:focus-visible,
.topic-btn:focus-visible,
input:focus-visible {
  outline: 2px solid var(--teal);
  outline-offset: 2px;
}

.primary-btn {
  background: var(--orange);
  color: #fff;
  border: none;
  padding: 13px 26px;
  border-radius: 9px;
  font-size: 1rem;
  font-weight: 800;
  cursor: pointer;
  margin-top: 20px;
  transition: background 0.15s ease;
}

.primary-btn:hover { background: var(--orange-deep); }
.primary-btn:disabled { background: var(--border); color: var(--ink-muted); cursor: not-allowed; }

.secondary-btn {
  background: none;
  border: 1.5px solid var(--teal);
  color: var(--teal-deep);
  padding: 11px 22px;
  border-radius: 9px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s ease;
}
.secondary-btn:hover { background: var(--teal-tint); }

.quiz-card {
  animation: question-in 0.25s ease;
}
@keyframes question-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.quiz-progress-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 18px;
}

.quiz-progress-track {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-alt);
  overflow: hidden;
}
.quiz-progress-fill {
  height: 100%;
  background: var(--teal);
  border-radius: 999px;
  transition: width 0.3s ease;
}

.quiz-progress-label {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--ink-muted);
  white-space: nowrap;
}

.quiz-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 4px;
}
.quiz-head h2 { margin: 0; }

.timer-ring {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  flex-shrink: 0;
  background: conic-gradient(var(--teal) calc(var(--pct, 100) * 1%), var(--surface-alt) 0);
  display: grid;
  place-items: center;
  transition: background 0.9s linear;
}
.timer-ring::before {
  content: "";
  position: absolute;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--surface);
}
.timer-ring { position: relative; }
.timer-text {
  position: relative;
  font-variant-numeric: tabular-nums;
  font-weight: 800;
  font-size: 0.85rem;
  color: var(--teal-deep);
}
.timer-ring.urgent { background: conic-gradient(var(--danger) calc(var(--pct, 100) * 1%), var(--surface-alt) 0); }
.timer-ring.urgent .timer-text { color: var(--danger); }

.question-block {
  padding: 20px 0;
  border-bottom: 1px solid var(--border);
}
.question-block:first-child { padding-top: 4px; }
.question-block:last-child { border-bottom: none; }
.question-block.no-border { border-bottom: none; padding-bottom: 4px; }

.question-text { font-weight: 800; margin-bottom: 12px; color: var(--ink); }

.question-image {
  display: block;
  max-width: 100%;
  max-height: 260px;
  margin: 0 auto 14px;
  border-radius: 9px;
  border: 1px solid var(--border);
}

.option-row {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 10px 12px;
  border-radius: 9px;
  cursor: pointer;
  margin-bottom: 6px;
  border: 1.5px solid transparent;
  transition: background 0.12s ease, border-color 0.12s ease;
}
.option-row:hover { background: var(--surface-alt); border-color: var(--border); }
.option-row input { cursor: pointer; accent-color: var(--teal); width: 18px; height: 18px; flex-shrink: 0; }

.score-banner {
  display: flex;
  align-items: center;
  gap: 22px;
  padding: 24px;
  border-radius: var(--radius);
  background: var(--teal-deep);
  color: #fff;
  box-shadow: var(--shadow);
}

.score-ring {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  flex-shrink: 0;
  background: conic-gradient(var(--orange) calc(var(--pct, 0) * 1%), rgba(255, 255, 255, 0.22) 0);
  display: grid;
  place-items: center;
}
.score-ring::before {
  content: "";
  width: 70px;
  height: 70px;
  border-radius: 50%;
  background: var(--teal-deep);
}
.score-ring-label {
  position: relative;
  margin-left: -70px;
  width: 70px;
  text-align: center;
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 700;
  font-size: 1.05rem;
  font-variant-numeric: tabular-nums;
}

.score-text .score-num {
  font-family: 'Fraunces', Georgia, serif;
  font-size: 2.1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}
.score-text .score-pct { font-size: 0.95rem; opacity: 0.85; margin-top: 4px; }

.review-item { padding: 16px 0; border-bottom: 1px solid var(--border); }
.review-item:first-child { padding-top: 4px; }
.review-item:last-child { border-bottom: none; }

.review-answer { padding: 8px 12px; border-radius: 7px; margin-top: 5px; font-size: 0.92rem; font-weight: 600; }
.review-answer.correct { background: var(--success-tint); color: var(--success); }
.review-answer.incorrect { background: var(--danger-tint); color: var(--danger); text-decoration: line-through; }
.review-explanation { font-size: 0.9rem; color: var(--ink-muted); margin-top: 8px; line-height: 1.5; }

.status-text { font-size: 0.86rem; color: var(--ink-muted); margin-top: 10px; }
.error-text { color: var(--danger); font-weight: 700; }

.progress-note {
  font-size: 0.85rem;
  color: var(--teal-deep);
  background: var(--teal-tint);
  display: inline-block;
  padding: 4px 10px;
  border-radius: 6px;
  margin-bottom: 6px;
  font-weight: 700;
  letter-spacing: 0.01em;
}

@media (prefers-reduced-motion: reduce) {
  .topic-btn, .primary-btn, .secondary-btn, .option-row, input, .back-nav-btn,
  .quiz-progress-fill, .timer-ring, .grade-select-btn { transition: none; }
  .quiz-card { animation: none; }
}
