:root {
  color-scheme: light dark;
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-muted: #f0eee8;
  --text: #1b1f28;
  --text-muted: #5c6573;
  --border: #e1ddd4;
  --accent: #2563eb;
  --accent-soft: rgba(37, 99, 235, 0.1);
  --ok: #16a34a;
  --ok-soft: rgba(22, 163, 74, 0.12);
  --warn: #d97706;
  --warn-soft: rgba(217, 119, 6, 0.13);
  --shadow: 0 2px 12px rgba(20, 24, 35, 0.05);
  --radius: 14px;
  --sidebar-w: 276px;
}

html[data-theme="dark"] {
  --bg: #11141a;
  --surface: #181c24;
  --surface-muted: #202632;
  --text: #edf1f7;
  --text-muted: #98a3b4;
  --border: #2c3444;
  --accent: #60a5fa;
  --accent-soft: rgba(96, 165, 250, 0.14);
  --ok: #4ade80;
  --ok-soft: rgba(74, 222, 128, 0.14);
  --warn: #fbbf24;
  --warn-soft: rgba(251, 191, 36, 0.14);
  --shadow: 0 2px 14px rgba(0, 0, 0, 0.36);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

button, input, textarea {
  font: inherit;
  color: inherit;
}

code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
  background: var(--surface-muted);
  padding: 1px 6px;
  border-radius: 6px;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.brand {
  font-weight: 700;
  font-size: 15px;
  color: var(--text);
  text-decoration: none;
  flex-shrink: 0;
}

.brand:hover { color: var(--accent); }

.topbar-sep { color: var(--text-muted); flex-shrink: 0; }

.topbar-title {
  font-size: 14.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

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

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

.btn:hover { background: var(--surface-muted); }
.btn:active { transform: scale(0.98); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}
html[data-theme="dark"] .btn-primary { color: #0b1220; }
.btn-primary:hover { opacity: 0.92; background: var(--accent); }

.btn-menu { display: none; }

/* Shelf View */
.shelf-shell {
  max-width: 1120px;
  margin: 0 auto;
  padding: 22px 18px 48px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.shelf-hero {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.shelf-hero-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
}

.shelf-hero h1 {
  margin: 8px 0 4px;
  font-size: 25px;
  line-height: 1.3;
}

.shelf-io-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

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

.stat-card {
  background: var(--surface-muted);
  border-radius: 12px;
  padding: 12px 14px;
}

.stat-value {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.2;
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: 2px;
}

.shelf-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
}

.search-input {
  flex: 1 1 240px;
  max-width: 360px;
  min-height: 38px;
  padding: 7px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  font-size: 14px;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  min-height: 32px;
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
}

.chip.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}

.course-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.course-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
  cursor: pointer;
  text-align: left;
  transition: transform 0.12s, border-color 0.15s;
}

.course-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
}

.course-card-title {
  margin: 0;
  font-size: 18px;
  line-height: 1.35;
}

.course-card-goal {
  margin: 0;
  font-size: 13.5px;
  color: var(--text-muted);
  flex: 1;
}

.course-card-foot {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.empty-state {
  background: var(--surface);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  padding: 32px 20px;
  text-align: center;
  color: var(--text-muted);
  font-size: 14px;
}

/* Course View */
.layout {
  max-width: 1180px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  gap: 24px;
  padding: 22px 18px 96px;
  align-items: start;
}

.sidebar {
  position: sticky;
  top: 74px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  max-height: calc(100vh - 96px);
  overflow-y: auto;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.sidebar-close { display: none; }

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

.toc-item {
  width: 100%;
  text-align: left;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  transition: background 0.15s;
}
.toc-item:hover { background: var(--surface-muted); }

.toc-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--border);
}
.toc-dot.done { background: var(--ok); }
.toc-dot.doubt { background: var(--warn); }

.toc-num {
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
  flex-shrink: 0;
}

.toc-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main {
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-width: 0;
}

.hero {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
}

.badge-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 10px;
}

.pill {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: var(--surface-muted);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.pill-accent {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: transparent;
}

.pill-warn {
  background: var(--warn-soft);
  color: var(--warn);
  border-color: transparent;
}

.pill-ok {
  background: var(--ok-soft);
  color: var(--ok);
  border-color: transparent;
}

.hero h1 {
  margin: 0 0 6px;
  font-size: 24px;
  line-height: 1.3;
}

.hero-sub {
  margin: 0 0 12px;
  color: var(--text-muted);
  font-size: 14.5px;
}

.hero-goal {
  margin: 0 0 14px;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--surface-muted);
  font-size: 14px;
}

.progress-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.progress-meta {
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12.5px;
  color: var(--text-muted);
}

.progress-bar {
  height: 8px;
  border-radius: 999px;
  background: var(--surface-muted);
  overflow: hidden;
}

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

.sources-box {
  margin-top: 14px;
  font-size: 13px;
  color: var(--text-muted);
}

.sources-box summary { cursor: pointer; font-weight: 600; }
.sources-box ul { margin: 8px 0 0; padding-left: 18px; }
.sources-box a { color: var(--accent); text-decoration: none; }
.sources-box a:hover { text-decoration: underline; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow);
  scroll-margin-top: 78px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.card.is-done { border-color: var(--ok); }
.card.is-doubt { border-color: var(--warn); }
.card.flash {
  box-shadow: 0 0 0 3px var(--accent-soft);
  border-color: var(--accent);
}

.card-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  margin-bottom: 12px;
}

.card-title-group {
  min-width: 0;
  flex: 1 1 280px;
}

.card-num {
  font-size: 12px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.04em;
  margin-bottom: 2px;
}

.card-title {
  margin: 0;
  font-size: 19px;
  line-height: 1.35;
}

.card-question {
  margin: 4px 0 0;
  font-size: 14px;
  color: var(--text-muted);
}

.card-controls {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.status-btn {
  min-height: 34px;
  padding: 4px 10px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
}

.status-btn.active-done {
  background: var(--ok-soft);
  border-color: var(--ok);
  color: var(--ok);
}

.status-btn.active-doubt {
  background: var(--warn-soft);
  border-color: var(--warn);
  color: var(--warn);
}

.prereq-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-bottom: 14px;
  font-size: 12.5px;
  color: var(--text-muted);
}

.prereq-chip {
  border: 1px solid var(--border);
  background: var(--surface-muted);
  border-radius: 8px;
  padding: 2px 9px;
  font-size: 12px;
  cursor: pointer;
  color: var(--accent);
}
.prereq-chip:hover { background: var(--accent-soft); }

.card-summary {
  padding: 10px 14px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 10px 10px 0;
  margin-bottom: 14px;
  font-size: 14.5px;
  font-weight: 500;
}

.block-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 12px;
  margin-bottom: 14px;
}

.block {
  background: var(--surface-muted);
  border-radius: 10px;
  padding: 12px 14px;
}

.block-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.block-body {
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

.card-extra {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.limits-box {
  border: 1px dashed var(--warn);
  background: var(--warn-soft);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
  white-space: pre-wrap;
  word-break: break-word;
}

.terms-box {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 13.5px;
}

.terms-list {
  margin: 6px 0 0;
  padding-left: 18px;
}

.card-source {
  font-size: 12px;
  color: var(--text-muted);
}

.doubt-panel {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid var(--warn);
  background: var(--warn-soft);
  display: none;
}

.doubt-panel.open { display: block; }

.doubt-panel label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
}

.doubt-panel textarea {
  width: 100%;
  min-height: 76px;
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  resize: vertical;
  font-size: 13.5px;
}

.doubt-save-hint {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-muted);
}

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

.section-card h2 {
  margin: 0 0 12px;
  font-size: 18px;
}

.task-box {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 12px;
  background: var(--surface-muted);
}
.task-box:last-child { margin-bottom: 0; }

.task-title {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 6px;
}

.task-prompt {
  font-size: 14px;
  margin-bottom: 10px;
  white-space: pre-wrap;
  word-break: break-word;
}

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

.checklist label {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 13.5px;
  cursor: pointer;
}

.answer-details summary {
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--accent);
}

.answer-body {
  margin-top: 8px;
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13.5px;
  white-space: pre-wrap;
  word-break: break-word;
}

.deferred-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13.5px;
}
.deferred-item:last-child { border-bottom: none; }

.bottom-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 25;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}

.bottom-info {
  font-size: 13px;
  color: var(--text-muted);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(15, 23, 42, 0.55);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-backdrop.open { display: flex; }

.modal {
  width: min(680px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.modal-head h3 { margin: 0; font-size: 16px; }

.modal textarea {
  width: 100%;
  min-height: 240px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-muted);
  font-size: 13px;
  margin-bottom: 12px;
}

.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  align-items: center;
}

.toast {
  position: fixed;
  bottom: 72px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--surface);
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.toast.show { opacity: 1; }

@media (max-width: 820px) {
  .layout {
    grid-template-columns: 1fr;
    padding: 14px 12px 104px;
  }

  .btn-menu { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(82vw, 310px);
    max-height: none;
    z-index: 40;
    border-radius: 0;
    transform: translateX(-105%);
    transition: transform 0.2s ease;
  }

  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: inline-flex; }
}

@media (max-width: 640px) {
  .topbar {
    flex-wrap: wrap;
    padding: 8px 12px;
    gap: 8px;
  }

  .topbar-left {
    flex: 1 1 auto;
    min-width: 0;
  }

  .topbar:has(#btn-back:not([hidden])) .brand,
  .topbar:has(#btn-back:not([hidden])) .topbar-sep {
    display: none;
  }

  .topbar-actions {
    margin-left: auto;
  }

  .btn {
    padding: 5px 10px;
    font-size: 12.5px;
    min-height: 34px;
  }
}
