/* ================================================
   Chapter Page Styles
   Premium, dynamic reading experience
   ================================================ */

:root {
    /* Extended color palette */
    --gradient-start: #084887;
    --gradient-end: #1a5fb4;
    --code-bg: #1e1e2e;
    --code-text: #cdd6f4;
    --sidebar-bg: #f8fafc;
    --sidebar-active: rgba(8, 72, 135, 0.1);
    --sidebar-border: #e2e8f0;
    --section-divider: linear-gradient(90deg, transparent, var(--border-color), transparent);

    /* Shadows */
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
    --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.07);
    --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
    --shadow-xl: 0 20px 25px rgba(0, 0, 0, 0.15);
}

[data-theme="dark"] {
    --sidebar-bg: #0f172a;
    --sidebar-active: rgba(56, 189, 248, 0.1);
    --sidebar-border: #1e293b;
    --code-bg: #020617;
    --code-text: #e2e8f0;
    --section-divider: linear-gradient(90deg, transparent, var(--border-color), transparent);

    /* Search and Inputs */
    --input-bg: #1e293b;
    --input-border: #334155;
}

/* ================================================
   Layout Structure
   ================================================ */

.chapter-page {
    --sidebar-width: 280px;
}

.chapter-layout {
    display: flex;
    min-height: calc(100vh - 60px);
}

/* ================================================
   Sidebar Navigation
   ================================================ */

.sidebar {
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    position: sticky;
    top: 60px;
    height: calc(100vh - 60px);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
}

/* Sidebar Main Navigation (Vertical Links) */
.sidebar-main-nav {
    padding: var(--spacing-md);
    border-bottom: 1px solid var(--sidebar-border);
    background: rgba(8, 72, 135, 0.03);
}

.sidebar-main-nav ul {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs, 4px);
}

.sidebar-main-nav li a {
    display: block;
    padding: 10px var(--spacing-md);
    color: var(--secondary-color);
    text-decoration: none;
    border-radius: var(--radius-md);
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s ease;
}

.sidebar-main-nav li a:hover {
    background: var(--sidebar-active);
    color: var(--primary-color);
}

.sidebar-header {
    padding: var(--spacing-lg);
    background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
    color: white;
}

.sidebar-header h2 {
    font-size: 1.25rem;
    margin-bottom: 4px;
    font-weight: 700;
}

.sidebar-subtitle {
    font-size: 0.875rem;
    opacity: 0.9;
}

.sidebar-nav {
    flex: 1;
    padding: var(--spacing-md) 0;
    overflow-y: auto;
}

.section-list {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-direction: column;
}

.section-item {
    position: relative;
}

.section-item a {
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: nowrap !important;
    align-items: center;
    gap: 10px;
    padding: 8px 16px;
    color: var(--secondary-color);
    text-decoration: none;
    transition: all 0.2s ease;
    border-left: 3px solid transparent;
}

.section-item a:hover {
    background: var(--sidebar-active);
    color: var(--primary-color);
}

.section-item.active a {
    background: var(--sidebar-active);
    color: var(--primary-color);
    border-left-color: var(--primary-color);
    font-weight: 600;
}

/* Viewed State - Replace number with checkmark */
.section-item.viewed:not(.active) .section-number {
    background: #084887;
    /* Primary color or Green #22c55e */
    color: white;
    font-size: 0;
    /* Hide the number */
    position: relative;
}

.section-item.viewed:not(.active) .section-number::after {
    content: '✓';
    font-size: 1rem;
    font-weight: 700;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.section-item .section-number {
    width: 24px;
    height: 24px;
    min-width: 24px;
    flex-shrink: 0;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    background: var(--border-color);
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
    order: 0;
    text-align: center !important;
    margin-bottom: 0 !important;
}

.section-item.active .section-number {
    background: var(--primary-color);
    color: white;
}

.section-item .section-title {
    display: inline !important;
    font-size: 0.875rem !important;
    line-height: 1.3;
    order: 1;
    text-align: left !important;
    margin-bottom: 0 !important;
}

.sidebar-footer {
    padding: var(--spacing-md);
    border-top: 1px solid var(--sidebar-border);
}

.next-chapter-btn {
    display: block;
    padding: var(--spacing-sm) var(--spacing-md);
    background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
    color: white !important;
    border-radius: var(--radius-md);
    text-align: center;
    font-weight: 600;
    font-size: 0.875rem;
    transition: all 0.3s ease;
    box-shadow: var(--shadow-md);
}

.next-chapter-btn:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-lg);
}

/* Sidebar Toggle (Mobile) */
.sidebar-toggle {
    display: none;
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 56px;
    height: 56px;
    background: var(--primary-color);
    border: none;
    border-radius: 50%;
    cursor: pointer;
    z-index: 200;
    box-shadow: var(--shadow-xl);
    transition: all 0.3s ease;
}

.sidebar-toggle span {
    display: block;
    width: 24px;
    height: 2px;
    background: white;
    margin: 5px auto;
    transition: all 0.3s ease;
}

.sidebar-toggle:hover {
    transform: scale(1.1);
}

/* ================================================
   Reading Progress Bar
   ================================================ */

.reading-progress {
    position: fixed;
    top: 60px;
    left: var(--sidebar-width);
    right: 0;
    height: 3px;
    background: var(--border-color);
    z-index: 50;
}

.progress-bar {
    height: 100%;
    width: 0%;
    background: linear-gradient(90deg, var(--gradient-start), var(--gradient-end));
    transition: width 0.1s ease;
}

/* ================================================
   Chapter Content
   ================================================ */

.chapter-content {
    flex: 1;
    min-width: 0;
    background: var(--bg-color);
}

.chapter-hero {
    background: linear-gradient(135deg, rgba(8, 72, 135, 0.08) 0%, rgba(255, 253, 247, 1) 100%);
    padding: var(--spacing-xl) var(--spacing-lg);
    border-bottom: 1px solid var(--border-color);
}

.chapter-hero-content {
    max-width: 800px;
    margin: 0 auto;
}

.chapter-label {
    display: inline-block;
    padding: 6px 12px;
    background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
    color: white;
    border-radius: 20px;
    font-size: 0.75rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: var(--spacing-md);
}

.chapter-title {
    font-size: 2.5rem;
    color: var(--accent-color);
    margin-bottom: var(--spacing-md);
    line-height: 1.2;
}

.chapter-description {
    font-size: 1.125rem;
    color: var(--secondary-color);
    line-height: 1.6;
    margin-bottom: var(--spacing-md);
}

.chapter-meta {
    display: flex;
    gap: var(--spacing-lg);
    flex-wrap: wrap;
}

.meta-item {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.9rem;
    color: var(--secondary-color);
}

.meta-item svg {
    opacity: 0.7;
}

/* ================================================
   Content Wrapper
   ================================================ */

.content-wrapper {
    max-width: 900px;
    margin: 0 auto;
    padding: var(--spacing-xl) var(--spacing-lg);
}

.content-article {
    position: relative;
}

.content-section {
    margin-bottom: var(--spacing-xl);
    padding-bottom: var(--spacing-xl);
    border-bottom: 1px solid var(--border-color);
    scroll-margin-top: 80px;
}

.content-section:last-child {
    border-bottom: none;
}

.section-header {
    margin-bottom: var(--spacing-lg);
}

.section-label {
    display: inline-block;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--primary-color);
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 8px;
}

.section-header h2 {
    font-size: 1.75rem;
    color: var(--accent-color);
    line-height: 1.3;
}

/* ================================================
   Markdown Content Styling
   ================================================ */

.markdown-content {
    font-size: 1.05rem;
    line-height: 1.8;
    color: var(--text-color);
}

.markdown-content h1,
.markdown-content h2,
.markdown-content h3,
.markdown-content h4,
.markdown-content h5,
.markdown-content h6 {
    color: var(--accent-color);
    margin-top: 2em;
    margin-bottom: 0.75em;
    line-height: 1.3;
}

.markdown-content h1 {
    font-size: 2rem;
}

.markdown-content h2 {
    font-size: 1.5rem;
}

.markdown-content h3 {
    font-size: 1.25rem;
}

.markdown-content h4 {
    font-size: 1.125rem;
}

.markdown-content p {
    margin-bottom: 1.25em;
}

.markdown-content a {
    color: var(--primary-color);
    text-decoration: underline;
    text-underline-offset: 2px;
}

.markdown-content a:hover {
    color: var(--gradient-end);
}

.markdown-content strong {
    font-weight: 600;
    color: var(--accent-color);
}

.markdown-content em {
    font-style: italic;
}

.markdown-content ul,
.markdown-content ol {
    margin-bottom: 1.25em;
    padding-left: 1.5em;
}

.markdown-content li {
    margin-bottom: 0.5em;
}

.markdown-content blockquote {
    border-left: 4px solid var(--primary-color);
    margin: 1.5em 0;
    padding: 1em 1.5em;
    background: rgba(8, 72, 135, 0.05);
    border-radius: 0 var(--radius-md) var(--radius-md) 0;
    font-style: italic;
}

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

/* Code Blocks */
.markdown-content code {
    font-family: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    font-size: 0.9em;
}

.markdown-content :not(pre)>code {
    background: rgba(8, 72, 135, 0.1);
    color: var(--primary-color);
    padding: 2px 6px;
    border-radius: 4px;
}

.markdown-content pre {
    background: var(--code-bg);
    color: var(--code-text);
    border-radius: var(--radius-md);
    padding: var(--spacing-lg);
    overflow-x: auto;
    margin: 1.5em 0;
    box-shadow: var(--shadow-md);
    position: relative;
}

.markdown-content pre code {
    background: none;
    padding: 0;
    color: inherit;
    font-size: 0.875rem;
    line-height: 1.6;
}

/* Copy Code Button */
.code-block-wrapper {
    position: relative;
}

.copy-code-btn {
    position: absolute;
    top: 8px;
    right: 8px;
    padding: 6px 12px;
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 4px;
    color: var(--code-text);
    font-size: 0.75rem;
    cursor: pointer;
    opacity: 0;
    transition: all 0.2s ease;
}

.code-block-wrapper:hover .copy-code-btn {
    opacity: 1;
}

.copy-code-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.copy-code-btn.copied {
    background: #22c55e;
    color: white;
    border-color: #22c55e;
}

/* Tables */
.markdown-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 1.5em 0;
    font-size: 0.95rem;
    box-shadow: var(--shadow-sm);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.markdown-content th,
.markdown-content td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid var(--border-color);
}

.markdown-content th {
    background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
    color: white;
    font-weight: 600;
}

.markdown-content tr:nth-child(even) {
    background: rgba(8, 72, 135, 0.03);
}

.markdown-content tr:hover {
    background: rgba(8, 72, 135, 0.07);
}

/* Images */
.markdown-content img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    margin: 1.5em 0;
}

/* Horizontal Rule */
.markdown-content hr {
    border: none;
    height: 1px;
    background: var(--section-divider);
    margin: 2em 0;
}

/* Mermaid Diagrams */
.mermaid {
    background: white;
    padding: var(--spacing-lg);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-sm);
    margin: 1.5em 0;
    overflow-x: auto;
}

/* ================================================
   Loading States
   ================================================ */

.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-xl);
    color: var(--secondary-color);
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin-bottom: var(--spacing-md);
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ================================================
   Chapter Navigation Footer
   ================================================ */

.chapter-nav {
    display: flex;
    justify-content: space-between;
    gap: var(--spacing-md);
    margin-top: var(--spacing-xl);
    padding-top: var(--spacing-xl);
    border-top: 1px solid var(--border-color);
}

.nav-link {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: var(--spacing-lg);
    background: white;
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    text-decoration: none;
    transition: all 0.3s ease;
}

.nav-link:hover {
    border-color: var(--primary-color);
    transform: translateY(-4px);
    box-shadow: var(--shadow-lg);
}

.nav-link.prev {
    text-align: left;
}

.nav-link.next {
    text-align: right;
}

.nav-direction {
    font-size: 0.875rem;
    color: var(--secondary-color);
    margin-bottom: 4px;
}

.nav-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--primary-color);
}

/* ================================================
   Breadcrumbs
   ================================================ */
.breadcrumbs {
    margin-bottom: 1rem;
    font-size: 0.9rem;
    color: var(--secondary-color);
}

.breadcrumbs ol {
    list-style: none;
    padding: 0;
    margin: 0;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.breadcrumbs a {
    color: var(--secondary-color);
    text-decoration: none;
    transition: color 0.2s ease;
}

.breadcrumbs a:hover {
    color: var(--primary-color);
    text-decoration: underline;
}

.breadcrumbs li[aria-current="page"] {
    color: var(--primary-color);
    font-weight: 500;
}

/* ================================================
   Responsive Design
   ================================================ */

@media (max-width: 1024px) {
    .sidebar {
        position: fixed;
        left: 0;
        top: 60px;
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
        box-shadow: var(--shadow-xl);
    }

    .sidebar-toggle {
        display: flex;
        flex-direction: column;
        justify-content: center;
    }

    .sidebar-toggle.active span:nth-child(1) {
        transform: rotate(45deg) translate(5px, 5px);
    }

    .sidebar-toggle.active span:nth-child(2) {
        opacity: 0;
    }

    .sidebar-toggle.active span:nth-child(3) {
        transform: rotate(-45deg) translate(7px, -6px);
    }

    .reading-progress {
        left: 0;
    }

    .chapter-title {
        font-size: 2rem;
    }
}

@media (max-width: 768px) {
    .chapter-hero {
        padding: var(--spacing-lg);
    }

    .chapter-title {
        font-size: 1.75rem;
    }

    .chapter-description {
        font-size: 1rem;
    }

    .content-wrapper {
        padding: var(--spacing-lg);
    }

    .chapter-nav {
        flex-direction: column;
    }

    .nav-link.prev {
        order: 2;
    }

    .nav-link.next {
        order: 1;
        text-align: left;
    }

    .markdown-content {
        font-size: 1rem;
    }

    .markdown-content pre {
        padding: var(--spacing-md);
        margin-left: calc(-1 * var(--spacing-lg));
        margin-right: calc(-1 * var(--spacing-lg));
        border-radius: 0;
    }
}

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

@media print {

    .sidebar,
    .sidebar-toggle,
    .reading-progress,
    .chapter-nav,
    header,
    footer {
        display: none !important;
    }

    .chapter-content {
        max-width: 100%;
    }

    .markdown-content pre {
        white-space: pre-wrap;
        word-wrap: break-word;
    }
}

/* ================================================
   Animations
   ================================================ */

.content-section {
    opacity: 0;
    transform: translateX(60px);
    transition: opacity 0.6s ease, transform 0.6s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

.content-section.is-visible {
    opacity: 1;
    transform: translateX(0);
}

/* Smooth scroll indicator */
.scroll-indicator {
    position: fixed;
    bottom: 100px;
    right: 24px;
    width: 44px;
    height: 44px;
    background: white;
    border: 1px solid var(--border-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    opacity: 0;
    transform: translateY(20px);
    transition: all 0.3s ease;
    box-shadow: var(--shadow-md);
    z-index: 50;
}

.scroll-indicator.visible {
    opacity: 1;
    transform: translateY(0);
}

.scroll-indicator:hover {
    background: var(--primary-color);
    color: white;
    border-color: var(--primary-color);
}

/* Continue Button */
.continue-btn-container {
    margin-top: var(--spacing-xl);
    display: flex;
    justify-content: center;
}

.continue-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 16px 32px;
    background: linear-gradient(135deg, var(--gradient-start), var(--gradient-end));
    color: #ffffff !important;
    border: none;
    border-radius: 50px;
    font-size: 1.125rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 4px 12px rgba(8, 72, 135, 0.2);
    text-decoration: none;
}

.continue-btn span {
    color: #ffffff !important;
}

.continue-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 16px rgba(8, 72, 135, 0.3);
    color: #ffffff !important;
}

.continue-btn svg {
    width: 24px;
    height: 24px;
    transition: transform 0.3s ease;
    stroke: #ffffff !important;
}

.continue-btn:hover svg {
    transform: translateX(4px);
}

/* ================================================
   Accessibility - Focus Indicators for Chapter Page
   ================================================ */

/* Sidebar section item focus */
.section-item a:focus-visible {
    background: var(--sidebar-active);
    border-left-color: var(--primary-color);
    outline: none;
    box-shadow: inset 0 0 0 2px var(--primary-color);
}

/* Sidebar toggle focus */
.sidebar-toggle:focus-visible {
    outline: 3px solid white;
    outline-offset: 2px;
    box-shadow: var(--shadow-xl), 0 0 0 6px rgba(8, 72, 135, 0.3);
}

/* Scroll indicator focus */
.scroll-indicator:focus-visible {
    outline: 3px solid var(--primary-color);
    outline-offset: 2px;
}

/* Next/prev navigation link focus */
.nav-link:focus-visible {
    border-color: var(--primary-color);
    outline: 3px solid var(--primary-color);
    outline-offset: 2px;
    transform: translateY(-4px);
}

/* Next chapter button focus */
.next-chapter-btn:focus-visible {
    outline: 2px solid white;
    outline-offset: 2px;
}

/* Continue button focus */
.continue-btn:focus-visible {
    outline: 3px solid var(--primary-color);
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(8, 72, 135, 0.2);
}

/* Code copy button focus */
.copy-code-btn:focus-visible {
    opacity: 1;
    outline: 2px solid var(--code-text);
    outline-offset: 2px;
}