/* ===== IMPORTS ===== */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&family=Merriweather:wght@400;700;900&family=Fira+Code:wght@400;500&display=swap');

/* ===== CSS VARIABLES ===== */
:root {
  /* Light Mode */
  --bg-primary: #f8f7f4;
  --bg-page: #ffffff;
  --bg-sidebar: #fafaf8;
  --bg-callout: #f0efeb;
  --bg-callout-blue: #e8f0fe;
  --bg-callout-green: #e6f4ea;
  --bg-callout-amber: #fef7e0;
  --bg-callout-red: #fce8e6;
  --bg-callout-purple: #f3e8fd;
  --bg-code: #f5f5f5;
  --bg-search: #ffffff;
  --text-primary: #1a1a2e;
  --text-secondary: #4a4a68;
  --text-muted: #8888a0;
  --text-heading: #0d0d1a;
  --border-color: #e0dfd8;
  --border-light: #eeeee8;
  --accent: #4361ee;
  --accent-light: #eef0ff;
  --accent-dark: #3a56d4;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.1);
  --sidebar-width: 300px;
  --page-max-width: 820px;
  --radius: 8px;
  --radius-lg: 12px;
}

[data-theme="dark"] {
  --bg-primary: #0d0d0d;
  --bg-page: #121212;
  --bg-sidebar: #0a0a0a;
  --bg-callout: #1a1a1a;
  --bg-callout-blue: #0f1822;
  --bg-callout-green: #0f1c14;
  --bg-callout-amber: #1c170c;
  --bg-callout-red: #1c0f0f;
  --bg-callout-purple: #180f22;
  --bg-code: #1a1a1a;
  --bg-search: #1a1a1a;
  --text-primary: #e0e0e8;
  --text-secondary: #a0a0b0;
  --text-muted: #606068;
  --text-heading: #f0f0f5;
  --border-color: #222222;
  --border-light: #1a1a1a;
  --accent: #6c83f7;
  --accent-light: #141828;
  --accent-dark: #8ea0ff;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.5);
}

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

html {
  scroll-behavior: smooth;
  scroll-padding-top: 30px;
  font-size: 16px;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.8;
  transition: background 0.3s, color 0.3s;
  -webkit-font-smoothing: antialiased;
}

::selection {
  background: var(--accent);
  color: #fff;
}

/* ===== LAYOUT ===== */
.app-layout {
  display: flex;
  min-height: 100vh;
}

/* ===== SIDEBAR ===== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  height: 100dvh;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 100;
  transition: transform 0.35s cubic-bezier(0.4, 0, 0.2, 1), background 0.3s;
  overflow: hidden;
}

.sidebar-header {
  padding: 24px 20px 16px;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.sidebar-title {
  font-family: 'Merriweather', serif;
  font-size: 1.15rem;
  font-weight: 900;
  color: var(--text-heading);
  letter-spacing: -0.02em;
  line-height: 1.3;
}

.sidebar-subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 4px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* Search */
.sidebar-search {
  padding: 12px 16px;
  flex-shrink: 0;
}

.search-box {
  position: relative;
}

.search-box input {
  width: 100%;
  padding: 9px 12px 9px 36px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  background: var(--bg-search);
  color: var(--text-primary);
  font-size: 0.85rem;
  font-family: 'Inter', sans-serif;
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.search-box input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-light);
}

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

.search-box .search-icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  color: var(--text-muted);
}

/* Navigation */
.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
  min-height: 0;
  scrollbar-width: thin;
  scrollbar-color: var(--border-color) transparent;
}

.sidebar-nav::-webkit-scrollbar {
  width: 5px;
}

.sidebar-nav::-webkit-scrollbar-track {
  background: transparent;
}

.sidebar-nav::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 10px;
}

.nav-unit {
  margin-bottom: 2px;
}

.nav-unit-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 20px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.84rem;
  font-weight: 600;
  font-family: 'Inter', sans-serif;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s;
  position: relative;
}

.nav-unit-btn::before {
  content: '';
  position: absolute;
  left: 0;
  top: 6px;
  bottom: 6px;
  width: 3px;
  border-radius: 0 3px 3px 0;
  background: var(--accent);
  opacity: 0;
  transition: opacity 0.2s;
}

.nav-unit-btn:hover {
  background: var(--bg-callout);
  color: var(--text-primary);
}

.nav-unit-btn.active {
  color: var(--accent);
  background: var(--accent-light);
}

.nav-unit-btn.active::before {
  opacity: 1;
}

.nav-unit-btn .unit-num {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  background: var(--bg-callout);
  font-size: 0.72rem;
  font-weight: 700;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

.nav-unit-btn.active .unit-num {
  background: var(--accent);
  color: #fff;
}

.nav-unit-btn .chevron {
  margin-left: auto;
  width: 16px;
  height: 16px;
  transition: transform 0.25s;
  flex-shrink: 0;
}

.nav-unit-btn.expanded .chevron {
  transform: rotate(90deg);
}

.nav-subtopics {
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.35s cubic-bezier(0.4, 0, 0.2, 1);
}

.nav-subtopics.open {
  max-height: 600px;
}

.nav-subtopic-link {
  display: block;
  padding: 7px 20px 7px 56px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.8rem;
  font-weight: 400;
  transition: color 0.15s, background 0.15s;
  border-left: 2px solid transparent;
}

.nav-subtopic-link:hover {
  color: var(--text-primary);
  background: var(--bg-callout);
}

.nav-subtopic-link.active {
  color: var(--accent);
  font-weight: 500;
}

/* Sidebar footer */
.sidebar-footer {
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-callout);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  padding: 7px 12px;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 0.8rem;
  font-family: 'Inter', sans-serif;
  font-weight: 500;
  flex: 1;
  justify-content: center;
  transition: background 0.2s, color 0.2s;
}

.theme-toggle:hover {
  background: var(--border-color);
  color: var(--text-primary);
}

.theme-toggle svg {
  width: 16px;
  height: 16px;
}

/* ===== MAIN CONTENT ===== */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-height: 100vh;
}

.content-wrapper {
  max-width: var(--page-max-width);
  margin: 0 auto;
  padding: 40px 48px 80px;
}

/* ===== HERO / TITLE PAGE ===== */
.hero-page {
  text-align: center;
  padding: 80px 20px 60px;
  margin-bottom: 40px;
  border-bottom: 2px solid var(--border-color);
}

.hero-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 16px;
  border-radius: 50px;
  background: var(--accent-light);
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 20px;
}

.hero-title {
  font-family: 'Merriweather', serif;
  font-size: 2.8rem;
  font-weight: 900;
  color: var(--text-heading);
  line-height: 1.2;
  letter-spacing: -0.03em;
  margin-bottom: 16px;
}

.hero-subtitle {
  font-size: 1.1rem;
  color: var(--text-secondary);
  max-width: 520px;
  margin: 0 auto 28px;
  line-height: 1.6;
}

.hero-meta {
  display: flex;
  justify-content: center;
  gap: 24px;
  flex-wrap: wrap;
  color: var(--text-muted);
  font-size: 0.82rem;
}

.hero-meta span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* ===== UNIT SECTIONS ===== */
.unit-section {
  margin-bottom: 56px;
  padding-top: 10px;
}

.unit-header {
  margin-bottom: 32px;
  padding-bottom: 20px;
  border-bottom: 2px solid var(--border-color);
}

.unit-label {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 5px 14px;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 12px;
}

.unit-title {
  font-family: 'Merriweather', serif;
  font-size: 1.85rem;
  font-weight: 900;
  color: var(--text-heading);
  line-height: 1.3;
  letter-spacing: -0.02em;
}

/* ===== TYPOGRAPHY ===== */
.topic-heading {
  font-family: 'Merriweather', serif;
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--text-heading);
  margin: 36px 0 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border-light);
  line-height: 1.4;
}

.subtopic-heading {
  font-size: 1.08rem;
  font-weight: 700;
  color: var(--text-heading);
  margin: 28px 0 12px;
  line-height: 1.4;
}

p {
  margin-bottom: 14px;
  color: var(--text-primary);
  line-height: 1.85;
}

strong {
  font-weight: 600;
  color: var(--text-heading);
}

/* ===== CALLOUT BOXES ===== */
.callout {
  border-radius: var(--radius-lg);
  padding: 18px 22px;
  margin: 20px 0;
  border-left: 4px solid;
  font-size: 0.92rem;
}

.callout-title {
  font-weight: 700;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.callout.definition {
  background: var(--bg-callout-blue);
  border-color: #4285f4;
}

.callout.definition .callout-title {
  color: #4285f4;
}

.callout.important {
  background: var(--bg-callout-amber);
  border-color: #f9ab00;
}

.callout.important .callout-title {
  color: #e69500;
}

.callout.tip {
  background: var(--bg-callout-green);
  border-color: #34a853;
}

.callout.tip .callout-title {
  color: #34a853;
}

.callout.warning {
  background: var(--bg-callout-red);
  border-color: #ea4335;
}

.callout.warning .callout-title {
  color: #ea4335;
}

.callout.example {
  background: var(--bg-callout-purple);
  border-color: #a259ff;
}

.callout.example .callout-title {
  color: #a259ff;
}

/* ===== CHEATSHEET BOXES ===== */
.cheatsheet {
  background: var(--bg-callout);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 22px 26px;
  margin: 28px 0;
  position: relative;
  overflow: hidden;
}

.cheatsheet::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent), #a259ff, #f472b6);
}

.cheatsheet-title {
  font-family: 'Merriweather', serif;
  font-weight: 700;
  font-size: 1rem;
  color: var(--accent);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.cheatsheet ul {
  list-style: none;
  padding: 0;
}

.cheatsheet li {
  padding: 5px 0 5px 22px;
  position: relative;
  font-size: 0.9rem;
  line-height: 1.65;
  color: var(--text-secondary);
}

.cheatsheet li::before {
  content: '▸';
  position: absolute;
  left: 4px;
  color: var(--accent);
  font-weight: 700;
}

/* ===== LISTS ===== */
ul,
ol {
  margin: 10px 0 16px 24px;
}

li {
  margin-bottom: 6px;
  line-height: 1.75;
}

ul li {
  list-style-type: disc;
}

ol li {
  list-style-type: decimal;
}

/* ===== TABLES ===== */
.table-wrapper {
  overflow-x: auto;
  margin: 20px 0;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

thead {
  background: var(--bg-callout);
}

th {
  padding: 12px 16px;
  font-weight: 700;
  text-align: left;
  color: var(--text-heading);
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border-bottom: 2px solid var(--border-color);
}

td {
  padding: 11px 16px;
  border-bottom: 1px solid var(--border-light);
  vertical-align: top;
}

tbody tr:last-child td {
  border-bottom: none;
}

tbody tr:hover {
  background: var(--bg-callout);
}

/* ===== CODE ===== */
code {
  font-family: 'Fira Code', monospace;
  background: var(--bg-code);
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 0.85em;
  color: var(--accent);
  font-weight: 500;
}

/* ===== KEYWORD TAGS ===== */
.keyword {
  display: inline-block;
  background: var(--accent-light);
  color: var(--accent);
  padding: 2px 10px;
  border-radius: 4px;
  font-size: 0.8rem;
  font-weight: 600;
  margin: 2px 2px;
}

/* ===== FOOTER ===== */
.site-footer {
  text-align: center;
  padding: 40px 20px;
  margin-top: 40px;
  border-top: 1px solid var(--border-color);
  color: var(--text-muted);
  font-size: 0.85rem;
  letter-spacing: 0.02em;
}

.site-footer .heart {
  color: #e25555;
  display: inline-block;
  animation: heartbeat 1.2s ease-in-out infinite;
}

@keyframes heartbeat {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

/* ===== PEEK ARROW ===== */
.sidebar-peek {
  display: none;
  position: fixed;
  top: 28px;
  left: 0;
  z-index: 9999;
  width: 22px;
  height: 60px;
  border-radius: 0 12px 12px 0;
  background: var(--bg-page);
  border: 1px solid var(--border-color);
  border-left: none;
  box-shadow: var(--shadow-md);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: width 0.2s, color 0.2s, background 0.2s;
  transform: translateZ(0);
  will-change: transform;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  contain: layout style;
}

.sidebar-peek:hover,
.sidebar-peek:active {
  width: 28px;
  color: var(--accent);
  background: var(--bg-callout);
}

.sidebar-peek svg {
  width: 16px;
  height: 16px;
}

.sidebar-peek.hidden {
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 90;
  opacity: 0;
  transition: opacity 0.3s;
}

.sidebar-overlay.visible {
  opacity: 1;
}

/* ===== BACK TO TOP ===== */
.back-to-top {
  position: fixed;
  bottom: 28px;
  right: 28px;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.3s, transform 0.3s, background 0.2s;
  z-index: 50;
}

.back-to-top.visible {
  opacity: 1;
  transform: translateY(0);
}

.back-to-top:hover {
  background: var(--accent-dark);
  transform: translateY(-2px);
}

.back-to-top svg {
  width: 20px;
  height: 20px;
}

/* ===== PROGRESS BAR ===== */
.progress-bar {
  position: fixed;
  top: 0;
  left: var(--sidebar-width);
  right: 0;
  height: 3px;
  background: var(--border-light);
  z-index: 80;
  transition: left 0.35s;
}

.progress-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), #a259ff);
  width: 0%;
  transition: width 0.15s;
  border-radius: 0 2px 2px 0;
}

/* ===== DIAGRAM BOX ===== */
.diagram-box {
  background: var(--bg-callout);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 24px;
  margin: 20px 0;
  text-align: center;
  font-family: 'Fira Code', monospace;
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--text-secondary);
  overflow-x: auto;
  white-space: pre;
}

/* ===== DIAGRAM IMAGES ===== */
.diagram-img {
  display: block;
  margin: 24px auto;
  max-width: 100%;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  box-shadow: var(--shadow-md);
}

.diagram-caption {
  text-align: center;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: -16px;
  margin-bottom: 20px;
  font-style: italic;
}

/* ===== COMPARISON GRID ===== */
.comparison-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin: 20px 0;
}

.comparison-card {
  background: var(--bg-callout);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 20px;
}

.comparison-card h4 {
  font-weight: 700;
  font-size: 0.95rem;
  color: var(--accent);
  margin-bottom: 10px;
}

.comparison-card ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.comparison-card li {
  padding: 4px 0 4px 18px;
  position: relative;
  font-size: 0.88rem;
  color: var(--text-secondary);
}

.comparison-card li::before {
  content: '•';
  position: absolute;
  left: 2px;
  color: var(--accent);
  font-weight: 700;
}

/* ===== FORMULA BOX ===== */
.formula-box {
  background: var(--bg-callout-blue);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 18px 24px;
  margin: 16px 0;
  font-family: 'Fira Code', monospace;
  font-size: 0.92rem;
  text-align: center;
  color: var(--text-heading);
}

.formula-box .label {
  display: block;
  font-family: 'Inter', sans-serif;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

/* ===== PRINT ===== */
@media print {

  .sidebar,
  .mobile-menu-btn,
  .back-to-top,
  .progress-bar,
  .sidebar-overlay {
    display: none !important;
  }

  .main-content {
    margin-left: 0 !important;
  }

  .content-wrapper {
    max-width: 100%;
    padding: 20px;
  }

  .unit-section {
    page-break-before: always;
  }

  .callout,
  .cheatsheet,
  .table-wrapper {
    break-inside: avoid;
  }
}

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }

  .sidebar-peek {
    display: flex;
  }

  .main-content {
    margin-left: 0;
  }

  .content-wrapper {
    padding: 60px 24px 60px;
  }

  .progress-bar {
    left: 0;
  }

  .hero-title {
    font-size: 2rem;
  }

  .comparison-grid {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 500px) {
  .content-wrapper {
    padding: 56px 16px 50px;
  }

  .hero-title {
    font-size: 1.65rem;
  }

  .unit-title {
    font-size: 1.45rem;
  }

  .topic-heading {
    font-size: 1.15rem;
  }

  .hero-meta {
    flex-direction: column;
    gap: 8px;
  }

  .callout,
  .cheatsheet {
    padding: 14px 16px;
  }
}