:root {
  --blue-dark: #0d2b4e;
  --blue: #1a63ad;
  --blue-light: #eaf2fb;
  --bg: #f2f4f7;
  --card-bg: #ffffff;
  --green: #1f8a4c;
  --green-bg: #e6f6ec;
  --red: #c0392b;
  --red-bg: #fbe9e7;
  --gray: #6b7684;
  --border: #dfe4ea;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior-y: none;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  background: var(--bg);
  color: #10151b;
  display: flex;
  flex-direction: column;
}

.topbar {
  background: var(--blue-dark);
  color: white;
  padding: calc(12px + var(--safe-top)) 16px 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.topbar h1 {
  font-size: clamp(16px, 4.5vw, 19px);
  margin: 0;
  font-weight: 700;
}

.global-stats {
  font-size: 13px;
  font-weight: 600;
  background: rgba(255,255,255,0.12);
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

main {
  flex: 1;
  display: flex;
  min-height: 0;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}

.screen {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  width: 100%;
}

.screen[hidden] { display: none; }

.scroll-area {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 16px 16px 12px;
}

h2 {
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--gray);
  font-weight: 700;
  margin: 20px 0 10px;
}

.scroll-area > h2:first-child { margin-top: 4px; }

/* ---------- Chapter cards ---------- */

.chapter-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.chapter-row {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  user-select: none;
  transition: border-color 0.15s, background 0.15s, opacity 0.15s;
}

.chapter-row:active { background: var(--blue-light); }

.chapter-row:has(.chapter-checkbox:not(:checked)) {
  opacity: 0.55;
}

.chapter-checkbox {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.checkbox-visual {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  border: 2px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, border-color 0.15s;
}

/* Single source of truth for the checked/correct/incorrect color: every
   trigger below sets the same var(--check-color), so cascade specificity
   between them can't cause a stale color to win. */
.chapter-row, .scope-option, .answer-option { --check-color: var(--blue-dark); }
.answer-option.correct { --check-color: var(--green); }
.answer-option.incorrect { --check-color: var(--red); }

/* Border reacts to checked OR correct/incorrect (so a missed correct
   answer still gets a colored ring). Background fill is checked-only —
   a correct answer you *didn't* pick must stay visually empty, otherwise
   it's indistinguishable from one you actually selected. */
.chapter-checkbox:checked + .checkbox-visual,
.answer-option input[type="checkbox"]:checked + .checkbox-visual,
.answer-option.correct .checkbox-visual,
.answer-option.incorrect .checkbox-visual {
  border-color: var(--check-color);
}

.chapter-checkbox:checked + .checkbox-visual,
.answer-option input[type="checkbox"]:checked + .checkbox-visual {
  background: var(--check-color);
}

.checkbox-visual::after {
  content: '';
  width: 6px;
  height: 11px;
  border: solid white;
  border-width: 0 2.5px 2.5px 0;
  transform: rotate(45deg) scale(0);
  transition: transform 0.1s;
  margin-bottom: 2px;
}

.chapter-checkbox:checked + .checkbox-visual::after,
.answer-option input[type="checkbox"]:checked + .checkbox-visual::after {
  transform: rotate(45deg) scale(1);
}

.chapter-checkbox:focus-visible + .checkbox-visual {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

.chapter-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.chapter-title {
  font-size: 14.5px;
  line-height: 1.3;
  font-weight: 500;
}

.chapter-digit { font-weight: 700; color: var(--blue); margin-right: 5px; }

.chapter-counts {
  font-size: 12px;
  color: var(--gray);
}

.chapter-counts .correct { color: var(--green); font-weight: 700; }
.chapter-counts .wrong { color: var(--red); font-weight: 700; }

.chapter-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 11.5px;
  color: var(--gray);
  margin: 0 0 10px;
}

.chapter-legend span::before {
  content: '●';
  margin-right: 4px;
}

.chapter-legend .correct::before { color: var(--green); }
.chapter-legend .wrong::before { color: var(--red); }
.chapter-legend .new::before { color: var(--gray); }

/* ---------- Scope radio options ---------- */

.scope-options {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.scope-option {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14.5px;
  cursor: pointer;
  user-select: none;
  transition: border-color 0.15s, background 0.15s;
}

.scope-option:active { background: var(--blue-light); }

.scope-option input { position: absolute; width: 1px; height: 1px; opacity: 0; }

.radio-visual {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 2px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s;
}

.radio-visual::after {
  content: '';
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--check-color);
  transform: scale(0);
  transition: transform 0.1s;
}

.scope-option input:checked ~ .radio-visual {
  border-color: var(--check-color);
}

.scope-option input:checked ~ .radio-visual::after {
  transform: scale(1);
}

.scope-option input:focus-visible ~ .radio-visual {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
}

.scope-option:has(input:checked) {
  border-color: var(--blue-dark);
  background: var(--blue-light);
}

/* ---------- Switch (shuffle toggle) ---------- */

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 14px;
  margin-top: 14px;
  font-size: 14.5px;
  cursor: pointer;
  user-select: none;
}

.switch { position: relative; width: 46px; height: 28px; flex-shrink: 0; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }

.switch-visual {
  position: absolute;
  inset: 0;
  background: #c9d1da;
  border-radius: 999px;
  transition: background 0.15s;
}

.switch-visual::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  background: white;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.25);
  transition: transform 0.15s;
}

.switch input:checked + .switch-visual { background: var(--blue-dark); }
.switch input:checked + .switch-visual::after { transform: translateX(18px); }
.switch input:focus-visible + .switch-visual { outline: 2px solid var(--blue); outline-offset: 2px; }

.link-danger {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: var(--red);
  font-size: 13.5px;
  padding: 18px 4px 4px;
  text-align: center;
  cursor: pointer;
}

.link-flag {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: var(--blue);
  font-size: 13.5px;
  font-weight: 600;
  padding: 10px 4px 0;
  text-align: center;
  cursor: pointer;
}

.link-flag:disabled { color: var(--gray); cursor: default; }
.link-flag[hidden] { display: none; }

/* ---------- Action bar (sticky bottom) ---------- */

.action-bar {
  flex-shrink: 0;
  padding: 12px 16px calc(12px + var(--safe-bottom));
  background: linear-gradient(to bottom, rgba(242,244,247,0), var(--bg) 20%);
  border-top: 1px solid var(--border);
}

.scope-count {
  text-align: center;
  font-size: 13px;
  color: var(--gray);
  margin-bottom: 10px;
}

.btn {
  display: block;
  width: 100%;
  padding: 15px;
  border-radius: 12px;
  border: none;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  margin-bottom: 8px;
  min-height: 52px;
  transition: transform 0.08s, opacity 0.15s;
}

.btn:last-child { margin-bottom: 0; }
.btn:active { transform: scale(0.98); }

.btn-primary { background: var(--blue-dark); color: white; }
.btn-primary:disabled { background: #b3bcc6; cursor: not-allowed; }
.btn-secondary { background: var(--card-bg); color: var(--blue-dark); border: 1px solid var(--border); }

/* ---------- Study screen ---------- */

.study-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px 0;
  font-size: 14px;
  flex-shrink: 0;
}

.link-back { color: var(--blue); text-decoration: none; font-weight: 600; }
#study-progress { color: var(--gray); font-weight: 700; }

.progress-track {
  height: 6px;
  background: var(--border);
  margin: 10px 16px 0;
  border-radius: 999px;
  overflow: hidden;
  flex-shrink: 0;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.06);
}

.progress-fill {
  height: 100%;
  background: var(--blue-dark);
  border-radius: 999px;
  transition: width 0.25s ease;
}

.question-card {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}

.chapter-tag {
  font-size: 12px;
  color: var(--gray);
  margin-bottom: 6px;
  font-weight: 600;
}

#study-question {
  font-size: clamp(17px, 5vw, 20px);
  line-height: 1.35;
  margin: 0 0 16px;
  color: #10151b;
  text-transform: none;
  letter-spacing: normal;
  font-weight: 700;
}

#study-image-wrap img {
  max-width: 100%;
  border-radius: 10px;
  margin-bottom: 16px;
  display: block;
  border: 1px solid var(--border);
}

.answers-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.answer-option {
  border: 1.5px solid var(--border);
  border-radius: 12px;
  padding: 13px 14px;
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1.35;
  min-height: 52px;
  transition: background 0.15s, border-color 0.15s;
}

.answer-option input { position: absolute; width: 1px; height: 1px; opacity: 0; }

.answer-option .checkbox-visual { margin: 0; }

.answer-option .key-hint {
  font-size: 11px;
  color: var(--gray);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 6px;
  min-width: 12px;
  text-align: center;
  flex-shrink: 0;
}

.answer-option:active { background: var(--blue-light); }

.answer-option.selected {
  border-color: var(--blue);
  background: var(--blue-light);
}

.answer-option.correct {
  border-color: var(--green);
  background: var(--green-bg);
}

.answer-option.incorrect {
  border-color: var(--red);
  background: var(--red-bg);
}

.missed-tag {
  display: inline-block;
  margin-left: 8px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--green);
  border: 1px solid var(--green);
  border-radius: 5px;
  padding: 1px 6px;
  vertical-align: middle;
}

.answer-option.disabled { cursor: default; }

/* ---------- Summary screen ---------- */

.summary-score {
  font-size: 26px;
  font-weight: 800;
  text-align: center;
  margin: 12px 0 24px;
}

.summary-wrong-list {
  margin-bottom: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.summary-wrong-item {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-left: 4px solid var(--red);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 14px;
  line-height: 1.4;
}

.summary-wrong-item .correct-answer {
  color: var(--green);
  font-weight: 600;
  margin-top: 5px;
}

/* ---------- Small screens ---------- */

@media (max-width: 380px) {
  .scroll-area { padding: 12px 12px 8px; }
  .action-bar { padding: 10px 12px calc(10px + var(--safe-bottom)); }
}
