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

:root {
  --bg: #0d0d0d;
  --surface: #181818;
  --surface2: #242424;
  --border: #2a2a2a;
  --accent: #1db954;
  --text: #e0e0e0;
  --muted: #888;
  --sidebar-w: 240px;
  --header-h: 56px;
  --player-h: 88px;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  display: grid;
  grid-template:
    "header  header" var(--header-h)
    "sidebar main"   1fr
    "player  player" var(--player-h)
    / var(--sidebar-w) 1fr;
}

/* ── Header ─────────────────────────────────────────────────────── */
header {
  grid-area: header;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 1.25rem;
  z-index: 20;
}
header h1 { font-size: 1.1rem; color: var(--accent); letter-spacing: 0.05em; }
#user-info { display: flex; align-items: center; gap: 0.75rem; font-size: 0.85rem; color: var(--muted); }
.btn-sm {
  background: none; border: 1px solid var(--border); color: var(--text);
  padding: 4px 12px; border-radius: 4px; cursor: pointer; font-size: 0.8rem;
}
.btn-sm:hover { border-color: var(--accent); color: var(--accent); }

/* ── Sidebar ─────────────────────────────────────────────────────── */
#sidebar {
  grid-area: sidebar;
  background: #111;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#search-wrap {
  padding: 0.6rem 0.75rem;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#search-input {
  width: 100%; background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); padding: 7px 12px; border-radius: 20px;
  font-size: 0.825rem; outline: none;
}
#search-input:focus { border-color: var(--accent); }
#sidebar-list { flex: 1; overflow-y: auto; padding: 0.5rem 0; }
.sidebar-label {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); padding: 0.75rem 1rem 0.3rem;
}
.nav-item {
  display: flex; align-items: center; gap: 0.6rem;
  padding: 6px 0.75rem; cursor: pointer; font-size: 0.85rem; color: var(--muted);
  border-radius: 0; transition: background 0.1s, color 0.1s;
  user-select: none;
}
.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active { background: var(--surface2); color: var(--text); }
.nav-art {
  width: 34px; height: 34px; border-radius: 3px; object-fit: cover; flex-shrink: 0;
}
.nav-art-placeholder {
  width: 34px; height: 34px; border-radius: 3px; background: var(--surface2);
  flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 0.9rem;
}
.nav-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── Main ─────────────────────────────────────────────────────────── */
#main {
  grid-area: main;
  display: flex;
  overflow: hidden;
}
#track-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
#view-header {
  padding: 1.25rem 1.5rem 1rem;
  border-bottom: 1px solid var(--border);
  display: none;
  align-items: flex-end;
  gap: 1.25rem;
  flex-shrink: 0;
}
#view-art {
  width: 110px; height: 110px; border-radius: 4px; object-fit: cover;
  box-shadow: 0 4px 20px rgba(0,0,0,0.6); flex-shrink: 0;
}
#view-art-placeholder {
  width: 110px; height: 110px; border-radius: 4px; background: var(--surface2);
  display: flex; align-items: center; justify-content: center; font-size: 2.5rem; flex-shrink: 0;
}
#view-type { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); margin-bottom: 4px; }
#view-title { font-size: 1.6rem; font-weight: 700; line-height: 1.2; }
#view-subtitle { color: var(--muted); font-size: 0.85rem; margin-top: 5px; }

#track-scroll { flex: 1; overflow-y: auto; }

/* ── Track table ─────────────────────────────────────────────────── */
#track-table { width: 100%; border-collapse: collapse; }
#track-table thead th {
  text-align: left; padding: 6px 12px; font-size: 0.72rem;
  color: var(--muted); text-transform: uppercase; letter-spacing: 0.07em;
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; background: var(--bg); z-index: 1;
}
#track-table thead th.col-num { width: 46px; text-align: center; }
#track-table thead th.col-dur { width: 64px; text-align: right; padding-right: 1.5rem; }

.track-row { cursor: pointer; }
.track-row:hover td { background: rgba(255,255,255,0.04); }
.track-row.selected td { background: rgba(29,185,84,0.08); }
.track-row.playing { color: var(--accent); }
.track-row td { padding: 7px 12px; vertical-align: middle; border-bottom: 1px solid rgba(255,255,255,0.03); }

.col-num { text-align: center; color: var(--muted); font-size: 0.85rem; width: 46px; }
.num-text { }
.play-icon { display: none; }
.track-row:hover .num-text,
.track-row.playing .num-text { display: none; }
.track-row:hover .play-icon,
.track-row.playing .play-icon { display: inline; }
.track-row.playing .play-icon { color: var(--accent); }

.track-title-cell { display: flex; align-items: center; gap: 10px; min-width: 0; }
.track-sm-art { width: 38px; height: 38px; border-radius: 3px; object-fit: cover; flex-shrink: 0; }
.track-sm-placeholder {
  width: 38px; height: 38px; border-radius: 3px; background: var(--surface2);
  flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 0.8rem;
}
.t-name { font-size: 0.875rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-artist { font-size: 0.775rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.track-row.playing .t-artist { color: rgba(29,185,84,0.7); }
.t-album { font-size: 0.825rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.t-dur { text-align: right; padding-right: 1.5rem !important; color: var(--muted); font-size: 0.825rem; font-variant-numeric: tabular-nums; }
.note-dot { display: inline-block; width: 5px; height: 5px; border-radius: 50%; background: var(--accent); margin-left: 5px; vertical-align: middle; }

.empty-state { color: var(--muted); text-align: center; padding: 4rem 2rem; font-size: 0.9rem; }
.loading-state { color: var(--muted); text-align: center; padding: 2rem; }

/* ── Annotation pane ─────────────────────────────────────────────── */
#annotation-pane {
  width: 290px;
  min-width: 290px;
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: none;
  flex-direction: column;
  overflow: hidden;
}
#annotation-pane.open { display: flex; }
.ann-head {
  padding: 1rem; border-bottom: 1px solid var(--border);
  display: flex; align-items: flex-start; justify-content: space-between; gap: 0.5rem;
  flex-shrink: 0;
}
#ann-track-name { font-weight: 600; font-size: 0.875rem; }
#ann-track-artist { color: var(--muted); font-size: 0.775rem; margin-top: 2px; }
#ann-close {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 1.3rem; line-height: 1; padding: 0; flex-shrink: 0;
}
#ann-close:hover { color: var(--text); }
.ann-body { flex: 1; overflow-y: auto; padding: 1rem; display: flex; flex-direction: column; gap: 1.25rem; }
.ann-label {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); margin-bottom: 6px;
}
#note-textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--border);
  color: var(--text); padding: 9px 10px; border-radius: 6px;
  font-size: 0.85rem; resize: vertical; min-height: 100px; outline: none; font-family: inherit;
}
#note-textarea:focus { border-color: var(--accent); }
#note-status { font-size: 0.75rem; color: var(--muted); height: 1em; margin-top: 4px; }
.tags-wrap { display: flex; flex-wrap: wrap; gap: 5px; }
.tag-pill {
  background: rgba(29,185,84,0.12); border: 1px solid rgba(29,185,84,0.4); color: var(--accent);
  padding: 3px 9px; border-radius: 10px; font-size: 0.775rem;
  display: inline-flex; align-items: center; gap: 4px; width: fit-content;
}
.tag-pill button { background: none; border: none; color: inherit; cursor: pointer; font-size: 0.9rem; padding: 0; line-height: 1; }
.tag-pill button:hover { color: #fff; }
#add-tag-row { display: flex; gap: 5px; margin-top: 6px; }
#add-tag-input {
  flex: 1; background: var(--bg); border: 1px solid var(--border); color: var(--text);
  padding: 6px 9px; border-radius: 6px; font-size: 0.8rem; outline: none;
}
#add-tag-input:focus { border-color: var(--accent); }
#add-tag-btn {
  background: var(--accent); color: #000; border: none;
  padding: 6px 11px; border-radius: 6px; font-weight: 600; cursor: pointer; font-size: 0.8rem;
}

/* ── Player bar ──────────────────────────────────────────────────── */
#player {
  grid-area: player;
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 0 1.25rem;
  gap: 1rem;
  z-index: 10;
}

/* Left: now playing */
#now-playing { display: flex; align-items: center; gap: 0.75rem; min-width: 0; cursor: pointer; border-radius: 6px; padding: 4px; margin: -4px; transition: background 0.15s; }
#now-playing:hover { background: rgba(255,255,255,0.06); }
#np-art {
  width: 50px; height: 50px; border-radius: 3px; object-fit: cover;
  flex-shrink: 0; display: none;
}
#np-art-placeholder {
  width: 50px; height: 50px; border-radius: 3px; background: var(--surface2);
  flex-shrink: 0; display: flex; align-items: center; justify-content: center; font-size: 1.1rem;
}
#np-track { font-size: 0.85rem; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#np-artist { font-size: 0.775rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }

/* Center: controls */
#player-center { display: flex; flex-direction: column; align-items: center; gap: 5px; }
.ctrl-row { display: flex; align-items: center; gap: 4px; }
.ctrl-btn {
  background: none; border: none; color: var(--muted); cursor: pointer;
  padding: 7px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  transition: color 0.1s;
}
.ctrl-btn:hover { color: var(--text); }
.ctrl-btn.active { color: var(--accent); }
.ctrl-btn svg { width: 18px; height: 18px; fill: currentColor; pointer-events: none; }
#btn-play { color: var(--text); }
#btn-play svg { width: 38px; height: 38px; }

.progress-row { display: flex; align-items: center; gap: 8px; width: 420px; }
.t-time { font-size: 0.7rem; color: var(--muted); font-variant-numeric: tabular-nums; min-width: 34px; }
#t-end { text-align: right; }
#progress-bar {
  flex: 1; height: 4px; background: var(--border); border-radius: 2px; cursor: pointer; position: relative;
}
#progress-fill { height: 100%; background: var(--muted); border-radius: 2px; width: 0%; pointer-events: none; transition: background 0.1s; }
#progress-bar:hover #progress-fill { background: var(--accent); }

/* Right: volume */
#player-right { display: flex; align-items: center; justify-content: flex-end; gap: 6px; }
#vol-bar {
  width: 80px; height: 4px; background: var(--border); border-radius: 2px; cursor: pointer; position: relative;
}
#vol-fill { height: 100%; background: var(--muted); border-radius: 2px; width: 70%; pointer-events: none; }
#vol-bar:hover #vol-fill { background: var(--accent); }

/* ── Login overlay ───────────────────────────────────────────────── */
#login-overlay {
  position: fixed; inset: 0; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1rem; z-index: 100;
}
#login-overlay h2 { font-size: 2rem; }
#login-overlay p { color: var(--muted); max-width: 400px; text-align: center; line-height: 1.6; }
#spotify-login-btn {
  background: var(--accent); color: #000; border: none;
  padding: 13px 30px; border-radius: 24px; font-size: 1rem; font-weight: 700; cursor: pointer; margin-top: 0.75rem;
}
#spotify-login-btn:hover { background: #1ed760; }
#login-err { color: #e55; font-size: 0.85rem; display: none; }

/* ── Device picker ───────────────────────────────────────────────── */
#device-btn {
  display: flex; align-items: center; gap: 5px; cursor: pointer;
  color: var(--muted); font-size: 0.75rem; padding: 4px 8px;
  border-radius: 4px; border: 1px solid transparent;
}
#device-btn:hover { color: var(--text); border-color: var(--border); }
#device-btn.active { color: var(--accent); }
#device-picker {
  position: fixed; bottom: calc(var(--player-h) + 8px); right: 1rem;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 8px; padding: 0.5rem; z-index: 50; min-width: 200px;
}
#device-picker h4 { font-size: 0.75rem; color: var(--muted); padding: 4px 8px; margin-bottom: 4px; text-transform: uppercase; letter-spacing: 0.07em; }
.device-item {
  display: flex; align-items: center; gap: 8px; padding: 8px;
  border-radius: 6px; cursor: pointer; font-size: 0.85rem;
}
.device-item:hover { background: var(--surface); }
.device-item.active-device { color: var(--accent); }
.device-item svg { flex-shrink: 0; }

/* ── Toast ───────────────────────────────────────────────────────── */
#toast {
  position: fixed; bottom: calc(var(--player-h) + 12px); left: 50%; transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--border); color: var(--text);
  padding: 9px 18px; border-radius: 6px; font-size: 0.85rem; z-index: 50;
  opacity: 0; transition: opacity 0.2s; pointer-events: none; white-space: nowrap;
}
#toast.show { opacity: 1; }
#toast.error { border-color: #e55; color: #e55; }
#toast.warn  { border-color: #f59e0b; color: #f59e0b; }

/* ── Playlist groups ─────────────────────────────────────────────── */
.pl-group-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 0.75rem 4px; cursor: pointer; user-select: none;
}
.pl-group-header:hover .sidebar-label { color: var(--text); }
.pl-chevron {
  font-size: 0.6rem; color: var(--muted); transition: transform 0.2s;
}
.pl-group.collapsed .pl-chevron { transform: rotate(-90deg); }
.pl-group.collapsed .pl-items { display: none; }

/* ── Scrollbar ───────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Hide hamburger on desktop; the mobile query below shows it */
@media (min-width: 641px) {
  #hamburger-btn { display: none; }
}

/* ── SDK badge ────────────────────────────────────────────────────── */
#sdk-badge {
  width: 8px; height: 8px; border-radius: 50%; background: var(--accent);
  display: inline-block; margin-left: 4px; vertical-align: middle;
  box-shadow: 0 0 4px var(--accent);
}

/* ── Tag browser ──────────────────────────────────────────────────── */
.tag-grid { display: flex; flex-wrap: wrap; gap: 10px; padding: 1.25rem 1.5rem; }
/* used inside grooves view (no extra padding needed) */
.tag-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 16px; cursor: pointer;
  display: flex; flex-direction: column; gap: 3px;
  transition: border-color 0.15s, background 0.15s;
  position: relative;
}
.tag-card:hover { border-color: var(--accent); background: rgba(29,185,84,0.06); }
.tag-card .tc-name { font-size: 0.875rem; color: var(--accent); }
.tag-card .tc-count { font-size: 0.75rem; color: var(--muted); }
.tag-card .tc-actions {
  display: none; position: absolute; top: 5px; right: 6px;
  gap: 4px;
}
.tag-card:hover .tc-actions { display: flex; }
.tag-card .tc-actions button {
  background: none; border: none; cursor: pointer; padding: 2px 5px;
  border-radius: 4px; font-size: 0.75rem; line-height: 1;
  transition: background 0.12s;
}
.tag-card .tc-actions .tc-rename { color: var(--muted); }
.tag-card .tc-actions .tc-rename:hover { background: rgba(255,255,255,0.1); color: var(--text); }
.tag-card .tc-actions .tc-delete { color: #e55; }
.tag-card .tc-actions .tc-delete:hover { background: rgba(220,50,50,0.15); }

/* ── My Grooves combined view ─────────────────────────────────────── */
.grooves-view { padding: 0 0 2rem; }
.grooves-section { padding: 0 1.5rem 1.5rem; }
.grooves-section + .grooves-section { border-top: 1px solid var(--border); padding-top: 1.5rem; }
.grooves-section-hdr {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.09em;
  color: var(--muted); margin-bottom: 0.75rem;
}
.grooves-tag-grid { display: flex; flex-wrap: wrap; gap: 10px; }
.grooves-search-wrap { margin-bottom: 1rem; }
.grooves-search-input {
  width: 100%; background: var(--surface2); border: 1px solid var(--border);
  color: var(--text); padding: 8px 14px; border-radius: 20px;
  font-size: 0.85rem; outline: none; box-sizing: border-box;
}
.grooves-search-input:focus { border-color: var(--accent); }
.note-snippet {
  font-size: 0.75rem; color: var(--muted); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 280px;
}
.note-snippet mark { background: none; color: var(--accent); }
.tag-dot {
  display: inline-block; width: 5px; height: 5px; border-radius: 50%;
  background: #888; margin-left: 5px; vertical-align: middle;
}

/* ── Mobile (portrait) ───────────────────────────────────────────────────── */
@media (max-width: 640px) {
  /* Keep CSS variables in sync for fixed-positioned elements (sidebar, annotation
     pane) that reference --header-h and --player-h via var(). */
  :root {
    --header-h: calc(56px + env(safe-area-inset-top));
    --player-h: calc(56px + env(safe-area-inset-bottom));
  }

  /* Stack layout: no sidebar column.
     env() used directly in grid-template (not via CSS variable) to work around
     Safari's unreliable cascade of custom-property updates inside media queries.
     100dvh keeps the body exactly the visible viewport height as Safari's toolbar
     shows/hides, preventing the header from being pushed off-screen. */
  body {
    grid-template:
      "header" calc(56px + env(safe-area-inset-top))
      "main"   1fr
      "player" calc(56px + env(safe-area-inset-bottom))
      / 1fr;
    height: 100vh;   /* fallback for older browsers */
    height: 100dvh;  /* dynamic viewport — shrinks/grows with Safari toolbar */
  }
  #main { grid-area: main; }

  /* Hamburger button: show on mobile */
  #hamburger-btn { display: flex; }

  /* Sidebar → slide-in drawer */
  #sidebar {
    position: fixed;
    top: var(--header-h);
    bottom: var(--player-h);
    left: 0;
    width: 280px;
    z-index: 30;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }
  #sidebar.drawer-open { transform: translateX(0); }

  /* Drawer backdrop */
  #drawer-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.55);
    z-index: 29;
  }
  #drawer-backdrop.show { display: block; }

  /* Annotation backdrop — tap outside the sheet to dismiss */
  #ann-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 24; /* below the sheet (25) but above track list */
  }
  #ann-backdrop.show { display: block; }

  /* Drag-handle pill at top of sheet — #annotation-pane is a flex column
     so ::before becomes the first flex item, sitting above .ann-head */
  #annotation-pane::before {
    content: '';
    display: block;
    width: 36px; height: 4px;
    background: var(--border);
    border-radius: 2px;
    margin: 0.6rem auto 0;
    flex-shrink: 0;
  }

  /* Annotation pane → bottom sheet */
  #annotation-pane {
    position: fixed;
    bottom: var(--player-h);
    left: 0; right: 0;
    height: 72vh;
    min-width: unset;
    width: unset;
    border-left: none;
    border-top: 1px solid var(--border);
    border-radius: 14px 14px 0 0;
    transform: translateY(100%);
    transition: transform 0.3s ease;
    display: flex; /* always in DOM so transition works */
    z-index: 25;
  }
  #annotation-pane.open { transform: translateY(0); }

  /* Compact player: hide progress bar, volume, shuffle, repeat */
  #player {
    grid-template-columns: 1fr auto;
    padding: 0 0.75rem;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .progress-row { display: none; }
  #player-right { display: none; }
  #btn-shuffle, #btn-repeat { display: none; }
  #btn-play svg { width: 30px; height: 30px; }

  /* Track table: hide album column */
  th.col-album, td.t-album-cell { display: none; }

  /* Playlist/view header: shrink artwork */
  #view-art, #view-art-placeholder { width: 70px; height: 70px; }
  #view-title { font-size: 1.2rem; }

  /* Push header content below the Dynamic Island / status bar */
  header {
    padding-top: env(safe-area-inset-top);
    padding-left: max(1.25rem, env(safe-area-inset-left));
    padding-right: max(1.25rem, env(safe-area-inset-right));
  }
}
