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

/* ─── Variables ────────────────────────────────────────────────────────────── */

:root {
  --bg:           #ffffff;
  --bg-sidebar:   #f9fafb;
  --bg-hover:     #f3f4f6;
  --bg-active:    #eef2ff;
  --text:         #111827;
  --text-2:       #6b7280;
  --text-active:  #4338ca;
  --accent:       #4f46e5;
  --border:       #e5e7eb;
  --sidebar-w:    272px;
  --toc-w:        220px;
  --header-h:     56px;
  --font:         -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --mono:         'Menlo', 'Monaco', 'Consolas', monospace;
  --radius:       6px;
}

[data-theme="dark"] {
  --bg:           #0f172a;
  --bg-sidebar:   #1e293b;
  --bg-hover:     #334155;
  --bg-active:    rgba(99,102,241,0.15);
  --text:         #f1f5f9;
  --text-2:       #94a3b8;
  --text-active:  #818cf8;
  --accent:       #818cf8;
  --border:       #334155;
}

/* ─── Base ──────────────────────────────────────────────────────────────────── */

html { height: 100%; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  font-size: 15px;
  line-height: 1.6;
}

/* ─── Header ────────────────────────────────────────────────────────────────── */

#header {
  height: var(--header-h);
  flex-shrink: 0;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 15px;
}

.logo { font-size: 20px; }

#theme-toggle {
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  font-size: 15px;
  color: var(--text);
  transition: background 0.15s;
}
#theme-toggle:hover { background: var(--bg-hover); }

/* ─── Layout ────────────────────────────────────────────────────────────────── */

#layout {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

/* ─── Left Sidebar ──────────────────────────────────────────────────────────── */

#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

/* ─── Search ────────────────────────────────────────────────────────────────── */

#search-container {
  position: relative;
  flex: 1;
  max-width: 420px;
  margin: 0 24px;
}

#search-input-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  transition: border-color 0.15s, box-shadow 0.15s;
}
#search-input-wrap:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(79,70,229,0.12);
}
[data-theme="dark"] #search-input-wrap:focus-within {
  box-shadow: 0 0 0 2px rgba(129,140,248,0.15);
}

.search-icon { color: var(--text-2); flex-shrink: 0; }

#search-input {
  border: none;
  outline: none;
  background: none;
  font-size: 13px;
  font-family: var(--font);
  color: var(--text);
  flex: 1;
  min-width: 0;
}
#search-input::placeholder { color: var(--text-2); }

#search-kbd {
  font-family: var(--mono);
  font-size: 10px;
  padding: 1px 5px;
  border: 1px solid var(--border);
  border-radius: 3px;
  color: var(--text-2);
  background: var(--bg);
  flex-shrink: 0;
  line-height: 1.6;
}
#search-input:focus ~ #search-kbd { display: none; }

#search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  max-height: 70vh;
  overflow-y: auto;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  z-index: 200;
}
[data-theme="dark"] #search-results {
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
}
#search-results.hidden { display: none; }

.search-status {
  padding: 16px;
  text-align: center;
  color: var(--text-2);
  font-size: 12.5px;
}

.search-result {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}
.search-result:last-child { border-bottom: none; }
.search-result:hover { background: var(--bg-hover); }

.search-result-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}

.search-result-num {
  font-size: 11px;
  color: var(--text-2);
  flex-shrink: 0;
}

.search-result-snippet {
  font-size: 12px;
  color: var(--text-2);
  margin-top: 4px;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

#search-results mark {
  background: rgba(79,70,229,0.15);
  color: var(--accent);
  border-radius: 2px;
  padding: 0 1px;
}
[data-theme="dark"] #search-results mark {
  background: rgba(129,140,248,0.2);
  color: var(--text-active);
}

#progress-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-2);
  font-weight: 500;
  position: sticky;
  top: 0;
  background: var(--bg-sidebar);
  z-index: 10;
}
#progress-count { font-weight: 600; color: var(--text); }

#course-nav { flex: 1; padding-bottom: 24px; }

/* Sections */

.section { border-bottom: 1px solid var(--border); }

.section-header {
  display: flex;
  align-items: center;
  padding: 10px 16px;
  cursor: pointer;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  user-select: none;
  gap: 6px;
  transition: background 0.1s;
}
.section-header:hover { background: var(--bg-hover); }

.section-title { flex: 1; line-height: 1.4; }
.section-chevron { color: var(--text-2); font-size: 13px; transition: transform 0.2s; }

.section-lessons { display: none; }
.section-lessons.expanded { display: block; }

/* Lessons */

.lesson-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 16px 7px 22px;
  cursor: pointer;
  font-size: 12.5px;
  color: var(--text-2);
  transition: background 0.1s, color 0.1s;
  position: relative;
  line-height: 1.4;
}
.lesson-item:hover { background: var(--bg-hover); color: var(--text); }

.lesson-item.active {
  background: var(--bg-active);
  color: var(--text-active);
  font-weight: 500;
}
.lesson-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: var(--accent);
  border-radius: 0 2px 2px 0;
}

.lesson-num {
  font-size: 11.5px;
  color: var(--text-2);
  min-width: 26px;
  flex-shrink: 0;
}
.lesson-item.active .lesson-num { color: var(--text-active); }
.lesson-title { flex: 1; }
.lesson-check { font-size: 11px; color: #10b981; font-weight: 700; flex-shrink: 0; }

/* ─── Main Content ──────────────────────────────────────────────────────────── */

#main {
  flex: 1;
  overflow-y: auto;
  min-width: 0;
  scroll-behavior: smooth;
}

#content-wrapper {
  max-width: 780px;
  margin: 0 auto;
  padding: 44px 48px 100px;
}

#content-header {
  margin-bottom: 36px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--border);
}

#lesson-title {
  font-size: 30px;
  font-weight: 700;
  line-height: 1.25;
  margin-bottom: 12px;
  letter-spacing: -0.3px;
}

#lesson-meta {
  display: flex;
  gap: 16px;
  color: var(--text-2);
  font-size: 13px;
  align-items: center;
}

/* ─── Markdown Styles ───────────────────────────────────────────────────────── */

#content h1 { font-size: 23px; font-weight: 700; margin: 36px 0 16px; letter-spacing: -0.2px; }
#content h2 { font-size: 19px; font-weight: 700; margin: 32px 0 14px; letter-spacing: -0.1px; }
#content h3 { font-size: 16px; font-weight: 600; margin: 24px 0 10px; }
#content h4 { font-size: 14px; font-weight: 600; margin: 18px 0 8px; }

#content p { margin: 0 0 16px; line-height: 1.75; }

#content a { color: var(--accent); text-decoration: none; }
#content a:hover { text-decoration: underline; }

#content ul, #content ol { margin: 0 0 16px 24px; }
#content li { margin-bottom: 6px; line-height: 1.7; }
#content li > ul, #content li > ol { margin-top: 6px; margin-bottom: 4px; }

#content blockquote {
  margin: 20px 0;
  padding: 14px 18px;
  border-left: 4px solid var(--accent);
  background: var(--bg-active);
  border-radius: 0 var(--radius) var(--radius) 0;
  font-size: 14px;
}
[data-theme="dark"] #content blockquote { background: rgba(99,102,241,0.1); }
#content blockquote p:last-child { margin-bottom: 0; }
#content blockquote strong { color: var(--text); }

/* Code */

#content pre {
  background: #f8fafc;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  overflow-x: auto;
  margin: 0 0 20px;
  line-height: 1.6;
}
[data-theme="dark"] #content pre { background: #1e293b; }
#content pre code { font-family: var(--mono); font-size: 13px; background: none; border: none; padding: 0; }

#content p code, #content li code, #content td code, #content th code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--bg-hover);
  padding: 2px 5px;
  border-radius: 4px;
  border: 1px solid var(--border);
}
[data-theme="dark"] #content p code,
[data-theme="dark"] #content li code,
[data-theme="dark"] #content td code { background: #334155; border-color: #475569; }

/* Tables */

#content table { width: 100%; border-collapse: collapse; margin: 0 0 24px; font-size: 14px; }
#content th {
  text-align: left;
  padding: 10px 14px;
  background: var(--bg-sidebar);
  border: 1px solid var(--border);
  font-weight: 600;
  font-size: 13px;
  white-space: nowrap;
}
#content td { padding: 9px 14px; border: 1px solid var(--border); vertical-align: top; }
#content tr:nth-child(even) td { background: var(--bg-sidebar); }
[data-theme="dark"] #content tr:nth-child(even) td { background: rgba(255,255,255,0.02); }

/* HR */
#content hr { border: none; border-top: 1px solid var(--border); margin: 28px 0; }

/* Mermaid */
.mermaid-container { margin: 24px 0; text-align: center; overflow-x: auto; }
.mermaid svg { max-width: 100%; height: auto; }

/* Loading / Error */
.loading, .error {
  padding: 60px 20px;
  text-align: center;
  color: var(--text-2);
  font-size: 15px;
}
.error { color: #ef4444; }

/* ─── Nav Buttons ───────────────────────────────────────────────────────────── */

#nav-buttons {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 56px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
  gap: 12px;
}

.nav-btn {
  padding: 9px 16px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 230px;
}
.nav-btn:hover:not(:disabled) { background: var(--bg-hover); }
.nav-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.complete-btn {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
  font-weight: 500;
  flex-shrink: 0;
}
.complete-btn:hover:not(:disabled) { opacity: 0.88; background: var(--accent); }
.complete-btn.completed { background: #059669; border-color: #059669; }

/* ─── TOC Sidebar ───────────────────────────────────────────────────────────── */

#toc-sidebar {
  width: var(--toc-w);
  min-width: var(--toc-w);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 24px 0 40px;
}

.toc-header {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--text-2);
  padding: 0 20px 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}

.toc-item {
  display: block;
  padding: 5px 20px;
  font-size: 12.5px;
  color: var(--text-2);
  text-decoration: none;
  line-height: 1.45;
  transition: color 0.1s;
}
.toc-item:hover { color: var(--accent); }
.toc-h3 { padding-left: 30px; font-size: 12px; }

.toc-back {
  display: block;
  padding: 10px 20px 0;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-2);
  text-decoration: none;
  border-top: 1px solid var(--border);
}
.toc-back:hover { color: var(--accent); }
.toc-empty { padding: 8px 20px; font-size: 12px; color: var(--text-2); }

/* ─── Scrollbars ────────────────────────────────────────────────────────────── */

::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-2); }

/* ─── GitHub Star Button ────────────────────────────────────────────────────── */

.header-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  text-decoration: none;
  background: var(--bg);
  transition: background 0.15s, border-color 0.15s;
  white-space: nowrap;
}
.header-btn:hover { background: var(--bg-hover); border-color: var(--text-2); }
.header-btn svg { flex-shrink: 0; }

#star-count {
  font-variant-numeric: tabular-nums;
  color: var(--text-2);
  font-size: 12px;
}

/* ─── Responsive ────────────────────────────────────────────────────────────── */

/* ─── Contribute Bar ────────────────────────────────────────────────────────── */

#contribute-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 10px;
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  font-size: 12.5px;
  color: var(--text-2);
}

.contribute-label { color: var(--text-2); }
.contribute-sep { color: var(--border); user-select: none; }

.contribute-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--text-2);
  text-decoration: none;
  transition: color 0.15s;
}
.contribute-link:hover { color: var(--accent); }
.contribute-link svg { flex-shrink: 0; }

/* ─── Mobile Footer ─────────────────────────────────────────────────────────── */

#mobile-footer {
  display: none;
}

.footer-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-2);
  text-decoration: none;
  font-size: 13px;
  font-weight: 500;
  transition: color 0.15s;
}
.footer-btn:hover { color: var(--accent); }
.footer-btn svg { flex-shrink: 0; }

/* ─── Menu Toggle Button ────────────────────────────────────────────────────── */

#menu-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  font-size: 16px;
  color: var(--text);
  line-height: 1;
  transition: background 0.15s;
}
#menu-toggle:hover { background: var(--bg-hover); }

/* ─── Sidebar Overlay ───────────────────────────────────────────────────────── */

#sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 199;
}
#sidebar-overlay.active { display: block; }

/* ─── Responsive ────────────────────────────────────────────────────────────── */

@media (max-width: 1200px) { #toc-sidebar { display: none; } }

@media (max-width: 820px) {
  #menu-toggle { display: block; }

  /* Compact header — single row, no wrapping */
  #header { padding: 0 12px; gap: 8px; }
  .header-left { gap: 6px; font-size: 13px; min-width: 0; flex: 1; overflow: hidden; }
  .logo-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .header-right { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

  /* Hide GitHub label text on mobile, keep icon + count */
  .header-btn .gh-label { display: none; }
  .header-btn { padding: 5px 8px; gap: 4px; }

  #search-container { margin: 0 8px; max-width: none; }
  #search-input { font-size: 12px; }

  #sidebar {
    position: fixed;
    top: var(--header-h);
    left: 0;
    bottom: 0;
    z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 2px 0 12px rgba(0,0,0,0.15);
  }
  #sidebar.open { transform: translateX(0); }

  /* Prevent code blocks from breaking layout */
  #main { overflow-x: hidden; }
  #content pre { max-width: 100%; overflow-x: auto; font-size: 12px; }
  #content table { display: block; overflow-x: auto; }

  #content-wrapper { padding: 24px 16px 80px; }

  #mobile-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    height: 48px;
    border-top: 1px solid var(--border);
    background: var(--bg);
    z-index: 10;
  }
}
