*,
*::before,
*::after { box-sizing: border-box; }

:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eceef2;
  --border: #e2e5ea;
  --text: #0f172a;
  --muted: #667085;
  --accent: #dc2626;
  --accent-strong: #b91c1c;
  --accent-soft: rgba(220, 38, 38, 0.1);
  --danger: #dc2626;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  color-scheme: light;
}

html[data-theme="dark"] {
  --bg: #0c0e12;
  --surface: #14171d;
  --surface-2: #1d222b;
  --border: #262c37;
  --text: #e9ecf1;
  --muted: #98a2b3;
  --accent: #ef4444;
  --accent-strong: #f87171;
  --accent-soft: rgba(239, 68, 68, 0.14);
  --danger: #f87171;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

html, body { height: 100%; overflow-x: clip; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Inter, Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

body.drawer-open { overflow: hidden; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
img { display: block; }
a { color: inherit; }
svg { flex: none; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ---------- Header ---------- */

.header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px;
  padding: 14px 20px;
  background: var(--bg);
  background: color-mix(in srgb, var(--bg) 84%, transparent);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 1.06rem;
  font-weight: 500;
  letter-spacing: -0.01em;
  user-select: none;
}

.brand b { font-weight: 800; }

.url-form {
  display: flex;
  flex: 1 1 300px;
  gap: 8px;
  min-width: 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.menu-btn { display: inline-flex; }

.header-only { display: none; }

@media (min-width: 1024px) {
  .menu-btn { display: none; }
  .header-only:not([hidden]) { display: inline-flex; }
}

/* ---------- Controls ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.05s ease;
}

.btn:hover { background: var(--surface-2); }
.btn:active { transform: scale(0.98); }
.btn:disabled,
.btn[aria-disabled="true"] { opacity: 0.45; cursor: not-allowed; pointer-events: none; }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

.btn-danger {
  background: #dc2626;
  border-color: #dc2626;
  color: #fff;
}

.btn-danger:hover { background: #b91c1c; border-color: #b91c1c; }

.btn-block { width: 100%; }

.btn-coffee {
  background: #ffdd00;
  border-color: #eacc00;
  color: #1a1a1a;
}

.btn-coffee:hover { background: #ffd400; border-color: #e0c400; }

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
  padding: 6px 10px;
}

.btn-ghost:hover { background: var(--surface-2); color: var(--text); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  transition: background 0.15s ease;
}

.icon-btn:hover { background: var(--surface-2); }
.icon-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.icon-btn.sm { width: 30px; height: 30px; border: 0; background: transparent; color: var(--muted); }
.icon-btn.sm:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.sm.danger:hover { color: var(--danger); background: var(--accent-soft); }

input[type="text"] {
  width: 100%;
  min-width: 0;
  padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

input[type="text"]::placeholder { color: var(--muted); }

input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Theme icon + label swap */

html[data-theme="light"] .icon-sun { display: none; }
html[data-theme="dark"] .icon-moon { display: none; }
.theme-label-dark { display: none; }
html[data-theme="dark"] .theme-label-dark { display: inline; }
html[data-theme="dark"] .theme-label-light { display: none; }

/* ---------- Layout ---------- */

.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 20px;
  width: 100%;
  max-width: 1440px;
  margin: 0 auto;
  padding: 20px;
}

.stage { min-width: 0; }

/* ---------- Player ---------- */

.player-shell {
  position: relative;
  aspect-ratio: 16 / 9;
  width: 100%;
  max-width: 100%;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}

.player-shell iframe,
.player-shell > div {
  position: absolute;
  inset: 0;
  width: 100%;
  max-width: 100%;
  height: 100%;
  border: 0;
}

.player-placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  text-align: center;
  color: #cfd4dd;
  background: radial-gradient(circle at 50% 40%, #1b1f27 0%, #0a0c10 75%);
  pointer-events: none;
  transition: opacity 0.35s ease, visibility 0.35s ease;
}

.player-placeholder.hidden { opacity: 0; visibility: hidden; }
.pp-logo { margin-bottom: 10px; opacity: 0.95; }
.pp-title { margin: 0; font-size: 1.05rem; font-weight: 600; }
.pp-hint { margin: 0; font-size: 0.84rem; color: #7b8494; }

/* ---------- Now playing ---------- */

.now-playing {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 14px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.np-text { flex: 1 1 220px; min-width: 0; }

.np-title {
  margin: 0;
  font-size: clamp(1rem, 1.4vw + 0.6rem, 1.18rem);
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.np-sub { margin: 2px 0 0; font-size: 0.84rem; color: var(--muted); }

.np-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.queue-controls { display: flex; gap: 6px; }
.queue-controls[hidden] { display: none; }

/* ---------- Add menu ---------- */

.menu-wrap { position: relative; }

.menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 60;
  width: min(300px, calc(100vw - 32px));
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  animation: menu-in 0.14s ease;
}

@keyframes menu-in {
  from { opacity: 0; transform: translateY(-4px); }
  to { opacity: 1; transform: translateY(0); }
}

.menu-label {
  padding: 4px 8px 6px;
  font-size: 0.74rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.menu-list { max-height: 210px; overflow: auto; }

.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 9px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  font-size: 0.9rem;
  font-weight: 600;
  text-align: left;
}

.menu-item span:first-child { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-item:hover { background: var(--surface-2); }
.menu-item:disabled { opacity: 0.6; cursor: default; }
.menu-item:disabled:hover { background: transparent; }
.menu-count { color: var(--muted); font-size: 0.8rem; display: inline-flex; }

.menu-empty { margin: 0; padding: 6px 10px 10px; font-size: 0.85rem; color: var(--muted); }

.menu-new-btn {
  margin-top: 6px;
  font-size: 0.88rem;
}

/* ---------- Sidebar ---------- */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  scroll-margin-top: 90px;
}

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface-2);
  border-radius: 12px;
}

.tab {
  flex: 1;
  padding: 8px 10px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 600;
  transition: background 0.15s ease, color 0.15s ease;
}

.tab[aria-selected="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
}

.panel { display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.panel[hidden] { display: none; }

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.panel-head h2 {
  margin: 0;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}

.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }
.inline-form .btn { padding: 9px 13px; }

.list { display: flex; flex-direction: column; gap: 6px; }

/* ---------- Media rows ---------- */

.media-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 6px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  text-align: left;
  transition: background 0.15s ease;
}

.media-row:hover { background: var(--surface-2); }

.media-main {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  padding: 0;
  border: 0;
  background: transparent;
  text-align: left;
}

.media-thumb {
  width: 96px;
  height: 54px;
  object-fit: cover;
  border-radius: 8px;
  background: var(--surface-2);
}

.media-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }

.media-title {
  font-size: 0.9rem;
  font-weight: 600;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.media-sub { font-size: 0.78rem; color: var(--muted); }

.media-play { color: var(--muted); opacity: 0; transition: opacity 0.15s ease, color 0.15s ease; }
.media-row:hover .media-play { opacity: 1; color: var(--accent); }

/* ---------- Playlist cards ---------- */

.pl-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
}

.pl-head { display: flex; align-items: center; gap: 2px; padding: 4px; }

.pl-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  padding: 8px 6px 8px 8px;
  border: 0;
  background: transparent;
  text-align: left;
}

.chev { display: inline-flex; color: var(--muted); transition: transform 0.18s ease; }
.chev.open { transform: rotate(90deg); }

.pl-name {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  font-size: 0.92rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pl-count {
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 700;
}

.pl-tools { display: flex; align-items: center; padding-right: 4px; }

.pl-items {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 4px 6px 8px;
  border-top: 1px solid var(--border);
}

.pl-items[hidden] { display: none; }

.pl-thumb {
  flex: none;
  width: 64px;
  height: 36px;
  border-radius: 6px;
  object-fit: cover;
  background: var(--surface-2);
}

.pl-thumb.placeholder {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  border: 1px dashed var(--border);
}

.pl-empty { margin: 6px 8px; font-size: 0.84rem; color: var(--muted); }

.pl-more { margin: 6px auto 2px; font-size: 0.82rem; padding: 6px 12px; }

.importing-pulse {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.25; }
}

/* ---------- Drawer ---------- */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(5, 8, 15, 0.5);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0.25s ease;
}

.drawer-backdrop.open { opacity: 1; visibility: visible; }

.drawer {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index: 90;
  width: min(320px, 86vw);
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 16px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  transform: translateX(102%);
  visibility: hidden;
  transition: transform 0.25s ease, visibility 0.25s ease;
  overflow-y: auto;
}

.drawer.open { transform: none; visibility: visible; }

.drawer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.drawer-nav,
.drawer-foot {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.drawer-foot {
  margin-top: auto;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.drawer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border: 0;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  font-weight: 600;
  text-align: left;
  text-decoration: none;
  transition: background 0.15s ease;
}

.drawer-item:hover { background: var(--surface-2); }
.drawer-item svg { color: var(--muted); }
.drawer-item[hidden] { display: none; }

/* ---------- Empty states ---------- */

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 26px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 0.88rem;
}

.empty svg { opacity: 0.7; }
.empty p { margin: 0; }

/* ---------- Footer ---------- */

.footer {
  max-width: 1440px;
  margin: 0 auto;
  padding: 6px 20px 28px;
  text-align: center;
  color: var(--muted);
  font-size: 0.86rem;
}

.footer p { margin: 0; }

.footer a {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.footer a:hover { text-decoration: underline; }
.footer .heart { color: var(--accent); }

/* ---------- Modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 110;
  background: rgba(5, 8, 15, 0.5);
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.modal-backdrop.open { opacity: 1; visibility: visible; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.2s ease, visibility 0.2s ease;
}

.modal.open { opacity: 1; visibility: visible; pointer-events: auto; }

.modal-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
  width: min(420px, 100%);
  padding: 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  transform: translateY(10px) scale(0.98);
  transition: transform 0.2s ease;
}

.modal.open .modal-card { transform: none; }

.modal-title {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.modal-text { margin: 0; font-size: 0.92rem; color: var(--muted); }

.modal-input[hidden] { display: none; }

.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  z-index: 100;
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.88rem;
  font-weight: 600;
  box-shadow: var(--shadow);
  opacity: 0;
  transform: translate(-50%, 12px);
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Desktop ---------- */

@media (min-width: 1024px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) 400px;
    align-items: start;
    gap: 24px;
    padding: 24px;
  }

  .sidebar {
    position: sticky;
    top: 86px;
    max-height: calc(100vh - 110px);
  }

  .panel { overflow: auto; scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
  .panel::-webkit-scrollbar { width: 8px; }
  .panel::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; }
}

/* ---------- Mobile ---------- */

@media (max-width: 600px) {
  .header { padding: 12px 14px; gap: 10px; }
  .header-actions { gap: 6px; }
  .header-actions .btn-coffee { padding: 9px 11px; }
  .header-actions .btn-coffee .btn-label { display: none; }
  .layout { padding: 14px; gap: 14px; }
  .media-thumb { width: 80px; height: 45px; }
  .pl-thumb { width: 52px; height: 29px; }
  .np-actions { width: 100%; }
  .np-actions .btn { flex: 1; justify-content: center; }
  .queue-controls { order: 2; width: 100%; }
  .queue-controls .icon-btn { flex: 1; }
  .menu-wrap { flex: 1; order: 1; }
  .menu { left: 0; right: 0; width: auto; }
  .footer { padding: 4px 14px 22px; }

  .modal { align-items: flex-end; padding: 0; }
  .modal-card {
    width: 100%;
    border-radius: 16px 16px 0 0;
    border-bottom: 0;
    padding: 20px 18px calc(20px + env(safe-area-inset-bottom));
    transform: translateY(100%);
  }
  .modal-actions .btn { flex: 1; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
