:root {
  --bg: #f6f4ef;
  --surface: #fffdf8;
  --surface-2: #efeae0;
  --ink: #26221c;
  --ink-soft: #6b6459;
  --line: #e2dccf;
  --accent: #9a3b2e;
  --accent-soft: #f3e4e0;
  --star: #d99a2b;
  --shadow: 0 6px 24px rgba(40, 32, 20, 0.12);
  --radius: 12px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171512;
    --surface: #201d18;
    --surface-2: #2a251f;
    --ink: #efe9df;
    --ink-soft: #a79f92;
    --line: #35302a;
    --accent: #e0836f;
    --accent-soft: #3a2620;
    --star: #e0b256;
    --shadow: 0 6px 28px rgba(0, 0, 0, 0.45);
  }
}

:root {
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
    "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; } /* beats display:grid/flex on hidden elements */
html { scroll-behavior: smooth; }
body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; flex-shrink: 0; }
.brand-mark { font-size: 30px; }
.topbar h1 { margin: 0; font-size: 22px; letter-spacing: 0.3px; }
.tagline { margin: 0; font-size: 12px; color: var(--ink-soft); }
.search-wrap { flex: 1; }
#search {
  width: 100%;
  padding: 11px 16px;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 999px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
#search:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.manage-link {
  flex-shrink: 0;
  text-decoration: none;
  color: var(--accent);
  border: 1px solid var(--line);
  padding: 9px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-family: system-ui, sans-serif;
}
.manage-link:hover { background: var(--accent-soft); }

/* ---------- Filters ---------- */
.filters {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 16px;
  margin: 10px 24px 4px;
  padding: 18px 20px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  animation: dropIn 0.16s ease;
}
@keyframes dropIn { from { opacity: 0; transform: translateY(-6px); } }
.filter-group { display: flex; align-items: flex-start; gap: 10px; max-width: 100%; }
.filter-group > label {
  font-family: system-ui, sans-serif;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-soft);
  padding-top: 6px;
  flex-shrink: 0;
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font-family: system-ui, sans-serif;
  font-size: 13px;
  padding: 4px 11px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink-soft);
  cursor: pointer;
  transition: all 0.12s;
}
.chip:hover { border-color: var(--accent); color: var(--ink); }
.chip.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.chip .count { opacity: 0.6; font-size: 11px; margin-left: 4px; }

/* ---------- Word cloud ---------- */
.wordcloud-group { width: 100%; }
.wordcloud {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px 12px;
  line-height: 1.3; max-width: 900px;
}
.cloud-word {
  font-family: inherit; background: none; border: none; cursor: pointer;
  color: var(--accent); padding: 0; line-height: 1;
  transition: transform 0.1s;
}
.cloud-word:hover { transform: scale(1.12); text-decoration: underline; }
.clear-btn {
  font-family: system-ui, sans-serif;
  font-size: 13px;
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 6px 0;
  text-decoration: underline;
}

/* ---------- Controls bar ---------- */
.controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px 2px;
  font-family: system-ui, sans-serif;
}
.controls-left, .controls-right { display: flex; align-items: center; gap: 18px; }
.filter-btn {
  display: inline-flex; align-items: center; gap: 8px;
  font: inherit; font-size: 14px; cursor: pointer;
  padding: 8px 15px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
.filter-btn:hover { border-color: var(--accent); }
.filter-btn .fb-icon { font-size: 11px; transition: transform 0.18s; display: inline-block; }
.filter-btn[aria-expanded="true"] { border-color: var(--accent); background: var(--accent-soft); }
.filter-btn[aria-expanded="true"] .fb-icon { transform: rotate(180deg); }
.fb-count {
  min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
}
.control-group { display: flex; align-items: center; gap: 8px; }
.pill-btn {
  display: inline-flex; align-items: center; gap: 6px;
  font: inherit; font-size: 14px; cursor: pointer;
  padding: 8px 15px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
}
.pill-btn:hover { border-color: var(--accent); }
.pill-btn.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
.pill-btn.accent:hover { filter: brightness(1.07); }
.control-group > label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft);
}
#sortField {
  font: inherit; font-size: 13px; padding: 6px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink); cursor: pointer;
}
#sortField:focus { outline: none; border-color: var(--accent); }
.density { display: inline-flex; gap: 4px; }
.icon-btn {
  font: inherit; cursor: pointer; border: 1px solid var(--line); background: var(--surface);
  color: var(--ink-soft); border-radius: 7px; width: 32px; height: 32px; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center; font-size: 13px;
}
.icon-btn:hover { border-color: var(--accent); color: var(--ink); }
.icon-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.density .icon-btn[data-size="s"] { font-size: 9px; }
.density .icon-btn[data-size="l"] { font-size: 16px; }
.control-spacer { flex: 1; }
.check-toggle {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--ink-soft); cursor: pointer; user-select: none;
}
.check-toggle input { accent-color: var(--accent); cursor: pointer; }

/* ---------- Grid ---------- */
main { padding: 20px 24px 60px; }
.grid {
  --cover-min: 150px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--cover-min), 1fr));
  gap: 26px 20px;
}
.card { cursor: pointer; text-align: left; }
.cover-frame {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface-2);
  box-shadow: var(--shadow);
  transition: transform 0.16s ease, box-shadow 0.16s ease;
}
.card:hover .cover-frame { transform: translateY(-4px); box-shadow: 0 12px 30px rgba(40, 32, 20, 0.22); }
/* contain (not cover) so wide/landscape covers show fully, centered, uncropped */
.cover-frame img { width: 100%; height: 100%; object-fit: contain; display: block; }
.cover-fallback {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  padding: 14px; text-align: center;
  font-size: 14px; color: var(--ink-soft);
}
.lent-badge {
  position: absolute; left: 6px; bottom: 6px;
  font-family: system-ui, sans-serif; font-size: 10px; font-weight: 600;
  background: rgba(20, 16, 10, 0.78); color: #fff;
  padding: 3px 7px; border-radius: 999px; max-width: calc(100% - 12px);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.lent-line { margin: 8px 0 0; font-size: 13px; font-family: system-ui, sans-serif; color: var(--ink-soft); }
.grid.covers-only .card h3,
.grid.covers-only .card .card-author { display: none; }
.card h3 { margin: 10px 2px 2px; font-size: 14px; font-weight: 600; line-height: 1.25; }
.card .card-author { margin: 0 2px; font-size: 12.5px; color: var(--ink-soft); font-family: system-ui, sans-serif; }
.empty { text-align: center; color: var(--ink-soft); padding: 60px 0; font-size: 16px; }

/* ---------- Drawer ---------- */
.overlay {
  position: fixed; inset: 0; z-index: 40;
  background: rgba(20, 16, 10, 0.5);
  animation: fade 0.18s ease;
}
@keyframes fade { from { opacity: 0; } }
.drawer {
  position: fixed; top: 50%; left: 50%; z-index: 50;
  transform: translate(-50%, -50%);
  width: min(720px, 94vw); max-height: 92vh;
  background: var(--surface);
  border-radius: 16px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.4);
  overflow-y: auto;
  animation: pop 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes pop { from { opacity: 0; transform: translate(-50%, -48%) scale(0.97); } }
.drawer-hero {
  display: flex; gap: 20px; padding: 26px 26px 20px;
  background: linear-gradient(160deg, var(--surface-2), var(--surface));
  border-bottom: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
}
.drawer-hero img, .drawer-hero .cover-fallback {
  width: 130px; aspect-ratio: 2/3; object-fit: contain;
  border-radius: 6px; box-shadow: var(--shadow); flex-shrink: 0;
  background: var(--surface-2);
}
.drawer-hero .h-meta { min-width: 0; }
.drawer-hero h2 { margin: 0 0 4px; font-size: 22px; line-height: 1.2; }
.drawer-hero .orig-title { margin: 0 0 8px; font-size: 14px; color: var(--ink-soft); font-style: italic; }
.drawer-hero .authors { margin: 0 0 10px; font-size: 15px; font-family: system-ui, sans-serif; }
.stars { color: var(--star); font-size: 15px; letter-spacing: 1px; }
.rating-line { font-family: system-ui, sans-serif; font-size: 13px; color: var(--ink-soft); }
.close-x {
  position: absolute; top: 16px; right: 18px; z-index: 2;
  width: 34px; height: 34px; border-radius: 50%;
  border: none; background: rgba(0,0,0,0.08); color: var(--ink);
  font-size: 18px; cursor: pointer; line-height: 1;
}
.close-x:hover { background: rgba(0,0,0,0.18); }

.drawer-body { padding: 22px 26px 40px; }
.tag-row { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 20px; }
.tag {
  font-family: system-ui, sans-serif; font-size: 12px;
  padding: 3px 10px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent);
  cursor: pointer;
}
.section-title {
  font-family: system-ui, sans-serif; font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.09em; color: var(--ink-soft);
  margin: 26px 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--line);
}
.description { font-size: 15px; line-height: 1.65; }
.description p { margin: 0 0 0.9em; }
.description p:last-child { margin-bottom: 0; }
.description strong, .description b { font-weight: 700; }
.description em, .description i { font-style: italic; }
.description ul, .description ol { margin: 0 0 0.9em; padding-left: 1.4em; }
.description li { margin: 0.2em 0; }
.specs { display: grid; grid-template-columns: auto 1fr; gap: 8px 18px; font-family: system-ui, sans-serif; font-size: 13.5px; }
.specs dt { color: var(--ink-soft); }
.specs dd { margin: 0; }
.bsr { font-family: system-ui, sans-serif; font-size: 13px; line-height: 1.7; color: var(--ink-soft); white-space: pre-line; }
.price-paid { color: var(--accent); font-weight: 600; }

.related-row { display: flex; gap: 14px; overflow-x: auto; padding-bottom: 6px; }
.related-card { flex: 0 0 84px; cursor: pointer; }
.related-card img, .related-card .cover-fallback {
  width: 84px; aspect-ratio: 2/3; object-fit: contain; border-radius: 4px;
  box-shadow: var(--shadow); background: var(--surface-2);
}
.related-card p { margin: 6px 0 0; font-size: 11px; line-height: 1.2; color: var(--ink-soft); font-family: system-ui, sans-serif; }
.amazon-link {
  display: inline-block; margin-top: 24px; font-family: system-ui, sans-serif;
  font-size: 13px; color: var(--accent); text-decoration: none;
}
.amazon-link:hover { text-decoration: underline; }

@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; }
  .search-wrap { order: 3; width: 100%; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 20px 14px; }
}
