/* ═══════════════════════════════════════════════
   BGM Player (Apple Music style)
   執筆オーバーレイ内で表示される浮遊プレイヤー
══════════════════════════════════════════════════ */

.bgm-player {
  position: fixed;
  right: 20px;
  /* bottomNav (z:12000) や writerOverlay の内部要素と重ならないよう最前面に */
  bottom: calc(20px + env(safe-area-inset-bottom, 0px));
  z-index: 20000;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans JP', sans-serif;
  color: #fff;
  pointer-events: auto;
  --bgm-hue: 210;
  --bgm-bg1: hsl(var(--bgm-hue), 55%, 40%);
  --bgm-bg2: hsl(calc(var(--bgm-hue) + 40), 60%, 25%);
  --bgm-accent: hsl(var(--bgm-hue), 85%, 70%);
}

.bgm-player[data-state="hidden"] {
  display: none;
}

/* ── MINI PLAYER ── */
.bgm-mini {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 340px;
  max-width: calc(100vw - 40px);
  padding: 10px 14px 10px 10px;
  background: rgba(28, 28, 30, 0.78);
  backdrop-filter: blur(22px) saturate(1.4);
  -webkit-backdrop-filter: blur(22px) saturate(1.4);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  box-shadow:
    0 12px 32px rgba(0, 0, 0, 0.32),
    0 2px 6px rgba(0, 0, 0, 0.18);
  position: relative;
  overflow: hidden;
  transition: transform 0.22s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.18s;
}

.bgm-player[data-state="expanded"] .bgm-mini {
  display: none;
}

.bgm-mini-artwork {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  overflow: hidden;
  flex-shrink: 0;
  background: linear-gradient(135deg, var(--bgm-bg1), var(--bgm-bg2));
  position: relative;
  box-shadow: inset 0 0 0 0.5px rgba(255, 255, 255, 0.15);
}

.bgm-mini-artwork::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.3), transparent 55%),
    radial-gradient(circle at 70% 80%, rgba(0, 0, 0, 0.25), transparent 60%);
}

.bgm-mini-artwork i {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.6);
  font-size: 16px;
  z-index: 1;
}

.bgm-mini-info {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.bgm-mini-title {
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  letter-spacing: -0.01em;
}

.bgm-mini-artist {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.55);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.bgm-mini-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: #fff;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, transform 0.12s;
  flex-shrink: 0;
  padding: 0;
  font-size: 13px;
}

.bgm-mini-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.bgm-mini-btn:active {
  transform: scale(0.92);
}

.bgm-mini-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 2px;
  background: rgba(255, 255, 255, 0.08);
}

.bgm-mini-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--bgm-accent);
  transition: width 0.2s linear;
}

/* ── EXPANDED PLAYER ── */
.bgm-expanded {
  display: none;
  width: 360px;
  max-width: calc(100vw - 40px);
  max-height: calc(100vh - 40px);
  background:
    linear-gradient(180deg,
      hsla(var(--bgm-hue), 60%, 22%, 0.95) 0%,
      hsla(var(--bgm-hue), 55%, 14%, 0.95) 100%);
  backdrop-filter: blur(28px) saturate(1.6);
  -webkit-backdrop-filter: blur(28px) saturate(1.6);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 20px;
  box-shadow:
    0 24px 60px rgba(0, 0, 0, 0.5),
    0 4px 12px rgba(0, 0, 0, 0.2);
  padding: 16px 20px 20px;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
  animation: bgmSlideUp 0.32s cubic-bezier(0.2, 0.9, 0.25, 1);
}

.bgm-player[data-state="expanded"] .bgm-expanded {
  display: flex;
}

@keyframes bgmSlideUp {
  from { transform: translateY(20px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.bgm-expanded-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-height: 28px;
}

.bgm-collapse-btn,
.bgm-close-btn {
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.1);
  color: #fff;
  cursor: pointer;
  font-size: 11px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, transform 0.12s;
}

.bgm-collapse-btn:hover,
.bgm-close-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}

.bgm-now-playing {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.5);
}

.bgm-artwork-large {
  width: 100%;
  aspect-ratio: 1 / 1;
  max-height: 220px;
  margin: 4px auto 6px;
  border-radius: 14px;
  overflow: hidden;
  position: relative;
  background: linear-gradient(135deg, var(--bgm-bg1), var(--bgm-bg2));
  box-shadow:
    0 16px 34px rgba(0, 0, 0, 0.35),
    inset 0 0 0 0.5px rgba(255, 255, 255, 0.15);
}

.bgm-artwork-large::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at 30% 15%, rgba(255, 255, 255, 0.32), transparent 55%),
    radial-gradient(ellipse at 75% 85%, rgba(0, 0, 0, 0.3), transparent 60%);
}

.bgm-artwork-large i {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.4);
  font-size: 48px;
  z-index: 1;
}

.bgm-artwork-large.playing i {
  animation: bgmPulse 2.4s ease-in-out infinite;
}

@keyframes bgmPulse {
  0%, 100% { transform: scale(1); opacity: 0.4; }
  50% { transform: scale(1.08); opacity: 0.65; }
}

.bgm-track-info {
  text-align: left;
  padding: 0 2px;
}

.bgm-track-title {
  font-size: 17px;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.015em;
  margin-bottom: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bgm-track-artist {
  font-size: 13px;
  color: rgba(255, 255, 255, 0.55);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bgm-progress {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.bgm-progress-track {
  position: relative;
  height: 4px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 2px;
  cursor: pointer;
  transition: height 0.15s;
}

.bgm-progress-track:hover {
  height: 6px;
}

.bgm-progress-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 0%;
  background: rgba(255, 255, 255, 0.85);
  border-radius: inherit;
  transition: width 0.1s linear;
}

.bgm-progress-thumb {
  position: absolute;
  top: 50%;
  left: 0%;
  transform: translate(-50%, -50%);
  width: 10px;
  height: 10px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  opacity: 0;
  transition: opacity 0.15s;
  pointer-events: none;
}

.bgm-progress-track:hover .bgm-progress-thumb {
  opacity: 1;
}

.bgm-progress-times {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.45);
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.bgm-controls {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 2px 0;
}

.bgm-ctrl-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: rgba(255, 255, 255, 0.9);
  cursor: pointer;
  font-size: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background 0.12s, transform 0.12s, color 0.12s;
}

.bgm-ctrl-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

.bgm-ctrl-btn:active {
  transform: scale(0.92);
}

.bgm-ctrl-btn[data-active="true"] {
  color: var(--bgm-accent);
}

.bgm-play-large {
  width: 54px;
  height: 54px;
  background: rgba(255, 255, 255, 0.1);
  font-size: 18px;
}

.bgm-play-large:hover {
  background: rgba(255, 255, 255, 0.18);
}

.bgm-volume {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 2px 2px 4px;
  color: rgba(255, 255, 255, 0.5);
  font-size: 11px;
}

.bgm-volume-track {
  flex: 1;
  position: relative;
  height: 3px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 2px;
  cursor: pointer;
}

.bgm-volume-fill {
  position: absolute;
  inset: 0 auto 0 0;
  width: 60%;
  background: rgba(255, 255, 255, 0.75);
  border-radius: inherit;
}

.bgm-playlist {
  margin-top: 2px;
  padding-top: 12px;
  border-top: 0.5px solid rgba(255, 255, 255, 0.12);
  max-height: 180px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}

.bgm-playlist::-webkit-scrollbar {
  width: 4px;
}

.bgm-playlist::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 2px;
}

.bgm-playlist-title {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: rgba(255, 255, 255, 0.45);
  margin-bottom: 10px;
  padding: 0 2px;
}

.bgm-playlist-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.bgm-playlist-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s;
}

.bgm-playlist-item:hover {
  background: rgba(255, 255, 255, 0.08);
}

.bgm-playlist-item.active {
  background: rgba(255, 255, 255, 0.08);
}

.bgm-playlist-item-artwork {
  width: 36px;
  height: 36px;
  border-radius: 4px;
  background: linear-gradient(135deg, var(--item-bg1, #555), var(--item-bg2, #333));
  flex-shrink: 0;
  box-shadow: inset 0 0 0 0.5px rgba(255, 255, 255, 0.1);
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: rgba(255, 255, 255, 0.5);
  font-size: 11px;
  overflow: hidden;
}

.bgm-playlist-item-thumb {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.bgm-playlist-item-info {
  flex: 1;
  min-width: 0;
}

.bgm-playlist-item-title {
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bgm-playlist-item-artist {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.5);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}

.bgm-playlist-item-eq {
  width: 14px;
  height: 14px;
  display: none;
}

.bgm-playlist-item.active .bgm-playlist-item-eq {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
}

.bgm-playlist-item-eq span {
  display: inline-block;
  width: 2px;
  background: var(--bgm-accent);
  border-radius: 1px;
  animation: bgmEq 0.9s ease-in-out infinite;
}

.bgm-playlist-item-eq span:nth-child(1) { height: 60%; animation-delay: 0s; }
.bgm-playlist-item-eq span:nth-child(2) { height: 100%; animation-delay: 0.15s; }
.bgm-playlist-item-eq span:nth-child(3) { height: 70%; animation-delay: 0.3s; }

.bgm-playlist-item-delete {
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.4);
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  font-size: 10px;
  transition: background 0.12s, color 0.12s;
  flex-shrink: 0;
}

.bgm-playlist-item:hover .bgm-playlist-item-delete {
  display: inline-flex;
}

.bgm-playlist-item-delete:hover {
  background: rgba(255, 80, 80, 0.2);
  color: #ff8080;
}

/* ── SEARCH FORM (上部配置) ── */
.bgm-search-row {
  position: relative;
  display: flex;
  align-items: center;
  margin-bottom: 4px;
}

/* 検索結果が出ているときは UP NEXT を隠す (スクロール不要で結果が見えるUX) */
.bgm-playlist:has(.bgm-search-results.active) .bgm-playlist-native {
  display: none;
}

.bgm-playlist-native {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 0.5px solid rgba(255, 255, 255, 0.1);
}

.bgm-search-icon {
  position: absolute;
  left: 11px;
  top: 50%;
  transform: translateY(-50%);
  color: rgba(255, 255, 255, 0.4);
  font-size: 11px;
  pointer-events: none;
}

.bgm-search-input {
  flex: 1;
  min-width: 0;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 10px;
  padding: 8px 32px 8px 30px;
  font-size: 12px;
  color: #fff;
  outline: none;
  font-family: inherit;
  transition: border-color 0.12s, background 0.12s;
}

.bgm-search-input::placeholder {
  color: rgba(255, 255, 255, 0.35);
}

.bgm-search-input:focus {
  border-color: rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.12);
}

.bgm-search-clear {
  position: absolute;
  right: 6px;
  top: 50%;
  transform: translateY(-50%);
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.45);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 11px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.bgm-search-clear:hover {
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
}

.bgm-search-results {
  margin-top: 8px;
  display: none;
  flex-direction: column;
  gap: 2px;
  max-height: 220px;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.2) transparent;
}

.bgm-search-results.active {
  display: flex;
}

.bgm-search-results::-webkit-scrollbar { width: 4px; }
.bgm-search-results::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.2); border-radius: 2px; }

.bgm-search-loading,
.bgm-search-empty,
.bgm-search-error {
  padding: 10px 6px;
  color: rgba(255, 255, 255, 0.55);
  font-size: 11px;
  text-align: center;
}

.bgm-search-error {
  color: #ff9f9f;
}

.bgm-search-fallback {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 2px 0;
  padding: 10px 12px;
  background: rgba(255, 0, 0, 0.12);
  border: 1px solid rgba(255, 80, 80, 0.28);
  border-radius: 10px;
  color: #fff;
  text-decoration: none;
  font-size: 12px;
  font-weight: 500;
  transition: background 0.12s;
}

.bgm-search-fallback:hover {
  background: rgba(255, 0, 0, 0.22);
}

.bgm-search-fallback > i.fa-youtube {
  color: #ff5050;
  font-size: 18px;
  flex-shrink: 0;
}

.bgm-search-fallback-ext {
  margin-left: auto;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.55);
}

.bgm-search-hint {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.45);
  padding: 6px 2px 2px;
  line-height: 1.45;
}

.bgm-search-input.bgm-add-input-error {
  border-color: #ff5050 !important;
  animation: bgmShake 0.3s;
}

@keyframes bgmShake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-4px); }
  75% { transform: translateX(4px); }
}

.bgm-search-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s;
}

.bgm-search-item:hover {
  background: rgba(255, 255, 255, 0.08);
}

.bgm-search-thumb {
  width: 44px;
  height: 44px;
  border-radius: 4px;
  object-fit: cover;
  flex-shrink: 0;
  background: rgba(255, 255, 255, 0.08);
}

.bgm-search-thumb-empty {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #ff5050;
  font-size: 18px;
}

.bgm-search-meta {
  flex: 1;
  min-width: 0;
}

.bgm-search-title {
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.bgm-search-sub {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.5);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.bgm-search-add-icon {
  font-size: 11px;
  color: rgba(255, 255, 255, 0.4);
  padding: 6px;
  flex-shrink: 0;
}

.bgm-search-item:hover .bgm-search-add-icon {
  color: #fff;
}

/* ── DRAG ── */
.bgm-mini,
.bgm-expanded-header {
  cursor: grab;
}

.bgm-mini:active,
.bgm-expanded-header:active {
  cursor: grabbing;
}

.bgm-player.dragging {
  transition: none !important;
  user-select: none;
  opacity: 0.95;
}

.bgm-player.dragging * {
  pointer-events: none;
}

/* ── YouTube mode ── */
.bgm-artwork-large.youtube-mode i {
  display: none;
}

.bgm-artwork-large.youtube-mode::after {
  display: none;
}

.bgm-player[data-paused="true"] .bgm-playlist-item-eq span {
  animation-play-state: paused;
}

@keyframes bgmEq {
  0%, 100% { transform: scaleY(0.4); }
  50% { transform: scaleY(1); }
}

/* ── Responsive: モバイルは bottomNav (高さ~68px) の上に退避 ── */
@media (max-width: 768px) {
  .bgm-player {
    right: 10px;
    /* bottomNav の高さ + safe area を避ける */
    bottom: calc(76px + env(safe-area-inset-bottom, 0px));
  }
}

@media (max-width: 520px) {
  .bgm-mini {
    width: calc(100vw - 20px);
  }

  .bgm-expanded {
    width: calc(100vw - 20px);
    padding: 14px 16px 16px;
  }

  .bgm-artwork-large {
    max-height: 180px;
  }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
  .bgm-expanded,
  .bgm-artwork-large.playing i,
  .bgm-playlist-item-eq span {
    animation: none !important;
  }
}
