:root {
  --bg: #1b1410;
  --bg2: #241a14;
  --ink: #f4ead9;
  --muted: #b9a790;
  --accent: #e0a458;
  --accent-ink: #2a1a08;
  --danger: #c4554d;
  --wood1: #6b4423;
  --wood2: #4a2e17;
  --wood3: #8a5a30;
  --radius: 10px;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at top, #2c2018, var(--bg) 70%) fixed;
  color: var(--ink);
  font: 15px/1.5 var(--sans);
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
input, textarea { -webkit-user-select: text; user-select: text; }
[hidden] { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }

/* Printing is disabled */
@media print {
  body * { display: none !important; }
  body::after { content: "Printing is disabled."; display: block; padding: 2rem; font: 20px sans-serif; color: #000; }
}
body.no-print .view { visibility: hidden; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid #5a4636; background: #33261d; color: var(--ink);
  padding: 9px 16px; border-radius: 999px; font: 600 14px var(--sans); cursor: pointer;
  transition: background .15s, transform .05s, opacity .15s; white-space: nowrap;
}
.btn:hover { background: #3f2f24; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { background: #ebb46c; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: #ffffff12; }
.btn.danger { background: transparent; border-color: var(--danger); color: #f1a29c; }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.buy { background: #2f6b45; border-color: #2f6b45; color: #eafff0; }
.btn.buy:hover { background: #387d52; }
.icon-btn {
  appearance: none; border: 0; background: transparent; color: var(--ink); width: 38px; height: 38px;
  border-radius: 50%; font-size: 20px; cursor: pointer; display: grid; place-items: center;
}
.icon-btn:hover { background: #ffffff18; }
.pill { background: #5a2f2b; color: #ffd4cf; font-size: 12px; padding: 3px 10px; border-radius: 999px; }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px; }
.auth-card {
  width: min(400px, 100%); background: #261c16e6; border: 1px solid #3d2e23; border-radius: 18px;
  padding: 28px; box-shadow: 0 30px 80px #0008;
}
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.brand img { width: 44px; height: 44px; border-radius: 10px; }
.brand h1 { font: 600 26px var(--serif); margin: 0; }
.brand.small img { width: 30px; height: 30px; border-radius: 7px; }
.brand.small span { font: 600 18px var(--serif); }
.tabs { display: flex; gap: 4px; background: #1a120d; border-radius: 999px; padding: 4px; margin-bottom: 18px; }
.tab { flex: 1; border: 0; background: transparent; color: var(--muted); padding: 8px; border-radius: 999px; font: 600 14px var(--sans); cursor: pointer; }
.tab.active { background: #3a2b20; color: var(--ink); }
form label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
form input, form textarea {
  display: block; width: 100%; margin-top: 5px; padding: 10px 12px; border-radius: 8px;
  border: 1px solid #4a392c; background: #17100c; color: var(--ink); font: 15px var(--sans);
}
form input:focus, form textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.form-error { color: #f1a29c; min-height: 1.4em; font-size: 13px; margin: 4px 0 10px; }
#auth-form .btn { width: 100%; padding: 12px; }

/* ---------- shelf ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5; display: flex; align-items: center; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 20px 10px; background: #1b1410e8; backdrop-filter: blur(10px);
  border-bottom: 1px solid #ffffff10;
}
#search {
  flex: 1; max-width: 420px; margin-left: auto; padding: 8px 14px; border-radius: 999px;
  border: 1px solid #4a392c; background: #120c09; color: var(--ink); font: 14px var(--sans);
}
.user-menu { position: relative; }
.popover {
  position: absolute; right: 0; top: calc(100% + 8px); background: #2a1f18; border: 1px solid #4a392c;
  border-radius: 12px; padding: 14px; min-width: 240px; display: grid; gap: 8px; box-shadow: 0 20px 50px #0009;
}

.bookcase {
  max-width: 1280px; margin: 24px auto 60px; padding: 0 24px;
}
.shelf-row {
  position: relative; display: flex; align-items: flex-end; gap: 30px; padding: 26px 24px 0;
  min-height: 250px; margin-bottom: 34px;
  background: linear-gradient(#00000055, #0000 30%), linear-gradient(90deg, #2a1c12, #34241a 50%, #2a1c12);
  border-radius: 6px 6px 0 0;
  box-shadow: inset 0 12px 20px -10px #000c;
}
.shelf-row::after { /* the plank */
  content: ""; position: absolute; left: -10px; right: -10px; bottom: -22px; height: 22px;
  background: linear-gradient(var(--wood3), var(--wood1) 35%, var(--wood2));
  border-radius: 3px; box-shadow: 0 14px 18px -6px #000c, inset 0 1px 0 #ffffff30;
}
.shelf-empty { width: 100%; text-align: center; color: var(--muted); align-self: center; padding-bottom: 20px; }
.shelf-empty-icon { font-size: 44px; }

.book {
  position: relative; width: 150px; aspect-ratio: 2 / 3; padding: 0; border: 0; background: none;
  cursor: pointer; transform-origin: bottom center; transform: rotate(var(--tilt, 0deg));
  transition: transform .25s cubic-bezier(.2, .8, .3, 1.2), filter .2s; filter: drop-shadow(0 8px 10px #000a);
}
.book:hover, .book:focus-visible { transform: translateY(-10px) rotate(0); filter: drop-shadow(0 18px 18px #000c); outline: none; }
.book.locked .cover { filter: grayscale(.7) brightness(.7); }
.cover {
  position: absolute; inset: 0; border-radius: 3px 6px 6px 3px; overflow: hidden; background: #333;
}
.cover::before { /* spine shading */
  content: ""; position: absolute; inset: 0 auto 0 0; width: 12px; z-index: 2;
  background: linear-gradient(90deg, #0007, #fff2 40%, #0003 70%, transparent);
}
.cover::after { /* gloss */
  content: ""; position: absolute; inset: 0; z-index: 2; pointer-events: none;
  background: linear-gradient(115deg, #ffffff1a, transparent 40%);
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; }
.cover.generated {
  background: linear-gradient(160deg, var(--c1), var(--c2)); display: flex; flex-direction: column;
  justify-content: center; align-items: center; padding: 16px 12px 16px 20px; text-align: center; color: #f6e9cf;
}
.g-title { font: 600 17px/1.2 var(--serif); display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden; }
.g-rule { width: 40%; height: 1px; background: #f6e9cf88; margin: 12px 0; }
.g-author { font: italic 13px var(--serif); opacity: .85; }
.badges { position: absolute; top: 6px; right: -6px; display: flex; flex-direction: column; gap: 4px; align-items: flex-end; z-index: 3; }
.badge { font: 700 10px var(--sans); padding: 2px 7px; border-radius: 4px; letter-spacing: .02em; box-shadow: 0 2px 4px #0006; }
.badge.ok { background: #2f6b45; color: #d9f7e3; }
.badge.preview { background: var(--accent); color: var(--accent-ink); }
.badge.locked { background: #222; }
.dl-bar { position: absolute; left: 8px; right: 8px; bottom: 8px; height: 5px; background: #0008; border-radius: 3px; z-index: 3; overflow: hidden; }
.dl-bar div { height: 100%; background: var(--accent); transition: width .2s; }

@media (max-width: 600px) {
  .topbar { flex-wrap: wrap; padding-left: 14px; padding-right: 14px; }
  #search { order: 10; max-width: none; flex-basis: 100%; }
  .bookcase { padding: 0 12px; }
  .shelf-row { gap: 18px; padding: 20px 14px 0; min-height: 190px; }
  .book { width: 112px; }
  .g-title { font-size: 14px; }
}

/* ---------- book dialog ---------- */
.book-dialog {
  border: 1px solid #4a392c; border-radius: 18px; padding: 0; background: #241a14; color: var(--ink);
  width: min(720px, calc(100% - 24px)); box-shadow: 0 40px 100px #000c;
}
.book-dialog::backdrop { background: #0009; backdrop-filter: blur(3px); }
.bd-inner { display: flex; gap: 24px; padding: 24px; position: relative; }
.bd-cover { position: relative; flex: 0 0 180px; aspect-ratio: 2 / 3; filter: drop-shadow(0 12px 16px #000a); }
.bd-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.bd-body h2 { font: 600 24px/1.2 var(--serif); margin: 0 30px 4px 0; }
.bd-meta { color: var(--muted); font-size: 12px; margin-top: 10px; }
.bd-body p { white-space: pre-line; max-height: 220px; overflow: auto; color: #e3d6c2; }
.bd-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: auto; padding-top: 12px; }
.bd-close { position: absolute; top: 10px; right: 10px; }
.bd-progress { height: 6px; background: #0008; border-radius: 3px; overflow: hidden; margin-top: 10px; }
.bd-progress div { height: 100%; background: var(--accent); width: 0; transition: width .2s; }
@media (max-width: 560px) {
  .bd-inner { flex-direction: column; align-items: center; }
  .bd-cover { flex-basis: auto; width: 140px; }
  .bd-body { width: 100%; }
}

/* ---------- reader ---------- */
.reader { position: fixed; inset: 0; display: flex; flex-direction: column; background: #121212; transition: background .25s; }
.reader-bar, .reader-foot {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: #1a1a1a; z-index: 6;
}
.reader-bar { padding-top: calc(8px + env(safe-area-inset-top)); border-bottom: 1px solid #ffffff10; }
.reader-foot { padding-bottom: calc(8px + env(safe-area-inset-bottom)); border-top: 1px solid #ffffff10; }
.r-title { flex: 1; font: 600 16px var(--serif); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.r-tools { display: flex; gap: 2px; align-items: center; }
.icon-btn.active { background: #e0a4582e; color: var(--accent); box-shadow: inset 0 0 0 1px #e0a45866; }
#r-bookmark-btn { font-size: 17px; filter: grayscale(1) opacity(0.65); transition: transform .15s, filter .15s, background .15s; }
#r-bookmark-btn.active { filter: none; background: #e0a4582b; transform: scale(1.06); }

/* Reader body & collapsible drawer */
.r-body { position: relative; flex: 1; min-height: 0; display: flex; overflow: hidden; }
.r-drawer {
  width: 310px; max-width: 86vw; background: #1c1612; border-right: 1px solid #ffffff14;
  display: flex; flex-direction: column; z-index: 5; flex-shrink: 0;
}
.r-drawer-head {
  display: flex; align-items: center; gap: 6px; padding: 8px 10px; border-bottom: 1px solid #ffffff12; background: #16110e;
}
.r-drawer-tabs {
  flex: 1; display: flex; gap: 4px; background: #0f0b09; padding: 3px; border-radius: 999px;
}
.r-dtab {
  flex: 1; appearance: none; border: 0; background: transparent; color: var(--muted);
  padding: 6px 8px; border-radius: 999px; font: 600 12px var(--sans); cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 5px;
}
.r-dtab.active { background: #36281e; color: var(--ink); }
.r-bm-badge {
  background: var(--accent); color: var(--accent-ink); border-radius: 999px;
  padding: 0 6px; font-size: 11px; line-height: 16px;
}
.r-drawer-pane { flex: 1; overflow-y: auto; padding: 10px; }
.r-drawer-empty { color: var(--muted); font-size: 13px; text-align: center; padding: 32px 14px; }

/* TOC list */
.r-toc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.r-toc-item {
  width: 100%; text-align: left; appearance: none; border: 0; background: transparent; color: var(--ink);
  padding: 8px 10px; border-radius: 8px; font: 13px/1.35 var(--sans); cursor: pointer;
  display: flex; justify-content: space-between; align-items: baseline; gap: 8px;
}
.r-toc-item:hover { background: #ffffff10; }
.r-toc-item.current { background: #e0a45824; color: var(--accent); font-weight: 600; }
.r-toc-page { color: var(--muted); font-size: 11px; flex-shrink: 0; }

/* Thumbnails grid */
.r-thumbs-grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; align-content: start;
}
.r-thumb-card {
  appearance: none; border: 2px solid transparent; background: #261d17; color: var(--ink);
  border-radius: 8px; padding: 6px; cursor: pointer; display: flex; flex-direction: column;
  align-items: center; gap: 6px; transition: border-color .15s, transform .1s;
}
.r-thumb-card:hover { border-color: #ffffff35; transform: translateY(-2px); }
.r-thumb-card.current { border-color: var(--accent); background: #36281e; }
.r-thumb-box {
  width: 100%; aspect-ratio: 1 / 1.414; background: #fff; border-radius: 4px; overflow: hidden;
  display: grid; place-items: center; position: relative; box-shadow: 0 4px 10px #0008;
}
.r-thumb-box canvas { width: 100%; height: 100%; object-fit: contain; display: block; }
.r-thumb-num { font: 600 11px var(--sans); color: var(--muted); }
.r-thumb-card.current .r-thumb-num { color: var(--accent); }

/* Bookmarks list */
.r-bm-actions { margin-bottom: 10px; }
.r-bm-actions .btn { width: 100%; }
.r-bm-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.r-bm-row {
  display: flex; align-items: center; gap: 6px; background: #261d17; border: 1px solid #3c2e24;
  border-radius: 8px; padding: 6px 8px;
}
.r-bm-row.current { border-color: var(--accent); }
.r-bm-go {
  flex: 1; text-align: left; appearance: none; border: 0; background: transparent; color: var(--ink);
  font: 13px/1.35 var(--sans); cursor: pointer; padding: 4px;
}
.r-bm-go strong { display: block; color: var(--accent); font-size: 12px; }
.r-bm-del {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  width: 28px; height: 28px; border-radius: 50%; cursor: pointer; font-size: 16px;
}
.r-bm-del:hover { background: #ffffff14; color: #f1a29c; }

.r-drawer-backdrop { position: absolute; inset: 0; background: #0008; z-index: 4; }
@media (max-width: 780px) {
  .r-drawer { position: absolute; left: 0; top: 0; bottom: 0; box-shadow: 16px 0 40px #000c; }
}

.r-stage { position: relative; flex: 1; overflow: auto; display: flex; align-items: center; justify-content: center; touch-action: pan-x pan-y; }
.r-pages { margin: auto; display: flex; align-items: center; justify-content: center; padding: 16px; position: relative; will-change: transform; }
.r-pages.flip-active, .r-pages.flat-active { touch-action: none; }
.r-flipbook { position: relative; filter: drop-shadow(0 18px 40px #000c); }
.r-page-sheet { background: #fdfbf7; overflow: hidden; user-select: none; box-shadow: inset 0 0 0 1px #00000014; }
.r-page-sheet.--left { box-shadow: inset -14px 0 22px -12px #00000038, inset 0 0 0 1px #00000014; }
.r-page-sheet.--right { box-shadow: inset 14px 0 22px -12px #00000038, inset 0 0 0 1px #00000014; }
.r-page-sheet[data-density="hard"] { background: #f7f2e8; }
.r-sheet-placeholder { position: absolute; inset: 0; display: grid; place-items: center; color: #998d7d; font: 600 13px var(--sans); }
.r-page { display: block; width: 100%; height: 100%; background: #fff; pointer-events: none; transition: filter .25s; }
.r-pages.zoomed .r-page, .r-pages.flat-active .r-page { width: auto; height: auto; box-shadow: 0 10px 40px #000c; }
.r-pages.zoomed .r-page + .r-page, .r-pages.flat-active .r-page + .r-page { box-shadow: 0 10px 40px #000c, inset 12px 0 18px -12px #0006; }
.reader.pseudo-fs .reader-foot { display: none; }

/* Reading Themes: Normal / Sepia / Night */
.reader[data-theme="sepia"] { background: #261e16; }
.reader[data-theme="sepia"] .r-page,
.reader[data-theme="sepia"] .r-thumb-box canvas {
  filter: sepia(0.38) contrast(0.95) brightness(0.96);
}
.reader[data-theme="sepia"] .r-page-sheet { background: #f4ead5; }
.reader[data-theme="night"] { background: #08090c; }
.reader[data-theme="night"] .r-page,
.reader[data-theme="night"] .r-thumb-box canvas {
  filter: invert(0.9) hue-rotate(180deg) contrast(0.96);
}
.reader[data-theme="night"] .r-page-sheet { background: #181a1f; }
.reader[data-theme="night"] .r-sheet-placeholder { color: #7a8291; }

.r-nav { position: fixed; top: 120px; bottom: 80px; width: 9%; max-width: 90px; border: 0; background: transparent; cursor: pointer; z-index: 2; }
.r-nav.prev { left: 0; cursor: w-resize; }
.r-nav.next { right: 0; cursor: e-resize; }
.r-pages.flip-active ~ .r-nav, .r-pages.zoomed ~ .r-nav { display: none; }
#r-slider { flex: 1; accent-color: var(--accent); }
.r-loading, .r-preview-end {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: #121212ee; z-index: 4; text-align: center; padding: 24px;
}
.r-preview-end h2 { font: 600 26px var(--serif); margin: 0; }
.r-preview-end p { max-width: 420px; color: var(--muted); }
.spinner { width: 36px; height: 36px; border: 3px solid #ffffff22; border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: #33261d; border: 1px solid #5a4636; padding: 10px 18px; border-radius: 999px; z-index: 100;
  box-shadow: 0 10px 30px #0009; max-width: calc(100% - 32px); text-align: center;
}

.app-version {
  text-align: center; padding: 0 16px calc(18px + env(safe-area-inset-bottom));
  font-size: 11px; color: var(--muted); opacity: .55; letter-spacing: .03em;
}
