/**
 * Data Stories — Story Map (scrollytelling) layout
 *
 * Sidecar pattern: narrative cards scroll on the left, a single sticky
 * Terria iframe stays pinned on the right and switches scene per card.
 */

.storymap-page .main {
  /* sticky positioning breaks inside ancestors with overflow != visible */
  overflow: visible;
}

.storymap {
  display: grid;
  grid-template-columns: minmax(380px, 42%) 1fr;
  grid-auto-rows: auto;
  align-items: start;
  width: 100%;
  margin: 2rem 0;
}

.storymap.has-no-map {
  grid-template-columns: 1fr;
}

/* ------------------------------------------------------------------ */
/* Sticky map panel                                                     */
/* ------------------------------------------------------------------ */

/* Media is first in the DOM (so it sits on top in the mobile single-column
   flow) but is placed in the right grid column on desktop. */
.storymap-media {
  position: sticky;
  top: 0;
  height: 100vh;
  grid-column: 2;
  align-self: start;
  z-index: 1;
  background: #dfe9f0;
  overflow: hidden;
  opacity: 1;
  visibility: visible;
  transition: opacity 0.3s ease, visibility 0.3s ease;
}

.storymap.is-full-section .storymap-media {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.storymap-iframe {
  width: 100%;
  height: 100%;
  border: 0;
  display: block;
}

.storymap-scene-error {
  position: absolute;
  top: 1rem;
  left: 1rem;
  right: 1rem;
  padding: 1rem;
  background: #fff4de;
  color: #533b0a;
  z-index: 5;
  border-radius: 8px;
}

.storymap-placeholder {
  position: absolute;
  inset: 0;
  z-index: 2;
  background: linear-gradient(135deg, #0072BC 0%, #003a61 100%);
  background-size: cover;
  background-position: center;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 1;
  transition: opacity 0.6s ease;
}

.storymap-placeholder.is-hidden {
  opacity: 0;
  pointer-events: none;
}

.storymap-placeholder-overlay {
  text-align: center;
  color: #fff;
  background: rgba(0, 42, 70, 0.55);
  padding: 2rem 3rem;
  border-radius: 16px;
  backdrop-filter: blur(4px);
}

.storymap-placeholder-overlay p {
  margin: 1rem 0 0 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.storymap-spinner {
  width: 44px;
  height: 44px;
  margin: 0 auto;
  border: 4px solid rgba(255, 255, 255, 0.3);
  border-top-color: #fff;
  border-radius: 50%;
  animation: storymap-spin 0.9s linear infinite;
}

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

/* Scene-transition veil. Cross-origin iframe pixels can't be snapshot, so
   the veil IS the crossfade: it tints+blurs in fast when a swap starts and
   fades out slowly once the new scene is applied — the asymmetric timing
   reads as a deliberate transition instead of a hard cut. */
.storymap-map-pane::after {
  content: '';
  position: absolute;
  inset: 0;
  z-index: 1;
  background: rgba(10, 46, 77, 0.45);
  -webkit-backdrop-filter: blur(3px) saturate(0.85);
  backdrop-filter: blur(3px) saturate(0.85);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.55s ease 0.05s;
}

.storymap.is-switching .storymap-map-pane::after {
  opacity: 1;
  transition: opacity 0.2s ease;
}

@media (prefers-reduced-motion: reduce) {
  .storymap-map-pane::after,
  .storymap-scene-loading {
    transition: none;
  }
}

/* "Loading scene…" pill shown during scene changes */
.storymap-scene-loading {
  position: absolute;
  top: 1rem;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  z-index: 4;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 1.2rem;
  border-radius: 24px;
  background: rgba(0, 42, 70, 0.85);
  color: #fff;
  font-size: 0.92rem;
  font-weight: 600;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease, transform 0.25s ease;
}

.storymap.is-switching .storymap-scene-loading {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.storymap-spinner-sm {
  width: 18px;
  height: 18px;
  margin: 0;
  border-width: 3px;
}

/* ------------------------------------------------------------------ */
/* Image slide over the map (image blocks)                              */
/* ------------------------------------------------------------------ */

/* Sits above the switching veil (z1), below dots (z3) and pill (z4). */
.storymap-media-image {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 24, 38, 0.92);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.35s ease;
}

.storymap-media-image.is-active {
  opacity: 1;
  pointer-events: auto;
}

.storymap-media-image img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.storymap-media-image-caption {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 0.9rem 1.5rem;
  background: linear-gradient(transparent, rgba(8, 24, 38, 0.85));
  color: #fff;
  font-size: 0.95rem;
  text-align: center;
}

.storymap-media-image-caption:empty {
  display: none;
}

/* Thumbnail trigger inside the narrative card */
.storymap-image-trigger {
  margin: 1.25rem 0;
  padding: 0;
  border-left: 4px solid transparent;
  padding-left: 1rem;
  transition: border-color 0.3s ease;
}

.storymap-image-trigger.is-active {
  border-left-color: #FFB800;
}

.storymap-image-trigger img {
  max-width: 100%;
  border-radius: 12px;
  box-shadow: 0 4px 16px rgba(0, 114, 188, 0.12);
}

.storymap-image-trigger figcaption {
  margin-top: 0.5rem;
  font-size: 0.9rem;
  color: #6c757d;
}

.storymap-image-trigger-hint {
  display: block;
  margin-top: 0.35rem;
  font-size: 0.8rem;
  color: #98a6b3;
}

.storymap-image-trigger-hint i {
  margin-right: 0.3rem;
}

/* ------------------------------------------------------------------ */
/* Progress dots                                                        */
/* ------------------------------------------------------------------ */

.storymap-progress {
  position: absolute;
  left: 1rem;
  top: 50%;
  transform: translateY(-50%);
  z-index: 3;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  align-items: center;
  padding: .5rem;
  border-radius: 24px;
  background: rgba(0, 42, 70, .8);
}

.storymap-dot {
  width: 12px;
  height: 12px;
  padding: 0;
  border-radius: 50%;
  border: 2px solid #fff;
  background: rgba(255, 255, 255, 0.35);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  cursor: pointer;
  transition: transform 0.2s ease, background 0.2s ease;
}

.storymap-dot:hover {
  transform: scale(1.25);
}

.storymap-dot.is-active {
  background: #FFB800;
  transform: scale(1.25);
}

/* Prev/next block buttons, bracketing the dots */
.storymap-nav-btn {
  width: 32px;
  height: 32px;
  margin: 0 auto;
  padding: 0;
  border-radius: 50%;
  border: 2px solid #fff;
  background: rgba(0, 42, 70, 0.55);
  color: #fff;
  font-size: 0.85rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  transition: transform 0.2s ease, background 0.2s ease;
}

.storymap-nav-btn:hover {
  transform: scale(1.1);
  background: rgba(0, 42, 70, 0.85);
}

.storymap-nav-prev {
  margin-bottom: 0.4rem;
}

.storymap-nav-next {
  margin-top: 0.4rem;
}

/* ------------------------------------------------------------------ */
/* Narrative cards column                                               */
/* ------------------------------------------------------------------ */

.storymap-cards {
  display: contents;
}

.storymap-card {
  grid-column: 1;
  min-height: 70vh;
  display: flex;
  align-items: center;
  padding: 0 2.5rem;
  background: #f4f7fa;
}

.storymap-card.is-full-width {
  grid-column: 1 / -1;
  position: relative;
  z-index: 2;
  min-height: 85vh;
  padding: 0 max(2.5rem, calc((100% - 1180px) / 2));
  background: linear-gradient(135deg, #f4f7fa 0%, #fff 100%);
}

.storymap-card-body {
  width: 100%;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 4px 20px rgba(0, 114, 188, 0.12);
  padding: 2.25rem 2.5rem;
  margin: 12vh 0;
  opacity: 1;
  transform: translateY(4px);
  transition: opacity 0.35s ease, transform 0.35s ease, box-shadow 0.35s ease;
}

.storymap-card.is-active .storymap-card-body {
  opacity: 1;
  transform: translateY(0);
  box-shadow: 0 8px 32px rgba(0, 114, 188, 0.2);
}

.storymap-card.is-full-width .storymap-card-body {
  max-width: 1180px;
  margin-left: auto;
  margin-right: auto;
}

.storymap-card.is-full-width .section-content {
  max-width: 78ch;
}

.storymap-card.is-full-width .section-content iframe,
.storymap-card.is-full-width .storymap-card-media,
.storymap-card.is-full-width .storymap-card-media iframe {
  max-width: none;
  width: 100%;
}

.storymap-card.is-full-width .storymap-image-trigger img {
  width: 100%;
  max-height: 75vh;
  object-fit: contain;
}

.storymap-card-header {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin-bottom: 1.25rem;
  padding-bottom: 1rem;
  border-bottom: 3px solid #E3F2FD;
}

.storymap-card-header h2 {
  margin: 0;
  color: #0072BC;
  font-size: 1.35rem;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.01em;
}

.storymap-card-number {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: linear-gradient(135deg, #0072BC 0%, #005A9C 100%);
  color: #fff;
  font-weight: 700;
  font-size: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.storymap-card .section-content {
  max-width: 62ch;
  font-size: 1.05rem;
  line-height: 1.8;
  color: #2c3e50;
}

.storymap-card .section-content p {
  margin-bottom: 1rem;
}

.storymap-card .section-content img {
  max-width: 100%;
  height: auto;
  border-radius: 12px;
  margin: 1.25rem 0;
  box-shadow: 0 4px 16px rgba(0, 114, 188, 0.12);
}

.storymap-card-media iframe {
  width: 100%;
  aspect-ratio: 16 / 9;
  height: auto;
  border: 0;
  border-radius: 12px;
  margin: 1rem 0;
}

.storymap-card.is-full-width .storymap-card-media iframe[style*="width: 100%"],
.storymap-card.is-full-width .storymap-card-media iframe[width="100%"] {
  aspect-ratio: auto;
}

/* ------------------------------------------------------------------ */
/* Story steps (Terria story scenes expanded as scroll stops)           */
/* ------------------------------------------------------------------ */

.storymap-steps {
  margin-top: 1.5rem;
  border-top: 3px solid #E3F2FD;
  padding-top: 0.5rem;
}

.storymap-step {
  min-height: 45vh;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.25rem 0;
  opacity: 1;
  border-left: 4px solid transparent;
  padding-left: 1rem;
  transition: opacity 0.3s ease, border-color 0.3s ease;
}

.storymap-step.is-active {
  opacity: 1;
  border-left-color: #FFB800;
}

.storymap-step-title {
  margin: 0 0 0.75rem 0;
  color: #0072BC;
  font-size: 1.15rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.storymap-step-number {
  flex: 0 0 auto;
  padding: 0.2rem 0.6rem;
  border-radius: 12px;
  background: #E3F2FD;
  color: #0072BC;
  font-size: 0.8rem;
  font-weight: 700;
}

.storymap-step-text {
  font-size: 1rem;
  color: #2c3e50;
}

/* Divider heading shown when a multi-map chapter's steps switch source */
.storymap-steps-source {
  margin: 2rem 0 0.25rem 0;
  padding-bottom: 0.35rem;
  border-bottom: 2px solid #E3F2FD;
  color: #5a7184;
  font-size: 0.85rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

/* Sub-scene tab buttons (multiple maps within one chapter) */
.storymap-scene-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 1.25rem 0 0.5rem 0;
}

.scene-tab-btn {
  padding: 0.5rem 1rem;
  border-radius: 20px;
  border: 2px solid #0072BC;
  background: #fff;
  color: #0072BC;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s ease, color 0.2s ease;
}

.scene-tab-btn:hover {
  background: #E3F2FD;
}

.scene-tab-btn.is-active {
  background: linear-gradient(135deg, #0072BC 0%, #005A9C 100%);
  color: #fff;
}

.storymap-open-terria {
  margin: 1.25rem 0 0 0;
  font-size: 0.9rem;
}

.storymap-open-terria a {
  color: #6c757d;
  text-decoration: none;
}

.storymap-open-terria a:hover {
  color: #0072BC;
}

/* ------------------------------------------------------------------ */
/* Hero / tail tweaks                                                   */
/* ------------------------------------------------------------------ */

.storymap-hero {
  margin-bottom: 0;
}

.storymap-scroll-hint i {
  animation: storymap-bounce 1.6s ease-in-out infinite;
  display: inline-block;
}

@keyframes storymap-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(4px); }
}

.storymap-edit-fab {
  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
  z-index: 50;
  padding: 0.75rem 1.25rem;
  border-radius: 28px;
  background: linear-gradient(135deg, #0072BC 0%, #005A9C 100%);
  color: #fff;
  font-weight: 600;
  text-decoration: none;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

.storymap-edit-fab:hover,
.storymap-edit-fab:focus {
  color: #fff;
  text-decoration: none;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.35);
}

/* ------------------------------------------------------------------ */
/* Mobile: single column, map pinned on top                             */
/* ------------------------------------------------------------------ */

@media (max-width: 768px) {
  .storymap {
    display: block;
  }

  .storymap-media {
    position: sticky;
    top: 0;
    height: 42vh;
    /* Mantener la geometría al alternar mapas y capítulos de ancho completo.
       El margen evita reservar un hueco antes del primer capítulo sin mapa. */
    margin-bottom: -42vh;
    z-index: 5;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    transition: opacity 0.3s ease, visibility 0.3s ease;
  }

  .storymap.is-full-section .storymap-media {
    box-shadow: none;
  }

  .storymap-cards {
    display: block;
  }

  .storymap-card {
    min-height: auto;
    padding: 0 1rem;
  }

  .storymap-card.is-full-width {
    min-height: auto;
    padding: 0 1rem;
  }

  .storymap-card-body {
    margin: 1.25rem 0;
    padding: 1.5rem;
    opacity: 1;
    transform: none;
  }

  .storymap:not(.has-story-dashboards):not(.is-slides):not(.has-no-map) .storymap-card:first-child:not(.is-full-width) .storymap-card-body {
    margin-top: calc(42vh + 1.25rem);
  }

  .storymap-card.is-full-width .storymap-card-body {
    max-width: none;
  }

  .storymap-card-header h2 {
    font-size: 1.2rem;
  }

  .storymap-progress {
    left: .5rem;
    gap: .25rem;
  }

  .storymap-progress .storymap-dot { display: none; }
  .storymap-nav-btn { width: 44px; height: 44px; margin: 0; }
  .storymap-map-pane > .storymap-visual-fullscreen {
    left: auto;
    right: 1rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .storymap-card-body,
  .storymap-media,
  .storymap-placeholder,
  .storymap-dot,
  .storymap-nav-btn,
  .storymap-media-image {
    transition: none;
  }

  .storymap-scroll-hint i,
  .storymap-spinner {
    animation: none;
  }
}
