:root {
  --bg: #fff;
  --panel: rgba(255, 255, 255, 0.96);
  --text: #111;
  --muted: #666;
  --page: #fff;
  --logo-width: clamp(120px, 18vw, 250px);
  --logo-height: calc(var(--logo-width) * 74 / 250);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
}

.app-shell {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 18px;
  padding: 14px 18px;
  background: var(--panel);
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.brand {
  display: flex;
  align-items: center;
  min-width: 0;
}

.logo-slot {
  position: relative;
  width: var(--logo-width);
  height: var(--logo-height);
  aspect-ratio: 250 / 74;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.brand-logo {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.logo-fallback {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  border: 1px dashed rgba(0, 0, 0, 0.22);
  border-radius: 10px;
  font-weight: 700;
  font-size: clamp(10px, 1.2vw, 14px);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  background: linear-gradient(180deg, rgba(0,0,0,.02), rgba(0,0,0,.01));
}

.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

button,
.download-btn,
.goto-wrap input {
  height: 38px;
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 999px;
  background: #fff;
  color: var(--text);
  font-size: 15px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.04);
  transition: transform .15s ease, background .2s ease, border-color .2s ease, box-shadow .2s ease;
}

button,
.download-btn {
  min-width: 38px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  cursor: pointer;
}

button:hover,
.download-btn:hover {
  transform: translateY(-1px);
  background: #f7f7f7;
}

button:active,
.download-btn:active {
  transform: translateY(0);
}

button:disabled {
  opacity: .4;
  cursor: default;
  transform: none;
}

.divider {
  width: 1px;
  height: 22px;
  background: rgba(0, 0, 0, 0.12);
}

.download-btn {
  justify-self: end;
  white-space: nowrap;
}

.goto-wrap {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.goto-label {
  font-size: 14px;
  color: var(--muted);
  white-space: nowrap;
}

.goto-wrap input {
  width: 78px;
  padding: 0 12px;
  text-align: center;
  outline: none;
}

.goto-wrap input:focus {
  border-color: rgba(0, 0, 0, 0.3);
  box-shadow: 0 0 0 3px rgba(0, 0, 0, 0.06);
}

.viewer-wrap {
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 12px;
}

.viewport {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  overflow: hidden;
  touch-action: pan-y;
}

.book-stage {
  position: relative;
  display: grid;
  place-items: center;
  perspective: 2600px;
  transform-origin: center center;
  transition: width .2s ease, height .2s ease;
}

.ambient-shadow {
  position: absolute;
  inset: 3% 4% 1%;
  background: radial-gradient(ellipse at center, rgba(0,0,0,.14), transparent 68%);
  filter: blur(28px);
  pointer-events: none;
}

.book-content {
  position: relative;
  width: 100%;
  height: 100%;
  transform-origin: center center;
  transition: transform .16s ease;
}

.spread-view {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
}

.page-surface {
  position: relative;
  height: 100%;
  overflow: hidden;
  background: var(--page);
  box-shadow: 0 18px 40px rgba(0,0,0,.12), 0 6px 14px rgba(0,0,0,.08);
}

.left-page { border-radius: 8px 2px 2px 8px; }
.right-page { border-radius: 2px 8px 8px 2px; }

.page-surface img,
.flip-face img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: contain;
  background: #fff;
}

.page-gloss {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(105deg, rgba(255,255,255,.12) 0%, rgba(255,255,255,.03) 18%, transparent 28%),
    linear-gradient(90deg, rgba(0,0,0,.02), transparent 20%, transparent 80%, rgba(0,0,0,.04));
  pointer-events: none;
}

.gutter-shadow {
  position: absolute;
  left: 50%;
  top: 0;
  width: 18px;
  height: 100%;
  transform: translateX(-50%);
  background: linear-gradient(90deg, rgba(0,0,0,.14), rgba(255,255,255,.05), rgba(0,0,0,.12));
  filter: blur(4px);
  pointer-events: none;
  z-index: 3;
}

.flip-layer {
  position: absolute;
  inset: 0;
  z-index: 20;
  pointer-events: none;
  transform-style: preserve-3d;
}

.hidden { display: none !important; }

.flip-sheet {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  transform-style: preserve-3d;
}

.flip-sheet.next {
  left: 50%;
  transform-origin: left center;
  animation: flipNext .85s cubic-bezier(.22,.61,.36,1) forwards;
}

.flip-sheet.prev {
  left: 0;
  transform-origin: right center;
  animation: flipPrev .85s cubic-bezier(.22,.61,.36,1) forwards;
}

.flip-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  backface-visibility: hidden;
  background: var(--page);
  box-shadow: 0 16px 34px rgba(0,0,0,.16);
}

.flip-face::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.flip-sheet.next .flip-face.front::after {
  background: linear-gradient(90deg, rgba(0,0,0,.16), transparent 28%, rgba(255,255,255,.08));
}

.flip-sheet.prev .flip-face.front::after {
  background: linear-gradient(270deg, rgba(0,0,0,.16), transparent 28%, rgba(255,255,255,.08));
}

.flip-face.back {
  transform: rotateY(180deg);
}

.flip-sheet.next .flip-face.back::after {
  background: linear-gradient(90deg, rgba(255,255,255,.1), transparent 26%, rgba(0,0,0,.12));
}

.flip-sheet.prev .flip-face.back::after {
  background: linear-gradient(270deg, rgba(255,255,255,.1), transparent 26%, rgba(0,0,0,.12));
}

@keyframes flipNext {
  0% { transform: rotateY(0deg); }
  50% { box-shadow: -10px 10px 30px rgba(0,0,0,.18); }
  100% { transform: rotateY(-180deg); }
}

@keyframes flipPrev {
  0% { transform: rotateY(0deg); }
  50% { box-shadow: 10px 10px 30px rgba(0,0,0,.18); }
  100% { transform: rotateY(180deg); }
}

.zoomed .viewport { cursor: grab; }
.dragging .viewport { cursor: grabbing; }

@media (max-width: 1180px) {
  .topbar {
    grid-template-columns: 1fr;
    justify-items: center;
  }

  .brand,
  .download-btn {
    justify-self: center;
  }

  .logo-slot {
    width: clamp(120px, 42vw, 220px);
    height: auto;
  }
}