/* Home Library. Palette borrows from cloth bookbindings: bottle-green cloth, gilt stamping, paper. */
:root {
  --paper: #f3f5f2;
  --surface: #ffffff;
  --cloth: #24403d;
  --cloth-soft: #dfe8e3;
  --gilt: #b98a2a;
  --gilt-bright: #d9ac4b;
  --ink: #1d2523;
  --muted: #5f6b67;
  --line: #d6ddd9;
  --alert: #b4432f;
  --alert-soft: #f7e2dc;
  --ok: #2f6b4f;
  --shadow: 0 1px 2px rgba(29, 37, 35, .08), 0 6px 18px -8px rgba(29, 37, 35, .25);
  --serif: "Literata", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --sans: "Public Sans", system-ui, -apple-system, "Segoe UI", sans-serif;
  --nav-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #121a19; --surface: #1b2624; --cloth: #8fb3a6; --cloth-soft: #22322f;
    --gilt: #d9ac4b; --gilt-bright: #e8c46f; --ink: #e6ece9; --muted: #9aa8a3; --line: #2c3b38;
    --alert: #e27a63; --alert-soft: #3a2420; --ok: #7cc39f;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 20px -10px rgba(0,0,0,.7);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.5 var(--sans);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; }
img { display: block; max-width: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--gilt); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- layout ---------- */
.shell { min-height: 100vh; }
.view { padding: 20px 16px calc(var(--nav-h) + 32px + env(safe-area-inset-bottom)); max-width: 1180px; margin: 0 auto; outline: none; }

.nav {
  position: fixed; inset: auto 0 0 0; z-index: 20;
  height: calc(var(--nav-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; justify-content: space-around; align-items: stretch;
  background: var(--surface); border-top: 1px solid var(--line);
}
.brand { display: none; }
.nav-link {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  text-decoration: none; font-size: 12px; font-weight: 600; color: var(--muted); position: relative;
}
.nav-link svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.nav-link[aria-current="page"] { color: var(--cloth); }
.nav-scan svg { width: 46px; height: 46px; padding: 10px; border-radius: 50%; background: var(--cloth); color: var(--paper); margin-top: -22px; box-shadow: var(--shadow); }
.nav-scan[aria-current="page"] svg { background: var(--gilt); }
.nav-badge { position: absolute; top: 8px; left: calc(50% + 6px); background: var(--cloth); color: #fff; font-size: 11px; min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px; display: grid; place-items: center; }

@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: 220px 1fr; }
  .nav { position: sticky; top: 0; height: 100vh; flex-direction: column; justify-content: flex-start; gap: 2px; padding: 20px 12px; border-top: 0; border-right: 1px solid var(--line); }
  .brand { display: flex; align-items: center; gap: 10px; padding: 4px 10px 22px; text-decoration: none; font: 600 18px/1.2 var(--serif); }
  .nav-link { flex: none; flex-direction: row; justify-content: flex-start; gap: 12px; padding: 10px 12px; border-radius: 8px; font-size: 15px; }
  .nav-link:hover { background: var(--cloth-soft); }
  .nav-link[aria-current="page"] { background: var(--cloth-soft); }
  .nav-scan svg { width: 24px; height: 24px; padding: 0; margin: 0; background: none; color: inherit; box-shadow: none; }
  .nav-scan[aria-current="page"] svg { background: none; }
  .nav-badge { position: static; margin-left: auto; }
}
.nav-badge.overdue { background: var(--alert); }
@media (min-width: 900px) {
  .view { padding: 32px 40px 64px; }
}

/* ---------- type ---------- */
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; margin: 0; }
h1 { font-size: 30px; letter-spacing: -.01em; }
h2 { font-size: 21px; }
h3 { font-size: 17px; }
p { margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 14px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 18px; flex-wrap: wrap; }
.page-head p { margin-top: 4px; }

/* ---------- controls ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; padding: 0 16px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); font-weight: 600; font-size: 15px; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--cloth); border-color: var(--cloth); color: var(--paper); }
.btn-primary:hover { border-color: var(--cloth); filter: brightness(1.1); }
.btn-gilt { background: var(--gilt); border-color: var(--gilt); color: #1d1608; }
.btn-danger { color: var(--alert); }
.btn-quiet { border-color: transparent; background: transparent; }
.btn-sm { min-height: 34px; padding: 0 12px; font-size: 14px; }
.btn:disabled { opacity: .55; cursor: default; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 14px; font-weight: 600; color: var(--muted); }
input[type=text], input[type=search], input[type=number], input[type=date], input[type=password], input[type=url], input:not([type]), select, textarea {
  width: 100%; min-height: 42px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface);
}
textarea { min-height: 96px; resize: vertical; line-height: 1.45; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--gilt); outline-offset: -1px; border-color: transparent; }
.form-grid { display: grid; gap: 14px; grid-template-columns: 1fr; }
@media (min-width: 640px) { .form-grid { grid-template-columns: 1fr 1fr; } .span-2 { grid-column: span 2; } }

.search { position: relative; flex: 1 1 280px; }
.search input { padding-left: 40px; min-height: 46px; font-size: 16px; }
.search svg { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; stroke: var(--muted); fill: none; stroke-width: 2; }

.filters { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 12px 0 20px; }
.filters select { width: auto; min-height: 36px; padding: 4px 30px 4px 10px; font-size: 14px; flex: none; }
.filters .chip { flex: none; }
@media (max-width: 899px) {
  .filters { flex-wrap: nowrap; overflow-x: auto; margin-left: -16px; margin-right: -16px; padding: 2px 16px 4px; scrollbar-width: none; }
  .filters::-webkit-scrollbar { display: none; }
  .hide-mobile { display: none !important; }
}
.chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 0 12px; border-radius: 18px;
  border: 1px solid var(--line); background: var(--surface); font-size: 14px; font-weight: 600; cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--cloth); border-color: var(--cloth); color: var(--paper); }

.tag { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px; border-radius: 12px; font-size: 13px; font-weight: 600; background: color-mix(in srgb, var(--tag, var(--gilt)) 16%, transparent); color: var(--ink); }
.tag::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--tag, var(--gilt)); }
.tag button { border: 0; background: none; padding: 0 0 0 2px; cursor: pointer; color: var(--muted); font-size: 15px; line-height: 1; }

/* ---------- the shelf grid ---------- */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); gap: 20px 14px; }
@media (min-width: 900px) { .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 28px 22px; } }
.card { text-decoration: none; display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.card-title { font: 600 14px/1.3 var(--serif); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-author { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: -4px; }
.card .flag { align-self: flex-start; font-size: 12px; font-weight: 700; padding: 1px 8px; border-radius: 10px; background: var(--cloth-soft); color: var(--cloth); }
.card .flag.overdue { background: var(--alert-soft); color: var(--alert); }

/* Covers sit like a real book: a binding shadow down the left edge. */
.cover {
  position: relative; aspect-ratio: 2 / 3; width: 100%; border-radius: 2px 5px 5px 2px; overflow: hidden;
  background: var(--cloth-soft); box-shadow: var(--shadow);
}
.cover img { width: 100%; height: 100%; object-fit: cover; }
.cover::after {
  content: ""; position: absolute; inset: 0; pointer-events: none;
  background: linear-gradient(90deg, rgba(0,0,0,.28) 0, rgba(255,255,255,.18) 3%, rgba(0,0,0,.08) 5%, transparent 9%);
}
.card:hover .cover { transform: translateY(-3px); }
.cover { transition: transform .18s ease; }

/* No cover? Bind one in cloth with gilt lettering. */
.cloth {
  --h: 160;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,.035) 0 2px, transparent 2px 4px),
    hsl(var(--h) 32% 28%);
  display: flex; flex-direction: column; justify-content: center; align-items: center; text-align: center;
  padding: 14% 12%; color: var(--gilt-bright);
}
.cloth .t { font: 600 clamp(12px, 1.1vw + 6px, 16px)/1.2 var(--serif); display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.cloth .a { font: italic 400 11px/1.3 var(--serif); margin-top: 10px; opacity: .85; }
.cloth::before { content: ""; position: absolute; inset: 7% 9%; border: 1px solid currentColor; opacity: .45; border-radius: 1px; }

.empty { padding: 48px 20px; text-align: center; border: 1px dashed var(--line); border-radius: 12px; display: grid; gap: 10px; justify-items: center; }
.empty h2 { font-size: 22px; }
.empty p { max-width: 42ch; color: var(--muted); }

/* ---------- panels ---------- */
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 18px; }
.panel + .panel { margin-top: 16px; }
.panel h2 { margin-bottom: 12px; }
.stack { display: grid; gap: 16px; }

/* ---------- book detail ---------- */
.detail { display: grid; gap: 24px; grid-template-columns: 1fr; }
.detail-cover { width: min(220px, 60vw); justify-self: center; }
@media (min-width: 760px) { .detail { grid-template-columns: 240px 1fr; gap: 36px; } .detail-cover { width: 100%; justify-self: stretch; } }
.detail-title h1 { font-size: 32px; }
.detail-title .sub { font: italic 400 19px/1.35 var(--serif); color: var(--muted); margin-top: 4px; }
.detail-title .by { margin-top: 10px; font-size: 17px; }
.series-line { color: var(--gilt); font-weight: 600; font-size: 14px; margin-bottom: 6px; }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 6px 18px; font-size: 14px; margin: 0; }
.facts dt { color: var(--muted); }
.facts dd { margin: 0; overflow-wrap: anywhere; }
.description { font: 400 16px/1.65 var(--serif); max-width: 68ch; white-space: pre-line; }
.description.clamped { display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }
.cover-actions { display: flex; gap: 6px; justify-content: center; margin-top: 10px; flex-wrap: wrap; }

.loan-box { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; padding: 14px 16px; border-radius: 10px; background: var(--cloth-soft); }
.loan-box.overdue { background: var(--alert-soft); }
.loan-box.overdue strong { color: var(--alert); }

.tag-editor { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.tag-editor input { width: 160px; min-height: 32px; padding: 4px 10px; font-size: 14px; }

/* ---------- lists ---------- */
.list { display: grid; gap: 0; }
.row { display: flex; align-items: center; gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row .thumb { width: 44px; flex: none; }
.row .grow { flex: 1; min-width: 0; }
.row .grow a { text-decoration: none; font: 600 16px/1.3 var(--serif); }
.row .grow p { font-size: 14px; color: var(--muted); }
.row.overdue .due { color: var(--alert); font-weight: 600; }

/* ---------- scanner ---------- */
.scan-layout { display: grid; gap: 20px; }
@media (min-width: 900px) { .scan-layout { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr); align-items: start; } }
.viewfinder {
  position: relative; border-radius: 14px; overflow: hidden; background: #0d1312; aspect-ratio: 4 / 3; color: #e6ece9;
}
.viewfinder video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.viewfinder .reticle {
  position: absolute; left: 7%; right: 7%; top: 28%; bottom: 28%; pointer-events: none;
  border: 2px solid rgba(217,172,75,.9); border-radius: 8px; box-shadow: 0 0 0 9999px rgba(0,0,0,.35);
}
.viewfinder .reticle::after { content: ""; position: absolute; left: 6%; right: 6%; top: 50%; height: 2px; background: #e8c46f; opacity: .8; animation: sweep 1.8s ease-in-out infinite alternate; }
@keyframes sweep { from { transform: translateY(-34px); } to { transform: translateY(34px); } }
@media (prefers-reduced-motion: reduce) { .viewfinder .reticle::after { animation: none; } .cover { transition: none; } }
.viewfinder.flash .reticle { border-color: #7cc39f; box-shadow: 0 0 0 9999px rgba(47,107,79,.35); }
.viewfinder .vf-msg { position: absolute; inset: 0; display: grid; place-content: center; gap: 14px; text-align: center; padding: 24px; }
.viewfinder .vf-msg p { max-width: 36ch; color: #b8c4c0; }

.segmented { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; padding: 3px; background: var(--surface); }
.segmented button { border: 0; background: none; padding: 7px 14px; border-radius: 6px; font-weight: 600; font-size: 14px; cursor: pointer; color: var(--muted); }
.segmented button[aria-pressed="true"] { background: var(--cloth); color: var(--paper); }

.result { display: grid; grid-template-columns: 96px 1fr; gap: 16px; align-items: start; }
.result h3 { font-size: 19px; }
.notice { padding: 10px 14px; border-radius: 8px; font-size: 14px; background: var(--cloth-soft); }
.notice.warn { background: var(--alert-soft); color: var(--ink); }
.notice.warn strong { color: var(--alert); }
.session-log .row { padding: 8px 0; }
.status-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--muted); }
.status-dot.added { background: var(--ok); }
.status-dot.duplicate { background: var(--gilt); }
.status-dot.not_found, .status-dot.error { background: var(--alert); }

/* ---------- modal & toasts ---------- */
.modal { border: 0; border-radius: 14px; padding: 0; width: min(520px, calc(100vw - 32px)); background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.modal::backdrop { background: rgba(10, 16, 15, .5); }
.modal form { padding: 20px; display: grid; gap: 14px; }
.modal .btn-row { justify-content: flex-end; margin-top: 4px; }

.toasts { position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); display: grid; gap: 8px; z-index: 50; width: min(440px, calc(100vw - 32px)); }
@media (min-width: 900px) { .toasts { bottom: 24px; left: auto; right: 24px; transform: none; } }
.toast { background: var(--ink); color: var(--paper); padding: 12px 16px; border-radius: 10px; box-shadow: var(--shadow); font-size: 15px; display: flex; gap: 12px; align-items: center; justify-content: space-between; }
.toast.error { background: var(--alert); color: #fff; }
.toast button { background: none; border: 0; color: var(--gilt-bright); font-weight: 700; cursor: pointer; }

.spinner { width: 22px; height: 22px; border: 3px solid var(--line); border-top-color: var(--cloth); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { display: flex; justify-content: center; padding: 48px; }

.org-grid { display: grid; gap: 16px; }
@media (min-width: 900px) { .org-grid { grid-template-columns: 1fr 1fr; align-items: start; } }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.inline-form input { flex: 1 1 140px; width: auto; min-width: 0; }
.inline-form.nowrap { flex-wrap: nowrap; }
input[type=color] { width: 42px; height: 42px; padding: 3px; border: 1px solid var(--line); border-radius: 8px; background: var(--surface); }
.stat-line { font-size: 15px; color: var(--muted); }
.stat-line strong { color: var(--ink); }

.detail-cover .cloth .t { font-size: 22px; }
.detail-cover .cloth .a { font-size: 15px; }
.inline-form input[type=color] { flex: none; width: 42px; }

/* ---------- sign in ---------- */
.signed-out .nav { display: none; }
.signed-out .shell { display: block; }
.signed-out .view { padding-bottom: 32px; }
.auth-wrap { min-height: calc(100vh - 64px); display: grid; place-items: center; padding: 24px 0; }
.auth-card { width: min(420px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 28px 24px; box-shadow: var(--shadow); }
.auth-card h1 { font-size: 26px; margin-bottom: 6px; }
.auth-brand { display: flex; align-items: center; gap: 10px; font: 600 18px/1.2 var(--serif); margin-bottom: 22px; color: var(--cloth); }
.auth-card .btn-primary { width: 100%; min-height: 46px; }
.auth-card input[readonly] { color: var(--muted); }
.field small { font-size: 13px; }

.loan-box.ask { background: color-mix(in srgb, var(--gilt) 16%, var(--surface)); }
.nav-badge.ask { background: var(--gilt); color: #1d1608; }
#linkOut { font-size: 14px; }
.row select { min-height: 36px; padding: 4px 28px 4px 10px; font-size: 14px; flex: none; }
@media (max-width: 520px) { .row:has(select) { flex-wrap: wrap; } .row:has(select) .grow { flex-basis: 100%; } }
@media (max-width: 640px) {
  .row.stack-mobile { flex-wrap: wrap; }
  .row.stack-mobile .grow { flex-basis: calc(100% - 58px); }
  .row.stack-mobile .btn-row { margin-left: 58px; }
}

.cam-tools { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.cam-tools select { width: auto; min-height: 34px; padding: 4px 28px 4px 10px; font-size: 14px; max-width: 100%; }
.cam-tools .btn[aria-pressed="true"] { background: var(--gilt); border-color: var(--gilt); color: #1d1608; }

/* ---------- branding ---------- */
.brand img, .auth-brand img { border-radius: 7px; object-fit: cover; box-shadow: 0 0 0 1px var(--line); }
.brand span { overflow: hidden; text-overflow: ellipsis; }
.brand-preview { display: flex; gap: 14px; align-items: center; padding: 14px; border-radius: 12px; background: var(--paper); }
.app-icon { width: 72px; height: 72px; border-radius: 17px; overflow: hidden; flex: none; box-shadow: var(--shadow); background: var(--surface); }
.app-icon img { width: 100%; height: 100%; object-fit: cover; }
.logo-options { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin: 6px 0 2px; }
