:root {
  --bg: #f4f6f8; --surface: #ffffff; --text: #111827; --muted: #6b7280;
  --accent: #0f766e; --accent-2: #14b8a6; --danger: #dc2626; --line: #e5e7eb;
  --bar-h: 56px; --tool-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0b1114; --surface: #151d21; --text: #e5e7eb; --muted: #9ca3af; --line: #26323a; color-scheme: dark; }
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overscroll-behavior: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
.view { display: none; position: fixed; inset: 0; flex-direction: column; background: var(--bg); }
.view.active { display: flex; }
.view.dark { background: #0b0f12; color: #f3f4f6; }

.bar { height: calc(var(--bar-h) + env(safe-area-inset-top)); padding: env(safe-area-inset-top) 8px 0;
  display: flex; align-items: center; gap: 8px; background: var(--accent); color: #fff; flex: none; }
.view.dark .bar { background: #111827; }
.bar h1, .bar h2 { margin: 0 8px; font-size: 19px; font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .icon { width: 44px; height: 44px; font-size: 26px; line-height: 1; border-radius: 50%; color: #fff; }
.bar .icon:active { background: rgba(255,255,255,.15); }
.bar .icon.accent { color: #5eead4; font-size: 28px; }
.engine { color: #fff; font-size: 12px; padding: 3px 8px; border-radius: 99px; background: rgba(255,255,255,.18); white-space: nowrap; }
.title-input { flex: 1; min-width: 0; background: transparent; border: 0; color: #fff; font: 600 18px system-ui, sans-serif; padding: 6px; border-radius: 6px; }
.title-input:focus { outline: none; background: rgba(255,255,255,.15); }

.search { padding: 10px 12px 0; }
.search input { width: 100%; padding: 10px 14px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--text); font-size: 15px; }

.grid { flex: 1; overflow-y: auto; padding: 12px; display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); align-content: start; padding-bottom: 110px; }
.card { background: var(--surface); border-radius: 12px; overflow: hidden; box-shadow: 0 1px 3px rgba(0,0,0,.12); text-align: left; padding: 0; position: relative; }
.card .thumb { aspect-ratio: 3/4; background: #d1d5db center/cover no-repeat; display: block; width: 100%; object-fit: cover; }
.card .meta { padding: 8px 10px; }
.card .name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { color: var(--muted); font-size: 12px; }
.card .badge { position: absolute; top: 8px; left: 8px; background: rgba(0,0,0,.65); color: #fff; font-size: 12px; padding: 2px 8px; border-radius: 99px; }
.card .ocr-dot { position: absolute; top: 8px; right: 8px; background: var(--accent); color: #fff; font-size: 11px; padding: 2px 6px; border-radius: 99px; }
.pages { padding-bottom: calc(var(--tool-h) + 24px); }
.empty { text-align: center; color: var(--muted); margin: 15vh 24px; display: none; }
.empty.show { display: block; }

.fab-row { position: fixed; right: 18px; bottom: calc(22px + env(safe-area-inset-bottom)); display: flex; gap: 14px; align-items: center; }
.fab { width: 66px; height: 66px; border-radius: 50%; background: var(--accent); box-shadow: 0 6px 16px rgba(15,118,110,.45); display: grid; place-items: center; }
.fab.secondary { width: 50px; height: 50px; background: var(--surface); box-shadow: 0 3px 10px rgba(0,0,0,.2); }
.fab svg { width: 30px; height: 30px; fill: none; stroke: #fff; stroke-width: 1.8; stroke-linejoin: round; }
.fab.secondary svg { width: 24px; height: 24px; stroke: var(--accent); }

.toolbar { height: calc(var(--tool-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  flex: none; display: flex; background: var(--surface); border-top: 1px solid var(--line); }
.view.dark .toolbar { background: #111827; border-color: #1f2937; }
.toolbar button { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; font-size: 11px; gap: 2px; }
.toolbar button span { font-size: 21px; line-height: 1; }
.toolbar button:active { background: rgba(127,127,127,.15); }
#view-doc .toolbar { position: absolute; left: 0; right: 0; bottom: 0; }

.preview { flex: 1; display: grid; place-items: center; overflow: auto; padding: 12px; min-height: 0; }
.preview img { max-width: 100%; max-height: 100%; box-shadow: 0 4px 18px rgba(0,0,0,.25); background: #fff; }

.crop-stage { flex: 1; position: relative; display: grid; place-items: center; padding: 28px; min-height: 0; touch-action: none; }
.crop-box { position: relative; max-width: 100%; max-height: 100%; }
.crop-box img { display: block; max-width: 100%; max-height: calc(100vh - var(--bar-h) - var(--tool-h) - 70px); user-select: none; -webkit-user-drag: none; }
.crop-box svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
#crop-poly { fill: rgba(20,184,166,.18); stroke: #2dd4bf; stroke-width: 2.5; }
.handle { position: absolute; width: 34px; height: 34px; margin: -17px 0 0 -17px; border-radius: 50%;
  border: 3px solid #2dd4bf; background: rgba(15,118,110,.35); touch-action: none; }
.handle::after { content: ""; position: absolute; inset: -12px; } /* zone tactile élargie */
.loupe { position: absolute; top: 10px; left: 10px; width: 120px; height: 120px; border-radius: 50%; border: 3px solid #2dd4bf; display: none; pointer-events: none; background: #000; }

.filters { display: flex; gap: 10px; padding: 12px; overflow-x: auto; background: #111827; padding-bottom: calc(12px + env(safe-area-inset-bottom)); flex: none; }
.filters button { flex: none; width: 82px; text-align: center; font-size: 12px; color: #d1d5db; }
.filters img { width: 82px; height: 104px; object-fit: cover; border-radius: 8px; border: 3px solid transparent; background: #374151; display: block; margin-bottom: 4px; }
.filters button.sel img { border-color: #2dd4bf; }
.filters button.sel { color: #5eead4; font-weight: 600; }

.engine-select { background: rgba(255,255,255,.18); color: #fff; border: 0; border-radius: 99px; padding: 6px 10px; font: 600 13px system-ui, sans-serif; }
.engine-select option { color: #111; }
.text-main { flex: 1; padding: 12px; min-height: 0; display: flex; }
.text-main textarea { flex: 1; width: 100%; resize: none; border: 1px solid var(--line); border-radius: 12px; padding: 14px; background: var(--surface); color: var(--text); font: 15px/1.5 ui-monospace, "SF Mono", Menlo, monospace; }

dialog { border: 0; border-radius: 16px; padding: 0; width: min(92vw, 380px); background: var(--surface); color: var(--text); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog form { display: flex; flex-direction: column; padding: 18px; gap: 8px; }
dialog h3 { margin: 0 0 6px; }
dialog .row { display: flex; align-items: center; gap: 8px; justify-content: space-between; font-size: 14px; padding: 6px 0; }
dialog select { padding: 6px; border-radius: 8px; }
dialog button { padding: 12px; border-radius: 10px; background: var(--bg); text-align: left; }
dialog button.danger { color: var(--danger); }

.busy { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; flex-direction: column; align-items: center; justify-content: center; color: #fff; z-index: 50; }
.busy[hidden], .toast[hidden] { display: none; }
.spinner { width: 46px; height: 46px; border: 4px solid rgba(255,255,255,.25); border-top-color: #2dd4bf; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.toast { position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%); background: #111827; color: #fff;
  padding: 10px 16px; border-radius: 10px; font-size: 14px; z-index: 60; max-width: 90vw; box-shadow: 0 6px 18px rgba(0,0,0,.3); }
