/*
 * 1. ROOT & THEME VARIABLES
 * ======================================== */
:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;

  --c-page-bg: #ffffff;
  --c-page-text: #24292e;
  --c-header-bg: #ffffff;
  --c-header-border: #e6e6e6;
  --c-sidebar-bg: #f9f9f9;
  --c-sidebar-border: #e6e6e6;
  --c-sidebar-text: #333;
  --c-sidebar-link-hover-bg: #ebebeb;
  --c-sidebar-link-active-bg: #e0e0e0;
  --c-sidebar-link-active-text: #1981d8;
  --c-toc-text: #586069;
  --c-toc-link-active: #1981d8;
  --c-heading-border: #eaecef;
  --c-link: #1981d8;
  --c-code-bg: #f6f8fa;
  --c-code-text: #24292e;
  --c-code-border: #e6e6e6;
  --c-shadow: rgba(0, 0, 0, 0.1);
  --c-search-bg: #f6f8fa;
  --c-search-border: #e6e6e6;
}

[data-theme="dark"] {
  --c-page-bg: #131316;
  --c-page-text: #c9d1d9;
  --c-header-bg: #1c1c21;
  --c-header-border: #30363d;
  --c-sidebar-bg: #161a1f;
  --c-sidebar-border: #30363d;
  --c-sidebar-text: #c9d1d9;
  --c-sidebar-link-hover-bg: #21262d;
  --c-sidebar-link-active-bg: #2a3038;
  --c-sidebar-link-active-text: #66b2ff;
  --c-toc-text: #8b949e;
  --c-toc-link-active: #66b2ff;
  --c-heading-border: #30363d;
  --c-link: #66b2ff;
  --c-code-bg: #21262d;
  --c-code-text: #c9d1d9;
  --c-code-border: #30363d;
  --c-shadow: rgba(0, 0, 0, 0.3);
  --c-search-bg: #21262d;
  --c-search-border: #30363d;
}

/*
 * 2. BASE & LAYOUT
 * ======================================== */
* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background-color: var(--c-page-bg);
  color: var(--c-page-text);
  line-height: 1.6;
  transition: background-color 0.2s, color 0.2s;
}

.page-grid {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 60px 1fr;
  grid-template-areas:
    "header header"
    "nav    content";
  min-height: 100vh;
}

@media (min-width: 1200px) {
  .page-grid {
    grid-template-columns: 260px 1fr 240px;
    grid-template-areas:
      "header header  header"
      "nav    content toc";
  }
}

/*
 * 3. HEADER
 * ======================================== */
.page-header {
  grid-area: header;
  position: sticky;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  background-color: var(--c-header-bg);
  border-bottom: 1px solid var(--c-header-border);
  z-index: 10;
  transition: background-color 0.2s, border-color 0.2s;
}

.header-left, .header-right {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-link {
  display: flex;
  align-items: center;
  gap: 12px;
  text-decoration: none;
  color: var(--c-page-text);
  font-weight: 600;
  font-size: 1.05rem;
  padding: 8px;
}
.logo { height: 2.2rem; }

.theme-toggle {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  color: var(--c-toc-text);
  display: flex;
  align-items: center;
  justify-content: center;
}
.theme-toggle:hover { 
  color: var(--c-page-text); 
  background-color: var(--c-sidebar-link-hover-bg);
}
.theme-toggle svg { width: 22px; height: 22px; }
.theme-toggle .moon { display: none; }
.theme-toggle .sun { display: block; }

[data-theme="dark"] .theme-toggle .moon { display: block; }
[data-theme="dark"] .theme-toggle .sun { display: none; }

/*
 * 4. LEFT NAV SIDEBAR
 * ======================================== */
.page-nav {
  grid-area: nav;
  position: sticky;
  top: 60px; /* Header height */
  height: calc(100vh - 60px);
  overflow-y: auto;
  background-color: var(--c-sidebar-bg);
  border-right: 1px solid var(--c-sidebar-border);
  padding: 24px 0;
  transition: background-color 0.2s, border-color 0.2s;
}

.page-nav ul {
  list-style: none;
  padding: 0;
  margin: 0;
}
.page-nav ul ul {
  padding-left: 16px;
  border-left: 1px solid var(--c-sidebar-border);
  margin-left: 12px;
}
.page-nav li { padding: 0; margin: 0; }
.page-nav a {
  display: block;
  padding: 8px 24px;
  text-decoration: none;
  color: var(--c-sidebar-text);
  font-size: 0.9rem;
  font-weight: 500;
  border-radius: 0 4px 4px 0;
  margin-right: 12px;
  border-left: 3px solid transparent;
}
.page-nav a:hover {
  background-color: var(--c-sidebar-link-hover-bg);
}
.page-nav li.active > a {
  color: var(--c-sidebar-link-active-text);
  background-color: var(--c-sidebar-link-active-bg);
  border-left-color: var(--c-sidebar-link-active-text);
}

/*
 * 5. MAIN CONTENT
 * ======================================== */
.page-content-wrapper {
  grid-area: content;
  padding: 32px 48px;
  background-color: var(--c-page-bg);
}
.page-content {
  max-width: 800px;
  margin: 0 auto;
}
.page-content h1, .page-content h2, .page-content h3, .page-content h4 {
  font-weight: 600;
  color: var(--c-page-text);
  border-bottom: 1px solid var(--c-heading-border);
  padding-bottom: 8px;
}
.page-content h1 { font-size: 2.25rem; }
.page-content h2 { font-size: 1.75rem; }
.page-content h3 { font-size: 1.25rem; }
.page-content a { color: var(--c-link); text-decoration: none; }
.page-content a:hover { text-decoration: underline; }
.page-content pre {
  background-color: var(--c-code-bg);
  color: var(--c-code-text);
  border: 1px solid var(--c-code-border);
  border-radius: 8px;
  padding: 1em;
  overflow-x: auto;
  font-family: var(--font-mono);
}
.page-content code {
  background-color: var(--c-code-bg);
  color: var(--c-code-text);
  padding: 0.2em 0.4em;
  border-radius: 4px;
  font-family: var(--font-mono);
  font-size: 85%;
}
.page-content pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: 1em;
}

/*
 * 6. RIGHT TOC SIDEBAR
 * ======================================== */
.page-toc {
  grid-area: toc;
  position: sticky;
  top: 60px; /* Header height */
  height: calc(100vh - 60px);
  overflow-y: auto;
  padding: 24px;
  border-left: 1px solid var(--c-sidebar-border);
  display: none; /* Hidden by default */
  font-size: 0.8rem;
  color: var(--c-toc-text);
}
@media (min-width: 1200px) {
  .page-toc { display: block; } /* Show on large screens */
}
.toc-sticky { position: sticky; top: 24px; }
.page-toc h3 {
  font-size: 0.9rem;
  font-weight: 600;
  margin-top: 0;
  color: var(--c-page-text);
  border-bottom: 1px solid var(--c-heading-border);
  padding-bottom: 4px;
}
.page-toc ul { list-style: none; padding: 0; margin: 0; }
.page-toc ul ul { padding-left: 16px; }
.page-toc a { display: block; padding: 4px 0; text-decoration: none; color: var(--c-toc-text); }
.page-toc a:hover { color: var(--c-page-text); }
.page-toc .toc-link.is-active-link { color: var(--c-toc-link-active); font-weight: 500; }

/*
 * 7. SEARCH
 * ======================================== */
.search-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}
.search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--c-toc-text);
  display: flex;
  align-items: center;
}
.search-icon svg { width: 20px; height: 20px; }
.search-input {
  background-color: var(--c-search-bg);
  border: 1px solid var(--c-search-border);
  border-radius: 6px;
  padding: 8px 8px 8px 36px;
  color: var(--c-page-text);
  font-family: var(--font-sans);
  font-size: 0.9rem;
  transition: all 0.2s ease;
  width: 200px;
}
.search-input:focus {
  background-color: var(--c-page-bg);
  border-color: var(--c-link);
  width: 250px;
}
.search-results {
  display: none;
  position: absolute;
  top: 120%;
  right: 0;
  width: 300px;
  background-color: var(--c-header-bg);
  border: 1px solid var(--c-header-border);
  border-radius: 8px;
  box-shadow: 0 4px 12px var(--c-shadow);
  max-height: 400px;
  overflow-y: auto;
  z-index: 20;
}
.search-results ul {
  list-style: none;
  padding: 8px;
  margin: 0;
}
.search-results li {
  padding: 0;
  margin: 0;
}
.search-results li a, .search-results li {
  display: block;
  padding: 8px 12px;
  text-decoration: none;
  color: var(--c-toc-text);
  font-size: 0.9rem;
  border-radius: 4px;
}
.search-results li a:hover {
  background-color: var(--c-sidebar-link-hover-bg);
  color: var(--c-page-text);
}
.search-results li:not(:has(a)) {
  font-style: italic;
  font-size: 0.9rem;
}


/*
 * 8. MOBILE RESPONSIVENESS & MENU
 * ======================================== */
.mobile-menu-toggle {
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  color: var(--c-toc-text);
  display: flex;
  align-items: center;
  justify-content: center;
}
.mobile-menu-toggle svg { width: 24px; height: 24px; }
.mobile-menu-toggle:hover { 
  color: var(--c-page-text); 
  background-color: var(--c-sidebar-link-hover-bg);
}
.mobile-menu-close {
  display: none; /* Hidden by default */
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  color: var(--c-toc-text);
  position: absolute;
  top: 12px;
  right: 12px;
}
.mobile-menu-close svg { width: 24px; height: 24px; }
.mobile-menu-close:hover {
  color: var(--c-page-text); 
  background-color: var(--c-sidebar-link-hover-bg);
}
.mobile-overlay {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  z-index: 998;
}

@media (min-width: 769px) {
  /* Hide mobile hamburger on desktop */
  .mobile-menu-toggle { display: none; }
}

@media (max-width: 768px) {
  .page-grid {
    grid-template-columns: 1fr;
    grid-template-rows: 60px 1fr;
    grid-template-areas:
      "header"
      "content";
  }
  .page-toc { display: none; } /* Hide ToC on mobile */
  .page-content-wrapper { padding: 24px; }
  .logo-title { display: none; } /* Hide title on mobile */
  .search-input:focus { width: 200px; }

  /* Mobile Nav Drawer */
  .page-nav { 
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    height: 100vh;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease-out;
    border-right: 1px solid var(--c-sidebar-border);
    box-shadow: 0 0 20px rgba(0,0,0,0.2);
    padding-top: 60px; /* Make space for close button */
  }
  .mobile-menu-close { display: flex; }

  /* Mobile Menu Open State */
  .is-mobile-menu-open .page-nav {
    transform: translateX(0);
  }
  .is-mobile-menu-open .mobile-overlay {
    display: block;
  }
}