/* ═══════════════════════════════════════════════════════
   THE SHELF — source-document library
   reuses tokens from style.css; layout + cover treatment only
   ═══════════════════════════════════════════════════════ */

.library {
  min-height: 100dvh;
  background: var(--ink);
  color: var(--bone);
}

.lib-head {
  padding: calc(18px + env(safe-area-inset-top)) 20px 14px;
  border-bottom: var(--rule) solid var(--ink-3);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--ink);
}
.lib-head h1 {
  font-family: var(--display);
  font-size: 20px;
  letter-spacing: .04em;
  text-transform: uppercase;
  font-weight: 800;
}
.lib-head .sub {
  font-family: var(--mono);
  font-size: 9.5px;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--mute);
}

/* ── layout: no grid, just the shelf ── */
.lib-body {
  position: relative;
}

/* ── shelf: horizontal scroll carousel ── */
.lib-grid-wrap {
  padding: 32px 24px 40px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.lib-grid-wrap::-webkit-scrollbar { display: none; }
.lib-grid {
  display: flex;
  gap: 20px;
  width: max-content;
  min-width: 100%;
  padding-right: 80px; /* breathing room before edge */
}
.lib-grid .spacer {
  width: 1px;
  flex-shrink: 0;
}

/* ── book cover tiles ── */
.lib-item {
  flex-shrink: 0;
  width: 140px;
  text-align: center;
}
.lib-item button {
  all: unset;
  cursor: pointer;
  display: block;
  width: 100%;
}

.cover {
  position: relative;
  aspect-ratio: 3 / 4;
  border: var(--hair) solid var(--ink-3);
  overflow: hidden;
  isolation: isolate;
  transition: transform .12s steps(3);
}
.lib-item button:hover .cover { transform: translateY(-3px); }
.lib-item[data-active="true"] .cover {
  border: 2px solid var(--ochre);
  box-shadow: 4px 4px 0 var(--ink-3);
  transform: translateY(-6px);
}

.cover .pattern { position: absolute; inset: 0; }
.cover--0 .pattern {
  background: repeating-linear-gradient(45deg,
    var(--terra) 0 14px, var(--ochre) 14px 28px,
    var(--veld) 28px 42px, var(--indigo) 42px 56px);
}
.cover--1 .pattern {
  background: repeating-linear-gradient(-45deg,
    var(--indigo) 0 16px, var(--ink-2) 16px 32px,
    var(--blood) 32px 40px, var(--ink-2) 40px 56px);
}
.cover--2 .pattern {
  background: repeating-linear-gradient(90deg,
    var(--veld) 0 12px, var(--bone-2) 12px 15px,
    var(--ink-2) 15px 40px);
}
.cover--3 .pattern {
  background: repeating-linear-gradient(0deg,
    var(--ochre) 0 10px, var(--ink-2) 10px 34px,
    var(--terra) 34px 44px, var(--ink-2) 44px 68px);
}

.cover .shade {
  position: absolute; inset: 0;
  background: linear-gradient(180deg, transparent 40%, var(--ink) 92%);
}

.cover .stamp {
  position: absolute; top: 8px; right: 8px;
  background: var(--ink); border: var(--hair) solid var(--ochre);
  color: var(--ochre); font-family: var(--mono); font-size: 8px;
  letter-spacing: .08em; padding: 3px 5px; transform: rotate(3deg);
}

.cover .title {
  position: absolute; left: 9px; right: 9px; bottom: 10px;
  font-family: var(--display); font-weight: 800;
  font-size: 12.5px; line-height: 1.18; letter-spacing: .01em;
  color: var(--bone); white-space: pre-line;
  text-shadow: 0 1px 0 var(--ink);
}

.lib-item .caption {
  margin-top: 8px;
  font-family: var(--mono); font-size: 9.5px; letter-spacing: .05em;
  color: var(--mute);
}
.lib-item[data-active="true"] .caption { color: var(--bone-2); }

/* ──────────────────────────────────────
   BOTTOM SHEET — slides up when a doc is selected
   ────────────────────────────────────── */

/* Backdrop overlay */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0,0,0,0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity .25s;
}
.sheet-backdrop--open {
  opacity: 1;
  pointer-events: auto;
}

/* Sheet panel */
.lib-panel {
  position: fixed;
  left: 0; right: 0; bottom: 0; z-index: 30;
  max-height: 85dvh;
  background: var(--ink-2);
  border-top: var(--rule) solid var(--ink-3);
  border-radius: 16px 16px 0 0;
  padding: 12px 20px 20px;
  display: flex;
  flex-direction: column;
  transform: translateY(100%);
  transition: transform .3s cubic-bezier(0.32, 0.72, 0, 1);
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}
.lib-panel--open {
  transform: translateY(0);
}

/* Expanded state — sheet grows to near full-screen height */
.lib-panel--expanded {
  height: 96dvh;
  max-height: 96dvh;
}

/* Top row: drag handle centered, expand/close controls pinned right */
.sheet-top-row {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 28px;
  margin-bottom: 12px;
  flex-shrink: 0;
}

/* Drag handle at top */
.sheet-handle {
  width: 36px; height: 4px;
  background: var(--ink-3);
  border-radius: 2px;
  flex-shrink: 0;
}

/* Expand / close controls, top-right of the sheet */
.sheet-toolbar {
  position: absolute;
  top: 50%; right: 0;
  transform: translateY(-50%);
  display: flex;
  gap: 8px;
}
.sheet-btn {
  all: unset;
  cursor: pointer;
  box-sizing: border-box;
  width: 28px; height: 28px;
  display: grid;
  place-items: center;
  background: var(--ink);
  border: var(--hair) solid var(--ink-3);
  border-radius: 50%;
  color: var(--bone-2);
  font-family: var(--mono);
  font-size: 13px;
  line-height: 1;
}
.sheet-btn:hover { color: var(--ochre); border-color: var(--ochre); }
.sheet-btn--close { font-size: 16px; }

/* ── sheet header ── */
.lib-panel .meta-line {
  font-family: var(--mono); font-size: 9px; letter-spacing: .1em;
  text-transform: uppercase; color: var(--terra); margin-bottom: 8px;
}
.lib-panel h2 {
  font-family: var(--display); font-weight: 800;
  font-size: 17px; line-height: 1.2; letter-spacing: .005em;
  margin-bottom: 10px;
}
.lib-panel .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.lib-panel .tag {
  font-family: var(--mono); font-size: 8.5px; letter-spacing: .08em;
  color: var(--ochre); border: var(--hair) solid var(--ochre);
  padding: 2px 6px;
}
.lib-panel p.summary {
  font-family: var(--body); font-size: 14px; line-height: 1.55;
  color: var(--bone-2); margin-bottom: 16px;
}
.lib-panel .read-link {
  display: inline-block; align-self: flex-start;
  font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em;
  text-transform: uppercase; color: var(--ink);
  background: var(--ochre); padding: 9px 14px;
  box-shadow: 4px 4px 0 var(--ink-3);
  border: none; cursor: pointer;
  text-decoration: none;
  margin-bottom: 18px;
}

/* ── sheet read mode header ── */
.sheet-read-top {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 12px; flex-shrink: 0;
}
.sheet-read-top .back-to-summary {
  all: unset; cursor: pointer;
  font-family: var(--mono); font-size: 10px; letter-spacing: .1em;
  text-transform: uppercase; color: var(--terra);
  white-space: nowrap;
  flex-shrink: 0;
}
.sheet-read-top .source {
  font-family: var(--mono); font-size: 9px; letter-spacing: .06em;
  color: var(--terra); text-decoration: none; white-space: nowrap;
  margin-left: auto;
}
.sheet-read-top .source:hover { color: var(--ochre); }

/* ── full document reader ── */
.full-read h2 {
  font-family: var(--display); font-weight: 800; font-size: 13px;
  letter-spacing: .02em; text-transform: uppercase;
  color: var(--ochre); margin: 20px 0 8px;
}
.full-read h2:first-child { margin-top: 2px; }
.full-read h3 {
  font-family: var(--display); font-weight: 700; font-size: 12px;
  color: var(--bone-2); margin: 12px 0 5px;
}
.full-read p {
  font-family: var(--body); font-size: 13px; line-height: 1.58;
  color: var(--bone-2); margin-bottom: 10px;
}
.full-read blockquote {
  border-left: var(--rule) solid var(--terra);
  padding: 2px 0 2px 12px; margin: 10px 0;
  font-family: var(--body); font-style: italic; font-size: 12px;
  line-height: 1.55; color: var(--bone); white-space: pre-line;
}
.full-read ul { margin: 0 0 10px 16px; }
.full-read li {
  font-family: var(--body); font-size: 12.5px; line-height: 1.5;
  color: var(--bone-2); margin-bottom: 4px;
}
.full-read .meta-block {
  background: var(--ink); border: var(--hair) solid var(--ink-3);
  padding: 10px 12px; margin-bottom: 14px;
}
.full-read .meta-block .row {
  display: flex; gap: 8px; font-family: var(--mono); font-size: 9.5px;
  line-height: 1.5; margin-bottom: 4px;
}
.full-read .meta-block .row b {
  color: var(--terra); text-transform: uppercase; letter-spacing: .05em;
  flex: none; width: 74px;
}
.full-read .meta-block .row span { color: var(--mute); }

/* ── collapsible section TOC ── */
.toc-wrap {
  margin: 14px 0 16px;
}
.toc-toggle {
  all: unset; cursor: pointer;
  display: flex; align-items: center; gap: 6px;
  font-family: var(--mono); font-size: 9.5px; letter-spacing: .08em;
  text-transform: uppercase; color: var(--terra);
  padding: 8px 12px;
  background: var(--ink);
  border: var(--hair) solid var(--ink-3);
  width: 100%; box-sizing: border-box;
}
.toc-toggle .arrow {
  font-size: 8px; transition: transform .15s;
  display: inline-block;
}
.toc-toggle .arrow--open { transform: rotate(90deg); }
.section-toc {
  padding: 8px 12px 10px;
  background: var(--ink);
  border: var(--hair) solid var(--ink-3);
  border-top: none;
}
.section-toc .toc-link {
  display: block;
  font-family: var(--body); font-size: 11.5px; line-height: 1.5;
  color: var(--bone-2); text-decoration: none;
  padding: 2px 0;
}
.section-toc .toc-link:hover { color: var(--ochre); }
.section-toc .toc-link--h3 {
  padding-left: 14px; font-size: 11px; color: var(--mute);
}

/* ── chapter navigation ── */
.chapter-nav {
  margin: 14px 0 16px;
}
.chapter-nav .chapter-controls {
  display: flex; align-items: center; gap: 8px; margin-bottom: 8px;
}
.chapter-nav .chapter-label {
  font-family: var(--mono); font-size: 9px; letter-spacing: .06em;
  color: var(--terra); margin-bottom: 8px;
}
.chapter-nav .chapter-controls button {
  all: unset; cursor: pointer;
  font-family: var(--mono); font-size: 9.5px; letter-spacing: .06em;
  text-transform: uppercase; color: var(--terra);
  background: var(--ink); border: var(--hair) solid var(--ink-3);
  padding: 5px 10px;
}
.chapter-nav .chapter-controls button:disabled {
  color: var(--ink-3); cursor: default;
}
.chapter-nav .chapter-controls button:not(:disabled):hover { color: var(--ochre); border-color: var(--ochre); }
.chapter-nav .chapter-info {
  font-family: var(--mono); font-size: 9px; color: var(--mute);
  min-width: 36px; text-align: center;
}
.chapter-nav .chapter-dropdown select {
  width: 100%; font-family: var(--mono); font-size: 9.5px;
  background: var(--ink); color: var(--bone-2);
  border: var(--hair) solid var(--ink-3); padding: 5px 8px;
}

/* ── loading / error ── */
.fulltext-loading, .fulltext-error {
  font-family: var(--mono); font-size: 10px; letter-spacing: .06em;
  color: var(--mute); padding: 14px 0;
}
.fulltext-error button {
  all: unset; cursor: pointer; color: var(--terra); text-decoration: underline;
}

/* ── notes ── */
.lib-panel .notes {
  margin-top: auto;
  background: var(--bone); color: var(--ink);
  padding: 12px 12px 10px;
  box-shadow: 4px 4px 0 var(--ink-3);
  flex-shrink: 0;
}
.lib-panel .notes label {
  display: block; font-family: var(--mono); font-size: 8.5px;
  letter-spacing: .1em; text-transform: uppercase; color: var(--mute);
  margin-bottom: 6px;
}
.lib-panel .notes textarea {
  width: 100%; min-height: 64px; resize: vertical;
  border: none; outline: none; background: transparent;
  font-family: var(--body); font-size: 13px; line-height: 1.4;
  color: var(--ink);
}
.lib-panel .notes .saved {
  font-family: var(--mono); font-size: 8px; color: var(--mute);
  text-align: right; margin-top: 4px; height: 10px;
}