/* ================================================
   AWS Notes Reader — Complete Stylesheet
   Dark / Light / Reading Mode Support
   ================================================ */

/* ===== CSS Custom Properties (Tokens) ===== */
:root {
    /* Base Typography */
    --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    --font-mono: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
    --font-reading: 'Merriweather', 'Georgia', serif;
    --font-size-base: 17px;

    /* Transitions */
    --transition-fast: 0.18s ease;
    --transition-base: 0.28s ease;
    --transition-slow: 0.4s ease;

    /* Layout */
    --sidebar-width: 320px;
    --topbar-height: 60px;
    --content-max-width: 820px;
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 20px;

    /* Z-index */
    --z-sidebar: 100;
    --z-overlay: 99;
    --z-topbar: 110;
    --z-popup: 120;
    --z-scroll-top: 90;
}

/* ===== Dark Theme (Default) ===== */
[data-theme="dark"] {
    --bg-primary: #0a0e17;
    --bg-secondary: #111827;
    --bg-tertiary: #1a2235;
    --bg-surface: #1e293b;
    --bg-surface-hover: #253347;
    --bg-code: #0d1321;
    --bg-code-inline: #1e293b;

    --text-primary: #e8ecf4;
    --text-secondary: #94a3b8;
    --text-tertiary: #64748b;
    --text-heading: #f1f5f9;
    --text-link: #60a5fa;
    --text-code: #e2e8f0;

    --accent-primary: #f59e0b;
    --accent-primary-dim: rgba(245, 158, 11, 0.15);
    --accent-secondary: #3b82f6;
    --accent-secondary-dim: rgba(59, 130, 246, 0.12);
    --accent-success: #10b981;
    --accent-danger: #ef4444;
    --accent-warning: #f59e0b;

    --border-color: #1e293b;
    --border-subtle: #152033;
    --border-active: #f59e0b;

    --sidebar-bg: #0c1120;
    --sidebar-border: #172032;
    --sidebar-item-hover: #162035;
    --sidebar-item-active: rgba(245, 158, 11, 0.08);
    --sidebar-item-active-border: #f59e0b;

    --topbar-bg: rgba(10, 14, 23, 0.85);
    --topbar-border: #172032;

    --shadow-sm: 0 1px 3px rgba(0,0,0,0.4);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.45);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.5);
    --shadow-glow: 0 0 40px rgba(245, 158, 11, 0.06);

    --scrollbar-track: #111827;
    --scrollbar-thumb: #334155;
    --scrollbar-thumb-hover: #475569;

    --table-header-bg: #1a2235;
    --table-row-alt: #111d2e;
    --table-border: #1e293b;

    --blockquote-bg: rgba(245, 158, 11, 0.06);
    --blockquote-border: #f59e0b;
}

/* ===== Light Theme ===== */
[data-theme="light"] {
    --bg-primary: #f8fafc;
    --bg-secondary: #ffffff;
    --bg-tertiary: #f1f5f9;
    --bg-surface: #ffffff;
    --bg-surface-hover: #f1f5f9;
    --bg-code: #f8f9fc;
    --bg-code-inline: #f1f5f9;

    --text-primary: #1e293b;
    --text-secondary: #475569;
    --text-tertiary: #94a3b8;
    --text-heading: #0f172a;
    --text-link: #2563eb;
    --text-code: #1e293b;

    --accent-primary: #d97706;
    --accent-primary-dim: rgba(217, 119, 6, 0.1);
    --accent-secondary: #2563eb;
    --accent-secondary-dim: rgba(37, 99, 235, 0.08);
    --accent-success: #059669;
    --accent-danger: #dc2626;
    --accent-warning: #d97706;

    --border-color: #e2e8f0;
    --border-subtle: #f1f5f9;
    --border-active: #d97706;

    --sidebar-bg: #ffffff;
    --sidebar-border: #e2e8f0;
    --sidebar-item-hover: #f8fafc;
    --sidebar-item-active: rgba(217, 119, 6, 0.06);
    --sidebar-item-active-border: #d97706;

    --topbar-bg: rgba(255, 255, 255, 0.88);
    --topbar-border: #e2e8f0;

    --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
    --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 32px rgba(0,0,0,0.1);
    --shadow-glow: none;

    --scrollbar-track: #f1f5f9;
    --scrollbar-thumb: #cbd5e1;
    --scrollbar-thumb-hover: #94a3b8;

    --table-header-bg: #f1f5f9;
    --table-row-alt: #f8fafc;
    --table-border: #e2e8f0;

    --blockquote-bg: rgba(217, 119, 6, 0.05);
    --blockquote-border: #d97706;
}

/* ===== Reading Theme ===== */
[data-theme="reading"] {
    --bg-primary: #faf6f0;
    --bg-secondary: #f5efe6;
    --bg-tertiary: #ede5d8;
    --bg-surface: #f5efe6;
    --bg-surface-hover: #ede5d8;
    --bg-code: #efe9df;
    --bg-code-inline: #ede5d8;

    --text-primary: #3d3229;
    --text-secondary: #6b5c4f;
    --text-tertiary: #998a7b;
    --text-heading: #2c2118;
    --text-link: #8b5e34;
    --text-code: #3d3229;

    --accent-primary: #b0722a;
    --accent-primary-dim: rgba(176, 114, 42, 0.12);
    --accent-secondary: #7c6147;
    --accent-secondary-dim: rgba(124, 97, 71, 0.1);
    --accent-success: #5a7a4c;
    --accent-danger: #a54a3a;
    --accent-warning: #b0722a;

    --border-color: #ddd5c8;
    --border-subtle: #e8e0d3;
    --border-active: #b0722a;

    --sidebar-bg: #f0eadf;
    --sidebar-border: #ddd5c8;
    --sidebar-item-hover: #e8e0d3;
    --sidebar-item-active: rgba(176, 114, 42, 0.08);
    --sidebar-item-active-border: #b0722a;

    --topbar-bg: rgba(250, 246, 240, 0.9);
    --topbar-border: #ddd5c8;

    --shadow-sm: 0 1px 3px rgba(60,40,20,0.06);
    --shadow-md: 0 4px 16px rgba(60,40,20,0.08);
    --shadow-lg: 0 8px 32px rgba(60,40,20,0.1);
    --shadow-glow: none;

    --scrollbar-track: #ede5d8;
    --scrollbar-thumb: #ccc2b3;
    --scrollbar-thumb-hover: #b0a594;

    --table-header-bg: #ede5d8;
    --table-row-alt: #f0eadf;
    --table-border: #ddd5c8;

    --blockquote-bg: rgba(176, 114, 42, 0.06);
    --blockquote-border: #b0722a;
}

/* Reading mode uses serif font for body */
[data-theme="reading"] .chapter-body {
    font-family: var(--font-reading);
    line-height: 1.9;
    letter-spacing: 0.01em;
}

[data-theme="reading"] .chapter-body code,
[data-theme="reading"] .chapter-body pre {
    font-family: var(--font-mono);
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    scroll-behavior: smooth;
    font-size: var(--font-size-base);
}

body {
    font-family: var(--font-sans);
    background: var(--bg-primary);
    color: var(--text-primary);
    transition: background var(--transition-base), color var(--transition-base);
    overflow-x: hidden;
    min-height: 100vh;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb {
    background: var(--scrollbar-thumb);
    border-radius: 4px;
}
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-thumb-hover); }


/* ================================================================
   TOP BAR
   ================================================================ */
.topbar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--topbar-height);
    background: var(--topbar-bg);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-bottom: 1px solid var(--topbar-border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    z-index: var(--z-topbar);
    transition: background var(--transition-base), border-color var(--transition-base);
}

.topbar-left {
    display: flex;
    align-items: center;
    gap: 14px;
    min-width: 200px;
}

.sidebar-toggle {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: var(--radius-sm);
    transition: background var(--transition-fast), color var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}
.sidebar-toggle:hover {
    background: var(--bg-surface-hover);
    color: var(--text-primary);
}

.topbar-brand {
    display: flex;
    align-items: center;
    gap: 8px;
}
.brand-icon { font-size: 1.4rem; }
.brand-text {
    font-weight: 700;
    font-size: 1.15rem;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.topbar-center {
    font-size: 0.85rem;
    color: var(--text-tertiary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 400px;
    text-align: center;
    opacity: 0;
    transition: opacity var(--transition-base);
}
.topbar-center.visible { opacity: 1; }

.topbar-right {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 200px;
    justify-content: flex-end;
    position: relative;
}

/* Theme Switcher */
.theme-switcher {
    display: flex;
    align-items: center;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: 999px;
    padding: 3px;
    gap: 2px;
}

.theme-btn {
    background: none;
    border: none;
    color: var(--text-tertiary);
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 999px;
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
    justify-content: center;
}
.theme-btn:hover {
    color: var(--text-primary);
    background: var(--bg-surface-hover);
}
.theme-btn.active {
    background: var(--accent-primary);
    color: #ffffff;
    box-shadow: 0 2px 8px rgba(245, 158, 11, 0.25);
}

/* Font Size Button & Popup */
.font-size-btn {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    cursor: pointer;
    padding: 7px 10px;
    border-radius: var(--radius-sm);
    transition: all var(--transition-fast);
    display: flex;
    align-items: center;
}
.font-size-btn:hover {
    color: var(--text-primary);
    border-color: var(--accent-primary);
    background: var(--accent-primary-dim);
}

.font-size-popup {
    position: absolute;
    top: calc(100% + 10px);
    right: 0;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    box-shadow: var(--shadow-lg);
    display: none;
    flex-direction: column;
    gap: 10px;
    min-width: 200px;
    z-index: var(--z-popup);
}
.font-size-popup.open { display: flex; }

.font-size-label {
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}
.font-size-value {
    font-size: 0.85rem;
    color: var(--accent-primary);
    font-weight: 600;
    text-align: center;
}

#fontSizeSlider {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 4px;
    border-radius: 2px;
    background: var(--border-color);
    outline: none;
}
#fontSizeSlider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--accent-primary);
    cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}


/* ================================================================
   SIDEBAR
   ================================================================ */
.sidebar {
    position: fixed;
    top: var(--topbar-height);
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    background: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    display: flex;
    flex-direction: column;
    z-index: var(--z-sidebar);
    transition: transform var(--transition-base), background var(--transition-base), border-color var(--transition-base);
    overflow: hidden;
}

.sidebar.collapsed {
    transform: translateX(-100%);
}

.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: var(--z-overlay);
    display: none;
    opacity: 0;
    transition: opacity var(--transition-base);
}
.sidebar-overlay.visible {
    display: block;
    opacity: 1;
}

.sidebar-header {
    padding: 20px 20px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
.sidebar-title {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-heading);
}
.chapter-count {
    font-size: 0.75rem;
    background: var(--accent-primary-dim);
    color: var(--accent-primary);
    padding: 3px 10px;
    border-radius: 999px;
    font-weight: 600;
}

/* Sidebar Search */
.sidebar-search {
    padding: 0 16px 12px;
    position: relative;
}
.search-icon {
    position: absolute;
    left: 28px;
    top: 50%;
    transform: translateY(calc(-50% - 6px));
    color: var(--text-tertiary);
    pointer-events: none;
}
#chapterSearch {
    width: 100%;
    padding: 10px 14px 10px 38px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 0.85rem;
    outline: none;
    transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}
#chapterSearch::placeholder { color: var(--text-tertiary); }
#chapterSearch:focus {
    border-color: var(--accent-primary);
    box-shadow: 0 0 0 3px var(--accent-primary-dim);
}

/* Chapter List */
.chapter-list {
    flex: 1;
    overflow-y: auto;
    padding: 4px 12px;
}

.chapter-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 14px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all var(--transition-fast);
    border: 1px solid transparent;
    margin-bottom: 2px;
    text-decoration: none;
    color: inherit;
}
.chapter-item:hover {
    background: var(--sidebar-item-hover);
    border-color: var(--border-subtle);
}
.chapter-item.active {
    background: var(--sidebar-item-active);
    border-color: var(--sidebar-item-active-border);
    border-left: 3px solid var(--sidebar-item-active-border);
}

.chapter-number {
    min-width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-surface);
    border-radius: var(--radius-sm);
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--text-tertiary);
    transition: all var(--transition-fast);
    flex-shrink: 0;
}
.chapter-item.active .chapter-number {
    background: var(--accent-primary);
    color: #fff;
}

.chapter-item-info {
    flex: 1;
    min-width: 0;
}
.chapter-item-title {
    font-size: 0.84rem;
    font-weight: 500;
    color: var(--text-primary);
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.chapter-item-category {
    font-size: 0.7rem;
    color: var(--text-tertiary);
    margin-top: 3px;
    font-weight: 500;
}

.chapter-item.read .chapter-number {
    background: var(--accent-success);
    color: #fff;
}
.chapter-item.read .chapter-number::after {
    content: '✓';
    font-size: 0.75rem;
}
.chapter-item.read .chapter-number span { display: none; }

/* Sidebar Footer (Progress) */
.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid var(--sidebar-border);
}
.progress-info {
    display: flex;
    justify-content: space-between;
    margin-bottom: 8px;
}
.progress-label {
    font-size: 0.72rem;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
}
.progress-percent {
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--accent-primary);
}
.progress-bar {
    height: 4px;
    background: var(--bg-surface);
    border-radius: 2px;
    overflow: hidden;
}
.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent-primary), var(--accent-secondary));
    border-radius: 2px;
    width: 0%;
    transition: width var(--transition-slow);
}


/* ================================================================
   MAIN CONTENT AREA
   ================================================================ */
.main-content {
    margin-left: var(--sidebar-width);
    margin-top: var(--topbar-height);
    min-height: calc(100vh - var(--topbar-height));
    transition: margin-left var(--transition-base);
    position: relative;
}

.sidebar.collapsed ~ .main-content,
.sidebar.collapsed ~ #sidebarOverlay ~ .main-content {
    margin-left: 0;
}


/* ================================================================
   WELCOME SCREEN
   ================================================================ */
.welcome-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 60px 24px;
    min-height: calc(100vh - var(--topbar-height));
    animation: fadeInUp 0.6s ease;
}

@keyframes fadeInUp {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.welcome-hero {
    text-align: center;
    margin-bottom: 40px;
}

.welcome-icon-ring {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--accent-primary-dim), var(--accent-secondary-dim));
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 24px;
    animation: pulse-ring 3s ease-in-out infinite;
}
.welcome-icon { font-size: 2.8rem; }

@keyframes pulse-ring {
    0%, 100% { box-shadow: 0 0 0 0 var(--accent-primary-dim); }
    50% { box-shadow: 0 0 0 20px transparent; }
}

.welcome-title {
    font-size: 2.8rem;
    font-weight: 800;
    background: linear-gradient(135deg, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    margin-bottom: 8px;
}
.welcome-subtitle {
    font-size: 1.15rem;
    color: var(--text-secondary);
    font-weight: 500;
    margin-bottom: 12px;
}
.welcome-desc {
    font-size: 0.95rem;
    color: var(--text-tertiary);
    max-width: 500px;
    line-height: 1.6;
}

/* Stats Row */
.stats-row {
    display: flex;
    gap: 16px;
    margin-bottom: 48px;
}
.stat-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 20px 30px;
    text-align: center;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}
.stat-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-md);
}
.stat-number {
    display: block;
    font-size: 1.8rem;
    font-weight: 800;
    color: var(--accent-primary);
    line-height: 1.1;
}
.stat-label {
    display: block;
    font-size: 0.78rem;
    color: var(--text-tertiary);
    margin-top: 4px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

/* Topic Groups on welcome */
.topic-groups {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 16px;
    width: 100%;
    max-width: 780px;
    margin-bottom: 40px;
}
.topic-group-card {
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 20px;
    cursor: pointer;
    transition: all var(--transition-fast);
}
.topic-group-card:hover {
    border-color: var(--accent-primary);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}
.topic-group-icon {
    font-size: 1.6rem;
    margin-bottom: 8px;
    display: block;
}
.topic-group-title {
    font-weight: 700;
    font-size: 0.95rem;
    color: var(--text-heading);
    margin-bottom: 4px;
}
.topic-group-count {
    font-size: 0.78rem;
    color: var(--text-tertiary);
}

.welcome-hint {
    font-size: 0.88rem;
    color: var(--text-tertiary);
    animation: fadeIn 1s ease 0.5s both;
}
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}


/* ================================================================
   CHAPTER CONTENT
   ================================================================ */
.chapter-content {
    max-width: var(--content-max-width);
    margin: 0 auto;
    padding: 40px 32px 80px;
    animation: fadeInUp 0.35s ease;
}

.chapter-header {
    margin-bottom: 36px;
    padding-bottom: 24px;
    border-bottom: 1px solid var(--border-color);
}
.chapter-badge {
    display: inline-block;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    padding: 5px 14px;
    border-radius: 999px;
    background: var(--accent-primary-dim);
    color: var(--accent-primary);
    margin-bottom: 14px;
}
.chapter-title {
    font-size: 2rem;
    font-weight: 800;
    color: var(--text-heading);
    line-height: 1.25;
}


/* ================================================================
   MARKDOWN BODY STYLES
   ================================================================ */
.markdown-body {
    line-height: 1.75;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

/* Headings */
.markdown-body h1 {
    font-size: 1.7rem;
    font-weight: 800;
    color: var(--text-heading);
    margin: 48px 0 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--border-color);
}
.markdown-body h1:first-child { margin-top: 0; }

.markdown-body h2 {
    font-size: 1.4rem;
    font-weight: 700;
    color: var(--text-heading);
    margin: 40px 0 16px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border-subtle);
}

.markdown-body h3 {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--text-heading);
    margin: 32px 0 12px;
}

.markdown-body h4, .markdown-body h5, .markdown-body h6 {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-heading);
    margin: 24px 0 10px;
}

/* Paragraphs & Text */
.markdown-body p {
    margin: 0 0 16px;
    color: var(--text-primary);
}

.markdown-body strong {
    font-weight: 700;
    color: var(--text-heading);
}

.markdown-body a {
    color: var(--text-link);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color var(--transition-fast);
}
.markdown-body a:hover { border-bottom-color: var(--text-link); }

/* Lists */
.markdown-body ul, .markdown-body ol {
    padding-left: 24px;
    margin: 0 0 16px;
}
.markdown-body li {
    margin-bottom: 6px;
    color: var(--text-primary);
}
.markdown-body li::marker {
    color: var(--accent-primary);
}

/* Code blocks */
.markdown-body pre {
    background: var(--bg-code);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 0;
    margin: 0 0 20px;
    overflow: hidden;
    position: relative;
}

.code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 16px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border-color);
    font-size: 0.75rem;
    color: var(--text-tertiary);
    font-family: var(--font-mono);
}
.code-lang {
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.code-copy-btn {
    background: none;
    border: 1px solid var(--border-color);
    color: var(--text-tertiary);
    cursor: pointer;
    padding: 3px 10px;
    border-radius: var(--radius-sm);
    font-size: 0.72rem;
    font-family: var(--font-sans);
    font-weight: 500;
    transition: all var(--transition-fast);
}
.code-copy-btn:hover {
    border-color: var(--accent-primary);
    color: var(--accent-primary);
    background: var(--accent-primary-dim);
}
.code-copy-btn.copied {
    border-color: var(--accent-success);
    color: var(--accent-success);
    background: rgba(16, 185, 129, 0.1);
}

.markdown-body pre code {
    display: block;
    padding: 16px 20px;
    overflow-x: auto;
    font-family: var(--font-mono);
    font-size: 0.85rem;
    line-height: 1.6;
    color: var(--text-code);
    white-space: pre-wrap;
    word-break: break-word;
    width: 100%;
}

/* Inline code */
.markdown-body code {
    font-family: var(--font-mono);
    font-size: 0.88em;
    background: var(--bg-code-inline);
    border: 1px solid var(--border-color);
    border-radius: 4px;
    padding: 2px 6px;
    color: var(--accent-primary);
}
.markdown-body pre code {
    background: none;
    border: none;
    border-radius: 0;
    padding: 16px 20px;
    color: var(--text-code);
    white-space: pre-wrap;
    word-break: break-word;
}

/* Tables */
.table-container {
    width: 100%;
    overflow-x: auto;
    margin: 0 0 20px;
    border-radius: var(--radius-md);
    border: 1px solid var(--table-border);
}
.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 0;
    font-size: 0.9rem;
}
.markdown-body th {
    background: var(--table-header-bg);
    font-weight: 700;
    color: var(--text-heading);
    padding: 12px 16px;
    text-align: left;
    border-bottom: 2px solid var(--table-border);
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}
.markdown-body td {
    padding: 10px 16px;
    border-bottom: 1px solid var(--table-border);
    color: var(--text-primary);
}
.markdown-body tr:nth-child(even) td { background: var(--table-row-alt); }

/* Images */
.markdown-img {
    max-width: 100%;
    height: auto;
    border-radius: var(--radius-md);
    margin: 16px auto;
    display: block;
    box-shadow: var(--shadow-sm);
    border: 1px solid var(--border-color);
}

/* Checkboxes / Task Lists */
.markdown-checkbox {
    width: 16px;
    height: 16px;
    accent-color: var(--accent-primary);
    cursor: default;
    vertical-align: middle;
    margin-right: 4px;
}
.markdown-body ul li {
    list-style-type: none;
    position: relative;
    padding-left: 4px;
}
/* Restore bullet for non-checkbox items */
.markdown-body ul li:not(:has(.markdown-checkbox)) {
    list-style-type: disc;
}

/* Horizontal Rules */
.markdown-body hr {
    border: none;
    height: 1px;
    background: var(--border-color);
    margin: 32px 0;
}

/* Blockquotes */
.markdown-body blockquote {
    margin: 0 0 20px;
    padding: 14px 20px;
    background: var(--blockquote-bg);
    border-left: 4px solid var(--blockquote-border);
    border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
    color: var(--text-primary);
}
.markdown-body blockquote p { margin-bottom: 0; }


/* ================================================================
   CHAPTER NAVIGATION (PREV / NEXT)
   ================================================================ */
.chapter-nav {
    display: flex;
    justify-content: space-between;
    gap: 16px;
    margin-top: 48px;
    padding-top: 32px;
    border-top: 1px solid var(--border-color);
}

.chapter-nav-btn {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    padding: 16px 20px;
    cursor: pointer;
    color: var(--text-primary);
    font-family: var(--font-sans);
    transition: all var(--transition-fast);
    flex: 1;
    max-width: 48%;
}
.chapter-nav-btn:hover {
    border-color: var(--accent-primary);
    background: var(--accent-primary-dim);
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}
.chapter-nav-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}
.chapter-nav-btn:disabled:hover {
    border-color: var(--border-color);
    background: var(--bg-surface);
}

.chapter-nav-btn.next { justify-content: flex-end; text-align: right; }
.chapter-nav-btn.prev { text-align: left; }

.nav-btn-text { display: flex; flex-direction: column; gap: 2px; }
.nav-btn-label {
    font-size: 0.72rem;
    text-transform: uppercase;
    color: var(--text-tertiary);
    font-weight: 600;
    letter-spacing: 0.04em;
}
.nav-btn-title {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--text-heading);
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    overflow: hidden;
}


/* ================================================================
   SCROLL TO TOP
   ================================================================ */
.scroll-top {
    position: fixed;
    bottom: 32px;
    right: 32px;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--accent-primary);
    color: #fff;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 16px rgba(245, 158, 11, 0.3);
    z-index: var(--z-scroll-top);
    opacity: 0;
    pointer-events: none;
    transform: translateY(10px);
    transition: opacity var(--transition-base), transform var(--transition-base), background var(--transition-fast);
}
.scroll-top.visible {
    opacity: 1;
    pointer-events: auto;
    transform: translateY(0);
}
.scroll-top:hover {
    background: var(--accent-secondary);
    transform: translateY(-2px);
}


/* ================================================================
   RESPONSIVE
   ================================================================ */
@media (max-width: 900px) {
    .sidebar {
        transform: translateX(-100%);
    }
    .sidebar.open {
        transform: translateX(0);
        box-shadow: var(--shadow-lg);
    }
    .main-content {
        margin-left: 0 !important;
    }
    .chapter-content {
        padding: 24px 18px 60px;
    }
    .welcome-title { font-size: 2rem; }
    .stats-row { flex-direction: column; gap: 10px; align-items: stretch; }
    .stat-card { padding: 14px 20px; }
    .topic-groups {
        grid-template-columns: 1fr;
    }
    .chapter-nav { flex-direction: column; }
    .chapter-nav-btn { max-width: 100%; }

    .topbar-center { display: none; }
}

@media (max-width: 500px) {
    .topbar { padding: 0 12px; }
    .topbar-left { min-width: auto; gap: 8px; }
    .brand-text { display: none; }
    .topbar-right { min-width: auto; }
    .theme-switcher { gap: 0; }
    .theme-btn { padding: 5px 7px; }
    .chapter-title { font-size: 1.4rem; }
}

/* ===== No Results ===== */
.no-results {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-tertiary);
}
.no-results-icon { font-size: 2rem; margin-bottom: 10px; display: block; }
.no-results-text { font-size: 0.9rem; }

/* ===== Utility animations ===== */
@keyframes slideIn {
    from { opacity: 0; transform: translateX(-10px); }
    to { opacity: 1; transform: translateX(0); }
}
.chapter-item {
    animation: slideIn 0.25s ease both;
}
.chapter-item:nth-child(2) { animation-delay: 0.03s; }
.chapter-item:nth-child(3) { animation-delay: 0.06s; }
.chapter-item:nth-child(4) { animation-delay: 0.09s; }
.chapter-item:nth-child(5) { animation-delay: 0.12s; }
.chapter-item:nth-child(6) { animation-delay: 0.15s; }
.chapter-item:nth-child(7) { animation-delay: 0.18s; }
.chapter-item:nth-child(8) { animation-delay: 0.21s; }
.chapter-item:nth-child(9) { animation-delay: 0.24s; }
.chapter-item:nth-child(10) { animation-delay: 0.27s; }
