:root {
  --paper: #ffffff;
  --mist: #f1f3f6;
  --line: #e2e6eb;
  --ink: #1b1f24;
  --graphite: #5b6470;
  --accent: #2346d8;        /* carbon-copy blue */
  --accent-ink: #ffffff;
  --lock: #129a52;          /* detection locked */
  --danger: #c8322a;
  --radius-lg: 22px;
  --radius-md: 14px;
  --radius-sm: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --st: env(safe-area-inset-top, 0px);
  --sb: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.4 var(--font);
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
button:focus-visible, label:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- screens ---------- */
.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  padding-top: var(--st);
  background: var(--paper);
}

.bar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  gap: 8px; min-height: 56px; padding: 0 12px;
}
.bar > :first-child { justify-self: start; }
.bar-end { justify-self: end; display: flex; gap: 4px; }
.brand { margin: 0; font-size: 19px; font-weight: 700; letter-spacing: -0.02em; }
.title { margin: 0; font-size: 16px; font-weight: 600; }

.icon-btn {
  width: 42px; height: 42px; border-radius: 50%;
  display: inline-grid; place-items: center; color: var(--ink);
}
.icon-btn:active { background: var(--mist); }
.icon-btn.lg { width: 52px; height: 52px; background: var(--mist); }
.icon-btn[aria-pressed="true"] { background: #fff4c2; color: #8a6400; }

.text-btn {
  display: inline-flex; align-items: center; gap: 2px;
  height: 40px; padding: 0 8px 0 2px; border-radius: var(--radius-sm);
  font-weight: 500; color: var(--accent);
}
.text-btn svg { width: 22px; height: 22px; }
.text-btn.danger { color: var(--danger); padding: 0 8px; }

/* segmented control */
.seg { display: inline-flex; background: var(--mist); border-radius: 999px; padding: 3px; }
.seg button {
  height: 32px; padding: 0 14px; border-radius: 999px;
  font-size: 14px; font-weight: 500; color: var(--graphite);
  transition: background .15s, color .15s;
}
.seg button[aria-checked="true"] { background: var(--paper); color: var(--ink); box-shadow: 0 1px 2px rgba(20,30,50,.14); }
.seg.small button { height: 28px; padding: 0 12px; font-size: 13px; }

.btn {
  height: 48px; padding: 0 20px; border-radius: var(--radius-md);
  background: var(--mist); color: var(--ink); font-weight: 600;
}
.btn:active { filter: brightness(.95); }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.danger { background: #fdecea; color: var(--danger); }
.btn:disabled { opacity: .45; }

/* ---------- stage ---------- */
.stage {
  position: relative; flex: 1; min-height: 0;
  margin: 0 12px; border-radius: var(--radius-lg); overflow: hidden;
  background: #0d0f12;
}
.stage.light { background: var(--mist); }
#video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
#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 .35s ease-out; }
@keyframes flash { from { opacity: .9 } to { opacity: 0 } }

.hint {
  position: absolute; left: 50%; bottom: 16px; transform: translateX(-50%);
  margin: 0; padding: 8px 14px; border-radius: 999px;
  background: rgba(255,255,255,.92); color: var(--ink);
  font-size: 14px; font-weight: 500; white-space: nowrap;
  box-shadow: 0 2px 10px rgba(0,0,0,.18);
}
.hint.locked { background: var(--lock); color: #fff; }

.cam-empty {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center;
  gap: 12px; padding: 24px; text-align: center; background: var(--mist); color: var(--graphite);
}
.cam-empty svg { width: 44px; height: 44px; }
.cam-empty p { margin: 0; max-width: 28ch; }

/* ---------- dock ---------- */
.dock {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 14px 20px calc(14px + var(--sb));
}
.dock.split .btn { flex: 1; }
.dock.split .seg { flex: 0 0 auto; }
.dock.stack { flex-direction: column; align-items: stretch; gap: 12px; border-top: 1px solid var(--line); }
.dock.stack .row { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dock.stack .row .btn { flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 0 12px; }
.dock.stack .row .btn svg { width: 20px; height: 20px; }
.dock.stack .row .btn.grow { flex: 1.6; }
.label { color: var(--graphite); font-size: 14px; }

/* shutter — fills as auto-capture locks on */
.shutter { width: 80px; height: 80px; border-radius: 50%; position: relative; }
.shutter svg { width: 80px; height: 80px; stroke: none; transform: rotate(-90deg); }
.shutter .ring-bg { fill: none; stroke: var(--line); stroke-width: 5; }
.shutter .ring-fg {
  fill: none; stroke: var(--lock); stroke-width: 5; stroke-linecap: round;
  stroke-dasharray: 226.2; stroke-dashoffset: 226.2; transition: stroke-dashoffset .12s linear;
}
.shutter .core { fill: var(--accent); transition: r .12s; }
.shutter:active .core { r: 25; }
.shutter:disabled { opacity: .5; }

.pages-btn { position: relative; width: 52px; height: 52px; }
.pages-thumb {
  display: block; width: 100%; height: 100%; border-radius: 12px;
  background: var(--mist) center/cover no-repeat; border: 1px solid var(--line);
}
.pages-btn:disabled .pages-thumb { opacity: .6; }
.badge {
  position: absolute; top: -6px; right: -6px; min-width: 22px; height: 22px; padding: 0 6px;
  border-radius: 11px; background: var(--accent); color: #fff;
  font-size: 12px; font-weight: 700; display: grid; place-items: center;
}
.pages-btn:disabled .badge { display: none; }

/* ---------- crop ---------- */
#crop-canvas, #edit-canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#edit-canvas { object-fit: contain; padding: 14px; }
.handle {
  position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  border-radius: 50%; touch-action: none; z-index: 2;
}
.handle::after {
  content: ""; position: absolute; inset: 11px; border-radius: 50%;
  background: var(--paper); border: 3px solid var(--accent);
  box-shadow: 0 1px 6px rgba(0,0,0,.3);
}
.handle.active::after { background: var(--accent); }
.loupe {
  position: absolute; top: 12px; width: 120px; height: 120px; border-radius: 50%;
  border: 3px solid var(--paper); box-shadow: 0 4px 16px rgba(0,0,0,.3);
  background: #000; z-index: 3; pointer-events: none;
}
.loupe.left { left: 12px; } .loupe.right { right: 12px; }

/* ---------- enhance ---------- */
.busy { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(241,243,246,.55); }
.mode-row { display: flex; align-items: center; justify-content: center; gap: 12px; padding: 14px 12px 0; flex-wrap: wrap; }
.mode-row .seg button { height: 38px; padding: 0 14px; font-size: 14px; }
.dpi { margin: 8px 0 0; text-align: center; font-size: 12px; color: var(--graphite); }
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; font-weight: 600; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track {
  position: relative; width: 46px; height: 28px; border-radius: 14px;
  background: var(--line); transition: background .15s;
}
.switch .knob {
  position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.25); transition: transform .15s;
}
.switch input:checked + .track { background: var(--ink); }
.switch input:checked + .track .knob { transform: translateX(18px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- pages ---------- */
.page-list {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 14px; padding: 8px 16px 20px; align-content: start;
}
.page-card { position: relative; display: flex; flex-direction: column; gap: 6px; }
.page-card .thumb {
  aspect-ratio: 3 / 4; width: 100%; border-radius: var(--radius-sm);
  background: var(--mist) center/contain no-repeat; border: 1px solid var(--line);
}
.page-card .num { font-size: 13px; color: var(--graphite); text-align: center; }
.page-card .move { position: absolute; top: 6px; right: 6px; display: flex; gap: 4px; }
.page-card .move button {
  width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  background: rgba(255,255,255,.94); box-shadow: 0 1px 4px rgba(0,0,0,.2);
}
.page-card .move svg { width: 16px; height: 16px; }
.page-card.add .thumb {
  display: grid; place-items: center; background: var(--paper);
  border: 2px dashed #c3cad4; color: var(--accent);
}
.page-card.add .thumb svg { width: 34px; height: 34px; }
.page-card.add .num { color: var(--accent); font-weight: 600; }
.empty { flex: 1; display: grid; place-items: center; color: var(--graphite); padding: 24px; text-align: center; }

.viewer {
  position: fixed; inset: 0; z-index: 20; background: rgba(255,255,255,.98);
  display: flex; flex-direction: column; padding: calc(12px + var(--st)) 12px calc(12px + var(--sb));
}
.viewer img { flex: 1; min-height: 0; width: 100%; object-fit: contain; }
.viewer-bar { display: flex; gap: 12px; padding-top: 12px; }
.viewer-bar .btn { flex: 1; }

/* ---------- misc ---------- */
.boot {
  position: fixed; z-index: 30; left: 50%; top: calc(64px + var(--st)); transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  padding: 8px 14px 8px 10px; border-radius: 999px; background: var(--paper);
  box-shadow: 0 4px 18px rgba(20,30,50,.18); font-size: 13px; white-space: nowrap;
}
.boot p { margin: 0; }
.boot.error { color: var(--danger); }
.spinner {
  width: 22px; height: 22px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--accent);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; z-index: 40; left: 50%; bottom: calc(120px + var(--sb));
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  padding: 10px 16px; border-radius: 12px; background: var(--ink); color: #fff;
  font-size: 14px; max-width: calc(100% - 40px); text-align: center;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 700px) {
  .screen { max-width: 520px; margin: 0 auto; box-shadow: 0 0 0 1px var(--line); }
  body { background: var(--mist); }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition: none !important; }
}
