:root {
  --font-main: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", Helvetica, Arial, sans-serif;
  --font-mono: "Fira Code", Consolas, Monaco, "Courier New", monospace;

  /* Light Theme */
  --bg-page: #f8fafc;
  --bg-surface: #ffffff;
  --bg-surface-hover: #f1f5f9;
  --bg-subtle: #f1f5f9;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --text-heading: #0f172a;
  --border-color: #cbd5e1;
  --border-hover: #94a3b8;

  --accent-primary: #0f766e;
  --accent-primary-hover: #115e59;
  --accent-soft: #ccfbf1;
  --accent-badge-text: #0f766e;

  --code-bg: #e2e8f0;
  --code-text: #0f172a;

  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);

  --content-width: 900px;
}

[data-theme="dark"] {
  /* Dark Theme */
  --bg-page: #0f172a;
  --bg-surface: #1e293b;
  --bg-surface-hover: #334155;
  --bg-subtle: #1e293b;
  --text-main: #f1f5f9;
  --text-muted: #94a3b8;
  --text-heading: #ffffff;
  --border-color: #334155;
  --border-hover: #475569;

  --accent-primary: #14b8a6;
  --accent-primary-hover: #2dd4bf;
  --accent-soft: rgba(20, 184, 166, 0.15);
  --accent-badge-text: #2dd4bf;

  --code-bg: #0f172a;
  --code-text: #38bdf8;

  --shadow-sm: 0 1px 3px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 14px rgba(0,0,0,0.3);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html {
  scroll-behavior: smooth;
  scroll-padding-top: 76px;
  background: var(--bg-page);
  font-size: 18px;
}

body {
  min-height: 100vh;
  color: var(--text-main);
  background: var(--bg-page);
  font-family: var(--font-main);
  font-size: 1rem;
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

button, input { font: inherit; }
a { color: inherit; text-decoration: none; }

/* Ultra-Slim Single Header (Height ~ 54px) */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-color);
  box-shadow: var(--shadow-sm);
}

.topbar-inner {
  max-width: var(--content-width);
  width: calc(100% - 32px);
  margin: 0 auto;
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  font-size: 20px;
  font-weight: 700;
  color: var(--text-heading);
}

.brand-mark {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  color: #ffffff;
  background: var(--accent-primary);
  border-radius: var(--radius-md);
}

.home-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-heading);
  font-size: 15px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: var(--radius-md);
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  transition: all 0.15s ease;
}

.home-link:hover {
  border-color: var(--accent-primary);
  color: var(--accent-primary);
}

.topbar-divider {
  color: var(--border-hover);
  font-size: 14px;
}

.topbar-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-heading);
  white-space: nowrap;
}

/* Compact Integrated Search */
.topbar-center {
  flex: 1;
  max-width: 320px;
  margin: 0 8px;
}

.search-wrap {
  position: relative;
  width: 100%;
}

.search-wrap > svg {
  position: absolute;
  top: 50%;
  left: 10px;
  color: var(--text-muted);
  pointer-events: none;
  transform: translateY(-50%);
}

.search-input {
  width: 100%;
  height: 34px;
  padding: 0 10px 0 34px;
  color: var(--text-main);
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 14px;
  transition: all 0.15s ease;
}

.search-input::placeholder { color: var(--text-muted); }

.search-input:hover,
.search-input:focus {
  border-color: var(--accent-primary);
  outline: none;
  background: var(--bg-surface);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.tool-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  color: var(--text-main);
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all 0.15s ease;
}

.tool-icon-btn:hover {
  color: var(--accent-primary);
  border-color: var(--accent-primary);
  background: var(--bg-surface-hover);
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  color: var(--text-main);
  cursor: pointer;
  transition: all 0.15s ease;
}

.theme-toggle:hover {
  background: var(--bg-surface-hover);
  border-color: var(--border-hover);
}

/* Page Main Container */
main, .page {
  max-width: var(--content-width);
  width: calc(100% - 32px);
  margin: 0 auto;
  padding-bottom: 80px;
}

main.index-main {
  padding-top: 40px;
}

/* Page Heading */
.page-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 20px;
  padding-bottom: 24px;
  border-bottom: 2px solid var(--border-color);
}

h1 {
  margin: 0;
  font-size: 36px;
  line-height: 1.25;
  font-weight: 800;
  color: var(--text-heading);
}

.library-count {
  margin: 0;
  color: var(--text-muted);
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  background: var(--bg-surface);
  padding: 6px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-full);
}

/* Field Section */
.field {
  margin-top: 36px;
}

.field-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 20px;
}

h2 {
  margin: 0;
  font-size: 24px;
  line-height: 1.3;
  font-weight: 750;
  color: var(--text-heading);
  display: flex;
  align-items: center;
  gap: 10px;
}

h2::before {
  content: "";
  width: 4px;
  height: 22px;
  background: var(--accent-primary);
  border-radius: 2px;
}

.field-count {
  color: var(--text-muted);
  font-size: 16px;
  font-weight: 500;
}

/* Course Grid */
.course-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 20px;
}

.course-link {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 150px;
  padding: 20px;
  background: var(--bg-surface);
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-lg);
  text-decoration: none;
  box-shadow: var(--shadow-sm);
  transition: all 0.2s ease;
}

.course-link:hover {
  border-color: var(--accent-primary);
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.course-top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}

.course-icon {
  display: grid;
  width: 44px;
  height: 44px;
  place-items: center;
  color: var(--accent-primary);
  background: var(--accent-soft);
  border-radius: var(--radius-md);
}

.arrow-icon {
  color: var(--text-muted);
  transition: transform 0.2s ease, color 0.2s ease;
}

.course-link:hover .arrow-icon {
  color: var(--accent-primary);
  transform: translateX(4px);
}

.course-name {
  display: block;
  font-size: 22px;
  font-weight: 750;
  color: var(--text-heading);
  margin-bottom: 10px;
}

.course-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--text-muted);
  font-size: 15px;
  font-weight: 500;
  padding-top: 12px;
  border-top: 1px solid var(--border-color);
}

/* Course Detail Header */
.course-header {
  padding: 24px 0 20px;
  border-bottom: 1px solid var(--border-color);
}

.header-top-info {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 8px;
}

.course-header h1 {
  font-size: 32px;
}

.course-stat-badge {
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  background: var(--bg-surface);
  padding: 4px 12px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-full);
}

/* Progress Tracker Bar */
.progress-bar-container {
  margin-top: 10px;
  max-width: 400px;
}

.progress-info {
  display: flex;
  justify-content: space-between;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.progress-track {
  width: 100%;
  height: 6px;
  background: var(--border-color);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent-primary);
  border-radius: var(--radius-full);
  transition: width 0.3s ease;
}

/* SVG Layer Map Diagram */
.layer-map-wrap {
  margin-top: 18px;
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 12px;
  box-shadow: var(--shadow-sm);
}

.layer-map {
  width: 100%;
  max-width: 580px;
  height: auto;
  display: block;
  margin: 0 auto;
}

/* Chapter Filter Bar (Non-sticky, in normal page flow right after header!) */
.chapter-filter-section {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 0 8px;
  flex-wrap: wrap;
}

.chapter-tabs-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
}

.chapter-tab-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--bg-surface);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-full);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
}

.chapter-tab-btn:hover {
  color: var(--accent-primary);
  border-color: var(--accent-primary);
}

.chapter-tab-btn.active {
  color: #ffffff;
  background: var(--accent-primary);
  border-color: var(--accent-primary);
}

.tab-badge {
  display: inline-grid;
  min-width: 18px;
  height: 18px;
  padding: 0 5px;
  place-items: center;
  background: rgba(0,0,0,0.08);
  border-radius: var(--radius-full);
  font-size: 11px;
  font-weight: 700;
}

[data-theme="dark"] .tab-badge {
  background: rgba(255,255,255,0.15);
}

.result-status {
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
}

/* Question Content Column */
.question-content {
  padding-top: 16px;
}

.question-group {
  margin-bottom: 40px;
  scroll-margin-top: 80px;
}

.question-group:last-child { margin-bottom: 0; }

.group-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--border-color);
}

.question-group h2 {
  font-size: 24px;
  line-height: 1.3;
  font-weight: 800;
  color: var(--text-heading);
}

.question-group h2::before {
  content: "";
  width: 4px;
  height: 22px;
  background: var(--accent-primary);
  border-radius: 3px;
}

.group-count {
  color: var(--text-muted);
  font-size: 14px;
  font-weight: 600;
  background: var(--bg-subtle);
  padding: 3px 10px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border-color);
}

.question-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Card Details Element */
.question {
  background: var(--bg-surface);
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease;
  overflow: hidden;
}

.question:hover {
  border-color: var(--border-hover);
}

.question[open] {
  border-color: var(--accent-primary);
  box-shadow: var(--shadow-md);
}

.question.random-highlight {
  outline: 3px solid var(--accent-primary);
  outline-offset: 2px;
}

.question.mastered {
  border-left: 5px solid var(--accent-primary);
}

summary {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 14px;
  padding: 18px 22px;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

summary::-webkit-details-marker { display: none; }

.q-number {
  padding: 4px 10px;
  color: var(--accent-badge-text);
  background: var(--accent-soft);
  border-radius: var(--radius-sm);
  font-family: var(--font-mono);
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
}

.q-text {
  font-size: 21px;
  line-height: 1.5;
  font-weight: 700;
  color: var(--text-heading);
}

.master-check-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--radius-full);
  border: 1px solid var(--border-color);
  background: var(--bg-page);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.master-check-btn:hover {
  border-color: var(--accent-primary);
  color: var(--accent-primary);
}

.question.mastered .master-check-btn {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: #ffffff;
}

.chevron-icon {
  color: var(--text-muted);
  transition: transform 0.2s ease;
}

.question[open] .chevron-icon {
  color: var(--accent-primary);
  transform: rotate(180deg);
}

/* Answer Container */
.answer {
  padding: 0 22px 20px 70px;
  color: var(--text-main);
  font-size: 18px;
  line-height: 1.8;
  border-top: 1px solid var(--border-color);
}

.answer-header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 14px 0 10px;
}

.answer-label {
  margin: 0;
  color: var(--accent-primary);
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
}

.copy-btn:hover {
  color: var(--accent-primary);
  border-color: var(--accent-primary);
}

.answer-paragraphs {
  display: grid;
  gap: 14px;
}

.answer-paragraph {
  margin: 0;
  font-size: 18px;
  line-height: 1.8;
}

.answer code {
  padding: 2px 6px;
  color: var(--code-text);
  background: var(--code-bg);
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 600;
}

/* Toast Notification */
.toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 18px;
  color: #ffffff;
  background: var(--text-heading);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  font-size: 14px;
  font-weight: 600;
  opacity: 0;
  transform: translateY(20px);
  transition: all 0.25s ease;
  pointer-events: none;
}

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

.empty-state {
  padding: 48px 20px;
  color: var(--text-muted);
  background: var(--bg-surface);
  border: 1px dashed var(--border-color);
  border-radius: var(--radius-lg);
  text-align: center;
  font-size: 18px;
  font-weight: 600;
}

[hidden] { display: none !important; }

@media (max-width: 680px) {
  html { font-size: 16px; }
  .topbar-inner, main, .page { width: calc(100% - 20px); }
  .topbar-title { display: none; }
  .topbar-divider { display: none; }
  .topbar-center { max-width: 180px; }
  h1, .course-header h1 { font-size: 28px; }
  summary {
    grid-template-columns: auto minmax(0, 1fr) auto;
    padding: 14px;
  }
  .master-check-btn { display: none; }
  .answer {
    padding: 0 14px 18px 14px;
  }
  .course-list { grid-template-columns: 1fr; }
}

/* Print Format */
@media print {
  @page { margin: 15mm; }
  body { color: #000 !important; background: #fff !important; font-size: 12pt; }
  .topbar, .chapter-filter-section, .theme-toggle, .copy-btn, .master-check-btn { display: none !important; }
  .page { width: 100%; max-width: none; }
  .course-header { padding: 0 0 10mm; border: none; }
  .question-group { margin-bottom: 10mm; page-break-inside: avoid; }
  .question { background: none !important; border: 1px solid #ccc !important; margin-bottom: 5mm; page-break-inside: avoid; }
  summary { padding: 4mm 6mm; }
  .chevron-icon { display: none; }
  .answer { padding: 0 6mm 4mm; display: block !important; }
  .answer-paragraphs { gap: 3mm; }
  .answer code { background: #eee !important; color: #000 !important; }
}
