:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #1a202c;
  --text-sub: #4a5568;
  --text-muted: #718096;
  --primary: #2b6cb0;
  --primary-dark: #2c5282;
  --accent: #38a169;
  --danger: #e53e3e;
  --warning: #dd6b20;
  --correct: #38a169;
  --wrong: #e53e3e;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Yu Gothic", "Helvetica Neue", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: white;
  padding: 14px 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 10;
}

header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.header-actions {
  display: flex;
  gap: 8px;
}

main {
  flex: 1;
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  padding: 20px 16px 40px;
}

.mode-tabs {
  max-width: 760px;
  margin: 0 auto;
  padding: 12px 16px 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mode-tab {
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-family: inherit;
  cursor: pointer;
  transition: border-color 0.12s ease, background 0.12s ease, transform 0.1s ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.mode-tab:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
}

.mode-tab.active {
  border-color: var(--primary);
  background: var(--primary);
  color: white;
}

.mode-tab-title {
  font-weight: 700;
  font-size: 0.98rem;
}

.mode-tab-sub {
  font-size: 0.76rem;
  color: var(--text-muted);
}

.mode-tab.active .mode-tab-sub {
  color: rgba(255,255,255,0.85);
}

.screen {
  animation: fadeIn 0.25s ease-out;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.hero {
  text-align: center;
  padding: 12px 0 20px;
}

.hero-sub {
  color: var(--text-sub);
  margin: 0;
  font-size: 0.95rem;
}

h2 {
  font-size: 1.05rem;
  margin: 28px 0 12px;
  color: var(--text-sub);
  font-weight: 600;
  letter-spacing: 0.03em;
}

.stats-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  text-align: center;
}

.stat-block .stat-num {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.1;
}

.stat-block .stat-label {
  font-size: 0.78rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.chapter-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.chapter-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  text-align: left;
  cursor: pointer;
  font-family: inherit;
  font-size: 1rem;
  color: var(--text);
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.chapter-card:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.chapter-card .chapter-left {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.chapter-card .chapter-index {
  color: var(--primary);
  font-weight: 700;
  font-size: 0.78rem;
  letter-spacing: 0.08em;
}

.chapter-card .chapter-name {
  font-weight: 600;
  font-size: 1rem;
}

.chapter-card .chapter-progress {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.chapter-card .mini-bar {
  width: 90px;
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  flex-shrink: 0;
}

.chapter-card .mini-bar > div {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  transition: width 0.3s ease;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.mode-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  color: var(--text);
  transition: transform 0.12s ease, border-color 0.12s ease, box-shadow 0.12s ease;
}

.mode-card:hover {
  border-color: var(--primary);
  transform: translateY(-1px);
  box-shadow: var(--shadow-lg);
}

.mode-card:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.mode-title {
  font-weight: 600;
  font-size: 0.98rem;
  margin-bottom: 3px;
}

.mode-desc {
  font-size: 0.8rem;
  color: var(--text-muted);
}

/* ===== Quiz screen ===== */
.quiz-header {
  margin-bottom: 14px;
}

.quiz-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 0.82rem;
}

.chapter-tag {
  background: var(--primary);
  color: white;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}

.progress-text {
  color: var(--text-muted);
  font-weight: 600;
}

.progress-bar {
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
}

.progress-bar > div {
  height: 100%;
  background: linear-gradient(90deg, var(--primary), var(--accent));
  transition: width 0.3s ease;
  width: 0%;
}

.question-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px 22px;
  box-shadow: var(--shadow);
  margin-bottom: 16px;
}

.question-num {
  display: inline-block;
  background: var(--bg);
  color: var(--text-sub);
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  margin-bottom: 10px;
}

.question-text {
  font-size: 1.02rem;
  line-height: 1.8;
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
}

.question-image {
  margin: 14px 0 0;
  text-align: center;
}

.question-image img {
  max-width: 100%;
  height: auto;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
}

.question-card.has-image .question-text {
  display: none;
}

.question-card.has-image .question-image {
  margin-top: 6px;
}

.answer-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.answer-controls .ox-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.answer-controls .choice-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.answer-controls .btn-choice {
  color: var(--primary);
  font-size: 1.4rem;
}

.answer-controls .btn-choice.picked-correct-reveal {
  background: #ebf8ff;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(43,108,176,0.15);
}

.answer-controls button {
  padding: 16px 18px;
  border: 2px solid var(--border);
  background: var(--surface);
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1.6rem;
  font-weight: 700;
  cursor: pointer;
  transition: transform 0.1s ease, border-color 0.1s ease, background 0.1s ease;
}

.answer-controls button:hover:not(:disabled) {
  border-color: var(--primary);
  transform: translateY(-1px);
}

.answer-controls .btn-maru { color: var(--correct); }
.answer-controls .btn-batsu { color: var(--wrong); }

.answer-controls button.picked-correct {
  background: #f0fff4;
  border-color: var(--correct);
}

.answer-controls button.picked-wrong {
  background: #fff5f5;
  border-color: var(--wrong);
}

.answer-controls button:disabled {
  cursor: default;
  opacity: 0.5;
}

.answer-controls .btn-reveal {
  width: 100%;
  padding: 14px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.1s ease;
}

.answer-controls .btn-reveal:hover {
  background: var(--primary-dark);
}

.explanation-card {
  background: var(--surface);
  border-left: 4px solid var(--primary);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  animation: slideDown 0.3s ease-out;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.answer-line {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.label {
  display: inline-block;
  background: var(--primary);
  color: white;
  padding: 2px 10px;
  border-radius: 4px;
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.05em;
}

.answer-mark {
  font-size: 1.3rem;
  font-weight: 700;
  color: var(--text);
}

.explanation p {
  margin: 10px 0 0;
  line-height: 1.8;
  white-space: pre-wrap;
  word-break: break-word;
}

.grade-row {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.grade-prompt {
  margin: 0 0 10px;
  font-size: 0.88rem;
  color: var(--text-sub);
}

.grade-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.btn-correct, .btn-wrong {
  padding: 12px;
  border-radius: var(--radius);
  border: none;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.1s ease;
}

.btn-correct {
  background: var(--correct);
  color: white;
}

.btn-wrong {
  background: var(--wrong);
  color: white;
}

.btn-correct:hover, .btn-wrong:hover { opacity: 0.88; }

.next-row {
  margin-top: 14px;
  display: flex;
  justify-content: flex-end;
}

.btn-primary {
  padding: 12px 22px;
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 0.98rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.1s ease;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-ghost {
  padding: 8px 14px;
  background: transparent;
  color: inherit;
  border: 1px solid rgba(255,255,255,0.35);
  border-radius: 8px;
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.1s ease;
}

header .btn-ghost:hover { background: rgba(255,255,255,0.15); }

#result .btn-ghost {
  color: var(--text-sub);
  border-color: var(--border);
}
#result .btn-ghost:hover { background: var(--bg); }

/* ===== Result ===== */
.result-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 30px 20px;
  text-align: center;
  box-shadow: var(--shadow);
}

.result-big {
  font-size: 2.6rem;
  font-weight: 700;
  color: var(--primary);
  line-height: 1.1;
  margin-bottom: 8px;
}

.result-detail {
  color: var(--text-sub);
  font-size: 0.98rem;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 18px;
}

footer {
  text-align: center;
  padding: 16px;
  font-size: 0.76rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* ===== Responsive ===== */
@media (max-width: 560px) {
  main { padding: 16px 12px 28px; }
  .question-card, .explanation-card { padding: 16px 14px; }
  .question-text { font-size: 0.98rem; }
  .stats-card { padding: 14px; }
  .stat-block .stat-num { font-size: 1.35rem; }
  .mode-grid { grid-template-columns: 1fr; }
  .result-actions { grid-template-columns: 1fr; }
}
