:root {
  --bg: #f4f1ea;
  --ink: #1a1a1a;
  --muted: #6b645a;
  --card: #fffdf8;
  --accent: #b33a2f;
  --accent-2: #1f4d63;
  --ok: #2f6b4f;
  --bad: #9b2c2c;
  --line: #ddd4c6;
  --shadow: 0 18px 50px rgba(26, 26, 26, 0.08);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Source Han Serif SC", "Noto Serif SC", "Songti SC", serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(179, 58, 47, 0.08), transparent 30%),
    radial-gradient(circle at bottom right, rgba(31, 77, 99, 0.08), transparent 35%),
    var(--bg);
  min-height: 100vh;
  padding-bottom: env(safe-area-inset-bottom, 0);
}

.app {
  max-width: 980px;
  margin: 0 auto;
  padding: 16px 14px 88px;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: flex-start;
  margin-bottom: 24px;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  letter-spacing: 0.12em;
  font-size: 12px;
  text-transform: uppercase;
}

h1, h2 {
  margin: 0;
  font-weight: 700;
}

.stats {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
  min-width: 220px;
  box-shadow: var(--shadow);
  font-size: 14px;
  line-height: 1.7;
}

.controls, .card, .progress-panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.controls {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  padding: 18px;
  margin-bottom: 18px;
  align-items: end;
}

.control-group label {
  display: block;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
}

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
  font: inherit;
}

.btn.active, .btn.primary {
  background: var(--accent-2);
  color: #fff;
  border-color: var(--accent-2);
}

.btn.primary { width: 100%; }

.btn.success { background: var(--ok); color: #fff; border-color: var(--ok); }
.btn.danger { background: var(--bad); color: #fff; border-color: var(--bad); }

select {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  font: inherit;
}

.card { padding: 22px; margin-bottom: 18px; }

.card-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.tag {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(31, 77, 99, 0.08);
  color: var(--accent-2);
}

.tag.school-tag {
  background: rgba(120, 90, 50, 0.14);
  color: #6b4f2a;
  font-weight: 600;
}

.tag.module-tag {
  background: rgba(179, 58, 47, 0.12);
  color: var(--accent);
  font-weight: 600;
}

.tag.branch-tag {
  background: rgba(31, 77, 99, 0.12);
  color: var(--accent-2);
  font-weight: 600;
}

.tag.topic-name {
  background: rgba(47, 107, 79, 0.12);
  color: var(--ok);
  font-weight: 600;
}

.nav-group {
  grid-column: 1 / -1;
}

.nav-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nav-row .btn.primary {
  width: auto;
  flex: 1;
}

.nav-position {
  min-width: 88px;
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.card-body {
  font-size: 18px;
  line-height: 1.8;
  white-space: pre-wrap;
}

.card-body img {
  width: 100%;
  border-radius: 12px;
  border: 1px solid var(--line);
  margin-top: 12px;
}

.options {
  margin-top: 10px;
  display: grid;
  gap: 10px;
}

.option-hint {
  margin: 14px 0 0;
  color: var(--muted);
  font-size: 14px;
}

.option {
  width: 100%;
  text-align: left;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 14px 16px;
  border: 2px solid var(--line);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
  font: inherit;
  line-height: 1.6;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}

.option:hover:not(:disabled) {
  border-color: var(--accent-2);
  background: rgba(31, 77, 99, 0.04);
  transform: translateY(-1px);
}

.option:active:not(:disabled) {
  transform: translateY(0);
}

.option:disabled {
  cursor: default;
}

.option-label {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  background: rgba(31, 77, 99, 0.1);
  color: var(--accent-2);
}

.option-text {
  flex: 1;
  white-space: pre-wrap;
}

.option.selected {
  border-color: var(--accent-2);
  background: rgba(31, 77, 99, 0.08);
}

.option.correct {
  border-color: var(--ok);
  background: rgba(47, 107, 79, 0.12);
}

.option.correct .option-label {
  background: var(--ok);
  color: #fff;
}

.option.wrong {
  border-color: var(--bad);
  background: rgba(155, 44, 44, 0.1);
}

.option.wrong .option-label {
  background: var(--bad);
  color: #fff;
}

.sub-question {
  padding: 10px 0;
  border-bottom: 1px dashed var(--line);
}

.sub-questions {
  margin-top: 16px;
}

.stem {
  white-space: pre-wrap;
}

.card-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 20px;
}

.answer-panel {
  margin-top: 18px;
  padding: 16px;
  border-radius: 14px;
  background: rgba(31, 77, 99, 0.06);
  border: 1px solid rgba(31, 77, 99, 0.16);
  line-height: 1.8;
}

.answer-panel.correct-panel {
  background: rgba(47, 107, 79, 0.1);
  border-color: rgba(47, 107, 79, 0.22);
}

.answer-panel.wrong-panel {
  background: rgba(155, 44, 44, 0.08);
  border-color: rgba(155, 44, 44, 0.2);
}

.explanation-wrap {
  margin-top: 14px;
}

.explanation {
  margin-top: 12px;
  white-space: pre-wrap;
  font-size: 15px;
  color: var(--ink);
  line-height: 1.85;
}

.explain-block {
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid rgba(0, 0, 0, 0.06);
}

.explain-block h4 {
  margin: 0 0 10px;
  font-size: 14px;
  color: var(--accent-2);
  letter-spacing: 0.04em;
}

.explain-text {
  white-space: pre-wrap;
  line-height: 1.85;
  font-size: 15px;
}

.answer-pages {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fff;
}

.answer-pages summary {
  cursor: pointer;
  color: var(--accent);
  font-size: 14px;
  font-weight: 600;
}

.answer-page-images img {
  width: 100%;
  margin-top: 10px;
  border-radius: 10px;
  border: 1px solid var(--line);
}

.hidden { display: none; }

.progress-panel { padding: 18px 22px; }

.progress-bar {
  height: 10px;
  background: #ece4d7;
  border-radius: 999px;
  overflow: hidden;
  margin: 12px 0;
}

.progress-fill {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--accent), #d66a4d);
  transition: width 0.25s ease;
}

.loading { color: var(--muted); }

.muted { color: var(--muted); }

.source-image {
  margin-top: 16px;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fff;
}

.source-image summary {
  cursor: pointer;
  color: var(--accent-2);
  font-size: 14px;
}

.filter-toggle {
  display: none;
  width: 100%;
  margin-bottom: 12px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--card);
  color: var(--accent-2);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
}

.filter-toggle .chevron {
  float: right;
  transition: transform 0.2s ease;
}

.filter-toggle.open .chevron {
  transform: rotate(180deg);
}

.mobile-bar {
  display: none;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0));
  background: rgba(255, 253, 248, 0.96);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(8px);
  gap: 8px;
  align-items: center;
  grid-template-columns: 1fr auto 1fr;
}

.mbtn {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  border-radius: 12px;
  padding: 14px 10px;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  min-height: 48px;
}

.mbtn.primary {
  background: var(--accent-2);
  color: #fff;
  border-color: var(--accent-2);
}

.mbtn:disabled {
  opacity: 0.45;
  cursor: default;
}

.mbtn-position {
  text-align: center;
  font-size: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 64px;
}

.stats {
  flex: 1;
  min-width: 0;
}

@media (max-width: 720px) {
  .topbar {
    flex-direction: row;
    align-items: stretch;
    gap: 10px;
  }

  .brand h1 {
    font-size: 1.25rem;
  }

  .stats {
    min-width: 0;
    font-size: 12px;
    padding: 10px 12px;
  }

  .filter-toggle {
    display: block;
  }

  .controls {
    display: grid;
    gap: 12px;
    padding: 14px;
    margin-bottom: 12px;
    overflow: hidden;
    transition: max-height 0.25s ease, opacity 0.2s ease, padding 0.2s ease, margin 0.2s ease;
    max-height: 2000px;
    opacity: 1;
  }

  .controls.collapsed {
    max-height: 0;
    opacity: 0;
    padding-top: 0;
    padding-bottom: 0;
    margin-bottom: 0;
    border-width: 0;
    pointer-events: none;
  }

  .desktop-nav {
    display: none;
  }

  .mobile-bar {
    display: grid;
  }

  .card {
    padding: 16px;
  }

  .card-body {
    font-size: 16px;
    line-height: 1.75;
  }

  .card-actions {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    gap: 8px;
  }

  .card-actions .btn {
    padding: 14px 8px;
    min-height: 48px;
    font-size: 15px;
  }

  .option {
    padding: 16px;
    min-height: 52px;
  }

  .btn {
    min-height: 44px;
  }

  select {
    min-height: 48px;
    font-size: 16px;
  }

  .tag {
    font-size: 11px;
  }

  .progress-panel {
    margin-bottom: 12px;
  }
}