:root {
  /* Opacity percentage for mixing foreground color with transparency */
  --toc-mix: 50%;
  /* Active/focused state color - uses the main foreground color */
  --toc-color-active: var(--fg);
  /* Mixed color for default state - blends foreground color with transparency */
  --toc-color-mix: color-mix(in srgb, var(--fg) var(--toc-mix), transparent);
}

/* Main container for the entire TOC component */
.toc-container {
  display: flex;
  justify-content: flex-end;
  /* Follow same rules as mdBook main content area */
  margin-inline-end: auto;
  margin-inline-start: auto;
  max-width: var(--content-max-width);
  /* Stick to viewport when scrolling*/
  position: sticky;
  top: calc(var(--menu-bar-height) * 1.5);
  /* Ensure TOC appears above other content (notably: code blocks) */
  z-index: 101;
}

/* The clickable TOC toggle button (hamburger menu style) */
.toc-btn {
  border-radius: 0.3rem;
  border: 1px solid currentColor;
  color: var(--toc-color-mix);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
  justify-content: space-between;
  /* Offset button to the right, outside content area */
  margin-inline-end: -4rem;
  padding: 0.5rem;
  position: absolute;
  transition: all 200ms;
}

/* Interactive states - make button more visible when hovered, focused, or open */
.toc-btn:hover,
.toc-btn:focus,
.toc-btn:focus-visible,
.toc-btn__open:not(:focus),
.toc-btn__open:not(:focus-visible) {
  color: var(--toc-color-active);
  outline: none;
}

/* Restore default focus outline when TOC is open and focused */
.toc-btn__open:focus,
.toc-btn__open:focus-visible {
  outline: revert;
}

/* Individual lines of the hamburger menu icon */
.toc-btn > .toc-btn__line {
  background-color: currentColor;
  border-radius: 1rem;
  height: 2px;
  width: 1.5rem;
}

/* Dropdown content container that holds all TOC links */
.toc-content {
  background: var(--bg);
  border-radius: 0.3rem;
  border: 1px solid currentColor;
  display: none;
  flex-direction: column;
  max-height: calc(100vh - var(--menu-bar-height) - 10rem);
  overflow-y: auto;
  padding-block: 1rem;
  position: absolute;
  right: 0;
  top: 3rem;
  /* Prevent text wrapping in TOC links */
  white-space: nowrap;
}

/* Show the dropdown when TOC button is in open state */
.toc-btn__open .toc-content {
  display: flex;
}

/* Styling for individual TOC links */
.toc-content > a {
  outline: none;
  padding-block: 0.3rem;
  padding-inline: 2rem;
}

/* Hover and focus states for TOC links */
.toc-content > a:hover,
.toc-content > a:focus,
.toc-content > a:focus-visible {
  /* Reduce transparency for stronger background highlight */
  --toc-mix: 20%;
  background: var(--toc-color-mix);
}

/* Link color states - ensure consistent appearance */
.toc-content > a:link,
.toc-content > a:hover,
.toc-content > a:visited {
  color: var(--toc-color-active);
  text-decoration: none;
}

/* Hierarchical indentation for different heading levels */
.toc-content > .toc-h2 {
  padding-inline-start: 3rem;
}

.toc-content > .toc-h3 {
  padding-inline-start: 4rem;
}

.toc-content > .toc-h4 {
  padding-inline-start: 5rem;
}

.toc-content > .toc-h5 {
  padding-inline-start: 6rem;
}

.toc-content > .toc-h6 {
  padding-inline-start: 7rem;
}

/* Hide TOC on smaller screens where space is limited */
@media only screen and (max-width: 1380px) {
  .toc-container {
    display: none;
  }
}

/* Hide TOC when printing - not needed in print layout */
@media print {
  .toc-container {
    display: none;
  }
}
