* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: #111827;
  color: #f3f4f6;
  font-family: Arial, Helvetica, sans-serif;
}

body {
  min-height: 100vh;
}

.viewer-shell {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.viewer-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 24px;
  padding: 18px 24px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(17, 24, 39, 0.92);
  backdrop-filter: blur(8px);
}

.viewer-heading h1 {
  margin: 0 0 6px;
  font-size: 28px;
  line-height: 1.1;
}

.viewer-heading p {
  margin: 0;
  color: rgba(243, 244, 246, 0.72);
  font-size: 14px;
}

.viewer-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.viewer-toolbar button {
  border: 0;
  border-radius: 10px;
  padding: 10px 14px;
  background: #2563eb;
  color: #fff;
  cursor: pointer;
  font-size: 14px;
  transition: transform 0.18s ease, opacity 0.18s ease, background 0.18s ease;
}

.viewer-toolbar button:hover:not(:disabled) {
  transform: translateY(-1px);
  background: #1d4ed8;
}

.viewer-toolbar button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#pageInfo {
  min-width: 90px;
  text-align: center;
  font-weight: 700;
  color: #e5e7eb;
}

.viewer-main {
  flex: 1;
  display: grid;
  grid-template-columns: 72px 1fr 72px;
  align-items: stretch;
  min-height: 0;
}

.book-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 28px;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.book {
  width: min(1200px, 100%);
  height: min(78vh, 900px);
  position: relative;
  overflow: hidden;
}

.click-zone {
  border: 0;
  margin: 0;
  padding: 0;
  background: transparent;
  cursor: pointer;
  transition: background 0.2s ease;
}

.click-zone:hover {
  background: rgba(255, 255, 255, 0.03);
}

.book-perspective {
  perspective: 2200px;
  perspective-origin: center center;
  width: 100%;
  height: 100%;
}

.spread {
  display: flex;
  width: 100%;
  height: 100%;
  gap: 0;
}

.page {
  position: relative;
  flex: 1 1 50%;
  height: 100%;
  overflow: hidden;
  background: transparent;
  padding: 6px;
}

.page.empty {
  visibility: hidden;
}

.page-sheet {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition:
    transform 1.8s cubic-bezier(0.22, 0.61, 0.36, 1),
    filter 1.8s ease,
    box-shadow 1.8s ease;
  will-change: transform;
}

.page.left .page-sheet {
  transform-origin: right center;
}

.page.right .page-sheet {
  transform-origin: left center;
}

.page-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  border-radius: 8px;
  overflow: hidden;
  background: #fff;
  box-shadow:
    0 10px 30px rgba(0, 0, 0, 0.25),
    inset 0 0 0 1px rgba(0, 0, 0, 0.06);
}

.page-face.front {
  z-index: 2;
}

.page-face.back {
  transform: rotateY(180deg);
  background:
    linear-gradient(
      to right,
      rgba(0, 0, 0, 0.10) 0%,
      rgba(255, 255, 255, 0.92) 12%,
      rgba(250, 250, 250, 1) 100%
    );
}

.zoom-surface {
  width: 100%;
  height: 100%;
  transform-origin: center center;
  transition: transform 0.25s ease;
  will-change: transform;
  cursor: zoom-in;
  touch-action: none;
}

.page img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #fff;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
}

.page-number {
  position: absolute;
  bottom: 12px;
  right: 16px;
  padding: 4px 8px;
  font-size: 13px;
  color: rgba(0, 0, 0, 0.65);
  background: rgba(255, 255, 255, 0.88);
  border-radius: 999px;
  z-index: 5;
}

.flip-anim-right {
  z-index: 30;
}

.flip-anim-right .page-sheet {
  animation: pageFlipRight 1.8s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.flip-anim-left {
  z-index: 30;
}

.flip-anim-left .page-sheet {
  animation: pageFlipLeft 1.8s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes pageFlipRight {
  0% {
    transform: rotateY(0deg);
    filter: brightness(1);
  }
  20% {
    transform: rotateY(-18deg);
    filter: brightness(0.98);
  }
  55% {
    transform: rotateY(-88deg);
    filter: brightness(0.92);
  }
  100% {
    transform: rotateY(-180deg);
    filter: brightness(0.96);
  }
}

@keyframes pageFlipLeft {
  0% {
    transform: rotateY(0deg);
    filter: brightness(1);
  }
  20% {
    transform: rotateY(18deg);
    filter: brightness(0.98);
  }
  55% {
    transform: rotateY(88deg);
    filter: brightness(0.92);
  }
  100% {
    transform: rotateY(180deg);
    filter: brightness(0.96);
  }
}

#book.zoomed .zoom-surface {
  cursor: grab;
}

#book.zoomed .zoom-surface:active {
  cursor: grabbing;
}

#book.zoomed .page-sheet {
  animation: none !important;
}

#book.zoomed .click-zone {
  pointer-events: none;
}

#book.single .page {
  flex-basis: 100%;
  max-width: 100%;
}

.empty-state {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 32px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.empty-state h2 {
  margin-top: 0;
}

.empty-state code {
  background: rgba(255, 255, 255, 0.08);
  padding: 2px 6px;
  border-radius: 6px;
}

#downloadBtn {
  background: #059669;
}

#downloadBtn:hover:not(:disabled) {
  background: #047857;
}

.viewer-footer {
  text-align: center;
  padding: 12px 20px;
  font-size: 13px;
  color: rgba(255,255,255,0.6);
  border-top: 1px solid rgba(255,255,255,0.08);
  background: rgba(17,24,39,0.9);
}

@media (max-width: 900px) {
  .viewer-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .viewer-main {
    grid-template-columns: 16px 1fr 16px;
  }

  .book-wrap {
    padding: 12px;
  }

  .book {
    height: min(72vh, 760px);
  }

  .page {
    padding: 0;
  }
}

/* Mobil 3D lapozás: egy oldal férjen ki teljesen, vágás nélkül. */
@media (max-width: 900px) {
  .viewer-shell {
    min-height: 100dvh;
  }

  .viewer-header {
    position: relative;
    padding: 10px 12px;
    gap: 10px;
  }

  .viewer-heading h1 {
    font-size: 19px;
  }

  .viewer-heading p {
    display: none;
  }

  .viewer-toolbar {
    width: 100%;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    gap: 7px;
  }

  .viewer-toolbar button {
    padding: 9px 8px;
    font-size: 12px;
    border-radius: 9px;
  }

  #prevBtn,
  #nextBtn {
    display: inline-block;
  }

  #zoomBtn,
  #fsBtn,
  #downloadBtn {
    display: none;
  }

  #pageInfo {
    min-width: 58px;
    align-self: center;
    font-size: 13px;
  }

  .viewer-main {
    grid-template-columns: 0 1fr 0;
    min-height: 0;
    flex: 1;
  }

  .click-zone {
    display: none;
  }

  .book-wrap {
    padding: 10px;
    overflow: visible;
    align-items: center;
  }

  .book {
    width: min(100%, calc((100dvh - 126px) * 0.7048));
    height: auto;
    aspect-ratio: 874 / 1240;
    max-height: calc(100dvh - 126px);
    overflow: visible;
    margin: 0 auto;
  }

  .spread.single-spread {
    width: 100%;
    height: 100%;
    display: block;
  }

  .single-spread .page {
    width: 100%;
    height: 100%;
    max-width: 100%;
    padding: 0;
    overflow: visible;
    perspective: 1800px;
  }

  .single-spread .page-sheet {
    width: 100%;
    height: 100%;
    transform-origin: left center;
  }

  .single-spread .page-face {
    border-radius: 9px;
  }


  /* Mobil Chrome javítás: a céloldalt nem forgatjuk át 180 fokra, mert egyes telefonokon a hátlap marad látható, ezért üresnek tűnik az oldal. */
  .single-spread .page-face.back {
    display: none;
  }

  .single-spread .page-sheet {
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
  }

  .page img {
    width: 100%;
    height: 100%;
    object-fit: contain;
  }

  .page-number {
    bottom: 8px;
    right: 8px;
    font-size: 11px;
  }

  .viewer-footer {
    display: block;
  }
}

.flip-anim-mobile-next {
  z-index: 30;
}

.flip-anim-mobile-next .page-sheet {
  animation: pageFlipMobileNext 0.95s cubic-bezier(0.22, 0.61, 0.36, 1);
}

.flip-anim-mobile-prev {
  z-index: 30;
}

.flip-anim-mobile-prev .page-sheet {
  animation: pageFlipMobilePrev 0.95s cubic-bezier(0.22, 0.61, 0.36, 1);
}

@keyframes pageFlipMobileNext {
  0% { transform: rotateY(58deg) translateX(18px) scale(0.985); opacity: 0.72; filter: brightness(0.90); }
  55% { transform: rotateY(-10deg) translateX(-3px) scale(1); opacity: 1; filter: brightness(0.98); }
  100% { transform: rotateY(0deg) translateX(0) scale(1); opacity: 1; filter: brightness(1); }
}

@keyframes pageFlipMobilePrev {
  0% { transform: rotateY(-58deg) translateX(-18px) scale(0.985); opacity: 0.72; filter: brightness(0.90); }
  55% { transform: rotateY(10deg) translateX(3px) scale(1); opacity: 1; filter: brightness(0.98); }
  100% { transform: rotateY(0deg) translateX(0) scale(1); opacity: 1; filter: brightness(1); }
}

/* === MOBIL STABIL 3D FIX v4 ===
   A korábbi mobil hibát az okozhatta, hogy Chrome alatt a 3D/backface/absolute rétegezés
   bizonyos oldalakon üresen maradt. Mobilon ezért a tartalmat mindig valódi, látható
   előlapi rétegként tartjuk meg, a 3D hatást pedig csak az egész lap finom animációja adja. */
@media (max-width: 900px) {
  html,
  body {
    width: 100%;
    min-height: 100dvh;
    overflow-x: hidden;
  }

  .viewer-shell {
    min-height: 100dvh;
  }

  .viewer-main {
    display: grid;
    grid-template-columns: 0 1fr 0;
    flex: 1 1 auto;
    min-height: 0;
  }

  .book-wrap {
    width: 100%;
    min-height: 0;
    padding: 8px 10px 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: visible;
  }

  .book {
    width: min(calc(100vw - 20px), calc((100dvh - 128px) * 0.70484));
    height: auto;
    aspect-ratio: 874 / 1240;
    max-width: calc(100vw - 20px);
    max-height: calc(100dvh - 128px);
    overflow: visible;
    position: relative;
    margin: 0 auto;
    contain: layout paint;
  }

  .book-perspective {
    width: 100%;
    height: 100%;
    perspective: 1400px;
    perspective-origin: center center;
  }

  .spread.single-spread {
    display: block !important;
    width: 100%;
    height: 100%;
    position: relative;
    overflow: visible;
  }

  .single-spread .page {
    display: block !important;
    position: relative !important;
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    flex: none !important;
    padding: 0 !important;
    overflow: visible !important;
    visibility: visible !important;
    opacity: 1 !important;
  }

  .single-spread .page-sheet {
    display: block !important;
    position: relative !important;
    width: 100% !important;
    height: 100% !important;
    transform-style: flat !important;
    -webkit-transform-style: flat !important;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    will-change: transform, opacity, filter;
  }

  .single-spread .page-face.front {
    display: block !important;
    position: relative !important;
    inset: auto !important;
    width: 100% !important;
    height: 100% !important;
    z-index: 5 !important;
    opacity: 1 !important;
    visibility: visible !important;
    transform: none !important;
    -webkit-transform: none !important;
    backface-visibility: visible !important;
    -webkit-backface-visibility: visible !important;
    overflow: hidden !important;
    background: #fff !important;
    border-radius: 9px;
  }

  .single-spread .page-face.back {
    display: none !important;
  }

  .single-spread .zoom-surface {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    opacity: 1 !important;
    visibility: visible !important;
    transform-origin: center center;
  }

  .single-spread .page img {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    max-width: 100% !important;
    max-height: 100% !important;
    object-fit: contain !important;
    background: #fff !important;
    opacity: 1 !important;
    visibility: visible !important;
  }

  .flip-anim-mobile-next .page-sheet {
    animation: pageFlipMobileNextStable 0.72s ease-out both !important;
  }

  .flip-anim-mobile-prev .page-sheet {
    animation: pageFlipMobilePrevStable 0.72s ease-out both !important;
  }
}

@keyframes pageFlipMobileNextStable {
  0% { transform: perspective(1400px) rotateY(46deg) translateX(18px) scale(0.985); opacity: .70; filter: brightness(.92); }
  60% { transform: perspective(1400px) rotateY(-5deg) translateX(-2px) scale(1); opacity: 1; filter: brightness(.98); }
  100% { transform: perspective(1400px) rotateY(0deg) translateX(0) scale(1); opacity: 1; filter: brightness(1); }
}

@keyframes pageFlipMobilePrevStable {
  0% { transform: perspective(1400px) rotateY(-46deg) translateX(-18px) scale(0.985); opacity: .70; filter: brightness(.92); }
  60% { transform: perspective(1400px) rotateY(5deg) translateX(2px) scale(1); opacity: 1; filter: brightness(.98); }
  100% { transform: perspective(1400px) rotateY(0deg) translateX(0) scale(1); opacity: 1; filter: brightness(1); }
}


/* === MOBIL 900px ALATT: STABIL KÉPMEGJELENÍTÉS ===
   A mobilos Chrome/Samsung böngészőknél a 3D-s backface + absolute réteg
   néha 0 magasságú/üres réteget eredményezett. Mobilon ezért a látható oldalt
   normál, stabil rétegként rajzoljuk ki, csak a lapozási animáció marad 3D jellegű. */
@media (max-width: 900px) {
  html,
  body {
    width: 100%;
    min-height: 100%;
    overflow-x: hidden;
  }

  .viewer-shell {
    min-height: 100vh;
    min-height: 100svh;
  }

  .viewer-header {
    flex: 0 0 auto;
  }

  .viewer-main {
    flex: 1 1 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 0;
    width: 100%;
  }

  .book-wrap {
    width: 100%;
    min-height: 0;
    padding: 8px 10px;
    overflow: visible;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .book {
    /* 874×1240 arány: teljes oldal férjen ki a kijelzőn */
    width: min(calc(100vw - 20px), calc((100svh - 118px) * 0.7048));
    height: min(calc((100vw - 20px) * 1.4188), calc(100svh - 118px));
    min-width: 260px;
    min-height: 360px;
    max-width: calc(100vw - 20px);
    max-height: calc(100svh - 118px);
    aspect-ratio: auto;
    overflow: visible;
    position: relative;
    margin: 0 auto;
  }

  @supports (height: 100dvh) {
    .book {
      width: min(calc(100vw - 20px), calc((100dvh - 118px) * 0.7048));
      height: min(calc((100vw - 20px) * 1.4188), calc(100dvh - 118px));
      max-height: calc(100dvh - 118px);
    }
  }

  .spread.single-spread,
  .single-spread .page,
  .single-spread .page-sheet,
  .single-spread .page-face.front,
  .single-spread .zoom-surface {
    width: 100% !important;
    height: 100% !important;
    min-height: 100% !important;
  }

  .spread.single-spread {
    display: block;
    position: relative;
    perspective: 1400px;
  }

  .single-spread .page {
    display: block;
    position: relative;
    padding: 0;
    overflow: visible;
    flex: none;
  }

  .single-spread .page-sheet {
    display: block;
    position: relative;
    transform-style: flat;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
  }

  .single-spread .page-face.front {
    display: block;
    position: relative;
    inset: auto;
    z-index: 5;
    overflow: hidden;
    background: #fff;
    backface-visibility: visible;
    -webkit-backface-visibility: visible;
  }

  .single-spread .page-face.back {
    display: none !important;
  }

  .single-spread .zoom-surface {
    display: block;
    position: relative;
    touch-action: pan-y;
  }

  .single-spread img {
    display: block !important;
    width: 100% !important;
    height: 100% !important;
    object-fit: contain !important;
    background: #fff;
    opacity: 1 !important;
    visibility: visible !important;
  }

  .flip-anim-mobile-next .page-sheet {
    animation: pageFlipMobileNextStable 0.55s ease-out;
  }

  .flip-anim-mobile-prev .page-sheet {
    animation: pageFlipMobilePrevStable 0.55s ease-out;
  }
}

@keyframes pageFlipMobileNextStable {
  0% { transform: perspective(1200px) rotateY(18deg) translateX(10px) scale(0.985); opacity: 0.88; }
  100% { transform: perspective(1200px) rotateY(0deg) translateX(0) scale(1); opacity: 1; }
}

@keyframes pageFlipMobilePrevStable {
  0% { transform: perspective(1200px) rotateY(-18deg) translateX(-10px) scale(0.985); opacity: 0.88; }
  100% { transform: perspective(1200px) rotateY(0deg) translateX(0) scale(1); opacity: 1; }
}

/* === MOBIL PDF LETÖLTÉS GOMB FIX ===
   900px alatt eddig a #downloadBtn is el volt rejtve. Mobilon most csak
   a Nagyítás és Teljes képernyő gombokat rejtjük el, a PDF letöltés marad. */
@media (max-width: 900px) {
  .viewer-toolbar {
    width: 100%;
    display: grid !important;
    grid-template-columns: 1fr auto 1fr;
    gap: 7px;
  }

  #zoomBtn,
  #fsBtn {
    display: none !important;
  }

  #downloadBtn {
    display: block !important;
    grid-column: 1 / -1;
    width: 100%;
    padding: 9px 8px;
    font-size: 12px;
    border-radius: 9px;
    background: #059669;
  }

  #downloadBtn:hover:not(:disabled) {
    background: #047857;
  }

  /* A plusz letöltés gomb miatt kicsit kevesebb hely marad a lapnak. */
  .book {
    width: min(calc(100vw - 20px), calc((100svh - 158px) * 0.7048));
    height: min(calc((100vw - 20px) * 1.4188), calc(100svh - 158px));
    max-height: calc(100svh - 158px);
  }

  @supports (height: 100dvh) {
    .book {
      width: min(calc(100vw - 20px), calc((100dvh - 158px) * 0.7048));
      height: min(calc((100vw - 20px) * 1.4188), calc(100dvh - 158px));
      max-height: calc(100dvh - 158px);
    }
  }
}
