/* ==========================================================================
   MelodySuite · Générateur de cartes
   - Écran : panneau latéral + aperçu des pages A4.
   - Impression : uniquement les pages A4 (@media print), grille CSS exacte.
   Variables de mise en page posées en JS sur chaque .page :
     --cols, --rows   (nombre de colonnes / lignes de cartes)
     --note-scale     (info, la taille réelle est gérée par le renderer)
   ========================================================================== */

:root {
  --sidebar-w: 360px;
  --bg: #eef1f5;
  --panel: #ffffff;
  --ink: #1d2733;
  --muted: #6b7785;
  --accent: #2e6be6;
  --accent-ink: #fff;
  --border: #d7dde5;
  --radius: 8px;
  --page-margin: 10mm;
  --cut-color: #8a8f98;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { display: flex; background: var(--bg); }

/* ───────────── Panneau latéral ───────────── */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  height: 100vh;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 14px 14px 24px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.sidebar-header h1 { font-size: 1.2rem; margin: 0; }
.subtitle { margin: 2px 0 0; color: var(--muted); font-size: .8rem; }

.panel { border: 1px solid var(--border); border-radius: var(--radius); padding: 6px 10px 10px; margin-bottom: 8px; }
.panel > summary { cursor: pointer; font-weight: 600; padding: 4px 0; user-select: none; }
.panel[open] > summary { margin-bottom: 6px; }

.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; font-size: .9rem; }
.row.two { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row.two > label { display: flex; flex-direction: column; gap: 2px; font-size: .8rem; color: var(--muted); }
.row input[type="range"] { flex: 1; }
.row-buttons { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; align-items: center; }
.inline { display: flex; align-items: center; gap: 4px; font-size: .8rem; color: var(--muted); }
.check { display: flex; align-items: center; gap: 6px; font-size: .9rem; margin: 4px 0; }
.hint { font-size: .75rem; color: var(--muted); margin: 4px 0; }
.btn-group { display: flex; gap: 4px; }
.lot-list { list-style: none; margin: 0 0 6px; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.lot { display: flex; align-items: center; gap: 4px; padding: 3px 4px 3px 8px; border: 1px solid var(--border); border-radius: 6px; font-size: .8rem; }
.lot.is-selected { border-color: var(--accent); background: #eaf0f8; }
.lot-summary { flex: 1; min-width: 0; }
.lot button { padding: 2px 7px; }
.lot-tag { font-weight: 400; color: var(--muted); font-size: .8rem; }

input[type="text"], input[type="number"], select {
  font: inherit; font-size: .9rem; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; background: #fff; min-width: 0; width: 100%;
}
button {
  font: inherit; font-size: .85rem; padding: 5px 10px; border: 1px solid var(--border); border-radius: 6px; background: #f7f9fb; cursor: pointer;
}
button:hover { background: #eaf0f8; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover { filter: brightness(1.08); }

.segmented { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; margin-bottom: 6px; }
.segmented label { flex: 1; text-align: center; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 6px 4px; font-size: .85rem; }
.segmented input:checked + span { background: var(--accent); color: var(--accent-ink); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }

/* Affichage conditionnel selon les modes (classes posées sur <body>). */
body:not(.mode-melodic) .melodic-only { display: none; }
body:not(.gen-random) .random-only { display: none; }
body.gen-random .catalog-only { display: none; }
body.landscape .portrait-only, body:not(.landscape) .landscape-only { display: none; }

/* Filtre niveaux */
.level-filter { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 4px; }
.level-chip { display: inline-flex; align-items: center; gap: 4px; font-size: .78rem; border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px 2px 4px; cursor: pointer; }
.level-chip input { margin: 0; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--c, #ccc); flex: 0 0 auto; }

.cells { border: 1px solid var(--border); border-radius: 6px; margin: 8px 0 0; padding: 4px 8px 8px; }
.cells legend { font-weight: 600; font-size: .85rem; padding: 0 4px; }
.cells legend small { font-weight: 400; color: var(--muted); }
.cell-list { display: grid; grid-template-columns: 1fr; gap: 2px; }
.cell-item { display: flex; align-items: center; gap: 6px; font-size: .85rem; padding: 2px 0; cursor: pointer; }
.cell-item .lvl { margin-left: auto; font-size: .7rem; color: var(--muted); }

.actions { position: sticky; bottom: 0; display: flex; gap: 8px; background: var(--panel); padding-top: 8px; }
.actions button { flex: 1; padding: 8px; }
.status { font-size: .8rem; min-height: 1.2em; margin: 0; color: var(--muted); }
.status.error { color: #c0392b; }

/* ───────────── Aperçu ───────────── */
.preview {
  flex: 1;
  height: 100vh;
  overflow: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  /* Centrage par margin auto (et non align-items) : si la page déborde, elle
     déborde à droite et reste entièrement accessible au défilement. */
  align-items: flex-start;
  gap: 24px;
}
.preview > * { margin-inline: auto; }
.empty { color: var(--muted); margin-top: 20vh; }

/* ───────────── Page A4 et cartes (écran + impression) ───────────── */
.page {
  width: 210mm;
  height: 297mm;
  padding: var(--page-margin);
  background: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, .15);
  flex: 0 0 auto;
  overflow: hidden;
}
.page.landscape { width: 297mm; height: 210mm; }
.page-grid {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(var(--cols, 2), 1fr);
  grid-template-rows: repeat(var(--rows, 5), 1fr);
}
.card {
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  padding: 3mm 4mm 2.5mm 5mm;
  break-inside: avoid;
  page-break-inside: avoid;
  overflow: hidden;
}
/* Cadre intérieur + thème : SVG de fond (repère en mm), contenu au-dessus. */
.card-decor { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.card.has-decor > .card-score,
.card.has-decor > .card-label { position: relative; z-index: 1; }
input[type="color"] { width: 100%; height: 30px; padding: 2px; border: 1px solid var(--border); border-radius: 6px; background: #fff; }

.card-score { position: relative; flex: 1; min-height: 0; }
.card-score svg {
  position: absolute;
  inset: 0;
  margin: auto;
  max-width: 100%;
  max-height: 100%;
}
.card-label {
  font-size: 9pt;
  text-align: center;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding-top: 1mm;
}
.card-error { color: #c0392b; font-size: 8pt; margin: auto; text-align: center; }

/* Code couleur du niveau : bandeau gauche + pastille */
.page.level-colors .card::before {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 2.2mm;
  background: var(--level-color, transparent);
}
.page.level-colors .card-label::after {
  content: attr(data-level);
  margin-left: 2mm;
  padding: 0 1.5mm;
  border-radius: 1mm;
  font-size: 7pt;
  color: #fff;
  background: var(--level-color, #999);
}
.page.hide-names .card-label { visibility: hidden; }
.page.hide-names.level-colors .card-label { visibility: visible; font-size: 0; }
.page.hide-names.level-colors .card-label::after { font-size: 7pt; }

/* Pointillés de découpe : bordure partagée entre cartes adjacentes */
.page.cut-lines .page-grid { border-top: .3mm dashed var(--cut-color); border-left: .3mm dashed var(--cut-color); }
.page.cut-lines .card { border-right: .3mm dashed var(--cut-color); border-bottom: .3mm dashed var(--cut-color); }

/* ───────────── Cartes de notes (cartes-notes.html) ───────────── */
/* Choix de clé et d'étoiles, même présentation que le Memory (games.css). */
.seg { display: flex; flex-wrap: wrap; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; margin-bottom: 6px; }
.seg label { flex: 1; text-align: center; cursor: pointer; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; padding: 6px 4px; font-size: .85rem; font-weight: 600; }
.seg input:checked + span { background: var(--accent); color: var(--accent-ink); }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: -2px; }
.tiles.stars { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 6px; }
.tile { position: relative; cursor: pointer; }
.tile input { position: absolute; opacity: 0; pointer-events: none; }
.tile > span { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 2px; height: 100%; padding: 6px 2px; border: 2px solid var(--border); border-radius: 10px; background: #fafaff; }
.tile small { color: var(--muted); font-size: .68rem; white-space: pre-line; }
.tile b { font-size: .8rem; }
.tile .star .ico { width: 2rem; height: 2rem; }
.tile input:checked + span { border-color: var(--lc); background: color-mix(in srgb, var(--lc) 12%, #fff); }
.tile input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Une note par carte : portée centrée, nom en grand, petite étoile dans le coin. */
.page.note-cards .card-label { font-size: 13pt; font-weight: 600; }
.page.note-cards .card-label::after { display: none; }
.page.note-cards.hide-names .card-label { visibility: hidden; }
.card-star { position: absolute; top: 1.5mm; right: 1.5mm; width: 5mm; height: 5mm; line-height: 0; z-index: 2; }
.card-star .ico { width: 100%; height: 100%; }
.page:not(.level-colors) .card-star { display: none; }

/* Recto-verso à plier : portée | nom sur deux cases voisines. Trait mixte (fold) entre les deux faces,
   au lieu des pointillés de découpe ; le verso n'a ni liseré, ni étoile, ni cadre.
   Fond gris clair : la portée ne se voit pas par transparence une fois pliée. */
.page.note-cards .card.fold-back { align-items: center; justify-content: center; padding: 3mm; background: #e4e4e4; }
.page.note-cards .card.fold-back::before { display: none; }
.card-fold-name { font-size: 28pt; font-weight: 600; color: #777; text-align: center; }
.page.cut-lines .card.fold-front {
  border-right-color: transparent;
  background: repeating-linear-gradient(to bottom, var(--cut-color) 0 3mm, transparent 3mm 4mm, var(--cut-color) 4mm 4.6mm, transparent 4.6mm 5.6mm) right top / .3mm 100% no-repeat border-box;
  background-origin: border-box;
}

/* ───────────── Impression ───────────── */
/* Orientation par défaut ; layoutPages() injecte la règle @page de l'orientation choisie. */
@page { size: A4 portrait; margin: 0; }

@media print {
  html, body { height: auto; background: #fff; }
  body { display: block; }
  .no-print, .sidebar { display: none !important; }
  .preview { display: block; height: auto; overflow: visible; padding: 0; gap: 0; }
  .page {
    box-shadow: none;
    margin: 0;
    break-after: page;
    page-break-after: always;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }
  .page:last-child { break-after: auto; page-break-after: auto; }
}

