/*
 * fiches.css — Fiches de révision (SPECS §9).
 *
 * Trois contextes pour UN même modèle de fiche (.fiche) :
 *   1. l'écran de configuration (#/fiches) : sélection, filtres, format ;
 *   2. le DECK consultable : fil vertical plein écran à défilement par fiche
 *      (scroll-snap, façon Shorts) ;
 *   3. l'IMPRESSION (@media print) : une fiche par page (ou N par page), couleurs
 *      d'encre, masquage du reste de l'app.
 *
 * Tout dérive des tokens de styles/main.css (clair/sombre + palettes), sauf le
 * bloc d'impression qui force des couleurs lisibles à l'encre.
 */

/* ===================== Écran de configuration ============================== */

.fiches-count { margin: 0; }
.small { font-size: 0.85rem; }

/* Sélection des caractéristiques + valeurs de filtre : cases à cocher en ligne. */
.fiches-cols { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.check {
  display: inline-flex; align-items: center; gap: 8px;
  cursor: pointer; user-select: none;
}
.check input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); flex: none; }

.fiches-format { align-items: flex-end; gap: 16px; }

/* Carte « Filtrer » repliable (repliée par défaut) : le titre vit dans le <summary>,
   avec un chevron explicite qui pivote à l'ouverture (le marqueur natif est masqué). */
.fiches-filter-card > summary {
  cursor: pointer; display: flex; align-items: center; gap: 8px; list-style: none;
}
.fiches-filter-card > summary::-webkit-details-marker { display: none; }
.fiches-filter-card .chevron { color: var(--muted); transition: transform var(--speed); }
.fiches-filter-card[open] > summary .chevron { transform: rotate(90deg); }
h2.inline { display: inline; margin: 0; }

/* Compartiments de colonnes (identifiantes / descriptives). */
.fiches-col-group { gap: 6px; }
.group-label { margin: 0; font-weight: 600; }

/* Repère des colonnes identifiantes (#nature = identifier/key) — ambre. */
.chip.col-id, .chip-check.col-id { background: var(--id-soft); border-color: var(--id); }
.check.col-id > span, .fiches-field-name.col-id { color: var(--id); font-weight: 600; }
.fiches-filter.col-id > summary > span:first-child { color: var(--id); font-weight: 600; }

/* Résumé du paramétrage. */
.fiche-summary-line { margin: 0; }

/* Filtres : un <details> par colonne. */
.fiches-filter { border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 6px 10px; }
.fiches-filter > summary {
  cursor: pointer; font-weight: 600; display: flex; align-items: center; gap: 8px;
  list-style-position: inside;
}
.fiches-filter[open] > summary { margin-bottom: 8px; }
.badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 1.5em; padding: 0 6px; height: 1.5em;
  background: var(--accent); color: var(--on-accent);
  border-radius: 999px; font-size: 0.8rem; font-weight: 700;
}
.fiches-filter-values { display: flex; flex-wrap: wrap; gap: 6px; }

/* Une valeur de filtre = puce cliquable ; grisée si la cocher ne laisserait
   aucune ligne (grisage dynamique, SPECS §7). */
.chip-check {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 4px 10px; border: 1px solid var(--border); border-radius: 999px;
  background: var(--surface); cursor: pointer; font-size: 0.9rem;
}
.chip-check input[type="checkbox"] { width: 15px; height: 15px; accent-color: var(--accent); flex: none; }
.chip-check:has(input:checked) { background: var(--accent-soft); border-color: var(--accent); }
.chip-check.unavailable { opacity: 0.4; cursor: not-allowed; }

/* ===================== Modèle de fiche (.fiche) ============================ */

.fiche {
  --fiche-gap: 14px;
  /* Couleurs de rôle (surchargées en ligne par l'éditeur, lot 3b) — par défaut
     elles suivent la palette effective de la fiche. */
  --fiche-accent: var(--accent);
  --fiche-box-bg: var(--surface-2);
  --fiche-box-border: var(--border);
  --fiche-text: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 22px;
  color: var(--fiche-text);
  display: flex; flex-direction: column; gap: var(--fiche-gap);
}
.fiche.compact { --fiche-gap: 8px; padding: 14px 16px; font-size: 0.92rem; }

/* En-tête héros : titre + sous-titre à gauche, photo en bandeau à droite. */
.fiche-head { display: flex; gap: 16px; align-items: flex-start; justify-content: space-between; }
.fiche-head-text { min-width: 0; flex: 1; }
.fiche-title {
  margin: 0; font-size: 1.5rem; line-height: 1.2;
  padding-bottom: 8px; border-bottom: 2px solid var(--fiche-accent);
}
.fiche.compact .fiche-title { font-size: 1.25rem; }
.fiche-subtitle { margin: 6px 0 0; font-style: italic; color: var(--muted); }

.fiche-hero { margin: 0; flex: 0 0 38%; max-width: 38%; }
.fiche-hero-frame {
  width: 100%; aspect-ratio: 4 / 3; border-radius: var(--radius-sm);
  overflow: hidden; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
}

/* Grille des caractéristiques : les champs COURTS s'écoulent en colonnes
   (auto-fill), les champs LONGS (.span-full) prennent toute la largeur. */
.fiche-grid {
  display: grid; gap: var(--fiche-gap) 18px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  align-items: start;
}
.fiche-field { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.fiche-field.span-full { grid-column: 1 / -1; }
.fiche-label {
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.04em;
  color: var(--muted); font-weight: 600;
}
.fiche-value { font-size: 1rem; overflow-wrap: anywhere; }
.fiche-chips { display: flex; flex-wrap: wrap; gap: 6px; }

/* Cadre média à RATIO FIXE : homogénéise des images de tailles différentes
   (object-fit: cover recadre sans déformer). */
.fiche-media { margin: 0; display: flex; flex-direction: column; gap: 4px; }
.fiche-media-frame {
  width: 100%; max-width: 420px; aspect-ratio: 4 / 3; border-radius: var(--radius-sm);
  overflow: hidden; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
}
.fiche-hero-frame .fiche-img, .fiche-hero-frame .media, .fiche-hero-frame img,
.fiche-aside-frame .fiche-img, .fiche-aside-frame .media, .fiche-aside-frame img,
.fiche-media-frame .fiche-img, .fiche-media-frame .media, .fiche-media-frame img {
  width: 100%; height: 100%; object-fit: cover; display: block;
}

/* Image « sous le bandeau » : ~50 % à côté du 1er encadré. */
.fiche-aside-row { display: flex; gap: 16px; align-items: flex-start; }
.fiche-aside { margin: 0; flex: 0 0 48%; max-width: 48%; }
.fiche-aside-frame {
  width: 100%; aspect-ratio: 3 / 4; border-radius: var(--radius-sm);
  overflow: hidden; background: var(--surface-2);
  display: flex; align-items: center; justify-content: center;
}
.fiche-aside-row > .fiche-box { flex: 1; min-width: 0; }
@media (max-width: 540px) {
  .fiche-aside-row { flex-direction: column; }
  .fiche-aside { max-width: 100%; flex-basis: auto; width: 100%; }
}
.fiche-media-frame audio { width: 90%; height: auto; }
.fiche-media-na { padding: 12px; text-align: center; }

/* Petite largeur : l'en-tête passe en colonne (photo sous le titre). */
@media (max-width: 540px) {
  .fiche-head { flex-direction: column; }
  .fiche-hero { max-width: 100%; flex-basis: auto; width: 100%; }
}

/* ===================== Deck consultable (scroll-snap) ===================== */

.fiches-deck-overlay {
  position: fixed; inset: 0; z-index: 1000;
  background: var(--bg); display: flex; flex-direction: column;
}
.fiches-deck-bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 10px 16px; border-bottom: 1px solid var(--border);
  background: var(--surface); flex: none;
}
.fiches-deck-list {
  flex: 1; overflow-y: auto; scroll-snap-type: y mandatory;
  scroll-behavior: smooth;
}
.fiches-deck-page {
  min-height: 100%; scroll-snap-align: start; scroll-snap-stop: always;
  display: flex; align-items: center; justify-content: center;
  padding: 20px; box-sizing: border-box;
}
.fiches-deck-page .fiche { width: 100%; max-width: 560px; }
@media (prefers-reduced-motion: reduce) { .fiches-deck-list { scroll-behavior: auto; } }

/* ===================== Impression (PDF) =================================== */

/* Caché à l'écran : n'existe que pour l'impression. */
.fiches-print { display: none; }

@media print {
  /* Ne montrer que les fiches : l'app entière disparaît. */
  body > *:not(.fiches-print) { display: none !important; }
  .fiches-print { display: block; }

  /* Couleurs d'encre, indépendantes du thème clair/sombre. */
  .fiches-print .fiche {
    background: #fff; color: #111; border: 1px solid #ccc; box-shadow: none;
    break-inside: avoid; -webkit-print-color-adjust: exact; print-color-adjust: exact;
  }
  /* Accents et bordures conservent la couleur choisie (lisibles sur blanc dans
     les deux modes) ; le texte et le fond d'encadré restent en encre. */
  .fiches-print .fiche-title { border-bottom-color: var(--fiche-accent); }
  .fiches-print .fiche-subtitle { color: #555; }
  .fiches-print .fiche-label { color: #555; }
  .fiches-print .fiche-hero-frame,
  .fiches-print .fiche-media-frame { background: #f0f0f0; }
  .fiches-print .chip { background: #eee; color: #111; border: 1px solid #ccc; }
  .fiches-print .fiche-box { break-inside: avoid; }
  .fiches-print .fiche-box.bordered { border-color: var(--fiche-box-border); }
  .fiches-print .fiche-box.filled { background: #f4f4f4; }
  .fiches-print .fiche-box-title { color: var(--fiche-accent); }
  .fiches-print .fiche.separators .fiche-field::after { background: #ccc; }
  .fiches-print .fiche.separators .fiche-box + .fiche-box { border-top-color: #ccc; }

  /* 1 fiche par page : saut de page après chaque fiche. */
  .fiches-print[data-perpage="1"] .fiche { break-after: page; }
  .fiches-print[data-perpage="1"] .fiche:last-of-type { break-after: auto; }

  /* 2 par page : empilées, l'évitement de coupure les répartit (~2/page). */
  .fiches-print[data-perpage="2"] .fiche { margin-bottom: 6mm; }

  /* 4 par page : grille 2 colonnes (~4/page via break-inside: avoid). */
  .fiches-print[data-perpage="4"] { display: grid; grid-template-columns: 1fr 1fr; gap: 6mm; }
  .fiches-print[data-perpage="4"] .fiche { padding: 12px 14px; }
  .fiches-print[data-perpage="4"] .fiche-title { font-size: 1.1rem; }
}

/* ===================== Encadrés (gabarit personnalisé) ==================== */

.fiche-box { display: flex; flex-direction: column; gap: 8px; }
.fiche-box.bordered { border: 1px solid var(--fiche-box-border); border-radius: var(--radius-sm); padding: 12px 14px; }
.fiche-box.filled { background: var(--fiche-box-bg); border-radius: var(--radius-sm); padding: 12px 14px; }
.fiche-box-title { margin: 0 0 2px; font-size: 0.95rem; font-weight: 700; color: var(--fiche-accent); }
.fiche-box-grid {
  display: grid; gap: 10px 18px; align-items: start;
  grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr));
}
.fiche-box-grid .fiche-field.span-full { grid-column: 1 / -1; }
.fiche-field-line { margin: 0; }
.fiche-label.inline { text-transform: none; letter-spacing: 0; font-size: 0.9rem; font-weight: 600; }
.fiche-value.b { font-weight: 700; }
.fiche-value.i { font-style: italic; }
.fiche-value.lg { font-size: 1.25rem; font-weight: 600; }

/* Lignes de séparation optionnelles : un trait court sous chaque champ
   (largeur ~88 % de la colonne), dans et hors encadrés. */
.fiche.separators .fiche-field { padding-bottom: 8px; }
.fiche.separators .fiche-field::after {
  content: ''; display: block; width: 88%; height: 1px;
  margin-top: 8px; background: var(--border);
}
.fiche.separators .fiche-box + .fiche-box { border-top: 1px solid var(--border); padding-top: 10px; }

/* ===================== Éditeur de mise en page =========================== */

/* L'éditeur sort du conteneur étroit (#app, 720px) pour occuper l'écran. */
.fiches-editor-screen {
  width: min(98vw, 1600px);
  margin-left: 50%;
  transform: translateX(-50%);
}
.fiches-editor {
  display: grid; gap: var(--gap); align-items: start;
  grid-template-columns: minmax(0, 1.7fr) minmax(320px, 1fr);
}
.fiches-editor.no-preview { grid-template-columns: 1fr; }
@media (max-width: 860px) { .fiches-editor { grid-template-columns: 1fr; } }
.fiches-editor-preview-wrap { position: sticky; top: 8px; }
.fiches-editor-preview {
  background: var(--bg); border: 1px dashed var(--border);
  border-radius: var(--radius); padding: 12px;
}
.fiches-editor-preview .fiche { max-width: 560px; margin: 0 auto; }
.fiches-preview-bar { margin-bottom: 8px; }

/* Encadrés empilés verticalement — l'éditeur reflète la fiche (les encadrés y
   sont aussi les uns sous les autres ; un encadré occupe toute la largeur). */
.fiches-boxes { display: flex; flex-direction: column; gap: var(--gap); }

.fiches-tray {
  display: flex; flex-wrap: wrap; gap: 8px; min-height: 42px; padding: 8px;
  border: 1px dashed var(--border); border-radius: var(--radius-sm);
}
.chip.draggable { gap: 6px; }
.drag-handle { cursor: grab; color: var(--muted); user-select: none; padding: 0 2px; }
.drag-handle:active { cursor: grabbing; }
.drop-hover { outline: 2px dashed var(--accent); outline-offset: 2px; }

.fiches-box-edit { gap: 10px; }
.fiches-box-head input[type="text"] { flex: 1; min-width: 140px; }
/* Champs d'un encadré : disposés selon le NOMBRE DE COLONNES de l'encadré
   (l'éditeur reflète la fiche). Sur très petite largeur, on retombe à 1 colonne. */
.fiches-box-fields {
  display: grid; gap: 6px; align-items: start;
  grid-template-columns: repeat(var(--cols, 1), minmax(0, 1fr));
}
@media (max-width: 560px) { .fiches-box-fields { grid-template-columns: 1fr; } }
.fiches-field-row {
  display: flex; flex-direction: column; gap: 4px;
  padding: 6px 8px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); background: var(--surface);
}
.fiches-box-empty { grid-column: 1 / -1; }
.field-row-main { gap: 6px; align-items: center; }
.field-row-tools { gap: 4px; flex-wrap: wrap; }
.fiches-field-name { flex: 1; font-weight: 600; min-width: 60px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inline-field { flex-direction: row; align-items: center; gap: 6px; }
.mini-select { font-size: 0.85rem; max-width: 100%; }
.fmt-toggle { font-weight: 700; min-width: 30px; }
.fmt-toggle.active { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }

/* Nuanciers de couleur (rôles de la fiche). */
.color-row { gap: 6px; align-items: center; }
.color-role { min-width: 64px; }
.swatch {
  width: 26px; height: 26px; min-width: 26px; padding: 0; line-height: 1;
  border-radius: 6px; border: 1px solid var(--border); cursor: pointer;
}
.swatch.active { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ===================== Tactile (cibles élargies) ========================== */

@media (pointer: coarse) {
  .check input[type="checkbox"] { width: 22px; height: 22px; }
  .chip-check { padding: 8px 12px; }
  .chip-check input[type="checkbox"] { width: 20px; height: 20px; }
}
