/**
 * Enhanced Table of Contents (TOC) with Intelligent Grouping
 *
 * Features:
 * - Hierarchical grouping by H2 sections
 * - Collapsible/expandable sections
 * - Progress indicator with position marker
 * - Quick navigation buttons
 * - Compact mode for deeply nested content
 * - Full keyboard navigation support
 */

/* ============================================================================
   Layout & Container
   ============================================================================ */

.page-layout {
    display: block;
}

.page-layout.page-with-toc {
    display: grid;
    grid-template-columns: 1fr;
    gap: 2rem;
    align-items: start;
}

@media (min-width: 1024px) {
    .page-layout.page-with-toc {
        grid-template-columns: 1fr 280px;
    }
}

/* TOC Sidebar Container */
.page-sidebar {
    order: 2;
}

@media (min-width: 1024px) {
    .page-sidebar {
        position: sticky;
        top: 2rem;
        max-height: calc(100vh - 4rem);
        overflow-y: auto;
    }
}

.toc-sidebar {
    display: flex;
    flex-direction: column;
    gap: 1rem;
    height: 100%;
    overflow-x: hidden;
    min-width: 0;
}

/* ============================================================================
   TOC Header with Controls
   ============================================================================ */

.toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding-bottom: 0.5rem;
    margin-bottom: 0.75rem;
    border-bottom: 1px solid var(--color-border-light);
    position: relative;
}

.toc-title {
    margin: 0;
    font-size: var(--text-sm);
    font-weight: var(--font-weight-semibold);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-tertiary);
    flex: 1;
}

/* Settings button - minimal dot menu */
.toc-settings-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    padding: 0;
    background: transparent;
    border: none;
    border-radius: var(--border-radius-small);
    color: var(--color-text-tertiary);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.toc-settings-btn:hover {
    background: var(--color-bg-hover);
    color: var(--color-text-primary);
}

.toc-settings-btn svg {
    flex-shrink: 0;
}

/* Settings dropdown menu */
.toc-settings-menu {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 0.25rem;
    background: var(--color-bg-primary, white);
    border: 1px solid var(--color-border);
    border-radius: var(--border-radius);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    padding: 0.25rem;
    z-index: 100;
    min-width: 140px;
}

.toc-settings-menu button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 0.5rem 0.75rem;
    font-size: var(--text-sm);
    color: var(--color-text-secondary);
    background: transparent;
    border: none;
    border-radius: var(--border-radius-small);
    cursor: pointer;
    transition: all var(--transition-fast);
}

.toc-settings-menu button:hover {
    background: var(--color-bg-hover);
    color: var(--color-text-primary);
}

/* ============================================================================
   Scroll Progress Indicator - Subtle
   ============================================================================ */

.toc-progress {
    position: relative;
    width: 1px;
    height: 100%;
    background: transparent;
    border-radius: 1px;
    overflow: visible;
}

.toc-progress-bar {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 0%;
    background: var(--color-primary);
    opacity: 0.3;
    transition: height 0.1s linear, opacity 0.2s ease;
    border-radius: 1px;
}

.toc-progress:hover .toc-progress-bar {
    opacity: 0.5;
}

/* ============================================================================
   TOC Navigation
   ============================================================================ */

.toc-nav {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.toc-scroll-container {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overflow-x: hidden;
    margin-right: -0.5rem;
    padding-right: 0.5rem;
}

.toc-scroll-container::-webkit-scrollbar {
    width: 4px;
}

.toc-scroll-container::-webkit-scrollbar-track {
    background: transparent;
}

.toc-scroll-container::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 2px;
}

.toc-scroll-container::-webkit-scrollbar-thumb:hover {
    background: var(--color-border-dark);
}

.toc-items {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.toc-item {
    margin: 0;
}

/* ============================================================================
   TOC Group (H2 Sections) - Minimal Design
   ============================================================================ */

.toc-group {
    border-radius: var(--border-radius-small);
    overflow: hidden;
    background: transparent;
    border: none;
    transition: all var(--transition-fast);
}

.toc-group-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.375rem 0;
    background: transparent;
    min-width: 0;
}

/* Count badge as toggle button - no chevron needed */
.toc-count-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    background: transparent;
    border: none;
    cursor: pointer;
    transition: all var(--transition-fast);
    flex-shrink: 0;
}

.toc-count-toggle:hover {
    transform: scale(1.1);
}

.toc-count-toggle:hover .toc-count {
    opacity: 1;
    background: var(--color-primary-light);
    color: var(--color-primary);
    border-color: var(--color-primary);
}

.toc-count-toggle:active {
    transform: scale(0.95);
}

/* Visual feedback for expanded state */
.toc-count-toggle[aria-expanded="true"] .toc-count {
    opacity: 1;
    background: var(--color-primary-light);
    color: var(--color-primary);
    font-weight: var(--font-weight-semibold);
}

.toc-link-h2 {
    flex: 1;
    font-weight: var(--font-weight-medium);
    font-size: var(--text-sm);
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.toc-count {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 0.375rem;
    font-size: 11px;
    font-weight: var(--font-weight-medium);
    color: var(--color-text-tertiary);
    background: var(--color-bg-secondary, #f5f5f5);
    border: 1px solid var(--color-border-light);
    border-radius: 10px;
    flex-shrink: 0;
    opacity: 0.7;
    transition: all var(--transition-fast);
}

.toc-subitems {
    list-style: none;
    padding: 0.25rem 0;
    padding-left: 1.5rem;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
    max-height: 500px;
    overflow: hidden;
    transition: max-height 0.3s ease, padding 0.3s ease;
}

.toc-group[data-collapsed="true"] .toc-subitems {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
}

/* ============================================================================
   TOC Links
   ============================================================================ */

.toc-link {
    display: block;
    padding: 0.25rem 0.5rem;
    font-size: var(--text-sm);
    line-height: 1.5;
    color: var(--color-text-secondary);
    text-decoration: none;
    border-left: 2px solid transparent;
    margin-left: -2px;
    border-radius: var(--border-radius-small);
    transition: all var(--transition-fast);
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
}

.toc-link:hover {
    color: var(--color-text-primary);
}

.toc-link.active {
    color: var(--color-primary);
    font-weight: var(--font-weight-medium);
    border-left-color: var(--color-primary);
}

/* ============================================================================
   Heading Levels - Enhanced hierarchy
   Note: level 1 = H2, level 2 = H3, level 3 = H4, etc.
   ============================================================================ */

.toc-level-2 .toc-link {
    padding-left: 1rem;
    font-size: var(--text-sm);
}

.toc-level-3 .toc-link {
    padding-left: 1.75rem;
    font-size: var(--text-xs);
    opacity: 0.9;
}

.toc-level-4 .toc-link {
    padding-left: 2.5rem;
    font-size: var(--text-xs);
    opacity: 0.85;
}

.toc-level-5 .toc-link {
    padding-left: 3.25rem;
    font-size: var(--text-xs);
    opacity: 0.8;
}

/* ============================================================================
   Compact Mode
   ============================================================================ */

[data-toc-mode="compact"] .toc-group {
    background: transparent;
    border: none;
    box-shadow: none;
}

[data-toc-mode="compact"] .toc-group-header {
    padding: 0.25rem 0;
    background: transparent;
}

[data-toc-mode="compact"] .toc-subitems {
    padding: 0.25rem 0;
    padding-left: 1.5rem;
}

[data-toc-mode="compact"] .toc-link {
    padding: 0.25rem 0.5rem;
}

[data-toc-mode="compact"] .toc-count {
    font-size: 10px;
    min-width: 18px;
    height: 18px;
}

[data-toc-mode="compact"] .toc-level-3,
[data-toc-mode="compact"] .toc-level-4,
[data-toc-mode="compact"] .toc-level-5 {
    display: none;
}


/* ============================================================================
   TOC Content (Fallback HTML)
   ============================================================================ */

.toc-content {
    font-size: var(--text-sm);
}

.toc-content ul {
    list-style: none;
    padding: 0;
    margin: 0;
}

.toc-content li {
    margin: 0;
}

.toc-content ul ul {
    padding-left: 0.75rem;
    margin-top: 0.25rem;
}

.toc-content a {
    display: block;
    padding: 0.375rem 0.75rem;
    color: var(--color-text-secondary);
    text-decoration: none;
    border-left: 2px solid transparent;
    margin-left: -2px;
    transition: all var(--transition-fast);
    border-radius: var(--border-radius-small);
}

.toc-content a:hover {
    color: var(--color-text-primary);
    background: var(--color-bg-hover);
}

.toc-content a.active {
    color: var(--color-primary);
    font-weight: var(--font-weight-medium);
    border-left-color: var(--color-primary);
    background: var(--color-primary-light);
}

/* ============================================================================
   Page Metadata
   ============================================================================ */

.toc-metadata {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    padding-top: 0.75rem;
    margin-top: 0.75rem;
    border-top: 1px solid var(--color-border-light);
    font-size: var(--text-xs);
}

.toc-meta-item {
    display: flex;
    gap: 0.375rem;
    color: var(--color-text-tertiary);
}

.toc-meta-item svg {
    flex-shrink: 0;
    margin-top: 0.125rem;
    opacity: 0.5;
}

.toc-meta-label {
    font-weight: var(--font-weight-medium);
    color: var(--color-text-tertiary);
    margin-bottom: 0.125rem;
}

.toc-meta-value {
    color: var(--color-text-secondary);
}

/* Edit Link - Minimal */
.toc-edit-link {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.375rem 0.5rem;
    font-size: var(--text-xs);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-tertiary);
    text-decoration: none;
    background: transparent;
    border: 1px solid var(--color-border-light);
    border-radius: var(--border-radius-small);
    transition: all var(--transition-fast);
}

.toc-edit-link:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
}

.toc-edit-link svg {
    flex-shrink: 0;
    opacity: 0.7;
}

/* ============================================================================
   Mobile & Responsive
   ============================================================================ */

@media (max-width: 1023px) {
    .page-sidebar {
        margin-top: 2rem;
    }

    .toc-sidebar {
        max-width: 100%;
    }

    /* Simplify on mobile */
    .toc-settings-btn {
        display: none;
    }
}

/* ============================================================================
   Print Styles
   ============================================================================ */

@media print {
    .page-sidebar {
        display: none;
    }

    .page-layout.page-with-toc {
        display: block;
    }
}

/* ============================================================================
   Ensure Main Article Takes Proper Space
   ============================================================================ */

.page-layout.page-with-toc .page,
.page-layout.page-with-toc .post {
    min-width: 0; /* Prevent grid blowout */
    max-width: none; /* Remove prose width constraint in grid layout */
    margin: 0; /* Remove auto centering in grid */
}

/* Scrollbar styling for TOC on desktop */
@media (min-width: 1024px) {
    .page-sidebar::-webkit-scrollbar {
        width: 6px;
    }

    .page-sidebar::-webkit-scrollbar-track {
        background: transparent;
    }

    .page-sidebar::-webkit-scrollbar-thumb {
        background: var(--color-border);
        border-radius: 3px;
    }

    .page-sidebar::-webkit-scrollbar-thumb:hover {
        background: var(--color-text-tertiary);
    }
}
