:root {
  --bg: #1b1712;
  --bg-glow: #3a2f22;
  --ink: #f4ede1;
  --muted: #b9ab95;
  --accent: #c9a46a;
  --panel: rgba(20, 17, 13, 0.72);
  --line: rgba(244, 237, 225, 0.14);
  --bar-h: 56px;
  --skeleton: #ebe5da;
  --fold-dark: rgba(60, 45, 30, 0.22);
  --fold-mid: rgba(60, 45, 30, 0.08);
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

/* Fills the notch areas iOS leaves beside the page in landscape. */
html { background: var(--bg); }

body {
  display: grid;
  grid-template-rows: var(--bar-h) 1fr auto;
  height: 100dvh;
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 45%, var(--bg-glow) 0%, var(--bg) 70%);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

/* StPageFlip's wrapper overflows the stage (padding-bottom aspect box), so the bars must sit above it. */
.topbar, .controls {
  position: relative;
  z-index: 1;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid var(--line);
}

.title { min-width: 0; }
.title h1 {
  margin: 0;
  font: 600 16px/1.2 Georgia, "Times New Roman", serif;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.title p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.stage {
  position: relative;
  isolation: isolate; /* keep StPageFlip's page z-indexes below the contents panel */
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 20px 16px;
}

.book {
  flex: none; /* sized in app.js to exactly one spread */
  transition: transform 0.6s ease;
}

.page {
  background: #fdfbf7;
  overflow: hidden;
}
/* Skeleton: a heading bar and text lines with a moving sheen, shown until the page image loads. */
.page::before,
.page::after {
  content: "";
  position: absolute;
  pointer-events: none;
}
.page::before {
  inset: 13% 12% 14%;
  background:
    linear-gradient(var(--skeleton), var(--skeleton)) top left / 58% 5% no-repeat,
    repeating-linear-gradient(to bottom, var(--skeleton) 0 2.2%, transparent 2.2% 5%) 0 100% / 100% 82% no-repeat;
}
.page::after {
  inset: 0;
  background: linear-gradient(100deg, transparent 35%, rgba(255, 255, 255, 0.7) 50%, transparent 65%) 0 0 / 250% 100%;
  animation: skeleton-sheen 1.4s linear infinite;
}
.page.is-loaded::before,
.page.is-loaded::after { display: none; }

/* Once loaded, inner pages reuse ::after as the shadow of the fold at the spine. */
.page.is-loaded:not([data-density="hard"]).--left::after,
.page.is-loaded:not([data-density="hard"]).--right::after {
  display: block;
  animation: none;
  background-size: auto;
}
.page.is-loaded:not([data-density="hard"]).--left::after {
  background-image: linear-gradient(to left, var(--fold-dark) 0, var(--fold-mid) 2.5%, transparent 10%);
}
.page.is-loaded:not([data-density="hard"]).--right::after {
  background-image: linear-gradient(to right, var(--fold-dark) 0, var(--fold-mid) 2.5%, transparent 10%);
}

@keyframes skeleton-sheen {
  from { background-position: 100% 0; }
  to { background-position: -150% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .page::after { animation: none; }
}

.page img {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  object-fit: fill;
  user-select: none;
  -webkit-user-drag: none;
  opacity: 0;
  transition: opacity 0.25s ease;
}
.page.is-loaded img { opacity: 1; }

.loading {
  position: absolute;
  color: var(--muted);
  font-family: Georgia, serif;
  letter-spacing: 0.04em;
}
.loading[hidden] { display: none; }

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px max(16px, env(safe-area-inset-right)) calc(10px + env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
  border-top: 1px solid var(--line);
  background: var(--panel);
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  flex: none;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: rgba(244, 237, 225, 0.08); border-color: rgba(244, 237, 225, 0.3); }
.icon-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.actions { display: flex; gap: 8px; flex: none; }

.text-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 40px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: transparent;
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
.text-btn:hover, .text-btn[aria-expanded="true"] { background: rgba(244, 237, 225, 0.08); }
.text-btn svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
}

.contents {
  position: fixed;
  z-index: 10;
  top: calc(var(--bar-h) + 8px);
  right: 16px;
  width: min(360px, calc(100vw - 32px));
  max-height: calc(100dvh - var(--bar-h) - 96px);
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #241e17;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.5);
}
.contents[hidden] { display: none; }
.contents ol { margin: 0; padding: 0; list-style: none; }
.contents .part {
  padding: 12px 12px 4px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}
.contents button {
  display: flex;
  gap: 10px;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink);
  font: 14px/1.35 Georgia, "Times New Roman", serif;
  text-align: left;
  cursor: pointer;
}
.contents button:hover { background: rgba(244, 237, 225, 0.08); }
.contents button small {
  flex: none;
  width: 22px;
  color: var(--muted);
  font: 12px/1.6 system-ui, sans-serif;
  font-variant-numeric: tabular-nums;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.slider {
  flex: 1;
  max-width: 420px;
  min-width: 0;
  accent-color: var(--accent);
}

.jump {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.jump input {
  width: 58px;
  height: 36px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.25);
  color: var(--ink);
  font: inherit;
  text-align: center;
  -moz-appearance: textfield;
}
.jump input::-webkit-inner-spin-button,
.jump input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

/* Short screens (phones in landscape): slimmer bars leave more height for the book. */
@media (max-height: 520px) {
  :root { --bar-h: 44px; }
  .title h1 { font-size: 14px; }
  .title p { display: none; }
  .stage { padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left)); }
  .controls { padding-top: 6px; padding-bottom: calc(6px + env(safe-area-inset-bottom)); }
  .icon-btn, .text-btn { height: 34px; }
  .icon-btn { width: 34px; }
  .jump input { height: 32px; }
}

@media (max-width: 560px) {
  .slider, .text-btn span { display: none; }
  .text-btn { width: 40px; padding: 0; justify-content: center; }
  .stage { padding: 12px 8px; }
}
