:root {
  color-scheme: light;
  --bg: #f6f5ef;
  --panel: #ffffff;
  --ink: #20252b;
  --muted: #69737d;
  --line: #d9ddd8;
  --accent: #2f7d6b;
  --accent-soft: #e4f2ee;
  --amber: #9a5b13;
  --danger: #b42318;
  --danger-soft: #fae8e6;
  --shadow: 0 12px 30px rgba(33, 38, 45, 0.08);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow: hidden;
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

button,
input,
select,
textarea {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  min-height: 40px;
  padding: 0 14px;
  cursor: pointer;
}

button:hover {
  border-color: #aeb7b1;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.primary {
  background: var(--accent);
  color: #fff;
  border-color: var(--accent);
}

.app-shell {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
  border-right: 1px solid var(--line);
  background: #fbfaf6;
  padding: 22px;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border-radius: 8px;
  background: #26332f;
  color: #f8f5ec;
  font-weight: 800;
  letter-spacing: 0;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
}

.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.filters {
  display: grid;
  gap: 8px;
}

.filters label {
  color: var(--muted);
  font-size: 13px;
}

.filters select,
.filters input {
  width: 100%;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 0 10px;
  color: var(--ink);
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

#practiceTools summary,
.source-image summary {
  padding: 6px 0;
  color: var(--muted);
  font-size: 13px;
  cursor: pointer;
}

#practiceTools .stats-grid,
#practiceTools .side-actions {
  margin-top: 8px;
}

.source-image img {
  display: block;
  margin-top: 8px;
}

.stats-grid div {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  padding: 10px;
}

.stats-grid span {
  display: block;
  font-size: 20px;
  font-weight: 700;
}

.stats-grid small {
  color: var(--muted);
}

.side-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.side-actions .wide {
  grid-column: 1 / -1;
}

.side-actions button[hidden] {
  display: none;
}

.side-actions .active {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: #1d5e50;
}

.question-list {
  display: grid;
  gap: 6px;
  overflow: auto;
  padding-right: 4px;
  min-height: 0;
  overscroll-behavior: contain;
}

.chapter-heading {
  position: sticky;
  top: 0;
  z-index: 1;
  margin: 8px 0 2px;
  padding: 8px 4px 6px;
  background: #fbfaf6;
  color: #53605a;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0;
}

.question-list button {
  display: grid;
  grid-template-columns: 52px minmax(0, 1fr) 34px;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 42px;
  text-align: left;
  padding: 8px 10px;
  background: transparent;
}

.question-list button.active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.question-list strong,
.question-list span {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.status {
  display: grid;
  place-items: center;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  color: #fff;
  font-size: 12px;
  font-weight: 700;
}

.status.ok {
  background: var(--accent);
}

.status.bad {
  background: var(--danger);
}

.status.blank {
  background: #b4bbb4;
}

.main {
  min-width: 0;
  min-height: 0;
  height: 100vh;
  overflow: auto;
  overscroll-behavior: contain;
  padding: 28px;
}

.topbar {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr) 48px;
  align-items: center;
  gap: 12px;
  max-width: 980px;
  margin: 0 auto 18px;
}

.topbar button {
  width: 48px;
  height: 48px;
  font-size: 28px;
  line-height: 1;
}

.position {
  min-width: 0;
  text-align: center;
}

.position span {
  display: block;
  color: var(--muted);
  font-size: 14px;
}

.position strong {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 18px;
}

.question-panel {
  max-width: 980px;
  margin: 0 auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
  padding: 28px;
}

.question-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.badge {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  border-radius: 999px;
  padding: 0 12px;
  background: var(--accent-soft);
  color: #1d5e50;
  font-weight: 700;
}

.source {
  color: var(--muted);
  font-size: 13px;
  text-align: right;
}

h2 {
  margin: 0 0 22px;
  font-size: 24px;
  line-height: 1.55;
  letter-spacing: 0;
}

.source-image {
  margin: 0 0 20px;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: auto;
  background: #f6f5ef;
}

.source-image img {
  display: block;
  width: 100%;
  min-width: 680px;
  height: auto;
}

.source-note {
  margin: 0;
  padding: 16px 18px;
  border: 1px solid #d8e4dc;
  background: #f4faf6;
  color: #315846;
  line-height: 1.7;
}

.answer-area {
  display: grid;
  gap: 12px;
}

.option-row {
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  min-height: 58px;
  padding: 15px 16px;
  cursor: pointer;
}

.option-row:hover {
  border-color: #b9c4bd;
}

.option-row input {
  width: 18px;
  height: 18px;
  margin: 0;
  accent-color: var(--accent);
}

.option-row strong {
  margin-right: 8px;
}

textarea,
.text-answer {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  line-height: 1.6;
}

.text-answer {
  min-height: 46px;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 22px;
}

.actions button {
  font-weight: 700;
}

#submitBtn {
  order: 2;
}

#markWrongBtn {
  order: 3;
}

#showAnswerBtn {
  order: 1;
}

#prevQuestionBtn {
  order: 4;
}

#randomBtn {
  order: 5;
}

.feedback {
  margin-top: 22px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  background: #fbfaf6;
}

.feedback.correct {
  border-color: #9fcabc;
  background: var(--accent-soft);
}

.feedback.wrong {
  border-color: #ecc0bb;
  background: var(--danger-soft);
}

.feedback-title {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  font-size: 18px;
}

#resultMark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: #fff;
  font-weight: 800;
  font-size: 18px;
  line-height: 1;
  white-space: nowrap;
}

.correct #resultMark {
  background: var(--accent);
}

.wrong #resultMark {
  background: var(--danger);
}

.feedback p {
  margin: 10px 0 0;
  line-height: 1.7;
}

#correctAnswerText,
#explanationText {
  white-space: pre-line;
}

.question-head .badge {
  flex-shrink: 0;
  white-space: nowrap;
}

#explanationText {
  border-top: 1px solid #9fcabc;
  padding-top: 10px;
}

.material-note {
  margin: 18px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.6;
}

@media (max-width: 1180px) {
  .app-shell {
    grid-template-columns: 286px minmax(0, 1fr);
  }

  .sidebar {
    gap: 12px;
    padding: 16px;
  }

  .filters select,
  .filters input,
  button {
    min-height: 44px;
  }

  .stats-grid {
    gap: 6px;
  }

  .stats-grid div {
    padding: 8px;
  }

  .side-actions {
    gap: 6px;
  }

  .question-list button {
    grid-template-columns: 50px minmax(0, 1fr) 30px;
    min-height: 48px;
    padding: 8px;
  }

  .main {
    padding: 18px 18px calc(108px + env(safe-area-inset-bottom));
  }

  .topbar,
  .question-panel {
    max-width: none;
  }

  .topbar {
    margin-bottom: 12px;
  }

  .question-panel {
    padding: 24px;
  }

  h2 {
    margin-bottom: 20px;
    font-size: 24px;
    line-height: 1.5;
  }

  .option-row {
    grid-template-columns: 36px minmax(0, 1fr);
    min-height: 66px;
    padding: 17px 18px;
    font-size: 18px;
  }

  .option-row input {
    width: 22px;
    height: 22px;
  }

  .actions {
    position: sticky;
    bottom: max(12px, env(safe-area-inset-bottom));
    z-index: 10;
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    margin: 18px -4px 0;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: var(--shadow);
    backdrop-filter: blur(8px);
  }

  .actions button {
    flex: 0 0 auto;
    min-height: 58px;
    padding: 0 12px;
    font-size: 18px;
  }

  #showAnswerBtn {
    width: 148px;
    margin-right: auto;
  }

  #submitBtn,
  #randomBtn {
    width: 156px;
  }

  .feedback {
    margin-top: 18px;
  }
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(240px, 40vh) minmax(0, 1fr);
  }

  .sidebar {
    border-right: 0;
    border-bottom: 1px solid var(--line);
    max-height: none;
    height: 40vh;
  }

  .sidebar:has(#practiceTools[open]) {
    overflow-y: auto;
  }

  .sidebar:has(#practiceTools[open]) .question-list {
    flex: none;
    max-height: 190px;
  }

  .main {
    height: auto;
    padding: 14px 14px calc(22px + env(safe-area-inset-bottom));
  }

  .question-panel {
    padding: 20px;
  }

  h2 {
    font-size: 20px;
  }

  .option-row {
    grid-template-columns: 34px minmax(0, 1fr);
    min-height: 62px;
    padding: 15px;
    font-size: 17px;
  }

  .actions {
    position: static;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
  }

  #showAnswerBtn,
  #submitBtn,
  #markWrongBtn,
  #prevQuestionBtn,
  #randomBtn {
    width: 100%;
    margin-right: 0;
  }

  #showAnswerBtn {
    order: 1;
    grid-column: 1 / -1;
  }

  #submitBtn {
    order: 2;
  }

  #markWrongBtn {
    order: 3;
  }

  #prevQuestionBtn {
    order: 4;
  }

  #randomBtn {
    order: 5;
  }
}
