/* Shared read-along engine styles (docs/curriculum/stories.md).
   Load order: common.css -> science-ui.css -> this -> story style.css -> responsive.css */

#story-stage {
  position: relative;
  flex: 1;
  min-height: 180px;
  margin: 0 auto;
  width: min(100%, 900px);
  border: var(--border-width) solid var(--border-color);
  border-radius: 20px;
  box-shadow: var(--shadow-md);
  overflow: hidden;
  touch-action: manipulation;
}

#story-stage canvas {
  display: block;
}

body.word-popover-open {
  overflow: hidden;
}

.word-popover[hidden] {
  display: none;
}

.word-popover {
  position: fixed;
  z-index: 2200;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(31, 27, 47, 0.55);
  backdrop-filter: blur(3px);
}

.word-card {
  width: min(430px, 100%);
  padding: clamp(22px, 5vw, 34px);
  border: 4px solid var(--border-color);
  border-radius: 22px;
  background: #FFFDF7;
  box-shadow: 0 12px 0 rgba(33, 33, 33, 0.2);
  text-align: center;
}

.word-card-label {
  display: inline-block;
  margin: 0 0 4px;
  padding: 5px 12px;
  border-radius: 999px;
  background: #2979FF;
  color: white;
  font: 850 0.82rem var(--font-heading);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.word-card h2 {
  margin: 8px 0;
  color: #174EA6;
  font: 900 clamp(1.55rem, 5vw, 2.2rem)/1.2 var(--font-heading);
}

.word-card p {
  margin: 0 0 20px;
  color: var(--color-text);
  font: 700 clamp(1.04rem, 3vw, 1.2rem)/1.55 var(--font-heading);
}

/* The book text: words light up as they are spoken */
#story-text {
  margin: 8px auto 0;
  width: min(96vw, 720px);
  min-height: 3.2em;
  background: var(--color-paper);
  border: var(--border-width) solid var(--border-color);
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
  padding: 10px 16px;
  font-family: var(--font-heading);
  font-size: clamp(1.05rem, 3.8vw, 1.4rem);
  line-height: 1.55;
  text-align: center;
  color: #9E9E9E;
}

#story-text .word {
  transition: color 0.15s ease;
}

#story-text .word.spoken {
  color: var(--color-text);
}

#story-text .word.now {
  color: #E65100;
  text-shadow: 0 0 8px rgba(255, 167, 38, 0.35);
}

#story-text .sparkle {
  color: #B8860B;
  font-weight: bold;
  cursor: pointer;
  text-decoration: underline dotted;
}

#story-text .sparkle.spoken {
  color: #B8860B;
}

#story-text .sparkle.now {
  color: #E65100;
}

/* "I'll read it myself" mode: words are dark and readable from the start,
   and tapping one speaks it - a gentle flash marks the helped word. */
#story-text.self-read .word {
  color: var(--color-text);
  cursor: pointer;
}

#story-text.self-read .word.helped {
  color: #2E7D32;
}

#story-text .word.helped {
  text-shadow: 0 0 6px rgba(102, 187, 106, 0.5);
}

.story-controls {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  padding: 8px 0 max(10px, env(safe-area-inset-bottom));
}

@media (max-width: 480px) {
  .story-controls {
    flex-wrap: nowrap;
    gap: 6px;
  }

  .story-controls .gravity-btn {
    min-height: 44px;
    padding: 8px 10px;
    font-size: clamp(0.82rem, 4vw, 1rem);
  }
}

@media (max-width: 768px) and (max-height: 700px) {
  #story-stage {
    flex: 0 0 min(44dvh, 240px);
    min-height: 180px;
  }
}

@media (max-height: 480px) {
  #story-text {
    min-height: 2.4em;
    padding: 6px 12px;
    font-size: clamp(0.95rem, 3.4vw, 1.1rem);
  }

  #story-stage {
    min-height: 120px;
  }

  .story-controls {
    padding: 5px 0 max(6px, env(safe-area-inset-bottom));
  }
}

/* Generated story panels are 3:2. On narrow screens, let the stage follow
   that ratio instead of stretching into a tall viewport-filling crop. */
@media (max-width: 768px) {
  #story-stage.story-stage--illustrated {
    flex: 0 0 auto;
    width: min(100%, 900px);
    height: auto;
    min-height: 0;
    aspect-ratio: 3 / 2;
  }
}
