:root {
  --bg: #f6f5f2;
  --card: #ffffff;
  --ink: #1b1d22;
  --muted: #6b6f78;
  --line: #e4e2dc;
  --accent: #1d3a8a;
  --accent-soft: #e8ecf7;
  --danger: #c0392b;
  --ok: #23854f;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--ink); }
body { min-height: 100dvh; overscroll-behavior-y: none; }
button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
input, textarea, select { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- Barre du haut ---------- */
.top {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px); -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid transparent;
}
.top.scrolled { border-bottom-color: var(--line); }
.top h1 { font-size: 28px; letter-spacing: -0.02em; margin: 0; flex: 1; }
.top .title { flex: 1; min-width: 0; font-weight: 600; font-size: 16px; text-align: center;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { min-width: 44px; height: 44px; display: inline-grid; place-items: center; border-radius: 22px;
  color: var(--accent); font-weight: 600; padding: 0 8px; }
.icon-btn:active { background: var(--accent-soft); }
.icon-btn svg { width: 24px; height: 24px; }

main.page { padding: 4px 16px calc(var(--safe-bottom) + 110px); max-width: 720px; margin: 0 auto; }

/* ---------- Onglets ---------- */
.tabs { display: flex; gap: 4px; background: #ebe9e3; padding: 4px; border-radius: 12px; margin: 6px 0 16px; }
.tabs button { flex: 1; height: 36px; border-radius: 9px; font-weight: 600; font-size: 15px; color: var(--muted); }
.tabs button.on { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.08); }

/* ---------- Cartes / listes ---------- */
.hint { background: var(--accent-soft); color: #22336b; border-radius: var(--radius); padding: 12px 14px;
  font-size: 14px; line-height: 1.4; margin-bottom: 16px; }
.hint b { user-select: all; -webkit-user-select: all; }
.list { display: flex; flex-direction: column; gap: 10px; }
.item { display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: var(--radius);
  padding: 14px; text-align: left; width: 100%; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.item:active { transform: scale(.99); }
.item .ico { width: 42px; height: 42px; border-radius: 10px; display: grid; place-items: center;
  background: #fdecea; color: var(--danger); font-weight: 800; font-size: 11px; flex: none; }
.item .ico.done { background: #e4f3ea; color: var(--ok); font-size: 20px; }
.item .txt { flex: 1; min-width: 0; }
.item .t1 { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .t2 { color: var(--muted); font-size: 13px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge { font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 99px; background: #e4f3ea; color: var(--ok); flex: none; }
.empty { text-align: center; color: var(--muted); padding: 40px 20px; line-height: 1.5; }
.empty .big { font-size: 40px; margin-bottom: 8px; }

.stamp-item { display: flex; align-items: center; gap: 12px; background: var(--card); border-radius: var(--radius);
  padding: 12px 14px; width: 100%; text-align: left; }
.stamp-item .prev { width: 120px; height: 64px; flex: none; display: grid; place-items: center;
  background: repeating-conic-gradient(#f3f2ee 0 25%, #fff 0 50%) 0 0/14px 14px; border-radius: 8px; overflow: hidden; }
.stamp-item .prev img { max-width: 100%; max-height: 100%; }
.stamp-item .name { flex: 1; font-weight: 600; }

/* ---------- Boutons ---------- */
.btn { height: 50px; border-radius: 14px; padding: 0 20px; font-weight: 600; font-size: 16px;
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:active { filter: brightness(.9); }
.btn.soft { background: var(--accent-soft); color: var(--accent); }
.btn.ghost { background: #ebe9e3; }
.btn.danger { background: #fdecea; color: var(--danger); }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .5; }
.fab { position: fixed; left: 16px; right: 16px; bottom: calc(var(--safe-bottom) + 16px); z-index: 30;
  max-width: 688px; margin: 0 auto; box-shadow: 0 8px 24px rgba(29,58,138,.3); }

/* ---------- Connexion ---------- */
.login { max-width: 380px; margin: 0 auto; padding: calc(var(--safe-top) + 60px) 24px 40px; text-align: center; }
.login img { width: 88px; height: 88px; border-radius: 22px; margin-bottom: 18px; }
.login h1 { margin: 0 0 6px; font-size: 28px; }
.login p { color: var(--muted); margin: 0 0 24px; line-height: 1.45; }
.field { width: 100%; height: 52px; border-radius: 14px; border: 1px solid var(--line); background: var(--card);
  padding: 0 16px; font-size: 18px; margin-bottom: 12px; text-align: center; letter-spacing: .1em; }
.field:focus { outline: 2px solid var(--accent); border-color: transparent; }
.err { color: var(--danger); min-height: 22px; font-size: 14px; margin-bottom: 8px; }

/* ---------- Éditeur PDF ---------- */
.editor { padding: 12px 0 calc(var(--safe-bottom) + 100px); }
.pdf-page { position: relative; margin: 0 auto 16px; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.12); width: calc(100% - 24px); max-width: 900px; }
.pdf-page canvas { display: block; width: 100%; height: auto; }
.pdf-page .pnum { position: absolute; bottom: -20px; left: 0; right: 0; text-align: center; font-size: 11px; color: var(--muted); }
.placed { position: absolute; touch-action: none; user-select: none; -webkit-user-select: none; }
.placed::before { content: ""; position: absolute; inset: -14px; } /* zone de toucher plus large */
.placed img { display: block; width: 100%; height: auto; pointer-events: none; }
.placed.sel { outline: 2px dashed var(--accent); outline-offset: 4px; }
.placed .h { position: absolute; width: 30px; height: 30px; border-radius: 50%; display: none; place-items: center;
  font-size: 15px; font-weight: 700; touch-action: none; }
.placed.sel .h { display: grid; }
.placed .h.del { top: -22px; left: -22px; background: var(--danger); color: #fff; }
.placed .h.rs { bottom: -22px; right: -22px; background: var(--accent); color: #fff; }
.placed .h.rs svg { width: 16px; height: 16px; }

.bottom-bar { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; gap: 10px;
  padding: 12px 16px calc(var(--safe-bottom) + 12px); background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); border-top: 1px solid var(--line); }
.bottom-bar .btn { flex: 1; }
.loading { text-align: center; padding: 60px 20px; color: var(--muted); }

/* ---------- Feuilles (bottom sheets) ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 50; animation: fade .2s; }
.sheet { position: fixed; left: 0; right: 0; bottom: 0; z-index: 51; background: var(--bg); border-radius: 20px 20px 0 0;
  max-height: 92dvh; display: flex; flex-direction: column; animation: up .25s cubic-bezier(.2,.8,.2,1);
  max-width: 720px; margin: 0 auto; }
.sheet header { display: flex; align-items: center; padding: 8px 8px 8px 20px; }
.sheet header h2 { flex: 1; font-size: 18px; margin: 0; }
.sheet .body { overflow-y: auto; padding: 4px 16px calc(var(--safe-bottom) + 20px); -webkit-overflow-scrolling: touch; }
.sheet .grab { width: 40px; height: 5px; border-radius: 3px; background: #cfccc4; margin: 8px auto 0; }
@keyframes up { from { transform: translateY(100%); } }
@keyframes fade { from { opacity: 0; } }

.label { font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin: 18px 4px 8px; }
.input, .textarea { width: 100%; border-radius: 12px; border: 1px solid var(--line); background: var(--card); padding: 12px 14px; font-size: 16px; }
.textarea { min-height: 84px; resize: vertical; line-height: 1.4; }
.small { font-size: 13px; color: var(--muted); margin: 6px 4px 0; line-height: 1.4; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { height: 38px; padding: 0 14px; border-radius: 99px; background: var(--card); border: 1px solid var(--line); font-weight: 600; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; }
.chip.on { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.dot { width: 14px; height: 14px; border-radius: 50%; display: inline-block; }
.preview-box { background: repeating-conic-gradient(#efede8 0 25%, #fff 0 50%) 0 0/16px 16px; border-radius: 14px;
  min-height: 120px; display: grid; place-items: center; padding: 14px; }
.preview-box canvas { max-width: 100%; max-height: 220px; }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; }
.img-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.img-grid button { aspect-ratio: 1; background: #fff; border-radius: 10px; overflow: hidden; display: grid; place-items: center; padding: 6px; border: 2px solid transparent; }
.img-grid button.on { border-color: var(--accent); }
.img-grid img { max-width: 100%; max-height: 100%; }
.range { width: 100%; accent-color: var(--accent); }

/* ---------- Pavé de signature ---------- */
.pad-wrap { position: fixed; inset: 0; z-index: 60; background: #fff; display: flex; flex-direction: column; }
.pad-wrap header { display: flex; align-items: center; padding: calc(var(--safe-top) + 6px) 8px 6px; border-bottom: 1px solid var(--line); }
.pad-wrap header .title { flex: 1; text-align: center; font-weight: 600; }
.pad-wrap canvas { flex: 1; width: 100%; touch-action: none; display: block;
  background: linear-gradient(transparent calc(70% - 1px), #d8d6cf calc(70% - 1px), #d8d6cf 70%, transparent 70%); }
.pad-wrap .hint2 { position: absolute; left: 0; right: 0; top: 50%; text-align: center; color: #c4c1b8; pointer-events: none; font-size: 15px; }

/* ---------- Divers ---------- */
#toast { position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 90px); transform: translate(-50%, 20px); z-index: 80;
  background: #1b1d22; color: #fff; padding: 12px 18px; border-radius: 12px; font-size: 15px; opacity: 0;
  transition: all .25s; pointer-events: none; max-width: calc(100% - 32px); text-align: center; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
.log-item { background: var(--card); border-radius: 12px; padding: 10px 12px; font-size: 13px; line-height: 1.4; }
.log-item .ok { color: var(--ok); font-weight: 700; }
.log-item .ko { color: var(--danger); font-weight: 700; }
.spinner { width: 22px; height: 22px; border-radius: 50%; border: 3px solid currentColor; border-right-color: transparent; animation: spin .8s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
