/* =====================================================================
   Formules Survey Sampling — styles (mobile d'abord, thème clair/sombre)
   ===================================================================== */
:root {
  --bg: #f6f3ea;
  --bg-grid: rgba(36, 70, 168, 0.07);
  --surface: #fffdf8;
  --surface-2: #f0ece0;
  --ink: #1b2233;
  --muted: #5d6475;
  --line: #ddd6c4;
  --accent: #2446a8;
  --accent-ink: #ffffff;
  --accent-soft: #e3e9fb;
  --red: #c8324d;
  --ok: #1d7a4b;
  --ok-soft: #dff3e7;
  --ko: #b3261e;
  --ko-soft: #fbe3e1;
  --warn: #b26b00;
  --general: #2446a8;
  --srswor: #0f7a6c;
  --bernoulli: #9a3fb5;
  --shadow: 0 1px 2px rgba(27, 34, 51, 0.06), 0 4px 14px rgba(27, 34, 51, 0.07);
  --radius: 14px;
  --tap: 48px;
  color-scheme: light;
}
/* Thème clair par défaut ; le thème sombre s'active avec le bouton (lune) en haut à droite. */
:root[data-theme="dark"] {
  --bg: #11151f; --bg-grid: rgba(140, 170, 255, 0.05); --surface: #1a2030; --surface-2: #232a3d;
  --ink: #e8ebf3; --muted: #9aa3b8; --line: #2f3850; --accent: #8fa8ff; --accent-ink: #0d1220;
  --accent-soft: #26305a; --red: #ff7a93; --ok: #5fd39a; --ok-soft: #173a2a; --ko: #ff8a80; --ko-soft: #432022;
  --general: #8fa8ff; --srswor: #4fd1bd; --bernoulli: #d59bf0;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 6px 18px rgba(0, 0, 0, 0.25);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--bg);
  background-image: linear-gradient(var(--bg-grid) 1px, transparent 1px), linear-gradient(90deg, var(--bg-grid) 1px, transparent 1px);
  background-size: 22px 22px;
  color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.center { text-align: center; }
.wrap { width: 100%; max-width: 760px; margin: 0 auto; padding: 0 16px; }

/* ---------- barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  height: 56px; padding: 0 8px; padding-top: env(safe-area-inset-top);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 {
  flex: 1; margin: 0 0 0 8px; font: 700 1.12rem/1.2 Georgia, "Times New Roman", serif; letter-spacing: 0.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topbar .icon-btn:not([hidden]) + h1 { margin-left: 0; }
.icon-btn {
  width: var(--tap); height: var(--tap); display: grid; place-items: center;
  border: 0; border-radius: 12px; background: transparent; color: var(--ink);
}
.icon-btn:hover { background: var(--surface-2); }
main { padding: 16px 0 40px; outline: none; }

/* ---------- boutons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 10px 18px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--surface); color: var(--ink);
  font-weight: 600; text-decoration: none; box-shadow: var(--shadow);
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.big { width: 100%; font-size: 1.1rem; min-height: 56px; margin-top: 8px; }
.btn.small { min-height: 44px; padding: 8px 14px; font-size: 0.95rem; }
.btn.danger { color: var(--ko); border-color: color-mix(in srgb, var(--ko) 40%, var(--line)); }
.row-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 12px 0 8px; }
.hint { font-size: 0.88rem; color: var(--muted); margin: 6px 0 0; }
.lead { color: var(--muted); margin: 0 0 16px; }
.h2 { font: 700 1.15rem/1.3 Georgia, serif; margin: 26px 0 10px; display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; }
.h2-def { font: 400 0.95rem system-ui, sans-serif; color: var(--muted); }

/* ---------- formules ---------- */
.fx {
  position: relative; /* contient le MathML absolu de KaTeX dans le conteneur défilant */
  max-width: 100%; overflow-x: auto; overflow-y: hidden;
  font-size: 1.08rem; padding: 2px 0; -webkit-overflow-scrolling: touch; scrollbar-width: thin;
}
.fx .katex-display { margin: 0.25em 0; text-align: left; }
.fx .katex-display > .katex { text-align: left; }
.fx-big { font-size: 1.28rem; }
.fx-big .katex-display > .katex { text-align: center; }
.fx-big .katex-display { text-align: center; }
.itex .katex { font-size: 1.05em; }
.tag {
  display: inline-block; font-size: 0.72rem; font-weight: 700; letter-spacing: 0.02em; text-transform: uppercase;
  padding: 2px 7px; border-radius: 999px; vertical-align: 0.15em;
  color: var(--c, var(--accent)); background: color-mix(in srgb, var(--c, var(--accent)) 14%, transparent);
}
.tag-general { --c: var(--general); }
.tag-srswor { --c: var(--srswor); }
.tag-bernoulli { --c: var(--bernoulli); }

/* ---------- accueil ---------- */
.hero {
  background: var(--surface); border: 1px solid var(--line); border-left: 5px solid var(--red);
  border-radius: var(--radius); padding: 18px 18px 10px; box-shadow: var(--shadow);
}
.hero-k { margin: 0; color: var(--red); font-weight: 700; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.08em; }
.hero-t { margin: 4px 0 6px; font: 700 1.5rem/1.2 Georgia, serif; }
.hero-f { color: var(--accent); }
.hero-f .fx { font-size: 1.3rem; }
.pseudo { margin: 16px 0; }
.pseudo label, .pseudo-inline label { display: block; font-weight: 600; margin-bottom: 6px; }
input {
  width: 100%; min-height: var(--tap); padding: 10px 14px; border-radius: 12px;
  border: 1.5px solid var(--line); background: var(--surface); font-size: 1rem;
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.tiles { display: grid; gap: 10px; grid-template-columns: 1fr 1fr; }
.tile {
  display: flex; flex-direction: column; gap: 4px; min-height: 124px; padding: 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  color: var(--ink); text-decoration: none; box-shadow: var(--shadow);
}
.tile:active { transform: scale(0.985); }
.tile-i { font-size: 1.5rem; }
.tile-t { font-weight: 700; font-size: 1.05rem; }
.tile-d { font-size: 0.85rem; color: var(--muted); line-height: 1.35; }
.foot { font-size: 0.85rem; margin-top: 20px; }

/* ---------- réglages ---------- */
.seg-field { border: 0; padding: 0; margin: 0 0 18px; }
.seg-field legend { font-weight: 700; margin-bottom: 8px; padding: 0; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg-btn {
  min-height: 44px; padding: 8px 14px; border-radius: 999px;
  border: 1.5px solid var(--line); background: var(--surface); font-weight: 600; font-size: 0.95rem;
}
.seg-btn.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- HUD ---------- */
.hud {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  font-variant-numeric: tabular-nums; font-weight: 600; margin-bottom: 10px;
}
.hud-time { font-size: 1.05rem; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); }
.hud-score { text-align: right; }
.hud-score b { font-size: 1.15rem; }
.hud-combo { display: block; font-style: normal; font-size: 0.78rem; color: var(--warn); min-height: 1em; }
.lives { color: var(--red); letter-spacing: 1px; margin-left: 6px; }
.lives .lost { opacity: 0.22; }
.qbar { height: 6px; border-radius: 99px; background: var(--surface-2); overflow: hidden; margin-bottom: 12px; }
.qbar-fill { height: 100%; background: var(--accent); transform-origin: left; transition: transform 0.1s linear; }
.qbar.urgent .qbar-fill { background: var(--red); }

/* ---------- questions ---------- */
.q-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 14px 16px; box-shadow: var(--shadow); margin-bottom: 12px;
}
.q-kicker { margin: 0 0 4px; color: var(--muted); font-size: 0.9rem; }
.q-ask { margin: 0; font-size: 1.2rem; font-weight: 700; }
.q-ask.small { font-size: 1rem; margin: 14px 0 8px; }
.options, .step2-opts .options { display: grid; gap: 10px; }
.opt {
  display: block; width: 100%; min-height: 60px; padding: 8px 14px; text-align: left;
  background: var(--surface); border: 2px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.opt:not(:disabled):active { border-color: var(--accent); }
.opt:disabled { cursor: default; }
.opt.right { border-color: var(--ok); background: var(--ok-soft); }
.opt.wrong { border-color: var(--ko); background: var(--ko-soft); }
.opt:disabled:not(.right):not(.wrong) { opacity: 0.6; }
.opt-name { font-size: 1.12rem; display: inline-flex; align-items: center; gap: 8px; min-height: 40px; }
.tf { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tf-btn { text-align: center; font-weight: 700; font-size: 1.1rem; min-height: 64px; }
.q-feedback { margin-top: 12px; }
.fb { border-radius: 12px; padding: 12px 14px; }
.fb-ok { background: var(--ok-soft); color: var(--ok); }
.fb-ko { background: var(--ko-soft); }
.fb-ko > b { color: var(--ko); }
.fb-why { margin: 6px 0 0; color: var(--ink); }
.fb-good { margin: 8px 0 0; font-weight: 600; font-size: 0.9rem; color: var(--muted); }
.q-feedback .next { width: 100%; margin-top: 12px; }

/* ---------- la grille ---------- */
.grille .hud { margin-bottom: 8px; }
.board { display: grid; gap: 14px; padding: 0 16px 16px; max-width: 1280px; margin: 0 auto; }
.plan-col { display: flex; flex-direction: column; gap: 10px; }
.plan-h {
  margin: 0; font: 700 1.05rem Georgia, serif; color: var(--c); padding: 8px 12px; border-radius: 10px;
  background: color-mix(in srgb, var(--c) 12%, var(--surface));
}
.param-block { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px; display: grid; gap: 8px; }
.param-h { margin: 0; font-weight: 700; font-size: 0.9rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.slot {
  display: flex; flex-direction: column; align-items: stretch; gap: 2px; width: 100%; min-height: 56px;
  padding: 6px 10px; text-align: left; border-radius: 10px; border: 2px dashed var(--line); background: var(--surface-2);
}
.slot-lbl { font-size: 0.8rem; font-weight: 700; color: var(--muted); }
.slot-body { min-height: 20px; }
.slot:not(.done) .slot-body::after { content: "…"; color: var(--line); font-weight: 700; }
.board.armed .slot:not(.done) { border-color: var(--accent); background: var(--accent-soft); }
.slot.over { border-style: solid; border-color: var(--accent); }
.slot.done { border-style: solid; border-color: var(--ok); background: var(--ok-soft); cursor: default; }
.slot.bad { animation: bad 0.5s; }
@keyframes bad { 0%, 100% { border-color: var(--line); } 20%, 60% { border-color: var(--ko); background: var(--ko-soft); } }
.tray {
  position: sticky; bottom: 0; z-index: 10; padding: 8px 0 calc(10px + env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  border-top: 1px solid var(--line);
}
.tray-hint { margin: 0 16px 6px; font-size: 0.88rem; font-weight: 600; color: var(--muted); }
.tray-hint.flash { animation: flash 0.6s; }
@keyframes flash { 30% { color: var(--red); } }
.tray-cards {
  position: relative; display: flex; gap: 8px; overflow-x: auto; padding: 2px 16px 6px; scroll-snap-type: x proximity; scroll-padding: 0 16px;
  -webkit-overflow-scrolling: touch;
}
.gcard {
  flex: 0 0 auto; width: min(78vw, 300px); min-height: 64px; padding: 6px 10px; text-align: left;
  scroll-snap-align: start; background: var(--surface); border: 2px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
}
.gcard .fx { font-size: 1rem; }
.gcard.sel { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 35%, transparent); }
.gcard.shake { animation: shake 0.4s; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }

/* ---------- fiche ---------- */
.tabs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-bottom: 10px; }
.tab {
  min-height: 44px; padding: 6px 4px; border-radius: 999px; border: 1.5px solid var(--line);
  background: var(--surface); font-weight: 700; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tab.on { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.fiche-tools { display: flex; gap: 8px; margin-bottom: 6px; }
.plan-intro {
  margin-top: 8px; padding: 12px 14px; border-radius: var(--radius); background: var(--surface);
  border: 1px solid var(--line); border-left: 5px solid var(--c);
}
.plan-title { margin: 0; font: 700 1.2rem Georgia, serif; color: var(--c); }
.plan-intro .muted { margin: 2px 0 6px; font-size: 0.9rem; }
.defs { display: flex; flex-wrap: wrap; gap: 6px 18px; }
.def { overflow-x: auto; max-width: 100%; }
.fgrid { display: grid; gap: 10px; grid-template-columns: minmax(0, 1fr); }
.fcard {
  min-width: 0; position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 10px 14px 12px; box-shadow: var(--shadow);
}
.fcard-h { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.fcard-type { font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.mini {
  min-height: 44px; min-width: 44px; padding: 6px 12px; margin: -6px -8px -6px 0; border: 0; border-radius: 10px;
  background: transparent; color: var(--accent); font-weight: 700; font-size: 0.9rem;
}
.fcard-main { display: flex; align-items: center; gap: 8px; margin-top: 2px; }
.fcard-lhs { flex: 0 0 auto; font-size: 1.12rem; white-space: nowrap; }
.fcard-rhs { flex: 1 1 auto; min-width: 0; }
.fcard-lhs span { color: var(--muted); }
.fnote { font-size: 0.9rem; color: var(--muted); margin-top: 4px; overflow-x: auto; }
.fixed {
  margin-top: 8px; font-size: 0.85rem; padding: 6px 10px; border-radius: 8px;
  background: color-mix(in srgb, var(--warn) 12%, transparent); color: var(--ink);
}
.fixed b { color: var(--warn); margin-right: 4px; }
.cover { display: none; }
.fcard.hidden .fcard-rhs, .fcard.hidden .fcard-notes { display: none; }
.fcard.hidden .cover {
  display: grid; place-items: center; flex: 1 1 auto; min-height: 56px;
  border: 2px dashed var(--line); border-radius: 10px; background: var(--surface-2); color: var(--muted); font-weight: 600;
}

/* ---------- résultat ---------- */
.score-card {
  position: relative; overflow: hidden; margin-top: 4px; padding: 18px 18px 14px; border-radius: 20px;
  background: linear-gradient(160deg, var(--accent) 0%, color-mix(in srgb, var(--accent) 55%, var(--bernoulli)) 100%);
  color: #fff; box-shadow: 0 10px 30px color-mix(in srgb, var(--accent) 35%, transparent);
}
:root[data-theme="dark"] .score-card, :root[data-theme="dark"] .score-card * { color: #0d1220; }
.score-card::after {
  content: "π"; position: absolute; right: -10px; top: -34px; font: 700 170px/1 Georgia, serif; opacity: 0.12; pointer-events: none;
}
.sc-head { display: flex; align-items: center; gap: 10px; }
.sc-icon { font-size: 1.7rem; width: 46px; height: 46px; display: grid; place-items: center; border-radius: 12px; background: rgba(255, 255, 255, 0.2); }
.sc-game { margin: 0; font: 700 1.2rem/1.2 Georgia, serif; }
.sc-variant { margin: 0; font-size: 0.88rem; opacity: 0.9; }
.sc-pseudo { margin: 14px 0 0; font-weight: 700; font-size: 1.15rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sc-score { margin: 0; display: flex; align-items: baseline; gap: 8px; }
.sc-score b { font-size: 3.4rem; line-height: 1.05; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.sc-score span { font-weight: 600; opacity: 0.9; }
.sc-badge { display: inline-block; margin: 4px 0 0; padding: 3px 10px; border-radius: 99px; background: rgba(255, 255, 255, 0.22); font-weight: 700; font-size: 0.9rem; }
.sc-stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 14px 0 8px; }
.sc-stats div { background: rgba(255, 255, 255, 0.16); border-radius: 12px; padding: 8px 12px; }
.sc-stats dt { font-size: 0.78rem; opacity: 0.9; }
.sc-stats dd { margin: 0; font-size: 1.3rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.sc-extra { margin: 0 0 4px; font-size: 0.82rem; opacity: 0.92; }
.sc-date { margin: 4px 0 0; font-size: 0.82rem; opacity: 0.9; }
.pseudo-inline { margin: 8px 0; }
.pseudo-inline div { display: flex; gap: 8px; }
.pseudo-inline .btn { flex: 0 0 auto; }
.missed { display: grid; gap: 10px; }
.miss { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
.miss-name { margin: 0 0 4px; }
.miss-name .muted { display: block; font-size: 0.85rem; }
.miss-lbl { margin: 6px 0 0; font-size: 0.78rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; }
.miss-lbl.ok { color: var(--ok); }
.miss-lbl.ko { color: var(--ko); }
.fx-ko { opacity: 0.8; text-decoration: line-through; text-decoration-color: color-mix(in srgb, var(--ko) 60%, transparent); }
.miss-why { margin: 6px 0 0; font-size: 0.9rem; color: var(--muted); }

/* ---------- records ---------- */
.rec { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; margin-bottom: 8px; }
.rec-var { margin: 0 0 4px; font-weight: 700; }
.rec-list { margin: 0; padding-left: 22px; font-variant-numeric: tabular-nums; }
.rec-list li { margin: 2px 0; }
.records .danger { margin-top: 24px; }

/* ---------- écrans larges ---------- */
@media (min-width: 700px) {
  .tiles { grid-template-columns: repeat(3, 1fr); }
  .fgrid { grid-template-columns: repeat(3, minmax(0, 1fr)); align-items: start; }
  .fiche .wrap, .fiche.wrap { max-width: 1180px; }
  .options { grid-template-columns: 1fr 1fr; }
  .quiz.wrap { max-width: 860px; }
}
@media (min-width: 980px) {
  .board-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .board-1 { max-width: 760px; }
  .tray-cards { flex-wrap: wrap; overflow-x: hidden; max-height: 30vh; overflow-y: auto; max-width: 1280px; margin: 0 auto; justify-content: center; }
  .tray-hint { max-width: 1248px; margin: 0 auto 6px; }
  .gcard { width: 250px; min-height: 56px; }
  .gcard .fx { font-size: 0.95rem; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
