:root {
  --accent: #113253;
  --accent-ink: #fff;
  --bg: #f3f4f6;
  --card: #fff;
  --ink: #111827;
  --muted: #6b7280;
  --line: #e5e7eb;
  --danger: #b91c1c;
  --warn-bg: #fef3c7;
  --ok-bg: #dcfce7;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --accent: #3b82f6;
    --bg: #0b1220;
    --card: #151d2e;
    --ink: #e5e7eb;
    --muted: #9ca3af;
    --line: #273245;
    --danger: #f87171;
    --warn-bg: #4a3a0a;
    --ok-bg: #0f3d23;
  }
}

* { box-sizing: border-box; }
/* Any display rule (.row is flex) outranks the browser's own [hidden], so the
   box dialog's Up/Down row showed for a box with nothing to reorder against. */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  /* A view sliding in starts off-screen; it must not widen the page. */
  overflow-x: clip;
  margin: 0;
  font: 16px/1.4 -apple-system, system-ui, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: env(safe-area-inset-bottom);
}
h1 { font-size: 18px; margin: 0; }
h2 { font-size: 15px; margin: 0 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
h3 { font-size: 16px; margin: 0; }
label { display: block; font-weight: 600; font-size: 14px; margin-bottom: 4px; }
p { margin: 0 0 8px; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.tabs { display: flex; gap: 4px; }
.tab {
  border: 1px solid var(--line); background: transparent; color: var(--ink);
  padding: 8px 14px; border-radius: 999px; font: inherit; font-weight: 600;
}
.tab.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

/* pan-y: sideways drags belong to the swipe between views, not the browser. */
.view { padding: 12px 16px 32px; max-width: 640px; margin: 0 auto; touch-action: pan-y; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 14px; margin-bottom: 12px; }
.hint { color: var(--muted); font-size: 14px; margin-top: 6px; }

input, textarea, select {
  width: 100%; font: inherit; font-size: 16px; /* 16px stops iOS zooming the page on focus */
  padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink);
}
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; }

.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row > input, .row > select { flex: 1; min-width: 0; }

/* Box codes are what gets written on labels, so they are big and monospaced. */
.box-id {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-weight: 700; font-size: 34px; letter-spacing: .12em; line-height: 1.1;
}
.box-id.big { font-size: 48px; text-align: center; margin: 8px 0 14px; }
.box-desc { font-size: 16px; }
.box-current { margin-top: 12px; }
.box-current .hint { margin-top: 2px; }
.btn.link.small { font-size: 14px; padding: 0; }
.row.order { margin-bottom: 10px; }
.row.order .hint { flex: 1; margin: 0; }

.btn {
  font: inherit; font-weight: 600; font-size: 16px;
  padding: 10px 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); cursor: pointer; -webkit-tap-highlight-color: transparent;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.big { width: 100%; padding: 14px; font-size: 18px; margin-top: 10px; }
.btn.link { background: transparent; border-color: transparent; color: var(--accent); padding-left: 4px; padding-right: 4px; }
.btn.danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn:disabled { opacity: .5; cursor: default; }
#btn-manual-book { margin-top: 6px; }
#btn-snapshot { margin-top: 10px; }

.scan-frame {
  position: relative; aspect-ratio: 4 / 3; background: #000; border-radius: 12px; overflow: hidden;
  outline: 3px solid transparent; transition: outline-color .15s;
}
.scan-frame.flash { outline-color: #22c55e; }
.scan-frame video {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: 100%; height: 100%; object-fit: cover; display: block;
}
.scan-status {
  position: absolute; left: 0; right: 0; bottom: 0; padding: 6px 10px;
  background: rgba(0,0,0,.55); color: #fff; font-size: 14px; text-align: center;
}
.scan-status.busy { background: rgba(22,163,74,.85); font-weight: 600; }

.books { list-style: none; margin: 0; padding: 0; }
.book {
  display: block; padding: 10px 4px; border-top: 1px solid var(--line); cursor: pointer;
  border-radius: 6px;
}
.book:first-child { border-top: 0; }
.book:active { background: var(--bg); }
.book .t { font-weight: 600; }
.book .a { font-size: 14px; }
.book .m { font-size: 13px; color: var(--muted); }
.book.pending .t { color: var(--muted); font-style: italic; }
.book.needs-edit { background: var(--warn-bg); padding-left: 10px; padding-right: 10px; }
.book.needs-edit .t { color: var(--ink); }
.book.just-added { background: var(--ok-bg); padding-left: 10px; padding-right: 10px; }
.book.ignored .t { color: var(--muted); }

.group { margin-bottom: 12px; }
.group h3 { display: flex; align-items: baseline; gap: 10px; padding: 4px 4px 6px; border-radius: 6px; }
.group h3[data-box] { cursor: pointer; }
.group h3[data-box]:active { background: var(--bg); }
.group.folded h3 { padding-bottom: 4px; }
.sealed-divider { display: flex; align-items: center; gap: 10px; margin: 22px 4px 12px; }
.sealed-divider::before, .sealed-divider::after { content: ''; flex: 1; border-top: 1px solid var(--line); }
.group h3 .fold {
  align-self: center; border: 0; background: transparent; color: var(--muted);
  font-size: 22px; line-height: 1; padding: 6px 10px; margin: -8px 0 -8px -8px; cursor: pointer;
}
.summary-row { justify-content: space-between; margin-top: 6px; }
.summary-row .hint { margin: 0; }
.group h3 .box-id { font-size: 18px; letter-spacing: .08em; }
.group h3 .box-desc { flex: 1; font-weight: 400; color: var(--muted); font-size: 14px; }
.group h3 .tag {
  font-size: 12px; font-weight: 600; color: var(--muted); border: 1px solid var(--line);
  border-radius: 999px; padding: 1px 8px; white-space: nowrap;
}
.group h3 .count { color: var(--muted); font-weight: 400; font-size: 14px; white-space: nowrap; }

.build { text-align: center; font-size: 12px; }

/* A wide gap between the summary and the button: with 8px, tapping to collapse
   the section could land on "Delete every book" instead. */
details.danger summary { color: var(--muted); font-size: 14px; cursor: pointer; padding: 4px 0; }
details.danger .btn { display: block; margin-top: 24px; }

dialog {
  border: 0; border-radius: 14px; padding: 18px; width: min(92vw, 480px);
  background: var(--card); color: var(--ink);
}
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h2 { color: var(--ink); text-transform: none; letter-spacing: 0; font-size: 18px; margin-bottom: 12px; }
dialog label { margin-bottom: 10px; }
dialog label input, dialog label textarea { margin-top: 4px; font-weight: 400; }
dialog .row { margin-top: 6px; }
dialog .row.field { margin-top: 0; margin-bottom: 10px; }
dialog .row.field .btn { white-space: nowrap; }
/* Delete on the far left, apart from Save on the far right. */
dialog .row.actions { margin-top: 14px; }
#pick-options .btn { width: 100%; text-align: left; }
dialog .row.pair { margin-top: 0; align-items: flex-start; }
dialog .row.pair .grow { flex: 1; min-width: 0; }
dialog .row.pair .num { flex: 0 0 5.5em; }
dialog .row.pair input { width: 100%; }
dialog .row.actions .btn.danger { margin-right: auto; }
/* A link button's 4px padding put "Cancel" 12px from Save's border, closer
   than Save's own text sits to it, and it read as cramped. */
dialog .row.actions .btn.link { padding-left: 12px; padding-right: 12px; }

.toast {
  /* Same 16px gutters and 640px column as .view, so it lines up with the page. */
  position: fixed; left: 16px; right: 16px; margin: 0 auto; max-width: 608px;
  bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateY(20px);
  background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px;
  font-size: 15px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 10;
}
.toast.show { opacity: 1; transform: translateY(0); }
