/* ============================================================
   Futsal Coach — thème sombre, mobile-first, contraste gymnase
   Étape ① : socle (en-tête, vues, barre d'onglets, réglages)
   ============================================================ */

:root {
  --bg:        #0f1419;   /* fond principal, sombre */
  --bg-2:      #1a2028;   /* cartes, surfaces */
  --bg-3:      #232c37;   /* survols, bordures */
  --txt:       #e8edf2;   /* texte principal */
  --txt-dim:   #9aa7b4;   /* texte secondaire */
  --accent:    #2dd4a7;   /* teal maison (PAS la charte de l'app iOS) */
  --accent-2:  #ffb020;   /* jaune d'appui (alertes douces) */
  --danger:    #ef5350;
  --tabbar-h:  64px;
  --header-h:  52px;
  /* zones sûres (encoches iPhone) */
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--txt);
  /* typographie système, aucune webfont */
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;        /* hauteur ferme : les vues gèrent leur propre scroll */
  overflow: hidden;      /* pas de scroll du body ; la barre d'onglets flotte au-dessus */
}

/* ---------- En-tête ---------- */
.app-header {
  height: var(--header-h);
  padding: calc(var(--safe-t) + 4px) 16px 0;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-2);
  border-bottom: 1px solid var(--bg-3);
}
.app-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: 0.3px;
}
.app-title::before {
  content: "⚽";
  margin-right: 8px;
}
.offline-badge {
  margin-left: auto;
  font-size: 11px;
  font-weight: 600;
  color: var(--bg);
  background: var(--accent-2);
  padding: 3px 8px;
  border-radius: 999px;
}

/* ---------- Vues ---------- */
.view {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px;
  /* on laisse la place à la barre d'onglets */
  padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px);
}
.view[hidden] { display: none; }

/* Vue Studio : le stage occupe tout l'espace dispo (au-dessus de la barre d'onglets). */
.view-studio {
  display: flex;
  overflow: hidden;
  padding: 8px;
  padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 8px);
}
.studio-stage {
  position: relative;   /* référence pour l'overlay d'export (inset:0) */
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;   /* plateau (terrain) au-dessus, timeline en dessous */
}
/* Plateau : reçoit le canvas. flex:1 → il prend toute la hauteur laissée libre
   par la timeline. Le canvas absolu le remplit → clientH = hauteur réellement
   disponible pour le terrain (l'échelle repart donc de cette valeur). */
.studio-plateau {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  min-width: 0;
}
/* Toile : conteneur du canvas. Absolue et pleine dans le plateau → la géométrie du
   canvas est EXACTEMENT celle d'avant (plateau relatif → toile inset:0 → canvas inset:0).
   Sur écran étroit elle repasse en flux (item flex) pour laisser la barre d'outils
   au-dessus du terrain sans la recouvrir. */
.studio-toile {
  position: absolute;
  inset: 0;
}
.studio-canvas {
  position: absolute;
  inset: 0;             /* remplit exactement la toile → clientW/H = taille dispo */
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 12px;
  background: var(--bg);
  touch-action: none;   /* on gérera les gestes nous-mêmes (Pointer Events, étape ④) */
}

/* Barre d'actions Studio (flotte en haut à droite du stage) */
.studio-toolbar {
  position: absolute;
  top: 12px;
  right: 12px;
  display: flex;
  flex-wrap: wrap;                 /* les boutons d'enregistrement peuvent passer à la ligne */
  justify-content: flex-end;
  align-items: center;
  gap: 8px;
  max-width: calc(100% - 24px);
  z-index: 5;
}
/* Groupe des 3 actions (Enregistrer / Enregistrer sous… / Exporter MP4).
   `display:contents` = le conteneur disparaît de la mise en page, ses enfants
   restent des items du flex de la barre → rendu desktop inchangé. Sous le seuil
   mobile, il redevient une vraie boîte : le menu déroulant du bouton ⋯. */
.menu-actions { display: contents; }
.btn-menu { display: none; }   /* bouton ⋯ : uniquement sous le seuil mobile */
.btn-primary,
.btn-secondary {
  min-height: 44px;
  padding: 0 16px;
  border: none;
  border-radius: 10px;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.btn-primary { background: var(--accent); color: #06231b; }
.btn-primary:disabled { opacity: 0.5; cursor: default; }
.btn-secondary { background: var(--bg-3); color: var(--txt); }

/* Boutons icônes (undo/redo) — cibles tactiles ≥ 44 px */
.btn-icone {
  min-width: 44px;
  min-height: 44px;
  padding: 0;
  border: none;
  border-radius: 10px;
  background: var(--bg-3);
  color: var(--txt);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}
.btn-icone:disabled { opacity: 0.4; cursor: default; }

/* ---------- Timeline des poses (étape ⑤-a) ---------- */
/* Bande flottante en bas du stage, au-dessus du canvas (même patron que la toolbar). */
.studio-timeline {
  flex: 0 0 auto;            /* réserve sa hauteur : le plateau prend le reste */
  margin-top: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  border-radius: 12px;
}
/* Défilement horizontal des puces si trop nombreuses (petits écrans). */
.pose-strip {
  flex: 1 1 auto;
  display: flex;
  gap: 6px;
  overflow-x: auto;
  min-width: 0;
  padding: 2px;
}
.pose-chip {
  flex: 0 0 auto;
  min-width: 44px;
  height: 44px;
  padding: 0 12px;
  border: 2px solid transparent;
  border-radius: 10px;
  background: var(--bg-3);
  color: var(--txt);
  font: inherit;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
}
/* Pose active : liseré ambre (--accent-2), bien visible sur fond sombre. */
.pose-chip.is-active {
  border-color: var(--accent-2);
  color: var(--accent-2);
}
/* Sélecteur de vitesse GLOBALE de lecture (n'altère pas les durées des poses). */
.vitesse-select {
  flex: 0 0 auto;
  height: 44px;
  padding: 0 8px;
  border: 1px solid var(--bg-3);
  border-radius: 10px;
  background: var(--bg-3);
  color: var(--txt);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

/* Réglages de la pose active (durée + réordre + suppression). */
.pose-reglages {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}
.pose-duree {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 13px;
  color: var(--txt-dim);
}
.pose-duree input {
  width: 56px;
  height: 44px;
  padding: 0 6px;
  border: 1px solid var(--bg-3);
  border-radius: 10px;
  background: var(--bg);
  color: var(--txt);
  font: inherit;
  text-align: right;
}
/* La 1re pose n'a pas de transition entrante → champ durée désactivé. */
.pose-duree input:disabled { opacity: 0.4; cursor: default; }
.btn-danger { color: var(--danger); }
.btn-icone:disabled { opacity: 0.4; cursor: default; }

/* ---------- Bancs de touche (étape ⑥) ---------- */
/* Deux bandes horizontales : rouge au-dessus, bleue en dessous du plateau.
   flex:0 0 auto → chaque bande RÉSERVE sa hauteur (le plateau prend le reste,
   l'échelle du terrain se recalcule seule — même patron que la timeline). */
.studio-banc {
  flex: 0 0 auto;
  display: flex;
  align-items: center;   /* centre le label sur la/les rangée(s) de puces (plus de marge à corriger) */
  gap: 6px;
  padding: 3px 6px;      /* compact : chrome vertical réduit → hauteur rendue au terrain (⑥ ajustement) */
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  border-radius: 12px;
}
.banc-rouge { margin-bottom: 3px; }
.banc-bleu  { margin-top: 3px; }
/* Pastille d'équipe (repère couleur de la bande). */
.banc-label {
  flex: 0 0 auto;
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 800;
  color: #fff;
}
.banc-rouge .banc-label { background: #c0392b; }
.banc-bleu  .banc-label { background: #2c6fb3; }
/* Grille de puces qui s'enroule ; hauteur bornée (2 rangées) + scroll si dépassement. */
.banc-puces {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 4px;
  max-height: 72px;   /* 2 rangées de 34px + 1 gap de 4px */
  overflow-y: auto;
}
/* Puce = jeton au banc. Couleur posée en JS (= couleur du jeton sur le terrain). */
.banc-puce {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 2px solid rgba(255, 255, 255, 0.85);
  border-radius: 999px;
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  touch-action: none;   /* gestes gérés au pointeur (drag, étape ⑥-b) */
}
.banc-puce:active { transform: scale(0.94); }
/* Emplacement ballon (dédié, hors équipes). */
.banc-ballon {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
}
.banc-puce-ballon {
  width: 30px;
  height: 30px;
  border-radius: 999px;
  border: 2px solid #000;
  background: #fff;
  cursor: pointer;
  touch-action: none;
  font-size: 15px;
  line-height: 1;
}
/* Pendant la lecture ▶ : le canvas est déjà neutralisé (pointerEvents='none'),
   mais les puces sont du DOM séparé → on les désactive explicitement (étape ⑥-b). */
.studio-banc.is-lecture {
  pointer-events: none;
  opacity: 0.5;
}
/* Fantôme de glisser banc → terrain : copie visuelle de la puce qui suit le
   pointeur (« je vois ce que je transporte »). Créé/détruit en JS, hors flux,
   transparent aux événements pour ne pas gêner le hit-test au lâcher. */
.banc-ghost {
  position: fixed;
  z-index: 1000;
  pointer-events: none;
  transform: translate(-50%, -50%) scale(1.1);
  opacity: 0.85;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
}

/* Overlay de progression d'export */
.export-overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  border-radius: 12px;
}
.export-overlay[hidden] { display: none; }
.export-box {
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  border-radius: 12px;
  padding: 20px 24px;
  min-width: 260px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.export-msg { margin: 0; font-size: 14px; }
.export-box progress { width: 100%; height: 10px; }


.placeholder {
  height: 100%;
  min-height: 60vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  color: var(--txt-dim);
}
.placeholder-icon { font-size: 56px; margin-bottom: 8px; }
.placeholder h2 { margin: 4px 0; color: var(--txt); }
.placeholder .hint { font-size: 13px; color: var(--accent); margin-top: 12px; }

/* ---------- Réglages ---------- */
.settings { max-width: 640px; margin: 0 auto; }
.settings h2 { margin-top: 0; }
.card {
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 12px;
}
.card h3 { margin: 0 0 8px; font-size: 14px; color: var(--accent); }
.mono {
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
  font-size: 13px;
  color: var(--txt-dim);
  margin: 4px 0;
  word-break: break-word;
}

/* ---------- Barre d'onglets (bas, pouce) ---------- */
.tabbar {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: flex;
  background: var(--bg-2);
  border-top: 1px solid var(--bg-3);
  z-index: 50;
}
.tab {
  flex: 1;
  min-height: 48px;               /* cible tactile ≥ 48 px */
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  color: var(--txt-dim);
  font: inherit;
  font-size: 11px;
  cursor: pointer;
}
.tab .tab-ico { font-size: 22px; line-height: 1; }
.tab.is-active { color: var(--accent); }
.tab.is-active .tab-ico { transform: translateY(-1px); }

/* Desktop : on élargit un peu le rembourrage… */
@media (min-width: 900px) {
  .view { padding: 24px; }
  /* …mais on PRÉSERVE la réserve pour la barre d'onglets sur la vue Studio,
     sinon le raccourci `padding: 24px` ci-dessus écraserait le padding-bottom
     et le canvas passerait derrière la barre. */
  .view-studio { padding-bottom: calc(var(--tabbar-h) + var(--safe-b) + 8px); }
}

/* ============================================================
   ÉCRAN ÉTROIT (téléphone) — refonte du Studio
   Deux règles, appliquées partout dans ce bloc :
     (A) rien ne flotte au-dessus du terrain ;
     (B) rien ne se compresse à zéro — ce qui ne rentre pas DÉFILE.
   Rien ici ne s'applique au-dessus de 700 px : le desktop est inchangé.
   ⚠️ Aucune de ces règles ne touche au rendu : renderFrame reste pure et
   l'export MP4 (1080×1920) est indépendant de la taille du canvas à l'écran.
   ============================================================ */
@media (max-width: 700px) {

  /* --- Stage en grille : les DEUX bancs partagent UNE rangée (une moitié chacun).
     Empilés, ils coûtaient 2 rangées de hauteur ; le terrain étant borné par la
     HAUTEUR sur mobile, cette rangée récupérée compense la barre d'outils qui
     passe dans le flux, et l'échelle px/m remonte (jetons plus gros). --- */
  .studio-stage {
    display: grid;
    /* minmax(0,·) est INDISPENSABLE : sans lui, le minimum automatique d'une piste
       vaut le min-content de son contenu — soit les 12 puces en `nowrap` du banc
       (≈ 456 px) — et la grille déborde largement l'écran au lieu de laisser le
       banc défiler. Même raison pour min-width:0 sur les items. */
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto minmax(0, 1fr) auto;   /* bancs / terrain / timeline */
    gap: 4px;
  }
  .studio-stage > * { min-width: 0; }
  .banc-rouge     { grid-area: 1 / 1 / 2 / 2; margin: 0; }
  .banc-bleu      { grid-area: 1 / 2 / 2 / 3; margin: 0; }
  .studio-plateau { grid-area: 2 / 1 / 3 / 3; }
  .studio-timeline{ grid-area: 3 / 1 / 4 / 3; margin-top: 0; }

  /* --- Bancs : UNE seule ligne de puces à défilement horizontal (règle B).
     Hauteur FIXE : le plateau ne se redimensionne pas quand un banc se vide,
     donc l'échelle du terrain ne saute pas en pleine séance. --- */
  .banc-puces {
    flex-wrap: nowrap;
    align-content: center;
    max-height: none;
    height: 38px;
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;              /* la puce coupée au bord suffit d'indice */
  }
  .banc-puces::-webkit-scrollbar { display: none; }
  /* Le doigt doit pouvoir FAIRE DÉFILER le banc : on rend l'axe horizontal au
     navigateur (pan-x) et on garde l'axe vertical — le seul qui mène au terrain,
     situé au-dessous — pour le glisser banc→terrain. Un geste horizontal envoie
     un pointercancel, déjà traité par le onUp de demarrerDragBanc (fantôme retiré,
     aucun jeton créé). */
  .banc-puce,
  .banc-puce-ballon { touch-action: pan-x; }

  /* --- Barre d'outils DANS LE FLUX, au-dessus du terrain (règle A) : elle ne
     recouvre plus rien. Le plateau devient une colonne [barre][toile]. --- */
  .studio-plateau {
    display: flex;
    flex-direction: column;
    gap: 6px;
  }
  .studio-toile {                        /* reprend la hauteur restante */
    position: relative;
    inset: auto;
    flex: 1 1 0;
    min-height: 0;
  }
  .studio-toolbar {
    position: relative;                  /* ancre du menu ⋯ ; plus d'absolute sur le canvas */
    top: auto;
    right: auto;
    order: -1;                           /* barre au-dessus de la toile */
    flex: 0 0 auto;
    flex-wrap: nowrap;                   /* plus de 3 lignes : une seule, qui TIENT */
    justify-content: flex-start;
    max-width: none;
  }
  /* Seul élément élastique de la rangée : l'indicateur de schéma (déjà en ellipsis).
     Il absorbe la largeur restante sans jamais tomber à zéro (min-width). */
  .schema-courant {
    flex: 1 1 auto;
    min-width: 60px;
    max-width: none;
  }

  /* --- Menu compact : Enregistrer / Enregistrer sous… / Exporter MP4.
     Ouvert à la demande, ancré sur la barre. On bascule avec une CLASSE et non
     l'attribut [hidden] : `[hidden]{display:none}` casserait le display:contents
     dont dépend le rendu desktop. --- */
  .btn-menu { display: block; }
  .menu-actions {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    right: 0;
    z-index: 20;
    flex-direction: column;
    gap: 8px;
    padding: 8px;
    background: var(--bg-2);
    border: 1px solid var(--bg-3);
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  }
  .menu-actions.is-ouvert { display: flex; }

  /* --- Timeline sur 2 rangées : la bande de poses récupère une vraie largeur.
     C'était LE défaut critique : .pose-strip, seul élément élastique d'une rangée
     dont les éléments fixes dépassaient déjà la largeur de l'écran, tombait à 4 px.
     Les réglages de pose passent seuls sur la 2ᵉ rangée (flex-basis 100 %). --- */
  .studio-timeline {
    flex-wrap: wrap;
    row-gap: 6px;
  }
  .pose-strip {
    /* flex-basis 0 (et non `auto`) : la décision de passage à la ligne se prend sur la
       taille de BASE, pas sur le contenu. Avec `auto`, une bande de 5 poses (248 px)
       poussait déjà la bande sur sa propre ligne → 3 rangées. Avec 0, elle occupe
       simplement la place restante de la 1ʳᵉ rangée et défile. */
    flex: 1 1 0;
    min-width: 120px;                    /* garde-fou : ne peut plus se compresser à zéro */
    scrollbar-width: none;
  }
  .pose-strip::-webkit-scrollbar { display: none; }
  .pose-reglages { flex: 1 0 100%; }
}

/* ---------- Indicateur de sauvegarde du brouillon (étape ⑦-a1) ---------- */
/* Toast fixe au-dessus de la barre d'onglets. Discret quand tout va bien,
   bannière rouge bien visible en cas d'échec de sauvegarde (jamais silencieux). */
.save-status {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabbar-h) + var(--safe-b) + 10px);
  transform: translateX(-50%);
  max-width: min(92vw, 460px);
  margin: 0;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  z-index: 60;
  pointer-events: none;   /* purement informatif : ne capte jamais les clics */
}
.save-status.ok {
  color: var(--txt-dim);
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  opacity: 0.7;
}
.save-status.echec {
  color: #fff;
  background: var(--danger);
  border: 1px solid var(--danger);
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  opacity: 1;
}

/* ---------- Bibliothèque de schémas nommés (étape ⑦-a2) ---------- */
/* Indicateur de schéma courant dans la barre Studio. */
.schema-courant {
  align-self: center;
  max-width: 40vw;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  color: var(--txt-dim);
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Liste des séances (onglet Séances). */
.seances { max-width: 720px; margin: 0 auto; }
.seances h2 { margin: 0 0 16px; }
/* Barre export/import au-dessus de la liste (étape ⑦-b). */
.seances-actions-haut {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
}
.seances-actions-haut button { min-height: 40px; padding: 6px 14px; }
.seances-liste { display: flex; flex-direction: column; gap: 10px; }
.seance-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  border-radius: 12px;
  padding: 12px 14px;
}
.seance-info { min-width: 0; }
.seance-nom { font-size: 15px; font-weight: 600; color: var(--txt); word-break: break-word; }
.seance-date { font-size: 12px; color: var(--txt-dim); margin-top: 2px; }
.seance-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.seance-actions button { min-height: 40px; padding: 6px 12px; font-size: 13px; }

/* Modale générique (prompt / confirm / alerte). */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.6);
  padding: 20px;
}
.modal-overlay[hidden] { display: none; }
.modal-box {
  background: var(--bg-2);
  border: 1px solid var(--bg-3);
  border-radius: 12px;
  padding: 20px 22px;
  width: min(92vw, 380px);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-titre { margin: 0; font-size: 15px; font-weight: 600; color: var(--txt); }
.modal-input {
  width: 100%;
  box-sizing: border-box;
  min-height: 44px;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--bg-3);
  background: var(--bg);
  color: var(--txt);
  font: inherit;
}
.modal-input[hidden] { display: none; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }
