:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --line: #2a2f3a;
  --text: #e6e9ef;
  --muted: #9aa3b2;
  --accent: #6ea8fe;
  --accent-2: #8b7cf6;
  --green: #3ecf8e;
  --amber: #f5b14c;
  --red: #ff6b6b;
  --radius: 12px;
  --shadow: 0 6px 24px rgba(0,0,0,.35);
}

/* Light theme — overrides only the palette vars; everything else inherits. */
:root[data-theme="light"] {
  --bg: #f4f6fb;
  --panel: #ffffff;
  --panel-2: #eef1f7;
  --line: #d8dee9;
  --text: #1b2330;
  --muted: #5b6678;
  --accent: #1f6feb;
  --accent-2: #3a7bd5;
  --green: #1a8a4a;
  --amber: #b9791a;
  --red: #c0392b;
  --shadow: 0 6px 24px rgba(20,30,50,.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 18px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 10;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand .logo { color: var(--accent); font-size: 22px; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: .3px; }
.badge {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 2px 10px;
  font-size: 12px;
}
.search-wrap { flex: 1; }
#search {
  width: 100%;
  padding: 10px 14px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  outline: none;
}
#search:focus { border-color: var(--accent); }
.icon-btn {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; height: 34px; min-width: 34px; padding: 0 8px;
  font-size: 16px; cursor: pointer; line-height: 1;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.view-controls { display: flex; gap: 8px; }
.view-controls select {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  outline: none;
}

/* Layout */
.layout { display: flex; flex: 1; min-height: 0; }
.sidebar {
  width: 250px;
  flex-shrink: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 12px 0;
}
.sidebar-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 4px 16px 8px;
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .8px;
}
.sidebar-head button {
  background: none; border: 1px solid var(--line); color: var(--muted);
  width: 22px; height: 22px; border-radius: 6px; cursor: pointer;
}
#cat-nav { display: flex; flex-direction: column; gap: 2px; padding: 0 8px; }
.cat-section > .cat-row {
  cursor: pointer;
  border-radius: 8px;
  padding: 7px 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  font-size: 13px;
  gap: 6px;
}
.cat-section > .cat-row .chev {
  flex: none;
  width: 16px;
  text-align: center;
  transition: transform .12s ease;
  opacity: .8;
}
.cat-section > .cat-row.collapsed .chev { transform: rotate(-90deg); }
.cat-section > .cat-row .sec-label { flex: 1; }
.cat-section > .cat-row:hover { background: var(--panel-2); }
.cat-row .count { color: var(--muted); font-weight: 500; font-size: 12px; }
.cat-children { margin: 2px 0 6px 12px; display: flex; flex-direction: column; gap: 1px; }
.cat-children.collapsed { display: none; }
.cat-child {
  cursor: pointer;
  padding: 5px 10px;
  border-radius: 8px;
  font-size: 12.5px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}
.cat-child:hover { background: var(--panel-2); color: var(--text); }
.cat-child.active { background: var(--panel-2); color: var(--accent); }
.cat-row.active-row { color: var(--accent); }

/* Content */
.content { flex: 1; overflow-y: auto; padding: 18px 22px; }
.result-meta { color: var(--muted); font-size: 13px; margin-bottom: 14px; }
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 16px;
}
.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  transition: transform .12s ease, border-color .12s ease, opacity .18s ease;
}
.card:hover { transform: translateY(-3px); border-color: var(--accent); }
.card.hidden { display: none; }

/* Top status header bar — sits ABOVE the cover. Positioned so the status
   dropdown (.status-menu, top:100%) anchors to the bar, not the whole card
   (otherwise overflow:hidden on .card clips it). */
.card-top {
  position: relative;
  display: flex;
  align-items: center;
  padding: 7px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel-2);
}
.card-top.status-wishlist { background: rgba(110,168,254,.16); border-bottom-color: rgba(110,168,254,.5); }
.card-top.status-reading  { background: rgba(245,177,76,.14); border-bottom-color: rgba(245,177,76,.5); }
.card-top.status-read     { background: rgba(76,175,80,.14); border-bottom-color: rgba(76,175,80,.5); }
/* Status control in the card-top bar. Custom (no native <select>) so it
   renders identicaly in every browser and matches the clean reference. */
.status-pick {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(20,24,32,.55);
  color: var(--text);
  padding: 3px 10px;
  cursor: pointer;
  position: relative;
}
.status-pick:hover { border-color: var(--accent); }
.status-pick .dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--muted); flex: none;
}
.status-pick .label { flex: 1; text-align: left; }
.status-pick .caret { font-size: 9px; opacity: .7; }
/* color by state */
.status-pick[data-state="wishlist"] { color: var(--accent-2); border-color: rgba(110,168,254,.5); }
.status-pick[data-state="wishlist"] .dot { background: var(--accent-2); }
.status-pick[data-state="reading"]  { color: var(--amber); border-color: rgba(245,177,76,.5); }
.status-pick[data-state="reading"]  .dot { background: var(--amber); }
.status-pick[data-state="read"]     { color: var(--green); border-color: rgba(76,175,80,.5); }
.status-pick[data-state="read"]     .dot { background: var(--green); }

/* Dropdown menu */
.status-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 10px; right: 10px;
  z-index: 10;
  display: flex; flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 4px;
}
.status-menu[hidden] { display: none; }
.status-opt {
  text-align: left;
  background: none; border: none;
  color: var(--text);
  font-size: 12px; font-weight: 500;
  padding: 6px 8px; border-radius: 6px; cursor: pointer;
}
.status-opt:hover { background: var(--panel-2); }
.status-opt.current { color: var(--accent); font-weight: 700; }
.card-body {
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  flex: 1;
}
.card-body .progress {
  display: flex; align-items: center; gap: 8px;
  height: 14px; font-size: 11px; color: var(--muted);
}
.card-body .progress .bar {
  flex: 1; height: 5px; border-radius: 3px;
  background: var(--line); overflow: hidden;
}
.card-body .progress.has .bar { background: rgba(110,168,254,.18); }
.card-body .progress.has .bar > .fill,
.card-body .progress .bar { position: relative; }
.card-body .progress .bar::after {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0;
  width: var(--w, 0%); background: var(--accent); border-radius: 3px;
  transition: width .2s ease;
}
.card-body .progress.has .pct { color: var(--accent); font-weight: 600; }
.cover-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 3 / 4;
  overflow: hidden;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.cover-fallback {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: 12px;
  text-align: center;
  background: linear-gradient(135deg, #1c2230, #232a3a);
}
/* Hide the fallback when a real cover is present and loaded. */
.cover-wrap:not(.no-cover) .cover-fallback { display: none; }
.cover-fallback span {
  font-size: 13px;
  line-height: 1.3;
  color: var(--text);
  font-weight: 600;
  word-break: break-word;
}
.card h3 { margin: 0; font-size: 14.5px; line-height: 1.3; }
.card .authors { color: var(--muted); font-size: 12.5px; min-height: 16px; }
.card .meta { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.tag {
  font-size: 11px;
  border: 1px solid var(--line);
  color: var(--muted);
  border-radius: 999px;
  padding: 2px 8px;
}
.tag.edition { color: var(--amber); border-color: rgba(245,177,76,.4); }
.tag.cat { color: var(--accent); border-color: rgba(110,168,254,.35); }
.card .actions { display: flex; gap: 8px; margin-top: auto; padding-top: 8px; }
.btn {
  flex: 1;
  text-align: center;
  text-decoration: none;
  font-size: 12.5px;
  padding: 7px 8px;
  border-radius: 8px;
  border: 1px solid var(--line);
  color: var(--text);
  cursor: pointer;
  background: var(--panel-2);
}
.btn.open { border-color: rgba(110,168,254,.5); color: var(--accent); }
.btn.open:hover { background: rgba(110,168,254,.12); }
.empty { text-align: center; color: var(--muted); padding: 60px 0; }
.footer {
  padding: 10px 18px; border-top: 1px solid var(--line);
  color: var(--muted); font-size: 12px; background: var(--panel);
}
.hidden { display: none; }

/* Hide PDF.js internal hidden canvas that leaks into the page. */
.hiddenCanvasElement { display: none !important; }

/* Card fade-in animation on first build. */
@keyframes card-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}
.card.card-in { animation: card-in .22s ease both; }

/* Reader modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: none; align-items: center; justify-content: center; z-index: 50;
}
.modal-backdrop.open { display: flex; }
.modal {
  width: min(96vw, 1100px); height: min(92dvh, 92vh);
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden; display: flex; flex-direction: column;
  box-shadow: var(--shadow);
}
.modal header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; border-bottom: 1px solid var(--line);
}
.modal header h2 { font-size: 14px; margin: 0; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal header .m-head { flex: 1; min-width: 0; }
.modal header .m-sub { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.modal header .close { cursor: pointer; border: none; background: none; color: var(--muted); font-size: 20px; padding: 4px 8px; flex-shrink: 0; }
.modal iframe { flex: 1; border: none; background: #fff; min-height: 0; }
.modal .status-bar {
  display: flex; gap: 8px; padding: 8px 14px; border-top: 1px solid var(--line); align-items: center;
  flex-wrap: wrap;
}
.modal .status-bar .label { color: var(--muted); font-size: 12px; }
.modal .status-bar select {
  background: var(--bg); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 10px; font-size: 13px;
}
.modal .status-bar .raw-link {
  margin-left: auto; font-size: 13px; font-weight: 600; text-decoration: none;
  color: var(--accent); padding: 6px 10px; border: 1px solid var(--line); border-radius: 8px;
}
.modal .status-bar .raw-link:hover { background: var(--panel-2); border-color: var(--accent); }

@media (max-width: 720px) {
  .sidebar { width: 180px; }
  .view-controls select { display: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 10px; }
  .content { padding: 12px 14px; }
  .topbar { gap: 10px; padding: 10px 12px; }
  /* Modal: go edge-to-edge on mobile */
  .modal { width: 100vw; height: 100dvh; height: 100vh; border-radius: 0; border: none; }
  .modal header h2 { font-size: 13px; }
  .modal .status-bar { padding: 6px 10px; gap: 6px; }
  .modal .status-bar .label { display: none; }
  .modal .status-bar select { font-size: 12px; padding: 5px 8px; }
  .modal .status-bar .raw-link { font-size: 12px; padding: 5px 8px; margin-left: 0; }
}
@media (max-width: 480px) {
  .sidebar { display: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
  .brand h1 { font-size: 15px; }
  .modal .status-bar { flex-direction: column; align-items: stretch; }
  .modal .status-bar .raw-link { text-align: center; }
}
