/* ===== CSS Variables & Reset ===== */
:root {
    --bg: #f5f7fa;
    --bg-card: #ffffff;
    --bg-sidebar: #1a1d23;
    --bg-sidebar-hover: #2a2d35;
    --bg-sidebar-active: #3a3d45;
    --text: #1a1a2e;
    --text-secondary: #555;
    --text-sidebar: #ccc;
    --text-sidebar-active: #fff;
    --accent: #e67e22;
    --accent-light: #fdf2e9;
    --accent-dark: #ca6c17;
    --border: #e0e4ea;
    --success: #2ecc71;
    --warning: #8e44ad;
    --danger: #e74c3c;
    --formula-bg: #fef9f4;
    --formula-border: #e67e22;
    --exercise-bg: #f9fafb;
    --correction-bg: #f0fff4;
    --correction-border: #2ecc71;
    --shadow: 0 2px 8px rgba(0,0,0,0.08);
    --shadow-lg: 0 4px 20px rgba(0,0,0,0.12);
    --radius: 10px;
    --radius-sm: 6px;
    --sidebar-width: 300px;
    --mobile-header-height: 56px;
    --transition: 0.25s ease;
}

[data-theme="dark"] {
    --bg: #0f1117;
    --bg-card: #1a1d25;
    --text: #e4e6eb;
    --text-secondary: #aaa;
    --border: #2a2d35;
    --accent-light: #3d2510;
    --formula-bg: #2a1a0f;
    --exercise-bg: #15171f;
    --correction-bg: #0f2018;
    --correction-border: #27ae60;
    --shadow: 0 2px 8px rgba(0,0,0,0.3);
    --shadow-lg: 0 4px 20px rgba(0,0,0,0.4);
}

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.6;
    display: flex;
    min-height: 100vh;
}

/* ===== Sidebar ===== */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--bg-sidebar);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    z-index: 1000;
    overflow: hidden;
}

.sidebar-header {
    padding: 24px 20px 12px;
    border-bottom: 1px solid rgba(255,255,255,0.1);
    display: flex;
    align-items: center;
    gap: 12px;
}

.back-home {
    color: rgba(255,255,255,0.6);
    text-decoration: none;
    font-size: 1.3rem;
    transition: var(--transition);
    flex-shrink: 0;
}

.back-home:hover {
    color: #fff;
}

.sidebar-header h1 {
    font-size: 1.3rem;
    font-weight: 700;
    color: #fff;
    margin-bottom: 2px;
}

.sidebar-header .subtitle {
    font-size: 0.85rem;
    color: rgba(255,255,255,0.5);
}

.sidebar-search {
    padding: 12px 16px;
}

.sidebar-search input {
    width: 100%;
    padding: 8px 12px;
    border-radius: var(--radius-sm);
    border: 1px solid rgba(255,255,255,0.15);
    background: rgba(255,255,255,0.08);
    color: #fff;
    font-size: 0.9rem;
    outline: none;
    transition: var(--transition);
}

.sidebar-search input::placeholder {
    color: rgba(255,255,255,0.4);
}

.sidebar-search input:focus {
    border-color: var(--accent);
    background: rgba(255,255,255,0.12);
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
    padding: 8px 0;
}

.sidebar-nav::-webkit-scrollbar {
    width: 4px;
}

.sidebar-nav::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.2);
    border-radius: 2px;
}

.nav-theme {
    padding: 12px 20px 6px;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    color: rgba(255,255,255,0.35);
    font-weight: 600;
}

.nav-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 20px;
    cursor: pointer;
    transition: var(--transition);
    font-size: 0.88rem;
    border-left: 3px solid transparent;
}

.nav-item:hover {
    background: var(--bg-sidebar-hover);
}

.nav-item.active {
    background: var(--bg-sidebar-active);
    border-left-color: var(--accent);
    color: var(--text-sidebar-active);
}

.nav-item .nav-check {
    font-size: 0.75rem;
    opacity: 0.4;
}

.nav-item .nav-check.done {
    color: var(--success);
    opacity: 1;
}

.nav-item.hidden {
    display: none;
}

.sidebar-footer {
    padding: 16px 20px;
    border-top: 1px solid rgba(255,255,255,0.1);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
}

.progress-label {
    font-size: 0.78rem;
    color: rgba(255,255,255,0.5);
    flex: 1;
}

.progress-bar {
    width: 100%;
    height: 6px;
    background: rgba(255,255,255,0.1);
    border-radius: 3px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--accent);
    border-radius: 3px;
    transition: width 0.4s ease;
    width: 0;
}

.progress-text {
    font-size: 0.78rem;
    color: rgba(255,255,255,0.5);
}

/* ===== Mobile Header ===== */
.mobile-header {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--mobile-header-height);
    background: var(--bg-sidebar);
    color: #fff;
    align-items: center;
    padding: 0 12px;
    z-index: 998;
    gap: 12px;
}

.mobile-header h1 {
    font-size: 1.05rem;
    font-weight: 600;
    flex: 1;
}

.mobile-header h1 span {
    color: var(--accent);
}

.overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 999;
}

.overlay.active {
    display: block;
}

/* ===== Buttons ===== */
.btn-icon {
    background: none;
    border: none;
    color: inherit;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 6px;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

.btn-icon:hover {
    background: rgba(255,255,255,0.1);
}

.btn-back {
    background: none;
    border: none;
    color: var(--accent);
    font-size: 0.95rem;
    cursor: pointer;
    padding: 4px 0;
    font-weight: 500;
    transition: var(--transition);
}

.btn-back:hover {
    opacity: 0.7;
}

.btn-nav {
    padding: 10px 20px;
    border: 1px solid var(--border);
    background: var(--bg-card);
    color: var(--text);
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: var(--transition);
}

.btn-nav:hover {
    border-color: var(--accent);
    color: var(--accent);
}

.btn-nav:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* ===== Main Content ===== */
.main-content {
    margin-left: var(--sidebar-width);
    flex: 1;
    min-height: 100vh;
    padding: 40px;
    max-width: 900px;
    margin-right: auto;
}

.nav-item:focus-visible, .chapter-card:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.page {
    display: none;
}

.page.active {
    display: block;
}

/* ===== Home Page ===== */
.hero {
    text-align: center;
    padding: 40px 0 50px;
}

.hero h2 {
    font-size: 2rem;
    margin-bottom: 12px;
    color: var(--text);
}

.hero > p {
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.feature-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
    margin-top: 30px;
}

.feature-card {
    background: var(--bg-card);
    padding: 24px 18px;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
    transition: var(--transition);
}

.feature-card:hover {
    transform: translateY(-3px);
    box-shadow: var(--shadow-lg);
}

.feature-icon {
    font-size: 2rem;
    margin-bottom: 10px;
}

.feature-card h3 {
    font-size: 1rem;
    margin-bottom: 6px;
}

.feature-card p {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.themes-overview h2 {
    font-size: 1.5rem;
    margin-bottom: 20px;
    padding-bottom: 10px;
    border-bottom: 2px solid var(--border);
}

.themes-grid {
    display: flex;
    flex-direction: column;
    gap: 24px;
}

.theme-section h3 {
    font-size: 1.1rem;
    color: var(--accent);
    margin-bottom: 12px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.theme-chapters {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 12px;
}

.chapter-card {
    background: var(--bg-card);
    padding: 16px 18px;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
    cursor: pointer;
    transition: var(--transition);
    display: flex;
    align-items: center;
    gap: 12px;
}

.chapter-card:hover {
    border-color: var(--accent);
    box-shadow: var(--shadow);
    transform: translateY(-2px);
}

.chapter-card .card-num {
    background: var(--accent-light);
    color: var(--accent);
    font-weight: 700;
    font-size: 0.85rem;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.chapter-card .card-title {
    font-size: 0.92rem;
    font-weight: 500;
}

.chapter-card .card-check {
    margin-left: auto;
    color: var(--success);
    font-size: 1.1rem;
    display: none;
}

.chapter-card.done .card-check {
    display: block;
}

/* ===== Chapter Page ===== */
.chapter-header {
    margin-bottom: 24px;
}

.chapter-theme {
    display: inline-block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin: 8px 0 6px;
}

#chapter-title {
    font-size: 1.7rem;
    line-height: 1.3;
}

/* Tabs */
.chapter-tabs {
    display: flex;
    gap: 4px;
    border-bottom: 2px solid var(--border);
    margin-bottom: 24px;
}

.tab {
    padding: 10px 18px;
    border: none;
    background: none;
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    border-bottom: 2px solid transparent;
    margin-bottom: -2px;
    transition: var(--transition);
}

.tab:hover {
    color: var(--text);
}

.tab.active {
    color: var(--accent);
    border-bottom-color: var(--accent);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* Content Styling */
.chapter-body h3 {
    font-size: 1.2rem;
    margin: 24px 0 12px;
    color: var(--text);
    border-left: 3px solid var(--accent);
    padding-left: 12px;
}

.chapter-body h3:first-child {
    margin-top: 0;
}

.chapter-body h4 {
    font-size: 1.05rem;
    margin: 18px 0 8px;
    color: var(--text);
}

.chapter-body p, .chapter-body li {
    font-size: 0.95rem;
    line-height: 1.7;
    color: var(--text-secondary);
}

.chapter-body ul, .chapter-body ol {
    padding-left: 20px;
    margin: 8px 0;
}

.chapter-body li {
    margin-bottom: 4px;
}

/* Formula cards */
.formula-card {
    background: var(--formula-bg);
    border: 1px solid var(--formula-border);
    border-left: 4px solid var(--formula-border);
    border-radius: var(--radius-sm);
    padding: 16px 20px;
    margin: 12px 0;
}

.formula-card .formula-title {
    font-weight: 600;
    font-size: 0.92rem;
    color: var(--accent);
    margin-bottom: 6px;
}

.formula-card .formula-math {
    font-size: 1.15rem;
    margin: 8px 0;
    text-align: center;
}

.formula-card .formula-legend {
    font-size: 0.83rem;
    color: var(--text-secondary);
    margin-top: 6px;
}

.formula-card .formula-legend p {
    margin: 2px 0;
    font-size: 0.83rem;
}

/* Definition cards */
.definition-card {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-left: 4px solid var(--warning);
    border-radius: var(--radius-sm);
    padding: 14px 18px;
    margin: 12px 0;
}

.definition-card .def-term {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--warning);
    margin-bottom: 4px;
}

.definition-card .def-text {
    font-size: 0.92rem;
    color: var(--text-secondary);
}

/* Exercises */
.exercise-block {
    background: var(--exercise-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 20px;
    margin: 16px 0;
}

.exercise-block .exercise-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.exercise-block .exercise-label {
    font-weight: 600;
    font-size: 1rem;
    color: var(--accent);
}

.exercise-block .exercise-difficulty {
    font-size: 0.78rem;
    padding: 3px 10px;
    border-radius: 12px;
    font-weight: 600;
}

.difficulty-facile {
    background: #e8f5e9;
    color: #2e7d32;
}

.difficulty-moyen {
    background: #fff3e0;
    color: #ef6c00;
}

.difficulty-difficile {
    background: #fce4ec;
    color: #c62828;
}

[data-theme="dark"] .difficulty-facile {
    background: #1b3d1f;
    color: #66bb6a;
}

[data-theme="dark"] .difficulty-moyen {
    background: #3d2e0f;
    color: #ffa726;
}

[data-theme="dark"] .difficulty-difficile {
    background: #3d1515;
    color: #ef5350;
}

.exercise-block .exercise-text {
    font-size: 0.93rem;
    color: var(--text-secondary);
    line-height: 1.7;
    margin-bottom: 12px;
}

.exercise-block .exercise-text p {
    margin-bottom: 8px;
}

.btn-correction {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 16px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.88rem;
    font-weight: 500;
    transition: var(--transition);
}

.btn-correction:hover {
    background: var(--accent-dark);
}

.correction-content {
    display: none;
    margin-top: 16px;
    padding: 16px 18px;
    background: var(--correction-bg);
    border: 1px solid var(--correction-border);
    border-left: 4px solid var(--correction-border);
    border-radius: var(--radius-sm);
}

.correction-content.visible {
    display: block;
}

.correction-content h4 {
    color: var(--correction-border);
    margin-bottom: 8px;
}

.correction-content p, .correction-content li {
    font-size: 0.92rem;
    line-height: 1.7;
    color: var(--text-secondary);
}

/* Bottom Nav */
.chapter-nav-bottom {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 40px;
    padding-top: 20px;
    border-top: 1px solid var(--border);
}

.checkbox-done {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    font-weight: 500;
    color: var(--text-secondary);
    cursor: pointer;
    user-select: none;
}

.checkbox-done input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--accent);
    cursor: pointer;
}

/* ===== KaTeX overrides ===== */
.katex {
    font-size: 1.05em;
}

.formula-math .katex {
    font-size: 1.2em;
}

/* ===== Responsive ===== */
@media (max-width: 900px) {
    .feature-grid {
        grid-template-columns: 1fr;
        gap: 12px;
    }
    .theme-chapters {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .mobile-header {
        display: flex;
    }

    .main-content {
        margin-left: 0;
        padding: 20px 16px;
        padding-top: calc(var(--mobile-header-height) + 16px);
        max-width: 100%;
    }

    .hero h2 {
        font-size: 1.5rem;
    }

    .chapter-tabs {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    .tab {
        padding: 8px 14px;
        font-size: 0.88rem;
        white-space: nowrap;
    }

    #chapter-title {
        font-size: 1.3rem;
    }

    .chapter-nav-bottom {
        flex-wrap: wrap;
        gap: 12px;
        justify-content: center;
    }

    .formula-card .formula-math {
        font-size: 1rem;
        overflow-x: auto;
    }
}

/* ===== Animations ===== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

.page.active {
    animation: fadeIn 0.3s ease;
}

/* ===== Print ===== */
@media print {
    .sidebar, .mobile-header, .overlay, .btn-back, .chapter-tabs, .chapter-nav-bottom {
        display: none !important;
    }
    .main-content {
        margin: 0;
        padding: 20px;
    }
    .tab-content {
        display: block !important;
    }
    .correction-content {
        display: block !important;
    }
}
