/* Table of Contents Container */
.toc-container {
    position: sticky;
    left: 0;
    top: 100px;
    width: 23%;
    min-width: 350px;
    z-index: 1000;
    transition: all 0.3s ease;
    float: left;
    height: fit-content;
}

/* When footer is visible, position TOC absolutely to stop before footer */
.toc-container.footer-visible {
    position: absolute;
    top: auto;
    bottom: 100px;
    /* Adjust as needed */
}

/* Toggle Button - when closed */
.toc-toggle {
    position: absolute;
    left: 43px;
    top: 60px;
    transform: rotate(-270deg);
    transform-origin: left top;
    background: #008ccc;
    color: white;
    padding: 10px 20px;
    cursor: pointer;
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
    font-family: 'Oswald', sans-serif;
    font-weight: 700;
    font-size: 16px;
    letter-spacing: 1px;
    border: none;
    outline: none;
    z-index: 1001;
    display: flex;
    align-items: center;
    gap: 5px;
}

/* Toggle Button - when TOC is open */
.toc-container:has(.toc-panel.open) .toc-toggle {
    left: 124%;
    top: 60px;
    transform: rotate(-270deg);
}

.toc-toggle-text {
    display: block;
}

/* Chevron rotation states */
#toc-chevron {
    transform: rotate(270deg);
    transition: transform 0.3s ease;
}

/* TOC Panel */
.toc-panel {
    position: absolute;
    left: 0;
    top: 0;
    transform: translateX(-100%);
    width: 0;
    background: white;
    border: 3px solid #008ccc;
    box-shadow: 2px 0 15px rgba(0, 0, 0, 0.1);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
    opacity: 0;
    max-height: 70vh;
    display: flex;
    flex-direction: column;
    padding: 5% 5%;
}

.toc-panel.open {
    transform: translateX(0);
    width: 100%;
    opacity: 1;
}

/* TOC Header */
.toc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 5px 10px 5px;
    border-bottom: 2px solid #008ccc;
    flex-shrink: 0;
}

.toc-header h3 {
    margin: 0;
    font-size: 24;
    font-weight: 700;
    color: #0F1B43;
    letter-spacing: 1px;
}

.toc-close {
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #666;
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: all 0.2s ease;
}

.toc-close:hover {
    background: #e9ecef;
    color: #008ccc;
}

/* TOC Navigation */
.toc-nav {
    flex: 1;
    overflow-y: auto;
    padding: 0;
}

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

.toc-item {
    margin: 0;
    border-bottom: none;
}

/* Add border after H2 items that are followed by H3 items */
.toc-item:not(.toc-item-h3)+.toc-item:not(.toc-item-h3) {
    border-top: 1px solid #e9ecef;
}

/* Add border after H3 items that are followed by H2 items */
.toc-item-h3+.toc-item:not(.toc-item-h3) {
    border-top: 1px solid #e9ecef;
}

.toc-link {
    display: block;
    padding: 5px 20px;
    margin: 5px 0;
    color: #008ccc;
    text-decoration: none;
    font-family: 'Oswald', sans-serif;
    font-size: 18px;
    font-weight: bold;
    line-height: 1.4;
    transition: all 0.2s ease;
    position: relative;
    overflow: hidden;
}

.toc-link:hover {
    background: #e7f3ff;
    color: #008ccc;
}

.toc-link.active {
    background: #e7f3ff;
    color: #008ccc;
}

/* H3 subsection styling */
.toc-link-h3 {
    font-family: 'Oswald', sans-serif;
    font-size: 14px;
    font-weight: 700;
    color: #0F1B43;
    margin: 2px 0;
    white-space: normal;
    overflow: visible;
    text-overflow: initial;
}

.toc-link-h3:hover {
    background: #e7f3ff;
    color: #0F1B43;
}

.toc-link-h3.active {
    background: #e7f3ff;
    color: #0F1B43;
    font-weight: 700;
}

/* Smooth scrolling for the entire page */
html {
    scroll-behavior: smooth;
}

/* Responsive Design */
@media (max-width: 768px) {
    .toc-container {
        position: fixed;
        left: 0;
        top: 0;
        width: 100%;
        min-width: auto;
        z-index: 1000;
        transition: all 0.3s ease;
        float: none;
        height: fit-content;
        padding: 0;
        margin: 0;
        /* Use safe area insets for iPhone notch/home indicator */
        padding-top: env(safe-area-inset-top);
    }

    .toc-container.footer-visible {
        position: fixed;
        /* Keep it fixed even when footer is visible on mobile */
        top: 0;
        bottom: auto;
    }

    .toc-toggle {
        position: fixed;
        left: 21px;
        top: calc(env(safe-area-inset-top, 0px) + 50px);
        transform: rotate(-270deg);
        transform-origin: left top;
        background: #008ccc;
        color: white;
        padding: 1px 15px 1px;
        cursor: pointer;
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        white-space: nowrap;
        font-family: 'Oswald', sans-serif;
        font-weight: 700;
        font-size: 14px;
        letter-spacing: 1px;
        border: none;
        outline: none;
        z-index: 1001;
        display: flex;
        align-items: center;
        gap: 5px;
    }

    .toc-container:has(.toc-panel.open) .toc-toggle {
        left: calc(98%);
        top: calc(env(safe-area-inset-top, 0px) + 50px);
        transform: rotate(-270deg);
    }

    .toc-toggle.hidden {
        display: none;
    }

    .toc-panel {
        position: fixed;
        left: 10px;
        top: calc(env(safe-area-inset-top, 0px) + 0px);
        transform: translateX(-100%);
        width: 0;
        background: white;
        border: 3px solid #008ccc;
        box-shadow: 2px 0 15px rgba(0, 0, 0, 0.1);
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        overflow: hidden;
        opacity: 0;
        max-height: calc(60vh - env(safe-area-inset-top, 0px));
        display: flex;
        flex-direction: column;
        padding: 5% 5%;
    }

    .toc-panel.open {
        transform: translateX(0);
        width: 80%;
        opacity: 1;
    }

    /* Adjust single-child width for mobile */
    .single-child {
        width: 90% !important;
    }
}

/* Animation for smooth transitions */
@keyframes slideIn {
    from {
        transform: translateX(-100%);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes slideOut {
    from {
        transform: translateX(0);
        opacity: 1;
    }

    to {
        transform: translateX(-100%);
        opacity: 0;
    }
}

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

.toc-nav::-webkit-scrollbar-track {
    background: #f1f1f1;
}

.toc-nav::-webkit-scrollbar-thumb {
    background: #008ccc;
    border-radius: 3px;
}

.toc-nav::-webkit-scrollbar-thumb:hover {
    background: #006ba6;
}

/* Ensure single-child transitions smoothly */
.single-child {
    transition: width 0.3s ease, margin-left 0.3s ease;
    padding: 0 5%;
}

@media screen and (max-width: 767px) {
    .single-child {
        padding: 0 7%;
    }
}

/* Apply same padding to about-the-author as single-child */
.about-the-author {
    padding: 0 5%;
}

/* When TOC is open, add margin to prevent overlap with TOC panel */
.toc-panel.open~* .single-child,
body:has(.toc-panel.open) .single-child {
    margin-left: auto;
}

/* When TOC is open, add margin to about-the-author as well */
.toc-panel.open~* .about-the-author,
body:has(.toc-panel.open) .about-the-author {
    margin-left: auto;
}

/* Loading state */
.toc-nav:empty::after {
    content: 'Loading table of contents...';
    display: block;
    padding: 20px;
    text-align: center;
    color: #666;
    font-style: italic;
}

/* No content state */
.toc-nav:empty+.toc-no-content {
    display: block;
    padding: 20px;
    text-align: center;
    color: #666;
}

.toc-no-content {
    display: none;
}

/* Volume buttons padding */
.volume-buttons {
    padding: 0 5%;
}