:root {
  --bg: #1c1917;
  --surface: #292524;
  --text: #fafaf9;
  --muted: #a8a29e;
  --accent: #d4a574;
  --progress: #d4a574;
  --page-width: min(920px, 100vw - 2rem);
  --aspect: 2000 / 2414;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
}

body {
  font-family: "Georgia", "Times New Roman", serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
}

/* Progress bar */
.progress-bar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(255, 255, 255, 0.08);
  z-index: 100;
}

.progress-bar__fill {
  height: 100%;
  width: 0%;
  background: var(--progress);
  transition: width 0.15s ease-out;
}

/* Header */
.header {
  position: sticky;
  top: 3px;
  z-index: 90;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  background: rgba(28, 25, 23, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
}

.header__title {
  font-size: 1rem;
  font-weight: 400;
  letter-spacing: 0.02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header__actions {
  display: flex;
  gap: 0.5rem;
  flex-shrink: 0;
}

.btn {
  appearance: none;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  font-family: system-ui, sans-serif;
  font-size: 0.8rem;
  padding: 0.4rem 0.75rem;
  border-radius: 6px;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.15s;
}

.btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

/* Book */
.book {
  max-width: var(--page-width);
  margin: 0 auto;
  padding: 1.5rem 0 4rem;
}

.page {
  position: relative;
  margin-bottom: 0.5rem;
  scroll-margin-top: 4rem;
}

.page__frame {
  position: relative;
  width: 100%;
  aspect-ratio: var(--aspect);
  background: #111;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
  cursor: zoom-in;
}

.page__frame img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.page__frame img[data-loaded="false"] {
  opacity: 0;
}

.page__frame img[data-loaded="true"] {
  opacity: 1;
  transition: opacity 0.3s;
}

.page__skeleton {
  position: absolute;
  inset: 0;
  background: linear-gradient(
    110deg,
    #1a1a1a 0%,
    #2a2a2a 40%,
    #1a1a1a 80%
  );
  background-size: 200% 100%;
  animation: shimmer 1.5s ease-in-out infinite;
}

.page__frame[data-loaded="true"] .page__skeleton {
  display: none;
}

@keyframes shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* Lightbox */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0, 0, 0, 0.95);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  cursor: zoom-out;
}

.lightbox[aria-hidden="false"] {
  display: flex;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  user-select: none;
}

.lightbox__close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  font-family: system-ui, sans-serif;
  font-size: 0.85rem;
  color: var(--muted);
}

/* TOC drawer */
.toc {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(320px, 90vw);
  z-index: 150;
  background: var(--surface);
  border-left: 1px solid rgba(255, 255, 255, 0.08);
  transform: translateX(100%);
  transition: transform 0.25s ease;
  overflow-y: auto;
  padding: 1.25rem;
}

.toc[aria-hidden="false"] {
  transform: translateX(0);
}

.toc__title {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin-bottom: 1rem;
}

.toc__grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
}

.toc__link {
  display: block;
  text-align: center;
  padding: 0.35rem;
  font-family: system-ui, sans-serif;
  font-size: 0.8rem;
  color: var(--muted);
  text-decoration: none;
  border-radius: 4px;
  transition: background 0.15s, color 0.15s;
}

.toc__link:hover,
.toc__link--active {
  background: rgba(212, 165, 116, 0.15);
  color: var(--accent);
}

.toc-backdrop {
  position: fixed;
  inset: 0;
  z-index: 140;
  background: rgba(0, 0, 0, 0.5);
  display: none;
}

.toc-backdrop[aria-hidden="false"] {
  display: block;
}

@media (max-width: 600px) {
  .header {
    padding: 0.6rem 1rem;
  }

  .header__title {
    font-size: 0.9rem;
  }

  .btn span { display: none; }
  .btn::after { content: attr(data-short); }
}
