:root {
  --bg: #0f0f0f;
  --bg-elevated: #212121;
  --bg-hover: #272727;
  --text: #f1f1f1;
  --text-secondary: #aaaaaa;
  --border: #303030;
  --red: #ff0000;
  --blue: #3ea6ff;
  --header-h: 56px;
  --sidebar-w: 240px;
  --sidebar-mini-w: 72px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Roboto", "Segoe UI", Arial, sans-serif;
  font-size: 14px;
}

a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; border: none; background: none; color: inherit; }

/* ----------------------------- Header ----------------------------- */
.header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 16px;
  background: var(--bg);
  z-index: 100;
}

.header-left { display: flex; align-items: center; gap: 16px; min-width: 140px; }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
}
.icon-btn:hover { background: var(--bg-hover); }

.logo { display: flex; align-items: center; gap: 4px; font-size: 20px; font-weight: 700; }
.logo .yt-mark {
  background: var(--red); color: #fff; border-radius: 4px;
  padding: 1px 6px; font-size: 15px; letter-spacing: -1px;
}

.header-center { flex: 1; display: flex; justify-content: center; }
.search-form { display: flex; width: 100%; max-width: 640px; }
.search-form input {
  flex: 1;
  height: 40px;
  background: #121212;
  border: 1px solid var(--border);
  border-radius: 40px 0 0 40px;
  padding: 0 16px;
  color: var(--text);
  font-size: 16px;
  outline: none;
}
.search-form input:focus { border-color: var(--blue); }
.search-form button {
  width: 64px; height: 40px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 40px 40px 0;
  font-size: 18px;
}
.search-form button:hover { background: var(--bg-hover); }
.header-right { min-width: 100px; display: flex; justify-content: flex-end; gap: 8px; }

/* ----------------------------- Sidebar ----------------------------- */
.sidebar {
  position: fixed;
  top: var(--header-h); left: 0; bottom: 0;
  width: var(--sidebar-w);
  padding: 12px 8px;
  overflow-y: auto;
  background: var(--bg);
}
.sidebar::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb { background: #555; border-radius: 4px; }

.side-item {
  display: flex; align-items: center; gap: 24px;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 14px;
}
.side-item .ic { font-size: 20px; width: 24px; text-align: center; }
.side-item:hover { background: var(--bg-hover); }
.side-item.active { background: var(--bg-hover); font-weight: 500; }
.side-sep { height: 1px; background: var(--border); margin: 12px 0; }
.side-title { padding: 8px 12px; color: var(--text-secondary); font-size: 14px; }

.sidebar { transition: width .15s, transform .2s; }

/* Nền mờ phía sau drawer (chỉ hiện trên mobile khi mở) */
.sidebar-backdrop {
  display: none; position: fixed; inset: var(--header-h) 0 0 0;
  background: rgba(0,0,0,.5); z-index: 105;
}

/* ----------------------------- Layout ----------------------------- */
.content {
  margin-top: var(--header-h);
  margin-left: var(--sidebar-w);
  padding: 24px;
  transition: margin-left .15s;
}
.page-title { font-size: 18px; margin-bottom: 16px; }

/* ----------------------------- Grid cards ----------------------------- */
.video-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px 16px;
}
.card { position: relative; }
.card-link { display: block; color: inherit; }
.card-thumb {
  position: relative;
  width: 100%; aspect-ratio: 16/9;
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg-elevated);
}
.card-thumb img { width: 100%; height: 100%; object-fit: cover; }
.card-duration {
  position: absolute; bottom: 8px; right: 8px;
  background: rgba(0,0,0,.8); color: #fff;
  padding: 1px 4px; border-radius: 4px;
  font-size: 12px; font-weight: 500;
}
.card-body { display: flex; gap: 12px; margin-top: 12px; }
.card-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: var(--bg-elevated); flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; color: var(--text-secondary);
}
/* Nút lưu (＋) / xóa khỏi playlist trên card */
.card-save {
  position: absolute; top: 8px; right: 8px;
  width: 34px; height: 34px; border-radius: 50%;
  background: rgba(0,0,0,.75); color: #fff;
  font-size: 18px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s; z-index: 3;
}
.card:hover .card-save { opacity: 1; }
.card-save:hover { background: rgba(0,0,0,.95); transform: scale(1.08); }
@media (hover: none) { .card-save { opacity: 1; } }

/* Nút xóa khỏi lịch sử (góc trái) */
.card-hist-remove {
  position: absolute; top: 8px; left: 8px;
  width: 34px; height: 34px; border-radius: 50%;
  background: rgba(0,0,0,.75); color: #fff; font-size: 14px;
  display: flex; align-items: center; justify-content: center;
  opacity: 0; transition: opacity .15s; z-index: 3;
}
.card:hover .card-hist-remove { opacity: 1; }
.card-hist-remove:hover { background: rgba(0,0,0,.95); }
@media (hover: none) { .card-hist-remove { opacity: 1; } }

.card-info { min-width: 0; }
.card-title {
  font-size: 14px; font-weight: 500; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-meta { color: var(--text-secondary); font-size: 13px; margin-top: 4px; }

/* ----------------------------- Pagination ----------------------------- */
.pagination {
  display: flex; align-items: center; justify-content: center;
  gap: 16px; margin: 32px 0 8px;
}
.page-btn {
  height: 40px; padding: 0 18px; border-radius: 20px;
  background: var(--bg-elevated); color: var(--text);
  font-size: 14px; font-weight: 500;
}
.page-btn:hover:not(:disabled) { background: var(--bg-hover); }
.page-btn:disabled { opacity: .4; cursor: not-allowed; }
.page-label { color: var(--text-secondary); font-size: 14px; }

.related-sentinel { height: 1px; }
.related-status { padding: 12px; text-align: center; color: var(--text-secondary); font-size: 13px; }

/* ----------------------------- Watch page ----------------------------- */
.watch {
  margin-top: var(--header-h);
  margin-left: var(--sidebar-w);
  padding: 24px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 402px;
  gap: 24px;
}
.player-box { width: 100%; aspect-ratio: 16/9; background: #000; border-radius: 12px; overflow: hidden; }
.player-box iframe, .player-box video { width: 100%; height: 100%; border: none; display: block; }

.tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.tab {
  padding: 8px 14px; border-radius: 18px;
  background: var(--bg-elevated); font-size: 14px; font-weight: 500;
}
.tab.active { background: var(--text); color: #0f0f0f; }

.watch-title { font-size: 20px; font-weight: 600; margin: 16px 0 8px; }
.watch-meta { display: flex; align-items: center; gap: 16px; flex-wrap: wrap; color: var(--text-secondary); }
.watch-channel { display: flex; align-items: center; gap: 12px; }
.watch-channel .card-avatar { width: 40px; height: 40px; }
.channel-name { color: var(--text); font-weight: 500; font-size: 16px; }

.description {
  background: var(--bg-elevated);
  border-radius: 12px;
  padding: 12px;
  margin-top: 16px;
  white-space: pre-wrap;
  font-size: 14px;
  max-height: 120px;
  overflow: hidden;
}
.description.expanded { max-height: none; }

/* ----------------------------- Download box ----------------------------- */
.download-box {
  background: var(--bg-elevated);
  border-radius: 12px;
  padding: 16px;
  margin-top: 16px;
}
.download-box h3 { font-size: 15px; margin-bottom: 12px; }
.dl-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.dl-row select, .dl-btn {
  height: 40px; border-radius: 20px; padding: 0 18px;
  background: #383838; color: var(--text); font-size: 14px; font-weight: 500;
  border: none;
}
.dl-btn { background: var(--blue); color: #0f0f0f; }
.dl-btn:hover { opacity: .9; }
.dl-btn.audio { background: #1ed760; }
.dl-btn:disabled { opacity: .5; cursor: not-allowed; }

.progress-wrap { margin-top: 14px; display: none; }
.progress-wrap.show { display: block; }
.progress-bar { height: 8px; background: #383838; border-radius: 4px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--blue); transition: width .3s; }
.progress-text { margin-top: 6px; font-size: 13px; color: var(--text-secondary); }

/* ----------------------------- Related list ----------------------------- */
.related-list { display: flex; flex-direction: column; gap: 8px; }
.related-item { display: flex; gap: 8px; cursor: pointer; }
.related-thumb {
  position: relative;
  width: 168px; aspect-ratio: 16/9; flex-shrink: 0;
  border-radius: 8px; overflow: hidden; background: var(--bg-elevated);
}
.related-thumb img { width: 100%; height: 100%; object-fit: cover; }
.related-info { min-width: 0; }
.related-title {
  font-size: 14px; font-weight: 500; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.related-meta { color: var(--text-secondary); font-size: 12px; margin-top: 4px; }

/* ----------------------------- States ----------------------------- */
.loading, .empty { padding: 40px; text-align: center; color: var(--text-secondary); }
.spinner {
  width: 36px; height: 36px; margin: 0 auto 12px;
  border: 4px solid var(--border); border-top-color: var(--red);
  border-radius: 50%; animation: spin 1s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.skeleton { background: var(--bg-elevated); border-radius: 12px; }
.skeleton.thumb { aspect-ratio: 16/9; }
.skeleton.line { height: 12px; margin-top: 8px; border-radius: 4px; }

.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: var(--bg-elevated); padding: 12px 20px; border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0,0,0,.5); z-index: 200; font-size: 14px;
  display: none;
}
.toast.show { display: block; }

/* ----------------------------- Page head / actions ----------------------------- */
.page-head { display: flex; align-items: center; gap: 16px; margin-bottom: 16px; }
.page-head .page-title { margin-bottom: 0; }
.page-count { color: var(--text-secondary); font-size: 14px; }
.icon-action {
  height: 36px; padding: 0 16px; border-radius: 18px;
  background: var(--bg-elevated); color: var(--text); font-size: 14px; font-weight: 500;
}
.icon-action:hover { background: var(--bg-hover); }
.icon-action.danger, button.danger { color: #ff6b6b; }
.back-link { display: inline-block; color: var(--text-secondary); margin-bottom: 16px; }
.back-link:hover { color: var(--text); }

/* ----------------------------- Modal ----------------------------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; z-index: 300;
}
.modal {
  background: #282828; border-radius: 12px;
  width: 380px; max-width: 92vw; max-height: 80vh;
  display: flex; flex-direction: column; overflow: hidden;
  box-shadow: 0 8px 32px rgba(0,0,0,.6);
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px; }
.modal-head h3 { font-size: 16px; }
.modal-close { width: 32px; height: 32px; border-radius: 50%; font-size: 16px; }
.modal-close:hover { background: var(--bg-hover); }
.modal-list { overflow-y: auto; padding: 0 16px; }
.modal-row { display: flex; align-items: center; gap: 12px; padding: 10px 0; cursor: pointer; }
.modal-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--blue); }
.modal-name { flex: 1; }
.modal-count { color: var(--text-secondary); font-size: 13px; }
.modal-empty { color: var(--text-secondary); padding: 12px 0; }
.modal-create { display: flex; gap: 8px; padding: 16px; border-top: 1px solid var(--border); }
.modal-create input {
  flex: 1; height: 40px; border-radius: 8px;
  background: #121212; border: 1px solid var(--border); color: var(--text);
  padding: 0 12px; outline: none;
}
.modal-create input:focus { border-color: var(--blue); }
.modal-create button {
  height: 40px; padding: 0 16px; border-radius: 8px;
  background: var(--blue); color: #0f0f0f; font-weight: 500;
}

/* ----------------------------- Playlist page ----------------------------- */
.pl-create { display: flex; gap: 8px; margin-bottom: 24px; max-width: 480px; }
.pl-create input {
  flex: 1; height: 40px; border-radius: 20px;
  background: var(--bg-elevated); border: none; color: var(--text);
  padding: 0 16px; outline: none;
}
.pl-create button {
  height: 40px; padding: 0 18px; border-radius: 20px;
  background: var(--blue); color: #0f0f0f; font-weight: 500;
}
.pl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 16px; }
.pl-card { background: var(--bg-elevated); border-radius: 12px; overflow: hidden; }
.pl-card-thumb { position: relative; display: block; aspect-ratio: 16/9; background: #000; }
.pl-card-thumb img { width: 100%; height: 100%; object-fit: cover; }
.pl-card-badge {
  position: absolute; bottom: 8px; right: 8px;
  background: rgba(0,0,0,.8); color: #fff; padding: 2px 8px;
  border-radius: 4px; font-size: 12px;
}
.pl-card-body { padding: 12px; }
.pl-card-name { display: block; font-weight: 500; margin-bottom: 8px; }
.pl-card-actions { display: flex; gap: 14px; }
.pl-card-actions button { font-size: 13px; color: var(--blue); }
.pl-card-actions button:hover { text-decoration: underline; }
.pl-detail-head { display: flex; align-items: center; gap: 12px; margin-bottom: 24px; flex-wrap: wrap; }
.pl-detail-head .page-title { margin-bottom: 0; }
.icon-action.play { background: var(--blue); color: #0f0f0f; font-weight: 600; }
.icon-action.play:hover { opacity: .9; background: var(--blue); }
select.icon-action { cursor: pointer; outline: none; }

/* ----------------------------- Queue panel (phát playlist) ----------------------------- */
.queue-panel { background: var(--bg-elevated); border-radius: 12px; margin-bottom: 16px; overflow: hidden; }
.queue-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.queue-title { font-weight: 600; }
.queue-sub { color: var(--text-secondary); font-size: 13px; margin-top: 2px; }
.queue-close { width: 28px; height: 28px; border-radius: 50%; font-size: 14px; flex-shrink: 0; }
.queue-close:hover { background: var(--bg-hover); }
.queue-nav { display: flex; gap: 8px; padding: 12px 16px; }
.queue-nav button { flex: 1; height: 36px; border-radius: 18px; background: #383838; color: var(--text); font-weight: 500; }
.queue-nav button:hover:not(:disabled) { background: #4a4a4a; }
.queue-nav button:disabled { opacity: .4; cursor: not-allowed; }
.queue-items { max-height: 320px; overflow-y: auto; }
.queue-item { display: flex; align-items: center; gap: 8px; padding: 6px 12px; }
.queue-item:hover { background: var(--bg-hover); }
.queue-item.current { background: var(--bg-hover); }
.queue-idx { width: 18px; text-align: center; color: var(--text-secondary); font-size: 12px; flex-shrink: 0; }
.queue-item img { width: 80px; aspect-ratio: 16/9; object-fit: cover; border-radius: 4px; flex-shrink: 0; }
.queue-item-title {
  font-size: 13px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}

/* ----------------------------- Trang Nhạc chung (public) ----------------------------- */
.public-banner {
  background: var(--bg-elevated); border-radius: 12px;
  padding: 12px 16px; margin-bottom: 16px; font-size: 14px; color: var(--text-secondary);
}
.public-banner b { color: var(--text); }
.nowplaying { margin-top: 16px; min-height: 48px; }
.np-label { color: var(--red); font-size: 12px; font-weight: 700; letter-spacing: .5px; }
.np-title { font-size: 18px; font-weight: 600; margin-top: 4px; }
.np-sub { color: var(--text-secondary); font-size: 14px; margin-top: 2px; }
.np-idle { color: var(--text-secondary); }
.public-controls { display: flex; align-items: center; gap: 12px; margin-top: 16px; flex-wrap: wrap; }
.conn { color: #ff6b6b; font-size: 13px; }

.public-h { font-size: 15px; margin: 4px 0 10px; }
.public-name {
  width: 100%; height: 38px; border-radius: 8px; margin-bottom: 8px;
  background: #121212; border: 1px solid var(--border); color: var(--text);
  padding: 0 12px; outline: none;
}
.public-name:focus { border-color: var(--blue); }
.search-form2 { display: flex; gap: 8px; margin-bottom: 12px; }
.search-form2 input {
  flex: 1; height: 38px; border-radius: 8px;
  background: #121212; border: 1px solid var(--border); color: var(--text);
  padding: 0 12px; outline: none;
}
.search-form2 input:focus { border-color: var(--blue); }
.search-form2 button {
  width: 48px; height: 38px; border-radius: 8px; background: var(--bg-elevated);
}
.search-form2 button:hover { background: var(--bg-hover); }

.add-results { max-height: 320px; overflow-y: auto; margin-bottom: 20px; }
.add-result { display: flex; align-items: center; gap: 8px; padding: 6px 0; }
.add-result img { width: 72px; aspect-ratio: 16/9; object-fit: cover; border-radius: 4px; flex-shrink: 0; }
.add-result-title {
  flex: 1; font-size: 13px; line-height: 1.3;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.add-btn {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  background: var(--blue); color: #0f0f0f; font-size: 18px; font-weight: 700;
}
.add-btn:hover:not(:disabled) { opacity: .9; }
.add-btn:disabled { opacity: .4; cursor: not-allowed; }
.q-by { color: var(--text-secondary); }

.role-badge { font-size: 13px; font-weight: 600; padding: 4px 10px; border-radius: 12px; background: var(--bg-elevated); }
.guest-note {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 100%; text-align: center; padding: 24px; color: var(--text-secondary);
  font-size: 15px; line-height: 1.6;
}
.guest-note b { color: var(--text); }
.gate-desc { padding: 0 16px 8px; color: var(--text-secondary); font-size: 14px; }
.gate-err { color: #ff6b6b; padding: 0 16px 8px; font-size: 13px; }
.gate-guest { padding: 0 16px 16px; }

/* ----------------------------- Responsive ----------------------------- */
@media (max-width: 1100px) {
  .watch { grid-template-columns: 1fr; }
  .related-thumb { width: 168px; }
}

/* Desktop: ☰ thu gọn sidebar thành thanh icon (mini-rail) */
@media (min-width: 901px) {
  body.sidebar-collapsed .sidebar { width: var(--sidebar-mini-w); }
  body.sidebar-collapsed .side-item {
    flex-direction: column; gap: 4px; padding: 14px 0;
    justify-content: center; font-size: 10px; text-align: center;
  }
  body.sidebar-collapsed .side-item span:not(.ic) {
    font-size: 10px; line-height: 1.1;
  }
  body.sidebar-collapsed .side-sep,
  body.sidebar-collapsed .side-title { display: none; }
  body.sidebar-collapsed .content,
  body.sidebar-collapsed .watch { margin-left: var(--sidebar-mini-w); }
}

/* Mobile/tablet: sidebar thành drawer trượt, ẩn mặc định */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
    z-index: 110; width: var(--sidebar-w);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }
  body.sidebar-open .sidebar-backdrop { display: block; }
  .content, .watch { margin-left: 0; }
}

@media (max-width: 600px) {
  .header-left { min-width: auto; }
  .logo span.txt { display: none; }
  .search-form input { font-size: 14px; }
}
