:root {
  --bg: #0e0f13;
  --bg2: #171920;
  --bg3: #22252e;
  --line: #2a2e38;
  --fg: #f2f3f5;
  --muted: #9097a3;
  --accent: #1ed760;
  --accent-fg: #06120a;
  --danger: #ff5c5c;
  --tabs-h: 64px;
  --mini-h: 68px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; background: var(--bg); color: var(--fg); }
body {
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabs-h) + var(--mini-h) + var(--safe-b) + 16px);
  overscroll-behavior-y: none;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.4em; }

svg.ic { width: 24px; height: 24px; fill: currentColor; display: block; }

#app { max-width: 820px; margin: 0 auto; padding: 16px; padding-top: calc(env(safe-area-inset-top, 0px) + 12px); }
.view h1 { font-size: 28px; margin: 4px 0 12px; }
.view h2 { font-size: 18px; margin: 28px 0 10px; }
.head { display: flex; align-items: center; gap: 10px; }
.head h1 { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- inputs & buttons */
.field {
  width: 100%; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg2); outline: none; font-size: 16px;
}
.field:focus { border-color: var(--accent); }
.row-form { display: flex; gap: 8px; }
.row-form .field { flex: 1; min-width: 0; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 18px; border-radius: 999px; border: 0; background: var(--bg3); font-weight: 600;
  white-space: nowrap; min-height: 48px;
}
.btn.primary { background: var(--accent); color: var(--accent-fg); }
.btn:disabled { opacity: .5; }
.btn svg.ic { width: 20px; height: 20px; }
.actions { display: flex; gap: 10px; margin: 12px 0; flex-wrap: wrap; }
.icon-btn {
  background: none; border: 0; padding: 10px; border-radius: 999px; display: inline-flex;
  align-items: center; justify-content: center; color: var(--fg);
}
.icon-btn:active { background: var(--bg3); }
.seg { display: flex; background: var(--bg2); border-radius: 12px; padding: 4px; margin-bottom: 10px; }
.seg button { flex: 1; border: 0; background: none; padding: 10px 4px; border-radius: 9px; color: var(--muted); font-weight: 600; }
.seg button.active { background: var(--bg3); color: var(--fg); }

/* ---------- lists */
.list { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 12px; padding: 8px 4px; min-height: 68px;
  border-radius: 12px; cursor: pointer;
}
.row:active { background: var(--bg2); }
.row.playing .title { color: var(--accent); }
.meta { flex: 1; min-width: 0; }
.title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sub { color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { color: var(--muted); text-align: center; padding: 40px 16px; }

.cover {
  flex: none; width: 52px; height: 52px; border-radius: 8px; background: var(--bg3) center/cover no-repeat;
  display: flex; align-items: center; justify-content: center; color: var(--muted); overflow: hidden;
}
.cover img { width: 100%; height: 100%; object-fit: cover; display: block; }
.cover.sm { width: 46px; height: 46px; }
.cover.xl { width: min(78vw, 42vh, 380px); height: min(78vw, 42vh, 380px); border-radius: 16px; margin: 8px auto 20px; box-shadow: 0 20px 60px #0008; }
.cover.xl svg.ic { width: 35%; height: 35%; }

/* playlists grid */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; }
.card { background: var(--bg2); border-radius: 14px; padding: 10px; cursor: pointer; border: 0; text-align: left; }
.card .cover { width: 100%; height: auto; aspect-ratio: 1; margin-bottom: 8px; }
.card .cover svg.ic { width: 40%; height: 40%; }
.card.new .cover { border: 2px dashed var(--line); background: none; }

/* jobs */
.job { background: var(--bg2); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; }
.job .url { font-size: 13px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.job .status { margin-top: 4px; font-size: 14px; }
.job.done .status { color: var(--accent); }
.job.error .status { color: var(--danger); word-break: break-word; }
.bar { height: 4px; background: var(--bg3); border-radius: 2px; margin-top: 8px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); transition: width .3s; }
.bar.indet > div { width: 30% !important; animation: indet 1.2s infinite ease-in-out; }
@keyframes indet { from { transform: translateX(-100%); } to { transform: translateX(340%); } }

/* ---------- tabs & mini player */
.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; height: calc(var(--tabs-h) + var(--safe-b));
  padding-bottom: var(--safe-b); display: flex; background: #0b0c10f2; backdrop-filter: blur(12px);
  border-top: 1px solid var(--line); z-index: 20;
}
.tabs button {
  flex: 1; border: 0; background: none; color: var(--muted); font-size: 12px; font-weight: 600;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
}
.tabs button.active { color: var(--fg); }

.mini {
  position: fixed; left: 8px; right: 8px; bottom: calc(var(--tabs-h) + var(--safe-b) + 6px);
  max-width: 804px; margin: 0 auto; height: var(--mini-h); background: var(--bg3); border-radius: 14px;
  z-index: 21; overflow: hidden; box-shadow: 0 8px 30px #0009; cursor: pointer;
}
.mini-body { display: flex; align-items: center; gap: 10px; height: 100%; padding: 0 6px 0 10px; }
.mini-progress { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: #ffffff14; }
#mini-bar { height: 100%; width: 0; background: var(--fg); }
.mini .icon-btn svg.ic { width: 30px; height: 30px; }

/* ---------- now playing */
.now {
  position: fixed; inset: 0; z-index: 40; overflow-y: auto;
  background: linear-gradient(180deg, #2a2f3a 0%, var(--bg) 60%);
  animation: up .22s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
.now-inner {
  max-width: 560px; margin: 0 auto; padding: calc(env(safe-area-inset-top, 0px) + 8px) 20px calc(var(--safe-b) + 24px);
}
.now-top { display: flex; align-items: center; justify-content: space-between; }
.now-top .icon-btn svg.ic { width: 32px; height: 32px; }
.now-meta { text-align: center; margin-bottom: 12px; }
.now-title { font-size: 24px; font-weight: 700; line-height: 1.25; }
.now-artist { color: var(--muted); font-size: 17px; margin-top: 4px; }
.times { display: flex; justify-content: space-between; color: var(--muted); font-size: 13px; margin-top: 2px; }
.controls { display: flex; align-items: center; justify-content: space-between; margin: 12px 0 8px; }
.controls .icon-btn.big svg.ic { width: 52px; height: 52px; }
.controls .icon-btn.huge { width: 92px; height: 92px; background: var(--fg); color: var(--bg); }
.controls .icon-btn.huge svg.ic { width: 52px; height: 52px; }
.controls .toggle { color: var(--muted); }
.controls .toggle.on { color: var(--accent); }
.controls .toggle svg.ic { width: 28px; height: 28px; }
.upnext-h { margin: 24px 0 6px; font-size: 16px; color: var(--muted); }

input[type=range] { width: 100%; accent-color: var(--accent); height: 32px; margin: 0; }
#seek { touch-action: none; }

/* ---------- sheet */
.sheet { position: fixed; inset: 0; z-index: 60; background: #000a; display: flex; align-items: flex-end; justify-content: center; }
.sheet-panel {
  width: 100%; max-width: 560px; background: var(--bg2); border-radius: 18px 18px 0 0;
  padding: 10px 10px calc(var(--safe-b) + 10px); max-height: 80vh; overflow-y: auto; animation: up .18s ease-out;
}
.sheet-title { padding: 10px 14px; color: var(--muted); font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-item {
  display: block; width: 100%; text-align: left; border: 0; background: none; padding: 16px 14px;
  border-radius: 12px; font-size: 17px;
}
.sheet-item:active { background: var(--bg3); }
.sheet-item.danger { color: var(--danger); }
.sheet-item.cancel { text-align: center; color: var(--muted); margin-top: 4px; }

/* ---------- toast & login */
.toast {
  position: fixed; left: 50%; top: calc(env(safe-area-inset-top, 0px) + 16px); transform: translate(-50%, -150%);
  background: var(--fg); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-weight: 600;
  z-index: 100; transition: transform .25s, opacity .25s; max-width: 90vw; text-align: center;
  opacity: 0; pointer-events: none;
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

.login { position: fixed; inset: 0; z-index: 200; background: var(--bg); display: flex; align-items: center; justify-content: center; padding: 24px; }
.login form { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; text-align: center; }
.login input { padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg2); outline: none; }
.login .logo { width: 72px; height: 72px; margin: 0 auto; background: url(icon.svg) center/contain no-repeat; }
.login h1 { margin: 0 0 8px; }

/* ---------- offline */
.off-badge { display: none; width: 14px; height: 14px; vertical-align: -2px; margin-right: 4px; fill: var(--accent); }
.row.cached .off-badge { display: inline-block; }
.netbar {
  position: sticky; top: 0; z-index: 15; margin: -4px 0 10px; padding: 8px 14px; border-radius: 10px;
  background: #3a2a10; color: #ffc86b; font-size: 14px; font-weight: 600; text-align: center;
}
.offline-status { color: var(--muted); font-size: 14px; margin: -4px 0 8px; min-height: 0; }
.offline-status:empty { display: none; }

/* ---------- chế độ xe (màn trên xe / box Android, vd VinFast Lux SA)
   Cả app là 1 khung cố định vừa màn hình, không cuộn cả trang; chỉ vùng danh sách (#app) cuộn.
   - Màn dọc (rộng/cao < 0.95): trình phát trên, danh sách dưới
   - Màn vuông/ngang rộng >= 680px: trình phát cột trái, danh sách cột phải (hẹp hơn thì xếp trên/dưới) */
body.car {
  --tabs-h: 72px; --mini-h: 0px; font-size: 18px;
  height: 100vh; height: 100dvh; overflow: hidden; padding: 0;
  display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "player" "main" "tabs";
}
body.car .mini { display: none !important; }
body.car #now.hidden { display: block !important; }
body.car #now {
  grid-area: player; position: relative; inset: auto; min-width: 0; min-height: 0;
  overflow: hidden; animation: none; z-index: 1;
  background: linear-gradient(180deg, #262b36 0%, #16181f 100%); border-bottom: 1px solid var(--line);
}
body.car #app {
  grid-area: main; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  max-width: none; width: 100%; margin: 0; padding: 12px 16px 16px;
}
body.car .tabs { grid-area: tabs; position: relative; }
body.car .netbar { top: 0; }

/* trình phát (bố cục dọc): ảnh bìa trái, thông tin phải, nút điều khiển hàng dưới */
body.car .now-inner {
  --cs: clamp(110px, min(26vh, 36vw), 300px);
  max-width: none; height: 100%; display: grid; grid-template-columns: var(--cs) minmax(0, 1fr);
  grid-template-areas: "cover info" "controls controls"; column-gap: 20px; align-items: center; align-content: center;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 20px 8px;
}
body.car .now-top, body.car .upnext-h, body.car #upnext { display: none; }
body.car #now-cover { grid-area: cover; width: var(--cs); height: var(--cs); margin: 0; border-radius: 14px; box-shadow: 0 10px 30px #0006; }
body.car .now-info { grid-area: info; min-width: 0; }
body.car .now-meta { text-align: left; margin-bottom: 6px; }
body.car .now-title {
  font-size: clamp(20px, 3.8vh, 34px); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
body.car .now-artist { font-size: clamp(16px, 2.6vh, 24px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.car #seek { height: 40px; }
body.car .times { font-size: 15px; }
body.car .controls { grid-area: controls; margin: 8px 0 0; justify-content: space-around; }
body.car .controls .icon-btn.big svg.ic { width: clamp(48px, 8vh, 76px); height: clamp(48px, 8vh, 76px); }
body.car .controls .icon-btn.huge { width: clamp(76px, 12vh, 128px); height: clamp(76px, 12vh, 128px); }
body.car .controls .icon-btn.huge svg.ic { width: 60%; height: 60%; }
body.car .controls .toggle svg.ic { width: 34px; height: 34px; }

/* màn vuông / ngang: chia 2 cột */
@media (min-aspect-ratio: 19/20) and (min-width: 680px) {
  body.car {
    grid-template-columns: minmax(300px, 44%) minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto;
    grid-template-areas: "player main" "player tabs";
  }
  body.car #now { border-bottom: 0; border-right: 1px solid var(--line); background: linear-gradient(160deg, #2a2f3a 0%, #121419 100%); }
  body.car .now-inner {
    display: flex; flex-direction: column; justify-content: center; gap: 10px;
    padding: calc(env(safe-area-inset-top, 0px) + 20px) clamp(16px, 3vw, 36px) 20px;
  }
  body.car #now-cover { width: min(100%, 48vh); height: auto; aspect-ratio: 1; align-self: center; }
  body.car .now-info { width: 100%; }
  body.car .now-meta { text-align: center; margin: 8px 0 4px; }
  body.car .controls { width: 100%; justify-content: space-between; margin-top: 4px; }
  /* cột trình phát chỉ ~44% chiều ngang: nút co theo cả chiều rộng (vw) lẫn chiều cao (vh) */
  body.car .controls .icon-btn { padding: 6px; }
  body.car .controls .icon-btn.big svg.ic { width: clamp(40px, min(8vh, 6vw), 76px); height: clamp(40px, min(8vh, 6vw), 76px); }
  body.car .controls .icon-btn.huge { width: clamp(64px, min(12vh, 9.5vw), 128px); height: clamp(64px, min(12vh, 9.5vw), 128px); }
  body.car .controls .toggle svg.ic { width: clamp(26px, 3.2vw, 34px); height: clamp(26px, 3.2vw, 34px); }
}
body.car .controls .icon-btn { flex: none; }

body.car .view h1 { font-size: 22px; margin: 0 0 8px; }
body.car .view h2 { font-size: 19px; margin-top: 20px; }
body.car .row { min-height: 80px; gap: 14px; }
body.car .row .cover { width: 62px; height: 62px; }
body.car .title { font-size: 19px; }
body.car .sub { font-size: 16px; }
body.car .row .icon-btn svg.ic { width: 30px; height: 30px; }
body.car .btn { min-height: 56px; font-size: 17px; padding: 12px 22px; }
body.car .field { padding: 16px 18px; font-size: 18px; }
body.car .tabs button { font-size: 15px; }
body.car .tabs svg.ic { width: 30px; height: 30px; }
body.car .sheet-item { padding: 20px 16px; font-size: 19px; }
body.car .toast { font-size: 18px; }
/* chế độ xe: gộp ô lọc + nút phát vào 1 hàng cho đỡ tốn chiều cao */
body.car .lib-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
body.car .lib-tools .field { flex: 1; min-width: 0; padding: 12px 16px; }
body.car .lib-tools .actions { margin: 0; flex-wrap: nowrap; gap: 8px; }
body.car .lib-tools .btn { width: 56px; padding: 0; }
body.car .lib-tools .btn svg.ic { width: 26px; height: 26px; }
body.car .lib-tools .lbl { display: none; }
body.car .head { min-height: 44px; }
body.car .head .icon-btn svg.ic { width: 30px; height: 30px; }

.icon-btn.spin svg.ic { animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* hộp thoại nhập/xác nhận (thay prompt/confirm) */
.dialog { padding: 20px 18px calc(var(--safe-b) + 16px); }
.dialog-title { font-size: 18px; font-weight: 700; margin-bottom: 12px; }
.dialog-field { display: block; margin-bottom: 12px; }
.dialog-field span { display: block; color: var(--muted); font-size: 14px; margin-bottom: 6px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 8px; }
.btn.danger { background: var(--danger); color: #fff; }
#dialog { z-index: 70; }
body.car .dialog-title { font-size: 21px; }
