/* =============================================================================
   Course HTML Export - Style (refactored)
   Features: Responsive, Dark Mode, Clean Typography
   ============================================================================= */

:root {
  /* Light */
  --bg: #ffffff;
  --bg2: #f8f9fa;
  --text: #2e3436;
  --muted: #6c757d;
  --link: #0077cc;
  --link-hover: #005fa3;
  --border: #dee2e6;
  --code-bg: #f4f4f4;
  --code-border: #e1e4e8;
  --toc-bg: #f8f9fa;
  --shadow: rgba(0, 0, 0, 0.1);
  --hl-bg: #fff3cd;
}

[data-theme="dark"] {
  --bg: #1a1a2e;
  --bg2: #16213e;
  --text: #e4e4e4;
  --muted: #a0a0a0;
  --link: #64b5f6;
  --link-hover: #90caf9;
  --border: #3a3a5c;
  --code-bg: #0f0f23;
  --code-border: #3a3a5c;
  --toc-bg: #16213e;
  --shadow: rgba(0, 0, 0, 0.3);
  --hl-bg: #3d3d00;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }

body {
  margin: 0;
  padding: 2rem;
  max-width: 100%;
  line-height: 1.7;
  font: 16px/1.7 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  transition: background-color 0.3s ease, color 0.3s ease;
}

h1, h2, h3, h4, h5, h6 {
  margin: 2rem 0 1rem;
  font-weight: 600;
  line-height: 1.3;
}

h1 { font-size: 2.2rem; border-bottom: 2px solid var(--border); padding-bottom: 0.5rem; }
h2 { font-size: 1.8rem; border-bottom: 1px solid var(--border); padding-bottom: 0.3rem; }
h3 { font-size: 1.4rem; }
h4 { font-size: 1.2rem; }

p { margin: 1rem 0; }

a { color: var(--link); text-decoration: none; }
a:hover { color: var(--link-hover); text-decoration: underline; }

/* =============================================================================
   Table of Contents (Sidebar)
   ============================================================================= */

#TOC {
  position: fixed;
  top: 0;
  left: 0;
  width: 260px;
  height: 100vh;
  overflow-y: auto;
  padding: 1.2rem 0.75rem;
  background: var(--toc-bg);
  border-right: 1px solid var(--border);
  font-size: 0.92rem;
  transition: transform 0.3s ease, background-color 0.3s ease;
  z-index: 150;
}

#TOC ul { list-style: none; margin: 0; padding-left: 0.25rem; }
#TOC > ul > li { margin: 0.28rem 0; }
#TOC > ul > li > a { font-weight: 700; font-size: 0.98rem; padding: 0.15rem 0.35rem; }

#TOC li ul { margin: 0.15rem 0 0 0.6rem; font-size: 0.86rem; }
#TOC li ul li { margin: 0.12rem 0; }

#TOC a {
  color: var(--text);
  display: block;
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
  line-height: 1.25;
  transition: background-color 0.18s ease;
}

#TOC a:hover { background: var(--bg2); color: var(--link); text-decoration: none; }
#TOC li a { font-weight: 500; }

.heading-number,
#TOC .toc-number {
  color: var(--muted);
  margin-right: 0.35rem;
  font-weight: 700;
}

/* Heading anchor link (appears on hover) */
.heading-anchor {
  margin-left: 0.5rem;
  color: var(--muted);
  opacity: 0;
  font-weight: 600;
  transition: opacity 180ms ease, transform 180ms ease;
}

:is(h1,h2,h3,h4,h5,h6):hover .heading-anchor { opacity: 1; }

/* Active TOC entry */
#TOC a.active { background: var(--link); color: #fff; }
#TOC a.active .toc-number { color: rgba(255,255,255,0.9); }

/* =============================================================================
   Code Blocks
   ============================================================================= */

pre {
  position: relative;
  background: var(--code-bg);
  color: var(--text);
  padding: 1rem 1rem 0.75rem;
  border-radius: 8px;
  border: 1px solid var(--code-border);
  overflow-x: auto;
  margin: 1.5rem 0;
  box-shadow: 0 2px 8px var(--shadow);
  transition: background-color 0.3s ease, border-color 0.3s ease;
}

pre code {
  background: none;
  padding: 0;
  border: 0;
  font-size: 0.9rem;
}

code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  background: var(--code-bg);
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  font-size: 0.9em;
}

.copy-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  padding: 0.3rem 0.6rem;
  font-size: 0.75rem;
  font-family: inherit;
  background: var(--bg2);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.2s ease, background-color 0.2s ease, color 0.2s ease;
}

pre:hover .copy-btn { opacity: 1; }
.copy-btn:hover { background: var(--border); color: var(--text); }
.copy-btn.copied { background: #28a745; color: #fff; border-color: #28a745; opacity: 1; }

/* =============================================================================
   Tables / Quotes
   ============================================================================= */

table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5rem 0;
  font-size: 0.95rem;
}

th, td { padding: 0.75rem; text-align: left; border: 1px solid var(--border); }
th { background: var(--bg2); font-weight: 600; }
tr:nth-child(even) { background: var(--bg2); }

blockquote {
  margin: 1.5rem 0;
  padding: 1rem 1.5rem;
  background: var(--bg2);
  border-left: 4px solid var(--link);
  border-radius: 0 8px 8px 0;
}
blockquote p { margin: 0; }

/* =============================================================================
   Floating action buttons
   ============================================================================= */

:is(.theme-toggle, .menu-toggle, .search-toggle) {
  position: fixed;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.3s ease, transform 0.2s ease;
  z-index: 200;
}

:is(.theme-toggle, .search-toggle):hover { transform: scale(1.1); }

.theme-toggle { top: 1rem; right: 1rem; font-size: 1.2rem; }
.search-toggle { top: 1rem; right: 4rem; font-size: 1.1rem; }
.menu-toggle { top: 1rem; left: 1rem; font-size: 1.4rem; display: none; }

/* =============================================================================
   Search Overlay
   ============================================================================= */

.search-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 1000;
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding-top: 15vh;
}

.search-overlay.open { display: flex; }

.search-box {
  background: var(--bg);
  border-radius: 12px;
  box-shadow: 0 8px 32px var(--shadow);
  width: min(600px, 90%);
  overflow: hidden;
  border: 1px solid var(--border);
}

.search-input-wrapper {
  display: flex;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid var(--border);
  gap: 0.75rem;
}

.search-icon { font-size: 1.2rem; color: var(--muted); }

.search-input {
  flex: 1;
  border: none;
  background: none;
  font-size: 1.1rem;
  color: var(--text);
  outline: none;
  font-family: inherit;
}

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

.search-info { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; color: var(--muted); }
.search-count { min-width: 60px; text-align: right; }

.search-nav-btn {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 4px;
  width: 28px;
  height: 28px;
  cursor: pointer;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.9rem;
}

.search-nav-btn:hover { background: var(--border); }
.search-nav-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.search-close {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--muted);
  padding: 0.25rem;
  line-height: 1;
}

.search-close:hover { color: var(--text); }

.search-results { max-height: 400px; overflow-y: auto; padding: 0.5rem; }

.search-result-item {
  padding: 0.75rem 1rem;
  cursor: pointer;
  border-radius: 6px;
  margin-bottom: 0.25rem;
}

.search-result-item:hover { background: var(--bg2); }
.search-result-item.active { background: var(--link); color: #fff; }

.search-result-context {
  font-size: 0.9rem;
  color: var(--muted);
  margin-top: 0.25rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-result-item.active .search-result-context { color: rgba(255,255,255,0.8); }
.search-result-section { font-weight: 600; font-size: 0.95rem; }

.search-empty { padding: 2rem; text-align: center; color: var(--muted); }

.search-hint {
  padding: 0.75rem 1rem;
  font-size: 0.8rem;
  color: var(--muted);
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: center;
  gap: 1.5rem;
}

.search-hint kbd {
  background: var(--bg2);
  padding: 0.15rem 0.4rem;
  border-radius: 3px;
  font-size: 0.75rem;
  border: 1px solid var(--border);
}

.search-highlight { background: var(--hl-bg); padding: 0.1rem 0.2rem; border-radius: 2px; }
.search-highlight.current { background: #ff9800; color: #000; }

/* =============================================================================
   Layout / Responsive
   ============================================================================= */

@media (min-width: 900px) {
  /* Center content while keeping room for fixed TOC */
  body > *:not(#TOC):not(.theme-toggle):not(.menu-toggle):not(.search-toggle):not(.search-overlay) {
    max-width: clamp(600px, 60vw, 1000px);
    margin-left: clamp(280px, calc(50vw - 500px), 60vw);
    margin-right: auto;
    width: 100%;
    transition: margin-left 300ms ease, max-width 300ms ease, width 300ms ease;
  }
}

@media (max-width: 900px) {
  body { padding: 1.5rem; padding-top: 4rem; }

  #TOC { transform: translateX(-100%); width: 280px; padding-top: 4rem; }
  #TOC.open { transform: translateX(0); box-shadow: 4px 0 20px var(--shadow); }

  .menu-toggle { display: flex; }

  h1 { font-size: 1.8rem; }
  h2 { font-size: 1.5rem; }
  h3 { font-size: 1.2rem; }

  pre { font-size: 0.85rem; padding: 2.5rem 0.75rem 0.75rem; }
  .copy-btn { opacity: 1; }

  .search-box { width: 95%; margin: 1rem; }
  .search-results { max-height: 50vh; }
}

@media (max-width: 480px) {
  body { padding: 1rem; padding-top: 4rem; font-size: 15px; }
  h1 { font-size: 1.5rem; }
  h2 { font-size: 1.3rem; }
  h3 { font-size: 1.1rem; }
}

/* =============================================================================
   Print
   ============================================================================= */

@media print {
  body { padding: 0; max-width: 100%; }
  #TOC, .theme-toggle, .menu-toggle, .copy-btn, .search-overlay, .search-toggle { display: none !important; }
  pre { white-space: pre-wrap; word-wrap: break-word; }
  a { color: inherit; text-decoration: underline; }
}

/* =============================================================================
   Server-side token classes (fallback) + Highlight.js compatibility
   ============================================================================= */

.kw { color: #d73a49; font-weight: 700; }
.dt { color: #6f42c1; }
.fu { color: #005cc5; }
.st { color: #22863a; }
.dv, .fl { color: #005cc5; }
.co { color: #6a737d; font-style: italic; }
.pp, .op { color: #d73a49; }

[data-theme="dark"] .kw { color: #ff7b72; }
[data-theme="dark"] .dt { color: #d2a8ff; }
[data-theme="dark"] .fu { color: #79c0ff; }
[data-theme="dark"] .st { color: #a5d6ff; }
[data-theme="dark"] .dv, [data-theme="dark"] .fl { color: #79c0ff; }
[data-theme="dark"] .co { color: #8b949e; }
[data-theme="dark"] .pp, [data-theme="dark"] .op { color: #ff7b72; }

.hljs { background: transparent !important; color: inherit !important; }
pre code.hljs { background: transparent !important; display: block; }
.hljs span { background: transparent !important; }
