:root {
  --paper: #f3f2ee;
  --surface: #ffffff;
  --ink: #1b1d21;
  --muted: #6b6e76;
  --line: #dcdbd4;
  --accent: #2f5bd3;      /* scanner-bar blue */
  --accent-ink: #ffffff;
  --danger: #b3261e;
  --dark-bg: #0f1114;
  --dark-surface: #1c1f24;
  --dark-ink: #eceef1;
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #16181c;
    --surface: #202328;
    --ink: #eceef1;
    --muted: #9a9ea8;
    --line: #30343b;
    --accent: #6f93f5;
    --accent-ink: #0d1330;
    --danger: #f2877f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
button, input, select, label.btn { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.view {
  height: 100%;
  display: flex;
  flex-direction: column;
}
.view.dark { background: var(--dark-bg); color: var(--dark-ink); }

/* ---- Bars ---- */
.bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-inline: max(16px, env(safe-area-inset-left));
}
.bar.top { padding-block: calc(env(safe-area-inset-top) + 10px) 10px; }
.bar.bottom { padding-block: 12px calc(env(safe-area-inset-bottom) + 12px); }
#home .bar.top { border-bottom: 1px solid var(--line); background: var(--paper); }
#home .bar.bottom { border-top: 1px solid var(--line); background: var(--surface); }
.bar-title { font-weight: 600; letter-spacing: .01em; }

.brand { display: flex; align-items: center; gap: 8px; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: -.01em; }
.logo { width: 24px; height: 24px; }

/* ---- Buttons ---- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 48px; padding: 0 18px;
  border-radius: 999px; border: 1px solid transparent;
  font-weight: 600; user-select: none; cursor: pointer;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.secondary { background: transparent; border-color: var(--line); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.scan { min-width: 136px; min-height: 56px; font-size: 17px; }
.btn.scan svg { width: 22px; height: 22px; }
.bar.bottom .btn.secondary { min-width: 84px; }

.ghost { background: none; border: 0; padding: 10px 4px; }
.ghost.strong { font-weight: 700; color: var(--accent); }
.view.dark .ghost.strong { color: #8fb0ff; }
.ghost.small { font-size: 14px; color: var(--muted); }
.ghost.small.armed { color: var(--danger); font-weight: 600; }

/* ---- Home ---- */
.pages-wrap {
  flex: 1;
  overflow-y: auto;
  padding: 20px max(16px, env(safe-area-inset-left)) 32px;
}
.empty { max-width: 360px; margin: 8vh auto 0; text-align: center; }
.empty-title { font-size: 20px; font-weight: 650; margin: 20px 0 6px; }
.muted { color: var(--muted); margin: 0; }
.empty-sheet {
  width: 96px; aspect-ratio: 1 / 1.414; margin: 0 auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 4px;
  padding: 18px 14px; display: flex; flex-direction: column; gap: 9px;
  position: relative; overflow: hidden;
}
.empty-sheet span { height: 4px; border-radius: 2px; background: var(--line); }
.empty-sheet span:nth-child(4) { width: 60%; }
.empty-sheet::after {
  content: ""; position: absolute; left: 0; right: 0; height: 2px;
  background: var(--accent); box-shadow: 0 0 12px var(--accent);
  animation: sweep 2.6s ease-in-out infinite alternate;
}
@keyframes sweep { from { top: 8%; } to { top: 90%; } }
@media (prefers-reduced-motion: reduce) { .empty-sheet::after { animation: none; top: 45%; } }

.pages {
  list-style: none; margin: 0; padding: 0;
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
}
.page {
  display: flex; flex-direction: column; gap: 6px;
}
.page-thumb {
  width: 100%; aspect-ratio: 1 / 1.3;
  background: var(--surface); border: 1px solid var(--line); border-radius: 6px;
  padding: 0; overflow: hidden; display: grid; place-items: center;
}
.page-thumb img { width: 100%; height: 100%; object-fit: contain; display: block; }
.page-meta { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.page-num { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.page-actions { display: flex; gap: 2px; }
.page-actions button {
  width: 34px; height: 34px; border-radius: 8px; border: 0; background: transparent;
  display: grid; place-items: center; color: var(--muted);
}
.page-actions button:disabled { opacity: .25; }
.page-actions button.del.armed { background: var(--danger); color: #fff; }
.page-actions svg { width: 18px; height: 18px; }

.export {
  margin-top: 28px; padding-top: 20px; border-top: 1px solid var(--line);
  display: grid; grid-template-columns: auto 1fr; gap: 10px 14px; align-items: center;
  max-width: 480px;
}
.export label { font-size: 13px; color: var(--muted); }
.export input, .export select {
  width: 100%; min-height: 42px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
}

/* ---- Crop & Enhance stage ---- */
.stage {
  flex: 1; position: relative; min-height: 0;
  display: grid; place-items: center;
  padding: 12px 16px;
  overflow: hidden;
}
.frame { position: relative; touch-action: none; }
.frame canvas { display: block; width: 100%; height: 100%; }
.overlay { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
#cropPoly {
  fill: rgba(111, 147, 245, .16);
  stroke: #8fb0ff; stroke-width: 2; vector-effect: non-scaling-stroke;
}
.handle {
  position: absolute; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  border-radius: 50%; touch-action: none; cursor: grab;
}
.handle::after {
  content: ""; position: absolute; inset: 11px;
  border-radius: 50%; background: #fff; border: 3px solid #2f5bd3;
  box-shadow: 0 1px 6px rgba(0,0,0,.5);
}
.handle.active::after { inset: 8px; }
.loupe {
  position: absolute; width: 110px; height: 110px; border-radius: 50%;
  border: 2px solid #fff; box-shadow: 0 4px 18px rgba(0,0,0,.6);
  pointer-events: none; background: #000;
}
.result {
  max-width: 100%; max-height: 100%;
  box-shadow: 0 6px 30px rgba(0,0,0,.5);
  background: #fff;
}
.busy {
  position: absolute; inset: auto 0 20px; margin: auto; width: max-content;
  padding: 8px 14px; border-radius: 999px; background: rgba(0,0,0,.7); font-size: 14px;
}

.tools { justify-content: center; gap: 10px; flex-wrap: wrap; }
.tool {
  min-height: 44px; padding: 0 16px; border-radius: 999px;
  background: var(--dark-surface); border: 1px solid #2c3037; color: var(--dark-ink);
}
.tool.icon { width: 44px; padding: 0; display: grid; place-items: center; }
.tool.icon svg { width: 22px; height: 22px; }
.tool:disabled { opacity: .5; }

/* Enhance footer: filter chips on their own row, rotate buttons below at the edges. */
#enhance .tools { justify-content: space-between; }
#enhance .chips { order: -1; flex-basis: 100%; }
/* Eight modes: one scrollable row rather than a wrapping block. */
.chips {
  display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none;
  padding-inline: 2px; scroll-snap-type: x proximity; -webkit-overflow-scrolling: touch;
}
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; scroll-snap-align: center; }
.chip {
  min-height: 40px; padding: 0 14px; border-radius: 999px;
  background: var(--dark-surface); border: 1px solid #2c3037; color: var(--dark-ink);
  font-size: 14px;
}
.chip[aria-checked="true"] { background: #eceef1; color: #111; border-color: #eceef1; font-weight: 600; }

/* ---- Sheet & toast ---- */
.sheet {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center; z-index: 10;
}
.sheet-card {
  width: min(480px, 100%); background: var(--surface); color: var(--ink);
  border-radius: 20px 20px 0 0;
  padding: 22px 16px calc(env(safe-area-inset-bottom) + 18px);
  display: flex; flex-direction: column; gap: 6px;
}
.sheet-title { font-size: 18px; font-weight: 650; margin: 0; }
.sheet-actions { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }

.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 96px);
  background: #1b1d21; color: #fff; padding: 10px 16px; border-radius: 999px;
  font-size: 14px; z-index: 20; max-width: calc(100% - 32px); text-align: center;
}

.export .check { display: flex; align-items: center; gap: 8px; font-size: 15px; color: var(--ink); }
.export .check input { width: 20px; height: 20px; min-height: 0; accent-color: var(--accent); }
.status { grid-column: 1 / -1; margin: 4px 0 0; font-size: 14px; color: var(--accent); font-variant-numeric: tabular-nums; }
.textbox {
  width: 100%; min-height: 160px; max-height: 40vh; padding: 10px 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--paper); color: var(--ink);
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; resize: vertical;
}

/* ---- Live camera ---- */
.camera { position: fixed; inset: 0; background: #000; color: #fff; z-index: 5; display: flex; flex-direction: column; }
.cam-stage { position: absolute; inset: 0; }
.cam-stage video, .cam-overlay { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; }
#camPoly { fill: rgba(143, 176, 255, .18); stroke: #fff; stroke-width: 6; stroke-linejoin: round; transition: fill .2s, stroke .2s; }
#camPoly.good { fill: rgba(84, 214, 140, .22); stroke: #54d68c; }
#camSpine { stroke: #ffd54a; stroke-width: 5; stroke-dasharray: 18 12; stroke-linecap: round; }
/* Hold-still ring: fills around the shutter while a steady page waits to be captured. */
.cam-shutter { position: relative; }
.cam-shutter::before {
  content: ''; position: absolute; inset: -4px; border-radius: 50%; pointer-events: none;
  background: conic-gradient(#54d68c calc(var(--hold, 0) * 1turn), transparent 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 5px), #000 calc(100% - 5px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 5px), #000 calc(100% - 5px));
}
.cam-flash { position: absolute; inset: 0; background: #fff; opacity: 0; pointer-events: none; }
.cam-flash.go { animation: camflash .35s ease-out; }
@keyframes camflash { from { opacity: .85; } to { opacity: 0; } }
.cam-focus {
  position: absolute; width: 72px; height: 72px; margin: -36px 0 0 -36px; pointer-events: none;
  border: 2px solid #ffd54a; border-radius: 12px;
}
.cam-focus.go { animation: camfocus .9s ease-out forwards; }
@keyframes camfocus { from { transform: scale(1.4); opacity: 1; } 60% { transform: scale(1); opacity: 1; } to { opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .cam-flash.go, .cam-focus.go { animation: none; } }

.cam-top, .cam-bottom { position: relative; display: flex; align-items: center; gap: 12px; padding-inline: 16px; }
.cam-top { padding-block: calc(env(safe-area-inset-top) + 12px) 8px; justify-content: space-between; }
.cam-bottom {
  margin-top: auto; justify-content: space-between;
  padding-block: 18px calc(env(safe-area-inset-bottom) + 22px);
  background: linear-gradient(transparent, rgba(0,0,0,.65));
}
.cam-round {
  width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none;
  background: rgba(0,0,0,.45); color: #fff; display: grid; place-items: center;
}
.cam-round svg { width: 22px; height: 22px; }
.cam-round[aria-pressed="true"] { background: #ffd54a; color: #111; }
.cam-guide {
  margin: 0; padding: 8px 14px; border-radius: 999px; font-size: 15px; font-weight: 600;
  background: rgba(0,0,0,.55); text-align: center; max-width: 70vw;
}
.cam-guide[data-tone="warn"] { background: rgba(255, 196, 0, .92); color: #1b1600; }
.cam-guide[data-tone="good"] { background: rgba(84, 214, 140, .95); color: #04210f; }
.cam-shutter {
  width: 76px; height: 76px; border-radius: 50%; border: 4px solid #fff; background: transparent; padding: 5px; flex: none;
}
.cam-shutter span { display: block; width: 100%; height: 100%; border-radius: 50%; background: #fff; transition: transform .1s; }
.cam-shutter:active span { transform: scale(.9); }
.cam-pages { position: relative; width: 96px; height: 60px; display: flex; align-items: center; }
.cam-pages img { width: 44px; height: 58px; object-fit: cover; border-radius: 4px; border: 2px solid #fff; background: #fff; }
.cam-count {
  position: absolute; left: 34px; top: -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;
}
.cam-right { width: 96px; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.cam-pill {
  min-height: 34px; padding: 0 14px; border-radius: 999px; border: 1px solid rgba(255,255,255,.5);
  background: rgba(0,0,0,.45); color: #fff; font-size: 14px; font-weight: 600;
}
.cam-pill[aria-pressed="false"] { opacity: .6; text-decoration: line-through; }
.cam-pill.strong { background: #fff; color: #111; border-color: #fff; }
.cam-pill.toggle[aria-pressed="false"] { text-decoration: none; }
.cam-pill.toggle[aria-pressed="true"] { background: #ffd54a; color: #111; border-color: #ffd54a; opacity: 1; }

/* ---- Quality score ---- */
.page-thumb-wrap { position: relative; }
.qbadge {
  position: absolute; top: 6px; right: 6px; min-width: 30px; height: 22px; padding: 0 7px;
  border-radius: 11px; border: 0; font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums;
}
.q-good { background: #17703d; color: #fff; }   /* 5.9:1 with white text */
.q-ok { background: #f2b705; color: #231a00; }
.q-poor { background: #c62828; color: #fff; }
.enh-quality {
  position: absolute; top: 8px; left: 16px; right: 16px; margin: 0 auto; width: fit-content; max-width: calc(100% - 32px);
  padding: 6px 12px; border-radius: 999px; font-size: 13px; font-weight: 600; z-index: 1; text-align: center;
}
.enh-quality:empty { display: none; }

/* ---- Library ---- */
.bar-spacer { min-width: 84px; }
.ghost.back { display: inline-flex; align-items: center; gap: 2px; font-weight: 600; color: var(--accent); padding-left: 0; }
.ghost.back svg { width: 22px; height: 22px; }
.ocr-status { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.search {
  display: flex; align-items: center; gap: 8px; margin-bottom: 16px;
  padding: 0 14px; min-height: 46px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line);
}
.search svg { width: 20px; height: 20px; color: var(--muted); flex: none; }
.search input { flex: 1; min-width: 0; border: 0; background: transparent; font-size: 16px; outline: none; }
.no-results { text-align: center; margin-top: 24px; }
.docs { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.doc {
  display: grid; grid-template-columns: 56px 1fr; gap: 14px; align-items: center; width: 100%;
  padding: 10px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); text-align: left;
}
.doc img { width: 56px; height: 72px; object-fit: cover; object-position: top; border-radius: 4px; border: 1px solid var(--line); background: #fff; }
.doc-name, .doc-meta, .doc-snippet { display: block; }
.doc-name { font-weight: 650; overflow-wrap: anywhere; }
.doc-meta { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.doc-snippet { font-size: 13px; margin-top: 4px; color: var(--ink); overflow-wrap: anywhere; }
.doc-snippet mark { background: #ffe58a; color: #1b1d21; border-radius: 2px; padding: 0 1px; }
.dupbadge {
  position: absolute; left: 6px; bottom: 6px; padding: 2px 8px; border-radius: 10px; border: 0;
  background: #7a4cc2; color: #fff; font-size: 11px; font-weight: 700;
}
.ocr-pending::after {
  content: "Reading text…"; position: absolute; left: 6px; bottom: 6px; font-size: 10px; font-weight: 600;
  padding: 2px 6px; border-radius: 8px; background: rgba(0,0,0,.6); color: #fff;
}

/* ---- Settings ---- */
.bar-right { display: flex; align-items: center; gap: 10px; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; border: 0; background: transparent; display: grid; place-items: center; color: var(--muted); }
.icon-btn svg { width: 22px; height: 22px; }
.privacy-note { font-size: 14px; margin: 4px 0 8px; }
.setting { display: flex; gap: 10px; align-items: flex-start; font-size: 15px; padding: 8px 0; }
.setting input { width: 20px; height: 20px; flex: none; accent-color: var(--accent); margin-top: 1px; }
.setting-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; font-size: 15px; padding: 6px 0; }
.setting-row select { min-height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--paper); padding: 0 10px; max-width: 60%; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger.armed { background: var(--danger); color: #fff; }

/* ---- Folders & details ---- */
.folders { display: flex; gap: 6px; overflow-x: auto; margin: -4px 0 14px; scrollbar-width: none; }
.folders::-webkit-scrollbar { display: none; }
.folder {
  flex: none; min-height: 34px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); font-size: 14px;
}
.folder[aria-selected="true"] { background: var(--ink); color: var(--paper); border-color: var(--ink); font-weight: 600; }
.folder .n { color: var(--muted); margin-left: 4px; font-variant-numeric: tabular-nums; }
.folder[aria-selected="true"] .n { color: inherit; opacity: .7; }
.details { margin-top: 26px; padding: 16px; border: 1px solid var(--line); border-radius: 14px; background: var(--surface); max-width: 480px; }
.details-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.details h2 { font-size: 16px; margin: 0; }
.details-head select { min-height: 34px; border-radius: 8px; border: 1px solid var(--line); background: var(--paper); padding: 0 8px; }
.details-note { font-size: 13px; margin: 6px 0 10px; }
.fields { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; margin: 0 0 12px; align-items: center; }
.fields dt { font-size: 13px; color: var(--muted); }
.fields dd { margin: 0; display: flex; align-items: center; gap: 6px; min-width: 0; }
.fields input {
  flex: 1; min-width: 0; min-height: 36px; padding: 0 10px; border-radius: 8px;
  border: 1px solid transparent; background: var(--paper); font-variant-numeric: tabular-nums;
}
.fields input:focus { border-color: var(--accent); }
.fields .unconfirmed input { border-style: dashed; border-color: #d9a400; }
.fields .flag { font-size: 11px; font-weight: 700; color: #8a6700; flex: none; }
.fields .ok .flag { color: #1f8a4c; }
.small-btn { min-height: 38px; font-size: 14px; }
/* Keep the Scan button centred whatever the side buttons' widths. */
#library .bar.bottom, #home .bar.bottom { display: grid; grid-template-columns: 1fr auto 1fr; }
#library .bar.bottom > :first-child, #home .bar.bottom > :first-child { justify-self: start; }
#library .bar.bottom > :last-child, #home .bar.bottom > :last-child { justify-self: end; }

.handle:focus-visible { outline: 3px solid #ffd54a; outline-offset: 2px; }
