:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --text: #16202a;
  --text-muted: #526273;
  --border: #dce3ec;
  --primary: #0f4c81;
  --primary-soft: #e6f0fa;
  --primary-hover: #0b3a63;
  --accent-holsbeeck: #0d9488;
  --accent-bigeleisen: #2563eb;
  --accent-hadzic: #7c3aed;
  --accent-wall: #d97706;
  --success: #15803d;
  --success-bg: #dcfce7;
  --danger: #b91c1c;
  --danger-bg: #fee2e2;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
  --radius: 12px;
  --font-sans: "Segoe UI", "PingFang TC", "Microsoft JhengHei", "Noto Sans TC", system-ui, -apple-system, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

html[data-theme="dark"] {
  --bg: #0c121a;
  --surface: #141e2b;
  --surface-alt: #1a2636;
  --text: #e6edf5;
  --text-muted: #9aaaba;
  --border: #263547;
  --primary: #4ea1f3;
  --primary-soft: rgba(78, 161, 243, 0.14);
  --primary-hover: #74b8f8;
  --success: #4ade80;
  --success-bg: rgba(21, 128, 61, 0.24);
  --danger: #f87171;
  --danger-bg: rgba(185, 28, 28, 0.25);
  --warn-bg: rgba(217, 119, 6, 0.22);
  --warn-text: #fcd34d;
  --shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

.hidden {
  display: none !important;
}

/* Bilingual visibility control */
html[data-lang="en"] .zh-block,
html[data-lang="en"] .zh-inline {
  display: none !important;
}

html[data-lang="zh"] .en-block,
html[data-lang="zh"] .en-inline {
  display: none !important;
}

/* Topbar & Navigation */
.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
}

.topbar-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0.7rem 1rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
}

.brand-logo {
  border-radius: 9px;
  flex-shrink: 0;
}

.brand-title {
  font-weight: 750;
  font-size: 1.08rem;
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.topbar-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}

.status-pill {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.25rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.status-online {
  background: var(--success-bg);
  color: var(--success);
  border-color: transparent;
}

.status-offline {
  background: var(--warn-bg);
  color: var(--warn-text);
  border-color: transparent;
}

.lang-switch {
  display: inline-flex;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 2px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 600;
  padding: 0.3rem 0.6rem;
  border-radius: 6px;
  cursor: pointer;
}

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

.nav-tabs {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 1rem;
  display: flex;
  gap: 0.25rem;
  overflow-x: auto;
  scrollbar-width: none;
}

.nav-tab {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.9rem;
  font-weight: 600;
  padding: 0.65rem 0.95rem;
  border-bottom: 3px solid transparent;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  white-space: nowrap;
}

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

.nav-tab.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

.tab-count {
  font-size: 0.72rem;
  padding: 0.1rem 0.45rem;
  border-radius: 999px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

/* Main Container */
.container {
  max-width: 1200px;
  margin: 1rem auto 2.5rem;
  padding: 0 1rem;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.05rem 1.15rem;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 600;
  padding: 0.42rem 0.8rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover:not(:disabled) {
  background: var(--surface-alt);
}

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

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

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

.btn-ghost {
  background: transparent;
}

.btn-danger-outline {
  border-color: var(--danger);
  color: var(--danger);
  background: transparent;
}

.btn-sm {
  font-size: 0.78rem;
  padding: 0.3rem 0.6rem;
}

.btn-lg {
  font-size: 0.95rem;
  padding: 0.6rem 1.25rem;
}

.muted {
  color: var(--text-muted);
  font-size: 0.88rem;
}

/* Filter Bar */
.filter-bar {
  margin-bottom: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.search-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: center;
  justify-content: space-between;
}

.search-wrap {
  position: relative;
  flex: 1 1 320px;
}

.search-wrap input {
  width: 100%;
  padding: 0.55rem 2.1rem 0.55rem 0.85rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 0.9rem;
}

.search-wrap input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.clear-btn {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.9rem;
}

.quick-toggles {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
}

.chip-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  font-size: 0.82rem;
  font-weight: 600;
  padding: 0.32rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  cursor: pointer;
}

.book-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
}

.book-pill {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0.34rem 0.72rem;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.book-pill.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.book-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
}

/* Book Overview Grid */
.book-overview-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.book-card {
  border-left: 4px solid var(--primary);
  padding: 0.85rem 1rem;
  cursor: pointer;
  transition: transform 0.12s ease;
}

.book-card:hover {
  transform: translateY(-1px);
}

.book-card-head {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.5rem;
}

.book-card-title {
  font-weight: 700;
  font-size: 0.92rem;
}

.book-card-pages {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.book-card-sub {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

/* Notes Cards */
.notes-list {
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.module-card {
  padding: 0;
  overflow: hidden;
  border-left: 5px solid var(--primary);
}

.module-header {
  padding: 0.95rem 1.15rem;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  cursor: pointer;
  background: var(--surface);
}

.module-header:hover {
  background: var(--surface-alt);
}

.module-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  margin-bottom: 0.35rem;
}

.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  padding: 0.16rem 0.5rem;
  border-radius: 6px;
  background: var(--primary-soft);
  color: var(--primary);
}

.badge-outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.module-title-en {
  font-size: 1.03rem;
  font-weight: 700;
}

.module-title-zh {
  font-size: 0.96rem;
  font-weight: 600;
  color: var(--primary);
}

.module-actions {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-shrink: 0;
}

.icon-btn {
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  border-radius: 8px;
  padding: 0.28rem 0.55rem;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
}

.icon-btn.active-bookmark {
  background: var(--warn-bg);
  color: var(--warn-text);
  border-color: transparent;
}

.icon-btn.active-mastered {
  background: var(--success-bg);
  color: var(--success);
  border-color: transparent;
}

.module-body {
  padding: 0 1.15rem 1.15rem;
  border-top: 1px solid var(--border);
}

.summary-box {
  margin-top: 0.85rem;
  padding: 0.75rem 0.9rem;
  border-radius: 9px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 0.75rem;
}

.point-section {
  margin-top: 1rem;
  padding-top: 0.85rem;
  border-top: 1px dashed var(--border);
}

.point-heading {
  font-weight: 700;
  font-size: 0.95rem;
  margin-bottom: 0.55rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.bilingual-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
}

.bilingual-item {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 0.65rem;
  padding: 0.48rem 0.65rem;
  border-radius: 7px;
  background: var(--surface-alt);
  font-size: 0.88rem;
}

html[data-lang="en"] .bilingual-item,
html[data-lang="zh"] .bilingual-item,
html[data-lang="en"] .summary-box,
html[data-lang="zh"] .summary-box {
  grid-template-columns: 1fr;
}

.callout-row {
  margin-top: 0.65rem;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 0.65rem;
}

.callout {
  padding: 0.65rem 0.8rem;
  border-radius: 8px;
  font-size: 0.84rem;
}

.callout-pearl {
  background: var(--success-bg);
  border-left: 4px solid var(--success);
}

.callout-pitfall {
  background: var(--warn-bg);
  border-left: 4px solid #d97706;
}

.callout-title {
  font-weight: 700;
  margin-bottom: 0.25rem;
}

/* Tables */
.tables-list {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.table-wrap {
  overflow-x: auto;
  margin-top: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 9px;
}

.hy-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}

.hy-table th,
.hy-table td {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
  border-right: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}

.hy-table th {
  background: var(--surface-alt);
  font-weight: 700;
}

.hy-table tr:last-child td {
  border-bottom: none;
}

.hy-table th:last-child,
.hy-table td:last-child {
  border-right: none;
}

.cell-zh {
  color: var(--text-muted);
  font-size: 0.82rem;
  margin-top: 0.2rem;
}

/* Quiz Setup & Runner */
.section-header {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.section-header h2 {
  margin: 0 0 0.25rem;
  font-size: 1.15rem;
}

.quiz-config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 0.85rem;
  margin-bottom: 1rem;
}

.field-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field-group label {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-muted);
}

.field-group select {
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  font-size: 0.88rem;
}

.quiz-launch-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--border);
}

.quiz-topbar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.65rem;
  margin-bottom: 0.6rem;
}

.quiz-progress-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
}

.timer-pill {
  font-family: var(--font-mono);
  font-size: 0.82rem;
  padding: 0.16rem 0.5rem;
  border-radius: 6px;
  background: var(--surface-alt);
  border: 1px solid var(--border);
}

.progress-track {
  width: 100%;
  height: 6px;
  background: var(--border);
  border-radius: 999px;
  overflow: hidden;
  margin-bottom: 0.85rem;
}

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

.question-card {
  margin-bottom: 0.9rem;
}

.stem-box {
  margin-bottom: 1rem;
}

.stem-en {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 0.35rem;
}

.stem-zh {
  font-size: 0.95rem;
  color: var(--primary);
  font-weight: 500;
}

.options-list {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.option-btn {
  width: 100%;
  text-align: left;
  border: 1.5px solid var(--border);
  background: var(--surface-alt);
  color: var(--text);
  border-radius: 10px;
  padding: 0.7rem 0.9rem;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  gap: 0.65rem;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.option-btn:hover:not(:disabled) {
  border-color: var(--primary);
}

.option-letter {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--surface);
  border: 1px solid var(--border);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  font-weight: 700;
  flex-shrink: 0;
  margin-top: 2px;
}

.option-btn.selected {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.option-btn.correct {
  border-color: var(--success);
  background: var(--success-bg);
}

.option-btn.wrong {
  border-color: var(--danger);
  background: var(--danger-bg);
}

.rationale-box {
  margin-top: 1rem;
  padding: 0.85rem 1rem;
  border-radius: 10px;
  background: var(--surface-alt);
  border-left: 4px solid var(--primary);
}

.rationale-status {
  font-weight: 700;
  margin-bottom: 0.4rem;
}

.quiz-nav-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 0.65rem;
}

.quiz-dots {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  max-width: 680px;
}

.quiz-dot {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  font-size: 0.74rem;
  font-weight: 600;
  cursor: pointer;
}

.quiz-dot.current {
  border-color: var(--primary);
  color: var(--primary);
  font-weight: 700;
}

.quiz-dot.answered {
  background: var(--primary-soft);
}

.quiz-dot.dot-correct {
  background: var(--success-bg);
  color: var(--success);
  border-color: var(--success);
}

.quiz-dot.dot-wrong {
  background: var(--danger-bg);
  color: var(--danger);
  border-color: var(--danger);
}

/* Dashboard & Review */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 0.8rem;
  margin-bottom: 1rem;
}

.stat-card {
  text-align: center;
  padding: 1rem;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--primary);
}

.stat-label {
  font-size: 0.82rem;
  color: var(--text-muted);
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 16, 24, 0.6);
}

.modal-content {
  position: relative;
  z-index: 2;
  max-width: 640px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.modal-header h3 {
  margin: 0;
}

.modal-body pre {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.75rem;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.8rem;
}

.modal-body code {
  font-family: var(--font-mono);
  font-size: 0.85em;
}

.footer {
  max-width: 1200px;
  margin: 0 auto 2rem;
  padding: 0 1rem;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
}

@media (max-width: 640px) {
  .topbar-inner {
    padding: 0.6rem 0.75rem;
  }
  .container {
    padding: 0 0.7rem;
  }
  .module-header,
  .module-body {
    padding-left: 0.85rem;
    padding-right: 0.85rem;
  }
}

@media print {
  .topbar,
  .filter-bar,
  .quiz-nav-row,
  .footer {
    display: none !important;
  }
  .module-body {
    display: block !important;
  }
}
