:root {
  --paper: #f5f0e6;
  --card: #fffcf5;
  --ink: #1b2620;
  --muted: #66695f;
  --line: #e2d9c7;
  --green: #1f4d3a;
  --green-ink: #f5f0e6;
  --amber: #f0b429;
  --amber-ink: #2a1d00;
  --red: #a8342a;
  --focus: #2f7d5b;
  --cam: #0e1512;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --tab-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #121815;
    --card: #1a221e;
    --ink: #ece6d8;
    --muted: #a2a597;
    --line: #2b3530;
    --green: #8fd0ad;
    --green-ink: #0e1a14;
    --red: #f08a7e;
    --focus: #8fd0ad;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 2.5px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

.ic { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.ic-xl { width: 44px; height: 44px; stroke-width: 1.4; color: var(--muted); }

/* ---------- layout ---------- */
main { height: 100%; }
.view { display: none; height: 100dvh; overflow-y: auto; padding-bottom: calc(var(--tab-h) + var(--safe-b) + 16px); }
.view.is-active { display: block; }
#view-scan { padding-bottom: 0; overflow: hidden; }

.bar {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-t) + 14px) 16px 12px;
  background: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.bar h2 { margin: 0 auto 0 0; font: 600 28px/1.1 var(--serif); letter-spacing: -0.01em; }
.count { font: 500 15px var(--sans); color: var(--muted); margin-left: 4px; font-variant-numeric: tabular-nums; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 18px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--ink);
  font-weight: 600; font-size: 15px; text-decoration: none;
  transition: transform .12s ease, background-color .15s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--green); border-color: var(--green); color: var(--green-ink); }
.btn-small { min-height: 38px; padding: 0 14px; font-size: 14px; }
.btn-small .ic { width: 18px; height: 18px; }
.btn-big { min-height: 56px; padding: 0 28px; font-size: 17px; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-danger { color: var(--red); }
.icon-btn {
  display: grid; place-items: center; width: 44px; height: 44px;
  border: 0; border-radius: 50%; background: transparent; color: var(--muted);
}
.file-btn { position: relative; overflow: hidden; }
.file-btn input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 5;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tab-h) + var(--safe-b)); padding-bottom: var(--safe-b);
  background: color-mix(in srgb, var(--card) 94%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid var(--line);
}
.tabbar button {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: 0; background: none; color: var(--muted); font-size: 11.5px; font-weight: 600; letter-spacing: .01em;
}
.tabbar button[aria-current="page"] { color: var(--green); }
body.on-scan .tabbar { background: rgba(10, 16, 13, .9); border-top-color: rgba(255,255,255,.08); }
body.on-scan .tabbar button { color: rgba(255,255,255,.6); }
body.on-scan .tabbar button[aria-current="page"] { color: var(--amber); }
.badge {
  position: absolute; top: 7px; left: calc(50% + 6px);
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px;
  background: var(--amber); color: var(--amber-ink); font-size: 11px; line-height: 18px; font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- scan ---------- */
.cam { position: relative; height: 100dvh; background: var(--cam); color: #f3efe6; overflow: hidden; }
.cam video, .cam canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.cam video { object-fit: cover; }
.cam canvas { pointer-events: none; }

.cam-idle {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center;
  padding: 24px 28px calc(var(--tab-h) + var(--safe-b) + 24px);
  background:
    radial-gradient(120% 70% at 50% 0%, rgba(240,180,41,.10), transparent 60%),
    linear-gradient(#14211b, #0e1512);
}
.cam-idle h1 { margin: 22px 0 8px; font: 600 40px/1.05 var(--serif); letter-spacing: -0.015em; }
.cam-idle p { margin: 0 0 26px; max-width: 30ch; color: rgba(243,239,230,.78); font-size: 16.5px; text-wrap: balance; }
.cam-idle .fine { margin: 18px 0 0; font-size: 13px; color: rgba(243,239,230,.5); }
.cam-idle .btn-primary { background: var(--amber); border-color: var(--amber); color: var(--amber-ink); }

.spines { display: flex; align-items: flex-end; gap: 5px; height: 92px; }
.spines i { display: block; width: 15px; border-radius: 3px 3px 2px 2px; background: rgba(243,239,230,.16); }
.spines i:nth-child(1) { height: 70px; }
.spines i:nth-child(2) { height: 84px; width: 19px; }
.spines i:nth-child(3) { height: 62px; }
.spines i:nth-child(4) { height: 92px; width: 21px; }
.spines i:nth-child(5) { height: 76px; }
.spines i:nth-child(6) { height: 66px; width: 12px; transform: rotate(-8deg); transform-origin: bottom left; margin-left: 4px; }
.spines i:nth-child(7) { height: 80px; opacity: .6; }
.spines i.hit { background: var(--amber); outline: 2px solid rgba(240,180,41,.45); outline-offset: 4px; }

.hud-top {
  position: absolute; inset: 0 0 auto 0; padding: calc(var(--safe-t) + 12px) 16px 28px;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  background: linear-gradient(rgba(8,12,10,.65), transparent);
}
.pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 8px 14px; border-radius: 999px;
  background: rgba(10,16,13,.7); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: #8fd0ad; animation: pulse 1.4s ease-in-out infinite; }
.pill.loading .dot { background: var(--amber); }
@keyframes pulse { 50% { opacity: .3; } }
.reading {
  margin: 0; max-width: 100%; min-height: 1.3em;
  font: 12.5px/1.3 ui-monospace, "SF Mono", Menlo, monospace; color: rgba(243,239,230,.62);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-transform: uppercase; letter-spacing: .04em;
}

.hud-bottom {
  position: absolute; inset: auto 0 calc(var(--tab-h) + var(--safe-b)) 0;
  display: grid; grid-template-columns: 52px 1fr 52px; align-items: center; gap: 12px;
  padding: 28px 20px 18px;
  background: linear-gradient(transparent, rgba(8,12,10,.6));
}
.round.is-off { visibility: hidden; }
.hud-bottom .btn-stop {
  grid-column: 2; justify-self: center; min-height: 52px; padding: 0 26px;
  background: rgba(243,239,230,.95); border: 0; color: #14211b;
}
.round {
  display: grid; place-items: center; width: 52px; height: 52px; border-radius: 50%;
  border: 1px solid rgba(255,255,255,.18); background: rgba(10,16,13,.55); color: #f3efe6;
}
.round[aria-pressed="true"] { background: var(--amber); color: var(--amber-ink); border-color: var(--amber); }

.found-card {
  position: absolute; left: 12px; right: 12px; bottom: calc(var(--tab-h) + var(--safe-b) + 92px);
  display: flex; align-items: center; gap: 12px;
  padding: 12px; border-radius: var(--radius);
  background: var(--amber); color: var(--amber-ink);
  box-shadow: 0 12px 36px rgba(0,0,0,.35);
  animation: rise .35s cubic-bezier(.2,.9,.3,1.2);
}
@keyframes rise { from { transform: translateY(24px) scale(.96); opacity: 0; } }
.found-card img { width: 52px; height: 78px; object-fit: cover; border-radius: 4px; background: rgba(0,0,0,.12); flex: none; }
.found-body { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.found-body strong { font: 600 19px/1.2 var(--serif); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.found-body span:last-child { font-size: 14px; opacity: .8; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.eyebrow { font-size: 11.5px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; opacity: .75; }
.found-card .btn { background: var(--amber-ink); color: var(--amber); border: 0; }

/* ---------- lists ---------- */
.filter-row { display: flex; gap: 8px; align-items: center; padding: 0 16px 10px; }
.filter-row input, .search-box input, .stack input:not([type="checkbox"]) {
  width: 100%; min-height: 44px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 12px; background: var(--card);
  font-size: 16px; /* 16px stops iOS zooming in on focus */
}
.filter-row input { flex: 1; min-width: 0; }

.books, .found-list, .results { list-style: none; margin: 0; padding: 0 16px; }
.book {
  display: flex; align-items: center; gap: 14px;
  padding: 12px 0; border-bottom: 1px solid var(--line);
}
.cover {
  width: 44px; height: 66px; flex: none; border-radius: 3px; object-fit: cover;
  background: var(--line); box-shadow: 0 1px 2px rgba(0,0,0,.12);
}
.cover-blank {
  position: relative; display: grid; place-items: center; padding: 5px 4px;
  background: var(--green); color: var(--green-ink);
  font: 600 8.5px/1.15 var(--serif); text-align: center; overflow: hidden;
}
.cover-blank > span { max-height: 100%; overflow: hidden; }
.cover-blank img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; opacity: 0; transition: opacity .3s ease; }
.cover-blank img.in { opacity: 1; }
.book-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.book-title { font: 600 17px/1.25 var(--serif); overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.book-author { font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.book-meta { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.book-meta.hot { color: var(--green); font-weight: 600; }
.book button.edit { align-self: stretch; border: 0; background: none; color: var(--muted); padding: 0 4px; min-width: 44px; }
.book-main { display: flex; align-items: center; gap: 14px; flex: 1; min-width: 0; border: 0; background: none; padding: 0; text-align: left; }

.result { display: flex; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.result .book-text { flex: 1; }
.result .btn { flex: none; }
.result .btn[disabled] { opacity: .55; }

.found-item { display: flex; gap: 12px; padding: 12px 0; border-bottom: 1px solid var(--line); align-items: center; }
.found-item .shot { width: 76px; height: 76px; flex: none; border-radius: 8px; object-fit: cover; background: var(--line); }
.found-item time { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.empty { padding: 48px 32px; text-align: center; color: var(--muted); }
.empty h3 { margin: 14px 0 6px; font: 600 22px var(--serif); color: var(--ink); }
.empty p { margin: 0 auto 20px; max-width: 32ch; text-wrap: balance; }

/* segmented control */
.seg { display: flex; padding: 3px; border-radius: 12px; background: color-mix(in srgb, var(--line) 70%, transparent); }
.seg button { flex: 1; min-height: 38px; border: 0; border-radius: 9px; background: none; font-size: 14px; font-weight: 600; color: var(--muted); white-space: nowrap; padding: 0 10px; }
.seg button[aria-selected="true"], .seg button[aria-checked="true"] { background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg-small { flex: none; }
.seg-small button { min-height: 38px; font-size: 13px; }

/* ---------- sheets ---------- */
.sheet {
  width: 100%; max-width: 560px; max-height: 92dvh;
  margin: auto auto 0; padding: 0 16px calc(var(--safe-b) + 20px);
  border: 0; border-radius: 20px 20px 0 0;
  background: var(--paper); color: var(--ink);
  overflow-y: auto; overscroll-behavior: contain;
}
.sheet[open] { animation: sheet-up .28s cubic-bezier(.2,.9,.3,1); }
@keyframes sheet-up { from { transform: translateY(40%); opacity: .4; } }
.sheet::backdrop { background: rgba(8,12,10,.5); }
.sheet-head {
  position: sticky; top: 0; z-index: 1; display: flex; align-items: center;
  margin: 0 -16px 8px; padding: 14px 8px 8px 16px; background: var(--paper);
}
.sheet-head h2 { margin: 0 auto 0 0; font: 600 24px var(--serif); }
.sheet .seg { margin-bottom: 14px; }
.pane { min-height: 320px; }
.search-box { display: flex; gap: 8px; }
.search-box input { flex: 1; min-width: 0; }
.hint { font-size: 13.5px; color: var(--muted); margin: 10px 2px; }
.hint.err { color: var(--red); }

.stack { display: flex; flex-direction: column; gap: 14px; }
.stack label:not(.check):not(.btn) { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 600; }
.stack small, fieldset small { font-weight: 400; color: var(--muted); font-size: 13px; }
.opt { font-weight: 400; color: var(--muted); }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-size: 15px; }
.check input { width: 22px; height: 22px; accent-color: var(--green); flex: none; }
.check small { color: var(--muted); }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row .btn { flex: 1; }
fieldset { border: 0; padding: 0; margin: 4px 0 0; display: flex; flex-direction: column; gap: 8px; }
legend { font: 600 17px var(--serif); margin-bottom: 8px; }
.small { font-size: 14px; color: var(--muted); margin: 0; }

.isbn-cam { position: relative; aspect-ratio: 4 / 3; border-radius: var(--radius); overflow: hidden; background: var(--cam); }
.isbn-cam video { width: 100%; height: 100%; object-fit: cover; }
.isbn-frame { position: absolute; inset: 30% 12%; border: 2px solid var(--amber); border-radius: 10px; box-shadow: 0 0 0 999px rgba(8,12,10,.35); }
.steps { margin: 0 0 12px; padding-left: 22px; line-height: 1.7; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--tab-h) + var(--safe-b) + 14px); z-index: 20;
  transform: translateX(-50%); max-width: calc(100% - 32px);
  padding: 10px 16px; border-radius: 12px;
  background: var(--ink); color: var(--paper); font-size: 14.5px; font-weight: 600;
  box-shadow: 0 8px 24px rgba(0,0,0,.25);
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
@media (min-width: 700px) {
  .books, .found-list, .results, .filter-row, .bar { max-width: 640px; margin-inline: auto; }
  .sheet { margin: auto; border-radius: 20px; }
}
