/* ═══════════════════════════════════════════════════════════════
   The Shelf — warm-paper reading room
   Type scale 1.25 · base 16px · measure ≈ 66ch
   ═══════════════════════════════════════════════════════════════ */

:root {
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, Charter, Georgia, serif;
  --font-sans: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Inter, Roboto, sans-serif;

  --paper: #f5f0e6;
  --paper-raised: #fcf9f2;
  --paper-sunken: #ece5d6;
  --ink: #221d15;
  --ink-soft: #57503f;
  --ink-faint: #8a8171;
  --hairline: rgba(34, 29, 21, 0.14);
  --accent: #9c4a1f;          /* burnt sienna */
  --accent-soft: rgba(156, 74, 31, 0.10);
  --shadow-book: 0 1px 2px rgba(30, 22, 10, 0.18), 0 8px 24px -8px rgba(30, 22, 10, 0.28);
  --shadow-book-hover: 0 2px 4px rgba(30, 22, 10, 0.2), 0 18px 40px -10px rgba(30, 22, 10, 0.38);
  --radius: 6px;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
  color-scheme: light;
}

[data-theme="dark"] {
  --paper: #16130e;
  --paper-raised: #1f1b14;
  --paper-sunken: #100e0a;
  --ink: #e9e2d2;
  --ink-soft: #b3aa95;
  --ink-faint: #7d7666;
  --hairline: rgba(233, 226, 210, 0.13);
  --accent: #d07a45;
  --accent-soft: rgba(208, 122, 69, 0.14);
  --shadow-book: 0 1px 2px rgba(0, 0, 0, 0.5), 0 10px 26px -8px rgba(0, 0, 0, 0.6);
  --shadow-book-hover: 0 2px 6px rgba(0, 0, 0, 0.55), 0 20px 44px -10px rgba(0, 0, 0, 0.7);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  transition: background 0.45s var(--ease), color 0.45s var(--ease);
}

::selection { background: var(--accent-soft); }

/* ── View switching ─────────────────────────────────────────── */

.view {
  display: none;
  height: 100%;
}
.view.is-active { display: flex; flex-direction: column; }

.view.is-entering { animation: view-in 0.45s var(--ease) both; }
@keyframes view-in {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}

/* ═══ SHELF ═══════════════════════════════════════════════════ */

.shelf-header { padding-top: clamp(2.5rem, 6vh, 4.5rem); }

.shelf-header__inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 clamp(1.25rem, 4vw, 3rem) 1.75rem;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 1.5rem;
  flex-wrap: wrap;
}

.wordmark { display: flex; align-items: baseline; gap: 1rem; }
.wordmark__rule {
  align-self: center;
  width: 34px; height: 2px;
  background: var(--accent);
}
.wordmark__title {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: clamp(1.9rem, 4vw, 2.6rem);
  letter-spacing: -0.015em;
}
.wordmark__sub {
  font-size: 0.8rem;
  color: var(--ink-faint);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.shelf-tools { display: flex; align-items: center; gap: 0.6rem; }

.search {
  display: flex; align-items: center; gap: 0.5rem;
  background: var(--paper-raised);
  border: 1px solid var(--hairline);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  transition: border-color 0.2s, box-shadow 0.2s;
}
.search:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.search__icon { color: var(--ink-faint); flex: none; }
.search input {
  border: 0; outline: 0; background: transparent;
  font: inherit; font-size: 0.9rem; color: var(--ink);
  width: 13rem;
}
.search input::placeholder { color: var(--ink-faint); }

.icon-btn {
  display: grid; place-items: center;
  width: 36px; height: 36px;
  border-radius: 50%;
  border: 1px solid var(--hairline);
  background: var(--paper-raised);
  color: var(--ink-soft);
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, transform 0.3s var(--ease);
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); transform: rotate(15deg); }
.icon-moon { display: none; }
[data-theme="dark"] .icon-sun { display: none; }
[data-theme="dark"] .icon-moon { display: block; }

.shelf-header__hairline {
  height: 1px;
  max-width: 1200px;
  margin: 0 auto;
  background: var(--hairline);
  margin-left: clamp(1.25rem, 4vw, 3rem);
  margin-right: clamp(1.25rem, 4vw, 3rem);
}

.shelf-main {
  flex: 1;
  overflow-y: auto;
  padding: clamp(1.75rem, 4vw, 3rem) clamp(1.25rem, 4vw, 3rem) 5rem;
}

.grid {
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: clamp(1.5rem, 3vw, 2.5rem) clamp(1.25rem, 2.5vw, 2rem);
}

/* ── Book card ─────────────────────────────────────────────── */

.book {
  position: relative;
  cursor: pointer;
  animation: card-in 0.55s var(--ease) both;
}
@keyframes card-in {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: none; }
}

.book__cover {
  position: relative;
  aspect-ratio: 2 / 3;
  border-radius: 3px 6px 6px 3px;
  overflow: hidden;
  box-shadow: var(--shadow-book);
  transition: transform 0.4s var(--ease), box-shadow 0.4s var(--ease);
}
.book:hover .book__cover,
.book:focus-visible .book__cover {
  transform: translateY(-6px) rotate(-0.6deg);
  box-shadow: var(--shadow-book-hover);
}
/* spine highlight */
.book__cover::after {
  content: "";
  position: absolute; inset: 0;
  background: linear-gradient(90deg,
    rgba(0,0,0,0.16) 0, rgba(255,255,255,0.10) 4%,
    rgba(0,0,0,0.05) 6%, transparent 10%);
  pointer-events: none;
}
.book__cover img {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
}

/* generated placeholder cover */
.book__placeholder {
  width: 100%; height: 100%;
  display: flex; flex-direction: column;
  justify-content: space-between;
  padding: 0.9rem 0.8rem;
  color: rgba(255, 251, 240, 0.94);
}
.book__placeholder-initial {
  font-family: var(--font-serif);
  font-size: 2.6rem;
  line-height: 1;
  border-bottom: 1px solid rgba(255,251,240,0.35);
  padding-bottom: 0.55rem;
}
.book__placeholder-title {
  font-family: var(--font-serif);
  font-size: 0.82rem;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.book__badge {
  position: absolute;
  top: 0; right: 10px;
  z-index: 2;
  background: var(--accent);
  color: #fbf4e8;
  font-size: 0.58rem;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 0.45rem 0.4rem 0.35rem;
  clip-path: polygon(0 0, 100% 0, 100% 100%, 50% calc(100% - 7px), 0 100%);
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
}

.book__info { padding-top: 0.8rem; }
.book__title {
  font-family: var(--font-serif);
  font-size: 0.98rem;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book__meta {
  margin-top: 0.3rem;
  font-size: 0.72rem;
  color: var(--ink-faint);
  letter-spacing: 0.02em;
}

.empty-note {
  max-width: 1200px;
  margin: 4rem auto 0;
  text-align: center;
  color: var(--ink-faint);
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1.05rem;
}

/* ═══ READER ═════════════════════════════════════════════════ */

#view-reader.is-active { flex-direction: row; }

.progress-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 3px;
  z-index: 30;
  background: transparent;
}
.progress-bar span {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s linear;
}

/* ── Sidebar ───────────────────────────────────────────────── */

.sidebar {
  width: 320px;
  flex: none;
  border-right: 1px solid var(--hairline);
  background: var(--paper-raised);
  height: 100vh;
  position: sticky;
  top: 0;
}
.sidebar__scroll {
  height: 100%;
  overflow-y: auto;
  padding: 1.5rem 1.5rem 3rem;
}

.back-btn {
  display: inline-flex; align-items: center; gap: 0.45rem;
  border: 0; background: none; cursor: pointer;
  font: inherit; font-size: 0.78rem;
  letter-spacing: 0.1em; text-transform: uppercase;
  color: var(--ink-faint);
  padding: 0.25rem 0;
  transition: color 0.2s;
}
.back-btn:hover { color: var(--accent); }

.sidebar__book { margin-top: 1.75rem; }
.sidebar__cover {
  width: 84px;
  aspect-ratio: 2/3;
  border-radius: 2px 4px 4px 2px;
  overflow: hidden;
  box-shadow: var(--shadow-book);
  margin-bottom: 1rem;
  position: relative;
}
.sidebar__cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.sidebar__cover .book__placeholder { padding: 0.5rem 0.45rem; }
.sidebar__cover .book__placeholder-initial { font-size: 1.3rem; padding-bottom: 0.3rem; }
.sidebar__cover .book__placeholder-title { font-size: 0.5rem; -webkit-line-clamp: 3; }

.sidebar__title {
  margin: 0;
  font-family: var(--font-serif);
  font-weight: 400;
  font-size: 1.3rem;
  line-height: 1.25;
  letter-spacing: -0.01em;
}
.sidebar__meta {
  margin: 0.5rem 0 0;
  font-size: 0.74rem;
  color: var(--ink-faint);
}

.sidebar__progress {
  display: flex; align-items: center; gap: 0.7rem;
  margin-top: 1.1rem;
}
.sidebar__progress-track {
  flex: 1; height: 2px;
  background: var(--hairline);
  border-radius: 2px;
  overflow: hidden;
}
.sidebar__progress-track span {
  display: block; height: 100%; width: 0%;
  background: var(--accent);
  transition: width 0.25s var(--ease);
}
.sidebar__progress-label {
  font-size: 0.72rem;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}

/* ── TOC ───────────────────────────────────────────────────── */

.toc { margin-top: 2.2rem; }
.toc__heading {
  margin: 0 0 0.6rem;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.toc__list { list-style: none; margin: 0; padding: 0; }

.toc__item {
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  width: 100%;
  text-align: left;
  border: 0; background: none; cursor: pointer;
  font: inherit;
  padding: 0.5rem 0.6rem;
  margin: 0 -0.6rem;
  border-radius: var(--radius);
  color: var(--ink-soft);
  transition: background 0.18s, color 0.18s;
}
.toc__item:hover { background: var(--accent-soft); color: var(--ink); }
.toc__item.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}
.toc__num {
  flex: none;
  width: 1.6em;
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  color: var(--ink-faint);
  text-align: right;
}
.toc__item.is-active .toc__num { color: var(--accent); }
.toc__label {
  flex: 1;
  font-size: 0.86rem;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* ── Chapter column ────────────────────────────────────────── */

.reader-main {
  flex: 1;
  height: 100vh;
  overflow-y: auto;
  scroll-behavior: smooth;
}

.chapter {
  max-width: calc(66ch + 4rem);
  margin: 0 auto;
  padding: clamp(3rem, 8vh, 5.5rem) 2rem 2rem;
}
.chapter.is-turning { animation: chapter-in 0.4s var(--ease) both; }
@keyframes chapter-in {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: none; }
}

.chapter__kicker {
  margin: 0 0 2.2rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
  display: flex;
  align-items: center;
  gap: 0.8rem;
}
.chapter__kicker::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--hairline);
}

/* ── Prose ─────────────────────────────────────────────────── */

.prose {
  font-family: var(--font-serif);
  font-size: 1.075rem;
  line-height: 1.75;
  color: var(--ink);
}

.prose h1, .prose h2, .prose h3, .prose h4 {
  font-family: var(--font-serif);
  font-weight: 600;
  letter-spacing: -0.01em;
  line-height: 1.3;
}
.prose h2 { font-size: 1.75rem; margin: 0 0 1.4rem; }
.prose h3 { font-size: 1.3rem;  margin: 2.6rem 0 0.9rem; }
.prose h4 { font-size: 1.05rem; margin: 2.1rem 0 0.7rem; color: var(--ink-soft); }

.prose p { margin: 0 0 1.25rem; }
.prose strong { font-weight: 700; }
.prose a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }

.prose blockquote {
  margin: 1.8rem 0;
  padding: 0.9rem 0 0.9rem 1.4rem;
  border-left: 2px solid var(--accent);
  background: linear-gradient(90deg, var(--accent-soft), transparent 70%);
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--ink-soft);
  font-size: 0.98em;
}
.prose blockquote p:last-child { margin-bottom: 0; }

.prose ul, .prose ol { margin: 0 0 1.25rem; padding-left: 1.4rem; }
.prose li { margin-bottom: 0.45rem; }
.prose li::marker { color: var(--accent); }

.prose hr {
  border: 0;
  height: 1px;
  background: var(--hairline);
  margin: 2.6rem auto;
  max-width: 8rem;
}

.prose code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.86em;
  background: var(--paper-sunken);
  border-radius: 4px;
  padding: 0.1em 0.35em;
}

/* ── Pager ─────────────────────────────────────────────────── */

.chapter__pager {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 3.5rem;
  padding-top: 2rem;
  border-top: 1px solid var(--hairline);
}
.pager-btn {
  display: inline-flex; align-items: center; gap: 0.6rem;
  max-width: 46%;
  border: 1px solid var(--hairline);
  background: var(--paper-raised);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  font: inherit; font-size: 0.85rem;
  font-family: var(--font-sans);
  color: var(--ink-soft);
  cursor: pointer;
  transition: border-color 0.2s, color 0.2s, background 0.2s;
}
.pager-btn:hover:not(:disabled) {
  border-color: var(--accent);
  color: var(--accent);
}
.pager-btn:disabled { opacity: 0.35; cursor: default; }
.pager-btn__label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.reader-footer {
  text-align: center;
  padding: 0 2rem 3rem;
  font-size: 0.72rem;
  color: var(--ink-faint);
  letter-spacing: 0.05em;
}

/* ── Breadcrumb ────────────────────────────────────────────── */

.crumbs[hidden] { display: none; }
.crumbs {
  max-width: 1200px;
  margin: 0 auto 1.6rem;
  display: flex;
  align-items: baseline;
  gap: 0.6rem;
  font-size: 0.78rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.crumbs__link {
  border: 0; background: none; cursor: pointer;
  font: inherit; letter-spacing: inherit; text-transform: inherit;
  color: var(--ink-faint);
  padding: 0.35rem 0;
  transition: color 0.2s;
}
.crumbs__link:hover { color: var(--accent); }
.crumbs__sep { color: var(--ink-faint); opacity: 0.6; }
.crumbs__current {
  color: var(--accent);
  font-weight: 600;
}

/* ── Category folder card ──────────────────────────────────── */

.cat {
  cursor: pointer;
  background: var(--paper-raised);
  border: 1px solid var(--hairline);
  border-radius: var(--radius);
  padding: 1.2rem 1.1rem 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
  animation: card-in 0.55s var(--ease) both;
  transition: transform 0.4s var(--ease), box-shadow 0.4s var(--ease), border-color 0.3s;
}
.cat:hover,
.cat:focus-visible {
  transform: translateY(-4px);
  box-shadow: var(--shadow-book-hover);
  border-color: var(--accent);
  outline: none;
}

.cat__fan {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  min-height: 108px;
  padding-top: 0.4rem;
}
.cat__mini {
  width: 64px;
  aspect-ratio: 2 / 3;
  border-radius: 2px 4px 4px 2px;
  overflow: hidden;
  box-shadow: var(--shadow-book);
  transform: rotate(var(--tilt, 0deg)) translateY(0);
  transition: transform 0.35s var(--ease);
  position: relative;
}
.cat__mini + .cat__mini { margin-left: -12px; }
.cat__mini:nth-child(even) { margin-bottom: 6px; }
.cat:hover .cat__mini { transform: rotate(var(--tilt, 0deg)) translateY(-5px); }
.cat__mini img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cat__placeholder { padding: 0.45rem 0.4rem; }
.cat__placeholder .book__placeholder-initial { font-size: 1.1rem; padding-bottom: 0.25rem; }
.cat__placeholder .book__placeholder-title { font-size: 0.46rem; -webkit-line-clamp: 3; }

.cat__label { text-align: center; }
.cat__name {
  display: block;
  font-family: var(--font-serif);
  font-size: 1.02rem;
  line-height: 1.3;
}
.cat__count {
  display: block;
  margin-top: 0.25rem;
  font-size: 0.68rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

/* category tag on book cards (search results) */
.book__cat {
  display: inline-block;
  margin: 0 0 0.15rem;
  padding: 0.1rem 0.45rem 0.06rem;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  font-size: 0.6rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
}

/* ── Reader mobile top bar ─────────────────────────────────── */

.reader-topbar { display: none; }
.toc-overlay { display: none; }

/* ── Narrow screens ────────────────────────────────────────── */

@media (max-width: 860px) {
  #view-reader.is-active { flex-direction: column; }

  /* sticky top bar with drawer controls */
  .reader-topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    padding: 0.55rem 0.9rem;
    border-bottom: 1px solid var(--hairline);
    background: var(--paper);
    position: sticky;
    top: 0;
    z-index: 25;
  }
  .topbar-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    min-height: 40px;
    padding: 0.45rem 0.8rem;
    border: 1px solid var(--hairline);
    border-radius: 999px;
    background: var(--paper-raised);
    color: var(--ink-soft);
    font: inherit;
    font-size: 0.78rem;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    cursor: pointer;
    transition: color 0.2s, border-color 0.2s;
  }
  .topbar-btn:hover { color: var(--accent); border-color: var(--accent); }
  .reader-topbar__title {
    flex: 1;
    min-width: 0;
    text-align: center;
    font-family: var(--font-serif);
    font-size: 0.92rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--ink-soft);
  }

  /* sidebar becomes an off-canvas drawer */
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(320px, 85vw);
    height: 100%;
    z-index: 40;
    border-right: 1px solid var(--hairline);
    transform: translateX(-105%);
    transition: transform 0.35s var(--ease);
  }
  #view-reader.is-toc-open .sidebar { transform: none; }
  .toc-overlay {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 35;
    background: rgba(20, 15, 8, 0.45);
  }
  .toc-overlay[hidden] { display: none; }

  .reader-main { height: auto; flex: 1; min-height: 0; }

  /* roomier touch targets */
  .toc__item { padding: 0.7rem 0.6rem; }
  .pager-btn { min-height: 44px; padding: 0.8rem 1rem; }
  .back-btn { padding: 0.5rem 0; }
}

@media (max-width: 640px) {
  .shelf-header { padding-top: 2rem; }
  .shelf-header__inner { align-items: flex-start; flex-direction: column; gap: 1rem; }
  .shelf-tools { width: 100%; }
  .search { flex: 1; }
  .search input { width: 100%; }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
    gap: 1.4rem 1rem;
  }

  .chapter { padding: 2.25rem 1.25rem 1.5rem; }
  .prose { font-size: 1rem; line-height: 1.7; }
  .prose h2 { font-size: 1.45rem; }
  .prose h3 { font-size: 1.18rem; }
  .chapter__pager { flex-direction: column; }
  .pager-btn { max-width: 100%; justify-content: center; }

  .reader-footer { display: none; }
}

@media (max-width: 400px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
  .wordmark__sub { display: none; }
  .cat { padding: 0.9rem 0.8rem 0.85rem; }
  .cat__mini { width: 56px; }
}
