@supports (height: 100dvh) {
  html, body {
    height: var(--app-height, 100dvh);
    min-height: var(--app-height, 100dvh);
  }
}

html, body {
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

button, .auth-btn, a.auth-btn, .music-ctrl-btn, .speed-btn, .speed-ctrl-btn, .track-preview-btn {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

@media (max-width: 640px) {
  html, body,
  #start-screen,
  #slideshow,
  #overlay {
    height: var(--app-height, 100dvh);
    min-height: var(--app-height, 100dvh);
  }

  body.slideshow-running {
    overscroll-behavior: none;
    position: fixed;
    width: 100%;
  }

  #start-screen {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 0 0.85rem max(1rem, env(safe-area-inset-bottom, 0px));
    padding-top: max(4.75rem, calc(env(safe-area-inset-top, 0px) + 3.75rem));
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  #start-screen h1 {
    font-size: clamp(1.25rem, 7vw, 1.85rem);
    letter-spacing: 0.06em;
    text-align: center;
    max-width: 95vw;
    line-height: 1.2;
    flex-shrink: 0;
  }

  #start-screen p,
  #start-hint {
    letter-spacing: 0.04em;
    font-size: 0.8rem;
    text-align: center;
    padding: 0 0.35rem;
    line-height: 1.45;
  }

  #auth-bar {
    top: 0;
    left: 0;
    right: 0;
    flex-wrap: nowrap;
    justify-content: flex-end;
    gap: 0.35rem;
    padding: max(0.5rem, env(safe-area-inset-top, 0px)) max(0.65rem, env(safe-area-inset-right, 0px)) 0.5rem max(0.65rem, env(safe-area-inset-left, 0px));
    background: rgba(10, 10, 10, 0.96);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  #auth-bar .auth-user {
    width: auto;
    flex: 1;
    text-align: left;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 0.72rem;
    min-width: 0;
  }

  .auth-btn,
  .auth-btn-primary {
    padding: 0.5rem 0.7rem;
    font-size: 0.72rem;
    min-height: 40px;
    flex-shrink: 0;
  }

  #my-loa-promo {
    width: 100%;
    max-width: 360px;
    padding: 0.5rem 0.15rem 0;
    flex-shrink: 0;
  }

  #my-loa-promo p {
    text-align: center;
    font-size: 0.78rem;
    letter-spacing: 0.06em;
  }

  #my-loa-promo .auth-btn {
    display: inline-flex;
    width: 100%;
    max-width: 320px;
    justify-content: center;
    align-items: center;
    min-height: 44px;
    padding: 0.65rem 1rem;
    font-size: 0.82rem;
    white-space: normal;
    line-height: 1.3;
    text-align: center;
  }

  #speed-picker {
    margin-top: 0.85rem;
    width: 100%;
    flex-shrink: 0;
  }

  #speed-options {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.4rem;
    width: 100%;
    max-width: 340px;
  }

  .speed-btn {
    width: 100%;
    min-height: 44px;
    font-size: 0.74rem;
    padding: 0.5rem 0.35rem;
  }

  #music-picker {
    width: 100%;
    padding: 0;
    margin-top: 1rem;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
  }

  #music-picker label {
    flex-shrink: 0;
  }

  #track-list {
    flex: 1;
    min-height: 120px;
    max-height: min(34vh, 240px);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .track-item {
    flex-wrap: nowrap;
    gap: 0.5rem;
    padding: 0.55rem 0.7rem;
  }

  .track-name {
    font-size: 0.78rem;
  }

  .track-preview-btn {
    width: auto;
    min-width: 72px;
    min-height: 40px;
    flex-shrink: 0;
    padding: 0.4rem 0.65rem;
  }

  #start-btn-wrap {
    position: sticky;
    bottom: max(0.5rem, env(safe-area-inset-bottom, 0px));
    z-index: 5;
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 0.85rem 0 0.25rem;
    margin-top: auto;
    background: linear-gradient(to top, #0a0a0a 65%, transparent);
    flex-shrink: 0;
  }

  #start-btn {
    width: 76px;
    height: 76px;
    font-size: 1.65rem;
    margin: 0;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.45);
  }

  /* Running slideshow — bottom control dock */
  #title {
    bottom: calc(max(0.5rem, env(safe-area-inset-bottom, 0px)) + 8.25rem);
    letter-spacing: 0.06em;
    font-size: clamp(0.68rem, 2.9vw, 0.88rem);
    width: min(94vw, 24rem);
    text-align: center;
    white-space: normal;
    line-height: 1.35;
    word-break: break-word;
    padding: 0 0.5rem;
  }

  #counter {
    top: max(0.65rem, env(safe-area-inset-top, 0px));
    left: max(0.65rem, env(safe-area-inset-left, 0px));
    font-size: 0.65rem;
    background: rgba(0, 0, 0, 0.35);
    padding: 0.2rem 0.45rem;
    border-radius: 999px;
  }

  #stop-btn {
    top: max(0.55rem, env(safe-area-inset-top, 0px));
    right: max(0.55rem, env(safe-area-inset-right, 0px));
    width: 48px;
    height: 48px;
    font-size: 1.35rem;
  }

  #display-mode-btn {
    top: max(0.55rem, env(safe-area-inset-top, 0px));
    right: calc(max(0.55rem, env(safe-area-inset-right, 0px)) + 3.35rem);
    width: 48px;
    height: 48px;
  }

  #display-mode-btn svg {
    width: 1.15rem;
    height: 1.15rem;
  }

  #speed-controls.visible {
    top: auto;
    bottom: max(0.5rem, env(safe-area-inset-bottom, 0px));
    left: max(0.55rem, env(safe-area-inset-left, 0px));
    right: max(0.55rem, env(safe-area-inset-right, 0px));
    transform: none;
    width: auto;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 0.3rem;
    padding: 0.4rem;
    border-radius: 14px;
  }

  .speed-ctrl-btn {
    min-height: 40px;
    font-size: 0.58rem;
    padding: 0.35rem 0.2rem;
    letter-spacing: 0;
    text-align: center;
  }

  #music-controls.visible {
    bottom: calc(max(0.5rem, env(safe-area-inset-bottom, 0px)) + 3.35rem);
    left: max(0.55rem, env(safe-area-inset-left, 0px));
    right: max(0.55rem, env(safe-area-inset-right, 0px));
    transform: none;
    width: auto;
    justify-content: center;
    padding: 0.4rem 0.55rem;
    border-radius: 14px;
  }

  #now-playing {
    max-width: none;
    flex: 1;
    min-width: 0;
    font-size: 0.66rem;
    text-align: center;
  }

  .music-ctrl-btn {
    width: 46px;
    height: 46px;
    font-size: 1rem;
  }

  .music-ctrl-btn svg {
    width: 1.2rem;
    height: 1.2rem;
  }

  body.slideshow-running.controls-hidden {
    cursor: auto;
  }

  body.slideshow-running.controls-hidden #title {
    opacity: 0.85;
  }

  #user-nav {
    flex-wrap: wrap;
    gap: 0.35rem;
    padding: max(0.5rem, env(safe-area-inset-top, 0px)) max(0.65rem, env(safe-area-inset-right, 0px)) 0.5rem max(0.65rem, env(safe-area-inset-left, 0px));
    background: rgba(10, 10, 10, 0.96);
  }

  #user-nav .nav-brand {
    font-size: 0.72rem;
  }

  #user-nav .nav-user {
    font-size: 0.68rem;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #user-nav .nav-links {
    width: 100%;
    justify-content: stretch;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.35rem;
  }

  #user-nav a {
    min-height: 42px;
    justify-content: center;
    font-size: 0.7rem;
    padding: 0.4rem 0.5rem;
    text-align: center;
  }

  #user-nav a.nav-primary {
    grid-column: 1 / -1;
  }
}

@media (max-width: 640px) and (orientation: landscape) {
  #start-screen {
    padding-top: max(3.5rem, calc(env(safe-area-inset-top, 0px) + 2.5rem));
  }

  #start-screen h1 {
    font-size: 1.15rem;
    margin-bottom: 0.35rem;
  }

  #speed-picker {
    margin-top: 0.5rem;
  }

  #music-picker {
    margin-top: 0.5rem;
  }

  #track-list {
    max-height: min(28vh, 120px);
  }

  #start-btn {
    width: 60px;
    height: 60px;
    font-size: 1.35rem;
  }

  #title {
    bottom: calc(max(0.35rem, env(safe-area-inset-bottom, 0px)) + 6.5rem);
    font-size: 0.65rem;
  }

  #music-controls.visible {
    bottom: calc(max(0.35rem, env(safe-area-inset-bottom, 0px)) + 2.75rem);
  }
}

@media (max-width: 380px) {
  #speed-options {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  #speed-controls.visible {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .speed-ctrl-btn:nth-child(4),
  .speed-ctrl-btn:nth-child(5) {
    grid-column: span 1;
  }

  #auth-bar {
    flex-wrap: wrap;
  }

  #auth-bar .auth-user {
    width: 100%;
    flex: none;
  }
}

body.slideshow-fullpage.slideshow-running #stop-btn.visible,
body.slideshow-fullpage.slideshow-running #display-mode-btn.visible,
body.slideshow-fullpage.slideshow-running #music-controls.visible,
body.slideshow-fullpage.slideshow-running #speed-controls.visible,
body.slideshow-fullpage.slideshow-running #counter,
body.slideshow-native-fs-fallback.slideshow-running #stop-btn.visible,
body.slideshow-native-fs-fallback.slideshow-running #display-mode-btn.visible,
body.slideshow-native-fs-fallback.slideshow-running #music-controls.visible,
body.slideshow-native-fs-fallback.slideshow-running #speed-controls.visible,
body.slideshow-native-fs-fallback.slideshow-running #counter {
  z-index: 100;
}

body.slideshow-fullpage.slideshow-running,
body.slideshow-fullpage.slideshow-running #slideshow,
body.slideshow-native-fs-fallback.slideshow-running,
body.slideshow-native-fs-fallback.slideshow-running #slideshow {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  min-height: 100%;
  z-index: 50;
}

body.slideshow-fullpage.slideshow-running #slideshow,
body.slideshow-native-fs-fallback.slideshow-running #slideshow {
  z-index: 1;
}
