:root {
  --violet: #6d28d9; --violet-clair: #ede9fe; --rose: #db2777; --vert: #059669; --vert-clair: #d1fae5;
  --bleu: #2563eb; --bleu-clair: #dbeafe; --orange: #d97706; --orange-clair: #fef3c7; --jaune-clair: #fefce8;
  --rouge: #dc2626; --encre: #1f2937; --gris: #6b7280; --bord: #e5e7eb; --fond: #f1effa;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; overflow: hidden; }
body { margin: 0; font-family: "Segoe UI", Arial, sans-serif; color: var(--encre); background: var(--fond); display: flex; flex-direction: column; overflow: hidden; }

/* ---------- barre du haut ---------- */
.barre { background: linear-gradient(90deg, var(--violet), var(--rose)); color: #fff; padding: 8px 12px; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; box-shadow: 0 2px 10px rgba(76,29,149,.3); z-index: 5; }
.marque { font-size: 18px; margin-right: 4px; white-space: nowrap; }
.nom-fichier { font-size: 13px; opacity: .92; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.espace { flex: 1; }
button, .bouton { font: inherit; font-size: 14px; border: 0; border-radius: 8px; padding: 7px 12px; cursor: pointer; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
button:disabled { opacity: .45; cursor: not-allowed; }
button:focus-visible, .bouton:focus-within { outline: 3px solid #fde047; outline-offset: 1px; }
.b-blanc { background: #fff; color: var(--violet); }
.b-vert { background: var(--vert); color: #fff; }
.barre .b-vert { background: #10b981; box-shadow: 0 0 0 2px rgba(255,255,255,.6); }
.b-violet { background: var(--violet); color: #fff; }
.b-bleu { background: var(--bleu); color: #fff; }
.b-gris { background: #fff; color: var(--encre); border: 1px solid var(--bord); }
.b-rouge { background: #fff; color: var(--rouge); border: 1px solid #fecaca; }
.b-transparent { background: transparent; color: #fff; padding: 4px 9px; font-size: 17px; }
.b-transparent:hover:not(:disabled) { background: rgba(255,255,255,.2); }
.modes, .groupe { display: inline-flex; align-items: center; gap: 2px; background: rgba(255,255,255,.18); border-radius: 10px; padding: 3px; font-size: 13px; }
.groupe span { padding: 0 4px; font-weight: 600; }
.modes button { background: transparent; color: #fff; padding: 6px 10px; }
.modes button.actif { background: #fff; color: var(--rose); }
#pageCourante { width: 52px; padding: 3px 4px; border-radius: 6px; border: 0; font: inherit; text-align: center; }

.bandeau { background: #fef3c7; color: #92400e; border-bottom: 1px solid #fcd34d; padding: 8px 16px; font-size: 14px; }
.bandeau.erreur { background: #fee2e2; color: #991b1b; border-color: #fca5a5; }

main { flex: 1; display: flex; min-height: 0; }

/* ---------- vignettes ---------- */
#vignettes { width: 150px; overflow-y: auto; background: #fff; border-right: 1px solid var(--bord); padding: 10px 8px; display: flex; flex-direction: column; gap: 10px; }
#vignettes:empty { display: none; }
.vignette { position: relative; cursor: pointer; border: 2px solid transparent; border-radius: 6px; padding: 3px; text-align: center; font-size: 12px; color: var(--gris); }
.vignette canvas { width: 100%; display: block; background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,.2); }
.vignette.courante { border-color: var(--violet); background: var(--violet-clair); color: var(--violet); font-weight: 600; }
.vignette .badges { position: absolute; top: 4px; right: 4px; display: flex; flex-direction: column; gap: 2px; align-items: flex-end; }
.badge { font-size: 10px; font-weight: 700; border-radius: 9px; padding: 1px 6px; color: #fff; }
.badge.modif { background: var(--rose); }
.badge.ocr { background: var(--bleu); }
.badge.alerte { background: var(--orange); }

/* ---------- pages ---------- */
#visionneuse { flex: 1; overflow: auto; position: relative; }
#pages { padding: 24px; display: flex; flex-direction: column; align-items: center; gap: 22px; min-width: min-content; }
#pages:empty { display: none; }
.page { position: relative; background: #fff; box-shadow: 0 4px 18px rgba(76,29,149,.18); flex: none; }
.page canvas.rendu { position: absolute; inset: 0; width: 100%; height: 100%; }
.page .numero { position: absolute; top: -18px; left: 0; font-size: 12px; color: var(--gris); }
.page .attente { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; color: #c4b5fd; font-size: 14px; }
.calque { position: absolute; inset: 0; z-index: 3; }
.mode-ajout .calque { cursor: crosshair; }
.mode-lire .calque { display: none; }
.zone { position: absolute; cursor: text; border-radius: 2px; }
.montrer-zones .zone { outline: 1px dashed rgba(109,40,217,.45); }
.montrer-zones .zone.ocr { outline-color: rgba(37,99,235,.55); }
.montrer-zones .zone.ocr.douteux { outline: 1px dashed var(--orange); background: rgba(217,119,6,.08); }
.zone:hover { background: rgba(109,40,217,.12); outline: 1px solid var(--violet) !important; }
.zone.choisie { outline: 2px solid var(--rose) !important; }
.zone.champ { outline: 1.5px dashed #16a34a !important; background: rgba(22,163,74,.06); cursor: pointer; display: flex; align-items: center; justify-content: center; overflow: hidden; white-space: pre; font-family: Arial, sans-serif; color: #111; }
.zone.champ.modifie { background: #fff; outline: 2px solid #16a34a !important; justify-content: flex-start; padding-left: 2px; }
.zone.champ.lecture-seule { outline-color: #9ca3af !important; cursor: not-allowed; }
.mode-ajout .zone.champ { pointer-events: none; }
.apercu { position: absolute; white-space: pre; line-height: 1.2; pointer-events: none; }
.apercu.ajout { pointer-events: auto; cursor: pointer; outline: 1px dashed var(--vert); }
.en-place { position: absolute; z-index: 4; border: 0; outline: 2px solid var(--rose); border-radius: 2px; padding: 0; margin: 0; resize: none; overflow: hidden; line-height: 1.2; white-space: pre; box-shadow: 0 0 0 4px rgba(219,39,119,.15); }

/* couche de texte pdf.js (mode Lire & copier) */
.textLayer { position: absolute; text-align: initial; inset: 0; overflow: hidden; opacity: 1; line-height: 1; -webkit-text-size-adjust: none; text-size-adjust: none; forced-color-adjust: none; transform-origin: 0 0; z-index: 2; pointer-events: none; }
.mode-lire .textLayer { pointer-events: auto; z-index: 4; }
.textLayer span, .textLayer br { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(37, 99, 235, .3); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: -1; cursor: default; user-select: none; }
.textLayer .endOfContent.active { top: 0; }

/* ---------- accueil ---------- */
#accueil { margin: 40px auto; text-align: center; border: 3px dashed #c4b5fd; border-radius: 20px; padding: 36px 40px; background: #fff; max-width: 560px; }
#accueil.survol, body.survol #accueil { border-color: var(--rose); background: #fdf2f8; }
.accueil-icone { font-size: 52px; }
#accueil h2 { color: var(--violet); margin: 6px 0; }
#accueil ul { text-align: left; display: inline-block; margin: 8px 0; padding-left: 18px; line-height: 1.8; }

/* ---------- panneau de droite ---------- */
aside { width: 370px; background: #fff; border-left: 1px solid var(--bord); overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.carte { border: 1px solid var(--bord); border-radius: 12px; padding: 12px; }
.carte h2 { font-size: 15px; margin: 0 0 8px; }
.carte.violet { border-color: #c4b5fd; background: var(--violet-clair); }
.carte.vert { border-color: #6ee7b7; background: var(--vert-clair); }
.carte.bleu { border-color: #93c5fd; background: var(--bleu-clair); }
.carte.jaune { border-color: #fde68a; background: var(--jaune-clair); }
.carte.orange { border-color: #fcd34d; background: var(--orange-clair); }
.carte.formulaire { border-color: #86efac; background: #f0fdf4; }
.champ-ligne { background: #fff; border: 1px solid #bbf7d0; border-radius: 8px; padding: 2px 8px 8px; margin-top: 8px; transition: box-shadow .3s; }
.champ-ligne label { margin-top: 6px; }
.champ-ligne.modifie { border-color: #16a34a; box-shadow: inset 3px 0 0 #16a34a; }
.champ-ligne.eclaire { box-shadow: 0 0 0 3px #fde047; }
.champ-ligne .origine-champ:empty { display: none; }
#listeChamps { max-height: 340px; overflow-y: auto; }
aside { position: relative; }
label { font-size: 13px; font-weight: 600; display: block; margin: 8px 0 4px; }
input[type=text], input[type=number], select, textarea { width: 100%; font: inherit; font-size: 14px; padding: 6px 8px; border: 1px solid #c4b5fd; border-radius: 8px; background: #fff; color: var(--encre); }
textarea { resize: vertical; }
#texteExtrait { font-size: 12.5px; font-family: Consolas, "Courier New", monospace; min-height: 140px; }
.ligne { display: flex; gap: 8px; align-items: end; flex-wrap: wrap; }
.ligne > * { flex: 1; }
.ligne .large { flex: 3; }
.ligne .etroit { flex: 0 0 80px; }
.case { display: flex; align-items: center; gap: 6px; font-size: 13px; font-weight: 500; margin: 5px 0; }
.case input { width: auto; margin: 0; }
input[type=color] { width: 100%; height: 34px; border: 1px solid #c4b5fd; border-radius: 8px; padding: 2px; background: #fff; }
.origine { font-size: 13px; background: #fff; border: 1px dashed #c4b5fd; border-radius: 8px; padding: 6px 8px; color: var(--gris); word-break: break-word; white-space: pre-wrap; }
.pastille { font-size: 11px; font-weight: 700; border-radius: 8px; padding: 1px 7px; background: var(--violet); color: #fff; }
.pastille.ocr { background: var(--bleu); }
.alerte { font-size: 13px; color: var(--rouge); font-weight: 600; margin-top: 6px; }
.aide { font-size: 12.5px; color: var(--gris); line-height: 1.45; }
.boutons { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.progression { height: 10px; background: #fff; border-radius: 6px; overflow: hidden; margin-top: 10px; border: 1px solid #93c5fd; }
.progression div { height: 100%; width: 0; background: linear-gradient(90deg, var(--bleu), var(--violet)); transition: width .3s; }
#alertesTexte div { font-size: 12.5px; background: #fff7ed; border: 1px solid #fed7aa; color: #9a3412; border-radius: 8px; padding: 5px 8px; margin: 6px 0; }
#liste { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 300px; overflow-y: auto; }
#liste li { background: #fff; border-radius: 8px; padding: 6px 8px; font-size: 12.5px; cursor: pointer; border: 1px solid #fde68a; display: flex; gap: 6px; align-items: flex-start; }
#liste li:hover { border-color: var(--orange); }
#liste .contenu { flex: 1; word-break: break-word; }
#liste .avant { text-decoration: line-through; color: var(--gris); }
#liste .apres { color: var(--vert); font-weight: 600; }
#liste .statut { font-size: 11px; font-weight: 700; border-radius: 8px; padding: 1px 6px; white-space: nowrap; }
#liste .statut.retire { background: var(--vert-clair); color: var(--vert); }
#liste .statut.masque { background: var(--bleu-clair); color: var(--bleu); }
#liste .statut.echec { background: #fee2e2; color: var(--rouge); }
#liste .suppr { background: none; border: 0; color: var(--gris); padding: 0 2px; font-size: 14px; }

/* ---------- divers ---------- */
#occupe { position: fixed; inset: 0; background: rgba(31,41,55,.35); display: flex; align-items: center; justify-content: center; z-index: 20; }
.occupe-boite { background: #fff; border-radius: 14px; padding: 22px 28px; display: flex; align-items: center; gap: 14px; font-weight: 600; color: var(--violet); box-shadow: 0 10px 40px rgba(0,0,0,.25); max-width: 80vw; }
.roue { width: 26px; height: 26px; border: 4px solid var(--violet-clair); border-top-color: var(--violet); border-radius: 50%; animation: tourner 0.8s linear infinite; }
@keyframes tourner { to { transform: rotate(360deg); } }
#message { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--encre); color: #fff; padding: 10px 18px; border-radius: 10px; font-size: 14px; display: none; z-index: 30; max-width: 90vw; box-shadow: 0 6px 24px rgba(0,0,0,.25); }
#message.erreur { background: var(--rouge); }
#message.succes { background: var(--vert); }

/* ---------- IA sur une zone ---------- */
.carte.rose { border-color: #f9a8d4; background: linear-gradient(160deg, #fdf2f8, #f5f3ff); }
.carte.rose h2 { color: var(--rose); }
.b-rose { background: var(--rose); color: #fff; }
.mode-ia .calque { cursor: crosshair; }
.mode-ia .zone, .mode-ia .apercu, .mode-ia .zone.champ { pointer-events: none; }
.mode-ia .textLayer { pointer-events: none; }
.selection-ia { position: absolute; z-index: 5; pointer-events: none; border: 2px dashed var(--rose); background: rgba(219,39,119,.08); border-radius: 3px; box-shadow: 0 0 0 9999px rgba(31,41,55,.06); }
.selection-ia.en-cours { box-shadow: none; }
.dessin-ia { position: absolute; z-index: 3; pointer-events: none; user-select: none; }
.dessin-ia.apercu-ia { z-index: 4; outline: 2px solid var(--violet); outline-offset: 1px; animation: clignoter 1.6s ease-in-out infinite; }
@keyframes clignoter { 50% { outline-color: var(--rose); } }
.ia-apercu { display: flex; flex-direction: column; gap: 4px; margin-bottom: 8px; }
.ia-apercu img { max-width: 100%; max-height: 160px; object-fit: contain; background: #fff; border: 1px solid var(--bord); border-radius: 8px; }
.puces { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.puce { background: #fff; border: 1px solid #f9a8d4; color: var(--rose); border-radius: 999px; padding: 3px 10px; font-size: 12px; cursor: pointer; }
.puce:hover { background: #fdf2f8; }
.ia-attente { margin-top: 8px; padding: 8px 10px; border-radius: 8px; background: #fff; color: var(--violet); font-weight: 600; animation: clignoter-fond 1.4s ease-in-out infinite; }
@keyframes clignoter-fond { 50% { background: var(--violet-clair); } }
.ia-texte { margin-top: 8px; padding: 10px; background: #fff; border: 1px solid var(--bord); border-radius: 8px; white-space: pre-wrap; font-size: 13px; line-height: 1.45; max-height: 320px; overflow: auto; }
.ia-dessin { display: block; margin-top: 8px; max-width: 100%; max-height: 240px; background: repeating-conic-gradient(#f3f4f6 0 25%, #fff 0 50%) 0 0 / 16px 16px; border: 1px solid var(--bord); border-radius: 8px; }
.lien { margin-top: 8px; color: var(--rose); font-size: 13px; padding: 0; background: none; border: 0; cursor: pointer; }
.credit { margin-top: 18px; font-size: 12px; color: var(--gris); }
.credit a { color: var(--violet); font-weight: 600; text-decoration: none; }

@media (max-width: 900px) {
  body { overflow: auto; }
  main { flex-direction: column; }
  #vignettes { display: none; }
  aside { width: auto; border-left: 0; border-top: 1px solid var(--bord); }
}
