/* ================================================
   FlipBook — Design System & Styles
   零外部依賴，純 CSS3
   ================================================ */

/* --- CSS Layer 宣告 --- */
@layer reset, layout, components, animations, responsive, print, accessibility;

/* --- CSS Custom Properties (from config) --- */
@layer reset {
  :root {
  --bg-color: #1F2232;
  --toolbar-color: #2a2d3e;
  --toolbar-text: #E0E0E0;
  --icon-color: #CCCCCC;
  --icon-hover: #FFFFFF;
  --page-bg: #FFFFFF;
  --accent: #4A90D9;
  --border-color: #572F0D;
  --border-width: 8px;
  --corner-round: 8px;
  --flip-duration: 0.6s;
  --shadow-soft: 0 2px 20px rgba(0,0,0,0.4);
  --shadow-page: 4px 0 12px rgba(0,0,0,0.3);
  --panel-width: 300px;
  }

  /* --- Reset & Base --- */
  *, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
  }

  html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: "Segoe UI", "Microsoft JhengHei", "PingFang TC", system-ui, -apple-system, sans-serif;
    background: var(--bg-color);
    color: var(--toolbar-text);
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
  }

  .hidden { display: none !important; }
}

@layer components {

/* ================================================
   Loading Screen
   ================================================ */
#loading-screen {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-color);
  transition: opacity 0.5s ease;
}

#loading-screen.fade-out {
  opacity: 0;
  pointer-events: none;
}

.loading-content {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

#loading-logo {
  width: 80px;
  height: 80px;
  object-fit: contain;
  border-radius: 16px;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { transform: scale(1); opacity: 0.9; }
  50% { transform: scale(1.05); opacity: 1; }
}

.loading-spinner {
  display: flex;
  gap: 8px;
}

.spinner-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.4s ease-in-out infinite;
}

.spinner-dot:nth-child(2) { animation-delay: 0.16s; }
.spinner-dot:nth-child(3) { animation-delay: 0.32s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

#loading-text {
  font-size: 16px;
  color: var(--toolbar-text);
  letter-spacing: 2px;
}

#loading-progress {
  font-size: 13px;
  color: var(--icon-color);
  font-variant-numeric: tabular-nums;
}

} /* end @layer components */

@layer layout {

/* ================================================
   App Layout
   ================================================ */
#app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;
}

/* ================================================
   Top Toolbar
   ================================================ */
#toolbar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 48px;
  padding: 0 12px;
  background: var(--toolbar-color);
  border-bottom: 1px solid rgba(255,255,255,0.08);
  z-index: 100;
  flex-shrink: 0;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

#app-logo {
  width: 28px;
  height: 28px;
  object-fit: contain;
  border-radius: 4px;
  flex-shrink: 0;
}

#book-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--toolbar-text);
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

/* --- Toolbar Buttons --- */
.toolbar-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  border-radius: 6px;
  cursor: pointer;
  transition: background 0.2s, transform 0.15s;
  color: var(--icon-color);
  flex-shrink: 0;
}

.toolbar-btn svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

.toolbar-btn:hover {
  background: rgba(255,255,255,0.1);
  color: var(--icon-hover);
}

.toolbar-btn:active {
  transform: scale(0.92);
}

.toolbar-btn.active {
  background: rgba(74,144,217,0.25);
  color: var(--accent);
}

.toolbar-btn.small {
  width: 32px;
  height: 32px;
}

.toolbar-btn.small svg {
  width: 18px;
  height: 18px;
}

/* ================================================
   Book Viewport
   ================================================ */
#book-viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none; /* 禁用預設觸控行為，防頁面滾動與彈跳 */
}

#book-container {
  position: relative;
  transform-origin: center center;
  transition: transform 0.3s ease;
  touch-action: none;
}

#book {
  display: flex;
  position: relative;
  perspective: 2000000px;
  box-shadow: var(--shadow-soft);
  border-radius: var(--corner-round);
  overflow: hidden;
  touch-action: none;
  gap: 1px; /* spacing between left/right pages in double-page mode */
}
#book.single-page {
  gap: 0;
}

/* Hard cover border */
#book.hard-cover {
  border: var(--border-width) solid var(--border-color);
  border-radius: calc(var(--corner-round) + 2px);
}

/* --- Page Slots --- */
.page-slot {
  flex: 1;
  position: relative;
  background: var(--page-bg);
  overflow: hidden;
}

.page-slot:first-child {
  border-radius: var(--corner-round) 0 0 var(--corner-round);
}

.page-slot:last-child {
  border-radius: 0 var(--corner-round) var(--corner-round) 0;
}

.page-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-slot img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

/* Page divider - single 1px line */
#page-left {
  border-right: 1px solid rgba(0,0,0,0.08);
}

#page-right {
  border-left: 1px solid rgba(0,0,0,0.08);
}

/* Single page mode */
#book.single-page #page-left {
  display: none;
}

#book.single-page #page-right {
  box-shadow: none;
}

/* Page empty state */
.page-slot.empty {
  background: var(--page-bg);
}

.page-slot.empty img {
  display: none;
}

} /* end @layer layout */

@layer animations {

/* ================================================
   Flip Animation Layer
   ================================================ */
#flip-layer {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
}

.flip-page {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform var(--flip-duration) ease-in-out;
  z-index: 20;
}

.flip-page.flipping-forward,
.flip-page.flipping-backward {
  transition: none !important;
}

.flip-page .flip-front,
.flip-page .flip-back {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  border-radius: var(--corner-round);
}

.flip-page .flip-front {
  background: var(--page-bg);
}

.flip-page .flip-back {
  background: var(--page-bg);
  transform: rotateY(180deg);
}

.flip-page .flip-front img,
.flip-page .flip-back img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

/* Central spine divider crease */
.book-spine {
  position: absolute;
  left: 50%;
  top: 0;
  bottom: 0;
  width: 20px;
  transform: translateX(-50%);
  background: linear-gradient(to right,
    rgba(0,0,0,0.3) 0%,
    rgba(0,0,0,0.1) 20%,
    rgba(255,255,255,0.18) 50%,
    rgba(0,0,0,0.1) 80%,
    rgba(0,0,0,0.3) 100%
  );
  z-index: 12;
  pointer-events: none;
  box-shadow: 0 0 10px rgba(0,0,0,0.4);
}

#book.single-page .book-spine {
  display: none;
}

/* ================================================
   Optimized 3D Page Turn Keyframes & Animations
   ================================================ */

/* Forward Page Turn: Right to Left */
.flip-page.flipping-forward {
  animation: flipForward var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

@keyframes flipForward {
  0% {
    transform: rotateY(0deg) translateZ(0px) scale(1) skewY(0deg);
  }
  30% {
    /* Lift up, twist slightly */
    transform: rotateY(-54deg) translateZ(90px) scale(0.982) skewY(-2.8deg);
  }
  50% {
    /* Max height lift and twisting */
    transform: rotateY(-90deg) translateZ(125px) scale(0.968) skewY(-4.5deg);
  }
  70% {
    /* Land back down, twisting decreases */
    transform: rotateY(-126deg) translateZ(90px) scale(0.982) skewY(-2.8deg);
  }
  100% {
    transform: rotateY(-180deg) translateZ(0px) scale(1) skewY(0deg);
  }
}

/* Backward Page Turn: Left to Right */
.flip-page.flipping-backward {
  animation: flipBackward var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

@keyframes flipBackward {
  0% {
    transform: rotateY(0deg) translateZ(0px) scale(1) skewY(0deg);
  }
  30% {
    /* Lift up, twist in opposite direction */
    transform: rotateY(54deg) translateZ(90px) scale(0.982) skewY(2.8deg);
  }
  50% {
    transform: rotateY(90deg) translateZ(125px) scale(0.968) skewY(4.5deg);
  }
  70% {
    transform: rotateY(126deg) translateZ(90px) scale(0.982) skewY(2.8deg);
  }
  100% {
    transform: rotateY(180deg) translateZ(0px) scale(1) skewY(0deg);
  }
}

/* Single-Page Mode 3D Turn Overrides & Keyframes */
#book.single-page {
  overflow: visible !important;
}

#book.single-page #flip-layer .flip-page {
  width: 100%;
  left: 0 !important;
  transform-origin: left center !important;
  perspective: 2000px;
}

/* Single-page dynamic volumetric shadow overrides */
#book.single-page #flip-layer .flip-shadow-left {
  left: -100%;
  width: 100%;
  background: linear-gradient(to left, rgba(0,0,0,0.2) 0%, transparent 80%);
}

#book.single-page #flip-layer .flip-shadow-right {
  left: 0;
  width: 100%;
  background: linear-gradient(to right, rgba(0,0,0,0.2) 0%, transparent 80%);
}

.flip-page.single-flipping-forward {
  animation: singleFlipForward var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

.flip-page.single-flipping-backward {
  animation: singleFlipBackward var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

@keyframes singleFlipForward {
  0% {
    transform: rotateY(0deg) translateZ(0px) scale(1) skewY(0deg);
  }
  30% {
    /* Lift page, twist corner slightly */
    transform: rotateY(-54deg) translateZ(90px) scale(0.982) skewY(-2.8deg);
  }
  50% {
    /* Max lift height and twisting */
    transform: rotateY(-90deg) translateZ(125px) scale(0.968) skewY(-4.5deg);
  }
  70% {
    /* Land page down, decrease twisting */
    transform: rotateY(-126deg) translateZ(90px) scale(0.982) skewY(-2.8deg);
  }
  100% {
    transform: rotateY(-180deg) translateZ(0px) scale(1) skewY(0deg);
  }
}

@keyframes singleFlipBackward {
  0% {
    transform: rotateY(-180deg) translateZ(0px) scale(1) skewY(0deg);
  }
  30% {
    /* Lift page, twist corner in opposite direction */
    transform: rotateY(-126deg) translateZ(90px) scale(0.982) skewY(2.8deg);
  }
  50% {
    transform: rotateY(-90deg) translateZ(125px) scale(0.968) skewY(4.5deg);
  }
  70% {
    transform: rotateY(-54deg) translateZ(90px) scale(0.982) skewY(2.8deg);
  }
  100% {
    transform: rotateY(0deg) translateZ(0px) scale(1) skewY(0deg);
  }
}

/* Single-Page Mode Crease & Specular Reflections (Dual-Sided support) */

/* Forward Turn (Right-to-Left) animations */
.single-flipping-forward .flip-front .page-lighting {
  animation: singleCreaseForwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.single-flipping-forward .flip-front .page-shine {
  animation: singleShineForwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.single-flipping-forward .flip-back .page-lighting {
  animation: singleCreaseForwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.single-flipping-forward .flip-back .page-shine {
  animation: singleShineForwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

/* Backward Turn (Left-to-Right) animations */
.single-flipping-backward .flip-front .page-lighting {
  animation: singleCreaseBackwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.single-flipping-backward .flip-front .page-shine {
  animation: singleShineBackwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.single-flipping-backward .flip-back .page-lighting {
  animation: singleCreaseBackwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.single-flipping-backward .flip-back .page-shine {
  animation: singleShineBackwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

/* Single crease & shine keyframe definitions */
@keyframes singleCreaseForwardFront {
  0% { opacity: 0; }
  25% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  50% { opacity: 0.85; background: linear-gradient(to right, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.02) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes singleShineForwardFront {
  0% {
    opacity: 0;
    background: linear-gradient(115deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.4) 50%, rgba(255,255,255,0) 100%);
    transform: translateX(80%) skewX(-15deg);
  }
  50% {
    opacity: 0.8;
    transform: translateX(-10%) skewX(-15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(-90%) skewX(-15deg);
  }
}

@keyframes singleCreaseForwardBack {
  0% { opacity: 0; }
  50% { opacity: 0.85; background: linear-gradient(to left, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to left, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes singleShineForwardBack {
  0% { opacity: 0; transform: translateX(80%) skewX(-15deg); }
  50% {
    opacity: 0.85;
    background: linear-gradient(115deg, rgba(255,255,255,0) 30%, rgba(255,255,255,0.55) 50%, rgba(255,255,255,0) 70%);
    transform: translateX(10%) skewX(-15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(-60%) skewX(-15deg);
  }
}

@keyframes singleCreaseBackwardFront {
  0% { opacity: 0; }
  25% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  50% { opacity: 0.85; background: linear-gradient(to right, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.02) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes singleShineBackwardFront {
  0% {
    opacity: 0;
    background: linear-gradient(65deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.4) 50%, rgba(255,255,255,0) 100%);
    transform: translateX(-80%) skewX(15deg);
  }
  50% {
    opacity: 0.8;
    transform: translateX(10%) skewX(15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(90%) skewX(15deg);
  }
}

@keyframes singleCreaseBackwardBack {
  0% { opacity: 0; }
  50% { opacity: 0.85; background: linear-gradient(to left, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to left, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes singleShineBackwardBack {
  0% { opacity: 0; transform: translateX(-80%) skewX(15deg); }
  50% {
    opacity: 0.85;
    background: linear-gradient(65deg, rgba(255,255,255,0) 30%, rgba(255,255,255,0.55) 50%, rgba(255,255,255,0) 70%);
    transform: translateX(-10%) skewX(15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(60%) skewX(15deg);
  }
}

/* ================================================
   Dynamic Page Crease Shadow & Specular Gloss Shine
   ================================================ */
.page-lighting {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
  mix-blend-mode: multiply;
  opacity: 0;
}

.page-shine {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 6;
  mix-blend-mode: overlay;
  opacity: 0;
}

/* Forward Turn (Right-to-Left) animations */
.flipping-forward .flip-front .page-lighting {
  animation: creaseForwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.flipping-forward .flip-front .page-shine {
  animation: shineForwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.flipping-forward .flip-back .page-lighting {
  animation: creaseForwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.flipping-forward .flip-back .page-shine {
  animation: shineForwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

/* Backward Turn (Left-to-Right) animations */
.flipping-backward .flip-front .page-lighting {
  animation: creaseBackwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.flipping-backward .flip-front .page-shine {
  animation: shineBackwardFront var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.flipping-backward .flip-back .page-lighting {
  animation: creaseBackwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}
.flipping-backward .flip-back .page-shine {
  animation: shineBackwardBack var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

/* Forward Creases & Shines Keyframes */
@keyframes creaseForwardFront {
  0% { opacity: 0; }
  25% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  50% { opacity: 0.85; background: linear-gradient(to right, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.02) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes shineForwardFront {
  0% {
    opacity: 0;
    background: linear-gradient(115deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.45) 50%, rgba(255,255,255,0) 100%);
    transform: translateX(60%) skewX(-15deg);
  }
  30% {
    opacity: 0.7;
    background: linear-gradient(115deg, rgba(255,255,255,0) 25%, rgba(255,255,255,0.5) 50%, rgba(255,255,255,0) 75%);
    transform: translateX(20%) skewX(-15deg);
  }
  50% {
    opacity: 0.95;
    background: linear-gradient(115deg, rgba(255,255,255,0) 30%, rgba(255,255,255,0.65) 50%, rgba(255,255,255,0) 70%);
    transform: translateX(-10%) skewX(-15deg);
  }
  75% {
    opacity: 0.45;
    background: linear-gradient(115deg, rgba(255,255,255,0) 35%, rgba(255,255,255,0.4) 50%, rgba(255,255,255,0) 65%);
    transform: translateX(-40%) skewX(-15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(-80%) skewX(-15deg);
  }
}

@keyframes creaseForwardBack {
  0% { opacity: 0; }
  50% { opacity: 0.85; background: linear-gradient(to left, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to left, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes shineForwardBack {
  0% { opacity: 0; transform: translateX(80%) skewX(-15deg); }
  50% {
    opacity: 0.85;
    background: linear-gradient(115deg, rgba(255,255,255,0) 30%, rgba(255,255,255,0.55) 50%, rgba(255,255,255,0) 70%);
    transform: translateX(10%) skewX(-15deg);
  }
  75% {
    opacity: 0.5;
    background: linear-gradient(115deg, rgba(255,255,255,0) 35%, rgba(255,255,255,0.35) 50%, rgba(255,255,255,0) 65%);
    transform: translateX(-20%) skewX(-15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(-60%) skewX(-15deg);
  }
}

/* Backward Creases & Shines Keyframes */
@keyframes creaseBackwardFront {
  0% { opacity: 0; }
  25% { opacity: 0.45; background: linear-gradient(to left, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  50% { opacity: 0.85; background: linear-gradient(to left, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to left, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.02) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes shineBackwardFront {
  0% {
    opacity: 0;
    background: linear-gradient(65deg, rgba(255,255,255,0) 0%, rgba(255,255,255,0.45) 50%, rgba(255,255,255,0) 100%);
    transform: translateX(-60%) skewX(15deg);
  }
  30% {
    opacity: 0.7;
    background: linear-gradient(65deg, rgba(255,255,255,0) 25%, rgba(255,255,255,0.5) 50%, rgba(255,255,255,0) 75%);
    transform: translateX(-20%) skewX(15deg);
  }
  50% {
    opacity: 0.95;
    background: linear-gradient(65deg, rgba(255,255,255,0) 30%, rgba(255,255,255,0.65) 50%, rgba(255,255,255,0) 70%);
    transform: translateX(10%) skewX(15deg);
  }
  75% {
    opacity: 0.45;
    background: linear-gradient(65deg, rgba(255,255,255,0) 35%, rgba(255,255,255,0.4) 50%, rgba(255,255,255,0) 65%);
    transform: translateX(40%) skewX(15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(80%) skewX(15deg);
  }
}

@keyframes creaseBackwardBack {
  0% { opacity: 0; }
  50% { opacity: 0.85; background: linear-gradient(to right, rgba(0,0,0,0.6) 0%, rgba(0,0,0,0.12) 40%, rgba(0,0,0,0) 100%); }
  75% { opacity: 0.45; background: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0) 100%); }
  100% { opacity: 0; }
}

@keyframes shineBackwardBack {
  0% { opacity: 0; transform: translateX(-80%) skewX(15deg); }
  50% {
    opacity: 0.85;
    background: linear-gradient(65deg, rgba(255,255,255,0) 30%, rgba(255,255,255,0.55) 50%, rgba(255,255,255,0) 70%);
    transform: translateX(-10%) skewX(15deg);
  }
  75% {
    opacity: 0.5;
    background: linear-gradient(65deg, rgba(255,255,255,0) 35%, rgba(255,255,255,0.35) 50%, rgba(255,255,255,0) 65%);
    transform: translateX(20%) skewX(15deg);
  }
  100% {
    opacity: 0;
    transform: translateX(60%) skewX(15deg);
  }
}

/* ================================================
   Interactive Volumetric Drop Shadows
   ================================================ */
.flip-shadow {
  position: absolute;
  top: 0;
  width: 50%;
  height: 100%;
  pointer-events: none;
  z-index: 15;
  transition: opacity var(--flip-duration) ease-in-out;
}

.flip-shadow-left {
  left: 0;
  background: linear-gradient(to left, rgba(0,0,0,0.2) 0%, transparent 80%);
}

.flip-shadow-right {
  right: 0;
  background: linear-gradient(to right, rgba(0,0,0,0.2) 0%, transparent 80%);
}

/* Animated active volumetric shadows */
.flip-shadow-left.shadow-active-left {
  transition: none !important;
  animation: shadowLeftForward var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

.flip-shadow-right.shadow-active-right {
  transition: none !important;
  animation: shadowRightBackward var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

.flip-shadow-right.shadow-fade-out {
  transition: none !important;
  animation: shadowFadeOut var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

.flip-shadow-left.shadow-fade-in {
  transition: none !important;
  animation: shadowFadeIn var(--flip-duration) cubic-bezier(0.2, 0.7, 0.35, 1) forwards;
}

@keyframes shadowLeftForward {
  0% {
    opacity: 0;
    transform: scaleX(0);
    transform-origin: right center;
    filter: blur(2px);
  }
  30% {
    opacity: 0.35;
    transform: scaleX(0.55);
    transform-origin: right center;
    filter: blur(8px);
  }
  50% {
    opacity: 0.75;
    transform: scaleX(0.9);
    transform-origin: right center;
    filter: blur(16px);
  }
  75% {
    opacity: 0.6;
    transform: scaleX(0.98);
    transform-origin: right center;
    filter: blur(7px);
  }
  100% {
    opacity: 0.3;
    transform: scaleX(1);
    transform-origin: right center;
    filter: blur(3px);
  }
}

@keyframes shadowRightBackward {
  0% {
    opacity: 0;
    transform: scaleX(0);
    transform-origin: left center;
    filter: blur(2px);
  }
  30% {
    opacity: 0.35;
    transform: scaleX(0.55);
    transform-origin: left center;
    filter: blur(8px);
  }
  50% {
    opacity: 0.75;
    transform: scaleX(0.9);
    transform-origin: left center;
    filter: blur(16px);
  }
  75% {
    opacity: 0.6;
    transform: scaleX(0.98);
    transform-origin: left center;
    filter: blur(7px);
  }
  100% {
    opacity: 0.3;
    transform: scaleX(1);
    transform-origin: left center;
    filter: blur(3px);
  }
}

@keyframes shadowFadeOut {
  0% { opacity: 0.45; transform: scaleX(1); filter: blur(3px); }
  35% { opacity: 0.28; transform: scaleX(0.85); filter: blur(10px); }
  60% { opacity: 0.08; transform: scaleX(0.45); filter: blur(18px); }
  100% { opacity: 0; transform: scaleX(0); }
}

@keyframes shadowFadeIn {
  0% { opacity: 0; transform: scaleX(0); filter: blur(18px); }
  40% { opacity: 0.18; transform: scaleX(0.45); filter: blur(12px); }
  75% { opacity: 0.38; transform: scaleX(0.85); filter: blur(6px); }
  100% { opacity: 0.45; transform: scaleX(1); filter: blur(3px); }
}

/* ================================================
   Navigation Arrows
   ================================================ */
.nav-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  z-index: 50;
  width: 44px;
  height: 80px;
  border: none;
  background: rgba(0,0,0,0.3);
  border-radius: 8px;
  cursor: pointer;
  color: rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s, opacity 0.3s;
  opacity: 0;
}

#book-viewport:hover .nav-arrow {
  opacity: 1;
}

.nav-arrow:hover {
  background: rgba(0,0,0,0.6);
  color: #fff;
}

.nav-arrow svg {
  width: 28px;
  height: 28px;
  fill: currentColor;
}

.nav-prev { left: 12px; }
.nav-next { right: 12px; }

.nav-arrow:disabled {
  opacity: 0.2 !important;
  cursor: default;
}

/* ================================================
   Bottom Toolbar
   ================================================ */
#toolbar-bottom {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 44px;
  gap: 8px;
  background: var(--toolbar-color);
  border-top: 1px solid rgba(255,255,255,0.08);
  z-index: 100;
  flex-shrink: 0;
}

#page-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  color: var(--toolbar-text);
}

#page-input {
  width: 42px;
  height: 28px;
  text-align: center;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 4px;
  background: rgba(255,255,255,0.08);
  color: var(--toolbar-text);
  font-size: 13px;
  font-family: inherit;
  outline: none;
  -moz-appearance: textfield;
}

#page-input::-webkit-inner-spin-button,
#page-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

#page-input:focus {
  border-color: var(--accent);
  background: rgba(255,255,255,0.12);
}

/* ================================================
   Side Panel (TOC / Bookmarks)
   ================================================ */
#panel-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s ease;
}

#panel-overlay.show {
  opacity: 1;
}

.side-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: var(--panel-width);
  max-width: 85vw;
  height: 100%;
  background: var(--toolbar-color);
  z-index: 210;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  box-shadow: 4px 0 20px rgba(0,0,0,0.4);
}

.side-panel.show {
  transform: translateX(0);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
}

.panel-header h2 {
  font-size: 16px;
  font-weight: 600;
  color: var(--toolbar-text);
}

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.panel-body::-webkit-scrollbar {
  width: 4px;
}

.panel-body::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
  border-radius: 2px;
}

#toc-list, #bookmark-list {
  list-style: none;
}

#toc-list li, #bookmark-list li {
  padding: 0;
}

.toc-item, .bookmark-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 20px;
  cursor: pointer;
  transition: background 0.15s;
  font-size: 14px;
  color: var(--toolbar-text);
  border: none;
  background: none;
  width: 100%;
  text-align: left;
  font-family: inherit;
}

.toc-item:hover, .bookmark-item:hover {
  background: rgba(255,255,255,0.08);
}

.toc-item .toc-page, .bookmark-item .bm-page {
  font-size: 16px;
  color: var(--icon-color);
  margin-left: auto;
  flex-shrink: 0;
  background: rgba(255,255,255,0.08);
  padding: 2px 8px;
  border-radius: 10px;
}

.toc-item.active {
  background: rgba(74,144,217,0.15);
  color: var(--accent);
}

/* Panel tabs for switching between TOC and Bookmarks */
.panel-tabs {
  display: flex;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  flex-shrink: 0;
}

.panel-tab {
  flex: 1;
  padding: 10px;
  text-align: center;
  font-size: 13px;
  color: var(--icon-color);
  cursor: pointer;
  border: none;
  background: none;
  transition: color 0.2s, border-color 0.2s;
  border-bottom: 2px solid transparent;
  font-family: inherit;
}

.panel-tab:hover {
  color: var(--toolbar-text);
}

.panel-tab.active {
  color: var(--accent);
  border-bottom-color: var(--accent);
}

/* ================================================
   Help Modal
   ================================================ */
.modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.6);
  opacity: 0;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.modal.show {
  opacity: 1;
  pointer-events: auto;
}

.modal-content {
  background: var(--toolbar-color);
  border-radius: 12px;
  width: 420px;
  max-width: 90vw;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
  transform: scale(0.9) translateY(20px);
  transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  overflow: hidden;
}

.modal.show .modal-content {
  transform: scale(1) translateY(0);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.modal-header h2 {
  font-size: 16px;
  font-weight: 600;
}

.modal-body {
  padding: 20px;
}

.help-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.help-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px;
  background: rgba(255,255,255,0.04);
  border-radius: 8px;
}

.help-icon {
  font-size: 24px;
}

.help-desc {
  font-size: 12px;
  color: var(--icon-color);
  text-align: center;
}

} /* end @layer animations */

@layer responsive {

/* ================================================
   Responsive
   ================================================ */
@media (max-width: 768px) {
  #toolbar-top {
    height: 42px;
    padding: 0 8px;
  }

  #book-title {
    font-size: 12px;
    max-width: 120px;
  }

  .toolbar-btn {
    width: 32px;
    height: 32px;
  }

  .toolbar-btn svg {
    width: 18px;
    height: 18px;
  }

  .nav-arrow {
    width: 36px;
    height: 60px;
    opacity: 0.6;
  }

  #book-viewport:hover .nav-arrow {
    opacity: 0.8;
  }

  .nav-prev { left: 4px; }
  .nav-next { right: 4px; }

  #toolbar-bottom {
    height: 40px;
  }

  .help-grid {
    grid-template-columns: 1fr;
  }

  .toolbar-right .toolbar-btn.hide-mobile {
    display: none;
  }
}

@media (max-width: 480px) {
  #book-title {
    display: none;
  }
}

} /* end @layer responsive */

@layer print {

/* ================================================
   Print Styles
   ================================================ */
@media print {
  #toolbar-top, #toolbar-bottom, .nav-arrow,
  .side-panel, .modal, #panel-overlay, #loading-screen {
    display: none !important;
  }

  body {
    background: white;
  }

  #book-viewport {
    position: static;
    overflow: visible;
  }

  #book-container {
    transform: none !important;
  }

  .page-slot img {
    max-width: 100%;
    height: auto;
  }
}

} /* end @layer print */

@layer accessibility {

/* ================================================
   Accessibility
   ================================================ */

/* ARIA live region for screen readers */
#page-announcer {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Focus visible styles */
.toolbar-btn:focus-visible,
.nav-arrow:focus-visible,
.toc-item:focus-visible,
.bookmark-item:focus-visible,
.panel-tab:focus-visible,
.bookmark-tab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Skip to content link (visually hidden but keyboard-accessible) */
.skip-link {
  position: absolute;
  top: -40px;
  left: 0;
  z-index: 10000;
  padding: 8px 16px;
  background: var(--accent);
  color: white;
  text-decoration: none;
  font-size: 14px;
  transition: top 0.2s ease;
}

.skip-link:focus {
  top: 0;
}

/* ================================================
   Reduced Motion
   ================================================ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }

  #loading-logo {
    animation: none !important;
  }

  .spinner-dot {
    animation: none !important;
    opacity: 0.5;
  }

  .bookmark-tab:hover {
    transform: none !important;
  }

  #book-content {
    transition: none !important;
  }
}

} /* end @layer accessibility */

@layer layout {

/* --- Interactive Bookmark Tabs --- */
.bookmark-tabs-container {
  position: absolute;
  top: 30px;
  bottom: 30px;
  width: 0;
  z-index: 5;
  pointer-events: auto;
}

#bookmark-tabs-left {
  left: 0;
  transform: translateX(-100%);
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  transition: transform 0.3s ease;
}
#bookmark-tabs-left:hover {
  transform: translateX(0);
}

#bookmark-tabs-right {
  right: 0;
  transform: translateX(100%);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 8px;
  transition: transform 0.3s ease;
}
#bookmark-tabs-right:hover {
  transform: translateX(0);
}

.bookmark-tab {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  min-height: 60px;
  max-height: 110px;
  color: white;
  font-size: 11px;
  font-weight: bold;
  cursor: pointer;
  box-shadow: 2px 2px 6px rgba(0,0,0,0.3);
  transition: transform 0.2s ease, filter 0.2s ease;
  writing-mode: vertical-rl;
  text-orientation: mixed;
  padding: 10px 4px;
  letter-spacing: 1.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-left: none;
}

.bookmark-tab.tab-left {
  border-radius: 6px 0 0 6px;
  box-shadow: -2px 2px 6px rgba(0,0,0,0.3);
  border-right: none;
  border-left: 1px solid rgba(255, 255, 255, 0.15);
}

.bookmark-tab.tab-right {
  border-radius: 0 6px 6px 0;
}

.bookmark-tab:hover {
  filter: brightness(1.15) saturate(1.1);
  writing-mode: horizontal-tb;
  text-orientation: mixed;
  width: auto;
  min-width: 200px;
  padding: 8px 12px;
  white-space: nowrap;
  overflow: visible;
  text-overflow: clip;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.4);
  z-index: 10;
  font-size: 44px; /* 4x base 11px */
  line-height: 1.2;
}

.bookmark-tab.tab-left:hover {
  transform: translateX(8px);
}

.bookmark-tab.tab-right:hover {
  transform: translateX(-8px);
}

} /* end @layer layout */

