/* =====================================================
   DOCUMENTATION SITE STYLING
   Modern documentation layout with sidebar, TOC, and themes
   ===================================================== */

/* ==================== CSS Variables ==================== */
:root {
  /* Light Theme Colors */
  --bg-primary: #ffffff;
  --bg-secondary: #f8f9fa;
  --bg-tertiary: #f1f3f5;
  --text-primary: #1a1d23;
  --text-secondary: #4a5568;
  --text-muted: #718096;
  --border-color: #e2e8f0;
  --accent-primary: #3182ce;
  --accent-hover: #2c5282;
  --accent-light: #ebf8ff;
  --success: #38a169;
  --warning: #dd6b20;
  --danger: #e53e3e;
  --code-bg: #1e1e1e;
  --code-text: #d4d4d4;
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.1);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 20px rgba(0, 0, 0, 0.15);
  --toc-width: 240px;
  --sidebar-width: 280px;
  --header-height: 180px;
}

/* Dark Theme */
body.dark-mode {
  --bg-primary: #0d1117;
  --bg-secondary: #161b22;
  --bg-tertiary: #1c2128;
  --text-primary: #f0f6fc;
  --text-secondary: #c9d1d9;
  --text-muted: #8b949e;
  --border-color: #30363d;
  --accent-primary: #58a6ff;
  --accent-hover: #79c0ff;
  --accent-light: #1f2937;
  --success: #3fb950;
  --warning: #d29922;
  --danger: #f85149;
  --code-bg: #0d1117;
  --code-text: #e6edf3;
  --shadow-sm: 0 0 transparent;
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 16px 48px rgba(0, 0, 0, 0.5);
}

/* ==================== Base Styles ==================== */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
  scroll-padding-top: 80px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Roboto", "Oxygen",
    "Ubuntu", "Cantarell", "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.6;
  color: var(--text-primary);
  background: var(--bg-primary);
  transition: background-color 0.3s ease, color 0.3s ease;
}

h1, h2, h3, h4, h5, h6 {
  font-weight: 600;
  line-height: 1.3;
  color: var(--text-primary);
  margin-bottom: 1rem;
}

h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; margin-top: 2.5rem; }
h3 { font-size: 1.5rem; margin-top: 1.5rem; }
h4 { font-size: 1.25rem; }

p {
  margin-bottom: 1rem;
  color: var(--text-secondary);
}

a {
  color: var(--accent-primary);
  text-decoration: none;
  transition: color 0.2s ease;
}

a:hover {
  color: var(--accent-hover);
}

ul, ol {
  margin-left: 1.5rem;
  margin-bottom: 1rem;
}

li {
  margin-bottom: 0.5rem;
  color: var(--text-secondary);
}

code {
  font-family: "Monaco", "Courier New", monospace;
  font-size: 0.9em;
  background: var(--bg-tertiary);
  padding: 0.2em 0.4em;
  border-radius: 4px;
  color: var(--accent-primary);
}

/* ==================== Desktop Sidebar ==================== */
.desktop-sidebar {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--sidebar-width);
  height: 100vh;
  background: var(--bg-secondary);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  z-index: 1000;
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-header {
  padding: 1.5rem 1.5rem 1rem;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  background: var(--bg-secondary);
  position: sticky;
  top: 0;
  z-index: 10;
}

.sidebar-logo h2 {
  font-size: 1.25rem;
  font-weight: 700;
  margin: 0;
  background: linear-gradient(135deg, var(--accent-primary), var(--accent-hover));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.sidebar-logo p {
  font-size: 0.875rem;
  color: var(--text-muted);
  margin: 0.25rem 0 0 0;
}

.sidebar-theme-toggle {
  width: 36px;
  height: 36px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: all 0.2s ease;
  flex-shrink: 0;
}

.sidebar-theme-toggle:hover {
  background: var(--bg-tertiary);
  color: var(--accent-primary);
  border-color: var(--accent-primary);
}

.sidebar-theme-toggle .toggle-icon-dark {
  display: none;
}

body.dark-mode .sidebar-theme-toggle .toggle-icon-light {
  display: none;
}

body.dark-mode .sidebar-theme-toggle .toggle-icon-dark {
  display: block;
}

.sidebar-nav {
  padding: 1rem 0;
  flex: 1;
}

.nav-section {
  margin-bottom: 2rem;
}

.nav-section-title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin: 0 0 0.75rem 0;
  padding: 0 1.5rem;
}

.nav-link {
  display: block;
  padding: 0.625rem 1.5rem;
  color: var(--text-secondary);
  font-size: 0.9375rem;
  font-weight: 500;
  transition: all 0.2s ease;
  border-left: 3px solid transparent;
  text-decoration: none;
}

.nav-link:hover {
  background: var(--bg-tertiary);
  color: var(--accent-primary);
  border-left-color: var(--accent-primary);
}

.nav-link.active {
  background: var(--accent-light);
  color: var(--accent-primary);
  border-left-color: var(--accent-primary);
  font-weight: 600;
}

/* Old sidebar removed - now using mobile fullscreen menu */

/* Mobile Top Header */
.mobile-header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 60px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border-color);
  display: none;
  align-items: center;
  justify-content: space-between;
  padding: 0 1rem;
  z-index: 1100;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.mobile-header__title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.mobile-header__logo {
  width: 32px;
  height: 32px;
  background: var(--accent-primary);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-weight: 900;
  font-size: 1.2rem;
}

.mobile-header__actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.mobile-header-theme-toggle {
  width: 40px;
  height: 40px;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  transition: all 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.mobile-header-theme-toggle:active {
  background: var(--bg-tertiary);
  color: var(--accent-primary);
  border-color: var(--accent-primary);
  transform: scale(0.95);
}

.mobile-header-theme-toggle .toggle-icon-dark {
  display: none;
}

body.dark-mode .mobile-header-theme-toggle .toggle-icon-light {
  display: none;
}

body.dark-mode .mobile-header-theme-toggle .toggle-icon-dark {
  display: block;
}

.mobile-menu-toggle {
  width: 44px;
  height: 44px;
  background: transparent;
  border: none;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 0;
  transition: all 0.3s ease;
  -webkit-tap-highlight-color: transparent;
}

.mobile-menu-toggle span {
  width: 24px;
  height: 2px;
  background: var(--text-primary);
  border-radius: 2px;
  transition: all 0.3s ease;
}

body.mobile-menu-open .mobile-menu-toggle span:nth-child(1) {
  transform: rotate(45deg) translate(5px, 5px);
}

body.mobile-menu-open .mobile-menu-toggle span:nth-child(2) {
  opacity: 0;
}

body.mobile-menu-open .mobile-menu-toggle span:nth-child(3) {
  transform: rotate(-45deg) translate(7px, -7px);
}

/* Mobile Fullscreen Menu */
.mobile-fullscreen-menu {
  position: fixed;
  top: 60px;
  left: 0;
  right: 0;
  bottom: 0;
  background: var(--bg-primary);
  z-index: 1050;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  opacity: 0;
  transform: translateY(-20px);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  pointer-events: none;
  visibility: hidden;
}

body.mobile-menu-open .mobile-fullscreen-menu {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
  visibility: visible;
}

.mobile-nav-section {
  padding: 1.5rem 1rem 0.5rem;
}

.mobile-nav-section__title {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  margin-bottom: 0.75rem;
  padding: 0 0.5rem;
}

.mobile-nav-link {
  display: block;
  padding: 1rem;
  color: var(--text-secondary);
  font-size: 1rem;
  font-weight: 500;
  border-radius: 10px;
  margin-bottom: 0.5rem;
  transition: all 0.2s ease;
  border-left: 3px solid transparent;
}

.mobile-nav-link:active {
  background: var(--accent-light);
  color: var(--accent-primary);
  border-left-color: var(--accent-primary);
  transform: scale(0.98);
}

.mobile-nav-link.active {
  background: var(--accent-light);
  color: var(--accent-primary);
  border-left-color: var(--accent-primary);
}

/* ==================== Responsive Breakpoints ==================== */
@media (max-width: 768px) {
  .desktop-sidebar {
    display: none;
  }
  
  .mobile-header {
    display: flex;
  }
  
  .docs-layout {
    margin-left: 0;
    padding-top: 60px;
  }

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

  .comparison-cell {
    border-right: none;
  }

  .comparison-cell:nth-child(3n) {
    border-right: none;
  }
}

@media (min-width: 769px) {
  .mobile-header {
    display: none !important;
  }
  
  .mobile-fullscreen-menu {
    display: none !important;
  }
}

/* ==================== Main Layout ==================== */
.docs-layout {
  margin-left: var(--sidebar-width);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.docs-header {
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
  padding: 2rem 2.5rem;
}

.header-content {
  max-width: 900px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.875rem;
  margin-bottom: 1rem;
  color: var(--text-muted);
}

.breadcrumb__separator {
  color: var(--border-color);
}

.breadcrumb__item--active {
  color: var(--accent-primary);
}

.page-title {
  font-size: 2.5rem;
  font-weight: 700;
  margin-bottom: 0.5rem;
  background: linear-gradient(135deg, var(--accent-primary), var(--accent-hover));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.page-subtitle {
  font-size: 1.125rem;
  color: var(--text-secondary);
  margin-bottom: 1.5rem;
  max-width: 700px;
}

.badge-row {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.badge {
  display: inline-block;
  padding: 0.375rem 0.75rem;
  background: var(--accent-light);
  color: var(--accent-primary);
  border-radius: 20px;
  font-size: 0.875rem;
  font-weight: 500;
  border: 1px solid var(--accent-primary);
}

/* ==================== Main Content Area ==================== */
.docs-main {
  display: flex;
  flex: 1;
  position: relative;
}

.docs-container {
  flex: 1;
  max-width: 900px;
  padding: 3rem 2.5rem;
  margin-right: var(--toc-width);
}

.doc-section {
  margin-bottom: 4rem;
}

.section-title {
  font-size: 2rem;
  font-weight: 700;
  margin-bottom: 1.5rem;
  padding-bottom: 0.5rem;
  border-bottom: 2px solid var(--border-color);
}

.section-content {
  color: var(--text-secondary);
}

.lead {
  font-size: 1.25rem;
  line-height: 1.7;
  margin-bottom: 2rem;
  color: var(--text-primary);
}

/* ==================== Info Cards ==================== */
.info-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5rem;
  margin: 1.5rem 0;
}

.info-card h3 {
  margin-top: 0;
  margin-bottom: 1rem;
}

.info-card h4 {
  margin-top: 0;
}

.info-card--primary {
  border-left: 4px solid var(--accent-primary);
}

.info-card--warning {
  border-left: 4px solid var(--warning);
  background: var(--bg-tertiary);
}

.info-card--tip {
  border-left: 4px solid var(--success);
}

.feature-list {
  list-style: none;
  margin-left: 0;
}

.feature-list li {
  padding-left: 1.5rem;
  position: relative;
  margin-bottom: 1rem;
}

.feature-list li::before {
  content: "→";
  position: absolute;
  left: 0;
  color: var(--accent-primary);
  font-weight: bold;
}

/* ==================== Comparison Table ==================== */
.comparison-table {
  display: grid;
  grid-template-columns: minmax(160px, 1fr) repeat(2, minmax(200px, 1.2fr));
  border: 1px solid var(--border-color);
  border-radius: 10px;
  overflow: hidden;
  margin: 1.5rem 0;
  background: var(--bg-primary);
}

.comparison-cell {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border-color);
  border-right: 1px solid var(--border-color);
  font-size: 0.95rem;
  color: var(--text-secondary);
}

.comparison-cell:nth-child(3n) {
  border-right: none;
}

.comparison-head {
  background: var(--bg-tertiary);
  font-weight: 600;
  color: var(--text-primary);
}

.comparison-label {
  font-weight: 600;
  color: var(--text-primary);
  background: var(--bg-secondary);
}

.comparison-table .comparison-cell:last-child,
.comparison-table .comparison-cell:nth-last-child(2),
.comparison-table .comparison-cell:nth-last-child(3) {
  border-bottom: none;
}

/* ==================== Code Blocks ==================== */
.code-block {
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  margin: 1.5rem 0;
  overflow: hidden;
}

.code-block pre {
  margin: 0;
  padding: 1.5rem;
  overflow-x: auto;
}

.code-block code {
  background: transparent;
  color: var(--code-text);
  font-size: 0.875rem;
  line-height: 1.6;
  padding: 0;
  display: block;
  white-space: pre;
}

body.dark-mode .code-block {
  background: var(--bg-tertiary);
  border-color: var(--border-color);
}

/* ==================== Feature Cards ==================== */
.grid-4 {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1.5rem;
  margin: 2rem 0;
}

.feature-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1.5rem;
  transition: all 0.3s ease;
}

.feature-card:hover {
  transform: translateY(-4px);
  box-shadow: var(--shadow-md);
  border-color: var(--accent-primary);
}

.feature-card h3 {
  margin-top: 0;
  color: var(--accent-primary);
}

/* ==================== Algorithm Cards ==================== */
.algo-card {
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 2rem;
  margin: 2rem 0;
}

.algo-card h3 {
  margin-top: 0;
  color: var(--text-primary);
}

/* ==================== Demo Section ==================== */
.demo-controls {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin: 1.5rem 0;
}

.demo-btn {
  padding: 0.5rem 1rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  transition: all 0.2s ease;
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.demo-btn:hover {
  background: var(--accent-light);
  border-color: var(--accent-primary);
  color: var(--accent-primary);
}

.demo-btn.is-active {
  background: var(--accent-primary);
  border-color: var(--accent-primary);
  color: white;
}

.demo-canvas-wrapper {
  margin: 1.5rem auto;
  max-width: 600px;
  background: var(--code-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 1rem;
  overflow: hidden;
  width: 100%;
}

.demo-canvas-wrapper canvas {
  width: 100%;
  max-width: 100%;
  height: auto;
  display: block;
  border-radius: 6px;
  aspect-ratio: 16 / 10;
}

@media (max-width: 768px) {
  .demo-canvas-wrapper {
    max-width: 100%;
    padding: 0.75rem;
    margin: 1.25rem auto;
  }
  
  .demo-canvas-wrapper canvas {
    max-width: 100%;
    aspect-ratio: 16 / 10;
    max-height: 320px;
    height: auto;
  }
}

@media (max-width: 480px) {
  .demo-canvas-wrapper {
    max-width: 100%;
    padding: 0.5rem;
    margin: 1rem auto;
  }

  .demo-canvas-wrapper canvas {
    width: 100%;
    max-width: 100%;
    aspect-ratio: 4 / 3;
    max-height: 180px;
    height: auto;
    margin: 0 auto;
    display: block;
  }
}

.canvas-caption {
  margin-top: 1rem;
  font-size: 0.875rem;
  color: var(--text-muted);
  text-align: center;
}

/* ==================== Video ==================== */
.video-wrapper {
  margin: 2rem auto;
  max-width: 600px;
}

.demo-video {
  width: 100%;
  max-height: 380px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  object-fit: contain;
}

@media (max-width: 768px) {
  .video-wrapper {
    max-width: 100%;
  }
}

.caption {
  margin-top: 0.75rem;
  font-size: 0.875rem;
  color: var(--text-muted);
  text-align: center;
}

/* ==================== Table of Contents ==================== */
.toc {
  position: fixed;
  top: var(--header-height);
  right: 0;
  width: var(--toc-width);
  height: calc(100vh - var(--header-height));
  padding: 2rem 1.5rem;
  overflow-y: auto;
  border-left: 1px solid var(--border-color);
  background: var(--bg-primary);
}

.toc__header h4 {
  font-size: 0.875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 1rem;
}

.toc__nav {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toc__link {
  display: block;
  padding: 0.375rem 0.75rem;
  color: var(--text-secondary);
  font-size: 0.875rem;
  border-left: 2px solid transparent;
  transition: all 0.2s ease;
}

.toc__link:hover,
.toc__link.active {
  color: var(--accent-primary);
  border-left-color: var(--accent-primary);
}

/* ==================== Footer ==================== */
.docs-footer {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  padding: 2rem 2.5rem;
  margin-left: 0;
  text-align: center;
}

.footer-content p {
  margin: 0.5rem 0;
  color: var(--text-muted);
}

.footer-meta {
  font-size: 0.875rem;
}

/* ==================== Responsive Design ==================== */
@media (max-width: 1400px) {
  :root {
    --toc-width: 0px;
  }
  
  .toc {
    display: none;
  }
  
  .docs-container {
    margin-right: 0;
    max-width: 1000px;
  }
}

@media (min-width: 1025px) {
  .sidebar-overlay {
    display: none;
  }
}

@media (max-width: 1024px) {
  body {
    overflow-x: hidden;
  }

  /* Show mobile header */
  .mobile-header {
    display: flex;
  }
  
  .docs-layout {
    margin-left: 0;
  }
  
  .docs-header {
    padding: 1.5rem;
    padding-top: 5rem;
  }
  
  .docs-container {
    padding: 2rem 1.5rem 4.5rem;
    margin-top: 60px;
  }
  
  body.mobile-menu-open {
    overflow: hidden;
  }
}

/* ==================== Tablet (768px and below) ==================== */
@media (max-width: 768px) {
  :root {
    --header-height: 150px;
  }

  html {
    scroll-padding-top: 60px;
  }

  body {
    font-size: 15px;
  }

  .docs-header {
    padding: 1.25rem 1rem 1.5rem;
    padding-top: 4rem;
  }

  .header-content {
    max-width: 100%;
  }

  .docs-container {
    width: 100%;
    max-width: none;
    padding: 1.5rem 1rem 5rem;
  }
  
  .page-title {
    font-size: 1.75rem;
    margin-bottom: 0.75rem;
  }
  
  .page-subtitle {
    font-size: 0.95rem;
    line-height: 1.5;
  }
  
  .section-title {
    font-size: 1.5rem;
    margin-top: 2rem;
  }
  
  h1 { font-size: 2rem; }
  h2 { font-size: 1.5rem; margin-top: 2rem; }
  h3 { font-size: 1.25rem; margin-top: 1.25rem; }
  h4 { font-size: 1.1rem; }
  
  p {
    margin-bottom: 0.875rem;
  }
  
  .grid-4 {
    grid-template-columns: 1fr;
    gap: 1rem;
  }
  
  .badge-row {
    gap: 0.5rem;
    flex-wrap: wrap;
  }
  
  .badge {
    font-size: 0.75rem;
    padding: 0.25rem 0.5rem;
  }
  
  .demo-controls {
    gap: 0.5rem;
    flex-wrap: wrap;
    display: grid;
    grid-template-columns: 1fr 1fr;
  }
  
  .demo-btn {
    padding: 0.5rem 1rem;
    font-size: 0.8125rem;
    min-width: auto;
    flex: 1 1 auto;
    min-height: 44px;
    white-space: normal;
    word-wrap: break-word;
    line-height: 1.2;
    overflow: hidden;
  }
  
  .feature-card,
  .algo-card {
    padding: 1.25rem;
  }
  
  .code-block {
    margin: 1.25rem 0;
    padding: 0;
  }

  .code-block pre {
    font-size: 0.8rem;
    padding: 1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  
  .info-card {
    padding: 1.125rem;
    margin: 1.25rem 0;
  }
  
  .demo-canvas-wrapper {
    max-width: 100%;
    padding: 0.75rem;
    margin: 1.25rem auto;
  }

  .demo-canvas-wrapper canvas {
    width: 100%;
    max-width: 100%;
    height: auto;
    aspect-ratio: 16 / 10;
    max-height: 320px;
  }

  .responsive-canvas {
    width: 100%;
    height: auto;
  }

  ul, ol {
    margin-left: 1.25rem;
  }

  li {
    margin-bottom: 0.375rem;
  }
}

/* ==================== Mobile Phone (480px and below) ==================== */
@media (max-width: 480px) {
  :root {
    --header-height: 140px;
  }

  html {
    scroll-padding-top: 50px;
  }

  body {
    font-size: 14px;
    line-height: 1.5;
  }

  /* Safe area insets for notched devices */
  .mobile-menu-toggle {
    top: max(8px, env(safe-area-inset-top));
    left: max(8px, env(safe-area-inset-left));
  }

  .mobile-theme-toggle {
    top: max(8px, env(safe-area-inset-top));
    right: max(8px, env(safe-area-inset-right));
  }
  
  .docs-header {
    padding: 1rem 0.75rem 1.25rem;
    padding-top: max(3.75rem, calc(env(safe-area-inset-top) + 3.75rem));
  }
  
  .docs-container {
    width: 100%;
    max-width: none;
    padding: 1.125rem 0.75rem max(4.5rem, calc(env(safe-area-inset-bottom) + 3rem));
  }
  
  .page-title {
    font-size: 1.5rem;
    line-height: 1.2;
    margin-bottom: 0.5rem;
  }
  
  .page-subtitle {
    font-size: 0.875rem;
    line-height: 1.4;
    margin-bottom: 1rem;
  }
  
  .section-title {
    font-size: 1.25rem;
    margin-top: 1.5rem;
    margin-bottom: 1rem;
  }
  
  h1 { font-size: 1.5rem; }
  h2 { font-size: 1.25rem; margin-top: 1.5rem; }
  h3 { font-size: 1.1rem; margin-top: 1rem; }
  h4 { font-size: 1rem; }
  
  p {
    margin-bottom: 0.75rem;
    font-size: 0.95rem;
  }

  .lead {
    font-size: 1.1rem;
  }

  /* Demo controls - 2 column grid on small screens */
  .demo-controls {
    gap: 0.375rem;
    display: grid;
    grid-template-columns: 1fr 1fr;
    margin: 1rem 0;
  }
  
  .demo-btn {
    padding: 0.5rem 0.5rem;
    font-size: 0.7rem;
    white-space: normal;
    word-wrap: break-word;
    min-height: 44px;
    line-height: 1.2;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    overflow: hidden;
  }

  .demo-btn:active {
    background: var(--accent-hover);
  }

  /* Canvas Optimization for Mobile */
  .demo-canvas-wrapper {
    max-width: 100%;
    padding: 0.5rem;
    margin: 0.75rem auto 0.5rem;
    overflow: hidden;
  }

  .demo-canvas-wrapper canvas {
    width: 100%;
    height: auto;
    max-width: 100%;
    aspect-ratio: 16 / 12;
    max-height: 240px;
    display: block;
  }

  .responsive-canvas {
    width: 100%;
    height: auto;
    aspect-ratio: 16 / 12;
  }

  #flowChart,
  #algoCanvas,
  #symmetryCanvas,
  #verifyCanvas {
    width: 100%;
    height: auto;
    max-width: 100%;
  }

  .canvas-caption {
    font-size: 0.75rem;
    line-height: 1.3;
    margin-top: 0.75rem;
  }
  
  .code-block {
    margin: 1rem 0;
    border-radius: 6px;
    padding: 0;
  }

  .code-block pre {
    padding: 0.875rem;
    font-size: 0.7rem;
    line-height: 1.4;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    word-break: break-word;
    white-space: pre-wrap;
  }

  .code-block code {
    font-size: 0.7rem;
  }
  
  .info-card {
    padding: 0.875rem;
    margin: 1rem 0;
    border-radius: 6px;
  }

  .info-card h3 {
    font-size: 1rem;
    margin-bottom: 0.75rem;
  }

  .info-card h4 {
    font-size: 0.95rem;
  }

  ul {
    margin-left: 1rem;
  }

  ol {
    margin-left: 1.25rem;
  }

  li {
    margin-bottom: 0.375rem;
    font-size: 0.95rem;
  }

  .feature-card,
  .algo-card {
    padding: 1rem;
    margin: 1rem 0;
    border-radius: 6px;
  }

  .feature-card h3,
  .algo-card h3 {
    font-size: 1.05rem;
    margin-bottom: 0.75rem;
  }

  .feature-list li {
    padding-left: 1.25rem;
    margin-bottom: 0.75rem;
  }

  .grid-4 {
    grid-template-columns: 1fr;
    gap: 0.75rem;
  }

  .badge-row {
    gap: 0.375rem;
    flex-wrap: wrap;
  }

  .badge {
    font-size: 0.7rem;
    padding: 0.2rem 0.4rem;
  }

  .breadcrumb {
    font-size: 0.75rem;
    margin-bottom: 0.75rem;
  }

  /* Video responsiveness */
  .video-wrapper {
    max-width: 100%;
    margin: 1.5rem auto;
  }

  .demo-video {
    width: 100%;
    max-height: 280px;
    aspect-ratio: 16 / 9;
  }

  .caption {
    font-size: 0.75rem;
    margin-top: 0.5rem;
  }

  /* Mobile navigation */
  .sidebar {
    width: 100%;
  }

  .sidebar-overlay {
    z-index: 899;
  }

  /* Safe area for buttons at bottom */
  .docs-container {
    padding-bottom: max(4.5rem, calc(env(safe-area-inset-bottom) + 3rem));
  }
}

/* ==================== Responsive Grid & Layout ==================== */
.responsive-canvas {
  width: 100%;
  height: auto;
  max-width: 100%;
  display: block;
}

/* Ensure canvas containers don't overflow */
.demo-canvas-wrapper,
.video-wrapper {
  overflow: hidden;
  width: 100%;
}

/* Prevent horizontal scroll */
body, html {
  max-width: 100%;
  overflow-x: hidden;
}

@media (max-width: 480px) {
  /* Tight margins on small screens */
  .docs-container {
    width: 100vw;
    max-width: 100%;
    overflow: hidden;
  }
  
  /* Ensure no horizontal overflow from code blocks */
  .code-block {
    width: 100%;
    overflow: hidden;
  }
  
  .code-block pre {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: calc(100vw - 1.5rem);
  }
  
  /* Flexible grid items */
  .grid-4 {
    width: 100%;
  }
  
  /* Demo controls fill width */
  .demo-controls {
    width: 100%;
  }
  
  .demo-btn {
    width: 100%;
  }
}

/* ==================== Touch Optimization ==================== */
/* Ensure buttons are at least 44x44px for touch */
button, a.nav-link, .demo-btn {
  min-height: 44px;
  min-width: 44px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

input, textarea, select {
  font-size: 16px; /* Prevents zoom on iOS */
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* Prevent double-tap zoom on clickable elements */
@media (hover: none) and (pointer: coarse) {
  /* Mobile devices */
  button, a, input, textarea, select {
    touch-action: manipulation;
  }
  
  /* Improve scrolling performance */
  .sidebar__nav,
  .docs-container,
  .demo-canvas-wrapper {
    -webkit-overflow-scrolling: touch;
  }
  
  /* Better spacing for touch on mobile */
  .demo-btn {
    padding: 0.625rem 1rem;
    font-size: 1rem;
  }
  
  .nav-link {
    padding: 0.75rem 1rem;
    min-height: 44px;
  }
  
  a {
    padding: 0.25rem;
    margin: -0.25rem;
  }
}
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: var(--bg-secondary);
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ==================== Animations ==================== */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.doc-section {
  animation: fadeIn 0.5s ease-out;
}

/* ==================== Print Styles ==================== */
@media print {
  .sidebar,
  .toc,
  .docs-footer,
  .demo-controls {
    display: none;
  }
  
  .docs-layout {
    margin-left: 0;
  }
  
  .docs-container {
    max-width: 100%;
    margin: 0;
  }
}
