:root {
  color-scheme: light;
  --stage: #e8e7e2;
  --paper: #f8f6ec;
  --ink: #201f1c;
  --quiet: #6d6a63;
  --line: rgba(32, 31, 28, 0.18);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  min-width: 320px;
  scroll-behavior: auto;
  scrollbar-gutter: stable;
}

body {
  min-height: 100dvh;
  margin: 0;
  overflow-x: hidden;
  background: var(--stage);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

.reader {
  min-height: 100dvh;
}

.pages {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(12px, 2vw, 24px);
  width: 100%;
  padding: clamp(12px, 3vw, 42px) 12px calc(88px + env(safe-area-inset-bottom));
}

.page-shell {
  position: relative;
  width: min(100%, 820px);
  aspect-ratio: var(--page-ratio, 2 / 3);
  overflow: hidden;
  background: var(--paper);
  box-shadow: 0 1px 2px rgba(32, 31, 28, 0.08), 0 12px 34px rgba(32, 31, 28, 0.09);
  contain: layout paint style;
}

.page-shell::after {
  position: absolute;
  inset: 0;
  content: "";
  border: 1px solid rgba(32, 31, 28, 0.045);
  pointer-events: none;
}

.page-canvas {
  display: block;
  width: 100%;
  height: 100%;
  background: var(--paper);
}

.annotation-layer {
  position: absolute;
  z-index: 2;
  inset: 0;
  pointer-events: none;
}

.annotation-link {
  position: absolute;
  display: block;
  border-radius: 2px;
  background: transparent;
  cursor: pointer;
  pointer-events: auto;
}

.annotation-link:hover {
  background: rgba(32, 31, 28, 0.045);
}

.annotation-link:focus-visible {
  outline: 2px solid rgba(32, 31, 28, 0.72);
  outline-offset: 1px;
}

.loading-progress {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background: var(--ink);
  transform: scaleX(0);
  transform-origin: left center;
  transition: transform 180ms ease, opacity 220ms ease;
}

.loading-progress.is-complete {
  opacity: 0;
}

.reader-controls {
  position: fixed;
  z-index: 30;
  left: 50%;
  bottom: calc(14px + env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 42px auto 42px;
  align-items: center;
  min-height: 46px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(248, 246, 236, 0.96);
  box-shadow: 0 8px 26px rgba(32, 31, 28, 0.14);
  transform: translateX(-50%);
  backdrop-filter: blur(10px);
  transition: opacity 140ms ease;
}

.reader-controls.is-scrolling {
  opacity: 0;
  pointer-events: none;
}

.page-button {
  display: grid;
  width: 42px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.page-button span {
  align-self: center;
  font-family: Georgia, serif;
  font-size: 27px;
  line-height: 1;
  transform: translateY(-1px);
}

.page-button:hover:not(:disabled) {
  background: rgba(32, 31, 28, 0.06);
}

.page-button:active:not(:disabled) {
  transform: translateY(1px);
}

.page-button:disabled {
  color: rgba(32, 31, 28, 0.28);
  cursor: default;
}

.page-button:focus-visible,
.page-position input:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: -3px;
}

.page-position {
  display: flex;
  align-items: baseline;
  justify-content: center;
  min-width: 94px;
  height: 44px;
  padding: 0 10px;
  border-right: 1px solid var(--line);
  border-left: 1px solid var(--line);
  color: var(--quiet);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}

.page-position input {
  width: 4ch;
  height: 100%;
  padding: 0;
  border: 0;
  appearance: textfield;
  background: transparent;
  color: var(--ink);
  text-align: right;
}

.page-position input::-webkit-inner-spin-button,
.page-position input::-webkit-outer-spin-button {
  margin: 0;
  appearance: none;
}

.page-divider {
  padding: 0 6px;
}

.reader-error,
.noscript-message {
  width: min(520px, calc(100% - 40px));
  margin: 22vh auto 0;
  text-align: center;
}

.reader-error h1 {
  margin: 0 0 12px;
  font-family: Georgia, serif;
  font-size: 24px;
  font-weight: 400;
}

.reader-error p,
.noscript-message {
  color: var(--quiet);
  font-size: 15px;
}

@media (max-width: 600px) {
  html {
    scrollbar-gutter: auto;
  }

  .pages {
    gap: 8px;
    padding: 0 0 calc(74px + env(safe-area-inset-bottom));
  }

  .page-shell {
    width: 100%;
    box-shadow: 0 1px 7px rgba(32, 31, 28, 0.11);
  }

  .reader-controls {
    bottom: calc(8px + env(safe-area-inset-bottom));
  }
}

@media (min-width: 1300px) {
  .reader-controls {
    right: 24px;
    left: auto;
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  .loading-progress {
    transition: none;
  }

  .reader-controls {
    transition: none;
  }
}

@media print {
  .reader-controls,
  .loading-progress {
    display: none;
  }
}
