/*
 * Cloud Music responsive layout
 * Loaded after feature styles so desktop styles remain unchanged and these
 * rules only adapt the existing UI for tablet and touch/mobile screens.
 */

:root {
  --mobile-gutter: 14px;
  --tap-size: 44px;
}

html {
  min-width: 320px;
  min-height: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

body, main, #login, #library-view, #discover-view, #playlists-view, #upload-view,
#song-list, #pl-songs, #disc-playlist-songs {
  min-width: 0;
}

main {
  overflow-x: hidden;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

button, select, input, textarea, label.secondary-btn {
  touch-action: manipulation;
}

button, select, label.secondary-btn {
  -webkit-tap-highlight-color: transparent;
}

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

input[type="range"] {
  min-width: 0;
  accent-color: var(--accent);
  touch-action: pan-y;
}

/* Tablet and compact laptop layouts. */
@media (max-width: 900px) {
  body {
    grid-template-columns: 184px minmax(0, 1fr);
  }

  main {
    padding: 22px 20px;
  }

  #player {
    padding-left: 16px;
    padding-right: 16px;
    gap: 10px;
  }

  #player .seek {
    gap: 7px;
  }
}

/* Phones and small tablets: content first, compact player, bottom navigation. */
@media (max-width: 700px) {
  :root {
    --mobile-gutter: 14px;
  }

  body {
    height: 100vh;
    height: 100dvh;
    min-height: 100svh;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 54px minmax(0, 1fr) auto auto;
    grid-template-areas:
      "top"
      "main"
      "play"
      "side";
  }

  .topbar {
    min-width: 0;
    padding: 0 var(--mobile-gutter);
    gap: 10px;
  }

  .logo {
    min-width: 0;
    font-size: 18px;
    white-space: nowrap;
  }

  #status {
    min-width: 0;
    max-width: 56%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: right;
    font-size: 11px;
  }

  .sidebar {
    z-index: 20;
    display: flex;
    flex-direction: row;
    align-items: stretch;
    justify-content: space-around;
    gap: 2px;
    padding: 4px 6px max(4px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
    box-shadow: 0 -5px 18px rgba(0, 0, 0, .18);
  }

  .sidebar button {
    flex: 1 1 0;
    min-width: 0;
    min-height: 48px;
    width: auto;
    padding: 7px 3px 5px;
    border: 0;
    border-bottom: 3px solid transparent;
    border-radius: 4px 4px 0 0;
    text-align: center;
    font-size: 12px;
    line-height: 1.2;
    white-space: nowrap;
  }

  .sidebar button.active {
    border-left-color: transparent;
    border-bottom-color: var(--accent);
    background: #333;
  }

  #storage {
    display: none !important;
  }

  main {
    min-height: 0;
    padding: 18px var(--mobile-gutter) 22px;
    overflow-y: auto;
    overflow-x: hidden;
    scrollbar-gutter: auto;
    -webkit-overflow-scrolling: touch;
  }

  h2 {
    margin-bottom: 14px;
    font-size: 21px;
  }

  .card {
    width: min(100%, 420px);
    margin: clamp(18px, 7vh, 54px) auto 24px;
    padding: 20px;
  }

  input[type="text"],
  input[type="password"],
  input[type="email"],
  input[type="search"],
  textarea,
  select {
    max-width: 100%;
    font-size: 16px;
  }

  button.primary,
  button.secondary,
  .secondary-btn {
    min-height: var(--tap-size);
  }

  .tools {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
    width: 100%;
  }

  .tools .search {
    width: 100%;
    min-width: 0;
    max-width: none;
  }

  .tools select {
    min-width: 0;
    max-width: 46vw;
    padding: 9px 7px;
  }

  /* The desktop song table becomes a compact touch-friendly list. */
  .song-head {
    display: none;
  }

  .song-row {
    grid-template-columns: 42px minmax(0, 1fr) 34px 34px 34px;
    gap: 5px;
    min-height: 58px;
    padding: 7px 2px;
  }

  .song-row .vis,
  .song-row .size {
    display: none;
  }

  .song-row .thumb {
    width: 42px;
    height: 42px;
  }

  .song-row button {
    min-width: 34px;
    min-height: 40px;
    padding: 4px;
  }

  /* Discover cards remain two-up where possible and never overflow. */
  .grid {
    grid-template-columns: repeat(auto-fill, minmax(min(142px, 100%), 1fr));
    gap: 14px 11px;
  }

  .discover-playlist-grid,
  .pl-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(146px, 100%), 1fr));
    gap: 12px;
  }

  .discover-playlist-card,
  .pl-card {
    padding: 14px;
  }

  .song-card .more {
    width: 36px;
    height: 36px;
    opacity: 1;
  }

  .discover-tabs {
    position: sticky;
    top: -18px;
    z-index: 3;
    padding-top: 4px;
    background: var(--bg);
  }

  .discover-tabs button {
    min-height: 42px;
    flex: 1 1 0;
    padding: 8px 10px;
  }

  /* Playlist creation and detail actions wrap cleanly on narrow screens. */
  .pl-new {
    width: 100%;
    max-width: none;
    gap: 8px;
  }

  .pl-new input {
    min-width: 0;
    flex: 1 1 auto;
  }

  .pl-new button {
    flex: 0 0 auto;
  }

  .pl-head {
    align-items: stretch;
    flex-direction: column;
    gap: 12px;
    margin: 14px 0;
  }

  .pl-title-wrap {
    width: 100%;
  }

  .pl-title-wrap h2 {
    overflow-wrap: anywhere;
  }

  .pl-actions {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  .pl-actions button {
    min-width: 0;
    min-height: 42px;
    padding: 8px 9px;
    white-space: normal;
  }

  .pl-row {
    grid-template-columns: 42px minmax(0, 1fr) 36px;
    gap: 8px;
    min-height: 58px;
    padding: 7px 2px;
  }

  .pl-row .thumb {
    width: 42px;
    height: 42px;
  }

  .pl-row .more {
    min-width: 36px;
    min-height: 40px;
  }

  #pl-back,
  #disc-playlist-back {
    min-height: 42px;
    margin-bottom: 8px;
  }

  /* Upload: larger touch target and no fixed-width form assumptions. */
  .up-form,
  #queue {
    width: 100%;
    max-width: none;
  }

  .drop {
    padding: 28px 16px;
    line-height: 1.5;
  }

  .up-thumb-row,
  .edit-art-row {
    flex-wrap: wrap;
  }

  .up-item > span {
    display: block;
    overflow-wrap: anywhere;
  }

  /* Player uses two compact rows: track/controls and seek bar. */
  #player {
    min-width: 0;
    min-height: 78px;
    grid-template-columns: minmax(0, 1fr) auto;
    grid-template-rows: 42px 20px;
    align-items: center;
    gap: 1px 6px;
    padding: 5px 10px 6px;
    padding-bottom: max(6px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--line);
  }

  #player .np {
    grid-column: 1;
    grid-row: 1;
    gap: 8px;
    min-width: 0;
  }

  #player .np .thumb {
    width: 36px;
    height: 36px;
    font-size: 16px;
  }

  #player .np-text > div:first-child {
    max-width: 48vw;
    font-size: 12px;
  }

  #player .np-artist {
    font-size: 10px;
  }

  #player .controls {
    grid-column: 2;
    grid-row: 1;
    justify-content: flex-end;
    white-space: nowrap;
  }

  #player .controls button {
    min-width: 29px;
    min-height: 36px;
    padding: 4px 3px;
    font-size: 16px;
  }

  #player .controls .big {
    font-size: 22px;
  }

  #player .controls .sm {
    font-size: 12px;
  }

  #player .seek {
    grid-column: 1 / -1;
    grid-row: 2;
    width: 100%;
    gap: 6px;
    font-size: 10px;
  }

  #player .seek input {
    height: 20px;
  }

  #player #expand {
    display: none;
  }

  /* Dialogs become bottom sheets for easier one-handed use. */
  #edit,
  #picker {
    align-items: flex-end;
    padding-top: 12px;
  }

  .edit-box,
  .picker-box {
    width: 100%;
    max-width: 560px;
    max-height: min(88dvh, 760px);
    padding: 20px var(--mobile-gutter) max(20px, env(safe-area-inset-bottom));
    border-radius: 16px 16px 0 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .edit-actions,
  .pk-actions {
    display: flex;
    gap: 8px;
  }

  .edit-actions > *,
  .pk-actions > * {
    flex: 1 1 0;
  }

  .pk-new {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 8px;
  }

  .pk-new input {
    min-width: 0;
  }

  .pk-list {
    max-height: min(32dvh, 260px);
  }

  .menu {
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 24px);
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .menu button {
    min-height: 42px;
    font-size: 14px;
  }

  .ly-text {
    font-size: 15px;
    line-height: 1.75;
    overflow-wrap: anywhere;
  }

  #ly-area {
    min-height: 40dvh;
    font-size: 16px;
  }

  .ly-actions {
    display: flex;
    gap: 8px;
  }

  .ly-actions > * {
    flex: 1 1 0;
  }

  /* Full-screen player: controls and artwork scale with the device. */
  #fs .fs-top {
    top: max(8px, env(safe-area-inset-top));
    right: 10px;
    left: 10px;
    justify-content: flex-end;
    gap: 6px;
  }

  #fs .fs-tab {
    min-height: 40px;
    padding: 8px 11px;
    font-size: 12px;
  }

  #fs .fs-btn {
    width: 40px;
    height: 40px;
    flex: 0 0 40px;
  }

  #fs .fs-main {
    padding: max(62px, calc(env(safe-area-inset-top) + 58px)) 14px 18px;
  }

  #fs .fs-art {
    width: min(68vw, 34vh, 300px);
    font-size: 64px;
  }

  #fs .fs-title {
    max-width: 90vw;
    margin-top: 18px;
    font-size: clamp(18px, 5vw, 22px);
  }

  #fs .fs-artist {
    font-size: 13px;
  }

  #fs .fs-seek {
    gap: 7px;
    margin-top: 18px;
  }

  #fs .fs-controls {
    gap: 7px;
    margin-top: 10px;
  }

  #fs .fs-controls button {
    min-width: 40px;
    min-height: 44px;
    padding: 6px;
    font-size: 24px;
  }

  #fs .fs-controls .big {
    font-size: 42px;
  }

  #fs .fs-controls .sm {
    font-size: 16px;
  }

  #fs .fs-side {
    position: absolute;
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: 100%;
    max-height: 52%;
    padding: 14px var(--mobile-gutter) max(16px, env(safe-area-inset-bottom));
    border-top: 1px solid #333;
    border-left: 0;
    border-radius: 14px 14px 0 0;
    overflow-y: auto;
    overscroll-behavior: contain;
  }
}

/* Very narrow phones: prevent the player and action buttons from colliding. */
@media (max-width: 360px) {
  :root {
    --mobile-gutter: 11px;
  }

  .logo {
    font-size: 16px;
  }

  #status {
    max-width: 48%;
    font-size: 10px;
  }

  .sidebar button {
    font-size: 11px;
  }

  .tools {
    grid-template-columns: minmax(0, 1fr);
  }

  .tools select {
    width: 100%;
    max-width: none;
  }

  #player {
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 1px 3px;
    padding-right: 7px;
    padding-left: 7px;
  }

  #player .np .thumb {
    width: 32px;
    height: 32px;
  }

  #player .controls button {
    min-width: 26px;
    padding-right: 2px;
    padding-left: 2px;
    font-size: 14px;
  }

  #player .controls .big {
    font-size: 20px;
  }
}

/* Short landscape phones have less vertical room for the persistent player. */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  body {
    grid-template-rows: 46px minmax(0, 1fr) 70px 48px;
  }

  .topbar .logo {
    font-size: 16px;
  }

  main {
    padding-top: 12px;
    padding-bottom: 14px;
  }

  .sidebar button {
    min-height: 40px;
    padding-top: 4px;
    padding-bottom: 4px;
  }

  #player {
    min-height: 70px;
    grid-template-rows: 36px 18px;
    padding-top: 3px;
    padding-bottom: 3px;
  }

  #fs .fs-art {
    width: min(38vw, 54vh, 220px);
  }

  #fs .fs-title {
    margin-top: 8px;
  }

  #fs .fs-seek {
    margin-top: 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    scroll-behavior: auto !important;
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}
