:root {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #222632;
  --border: #2b303d;
  --text: #e8eaf0;
  --muted: #9097a8;
  --accent: #7c5cff;
  --accent-text: #fff;
  --danger: #ff6b6b;
  --ok: #3ddc97;
  --warn: #ffb84d;
  --radius: 12px;
  --tap: 44px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f4f8;
    --surface: #ffffff;
    --surface-2: #eceef4;
    --border: #d9dce6;
    --text: #1a1d26;
    --muted: #5d6475;
    --accent: #5b3df5;
    --danger: #c92a2a;
    --ok: #12945f;
    --warn: #b36b00;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  min-height: 100vh;
  overflow-x: hidden;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; font-size: 1.1rem; }
h2 { font-size: 1rem; }
a { color: inherit; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.small { font-size: 0.82rem; }

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

/* ---------- header ---------- */
.topbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.me { margin-left: auto; font-size: 0.8rem; color: var(--muted); max-width: 10em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me:not([hidden]) + .conn { margin-left: 0; }
.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 0.8rem; color: var(--muted); margin-left: auto; }
.conn i { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); }
.conn[data-state="live"] i { background: var(--ok); }
.conn[data-state="down"] i { background: var(--danger); }
.conn[data-state="connecting"] i { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }
.pickers { display: flex; flex-wrap: wrap; gap: 8px; width: 100%; }
.field { flex: 1 1 140px; min-width: 0; }

select, input[type="search"], input[type="text"], input[type="password"] {
  width: 100%; min-height: var(--tap);
  padding: 0 12px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  font: inherit;
}
select { text-overflow: ellipsis; }

.btn {
  min-height: var(--tap); min-width: var(--tap);
  padding: 0 14px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  font: inherit; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger:hover:not(:disabled) { border-color: var(--danger); color: var(--danger); }
.btn.icon { font-size: 1.2rem; padding: 0; width: var(--tap); }
.btn.busy { position: relative; }
.btn.busy::after, .spinner {
  content: ""; width: 1em; height: 1em;
  border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%;
  animation: spin 0.7s linear infinite; display: inline-block;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- layout ---------- */
.layout { display: flex; flex-direction: column; gap: 12px; padding: 12px 16px 16px; max-width: 1200px; margin: 0 auto; }
.col-left { display: contents; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; min-width: 0; }
.empty { color: var(--muted); padding: 16px 4px; text-align: center; }

/* ---------- now playing ---------- */
.np { order: 99; position: sticky; bottom: 0; z-index: 5; box-shadow: 0 -6px 20px rgba(0,0,0,0.35); padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
.np-main { display: flex; gap: 12px; align-items: center; min-width: 0; }
.thumb { object-fit: cover; background: var(--surface-2); border-radius: 8px; flex: none; }
.thumb.big { width: 48px; height: 48px; }
.np-text { min-width: 0; flex: 1; }
.title { display: block; font-weight: 600; text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
a.title:hover { text-decoration: underline; }
#np-channel, #np-req { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#np-req { display: none; }
.progress { margin-top: 8px; display: flex; align-items: center; gap: 10px; }
.bar { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
#bar-fill { height: 100%; width: 0; background: var(--accent); }
.bar.live #bar-fill { width: 100% !important; opacity: 0.4; }
.time { font-variant-numeric: tabular-nums; font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 10px; }
.vol { display: flex; align-items: center; gap: 8px; flex: 1 1 140px; min-width: 120px; min-height: var(--tap); }
.vol input { flex: 1; min-width: 0; height: var(--tap); accent-color: var(--accent); margin: 0; }
.vol output { width: 2.2em; text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 0.85rem; }

/* ---------- search ---------- */
.search-form { display: flex; gap: 8px; }
.search-form input { flex: 1; min-width: 0; }
.url-actions { display: flex; gap: 8px; margin-top: 8px; }
.url-actions .btn { flex: 1; }
#search-status { min-height: 1.2em; margin-top: 8px; display: flex; align-items: center; gap: 8px; }

/* ---------- lists ---------- */
.list { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.row {
  display: flex; align-items: center; gap: 10px;
  padding: 6px; border-radius: 10px;
  background: var(--surface-2); border: 1px solid transparent;
  position: relative; min-width: 0;
}
.row .thumb { width: 64px; height: 36px; }
.row .info { flex: 1; min-width: 0; }
.row .info .title { font-size: 0.92rem; }
.row .meta { font-size: 0.78rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .pos { width: 1.6em; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 0.82rem; flex: none; }
.row .actions { display: flex; gap: 4px; flex: none; }
.results .row { flex-wrap: wrap; }
.results .row .info { flex: 1 1 calc(100% - 80px); }
.results .row .actions { flex: 1 1 100%; }
.results .row .actions .btn { flex: 1; }
.results .row .actions .btn { padding: 0 10px; font-size: 0.85rem; }
.queue .row .actions .btn { padding: 0; width: 36px; min-width: 36px; min-height: var(--tap); }
.handle {
  touch-action: none; cursor: grab; flex: none;
  width: 32px; min-height: var(--tap);
  display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 1.2rem; user-select: none; border-radius: 8px;
}
.handle:active { cursor: grabbing; }
.row.dragging { opacity: 0.6; border-color: var(--accent); z-index: 2; }
.row.drop-before::before, .row.drop-after::after {
  content: ""; position: absolute; left: 0; right: 0; height: 3px; background: var(--accent); border-radius: 2px;
}
.row.drop-before::before { top: -5px; }
.row.drop-after::after { bottom: -5px; }
.queue-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }

/* ---------- health banner ---------- */
.health {
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 2px 10px;
  width: calc(100% - 32px); max-width: 1168px; margin: 12px auto 0; padding: 10px 14px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--warn); border-left-width: 5px; border-radius: 10px;
}
.health strong { color: var(--warn); }
.health span { color: var(--muted); overflow-wrap: anywhere; }

/* ---------- toasts ---------- */
.toasts { position: fixed; top: 10px; left: 0; right: 0; z-index: 20; display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 0 16px; pointer-events: none; }
.toast {
  pointer-events: auto; max-width: 520px; width: 100%;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--danger); border-left-width: 5px; border-radius: 10px;
  padding: 10px 14px; box-shadow: 0 6px 24px rgba(0,0,0,0.4);
  overflow-wrap: anywhere; animation: slide 0.2s ease-out;
}
.toast.info { border-color: var(--accent); }
@keyframes slide { from { transform: translateY(-12px); opacity: 0; } }

.login { position: fixed; inset: 0; z-index: 100; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 14px; padding: 24px; }
.login-field { display: flex; flex-direction: column; gap: 6px; }
.login-field span { color: var(--muted); font-size: 0.9rem; }
.login-err { color: var(--danger); min-height: 1.2em; }
.login-card p { margin: 0; }

/* ---------- wide ---------- */
@media (min-width: 900px) {
  .results .row .info { flex: 1 1 0; }
  .results .row .actions { flex: none; }
  .results .row .actions .btn { flex: none; }
  .layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); align-items: start; gap: 16px; padding: 16px; }
  .col-left { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
  .np { order: 0; position: static; box-shadow: none; padding-bottom: 12px; }
  .thumb.big { width: 160px; height: 90px; }
  .np-main .title { font-size: 1.1rem; white-space: normal; }
  #np-req { display: block; }
  .pickers { width: auto; flex: 1; justify-content: flex-end; }
  .field { flex: 0 1 220px; }
  .topbar { padding: 12px 24px; }
  .conn { margin-left: 0; order: 3; }
}

@media (max-width: 899px) {
  #np-channel { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
  .btn.busy::after, .spinner { animation: none !important; border-right-color: currentColor; }
}

@media (max-width: 520px) {
  .queue .row { flex-wrap: wrap; }
  .queue .row .info { flex: 1 1 calc(100% - 150px); }
  .queue .row .actions { flex: 1 1 100%; justify-content: flex-end; gap: 8px; }
  .queue .row .actions .btn { width: 44px; min-width: 44px; }
}
