/* Move @import to the top */
@import url("https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,600,600i,700,700i&display=swap");

/* Clean body styling - base mobile-first approach */
body {
    max-width: 50em !important;
    margin: 0 auto !important;
    font-family: 'Source Sans Pro', 'Lato', Verdana, Helvetica, sans-serif !important;
    font-weight: 400 !important;
    -webkit-font-smoothing: antialiased !important;
    line-height: 1.6 !important;
    padding: 0 20px !important;
    /* No left margin by default - mobile first */
}

/* Desktop: Add left margin for TOC */
@media screen and (min-width: 992px) {
    body {
        margin-left: 280px !important; /* Space for left TOC */
    }
}

/* Responsive font sizing */
@media screen and (min-width: 992px) {
    body { font-size: 16px !important; }
    h1 { font-size: 32px !important; font-weight: 300 !important; }
    h2 { font-weight: 600 !important; font-size: 28px !important; }
    h3 { font-weight: 600 !important; font-size: 24px !important; }
    h4 { font-weight: 600 !important; font-size: 20px !important; }
    h5 {font-weight: 600 !important; font-size: 18px !important;}
}

/* Enhanced mobile math overflow fixes */
@media screen and (max-width: 991px) {
    body { 
        font-size: 17px !important; 
        padding: 0 15px !important;
        /* margin-left is auto by default now */
        /* Prevent page-level horizontal scroll */
        overflow-x: hidden !important;
    }
    
    /* Ensure all containers respect viewport width */
    html {
        overflow-x: hidden !important;
    }
    
    h1 { font-size: 32px !important; font-weight: 300 !important; }
    h2 { font-weight: 300 !important; }
    h3 { font-weight: 300 !important; }
    
    /* Display math with proper containment */
    .math.display {
        overflow-x: auto !important;
        overflow-y: visible !important;
        padding: 5px 0 !important;
        width: 100% !important;
        /* Force it to be a containing block */
        display: block !important;
        box-sizing: border-box !important;
    }
    
    .math.display .MathJax {
        font-size: 0.9em !important;
        /* Ensure MathJax doesn't break out */
        display: block !important;
        white-space: nowrap !important;
    }
    
    /* Keep inline math as is - no overflow changes */
    .math.inline .MathJax {
        font-size: 1.1em !important;
    }
    
    .math.inline mjx-frac {
        font-size: 1.1em !important;
    }
    
    /* Ensure paragraphs don't expand beyond viewport */
    p {
        max-width: 100% !important;
        box-sizing: border-box !important;
    }
}

/* Even more aggressive for very small screens */
@media screen and (max-width: 480px) {
    .math.display .MathJax {
        font-size: 0.8em !important;
    }
    
    .math.inline .MathJax {
        font-size: 1.05em !important;
    }
    
    body {
        padding: 0 10px !important;
        /* margin-left is auto by default now */
    }
}

/* Math styling */
.math.inline .MathJax {
    font-size: 1.3em !important;
}
.math.display .MathJax {
    font-size: 1.15em !important;
}
.math.inline mjx-frac {
    font-size: 1.3em !important;
}

/* Code block styling */
.sourceCode {
    background-color: #f8f8f8 !important;
    border: 1px solid #ddd !important;
    border-radius: 5px !important;
    padding: 15px !important;
    overflow-x: auto !important;
    margin: 20px 0 !important;
}
.sourceCode pre {
    margin: 0 !important;
    background: none !important;
    border: none !important;
    padding: 0 !important;
}
.sourceCode code {
    background: none !important;
    padding: 0 !important;
    border: none !important;
    font-family: 'Monaco', 'Consolas', 'Courier New', monospace !important;
    font-size: 0.9em !important;
    line-height: 1.4 !important;
}

/* Inline code styling */
code {
    background-color: rgba(175, 184, 193, 0.2) !important;
    padding: 0.2em 0.4em !important;
    margin: 0 !important;
    font-size: 85% !important;
    border-radius: 6px !important;
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace !important;
}

img {
    display: block;
    margin: 15px auto;
    max-width: 65%;
    height: auto;
    width: auto;
}

/* ============================================
   LEFT MARGIN TABLE OF CONTENTS (TOC) STYLES
   ============================================ */

/* Remove any existing TOC elements that might conflict */
div[id^="toc-"]:not(#toc-container) {
    display: none !important;
}

/* Main TOC container - positioned in left margin */
#toc-container {
    position: fixed !important;
    top: 20px !important;
    left: 20px !important;
    width: 240px !important;
    max-height: calc(100vh - 40px) !important;
    overflow-y: auto !important;
    z-index: 100 !important;
    font-family: 'Source Sans Pro', 'Lato', Verdana, Helvetica, sans-serif !important;
    padding: 20px 0 !important;
    background: none !important;
    border: none !important;
    box-shadow: none !important;
}

/* Ensure only one TOC container exists */
#toc-container ~ #toc-container {
    display: none !important;
}

/* TOC list styling - minimal like HackMD */
#toc-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

#toc-list li {
    margin-bottom: 2px;
}

#toc-list a {
    text-decoration: none;
    color: #888;
    font-size: 13px;
    line-height: 1.4;
    display: block;
    padding: 2px 0;
    transition: color 0.2s ease;
    border-left: 2px solid transparent;
    padding-left: 8px;
}

#toc-list a:hover {
    color: #1772d0;
}

#toc-list a.active {
    color: #1772d0;
    border-left-color: #1772d0;
    font-weight: 500;
}

/* Indent nested headings - subtle indentation */
#toc-list .toc-h1 { padding-left: 8px; }
#toc-list .toc-h2 { padding-left: 8px; }
#toc-list .toc-h3 { padding-left: 16px; }
#toc-list .toc-h4 { padding-left: 24px; }
#toc-list .toc-h5 { padding-left: 32px; }
#toc-list .toc-h6 { padding-left: 40px; }

/* Active state maintains indentation */
#toc-list .toc-h1.active { padding-left: 6px; }
#toc-list .toc-h2.active { padding-left: 6px; }
#toc-list .toc-h3.active { padding-left: 14px; }
#toc-list .toc-h4.active { padding-left: 22px; }
#toc-list .toc-h5.active { padding-left: 30px; }
#toc-list .toc-h6.active { padding-left: 38px; }

/* Custom scrollbar for TOC */
#toc-container::-webkit-scrollbar {
    width: 4px;
}

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

#toc-container::-webkit-scrollbar-thumb {
    background: #ddd;
    border-radius: 2px;
}

#toc-container::-webkit-scrollbar-thumb:hover {
    background: #bbb;
}

/* ============================================
   MOBILE RESPONSIVE TOC STYLES
   ============================================ */

@media screen and (max-width: 991px) {
    /* Hide TOC on mobile/tablet */
    #toc-container {
        display: none;
    }
    
    /* Body margin is already auto by default */
}

/* Tablet adjustments */
@media screen and (min-width: 992px) and (max-width: 1200px) {
    #toc-container {
        width: 200px;
        left: 15px;
    }
    
    body {
        margin-left: 240px !important;
    }
}

/* Large desktop adjustments */
@media screen and (min-width: 1400px) {
    #toc-container {
        width: 260px;
        left: 30px;
    }
    
    body {
        margin-left: 320px !important;
    }
}

/* Ensure headings have proper spacing for TOC navigation */
h1, h2, h3, h4, h5, h6 {
    scroll-margin-top: 20px;
}

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