/* Reader Content Styles */
.chapter-content {
    max-width: 576px;
    margin: 0 auto;
    padding: 0 0;
    position: relative;
    overflow: visible;
    margin-top: 40vh;
    margin-bottom: 70vh;
    font-family: var(--font-family); /* Chapter content uses selected font */
}

/* Font family classes applied to chapter content */
.serif .chapter-content {
    font-family: var(--font-family-serif);
}

.sans-serif .chapter-content {
    font-family: var(--font-family-sans);
}

/* Loading indicator */
.chapter-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 200px;
    font-size: 1.1em;
    color: var(--text-color);
    text-align: center;
}

/* Blur effect for chapter content during loading */
.chapter-content.loading > *:not(.chapter-loading) {
    filter: blur(3px);
    transition: filter 0.3s ease;
    pointer-events: none;
}

.chapter-content:not(.loading) > * {
    filter: none;
    transition: filter 0.3s ease;
}

/* Ensure loading indicator stays crisp */
.chapter-loading {
    filter: none !important;
}

/* Typography */
.chapter-content h1,
.chapter-content h2,
.chapter-content h3,
.chapter-content h4,
.chapter-content h5,
.chapter-content h6 {
    font-family: inherit; /* Inherit from chapter-content */
}

.chapter-content h1 {
    font-size: var(--font-size-h1);
    font-weight: 600;
    color: var(--heading-color);
    margin-bottom: var(--spacing-lg);
    line-height: var(--line-height-tight);
    border-bottom: 2px solid var(--primary-color);
    padding-bottom: var(--spacing-md);
}

.chapter-content h2 {
    font-size: var(--font-size-h2);
    font-weight: 500;
    color: var(--heading-color);
    margin: var(--spacing-xl) 0 var(--spacing-md);
    line-height: var(--line-height-tight);
}

.chapter-content h3 {
    font-size: var(--font-size-h3);
    font-weight: 500;
    color: var(--heading-color);
    margin: var(--spacing-lg) 0 var(--spacing-sm);
    line-height: var(--line-height-tight);
}

.chapter-content h4 {
    font-size: var(--font-size-h4);
    font-weight: 500;
    color: var(--heading-color);
    margin: var(--spacing-md) 0 var(--spacing-xs);
}

.chapter-content h5,
.chapter-content h6 {
    font-size: var(--font-size-h5);
    font-weight: 500;
    color: var(--heading-color);
    margin: var(--spacing-md) 0 var(--spacing-xs);
}

.chapter-content h6 {
    font-size: var(--font-size-h6);
}

/* Paragraphs */
.chapter-content p {
    font-size: inherit; /* Inherit from body which uses var(--font-size) */
    line-height: var(--line-height-normal);
    margin-bottom: var(--spacing-md);
    text-align: justify;
    hyphens: auto;
    position: relative; /* Allow sidenotes to be positioned relative to paragraphs */
}

.chapter-content p:first-of-type {
    font-size: calc(var(--font-size) * 1.125); /* 1.125 = 9/8 golden ratio step */
    font-weight: 400;
    line-height: var(--line-height-normal);
}

/* Lists */
.chapter-content ul,
.chapter-content ol {
    margin: var(--spacing-md) 0;
    padding-left: var(--spacing-lg);
    line-height: var(--line-height-normal);
}

.chapter-content ul {
    list-style-type: none;
}

.chapter-content ul li {
    position: relative;
    margin-bottom: var(--spacing-xs);
    padding-left: var(--spacing-md);
}

.chapter-content ul li::before {
    content: "•";
    color: var(--primary-color);
    font-weight: 600;
    position: absolute;
    left: 0;
    font-size: calc(var(--font-size) * 1.2);
}

.chapter-content ol li {
    margin-bottom: var(--spacing-xs);
    color: var(--text-color);
}

.chapter-content ol li::marker {
    color: var(--primary-color);
    font-weight: 600;
}

/* Nested Lists */
.chapter-content ul ul,
.chapter-content ol ol,
.chapter-content ul ol,
.chapter-content ol ul {
    margin: var(--spacing-xs) 0;
    padding-left: var(--spacing-md);
}

.chapter-content ul ul li::before {
    content: "◦";
    font-size: var(--font-size);
}

/* Blockquotes */
.chapter-content blockquote {
    border-left: 4px solid var(--primary-color);
    background: var(--quote-bg);
    margin: var(--spacing-xl) 0;
    padding: var(--spacing-md) var(--spacing-lg);
    font-style: italic;
    position: relative;
    border-radius: 0 8px 8px 0;
    line-height: var(--line-height-normal);
}

.chapter-content blockquote p {
    margin-bottom: var(--spacing-xs);
}

.chapter-content blockquote p:last-child {
    margin-bottom: 0;
}

/* Code */
.chapter-content code {
    background: var(--code-bg);
    color: var(--code-text);
    padding: calc(var(--spacing-xs) / 3) calc(var(--spacing-xs) / 1.5);
    border-radius: 4px;
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    font-size: calc(var(--font-size) * 0.9);
}

.chapter-content pre {
    background: var(--code-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    padding: var(--spacing-md);
    margin: var(--spacing-xl) 0;
    overflow-x: auto;
    line-height: var(--line-height-normal);
}

.chapter-content pre code {
    background: none;
    padding: 0;
    border-radius: 0;
    color: var(--code-text);
}

/* Images */
.chapter-content img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
}

/* Tables */
.chapter-content table {
    width: 100%;
    border-collapse: collapse;
    margin: var(--spacing-xl) 0;
    background: var(--table-bg);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    font-size: var(--font-size);
}

.chapter-content th,
.chapter-content td {
    padding: var(--spacing-sm) var(--spacing-md);
    text-align: left;
    border-bottom: 1px solid var(--border-color);
    line-height: var(--line-height-normal);
}

.chapter-content th {
    background: var(--table-header-bg);
    font-weight: 500;
    color: var(--heading-color);
}

.chapter-content tr:hover {
    background: var(--table-hover-bg);
}

/* Links */
.chapter-content a {
    color: var(--link-color);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: all 0.2s ease;
}

.chapter-content a:hover {
    color: var(--link-hover-color);
    border-bottom-color: var(--link-hover-color);
}

/* Horizontal Rules */
.chapter-content hr {
    border: none;
    height: 2px;
    background: linear-gradient(to right, transparent, var(--border-color), transparent);
    margin: var(--spacing-xxl) 0;
}

/* Strong and Emphasis */
.chapter-content strong {
    font-weight: 600;
    color: var(--heading-color);
}

.chapter-content em {
    font-style: italic;
    color: var(--emphasis-color);
}

/* Small text */
.chapter-content small {
    font-size: var(--font-size-small);
    line-height: var(--line-height-normal);
}

/* Custom Elements */
.chapter-content .highlight {
    background: var(--highlight-bg);
    padding: calc(var(--spacing-xs) / 3) calc(var(--spacing-xs) / 2);
    border-radius: 3px;
}

/* Footnotes and Sidenotes */
.chapter-content .footnote-ref {
    position: relative;
    display: inline;
    cursor: pointer;
}

.chapter-content .footnote-ref sup {
    color: var(--primary-color);
    font-weight: 500;
    cursor: pointer;
    text-decoration: none;
    font-size: calc(var(--font-size) * 0.75);
}

.chapter-content .sidenote {
    position: absolute;
    left: 100%;
    top: -0.5em;
    width: 200px;
    margin-left: 1em;
    background: var(--quote-bg);
    border-left: 3px solid var(--primary-color);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: 0 6px 6px 0;
    font-size: calc(var(--font-size) * 0.85);
    line-height: var(--line-height-normal);
    color: var(--text-color);
    opacity: 0;
    visibility: hidden;
    transform: translateX(-10px);
    transition: all 0.3s ease;
    z-index: 100;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    pointer-events: none;
    white-space: normal;
}

.chapter-content .footnote-ref:hover .sidenote {
    opacity: 1;
    visibility: visible;
    transform: translateX(0);
    pointer-events: auto;
}

/* Footnotes Section at End of Chapter */
.footnotes-section {
    margin-top: 4rem;
    padding-top: 2rem;
    border-top: 1px solid var(--border-color);
}

.footnotes-section h3 {
    font-size: calc(var(--font-size) * 1.1);
    color: var(--secondary-color);
    margin-bottom: 1rem;
    font-weight: 500;
}

.footnotes-list {
    font-size: calc(var(--font-size) * 0.85);
    line-height: var(--line-height-normal);
    color: var(--secondary-color);
}

.footnotes-list li {
    margin-bottom: 0.75rem;
    list-style-type: decimal;
}

.footnotes-list li:target {
    background-color: var(--highlight-bg, rgba(255, 255, 0, 0.3));
    padding: 0.25rem;
    border-radius: 3px;
    margin: -0.25rem;
}

.footnote-backlink {
    color: var(--primary-color);
    text-decoration: none;
    margin-left: 0.5rem;
    font-weight: 500;
}

.footnote-backlink:hover {
    text-decoration: underline;
}

.chapter-content .chapter-number {
    color: var(--primary-color);
    font-weight: 500;
    font-size: var(--font-size-small);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: var(--spacing-xs);
    display: block;
    line-height: var(--line-height-normal);
}

/* Reading Progress */
.reading-progress {
    position: fixed;
    top: 0px;
    left: 0;
    width: 100%;
    height: 3px;
    background: var(--progress-bg);
    z-index: 9999;
    transition: top 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

.reading-progress.header-hidden {
    top: 0;
}

.reading-progress-bar {
    height: 100%;
    background: var(--primary-color);
    width: 0%;
    transition: width 0.1s linear;
    will-change: width;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* Larger screens - ensure enough space for sidenotes */
@media (min-width: 1200px) {
    .chapter-content .sidenote {
        width: 240px;
        margin-left: 1.5em;
    }
}

/* Medium screens - adjust sidenote positioning */
@media (max-width: 1199px) and (min-width: 769px) {
    .chapter-content .sidenote {
        width: 180px;
        margin-left: 1em;
    }
}

/* Responsive Typography */
@media (max-width: 768px) {
    /* Scale down base font size by 0.875 (7/8) for mobile */
    .font-small {
        --font-size: 11.67px;    /* 13.33 × 0.875 */
    }
    
    .font-medium {
        --font-size: 14px;       /* 16 × 0.875 */
    }
    
    .font-large {
        --font-size: 16.8px;     /* 19.2 × 0.875 */
    }
    
    .font-xl {
        --font-size: 20.16px;    /* 23.04 × 0.875 */
    }
    
    .font-xxl {
        --font-size: 24.19px;    /* 27.65 × 0.875 */
    }
    
    .chapter-content ul,
    .chapter-content ol {
        padding-left: var(--spacing-md);
    }
    
    .chapter-content blockquote {
        padding: var(--spacing-sm) var(--spacing-md);
        margin: var(--spacing-lg) 0;
    }
    
    .chapter-content pre {
        padding: var(--spacing-sm);
        font-size: calc(var(--font-size) * 0.9);
    }
    
    .chapter-content table {
        font-size: calc(var(--font-size) * 0.9);
    }
    
    .chapter-content th,
    .chapter-content td {
        padding: calc(var(--spacing-xs) * 0.8) var(--spacing-xs);
    }
    
    /* Mobile footnote styles - use hover overlay like desktop */
    .chapter-content .sidenote {
        /* On mobile, position more conservatively to stay in viewport */
        left: 50%;
        transform: translateX(-50%);
        right: auto;
        width: min(280px, 90vw);
        margin-left: 0;
        margin-right: 0;
        top: -0.5em;
    }
    
    /* Add touch support for mobile */
    .chapter-content .footnote-ref:active .sidenote {
        opacity: 1;
        visibility: visible;
        pointer-events: auto;
    }
}
