/* ─── Переменные — фоллбэки для тестирования вне Telegram ─────────────────── */
:root {
  --tg-theme-bg-color: #1a1a2e;
  --tg-theme-secondary-bg-color: #16213e;
  --tg-theme-text-color: #eaeaea;
  --tg-theme-hint-color: #8892b0;
  --tg-theme-link-color: #64ffda;
  --tg-theme-button-color: #0f3460;
  --tg-theme-button-text-color: #ffffff;

  /* Дополнительные токены, вычисленные из темы */
  --accent: var(--tg-theme-link-color, #64ffda);
  --surface: var(--tg-theme-secondary-bg-color, #16213e);
  --text-primary: var(--tg-theme-text-color, #eaeaea);
  --text-secondary: var(--tg-theme-hint-color, #8892b0);
  --btn-bg: var(--tg-theme-button-color, #0f3460);
  --btn-text: var(--tg-theme-button-text-color, #ffffff);

  --cover-size: clamp(200px, 55vw, 280px);
  --radius-cover: 20px;
  --transition: 0.25s ease;
}

/* ─── Сброс и базовые стили ────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden; /* Web App не должен скроллиться */
}

body {
  background-color: var(--tg-theme-bg-color);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ─── Главный контейнер плеера ──────────────────────────────────────────────── */
.player {
  width: 100%;
  max-width: 420px;
  padding: 32px 28px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

/* ─── Обложка ───────────────────────────────────────────────────────────────── */
.cover-wrapper {
  position: relative;
  width: var(--cover-size);
  height: var(--cover-size);
}

.cover {
  width: 100%;
  height: 100%;
  border-radius: var(--radius-cover);
  background: var(--surface);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
  z-index: 1;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.cover-placeholder {
  font-size: clamp(80px, 22vw, 110px);
  color: var(--text-secondary);
  line-height: 1;
}

#cover-img {
  position: absolute; /* перекрывает иконку-плейсхолдер */
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;           /* скрыт по умолчанию — показывается после загрузки */
  transition: opacity 0.3s ease;
}

/* Кольцо-пульсация вокруг обложки при воспроизведении */
.cover-ring {
  position: absolute;
  inset: -8px;
  border-radius: calc(var(--radius-cover) + 8px);
  border: 2px solid var(--accent);
  opacity: 0;
  transition: opacity var(--transition);
  z-index: 0;
}

.cover-ring.playing {
  opacity: 0.45;
  animation: ring-pulse 2s ease-in-out infinite;
}

@keyframes ring-pulse {
  0%, 100% { transform: scale(1);   opacity: 0.45; }
  50%       { transform: scale(1.04); opacity: 0.15; }
}

/* ─── Информация о треке ────────────────────────────────────────────────────── */
.track-info {
  width: 100%;
  text-align: center;
}

.track-title {
  font-size: 1.25rem;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.01em;
  color: var(--text-primary);
  /* Обрезаем слишком длинные названия */
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.track-author {
  margin-top: 6px;
  font-size: 0.9rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ─── Прогресс-бар ──────────────────────────────────────────────────────────── */
.progress-section {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-bar {
  position: relative;
  height: 4px;
  background: transparent;
  border-radius: 2px;
  cursor: pointer;
  /* Увеличиваем область клика не меняя визуальный размер */
  padding: 10px 0;
  margin: -10px 0;
  outline: none;
  -webkit-tap-highlight-color: transparent;
}

/* Видимая серая дорожка (фон активной полосы) */
.progress-bar::before {
  content: '';
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 4px;
  transform: translateY(-50%);
  background: rgba(255, 255, 255, 0.18);
  border-radius: 2px;
}

.progress-fill {
  position: absolute;
  top: 50%;
  left: 0;
  height: 4px;
  width: 0%;
  transform: translateY(-50%);
  background: var(--accent);
  border-radius: 2px;
  pointer-events: none;
  transition: width 0.1s linear;
}

.progress-thumb {
  position: absolute;
  top: 50%;
  left: 0%;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--text-primary);
  transform: translate(-50%, -50%) scale(0);
  pointer-events: none;
  box-shadow: none;
  transition: left 0.1s linear, transform var(--transition);
}

/* Во время drag убираем transition — полоска и кружок идут точно за курсором */
.progress-bar.dragging .progress-thumb,
.progress-bar.dragging .progress-fill {
  transition: none;
}

.progress-bar:hover .progress-thumb,
.progress-bar:active .progress-thumb {
  transform: translate(-50%, -50%) scale(1);
}

.time-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--text-secondary);
  user-select: none;
}

/* ─── Кнопки управления ─────────────────────────────────────────────────────── */
.controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
}

.ctrl-btn {
  border: none;
  background: none;
  cursor: pointer;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: transform var(--transition), opacity var(--transition), background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

/* Кнопка Play/Pause — основная, большая */
.ctrl-btn.primary {
  position: relative; /* нужно для абсолютного позиционирования SVG-иконок */
  width: 68px;
  height: 68px;
  background: var(--accent);
  color: #0a0a0a;
}

/* Обе иконки накладываются друг на друга по центру кнопки */
.ctrl-btn.primary svg {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 42px;
  height: 42px;
  transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s;
}

/* ── Базовое состояние (пауза): play видна, pause скрыта ── */
#icon-play {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  visibility: visible;
}

#icon-pause {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.5);
  visibility: hidden;
}

/* ── Состояние воспроизведения: play скрыта, pause видна ── */
.ctrl-btn.primary.playing #icon-play {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.5);
  visibility: hidden;
}

.ctrl-btn.primary.playing #icon-pause {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
  visibility: visible;
}

.ctrl-btn.primary:active {
  transform: scale(0.93);
}

/* Кнопки Prev/Next — вторичные, меньше */
.ctrl-btn.secondary {
  width: 48px;
  height: 48px;
  background: var(--surface);
}

.ctrl-btn.secondary svg {
  width: 22px;
  height: 22px;
}

/* Правая кнопка — зеркальное отражение левой */
#btn-next svg {
  transform: scaleX(-1);
}

.ctrl-btn.secondary:active {
  transform: scale(0.9);
  opacity: 0.7;
}

/* ─── Анимация появления при загрузке ───────────────────────────────────────── */
.player {
  animation: fade-up 0.4s ease both;
}

@keyframes fade-up {
  from { opacity: 0; transform: translateY(18px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ─── Кнопка "Назад" в плеере ──────────────────────────────────────────────── */
#back-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  align-self: flex-start; /* прижимаем к левому краю .player */
  padding: 8px 4px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 0.9rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--transition);
}

#back-btn:hover,
#back-btn:active {
  color: var(--text-primary);
}

/* ─── Утилиты ───────────────────────────────────────────────────────────────── */
.hidden {
  display: none !important;
}

.disabled-btn {
  opacity: 0.3;
  pointer-events: none;
}

/* ─── Кнопка повтора ────────────────────────────────────────────────────────── */
.repeat-btn {
  width: 36px;
  height: 36px;
  background: none;
  border: none;
  color: var(--text-secondary);
  opacity: 0.45;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: color var(--transition), opacity var(--transition), transform var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.repeat-btn:active {
  transform: scale(0.9);
}

.repeat-btn.active-repeat {
  color: var(--accent);
  opacity: 1;
  transform: scale(1.1);
}

/* ─── Экран плеера — центрирование обёртки ──────────────────────────────────── */
#player-view {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ─── Экран поиска ──────────────────────────────────────────────────────────── */
#search-view {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 24px 20px 16px;
  gap: 16px;
  overflow: hidden;
}

#search-input {
  width: 100%;
  padding: 14px 18px;
  background: var(--surface);
  color: var(--text-primary);
  border: none;
  border-radius: 14px;
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  -webkit-appearance: none;
  flex-shrink: 0;
}

#search-input::placeholder {
  color: var(--text-secondary);
}

/* ─── Список результатов ────────────────────────────────────────────────────── */
#search-results {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  /* Скрываем скроллбар визуально, сохраняя функциональность */
  scrollbar-width: none;
}

#search-results::-webkit-scrollbar {
  display: none;
}

.search-result {
  padding: 12px 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.search-result:hover,
.search-result:active {
  background: var(--surface);
}

.result-title {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.result-meta {
  margin-top: 3px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.search-status {
  padding: 20px 0;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ─── Нижняя панель навигации ───────────────────────────────────────────────── */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  width: 100%;
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 8px 0 max(12px, env(safe-area-inset-bottom));
  background: var(--tg-theme-bg-color);
  border-top: 1px solid rgba(255, 255, 255, 0.07);
  z-index: 100;
}

.nav-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  flex: 1;
  padding: 4px 0;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.68rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--transition);
}

.nav-btn.active {
  color: var(--accent);
}

.nav-btn:active {
  opacity: 0.7;
}

/* Экраны поиска/главной/медиатеки — нижний отступ под nav-bar */
#search-view,
#home-view,
#library-view {
  padding-bottom: 72px;
}

/* ─── Заглушки для home ─────────────────────────────────────────────────────── */
.stub-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  gap: 10px;
  text-align: center;
}

.stub-title {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--text-primary);
}

.stub-hint {
  font-size: 0.85rem;
  color: var(--text-secondary);
}

/* ─── Медиатека — плейлисты ─────────────────────────────────────────────────── */
#library-view {
  display: flex;
  flex-direction: column;
  padding: 24px 20px 16px;
  gap: 16px;
  overflow: hidden;
}

.library-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  flex-shrink: 0;
}

.create-playlist-box {
  display: flex;
  gap: 8px;
  width: 100%;
  box-sizing: border-box;
  flex-shrink: 0;
}

#new-playlist-name {
  flex: 1;
  min-width: 0; /* корректное сжатие на малых экранах */
  padding: 14px 18px;
  background: var(--surface);
  color: var(--text-primary);
  border: none;
  border-radius: 14px;
  font-size: 1rem;
  font-family: inherit;
  outline: none;
  -webkit-appearance: none;
}

#new-playlist-name::placeholder {
  color: var(--text-secondary);
}

#create-playlist-btn {
  flex-shrink: 0;
  white-space: nowrap;
  padding: 14px 20px;
  background: var(--accent);
  color: #0a0a0a;
  border: none;
  border-radius: 14px;
  font-size: 0.95rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: opacity var(--transition);
  -webkit-tap-highlight-color: transparent;
}

#create-playlist-btn:active {
  opacity: 0.75;
}

#playlists-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scrollbar-width: none;
}

#playlists-container::-webkit-scrollbar {
  display: none;
}

.playlist-item {
  display: flex;
  flex-direction: column;
  gap: 3px;
  padding: 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.playlist-item:hover,
.playlist-item:active {
  background: var(--surface);
}

.playlist-item__name {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.playlist-item__count {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.library-empty {
  padding: 20px 0;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ─── Модальное окно выбора плейлиста ───────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(3px);
}

.modal-content {
  background: var(--tg-theme-bg-color);
  border-radius: 16px;
  padding: 20px;
  width: 90%;
  max-width: 350px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-content h3 {
  font-size: 1rem;
  font-weight: 700;
  color: var(--text-primary);
}

#modal-playlists-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 260px;
  overflow-y: auto;
  scrollbar-width: none;
}

#modal-playlists-list::-webkit-scrollbar {
  display: none;
}

.modal-playlist-item {
  padding: 12px 14px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  text-align: left;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.modal-playlist-item:hover,
.modal-playlist-item:active {
  background: var(--surface);
}

.modal-empty {
  padding: 12px 0;
  font-size: 0.85rem;
  color: var(--text-secondary);
}

#close-modal-btn {
  padding: 12px;
  background: var(--surface);
  color: var(--text-secondary);
  border: none;
  border-radius: 12px;
  font-size: 0.95rem;
  font-family: inherit;
  cursor: pointer;
  transition: opacity var(--transition);
  -webkit-tap-highlight-color: transparent;
}

#close-modal-btn:active {
  opacity: 0.7;
}

/* ─── Экран деталей плейлиста ────────────────────────────────────────────────── */
#playlist-details-view {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: 24px 20px 16px;
  gap: 16px;
  overflow: hidden;
}

#close-playlist-details {
  display: flex;
  align-items: center;
  gap: 6px;
  align-self: flex-start;
  padding: 8px 4px;
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 0.9rem;
  cursor: pointer;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
  transition: color var(--transition);
}

#close-playlist-details:hover,
#close-playlist-details:active {
  color: var(--text-primary);
}

#current-playlist-name {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text-primary);
  flex-shrink: 0;
}

#playlist-tracks-container {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scrollbar-width: none;
}

#playlist-tracks-container::-webkit-scrollbar {
  display: none;
}

/* ─── Шапка экрана деталей плейлиста ────────────────────────────────────────── */
.playlist-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.playlist-header #current-playlist-name {
  flex: 1;
  margin: 0 8px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.playlist-options-wrapper {
  position: relative;
  flex-shrink: 0;
}

.icon-btn {
  width: 36px;
  height: 36px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.icon-btn:hover,
.icon-btn:active {
  background: var(--surface);
}

/* ─── Выпадающее меню плейлиста ──────────────────────────────────────────────── */
.dropdown-menu {
  position: absolute;
  right: 0;
  top: 100%;
  background: var(--surface);
  border-radius: 12px;
  padding: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  gap: 2px;
  z-index: 50;
  min-width: 190px;
}

.dropdown-menu button {
  background: transparent;
  border: none;
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 0.9rem;
  font-family: inherit;
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.dropdown-menu button:hover,
.dropdown-menu button:active {
  background: rgba(255, 255, 255, 0.07);
}

.danger-text {
  color: #ff4d4d !important;
}

/* ─── Треки внутри плейлиста ─────────────────────────────────────────────────── */
.playlist-track-item {
  display: flex;
  align-items: center;
  padding: 12px 14px;
  border-radius: 12px;
  cursor: pointer;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.playlist-track-item:hover,
.playlist-track-item:active {
  background: var(--surface);
}

.track-info-block {
  flex: 1;
  min-width: 0; /* предотвращает выход текста за границы */
}

.remove-track-btn {
  flex-shrink: 0;
  margin-left: 8px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 0.85rem;
  cursor: pointer;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  transition: color var(--transition), background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.remove-track-btn:hover,
.remove-track-btn:active {
  color: #ff4d4d;
  background: rgba(255, 77, 77, 0.12);
}

.playlist-empty {
  padding: 20px 0;
  text-align: center;
  color: var(--text-secondary);
  font-size: 0.9rem;
}

/* ─── Кнопка "Добавить в плейлист" в результатах поиска ─────────────────────── */
.search-result {
  position: relative;
  padding-right: 48px; /* место под кнопку ➕ */
}

.add-to-pl-btn {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  width: 32px;
  height: 32px;
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background var(--transition);
  -webkit-tap-highlight-color: transparent;
}

.add-to-pl-btn:hover,
.add-to-pl-btn:active {
  background: var(--surface);
}
