/* =========================================================================
   Spenderrific magazine viewer
   ========================================================================= */

:root {
  --mag-bg: #0F0F12;
  --mag-paper: #ffffff;
  --mag-bar-bg: rgba(15, 15, 18, 0.85);
  --mag-bar-text: #ffffff;
  --mag-shadow: 0 30px 60px -20px rgba(0, 0, 0, 0.6);
  --mag-spine-shadow: inset 8px 0 12px -8px rgba(0,0,0,0.45);
  --mag-spine-shadow-r: inset -8px 0 12px -8px rgba(0,0,0,0.45);
  --mag-flip-duration: 480ms;
  --mag-flip-easing: cubic-bezier(0.55, 0.085, 0.4, 0.98);
}

/* Reset the body for full-screen viewer */
.mag-viewer-body {
  margin: 0;
  background: var(--mag-bg);
  color: var(--mag-bar-text);
  font-family: 'Plus Jakarta Sans', system-ui, sans-serif;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  /* Prevent overscroll/pull-to-refresh on mobile */
  overscroll-behavior: none;
}

.mag-viewer {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto 1fr auto;
  user-select: none;
  -webkit-user-select: none;
}

/* -------------------------------------------------------------------------
   Top + bottom bars
   ------------------------------------------------------------------------- */
.mag-bar {
  background: var(--mag-bar-bg);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid rgba(255,255,255,0.08);
  z-index: 20;
  display: flex;
  align-items: center;
  padding: 0 18px;
  transition: opacity 0.3s ease, transform 0.3s ease;
}
.mag-bar-top {
  height: 56px;
  justify-content: space-between;
  gap: 16px;
}
.mag-bar-bottom {
  height: 44px;
  border-bottom: 0;
  border-top: 1px solid rgba(255,255,255,0.08);
  justify-content: center;
}
.mag-viewer.is-immersive .mag-bar {
  opacity: 0;
  pointer-events: none;
}
.mag-viewer.is-immersive .mag-bar-top { transform: translateY(-100%); }
.mag-viewer.is-immersive .mag-bar-bottom { transform: translateY(100%); }

.mag-bar-left,
.mag-bar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}
.mag-bar-center {
  flex: 1;
  text-align: center;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  min-width: 0;
}
.mag-bar-center strong {
  font-weight: 600;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 50vw;
}
.mag-bar-meta {
  font-size: 12px;
  color: rgba(255,255,255,0.5);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  flex-shrink: 0;
}

.mag-bar-home {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--mag-bar-text);
  text-decoration: none;
  font-weight: 600;
  font-size: 14px;
  padding: 6px 10px;
  border-radius: 6px;
  transition: background .15s ease;
}
.mag-bar-home:hover { background: rgba(255,255,255,0.08); }

.mag-icon-btn {
  background: transparent;
  border: none;
  color: var(--mag-bar-text);
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  transition: background .15s ease;
}
.mag-icon-btn:hover { background: rgba(255,255,255,0.08); }
.mag-icon-btn:focus-visible {
  outline: 2px solid var(--theme-color, #34E0A1);
  outline-offset: 2px;
}

.mag-bar-counter {
  font-size: 13px;
  color: rgba(255,255,255,0.7);
  font-variant-numeric: tabular-nums;
}
.mag-bar-counter span:first-child {
  color: #fff;
  font-weight: 600;
}

/* -------------------------------------------------------------------------
   Stage (the reading area)
   ------------------------------------------------------------------------- */
.mag-stage {
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 24px 12px;
  /* Set up the 3D stage for page-flip transforms */
  perspective: 2000px;
  perspective-origin: center;
}

.mag-deck {
  position: relative;
  width: min(95vw, 1400px);
  /* Aspect ratio for a two-page spread (each page is 612x792, so 2x is 1224x792) */
  aspect-ratio: 1224 / 792;
  max-height: calc(100% - 16px);
  transform-style: preserve-3d;
}

/* Each spread layers on top of the deck; only one is visible at a time */
.mag-spread {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: transparent;
  box-shadow: var(--mag-shadow);
  border-radius: 6px;
  overflow: hidden;
  opacity: 0;
  transition: opacity 250ms ease;
}
.mag-spread:not([hidden]) { opacity: 1; }

/* Cover spread is centered on the right (page 1 alone) */
.mag-spread-cover {
  grid-template-columns: 1fr;
  max-width: 50%;
  margin: 0 auto;
  left: 0;
  right: 0;
  aspect-ratio: 612 / 792;
}
.mag-spread-cover .mag-page-cover {
  border-radius: 6px;
}

/* Pages */
.mag-page {
  position: relative;
  background: var(--mag-paper);
  overflow: hidden;
  /* Smooth subpixel layout so text rendering doesn't shift between spreads */
  contain: paint;
}
.mag-page img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  pointer-events: none;
  -webkit-user-drag: none;
}
/* Spine shadows — give the binding-fold feel */
.mag-page-left::after {
  content: '';
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 32px;
  box-shadow: var(--mag-spine-shadow-r);
  pointer-events: none;
}
.mag-page-right:not(.mag-page-cover)::before {
  content: '';
  position: absolute;
  top: 0; left: 0; bottom: 0;
  width: 32px;
  box-shadow: var(--mag-spine-shadow);
  pointer-events: none;
}

/* Folio (page number badge) — small, bottom corner, fades out in immersive mode */
.mag-page-folio {
  position: absolute;
  bottom: 8px;
  font-size: 10px;
  font-weight: 600;
  color: rgba(0,0,0,0.4);
  letter-spacing: 0.06em;
  background: rgba(255,255,255,0.85);
  padding: 2px 7px;
  border-radius: 999px;
  pointer-events: none;
  z-index: 4;
}
.mag-page-left .mag-page-folio { left: 14px; }
.mag-page-right .mag-page-folio { right: 14px; }

/* -------------------------------------------------------------------------
   Page-flip animation
   The spread that's leaving rotates around its outer edge while the
   incoming spread fades in. Forward: outgoing rotates Y(-180deg) around
   the right edge. Back: outgoing rotates Y(180deg) around the left edge.
   ------------------------------------------------------------------------- */
.mag-spread.is-flipping-forward {
  transform-origin: 0% 50%;          /* Rotate around left edge */
  animation: mag-flip-forward var(--mag-flip-duration) var(--mag-flip-easing) forwards;
  z-index: 10;
}
.mag-spread.is-flipping-back {
  transform-origin: 100% 50%;        /* Rotate around right edge */
  animation: mag-flip-back var(--mag-flip-duration) var(--mag-flip-easing) forwards;
  z-index: 10;
}
@keyframes mag-flip-forward {
  0%   { transform: rotateY(0deg);    opacity: 1; }
  60%  { transform: rotateY(-90deg);  opacity: 0.7; }
  100% { transform: rotateY(-180deg); opacity: 0; }
}
@keyframes mag-flip-back {
  0%   { transform: rotateY(0deg);    opacity: 1; }
  60%  { transform: rotateY(90deg);   opacity: 0.7; }
  100% { transform: rotateY(180deg);  opacity: 0; }
}
/* The incoming spread starts behind, fades up cleanly */
.mag-spread.is-arriving {
  z-index: 5;
  animation: mag-spread-in var(--mag-flip-duration) var(--mag-flip-easing) forwards;
}
@keyframes mag-spread-in {
  0%   { opacity: 0; transform: scale(0.985); }
  60%  { opacity: 0.6; }
  100% { opacity: 1; transform: scale(1); }
}

/* Reduce motion preference: instant cuts only, no rotations */
@media (prefers-reduced-motion: reduce) {
  .mag-spread.is-flipping-forward,
  .mag-spread.is-flipping-back,
  .mag-spread.is-arriving {
    animation: none;
  }
}

/* -------------------------------------------------------------------------
   Flip buttons (left/right arrows in the stage)
   ------------------------------------------------------------------------- */
.mag-flip-btn {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  color: #fff;
  cursor: pointer;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  z-index: 15;
  transition: background .15s ease, transform .15s ease, opacity .15s ease;
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.mag-flip-btn:hover:not(:disabled) {
  background: rgba(255,255,255,0.16);
  transform: translateY(-50%) scale(1.05);
}
.mag-flip-btn:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}
.mag-flip-prev { left: 16px; }
.mag-flip-next { right: 16px; }

/* -------------------------------------------------------------------------
   Hotspots
   ------------------------------------------------------------------------- */
.mag-hotspots {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 3;
}
.mag-hotspot {
  position: absolute;
  border: 2px solid transparent;
  background: transparent;
  pointer-events: auto;
  border-radius: 4px;
  transition: background .2s ease, border-color .2s ease, box-shadow .2s ease;
  cursor: pointer;
  text-decoration: none;
}
.mag-hotspot:hover,
.mag-hotspot:focus-visible {
  background: rgba(52, 224, 161, 0.18);
  border-color: rgba(52, 224, 161, 0.6);
  box-shadow: 0 0 0 4px rgba(52, 224, 161, 0.12);
  outline: none;
}
.mag-hotspot-label {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%) translateY(4px);
  background: #0a0a0a;
  color: #fff;
  padding: 6px 12px;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .15s ease, transform .15s ease;
  z-index: 5;
  max-width: 240px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.mag-hotspot-label::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: #0a0a0a;
}
.mag-hotspot:hover .mag-hotspot-label,
.mag-hotspot:focus-visible .mag-hotspot-label {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
/* Empty label → don't show the tooltip box */
.mag-hotspot-label:empty { display: none; }

/* -------------------------------------------------------------------------
   Page strip drawer
   ------------------------------------------------------------------------- */
.mag-strip {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 88vw);
  background: rgba(15, 15, 18, 0.96);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border-left: 1px solid rgba(255,255,255,0.08);
  z-index: 40;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform 0.3s ease;
}
.mag-strip.is-open {
  transform: translateX(0);
}
.mag-strip-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  font-size: 14px;
  flex-shrink: 0;
}
.mag-strip-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  padding: 16px;
  overflow-y: auto;
  flex: 1;
}
.mag-strip-item {
  background: transparent;
  border: 2px solid transparent;
  border-radius: 4px;
  padding: 0;
  cursor: pointer;
  position: relative;
  display: block;
  transition: border-color .15s ease, transform .15s ease;
}
.mag-strip-item:hover {
  border-color: rgba(52,224,161,0.5);
  transform: translateY(-2px);
}
.mag-strip-item.is-current {
  border-color: #34E0A1;
}
.mag-strip-item img {
  display: block;
  width: 100%;
  aspect-ratio: 612 / 792;
  object-fit: cover;
  border-radius: 2px;
  background: #1a1a1f;
}
.mag-strip-item span {
  position: absolute;
  bottom: 4px;
  left: 4px;
  background: rgba(0,0,0,0.7);
  color: #fff;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 3px;
}

/* -------------------------------------------------------------------------
   Mobile — single page mode
   On viewports under 720px, we collapse to single-page-at-a-time.
   The deck still contains all spreads, but we change which page within
   the spread is visible based on the active sub-index.
   ------------------------------------------------------------------------- */
@media (max-width: 720px) {
  .mag-stage {
    padding: 12px 4px;
  }
  .mag-deck {
    aspect-ratio: 612 / 792;
    width: min(96vw, 540px);
  }
  /* Hide the spread layout, show one page at a time */
  .mag-spread {
    grid-template-columns: 1fr;
  }
  .mag-spread-cover {
    max-width: 100%;
  }
  /* For non-cover spreads, hide the right page by default; JS toggles which
     side is visible based on the .show-left or .show-right class on the spread */
  .mag-spread:not(.mag-spread-cover) .mag-page-left { display: none; }
  .mag-spread:not(.mag-spread-cover) .mag-page-right { display: block; }
  .mag-spread.show-left .mag-page-left { display: block; }
  .mag-spread.show-left .mag-page-right { display: none; }
  .mag-spread.show-right .mag-page-left { display: none; }
  .mag-spread.show-right .mag-page-right { display: block; }
  /* Folio always at bottom-right since there's no spine concept */
  .mag-page-folio {
    right: 14px;
    left: auto;
  }
  /* Spine shadows off in single-page mode */
  .mag-page-left::after,
  .mag-page-right::before { display: none; }
  /* Smaller flip buttons that don't crowd the page */
  .mag-flip-btn {
    width: 44px;
    height: 44px;
  }
  .mag-flip-prev { left: 6px; }
  .mag-flip-next { right: 6px; }
  /* Smaller bar */
  .mag-bar-top { height: 48px; padding: 0 10px; }
  .mag-bar-home span { display: none; }
  .mag-bar-center strong { font-size: 13px; max-width: 40vw; }
  .mag-bar-meta { display: none; }
  /* Strip becomes near-full-width */
  .mag-strip-grid { grid-template-columns: repeat(2, 1fr); }
}

/* Desktop, smaller viewports — keep spread but tighter padding */
@media (max-width: 1024px) and (min-width: 721px) {
  .mag-stage { padding: 16px 8px; }
  .mag-flip-btn { width: 48px; height: 48px; }
}

/* -------------------------------------------------------------------------
   Fullscreen API
   ------------------------------------------------------------------------- */
.mag-viewer:fullscreen,
.mag-viewer:-webkit-full-screen {
  background: var(--mag-bg);
}
