:root {
    color-scheme: light dark;
    --bg: #f1f5f9;
    --card-bg: #ffffff;
    --text: #1f2937;
    --muted: #6b7280;
    --border: #d1d5db;
    --accent: #2563eb;
    --accent-soft: #eff6ff;
    --done: #16a34a;
    --done-soft: #f0fdf4;
    --danger: #dc2626;
    --danger-soft: #fef2f2;
    --warn: #d97706;
    --warn-soft: #fef3c7;
    --radius: 10px;
    --track: #e5e7eb;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f172a;
        --card-bg: #1e293b;
        --text: #e5e7eb;
        --muted: #9ca3af;
        --border: #334155;
        --accent: #60a5fa;
        --accent-soft: #1e3a5f;
        --done: #4ade80;
        --done-soft: #14301f;
        --danger: #f87171;
        --danger-soft: #3f1d1d;
        --warn: #fbbf24;
        --warn-soft: #3f2f0f;
        --track: #334155;
    }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
    font-family: system-ui, "Segoe UI", Arial, sans-serif;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    line-height: 1.55;
    font-size: 16px;
}

.page {
    max-width: 900px;
    margin: 0 auto;
    padding: 1rem 1rem 3rem;
}
@media (min-width: 640px) {
    .page { padding: 1.5rem 1.5rem 3.5rem; }
}
.page.page-wide { max-width: 1200px; }

h1 { font-size: 1.3rem; margin: 0.6rem 0 1rem; line-height: 1.3; }
h2 { font-size: 1.1rem; margin-top: 1.75rem; }
h3 { font-size: 1rem; margin-top: 1.25rem; }
@media (min-width: 640px) {
    h1 { font-size: 1.6rem; }
    h2 { font-size: 1.25rem; }
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
    border-radius: 4px;
}

/* Top bar */
.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    background: var(--card-bg);
    border-bottom: 1px solid var(--border);
}
.topbar-inner {
    max-width: 900px;
    margin: 0 auto;
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}
.topbar-inner-wide { max-width: 1200px; }
@media (min-width: 640px) {
    .topbar-inner { padding: 0.75rem 1.5rem; }
}
.brand { font-weight: 700; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; opacity: 0.8; }
.crumb { color: var(--muted); font-size: 0.9rem; }
.crumb::before { content: "/"; margin-right: 0.75rem; color: var(--border); }

/* Sub navigation (Suivi / Lire / Quiz) */
.subnav {
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0.25rem 0 1.25rem;
    padding-bottom: 0.25rem;
}
.subnav a {
    flex: none;
    padding: 0.5rem 0.9rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--text);
    font-size: 0.9rem;
    white-space: nowrap;
}
.subnav a:hover { text-decoration: none; border-color: var(--accent); }
.subnav a.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* Cards */
.card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.1rem;
    margin: 1rem 0;
}

/* Chapter grid on index */
.chapter-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.9rem;
    margin: 1.25rem 0;
}
@media (min-width: 640px) {
    .chapter-grid { grid-template-columns: repeat(2, 1fr); }
}
.chapter-card {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.1rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}
.chapter-card-top { display: flex; align-items: baseline; gap: 0.5rem; }
.chapter-num { color: var(--muted); font-weight: 700; font-size: 0.9rem; }
.chapter-card h2 { margin: 0; font-size: 1.05rem; flex: 1; }
.chapter-card-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; margin-top: 0.25rem; }

/* Badges */
.badge {
    display: inline-block;
    padding: 0.2rem 0.6rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    white-space: nowrap;
}
.badge.not_started { background: var(--track); color: var(--muted); }
.badge.in_progress { background: var(--warn-soft); color: var(--warn); }
.badge.done { background: var(--done-soft); color: var(--done); }

/* Progress bar */
.progress-bar {
    background: var(--track);
    border-radius: 999px;
    height: 10px;
    width: 100%;
    overflow: hidden;
}
.progress-bar > div {
    background: var(--accent);
    height: 100%;
    transition: width 0.2s ease;
}
.progress-label { color: var(--muted); font-size: 0.85rem; }

/* Buttons (real <button> and link-buttons via .btn) */
button, .btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
    font-family: inherit;
    font-size: 0.95rem;
    padding: 0.6rem 1rem;
    min-height: 44px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: var(--accent);
    color: #fff;
    cursor: pointer;
    text-decoration: none;
}
button:hover, .btn:hover { opacity: 0.9; text-decoration: none; }
button.secondary, .btn.secondary {
    background: transparent;
    color: var(--text);
    border-color: var(--border);
}
button.danger, .btn.danger { background: var(--danger); }

/* Form elements */
fieldset { border: 1px solid var(--border); border-radius: var(--radius); margin: 1rem 0; padding: 1rem; }
legend { font-weight: 600; padding: 0 0.4rem; }

label.step {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 0.4rem 0;
    padding: 0.3rem 0;
    min-height: 44px;
}
label.step input[type=checkbox] { width: 1.2rem; height: 1.2rem; flex: none; }

textarea {
    width: 100%;
    min-height: 100px;
    font-family: inherit;
    font-size: 1rem;
    padding: 0.6rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card-bg);
    color: var(--text);
}

select, input[type=text] {
    font-family: inherit;
    font-size: 1rem;
    padding: 0.55rem 0.6rem;
    min-height: 44px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card-bg);
    color: var(--text);
    max-width: 100%;
}

.glossary-form { display: flex; gap: 0.5rem; flex-wrap: wrap; margin-top: 0.5rem; }
.glossary-form input { flex: 1 1 160px; }
.glossary-form button { flex: none; }

.back { display: inline-block; margin-bottom: 1rem; }
.saved-note { color: var(--done); font-size: 0.9rem; font-weight: 600; }

/* Responsive tables -> stacked cards under 640px */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
th, td { text-align: left; padding: 0.6rem 0.5rem; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.02em; }

@media (max-width: 640px) {
    table.responsive, table.responsive thead, table.responsive tbody, table.responsive tr, table.responsive td {
        display: block;
        width: 100%;
    }
    table.responsive thead { display: none; }
    table.responsive tr {
        border: 1px solid var(--border);
        border-radius: var(--radius);
        margin-bottom: 0.75rem;
        padding: 0.5rem 0.75rem;
    }
    table.responsive td {
        border-bottom: none;
        padding: 0.35rem 0;
        display: flex;
        gap: 0.5rem;
    }
    table.responsive td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--muted);
        min-width: 90px;
        flex: none;
    }
    table.responsive td.no-label::before { content: none; }
}

/* read.php lesson layout */
.lesson-intro { color: var(--muted); font-size: 0.92rem; }
.lesson-layout {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1rem;
}
@media (min-width: 800px) {
    .lesson-layout { grid-template-columns: 240px 1fr; align-items: start; }
    .toc-sidebar { position: sticky; top: 70px; }
}
@media (min-width: 1000px) {
    .lesson-layout { grid-template-columns: 260px 1fr; }
    .section { padding: 1.4rem 1.7rem; }
}
.toc-sidebar { display: flex; flex-direction: column; gap: 0.3rem; }
.toc-sidebar a {
    padding: 0.4rem 0.6rem;
    border-radius: 8px;
    font-size: 0.88rem;
    color: var(--text);
}
.toc-sidebar a:hover { background: var(--accent-soft); text-decoration: none; }

.section {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.1rem 1.2rem;
    margin: 0 0 1.2rem;
    scroll-margin-top: 70px;
}
.section-pdf-link {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.85rem;
    margin-bottom: 0.5rem;
}
.brief {
    background: var(--accent-soft);
    border-left: 3px solid var(--accent);
    padding: 0.6rem 0.8rem;
    margin: 0.8rem 0;
    font-size: 0.92rem;
    border-radius: 0 6px 6px 0;
}
.vocab-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.6rem;
    padding: 0.55rem 0;
    border-bottom: 1px dashed var(--border);
    flex-wrap: wrap;
}
.vocab-item:last-child { border-bottom: none; }
.vocab-text { flex: 1 1 220px; }
.vocab-en { font-weight: 600; }
.vocab-def { color: var(--muted); font-size: 0.9rem; }
.vocab-fr { font-style: italic; font-size: 0.9rem; }
.add-btn { font-size: 0.8rem; padding: 0.4rem 0.7rem; min-height: 36px; flex: none; }
.add-btn.added { background: var(--done); cursor: default; }

.note-item {
    background: var(--warn-soft);
    border-left: 3px solid var(--warn);
    border-radius: 0 6px 6px 0;
    padding: 0.5rem 0.7rem;
    margin: 0.5rem 0;
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 0.6rem;
    flex-wrap: wrap;
}
.note-item p { margin: 0; flex: 1 1 200px; white-space: pre-wrap; }
.note-delete { font-size: 0.78rem; padding: 0.25rem 0.6rem; min-height: 32px; flex: none; }
.add-note-btn { font-size: 0.85rem; margin-top: 0.3rem; }

.note-modal {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card-bg);
    color: var(--text);
    padding: 1.2rem;
    width: min(90vw, 480px);
}
.note-modal::backdrop { background: rgba(15, 23, 42, 0.5); }
.note-modal h3 { margin-top: 0; }
.note-modal textarea { width: 100%; min-height: 120px; }
.note-modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; margin-top: 0.8rem; }

details {
    margin: 0.5rem 0;
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.5rem 0.75rem;
}
summary { cursor: pointer; color: var(--accent); font-weight: 500; }
summary:hover { text-decoration: underline; }
details p { margin: 0.5rem 0 0.1rem; }

/* quiz.php */
.score-banner {
    position: sticky;
    top: 58px;
    z-index: 5;
    padding: 0.8rem 1rem;
    border-radius: var(--radius);
    background: var(--accent-soft);
    margin: 1rem 0;
    border: 1px solid var(--border);
}
.question {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--card-bg);
    padding: 1rem;
    margin: 1rem 0;
}
.question.result-ok { border-color: var(--done); background: var(--done-soft); }
.question.result-ko { border-color: var(--danger); background: var(--danger-soft); }
.question.result-open { border-color: var(--accent); background: var(--accent-soft); }
.opt {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 0.2rem 0;
    padding: 0.4rem 0.3rem;
    min-height: 40px;
    border-radius: 6px;
}
.opt:hover { background: rgba(127,127,127,0.08); }
.opt input { width: 1.15rem; height: 1.15rem; flex: none; }
.match-row {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin: 0.4rem 0;
    flex-wrap: wrap;
}
.match-row label { min-width: 140px; flex: 1 1 140px; font-weight: 500; }
.match-row select { flex: 1 1 220px; }
.refs {
    font-size: 0.85rem;
    margin-top: 0.5rem;
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}
.verdict { font-weight: 600; }
textarea.open-answer { width: 100%; min-height: 70px; }

.sticky-actions {
    position: sticky;
    bottom: 0;
    background: var(--bg);
    padding: 0.75rem 0;
    margin-top: 0.5rem;
    border-top: 1px solid var(--border);
}

/* Top bar quick link */
.topbar-link {
    margin-left: auto;
    font-size: 0.85rem;
    white-space: nowrap;
}

/* Quiz mode toggle */
.mode-toggle {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-bottom: 1rem;
}

/* Practice mode */
.q-actions { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-top: 0.6rem; }
.q-result { margin-top: 0.6rem; }
.practice-bar {
    position: sticky;
    bottom: 0;
    background: var(--bg);
    padding: 0.75rem 0;
    margin-top: 0.5rem;
    border-top: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}
#practice-stats { font-weight: 600; font-size: 0.9rem; }

/* AI explain button + output */
.explain-btn { font-size: 0.85rem; }
.explain-output {
    background: var(--accent-soft);
    border-left: 3px solid var(--accent);
    border-radius: 0 6px 6px 0;
    padding: 0.6rem 0.8rem;
    margin-top: 0.5rem;
    font-size: 0.92rem;
    white-space: pre-wrap;
}

/* Revision passive (auto-scrolling notes) */
.revision-player {
    max-width: 640px;
    margin: 1.5rem auto;
    text-align: left;
}
.revision-meta {
    display: flex;
    justify-content: space-between;
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 0.4rem;
}
.revision-kind {
    text-transform: uppercase;
    letter-spacing: 0.03em;
    font-weight: 600;
    color: var(--accent);
}
#revision-title { margin-top: 0.2rem; }
#revision-text { min-height: 3.5rem; white-space: pre-wrap; }
.revision-progress {
    background: var(--track);
    border-radius: 999px;
    height: 6px;
    width: 100%;
    overflow: hidden;
    margin: 0.8rem 0;
}
#revision-progress-bar {
    background: var(--accent);
    height: 100%;
    width: 0%;
    transition: width 0.1s linear;
}
.revision-controls {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    margin-top: 0.6rem;
}
.revision-controls select { flex: none; }
.tips-list {
    padding-left: 1.4rem;
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}
.tips-list li {
    background: var(--card-bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.6rem 0.9rem;
}
.tip-page-link {
    white-space: nowrap;
    font-size: 0.85rem;
    margin-left: 0.4rem;
}
