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

:root {
  --sidebar-width: 260px;
  --bg-color: #f6f8fa;
  --sidebar-bg: #ffffff;
  --text-color: #24292f;
  --link-color: #0969da;
  --accent-color: #af52de;
  --border-color: #d0d7de;
  --toc-bg: #ffffff;
  --max-width: 1200px;
  --code-bg: #f6f8fa;
  --breadcrumb-color: #57606a;
  --syntax-keyword: #d73a49;
  --syntax-string: #032f62;
  --syntax-comment: #6a737d;
  --syntax-function: #6f42c1;
  --syntax-number: #005cc5;
  --syntax-builtin: #e36209;
  --syntax-type: #005cc5;
  --syntax-constant: #005cc5;
  --syntax-operator: #d73a49;
  --syntax-attr: #22863a;
  --syntax-variable: #e36209;
  --syntax-symbol: #005cc5;
  --syntax-section: #005cc5;
  --syntax-addition: #22863a;
  --syntax-deletion: #d73a49;
  --syntax-meta: #6a737d;
}

[data-theme="dark"] {
  --bg-color: #0d1117;
  --sidebar-bg: #161b22;
  --text-color: #c9d1d9;
  --link-color: #58a6ff;
  --accent-color: #bc8cff;
  --border-color: #30363d;
  --toc-bg: #161b22;
  --code-bg: #161b22;
  --breadcrumb-color: #8b949e;
  --syntax-keyword: #ff7b72;
  --syntax-string: #a5d6ff;
  --syntax-comment: #8b949e;
  --syntax-function: #d2a8ff;
  --syntax-number: #79c0ff;
  --syntax-builtin: #ffa657;
  --syntax-type: #79c0ff;
  --syntax-constant: #79c0ff;
  --syntax-operator: #ff7b72;
  --syntax-attr: #7ee787;
  --syntax-variable: #ffa657;
  --syntax-symbol: #79c0ff;
  --syntax-section: #79c0ff;
  --syntax-addition: #7ee787;
  --syntax-deletion: #ff7b72;
  --syntax-meta: #8b949e;
}

body {
  margin: 0;
  padding: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  color: var(--text-color);
  background-color: var(--bg-color);
  line-height: 1.6;
  min-height: 100vh;
  transition: background-color 0.3s, color 0.3s;
}

/* Breadcrumbs */
.breadcrumbs {
  font-size: 0.85rem;
  color: var(--breadcrumb-color);
  margin-bottom: 1.5rem;
}

.breadcrumbs a {
  color: var(--breadcrumb-color);
}

.breadcrumbs span {
  margin: 0 0.5rem;
  opacity: 0.5;
}

.algorithm-container {
  margin: 2rem 0;
  max-width: 100%;
  overflow-x: auto;
  font-size: 1rem;
}

.ps-root {
  font-family: 'Inter', sans-serif !important;
}

img, video {
  max-width: 100%;
  height: auto;
}

table {
  display: block;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

/* Sidebar & Content Layout */
#preamble {
  position: static;
}

.navigation {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background-color: var(--sidebar-bg);
  border-right: 1px solid var(--border-color);
  padding: 2rem 1.5rem;
  overflow-y: auto;
  z-index: 100;
}

#content {
  margin-left: var(--sidebar-width);
  padding: 2rem 4rem;
  max-width: calc(var(--max-width) - var(--sidebar-width));
  flex: 1;
}

#postamble {
  margin-left: var(--sidebar-width);
  padding: 2rem 4rem;
  border-top: 1px solid var(--border-color);
  font-size: 0.8rem;
  color: #57606a;
  margin-top: 3rem;
}

/* Typography */
h1.title { font-size: 2.5rem; margin-top: 0; border-bottom: 1px solid var(--border-color); padding-bottom: 0.5rem; }
h2 { border-bottom: 1px solid var(--border-color); padding-bottom: 0.3rem; margin-top: 2.5rem; }
h3 { margin-top: 2rem; }

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

/* Navigation in Sidebar */
.navigation {
  display: flex;
  flex-direction: column;
}

.navigation a {
  padding: 0.5rem 0;
  font-weight: 600;
  color: var(--text-color);
  display: flex;
  align-items: center;
}

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

.navigation .nav-header {
  font-size: 0.8rem;
  text-transform: uppercase;
  color: #57606a;
  margin: 1.5rem 0 0.5rem 0;
  letter-spacing: 0.05rem;
}

/* Table of Contents */
#table-of-contents {
  position: fixed;
  right: 2rem;
  top: 2rem;
  width: 250px;
  background: var(--toc-bg);
  padding: 1rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 0.9rem;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 4px 12px rgba(0,0,0,0.05);
}

#table-of-contents h2 {
  font-size: 1rem;
  margin-top: 0;
  border: none;
}

#table-of-contents ul {
  padding-left: 1.2rem;
  list-style-type: none;
}

#table-of-contents li {
  margin: 0.3rem 0;
}

/* Org Code blocks */
pre.src {
  background-color: var(--code-bg);
  padding: 1rem;
  border-radius: 6px;
  border: 1px solid var(--border-color);
  overflow-x: auto;
}

/* org-mode syntax highlighting colors */
pre.src .org-keyword,
pre.src .org-builtin,
pre.src .org-preprocessor { color: var(--syntax-keyword); }
pre.src .org-string,
pre.src .org-doc { color: var(--syntax-string); }
pre.src .org-comment { color: var(--syntax-comment); }
pre.src .org-number { color: var(--syntax-number); }
pre.src .org-function-name { color: var(--syntax-function); }
pre.src .org-type { color: var(--syntax-type); }
pre.src .org-constant { color: var(--syntax-constant); }
pre.src .org-variable-name { color: var(--syntax-variable); }
pre.src .org-warning { color: var(--syntax-deletion); }

/* Tags */
.tag {
  float: right;
  font-size: 0.7rem;
  background: #eaeef2;
  padding: 2px 8px;
  border-radius: 12px;
  margin-left: 10px;
}

/* Search Bar */
#wiki-search {
  width: 100%;
  padding: 0.6rem;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  outline: none;
  transition: border-color 0.2s;
}

#wiki-search:focus {
  border-color: var(--link-color);
  box-shadow: 0 0 0 3px rgba(9, 105, 218, 0.1);
}

/* Backlinks */
.backlinks {
  margin-top: 4rem;
  padding: 1.5rem 2rem;
  background: var(--toc-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
}

.backlinks h4 {
  margin-top: 0;
  margin-bottom: 1rem;
  font-size: 1rem;
  color: var(--text-color);
  opacity: 0.7;
  border: none;
}

.backlinks ul {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.backlinks li a {
  background: var(--sidebar-bg);
  padding: 4px 12px;
  border-radius: 15px;
  border: 1px solid var(--border-color);
  font-size: 0.85rem;
  color: var(--text-color);
  text-decoration: none;
  transition: background 0.2s, color 0.2s;
}

.backlinks li a:hover {
  background: var(--link-color);
  color: white;
  border-color: var(--link-color);
}

.footer-info {
  margin-top: 2rem;
  text-align: center;
  font-size: 0.8rem;
  color: #8c959f;
}

.nav-spacer {
  flex-grow: 1;
}

/* Content Wrapper */
.main-content-wrapper {
  margin-left: var(--sidebar-width);
  padding: 2rem 4rem 0;
  max-width: calc(var(--max-width) - var(--sidebar-width));
}

#mynetwork {
  width: 100%;
  height: calc(100vh - 150px);
  min-height: 500px;
  border: 1px solid var(--border-color);
  background: var(--sidebar-bg);
  border-radius: 8px;
  margin-top: 1rem;
}

/* Breadcrumbs */
.breadcrumbs {
  margin-left: var(--sidebar-width);
  padding: 1rem 4rem;
  font-size: 0.85rem;
  color: var(--text-color);
  background: var(--sidebar-bg);
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
}

.breadcrumbs a {
  color: var(--link-color);
  text-decoration: none;
  font-weight: 500;
}

.breadcrumbs a:hover {
  text-decoration: underline;
}

.breadcrumbs span {
  margin: 0 0.6rem;
  color: var(--border-color);
}

/* Theme Toggle */
.theme-switch-wrapper {
  display: flex;
  align-items: center;
  margin-bottom: 1rem;
}

.theme-switch {
  display: inline-block;
  height: 24px;
  position: relative;
  width: 44px;
}

.theme-switch input {
  display:none;
}

.slider {
  background-color: #ccc;
  bottom: 0;
  cursor: pointer;
  left: 0;
  position: absolute;
  right: 0;
  top: 0;
  transition: .4s;
  border-radius: 24px;
}

.slider:before {
  background-color: #fff;
  bottom: 4px;
  content: "";
  height: 16px;
  left: 4px;
  position: absolute;
  transition: .4s;
  width: 16px;
  border-radius: 50%;
}

input:checked + .slider {
  background-color: var(--link-color);
}

input:checked + .slider:before {
  transform: translateX(20px);
}

/* Search Results Dropdown */
#wiki-search-results {
  position: absolute;
  background: var(--toc-bg);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  display: none;
  z-index: 1000;
  overflow: hidden;
}

.search-result-item {
  display: block;
  padding: 0.6rem 1rem;
  color: var(--text-color);
  text-decoration: none;
  font-size: 0.9rem;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.2s;
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-item:hover {
  background: var(--sidebar-bg);
  color: var(--link-color);
}

.search-result-title {
  display: block;
  font-weight: 600;
  font-size: 0.9rem;
}

.search-result-snippet {
  display: block;
  font-size: 0.78rem;
  color: var(--text-muted, #888);
  margin-top: 0.15rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Page Previews */
#wiki-preview {
  position: absolute;
  display: none;
  background: var(--toc-bg);
  border: 1px solid var(--border-color);
  padding: 1rem;
  border-radius: 8px;
  box-shadow: 0 10px 25px rgba(0,0,0,0.1);
  width: 300px;
  z-index: 1000;
  font-size: 0.9rem;
  pointer-events: none;
}

#wiki-preview strong {
  display: block;
  margin-bottom: 0.5rem;
  color: var(--link-color);
}

/* Lightbox */
#wiki-lightbox-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  cursor: zoom-out;
}

#wiki-lightbox-overlay img {
  max-width: 90%;
  max-height: 90%;
  border-radius: 4px;
  box-shadow: 0 0 30px rgba(0,0,0,0.5);
}

/* Mobile Sidebar Toggle - hidden on desktop */
.nav-toggle {
  display: none;
}

.sidebar-overlay {
  display: none;
}

/* Responsive */
@media (max-width: 1100px) {
  #table-of-contents {
    position: static;
    width: auto;
    margin-bottom: 2rem;
    box-shadow: none;
  }
}

@media (max-width: 768px) {
  .nav-toggle {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 4px;
    position: fixed;
    top: 0.5rem;
    right: 0.5rem;
    z-index: 250;
    width: 40px;
    height: 40px;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    padding: 0;
    transition: background 0.2s;
  }

  .nav-toggle:hover {
    background: var(--sidebar-bg);
  }

  .nav-toggle span {
    display: block;
    width: 20px;
    height: 2px;
    background: var(--text-color);
    border-radius: 1px;
    transition: transform 0.3s, opacity 0.3s;
  }

  .nav-toggle.open span:nth-child(1) {
    transform: translateY(6px) rotate(45deg);
  }
  .nav-toggle.open span:nth-child(2) {
    opacity: 0;
  }
  .nav-toggle.open span:nth-child(3) {
    transform: translateY(-6px) rotate(-45deg);
  }

  .sidebar-overlay.open {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.4);
    z-index: 200;
  }

  .navigation {
    position: fixed;
    left: -280px;
    top: 0;
    bottom: 0;
    width: var(--sidebar-width);
    z-index: 300;
    transition: left 0.3s ease;
    box-shadow: none;
  }

  .navigation.open {
    left: 0;
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
  }

  #preamble {
    position: static;
    width: auto;
    border-right: none;
    border-bottom: 1px solid var(--border-color);
  }

  #content, #postamble {
    margin-left: 0;
    padding: 1rem;
    max-width: 100%;
  }

  .breadcrumbs {
    margin-left: 0;
    padding: 0.75rem 1rem;
    flex-wrap: wrap;
  }

  h1.title { font-size: 1.8rem; }
  h2 { font-size: 1.4rem; }
  h3 { font-size: 1.2rem; }

  .navigation a {
    padding: 0.75rem 0;
  }

  #mynetwork {
    height: 50vh;
    min-height: 300px;
  }

  #wiki-search-results {
    position: fixed;
    left: 0 !important;
    right: 0;
    top: auto !important;
    width: auto !important;
    margin: 0 0.5rem;
    max-height: 60vh;
    overflow-y: auto;
  }

  .backlinks {
    padding: 1rem;
  }

  .tag {
    float: none;
    display: inline-block;
    margin: 0.2rem 0;
  }

  pre.src {
    padding: 0.75rem;
    font-size: 0.85rem;
  }

  .algorithm-container {
    font-size: 0.85rem;
  }
}

@media (max-width: 480px) {
  #content, #postamble {
    padding: 0.75rem;
  }

  .breadcrumbs {
    padding: 0.5rem 0.75rem;
    font-size: 0.75rem;
  }

  h1.title { font-size: 1.5rem; }
  h2 { font-size: 1.2rem; }
  h3 { font-size: 1.1rem; }

  .navigation {
    width: 240px;
    left: -260px;
  }

  .backlinks {
    padding: 0.75rem;
  }

  .backlinks ul {
    gap: 6px;
  }

  .backlinks li a {
    font-size: 0.8rem;
    padding: 3px 10px;
  }
}
