/* Navigation styles */

#main-nav {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: var(--nav-width);
  padding: 2rem 1rem 4rem;
  background: var(--card-bg);
  border-right: 1px solid var(--border-muted);
  display: flex;
  flex-direction: column;
  text-align: center;
  z-index: 100;
}

#main-nav hgroup {
  margin-bottom: 2rem;
}

#main-nav .name {
  font-size: 1.1rem;
  margin: 0;
  padding: 0.5rem;
  background: var(--primary);
  color: var(--primary-inverse);
  border-radius: 0.25rem;
  white-space: nowrap;
}

#main-nav .title {
  font-size: 1rem;
  color: var(--text-muted);
  margin: 0.5rem 0 0 0;
  white-space: nowrap;
}

#main-nav ul {
  flex-direction: column;
  margin: auto 0;
  padding: 0;
}

#main-nav li {
  padding: 0.5rem 0;
}

/* Docs variant of the sidebar (moved from docs.css so all #main-nav
   chrome lives in one file). */
#main-nav.docs-nav {
  text-align: left;
  overflow-y: auto;
  scrollbar-width: none;

  &::-webkit-scrollbar {
    display: none;
  }

  hgroup {
    margin-bottom: 1rem;
    text-align: left;
  }

  .nav-back {
    display: inline-block;
    font-size: var(--font-size-sm);
    color: var(--text-muted);
    text-decoration: none;
    margin-bottom: 0.5rem;

    &:hover {
      color: var(--text);
    }

    i {
      margin-right: 0.25rem;
    }
  }

  .name {
    white-space: normal;

    a {
      color: var(--primary-inverse);
      text-decoration: none;

      &:hover {
        opacity: 0.8;
      }
    }
  }
}

/* ===== TOP NAV BAR (home/non-docs pages) ===== */

#main-nav.top-nav {
  bottom: auto;
  right: 0;
  width: 100%;
  height: 60px;
  padding: 0 clamp(1rem, 3vw, 2rem);
  border-right: none;
  border-bottom: 1px solid var(--border-muted);
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  text-align: left;
  z-index: 200;
}

.top-nav-brand {
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;

  &:hover {
    color: var(--primary-inverse);
  }
}

.top-nav-icons {
  display: flex;
  align-items: center;
  gap: 1rem;

  a {
    color: var(--text-muted);
    font-size: 1.15rem;
    text-decoration: none;
    transition: color 0.15s ease;

    &:hover {
      color: var(--text);
    }
  }
}

.top-nav .theme-toggle {
  position: static;
  width: auto;
  bottom: auto;
  left: auto;
  border: none;
  padding: 0.25rem;
  font-size: 1.15rem;
  color: var(--text-muted);
  z-index: auto;

  &:hover {
    background: transparent;
    color: var(--text);
  }
}

/* Mobile hamburger button (lives in the docs toolbar; the drawer slides
   out from under the bar). Visibility is controlled by the
   .toolbar-btn.hamburger rule in toolbar.css — this holds only the
   burger-lines styling. */
.hamburger {
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;

  span {
    display: block;
    width: 24px;
    height: 3px;
    background: var(--text);
    transition: all 0.3s ease;
    transform-origin: center;
  }

  &.active {
    span:nth-child(1) {
      transform: translateY(4px) rotate(45deg);
    }
    span:nth-child(2) {
      transform: translateY(-4px) rotate(-45deg);
    }
  }
}

.theme-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text);
  transition: background 0.2s ease, color 0.2s ease;

  i {
    font-size: 1rem;
  }

  .fa-sun {
    display: none;
  }

  .fa-moon {
    display: inline;
  }
}

[data-theme="light"] .theme-toggle {
  .fa-sun {
    display: inline;
  }

  .fa-moon {
    display: none;
  }
}

.nav-content {
  display: contents;
}

.nav-overlay {
  display: none;
}

/* ===== SIDEBAR RESIZE HANDLE ===== */

.sidebar-resize-indicator {
  position: absolute;
  top: 0;
  right: -4px;
  width: 8px;
  height: 100%;
  cursor: col-resize;
  z-index: 200;

  &:hover::after, &.dragging::after {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 2px;
    height: 100%;
    background: var(--primary-inverse);
    opacity: 0.4;
  }
}

/* ===== RESPONSIVE NAV STYLES ===== */

@media (max-width: 768px) {
  /* Desktop panel toggle is meaningless on mobile (the drawer takes
     over); the hamburger occupies its slot. */
  .sidebar-toggle {
    display: none;
  }

  #main-nav:not(.top-nav) {
    /* Starts below the toolbar and slides out from under it: z-index
       below the toolbar's 50 so the bar masks the drawer mid-slide. */
    top: calc(var(--toolbar-height) + 1px);
    z-index: 40;
    width: 100%;
    max-height: calc(100vh - var(--toolbar-height));
    padding-top: 1rem;
    border: none;
    border-bottom: 1px solid var(--border-muted);
    transform: translateY(-100%);
    transition: transform 0.3s ease;
    overflow-y: auto;

    &.open {
      transform: translateY(0);
    }
  }

  #main-nav.top-nav {
    padding: 0 1rem;
  }

  .top-nav-icons {
    gap: 0.75rem;
  }

  .nav-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.4);
    /* Below the drawer (40) and toolbar (50). */
    z-index: 30;

    &.visible {
      display: block;
    }
  }

  #main-nav:not(.top-nav) .theme-toggle {
    display: none;
  }

  #main-nav ul {
    flex-direction: column;
    flex-wrap: nowrap;
    margin: 0;
  }

  #main-nav hgroup {
    display: none;
  }
}

/* ===== SIDEBAR COLLAPSE (desktop) ===== */

@media (min-width: 769px) {
  #main-nav:not(.top-nav) {
    transition: transform 0.3s ease;
    /* The Contents header supplies the top spacing; zero padding lets
       its sticky position pin flush at the scrollport top. */
    padding-top: 0;
  }

  .docs-page main {
    transition: margin-left 0.3s ease;
  }

  html.sidebar-collapsed #main-nav {
    transform: translateX(-100%);
  }

  /* Recenter with an animatable margin instead of `margin: 0 auto` +
     a max-width jump — auto margins and none→px max-width can't
     transition and used to make the content leap. */
  html.sidebar-collapsed main {
    margin-left: max(0px, calc((100vw - 1100px) / 2));
  }

  html.sidebar-collapsed .docs-layout {
    max-width: 1100px;
  }

  html.sidebar-collapsed .docs-toc {
    flex-basis: 0;
    opacity: 0;
  }

  html.sidebar-collapsed .site-footer {
    margin-left: max(0px, calc((100vw - 1100px) / 2));
    max-width: 1100px;
  }
}

/* Rustdoc-style sidebar resize handle: two staggered dot columns. Ported
   from the Notch docs. Fixed to the viewport (the nav is its own scroll
   container, so an absolute handle would scroll away); tracking
   --nav-width keeps it glued to the sidebar edge during a drag. The drag
   behavior lives in assets/js/modules/docs-sidebar.js. */
.sidebar-resize-indicator {
  touch-action: none;
  position: fixed;
  top: 0;
  bottom: 0;
  left: var(--nav-width);
  width: 14px;
  padding-left: 5px;
  cursor: ew-resize;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  color: var(--text-muted);

  &::before {
    content: '';
    width: 2px;
    height: 16px;
    margin-right: 2px;
    background-image: radial-gradient(circle, currentColor 0.9px, transparent 1.1px);
    background-size: 2px 4px;
    background-repeat: repeat;
  }

  &::after {
    content: '';
    width: 2px;
    height: 20px;
    background-image: radial-gradient(circle, currentColor 0.9px, transparent 1.1px);
    background-size: 2px 4px;
    background-repeat: repeat;
  }

  &:hover,
  &:active {
    width: 15px;
    left: calc(var(--nav-width) - 1px);
    border-left: solid 1px var(--sidebar-resizer-hover);
    color: var(--sidebar-resizer-hover);
  }

  /* Wide invisible hit area while dragging so fast pointer movement
     can't escape the handle. */
  &.dragging {
    padding: 0 140px;
    width: 291px;
    margin-left: -140px;
    border-left: none;
    color: var(--sidebar-resizer-active);
  }

  @media (max-width: 768px), (pointer: coarse) {
    display: none;
  }
}

/* Momentarily disables all transitions while docs-sidebar.js measures
   the collapse destination layout between two invisible class flips. */
html.sidebar-no-anim *,
html.sidebar-no-anim *::before,
html.sidebar-no-anim *::after {
  transition: none !important;
}

/* Docs sidebar/content separation drawn as a pseudo-element starting
   BELOW the blended top bar, so no vertical seam cuts the bar; only the
   horizontal border line crosses the corner. */
@media (min-width: 769px) {
  #main-nav.docs-nav {
    border-right: none;

    &::after {
      content: '';
      position: absolute;
      top: calc(var(--toolbar-height) + 1px);
      bottom: 0;
      right: 0;
      width: 1px;
      background: var(--border-muted);
    }
  }

  /* Solid guide line while a drag is in progress (widens/recolors the
     separation line). */
  #main-nav:has(.sidebar-resize-indicator.dragging)::after {
    width: 2px;
    background: var(--sidebar-resizer-active);
  }
}

html.sidebar-collapsed .sidebar-resize-indicator {
  display: none;
}

/* "Contents" header pinned at the top of the docs sidebar, holding the
   canonical side-panel close button (see sidebar-contents-header.html). */
.sidebar-contents-header {
  /* Pinned to the top of the nav's own scrollport so "Contents" stays
     visible while the sidebar list scrolls. */
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  /* Label centered over the sidebar. */
  justify-content: center;
  gap: 0.5rem;
  /* +1px so the bottom border lands at the same y as the toolbar's
     (toolbar = --toolbar-height content + 1px border; this is border-box). */
  height: calc(var(--toolbar-height) + 1px);
  flex-shrink: 0;
  margin: 0 -1rem 1rem;
  padding: 0 0.5rem;
  background: var(--toolbar-bg);
  border-bottom: 1px solid var(--toolbar-border);
  text-align: left;
}

.sidebar-contents-label {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  /* Emerges from behind the toggle button: fades + slides out to the
     right slightly after the panel starts opening. */
  transition: opacity 0.18s ease 0.12s, transform 0.22s ease 0.08s;
}

html.sidebar-collapsed .sidebar-contents-label {
  opacity: 0;
  transform: translateX(-10px);
  transition-delay: 0s, 0s;
}

@media (max-width: 768px) {
  /* The mobile drawer is closed by the hamburger, not this header. */
  .sidebar-contents-header {
    display: none;
  }
}
