.toc {
  position: sticky;
  top: calc(var(--top-stack-height) + 1rem);
  height: fit-content;
  max-height: calc(100vh - var(--header-height) - 2rem);
  z-index: 5;
  border-radius: var(--border-radius-sm);
  display: flex;
  flex-direction: column;

  @media (min-width: 1025px) {
    overflow: hidden;
  }

  & .toc-title {
    font-weight: 500;
    /* margin-bottom: 1.5rem; */
    padding-left: 8px;
    font-size: 1.25rem;
  }

  & .toc-nav {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
  }

  .toc-header {
    padding: 32px 24px 18px;

    /* & .toc-header {
    padding-inline: 1rem;
  } */
  }

  & .toc-sublist {
    margin-top: 4px;
    margin-left: 16px;
  }

  .toc-btn {
    border-radius: var(--button-border-radius);
    background-color: var(--background-color);

    @media (min-width: 1025px) {
      display: none;
    }

    &.toc-btn-active {
      display: none;
    }
  }

  .toc-close-btn {
    position: absolute;
    top: 1rem;
    right: 1rem;

    @media (min-width: 1025px) {
      display: none;
    }
  }

  .toc-content {
    /* overflow-y: auto; */
    border: 1px solid var(--neutral-300);
    border-radius: var(--border-radius-sm);
    display: flex;
    flex-direction: column;
    background-color: #fff;

    @media (min-width: 1025px) {
      flex: 1;
      min-height: 0;
      display: flex;
      flex-direction: column;
    }
  }

  & .toc-list {
    padding: 0 1.5rem 1.5rem 1.5rem;

    & .toc-item {
      margin-bottom: 4px;

      & .toc-item-link {
        padding: 8px 16px;
        display: block;

        &.current-show {
          background-color: var(--neutral-800);
          border-radius: 999px;
          color: #fff;
        }
      }
    }

    a {
      color: var(--neutral-800);
      transition: color 0.3s ease;
    }
  }
}

/* Mobile sticky bottom TOC */
@media (max-width: 1024px) {
  .toc {
    position: fixed;
    bottom: 1rem;
    right: 1rem;
    top: auto;
    max-height: none;
    height: auto;
    z-index: 99;
  }

  .toc .toc-btn {
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
    transition:
      transform 0.2s ease,
      box-shadow 0.2s ease;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2);
    }
  }

  .toc .toc-content {
    position: absolute;
    bottom: 0;
    right: 0;
    width: min(85vw, 320px);
    max-height: 70vh;
    opacity: 0;
    visibility: hidden;
    transform: translateY(10px);
    transition:
      opacity 0.25s ease,
      transform 0.25s ease,
      visibility 0.25s;
    overflow-y: auto;
  }

  .toc.toc-open .toc-content {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
  }

  .toc-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    /* Dynamic viewport height for mobile browsers */
    background: rgba(0, 0, 0, 0.3);
    opacity: 0;
    visibility: hidden;
    transition:
      opacity 0.25s ease,
      visibility 0.25s;
    z-index: 98;
  }

  .toc-overlay.active {
    opacity: 1;
    visibility: visible;
  }
}
