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

  :root {
    --bg:        #0d0d12;
    --surface:   #16161f;
    --surface2:  #1e1e2c;
    --border:    #2a2a3d;
    --accent:    #7c3aed;
    --accent2:   #a855f7;
    --green:     #22c55e;
    --yellow:    #eab308;
    --red:       #ef4444;
    --text:      #e2e2f0;
    --muted:     #888899;
    --radius:    10px;
    --shadow:    0 4px 24px rgba(0,0,0,.45);
  }

  html, body {
    height: 100%;
    background: var(--bg);
    color: var(--text);
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    font-size: 15px;
    line-height: 1.5;
  }

  /* ── Layout ────────────────────────────────────────────────── */
  .layout {
    display: grid;
    grid-template-rows: auto 1fr auto;
    min-height: 100vh;
  }

  header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0 2rem;
    display: flex;
    align-items: center;
    gap: 1.5rem;
    height: 60px;
  }

  .logo {
    font-size: 1.2rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--accent), var(--accent2));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    white-space: nowrap;
  }

  main {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0;
    max-width: 1100px;
    width: 100%;
    margin: 0 auto;
    padding: 2rem 1.5rem;
  }

  /* ── Search bar ─────────────────────────────────────────────── */
  .search-section { }

  .search-bar {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 1rem;
  }

  .search-bar input {
    flex: 1;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 1rem;
    padding: 0.65rem 1rem;
    outline: none;
    transition: border-color .15s;
  }
  .search-bar input:focus { border-color: var(--accent); }
  .search-bar input::placeholder { color: var(--muted); }

  .btn {
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: var(--radius);
    padding: 0.65rem 1.2rem;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 600;
    transition: background .15s, transform .1s;
    white-space: nowrap;
  }
  .btn:hover  { background: var(--accent2); }
  .btn:active { transform: scale(.97); }
  .btn:disabled { opacity: .5; cursor: not-allowed; }

  .btn-sm {
    padding: 0.35rem 0.85rem;
    font-size: 0.82rem;
  }
  .btn-ghost {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
  }
  .btn-ghost:hover { border-color: var(--accent); color: var(--text); background: transparent; }

  /* Mode toggle */
  .mode-toggle {
    display: flex;
    gap: 0.4rem;
    margin-bottom: 1.5rem;
  }

  .mode-btn {
    background: var(--surface);
    border: 1px solid var(--border);
    color: var(--muted);
    border-radius: 999px;
    padding: 0.3rem 1rem;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    transition: all .15s;
  }
  .mode-btn.active {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
  }

  /* ── Results ─────────────────────────────────────────────────── */
  .results-header {
    color: var(--muted);
    font-size: 0.82rem;
    margin-bottom: 0.75rem;
    letter-spacing: .04em;
    text-transform: uppercase;
  }

  .result-list { display: flex; flex-direction: column; gap: 0.5rem; }

  .result-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1rem;
    display: flex;
    align-items: center;
    gap: 1rem;
    transition: border-color .15s, background .15s;
    cursor: default;
  }
  .result-card:hover { border-color: var(--accent); background: var(--surface2); }
  .result-card.in-library { border-left: 3px solid var(--green); }

  .thumb {
    width: 54px;
    height: 54px;
    border-radius: 6px;
    object-fit: cover;
    background: var(--surface2);
    flex-shrink: 0;
  }
  .thumb-placeholder {
    width: 54px;
    height: 54px;
    border-radius: 6px;
    background: var(--surface2);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.4rem;
    flex-shrink: 0;
  }

  .result-info { flex: 1; min-width: 0; }
  .result-title {
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .result-sub {
    color: var(--muted);
    font-size: 0.83rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .result-snippet {
    color: var(--muted);
    font-size: 0.78rem;
    margin-top: 0.25rem;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
  .result-snippet mark { background: transparent; color: var(--accent2); font-weight: 600; }

  .result-actions { display: flex; flex-direction: column; gap: 0.35rem; align-items: flex-end; }

  .badge-library {
    font-size: 0.72rem;
    background: rgba(34,197,94,.15);
    color: var(--green);
    border-radius: 999px;
    padding: 0.15rem 0.55rem;
    white-space: nowrap;
  }

  .badge-source {
    font-size: 0.72rem;
    color: var(--muted);
    border-radius: 999px;
    border: 1px solid var(--border);
    padding: 0.1rem 0.5rem;
  }

  .duration { color: var(--muted); font-size: 0.8rem; }

  /* ── Download Queue Tab ──────────────────────────────────────── */
  .queue-section { }

  .queue-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-bottom: 1.25rem;
    flex-wrap: wrap;
  }
  .queue-header h2 { font-size: 1.05rem; font-weight: 700; }

  .queue-stats {
    font-size: 0.8rem;
    color: var(--muted);
    margin-left: auto;
  }

  .queue-list { display: flex; flex-direction: column; gap: 0.5rem; }

  .queue-item {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.65rem 0.9rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    transition: border-color .15s;
  }
  .queue-item:hover { border-color: var(--accent); }
  .queue-item.is-active { border-color: var(--yellow); }

  .queue-thumb {
    width: 40px;
    height: 40px;
    border-radius: 6px;
    background: var(--surface2);
    flex-shrink: 0;
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
  }
  .queue-thumb img { width: 100%; height: 100%; object-fit: cover; }

  .queue-item-info { flex: 1; min-width: 0; }
  .queue-item-title {
    font-size: 0.88rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .queue-item-meta {
    font-size: 0.75rem;
    color: var(--muted);
    display: flex;
    align-items: center;
    gap: 0.5rem;
    margin-top: 0.1rem;
    flex-wrap: wrap;
  }

  .status-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    font-size: 0.72rem;
    padding: 0.1rem 0.45rem;
    border-radius: 999px;
    font-weight: 600;
    letter-spacing: .01em;
  }
  .status-badge.queued      { background: var(--surface2); color: var(--muted); }
  .status-badge.downloading { background: rgba(234,179,8,.15); color: var(--yellow); }
  .status-badge.tagging     { background: rgba(168,85,247,.15); color: var(--accent2); }
  .status-badge.complete    { background: rgba(34,197,94,.12); color: var(--green); }
  .status-badge.failed      { background: rgba(239,68,68,.12); color: var(--red); }

  .status-badge.downloading::before,
  .status-badge.tagging::before {
    content: '';
    width: 7px; height: 7px;
    border-radius: 50%;
    background: currentColor;
    animation: pulse 1s infinite;
    flex-shrink: 0;
  }

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

  .queue-controls {
    display: flex;
    align-items: center;
    gap: 0.3rem;
    flex-shrink: 0;
  }
  .q-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--muted);
    cursor: pointer;
    font-size: 0.78rem;
    padding: 0.2rem 0.45rem;
    line-height: 1.4;
    transition: color .15s, border-color .15s, background .15s;
  }
  .q-btn:hover:not(:disabled) { color: var(--text); border-color: var(--accent); background: var(--surface2); }
  .q-btn:disabled { opacity: 0.3; cursor: default; }
  .q-btn.danger:hover:not(:disabled) { color: var(--red); border-color: var(--red); }

  .queue-section-label {
    font-size: 0.72rem;
    font-weight: 700;
    letter-spacing: .06em;
    text-transform: uppercase;
    color: var(--muted);
    margin: 1rem 0 0.4rem;
  }

  .queue-empty {
    color: var(--muted);
    font-size: 0.85rem;
    text-align: center;
    padding: 3rem 0;
  }

  /* Tab badge */
  .tab-badge {
    display: none;
    background: var(--accent);
    color: #fff;
    border-radius: 999px;
    font-size: 0.68rem;
    padding: 0.05rem 0.4rem;
    min-width: 18px;
    text-align: center;
    line-height: 1.5;
    font-weight: 700;
  }
  .tab-badge.visible { display: inline-block; }

  /* ── Loading spinner ─────────────────────────────────────────── */
  .spinner {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin .6s linear infinite;
    vertical-align: middle;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* ── Download modal ─────────────────────────────────────────── */
  .modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 1rem;
  }
  .modal-overlay.hidden { display: none; }

  .modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 1.5rem;
    width: 100%;
    max-width: 440px;
    box-shadow: var(--shadow);
  }

  .modal h3 { margin-bottom: 1rem; font-size: 1rem; }

  .modal label { font-size: 0.83rem; color: var(--muted); display: block; margin-bottom: 0.25rem; }
  .modal input {
    width: 100%;
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 7px;
    color: var(--text);
    font-size: 0.9rem;
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.75rem;
    outline: none;
  }
  .modal input:focus { border-color: var(--accent); }

  .modal-actions { display: flex; gap: 0.5rem; justify-content: flex-end; margin-top: 0.5rem; }
  .modal-body-text { color: var(--muted); font-size: .86rem; line-height: 1.35; margin-bottom: .5rem; }
  .modal-candidate-list { display: flex; flex-direction: column; gap: .45rem; max-height: 48vh; overflow-y: auto; margin: .5rem 0; }
  .modal-candidate-item {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: .45rem .55rem;
    background: var(--surface2);
    font-size: .82rem;
  }
  .modal-candidate-item label { display: flex; gap: .5rem; align-items: center; margin: 0; cursor: pointer; }
  .modal-candidate-item input[type="radio"] { margin: 0; align-self: center; }
  .modal-candidate-thumb {
    width: 48px;
    height: 36px;
    border-radius: 4px;
    border: 1px solid var(--border);
    object-fit: cover;
    background: var(--surface);
    flex: 0 0 auto;
  }
  .modal-candidate-content { min-width: 0; }
  .modal-candidate-meta { color: var(--muted); font-size: .74rem; margin-top: .2rem; }
  .modal-candidate-link { color: var(--accent2); font-size: .74rem; word-break: break-all; text-decoration: none; }
  .modal-candidate-link:hover { text-decoration: underline; }
  .feedback-range {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 8px;
    border-radius: 999px;
    outline: none;
    border: 1px solid var(--border);
    background: linear-gradient(to right, var(--accent) 0%, var(--accent) 100%, var(--border) 100%, var(--border) 100%);
    margin: .2rem 0 .25rem;
  }
  .feedback-range::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    border: 2px solid var(--accent);
    cursor: pointer;
  }
  .feedback-range::-moz-range-thumb {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #fff;
    border: 2px solid var(--accent);
    cursor: pointer;
  }
  .feedback-range-scale {
    display: flex;
    justify-content: space-between;
    font-size: .7rem;
    color: var(--muted);
    user-select: none;
    margin-top: .1rem;
  }
  .feedback-range-ticks {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: .05rem;
    margin-bottom: .02rem;
    padding: 0 2px;
  }
  .feedback-range-ticks span {
    width: 1px;
    height: 8px;
    background: var(--border);
    opacity: .95;
    display: inline-block;
  }
  .feedback-range-value {
    font-size: .74rem;
    color: var(--muted);
    margin-bottom: .2rem;
  }

  /* ── Toast ───────────────────────────────────────────────────── */
  .toast-container {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    z-index: 200;
  }

  .toast {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.6rem 1rem;
    font-size: 0.88rem;
    box-shadow: var(--shadow);
    animation: slideIn .2s ease;
    max-width: 300px;
  }
  .toast.success { border-left: 3px solid var(--green); }
  .toast.error   { border-left: 3px solid var(--red); }
  @keyframes slideIn { from { transform: translateX(20px); opacity: 0; } }

  /* ── Nav tabs (Search / Spotify) ───────────────────────────────── */
  .nav-tabs {
    display: flex;
    gap: 0.25rem;
    align-items: center;
    flex: 1;
  }
  .nav-tab {
    background: transparent;
    border: none;
    color: var(--muted);
    font-size: 0.9rem;
    font-weight: 600;
    padding: 0.4rem 0.9rem;
    border-radius: 6px;
    cursor: pointer;
    transition: all .15s;
    display: flex;
    align-items: center;
    gap: 0.4rem;
  }
  .nav-tab:hover  { color: var(--text); background: var(--surface2); }
  .nav-tab.active { color: var(--text); background: var(--surface2); }
  .nav-tab .spotify-dot {
    width: 7px; height: 7px;
    border-radius: 50%;
    background: var(--green);
    display: none;
  }
  .nav-tab .spotify-dot.visible { display: block; }

  .tab-panel { display: none; }
  .tab-panel.active { display: contents; }

  /* ── Spotify section ─────────────────────────────────────────────── */
  .spotify-section { }

  /* Connect panel */
  .spotify-connect {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 2rem;
    text-align: center;
    max-width: 480px;
  }
  .spotify-logo { font-size: 2.5rem; margin-bottom: 0.75rem; }
  .spotify-connect h2 { font-size: 1.1rem; margin-bottom: 0.5rem; }
  .spotify-connect p  { color: var(--muted); font-size: 0.88rem; margin-bottom: 1rem; line-height: 1.6; }
  .redirect-uri {
    background: var(--surface2);
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 0.5rem 0.85rem;
    font-family: monospace;
    font-size: 0.82rem;
    color: var(--accent2);
    margin-bottom: 1rem;
    word-break: break-all;
    text-align: left;
  }

  /* User profile bar */
  .spotify-user {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.65rem 1rem;
    margin-bottom: 1.25rem;
  }
  .spotify-avatar {
    width: 36px; height: 36px;
    border-radius: 50%;
    object-fit: cover;
    background: var(--surface2);
    font-size: 1.2rem;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
  }
  .spotify-user-name { font-weight: 600; font-size: 0.95rem; }
  .spotify-user-sub  { color: var(--muted); font-size: 0.78rem; }

  /* Playlist grid */
  .playlist-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 0.85rem;
    margin-bottom: 1.5rem;
  }
  .playlist-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem;
    cursor: pointer;
    transition: border-color .15s, background .15s, transform .1s;
    position: relative;
  }
  .playlist-card:hover  { border-color: var(--accent); background: var(--surface2); transform: translateY(-2px); }
  .playlist-card.active { border-color: var(--accent); background: var(--surface2); }
  /* Folder-assign button — top-right of card, hidden until hover */
  .playlist-folder-btn {
    position: absolute; top: .35rem; right: .35rem;
    background: var(--surface2); border: 1px solid var(--border);
    border-radius: 4px; padding: .1rem .3rem; font-size: .7rem;
    cursor: pointer; opacity: 0; transition: opacity .15s;
    line-height: 1.4; color: var(--muted);
  }
  .playlist-card:hover .playlist-folder-btn,
  .playlist-card.in-folder .playlist-folder-btn { opacity: 1; }
  .playlist-card.in-folder .playlist-folder-btn { color: var(--accent2); border-color: var(--accent2); }
  .playlist-repair-btn {
    position: absolute; top: .35rem; left: .35rem;
    background: var(--surface2); border: 1px solid var(--border);
    border-radius: 4px; padding: .1rem .3rem; font-size: .68rem;
    cursor: pointer; opacity: 0; transition: opacity .15s;
    line-height: 1.4; color: var(--muted);
  }
  .playlist-card:hover .playlist-repair-btn { opacity: 1; }
  /* View filter tabs */
  .view-tab {
    padding: .2rem .55rem; border-radius: 999px; border: 1px solid var(--border);
    background: transparent; color: var(--muted); font-size: .75rem; cursor: pointer;
    transition: background .12s, color .12s, border-color .12s; white-space: nowrap;
  }
  .view-tab:hover  { background: var(--surface2); color: var(--text); }
  .view-tab.active { background: var(--accent); color: #fff; border-color: var(--accent); }
  .view-tab.folder-tab { border-style: dashed; }
  .view-tab.folder-tab.active { border-style: solid; }
  .playlist-art {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 6px;
    background: var(--surface2);
    margin-bottom: 0.6rem;
    font-size: 2.5rem;
    display: flex; align-items: center; justify-content: center;
  }
  .playlist-art img { width: 100%; height: 100%; object-fit: cover; border-radius: 6px; }
  .playlist-name  { font-weight: 600; font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .playlist-meta  { color: var(--muted); font-size: 0.75rem; }

  /* Track list */
  .track-panel {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 1.1rem;
  }
  .track-panel-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.85rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface2);
  }
  .track-panel-title { font-weight: 700; font-size: 0.95rem; flex: 1; }

  .track-list { height: 45vh; overflow-y: auto; }
  .track-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.55rem 1rem;
    border-bottom: 1px solid rgba(255,255,255,.04);
    transition: background .1s;
    cursor: pointer;
  }
  .track-row:hover   { background: var(--surface2); }
  .track-row.checked { background: rgba(124,58,237,.12); }
  .track-row input[type=checkbox] { accent-color: var(--accent); flex-shrink: 0; }
  .track-thumb {
    width: 36px; height: 36px;
    border-radius: 4px;
    object-fit: cover;
    background: var(--surface2);
    flex-shrink: 0;
    font-size: 1.2rem;
    display: flex; align-items: center; justify-content: center;
  }
  .track-thumb img { width: 100%; height: 100%; border-radius: 4px; object-fit: cover; }
  .track-info { flex: 1; min-width: 0; }
  .track-name   { font-size: 0.88rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .track-artist { font-size: 0.78rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .track-dur    { font-size: 0.78rem; color: var(--muted); white-space: nowrap; }
  .track-sync-badge {
    margin-left: .45rem;
    font-size: .68rem;
    color: #facc15;
    border: 1px solid rgba(250,204,21,.4);
    border-radius: 999px;
    padding: .05rem .35rem;
    vertical-align: middle;
    white-space: nowrap;
  }

  /* ── YouTube Profiles section ──────────────────────────────────── */
  .yt-profiles-section { padding-right: 2rem; }

  .yt-profile-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    display: flex;
    align-items: center;
    gap: 0.85rem;
    margin-bottom: 0.5rem;
    transition: border-color .15s;
  }
  .yt-profile-card:hover { border-color: var(--accent); }
  .yt-profile-card.yt-active { border-color: var(--green); background: rgba(34,197,94,.04); }

  .yt-profile-icon {
    font-size: 1.6rem;
    width: 44px; height: 44px;
    display: flex; align-items: center; justify-content: center;
    background: var(--surface2);
    border-radius: 50%;
    flex-shrink: 0;
  }
  .yt-profile-info { flex: 1; min-width: 0; }
  .yt-profile-name { font-weight: 600; font-size: 0.9rem; }
  .yt-profile-sub  { font-size: 0.78rem; color: var(--muted); margin-top: 0.15rem; }

  .badge-active {
    font-size: 0.72rem;
    background: rgba(34,197,94,.18);
    color: var(--green);
    border-radius: 999px;
    padding: 0.15rem 0.55rem;
    font-weight: 600;
  }

  .yt-info-box {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
    font-size: 0.84rem;
    color: var(--muted);
    line-height: 1.6;
    margin-bottom: 1.25rem;
  }
  .yt-info-box a    { color: var(--accent2); text-decoration: none; }
  .yt-info-box a:hover { text-decoration: underline; }
  .yt-info-box code {
    background: var(--surface2);
    border-radius: 4px;
    padding: .1rem .35rem;
    font-family: monospace;
    font-size: .82rem;
    color: var(--text);
  }
  .yt-profile-actions {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
    align-items: flex-end;
    flex-shrink: 0;
  }
