@media (max-width: 899px) {
  .page {
    width: min(1180px, calc(100% - 40px));
  }

  .logo-mark {
    width: 56px;
    height: 56px;
    border-radius: 0;
  }

  .logo-pads {
    width: 42px;
    height: 12px;
    gap: 5px;
  }

  .logo-pad {
    border-radius: 2px;
  }

  .pad-grid {
    gap: 16px;
  }

  .pad {
    height: var(--pad-h-tablet);
  }

  .setup-panel .setup-grid {
    row-gap: 14px;
    column-gap: clamp(18px, 3vw, 32px);
  }

  .activity-shell {
    grid-template-columns: 1fr;
  }

  .settings-modal {
    width: min(400px, 94vw);
    max-height: min(78vh, 520px);
  }

  .activity-pads:not(.activity-pads--free) .pad {
    height: calc(min(48vh, 320px) * var(--tile-scale, 1));
  }

  .activity-pads:not(.activity-pads--free)[data-count="4"] .pad,
  .activity-pads:not(.activity-pads--free)[data-count="6"] .pad,
  .activity-pads:not(.activity-pads--free)[data-count="8"] .pad {
    height: calc(min(26vh, 210px) * var(--tile-scale, 1));
  }

  .activity-pads {
    min-height: 0;
  }
}

/* Short viewports (landscape phones): keep everything on one screen. */
@media (max-height: 560px) {
  .activity-pads {
    min-height: 0;
  }
}

@media (max-width: 599px) {
  .page {
    width: calc(100% - 28px);
    gap: 20px;
    padding-top: calc(var(--safe-top) + 12px);
    padding-left: 0;
    padding-right: 0;
  }

  .logo-mark {
    width: 40px;
    height: 40px;
    border-radius: 0;
  }

  .logo-pads {
    width: 34px;
    height: 10px;
    gap: 3px;
  }

  .brand-title {
    font-size: clamp(1.15rem, 5.5vw, 1.45rem);
  }

  .mode-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .mode-card {
    min-height: 105px;
  }

  .setup-panel,
  .preview-panel,
  .panel {
    border-radius: 18px;
    padding: 16px;
  }

  .setup-panel .setup-grid {
    grid-template-columns: 1fr;
  }

  .setup-panel {
    overflow: visible;
  }

  .select-menu.is-sheet {
    max-height: min(78dvh, 520px) !important;
    z-index: 10050 !important;
  }

  .pad-grid {
    gap: 8px;
  }

  .preview-panel .pad-grid,
  body.comic-ui .preview-pad-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    max-width: 100%;
  }

  .preview-panel .pad-wrap,
  body.comic-ui .preview-pad-grid .pad-wrap {
    width: 100%;
    max-width: none;
  }

  .pad {
    height: clamp(155px, 22vh, 180px);
    border-radius: 18px;
  }

  .preview-panel .pad,
  body.comic-ui .preview-panel .pad {
    width: 100%;
    height: auto !important;
    aspect-ratio: 1 / 1;
    max-height: none;
  }

  .preview-panel .finger-label,
  body.comic-ui .preview-panel .finger-label {
    font-size: 0.58rem;
    line-height: 1.1;
  }

  .preview-panel .pad-label,
  body.comic-ui .preview-panel .pad-label {
    font-size: clamp(1rem, 9vw, 1.6rem);
  }

  .preview-panel .pad-label.is-dark,
  body.comic-ui .preview-panel .pad-label.is-dark {
    font-size: clamp(0.7rem, 5.5vw, 1rem);
  }

  .pad-label {
    font-size: clamp(2.3rem, 13vw, 4rem);
  }

  .pad-label.is-dark {
    font-size: clamp(0.9rem, 4vw, 1.25rem);
  }

  .finger-label {
    font-size: 12px;
  }

  .start-btn {
    min-height: 64px;
    font-size: 1.15rem;
  }

  .app-header {
    gap: 8px;
    padding: 8px;
    flex-wrap: wrap;
    align-items: center;
    overflow: visible;
    min-height: 0;
  }

  .brand {
    flex: 1 1 auto;
    min-width: 0;
    max-width: 100%;
  }

  .brand-byline {
    display: none;
  }

  .header-actions {
    flex: 1 1 100%;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 6px;
    flex-shrink: 1;
    max-width: 100%;
  }

  .header-actions .theme-btn--classroom,
  .header-actions .theme-btn--signout,
  .back-to-chooser {
    height: 36px;
    min-height: 36px;
    max-height: 36px;
    padding: 0 10px;
    font-size: 12px;
  }

  .theme-switch {
    width: auto;
    max-width: none;
    height: 36px;
    min-height: 36px;
    flex: 0 0 auto;
  }

  .theme-btn {
    min-width: 0;
    max-width: none;
    padding: 0 8px;
    font-size: 12px;
    gap: 3px;
  }

  .theme-switch .theme-btn > span:not(.icon) {
    display: none;
  }

  .theme-btn .icon {
    font-size: 16px;
  }

  .header-actions .theme-btn--signout > span:not(.icon) {
    display: inline;
  }

  .chrome-group--flow {
    gap: 6px;
  }

  .chrome-group--flow .chrome-btn--action {
    min-width: 62px;
    padding: 0 10px;
    font-size: 0.82rem;
  }

  .activity-chrome {
    flex-wrap: wrap;
    overflow: visible;
    align-items: flex-start;
    row-gap: 6px;
  }

  .activity-chrome__body {
    flex: 1 1 100%;
    width: 100%;
    flex-wrap: wrap;
    row-gap: 6px;
  }

  .beat-light--bar {
    flex: 1 1 100%;
    max-width: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    height: auto;
    min-height: 36px;
    order: 20;
  }

  .beat-light--bar .beat-light__box {
    width: min(100%, calc(180px * var(--beat-flash-width, var(--beat-flash-size, 1))));
  }

  .chrome-group,
  .chrome-btn {
    flex-shrink: 0;
  }

  .activity-pads:not(.activity-pads--free)[data-count="4"],
  .activity-pads:not(.activity-pads--free)[data-count="6"],
  .activity-pads:not(.activity-pads--free)[data-count="8"] {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  html[data-theme="light"] .hero::after {
    display: none;
  }
}

@media (max-width: 359px) {
  .pad-grid:not(.preview-pad-grid):not(.activity-pads--free) {
    grid-template-columns: 1fr;
  }

  .activity-pads:not(.activity-pads--free)[data-count="2"],
  .activity-pads:not(.activity-pads--free)[data-count="3"] {
    grid-template-columns: 1fr;
  }

  .preview-panel .pad-grid,
  body.comic-ui .preview-pad-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }
}

@media (min-width: 1920px) {
  .page {
    width: min(1180px, calc(100% - 64px));
  }
}

/* Short landscape tablets/phones: keep setup usable without horizontal overflow. */
@media (max-height: 700px) and (orientation: landscape) {
  .page {
    gap: 10px;
    padding-top: calc(var(--safe-top) + 6px);
    padding-bottom: calc(var(--safe-bottom) + 8px);
  }

  .mode-card {
    min-height: 0;
    padding: 10px 14px;
  }

  .mode-card .mode-icon {
    width: 56px;
    height: 56px;
  }

  .app-header {
    padding: 6px 8px;
  }

  .setup-panel,
  .preview-panel,
  .panel {
    padding: 12px;
  }

  .start-btn {
    min-height: 48px;
  }
}

/* Very narrow phones */
@media (max-width: 380px) {
  .page {
    width: calc(100% - 16px);
  }

  .mode-copy b {
    font-size: 1rem;
  }

  .mode-copy span {
    font-size: 0.82rem;
  }

  .keep-song-play {
    width: 36px;
    min-height: 36px;
  }
}

/* Large touch / desktop: prevent flex children from forcing page overflow */
.mode-setup,
.setup-panel,
.preview-panel,
.keep-sounds-layout,
.keep-music-browser,
.keep-beat-settings,
.keep-song-pane,
.keep-cat-pane {
  min-width: 0;
}

img,
svg,
video,
canvas {
  max-width: 100%;
  height: auto;
}

button,
input,
select,
textarea {
  max-width: 100%;
}

