/*
 * RAYAN LOGISTICS — 3D Interactive Flipbook Physics & Layout
 */

/* Main Fullscreen Stage */
.rayan-book-stage {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 2600px;
  background: 
    radial-gradient(circle at 50% 30%, rgba(255, 255, 255, 0.7) 0%, rgba(247, 246, 241, 0.95) 70%, rgba(236, 237, 233, 1) 100%);
  overflow: hidden;
  touch-action: pan-y;
}

/* Wood/Desk ambient shadow beneath the book */
.rayan-book-stage::after {
  content: "";
  position: absolute;
  bottom: calc(50% - (var(--page-height) / 2) - 25px);
  width: calc(var(--page-width) * 2 + 60px);
  height: 45px;
  background: radial-gradient(ellipse at 50% 50%, rgba(24, 44, 62, 0.22) 0%, rgba(24, 44, 62, 0.05) 50%, transparent 75%);
  filter: blur(12px);
  z-index: 1;
  pointer-events: none;
  transition: transform 0.6s cubic-bezier(0.2, 0.8, 0.2, 1), width 0.6s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.rayan-book-stage.mode-single::after {
  width: calc(var(--page-width) + 40px);
}

/* Main Book Container */
.rayan-book {
  position: relative;
  width: calc(var(--page-width) * 2);
  height: var(--page-height);
  transform-style: preserve-3d;
  transition: transform 0.65s cubic-bezier(0.2, 0.8, 0.2, 1);
  z-index: 10;
  cursor: grab;
}

.rayan-book.is-dragging {
  cursor: grabbing;
}

/* Desktop Closed Cover View Centering */
/* When closed at front cover, center the right page in the screen */
.rayan-book.is-cover-view {
  transform: translateX(-25%);
}

.rayan-book-stage:has(.rayan-book.is-cover-view)::after {
  transform: translateX(-25%);
  width: calc(var(--page-width) + 60px);
}

/* When closed at back cover, center the left page in the screen */
.rayan-book.is-back-cover-view {
  transform: translateX(25%);
}

.rayan-book-stage:has(.rayan-book.is-back-cover-view)::after {
  transform: translateX(25%);
  width: calc(var(--page-width) + 60px);
}

/* Open spread view centered */
.rayan-book.is-spread-view {
  transform: translateX(0);
}

/* Leaves Container holds all 3D turning sheets */
.rayan-leaves-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
}

/* Realistic Central Spine */
.rayan-spine {
  position: absolute;
  left: calc(50% - 4px);
  top: -2px;
  width: 8px;
  height: calc(100% + 4px);
  background: linear-gradient(
    to right,
    rgba(24, 44, 62, 0.25) 0%,
    rgba(255, 255, 255, 0.35) 45%,
    rgba(24, 44, 62, 0.3) 55%,
    rgba(24, 44, 62, 0.1) 100%
  );
  z-index: 45;
  pointer-events: none;
  box-shadow: 0 0 10px rgba(24, 44, 62, 0.15);
  border-radius: 2px;
}

/* Gutter shadow overlay on left and right pages near spine */
.rayan-spine::before {
  content: "";
  position: absolute;
  top: 0;
  right: 100%;
  width: 32px;
  height: 100%;
  background: linear-gradient(to left, rgba(24, 44, 62, 0.14) 0%, rgba(24, 44, 62, 0.04) 50%, transparent 100%);
}

.rayan-spine::after {
  content: "";
  position: absolute;
  top: 0;
  left: 100%;
  width: 32px;
  height: 100%;
  background: linear-gradient(to right, rgba(24, 44, 62, 0.14) 0%, rgba(24, 44, 62, 0.04) 50%, transparent 100%);
}

/* Silk Ribbon Bookmark Marker hanging from spine */
.spine-ribbon {
  position: absolute;
  top: 0;
  left: 1px;
  width: 6px;
  height: 140px;
  background: linear-gradient(to bottom, #648C87 0%, #4B6E6A 75%, #385552 100%);
  border-radius: 0 0 2px 2px;
  box-shadow: 2px 5px 8px rgba(0, 0, 0, 0.22);
  transform-origin: top center;
  transform: rotate(4deg);
  pointer-events: none;
  z-index: 46;
  transition: transform 0.5s ease;
}

.spine-ribbon::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  width: 0;
  height: 0;
  border-left: 3px solid transparent;
  border-right: 3px solid transparent;
  border-bottom: 6px solid var(--color-bg-ivory);
}

/* Stacking Thickness Simulation on Outer Edges */
.book-stack {
  position: absolute;
  top: 4px;
  bottom: 4px;
  background: repeating-linear-gradient(
    to bottom,
    #FFFFFF 0px,
    #FFFFFF 2px,
    #DADDD8 2px,
    #DADDD8 3px
  );
  box-shadow: inset 0 0 4px rgba(24, 44, 62, 0.15);
  border-radius: 2px;
  z-index: 5;
  transition: width 0.3s ease, opacity 0.3s ease;
  pointer-events: none;
}

.stack-left {
  left: -8px;
  width: 8px;
  border-left: 1px solid #C4C7C1;
  transform: skewY(1deg);
}

.stack-right {
  right: -8px;
  width: 8px;
  border-right: 1px solid #C4C7C1;
  transform: skewY(-1deg);
}

/* 3D Leaf Sheet */
.rayan-leaf {
  position: absolute;
  top: 0;
  left: 50%;
  width: 50%;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  will-change: transform;
}

/* Leaf Faces: Front (faces right initially) & Back (faces left after 180deg flip) */
.leaf-face {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  border-radius: 0 var(--edge-radius) var(--edge-radius) 0;
  box-shadow: 0 4px 20px rgba(24, 44, 62, 0.08);
}

.leaf-face.leaf-front {
  transform: rotateY(0deg) translateZ(1px);
  -webkit-transform: rotateY(0deg) translateZ(1px);
  z-index: 2;
}

.leaf-face.leaf-back {
  transform: rotateY(180deg) translateZ(1px);
  -webkit-transform: rotateY(180deg) translateZ(1px);
  border-radius: var(--edge-radius) 0 0 var(--edge-radius);
  z-index: 1;
}

/* Mobile Single-Page 3D Architecture */
.rayan-single-container {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
}

.single-page-sheet {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  transform-origin: left center;
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: var(--edge-radius);
  box-shadow: 0 10px 32px rgba(24, 44, 62, 0.14);
  will-change: transform;
}

[data-lang="ar"] .single-page-sheet {
  transform-origin: right center;
}

.single-lighting-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  border-radius: inherit;
  z-index: 15;
  transition: background 0.05s linear;
}

/* Dynamic Lighting & Shadow Overlay */
.leaf-lighting-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
  transition: background 0.05s linear;
}

/* Realistic Page Surface */
.book-page {
  position: relative;
  width: 100%;
  height: 100%;
  background-color: var(--color-paper-white);
  box-sizing: border-box;
  padding: 44px 48px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  overflow: hidden;
}

/* Paper texture subtle grain */
.book-page::before {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: radial-gradient(rgba(24, 44, 62, 0.02) 1px, transparent 0);
  background-size: 24px 24px;
  pointer-events: none;
  z-index: 1;
}

/* Corner Hover Peek Simulation */
.hover-peek-next-top .rayan-leaf:not([style*="-180"]):first-of-type,
.hover-peek-next-bottom .rayan-leaf:not([style*="-180"]):first-of-type {
  transition: transform 0.25s ease-out;
}

.hover-peek-next-top .rayan-leaf:not([style*="-180"]):first-of-type {
  transform: rotateY(-10deg) skewY(-1deg) !important;
}

.hover-peek-next-bottom .rayan-leaf:not([style*="-180"]):first-of-type {
  transform: rotateY(-10deg) skewY(1deg) !important;
}

/* Corner Fold Indicators (subtle dog-ear hover hints) */
.page-corner-hint {
  position: absolute;
  bottom: 0;
  right: 0;
  width: 36px;
  height: 36px;
  cursor: pointer;
  z-index: 12;
  transition: transform 0.3s ease;
}

.page-corner-hint::before {
  content: "";
  position: absolute;
  bottom: 0;
  right: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 0 0 28px 28px;
  border-color: transparent transparent var(--color-bg-warm-grey) transparent;
  transition: border-width 0.25s ease, filter 0.25s ease;
  filter: drop-shadow(-2px -2px 3px rgba(24, 44, 62, 0.12));
}

.page-corner-hint:hover::before {
  border-width: 0 0 38px 38px;
  filter: drop-shadow(-3px -3px 5px rgba(24, 44, 62, 0.2));
}

.page-corner-hint-left {
  right: auto;
  left: 0;
}

.page-corner-hint-left::before {
  border-width: 28px 0 0 28px;
  border-color: var(--color-bg-warm-grey) transparent transparent transparent;
}

/* =====================================================================
   RESPONSIVE MODES: SINGLE PAGE (MOBILE & TABLET PORTRAIT)
   ===================================================================== */
@media (max-width: 860px), (orientation: portrait) and (max-width: 1024px) {
  :root {
    --page-width: min(calc(100vw - 32px), 440px);
    --page-height: min(calc(100vh - 130px - env(safe-area-inset-top) - env(safe-area-inset-bottom)), calc(min(calc(100vw - 32px), 440px) * 1.38));
  }

  .rayan-book-stage {
    perspective: 1800px;
    padding-top: max(50px, calc(env(safe-area-inset-top) + 44px));
    padding-bottom: max(56px, calc(env(safe-area-inset-bottom) + 50px));
    box-sizing: border-box;
  }

  .rayan-book {
    width: var(--page-width);
    height: var(--page-height);
    transform: none !important;
  }

  .rayan-spine,
  .book-stack,
  .spine-ribbon {
    display: none !important;
  }

  .book-page {
    padding: 20px 18px;
    border-radius: var(--edge-radius);
  }
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {
  .rayan-book,
  .rayan-leaf,
  .book-page,
  .rayan-book-stage::after {
    transition-duration: 0.01s !important;
    animation-duration: 0.01s !important;
  }
}
