/* Carnet : interface sobre, façon papier. */
:root {
  --bg: #efede8;
  --panel: #fbfaf7;
  --paper: #ffffff;
  --ink: #1c1c1c;
  --muted: #77746d;
  --line: #dcd9d1;
  --hover: #ebe8e1;
  --desk: #e4e1da;
  --danger: #c0392b;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
body { position: fixed; inset: 0; }
#app { height: 100%; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:disabled { cursor: default; }
input { font: inherit; -webkit-user-select: text; user-select: text; }
svg { width: 22px; height: 22px; display: block; flex: none; }
.ic { display: inline-grid; place-items: center; }
.grow { flex: 1; }
.file-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

/* ---------- Boutons, champs ---------- */

.btn {
  padding: 10px 16px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--paper);
  font-weight: 500;
  white-space: nowrap;
}
.btn.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.ghost { background: none; border-color: transparent; }
.btn:disabled { opacity: 0.4; }
.icon-btn { width: 42px; height: 42px; display: grid; place-items: center; border-radius: 10px; }
.icon-btn:hover, .act:hover { background: var(--hover); }
/* Synchronisation : pastille sur le nuage (vert à jour, orange en cours, gris hors ligne, rouge en cas de problème) */
.sync-btn { position: relative; }
.sync-btn[data-state="off"] svg { opacity: 0.4; }
.sync-btn::after { content: ''; position: absolute; right: 8px; bottom: 9px; width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--panel); display: none; }
.sync-btn[data-state="idle"]::after { display: block; background: #2f9e44; }
.sync-btn[data-state="busy"]::after, .sync-btn[data-state="pending"]::after { display: block; background: #f08c00; }
.sync-btn[data-state="busy"]::after { animation: blink 1s ease-in-out infinite; }
.sync-btn[data-state="offline"]::after { display: block; background: #adb5bd; }
.sync-btn[data-state="error"]::after { display: block; background: #e03131; }
@keyframes blink { 50% { opacity: 0.3; } }
.sync-panel { display: flex; flex-direction: column; gap: 12px; }
.sync-panel p { margin: 0; }
.sync-error { margin: 0; color: var(--danger); font-size: 14px; }
.sync-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.field {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  font-size: 16px; /* 16px minimum : sinon Safari zoome sur le champ */
  outline: none;
}
.field:focus { border-color: var(--ink); }
.muted { color: var(--muted); margin: 0; line-height: 1.45; }
.muted + .muted { margin-top: 10px; }
.label { font-size: 13px; color: var(--muted); margin: 18px 0 8px; }

/* ---------- Bibliothèque ---------- */

.library { height: 100%; display: flex; flex-direction: column; }
.lib-head {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) calc(20px + env(safe-area-inset-right)) 14px calc(20px + env(safe-area-inset-left));
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.lib-top { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 14px; min-height: 48px; }
.lib-title { flex: 1; min-width: 120px; margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -0.01em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lib-actions { display: flex; align-items: center; gap: 8px; margin-left: auto; }

/* Retour au dossier parent : grande zone à toucher */
.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  max-width: 36%;
  height: 48px;
  padding: 0 16px 0 8px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--paper);
  font-size: 16px;
  font-weight: 600;
}
.back-btn svg { width: 26px; height: 26px; }
.back-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back-btn:hover { background: var(--hover); }

/* Chemin complet en pastilles, défilable horizontalement */
.pathbar { display: flex; align-items: center; gap: 4px; margin: 0 -4px; padding: 2px 4px; overflow-x: auto; scrollbar-width: none; }
.pathbar::-webkit-scrollbar { display: none; }
.path-chip {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 240px;
  height: 40px;
  padding: 0 16px;
  border-radius: 20px;
  border: 1px solid var(--line);
  background: var(--paper);
  font-size: 15px;
}
.path-chip span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.path-chip svg { width: 18px; height: 18px; }
.path-chip.current { background: var(--ink); border-color: var(--ink); color: #fff; font-weight: 600; }
button.path-chip:hover { background: var(--hover); }
.path-sep { flex: none; color: var(--muted); }
.path-sep svg { width: 16px; height: 16px; }
.search {
  width: 170px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--paper);
  font-size: 16px;
  outline: none;
}
.act {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--paper);
  font-weight: 500;
}
.act svg { width: 20px; height: 20px; }
.act.primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.act.primary:hover { background: #333; }

.grid {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;  /* les glissements horizontaux restent à l'app (retour par le bord gauche) */
  padding: 26px calc(24px + env(safe-area-inset-right)) calc(40px + env(safe-area-inset-bottom)) calc(24px + env(safe-area-inset-left));
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 28px 22px;
  align-content: start;
}
.card { position: relative; display: flex; flex-direction: column; gap: 8px; cursor: pointer; min-width: 0; }
.card .thumb {
  position: relative;
  aspect-ratio: 210 / 297;
  background: var(--paper);
  border-radius: 5px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 6px 18px rgba(0, 0, 0, 0.06);
  overflow: hidden;
  display: grid;
  place-items: center;
  transition: transform 0.15s;
}
.card:active .thumb { transform: scale(0.97); }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; }
.card .thumb img:not([src]) { visibility: hidden; }
.card.folder .thumb { background: none; box-shadow: none; }
.card.folder .thumb svg { width: 78%; height: auto; color: #b9b3a5; stroke-width: 0.9; }
.badge {
  position: absolute;
  top: 6px;
  left: 6px;
  font-size: 10px;
  font-weight: 600;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(28, 28, 28, 0.75);
  color: #fff;
}
.meta { min-width: 0; padding-right: 30px; }
.meta .name { font-weight: 600; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.meta .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.card-more { position: absolute; right: -6px; bottom: 0; width: 36px; height: 36px; display: grid; place-items: center; border-radius: 8px; color: var(--muted); }
.card-more:hover { background: var(--hover); }
.empty { grid-column: 1 / -1; text-align: center; padding: 90px 20px; color: var(--muted); }
.card .thumb img { pointer-events: none; -webkit-user-drag: none; }

/* Glisser-déposer */
.card.lifted { opacity: 0.3; }
.card.folder.drop-target { outline: 2.5px solid var(--ink); outline-offset: 6px; border-radius: 8px; }
.card.folder.drop-target .thumb { transform: scale(1.08); }
.card.folder.drop-target .thumb svg { color: var(--ink); stroke-width: 1.3; }
.path-chip.drop-target, .back-btn.drop-target { background: var(--ink); border-color: var(--ink); color: #fff; }
.drag-ghost {
  position: fixed;
  z-index: 70;
  width: 110px;
  pointer-events: none;
  transform: translate(-50%, -60%) rotate(-3deg) scale(1.05);
  filter: drop-shadow(0 14px 24px rgba(0, 0, 0, 0.28));
}
.drag-ghost .thumb {
  position: relative;
  aspect-ratio: 210 / 297;
  border-radius: 5px;
  background: var(--paper);
  overflow: hidden;
  display: grid;
  place-items: center;
}
.drag-ghost .thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; }
.drag-ghost .thumb svg { width: 80%; height: auto; color: #b9b3a5; stroke-width: 0.9; }
.drag-name {
  margin-top: 6px;
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--ink);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.empty-title { font-size: 18px; font-weight: 600; color: var(--ink); margin-bottom: 6px; }

/* ---------- Éditeur ---------- */

.editor { height: 100%; display: flex; }
/* Aucun geste natif de Safari dans l'éditeur : il pourrait prendre la main sur le stylet quand la paume se pose. */
.editor, .editor * { touch-action: none; }
.toolbar {
  width: calc(68px + env(safe-area-inset-left));
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: calc(8px + env(safe-area-inset-top)) 0 calc(8px + env(safe-area-inset-bottom)) env(safe-area-inset-left);
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  scrollbar-width: none;
  z-index: 2;
}
.tb {
  width: 58px;
  flex: none;
  padding: 6px 0 4px;
  border-radius: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.tb small { font-size: 9.5px; color: var(--muted); letter-spacing: 0.01em; }
.tb:hover:not(:disabled):not(.active) { background: var(--hover); }
.tb.active { background: var(--ink); color: #fff; }
.tb.active small { color: #d8d6d0; }
.tb:disabled { opacity: 0.3; }
.sep { width: 36px; height: 1px; background: var(--line); margin: 5px 0; flex: none; }
/* iPad en paysage : tous les boutons doivent tenir en hauteur (la barre ne défile pas au doigt) */
@media (max-height: 860px) {
  .toolbar { gap: 0; }
  .tb { padding: 4px 0 2px; }
  .sep { margin: 3px 0; }
}
.ring { width: 26px; height: 26px; border-radius: 50%; border: 1.5px solid var(--line); display: grid; place-items: center; background: var(--paper); }
.ring .dot { display: block; border-radius: 50%; }

.stage { position: relative; flex: 1; min-width: 0; overflow: hidden; background: var(--desk); touch-action: none; }
.layers { position: absolute; inset: 0; transform-origin: 0 0; touch-action: none; }
.layers canvas { position: absolute; left: 0; top: 0; pointer-events: none; }
/* L'encre se mélange au fond en « produit » : le surligneur laisse le texte noir lisible, comme sur papier. */
.layers canvas.ink, .layers canvas.live { mix-blend-mode: multiply; }
/* Onglets des carnets ouverts, en haut de l'écran ; ils rétrécissent pour toujours tenir sur la largeur. */
.tabbar {
  position: absolute;
  top: calc(7px + env(safe-area-inset-top));
  /* Centrée sur toute la largeur (avec left: 50 %, elle n'aurait que la moitié de la place et couperait les noms) */
  left: 0;
  right: 0;
  margin: 0 auto;
  width: -webkit-fit-content;
  width: fit-content;
  max-width: calc(100% - 24px);
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 3px;
  border-radius: 12px;
  background: rgba(251, 250, 247, 0.9);
  border: 1px solid var(--line);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}
.tabbar:empty { display: none; }
.tab { flex: 0 1 auto; max-width: 220px; min-width: 72px; display: flex; align-items: center; border-radius: 9px; color: var(--muted); }
.tab.on { background: var(--ink); color: #fff; }
.tab:not(.on):hover, .tab-add:hover { background: var(--hover); }
.tab-name {
  flex: 1;
  min-width: 0;
  padding: 6px 2px 6px 12px;
  font-size: 13px;
  text-align: left;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.tab.on .tab-name { font-weight: 600; }
.tab-close, .tab-add { flex: none; width: 30px; height: 30px; display: grid; place-items: center; border-radius: 8px; }
.tab-close svg { width: 13px; height: 13px; }
/* Plein écran : barre d'outils masquée, un bouton en haut à gauche pour la rappeler */
.show-tools {
  display: none;
  position: absolute;
  top: calc(7px + env(safe-area-inset-top));
  left: calc(10px + env(safe-area-inset-left));
  width: 42px;
  height: 42px;
  place-items: center;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
}
.show-tools:hover { background: var(--hover); }
.editor.full .toolbar { display: none; }
.editor.full .show-tools { display: grid; }
.editor.full .tabbar { max-width: calc(100% - 130px); }
.tab-add svg { width: 18px; height: 18px; }
.pagebar {
  position: absolute;
  bottom: calc(14px + env(safe-area-inset-bottom));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
}
.pb { width: 40px; height: 36px; display: grid; place-items: center; border-radius: 10px; }
.pb:hover { background: var(--hover); }
.pl { min-width: 64px; text-align: center; font-size: 14px; font-variant-numeric: tabular-nums; }
.selbar {
  position: absolute;
  z-index: 3;
  transform: translateX(-50%);
  display: flex;
  gap: 2px;
  padding: 4px;
  background: var(--ink);
  color: #fff;
  border-radius: 12px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}
.selbar button { padding: 8px 12px; border-radius: 8px; font-size: 14px; }
.selbar button:hover { background: #3a3a3a; }
.selbar .danger { color: #ff8a80; }
/* Fiche, Titre, Étiquette, Lien : bloc compact de 2 × 2, teinté, qui n'apparaît qu'avec une sélection au lasso */
.mark-tools {
  width: 58px;
  flex: none;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
  margin: 4px 0;
  padding: 3px;
  border-radius: 11px;
  background: #fdf3cf;
  border: 1px solid #ecd58a;
  animation: fade 0.15s ease-out;
}
.mark-tools[hidden] { display: none; }
.mt { height: 34px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; border-radius: 8px; color: #6b5300; }
.mt svg { width: 17px; height: 17px; }
.mt small { font-size: 8.5px; line-height: 1; }
.mt:hover { background: #f6e3a3; }
/* En plein écran (ou barre d'outils trop courte) : en haut à gauche de la page, sous le bouton des outils */
.mark-tools.floating { position: absolute; z-index: 4; top: calc(56px + env(safe-area-inset-top)); left: calc(10px + env(safe-area-inset-left)); margin: 0; box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12); }

/* ---------- Repères : fiches, titres, étiquettes, liens ---------- */

/* À droite d'un passage marqué ; un tap au stylet l'ouvre */
.links { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.mark {
  position: absolute;
  left: 0;
  top: 0;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
  white-space: nowrap;
}
.mark.icon { width: 26px; }
.mark svg { width: 17px; height: 17px; }
.mark.fiche { background: #fff3c4; border: 1.5px solid #e0b100; color: #8a6a00; }
.mark.lien { background: #e3efff; border: 1.5px solid #1c7ed6; color: #1864ab; }
.mark.pill { padding: 0 9px; font-size: 12px; font-weight: 600; color: #fff; }
.mark.titre { background: #e9edf3; border: 1.5px solid #8fa3c0; color: #4a5d78; }
.menu-dot { display: block; width: 14px; height: 14px; margin: 0 auto; border-radius: 50%; }

/* Sommaire, en tête de la vue des pages */
.ov-section { margin: 4px 0 10px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.toc { display: flex; flex-direction: column; gap: 4px; margin-bottom: 22px; }
.toc-row { display: flex; align-items: center; gap: 12px; padding: 6px 10px; border-radius: 10px; text-align: left; }
.toc-row:hover { background: var(--hover); }
.toc-row.sub { padding-left: 40px; }
.toc-row small { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
.shot { min-width: 0; flex: 0 1 auto; }
.shot canvas { display: block; height: 36px; width: auto; max-width: 100%; object-fit: contain; object-position: left; border-radius: 6px; }

/* Liste des étiquettes */
.tag-chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.tag-chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 99px; border: 1.5px solid var(--line); font-size: 13px; font-weight: 600; }
.tag-chip.all.on { background: var(--ink); border-color: var(--ink); color: #fff; }
.tag-chip .count { opacity: 0.75; font-variant-numeric: tabular-nums; }
.chip-btn { padding: 0; }
.tag-list { display: flex; flex-direction: column; gap: 6px; }
.tag-row { display: flex; align-items: center; gap: 14px; padding: 8px 10px; border-radius: 10px; text-align: left; border: 1px solid var(--line); }
.tag-row:hover { background: var(--hover); }
.tag-meta { margin-left: auto; display: flex; flex-direction: column; align-items: flex-end; gap: 4px; font-size: 13px; flex: none; }

/* Choix de la page d'un lien */
.link-pick { display: flex; flex-direction: column; gap: 4px; }
.pick-row { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding: 10px 12px; border-radius: 10px; text-align: left; }
.pick-row:hover { background: var(--hover); }
.pick-name { font-weight: 600; }
.pick-back { align-self: flex-start; padding: 6px 10px; margin-bottom: 10px; border-radius: 8px; color: var(--muted); }
.pick-back:hover { background: var(--hover); }
/* Fiche ouverte par-dessus la page ; les onglets restent visibles pour y déposer la fiche */
.tabbar { z-index: 6; transition: box-shadow 0.15s, border-color 0.15s; }
.tabbar.drop { border-color: #e0b100; box-shadow: 0 0 0 3px rgba(224, 177, 0, 0.35); }
.card-view {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: calc(56px + env(safe-area-inset-top)) 16px 16px;
  background: rgba(28, 28, 28, 0.22);
  animation: fade 0.15s ease-out;
}
.card-panel {
  border-radius: 16px;
  overflow: hidden;
  background: var(--panel);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
  animation: rise 0.18s ease-out;
}
.card-head { display: flex; align-items: center; gap: 8px; height: 46px; padding: 0 6px 0 12px; border-bottom: 1px solid var(--line); cursor: grab; }
.card-grip { color: #b08a00; }
.card-title { flex: 1; min-width: 0; font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-btn { height: 34px; padding: 0 12px; border-radius: 9px; font-size: 14px; }
.card-btn.icon { width: 34px; padding: 0; display: grid; place-items: center; }
.card-btn.icon svg { width: 16px; height: 16px; }
.card-btn:hover { background: var(--hover); }
/* Éditeur de la fiche : outils en une rangée au-dessus de la fiche */
.editor.card-editor { height: auto; flex-direction: column; }
.card-editor .toolbar {
  width: auto;
  flex-direction: row;
  justify-content: center;
  padding: 3px 6px;
  border-right: none;
  border-bottom: 1px solid var(--line);
  overflow: hidden;
}
.card-editor .tb { width: 52px; padding: 4px 0 2px; }
.card-editor .sep { width: 1px; height: 30px; margin: 0 4px; }
.card-editor .stage { flex: none; }
.sel-handle {
  position: absolute;
  z-index: 3;
  width: 36px;
  height: 36px;
  transform: translate(-50%, -50%);
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--paper);
  border: 1.5px solid var(--ink);
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.18);
  cursor: nwse-resize;
  touch-action: none;
}
.sel-handle svg { width: 18px; height: 18px; }
.sel-handle.rot { cursor: grab; font-size: 11px; font-weight: 600; font-variant-numeric: tabular-nums; }

.toolopts { width: 290px; padding: 8px; display: flex; flex-direction: column; gap: 14px; }
.opt-title { font-weight: 600; font-size: 14px; }
.sizes, .colors, .modes { display: flex; flex-wrap: wrap; gap: 8px; }
.colors.pad { padding: 8px; max-width: 240px; }
.size {
  width: 52px;
  height: 40px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--paper);
  display: grid;
  place-items: center;
}
.size span { display: block; border-radius: 50%; }
.size.on, .mode.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.swatch { width: 32px; height: 32px; border-radius: 50%; border: 1px solid rgba(0, 0, 0, 0.15); }
.swatch.on { box-shadow: 0 0 0 2px var(--panel), 0 0 0 4px var(--ink); }
.mode { flex: 1; padding: 9px; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); font-size: 14px; }
.swatch.wheel-btn { background: conic-gradient(#f33, #fd2, #3d4, #2dd, #36f, #d3f, #f33); border-color: transparent; }
.size-custom { display: flex; align-items: center; gap: 10px; }
.size-preview {
  width: 44px;
  height: 44px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--paper);
  overflow: hidden;
}
.size-preview span { display: block; border-radius: 50%; }
.opt-label { font-size: 13px; color: var(--muted); }
.size-value { min-width: 54px; text-align: right; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

/* Curseurs (taille, luminosité) */
.slider { flex: 1; min-width: 0; height: 28px; margin: 0; background: transparent; -webkit-appearance: none; appearance: none; }
.slider::-webkit-slider-runnable-track { height: 6px; border-radius: 3px; background: var(--line); }
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -10px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid rgba(0, 0, 0, 0.15);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.3);
}
.slider::-moz-range-track { height: 6px; border-radius: 3px; background: var(--line); }
.slider::-moz-range-thumb { width: 24px; height: 24px; border-radius: 50%; background: #fff; border: 1px solid rgba(0, 0, 0, 0.15); box-shadow: 0 1px 5px rgba(0, 0, 0, 0.3); }
.slider.light { height: 14px; border-radius: 7px; }
.slider.light::-webkit-slider-runnable-track { height: 14px; border-radius: 7px; background: transparent; }
.slider.light::-webkit-slider-thumb { margin-top: -6px; }
.slider.light::-moz-range-track { background: transparent; }

/* Roue des couleurs */
.cp { display: flex; flex-direction: column; align-items: center; gap: 18px; }
.wheel { display: block; border-radius: 50%; touch-action: none; cursor: crosshair; }
.cp-row { display: flex; align-items: center; gap: 12px; width: 100%; font-size: 14px; color: var(--muted); }
.cp-old, .cp-new { width: 40px; height: 40px; flex: none; border-radius: 10px; border: 1px solid rgba(0, 0, 0, 0.12); }
.cp-hex { color: var(--ink); font-weight: 500; font-variant-numeric: tabular-nums; }

.overview { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 16px; }
.ov-page { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 6px; border-radius: 10px; border: 2px solid transparent; }
.ov-page.on { border-color: var(--ink); }
.ov-page.picked { border-color: var(--accent, #0ea5a4); background: color-mix(in srgb, var(--accent, #0ea5a4) 10%, transparent); }
.ov-page.picked small { color: var(--ink); font-weight: 600; }
.ov-actions { display: flex; align-items: center; gap: 10px; margin: 0 0 14px; padding: 10px 12px; background: var(--hover); border-radius: 10px; }
.ov-actions .ov-count { flex: 1; font-weight: 500; }
.ov-page small { font-size: 12px; color: var(--muted); }
.ov-thumb {
  width: 100%;
  aspect-ratio: 210 / 297;
  background: var(--paper);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  overflow: hidden;
  display: grid;
  place-items: center;
  color: var(--muted);
}
.ov-thumb canvas { width: 100%; height: auto; align-self: start; }
.ov-page.add .ov-thumb { background: none; box-shadow: none; border: 1.5px dashed var(--line); }
.ov-cell { position: relative; display: flex; }
.ov-cell .ov-page { flex: 1; min-width: 0; }
/* « + » à cheval entre deux miniatures : insère une page à cet endroit */
.ov-insert {
  position: absolute;
  top: 40%;
  right: -15px;
  z-index: 1;
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.12);
  color: var(--muted);
}
.ov-insert svg { width: 16px; height: 16px; }
.ov-insert:hover { color: var(--ink); }

/* ---------- Fenêtres, menus ---------- */

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(28, 28, 28, 0.28);
  animation: fade 0.15s ease-out;
}
.modal {
  width: min(440px, 100%);
  max-height: 86vh;
  overflow: auto;
  padding: 22px;
  border-radius: 16px;
  background: var(--panel);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  animation: rise 0.18s ease-out;
}
.modal.wide { width: min(860px, 100%); }
.modal h2 { margin: 0 0 14px; font-size: 18px; }
.modal-actions { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; margin-top: 20px; }
.templates { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.tpl { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 6px 4px; border-radius: 10px; border: 2px solid transparent; }
.tpl.on { border-color: var(--ink); }
.tpl-prev { display: block; width: 100%; aspect-ratio: 210 / 297; background: var(--paper); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15); overflow: hidden; }
.tpl-prev canvas { display: block; width: 100%; height: 100%; }
.tpl small { font-size: 11px; color: var(--muted); text-align: center; line-height: 1.2; }
.progress { height: 6px; margin-top: 14px; border-radius: 3px; background: var(--line); overflow: hidden; }
.progress span { display: block; width: 0; height: 100%; background: var(--ink); transition: width 0.2s; }
.tree { display: flex; flex-direction: column; gap: 2px; max-height: 50vh; overflow: auto; }
.tree-item { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-radius: 9px; text-align: left; }
.tree-item:hover:not(:disabled) { background: var(--hover); }
.tree-item:disabled { opacity: 0.45; }

.pop-layer { position: fixed; inset: 0; z-index: 40; }
.popover {
  position: fixed;
  padding: 6px;
  min-width: 200px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.18);
  animation: fade 0.12s ease-out;
}
.menu { display: flex; flex-direction: column; }
.menu-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 9px; text-align: left; font-size: 15px; }
.menu-item:hover { background: var(--hover); }
.menu-item.danger { color: var(--danger); }
.menu-item .ic { width: 20px; }
.menu-item svg { width: 19px; height: 19px; }
.menu-sep { height: 1px; margin: 4px 6px; background: var(--line); }
.check { position: relative; width: 34px; height: 20px; margin-left: auto; border-radius: 10px; background: var(--line); transition: background 0.15s; }
.check::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left 0.15s; }
.check.on { background: var(--ink); }
.check.on::after { left: 16px; }

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(80px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 60;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--ink);
  color: #fff;
  font-size: 14px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}

/* Bandeau de mise à jour */
.update-bar {
  position: fixed;
  top: calc(12px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  z-index: 55;
  display: flex;
  align-items: center;
  gap: 14px;
  max-width: calc(100% - 32px);
  padding: 8px 8px 8px 18px;
  border-radius: 16px;
  background: var(--ink);
  color: #fff;
  font-size: 15px;
  font-weight: 500;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  animation: drop-in 0.25s ease-out;
}
.update-bar .btn.primary { background: #fff; color: var(--ink); border-color: #fff; }

@keyframes drop-in { from { opacity: 0; transform: translate(-50%, -10px); } }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Scanner ---------- */

.scanner { height: 100%; display: flex; flex-direction: column; background: #1d1d1f; color: #f4f4f4; }
.scanner .btn.ghost { color: #f4f4f4; }
.scanner .btn.primary { background: #f4f4f4; color: #1d1d1f; border-color: #f4f4f4; }
.scanner .icon-btn:hover { background: #333336; }
.scanner .muted { color: #a1a1a6; }
.scan-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: calc(10px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right)) 10px calc(16px + env(safe-area-inset-left));
}
.scan-title { font-weight: 600; }
.scan-main { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; }
.crop-canvas { position: absolute; inset: 0; touch-action: none; }
.adjust-canvas { box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5); }
.busy { color: #a1a1a6; }
.scan-start { display: flex; flex-direction: column; align-items: center; gap: 14px; width: min(360px, 100%); padding: 20px; text-align: center; }
.big-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 18px;
  border-radius: 14px;
  background: #f4f4f4;
  color: #1d1d1f;
  font-size: 17px;
  font-weight: 600;
}
.big-btn.alt { background: #333336; color: #f4f4f4; }
.scan-controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px calc(16px + env(safe-area-inset-right)) 10px calc(16px + env(safe-area-inset-left));
}
.scan-controls:empty { display: none; }
.chips { flex: 1; display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.chip { padding: 8px 12px; border-radius: 20px; background: #333336; font-size: 14px; }
.chip.on { background: #f4f4f4; color: #1d1d1f; }
.scan-strip {
  display: flex;
  gap: 12px;
  padding: 12px calc(16px + env(safe-area-inset-right)) calc(12px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
  overflow-x: auto;
  border-top: 1px solid #333336;
}
.strip-item { position: relative; flex: none; height: 84px; }
.strip-item img { display: block; height: 100%; border-radius: 4px; }
.strip-n { position: absolute; left: 4px; bottom: 4px; padding: 1px 6px; border-radius: 6px; background: rgba(0, 0, 0, 0.6); font-size: 11px; }
.strip-del { position: absolute; top: -7px; right: -7px; width: 24px; height: 24px; display: grid; place-items: center; border-radius: 50%; background: #f4f4f4; color: #1d1d1f; }
.strip-del svg { width: 14px; height: 14px; }

/* ---------- Petits écrans ---------- */

@media (max-width: 900px) {
  .act:not(.primary) .act-label { display: none; }
  .search { width: 120px; }
  .back-btn { max-width: 30%; }
  .lib-title { font-size: 22px; }
}

@media (max-width: 600px) {
  .act-label { display: none; }
  .search { width: 110px; }
  .templates { grid-template-columns: repeat(3, 1fr); }
}
