/* Sticky Table of Contents */

/* Desktop: floating sidebar */
.sticky-toc-container {
  --sticky-top: 5rem;
  --header-height: 80px;
  position: sticky;
  top: var(--sticky-top);
  max-width: 250px;
  width: 100%;
  background: #ffffff;
  border: 1px solid #e5e7eb;
  border-radius: 0.75rem;
  box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
  font-size: 0.875rem;
  max-height: calc(100vh - var(--sticky-top) - 2rem);
  overflow-y: auto;
  scrollbar-width: thin;
}

.sticky-toc-container .toc-header {
  padding: 0.75rem 1rem;
  font-weight: 700;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: #6b7280;
  border-bottom: 1px solid #f3f4f6;
  background: #f9fafb;
}

.sticky-toc-container .toc-nav {
  padding: 0.5rem 0;
}

.sticky-toc-container .toc-item {
  display: block;
  padding: 0.375rem 1rem;
  color: #6b7280;
  text-decoration: none;
  border-left: 3px solid transparent;
  transition: color 0.2s ease, border-color 0.2s ease, background 0.2s ease;
  line-height: 1.4;
}

.sticky-toc-container .toc-item:hover {
  color: #1e40af;
  background: #f0f4ff;
  border-left-color: #93c5fd;
}

.sticky-toc-container .toc-item.active {
  color: #1e40af;
  font-weight: 600;
  border-left-color: #1e40af;
  background: #eff6ff;
}

.sticky-toc-container .toc-item.level-3 {
  padding-left: 1.75rem;
  font-size: 0.8125rem;
}

.sticky-toc-container .toc-item.level-2 {
  font-weight: 500;
}

/* Tablet: inline above article */
@media (min-width: 768px) and (max-width: 1024px) {
  .sticky-toc-container {
    position: static;
    max-width: 100%;
    width: 100%;
    margin-bottom: 1.5rem;
    border-radius: 0.5rem;
    max-height: none;
    overflow: visible;
  }

  .sticky-toc-container .toc-nav {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 0.75rem 1rem;
  }

  .sticky-toc-container .toc-header {
    border-bottom: none;
    background: transparent;
    padding: 0.75rem 1rem 0 1rem;
  }

  .sticky-toc-container .toc-item {
    border-left: none;
    border: 1px solid #e5e7eb;
    border-radius: 9999px;
    padding: 0.25rem 0.75rem;
    font-size: 0.8125rem;
  }

  .sticky-toc-container .toc-item:hover {
    border-left: none;
    border-color: #93c5fd;
    background: #f0f4ff;
  }

  .sticky-toc-container .toc-item.active {
    border-left: none;
    border-color: #1e40af;
    background: #eff6ff;
    color: #1e40af;
  }

  .sticky-toc-container .toc-item.level-3 {
    padding-left: 0.75rem;
  }
}

/* Mobile: collapsed toggle */
@media (max-width: 767px) {
  .sticky-toc-wrapper {
    margin-bottom: 1.5rem;
  }

  .sticky-toc-container {
    position: static;
    max-width: 100%;
    width: 100%;
    max-height: 0;
    border-radius: 0;
    border: none;
    box-shadow: none;
    overflow: hidden;
    transition: max-height 0.3s ease, box-shadow 0.3s ease;
  }

  .sticky-toc-container.expanded {
    max-height: 60vh;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.05);
  }

  .sticky-toc-container .toc-nav {
    padding: 0.5rem 0;
  }

  .sticky-toc-container .toc-item.level-3 {
    padding-left: 1.75rem;
  }

  .toc-toggle-btn {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    padding: 0.75rem 1rem;
    background: #ffffff;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    font-size: 0.875rem;
    font-weight: 600;
    color: #374151;
    cursor: pointer;
    transition: background 0.2s ease;
    font-family: inherit;
  }

  .toc-toggle-btn:hover {
    background: #f9fafb;
  }

  .toc-toggle-btn svg {
    transition: transform 0.2s ease;
    flex-shrink: 0;
  }

  .toc-toggle-btn.open svg {
    transform: rotate(180deg);
  }
}

@media (min-width: 768px) {
  .toc-toggle-btn {
    display: none;
  }
}