:root {
  --bg: #f1f0ec;
  --surface: #ffffff;
  --ink: #1b1b1a;
  --muted: #7a7a74;
  --line: #deddd6;
  --accent: #e4572e;
  --danger: #c0392b;
  --ok: #2a9d6f;
  --warn: #f0a202;
  --radius: 8px;
  --pw: 140px; /* largeur d'une page sur le tableau */
  --ratio: 0.7667;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); font-size: 14px; }
body.no-scroll { overflow: hidden; }
h1, h2, h3 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); }
.print-only { display: none; }

/* Boutons ------------------------------------------------------------------ */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 7px 12px; border: 1px solid var(--line); border-radius: 6px;
  background: var(--surface); cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #bdbcb4; }
.btn.primary { background: var(--ink); border-color: var(--ink); color: #fff; }
.btn.primary.danger, .btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: rgba(0,0,0,.05); }
.btn.light { color: #fff; }
.btn:disabled { opacity: .5; cursor: default; }
.link { border: 0; background: none; padding: 0; color: var(--ink); text-decoration: underline; text-underline-offset: 2px; cursor: pointer; font-size: 12px; }
.link.danger { color: var(--danger); }
input:not([type=range]):not([type=color]), select, textarea {
  padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); width: 100%;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--ink); outline-offset: -1px; }

.logo { display: inline-flex; gap: 2px; }
.logo span { width: 10px; height: 16px; background: var(--ink); }
.logo span + span { background: var(--accent); }

/* Connexion ---------------------------------------------------------------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { background: var(--surface); padding: 40px; border-radius: 14px; text-align: center; width: min(380px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.08); }
.login-card h1 { margin: 14px 0 6px; font-size: 24px; }
.auth-form { display: grid; gap: 8px; margin-top: 20px; text-align: left; }
.auth-form .btn { justify-content: center; }

/* Barre du haut ------------------------------------------------------------ */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 20px; flex-wrap: wrap;
  padding: 12px 20px; background: rgba(241,240,236,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand h1 { font-size: 17px; }
.stats { flex: 1; min-width: 200px; max-width: 460px; }
.progress { display: flex; height: 6px; border-radius: 3px; overflow: hidden; background: var(--line); }
.legend { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 6px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }
.tools { display: flex; align-items: center; gap: 8px; margin-left: auto; flex-wrap: wrap; }
.zoom { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.zoom input { width: 110px; accent-color: var(--ink); }
.user { display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 4px 8px; border-radius: 20px; }
.user:hover { background: rgba(0,0,0,.05); }
.user img, .avatar { width: 26px; height: 26px; border-radius: 50%; }
.avatar { display: grid; place-items: center; background: var(--ink); color: #fff; font-size: 11px; font-weight: 600; }

.selection-bar {
  position: sticky; top: 64px; z-index: 15; margin: 12px 20px 0;
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--ink); color: #fff; border-radius: var(--radius);
}
.selection-bar select { width: auto; color: var(--ink); }
.selection-bar .btn { color: var(--ink); }
.selection-bar .btn.ghost { color: #fff; }

.swatches { display: flex; gap: 4px; flex-wrap: wrap; }
.swatches button { width: 20px; height: 20px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.swatches button.on { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--ink); }
.swatches button.none { background: var(--surface); border: 1px dashed var(--muted); color: var(--muted); font-size: 11px; line-height: 1; }

/* Tableau (planches) ------------------------------------------------------- */
.board {
  display: flex; flex-wrap: wrap; gap: 28px 26px; padding: 24px 20px 12px;
  align-items: flex-start;
}
.spread { display: flex; }
.slot { width: var(--pw); }
.hint { padding: 0 20px 40px; }

.page { position: relative; cursor: pointer; user-select: none; }
.page .sheet {
  position: relative; aspect-ratio: var(--ratio); background: var(--surface);
  display: grid; place-items: center; overflow: hidden;
  box-shadow: 0 1px 2px rgba(0,0,0,.12), 0 0 0 1px rgba(0,0,0,.04);
  border-bottom: 4px solid var(--status);
  transition: transform .12s, box-shadow .12s;
}
.page.left .sheet { box-shadow: inset -8px 0 10px -8px rgba(0,0,0,.18), 0 1px 2px rgba(0,0,0,.12); }
.page.right .sheet { box-shadow: inset 8px 0 10px -8px rgba(0,0,0,.18), 0 1px 2px rgba(0,0,0,.12); }
.page:hover .sheet { transform: translateY(-2px); }
.page .sheet img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; }
.page .empty-title { padding: 8px; text-align: center; font-size: 12px; color: var(--muted); word-break: break-word; }
.page .rubric { position: absolute; top: 0; left: 0; right: 0; height: 5px; z-index: 1; }
.page.selected .sheet { outline: 3px solid var(--ink); outline-offset: 2px; }
.page.dragging { opacity: .35; }
.page.drop-target .sheet { outline: 3px dashed var(--ink); outline-offset: 3px; transform: scale(1.03); }
.page.drop-target.drop-files .sheet { outline-color: var(--accent); }

.badge {
  position: absolute; z-index: 2; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; font-size: 10.5px; font-weight: 700; line-height: 18px; text-align: center;
}
.badge.comments { top: 8px; right: 6px; background: var(--accent); color: #fff; }
.badge.presence { top: 8px; left: 6px; background: var(--ink); color: #fff; }
.badge.versions { bottom: 4px; left: 5px; background: rgba(255,255,255,.9); color: var(--ink); border: 1px solid var(--line); }
.menu-btn {
  position: absolute; bottom: 4px; right: 4px; z-index: 2; opacity: 0;
  width: 24px; height: 20px; border-radius: 4px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; line-height: 1;
}
.page:hover .menu-btn, .menu-btn:focus { opacity: 1; }

.groupbar { height: 3px; margin-top: 4px; position: relative; }
.groupbar span { position: absolute; top: 5px; left: 0; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; white-space: nowrap; }
.page.left .groupbar { margin-left: 0; }
.meta { display: flex; align-items: baseline; gap: 5px; margin-top: 16px; font-size: 12px; padding: 0 2px; }
.meta .num { font-weight: 700; font-variant-numeric: tabular-nums; }
.meta .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.meta .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; align-self: center; }

/* Menus contextuels -------------------------------------------------------- */
.menu {
  position: absolute; z-index: 1000; min-width: 200px; padding: 4px;
  background: var(--surface); border-radius: var(--radius); box-shadow: 0 8px 30px rgba(0,0,0,.18);
}
.menu button { display: block; width: 100%; text-align: left; padding: 8px 10px; border: 0; background: none; border-radius: 5px; cursor: pointer; }
.menu button:hover { background: var(--bg); }
.menu button.danger { color: var(--danger); }

/* Panneau activité --------------------------------------------------------- */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(360px, 100%); z-index: 30;
  background: var(--surface); box-shadow: -10px 0 40px rgba(0,0,0,.1); display: flex; flex-direction: column;
}
.panel header { display: flex; justify-content: space-between; align-items: center; padding: 16px; border-bottom: 1px solid var(--line); }
.panel ol { list-style: none; margin: 0; padding: 8px 16px; overflow: auto; }
.panel li { padding: 8px 0; border-bottom: 1px solid var(--bg); line-height: 1.4; }
.panel time { display: block; font-size: 11px; color: var(--muted); }

/* Boîtes de dialogue ------------------------------------------------------- */
dialog { border: 0; border-radius: 12px; padding: 0; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(20,20,18,.55); }
.form-dialog { width: min(480px, calc(100vw - 32px)); }
.form-dialog.wide { width: min(820px, calc(100vw - 32px)); }
.form-dialog form, #importBody { padding: 22px; display: grid; gap: 14px; }
.form-dialog label { display: grid; gap: 5px; font-size: 12px; color: var(--muted); }
.form-dialog .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-dialog h3 { font-size: 13px; margin-top: 6px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.groups { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.groups li { display: grid; grid-template-columns: 32px 1fr auto auto; gap: 8px; align-items: center; }
.groups input[type=color] { width: 32px; height: 32px; border: 0; padding: 0; background: none; cursor: pointer; }

.table-wrap { max-height: 50vh; overflow: auto; border: 1px solid var(--line); border-radius: var(--radius); }
.import-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.import-table td { padding: 7px 10px; border-bottom: 1px solid var(--bg); }
.import-table .arrow { color: var(--muted); }
.tag { display: inline-block; padding: 1px 7px; border-radius: 10px; font-size: 11px; background: var(--bg); color: var(--muted); white-space: nowrap; }
.tag.ok { background: #e3f4ec; color: var(--ok); }
.tag.warn { background: #fdf1d6; color: #9a6700; }
.progress-wrap .bar { height: 8px; background: var(--bg); border-radius: 4px; overflow: hidden; }
.progress-wrap .bar span { display: block; height: 100%; width: 0; background: var(--ink); transition: width .2s; }

/* Comptes ---------------------------------------------------------------- */
.users-body { padding: 22px; display: grid; gap: 16px; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; }
.users-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.users-table td { padding: 8px 6px; border-bottom: 1px solid var(--bg); }
.users-table td:last-child { text-align: right; white-space: nowrap; }
.users-table .link + .link { margin-left: 10px; }
.user-form { display: grid; gap: 8px; }
.user-form h3 { font-size: 13px; }
.row3 { display: grid; grid-template-columns: 1.3fr 1fr auto auto; gap: 8px; align-items: center; }
.form-dialog label.check, .check { display: flex; align-items: center; gap: 6px; font-size: 13px; white-space: nowrap; }
.credentials { background: #e3f4ec; border-radius: var(--radius); padding: 12px 14px; display: grid; gap: 6px; }
.credentials code { font-size: 15px; font-weight: 700; user-select: all; }
@media (max-width: 640px) { .row3 { grid-template-columns: 1fr; } }

/* Page en grand ------------------------------------------------------------ */
.page-dialog {
  width: calc(100vw - 32px); height: calc(100vh - 32px); max-width: 1500px; max-height: none;
  display: none; grid-template-columns: 1fr 360px; background: var(--bg);
}
.page-dialog[open] { display: grid; }
.viewer { position: relative; display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; padding: 20px 56px; }
.stage { position: relative; height: 100%; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; }
.stage.drop-target { outline: 3px dashed var(--accent); outline-offset: -8px; border-radius: 10px; }
.sheet-lg {
  position: relative; max-height: calc(100% - 50px); max-width: 100%; height: 100%;
  background: var(--surface); box-shadow: 0 4px 30px rgba(0,0,0,.15); cursor: crosshair;
}
.sheet-lg img { width: 100%; height: 100%; object-fit: contain; display: block; user-select: none; }
.sheet-lg .empty { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; color: var(--muted); font-size: 18px; }
.stage-hint { margin: 0; }
.banner { background: #fdf1d6; color: #7a5200; padding: 6px 12px; border-radius: 6px; font-size: 13px; }
.nav {
  position: absolute; top: 50%; transform: translateY(-50%); z-index: 3;
  width: 40px; height: 64px; border: 0; border-radius: 8px; background: rgba(255,255,255,.7); font-size: 30px; cursor: pointer; color: var(--ink);
}
.nav:hover { background: #fff; }
.nav.prev { left: 8px; }
.nav.next { right: 8px; }

.pin {
  position: absolute; z-index: 3; transform: translate(-50%, -50%);
  width: 26px; height: 26px; border-radius: 50%;
  background: var(--accent); color: #fff; border: 2px solid #fff; font-size: 11px; font-weight: 700;
  display: grid; place-items: center; cursor: pointer; box-shadow: 0 2px 6px rgba(0,0,0,.35); padding: 0;
}
.pin:hover { transform: translate(-50%, -50%) scale(1.15); }
.pin.resolved { background: var(--muted); opacity: .7; }
.pin.other { background: #9a7a6f; }
.pin.draft { background: var(--ink); }
.pin-form {
  position: absolute; z-index: 4; width: 260px; margin: 8px 0 0 -130px;
  background: var(--surface); padding: 10px; border-radius: 8px; box-shadow: 0 8px 30px rgba(0,0,0,.25);
  display: grid; gap: 8px; cursor: default;
}
.pin-form div { display: flex; justify-content: flex-end; gap: 6px; }

.side { background: var(--surface); overflow: auto; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.side header { display: flex; justify-content: space-between; align-items: flex-start; }
.big-label { font-size: 28px; font-weight: 750; letter-spacing: -0.02em; }
.presence-note { background: #eef3fb; color: #25508f; padding: 7px 10px; border-radius: 6px; font-size: 13px; }
.field { display: grid; gap: 6px; font-size: 12px; color: var(--muted); }
.segs { display: flex; gap: 4px; flex-wrap: wrap; }
.seg { flex: 1; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: pointer; font-size: 12px; color: var(--ink); }
.seg.on { background: var(--c); border-color: var(--c); color: #fff; font-weight: 600; }
.side section { border-top: 1px solid var(--line); padding-top: 12px; display: grid; gap: 8px; }
.side h3 { font-size: 13px; display: flex; align-items: center; gap: 8px; }
.count { background: var(--bg); border-radius: 9px; padding: 0 7px; font-size: 11px; color: var(--muted); }
.side h3 .link { margin-left: auto; font-weight: 400; }
.comments, .versions { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.comments li { display: grid; grid-template-columns: 22px 1fr; gap: 8px; padding: 8px; border-radius: 6px; background: var(--bg); transition: background .3s; }
.comments li.resolved { opacity: .6; }
.comments li.flash { background: #fde3d9; }
.comments p { margin: 4px 0; line-height: 1.4; word-break: break-word; }
.comments .c-actions { display: flex; gap: 10px; align-items: center; }
.pin-num { width: 22px; height: 22px; border-radius: 50%; background: var(--accent); color: #fff; font-size: 11px; font-weight: 700; display: grid; place-items: center; }
.resolved .pin-num { background: var(--muted); }
.versions li { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 6px; border-radius: 6px; }
.versions li.current { background: #f3faf6; }
.versions li.viewed { outline: 1px solid var(--ink); }
.versions img { width: 44px; height: 58px; object-fit: contain; background: var(--bg); }
.versions .v-actions { display: grid; gap: 4px; justify-items: end; }
.btn.upload { justify-content: center; }

/* Liseuse ------------------------------------------------------------------ */
.reader { position: fixed; inset: 0; z-index: 50; background: #1b1b1a; display: flex; align-items: center; justify-content: center; }
.reader-spread { display: flex; height: calc(100vh - 110px); max-width: calc(100vw - 120px); }
.r-page { height: 100%; aspect-ratio: var(--ratio); background: #fff; display: grid; place-items: center; cursor: zoom-in; overflow: hidden; }
.r-page.blank { background: transparent; cursor: default; }
.r-page img { width: 100%; height: 100%; object-fit: contain; }
.r-page span { color: var(--muted); }
.reader-bar { position: absolute; bottom: 14px; left: 0; right: 0; display: flex; justify-content: center; align-items: center; gap: 20px; color: #ddd; }
.reader .nav { background: rgba(255,255,255,.12); color: #fff; }

/* Notifications ------------------------------------------------------------ */
.toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 2000; display: grid; gap: 8px; justify-items: center; }
.toast { background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.2); transition: opacity .4s; max-width: min(520px, calc(100vw - 32px)); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--ok); }
.toast.out { opacity: 0; }
.fatal { padding: 40px; text-align: center; color: var(--danger); }

/* Mobile ------------------------------------------------------------------- */
@media (max-width: 820px) {
  .topbar { padding: 10px 16px; gap: 10px; }
  .board { padding: 16px; gap: 20px 16px; }
  .page-dialog { grid-template-columns: 1fr; grid-template-rows: 55vh auto; overflow: auto; width: 100vw; height: 100vh; max-height: 100vh; border-radius: 0; }
  .viewer { padding: 12px 44px; }
}

/* Impression : export du chemin de fer ------------------------------------- */
@media print {
  :root { --pw: 23mm; background: #fff; }
  body { background: #fff; font-size: 8pt; }
  .topbar, .selection-bar, .hint, .panel, dialog, .reader, .toasts, .menu, .menu-btn, .badge.presence { display: none !important; }
  body[data-print="A3"] { --pw: 33mm; }
  #app { display: block !important; }
  .print-only { display: block; margin-bottom: 6mm; }
  #printHeader h1 { font-size: 16pt; }
  #printHeader p { margin: 1mm 0; color: #555; }
  .board { padding: 0; gap: 7mm 5mm; }
  .spread { break-inside: avoid; }
  .page:hover .sheet { transform: none; }
  .page .sheet { box-shadow: 0 0 0 0.3pt #999; }
  .page.selected .sheet { outline: 0; }
  .meta { margin-top: 3.5mm; font-size: 7pt; }
  .groupbar span { font-size: 6pt; top: 1mm; }
  .badge { transform: scale(.8); }
  * { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
