/* ============================================================
   Variables — Dark mode (défaut)
   ============================================================ */
[data-theme="dark"] {
    --primary:         #818cf8;
    --primary-dark:    #6366f1;
    --primary-light:   rgba(129, 140, 248, 0.14);
    --danger:          #ef4444;
    --success:         #22c55e;
    --success-light:   rgba(34, 197, 94, 0.12);
    --success-border:  rgba(34, 197, 94, 0.35);
    --bg:              #0f172a;
    --surface:         #1e293b;
    --surface-2:       #273549;
    --border:          #334155;
    --text:            #e2e8f0;
    --text-muted:      #94a3b8;
    --nav-hover:       rgba(255, 255, 255, 0.05);
    --nav-active-bg:   rgba(129, 140, 248, 0.16);
    --shadow:          0 1px 4px rgba(0, 0, 0, 0.5);
    --radius:          8px;
}

[data-theme="light"] {
    --primary:         #2563eb;
    --primary-dark:    #1d4ed8;
    --primary-light:   #eff6ff;
    --danger:          #ef4444;
    --success:         #16a34a;
    --success-light:   #f0fdf4;
    --success-border:  #bbf7d0;
    --bg:              #f1f5f9;
    --surface:         #ffffff;
    --surface-2:       #f8fafc;
    --border:          #e2e8f0;
    --text:            #1e293b;
    --text-muted:      #64748b;
    --nav-hover:       #f8fafc;
    --nav-active-bg:   #eff6ff;
    --shadow:          0 1px 3px rgba(0, 0, 0, 0.1);
    --radius:          8px;
}

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

body {
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    font-size: 0.9rem;
    line-height: 1.5;
}


/* ============================================================
   NAVBAR BOOTSTRAP AM
   ============================================================ */
.am-navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 1rem;
    height: 56px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}
.am-navbar-brand {
    color: var(--text);
    text-decoration: none;
    font-weight: 600;
    font-size: 0.95rem;
    padding: 0.35rem 0.65rem;
    border-radius: var(--radius);
    transition: background 0.15s;
}
.am-navbar-brand:hover { background: var(--nav-hover); color: var(--text); }
.am-navbar #theme-toggle {
    background: none;
    border: none;
    color: var(--text);
    font-size: 1.1rem;
    cursor: pointer;
    padding: 0.35rem 0.5rem;
    border-radius: var(--radius);
    transition: background 0.15s;
    line-height: 1;
}
.am-navbar #theme-toggle:hover { background: var(--nav-hover); }

/* ============================================================
   PAGE INDEX — layout desktop 4 colonnes
   ============================================================ */
body.page-index {
    height: 100vh;
    overflow: hidden;
    display: grid;
    grid-template-columns: 170px 210px 1fr 290px;
    grid-template-rows: auto auto 1fr;
    grid-template-areas:
        "navbar  navbar   navbar  navbar"
        "header  header   header  header"
        "niveaux themes   exos    sidebar";
}

/* Assigne chaque colonne à sa zone */
body.page-index > nav.am-navbar    { grid-area: navbar; }
body.page-index > header          { grid-area: header; }
body.page-index #col-niveaux      { grid-area: niveaux; }
body.page-index #col-themes       { grid-area: themes; }
body.page-index #col-exercices    { grid-area: exos; }
body.page-index .panneau-selection { grid-area: sidebar; }


/* ============================================================
   En-tête
   ============================================================ */
.page-index header {
    padding: 0 1.25rem;
    height: 48px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.page-index header h1 {
    font-size: 1rem;
    font-weight: 700;
    white-space: nowrap;
}

.sous-titre {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-left: auto;
    white-space: nowrap;
}

.breadcrumb {
    font-size: 0.78rem;
    color: var(--text-muted);
    margin-left: 0.25rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    display: none; /* desktop: masqué */
}

.btn-theme {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.25rem 0.5rem;
    cursor: pointer;
    font-size: 0.95rem;
    color: var(--text);
    flex-shrink: 0;
    transition: background 0.12s;
}
.btn-theme:hover { background: var(--nav-hover); }

.mobile-back {
    display: none; /* masqué sur desktop */
    background: none;
    border: none;
    cursor: pointer;
    color: var(--primary);
    font-size: 0.85rem;
    font-weight: 600;
    padding: 0.2rem 0.5rem 0.2rem 0;
    white-space: nowrap;
}


/* ============================================================
   Colonnes de navigation
   ============================================================ */
.col-nav {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

#col-exercices {
    background: var(--bg);
    border-right: none;
}

.col-header {
    padding: 0 1rem;
    height: 36px;
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--surface);
    flex-shrink: 0;
}

.col-content {
    overflow-y: auto;
    flex: 1;
}

/* ── Items de navigation (niveau, thème) ── */
.nav-item {
    padding: 0.55rem 1rem;
    cursor: pointer;
    font-size: 0.82rem;
    border-left: 3px solid transparent;
    color: var(--text);
    line-height: 1.35;
    transition: background 0.1s, border-color 0.1s, color 0.1s;
    user-select: none;
}

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

.nav-item.actif {
    background: var(--nav-active-bg);
    border-left-color: var(--primary);
    color: var(--primary);
    font-weight: 600;
}

.nav-placeholder {
    padding: 1.5rem 1rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
    line-height: 1.7;
}

.compteur-affichage {
    font-size: 0.7rem;
    color: var(--text-muted);
    font-weight: 400;
    text-transform: none;
    letter-spacing: 0;
}


/* ============================================================
   Liste des exercices
   ============================================================ */
#liste-exercices {
    padding: 0.6rem;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}

.exercice-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.55rem 0.8rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: grab;
    transition: border-color 0.15s, background 0.15s;
}

.exercice-item:hover    { border-color: var(--primary); }
.exercice-item.masque   { display: none; }
.exercice-item.dragging { opacity: 0.35; }

.exercice-item.selectionne {
    border-color: var(--primary);
    background: var(--primary-light);
}

.exercice-info { flex: 1; min-width: 0; }

.exercice-nom {
    font-weight: 600;
    font-size: 0.83rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.exercice-desc {
    font-size: 0.74rem;
    color: var(--text-muted);
    margin-top: 0.1rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.exercice-controls {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    flex-shrink: 0;
}

/* Boutons +/- partagés (liste + panneau) */
.btn-moins, .btn-plus,
.btn-sel-moins, .btn-sel-plus {
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--bg);
    cursor: pointer;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text);
    line-height: 1;
    transition: background 0.12s, color 0.12s, border-color 0.12s;
}

.btn-moins, .btn-plus {
    width: 26px;
    height: 26px;
    font-size: 1.05rem;
}

.btn-sel-moins, .btn-sel-plus {
    width: 22px;
    height: 22px;
    font-size: 0.95rem;
}

.btn-plus:hover, .btn-sel-plus:hover  { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-moins:hover, .btn-sel-moins:hover { background: var(--danger);  color: #fff; border-color: var(--danger); }
.btn-moins:disabled { opacity: 0.2; cursor: not-allowed; pointer-events: none; }

.compteur-exercice {
    min-width: 1.3rem;
    text-align: center;
    font-weight: 700;
    font-size: 0.88rem;
    color: var(--primary);
}

.compteur-exercice.zero { color: var(--text-muted); font-weight: 400; }


/* ============================================================
   Panneau de sélection
   ============================================================ */
.panneau-selection {
    background: var(--surface);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.panneau-header {
    padding: 0 1.25rem;
    height: 36px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.panneau-header h2 {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.07em;
    color: var(--text-muted);
}

.panneau-count { font-weight: 400; }

.panneau-chevron {
    font-size: 0.65rem;
    color: var(--text-muted);
    display: none; /* masqué sur desktop */
    transition: transform 0.3s;
}

.panneau-body {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
}

.liste-selection-droppable {
    flex: 1;
    overflow-y: auto;
    padding: 0.6rem 0.9rem;
    transition: background 0.15s;
}

.liste-selection-droppable.drop-target {
    background: var(--primary-light);
    outline: 2px dashed var(--primary);
    outline-offset: -4px;
    border-radius: var(--radius);
}

.selection-vide {
    color: var(--text-muted);
    font-size: 0.8rem;
    text-align: center;
    margin-top: 2rem;
    line-height: 1.7;
}

.selection-item {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.45rem 0;
    border-bottom: 1px solid var(--border);
    cursor: grab;
}

.selection-item:last-child { border-bottom: none; }
.selection-item.sel-dragging { opacity: 0.35; }
.selection-item.drag-over    { background: var(--primary-light); border-radius: var(--radius); padding: 0.45rem 0.4rem; }

.drag-handle {
    color: var(--text-muted);
    font-size: 0.95rem;
    cursor: grab;
    user-select: none;
    flex-shrink: 0;
    opacity: 0.5;
}

.selection-item:hover .drag-handle { opacity: 1; }

.selection-item-nom {
    font-size: 0.78rem;
    flex: 1;
    line-height: 1.35;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.selection-item-controls {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    flex-shrink: 0;
}

.selection-item-count {
    font-weight: 700;
    color: var(--primary);
    font-size: 0.8rem;
    min-width: 1.8rem;
    text-align: center;
}

.selection-footer {
    padding: 0.85rem 1.25rem;
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

#btn-generer {
    width: 100%;
    padding: 0.65rem;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    font-size: 0.9rem;
    font-weight: 700;
    cursor: pointer;
    letter-spacing: 0.01em;
    transition: background 0.15s;
}

#btn-generer:hover:not(:disabled) { background: var(--primary-dark); }

#btn-generer:disabled {
    background: var(--border);
    color: var(--text-muted);
    cursor: not-allowed;
}


/* ============================================================
   RESPONSIVE MOBILE — <= 768px
   ============================================================ */
@media (max-width: 768px) {

    body.page-index {
        display: block;
        height: 100dvh;
        overflow: hidden;
        position: relative;
    }

    /* En-tête */
    body.page-index > header {
        position: sticky;
        top: 0;
        z-index: 30;
    }

    .breadcrumb     { display: block; flex: 1; }
    .sous-titre     { display: none; }
    .mobile-back    { display: block; }

    /* Colonnes : superposées, une seule visible à la fois */
    .col-nav {
        position: absolute;
        top: 104px; /* 56px navbar + 48px app-header */
        left: 0;
        right: 0;
        bottom: 58px; /* espace pour la barre de sélection */
        background: var(--bg);
        display: none;
        z-index: 10;
        border-right: none;
    }

    .col-nav.visible { display: flex; }

    #col-exercices { background: var(--bg); }

    /* Panneau de sélection : drawer fixe en bas */
    .panneau-selection {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: auto;
        max-height: 68dvh;
        border-left: none;
        border-top: 1px solid var(--border);
        border-radius: 14px 14px 0 0;
        z-index: 20;
        transform: translateY(calc(100% - 58px));
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: 0 -4px 24px rgba(0, 0, 0, 0.35);
    }

    .panneau-selection.ouvert { transform: translateY(0); }

    .panneau-header {
        height: 58px;
        cursor: pointer;
        padding: 0 1.25rem;
    }

    .panneau-chevron { display: block; }

    .panneau-selection.ouvert .panneau-chevron { transform: rotate(180deg); }

    .panneau-body {
        max-height: calc(68dvh - 58px);
    }
}


/* ── Badges de contexte — masqués par défaut, visibles uniquement sur mobile via .visible ── */
.niveau-badge,
.theme-badge { display: none; }

@media (max-width: 768px) {
    .niveau-badge.visible {
        display: block;
        padding: 0.55rem 1rem;
        background: var(--primary-light);
        color: var(--primary);
        font-size: 0.82rem;
        font-weight: 700;
        cursor: pointer;
        border-bottom: 1px solid var(--border);
        flex-shrink: 0;
    }
    .niveau-badge.visible:active { opacity: 0.7; }

    .theme-badge.visible {
        display: flex;
        padding: 0.45rem 1rem;
        background: var(--primary-light);
        border-bottom: 1px solid var(--border);
        flex-shrink: 0;
        gap: 0.3rem;
        align-items: center;
        font-size: 0.8rem;
        flex-wrap: wrap;
    }

    .badge-retour-niveau,
    .badge-retour-theme {
        color: var(--primary);
        font-weight: 700;
        cursor: pointer;
    }
    .badge-retour-niveau:active,
    .badge-retour-theme:active { opacity: 0.7; }
}

/* ── Onglet Recherche ── */
.nav-recherche {
    font-weight: 600;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.2rem;
}

/* ── Search box (colonne thèmes en mode recherche) ── */
.search-box {
    display: none;
    padding: 0.6rem 0.75rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.search-box input {
    width: 100%;
    padding: 0.45rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    background: var(--bg);
    color: var(--text);
    font-size: 0.85rem;
    outline: none;
    transition: border-color 0.15s, box-shadow 0.15s;
}

.search-box input:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 2px var(--primary-light);
}

/* ── Drag sélection → extérieur (visual hint) ── */
body.sel-drag-active .panneau-selection {
    outline: 2px dashed var(--danger);
    outline-offset: -3px;
}

/* ── Touch drag clone (mobile) ── */
.touch-clone { transition: none; }

/* ── Panneau : touch drag hover ── */
.panneau-selection.drag-over-touch {
    box-shadow: 0 -4px 24px rgba(59, 130, 246, 0.5) !important;
    border-top-color: var(--primary) !important;
}

@media (max-width: 768px) {
    .panneau-selection.drag-over-touch {
        transform: translateY(0) !important;
    }
}


/* ============================================================
   PAGE RÉSULTAT
   ============================================================ */
body.page-resultat {
    min-height: 100vh;
}

.header-resultat {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0 2rem;
    height: 50px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 56px;
    z-index: 10;
}

.btn-retour {
    text-decoration: none;
    color: var(--primary);
    font-weight: 600;
    font-size: 0.875rem;
    white-space: nowrap;
}

.btn-retour:hover { text-decoration: underline; }

.header-resultat h1 { font-size: 1rem; font-weight: 700; }

.header-controls {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    margin-left: auto;
    flex-shrink: 0;
}

.header-resultat .compteur-blocs {
    font-size: 0.8rem;
    color: var(--text-muted);
    white-space: nowrap;
}

.svg-zoom-control {
    display: flex;
    align-items: center;
    gap: 0.35rem;
}

.zoom-a {
    color: var(--text-muted);
    font-weight: 700;
    line-height: 1;
    user-select: none;
}
.zoom-a-sm { font-size: 0.6rem; }
.zoom-a-lg { font-size: 1rem; }

.zoom-slider {
    width: 80px;
    accent-color: var(--primary);
    cursor: pointer;
}

.blocs-exercices {
    max-width: min(calc(var(--svg-zoom, 1) * 860px), calc(100vw - 2rem));
    margin: 1.5rem auto 3rem;
    padding: 0 1rem;
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.bloc-exercice {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 10px;
    overflow: hidden;
}

.bloc-header {
    padding: 0.65rem 1.25rem;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.bloc-header h2 { font-size: 0.88rem; font-weight: 700; }

.bloc-numero {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 500;
}

.bloc-enonce {
    padding: 1.25rem;
}

.bloc-enonce img {
    display: block;
    width: 100%;
    border-radius: 4px;
}

.placeholder-svg {
    background: var(--surface-2);
    border: 2px dashed var(--border);
    border-radius: var(--radius);
    padding: 2.5rem 1.5rem;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.82rem;
}

.bloc-footer {
    padding: 0.75rem 1.25rem;
    border-top: 1px solid var(--border);
    background: var(--surface-2);
}

.btn-correction {
    padding: 0.4rem 0.9rem;
    background: var(--success-light);
    color: var(--success);
    border: 1px solid var(--success-border);
    border-radius: 6px;
    cursor: pointer;
    font-size: 0.82rem;
    font-weight: 600;
    transition: background 0.12s;
}

.btn-correction:hover { filter: brightness(1.1); }

.correction-contenu {
    margin-top: 1rem;
    border-top: 1px solid var(--success-border);
    padding-top: 1rem;
}

.correction-contenu img {
    display: block;
    width: 100%;
    border-radius: 4px;
}

[data-theme="dark"] .bloc-enonce img,
[data-theme="dark"] .correction-contenu img {
    filter: invert(1);
}

.aucun-exercice {
    text-align: center;
    padding: 4rem 2rem;
    color: var(--text-muted);
}

@media (max-width: 600px) {
    .header-resultat  { padding: 0 0.75rem; gap: 0.5rem; }
    .blocs-exercices  { max-width: 100%; padding: 0 0.5rem; gap: 0.9rem; }
    .bloc-enonce      { padding: 0.75rem; }
    .bloc-footer      { padding: 0.6rem 0.75rem; }
    .svg-zoom-control { display: none; }
}
