/* Mobile layout fixes — keep living-line visuals without creating sideways scroll. */
html, body, #root {
  max-width: 100%;
  overflow-x: hidden;
}

.app,
.course-app {
  min-width: 0 !important;
  max-width: 100%;
}

@media (max-width: 900px) {
  .scene,
  .course-scene {
    overflow-x: hidden !important;
    overflow-y: auto;
    overscroll-behavior-x: none;
  }

  /* Pin decorative field inside the viewport so it can't widen scrollWidth. */
  .form-stage {
    right: max(-18px, calc(100% - min(72vw, 300px) - 8px)) !important;
    left: auto !important;
    width: min(68vw, 280px) !important;
    max-width: calc(100% - 12px);
    bottom: 12px !important;
  }

  .story-card.intro ~ .form-stage {
    right: max(-14px, calc(100% - min(64vw, 270px) - 8px)) !important;
    width: min(64vw, 270px) !important;
    bottom: 8px !important;
  }

  .app[data-agent-mode="true"] .form-stage {
    right: max(-12px, calc(100% - min(58vw, 240px) - 8px)) !important;
    width: min(58vw, 240px) !important;
    bottom: 10px !important;
  }

  .course-line-field,
  .course-app[data-course-screen="hub"] .course-line-field {
    right: max(-16px, calc(100% - min(58vw, 240px) - 10px)) !important;
    left: auto !important;
    width: min(58vw, 240px) !important;
    max-width: calc(100% - 20px);
    bottom: 72px !important;
    pointer-events: none;
  }

  .course-app[data-course-screen="lesson"] .course-line-field {
    width: min(42vw, 168px) !important;
    max-width: 168px;
    right: max(-10px, calc(100% - min(42vw, 168px) - 8px)) !important;
    bottom: 84px !important;
    opacity: 0.92;
  }

  .course-hub {
    width: 100% !important;
    max-width: 100%;
    padding-left: max(16px, env(safe-area-inset-left)) !important;
    padding-right: max(16px, env(safe-area-inset-right)) !important;
    padding-bottom: 180px !important;
    box-sizing: border-box;
  }

  .course-lesson-picker,
  .course-hub-copy,
  .course-journey-summary,
  .course-step,
  .course-activity,
  .course-narration {
    max-width: 100%;
    box-sizing: border-box;
  }

  .course-step {
    padding-left: max(16px, env(safe-area-inset-left)) !important;
    padding-right: max(16px, env(safe-area-inset-right)) !important;
    padding-bottom: 160px !important;
  }

  .course-step-heading h1 {
    max-width: 100%;
    overflow-wrap: anywhere;
    text-wrap: balance;
  }

  .course-hub-copy h1 {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  /* Demo chrome: keep actions usable on narrow phones. */
  .titlebar {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
  }

  .header-actions {
    max-width: 100%;
    flex-wrap: nowrap;
  }

  .launch-course {
    max-width: 42vw;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .transport {
    max-width: 100%;
    box-sizing: border-box;
  }

  .script-switch {
    max-width: 100%;
    box-sizing: border-box;
  }


  /* Course footer: keep mute / call / skip tappable above the iOS home indicator. */
  .course-app[data-course-screen="lesson"] {
    grid-template-rows: 56px minmax(0, 1fr) calc(68px + env(safe-area-inset-bottom, 0px)) !important;
  }

  .course-navigation {
    height: calc(68px + env(safe-area-inset-bottom, 0px)) !important;
    padding-bottom: env(safe-area-inset-bottom, 0px) !important;
    box-sizing: border-box;
    align-items: center;
    overflow: visible;
  }

  .course-agent-dock {
    gap: 8px !important;
    max-width: 100%;
    flex-wrap: nowrap;
  }

  .course-call {
    min-width: 0 !important;
    min-height: 40px !important;
    height: 40px !important;
    padding: 0 10px !important;
    font-size: 7px !important;
    letter-spacing: 0.08em !important;
  }

  .course-mic {
    width: auto !important;
    min-width: 44px !important;
    min-height: 40px !important;
    height: 40px !important;
    padding: 0 10px !important;
    overflow: visible !important;
    font-size: 7px !important;
    letter-spacing: 0.08em !important;
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
  }

  .course-mic:after {
    content: none !important;
  }

  .course-back,
  .course-continue {
    min-height: 44px !important;
    height: 44px !important;
  }

  .course-continue {
    width: auto !important;
    min-width: 96px !important;
    padding: 0 10px !important;
  }

  /* Quiz / choice cards: never use the desktop 2-col orphan layout on phones.
     Edo's screenshot: option 02 floated beside 01 while 03 wrapped alone. */
  .course-choice-grid,
  .course-choice-grid.compact,
  .course-choice-grid.wide,
  .creation-grid,
  .career-grid,
  .dataset-grid {
    grid-template-columns: 1fr !important;
  }

  .course-choice-grid button {
    min-height: 52px;
    width: 100%;
  }
}

@media (max-width: 430px) {
  .course-app[data-course-screen="lesson"] {
    grid-template-rows: 56px minmax(0, 1fr) calc(72px + env(safe-area-inset-bottom, 0px)) !important;
  }

  .course-navigation {
    grid-template-columns: 44px minmax(0, 1fr) minmax(96px, 112px) !important;
    height: calc(72px + env(safe-area-inset-bottom, 0px)) !important;
    gap: 4px;
  }

  .course-mic {
    min-width: 44px !important;
    width: auto !important;
    font-size: 6.5px !important;
  }

  .course-call {
    min-width: 72px !important;
    font-size: 6px !important;
  }

  .style-switch span {
    min-width: 0 !important;
    padding: 0 4px !important;
    font-size: 5px !important;
  }

  .launch-course {
    padding: 0 7px !important;
    font-size: 5.5px !important;
    letter-spacing: 0.08em !important;
  }

  .course-app[data-course-screen="lesson"] .course-line-field {
    display: none;
  }

  /* While Alma is talking / on a live call, pin the orange living line bottom-right. */
  .course-app[data-course-screen="lesson"]:has(.course-call.active) .course-line-field,
  .course-app[data-course-screen="lesson"]:has(.course-narration.agent-live) .course-line-field {
    display: block !important;
    position: fixed !important;
    z-index: 25 !important;
    right: max(8px, env(safe-area-inset-right)) !important;
    bottom: calc(84px + env(safe-area-inset-bottom, 0px)) !important;
    left: auto !important;
    width: min(38vw, 148px) !important;
    max-width: 148px !important;
    opacity: 0.98;
    pointer-events: none;
  }

  .course-step {
    padding-bottom: 28px !important;
  }

  .course-hub {
    padding-bottom: 120px !important;
  }

  .course-app[data-course-screen="hub"] .course-line-field {
    width: min(48vw, 180px) !important;
    bottom: 24px !important;
  }
}
