:root {
  --bg: #f4f6fb;
  --card: #ffffff;
  --text: #1f2937;
  --muted: #6b7280;
  --line: #e5e7eb;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #e8efff;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --ok: #15803d;
  --ok-soft: #e9f8ef;
  --warn: #b45309;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC",
    "PingFang TC", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
}

body.modal-open {
  overflow: hidden;
}

h1, h2, h3 {
  line-height: 1.35;
}

h2 {
  font-size: 1.25rem;
  margin: 0 0 4px;
}

h3 {
  font-size: 1.05rem;
  margin: 0 0 6px;
}

/* ---------- 頁首與導覽 ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(255, 255, 255, 0.97);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
}

.header-inner {
  max-width: 1080px;
  margin: 0 auto;
  padding: 12px 20px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  font-size: 1.7rem;
  line-height: 1;
}

.brand h1 {
  font-size: 1.2rem;
  margin: 0;
}

.brand-sub {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
}

.tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  scrollbar-width: none;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  border: 0;
  background: transparent;
  color: var(--muted);
  padding: 10px 16px;
  font: inherit;
  font-size: 0.95rem;
  cursor: pointer;
  border-bottom: 3px solid transparent;
  white-space: nowrap;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
  font-weight: 700;
}

main {
  max-width: 1080px;
  margin: 0 auto;
  padding: 22px 20px 64px;
}

/* ---------- 版面元件 ---------- */
.view-head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

.card {
  background: var(--card);
  border: 1px solid rgba(226, 232, 240, 0.9);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 16px;
}

.stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 4px 0 0;
}

.pill {
  display: inline-block;
  background: #f1f5f9;
  color: #334155;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.78rem;
  font-weight: 600;
}

.badge {
  display: inline-block;
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 0.78rem;
  font-weight: 700;
  background: var(--primary-soft);
  color: var(--primary-dark);
}

.badge.type-single {
  background: #e8efff;
  color: #1d4ed8;
}

.badge.type-multiple {
  background: #f3e8ff;
  color: #7e22ce;
}

.badge.type-truefalse {
  background: #e0f2fe;
  color: #0369a1;
}

.tag {
  display: inline-block;
  border-radius: 6px;
  padding: 1px 7px;
  font-size: 0.72rem;
  font-weight: 700;
}

.tag.ok {
  background: var(--ok-soft);
  color: var(--ok);
}

.tag.bad {
  background: var(--danger-soft);
  color: var(--danger);
}

.notice {
  background: #fffbeb;
  border: 1px solid #fde68a;
  color: #92400e;
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.9rem;
  margin: 12px 0;
}

.empty {
  text-align: center;
  padding: 44px 20px;
}

.empty-icon {
  font-size: 2.4rem;
}

.empty h3 {
  margin: 8px 0 4px;
}

.empty .btn {
  margin-top: 14px;
}

.section-title {
  font-size: 1.05rem;
  margin: 22px 0 10px;
}

/* ---------- 按鈕 ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  border-radius: 10px;
  padding: 8px 14px;
  font: inherit;
  font-size: 0.92rem;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.btn:hover {
  background: #f8fafc;
  border-color: #cbd5e1;
}

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

.btn.primary:hover {
  background: var(--primary-dark);
  border-color: var(--primary-dark);
}

.btn.danger {
  color: var(--danger);
}

.btn.danger:hover {
  background: var(--danger-soft);
  border-color: #fecaca;
}

.btn.ghost {
  background: transparent;
}

.btn.tiny {
  padding: 4px 9px;
  font-size: 0.8rem;
  border-radius: 8px;
}

.btn.big {
  padding: 12px 28px;
  font-size: 1rem;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

.btn-row.center {
  justify-content: center;
  margin-top: 8px;
}

.icon-btn {
  border: 0;
  background: transparent;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 8px;
}

.icon-btn:hover {
  background: #f1f5f9;
  color: var(--text);
}

/* ---------- 表單 ---------- */
input[type="text"],
input[type="number"],
input[type="search"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid #cbd5e1;
  border-radius: 10px;
  background: #fff;
  color: inherit;
  font: inherit;
}

input:focus,
select:focus,
textarea:focus {
  outline: 2px solid var(--primary-soft);
  border-color: var(--primary);
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.88rem;
  font-weight: 600;
}

.field-grid {
  display: grid;
  grid-template-columns: 1fr 140px;
  gap: 14px;
  margin-bottom: 14px;
}

.grid-form {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 16px;
  align-items: end;
  margin-top: 18px;
}

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

.checkbox-field {
  justify-content: flex-end;
}

.checkbox-line {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-weight: 400;
  padding: 10px 0;
}

input[type="checkbox"],
input[type="radio"] {
  accent-color: var(--primary);
  width: 17px;
  height: 17px;
  flex: 0 0 auto;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  margin-bottom: 12px;
}

.toolbar-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.82rem;
  font-weight: 600;
  min-width: 180px;
}

.toolbar-field.compact {
  min-width: 150px;
}

.toolbar-field.grow {
  flex: 1 1 220px;
}

/* ---------- 題庫列表 ---------- */
.q-item-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 10px;
}

.q-item-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.q-index {
  font-weight: 700;
  font-size: 0.9rem;
}

.q-stem-text {
  margin: 10px 0 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.option-list {
  list-style: none;
  margin: 10px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.option-item {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 8px;
  background: #f8fafc;
  border: 1px solid transparent;
  font-size: 0.92rem;
}

.option-item.is-correct {
  background: var(--ok-soft);
  border-color: #bbf0cc;
}

.option-item.is-wrong {
  background: var(--danger-soft);
  border-color: #f6c6c6;
}

.option-letter {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #e2e8f0;
  display: inline-grid;
  place-items: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: #334155;
}

.option-body,
.option-text {
  overflow-wrap: anywhere;
}

.option-editor {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.option-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.option-row .option-text {
  flex: 1 1 auto;
}

.option-row .option-text[readonly] {
  background: #f8fafc;
  color: var(--muted);
}

.option-correct {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  padding: 4px;
}

.option-row .remove-option {
  flex: 0 0 auto;
}

/* ---------- 測驗 ---------- */
.quiz-top {
  margin-bottom: 12px;
}

.quiz-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px;
  font-size: 0.88rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.timer {
  color: var(--primary-dark);
  font-variant-numeric: tabular-nums;
}

.progress {
  height: 8px;
  background: #e2e8f0;
  border-radius: 999px;
  overflow: hidden;
}

.progress-bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, #60a5fa, #2563eb);
  border-radius: 999px;
  transition: width 0.25s ease;
}

.quiz-card {
  padding: 26px;
}

.q-stem {
  font-size: 1.2rem;
  margin: 12px 0 16px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

#quiz-options {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.option-choice {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #fff;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.option-choice:hover {
  border-color: #bfdbfe;
  background: #f8faff;
}

.option-choice:has(input:checked) {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.option-choice input {
  margin-top: 2px;
}

.quiz-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 16px;
}

.quiz-nav .hint {
  text-align: center;
  flex: 1 1 auto;
}

#quiz-next.submit {
  background: var(--ok);
  border-color: var(--ok);
}

#quiz-next.submit:hover {
  background: #116932;
  border-color: #116932;
}

/* ---------- 作答結果 ---------- */
.result-hero {
  text-align: center;
  padding: 30px 20px;
}

.score-big {
  font-size: 3.4rem;
  font-weight: 800;
  line-height: 1.1;
  color: var(--primary-dark);
  font-variant-numeric: tabular-nums;
}

.score-big span {
  font-size: 1.2rem;
  color: var(--muted);
  font-weight: 600;
}

.score-percent {
  font-size: 1.15rem;
  font-weight: 700;
  margin: 4px 0;
  color: var(--ok);
}

.review-item {
  padding: 16px 18px;
  border-left: 4px solid var(--line);
}

.review-item.ok {
  border-left-color: #22c55e;
}

.review-item.bad {
  border-left-color: #ef4444;
}

.review-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.review-mark {
  font-weight: 700;
  font-size: 0.9rem;
}

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

.review-item.bad .review-mark {
  color: var(--danger);
}

.review-options .option-letter {
  width: 20px;
  height: 20px;
}

.result-mini {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  padding: 12px 14px;
  background: #f8fafc;
  border-radius: 10px;
  margin-bottom: 14px;
  font-size: 0.9rem;
}

/* ---------- 成績表 ---------- */
.table-wrap {
  overflow-x: auto;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
  min-width: 720px;
}

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

.data-table th {
  background: #f8fafc;
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 700;
  white-space: nowrap;
}

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

.data-table tbody tr:hover {
  background: #fafbff;
}

.data-table .num {
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.data-table .actions-col {
  width: 1%;
}

.data-table .actions {
  white-space: nowrap;
}

.data-table .actions .btn {
  margin-right: 4px;
}

.data-table .empty-row {
  text-align: center;
  color: var(--muted);
  padding: 30px 12px;
}

/* ---------- 統計 ---------- */
.stat-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 16px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-label {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: 600;
}

.stat-card strong {
  font-size: 1.5rem;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
}

.stat-card .hint {
  margin: 0;
}

.chart {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  padding: 20px 4px 0;
  overflow-x: auto;
}

.chart-col {
  flex: 1 0 48px;
  min-width: 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.chart-slot {
  width: 100%;
  height: 150px;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.chart-bar {
  width: 68%;
  min-height: 3px;
  background: linear-gradient(180deg, #60a5fa, #2563eb);
  border-radius: 8px 8px 3px 3px;
  position: relative;
  transition: height 0.3s ease;
}

.chart-count {
  position: absolute;
  top: -1.45em;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.78rem;
  color: var(--muted);
  font-weight: 700;
}

.chart-label {
  font-size: 0.74rem;
  color: var(--muted);
  white-space: nowrap;
}

.qstats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 12px;
}

.qstat {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px;
  background: #fff;
}

.qstat-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.qstat-title {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.qstat-head strong {
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}

.qstat-stem {
  margin: 8px 0;
  font-size: 0.92rem;
  overflow-wrap: anywhere;
}

.qstat .hint {
  margin-top: 6px;
}

.bar {
  height: 8px;
  background: #eef2f7;
  border-radius: 999px;
  overflow: hidden;
}

.bar-fill {
  height: 100%;
  background: #22c55e;
  border-radius: 999px;
}

.bar-fill.is-bad {
  background: #ef4444;
}

/* ---------- 彈出視窗 ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
}

.modal-panel {
  position: relative;
  background: #fff;
  border-radius: 16px;
  width: min(720px, 100%);
  max-height: 90vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 24px 60px rgba(15, 23, 42, 0.25);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 20px;
  border-bottom: 1px solid var(--line);
}

.modal-head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
}

.modal-body .field {
  margin-bottom: 14px;
}

.field-label {
  display: block;
  font-size: 0.88rem;
  font-weight: 600;
  margin-bottom: 6px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.steps-hint {
  background: #f8fafc;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.82rem;
  color: var(--muted);
}

.steps-hint ol {
  margin: 4px 0 0;
  padding-left: 20px;
}

/* ---------- 提示訊息 ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: 22px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  z-index: 80;
  pointer-events: none;
  width: max-content;
  max-width: 90vw;
}

.toast-item {
  background: #111827;
  color: #fff;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.88rem;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  max-width: 100%;
}

.toast-item.show {
  opacity: 1;
  transform: translateY(0);
}

.toast-item.error {
  background: #991b1b;
}

/* ---------- 列印 ---------- */
#print-area {
  display: none;
}

@media print {
  body {
    background: #fff;
    color: #000;
  }

  body > *:not(#print-area) {
    display: none !important;
  }

  #print-area {
    display: block !important;
  }

  .print-sheet {
    font-size: 11pt;
    color: #000;
  }

  .print-sheet h1 {
    font-size: 18pt;
    margin: 0 0 4px;
  }

  .print-sheet h2 {
    font-size: 13pt;
    margin: 18px 0 6px;
  }

  .print-meta {
    color: #444;
    font-size: 10pt;
    margin-bottom: 10px;
  }

  .print-sheet table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 6px;
  }

  .print-sheet th,
  .print-sheet td {
    border: 1px solid #999;
    padding: 5px 8px;
    text-align: left;
    font-size: 10pt;
  }

  .print-sheet th {
    background: #f0f0f0;
  }

  .print-summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    margin-top: 8px;
  }

  .print-summary div {
    border: 1px solid #999;
    padding: 6px 8px;
    font-size: 10pt;
  }

  .print-summary strong {
    display: block;
    font-size: 12pt;
  }
}

/* ---------- 手機版 ---------- */
@media (max-width: 640px) {
  .header-inner {
    padding: 10px 12px 0;
  }

  main {
    padding: 16px 12px 48px;
  }

  .brand-icon {
    font-size: 1.4rem;
  }

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

  .brand-sub {
    display: none;
  }

  .tab {
    padding: 9px 12px;
    font-size: 0.9rem;
  }

  .view-head {
    flex-direction: column;
    align-items: stretch;
  }

  .view-head .btn-row {
    width: 100%;
  }

  .view-head .btn-row .btn {
    flex: 1 1 auto;
  }

  .card {
    padding: 16px;
  }

  .quiz-card {
    padding: 18px 16px;
  }

  .q-stem {
    font-size: 1.08rem;
  }

  .field-grid {
    grid-template-columns: 1fr;
  }

  .quiz-nav {
    flex-wrap: wrap;
  }

  .quiz-nav .hint {
    order: -1;
    flex-basis: 100%;
  }

  .quiz-nav .btn {
    flex: 1 1 40%;
  }

  .stat-cards {
    grid-template-columns: repeat(2, 1fr);
  }

  .stat-card strong {
    font-size: 1.25rem;
  }

  .qstats {
    grid-template-columns: 1fr;
  }

  .modal {
    padding: 0;
    align-items: flex-end;
  }

  .modal-panel {
    max-height: 92vh;
    border-radius: 16px 16px 0 0;
  }

  .score-big {
    font-size: 2.8rem;
  }
}
