:root {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-soft: #eef3f8;
  --control-bg: #ffffff;
  --topbar-bg: rgba(245, 247, 251, 0.94);
  --text: #17202a;
  --muted: #647184;
  --line: #d8e0ea;
  --accent: #2563eb;
  --accent-dark: #1e40af;
  --good: #15803d;
  --bad: #b91c1c;
  --warning: #b45309;
  --shadow: 0 16px 42px rgba(24, 39, 75, 0.1);
}

[data-theme="dark"] {
  --bg: #0b1120;
  --surface: #111827;
  --surface-soft: #182235;
  --control-bg: #0f172a;
  --topbar-bg: rgba(11, 17, 32, 0.94);
  --text: #eef4ff;
  --muted: #9aa8bd;
  --line: #2c3a4f;
  --accent: #60a5fa;
  --accent-dark: #3b82f6;
  --good: #4ade80;
  --bad: #fb7185;
  --warning: #facc15;
  --shadow: 0 18px 46px rgba(0, 0, 0, 0.32);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", "Noto Sans TC", "PingFang TC", Arial, sans-serif;
  transition: background-color 0.32s ease, color 0.32s ease;
}

body {
  position: relative;
  overflow-x: hidden;
  min-height: 100vh;
  margin: 0;
  background:
    linear-gradient(180deg, rgba(37, 99, 235, 0.09), transparent 260px),
    var(--bg);
  transition: background 0.32s ease, color 0.32s ease;
  animation: pageEnter 0.36s ease both;
}

body.page-exit-left {
  animation: pageExitLeft 0.28s ease both;
}

button,
input {
  font: inherit;
}

button {
  min-height: 44px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.16s ease, background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

button:hover {
  background: var(--accent-dark);
  transform: translateY(-1px);
}

button.is-stop {
  background: var(--bad);
}

button.is-stop:hover {
  background: color-mix(in srgb, var(--bad) 82%, #000);
}

button:active {
  transform: translateY(0) scale(0.98);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

button.secondary {
  border: 1px solid var(--line);
  background: var(--control-bg);
  color: var(--text);
}

button.secondary:hover {
  border-color: #b8c5d6;
  background: var(--surface-soft);
}

.topbar {
  position: relative;
  z-index: 20;
  padding-top: env(safe-area-inset-top);
  border-bottom: 1px solid var(--line);
  background: var(--topbar-bg);
  backdrop-filter: blur(14px);
  transition: background-color 0.32s ease, border-color 0.32s ease;
}

.topbar-inner {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto auto;
  gap: 16px;
  align-items: center;
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 16px 20px 10px;
}

.topbar-inner > div {
  min-width: 0;
}

.back-link {
  display: inline-grid;
  min-width: 54px;
  min-height: 40px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
  color: var(--text);
  font-size: 15px;
  font-weight: 800;
  text-decoration: none;
  transition: transform 0.16s ease, background-color 0.2s ease, border-color 0.2s ease;
}

.back-link:hover {
  transform: translateY(-1px);
  background: var(--surface-soft);
}

.theme-toggle {
  min-width: 58px;
  padding: 0 14px;
  border: 1px solid var(--line);
  background: var(--control-bg);
  color: var(--text);
}

.login-toggle {
  display: inline-grid;
  min-height: 44px;
  min-width: 58px;
  place-items: center;
  padding: 0 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--control-bg);
  color: var(--text);
  font-weight: 800;
  text-decoration: none;
}

h1 {
  margin: 0;
  font-size: clamp(22px, 3vw, 34px);
  line-height: 1.2;
  letter-spacing: 0;
}

.topbar p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 15px;
}

.category-bar {
  width: 100%;
  overflow: hidden;
}

.category-scroll {
  display: flex;
  gap: 8px;
  width: min(1120px, 100%);
  min-height: 56px;
  margin: 0 auto;
  padding: 8px 20px 10px;
  overflow-x: auto;
  overflow-y: hidden;
  overscroll-behavior-x: contain;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.category-tab {
  flex: 0 0 auto;
  min-width: max-content;
  min-height: 38px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
  color: var(--text);
  white-space: nowrap;
  transition: transform 0.16s ease, background-color 0.2s ease, border-color 0.2s ease;
}

.category-tab:hover {
  transform: translateY(-1px);
}

.category-tab.is-active {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}

.app-shell {
  display: grid;
  gap: 18px;
  width: min(1120px, 100%);
  margin: 0 auto;
  padding: 22px 20px 98px;
}

.controls,
.login-panel,
.history-panel,
.result-panel,
.progress-panel,
.question-card,
.bottom-actions {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.controls {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr)) repeat(3, minmax(150px, auto));
  gap: 14px;
  align-items: end;
  padding: 18px;
}

.field {
  display: grid;
  gap: 6px;
}

.field label {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.field input,
.field select {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
  color: var(--text);
  transition: background-color 0.32s ease, border-color 0.32s ease, color 0.32s ease;
}

.field select {
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, var(--muted) 50%),
    linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position:
    calc(100% - 18px) 19px,
    calc(100% - 12px) 19px;
  background-repeat: no-repeat;
  background-size: 6px 6px;
  padding-right: 34px;
}

.field input[type="number"] {
  appearance: textfield;
  -moz-appearance: textfield;
}

.field input[type="number"]::-webkit-inner-spin-button,
.field input[type="number"]::-webkit-outer-spin-button {
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
}

.toggle-field {
  display: inline-grid;
  grid-template-columns: auto 1fr;
  gap: 10px;
  align-items: center;
  min-height: 44px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
  color: var(--text);
  font-weight: 800;
  transition: transform 0.16s ease, background-color 0.32s ease, border-color 0.32s ease;
}

.toggle-field:hover {
  transform: translateY(-1px);
  background: var(--surface-soft);
}

.toggle-field input {
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  grid-column: 1 / -1;
  gap: 10px;
}

.actions button {
  padding: 0 16px;
}

.login-panel,
.history-panel {
  padding: 18px;
}

.login-head,
.history-head {
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
}

.login-head b,
.history-head b {
  display: block;
  font-size: 18px;
}

.login-head span,
.history-head span,
.login-message {
  color: var(--muted);
  font-size: 14px;
}

.login-message.is-error {
  color: var(--bad);
}

.login-body {
  display: grid;
  gap: 14px;
  margin-top: 16px;
}

.role-switch {
  display: inline-flex;
  width: fit-content;
  gap: 8px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
}

.role-button {
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
}

.role-button:hover,
.role-button:active {
  transform: none;
}

.role-button.is-active {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--control-bg));
}

.login-fields,
.history-filters {
  display: grid;
  grid-template-columns: repeat(2, minmax(160px, 1fr));
  gap: 14px;
}

.history-filters {
  grid-template-columns: repeat(5, minmax(130px, 1fr));
  align-items: end;
  margin-top: 16px;
}

.history-sort {
  display: grid;
  grid-template-columns: repeat(2, minmax(150px, 1fr)) auto auto;
  gap: 14px;
  align-items: end;
  margin-top: 14px;
}

.history-list {
  display: grid;
  gap: 10px;
  margin-top: 16px;
}

.history-item {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
}

.history-item b {
  display: block;
}

.history-item span {
  color: var(--muted);
  font-size: 14px;
}

.history-score {
  align-self: center;
  color: var(--text);
  font-weight: 900;
  white-space: nowrap;
}

.history-score span {
  color: var(--muted);
}

.history-score b {
  display: inline;
}

.score-good {
  color: var(--good);
}

.score-bad {
  color: var(--bad);
}

.history-actions {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  justify-self: end;
}

.history-delete {
  min-height: 34px;
  padding: 0 12px;
  background: var(--bad);
  font-size: 13px;
}

.history-delete:hover {
  background: color-mix(in srgb, var(--bad) 82%, #000);
  transform: none;
}

.result-panel {
  display: flex;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
}

.result-panel b {
  display: block;
  font-size: 20px;
}

.result-panel span {
  color: var(--muted);
}

.progress-panel {
  position: fixed;
  z-index: 35;
  top: max(16px, env(safe-area-inset-top));
  right: 20px;
  overflow: hidden;
  width: min(520px, calc(100vw - 40px));
}

.progress-toggle {
  display: flex;
  width: 100%;
  min-height: 50px;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  border-radius: 0;
  background: var(--surface-soft);
  color: var(--text);
  text-align: left;
}

.progress-toggle::after,
.action-toggle::after {
  content: "▾";
  display: inline-block;
  margin-left: 8px;
  transition: transform 0.2s ease;
}

.progress-panel.is-collapsed .progress-toggle::after,
.bottom-actions.is-collapsed .action-toggle::after {
  transform: rotate(-90deg);
}

.progress-toggle:hover,
.progress-toggle:active {
  transform: none;
}

.progress-toggle span:first-child {
  font-size: 16px;
  font-weight: 900;
}

.progress-toggle span:last-child {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.progress-body {
  display: grid;
  grid-template-columns: repeat(auto-fill, 42px);
  justify-content: start;
  gap: 8px;
  max-height: min(360px, calc(100vh - 160px));
  overflow-y: auto;
  padding: 14px 16px 16px;
  opacity: 1;
  transition: max-height 0.24s ease, opacity 0.2s ease, padding 0.24s ease;
}

.progress-panel.is-collapsed .progress-body {
  max-height: 0;
  overflow: hidden;
  padding-top: 0;
  padding-bottom: 0;
  opacity: 0;
}

.progress-track {
  height: 5px;
  background: color-mix(in srgb, var(--line) 72%, transparent);
}

.progress-fill {
  width: 0%;
  height: 100%;
  background: var(--accent);
  transition: width 0.32s ease;
}

.progress-jump {
  width: 42px;
  min-width: 0;
  height: 36px;
  min-height: 0;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--control-bg);
  color: var(--text);
  font-size: 13px;
  line-height: 1;
  text-align: center;
  white-space: nowrap;
}

.progress-jump:hover,
.progress-jump:active {
  transform: none;
}

.progress-jump.is-answered {
  border-color: color-mix(in srgb, var(--accent) 48%, var(--line));
  background: color-mix(in srgb, var(--accent) 10%, var(--control-bg));
}

.progress-jump.is-correct {
  border-color: color-mix(in srgb, var(--good) 72%, var(--line));
  background: color-mix(in srgb, var(--good) 13%, var(--control-bg));
}

.progress-jump.is-wrong {
  border-color: color-mix(in srgb, var(--bad) 72%, var(--line));
  background: color-mix(in srgb, var(--bad) 13%, var(--control-bg));
}

.progress-jump.is-uncertain {
  outline: 2px solid var(--warning);
  outline-offset: 1px;
}

.quiz-list {
  display: grid;
  gap: 14px;
}

.quiz-list.is-collapsing {
  overflow: hidden;
  animation: quizListCollapse 0.24s ease both;
}

.empty-state {
  padding: 34px 22px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--muted);
  text-align: center;
}

.question-card {
  overflow: hidden;
  animation: cardEnter 0.34s ease both;
  transition: background-color 0.32s ease, border-color 0.32s ease, box-shadow 0.32s ease;
}

.question-head {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-soft);
}

.question-tools {
  display: inline-flex;
  flex: 0 0 auto;
  gap: 8px;
  align-items: center;
}

.uncertain-button {
  min-width: 34px;
  min-height: 30px;
  padding: 0 8px;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
}

.uncertain-button:hover,
.uncertain-button:active {
  transform: none;
  background: transparent;
  color: var(--warning);
}

.uncertain-button.is-active {
  background: transparent;
  color: var(--text);
}

.question-meta {
  color: var(--muted);
  font-size: 14px;
  font-weight: 800;
}

.question-type {
  flex: 0 0 auto;
  padding: 3px 9px;
  border-radius: 999px;
  background: #dbeafe;
  color: #1d4ed8;
  font-size: 13px;
  font-weight: 900;
}

[data-theme="dark"] .question-type {
  background: rgba(96, 165, 250, 0.18);
  color: #bfdbfe;
}

.question-body {
  display: grid;
  gap: 14px;
  padding: 18px;
}

.question-text {
  margin: 0;
  color: var(--text);
  font-size: 18px;
  line-height: 1.65;
}

.question-images {
  display: grid;
  gap: 10px;
}

.question-images img {
  display: block;
  max-width: 100%;
  max-height: 420px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  object-fit: contain;
}

.choices {
  display: grid;
  gap: 10px;
}

.choice {
  display: grid;
  width: 100%;
  grid-template-columns: 1fr;
  align-items: start;
  min-height: 46px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--control-bg);
  color: var(--text);
  font-weight: 600;
  text-align: left;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.choice:hover {
  transform: none;
  border-color: color-mix(in srgb, var(--accent) 48%, var(--line));
  background: var(--surface-soft);
}

.choice:active {
  transform: none;
}

.choice.is-picked {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, var(--control-bg));
}

.choice span {
  line-height: 1.55;
}

.choice-key {
  margin-right: 4px;
  color: var(--muted);
  font-weight: 900;
}

.question-card.is-checked .choice.is-answer {
  border-color: rgba(21, 128, 61, 0.7);
  background: rgba(21, 128, 61, 0.08);
}

.question-card.is-checked .choice.is-selected:not(.is-answer) {
  border-color: rgba(185, 28, 28, 0.7);
  background: rgba(185, 28, 28, 0.08);
}

.answer-line {
  display: none;
  padding-top: 4px;
  color: var(--muted);
  font-size: 15px;
  font-weight: 800;
}

.question-card.is-checked .answer-line {
  display: block;
}

.question-card.is-correct .answer-line {
  color: var(--good);
}

.question-card.is-wrong .answer-line {
  color: var(--bad);
}

.bottom-actions {
  position: fixed;
  z-index: 30;
  right: 20px;
  bottom: max(18px, env(safe-area-inset-bottom));
  display: grid;
  gap: 8px;
  padding: 0;
  border: 0;
  border-color: transparent;
  background: transparent;
  box-shadow: none;
}

.action-toggle {
  min-width: 96px;
  justify-self: end;
  padding: 0 16px;
}

.action-body {
  display: flex;
  gap: 10px;
  max-height: 60px;
  overflow: hidden;
  opacity: 1;
  transition: max-height 0.24s ease, opacity 0.2s ease;
}

.bottom-actions.is-collapsed .action-body {
  max-height: 0;
  opacity: 0;
}

.action-body button {
  min-width: 112px;
  padding: 0 18px;
}

@media (max-width: 720px) {
  body {
    width: 100%;
    overflow-x: hidden;
  }

  .topbar-inner {
    grid-template-columns: 1fr;
    gap: 10px;
    padding: 12px 14px 8px;
  }

  .back-link {
    width: 54px;
  }

  .theme-toggle {
    position: absolute;
    top: calc(12px + env(safe-area-inset-top));
    right: 14px;
    min-height: 40px;
  }

  .login-toggle {
    position: absolute;
    top: calc(12px + env(safe-area-inset-top));
    right: 86px;
    max-width: 66px;
    min-height: 40px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .history-toggle {
    right: 156px;
    max-width: 58px;
  }

  h1 {
    font-size: 22px;
  }

  .topbar p {
    font-size: 14px;
  }

  .category-scroll {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
    min-height: 0;
    padding: 8px 14px 10px;
    overflow: visible;
    scrollbar-width: none;
  }

  .category-scroll::-webkit-scrollbar {
    display: none;
  }

  .category-tab {
    width: 100%;
    min-width: 0;
    min-height: 36px;
    padding: 0 12px;
    font-size: 14px;
    white-space: normal;
  }

  .history-item {
    grid-template-columns: 1fr;
  }

  .history-actions {
    justify-self: start;
    flex-wrap: wrap;
  }

  .app-shell {
    padding: 16px 12px 110px;
  }

  .controls {
    grid-template-columns: 1fr 1fr;
    padding: 14px;
  }

  .toggle-field {
    grid-column: 1 / -1;
  }

  .login-fields,
  .history-filters,
  .history-sort {
    grid-template-columns: 1fr;
  }

  .actions {
    grid-column: 1 / -1;
  }

  .actions button {
    flex: 1 1 120px;
    padding: 0 10px;
  }

  .result-panel {
    align-items: stretch;
    flex-direction: column;
  }

  .question-head,
  .question-body {
    padding-right: 14px;
    padding-left: 14px;
  }

  .question-text {
    font-size: 17px;
  }

  .bottom-actions {
    right: 12px;
    left: auto;
    max-width: calc(100vw - 24px);
  }

  .action-body {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .action-body button {
    flex: 1 1 0;
    min-width: 108px;
    padding: 0 10px;
  }

  .progress-panel {
    left: 12px;
    right: 12px;
    width: auto;
  }
}

@keyframes pageEnter {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes pageExitLeft {
  to {
    opacity: 0;
    transform: translateX(-34px);
  }
}

@keyframes cardEnter {
  from {
    opacity: 0;
  }

  to {
    opacity: 1;
  }
}

@keyframes quizListCollapse {
  from {
    max-height: var(--quiz-list-height);
    opacity: 1;
  }

  to {
    max-height: 0;
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
