/**
 * 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: var(--space-8);
    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: var(--space-8);
        max-height: calc(100vh - 4rem);
        overflow-y: auto;
    }
}

.toc-sidebar {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    height: 100%;
    overflow-x: hidden;
    min-width: 0;
    position: relative;
    /* Breathing room from parent's scrollbar (docs-toc overflow-y: auto) */
    padding-inline-end: 0.5rem;
}

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

.toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    margin-block-end: 0.5rem;
    position: relative;
}

.toc-title {
    margin: 0;
    font-size: var(--text-sm);
    font-weight: var(--weight-medium);
    color: var(--color-text-tertiary);
    flex: 1;
}

/* Toggle all button - inline with graph, matches graph styling */
.toc-toggle-all {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    margin-left: auto;
    margin-bottom: var(--space-3);
    padding: 0;
    background: var(--color-bg-elevated, var(--color-bg-secondary));
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md, var(--border-radius-small));
    color: var(--color-text-tertiary);
    cursor: pointer;
    transition: all var(--transition-fast);
    touch-action: manipulation;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
}

.toc-toggle-all:hover {
    color: var(--color-primary);
    border-color: var(--color-primary);
    background: var(--color-bg-tertiary, var(--color-bg-secondary));
    transform: scale(1.05);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.toc-toggle-all:active {
    transform: scale(0.98);
}

.toc-toggle-all svg {
    flex-shrink: 0;
}

/* Show expand icon by default, collapse when expanded */
.toc-toggle-icon--collapse {
    display: none;
}

.toc-toggle-all[aria-expanded="true"] .toc-toggle-icon--expand {
    display: none;
}

.toc-toggle-all[aria-expanded="true"] .toc-toggle-icon--collapse {
    display: block;
}

/* Legacy settings button - keep for backwards compatibility */
.toc-settings-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    max-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);
    touch-action: manipulation;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    overflow: hidden;
}

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

/* Settings dropdown menu (legacy) */
.toc-settings-menu {
    position: absolute;
    inset-block-start: 100%;
    inset-inline-end: 0;
    margin-block-start: 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-inline-size: 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);
    touch-action: manipulation;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* ============================================================================
   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.5;
    transition: height 0.1s linear, opacity 0.2s ease;
    border-radius: 1px;
}

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

/* ============================================================================
   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;
    padding-inline-end: 0.75rem; /* Extra padding to prevent shadow clipping on badges */
}

.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.25rem;
}

.toc-item {
    margin: 0;
}

/* ============================================================================
   TOC Group (H2 Sections) - Native <details>/<summary>
   Uses native HTML disclosure widget for expand/collapse (no JS required)
   ============================================================================ */

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

/* Summary acts as the toggle trigger */
summary.toc-group-header {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0;
    padding-inline-end: 0.25rem; /* Add small padding to prevent shadow clipping */
    background: transparent;
    min-width: 0;
    cursor: pointer;
    list-style: none; /* Remove default disclosure marker */
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Remove default marker in all browsers */
summary.toc-group-header::-webkit-details-marker {
    display: none;
}

summary.toc-group-header::marker {
    display: none;
}

/* Focus visible for keyboard users */
summary.toc-group-header:focus-visible {
    outline: 2px solid var(--color-primary);
    outline-offset: 2px;
    border-radius: var(--border-radius-small);
}

/* Count badge (was button, now just a span) */
.toc-count-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    padding-inline-end: 0.125rem; /* Small padding to prevent shadow clipping */
    transition: transform var(--transition-smooth);
    flex-shrink: 0;
    /* Ensure shadows aren't clipped */
    margin-inline-end: 0.125rem;
}

summary.toc-group-header:hover .toc-count-badge {
    transform: translate3d(0, 0, 0) scale(1.1);
}

summary.toc-group-header:hover .toc-count {
    opacity: 1;
    background: var(--color-primary-light);
    color: var(--color-primary);
    border-color: var(--color-primary);
    /* Subtle neumorphic hover - standardized */
    box-shadow: var(--neumorphic-subtle);
}

summary.toc-group-header:active .toc-count {
    /* Subtle neumorphic pressed state - standardized */
    box-shadow: var(--neumorphic-subtle-pressed);
    transform: translate3d(0, 0, 0) scale(0.95);
}

/* Visual feedback for expanded state (native [open] attribute) */
details.toc-group[open] .toc-count {
    opacity: 1;
    background: var(--color-primary-light);
    color: var(--color-primary);
    font-weight: var(--weight-semibold);
    /* Subtle neumorphic shadow when expanded - standardized */
    box-shadow: var(--neumorphic-subtle);
}

.toc-link-h2 {
    flex: 1;
    font-weight: var(--weight-medium);
    font-size: var(--text-xs);
    color: var(--color-text-tertiary);
    min-inline-size: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

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

.toc-count {
    display: flex;
    align-items: center;
    justify-content: center;
    min-inline-size: 20px;
    block-size: 20px;
    padding-inline: 0.375rem;
    font-size: var(--text-xxs); /* 11px - smallest defined size */
    font-weight: var(--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), box-shadow 0.2s ease;
    /* Subtle neumorphic shadow - very light for small badge */
    box-shadow:
        inset 0.3px 0.3px 0.5px rgba(255, 255, 255, 0.5),
        inset -0.3px -0.3px 0.5px rgba(0, 0, 0, 0.08),
        0.5px 0.5px 1px rgba(0, 0, 0, 0.04);
}

.toc-subitems {
    list-style: none;
    padding-block: 0.25rem;
    padding-inline-start: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    max-height: 500px;
    overflow: hidden;
    transition: max-height var(--transition-smooth), padding var(--transition-smooth), opacity var(--transition-smooth);
    opacity: 1;
    will-change: max-height, padding, opacity;
}

/* Collapsed state (native [open] attribute) */
details.toc-group:not([open]) .toc-subitems {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
    will-change: auto;
}

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

.toc-link {
    display: block;
    padding: 0.2rem 0.5rem;
    font-size: var(--text-xs);
    font-weight: var(--weight-regular, 400);
    line-height: 1.4;
    letter-spacing: 0.01em;
    color: var(--color-text-tertiary);
    text-decoration: none;
    border-inline-start: 1px solid transparent;
    margin-inline-start: -1px;
    border-radius: var(--border-radius-small);
    transition: all var(--transition-fast);
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    /* Truncate long titles (especially those with code) */
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 100%;
}

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

.toc-link.active {
    color: var(--color-primary);
    border-inline-start-color: transparent;
    position: relative;
}

/* Soft glowing accent for active TOC item */
.toc-link.active::after {
    content: '';
    position: absolute;
    left: -1px;
    top: 0;
    bottom: 0;
    width: 1px;
    /* Very soft gradient - almost invisible line, glow does the work */
    background: linear-gradient(180deg,
        transparent 0%,
        color-mix(in srgb, var(--color-primary) 50%, transparent) 30%,
        color-mix(in srgb, var(--color-primary) 60%, transparent) 50%,
        color-mix(in srgb, var(--color-primary) 50%, transparent) 70%,
        transparent 100%);
    border-radius: 1px;
    animation: toc-accent-glow 8s ease-in-out infinite;
}

@keyframes toc-accent-glow {
    0%, 100% {
        box-shadow: 3px 0 12px 0 color-mix(in srgb, var(--color-primary) 12%, transparent);
    }
    50% {
        box-shadow: 5px 0 18px 0 color-mix(in srgb, var(--color-primary) 22%, transparent);
    }
}

[data-theme="dark"] .toc-link.active::after {
    background: linear-gradient(180deg,
        transparent 0%,
        color-mix(in srgb, var(--color-primary) 60%, transparent) 30%,
        color-mix(in srgb, var(--color-primary) 70%, transparent) 50%,
        color-mix(in srgb, var(--color-primary) 60%, transparent) 70%,
        transparent 100%);
    animation: toc-accent-glow-dark 8s ease-in-out infinite;
}

@keyframes toc-accent-glow-dark {
    0%, 100% {
        box-shadow: 4px 0 15px 0 color-mix(in srgb, var(--color-primary) 18%, transparent);
    }
    50% {
        box-shadow: 6px 0 22px 0 color-mix(in srgb, var(--color-primary) 32%, transparent);
    }
}

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

.toc-level-2 .toc-link {
    padding-left: 0.75rem;
}

.toc-level-3 .toc-link {
    padding-left: 1.25rem;
    opacity: 0.85;
}

.toc-level-4 .toc-link {
    padding-left: 1.75rem;
    opacity: 0.75;
}

.toc-level-5 .toc-link {
    padding-left: 2.25rem;
    opacity: 0.7;
}

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

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

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

[data-toc-mode="compact"] .toc-subitems {
    padding-block: 0.25rem;
    padding-inline-start: 0;
}

[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(--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;
    margin-top: 1.25rem;
    font-size: var(--text-xs);
    /* Editorial: no border, spacing creates separation */
}

.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(--weight-medium);
    color: var(--color-text-tertiary);
    margin-bottom: 0.125rem;
}

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

.toc-meta-more {
    font-size: var(--text-xs);
    color: var(--color-text-muted);
}

/* 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(--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
   ============================================================================ */

/* Laptop and below: Adjust sidebar layout */
@media (max-width: 1024px) {
    .page-sidebar {
        margin-top: var(--space-8);
    }

    .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);
    }
}

/* ============================================================================
   Dark Mode - TOC Count Badge
   ============================================================================ */

[data-theme="dark"] .toc-count {
    /* Subtle neumorphic shadow for dark mode */
    box-shadow:
        inset 0.3px 0.3px 0.5px rgba(255, 255, 255, 0.1),
        inset -0.3px -0.3px 0.5px rgba(0, 0, 0, 0.3),
        0.5px 0.5px 1px rgba(0, 0, 0, 0.2);
}

[data-theme="dark"] summary.toc-group-header:hover .toc-count {
    /* Subtle neumorphic hover for dark mode */
    box-shadow:
        inset 0.3px 0.3px 0.8px rgba(255, 255, 255, 0.15),
        inset -0.3px -0.3px 0.8px rgba(0, 0, 0, 0.4),
        1px 1px 2px rgba(0, 0, 0, 0.25),
        -0.3px -0.3px 1px rgba(255, 255, 255, 0.08);
}

[data-theme="dark"] summary.toc-group-header:active .toc-count {
    /* Subtle neumorphic pressed state for dark mode */
    box-shadow:
        inset 0.5px 0.5px 1px rgba(0, 0, 0, 0.4),
        inset -0.3px -0.3px 0.5px rgba(255, 255, 255, 0.1),
        0.3px 0.3px 0.5px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] details.toc-group[open] .toc-count {
    /* Subtle neumorphic shadow when expanded - dark mode */
    box-shadow:
        inset 0.3px 0.3px 0.8px rgba(255, 255, 255, 0.15),
        inset -0.3px -0.3px 0.8px rgba(0, 0, 0, 0.4),
        1px 1px 2px rgba(0, 0, 0, 0.25),
        -0.3px -0.3px 1px rgba(255, 255, 255, 0.08);
}
