:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #334155;
  --text: #f1f5f9;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-text: #082f49;
  --danger: #ef4444;
  --radius: 14px;
  --tap: 56px; /* hauteur mini des boutons : thumb-friendly */
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-tap-highlight-color: transparent;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

[hidden] { display: none !important; }

.app-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
  background: var(--panel);
}
.app-header h1 { margin: 0; font-size: 1.2rem; }

.badge {
  background: #b45309; color: #fff; font-size: .75rem;
  padding: 4px 10px; border-radius: 999px;
}

main {
  flex: 1; width: 100%; max-width: 720px; margin: 0 auto;
  padding: 16px; display: flex; flex-direction: column;
}

.step { display: flex; flex-direction: column; gap: 16px; flex: 1; }

.dropzone {
  flex: 1; min-height: 220px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center; gap: 4px; padding: 24px;
  border: 2px dashed var(--panel-2); border-radius: var(--radius);
  color: var(--muted);
}
.dropzone.drag { border-color: var(--accent); color: var(--text); background: rgba(56,189,248,.08); }
.dz-icon { font-size: 3rem; margin: 0; }
.hint { color: var(--muted); font-size: .9rem; margin: 0; }

.actions { display: flex; gap: 12px; }
.actions-stack { flex-direction: column; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px;
  border: 0; border-radius: var(--radius);
  font: inherit; font-weight: 600; cursor: pointer; text-align: center;
  color: var(--text); background: var(--panel-2);
  transition: transform .05s, opacity .2s;
}
.btn:active { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-text); }
.btn-secondary { background: var(--panel-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-big { flex: 1; font-size: 1.05rem; min-height: 60px; }
.btn:focus-visible, .dropzone:focus-visible, select:focus-visible, textarea:focus-visible {
  outline: 3px solid var(--accent); outline-offset: 2px;
}

.preview-wrap {
  background: #000; border-radius: var(--radius); overflow: hidden;
  display: flex; justify-content: center; align-items: center;
  max-height: 50dvh; padding: 16px;  /* marge : laisse les poignées de recadrage accessibles au bord */
}
.stage { position: relative; display: inline-block; line-height: 0; max-width: 100%; }
#preview { display: block; max-width: 100%; max-height: calc(50dvh - 32px); object-fit: contain; }

.controls { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 12px; }
.row .btn { flex: 1; }

.field { display: flex; flex-direction: column; gap: 4px; font-size: .9rem; color: var(--muted); }
select {
  min-height: 48px; padding: 0 12px; font: inherit; color: var(--text);
  background: var(--panel); border: 1px solid var(--panel-2); border-radius: 10px;
}

.progress-box { background: var(--panel); padding: 12px 14px; border-radius: var(--radius); }
.progress-label { font-size: .9rem; margin-bottom: 8px; }
.progress { height: 10px; background: var(--panel-2); border-radius: 999px; overflow: hidden; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .15s linear; }
.progress-bar.indeterminate { width: 40%; animation: slide 1.1s ease-in-out infinite; }
@keyframes slide { 0% { margin-left: -40%; } 100% { margin-left: 100%; } }

.sticky-actions {
  position: sticky; bottom: 0; padding: 12px 0 calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 75%, transparent);
  margin-top: auto;
}
.grid-actions { flex-wrap: wrap; }
.grid-actions .btn { flex: 1 1 calc(50% - 12px); }

#editor {
  flex: 1; min-height: 45dvh; width: 100%; resize: vertical;
  padding: 14px; font: 1rem/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color: var(--text); background: var(--panel);
  border: 1px solid var(--panel-2); border-radius: var(--radius);
}

.toast {
  position: fixed; left: 16px; right: 16px; z-index: 10;
  bottom: calc(96px + env(safe-area-inset-bottom));
  max-width: 560px; margin: 0 auto;
  padding: 14px 16px; border-radius: var(--radius);
  background: #111827; border: 1px solid var(--panel-2); box-shadow: 0 8px 24px rgba(0,0,0,.4);
}
.toast.error { border-color: var(--danger); }
.toast.ok { border-color: #22c55e; }

@media (min-width: 720px) {
  .btn-big { flex: 0 1 auto; min-width: 220px; }
  .actions-stack { flex-direction: row; justify-content: center; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* ===================== v2 : en-tête, accueil, historique ===================== */
.icon-btn {
  width: 44px; height: 44px; margin-right: 8px; border: 0; border-radius: 12px;
  background: var(--panel-2); color: var(--text); font-size: 1.6rem; line-height: 1; cursor: pointer;
}
.app-header h1 { flex: 1; }
.check { display: flex; gap: 10px; align-items: center; color: var(--muted); font-size: .9rem; min-height: 44px; }
.check input { width: 22px; height: 22px; accent-color: var(--accent); }
.link-btn { background: none; border: 0; color: var(--accent); font: inherit; cursor: pointer; padding: 10px 6px; min-height: 44px; }
.link-btn:hover { text-decoration: underline; }

.history { display: flex; flex-direction: column; gap: 10px; }
.history-head { display: flex; align-items: center; justify-content: space-between; }
.history-head h2 { margin: 0; font-size: 1.05rem; }
.search {
  min-height: 48px; padding: 0 14px; font: inherit; color: var(--text);
  background: var(--panel); border: 1px solid var(--panel-2); border-radius: 10px;
}
.history-list, .pages-grid { list-style: none; margin: 0; padding: 0; }
.history-list { display: flex; flex-direction: column; gap: 8px; }
.history-list li { display: flex; gap: 8px; align-items: stretch; }
.hist-item {
  flex: 1; min-width: 0; display: flex; gap: 12px; align-items: center; text-align: left;
  padding: 8px; border: 0; border-radius: var(--radius); background: var(--panel); color: var(--text);
  font: inherit; cursor: pointer; min-height: 72px;
}
.hist-item img, .hist-item .ph { width: 52px; height: 52px; object-fit: cover; border-radius: 8px; background: #fff; flex: none; }
.hist-txt { display: flex; flex-direction: column; min-width: 0; }
.hist-txt strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hist-txt small { color: var(--muted); }
.hist-del { width: 52px; border: 0; border-radius: var(--radius); background: var(--panel); color: var(--text); font-size: 1.2rem; cursor: pointer; }

/* ===================== Édition : recadrage en quadrilatère ===================== */
.crop-overlay { position: absolute; inset: 0; touch-action: none; user-select: none; -webkit-user-select: none; }
.crop-svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.qh {
  position: absolute; width: 48px; height: 48px; margin: -24px 0 0 -24px; touch-action: none; cursor: grab;
}
.qh::after {
  content: ''; position: absolute; left: 50%; top: 50%; width: 20px; height: 20px;
  margin: -10px 0 0 -10px; border-radius: 50%; background: var(--accent); border: 3px solid #fff;
}
.crop-actions .btn { flex: 1; padding: 0 8px; }
input[type=range] { width: 100%; min-height: 40px; accent-color: var(--accent); }

/* ===================== Document : grille des pages ===================== */
.pages-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
.page-card { background: var(--panel); border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column; }
.pg-thumb { position: relative; display: block; width: 100%; aspect-ratio: 3 / 4; border: 0; padding: 0; background: #fff; cursor: pointer; }
.pg-thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.pg-num { position: absolute; left: 6px; top: 6px; background: rgba(15,23,42,.85); color: #fff; border-radius: 999px; padding: 2px 10px; font-size: .8rem; font-weight: 700; }
.pg-badge { position: absolute; right: 6px; bottom: 6px; background: rgba(15,23,42,.85); color: #fff; border-radius: 999px; padding: 2px 8px; font-size: .72rem; }
.pg-badge.ok { background: #15803d; }
.pg-badge.todo { background: #b45309; }
.pg-actions { display: flex; }
.pg-actions button { flex: 1; min-height: 48px; border: 0; background: var(--panel-2); color: var(--text); font-size: 1.1rem; cursor: pointer; }
.pg-actions button:disabled { opacity: .3; cursor: default; }
.pg-actions button + button { border-left: 1px solid var(--panel); }
.add-row { display: flex; gap: 8px; }
.add-row .btn { flex: 1; padding: 0 6px; }
.doc-actions { align-items: center; flex-wrap: wrap; }
.doc-actions .link-btn { flex: 1 0 100%; text-align: center; }

/* ===================== Résultat ===================== */
.result-pages { display: flex; flex-direction: column; gap: 14px; }
.result-pages h3 { margin: 0 0 6px; font-size: .95rem; color: var(--muted); }
.result-pages textarea {
  width: 100%; min-height: 30dvh; resize: vertical; padding: 14px;
  font: 1rem/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color: var(--text); background: var(--panel); border: 1px solid var(--panel-2); border-radius: var(--radius);
}
.result-pages textarea:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.export-box { background: var(--panel); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.export-box h3 { margin: 0; font-size: 1rem; }
.export-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.export-grid .btn { padding: 0 8px; font-size: .95rem; }
.export-box select { background: var(--bg); }

/* ===================== Caméra live ===================== */
.camera { position: fixed; inset: 0; z-index: 20; background: #000; display: flex; flex-direction: column; }
.cam-stage { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.cam-stage video { width: 100%; height: 100%; object-fit: contain; }
.cam-svg { position: absolute; pointer-events: none; }
.cam-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; transition: opacity .25s; }
.cam-flash.on { opacity: .8; transition: none; }
.cam-top, .cam-bottom {
  position: absolute; left: 0; right: 0; display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; z-index: 2;
}
.cam-top { top: 0; padding-top: calc(12px + env(safe-area-inset-top)); background: linear-gradient(rgba(0,0,0,.55), transparent); }
.cam-bottom { bottom: 0; padding-bottom: calc(18px + env(safe-area-inset-bottom)); background: linear-gradient(transparent, rgba(0,0,0,.6)); }
.cam-btn { width: 48px; height: 48px; border: 0; border-radius: 50%; background: rgba(15,23,42,.7); color: #fff; font-size: 1.2rem; cursor: pointer; }
.cam-btn.on { background: var(--accent); color: var(--accent-text); }
.cam-count { color: #fff; font-weight: 600; background: rgba(15,23,42,.7); padding: 6px 14px; border-radius: 999px; }
.cam-last { width: 56px; height: 56px; border-radius: 10px; background: rgba(255,255,255,.12) center / cover no-repeat; border: 2px solid rgba(255,255,255,.4); }
.shutter {
  width: 76px; height: 76px; border-radius: 50%; border: 5px solid #fff; background: rgba(255,255,255,.35); cursor: pointer;
  box-shadow: inset 0 0 0 6px #fff;
}
.shutter:active { transform: scale(.94); }
.shutter:disabled { opacity: .5; }
#cam-done { min-width: 96px; }
