:root {
  color-scheme: light;
  --paper: #fffaf0;
  --ink: #202124;
  --muted: #6d6a60;
  --line: #ded7c9;
  --accent: #286c5b;
  --accent-strong: #174c41;
  --accent-soft: #dceee9;
  --gold: #e1a633;
  --danger: #b43c36;
  --ok: #2e7d54;
  --surface: #ffffff;
  --shadow: 0 16px 50px rgba(54, 48, 36, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  min-height: 100%;
}

body {
  margin: 0;
  font-family: ui-rounded, "SF Pro Rounded", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background:
    linear-gradient(135deg, rgba(225, 166, 51, 0.14), transparent 34%),
    linear-gradient(225deg, rgba(40, 108, 91, 0.16), transparent 42%),
    var(--paper);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  min-height: 100svh;
  padding: max(20px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
}

.view {
  width: min(1120px, 100%);
  margin: 0 auto;
}

.language-switch {
  display: flex;
  justify-content: flex-end;
  width: min(1120px, 100%);
  margin: 0 auto 14px;
}

.language-switch button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-right: 0;
  padding: 0 14px;
  background: rgba(255, 255, 255, 0.78);
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
}

.language-switch button:first-child {
  border-radius: 7px 0 0 7px;
}

.language-switch button:last-child {
  border-right: 1px solid var(--line);
  border-radius: 0 7px 7px 0;
}

.language-switch button[aria-pressed="true"] {
  background: var(--accent);
  color: #ffffff;
}

.auth-view {
  display: grid;
  min-height: min(640px, calc(100svh - 100px));
  place-items: center;
}

.auth-panel {
  display: grid;
  width: min(440px, 100%);
  gap: 10px;
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: var(--shadow);
}

.auth-panel h1 {
  margin-bottom: 4px;
  font-size: 2.5rem;
}

.auth-intro {
  margin-bottom: 12px;
  color: var(--muted);
  font-weight: 700;
}

.field-label {
  color: var(--ink);
  font-size: 0.9rem;
  font-weight: 850;
}

.auth-panel input,
.form-field input,
.form-field select {
  width: 100%;
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  background: #fffdf7;
  color: var(--ink);
}

.auth-panel input:focus,
.form-field input:focus,
.form-field select:focus {
  border-color: var(--accent);
  outline: 3px solid rgba(40, 108, 91, 0.18);
}

.auth-panel .primary-action {
  margin-top: 10px;
}

.form-message {
  grid-column: 1 / -1;
  margin: 4px 0 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-weight: 800;
}

.error-message {
  background: #f9dfdd;
  color: #8f1f1a;
}

.user-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(1120px, 100%);
  margin: 0 auto 14px;
  padding: 8px 10px 8px 14px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.78);
}

#current-user {
  min-width: 0;
  overflow: hidden;
  font-weight: 850;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.hidden {
  display: none !important;
}

.brand-row,
.exam-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 20px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(2.2rem, 7vw, 4.8rem);
  line-height: 0.95;
}

h2,
legend {
  font-size: 1.1rem;
  font-weight: 850;
}

.mini-score,
.timer-box {
  min-width: 102px;
  padding: 14px 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  box-shadow: var(--shadow);
  text-align: center;
}

.mini-score span,
.timer-box span {
  display: block;
  color: var(--accent-strong);
  font-size: 2rem;
  font-weight: 900;
  line-height: 1;
}

.mini-score small,
.timer-box small {
  color: var(--muted);
  font-weight: 750;
}

.settings-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.panel {
  margin: 0;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.84);
  box-shadow: var(--shadow);
}

fieldset.panel {
  border: 1px solid var(--line);
}

legend {
  padding: 0 8px;
}

.segmented,
.operator-grid {
  display: grid;
  gap: 10px;
}

.segmented {
  grid-template-columns: repeat(5, minmax(0, 1fr));
}

.operator-grid {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.segmented input,
.operator-grid input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented span,
.operator-grid span {
  display: grid;
  min-height: 54px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  color: var(--ink);
  font-weight: 850;
}

.operator-grid span {
  font-size: 2rem;
}

.segmented input:checked + span,
.operator-grid input:checked + span {
  border-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent-strong);
  box-shadow: inset 0 0 0 2px rgba(40, 108, 91, 0.18);
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 18px;
}

.panel-head h2 {
  margin-bottom: 0;
}

output {
  color: var(--accent-strong);
  font-weight: 900;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.primary-action,
.secondary-action {
  min-height: 58px;
  border: 0;
  border-radius: 8px;
  padding: 0 24px;
  font-weight: 900;
}

.primary-action {
  grid-column: 1 / -1;
  background: var(--accent);
  color: white;
  box-shadow: var(--shadow);
}

.primary-action:active,
.secondary-action:active,
.icon-button:active {
  transform: translateY(1px);
}

.compact {
  grid-column: auto;
  min-width: 150px;
}

.secondary-action {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
}

.small-action {
  min-height: 42px;
  padding: 0 14px;
  font-size: 0.92rem;
}

.progress-track {
  height: 9px;
  margin-bottom: 14px;
  overflow: hidden;
  border-radius: 999px;
  background: rgba(32, 33, 36, 0.1);
}

.progress-fill {
  width: 100%;
  height: 100%;
  transform-origin: left center;
  background: linear-gradient(90deg, var(--accent), var(--gold));
}

.exam-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(260px, 30%);
  gap: 14px;
  align-items: start;
}

.question-area {
  min-height: 60svh;
}

.question-list {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
  gap: 10px;
}

.question-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 8px 24px rgba(54, 48, 36, 0.08);
}

.question-card label {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 112px;
  gap: 12px;
  align-items: center;
  min-height: 72px;
  padding: 12px;
}

.question-text {
  min-width: 0;
  font-size: 1.22rem;
  font-weight: 900;
  white-space: nowrap;
}

.answer-input {
  width: 100%;
  min-width: 0;
  min-height: 46px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: #fffdf7;
  text-align: center;
  font-size: 1.25rem;
  font-weight: 850;
}

.answer-input:focus {
  border-color: var(--accent);
  outline: 3px solid rgba(40, 108, 91, 0.18);
}

.scratch-panel {
  position: sticky;
  top: max(14px, env(safe-area-inset-top));
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf7;
  box-shadow: var(--shadow);
}

.scratch-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 58px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.scratch-head h2 {
  margin-bottom: 0;
}

.icon-button {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  color: var(--danger);
  font-size: 1.35rem;
  font-weight: 900;
}

#scratch-canvas {
  display: block;
  width: 100%;
  height: min(58svh, 520px);
  touch-action: none;
  background:
    linear-gradient(#eef1ef 1px, transparent 1px),
    linear-gradient(90deg, #eef1ef 1px, transparent 1px),
    #fffdf7;
  background-size: 26px 26px;
}

.exam-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 14px;
}

.result-view {
  display: grid;
  gap: 14px;
}

.result-hero {
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: var(--shadow);
}

.result-hero h1 {
  color: var(--accent-strong);
}

.result-hero p:last-child {
  margin-bottom: 0;
  color: var(--muted);
  font-weight: 800;
}

#time-summary {
  margin-bottom: 0;
  color: var(--accent-strong);
  font-weight: 900;
}

.review-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 10px;
}

.review-item {
  padding: 14px;
  border: 1px solid var(--line);
  border-left: 6px solid var(--ok);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
}

.review-item.wrong {
  border-left-color: var(--danger);
  border-color: #8f1f1a;
  background: #c62828;
  box-shadow: 0 12px 30px rgba(198, 40, 40, 0.28);
}

.review-item strong {
  display: block;
  margin-bottom: 8px;
  font-size: 1.15rem;
}

.review-item span {
  display: block;
  color: var(--muted);
  font-weight: 750;
}

.review-item.wrong strong,
.review-item.wrong span,
.review-item.wrong .correct-answer {
  color: #ffffff;
}

.review-item .correct-answer {
  color: var(--ok);
}

.result-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.history-section {
  margin-top: 18px;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 12px;
}

.section-head h2 {
  margin-bottom: 0;
  font-size: 1.45rem;
}

.history-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}

.history-pagination {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-top: 18px;
  color: #6d6a60;
  font-weight: 700;
}

.history-item {
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: var(--shadow);
}

.history-item img {
  display: block;
  width: 100%;
  aspect-ratio: 720 / 460;
  object-fit: cover;
  border-bottom: 1px solid var(--line);
  background: var(--paper);
}

.history-body {
  display: grid;
  gap: 5px;
  padding: 12px;
}

.history-score-button {
  width: fit-content;
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--accent-strong);
  font-size: 1.35rem;
  font-weight: 900;
  text-align: left;
  text-decoration: underline;
  text-underline-offset: 4px;
}

.history-score-button::after {
  content: " " attr(data-review-label);
  color: var(--muted);
  font-size: 0.86rem;
  font-weight: 800;
  text-decoration: none;
}

.history-body span,
.empty-history {
  color: var(--muted);
  font-weight: 750;
}

.admin-view {
  display: grid;
  gap: 18px;
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.create-user-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) minmax(150px, 0.5fr) auto;
  gap: 14px;
  align-items: end;
}

.create-user-form h2 {
  grid-column: 1 / -1;
  margin-bottom: 0;
}

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

.admin-list-section {
  min-width: 0;
}

.user-table-wrap {
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: var(--shadow);
}

.user-table {
  width: 100%;
  min-width: 840px;
  border-collapse: collapse;
}

.user-table th,
.user-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

.user-table th {
  background: rgba(220, 238, 233, 0.45);
  color: var(--accent-strong);
  font-size: 0.86rem;
}

.user-table tr:last-child td {
  border-bottom: 0;
}

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

.table-action {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  background: var(--surface);
  color: var(--ink);
  font-weight: 800;
  white-space: nowrap;
}

.table-action.danger {
  border-color: #d99894;
  color: var(--danger);
}

.role-badge {
  display: inline-block;
  padding: 4px 7px;
  border-radius: 6px;
  background: var(--accent-soft);
  color: var(--accent-strong);
  font-size: 0.82rem;
  font-weight: 850;
}


.empty-history {
  margin: 0;
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.7);
}

@media (max-width: 820px) {
  .app-shell {
    padding-inline: 12px;
  }

  .settings-grid,
  .exam-layout,
  .create-user-form {
    grid-template-columns: 1fr;
  }

  .create-user-form h2 {
    grid-column: auto;
  }

  .question-list {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  }

  .segmented {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .operator-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .scratch-panel {
    position: relative;
    top: auto;
  }

  #scratch-canvas {
    height: 34svh;
  }

  .exam-actions,
  .result-actions,
  .section-head,
  .admin-header {
    justify-content: stretch;
  }

  .exam-actions button,
  .result-actions button,
  .section-head button {
    flex: 1;
  }

  .section-head {
    align-items: stretch;
  }

  .admin-header {
    align-items: center;
  }
}

@media (max-width: 520px) {
  .language-switch {
    justify-content: stretch;
  }

  .language-switch button {
    flex: 1;
    min-width: 0;
    padding-inline: 8px;
  }

  .user-bar {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
  }

  .user-actions button {
    flex: 1;
  }

  h1 {
    font-size: 2.3rem;
  }

  .brand-row,
  .exam-header {
    align-items: stretch;
  }

  .mini-score,
  .timer-box {
    min-width: 86px;
    padding-inline: 10px;
  }

  .operator-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .question-list,
  .review-list {
    grid-template-columns: 1fr;
  }
}
