:root {
  --accent: #3d7bff;
  --accent-rgb: 61, 123, 255;
  --ok: #22c55e;
  --bg: #0b0d12;
  --panel: #151922;
  --panel-2: #1d2230;
  --line: rgba(255, 255, 255, 0.1);
  --text: #f4f6fb;
  --muted: #9aa3b5;
  --danger: #ff5a5f;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overflow: hidden; }
body { font: 15px/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; padding: 0; }
button:disabled { opacity: 0.35; cursor: default; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }

/* ---------- Kamera ---------- */
.stage { position: relative; flex: 1; overflow: hidden; background: #000; touch-action: manipulation; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
#overlay { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.flash.go { animation: flash 0.32s ease-out; }
@keyframes flash { 0% { opacity: 0.85; } 100% { opacity: 0; } }

.topbar {
  position: absolute; top: 0; left: 0; right: 0; z-index: 5;
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--safe-t)) 12px 26px;
  background: linear-gradient(rgba(0, 0, 0, 0.6), transparent);
}
.title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.title strong { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.title span { font-size: 12px; color: rgba(255, 255, 255, 0.75); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-actions { display: flex; gap: 6px; align-items: center; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(20, 22, 30, 0.55); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.icon-btn.is-on { background: #ffd24a; color: #111; }
.icon-btn.danger { color: var(--danger); }
.chip-btn {
  height: 34px; padding: 0 12px 0 8px; border-radius: 17px; display: flex; align-items: center; gap: 4px;
  background: rgba(20, 22, 30, 0.55); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  font-size: 13px; font-weight: 600;
}
.chip-btn svg { width: 18px; height: 18px; }
.chip-btn.is-on { background: var(--accent); }

.hint {
  position: absolute; left: 50%; bottom: 18px; transform: translateX(-50%); z-index: 4;
  display: flex; align-items: center; gap: 8px; max-width: calc(100% - 32px);
  padding: 9px 16px; border-radius: 999px; font-size: 14px; font-weight: 500; white-space: nowrap;
  background: rgba(15, 17, 24, 0.72); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  transition: background 0.25s;
}
.hint span:last-child { overflow: hidden; text-overflow: ellipsis; }
.hint .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.hint.found .dot { background: #ffd24a; }
.hint.hold { background: rgba(var(--accent-rgb), 0.9); }
.hint.hold .dot { background: #fff; animation: pulse 0.6s infinite alternate; }
.hint.done { background: rgba(34, 197, 94, 0.92); }
.hint.done .dot { background: #fff; }
@keyframes pulse { to { transform: scale(1.6); opacity: 0.6; } }

.replace-banner {
  position: absolute; top: calc(70px + var(--safe-t)); left: 50%; transform: translateX(-50%); z-index: 5;
  display: flex; align-items: center; gap: 12px; padding: 8px 8px 8px 16px; border-radius: 999px;
  background: #ffd24a; color: #111; font-weight: 600; font-size: 14px; white-space: nowrap;
}
.replace-banner button { background: rgba(0, 0, 0, 0.12); padding: 6px 12px; border-radius: 999px; font-size: 13px; }

.fallback { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; z-index: 6; background: radial-gradient(circle at 50% 30%, #1b2233, #07080c); }
.fallback-card { max-width: 360px; text-align: center; }
.fallback-card .big { width: 56px; height: 56px; color: var(--muted); }
.fallback-card h2 { margin: 12px 0 6px; font-size: 20px; }
.fallback-card p { color: var(--muted); margin: 0 0 20px; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* ---------- Dock ---------- */
.dock { background: var(--bg); padding-bottom: calc(10px + var(--safe-b)); border-top: 1px solid var(--line); }
.tray { display: flex; gap: 10px; padding: 12px 14px 8px; overflow-x: auto; min-height: 88px; scroll-behavior: smooth; scrollbar-width: none; }
.tray::-webkit-scrollbar { display: none; }
.tray:empty::before { content: "Gescannte Seiten erscheinen hier"; color: var(--muted); font-size: 13px; margin: auto; }
.thumb {
  position: relative; flex: none; width: 52px; height: 70px; border-radius: 6px; overflow: hidden;
  background: #fff; box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5); outline: 2px solid transparent; outline-offset: 2px;
  animation: pop 0.35s cubic-bezier(.2, 1.4, .4, 1);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .num { position: absolute; right: 2px; bottom: 2px; min-width: 18px; height: 18px; padding: 0 4px; border-radius: 9px; background: rgba(0, 0, 0, 0.72); font-size: 11px; font-weight: 700; display: grid; place-items: center; color: #fff; }
.thumb.pending { background: var(--panel-2); }
.thumb.pending::after { content: ""; position: absolute; inset: 0; margin: auto; width: 20px; height: 20px; border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.25); border-top-color: #fff; animation: spin 0.8s linear infinite; }
.thumb.replacing { outline-color: #ffd24a; }
.thumb.flash-in { outline-color: var(--ok); }
@keyframes pop { from { transform: scale(0.6); opacity: 0; } }
@keyframes spin { to { transform: rotate(360deg); } }

.controls { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; padding: 4px 18px 0; }
.ctl { display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; color: var(--muted); justify-self: start; min-width: 76px; }
.ctl svg { color: var(--text); }
.ctl.done { justify-self: end; color: var(--text); font-weight: 600; }
.ctl .badge { min-width: 30px; height: 30px; padding: 0 8px; border-radius: 15px; background: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: 15px; color: #fff; }
.shutter { position: relative; width: 76px; height: 76px; display: grid; place-items: center; }
.shutter .ring { position: absolute; inset: 0; width: 76px; height: 76px; transform: rotate(-90deg); }
.shutter .track { stroke: rgba(255, 255, 255, 0.85); stroke-width: 4; }
.shutter .progress { stroke: var(--accent); stroke-width: 5; stroke-dasharray: 226.2; stroke-dashoffset: 226.2; transition: stroke-dashoffset 0.1s linear; }
.shutter .core { width: 58px; height: 58px; border-radius: 50%; background: #fff; transition: transform 0.15s; }
.shutter:active .core { transform: scale(0.88); }
.auto-on .shutter .core { background: radial-gradient(circle, #fff 55%, rgba(var(--accent-rgb), 0.35) 56%); }

/* fliegendes Blatt */
.ghost { position: fixed; z-index: 30; pointer-events: none; will-change: transform; box-shadow: 0 10px 40px rgba(0, 0, 0, 0.55); }

/* ---------- Sheets ---------- */
.sheet { position: fixed; inset: 0; z-index: 20; background: #08090d; display: flex; flex-direction: column; animation: slideUp 0.28s cubic-bezier(.2, .8, .2, 1); }
@keyframes slideUp { from { transform: translateY(30px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: calc(10px + var(--safe-t)) 12px 10px; }
.sheet-body { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; padding: 8px 16px; }
.sheet-body { overflow: hidden; touch-action: pan-y; user-select: none; -webkit-user-select: none; }
.sheet-body img { max-width: 100%; max-height: 100%; border-radius: 4px; box-shadow: 0 8px 40px rgba(0, 0, 0, 0.6); background: #fff; will-change: transform; -webkit-user-drag: none; }
.page-dots { display: flex; gap: 6px; justify-content: center; padding: 4px 0 0; min-height: 10px; }
.page-dots i { width: 6px; height: 6px; border-radius: 50%; background: rgba(255, 255, 255, 0.28); transition: background 0.2s, transform 0.2s; }
.page-dots i.on { background: #fff; transform: scale(1.3); }
.dialog-wrap { position: fixed; inset: 0; z-index: 40; background: rgba(5, 6, 10, 0.6); display: flex; align-items: flex-end; justify-content: center; padding: 16px 16px calc(16px + var(--safe-b)); animation: fadeIn 0.2s; }
@keyframes fadeIn { from { opacity: 0; } }
.dialog { width: 100%; max-width: 420px; background: #fff; color: #141824; border-radius: 22px; padding: 22px 18px 16px; display: grid; gap: 10px; animation: slideUp 0.28s cubic-bezier(.2, .8, .2, 1); color-scheme: light; }
.dialog h2 { margin: 0; font-size: 19px; text-align: center; }
.dialog p { margin: 0 0 6px; color: #5b6478; text-align: center; }
.dialog .btn { background: #e6eaf2; color: #141824; }
.dialog .btn.primary { background: var(--accent); color: #fff; }
.dialog .btn.danger { background: transparent; color: #d92d32; }
.draft { position: fixed; left: 12px; right: 12px; top: calc(70px + var(--safe-t)); z-index: 12; display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 14px; border-radius: 16px; background: #fff; color: #141824; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4); animation: slideUp 0.3s; max-width: 520px; margin: 0 auto; }
.draft > div { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.draft strong { font-size: 14px; }
.draft span { font-size: 12px; color: #5b6478; }
.draft .btn { padding: 8px 12px; font-size: 13px; background: #e6eaf2; color: #141824; border-radius: 10px; }
.draft .btn.primary { background: var(--accent); color: #fff; }
.spinner { position: absolute; width: 36px; height: 36px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.2); border-top-color: #fff; animation: spin 0.8s linear infinite; }
.filters { display: flex; gap: 8px; justify-content: center; padding: 10px 12px 4px; flex-wrap: wrap; }
.filter-chip { padding: 7px 14px; border-radius: 999px; background: var(--panel-2); font-size: 13px; font-weight: 600; color: var(--muted); }
.filter-chip.is-on { background: var(--accent); color: #fff; }
.sheet-actions { display: grid; grid-template-columns: repeat(5, 1fr); padding: 8px 6px calc(10px + var(--safe-b)); border-top: 1px solid var(--line); }
.sheet-actions button { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 11px; color: var(--muted); padding: 6px 2px; }
.sheet-actions svg { color: var(--text); }
.text-btn { padding: 8px 10px; font-weight: 600; color: var(--muted); }
.text-btn.accent { color: var(--accent); }

.crop-stage { position: relative; flex: 1; min-height: 0; margin: 8px 16px; touch-action: none; }
#cropCanvas { position: absolute; }
#cropSvg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
#cropPoly { fill: rgba(var(--accent-rgb), 0.18); stroke: var(--accent); stroke-width: 2; }
.handle { position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px; border-radius: 50%; touch-action: none; }
.handle::after { content: ""; position: absolute; inset: 11px; border-radius: 50%; background: #fff; border: 3px solid var(--accent); box-shadow: 0 2px 8px rgba(0, 0, 0, 0.5); }
.loupe { position: absolute; width: 120px; height: 120px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 4px 20px rgba(0, 0, 0, 0.6); pointer-events: none; display: none; background: #000; }
.crop-foot { display: flex; gap: 10px; justify-content: center; padding: 10px 16px calc(14px + var(--safe-b)); }

/* ---------- Prüfen ---------- */
.review, .result { position: fixed; inset: 0; z-index: 15; background: #f3f5f9; color: #141824; display: flex; flex-direction: column; color-scheme: light; animation: slideUp 0.3s cubic-bezier(.2, .8, .2, 1); }
.review-head { display: flex; align-items: center; gap: 10px; padding: calc(10px + var(--safe-t)) 12px 10px; background: #fff; border-bottom: 1px solid #e3e7ef; }
.review-head > div { flex: 1; display: flex; flex-direction: column; }
.review-head span { font-size: 12px; color: #6b7487; }
.review .icon-btn { background: #eef1f6; }
.review-body { flex: 1; overflow-y: auto; padding: 16px; }
.field { display: block; margin-bottom: 18px; }
.field > span { display: block; font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; color: #6b7487; margin-bottom: 6px; }
.field small { display: block; color: #6b7487; margin-top: 6px; }
.input-wrap { display: flex; align-items: center; background: #fff; border: 1px solid #d9deea; border-radius: 12px; padding: 0 12px; }
.input-wrap input { flex: 1; min-width: 0; border: 0; outline: 0; padding: 12px 0; font: inherit; font-size: 16px; background: transparent; color: inherit; }
.input-wrap span { color: #8a93a6; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 12px; }
.grid-item { position: relative; aspect-ratio: 0.72; border-radius: 8px; overflow: hidden; background: #fff; box-shadow: 0 2px 10px rgba(20, 30, 60, 0.15); touch-action: pan-y; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.grid-item img { width: 100%; height: 100%; object-fit: contain; display: block; pointer-events: none; background: #fff; }
.grid-item .num { position: absolute; left: 6px; bottom: 6px; background: rgba(20, 24, 36, 0.8); color: #fff; font-size: 12px; font-weight: 700; border-radius: 10px; padding: 1px 8px; }
.grid-item.dragging { z-index: 5; box-shadow: 0 16px 40px rgba(20, 30, 60, 0.35); transform-origin: center; transition: none; }
.grid-item.drop-target { outline: 3px dashed var(--accent); outline-offset: 2px; }
.tip { font-size: 12px; color: #8a93a6; text-align: center; margin: 10px 0 20px; }
.segmented { display: flex; background: #e6eaf2; border-radius: 12px; padding: 3px; }
.segmented button { flex: 1; padding: 8px 4px; border-radius: 9px; font-size: 13px; line-height: 1.2; font-weight: 600; color: #5b6478; }
.segmented button.is-on { background: #fff; color: #141824; box-shadow: 0 1px 4px rgba(20, 30, 60, 0.15); }
.review-foot { padding: 12px 16px calc(12px + var(--safe-b)); background: #fff; border-top: 1px solid #e3e7ef; }

.btn { padding: 12px 18px; border-radius: 12px; background: var(--panel-2); font-weight: 600; }
.review .btn, .result .btn { background: #e6eaf2; color: #141824; }
.btn.primary, .review .btn.primary, .result .btn.primary { background: var(--accent); color: #fff; }
.btn.wide { width: 100%; padding: 15px; font-size: 16px; }

.result { display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at 50% 0%, #e7eeff, #f3f5f9 60%); }
.result-card { width: 100%; max-width: 380px; text-align: center; }
.check { width: 84px; height: 84px; margin: 0 auto 16px; border-radius: 50%; background: var(--ok); color: #fff; display: grid; place-items: center; animation: pop 0.45s cubic-bezier(.2, 1.4, .4, 1); box-shadow: 0 10px 30px rgba(34, 197, 94, 0.4); }
.check svg { width: 44px; height: 44px; stroke-width: 3; }
.check.busy { background: var(--accent); box-shadow: 0 10px 30px rgba(var(--accent-rgb), 0.35); }
.check.busy svg { display: none; }
.check.busy::after { content: ""; width: 34px; height: 34px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, 0.35); border-top-color: #fff; animation: spin 0.8s linear infinite; }
.check.err { background: var(--danger); box-shadow: none; }
.result h2 { margin: 0 0 4px; font-size: 22px; }
.result p { margin: 0 0 22px; color: #5b6478; }
.result-actions { display: grid; gap: 10px; }
.progress-bar { height: 6px; border-radius: 3px; background: #dfe4ee; overflow: hidden; margin: -8px 0 22px; }
.progress-bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }

.toast { position: fixed; left: 50%; top: calc(16px + var(--safe-t)); transform: translate(-50%, -150%); z-index: 50; padding: 10px 16px; border-radius: 12px; background: #1d2230; color: #fff; font-size: 14px; font-weight: 500; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4); transition: transform 0.3s cubic-bezier(.2, .8, .2, 1); max-width: calc(100% - 32px); }
.toast.show { transform: translate(-50%, 0); }

@media (min-width: 900px) {
  .review-body { max-width: 760px; margin: 0 auto; width: 100%; }
  .review-foot { display: flex; justify-content: center; }
  .review-foot .btn { max-width: 728px; }
}

/* Warnung: unscharfe Seite */
.warn-banner {
  position: absolute; top: calc(70px + var(--safe-t)); left: 12px; right: 12px; z-index: 6; margin: 0 auto; max-width: 460px;
  display: flex; align-items: center; gap: 10px; padding: 8px 8px 8px 12px; border-radius: 16px;
  background: #ff9f1a; color: #1a1205; font-weight: 600; font-size: 14px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  animation: slideUp 0.25s;
}
.warn-banner svg { flex: none; width: 22px; height: 22px; }
.warn-banner span { flex: 1; min-width: 0; }
.warn-banner button { background: #1a1205; color: #fff; padding: 7px 12px; border-radius: 10px; font-size: 13px; font-weight: 700; }
.warn-banner button.x { background: transparent; color: #1a1205; padding: 6px; font-size: 15px; }
.thumb .warn, .grid-item .warn { position: absolute; left: 2px; top: 2px; width: 18px; height: 18px; border-radius: 50%; background: #ff9f1a; color: #1a1205; font-size: 12px; font-weight: 900; display: grid; place-items: center; }
.grid-item .warn { left: 6px; top: 6px; width: 22px; height: 22px; font-size: 14px; }
.thumb .orienting { position: absolute; left: 2px; top: 2px; width: 16px; height: 16px; border-radius: 50%; border: 2px solid rgba(61, 123, 255, 0.35); border-top-color: var(--accent); animation: spin 0.8s linear infinite; background: rgba(255, 255, 255, 0.85); }
.sheet-warn { margin: 6px 16px 0; padding: 8px 12px; border-radius: 10px; background: rgba(255, 159, 26, 0.16); color: #ffb54d; font-size: 13px; font-weight: 600; text-align: center; }

/* Licht-Knopf: A = automatisch */
.light-btn { position: relative; }
.light-mode { position: absolute; right: -2px; bottom: -2px; min-width: 16px; height: 16px; padding: 0 3px; border-radius: 8px; background: var(--accent); color: #fff; font-size: 10px; font-weight: 800; display: grid; place-items: center; }
.light-mode:empty { display: none; }
.icon-btn.is-on .light-mode { background: #111; }

.icon-btn.multi-on { background: var(--accent); color: #fff; }
