/* Final responsive layer. Keep this stylesheet last so phone-specific rules
   can refine the shared desktop system without duplicating it. */
html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html,
body {
  max-width: 100%;
  overflow-x: clip;
}

button,
input,
textarea,
select {
  font: inherit;
}

.episode-heading > div,
.episode-grid > *,
.now-playing > div,
.timeline-item > div,
.source-card > div,
.history-card,
.secondary-actions {
  min-width: 0;
}

.episode-heading h2,
.episode-heading p,
.now-playing h3,
.now-playing p,
.timeline-item h4,
.source-card h3,
.history-card strong,
.history-card p {
  overflow-wrap: anywhere;
}

@media (max-width: 950px) {
  .episode-grid {
    gap: 18px;
  }

  .timeline-column,
  .timeline-column .timeline-card {
    height: auto;
  }

  .timeline-column .timeline-card {
    max-height: min(70dvh, 620px);
  }

  .timeline-column .timeline {
    min-height: 190px;
    max-height: none;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }

  .share-action {
    position: relative;
    z-index: 2;
  }
}

@media (max-width: 620px) {
  :root {
    --mobile-edge: max(16px, env(safe-area-inset-left));
  }

  .topbar,
  main,
  footer {
    padding-left: var(--mobile-edge);
    padding-right: max(16px, env(safe-area-inset-right));
  }

  .topbar {
    height: 70px;
    gap: 10px;
  }

  .top-actions {
    gap: 8px;
  }

  .icon-button,
  .quiet-button,
  .report-button,
  .share-button,
  .bank-back,
  .dialog-head button {
    min-height: 44px;
  }

  main {
    padding-bottom: 56px;
  }

  .hero {
    margin: 42px auto 54px;
  }

  .hero h1 {
    margin: 19px 0 21px;
    font-size: clamp(38px, 12vw, 48px);
    line-height: 1.01;
  }

  .hero-copy {
    margin-bottom: 28px;
    font-size: 15px;
    line-height: 1.55;
  }

  .composer {
    border-radius: 18px;
    padding: 12px;
  }

  .question-row {
    gap: 10px;
  }

  .question-row textarea,
  .question-search input,
  .share-confirmation input {
    font-size: 16px;
  }

  .question-row textarea {
    min-height: 118px;
    padding: 15px 16px;
  }

  .generate-button {
    width: 100%;
    min-width: 0;
    min-height: 52px;
  }

  .composer-tools {
    display: grid;
    grid-template-columns: 1fr;
    align-items: start;
    gap: 12px;
    padding: 14px 2px 2px;
  }

  .composer-tools > .quiet-button {
    width: 100%;
  }

  .option-group {
    width: 100%;
    justify-content: flex-start;
    gap: 16px;
  }

  .option-group label {
    min-height: 38px;
  }

  .duration-plan {
    min-height: 32px;
  }

  .generation-panel {
    border-radius: 16px;
    padding: 20px 16px;
  }

  .generation-head {
    align-items: flex-start;
    gap: 12px;
  }

  .generation-head h2 {
    font-size: 22px;
    line-height: 1.2;
  }

  .episode-shell,
  .shared-view .episode-shell {
    padding-top: 38px;
  }

  .shared-episode-welcome {
    margin: 12px 0 25px;
    padding: 18px;
  }

  .episode-heading {
    margin-bottom: 20px;
  }

  .episode-heading h2 {
    margin-top: 11px;
    font-size: clamp(31px, 9.5vw, 40px);
    line-height: 1.04;
  }

  .episode-heading p {
    font-size: 13px;
    line-height: 1.5;
  }

  .episode-actions,
  .secondary-actions,
  .secondary-actions > * {
    width: 100%;
  }

  .secondary-actions {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
  }

  .player-card,
  .timeline-card {
    border-radius: 16px;
  }

  .stage-badge {
    left: 10px;
    top: 10px;
    max-width: calc(100% - 20px);
  }

  .narration-stage {
    padding: 10px;
  }

  .presentation-slide {
    padding: 13px 15px 11px;
  }

  .slide-rule {
    margin: 7px 0;
  }

  .slide-section {
    margin-bottom: 5px;
  }

  .narration-copy {
    max-width: 96%;
  }

  .narration-copy h3 {
    margin-bottom: 6px;
    font-size: clamp(18px, 5.8vw, 25px);
    line-height: 1.04;
  }

  .narration-copy p {
    font-size: clamp(10px, 3.2vw, 14px);
    line-height: 1.3;
  }

  .slide-footer {
    font-size: 6px;
  }

  .video-captions {
    left: 2.5%;
    right: 2.5%;
    bottom: 3%;
    max-height: 45%;
    overflow: hidden;
    padding: 6px 9px;
    font-size: clamp(11px, 3.2vw, 14px);
  }

  .youtube-shield-message {
    max-width: calc(100% - 24px);
  }

  .now-playing {
    min-height: 78px;
    padding: 14px 15px;
  }

  .now-playing h3 {
    font-size: 14px;
    line-height: 1.3;
  }

  .now-playing p {
    font-size: 10px;
    line-height: 1.4;
  }

  .controls {
    height: auto;
    min-height: 112px;
    gap: 4px;
    padding: 10px 12px 12px;
  }

  .controls button {
    min-width: 44px;
    width: 44px;
    height: 44px;
  }

  .controls .play-button {
    width: 52px;
    height: 52px;
  }

  .control-divider {
    margin-left: 6px;
    margin-right: 2px;
  }

  .episode-rating {
    min-width: 0;
    padding-top: 4px;
  }

  .controls .episode-rating button {
    min-width: 35px;
    width: 35px;
    height: 40px;
    font-size: 19px;
  }

  .timeline-column .timeline-card {
    max-height: min(68dvh, 560px);
  }

  .timeline-head {
    padding: 18px 16px;
  }

  .timeline-item {
    grid-template-columns: 36px minmax(0, 1fr) auto;
    gap: 9px;
    min-height: 72px;
    padding: 13px 14px;
  }

  .timeline-icon {
    width: 32px;
    height: 32px;
  }

  .timeline-item h4 {
    font-size: 12px;
  }

  .share-action {
    padding: 15px 14px max(15px, env(safe-area-inset-bottom));
  }

  .share-button,
  .share-confirmation {
    width: 100%;
  }

  .share-confirmation > div {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .share-confirmation button {
    min-height: 44px;
  }

  .sources-section,
  .history-section {
    margin-top: 55px;
  }

  .section-head h2 {
    font-size: 28px;
  }

  .source-card {
    grid-template-columns: 68px minmax(0, 1fr);
    padding: 13px;
  }

  .source-card details {
    grid-column: 1 / -1;
  }

  .history-card {
    min-height: 154px;
    padding: 16px;
  }

  footer {
    padding-top: 24px;
    padding-bottom: max(26px, env(safe-area-inset-bottom));
    line-height: 1.5;
  }

  .question-bank-dialog {
    width: calc(100% - 12px);
    height: calc(100dvh - 12px);
    max-height: calc(100dvh - 12px);
    border-radius: 16px;
  }

  .question-bank-dialog .dialog-head {
    padding-top: max(16px, env(safe-area-inset-top));
  }

  .question-bank-toolbar {
    align-items: stretch;
    flex-direction: column;
  }

  .question-bank-toolbar .bank-back {
    width: 100%;
  }

  .question-bank-content {
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    scrollbar-gutter: auto;
  }

  .bank-card {
    min-height: 58px;
  }

  .toast {
    right: 12px;
    bottom: max(12px, env(safe-area-inset-bottom));
    left: 12px;
    max-width: none;
  }
}

@media (max-width: 360px) {
  .channel-brand strong {
    font-size: 14px;
  }

  .channel-brand small {
    letter-spacing: .12em;
  }

  .hero h1 {
    font-size: 37px;
  }

  .controls {
    padding-inline: 7px;
  }

  .controls button {
    min-width: 40px;
    width: 40px;
  }

  .control-divider {
    display: none;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .topbar {
    height: 60px;
  }

  .hero {
    margin-top: 34px;
  }

  .question-bank-dialog {
    height: calc(100dvh - 8px);
    max-height: calc(100dvh - 8px);
  }

  .timeline-column .timeline-card {
    max-height: 82dvh;
  }
}

@media (hover: none) and (pointer: coarse) {
  .history-card:hover,
  .bank-card:hover {
    transform: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  html:focus-within {
    scroll-behavior: auto;
  }
}
