:root {
  --bg: #f7f3ec;
  --paper: #fffdf8;
  --ink: #2a211b;
  --muted: #7a6c60;
  --line: #e6dccd;
  --accent: #c2502b;
  --accent-ink: #fff;
  --green: #2f7d5b;
  --wood-1: #8a5a36;
  --wood-2: #6e4428;
  --wood-shadow: rgba(40, 20, 5, 0.35);
  --wall: #efe5d5;
  --radius: 12px;
  --serif: 'Fraunces', Georgia, serif;
  --sans: 'Inter', system-ui, -apple-system, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--ink); font: 15px/1.5 var(--sans); }
a { color: inherit; }
button, input { font: inherit; }
h1, h2, h3 { font-family: var(--serif); font-weight: 700; line-height: 1.15; margin: 0 0 .4em; }
h1 { font-size: clamp(28px, 5vw, 44px); }
h2 { font-size: 24px; }
h3 { font-size: 18px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 16px; padding: 10px 24px; background: rgba(255, 253, 248, .92); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); }
.brand { font-family: var(--serif); font-weight: 700; font-size: 22px; text-decoration: none; display: flex; align-items: center; gap: 8px; }
.brand-mark { display: inline-grid; place-items: center; width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: #fff; font-size: 18px; }
#nav { display: flex; gap: 4px; margin-left: auto; align-items: center; flex-wrap: wrap; }
#nav a, #nav button { text-decoration: none; padding: 7px 12px; border-radius: 999px; color: var(--muted); font-weight: 500; background: none; border: 0; cursor: pointer; }
#nav a:hover, #nav button:hover { color: var(--ink); background: var(--wall); }
#nav a.active { color: var(--ink); background: var(--wall); }
.badge { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; margin-left: 4px; border-radius: 999px; background: var(--accent); color: #fff; font-size: 12px; font-weight: 600; }

main { max-width: 1180px; margin: 0 auto; padding: 28px 24px 80px; }

/* ---------- buttons & forms ---------- */
.btn { display: inline-flex; align-items: center; gap: 8px; padding: 10px 18px; border-radius: 999px; border: 1px solid var(--ink); background: var(--ink); color: #fff; font-weight: 600; cursor: pointer; text-decoration: none; }
.btn:hover { opacity: .9; }
.btn.accent { background: var(--accent); border-color: var(--accent); }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-weight: 500; font-size: 14px; }
.input { width: 100%; padding: 11px 14px; border: 1px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink); }
.input:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: transparent; }
.error { color: #b3261e; font-size: 14px; min-height: 1.2em; }
.card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 24px; }

/* ---------- landing ---------- */
.hero { display: grid; grid-template-columns: 1.2fr 1fr; gap: 48px; align-items: center; padding: 24px 0 40px; }
.hero p.lead { font-size: 18px; color: var(--muted); max-width: 520px; }
.steps { display: grid; gap: 12px; margin: 24px 0 0; padding: 0; list-style: none; }
.steps li { display: flex; gap: 12px; align-items: flex-start; }
.steps b { flex: none; display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%; background: var(--wall); font-family: var(--serif); }
.auth-tabs { display: flex; gap: 6px; margin-bottom: 18px; }
.auth-tabs button { flex: 1; padding: 9px; border-radius: 999px; border: 1px solid var(--line); background: none; cursor: pointer; font-weight: 600; color: var(--muted); }
.auth-tabs button.on { background: var(--ink); color: #fff; border-color: var(--ink); }
@media (max-width: 820px) { .hero { grid-template-columns: 1fr; gap: 24px; } }

/* ---------- page header ---------- */
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 20px; }
.page-head p { margin: 0; }

/* ---------- virtual bookshelf ---------- */
.bookcase { background: linear-gradient(var(--wall), #e7dac6); border: 14px solid var(--wood-2); border-radius: 8px; padding: 18px 18px 0; box-shadow: inset 0 10px 30px rgba(0, 0, 0, .12), 0 10px 30px rgba(60, 30, 10, .18); }
.shelf-row { position: relative; display: flex; align-items: flex-end; gap: 14px; padding: 14px 12px 0; min-height: 210px; margin-bottom: 34px; flex-wrap: nowrap; overflow-x: auto; scrollbar-width: thin; }
.shelf-row::after { content: ''; position: sticky; }
.shelf-board { position: relative; height: 18px; margin: -34px -18px 34px; background: linear-gradient(var(--wood-1), var(--wood-2)); box-shadow: 0 8px 12px var(--wood-shadow); }
.shelf-board:last-child { margin-bottom: 0; }
.shelf-book { position: relative; flex: none; width: 116px; transition: transform .2s; cursor: pointer; }
.shelf-book:hover, .shelf-book:focus-within { transform: translateY(-8px); }
.shelf-book .cover { width: 116px; height: 176px; box-shadow: 4px 4px 10px rgba(0, 0, 0, .35); border-radius: 2px 4px 4px 2px; }
.shelf-book .cover::before { content: ''; position: absolute; inset: 0 auto 0 0; width: 6px; background: linear-gradient(90deg, rgba(0,0,0,.25), rgba(255,255,255,.1), transparent); z-index: 2; border-radius: 2px 0 0 2px; }
.shelf-book.unavailable .cover { filter: grayscale(.7) brightness(.85); }
.shelf-tag { position: absolute; top: -10px; right: -8px; z-index: 2; background: var(--green); color: #fff; font-size: 11px; font-weight: 600; padding: 3px 7px; border-radius: 999px; box-shadow: 0 2px 6px rgba(0,0,0,.2); }
.shelf-label { margin-top: 8px; background: rgba(255, 252, 245, .92); border-radius: 4px; padding: 5px 6px; box-shadow: 0 1px 3px rgba(60, 30, 10, .15); text-align: center; }
.shelf-label .t { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; font-size: 12px; font-weight: 600; line-height: 1.25; color: var(--ink); }
.shelf-label .a { display: block; font-size: 11px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.shelf-empty { display: grid; place-items: center; text-align: center; min-height: 210px; color: var(--muted); padding: 20px; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; margin-top: 12px; font-size: 13px; color: var(--muted); }

/* ---------- covers ---------- */
.cover { position: relative; overflow: hidden; background: #ccc; border-radius: 4px; }
.cover img { position: relative; z-index: 1; width: 100%; height: 100%; object-fit: cover; display: block; }
.cover .fallback { position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: space-between; padding: 12px 10px; color: #fff; font-family: var(--serif); }
.cover .fallback .t { font-size: 14px; font-weight: 700; line-height: 1.2; display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.cover .fallback .a { font-family: var(--sans); font-size: 11px; opacity: .85; }

/* ---------- book grid (browse) ---------- */
.section { margin: 36px 0; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.section-head h2 { margin: 0; }
.row-scroll { display: grid; grid-auto-flow: column; grid-auto-columns: 150px; gap: 18px; overflow-x: auto; padding: 4px 2px 14px; scrollbar-width: thin; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 22px 18px; }
.book-card { position: relative; display: flex; flex-direction: column; gap: 8px; }
.book-card .cover { aspect-ratio: 2 / 3; width: 100%; box-shadow: 0 6px 14px rgba(40, 20, 5, .18); }
.book-card .title { font-weight: 600; line-height: 1.25; font-size: 14px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.book-card .author { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip { display: inline-block; font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: #e3f1ea; color: var(--green); }
.chip.gray { background: var(--wall); color: var(--muted); }
.heart { position: absolute; top: 8px; right: 8px; z-index: 3; width: 36px; height: 36px; border-radius: 50%; border: 0; background: rgba(255, 255, 255, .92); box-shadow: 0 2px 8px rgba(0, 0, 0, .2); cursor: pointer; display: grid; place-items: center; transition: transform .15s; }
.heart:hover { transform: scale(1.1); }
.heart svg { width: 20px; height: 20px; fill: none; stroke: var(--accent); stroke-width: 2; }
.heart.on svg { fill: var(--accent); }
.heart.pop { animation: pop .3s; }
@keyframes pop { 50% { transform: scale(1.35); } }

.cat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 18px; }
.cat { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.cat h3 { margin-bottom: 12px; }
.cat-list { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; }
.cat-list .book-card .title { font-size: 12px; }
.cat-list .book-card .author { display: none; }
.cat-list .heart { width: 28px; height: 28px; top: 4px; right: 4px; }
.cat-list .heart svg { width: 15px; height: 15px; }
.rank { position: absolute; top: 4px; left: 4px; z-index: 3; background: var(--ink); color: #fff; font-size: 11px; font-weight: 700; width: 20px; height: 20px; border-radius: 50%; display: grid; place-items: center; }

.searchbar { display: flex; gap: 10px; }
.searchbar .input { font-size: 16px; padding: 13px 16px; border-radius: 999px; }
.tabs { display: inline-flex; gap: 4px; padding: 4px; background: var(--wall); border-radius: 999px; margin-bottom: 20px; flex-wrap: wrap; }
.tabs button { padding: 8px 16px; border: 0; border-radius: 999px; background: none; cursor: pointer; font-weight: 600; color: var(--muted); }
.tabs button.on { background: var(--paper); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.notice { padding: 14px 16px; border-radius: 10px; background: var(--wall); color: var(--muted); }
.skeleton { background: linear-gradient(90deg, var(--wall), #f6efe3, var(--wall)); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 4px; aspect-ratio: 2/3; }
@keyframes sk { to { background-position: -200% 0; } }

/* ---------- add book ---------- */
.add-layout { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; align-items: start; }
@media (max-width: 820px) { .add-layout { grid-template-columns: 1fr; } }
.add-layout.single { grid-template-columns: 1fr; }
.photo-intro { display: flex; gap: 20px; justify-content: space-between; align-items: center; flex-wrap: wrap; }
.photo-intro .tips { margin: 8px 0 0; padding-left: 18px; }
.scan-progress { display: flex; gap: 16px; align-items: center; margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--line); }
.scan-progress img { width: 120px; max-height: 120px; object-fit: cover; border-radius: 8px; flex: none; }
.scan-progress p { margin: 0 0 6px; }
.scan-progress .bar { height: 6px; width: 220px; max-width: 100%; background: var(--wall); border-radius: 999px; overflow: hidden; }
.scan-progress .bar span { display: block; height: 100%; width: 40%; background: var(--accent); border-radius: 999px; animation: slide 1.4s ease-in-out infinite; }
@keyframes slide { 0% { transform: translateX(-100%); } 100% { transform: translateX(250%); } }
.scan-head { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; flex-wrap: wrap; margin-bottom: 14px; }
.scan-btns { display: flex; gap: 8px; }
.scan-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 10px; }
.scan-row { display: flex; gap: 12px; align-items: flex-start; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; position: relative; }
.scan-row.on { border-color: var(--green); box-shadow: 0 0 0 1px var(--green) inset; }
.scan-row input[type=checkbox] { width: 20px; height: 20px; margin-top: 4px; flex: none; accent-color: var(--green); cursor: pointer; }
.scan-row .cb-gap { width: 20px; flex: none; }
.scan-row .cover-btn { width: 56px; flex: none; }
.scan-row .cover { width: 56px; aspect-ratio: 2/3; }
.scan-row .cover .fallback { padding: 5px 4px; }
.scan-row .cover .fallback .t { font-size: 9px; -webkit-line-clamp: 4; }
.scan-row .cover .fallback .a { display: none; }
.scan-row .nocover { display: grid; place-items: center; background: var(--wall); color: var(--muted); font-size: 22px; flex: none; }
.scan-row .info { flex: 1; min-width: 0; padding-right: 22px; }
.scan-row .title { font-weight: 600; line-height: 1.25; }
.scan-row .readas { color: var(--muted); margin-top: 4px; display: flex; flex-wrap: wrap; gap: 4px 6px; align-items: center; }
.scan-row .alt { display: block; margin-top: 6px; color: var(--muted); }
.scan-row select { max-width: 100%; font: inherit; font-size: 12px; padding: 3px 4px; border: 1px solid var(--line); border-radius: 6px; background: #fff; }
.scan-row .remove { position: absolute; top: 6px; right: 6px; width: 26px; height: 26px; border: 0; border-radius: 50%; background: none; color: var(--muted); font-size: 18px; cursor: pointer; }
.scan-row .remove:hover { background: var(--wall); color: var(--ink); }
.chip.warn { background: #fdecc8; color: #8a5a00; }
.scan-foot { display: flex; justify-content: flex-end; margin-top: 16px; position: sticky; bottom: 0; background: var(--paper); padding: 12px 0 4px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
@media (max-width: 640px) { .scan-list { grid-template-columns: 1fr; } .scan-foot .btn { width: 100%; } .scan-progress img { width: 80px; } }
.scanner { position: relative; width: 100%; aspect-ratio: 4 / 3; background: #111; border-radius: var(--radius); overflow: hidden; }
.scanner video { width: 100%; height: 100%; object-fit: cover; }
.scanner .frame { position: absolute; left: 12%; right: 12%; top: 34%; bottom: 34%; border: 3px solid rgba(255, 255, 255, .9); border-radius: 10px; box-shadow: 0 0 0 999px rgba(0, 0, 0, .35); }
.scanner .laser { position: absolute; left: 14%; right: 14%; top: 50%; height: 2px; background: var(--accent); box-shadow: 0 0 10px var(--accent); animation: laser 1.6s ease-in-out infinite alternate; }
@keyframes laser { from { transform: translateY(-30px); } to { transform: translateY(30px); } }
.scanner .msg { position: absolute; inset: 0; display: grid; place-items: center; color: #ddd; text-align: center; padding: 20px; }
.result { display: flex; gap: 18px; align-items: flex-start; }
.result .cover { flex: none; width: 110px; aspect-ratio: 2 / 3; }
.result-list { display: grid; gap: 12px; }
.result-item { display: flex; gap: 14px; align-items: center; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
.result-item .cover { flex: none; width: 52px; aspect-ratio: 2/3; }
.result-item .info { flex: 1; min-width: 0; }
.result-item .info .title { font-weight: 600; }

/* ---------- matches ---------- */
.match { display: grid; gap: 16px; }
.match-card { background: var(--paper); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.match-swap { display: grid; grid-template-columns: 1fr auto 1fr; gap: 18px; align-items: center; margin: 14px 0; }
.match-swap .arrow { font-size: 28px; color: var(--accent); }
.mini-books { display: flex; gap: 10px; flex-wrap: wrap; }
.mini-book { width: 74px; }
.mini-book .cover { width: 74px; aspect-ratio: 2/3; }
.mini-book .t { font-size: 12px; line-height: 1.2; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
@media (max-width: 640px) { .match-swap { grid-template-columns: 1fr; } .match-swap .arrow { transform: rotate(90deg); justify-self: center; } }

/* ---------- book detail popover ---------- */
.modal-bg { position: fixed; inset: 0; z-index: 50; background: rgba(30, 20, 10, .45); display: grid; place-items: center; padding: 16px; }
.modal { background: var(--paper); border-radius: 16px; padding: 24px; max-width: 520px; width: 100%; }
.modal .result .cover { width: 130px; }
.modal { position: relative; max-height: calc(100vh - 32px); overflow-y: auto; }
.modal.wide { max-width: 640px; }
.modal-x { position: absolute; top: 10px; right: 12px; width: 34px; height: 34px; border: 0; border-radius: 50%; background: var(--wall); font-size: 22px; line-height: 1; cursor: pointer; color: var(--ink); }
.modal-cover { position: relative; flex: none; }
.modal .result h2 { padding-right: 30px; }
.cover-btn { display: block; width: 100%; padding: 0; border: 0; background: none; cursor: pointer; border-radius: 4px; }
.cover-btn:hover .cover, .cover-btn:focus-visible .cover { transform: translateY(-3px); box-shadow: 0 10px 20px rgba(40, 20, 5, .25); }
.cover-btn .cover { transition: transform .15s, box-shadow .15s; }
.details { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.details h3 { font-size: 15px; margin: 14px 0 6px; }
.details h3:first-child { margin-top: 0; }
.synopsis p { margin: 0 0 10px; line-height: 1.55; }
.synopsis.clamped { max-height: 200px; overflow: hidden; -webkit-mask-image: linear-gradient(#000 70%, transparent); mask-image: linear-gradient(#000 70%, transparent); }
.linkbtn { border: 0; background: none; padding: 0; color: var(--accent); font-weight: 600; cursor: pointer; margin-bottom: 8px; }
.rating { margin: 0; }
.stars { --r: 0; display: inline-block; font-size: 17px; letter-spacing: 2px; background: linear-gradient(90deg, #e0a030 calc(var(--r) / 5 * 100%), #d8cfc2 0); -webkit-background-clip: text; background-clip: text; color: transparent; }
.stars::before { content: '★★★★★'; }
.tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; }
.links { display: flex; flex-wrap: wrap; gap: 8px 16px; margin: 0; }
.links a { font-weight: 600; }
.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { width: 40px; height: 22px; appearance: none; background: #ccc; border-radius: 999px; position: relative; cursor: pointer; transition: background .2s; }
.switch input::after { content: ''; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch input:checked { background: var(--green); }
.switch input:checked::after { transform: translateX(18px); }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%) translateY(120px); background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 999px; transition: transform .25s; z-index: 60; max-width: calc(100% - 32px); }
#toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 640px) {
  .topbar { padding: 10px 16px; flex-wrap: wrap; }
  #nav { margin-left: 0; width: 100%; overflow-x: auto; flex-wrap: nowrap; }
  main { padding: 20px 16px 80px; }
  .cat-list { grid-template-columns: repeat(5, minmax(56px, 1fr)); }
  .cat-grid { grid-template-columns: 1fr; }
  .shelf-book, .shelf-book .cover { width: 92px; }
  .shelf-book .cover { height: 140px; }
  .shelf-row { min-height: 170px; }
}

/* compact cards inside popular categories */
.chip { white-space: nowrap; align-self: flex-start; }
.cat-list .chip { display: none; }
.cat-list .cover .fallback { padding: 22px 5px 6px; }
.cat-list .cover .fallback .t { font-size: 10px; -webkit-line-clamp: 4; }
.cat-list .cover .fallback .a { display: none; }
.cat-list .book-card { gap: 5px; }
#nav a, #nav button { white-space: nowrap; }
.result-item .cover .fallback, .mini-book .cover .fallback { padding: 6px 5px; }
.result-item .cover .fallback .t { font-size: 9px; -webkit-line-clamp: 4; }
.result-item .cover .fallback .a { display: none; }
.mini-book .cover .fallback .t { font-size: 11px; }
@media (max-width: 640px) {
  .bookcase { border-width: 8px; padding: 12px 10px 0; }
  .shelf-row { gap: 8px; padding: 12px 4px 0; min-height: 120px; margin-bottom: 26px; }
  .shelf-board { margin: -26px -10px 26px; height: 12px; }
  .shelf-book, .shelf-book .cover { width: 84px; }
  .shelf-book .cover { height: 126px; }
  .shelf-label { padding: 4px; margin-top: 6px; }
  .shelf-label .t { font-size: 10.5px; }
  .shelf-label .a { font-size: 9.5px; }
  .shelf-book .cover .fallback { padding: 6px 5px; }
  .shelf-book .cover .fallback .t { font-size: 10px; }
  .shelf-book .cover .fallback .a { font-size: 9px; }
  .shelf-tag { font-size: 10px; padding: 2px 5px; right: -4px; }
}
.tabs button { white-space: nowrap; }
@media (max-width: 640px) { .tabs button { padding: 7px 10px; font-size: 14px; } .tabs { flex-wrap: nowrap; } }
