/* ═══════════════════════════════════════════════════════════════════════════
   App-Cap — Couche de design unifiée (v1)
   Chargée en dernier : harmonise l'ensemble des espaces (Bootstrap + CSS d'espace) sans
   toucher à leur structure. Principes : surfaces douces, rayons cohérents, ombres légères,
   focus visibles, cibles tactiles confortables, aucun débordement horizontal sur mobile.
   ═══════════════════════════════════════════════════════════════════════════ */

:root {
  --ui-bg: #f4f6fa;
  --ui-surface: #ffffff;
  --ui-border: var(--ui-border);
  --ui-border-strong: #d3d9e6;
  --ui-text: #1f2a44;
  --ui-muted: #64748b;
  --ui-primary: var(--c-primary, #4f46e5);
  --ui-primary-rgb: var(--c-primary-rgb, 79, 70, 229);
  --ui-radius: 14px;
  --ui-radius-sm: 10px;
  --ui-shadow-xs: 0 1px 2px rgba(16, 24, 40, .05);
  --ui-shadow-sm: 0 1px 3px rgba(16, 24, 40, .06), 0 1px 2px rgba(16, 24, 40, .04);
  --ui-shadow-md: 0 8px 24px rgba(16, 24, 40, .08);
  --ui-ring: 0 0 0 3px rgba(var(--ui-primary-rgb), .22);
  --ui-font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --ui-safe-b: env(safe-area-inset-bottom, 0px);
}
[data-bs-theme="dark"] {
  --ui-bg: #0f1623;
  --ui-surface: #172033;
  --ui-border: #263149;
  --ui-border-strong: #33415c;
  --ui-text: #e6ebf5;
  --ui-muted: #94a3b8;
  --ui-shadow-sm: 0 1px 3px rgba(0, 0, 0, .35);
  --ui-shadow-md: 0 8px 24px rgba(0, 0, 0, .4);
}

/* ── Base ─────────────────────────────────────────────────────────────────── */
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
html body {
  font-family: var(--ui-font);
  -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}
html[dir="rtl"] body, html[lang="ar"] body { font-family: 'Cairo', var(--ui-font); }
html body:not(.formateur-page):not(.exam-page) .app-main { background: var(--c-content-bg, var(--ui-bg)); }
::selection { background: rgba(var(--ui-primary-rgb), .22); }
img, video, canvas, svg { max-width: 100%; }
h1, h2, h3, h4, h5, h6 { letter-spacing: -.01em; }

/* Focus clavier cohérent et visible */
html body :is(a, button, input, select, textarea, summary, [tabindex]):focus-visible {
  outline: 2px solid var(--ui-primary); outline-offset: 2px; border-radius: 6px;
}
html body :is(.form-control, .form-select, .form-check-input):focus-visible { outline: none; }

/* Barres de défilement fines */
* { scrollbar-width: thin; scrollbar-color: var(--ui-border-strong) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--ui-border-strong); border-radius: 8px; }
*::-webkit-scrollbar-track { background: transparent; }

/* ── En-tête & navigation ────────────────────────────────────────────────── */
html body .app-header {
  background: rgba(255, 255, 255, .86);
  -webkit-backdrop-filter: saturate(180%) blur(12px); backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--ui-border);
  box-shadow: none;
}
[data-bs-theme="dark"] html body .app-header, html[data-bs-theme="dark"] body .app-header { background: rgba(23, 32, 51, .86); }

html body .app-sidebar {
  background: linear-gradient(180deg, #1b2740 0%, #243352 100%);
  box-shadow: 1px 0 0 rgba(255, 255, 255, .04), 4px 0 24px rgba(15, 23, 42, .12);
}
html body .app-sidebar .nav-link { border-radius: 10px; margin: 2px 8px; transition: background .15s, color .15s; }
html body .app-sidebar .nav-link:hover { background: rgba(255, 255, 255, .08); }
html body .app-sidebar .nav-link.active { background: rgba(var(--ui-primary-rgb), .22); box-shadow: inset 3px 0 0 var(--ui-primary); }

/* ── Cartes ──────────────────────────────────────────────────────────────── */
html body .card {
  background: var(--ui-surface);
  border: 1px solid var(--ui-border);
  border-radius: var(--ui-radius);
  box-shadow: var(--ui-shadow-xs);
}
html body .card-header {
  background: transparent; border-bottom: 1px solid var(--ui-border);
  border-radius: var(--ui-radius) var(--ui-radius) 0 0;
}
html body .card-footer { background: transparent; border-top: 1px solid var(--ui-border); }
html body a.card, html body .card.card-hover, html body .card[onclick] { transition: transform .15s ease, box-shadow .15s ease; cursor: pointer; }
html body a.card:hover, html body .card.card-hover:hover, html body .card[onclick]:hover { transform: translateY(-2px); box-shadow: var(--ui-shadow-md); }

/* ── Boutons ─────────────────────────────────────────────────────────────── */
html body .btn {
  border-radius: var(--ui-radius-sm); font-weight: 600; letter-spacing: .005em;
  transition: background-color .15s, border-color .15s, box-shadow .15s, transform .1s, color .15s;
}
html body .btn:active:not(:disabled) { transform: translateY(1px); }
html body .btn-primary {
  background: linear-gradient(180deg, var(--c-primary-light, var(--ui-primary-solid)), var(--ui-primary));
  border-color: var(--ui-primary); box-shadow: 0 1px 2px rgba(var(--ui-primary-rgb), .35);
}
html body .btn-primary:hover:not(:disabled) { filter: brightness(.96); box-shadow: 0 4px 12px rgba(var(--ui-primary-rgb), .3); }
html body .btn:disabled, html body .btn.disabled { opacity: .55; cursor: not-allowed; }
html body .btn-group > .btn { border-radius: 0; }
html body .btn-group > .btn:first-child { border-radius: var(--ui-radius-sm) 0 0 var(--ui-radius-sm); }
html body .btn-group > .btn:last-child { border-radius: 0 var(--ui-radius-sm) var(--ui-radius-sm) 0; }

/* ── Formulaires ─────────────────────────────────────────────────────────── */
html body :is(.form-control, .form-select) {
  border-radius: var(--ui-radius-sm); border-color: var(--ui-border-strong);
  background-color: var(--ui-surface); color: var(--ui-text);
  transition: border-color .15s, box-shadow .15s;
}
html body :is(.form-control, .form-select):focus { border-color: var(--ui-primary); box-shadow: var(--ui-ring); }
html body .form-label { font-weight: 600; color: var(--ui-text); }
html body .form-check-input:checked { background-color: var(--ui-primary); border-color: var(--ui-primary); }
html body .input-group > .form-control:not(:last-child), html body .input-group > .form-select:not(:last-child) { border-top-right-radius: 0; border-bottom-right-radius: 0; }

/* ── Tableaux ────────────────────────────────────────────────────────────── */
html body .table { --bs-table-bg: transparent; color: var(--ui-text); }
html body .table > thead > tr > th {
  font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  color: var(--ui-muted); border-bottom-color: var(--ui-border-strong); white-space: nowrap;
}
html body .table > tbody > tr > td { border-color: var(--ui-border); vertical-align: middle; }
html body .table-hover > tbody > tr:hover > * { --bs-table-hover-bg: rgba(var(--ui-primary-rgb), .05); }
/* Enveloppe injectée par ui.js : défilement horizontal propre au lieu d'un débordement de page */
.ui-table-scroll { width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; }

/* ── Badges, alertes, listes, onglets ────────────────────────────────────── */
html body .badge { border-radius: 999px; font-weight: 600; letter-spacing: .01em; }
html body .alert { border-radius: var(--ui-radius-sm); border-width: 1px; }
html body .list-group-item { border-color: var(--ui-border); }
html body .nav-tabs { border-bottom-color: var(--ui-border); }
html body .nav-tabs .nav-link { border-radius: var(--ui-radius-sm) var(--ui-radius-sm) 0 0; font-weight: 600; }
html body .nav-pills .nav-link { border-radius: 999px; font-weight: 600; }
html body .progress { border-radius: 999px; background: var(--ui-border); }
html body .progress-bar { border-radius: 999px; }

/* ── Fenêtres modales & menus déroulants ─────────────────────────────────── */
html body .modal-content { border: 1px solid var(--ui-border); border-radius: calc(var(--ui-radius) + 2px); box-shadow: var(--ui-shadow-md); background: var(--ui-surface); }
html body .modal-header, html body .modal-footer { border-color: var(--ui-border); }
html body .dropdown-menu { border: 1px solid var(--ui-border); border-radius: var(--ui-radius-sm); box-shadow: var(--ui-shadow-md); padding: .35rem; }
html body .dropdown-item { border-radius: 8px; }
html body .tooltip-inner { border-radius: 8px; }
html body .offcanvas { border-color: var(--ui-border); }

/* ── Gabarits de page ────────────────────────────────────────────────────── */
html body .app-main .container-page { max-width: 1480px; margin-inline: auto; }
html body .page-title, html body .app-main h1.h3, html body .app-main h1.h4 { font-weight: 800; }

/* Animation d'entrée discrète des pages */
@media (prefers-reduced-motion: no-preference) {
  html body .app-main > * { animation: ui-fade-in .25s ease-out both; }
  @keyframes ui-fade-in { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; } }

/* ═══════════════════════════════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════════════════════════════ */

/* Tablettes et en dessous */
@media (max-width: 991.98px) {
  html body .app-main .container-page { padding-inline: 1rem; }
  /* Rangées d'actions d'en-tête : retour à la ligne plutôt que débordement */
  html body :is(.header-actions, .page-actions, .toolbar, .d-flex.justify-content-between) { flex-wrap: wrap; gap: .5rem; }
  html body .header-actions { max-width: 100%; }
}

/* Mobiles */
@media (max-width: 767.98px) {
  html body { font-size: .95rem; }
  html body .app-main { overflow-x: hidden; }
  html body .app-main .container-page, html body .container, html body .container-fluid { padding-inline: .75rem; }
  html body .row { --bs-gutter-x: .75rem; }
  html body .card { border-radius: 12px; }
  html body .card-body { padding: .9rem; }
  html body h1, html body .h1 { font-size: 1.45rem; }
  html body h2, html body .h2 { font-size: 1.25rem; }
  html body h3, html body .h3 { font-size: 1.12rem; }

  /* 16 px minimum dans les champs : empêche le zoom automatique d'iOS ; cibles tactiles ≥ 40 px */
  html body :is(input:not([type=checkbox]):not([type=radio]):not([type=range]), select, textarea, .form-control, .form-select) { font-size: 16px; min-height: 42px; }
  html body .btn { min-height: 40px; }
  html body .btn-sm { min-height: 36px; }
  html body .dropdown-item { padding-block: .6rem; }

  /* Fenêtres modales : quasi plein écran, contenu défilant */
  html body .modal-dialog { margin: .5rem; max-width: calc(100vw - 1rem); }
  html body .modal-dialog:not(.modal-dialog-centered) { margin-top: .75rem; }
  html body .modal-content { max-height: calc(100dvh - 1.5rem); }
  html body .modal-body { overflow-y: auto; }
  html body .modal-dialog.modal-lg, html body .modal-dialog.modal-xl { max-width: calc(100vw - 1rem); }

  /* Menus déroulants contenus dans l'écran */
  html body .dropdown-menu { max-width: calc(100vw - 1.5rem); }

  /* Grilles : une colonne pour les blocs larges */
  html body .row > [class*="col-md-"], html body .row > [class*="col-lg-"], html body .row > [class*="col-xl-"] { width: 100%; }
  html body .row > .col-6, html body .row > [class*="col-sm-6"] { width: 50%; }

  /* Zone libre sous la barre de navigation basse */
  html body { padding-bottom: calc(var(--ui-safe-b) + 4px); }
}

/* Très petits écrans */
@media (max-width: 400px) {
  html body .row > .col-6 { width: 100%; }
  html body .btn-group { flex-wrap: wrap; }
}

/* Écrans larges : contenu centré, jamais étiré à l'infini */
@media (min-width: 1700px) { html body .app-main .container-page { max-width: 1600px; } }

/* Impression */
@media print {
  .app-sidebar, .app-header, .mobile-bottom-nav, .fm-mobile-nav, .btn { display: none !important; }
  html body .app-main { margin: 0 !important; padding: 0 !important; }
  html body .card { box-shadow: none; border-color: var(--ui-border); }
}

/* ── Mobile : en-têtes d'espace, barres de navigation défilantes, tableaux ─── */
@media (max-width: 991.98px) {
  html body .app-header { flex-wrap: nowrap; gap: 6px; padding-inline: 8px; overflow: hidden; }
  /* Rangée de boutons de navigation (espaces Formateur, Préparation, Paramètres…) : défilement horizontal */
  html body .app-header > .d-flex.flex-grow-1, html body .app-header > .flex-grow-1 {
    min-width: 0; flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
  }
  html body .app-header > .d-flex.flex-grow-1::-webkit-scrollbar { display: none; }
  html body .app-header .nav-btn, html body .app-header .fm-hdr-btn { flex: 0 0 auto !important; min-width: 44px; }
  html body .app-header > :not(.flex-grow-1) { flex-shrink: 0; }
  html body .nav-tooltip { display: none !important; }
}
@media (max-width: 575.98px) {
  html body .app-header :is(.header-lang-adminty, .lang-toggle-desktop, .header-search-adminty) { display: none !important; }
}
@media (max-width: 767.98px) {
  html body .ui-table-scroll :is(td, th) { white-space: nowrap; }
  html body .ui-table-scroll { border-radius: 10px; }
  /* En-têtes de page (titre + actions) : empilés proprement */
  html body .app-main :is(.header-actions) { width: 100%; }
  html body .app-main .d-flex.justify-content-between.align-items-center { flex-wrap: wrap; gap: .5rem; }
}

/* ── Corrections de gabarit sur tablettes/mobiles ─────────────────────────── */
@media (max-width: 991.98px) {
  /* Administration : la barre latérale secondaire est masquée, le contenu reprend toute la largeur */
  html body.admin-page .app-main, html body.admin-page.main-sidebar-mini .app-main, html body.admin-page.sidebar-pinned .app-main { margin-left: 0; }
  html body.admin-page .app-header, html body.admin-page.main-sidebar-mini .app-header, html body.admin-page.sidebar-pinned .app-header { left: 0; }
  /* Bandeaux de titre collants avec marges négatives codées en dur : ils ne doivent jamais élargir la page */
  html body header.sticky-top[style*="margin-left:-1rem"] { margin-left: 0 !important; margin-right: 0 !important; }
  /* Marges négatives des .row dans un conteneur sans padding : on coupe sans créer de défilement */
  html body .app-main { overflow-x: clip; }
}

/* Aucune page ne doit défiler horizontalement (rien n'est coupé : seuls des débordements de gouttière, invisibles) */
html body .app-main { overflow-x: clip; }
html, body { max-width: 100%; overflow-x: clip; }

/* Rangées de navigation d'en-tête (Pédagogie, Préparation…) : une seule ligne, défilement horizontal si l'espace manque */
html body .app-header > .d-flex.flex-grow-1, html body .app-header > .flex-grow-1 {
  min-width: 0; flex-wrap: nowrap; overflow-x: auto; overflow-y: visible; scrollbar-width: none;
}
html body .app-header > .d-flex.flex-grow-1::-webkit-scrollbar { display: none; }
html body .app-header > .d-flex.flex-grow-1 > * { flex-shrink: 0; white-space: nowrap; }

/* Préparation : le titre de page ne doit pas coller au bord de la barre latérale */
html body.preparation-page header.sticky-top[style*="margin-left:-1rem"] { padding-left: 1.5rem !important; padding-right: 1.5rem !important; }
