/* ============================
   VARIABLES — palette reprise du logo MathsMDE (identique au reste du site)
============================ */
:root {
  --bleu-clair: #d0e4ff;
  --bleu-accent: #7fb3ff;
  --bleu-primaire: #0050a0;
  --bleu-fonce: #003a80;
  --bleu-marine: #002a60;
  --bleu-nuit: #001a40;

  --bg: #f4f8ff;
  --text: #1c2b3a;
  --text-muted: #5b6b7d;
  --card-bg: #fff;
  --shadow: rgba(0, 42, 96, 0.10);
  --shadow-strong: rgba(0, 42, 96, 0.18);
  --radius: 18px;
  --ok: #1c8a4d;
  --warn: #b4232b;
}

/* ============================
   RESET & BASE
============================ */
* { margin: 0; padding: 0; box-sizing: border-box; }
html { overflow-x: hidden; width: 100%; }

body {
  font-family: 'Inter', system-ui, sans-serif;
  background:
    radial-gradient(circle at 15% 0%, var(--bleu-clair) 0%, transparent 45%),
    radial-gradient(circle at 100% 20%, #eaf2ff 0%, transparent 40%),
    var(--bg);
  background-attachment: fixed;
  color: var(--text);
  min-height: 100vh;
  max-width: 100%;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  line-height: 1.55;
}

img, svg { max-width: 100%; height: auto; }

/* =========================================================
   CORRECTION KATEX — ne pas laisser la règle globale svg
   modifier les SVG utilisés par les flèches mathématiques
   (notamment \overrightarrow).
   ========================================================= */
.mcard .katex svg {
  max-width: none !important;
  max-height: none !important;
}

.mcard .katex .stretchy svg,
.mcard .katex .stretchy svg path {
  max-width: none !important;
}

.mcard .katex .stretchy {
  max-width: 100%;
  overflow: hidden;
}

.mcard .katex {
  color: inherit;
}
h1, h2, .brand-text { font-family: 'Lexend', 'Inter', sans-serif; color: var(--bleu-fonce); }
a { color: var(--bleu-primaire); }
a:hover { color: var(--bleu-fonce); }

/* ============================
   HEADER (identique au reste du site)
============================ */
header {
  position: fixed; top: 0; left: 0; right: 0;
  background: rgba(255, 255, 255, 0.85);
  backdrop-filter: blur(10px) saturate(160%);
  -webkit-backdrop-filter: blur(10px) saturate(160%);
  border-bottom: 1px solid rgba(0, 58, 128, 0.08);
  box-shadow: 0 2px 20px rgba(0, 42, 96, 0.06);
  padding: 12px 24px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px; z-index: 100;
}
header::after {
  content: ""; position: absolute; left: 0; right: 0; bottom: -3px; height: 3px;
  background: linear-gradient(90deg, var(--bleu-fonce), var(--bleu-accent), var(--bleu-clair));
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-text {
  font-size: 20px; font-weight: 700; color: var(--bleu-fonce);
  display: flex; flex-direction: column; line-height: 1.15;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.brand-text small {
  font-family: 'Inter', sans-serif; font-size: 12px; font-weight: 500;
  color: var(--text-muted); letter-spacing: 0.02em;
}
.logo img { height: 52px; width: auto; filter: drop-shadow(0 3px 8px rgba(0, 58, 128, 0.25)); }
.badge-ens {
  background: linear-gradient(135deg, var(--bleu-primaire), var(--bleu-fonce));
  color: #fff; padding: 6px 14px; border-radius: 999px;
  font-size: 13px; font-weight: 600; box-shadow: 0 4px 12px var(--shadow-strong);
  letter-spacing: 0.02em;
}

/* ============================
   MENU BOUTON + LATERAL (identique au reste du site)
============================ */
.menu-button {
  width: 44px; height: 44px; border-radius: 999px; border: none;
  background: linear-gradient(135deg, var(--bleu-primaire), var(--bleu-fonce));
  color: #fff; font-size: 20px; cursor: pointer;
  box-shadow: 0 6px 16px var(--shadow-strong); transition: transform 0.15s ease;
}
.menu-button:hover { transform: scale(1.06); }

#side-menu-overlay {
  position: fixed; inset: 0; background: rgba(0, 26, 64, 0.35);
  opacity: 0; pointer-events: none; transition: opacity 0.25s ease; z-index: 98;
}
#side-menu-overlay.visible { opacity: 1; pointer-events: auto; }

#side-menu {
  position: fixed; top: 0; right: -300px; width: 280px; height: 100vh;
  background: #fff; box-shadow: -8px 0 30px var(--shadow-strong);
  transition: right 0.3s cubic-bezier(.25,.8,.25,1);
  padding: 100px 20px 24px; z-index: 99; overflow-y: auto;
}
#side-menu.open { right: 0; }
#side-menu nav a {
  display: flex; align-items: center; gap: 12px; padding: 13px 14px; margin-bottom: 4px;
  text-decoration: none; color: var(--text); font-size: 15px; font-weight: 500;
  border-radius: 12px; transition: background 0.15s ease, color 0.15s ease;
}
#side-menu nav a .ic { font-size: 18px; }
#side-menu nav a:hover { background: var(--bleu-clair); color: var(--bleu-fonce); }
.side-menu-sep { height: 1px; background: #e6edf7; margin: 12px 4px; }
.side-menu-note { font-size: 13px; color: var(--text-muted); padding: 8px 14px; line-height: 1.5; }

/* ============================
   MAIN / LAYOUT
============================ */
.wrap { flex: 1; max-width: 1100px; width: 100%; margin: 0 auto; padding: 150px 20px 40px; position: relative; z-index: 1; }

h1 { font-size: 28px; font-weight: 800; margin-bottom: 8px; text-align: center; }
h2 { font-size: 19px; font-weight: 700; margin: 26px 0 10px; }
.lead { color: var(--text-muted); text-align: center; margin-bottom: 26px; }
.meta { color: var(--text-muted); font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; justify-content: center; flex-wrap: wrap; }

/* ============================
   CARTE (conteneur générique)
============================ */
.card {
  background: var(--card-bg); border: 1px solid rgba(0, 58, 128, 0.06);
  border-radius: var(--radius); padding: 22px 24px; box-shadow: 0 10px 28px var(--shadow);
}

/* ============================
   PANNEAU DE CONFIGURATION
============================ */
.setup {
  max-width: 520px; margin: 0 auto 24px; display: grid; gap: 14px;
}
.setup label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--text); }
.setup select {
  font: inherit; padding: 10px 13px; border: 1.5px solid #dde6f3; border-radius: 12px;
  background: #fff; color: var(--text); width: 100%;
}
.setup select:focus { outline: none; border-color: var(--bleu-primaire); }

/* ============================
   BOUTONS
============================ */
.btn {
  display: inline-block; padding: 12px 24px; border-radius: 999px; border: none;
  background: linear-gradient(135deg, var(--bleu-primaire), var(--bleu-fonce));
  color: #fff; font-weight: 700; cursor: pointer; font: inherit; text-decoration: none;
  box-shadow: 0 8px 20px var(--shadow-strong); transition: transform 0.15s ease;
}
.btn:hover { transform: translateY(-2px); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }
.btn.alt { background: var(--bleu-clair); color: var(--bleu-fonce); box-shadow: none; }
.btn.alt:hover { background: #b9d6ff; color: var(--bleu-fonce); }

/* ============================
   HUD
============================ */
.hud {
  display: flex; justify-content: center; gap: 22px; flex-wrap: wrap;
  background: #fff; border: 1px solid rgba(0, 58, 128, 0.06); border-radius: 999px;
  padding: 12px 20px; margin: 0 auto 24px; max-width: 640px;
  box-shadow: 0 6px 18px var(--shadow); font-size: 14px; font-weight: 600; color: var(--bleu-fonce);
}

/* ============================
   GRILLE DE CARTES (retournement 3D)
============================ */
.game {
  display: grid; gap: 12px; margin: 0 auto;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  -webkit-perspective: 1000px; perspective: 1000px;
}
.mcard { aspect-ratio: 3 / 4; cursor: pointer; }
.mcard .inner {
  position: relative; width: 100%; height: 100%;
  -webkit-transform-style: preserve-3d; transform-style: preserve-3d;
  transition: -webkit-transform 0.5s cubic-bezier(.25,.8,.25,1), transform 0.5s cubic-bezier(.25,.8,.25,1);
  will-change: transform;
}
.mcard.flipped .inner { -webkit-transform: rotateY(180deg); transform: rotateY(180deg); }
.mcard.found { pointer-events: none; }
.mcard .face {
  position: absolute; inset: 0;
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
  border-radius: 12px; display: flex; align-items: center; justify-content: center;
  padding: 8px; text-align: center; font-size: 13px; overflow: hidden;
  box-shadow: 0 6px 16px var(--shadow);
}
/* Double sécurité indépendante de backface-visibility (mal supporté par
   certains navigateurs) : on masque explicitement la face inactive via
   opacity + visibility, en plus de la rotation 3D. */
.mcard .face.back { opacity: 1; visibility: visible; }
.mcard.flipped .face.back { opacity: 0; visibility: hidden; }
.mcard .face.front { opacity: 0; visibility: hidden; }
.mcard.flipped .face.front { opacity: 1; visibility: visible; }
.mcard.found .face.front { opacity: 0.35; }
.mcard .face.back {
  background: linear-gradient(135deg, var(--bleu-primaire), var(--bleu-fonce));
  display: flex; align-items: center; justify-content: center;
}
.mcard .face.back img { width: 62%; filter: drop-shadow(0 3px 8px rgba(0,0,0,0.25)); }
.mcard .face.back .back-fallback { font-size: 30px; font-weight: 800; color: #fff; opacity: 0.85; font-family: 'Lexend', serif; }
.mcard .face.front {
  -webkit-transform: rotateY(180deg); transform: rotateY(180deg); background: #fff; color: var(--text);
  border: 1.5px solid var(--bleu-clair); line-height: 1.3;
}
.mcard .face.front.question { background: var(--bleu-clair); color: var(--bleu-fonce); font-weight: 600; }

/* Contenu de la carte : texte et formules sont séparés.
   Une formule occupe sa propre ligne et reste centrée. */
.mcard .face.front {
  padding: 12px;
  box-sizing: border-box;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.mcard .face.front .card-content {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  justify-content: center;
  gap: 0.65em;
  text-align: center;
  overflow: visible;
  overflow-wrap: break-word;
  word-break: normal;
  box-sizing: border-box;
}

/* Les phrases ont leur propre bloc : elles ne peuvent plus se coller
   directement à une formule. */
.mcard .face.front .card-text {
  width: 100%;
  text-align: center;
  line-height: 1.28;
  overflow-wrap: break-word;
  word-break: normal;
}

/* Chaque formule est un bloc indépendant. */
.mcard .face.front .card-math {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  line-height: 1.15;
  overflow: visible;
  box-sizing: border-box;
  padding: 0.12em 0;
}

.mcard .face.front .card-math .katex {
  display: inline-block;
  max-width: 100%;
  font-size: 1em;
  line-height: 1.15;
  text-align: center;
}

.mcard .face.front .card-math .katex-display {
  margin: 0 !important;
  max-width: 100%;
}

.mcard .face.front .card-math .katex-display > .katex {
  display: inline-block;
  max-width: 100%;
}

/* Les mots écrits avec \\text{...} restent lisibles. */
.mcard .face.front .card-math .katex .text {
  white-space: normal;
}

/* Cartes légèrement plus hautes pour améliorer la lisibilité. */
.mcard {
  aspect-ratio: 4 / 5;
  min-height: 175px;
}

@media (max-width: 640px) {
  .mcard {
    min-height: 150px;
  }

  .mcard .face.front {
    padding: 9px;
  }
}

/* ============================
   POPUP RÉSULTAT
============================ */
.popup-overlay {
  position: fixed; inset: 0; background: rgba(0, 26, 64, 0.45);
  display: none; align-items: center; justify-content: center; z-index: 200; padding: 20px;
}
.popup-overlay.visible { display: flex; }
.popup { max-width: 360px; width: 100%; text-align: center; }
.popup h2 { margin-bottom: 14px; }
.popup p { margin-bottom: 6px; font-weight: 600; }
.popup .row { margin-top: 16px; }

/* ============================
   TABLEAUX (résultats, suivi enseignant)
============================ */
.table { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid rgba(0, 58, 128, 0.06); border-radius: 14px; overflow: hidden; box-shadow: 0 6px 18px var(--shadow); font-size: 14px; }
.table th, .table td { padding: 10px 12px; border-bottom: 1px solid #eef2f8; text-align: left; }
.table thead { background: var(--bleu-clair); color: var(--bleu-fonce); }
.table thead th { font-weight: 700; }
.nowrap { white-space: nowrap; }
.scroll-x { overflow-x: auto; }

/* ============================
   FOOTER
============================ */
.foot { max-width: 1100px; margin: 0 auto; padding: 20px; text-align: center; color: var(--text-muted); font-size: 13px; border-top: 1px solid #e6edf7; }
.foot p { margin-bottom: 4px; }

@media (max-width: 640px) {
  .wrap { padding: 140px 14px 30px; }
  .game { grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); }
  .mcard .face { font-size: 11px; }
}
