.flipbook-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.flipbook-stage {
  flex: 1;
  min-height: 0;
  overflow: visible;
  padding: 16px 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  position: relative;
  height: 100%;
}

.flipbook-meta {
  text-align: center;
  margin-bottom: 8px;
}

.flipbook-badge {
  display: inline-block;
  background: rgba(120, 120, 120, 0.45);
  color: #fff;
  font-size: 15px;
  border-radius: 4px;
  padding: 3px 10px;
  font-weight: 600;
}

.book-shell {
  width: 100%;
  height: auto;
  max-width: 1360px;
  max-height: 100%;
  margin: 0 auto;
  padding: 16px 20px;
  box-sizing: border-box;
  position: relative;
  perspective: 2400px;
  perspective-origin: 50% 52%;
  transform-style: preserve-3d;
  transform-origin: center center;
  transition: transform 360ms cubic-bezier(0.24, 0.64, 0.28, 1);
  will-change: transform;
  overflow: visible;
}

.book-stack {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
}

.book-stack .book-surface {
  position: absolute;
  inset: 0;
}

.book-stack .book-surface.top {
  z-index: 3;
  transform-style: preserve-3d;
}

.book-stack .book-surface.top.flipping-next .book-page.right {
  visibility: hidden;
}

.book-stack .book-surface.top.flipping-prev .book-page.left {
  visibility: hidden;
}

.book-stack .book-surface.underlay {
  z-index: 1;
  opacity: 0;
  pointer-events: none;
  transition: opacity 90ms linear;
  transform: translateZ(0px);
  /* Note: Keep at 0, not -80px. Negative depth causes layout recalc snapping during animation */
}

.book-stack .book-surface.underlay.active {
  opacity: 1;
}

.book-stack .book-surface.underlay.mask-left .book-page.left {
  visibility: hidden;
}

.book-stack .book-surface.underlay.mask-right .book-page.right {
  visibility: hidden;
}

.book-surface {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: 1fr;
  align-items: stretch;
  justify-items: center;
  position: relative;
  transform-style: preserve-3d;
  contain: layout;
}

.book-surface.paired {
  grid-template-columns: 1fr 1fr;
  gap: 0;
}

.book-surface.solo {
  grid-template-columns: 1fr 1fr;
}

.book-page {
  position: relative;
  width: 100%;
  height: 100%;
  background: #f4f4f4;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.16);
  transform: translateZ(0);
  backface-visibility: hidden;
}

.book-stack .book-surface.top.solo .book-page {
  transition: transform 280ms cubic-bezier(0.24, 0.64, 0.28, 1);
}

.book-stack .book-surface.top.solo.pre-shift-right .book-page.right {
  transform: translateZ(0) translateX(50%);
}

.book-stack .book-surface.top.solo.pre-shift-left .book-page.left {
  transform: translateZ(0) translateX(-50%);
}

.book-surface.solo .book-page.left,
.book-surface.solo .book-page.right {
  grid-column: 1 / span 2;
  width: 50%;
  justify-self: center;
  border: 0;
}

.book-surface.paired .book-page.left {
  border-right: 1px solid rgba(0, 0, 0, 0.06);
}

.book-surface.paired .book-page.right {
  border-left: 1px solid rgba(0, 0, 0, 0.06);
}

.page-image {
  width: 100%;
  height: 100%;
  background-repeat: no-repeat;
  background-color: #ececec;
}

.book-shell.commenting-enabled .book-page {
  cursor: crosshair;
}

.book-shell.commenting-enabled .book-page.comment-target-selected {
  outline: 3px solid #f59e0b;
  outline-offset: -3px;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, 0.28), 0 10px 24px rgba(0, 0, 0, 0.16);
}

.page-comment-hint {
  position: absolute;
  z-index: 43;
  transform: translate(-50%, 0);
  pointer-events: none;
  background: rgba(0, 0, 0, 0.72);
  color: #fff;
  font-size: 11px;
  line-height: 1.2;
  white-space: nowrap;
  border-radius: 4px;
  padding: 4px 8px;
}

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

.tag-badge {
  position: absolute;
  transform: translate(-50%, -50%);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: #ff1e1e;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.28);
  pointer-events: auto;
}

.tag-badge:hover,
.tag-badge:focus {
  background: #ff4b4b;
}

.tag-compose-anchor {
  position: absolute;
  z-index: 45;
}

.tag-compose-anchor .tag-badge {
  position: absolute;
}

.tag-overlay {
  position: absolute;
  z-index: 44;
  min-width: 340px;
  max-width: min(340px, calc(100vw - 48px));
  max-height: calc(100% - 24px);
  overflow: auto;
  transform: translate(16px, -50%);
  background: rgba(28, 28, 28, 0.88);
  color: #fff;
  border-radius: 10px;
  padding: 12px 12px 10px 12px;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.32);
  pointer-events: auto;
}

.tag-overlay.compose {
  transform: translate(18px, -50%);
}

.tag-overlay.view {
  background: rgba(28, 28, 28, 0.9);
}

.tag-overlay.compose {
  background: rgba(28, 28, 28, 0.9);
}

.tag-overlay-badge {
  position: absolute;
  left: -16px;
  top: -16px;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid #fff;
  background: #ff1e1e;
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 14px rgba(0, 0, 0, 0.28);
}

.tag-overlay-close {
  position: absolute;
  top: 4px;
  right: 8px;
  border: 0;
  background: transparent;
  color: #fff;
  font-size: 24px;
  line-height: 1;
  font-weight: 700;
}

.tag-overlay-body {
  padding-top: 26px;
}

.tag-overlay-thread {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tag-overlay-comments {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.tag-overlay-comment {
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.05);
}

.tag-overlay-comment-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.82);
}

.tag-overlay-comment-date {
  font-weight: 600;
}

.tag-overlay-comment-edit {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tag-overlay-textarea {
  resize: vertical;
  min-height: 80px;
}

.tag-overlay-text {
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.tag-overlay-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
  margin-top: 10px;
}

.book-surface.solo .page-image.single-page {
  background-size: cover !important;
  background-position: center center !important;
}

.book-surface.solo .page-image.left-half {
  background-size: 200% 100%;
  background-position: left center;
}

.book-surface.solo .page-image.right-half {
  background-size: 200% 100%;
  background-position: right center;
}

.book-surface.paired .page-image.left-half {
  background-size: 200% 100%;
  background-position: left center;
}

.book-surface.paired .page-image.right-half {
  background-size: 200% 100%;
  background-position: right center;
}

.book-surface.paired .page-image.single-page {
  background-size: cover;
  background-position: center center;
}

.page-image.filler-page {
  background-color: #fff;
  background-image: none !important;
}

.page-image.spread-front-cover {
  background-position: center right !important;
}

.page-image.spread-back-cover {
  background-position: center left !important;
}

.book-surface.solo .page-image.single-page.spread-front-cover {
  background-position: center right !important;
  background-size: cover !important;
}

.book-surface.solo .page-image.single-page.spread-back-cover {
  background-position: center left !important;
  background-size: cover !important;
}

.book-spine {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 100%;
  /* background: linear-gradient(to right, rgba(0, 0, 0, 0.12), rgba(255, 255, 255, 0.12), rgba(0, 0, 0, 0.1)); */
  pointer-events: none;
}

.flip-overlay {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  transform-style: preserve-3d;
  z-index: 40;
  will-change: transform;
  pointer-events: none;
  overflow: visible;
}

.flip-overlay.next {
  right: 0;
}

.flip-overlay.prev {
  left: 0;
}

.flip-overlay.from-solo.solo-right {
  left: 50%;
  right: auto;
}

.flip-overlay.from-solo.solo-left {
  left: 0;
  right: auto;
}

.flip-overlay.from-solo.solo-right .flip-sheet {
  transform-origin: 0% 50%;
}

.flip-overlay.from-solo.solo-left .flip-sheet {
  transform-origin: 100% 50%;
}

.flip-sheet {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  /* box-shadow: 0 16px 32px rgba(0, 0, 0, 0.28); */
  will-change: transform;
}

.flip-overlay.next .flip-sheet {
  animation: flip-next 360ms cubic-bezier(0.24, 0.64, 0.28, 1) both;
  transform-origin: 0% 50%;
}

.flip-overlay.prev .flip-sheet {
  animation: flip-prev 360ms cubic-bezier(0.24, 0.64, 0.28, 1) both;
  transform-origin: 100% 50%;
}

.flip-face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  background: #f4f4f4;
}

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

.flip-face .page-image {
  width: 100%;
  height: 100%;
}

.flip-shadow {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  will-change: transform, opacity;
}

.flip-overlay.next .flip-shadow {
  background: linear-gradient(to right, rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.06) 30%, rgba(255, 255, 255, 0) 70%);
  animation: shadow-next 360ms cubic-bezier(0.24, 0.64, 0.28, 1) both;
}

.flip-overlay.prev .flip-shadow {
  background: linear-gradient(to left, rgba(0, 0, 0, 0.18), rgba(0, 0, 0, 0.06) 30%, rgba(255, 255, 255, 0) 70%);
  animation: shadow-prev 360ms cubic-bezier(0.24, 0.64, 0.28, 1) both;
}

@keyframes flip-next {
  0% { transform: translateZ(3px) rotateY(-8deg); }
  15% { transform: translateZ(7px) rotateY(-25deg); }
  25% { transform: translateZ(10px) rotateY(-40deg); }
  34% { transform: translateZ(14px) rotateY(-58deg); }
  40% { transform: translateZ(18px) rotateY(-75deg); }
  52% { transform: translateZ(22px) rotateY(-90deg); }
  70% { transform: translateZ(12px) rotateY(-145deg); }
  100% { transform: translateZ(0px) rotateY(-180deg); }

}

@keyframes flip-prev {
  0% { transform: translateZ(3px) rotateY(8deg); }
  15% { transform: translateZ(7px) rotateY(25deg); }
  25% { transform: translateZ(10px) rotateY(40deg); }
  34% { transform: translateZ(14px) rotateY(58deg); }
  40% { transform: translateZ(18px) rotateY(75deg); }
  52% { transform: translateZ(22px) rotateY(90deg); }
  70% { transform: translateZ(12px) rotateY(145deg); }
  100% { transform: translateZ(0px) rotateY(180deg); }
}

@keyframes shadow-next {
  0% { opacity: 0; transform: translateX(0); }
  48% { opacity: 0.28; transform: translateX(8px); }
  100% { opacity: 0; transform: translateX(20px); }
}

@keyframes shadow-prev {
  0% { opacity: 0; transform: translateX(0); }
  48% { opacity: 0.28; transform: translateX(-8px); }
  100% { opacity: 0; transform: translateX(-20px); }
}

.flip-tab {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 4px;
  background: rgba(165, 165, 165, 0.85);
  color: #fff;
  font-size: 24px;
  line-height: 1;
  z-index: 30;
}

.flip-tab.left {
  left: 8px;
}

.flip-tab.right {
  right: 8px;
}

.flip-tab:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.flipbook-empty,
.flipbook-loading {
  text-align: center;
  color: #555;
  padding: 80px 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 1;
}

.flipbook-loader {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.flipbook-loader-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid rgba(100, 130, 160, 0.2);
  border-top-color: #6b8fb5;
  border-radius: 50%;
  animation: loader-spin 0.75s linear infinite;
}

@keyframes loader-spin {
  to { transform: rotate(360deg); }
}

.flipbook-loader-text {
  color: #6b7a8d;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.03em;
}

@media (max-width: 900px) {
  .flipbook-stage {
    padding: 10px 8px 10px;
  }

  .flip-tab {
    width: 40px;
    height: 40px;
  }

  .flip-tab.left {
    left: 4px;
  }

  .flip-tab.right {
    right: 4px;
  }
}
