/* Flipbook Modal */
.flipbook-modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(8px);
  z-index: 10000;
  justify-content: center;
  align-items: center;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.flipbook-modal.active {
  display: flex;
  opacity: 1;
}

.flipbook-close {
  position: absolute;
  top: 20px;
  right: 20px;
  background: transparent;
  border: none;
  color: #333;
  font-size: 30px;
  cursor: pointer;
  z-index: 10100;
  transition: transform 0.2s;
}

.flipbook-close:hover {
  transform: scale(1.1);
}

/* Book Layout */
.book-wrapper {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 1000px;
  height: 650px;
  perspective: 2000px;
}

.book {
  position: absolute;
  left: 50%;
  width: 450px; /* Width of one page */
  height: 600px; /* Height of page */
  transition: transform 0.5s;
  transform-style: preserve-3d;
  /* Centering the book spine when open, or shifting it when closed */
  transform: translateX(-50%); /* Closed cover: shift left by half page width to center the cover */
}

/* Adjust transform depending on current page */
.book.state-middle {
  transform: translateX(0); /* Open: spine is at center */
}

.book.state-end {
  transform: translateX(50%); /* Closed back cover: shift right by half page width to center the back cover */
}

.paper {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  perspective: 2000px;
  transform-style: preserve-3d;
  transform-origin: left center;
  transition: transform 0.6s cubic-bezier(0.645, 0.045, 0.355, 1);
  z-index: 1;
}

.paper.flipped {
  transform: rotateY(-180deg);
}

.front, .back {
  position: absolute;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  backface-visibility: hidden;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}

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

.front img, .back img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

/* Control Buttons */
.flip-control-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(0, 0, 0, 0.15);
  border: 1px solid rgba(0, 0, 0, 0.2);
  color: #000;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
  z-index: 10050;
  transition: all 0.3s ease;
}

.flip-control-btn:hover {
  background: rgba(255, 255, 255, 0.3);
  transform: translateY(-50%) scale(1.05);
}

.flip-control-btn.prev-btn {
  left: 30px;
}

.flip-control-btn.next-btn {
  right: 30px;
}

.flip-control-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

/* Instruction indicator */
.flipbook-tip {
  color: rgba(0, 0, 0, 0.6);
  font-family: 'Poppins', sans-serif;
  font-size: 13px;
  margin-top: 20px;
  letter-spacing: 1px;
}

/* Mobile responsive styles */
@media (max-width: 991px) and (min-width: 768px) {
  .book-wrapper {
    transform: scale(0.8);
    transform-origin: center center;
  }
  .flip-control-btn.prev-btn {
    left: 15px;
  }
  .flip-control-btn.next-btn {
    right: 15px;
  }
}

@media (max-width: 767px) {
  /* Reset book wrapper and perspective */
  .book-wrapper {
    perspective: none;
    height: 80vh;
    max-height: 500px;
    width: 90%;
    transform: none !important;
  }

  .book {
    position: relative;
    left: 0;
    width: 100%;
    height: 100%;
    transform: none !important;
    transform-style: flat;
  }

  .paper {
    position: absolute;
    width: 100%;
    height: 100%;
    transform: none !important;
    transform-style: flat;
    display: none; /* Hidden by default on mobile */
    z-index: 1 !important;
  }

  /* When active on mobile, we display the paper */
  .paper.mobile-active {
    display: block;
  }

  .front, .back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: visible;
    transform: none !important;
    display: none;
  }

  /* Toggle front/back display on mobile active paper */
  .paper.mobile-active-front .front {
    display: block;
  }

  .paper.mobile-active-back .back {
    display: block;
  }

  .front img, .back img {
    object-fit: contain;
    box-shadow: none;
  }
  
  .flipbook-tip {
    margin-top: 10px;
  }

  .flip-control-btn {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }
  .flip-control-btn.prev-btn {
    left: 10px;
  }
  .flip-control-btn.next-btn {
    right: 10px;
  }
}

/* Landscape Book Styles Override (Desktop only) */
@media (min-width: 768px) {
  .landscape-book .book-wrapper {
    max-width: 1500px;
    height: 700px;
  }
  .landscape-book .book {
    width: 680px;
    height: 480px;
  }
}
