:root {
  color-scheme: dark;
  --navy: #07111e;
  --navy-2: #0b1d31;
  --blue: #32a9f4;
  --paper: #ffffff;
  --toolbar-h: 64px;
}

* { box-sizing: border-box; }

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

body {
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  padding-top: env(safe-area-inset-top);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 50% 15%, rgba(39, 139, 205, .16), transparent 38%),
    linear-gradient(145deg, #0a192a 0%, var(--navy) 55%, #040a12 100%);
  color: #fff;
}

button { font: inherit; }

.toolbar {
  height: var(--toolbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid rgba(255, 255, 255, .09);
  background: rgba(4, 12, 21, .78);
  backdrop-filter: blur(14px);
  position: relative;
  z-index: 20;
}

.brand { display: flex; align-items: baseline; gap: 14px; min-width: 0; }
.brand-mark { color: var(--blue); font-size: 2rem; line-height: 1; font-weight: 700; font-style: italic; letter-spacing: -.08em; }
.title { font-size: .95rem; font-weight: 600; letter-spacing: .08em; white-space: nowrap; }
.toolbar-actions { display: flex; align-items: center; gap: 6px; }

.icon-button, .nav-button {
  display: grid;
  place-items: center;
  border: 1px solid rgba(255,255,255,.13);
  color: #fff;
  background: rgba(255,255,255,.06);
  cursor: pointer;
  transition: background .2s, border-color .2s, transform .2s, opacity .2s;
}

.icon-button:hover, .nav-button:hover { background: rgba(50,169,244,.18); border-color: rgba(50,169,244,.6); }
.icon-button:active, .nav-button:active { transform: scale(.94); }
.icon-button:focus-visible, .nav-button:focus-visible, .product-link:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
.icon-button { width: 38px; height: 38px; border-radius: 10px; font-size: 1.35rem; }
.icon-button svg, .nav-button svg { width: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.sound-toggle[aria-pressed="true"] { color: var(--blue); }
.sound-toggle .sound-muted, .sound-toggle[aria-pressed="false"] .sound-waves { display: none; }
.sound-toggle[aria-pressed="false"] .sound-muted { display: block; }
.zoom-value { width: 50px; text-align: center; color: rgba(255,255,255,.72); font-size: .78rem; font-variant-numeric: tabular-nums; }

.viewer {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template: minmax(0, 1fr) / minmax(0, 1fr);
  place-items: center;
  padding: 16px 24px;
  overflow: hidden;
  perspective: 2400px;
}

.book-wrap {
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
  display: flex;
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: auto;
  scrollbar-width: thin;
}

.book {
  /* Exact fitted dimensions are set from the available viewport, not image size. */
  width: 0;
  height: 0;
  flex: 0 0 auto;
  margin: auto;
  aspect-ratio: 1.4136;
  display: flex;
  justify-content: center;
  position: relative;
  filter: drop-shadow(0 22px 34px rgba(0,0,0,.46));
  transform-style: preserve-3d;
}

.book.single { aspect-ratio: .7068; }

.page {
  height: 100%;
  aspect-ratio: .7068;
  position: relative;
  overflow: hidden;
  background: var(--paper);
  flex: 0 0 auto;
  backface-visibility: hidden;
}

.page.left { box-shadow: inset -16px 0 22px -20px rgba(0,0,0,.45); }
.page.right { box-shadow: inset 16px 0 22px -20px rgba(0,0,0,.45); }
.page img { display: block; width: 100%; height: 100%; object-fit: contain; user-select: none; -webkit-user-drag: none; }

.product-link { position: absolute; z-index: 12; display: block; border-radius: 2px; }
.product-link:hover { background: rgba(50,169,244,.16); box-shadow: inset 0 0 0 2px rgba(50,169,244,.7); }

.turn-sheet {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  aspect-ratio: .7068;
  transform-origin: left center;
  transform-style: preserve-3d;
  z-index: 8;
  pointer-events: none;
  animation: turnNext .48s cubic-bezier(.22,.72,.22,1) forwards;
  background: #fff;
  box-shadow: -10px 8px 28px rgba(0,0,0,.28);
  will-change: transform, filter;
}

.turn-sheet::after {
  content: '';
  position: absolute;
  inset: 0;
  background: linear-gradient(90deg, rgba(0,0,0,.02), rgba(0,0,0,.18) 82%, rgba(0,0,0,.3));
  opacity: 0;
  animation: pageShade .48s cubic-bezier(.22,.72,.22,1) forwards;
  pointer-events: none;
}

.book.single .turn-sheet { right: auto; left: 0; transform-origin: left center; }
.turn-sheet.prev { left: 0; right: auto; transform-origin: right center; animation-name: turnPrev; }
.turn-sheet img { width: 100%; height: 100%; object-fit: contain; backface-visibility: hidden; }

@keyframes turnNext { 0% { transform: rotateY(0) scaleX(1); } 48% { transform: rotateY(-88deg) scaleX(.985); } 100% { transform: rotateY(-180deg) scaleX(1); } }
@keyframes turnPrev { 0% { transform: rotateY(0) scaleX(1); } 48% { transform: rotateY(88deg) scaleX(.985); } 100% { transform: rotateY(180deg) scaleX(1); } }
@keyframes pageShade { 0%,100% { opacity: 0; } 48% { opacity: .72; } }

.catalog-loading {
  position: fixed;
  inset: var(--toolbar-h) 0 0;
  z-index: 100;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  background: linear-gradient(145deg, #0a192a, #07111e 58%, #040a12);
  color: #fff;
  transition: opacity .28s ease, visibility .28s ease;
}
.catalog-loading.is-complete { opacity: 0; visibility: hidden; }
.catalog-loading strong { font-size: 1rem; font-weight: 650; }
.catalog-loading span { color: #8ed2ff; font-variant-numeric: tabular-nums; }
.catalog-loading-spinner {
  width: 46px;
  height: 46px;
  border: 3px solid rgba(255,255,255,.16);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: catalogSpin .8s linear infinite;
}
@keyframes catalogSpin { to { transform: rotate(360deg); } }

.edge-zone { position: absolute; z-index: 5; top: 0; bottom: 0; width: 15%; padding: 0; border: 0; background: transparent; cursor: pointer; }
.edge-zone.left-edge { left: 0; }
.edge-zone.right-edge { right: 0; }
.book[data-magnified="true"] .edge-zone { display: none; }

.controls {
  position: relative;
  justify-self: center;
  margin: 0 0 max(12px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px;
  border-radius: 999px;
  background: rgba(4, 12, 21, .82);
  border: 1px solid rgba(255,255,255,.11);
  backdrop-filter: blur(14px);
  box-shadow: 0 10px 28px rgba(0,0,0,.3);
  z-index: 30;
}

.nav-button { width: 44px; height: 44px; border-radius: 50%; }
.nav-button:disabled { opacity: .28; cursor: default; }
.utility-button { text-decoration: none; flex-shrink: 0; }
.utility-button:has(.control-label) { width: auto; grid-auto-flow: column; gap: 8px; padding: 0 14px; border-radius: 24px; }
.control-label { font-size: .875rem; white-space: nowrap; }
.control-divider { height: 26px; width: 1px; background: rgba(255,255,255,.16); flex-shrink: 0; }
.page-status { min-width: 72px; text-align: center; font-size: .9rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.separator { padding: 0 8px; color: rgba(255,255,255,.4); }

.hint {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translate(-50%, 8px);
  padding: 7px 12px;
  border-radius: 8px;
  background: rgba(4,12,21,.72);
  color: rgba(255,255,255,.7);
  font-size: .75rem;
  opacity: 0;
  pointer-events: none;
  animation: hint 4s .8s both;
  z-index: 25;
}

@keyframes hint { 0%,100% { opacity: 0; transform: translate(-50%,8px); } 15%,75% { opacity: 1; transform: translate(-50%,0); } }

.share-dialog { width: min(480px, calc(100vw - 32px)); max-height: calc(100dvh - 40px); overflow: auto; margin: auto; padding: 24px; border: 1px solid rgba(255,255,255,.16); border-radius: 18px; background: #0b1d31; color: #fff; box-shadow: 0 28px 80px #0009; }
.share-dialog::backdrop { background: rgba(0,5,12,.72); backdrop-filter: blur(5px); }
.share-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.share-heading h2 { margin: 0; font-size: 1.2rem; }
.share-heading button { flex-shrink: 0; }
.share-dialog p { color: #b6c9da; font-size: 1rem; line-height: 1.5; }
.share-dialog label { display: block; font-size: .875rem; margin: 22px 0 8px; }
.copy-row { display: flex; gap: 8px; }
.copy-row input { min-width: 0; flex: 1; width: 100%; padding: 12px; border: 1px solid #486074; border-radius: 8px; background: #07111e; color: #fff; font: inherit; font-size: 1rem; }
.share-action { min-height: 44px; display: flex; align-items: center; justify-content: center; text-align: center; text-decoration: none; padding: 10px 14px; border: 1px solid #486074; border-radius: 8px; background: #10283e; color: #fff; font: inherit; font-size: .875rem; cursor: pointer; }
.share-action.primary { background: #0875b4; border-color: #32a9f4; }
.share-action:hover { background: #154060; }
.share-action:focus-visible, .copy-row input:focus-visible { outline: 3px solid var(--blue); outline-offset: 2px; }
.share-action[hidden] { display: none; }
.share-options { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 16px; }
.share-dialog .share-status { font-size: .875rem; min-height: 1.5em; margin-bottom: 0; }

@media (max-width: 760px) {
  :root { --toolbar-h: 56px; }
  .toolbar { padding: 0 12px; }
  .brand { gap: 8px; margin-right: 8px; }
  .brand-mark { font-size: 1.55rem; flex-shrink: 0; }
  .title { font-size: .72rem; letter-spacing: .04em; overflow: hidden; text-overflow: ellipsis; }
  .toolbar-actions { flex-shrink: 0; }
  .fullscreen, .zoom-value { display: none; }
  .icon-button { width: 34px; height: 34px; }
  .viewer { padding: 10px max(10px, env(safe-area-inset-right)) 10px max(10px, env(safe-area-inset-left)); }
  .controls { gap: 6px; max-width: calc(100vw - 12px); }
  .control-label { display: none; }
  .utility-button:has(.control-label) { width: 44px; padding: 0; border-radius: 50%; }
  .share-dialog { padding: 18px; }
  .share-options > * { flex: 1 1 100%; }
  .hint { width: max-content; max-width: calc(100% - 30px); text-align: center; bottom: 78px; }
}

/* A phone tap/double-tap is for the content, not an invisible page-turn button. */
@media (hover: none), (pointer: coarse) {
  .edge-zone { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}
