* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang TC", sans-serif;
  color: #20313d;
  background: #cdd6df;
}

button, input, select {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: minmax(330px, 39%) 1fr;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 24px;
  background: #dce5ec;
  border-right: 1px solid #b8c5cf;
}

h1 {
  margin: 0 0 24px;
  font-size: 32px;
  color: #203847;
}

.label {
  margin-bottom: 8px;
  font-size: 17px;
  color: #5f7180;
}

.search {
  width: 100%;
  height: 52px;
  padding: 0 16px;
  border: 1px solid #b6c6d2;
  border-radius: 12px;
  outline: none;
  background: white;
  font-size: 20px;
}

.search:focus {
  border-color: #62839a;
  box-shadow: 0 0 0 3px rgba(53, 95, 124, .12);
}

.buttons {
  display: flex;
  justify-content: space-between;
  margin: 12px 0 18px;
}

button {
  min-height: 44px;
  padding: 0 20px;
  border-radius: 11px;
  border: 1px solid transparent;
  cursor: pointer;
}

.primary {
  background: #355f7c;
  color: white;
  font-weight: 600;
}

.secondary {
  background: #eef3f6;
  color: #314957;
  border-color: #afc0cc;
}

.filters {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

select {
  width: 100%;
  min-height: 48px;
  padding: 0 12px;
  border: 1px solid #b7c5d0;
  border-radius: 10px;
  background: white;
  color: #24313b;
  font-size: 18px;
}

.count {
  margin: 18px 2px 10px;
  color: #607482;
  font-size: 17px;
}

.section-title {
  margin-bottom: 9px;
  font-size: 18px;
  font-weight: 700;
}

.song-list {
  flex: 1;
  overflow-y: auto;
  background: #f7f9fb;
  border: 1px solid #b7c5d0;
  border-radius: 10px;
}

.song {
  display: grid;
  grid-template-columns: 74px 1fr;
  align-items: center;
  min-height: 53px;
  padding: 8px 16px;
  cursor: pointer;
  font-size: 19px;
}

.song:hover {
  background: #e7eef3;
}

.song.active {
  background: #bfd2e1;
  color: #1e3443;
}

.song-no {
  font-variant-numeric: tabular-nums;
}

.viewer {
  display: grid;
  grid-template-rows: auto 1fr;
  min-width: 0;
  background: #cdd6df;
}

.viewer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 68px;
  padding: 12px 24px;
}

.song-title {
  font-size: 24px;
  font-weight: 700;
  color: #1f3442;
}

.song-meta {
  font-size: 17px;
  color: #637684;
}

.image-area {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 0;
  overflow: auto;
  padding: 0 18px 18px;
}

#songImage {
  display: none;
  max-width: 100%;
  height: auto;
  background: white;
  box-shadow: 0 2px 10px rgba(36, 49, 59, .10);
}

@media (max-width: 820px) {
  .app {
    grid-template-columns: 42% 58%;
  }

  .sidebar {
    padding: 18px;
  }

  h1 {
    font-size: 27px;
  }

  .song {
    grid-template-columns: 62px 1fr;
    font-size: 17px;
  }

  .song-title {
    font-size: 20px;
  }
}

/* ===== iPad / 平板版 ===== */
@media (max-width: 1180px) {
  .app {
    grid-template-columns: 36% 64%;
  }

  .sidebar {
    padding: 18px;
  }

  h1 {
    font-size: 28px;
    margin-bottom: 18px;
  }

  .label {
    font-size: 15px;
  }

  .search {
    height: 46px;
    font-size: 18px;
  }

  .buttons {
    margin: 10px 0 14px;
  }

  button {
    min-height: 40px;
    padding: 0 16px;
  }

  select {
    min-height: 42px;
    font-size: 16px;
  }

  .count {
    margin: 14px 2px 8px;
    font-size: 15px;
  }

  .section-title {
    font-size: 16px;
  }

  .song {
    grid-template-columns: 62px 1fr;
    min-height: 48px;
    padding: 7px 12px;
    font-size: 17px;
  }

  .viewer-header {
    min-height: 58px;
    padding: 10px 16px;
  }

  .song-title {
    font-size: 21px;
  }

  .song-meta {
    font-size: 15px;
  }

  .image-area {
    padding: 0 10px 10px;
    overflow: auto;
  }

  #songImage {
    max-width: 100%;
    max-height: calc(100dvh - 70px);
    width: auto;
    height: auto;
    object-fit: contain;
  }
}

/* ===== iPad 直向 / 較窄畫面 ===== */
@media (max-width: 850px) {
  .app {
    grid-template-columns: 100%;
    grid-template-rows: 46% 54%;
    overflow: hidden;
  }

  .sidebar {
    border-right: none;
    border-bottom: 1px solid #b8c5cf;
    padding: 14px 18px;
  }

  h1 {
    font-size: 25px;
    margin-bottom: 12px;
  }

  .label {
    display: none;
  }

  .search {
    height: 42px;
    font-size: 17px;
  }

  .buttons {
    margin: 8px 0 10px;
  }

  .filters {
    gap: 8px;
  }

  .count {
    margin: 9px 2px 6px;
  }

  .section-title {
    display: none;
  }

  .song-list {
    min-height: 0;
  }

  .song {
    min-height: 43px;
    font-size: 16px;
  }

  .viewer {
    min-height: 0;
  }

  .viewer-header {
    min-height: 48px;
    padding: 8px 14px;
  }

  .song-title {
    font-size: 18px;
  }

  .song-meta {
    font-size: 14px;
  }

  .image-area {
    padding: 0 8px 8px;
  }

  #songImage {
    max-height: calc(54dvh - 52px);
  }
}

/* ===== iPad 橫向最終調整 ===== */
@media (min-width: 851px) and (max-width: 1400px) {
  .app {
    grid-template-columns: 34% 66%;
  }

  .sidebar {
    padding: 18px 20px;
  }

  .viewer-header {
    min-height: 56px;
    padding: 10px 16px;
  }

  .image-area {
    align-items: flex-start;
    justify-content: center;
    overflow: auto;
    padding: 0 12px 12px;
  }

  #songImage {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: calc(100dvh - 70px);
    object-fit: contain;
  }
}

/* ===== 手機直向版 ===== */
@media (max-width: 600px) and (orientation: portrait) {
  html, body {
    height: 100%;
    overflow: hidden;
  }

  .app {
    display: grid;
    grid-template-columns: 100%;
    grid-template-rows: 44% 56%;
    width: 100vw;
    height: 100dvh;
    overflow: hidden;
  }

  .sidebar {
    min-height: 0;
    padding: 12px 14px;
    border-right: none;
    border-bottom: 1px solid #b8c5cf;
    overflow: hidden;
  }

  h1 {
    margin: 0 0 10px;
    font-size: 24px;
  }

  .label {
    display: none;
  }

  .search {
    height: 42px;
    padding: 0 12px;
    border-radius: 10px;
    font-size: 17px;
  }

  .buttons {
    margin: 8px 0 9px;
  }

  button {
    min-height: 38px;
    padding: 0 16px;
    border-radius: 10px;
    font-size: 16px;
  }

  .filters {
    grid-template-columns: 1fr 1fr;
    gap: 7px;
  }

  select {
    min-height: 39px;
    padding: 0 8px;
    border-radius: 9px;
    font-size: 15px;
  }

  .count {
    margin: 8px 2px 5px;
    font-size: 14px;
  }

  .section-title {
    display: none;
  }

  .song-list {
    min-height: 0;
    height: 100%;
    border-radius: 9px;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .song {
    grid-template-columns: 58px 1fr;
    min-height: 42px;
    padding: 6px 10px;
    font-size: 16px;
  }

  .song-no {
    font-size: 15px;
  }

  .viewer {
    min-height: 0;
    overflow: hidden;
  }

  .viewer-header {
    min-height: 46px;
    padding: 7px 12px;
    gap: 8px;
  }

  .song-title {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 17px;
  }

  .song-meta {
    flex-shrink: 0;
    font-size: 13px;
  }

  .image-area {
    min-height: 0;
    padding: 0 6px 6px;
    overflow: auto;
    align-items: flex-start;
    justify-content: center;
    -webkit-overflow-scrolling: touch;
  }

  #songImage {
    display: block;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: calc(56dvh - 52px);
    object-fit: contain;
    box-shadow: none;
  }
}

/* 超窄手機 */
@media (max-width: 390px) and (orientation: portrait) {
  .app {
    grid-template-rows: 46% 54%;
  }

  .sidebar {
    padding: 10px 11px;
  }

  h1 {
    font-size: 22px;
    margin-bottom: 8px;
  }

  .search {
    height: 40px;
    font-size: 16px;
  }

  button {
    min-height: 36px;
    font-size: 15px;
  }

  select {
    min-height: 37px;
    font-size: 14px;
  }

  .song {
    grid-template-columns: 54px 1fr;
    min-height: 40px;
    font-size: 15px;
  }

  .viewer-header {
    min-height: 44px;
  }

  .song-title {
    font-size: 16px;
  }

  .song-meta {
    font-size: 12px;
  }

  #songImage {
    max-height: calc(54dvh - 48px);
  }
}

/* ===== 修正手機 / iPad 歌曲清單滑動 ===== */
@media (max-width: 850px) {
  .sidebar {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
  }

  .song-list {
    flex: 1 1 auto;
    height: auto !important;
    min-height: 0;
    overflow-y: auto !important;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
    overscroll-behavior: contain;
  }
}

/* 手機直向再保險 */
@media (max-width: 600px) and (orientation: portrait) {
  .song-list {
    flex: 1 1 0;
    height: auto !important;
    min-height: 80px;
    overflow-y: scroll !important;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-y;
  }
}
