/* ===== CSS Custom Properties ===== */
:root {
  --font-base: 16px;
  --line-height: 1.8;
  --max-width: 42rem;

  /* Light theme */
  --color-bg: #fafaf9;
  --color-surface: #ffffff;
  --color-text: #1c1917;
  --color-text-secondary: #78716c;
  --color-border: #e7e5e4;
  --color-accent: #2563eb;
  --color-accent-hover: #1d4ed8;

  /* Section tints */
  --color-speech-bg: #eff6ff;
  --color-speech-border: #3b82f6;
  --color-speech-tag: #1d4ed8;
  --color-keypoints-bg: #fffbeb;
  --color-keypoints-border: #f59e0b;
  --color-keypoints-tag: #b45309;
  --color-followup-bg: #fdf2f8;
  --color-followup-border: #ec4899;
  --color-followup-tag: #be185d;
  --color-template-bg: #f0fdf4;
  --color-template-border: #22c55e;
  --color-template-tag: #15803d;

  /* Nav */
  --nav-width: 280px;
  --topbar-height: 48px;

  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.05);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.1);

  /* Code */
  --color-code-bg: #1e1e1e;
  --color-code-text: #d4d4d4;
  --color-inline-code-bg: #f1f5f9;
  --color-inline-code-text: #be123c;
}

[data-theme="dark"] {
  --color-bg: #0c0a09;
  --color-surface: #1c1917;
  --color-text: #e7e5e4;
  --color-text-secondary: #a8a29e;
  --color-border: #292524;
  --color-accent: #60a5fa;
  --color-accent-hover: #93bbfd;

  --color-speech-bg: #172033;
  --color-speech-border: #3b82f6;
  --color-speech-tag: #93bbfd;
  --color-keypoints-bg: #201e0d;
  --color-keypoints-border: #f59e0b;
  --color-keypoints-tag: #fcd34d;
  --color-followup-bg: #1f111a;
  --color-followup-border: #ec4899;
  --color-followup-tag: #f9a8d4;
  --color-template-bg: #0f1f14;
  --color-template-border: #22c55e;
  --color-template-tag: #86efac;

  --color-code-bg: #1a1a1a;
  --color-code-text: #d4d4d4;
  --color-inline-code-bg: #292524;
  --color-inline-code-text: #fda4af;

  --shadow-sm: 0 1px 2px rgba(0,0,0,0.3);
  --shadow-md: 0 4px 6px -1px rgba(0,0,0,0.4);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  font-size: var(--font-base);
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue",
               Helvetica, Arial, sans-serif;
  line-height: var(--line-height);
  color: var(--color-text);
  background: var(--color-bg);
  transition: background 0.3s, color 0.3s;
  overflow-x: hidden;
}

a {
  color: var(--color-accent);
  text-decoration: none;
}

ul, ol {
  padding-left: 1.5rem;
}

/* ===== Top Navigation Bar ===== */
.topbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--topbar-height);
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  display: flex;
  align-items: center;
  padding: 0 0.75rem;
  gap: 0.5rem;
  z-index: 100;
  box-shadow: var(--shadow-sm);
}

.topbar-title {
  flex: 1;
  font-size: 0.95rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.topbar-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--color-text);
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  flex-shrink: 0;
  transition: background 0.15s;
}
.topbar-btn:hover {
  background: var(--color-border);
}
.topbar-btn.active {
  background: var(--color-accent);
  color: #fff;
}

/* Hamburger */
.hamburger {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 18px;
}
.hamburger span {
  display: block;
  height: 2px;
  background: var(--color-text);
  border-radius: 1px;
  transition: transform 0.2s, opacity 0.2s;
}

/* Reading progress */
.reading-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 2px;
  background: var(--color-accent);
  width: 0;
  transition: width 0.1s linear;
}

/* ===== Navigation Drawer ===== */
.nav-drawer {
  position: fixed;
  top: 0;
  left: 0;
  width: 85vw;
  max-width: 320px;
  height: 100%;
  height: 100dvh;
  background: var(--color-surface);
  z-index: 200;
  transform: translateX(-100%);
  transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-md);
}
.nav-drawer.open {
  transform: translateX(0);
}

.drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--color-border);
  flex-shrink: 0;
}
.drawer-header h2 {
  font-size: 1.1rem;
  font-weight: 700;
}

.drawer-close {
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  color: var(--color-text);
  font-size: 1.25rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}
.drawer-close:hover {
  background: var(--color-border);
}

.drawer-search {
  padding: 0.5rem 1rem;
  flex-shrink: 0;
}
.drawer-search input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: var(--color-bg);
  color: var(--color-text);
  font-size: 0.875rem;
  outline: none;
  transition: border-color 0.15s;
}
.drawer-search input:focus {
  border-color: var(--color-accent);
}

.drawer-progress {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0 1rem 0.65rem;
  flex-shrink: 0;
}
.drawer-progress .study-progress-text {
  font-size: 0.76rem;
}
.drawer-reset-btn {
  padding: 0.22rem 0.6rem;
  white-space: nowrap;
}

.drawer-nav {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 0.5rem 0 1rem;
}

/* Accordion category */
.nav-category {
  border-bottom: 1px solid var(--color-border);
}
.nav-category:last-child {
  border-bottom: none;
}

.nav-category summary {
  padding: 0.6rem 1rem;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.nav-category summary::-webkit-details-marker {
  display: none;
}
.nav-category summary::after {
  content: '▾';
  font-size: 0.7rem;
  transition: transform 0.2s;
}
.nav-category[open] summary::after {
  transform: rotate(-180deg);
}

.nav-category ul {
  list-style: none;
  padding: 0 0 0.5rem;
}

.nav-module-item {
  display: flex;
  align-items: center;
  padding-left: 0.75rem;
  border-left: 3px solid transparent;
}
.nav-module-item.done {
  background: rgba(34, 197, 94, 0.08);
}
.nav-module-item.done .nav-link {
  color: var(--color-text-secondary);
}

.nav-category li:not(.nav-module-item) .nav-link {
  padding-left: 1.5rem;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.45rem 1rem 0.45rem 0.35rem;
  font-size: 0.875rem;
  color: var(--color-text);
  text-decoration: none;
  transition: background 0.1s;
  border-left: 3px solid transparent;
}
.nav-link:hover {
  background: var(--color-bg);
}
.nav-link.active {
  background: var(--color-speech-bg);
  border-left-color: var(--color-accent);
  font-weight: 600;
  color: var(--color-accent);
}
.nav-link .module-num {
  color: var(--color-text-secondary);
  font-size: 0.75rem;
  min-width: 1.5rem;
  text-align: right;
}
.nav-link .module-priority {
  font-size: 0.65rem;
  margin-left: auto;
}

.nav-sub {
  font-size: 0.8rem;
  padding-left: 2.5rem;
}

/* Drawer overlay */
.drawer-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 199;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}
.drawer-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

/* ===== Main Content Area ===== */
.main {
  padding-top: var(--topbar-height);
  padding-bottom: 3rem;
  min-height: 100vh;
  min-height: 100dvh;
  transition: padding 0.3s;
}

/* ===== Home View (Module List) ===== */
.home-view {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 1.5rem 1rem;
}

.home-hero {
  text-align: center;
  padding: 2rem 0 1.5rem;
}
.home-hero h1 {
  font-size: 1.5rem;
  font-weight: 800;
  margin-bottom: 0.25rem;
}
.home-hero p {
  color: var(--color-text-secondary);
  font-size: 0.875rem;
}

.progress-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.65rem;
  margin-top: 0.9rem;
  flex-wrap: wrap;
}

.study-progress-text {
  color: var(--color-text-secondary);
  font-size: 0.8rem;
}

.reset-progress-btn {
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  border-radius: 999px;
  padding: 0.28rem 0.75rem;
  font-size: 0.76rem;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s;
}
.reset-progress-btn:hover {
  border-color: var(--color-accent);
  color: var(--color-accent);
}

.home-category {
  margin-bottom: 1.5rem;
}
.home-category h2 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-secondary);
  margin-bottom: 0.5rem;
  padding: 0 0.25rem;
}

.home-card {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 10px;
  margin-bottom: 0.4rem;
  text-decoration: none;
  color: var(--color-text);
  transition: border-color 0.15s, box-shadow 0.15s;
  cursor: pointer;
}
.home-card:hover {
  border-color: var(--color-accent);
  box-shadow: var(--shadow-sm);
}
.home-card.done {
  border-color: rgba(34, 197, 94, 0.5);
  background: #f0fdf4;
}
[data-theme="dark"] .home-card.done {
  background: #0f1f14;
}
.home-card:active {
  transform: scale(0.995);
}

.home-card-link {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
  color: inherit;
  text-decoration: none;
}

.home-card .card-num {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--color-text-secondary);
  min-width: 2rem;
}
.home-card .card-title {
  flex: 1;
  font-size: 0.9rem;
  font-weight: 500;
}
.home-card .card-meta {
  font-size: 0.7rem;
  color: var(--color-text-secondary);
  white-space: nowrap;
}
.home-card .card-priority {
  color: #f59e0b;
  font-size: 0.65rem;
}

.module-progress-check {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 1.35rem;
  height: 1.35rem;
  cursor: pointer;
}
.module-progress-check input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}
.module-progress-check span {
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  border: 1.5px solid var(--color-border);
  background: var(--color-bg);
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
.module-progress-check input:checked + span {
  border-color: #22c55e;
  background: #22c55e;
  box-shadow: inset 0 0 0 3px var(--color-surface);
}
.module-progress-check:hover span {
  border-color: var(--color-accent);
}

/* ===== Content View (Reading) ===== */
.content-view {
  display: none;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 1rem 1rem 2rem;
}
.content-view.visible {
  display: block;
}

/* In-page TOC */
.toc {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  padding: 0;
  margin-bottom: 1.5rem;
}
.toc summary {
  padding: 0.6rem 1rem;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  list-style: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.toc summary::-webkit-details-marker {
  display: none;
}
.toc summary::after {
  content: '▾';
  font-size: 0.7rem;
  transition: transform 0.2s;
}
.toc[open] summary::after {
  transform: rotate(-180deg);
}
.toc ul {
  list-style: none;
  padding: 0 1rem 0.75rem;
  border-top: 1px solid var(--color-border);
  padding-top: 0.5rem;
}
.toc ul li {
  padding: 0.15rem 0;
}
.toc ul li a {
  font-size: 0.8rem;
  color: var(--color-text-secondary);
  text-decoration: none;
  display: block;
  padding: 0.2rem 0.5rem;
  border-radius: 4px;
  transition: color 0.1s, background 0.1s;
}
.toc ul li a:hover,
.toc ul li a:focus {
  color: var(--color-accent);
  background: var(--color-speech-bg);
}
.toc .toc-H3 {
  padding-left: 1rem;
  font-size: 0.75rem;
}

/* ===== Markdown Body (Rendered Content) ===== */
.markdown-body h1 {
  font-size: 1.5rem;
  font-weight: 800;
  margin: 0.5rem 0 1rem;
  line-height: 1.3;
}

.markdown-body h2 {
  font-size: 1.25rem;
  font-weight: 700;
  margin: 2em 0 0.75em;
  padding-bottom: 0.3em;
  border-bottom: 1px solid var(--color-border);
  line-height: 1.35;
}

.markdown-body h3 {
  font-size: 1.1rem;
  font-weight: 600;
  margin: 1.5em 0 0.5em;
  line-height: 1.4;
}

.markdown-body h4 {
  font-size: 1rem;
  font-weight: 600;
  margin: 1.2em 0 0.4em;
}

.markdown-body p {
  margin: 0.75em 0;
}

.markdown-body blockquote {
  margin: 0.75em 0;
  padding: 0.5rem 1rem;
  border-left: 3px solid var(--color-accent);
  background: var(--color-surface);
  border-radius: 0 6px 6px 0;
  color: var(--color-text-secondary);
  font-size: 0.9rem;
}

.markdown-body hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 1.5rem 0;
}

/* ===== Special Sections ===== */
.section-speech,
.section-keypoints,
.section-followup,
.section-template {
  margin: 1rem 0;
  padding: 0.75rem 1rem;
  border-radius: 0 8px 8px 0;
  border-left: 4px solid;
}

.section-speech {
  background: var(--color-speech-bg);
  border-left-color: var(--color-speech-border);
}
.section-keypoints {
  background: var(--color-keypoints-bg);
  border-left-color: var(--color-keypoints-border);
}
.section-followup {
  background: var(--color-followup-bg);
  border-left-color: var(--color-followup-border);
}
.section-template {
  background: var(--color-template-bg);
  border-left-color: var(--color-template-border);
}

/* Section tag badge */
.section-tag {
  display: inline-block;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 0.1rem 0.5rem;
  border-radius: 4px;
  margin-bottom: 0.5rem;
  letter-spacing: 0.02em;
}
.section-speech .section-tag {
  color: var(--color-speech-tag);
  background: rgba(59,130,246,0.1);
}
.section-keypoints .section-tag {
  color: var(--color-keypoints-tag);
  background: rgba(245,158,11,0.1);
}
.section-followup .section-tag {
  color: var(--color-followup-tag);
  background: rgba(236,72,153,0.1);
}
.section-template .section-tag {
  color: var(--color-template-tag);
  background: rgba(34,197,94,0.1);
}

/* Section headings inside special sections */
.section-speech h3,
.section-speech h4,
.section-keypoints h3,
.section-keypoints h4,
.section-followup h3,
.section-followup h4,
.section-template h3,
.section-template h4 {
  margin-top: 0;
}

/* ===== Code Blocks ===== */
.markdown-body code {
  font-family: "SF Mono", "Fira Code", "Cascadia Code", Consolas, "Liberation Mono", monospace;
  font-size: 0.85em;
}

.markdown-body p code,
.markdown-body li code,
.markdown-body td code {
  background: var(--color-inline-code-bg);
  color: var(--color-inline-code-text);
  padding: 0.1em 0.4em;
  border-radius: 3px;
  white-space: nowrap;
}

.markdown-body pre {
  background: var(--color-code-bg);
  color: var(--color-code-text);
  padding: 1rem;
  border-radius: 8px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 1rem 0;
  font-size: 0.8rem;
  line-height: 1.6;
}

.markdown-body pre code {
  background: none;
  color: inherit;
  padding: 0;
  white-space: pre;
  font-size: inherit;
}

/* ===== Tables ===== */
.markdown-body .table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 1rem 0;
  border-radius: 8px;
  border: 1px solid var(--color-border);
}

.markdown-body table {
  border-collapse: collapse;
  width: 100%;
  min-width: 500px;
  font-size: 0.8rem;
}

.markdown-body th,
.markdown-body td {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--color-border);
  text-align: left;
}

.markdown-body th {
  background: var(--color-surface);
  font-weight: 600;
  white-space: nowrap;
}

.markdown-body tr:nth-child(even) td {
  background: var(--color-bg);
}

/* ===== Images ===== */
.markdown-body img {
  max-width: 100%;
  height: auto;
  border-radius: 6px;
}

/* ===== Bottom Navigation ===== */
.bottom-nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.5rem;
  margin-top: 2.5rem;
  padding-top: 1rem;
  border-top: 1px solid var(--color-border);
}

.bottom-nav a {
  font-size: 0.8rem;
  color: var(--color-accent);
  text-decoration: none;
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  white-space: nowrap;
  transition: border-color 0.15s;
}
.bottom-nav a:hover {
  border-color: var(--color-accent);
}
.bottom-nav .nav-center {
  color: var(--color-text-secondary);
  font-size: 0.75rem;
}

/* ===== Back to Top ===== */
.back-to-top {
  position: fixed;
  bottom: 1.5rem;
  right: 1rem;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text);
  font-size: 1.2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transform: translateY(10px);
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 50;
}
.back-to-top.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

/* ===== Loading ===== */
.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem 1rem;
  color: var(--color-text-secondary);
  font-size: 0.9rem;
}
.loading::before {
  content: '';
  width: 20px;
  height: 20px;
  border: 2px solid var(--color-border);
  border-top-color: var(--color-accent);
  border-radius: 50%;
  margin-right: 0.6rem;
  animation: spin 0.6s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Error / Not Found ===== */
.error-msg {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--color-text-secondary);
}
.error-msg p {
  margin-bottom: 1rem;
}
.error-msg a {
  color: var(--color-accent);
}

/* ===== Empty State (search) ===== */
.empty-state {
  text-align: center;
  padding: 1.5rem 1rem;
  color: var(--color-text-secondary);
  font-size: 0.85rem;
}

/* ===== Desktop: persistent sidebar ===== */
@media (min-width: 768px) {
  .topbar {
    left: var(--nav-width);
  }
  .topbar .hamburger-btn {
    display: none;
  }

  .nav-drawer {
    transform: translateX(0);
    width: var(--nav-width);
    max-width: var(--nav-width);
    box-shadow: none;
    border-right: 1px solid var(--color-border);
  }
  .drawer-header .drawer-close {
    display: none;
  }
  .drawer-overlay {
    display: none;
  }

  .main {
    padding-left: var(--nav-width);
  }

  .bottom-nav {
    max-width: var(--max-width);
  }
}

/* ===== Font size adjustments ===== */
html[data-font="small"] {
  --font-base: 14px;
}
html[data-font="large"] {
  --font-base: 18px;
}

/* ===== Print ===== */
@media print {
  .topbar, .nav-drawer, .drawer-overlay, .bottom-nav, .back-to-top {
    display: none;
  }
  .main {
    padding-left: 0;
    padding-top: 0;
  }
  .markdown-body {
    max-width: 100%;
  }
}
