:root {
  --font: 1.15rem;
  --lh: 1.6;
  --fg: #ece6da;
  --fg-dim: rgba(236, 230, 218, 0.62);
  --fg-faint: rgba(236, 230, 218, 0.34);
  --bg: #0b132b;
  --paper: transparent;
  --chip: rgba(255, 255, 255, 0.08);
  --chip-fg: #fff;
  --accent: #f2b880;
  --heart: #ff5f7e;
  --sheet-bg: #141a2b;
  --sheet-fg: #ece6da;
  --grad-opacity: 1;
  --sa-top: env(safe-area-inset-top, 0px);
  --sa-bottom: env(safe-area-inset-bottom, 0px);
}
html[data-theme="light"] {
  --fg: #1d1a16; --fg-dim: rgba(29, 26, 22, 0.6); --fg-faint: rgba(29, 26, 22, 0.32);
  --bg: #f6f1e7; --chip: rgba(0, 0, 0, 0.06); --chip-fg: #1d1a16; --accent: #9a4f1a;
  --sheet-bg: #fbf8f1; --sheet-fg: #1d1a16; --grad-opacity: 0.08;
}
html[data-theme="sepia"] {
  --fg: #3b2f22; --fg-dim: rgba(59, 47, 34, 0.62); --fg-faint: rgba(59, 47, 34, 0.34);
  --bg: #efe3c8; --chip: rgba(70, 50, 20, 0.08); --chip-fg: #3b2f22; --accent: #8b4a12;
  --sheet-bg: #f5ecd8; --sheet-fg: #3b2f22; --grad-opacity: 0.12;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: Literata, "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }

#app { position: fixed; inset: 0; overflow: hidden; }
.feed { position: absolute; inset: 0; will-change: transform; }
.feed.animate { transition: transform 0.32s cubic-bezier(0.22, 0.61, 0.36, 1); }

.pane {
  position: absolute; left: 0; right: 0; height: 100%;
  display: flex; flex-direction: column;
  touch-action: none;
  user-select: none; -webkit-user-select: none;
  background: var(--bg);
}
.pane::before {
  content: ""; position: absolute; inset: 0; opacity: var(--grad-opacity); pointer-events: none;
  background:
    radial-gradient(120% 80% at 80% -10%, var(--c1) 0%, transparent 60%),
    radial-gradient(100% 70% at 0% 100%, color-mix(in srgb, var(--c1) 70%, #000) 0%, transparent 55%),
    linear-gradient(180deg, var(--c2), var(--bg));
}
.pane.prev { top: -100%; } .pane.cur { top: 0; } .pane.next { top: 100%; }
.pane > * { position: relative; }

.top {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + var(--sa-top)) 12px 8px;
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; display: grid; place-items: center;
  background: var(--chip); color: var(--chip-fg); font-size: 1.05rem; flex: none;
}
.bookchip { flex: 1; min-width: 0; text-align: left; line-height: 1.2; }
.bookchip b { display: block; font-weight: 600; font-size: 0.98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bookchip span { display: block; font-size: 0.78rem; color: var(--fg-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.progress { height: 2px; margin: 0 12px; background: var(--fg-faint); border-radius: 2px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.3s; }
/* scrubber: a 2px bar with a 32px touch zone; drag to peek anywhere in the book */
.scrub { position: relative; height: 32px; margin: -12px 0 -12px; padding: 15px 12px; touch-action: none; cursor: pointer; z-index: 2; }
.scrub .bar { height: 2px; background: var(--fg-faint); border-radius: 2px; overflow: hidden; transition: height 0.15s; }
.scrub .bar i { display: block; height: 100%; width: 0; background: var(--accent); }
.scrub.active .bar { height: 6px; }
.scrub .bubble {
  position: absolute; top: -34px; transform: translateX(-50%); white-space: nowrap;
  background: var(--sheet-bg); color: var(--sheet-fg); font-size: 0.8rem; padding: 6px 10px; border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35); max-width: 80vw; overflow: hidden; text-overflow: ellipsis; pointer-events: none;
}
.peekpill {
  position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); display: flex; gap: 2px; z-index: 3;
  background: var(--sheet-bg); color: var(--sheet-fg); border-radius: 999px; padding: 4px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
  font-size: 0.82rem; white-space: nowrap; max-width: calc(100% - 24px);
}
.peekpill button { padding: 7px 12px; border-radius: 999px; }
.peekpill button:first-child { color: var(--accent); font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.peekpill button:last-child { background: var(--chip); }

.track { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.slides { position: absolute; inset: 0; display: flex; width: 300%; left: -100%; will-change: transform; }
.slides.animate { transition: transform 0.26s cubic-bezier(0.22, 0.61, 0.36, 1); }
.card {
  width: 33.3333%; height: 100%; flex: none;
  display: flex; flex-direction: column; justify-content: center;
  padding: 8px 22px 12px;
}
.card-scroll {
  max-height: 100%; overflow-y: auto; overscroll-behavior: contain;
  touch-action: none; scrollbar-width: none;
  -webkit-mask-image: linear-gradient(transparent, #000 10px, #000 calc(100% - 10px), transparent);
  mask-image: linear-gradient(transparent, #000 10px, #000 calc(100% - 10px), transparent);
  padding: 10px 0;
}
.card-scroll::-webkit-scrollbar { display: none; }
.card p { margin: 0 0 0.85em; font-size: var(--font); line-height: var(--lh); text-wrap: pretty; hyphens: auto; }
.card p:last-child { margin-bottom: 0; }
.card p.verse { white-space: pre-line; font-style: italic; padding-left: 1em; }
.card p.start-line { text-indent: 0; }
.ch-title {
  font-size: 0.78rem; letter-spacing: 0.14em; text-transform: uppercase; color: var(--accent);
  margin: 0 0 1.1em; font-weight: 600;
}
.book-title-card { margin-bottom: 1.4em; }
.book-title-card h1 { font-size: 1.7rem; line-height: 1.15; margin: 0 0 0.25em; font-weight: 600; }
.book-title-card .by { color: var(--fg-dim); font-size: 0.95rem; margin: 0 0 0.9em; }
.book-title-card .blurb { color: var(--fg-dim); font-style: italic; font-size: 1rem; }
.landing {
  font-size: 0.9rem; color: var(--fg-dim); font-style: italic; margin: 0 0 1.2em;
  padding-left: 0.9em; border-left: 2px solid var(--accent);
}
.landing b { color: var(--fg); font-style: normal; font-weight: 600; }
.cover h1 { font-size: 1.9rem; line-height: 1.12; margin: 0 0 0.2em; font-weight: 600; }
.cover .by { color: var(--fg-dim); font-size: 0.98rem; margin: 0 0 1em; }
.cover .blurb { font-size: 1.05rem; line-height: 1.5; margin: 0 0 1.3em; }
.cover blockquote { margin: 0 0 1.4em; padding-left: 0.9em; border-left: 2px solid var(--accent); }
.cover blockquote p { font-style: italic; font-size: 1rem; color: var(--fg-dim); margin: 0 0 0.5em; }
.cover blockquote footer { font-size: 0.78rem; color: var(--fg-dim); font-style: normal; }
.cover .cta-line { color: var(--accent); font-weight: 600; font-size: 0.95rem; margin: 0; }
.the-end { text-align: center; color: var(--fg-dim); font-style: italic; }
.the-end h2 { font-weight: 600; color: var(--fg); margin: 0 0 0.4em; }

.bottom {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 12px calc(10px + var(--sa-bottom));
  min-height: 56px;
}
.hint { flex: 1; min-width: 0; font-size: 0.8rem; color: var(--fg-dim); line-height: 1.3; }
.hint .cta { color: var(--accent); font-weight: 600; font-style: normal; }
.hint button { color: var(--accent); font-weight: 600; text-decoration: underline; text-underline-offset: 3px; }
.actions { display: flex; gap: 8px; }
.actions .icon-btn { font-size: 1.15rem; }
.like.on { color: var(--heart); }
.play.on { background: var(--accent); color: #000; }
.play.loading { opacity: 0.7; }
.w { border-radius: 3px; transition: background 0.08s; }
.w.on { background: color-mix(in srgb, var(--accent) 38%, transparent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 38%, transparent); }
.popover select { font: inherit; font-size: 0.85rem; padding: 6px 8px; border-radius: 8px; border: 1px solid var(--fg-faint); background: var(--chip); color: inherit; max-width: 170px; }
.like.pop { animation: pop 0.35s ease; }
@keyframes pop { 40% { transform: scale(1.35); } }

/* sheets */
.sheet { position: fixed; inset: 0; z-index: 20; }
.sheet-bg { position: absolute; inset: 0; background: rgba(0, 0, 0, 0.45); }
.sheet-body {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 88%; overflow-y: auto;
  background: var(--sheet-bg); color: var(--sheet-fg);
  border-radius: 18px 18px 0 0; padding: 12px 16px calc(16px + var(--sa-bottom));
  animation: up 0.25s ease;
}
@keyframes up { from { transform: translateY(30px); opacity: 0; } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sheet-head h2 { margin: 0; font-size: 1.2rem; font-weight: 600; }
.stats { font-size: 0.85rem; color: var(--fg-dim); margin-bottom: 10px; }
.search {
  width: 100%; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--fg-faint);
  background: var(--chip); color: inherit; font: inherit; font-size: 0.95rem; margin-bottom: 10px;
}
.tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.tags button { font-size: 0.78rem; padding: 4px 10px; border-radius: 999px; background: var(--chip); color: inherit; }
.tags button.on { background: var(--accent); color: #000; }
.books { display: grid; gap: 8px; }
.book {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 12px; align-items: center;
  padding: 10px; border-radius: 12px; background: var(--chip); text-align: left; width: 100%;
}
.book .cover {
  width: 44px; height: 60px; border-radius: 4px 8px 8px 4px;
  background: linear-gradient(160deg, var(--c1), var(--c2)); box-shadow: inset 3px 0 0 rgba(255, 255, 255, 0.15);
  display: grid; place-items: center; font-size: 1.1rem; font-weight: 600; color: #fff;
}
.book .title { display: block; font-weight: 600; font-size: 0.95rem; line-height: 1.2; }
.book .sub { display: block; font-size: 0.78rem; color: var(--fg-dim); margin-top: 3px; }
.book .bar { display: block; height: 3px; background: var(--fg-faint); border-radius: 2px; margin-top: 8px; overflow: hidden; width: 100%; }
.book .bar i { display: block; height: 100%; background: var(--accent); }
.book .pct { font-size: 0.78rem; color: var(--fg-dim); white-space: nowrap; }
.fine { font-size: 0.75rem; color: var(--fg-dim); margin: 16px 0 0; line-height: 1.4; }
.fine a { color: inherit; }
.chapter-list { display: grid; }
.chapter-list button { text-align: left; padding: 10px 6px; border-bottom: 1px solid var(--fg-faint); font-size: 0.92rem; }
.chapter-list button.on { color: var(--accent); font-weight: 600; }

.popover {
  position: fixed; z-index: 15; top: calc(56px + var(--sa-top)); right: 12px;
  background: var(--sheet-bg); color: var(--sheet-fg); border-radius: 14px; padding: 10px 12px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35); min-width: 240px;
}
.popover .row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 6px 0; font-size: 0.9rem; }
.seg { display: inline-flex; gap: 4px; }
.seg button { padding: 6px 10px; border-radius: 8px; background: var(--chip); font-size: 0.85rem; }
.seg button.on { background: var(--accent); color: #000; }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--sa-bottom)); transform: translateX(-50%);
  background: #000; color: #fff; padding: 8px 14px; border-radius: 999px; font-size: 0.85rem; z-index: 30;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35); white-space: nowrap;
}
.coach {
  position: fixed; inset: 0; z-index: 25; display: grid; place-content: center; gap: 14px; text-align: center;
  background: rgba(0, 0, 0, 0.55); color: #fff; font-size: 1.1rem; backdrop-filter: blur(2px); pointer-events: none;
}
.coach b { display: inline-block; width: 1.6em; }
.coach small { opacity: 0.7; font-size: 0.85rem; }

@media (min-width: 720px) {
  #app { max-width: 560px; left: 50%; transform: translateX(-50%); box-shadow: 0 0 80px rgba(0, 0, 0, 0.35); }
  .sheet-body, .popover { max-width: 560px; margin: 0 auto; }
  .popover { right: calc(50% - 268px); }
  .card { padding: 8px 40px 12px; }
}

/* ---------- card illustrations ---------- */
/* The picture sits above the text inside the scrolling card, not behind it: a
   backdrop behind prose costs legibility on every card to decorate a few. The
   bottom edge fades into the pane so it reads as part of the page rather than a
   pasted-in rectangle. */
.card-img {
  margin: 0 0 1.35rem;
  position: relative;
  opacity: 0;
  transition: opacity .45s ease;
}
.card-img.ready { opacity: 1; }
.card-img-frame {
  position: relative;
  aspect-ratio: 3 / 2;
  border-radius: 14px;
  overflow: hidden;
  background: rgba(255, 255, 255, .04);
}
.card-img img { width: 100%; height: 100%; object-fit: cover; display: block; }
/* Fade the lower edge into the card so text below feels continuous with it. */
.card-img-frame::after {
  content: ''; position: absolute; inset: auto 0 -1px 0; height: 42%;
  background: linear-gradient(to bottom, transparent, var(--c2, #0b132b));
  pointer-events: none;
}
.card-img.loading .card-img-frame { animation: img-pulse 1.4s ease-in-out infinite; }
@keyframes img-pulse { 0%, 100% { opacity: .35; } 50% { opacity: .6; } }
.card-img.loading { opacity: 1; }

.img-regen {
  position: absolute; top: .5rem; right: .5rem;
  width: 34px; height: 34px; border-radius: 50%;
  border: 0; background: rgba(10, 14, 26, .55); color: #fff;
  font-size: 1rem; line-height: 1; cursor: pointer;
  backdrop-filter: blur(6px);
  opacity: 0; transition: opacity .25s ease;
}
.card-img.ready .img-regen { opacity: .75; }
.img-regen:hover, .img-regen:focus-visible { opacity: 1; }
.img-regen:disabled { opacity: .4; cursor: default; }
.card-img.loading .img-regen { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .card-img, .card-img.loading .card-img-frame, .card-img.loading .img-regen { transition: none; animation: none; }
}
