:root {
  --ink: #2b2118;
  --muted: #74675b;
  --paper: #fbf8f3;
  --card: #ffffff;
  --line: #e7ddd0;
  --accent: #7a3e1d;
  --accent-ink: #ffffff;
  --accent-soft: #f3e4d8;
  --ok: #1f7a4d;
  --ok-soft: #dff2e7;
  --warn: #8a5a00;
  --warn-soft: #fbefd2;
  --bad: #a1271f;
  --bad-soft: #fbe2df;
  --info-soft: #e3eef8;
  --shadow: 0 1px 2px rgb(0 0 0 / 0.06), 0 4px 16px rgb(0 0 0 / 0.06);
  --tabs-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --ink: #efe7dc;
    --muted: #b1a493;
    --paper: #181512;
    --card: #231e1a;
    --line: #3a322a;
    --accent: #e09460;
    --accent-ink: #181512;
    --accent-soft: #3d2a1d;
    --ok: #6fd3a0;
    --ok-soft: #1b3326;
    --warn: #f1c46b;
    --warn-soft: #3a2f14;
    --bad: #ff8f84;
    --bad-soft: #3d1d1a;
    --info-soft: #1d2a36;
    --shadow: 0 1px 2px rgb(0 0 0 / 0.4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.45 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--ink);
  background: var(--paper);
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; }
h2 { font-size: 1.15rem; margin: 1.2em 0 0.5em; }
h3 { font-size: 1rem; margin: 0.8em 0 0.3em; }
.muted { color: var(--muted); }
.small { font-size: 0.88rem; }
.pad { padding: 16px; }
.hide { display: none !important; }

.appbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--accent); color: var(--accent-ink);
}
.appbar h1 { font-size: 1.1rem; margin: 0; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.net { font-size: 0.8rem; background: rgb(0 0 0 / 0.25); padding: 2px 8px; border-radius: 10px; }

main { max-width: 760px; margin: 0 auto; padding: 12px 16px 24px; outline: none; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(6, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs a {
  position: relative; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  color: var(--muted); text-decoration: none; font-size: 0.75rem;
}
.tabs a.on { color: var(--accent); font-weight: 600; }
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabs b {
  position: absolute; top: 6px; left: calc(50% + 6px);
  min-width: 18px; padding: 0 5px; border-radius: 9px; font-size: 0.7rem; line-height: 18px; text-align: center;
  background: var(--bad); color: #fff;
}

.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 14px; margin: 12px 0; box-shadow: var(--shadow); }
.card > :first-child { margin-top: 0; }
.card > :last-child { margin-bottom: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row > .grow { flex: 1; min-width: 0; }
.stack > * + * { margin-top: 10px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

button, .btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font: 600 0.95rem system-ui, sans-serif; text-decoration: none; cursor: pointer;
}
button.primary, .btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.danger { color: var(--bad); border-color: var(--bad); }
button.ghost { background: none; border-color: transparent; color: var(--accent); padding: 0 8px; }
button.small, .btn.small { min-height: 34px; padding: 0 10px; font-size: 0.85rem; }
button:disabled { opacity: 0.5; cursor: default; }
button.block, .btn.block { width: 100%; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 4px; }
input, select, textarea {
  width: 100%; min-height: 44px; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink); font: 16px system-ui, sans-serif;
}
textarea { min-height: 80px; resize: vertical; }
input[type='checkbox'] { width: 22px; height: 22px; min-height: 0; accent-color: var(--accent); }
.check { display: flex; gap: 10px; align-items: center; color: var(--ink); font-size: 0.95rem; }
fieldset { border: 0; padding: 0; margin: 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 36px; padding: 0 12px; border-radius: 18px; border: 1px solid var(--line);
  background: var(--card); color: var(--ink); font-size: 0.88rem; font-weight: 500;
}
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 0.75rem; font-weight: 600; background: var(--accent-soft); color: var(--accent); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.info { background: var(--info-soft); color: var(--ink); }

.note { padding: 10px 12px; border-radius: 10px; background: var(--info-soft); font-size: 0.92rem; }
.note.warn { background: var(--warn-soft); }
.note.bad { background: var(--bad-soft); }
.note.ok { background: var(--ok-soft); }

/* Scanner */
.viewfinder { position: relative; border-radius: 14px; overflow: hidden; background: #000; aspect-ratio: 4 / 3; }
.viewfinder video { width: 100%; height: 100%; object-fit: cover; display: block; }
.viewfinder .guide { position: absolute; left: 12%; right: 12%; top: 50%; height: 2px; background: rgb(255 80 60 / 0.85); box-shadow: 0 0 8px rgb(255 80 60); }
.viewfinder .vf-msg { position: absolute; left: 0; right: 0; bottom: 0; padding: 8px 12px; color: #fff; background: linear-gradient(transparent, rgb(0 0 0 / 0.7)); font-size: 0.9rem; }
.viewfinder .vf-tools { position: absolute; top: 8px; right: 8px; display: flex; gap: 6px; }
.viewfinder .vf-tools button { min-height: 36px; background: rgb(0 0 0 / 0.5); color: #fff; border-color: transparent; }
.viewfinder.idle { display: grid; place-items: center; }
.viewfinder.idle button { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.flash { animation: flash 0.5s ease-out; }
@keyframes flash { from { box-shadow: inset 0 0 0 6px var(--ok); } to { box-shadow: inset 0 0 0 0 transparent; } }

/* Books */
.book-head { display: grid; grid-template-columns: 84px 1fr; gap: 12px; align-items: start; }
.cover { width: 84px; aspect-ratio: 2 / 3; border-radius: 6px; object-fit: cover; background: var(--line); display: block; }
.book-head h2 { margin: 0 0 2px; font-size: 1.1rem; }
.list { list-style: none; padding: 0; margin: 0; }
.list > li { border-bottom: 1px solid var(--line); }
.list > li:last-child { border-bottom: 0; }
.list a.item, .list .item { display: grid; grid-template-columns: 44px 1fr auto; gap: 10px; align-items: center; padding: 10px 0; color: inherit; text-decoration: none; }
.list.selecting .item { grid-template-columns: 28px 44px 1fr auto; }
.thumb { width: 44px; height: 62px; border-radius: 4px; object-fit: cover; background: var(--line); display: block; }
.item .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .s { font-size: 0.85rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .p { text-align: right; font-weight: 600; }
.photos { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scroll-snap-type: x mandatory; }
.photos img { height: 160px; border-radius: 8px; scroll-snap-align: start; background: var(--line); }
.photos .add { flex: 0 0 auto; height: 160px; width: 110px; display: grid; place-items: center; border: 2px dashed var(--line); border-radius: 8px; color: var(--muted); font-size: 0.85rem; text-align: center; padding: 8px; }
.photo-slots { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.photo-slot { position: relative; aspect-ratio: 3 / 4; border: 2px dashed var(--line); border-radius: 10px; display: grid; place-items: center; overflow: hidden; color: var(--muted); font-size: 0.85rem; background: var(--paper); }
.photo-slot img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.photo-slot input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.stat b { display: block; font-size: 1.3rem; }
.stat span { font-size: 0.82rem; color: var(--muted); }
.value-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; }
.value-grid > div { background: var(--paper); border-radius: 10px; padding: 8px 10px; }
.value-grid b { display: block; font-size: 1.05rem; }
.value-grid span { font-size: 0.8rem; color: var(--muted); }
.links { display: flex; flex-wrap: wrap; gap: 6px 12px; font-size: 0.9rem; }
.big-count { font-size: 2.4rem; font-weight: 700; line-height: 1; }
.scan-result { padding: 12px; border-radius: 12px; font-weight: 600; }
.scan-result.counted { background: var(--ok-soft); color: var(--ok); }
.scan-result.unexpected, .scan-result.duplicate { background: var(--warn-soft); color: var(--warn); }
.scan-result.unknown { background: var(--bad-soft); color: var(--bad); }
.scan-result .sub { font-weight: 400; font-size: 0.9rem; color: var(--ink); }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.seg button { flex: 1; border: 0; border-radius: 0; min-height: 40px; font-weight: 500; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 0.92rem; margin: 0; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.menu a { display: flex; justify-content: space-between; align-items: center; padding: 14px 4px; color: var(--ink); text-decoration: none; border-bottom: 1px solid var(--line); }
.menu a:last-child { border-bottom: 0; }
.menu a::after { content: '›'; color: var(--muted); font-size: 1.3rem; }
pre.copy { white-space: pre-wrap; background: var(--paper); border: 1px solid var(--line); border-radius: 10px; padding: 10px; font: 0.9rem/1.4 system-ui, sans-serif; margin: 0; }
.bulkbar { position: sticky; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 8px); z-index: 4; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px; box-shadow: var(--shadow); }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom) + 12px); transform: translateX(-50%) translateY(20px);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px; background: #2b2118; color: #fff; font-size: 0.92rem;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 30;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.bad { background: var(--bad); }

dialog#sheet {
  width: min(640px, 100%); max-height: 90vh; margin: auto auto 0; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  border: 0; border-radius: 16px 16px 0 0; background: var(--card); color: var(--ink); overflow: auto;
}
@media (min-width: 700px) { dialog#sheet { margin: auto; border-radius: 16px; } }
dialog#sheet::backdrop { background: rgb(0 0 0 / 0.45); }
.sheet-close { position: sticky; top: 0; float: right; }
.sheet-close button { min-height: 36px; width: 36px; padding: 0; border-radius: 18px; }
#sheet-body h2 { margin-top: 4px; }

/* Watchlist */
.watch-row { display: block; padding: 10px 0; border-bottom: 1px solid var(--line); color: inherit; text-decoration: none; }
.watch-row:last-child { border-bottom: 0; }
.watch-row b { display: block; font-weight: 600; }
.watch-row .tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.filters select { min-width: 0; width: 100%; }
.reading { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); }
.reading:last-child { border-bottom: 0; }
.reading input[type='checkbox'] { margin-top: 4px; flex: none; width: 20px; height: 20px; }
.modules { margin: 0; padding: 0; list-style: none; }
.modules li { padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 0.92rem; }
.modules li:last-child { border-bottom: 0; }
