:root {
  --accent: #0f766e;
  --accent-2: #14b8a6;
  --accent-ink: #ffffff;
  --bg: #f4f6f8;
  --surface: #ffffff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --danger: #dc2626;
  --dark: #0b0f14;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --surface: #1a2027;
    --ink: #e8edf2;
    --muted: #94a3b8;
    --line: #2a323c;
    --accent-2: #2dd4bf;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}
input { -webkit-user-select: text; user-select: text; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
[hidden] { display: none !important; }

.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ic.sm { width: 18px; height: 18px; }
.ic.big { width: 56px; height: 56px; stroke-width: 1.5; }

/* ---------- vues ---------- */
.view { position: fixed; inset: 0; display: none; flex-direction: column; background: var(--bg); }
.view.active { display: flex; }
.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 16px 24px; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 8px) 8px 8px;
  background: var(--surface); border-bottom: 1px solid var(--line);
  min-height: 56px;
}
.topbar h1 { font-size: 20px; margin: 0; font-weight: 700; }
.topbar.dark { background: var(--dark); border-color: #1f2937; color: #fff; }
.brand { display: flex; align-items: center; gap: 10px; flex: 1; padding-left: 8px; }
.brand img { width: 30px; height: 30px; border-radius: 8px; }

.icon-btn { width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; border-radius: 50%; flex: none; }
.icon-btn:active { background: rgba(127,127,127,.18); }
.icon-btn.light { color: #fff; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 50px; padding: 0 18px; border-radius: 12px; font-weight: 600;
  background: var(--surface); border: 1px solid var(--line);
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.sm { height: 38px; padding: 0 12px; font-size: 15px; gap: 6px; }
.btn.grow { flex: 1; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .45; }
.btn:active:not(:disabled) { transform: scale(.98); }

.bottom-bar {
  display: flex; gap: 10px; padding: 10px 16px calc(var(--safe-bottom) + 10px);
  background: var(--surface); border-top: 1px solid var(--line);
}

/* segments */
.seg { display: inline-flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button { padding: 7px 12px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--muted); display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.seg button.on { background: var(--accent); color: #fff; }
.seg.dark { background: rgba(255,255,255,.1); border-color: transparent; }
.seg.dark button { color: #cbd5e1; }
.seg.dark button.on { background: #fff; color: #111; }
.topbar.dark { justify-content: space-between; }
@media (max-width: 420px) {
  #ed-tabs .ic { display: none; }
  #ed-tabs button { padding: 7px 10px; }
  #ed-save { padding: 0 10px; }
}

/* ---------- bibliothèque ---------- */
.doc-list { display: flex; flex-direction: column; gap: 10px; }
.doc-card {
  display: flex; align-items: center; gap: 12px; padding: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
}
.doc-card:active { background: var(--bg); }
.doc-thumb { width: 56px; height: 72px; object-fit: cover; border-radius: 6px; background: var(--line); flex: none; border: 1px solid var(--line); }
.doc-info { flex: 1; min-width: 0; text-align: left; }
.doc-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.doc-meta { color: var(--muted); font-size: 14px; margin-top: 2px; }
.empty { text-align: center; color: var(--muted); padding: 60px 24px; }
.empty h2 { color: var(--ink); margin: 12px 0 6px; font-size: 20px; }
.storage-info { text-align: center; color: var(--muted); font-size: 13px; margin-top: 18px; }

/* ---------- caméra ---------- */
.view.camera { background: #000; color: #fff; }
.cam-stage { position: absolute; inset: 0; }
#cam-video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
#cam-overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.cam-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; transition: opacity .25s; }
.cam-flash.on { opacity: .85; transition: none; }
.cam-top {
  position: absolute; left: 0; right: 0; top: 0; display: flex; align-items: center; justify-content: space-between;
  padding: calc(var(--safe-top) + 8px) 8px 8px; background: linear-gradient(rgba(0,0,0,.55), transparent);
}
.cam-hint { position: absolute; left: 0; right: 0; top: calc(var(--safe-top) + 64px); text-align: center; font-size: 14px; margin: 0; text-shadow: 0 1px 3px #000; opacity: .9; pointer-events: none; }
.cam-bottom {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; align-items: center; justify-content: space-around;
  padding: 18px 16px calc(var(--safe-bottom) + 22px); background: linear-gradient(transparent, rgba(0,0,0,.65));
}
.shutter { width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; background: rgba(255,255,255,.25); position: relative; }
.shutter::after { content: ""; position: absolute; inset: 6px; border-radius: 50%; background: #fff; transition: transform .1s; }
.shutter:active::after { transform: scale(.88); }
.shutter:disabled { opacity: .5; }
.cam-done { position: relative; width: 60px; height: 60px; display: flex; flex-direction: column; align-items: center; }
.cam-done img { width: 44px; height: 56px; object-fit: cover; border: 2px solid #fff; border-radius: 6px; background: #333; }
.cam-done-label { font-size: 12px; font-weight: 600; margin-top: 2px; }
.badge { position: absolute; top: -8px; right: -2px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px; background: var(--accent-2); color: #042f2e; font-size: 13px; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.cam-msg { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px; padding: 32px; text-align: center; }

/* ---------- éditeur ---------- */
.view.editor { background: var(--dark); color: #fff; }
.ed-stage { position: relative; flex: 1; overflow: hidden; touch-action: none; }
#ed-canvas { position: absolute; box-shadow: 0 4px 24px rgba(0,0,0,.6); }
.ed-quad { position: absolute; overflow: visible; }
.ed-quad .mask { fill: rgba(0,0,0,.5); fill-rule: evenodd; }
.ed-quad .edge { fill: rgba(20,184,166,.12); stroke: var(--accent-2); stroke-width: 2; }
.ed-quad .handle { fill: rgba(255,255,255,.95); stroke: var(--accent-2); stroke-width: 3; }
.ed-quad .handle.active { fill: var(--accent-2); }
.ed-loupe { position: absolute; top: 12px; left: 12px; width: 120px; height: 120px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 4px 16px rgba(0,0,0,.6); display: none; background: #000; }
.ed-loupe.on { display: block; }
.ed-loupe.right { left: auto; right: 12px; }
.ed-tools { display: flex; justify-content: space-around; padding: 10px 8px calc(var(--safe-bottom) + 12px); background: #111827; }
.ed-tools.col { flex-direction: column; gap: 10px; padding-left: 16px; padding-right: 16px; }
.tool { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: #d1d5db; padding: 6px 10px; border-radius: 10px; min-width: 64px; }
.tool:active { background: rgba(255,255,255,.1); }
.tool.inline { flex-direction: row; gap: 6px; font-size: 13px; min-width: 0; }
.row { display: flex; justify-content: space-between; }
.filters { display: flex; gap: 10px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.filters::-webkit-scrollbar { display: none; }
.filter { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 12px; color: #d1d5db; flex: none; }
.filter canvas { width: 56px; height: 72px; object-fit: cover; border-radius: 8px; border: 2px solid transparent; background: #333; }
.filter.on { color: #fff; font-weight: 700; }
.filter.on canvas { border-color: var(--accent-2); }
.slider { display: grid; grid-template-columns: 20px 86px 1fr 34px; align-items: center; gap: 8px; font-size: 13px; color: #d1d5db; }
.slider input { width: 100%; accent-color: var(--accent-2); }
.slider output { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- document ---------- */
.doc-title { flex: 1; min-width: 0; font-size: 18px; font-weight: 700; text-align: left; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 8px 4px; }
.doc-sub { color: var(--muted); font-size: 14px; margin: 0 0 12px; }
.page-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 14px; }
.page-card { position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 6px; aspect-ratio: 3 / 4; display: flex; align-items: center; justify-content: center; }
.page-card img { max-width: 100%; max-height: 100%; object-fit: contain; border-radius: 4px; box-shadow: 0 1px 4px rgba(0,0,0,.2); }
.page-num { position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%); background: rgba(17,24,39,.8); color: #fff; font-size: 12px; font-weight: 700; padding: 2px 9px; border-radius: 10px; }
.page-card:active { background: var(--bg); }
.page-empty { grid-column: 1 / -1; text-align: center; color: var(--muted); padding: 40px 0; }

/* ---------- feuilles ---------- */
.backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 20; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 21; max-height: 88vh; overflow-y: auto;
  background: var(--surface); color: var(--ink); border-radius: 18px 18px 0 0;
  padding: 8px 16px calc(var(--safe-bottom) + 16px); animation: up .2s ease-out;
  max-width: 560px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 4px auto 10px; }
.sheet h3 { margin: 0 0 12px; font-size: 18px; }
.menu-item { display: flex; align-items: center; gap: 14px; width: 100%; padding: 14px 6px; border-top: 1px solid var(--line); font-size: 16px; text-align: left; }
.menu-item:active { background: var(--bg); }
.menu-item.danger { color: var(--danger); }
.menu-item:disabled { opacity: .4; }
.field { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; }
.field .label { color: var(--muted); font-size: 15px; min-width: 60px; }
.field input { flex: 1; min-width: 0; height: 42px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; font: inherit; background: var(--bg); color: var(--ink); }
.ex-info { color: var(--muted); font-size: 14px; margin: 14px 0 10px; min-height: 20px; }
.hint { color: var(--muted); font-size: 13px; text-align: center; margin: 6px 0 12px; }

/* ---------- dialogues, attente, toast ---------- */
.dlg { border: 0; border-radius: 16px; padding: 20px; width: min(92vw, 400px); background: var(--surface); color: var(--ink); }
.dlg::backdrop { background: rgba(0,0,0,.5); }
.dlg h3 { margin: 0 0 8px; }
.dlg p { margin: 0 0 12px; color: var(--muted); white-space: pre-line; }
.dlg input { width: 100%; height: 44px; border: 1px solid var(--line); border-radius: 10px; padding: 0 12px; font: inherit; margin-bottom: 14px; background: var(--bg); color: var(--ink); }
.dlg-actions { display: flex; gap: 10px; justify-content: flex-end; }
.dlg-actions .btn { height: 44px; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }

.busy { position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.55); display: flex; flex-direction: column; align-items: center; justify-content: center; color: #fff; gap: 14px; }
.spinner { width: 44px; height: 44px; border-radius: 50%; border: 4px solid rgba(255,255,255,.25); border-top-color: #fff; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(var(--safe-bottom) + 90px); transform: translateX(-50%); z-index: 60; background: #111827; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; max-width: 90vw; box-shadow: 0 4px 16px rgba(0,0,0,.35); text-align: center; }
