/* Dynamic per-post table of contents (topics/subtopics sidebar). */

html {
  scroll-behavior: smooth;
}

#content h2,
#content h3 {
  scroll-margin-top: calc(var(--nav-height) + var(--space-lg));
}

#toc {
  margin: 0;
  padding: var(--space-sm);
  box-sizing: border-box;
}

#toc,
#toc ul {
  list-style: none;
}

#toc > ul {
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-xs);
}

#toc li.toc-topic {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#toc ul.toc-subtopics {
  margin: 2px 0 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#toc a {
  display: block;
  padding: 2px var(--space-xs) 2px var(--space-sm);
  border-left: 2px solid transparent;
  line-height: 1.3;
}

#toc li.toc-topic > a {
  font-size: .9em;
  font-weight: var(--font-weight-bold);
}

#toc li.toc-subtopic > a {
  font-size: .8em;
  font-weight: var(--font-weight-light);
  opacity: .8;
}

#toc a.active,
#toc li.active-parent > a {
  border-left-color: var(--accent-color);
  opacity: 1;
}

/* Desktop: fixed sidebar living in the left gutter, content stays centered. */
@media (min-width: 1440px) {
  #toc {
    position: fixed;
    top: calc(var(--nav-height) + var(--space-lg));
    right: calc(50% + (var(--body-max-width) / 2) + var(--space-md));
    width: 190px;
    max-height: calc(100dvh - var(--nav-height) - var(--space-lg) * 2);
    overflow-y: auto;
  }

  #toc-fab,
  #toc-overlay {
    display: none;
  }
}

/* Below the gutter threshold (tablet/phone too): collapsible drawer + FAB. */
@media (max-width: 1439.98px) {
  #toc {
    position: fixed;
    top: var(--nav-height);
    bottom: 0;
    inset-inline-start: 0;
    width: min(280px, 80vw);
    padding-top: var(--space-sm);
    background-color: var(--surface-color);
    border-right: var(--border);
    overflow-y: auto;
    transform: translateX(-100%);
    transition: transform .25s ease;
    z-index: 40;
  }

  #toc.open {
    transform: translateX(0);
  }

  #toc-fab {
    position: fixed;
    inset-inline-end: var(--space-md);
    inset-block-end: var(--space-md);
    width: 3em;
    height: 3em;
    display: flex;
    align-items: center;
    justify-content: center;
    border: var(--border);
    border-radius: 50%;
    background-color: var(--accent-color);
    color: var(--background-color);
    font-size: 1em;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
    z-index: 50;
  }

  #toc-fab:hover {
    opacity: var(--hover-opacity);
  }

  #toc-overlay {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, .5);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s ease;
    z-index: 30;
  }

  #toc-overlay.open {
    opacity: 1;
    pointer-events: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  #toc,
  #toc-overlay {
    transition: none;
  }
}
