:root {
  --bg: #faf6f0;
  --surface: #ffffff;
  --text: #2b2118;
  --muted: #7b6d60;
  --border: #e7dccd;
  --accent: #7a4b2a;
  --accent-text: #ffffff;
  --accent-soft: #f1e4d4;
  --read: #2f7a4b;
  --read-soft: #dff1e5;
  --reading: #8a5a00;
  --reading-soft: #fbeccb;
  --lent: #2c5d8a;
  --lent-soft: #dceaf7;
  --danger: #b3261e;
  --radius: 12px;
  --shadow: 0 1px 3px rgb(60 40 20 / .08), 0 4px 12px rgb(60 40 20 / .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1814;
    --surface: #27211b;
    --text: #efe6db;
    --muted: #a8998a;
    --border: #3b322a;
    --accent: #d89a68;
    --accent-text: #1c1814;
    --accent-soft: #3a2c20;
    --read: #7fd19c;
    --read-soft: #1f3a29;
    --reading: #f0c060;
    --reading-soft: #3d311a;
    --lent: #8fc0ee;
    --lent-soft: #1d3247;
    --danger: #f2827a;
    --shadow: 0 1px 3px rgb(0 0 0 / .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

a { color: var(--accent); }
h1, h2, h3 { line-height: 1.25; }

/* ---------- Header / navigation ---------- */

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand {
  display: flex; align-items: center; gap: 10px;
  font-weight: 700; font-size: 1.2rem; text-decoration: none; color: var(--text);
}
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tabs a, .account-link {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 14px; border-radius: 999px;
  color: var(--muted); text-decoration: none; font-weight: 500;
}
.tabs a.active, .account-link.active { background: var(--accent-soft); color: var(--accent); }
.tabs svg, .account-link svg { width: 20px; height: 20px; fill: currentColor; }
.account-link { margin-left: 8px; padding: 8px; }
body.signed-out .tabs, body.signed-out .account-link { display: none; }

main {
  max-width: 1100px; margin: 0 auto;
  padding: 20px 20px 40px;
}

@media (max-width: 700px) {
  .topbar { padding: 10px 16px; }
  .tabs {
    position: fixed; left: 0; right: 0; bottom: 0;
    justify-content: space-around;
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    background: var(--surface);
    border-top: 1px solid var(--border);
  }
  .tabs a { flex-direction: column; gap: 2px; font-size: .75rem; padding: 6px 18px; }
  .tabs svg { width: 24px; height: 24px; }
  .account-link { margin-left: auto; }
  .account-link svg { width: 24px; height: 24px; }
  main { padding: 16px 16px calc(90px + env(safe-area-inset-bottom)); }
}

/* ---------- Controls ---------- */

input, select, textarea, button, .button {
  font: inherit; color: inherit;
}
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px;
}
button, .button {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 10px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer; text-decoration: none; font-weight: 500;
  white-space: nowrap;
}
button:disabled { opacity: .6; cursor: default; }
button.primary, .button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
button.danger { color: var(--danger); }
.link-button { color: var(--accent); cursor: pointer; font-size: .9rem; text-align: center; }

/* ---------- Library ---------- */

.toolbar { display: flex; gap: 10px; }
.toolbar select { width: auto; }

.chips { display: flex; gap: 8px; margin: 14px 0 18px; overflow-x: auto; scrollbar-width: none; }
.chip { border-radius: 999px; padding: 6px 14px; font-size: .9rem; }
.chip span { color: var(--muted); margin-left: 4px; font-variant-numeric: tabular-nums; }
.chip.active { background: var(--text); color: var(--bg); border-color: var(--text); }
.chip.active span { color: inherit; opacity: .7; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 22px 18px;
}
@media (max-width: 700px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(105px, 1fr)); gap: 18px 12px; }
}

.card { text-decoration: none; color: inherit; display: flex; flex-direction: column; }
.card:hover .cover { transform: translateY(-3px); }
.card-body h3 {
  margin: 8px 0 2px; font-size: .95rem;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card-body p { margin: 0; color: var(--muted); font-size: .85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.cover {
  width: 100%; aspect-ratio: 2 / 3; object-fit: cover;
  border-radius: 4px 8px 8px 4px;
  box-shadow: var(--shadow);
  background: var(--accent-soft);
  transition: transform .15s;
}
.cover.placeholder {
  display: flex; align-items: center; justify-content: center;
  padding: 12px; text-align: center;
  background: linear-gradient(135deg, var(--accent) 0%, color-mix(in srgb, var(--accent) 70%, black) 100%);
  color: var(--accent-text);
  font-weight: 600; font-size: .85rem;
  border-left: 6px solid color-mix(in srgb, var(--accent) 60%, black);
}
.cover.placeholder span { display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }

.badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.badge { font-size: .72rem; font-weight: 600; padding: 2px 8px; border-radius: 999px; white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.badge.read { background: var(--read-soft); color: var(--read); }
.badge.reading { background: var(--reading-soft); color: var(--reading); }
.badge.lent { background: var(--lent-soft); color: var(--lent); }

.empty { color: var(--muted); text-align: center; padding: 40px 0; grid-column: 1 / -1; }
.empty .button { margin-top: 8px; }

/* ---------- Forms ---------- */

.page-title { margin-top: 0; }
.book-form { display: grid; gap: 14px; max-width: 600px; }
.book-form label { display: grid; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--muted); }
.book-form label > * { font-weight: 400; color: var(--text); font-size: 1rem; }
.book-form .row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-cover { width: 110px; border-radius: 4px 8px 8px 4px; box-shadow: var(--shadow); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }

.inline-form { display: flex; gap: 8px; }
.inline-form.wrap { flex-wrap: wrap; }
.inline-form.wrap input[name=borrower] { flex: 1 1 200px; }
.inline-form.wrap input[type=date] { flex: 0 1 170px; }

.notice { background: var(--accent-soft); padding: 12px 14px; border-radius: 10px; }
.notice.error { color: var(--danger); }
.hint { color: var(--muted); }

/* ---------- Scan ---------- */

.scan { max-width: 600px; margin: 0 auto; }
#reader { border-radius: var(--radius); overflow: hidden; background: #000; border: 0 !important; }
#reader video { display: block; }
#reader[hidden] { display: none; }
#result { margin-top: 20px; }

/* ---------- Detail ---------- */

.detail { display: grid; grid-template-columns: 220px 1fr; gap: 36px; align-items: start; }
.detail-cover { display: grid; gap: 10px; position: sticky; top: 80px; }
.detail h1 { margin: 0 0 4px; font-size: 1.8rem; }
.detail h2 { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 28px 0 10px; }
.authors { font-size: 1.15rem; margin: 0 0 6px; }
.meta { color: var(--muted); font-size: .9rem; margin: 0; }
.prose { white-space: pre-line; margin: 0; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; border-right: 1px solid var(--border); }
.segmented button:last-child { border-right: 0; }
.segmented button.active { background: var(--accent); color: var(--accent-text); }
.inline-label { display: flex; align-items: center; gap: 10px; margin-top: 10px; color: var(--muted); font-size: .9rem; }
.inline-label input { width: auto; }

.loan-current {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  background: var(--lent-soft); color: var(--lent);
  padding: 12px 14px; border-radius: 10px;
}
.loan-current p { margin: 0; }
.history { margin-top: 12px; color: var(--muted); font-size: .9rem; }
.history summary { cursor: pointer; }
.history ul { padding-left: 18px; margin: 8px 0 0; }

@media (max-width: 700px) {
  .detail { grid-template-columns: 1fr; gap: 20px; }
  .detail-cover { position: static; width: 150px; margin: 0 auto; }
  .detail h1 { font-size: 1.5rem; }
}

/* ---------- Sign in / account ---------- */

.auth { max-width: 380px; margin: 6vh auto 0; text-align: center; }
.auth h1 { margin: 12px 0 4px; }
.auth .book-form { text-align: left; margin-top: 24px; }
.auth button[type=submit] { width: 100%; margin-top: 4px; }
.book-form label.check { display: flex; align-items: center; gap: 8px; font-size: .95rem; font-weight: 500; color: var(--text); }
.book-form label.check input { width: auto; margin: 0; }
.notice[hidden] { display: none; }

.auth .hint.small { font-size: .9rem; margin-top: 20px; }

.account-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; max-width: 600px; }
.section-title { font-size: 1.1rem; margin: 32px 0 14px; }
.subsection-title { font-size: 1rem; margin: 24px 0 10px; }
.badge.admin { background: var(--accent-soft); color: var(--accent); }

.user-list {
  list-style: none; padding: 0; margin: 0; max-width: 600px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.user-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  padding: 12px 14px; border-bottom: 1px solid var(--border);
}
.user-list li:last-child { border-bottom: 0; }
.user-actions { display: flex; gap: 8px; }
.user-actions button { padding: 6px 12px; font-size: .9rem; }
.reset-form { flex: 1 1 100%; }

/* ---------- Toast ---------- */

#toast {
  position: fixed; left: 50%; bottom: 24px; z-index: 20;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 999px; font-weight: 500;
  transition: opacity .2s, transform .2s;
  max-width: calc(100% - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: var(--danger); color: #fff; }
@media (max-width: 700px) { #toast { bottom: calc(90px + env(safe-area-inset-bottom)); } }
