:root {
  --bg: #f4f6fb;
  --sidebar-bg: #ffffff;
  --card-bg: #ffffff;
  --text: #1a1d29;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --accent: #4f46e5;
  --accent-light: #eef2ff;
  --accent-contrast: #ffffff;
  --success: #16a34a;
  --danger: #ef4444;
  --shadow: 0 4px 20px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --player-h: 128px;
  --sidebar-w: 320px;
}

[data-theme="dark"] {
  --bg: #0f1220;
  --sidebar-bg: #161a2b;
  --card-bg: #1c2136;
  --text: #eef0f8;
  --text-muted: #9297ab;
  --border: #2a2f45;
  --accent: #818cf8;
  --accent-light: #262c4a;
  --accent-contrast: #10121e;
  --shadow: 0 4px 24px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.sidebar, .topbar, .track-card, .player, .pdf-panel, .icon-btn, .filter-chip {
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease, box-shadow 0.2s ease;
}

.category-list::-webkit-scrollbar,
.track-list-wrap::-webkit-scrollbar {
  width: 8px;
}
.category-list::-webkit-scrollbar-thumb,
.track-list-wrap::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
}
.category-list::-webkit-scrollbar-thumb:hover,
.track-list-wrap::-webkit-scrollbar-thumb:hover {
  background: var(--accent);
}

.app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: 1fr auto;
  grid-template-areas:
    "sidebar main"
    "sidebar player";
  height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  grid-area: sidebar;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  z-index: 30;
}

.sidebar-header {
  padding: 20px 18px 12px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand { display: flex; align-items: center; gap: 10px; }
.brand-icon { font-size: 30px; }
.brand h1 { font-size: 17px; margin: 0; font-weight: 800; }
.brand p { font-size: 12px; margin: 2px 0 0; color: var(--text-muted); }

.search-box {
  margin: 6px 16px 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
}
.search-box input {
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  width: 100%;
  color: var(--text);
}
.search-icon { font-size: 14px; opacity: 0.7; }

.category-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 10px 10px;
}

.category-item {
  border-radius: 12px;
  margin-bottom: 6px;
  overflow: hidden;
}

.category-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13.5px;
  border-radius: 12px;
  transition: background 0.15s ease;
}
.category-header:hover { background: var(--accent-light); }
.category-header.active { background: var(--accent-light); color: var(--accent); }

.category-icon { font-size: 18px; }
.category-name { flex: 1; }
.category-count {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg);
  border-radius: 999px;
  padding: 2px 8px;
  flex-shrink: 0;
}
.category-header.active .category-count { color: var(--accent); }
.category-header.active {
  box-shadow: inset 3px 0 0 var(--accent);
}

.category-progress {
  height: 4px;
  background: var(--bg);
  border-radius: 999px;
  margin: 2px 12px 8px;
  overflow: hidden;
}
.category-progress-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.25s ease;
}

.sidebar-footer {
  padding: 12px 18px 18px;
  border-top: 1px solid var(--border);
}
.progress-summary-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 6px;
}
#progressPercent { font-weight: 700; color: var(--accent); }
.progress-bar-track {
  height: 6px;
  background: var(--bg);
  border-radius: 999px;
  overflow: hidden;
}
.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  width: 0%;
  transition: width 0.3s ease;
}

/* ---------- Main ---------- */
.main {
  grid-area: main;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.topbar-title-wrap { flex: 1; min-width: 0; }
.topbar h2 {
  font-size: 18px;
  margin: 0;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-subtitle {
  margin: 2px 0 0;
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-actions { display: flex; gap: 8px; flex-shrink: 0; }

.icon-btn {
  background: var(--card-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  width: 38px;
  height: 38px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text);
}
.icon-btn:hover { border-color: var(--accent); }

.content-area {
  flex: 1;
  display: flex;
  overflow: hidden;
  min-height: 0;
}

.track-list-wrap {
  flex: 1 1 auto;
  min-width: 0;
  overflow-y: auto;
  padding: 16px 24px calc(var(--player-h) + 24px);
}

.filter-chips {
  display: flex;
  gap: 8px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}
.filter-chip {
  border: 1px solid var(--border);
  background: var(--card-bg);
  color: var(--text-muted);
  font-size: 12.5px;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.filter-chip:hover { border-color: var(--accent); color: var(--accent); }
.filter-chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- PDF panel ---------- */
.resizer {
  display: none;
  width: 6px;
  flex-shrink: 0;
  cursor: col-resize;
  background: var(--border);
  position: relative;
}
.resizer::after {
  content: '';
  position: absolute;
  top: 50%; left: 50%;
  width: 2px; height: 32px;
  transform: translate(-50%, -50%);
  background: var(--text-muted);
  border-radius: 2px;
  opacity: 0.6;
}
.resizer:hover, .resizer.dragging { background: var(--accent); }

.pdf-panel {
  display: none;
  flex-direction: column;
  flex: 1 1 45%;
  min-width: 320px;
  border-left: 1px solid var(--border);
  background: var(--card-bg);
  overflow: hidden;
}
.content-area.pdf-open .resizer { display: block; }
.content-area.pdf-open .pdf-panel { display: flex; }

.pdf-toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.pdf-doc-select {
  flex: 1;
  min-width: 0;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 8px;
  font-size: 13px;
  padding: 7px 8px;
  cursor: pointer;
}
.pdf-toolbar-actions { display: flex; gap: 6px; flex-shrink: 0; }
.pdf-toolbar-actions .icon-btn.small { width: 32px; height: 32px; font-size: 14px; text-decoration: none; }

.pdf-hint {
  margin: 0;
  padding: 8px 14px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--accent-light);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.pdf-frame {
  flex: 1;
  border: none;
  width: 100%;
  background: #525659;
}

#pdfToggleBtn:disabled { opacity: 0.4; cursor: not-allowed; }
#pdfToggleBtn.active { color: var(--accent); background: var(--accent-light); border-color: var(--accent); }

.track-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 14px;
}

.track-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  padding-bottom: 20px;
  border-radius: var(--radius);
  background: var(--card-bg);
  border: 1px solid var(--border);
  cursor: pointer;
  overflow: hidden;
  transition: border-color 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
}
.track-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: var(--shadow);
}
.track-card.playing {
  border-color: var(--accent);
  background: var(--accent-light);
}
.track-card.active {
  box-shadow: 0 0 0 2px var(--accent);
}

.track-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.track-index {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--text-muted);
  flex-shrink: 0;
}
.track-card.playing .track-index { background: var(--accent); color: #fff; }

.track-play-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--bg);
  color: var(--accent);
  font-size: 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transform: scale(0.85);
  transition: opacity 0.15s ease, transform 0.15s ease, background 0.15s ease;
}
.track-card:hover .track-play-btn,
.track-card.playing .track-play-btn {
  opacity: 1;
  transform: scale(1);
}
.track-card.playing .track-play-btn {
  background: var(--accent);
  color: #fff;
}

.track-index.done { background: var(--success); color: #fff; }
.track-index.in-progress { box-shadow: inset 0 0 0 2px var(--accent); color: var(--accent); background: var(--accent-light); }

.track-mini-progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: var(--border);
}
.track-mini-progress-fill {
  height: 100%;
  background: var(--accent);
}

.track-info { min-width: 0; }
.track-name {
  font-size: 14px;
  font-weight: 600;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  line-height: 1.35;
}
.empty-hint {
  color: var(--text-muted);
  text-align: center;
  margin-top: 60px;
  font-size: 14px;
}

/* ---------- Player ---------- */
.player {
  grid-area: player;
  height: var(--player-h);
  background: var(--card-bg);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: 260px 1fr auto;
  align-items: center;
  gap: 20px;
  padding: 0 24px;
  box-shadow: var(--shadow);
  z-index: 20;
}

.player-track-info { display: flex; align-items: center; gap: 12px; min-width: 0; }
.disc {
  position: relative;
  width: 46px;
  height: 46px;
  border-radius: 50%;
  background: var(--accent-light);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
  overflow: hidden;
}
.disc-icon { transition: opacity 0.2s ease; }
.disc.spin .disc-icon { opacity: 0; }

.visualizer {
  position: absolute;
  inset: 0;
  display: none;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
  padding-bottom: 10px;
}
.disc.spin .visualizer { display: flex; }
.visualizer i {
  display: block;
  width: 3px;
  border-radius: 2px;
  background: var(--accent);
  animation: bounce 0.9s ease-in-out infinite;
}
.visualizer i:nth-child(1) { height: 8px; animation-delay: 0s; }
.visualizer i:nth-child(2) { height: 16px; animation-delay: 0.15s; }
.visualizer i:nth-child(3) { height: 22px; animation-delay: 0.3s; }
.visualizer i:nth-child(4) { height: 14px; animation-delay: 0.1s; }
.visualizer i:nth-child(5) { height: 10px; animation-delay: 0.25s; }
@keyframes bounce {
  0%, 100% { transform: scaleY(0.4); }
  50% { transform: scaleY(1.3); }
}

.track-meta { min-width: 0; }
.track-title {
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.track-sub { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.player-center { display: flex; flex-direction: column; gap: 6px; align-items: center; }
.player-controls { display: flex; align-items: center; gap: 14px; }

.ctrl-btn {
  background: transparent;
  border: none;
  font-size: 18px;
  cursor: pointer;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.ctrl-btn:hover { background: var(--accent-light); }
.ctrl-btn.small { font-size: 15px; width: 32px; height: 32px; }
.ctrl-btn.active { color: var(--accent); background: var(--accent-light); }

.play-btn {
  background: var(--accent);
  color: #fff;
  width: 46px;
  height: 46px;
  font-size: 18px;
}
.play-btn:hover { background: var(--accent); opacity: 0.9; }

.seek-row { display: flex; align-items: center; gap: 10px; width: 100%; max-width: 560px; }
.time { font-size: 11.5px; color: var(--text-muted); width: 38px; text-align: center; flex-shrink: 0; }

.seek-bar, .volume-box input {
  -webkit-appearance: none;
  appearance: none;
  height: 5px;
  border-radius: 999px;
  background: var(--border);
  outline: none;
  flex: 1;
}
.seek-bar::-webkit-slider-thumb, .volume-box input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 13px;
  height: 13px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}
.seek-bar::-moz-range-thumb, .volume-box input::-moz-range-thumb {
  width: 13px;
  height: 13px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
}

.player-extra { display: flex; align-items: center; gap: 10px; }

.ab-repeat { display: flex; gap: 4px; }
.chip-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  cursor: pointer;
}
.chip-btn.active { border-color: var(--accent); color: var(--accent); background: var(--accent-light); }

.speed-select {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 8px;
  font-size: 12px;
  padding: 5px 6px;
  cursor: pointer;
}

.volume-box { display: flex; align-items: center; gap: 6px; }
.volume-box input { width: 70px; }

/* ---------- Mobile ---------- */
.overlay { display: none; }
.only-mobile { display: none; }

@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "main"
      "player";
  }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: min(85vw, 320px);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 25;
  }
  .overlay.show { display: block; }
  .only-mobile { display: flex; }

  .player {
    grid-template-columns: 1fr;
    grid-template-rows: auto auto auto;
    height: auto;
    padding: 10px 14px 14px;
    gap: 8px;
  }
  .player-extra { justify-content: center; flex-wrap: wrap; }
  .player-track-info { justify-content: center; }
  :root { --player-h: 168px; }

  .content-area.pdf-open .track-list-wrap { display: none; }
  .content-area.pdf-open .resizer { display: none; }
  .content-area.pdf-open .pdf-panel { flex: 1 1 100%; min-width: 0; border-left: none; }

  .topbar { padding: 12px 14px; gap: 8px; }
  .topbar h2 { font-size: 15px; }
  .topbar-subtitle { font-size: 11px; }

  .track-list-wrap { padding: 12px 14px calc(var(--player-h) + 16px); }

  .filter-chips {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -14px 14px;
    padding: 0 14px 4px;
  }
  .filter-chip { flex-shrink: 0; }
  .filter-chips::-webkit-scrollbar { display: none; }

  .track-grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 10px;
  }

  .sidebar-footer { padding: 10px 14px 14px; }

  .pdf-toolbar { padding: 8px 10px; }
  .pdf-hint { padding: 6px 10px; font-size: 11px; }
}

@media (max-width: 420px) {
  .topbar-subtitle { display: none; }
  .track-grid { grid-template-columns: repeat(2, 1fr); }
}
