/* Responsive Table of Contents (TOC) System */
/* Desktop: Right sidebar | Mobile: Sticky top subbar with dropdown */

/* ========================================
   Desktop TOC Sidebar (Right Column)
   ======================================== */

/* Desktop TOC Container */
@media (min-width: 992px) {
    .toc-sidebar {
        position: sticky;
        top: 80px;
        max-height: calc(100vh - 100px);
        overflow-y: auto;
        z-index: 10;
    }

    .toc-sidebar .card {
        background: var(--bg-card, #ffffff);
        border: 1px solid var(--border-color, #e2e8f0);
        border-radius: var(--radius-lg, 0.75rem);
        box-shadow: var(--shadow-sm);
        transition: box-shadow 0.3s ease;
    }

    .toc-sidebar .card:hover {
        box-shadow: var(--shadow-md);
    }

    .toc-sidebar .card-title {
        color: var(--primary, #5b21b6);
        font-size: 1.1rem;
        font-weight: 600;
        margin-bottom: 1rem;
        padding-bottom: 0.75rem;
        border-bottom: 2px solid var(--border-color, #e2e8f0);
    }

    /* TOC Navigation Styles */
    .toc-sidebar nav.toc {
        padding: 0;
    }

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

    .toc-sidebar .toc-item {
        margin-bottom: 0.5rem;
        position: relative;
    }

    .toc-sidebar .toc-item a {
        display: block;
        padding: 0.5rem 1rem;
        color: var(--text-secondary, #475569);
        text-decoration: none;
        border-radius: var(--radius-md, 0.5rem);
        transition: all 0.3s ease;
        font-size: 0.95rem;
        position: relative;
        overflow: hidden;
    }

    /* Level Indentation */
    .toc-sidebar .toc-level-2 > a {
        padding-right: 1.5rem;
    }

    .toc-sidebar .toc-level-3 > a {
        padding-right: 2.5rem;
        font-size: 0.9rem;
    }

    .toc-sidebar .toc-level-4 > a {
        padding-right: 3.5rem;
        font-size: 0.85rem;
    }

    /* Hover Effects */
    .toc-sidebar .toc-item a:hover {
        background-color: var(--bg-hover, #f1f5f9);
        color: var(--primary, #5b21b6);
        transform: translateX(-3px);
    }

    /* Active Item Indicator */
    .toc-sidebar .toc-item a.active {
        background-color: rgba(91, 33, 182, 0.1);
        color: var(--primary, #5b21b6);
        font-weight: 600;
    }

    .toc-sidebar .toc-item a.active::before {
        content: "";
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 3px;
        background: var(--primary, #5b21b6);
    }

    /* Nested Lists */
    .toc-sidebar .toc-list ul {
        list-style: none;
        padding-right: 1rem;
        margin-top: 0.25rem;
    }

    /* Custom Scrollbar for TOC */
    .toc-sidebar::-webkit-scrollbar {
        width: 6px;
    }

    .toc-sidebar::-webkit-scrollbar-track {
        background: var(--bg-surface, #f8fafc);
        border-radius: 3px;
    }

    .toc-sidebar::-webkit-scrollbar-thumb {
        background: var(--border-color, #e2e8f0);
        border-radius: 3px;
    }

    .toc-sidebar::-webkit-scrollbar-thumb:hover {
        background: var(--text-muted, #64748b);
    }
}

/* ========================================
   Mobile TOC Sticky Header with Dropdown
   ======================================== */

/* Mobile TOC Container */
@media (max-width: 991px) {
    /* Hide desktop TOC */
    .toc-sidebar {
        display: none !important;
    }

    /* Mobile TOC Sticky Bar */
    .mobile-toc-bar {
        position: fixed;
        top: 0; /* Dynamic positioning handled by JavaScript */
        left: 0;
        right: 0;
        background: var(--bg-card, #ffffff);
        border-bottom: 1px solid var(--border-color, #e2e8f0);
        box-shadow: var(--shadow-sm);
        z-index: 1020;
        transition: top 0.3s ease;
        height: 50px;
    }

    /* Dark theme support */
    [data-theme="dark"] .mobile-toc-bar {
        background: var(--bg-card, #1e293b);
        border-bottom-color: var(--border-color, #334155);
    }

    /* TOC Toggle Button */
    .mobile-toc-toggle {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        height: 50px;
        padding: 0 1rem;
        background: none;
        border: none;
        color: var(--text-primary, #0f172a);
        text-align: right;
        cursor: pointer;
        transition: background-color 0.3s ease;
    }

    .mobile-toc-toggle:hover {
        background-color: var(--bg-hover, #f1f5f9);
    }

    /* Current Section Display */
    .mobile-toc-current {
        display: flex;
        align-items: center;
        flex-grow: 1;
        overflow: hidden;
    }

    .mobile-toc-current-label {
        color: var(--text-muted, #64748b);
        font-size: 0.75rem;
        margin-left: 0.5rem;
        white-space: nowrap;
    }

    .mobile-toc-current-title {
        color: var(--text-primary, #0f172a);
        font-size: 0.9rem;
        font-weight: 500;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        margin-right: 0.5rem;
    }

    /* Toggle Icon */
    .mobile-toc-icon {
        display: flex;
        align-items: center;
        margin-right: 0.5rem;
    }

    .mobile-toc-icon svg,
    .mobile-toc-icon i {
        width: 20px;
        height: 20px;
        transition: transform 0.3s ease;
    }

    .mobile-toc-toggle[aria-expanded="true"] .mobile-toc-icon svg,
    .mobile-toc-toggle[aria-expanded="true"] .mobile-toc-icon i {
        transform: rotate(180deg);
    }

    /* Dropdown Container */
    .mobile-toc-dropdown {
        position: fixed;
        top: 50px; /* Dynamic positioning handled by JavaScript */
        left: 0;
        right: 0;
        bottom: 0;
        background: var(--bg-card, #ffffff);
        overflow-y: auto;
        transform: translateY(-100%);
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;
        z-index: 1019;
    }

    .mobile-toc-dropdown.show {
        transform: translateY(0);
        opacity: 1;
        visibility: visible;
    }

    /* Dropdown Overlay */
    .mobile-toc-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: var(--bg-overlay, rgba(15, 23, 42, 0.5));
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s ease;
        z-index: 1018;
    }

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

    /* Dropdown Content */
    .mobile-toc-content {
        padding: 1rem;
        max-height: 100%;
        overflow-y: auto;
    }

    .mobile-toc-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0.75rem 1rem;
        border-bottom: 1px solid var(--border-color, #e2e8f0);
        margin-bottom: 1rem;
    }

    .mobile-toc-header h5 {
        color: var(--primary, #5b21b6);
        font-size: 1rem;
        font-weight: 600;
        margin: 0;
    }

    .mobile-toc-close {
        background: none;
        border: none;
        color: var(--text-muted, #64748b);
        font-size: 1.25rem;
        cursor: pointer;
        padding: 0.25rem;
        line-height: 1;
    }

    /* Mobile TOC List */
    .mobile-toc-dropdown .toc-list {
        list-style: none;
        padding: 0;
        margin: 0;
    }

    .mobile-toc-dropdown .toc-item {
        margin-bottom: 0.25rem;
    }

    .mobile-toc-dropdown .toc-item a {
        display: block;
        padding: 0.75rem 1rem;
        color: var(--text-secondary, #475569);
        text-decoration: none;
        border-radius: var(--radius-md, 0.5rem);
        transition: all 0.2s ease;
        font-size: 0.95rem;
    }

    /* Mobile Level Indentation */
    .mobile-toc-dropdown .toc-level-2 > a {
        padding-right: 1.5rem;
    }

    .mobile-toc-dropdown .toc-level-3 > a {
        padding-right: 2.5rem;
        font-size: 0.9rem;
    }

    .mobile-toc-dropdown .toc-level-4 > a {
        padding-right: 3.5rem;
        font-size: 0.85rem;
    }

    /* Mobile Active State */
    .mobile-toc-dropdown .toc-item a:active {
        background-color: var(--bg-active, #e2e8f0);
    }

    .mobile-toc-dropdown .toc-item a.active {
        background-color: rgba(91, 33, 182, 0.1);
        color: var(--primary, #5b21b6);
        font-weight: 600;
    }

    /* Nested Lists in Mobile */
    .mobile-toc-dropdown .toc-list ul {
        list-style: none;
        padding-right: 1rem;
        margin-top: 0.25rem;
    }

    /* Adjust main content padding - handled dynamically by JS */
    .single-post {
        padding-top: 0; /* Dynamic padding handled by JavaScript */
    }
}

/* ========================================
   Tablet Specific Adjustments
   ======================================== */

@media (min-width: 768px) and (max-width: 991px) {
    .mobile-toc-dropdown {
        left: auto;
        right: 0;
        width: 400px;
        box-shadow: -2px 0 10px rgba(0, 0, 0, 0.1);
    }
}

/* ========================================
   Progress Indicator (Optional)
   ======================================== */

.toc-progress {
    position: absolute;
    bottom: 0;
    left: 0;
    height: 2px;
    background: var(--primary, #5b21b6);
    transition: width 0.3s ease;
    width: 0%;
}

/* ========================================
   Smooth Scrolling for TOC Links
   ======================================== */

html {
    scroll-behavior: smooth;
    scroll-padding-top: var(
        --scroll-padding,
        120px
    ); /* Dynamic based on visible headers */
}

@media (max-width: 991px) {
    html {
        scroll-padding-top: var(--scroll-padding-mobile, 100px);
    }
}

/* ========================================
   Animation Classes
   ======================================== */

@keyframes slideDown {
    from {
        transform: translateY(-10px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

.toc-animate-in {
    animation: slideDown 0.3s ease forwards;
}

.toc-fade-in {
    animation: fadeIn 0.3s ease forwards;
}

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

@media print {
    .mobile-toc-bar,
    .mobile-toc-dropdown,
    .mobile-toc-overlay,
    .toc-sidebar {
        display: none !important;
    }
}

/* ========================================
   RTL Support for Persian/Farsi
   ======================================== */

[dir="rtl"] .toc-sidebar .toc-item a.active::before {
    right: auto;
    left: 0;
}

[dir="rtl"] .toc-sidebar .toc-item a:hover {
    transform: translateX(3px);
}

[dir="rtl"] .toc-sidebar .toc-level-2 > a {
    padding-right: 1rem;
    padding-left: 1.5rem;
}

[dir="rtl"] .toc-sidebar .toc-level-3 > a {
    padding-right: 1rem;
    padding-left: 2.5rem;
}

[dir="rtl"] .toc-sidebar .toc-level-4 > a {
    padding-right: 1rem;
    padding-left: 3.5rem;
}

[dir="rtl"] .mobile-toc-dropdown .toc-level-2 > a {
    padding-right: 1rem;
    padding-left: 1.5rem;
}

[dir="rtl"] .mobile-toc-dropdown .toc-level-3 > a {
    padding-right: 1rem;
    padding-left: 2.5rem;
}

[dir="rtl"] .mobile-toc-dropdown .toc-level-4 > a {
    padding-right: 1rem;
    padding-left: 3.5rem;
}

[dir="rtl"] .mobile-toc-current-label {
    margin-left: 0;
    margin-right: 0.5rem;
}

[dir="rtl"] .mobile-toc-current-title {
    margin-right: 0;
    margin-left: 0.5rem;
}
