/* ==========================================================================
   elektromekka.com — playlists + history (Phase 3)
   My-playlists grid, playlist page (collage, share box, reorderable rows),
   history list, add-to-playlist picker popover, styled confirm dialog and
   the account-drawer library shortcuts. Uses ONLY the design tokens from
   css/styles.css and reuses .btn / .btn-icon / .input / .like-btn /
   .catalog-status / .back-link from the shared layers.
   ========================================================================== */

/* ---- 2×2 cover collage ------------------------------------------------------
   Up to 4 distinct album covers on a CSS grid (no canvas). data-n carries the
   cover count: 1 → full bleed, 2 → two full-height halves, 3 → the third
   cover spans the bottom row. Empty playlists keep a quiet surface tile. */
.pl-collage {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  aspect-ratio: 1 / 1;
  overflow: hidden;
  border-radius: var(--radius-lg);
  background: var(--surface-2);
  border: 1px solid var(--border);
}
.pl-collage img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.pl-collage[data-n="1"] img { grid-column: 1 / -1; grid-row: 1 / -1; }
.pl-collage[data-n="2"] img { grid-row: 1 / -1; }
.pl-collage[data-n="3"] img:nth-child(3) { grid-column: 1 / -1; }

/* ---- My playlists ('#playlists') ------------------------------------------ */
.pl-mine { padding-top: 8px; }
.pl-mine[hidden] { display: none; }
.pl-mine__head { padding: 16px 0 8px; display: flex; flex-direction: column; gap: 8px; max-width: 640px; }
.pl-mine__title { margin: 0; font-size: 40px; line-height: 1.05; font-weight: 700; letter-spacing: -0.02em; }
.pl-mine__hint { margin: 0; font-size: 14px; color: var(--muted); }

.pl-grid {
  list-style: none;
  margin: 24px 0 48px;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(190px, 1fr));
  gap: 32px 20px;
}
.pl-card { min-width: 0; }
.pl-card__link {
  display: flex;
  flex-direction: column;
  min-width: 0;
  text-decoration: none;
  color: var(--text);
}
.pl-card__collage { transition: border-color 140ms, box-shadow 140ms; }
.pl-card__link:hover .pl-card__collage,
.pl-card__link:focus-visible .pl-card__collage {
  border-color: var(--border-strong);
  box-shadow: 0 0 40px rgba(122, 0, 255, 0.22);
}
.pl-card__title {
  margin-top: 12px;
  font-size: 15.5px;
  font-weight: 600;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.pl-card__link:hover .pl-card__title { color: var(--lime); }
.pl-card__meta { margin-top: 2px; font-size: 13px; color: var(--muted); }

/* ---- Playlist page ('#playlist/<id>' · '#playlist/s/<token>') ------------- */
.pl { padding-top: 8px; }
.pl[hidden] { display: none; }

.pl-head {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  column-gap: 48px;
  align-items: start;
  padding: 24px 0 32px;
}
.pl-head[hidden] { display: none; }
.pl-head .pl-collage {
  width: 280px;
  box-shadow: var(--glow-violet);
}
.pl-head__main { min-width: 0; display: flex; flex-direction: column; gap: 14px; }

.pl-titlerow { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 18px; }
.pl-titlerow[hidden] { display: none; }
.pl-title {
  margin: 0;
  font-size: 44px;
  line-height: 1.05;
  font-weight: 700;
  letter-spacing: -0.02em;
  overflow-wrap: anywhere;
}
.pl-rename-btn { min-height: 36px; padding: 0 16px; font-size: 13px; color: var(--muted); }
.pl-rename-btn:hover { color: var(--text); }

.pl-rename-form { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.pl-rename-form[hidden] { display: none; }
.pl-rename-form .input { flex: 1 1 240px; min-width: 0; }

.pl-owner { font-size: 15px; color: var(--violet-text); }
.pl-owner[hidden] { display: none; }
.pl-meta { font-size: 13px; color: var(--muted); }

.pl-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 16px; padding-top: 4px; }

/* destructive actions share one quiet danger treatment */
.pl-danger { border-color: rgba(255, 138, 128, 0.4); color: var(--danger); }
.pl-danger:hover { border-color: var(--danger); color: var(--danger); }

/* share box: state line + link row (visible while the playlist is shared) */
.pl-share {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
}
.pl-share[hidden] { display: none; }
.pl-share__state { margin: 0; font-size: 13px; color: var(--muted); }
.pl-share__row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.pl-share__link {
  flex: 1 1 220px;
  min-width: 0;
  font-size: 13px;
  color: var(--muted);
  background: var(--surface-3);
}
.pl-share__row .btn { min-height: 38px; padding: 0 14px; font-size: 13px; }

.pl-msg { margin: 0; font-size: 13px; color: var(--lime); }
.pl-msg.is-warn { color: var(--danger-soft); }
.pl-msg[hidden] { display: none; }

/* ---- Track rows (playlist page + history) ---------------------------------- */
.pl-tracks { list-style: none; margin: 0 0 48px; padding: 0; }

.pl-row {
  display: flex;
  align-items: center;
  gap: 10px 14px;
  padding: 10px 8px;
  border-bottom: 1px solid var(--border);
}
.pl-row.is-busy { opacity: 0.45; pointer-events: none; }
.pl-row.is-dragging { opacity: 0.4; }
.pl-row.is-current {
  background: var(--surface);
  border-radius: 8px;
  border-bottom-color: transparent;
}

.pl-row__handle {
  flex: 0 0 auto;
  width: 36px;
  height: 44px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: grab;
  touch-action: none;
}
.pl-row__handle:hover { color: var(--text); }
.pl-row__handle:active { cursor: grabbing; }
.pl-row__handle .icon { width: 18px; height: 18px; }
.pl-row__handle[hidden] { display: none; }

.pl-row__index {
  flex: 0 0 auto;
  width: 24px;
  font-family: var(--font-label);
  font-size: 11px;
  color: var(--muted);
}
.pl-row.is-current .pl-row__index { color: var(--lime); }

.pl-row__main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.pl-row__thumb {
  flex: 0 0 auto;
  width: 48px;
  height: 48px;
  object-fit: cover;
  border-radius: 4px;
  background: var(--surface-2);
}
.pl-row__text { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.pl-row__title {
  font-size: 15.5px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pl-row.is-current .pl-row__title { color: var(--lime); }
.pl-row__main:hover .pl-row__title { color: var(--lime); }
.pl-row__sub {
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pl-row__sub[hidden] { display: none; }

.pl-row__duration { flex: 0 0 auto; width: 44px; text-align: right; font-size: 13px; color: var(--muted); }
.pl-row__time { flex: 0 0 auto; font-size: 12.5px; color: var(--muted); white-space: nowrap; }

.pl-row__remove {
  flex: 0 0 auto;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.pl-row__remove:hover { background: var(--surface-2); color: var(--danger); }
.pl-row__remove[hidden] { display: none; }

.pl-empty { padding: 24px 8px; font-size: 14px; color: var(--muted); }

/* ---- History ('#history') -------------------------------------------------- */
.pl-history { padding-top: 8px; }
.pl-history[hidden] { display: none; }
.pl-history__head {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px 24px;
  padding: 16px 0 16px;
  border-bottom: 1px solid var(--border);
}
.pl-history__text { display: flex; flex-direction: column; gap: 8px; max-width: 640px; }
.pl-history__title { margin: 0; font-size: 40px; line-height: 1.05; font-weight: 700; letter-spacing: -0.02em; }
.pl-history__hint { margin: 0; font-size: 14px; color: var(--muted); }

.pl-note {
  margin: 14px 0 0;
  padding: 12px 16px;
  font-size: 13px;
  color: var(--violet-text);
  background: var(--violet-dim);
  border-radius: var(--radius);
}
.pl-note[hidden] { display: none; }

.pl-tracks--history { margin-top: 8px; }
.pl-row--history .pl-row__main { gap: 14px; }

/* ---- Add-to-playlist '+' on album track rows (template in index.html) ------ */
.track__addpl { width: 40px; height: 40px; color: var(--muted); }
.track__addpl:hover { color: var(--lime); }
.track__addpl .icon { width: 18px; height: 18px; }

/* ---- Picker popover --------------------------------------------------------
   Bottom sheet on phones; on desktop it anchors to the invoking '+' button
   (js sets fixed coordinates and adds .is-anchored). Above the now-playing
   sheet (80) and the auth drawer (90). */
.plpicker {
  position: fixed;
  inset: 0;
  z-index: 95;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.plpicker[hidden] { display: none; }

body.pl-modal-open { overflow: hidden; }

.plpicker__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 10, 0.78);
}

.plpicker__panel {
  position: relative;
  width: 100%;
  max-width: 480px;
  max-height: calc(100% - 48px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom: 0;
  border-radius: 16px 16px 0 0;
  padding: 18px 20px calc(24px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* desktop: floating card; anchored mode pins it next to the '+' button */
@media (min-width: 760px) {
  .plpicker { align-items: center; }
  .plpicker__panel {
    width: 340px;
    max-height: calc(100% - 64px);
    border-bottom: 1px solid var(--border);
    border-radius: var(--radius-lg);
    padding: 18px 20px 20px;
    box-shadow: 0 16px 56px rgba(0, 0, 0, 0.55);
  }
  .plpicker.is-anchored { align-items: flex-start; justify-content: flex-start; }
  .plpicker.is-anchored .plpicker__panel { position: fixed; max-height: min(480px, calc(100vh - 32px)); }
}

.plpicker__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.plpicker__title { font-size: 16px; font-weight: 700; letter-spacing: -0.01em; }
.plpicker__close {
  flex: none;
  width: 34px;
  height: 34px;
  margin-right: -6px;
  border: 0;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.plpicker__close:hover { color: var(--text); }

.plpicker__status { margin: 0; font-size: 13px; color: var(--lime); }
.plpicker__status.is-warn { color: var(--danger-soft); }
.plpicker__status[hidden] { display: none; }

.plpicker__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  max-height: 260px;
  overflow-y: auto;
}
.plpicker__item + .plpicker__item { border-top: 1px solid var(--border); }
.plpicker__btn {
  width: 100%;
  min-height: 48px;
  padding: 6px 4px;
  border: 0;
  background: transparent;
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  text-align: left;
  font-size: 14.5px;
}
.plpicker__btn:hover { color: var(--lime); }
.plpicker__name {
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}
.plpicker__count { flex: none; font-size: 12.5px; color: var(--muted); }
.plpicker__empty { padding: 12px 4px; font-size: 13.5px; color: var(--muted); }

.plpicker__new {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 48px;
  padding: 0 4px;
  border: 0;
  border-top: 1px solid var(--border);
  background: transparent;
  color: var(--lime);
  font-size: 14.5px;
  font-weight: 500;
  text-align: left;
}
.plpicker__new:hover { color: var(--lime-hover); }
.plpicker__new .icon { width: 18px; height: 18px; }
.plpicker__new[hidden] { display: none; }

.plpicker__form { display: flex; gap: 8px; align-items: center; }
.plpicker__form[hidden] { display: none; }
.plpicker__form .input { flex: 1 1 auto; min-width: 0; }

/* ---- Confirm dialog (delete playlist, clear history) ----------------------- */
.plconfirm {
  position: fixed;
  inset: 0;
  z-index: 96; /* above the picker */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px));
}
.plconfirm[hidden] { display: none; }

.plconfirm__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 10, 10, 0.78);
}
.plconfirm__panel {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 16px 56px rgba(0, 0, 0, 0.55);
}
.plconfirm__title { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -0.01em; }
.plconfirm__text { margin: 0; font-size: 14px; color: var(--muted); }
.plconfirm__actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding-top: 8px;
}

/* ---- Account drawer: library shortcuts (markup in js/auth-ui.js) ----------- */
.auth-shortcuts { display: flex; gap: 10px; }
.auth-shortcuts .btn { flex: 1 1 auto; text-decoration: none; }

/* ---- Phone (≤ 759px) -------------------------------------------------------- */
@media (max-width: 759px) {
  .pl-mine__title, .pl-history__title { font-size: 28px; }
  .pl-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 24px 14px; margin-top: 18px; }

  .pl-head {
    grid-template-columns: 1fr;
    row-gap: 18px;
    padding: 16px 0 24px;
  }
  .pl-head .pl-collage { width: min(280px, 60vw); }
  .pl-title { font-size: 28px; }

  .pl-row { gap: 8px 10px; }
  .pl-row__index { display: none; }
  .pl-row__duration { display: none; }
  .pl-row .like-btn { padding: 0 4px; }
  .pl-row .like-btn [data-like="count"] { display: none; }
  .pl-row__handle { width: 32px; }

  .pl-history__head { padding-top: 10px; }
  .pl-share__row .btn { flex: 1 1 auto; }

  /* compact '+' beside the heart in the album rows */
  .track .track__addpl { width: 36px; height: 36px; }
}

@media (prefers-reduced-motion: reduce) {
  .pl-card__collage, .pl-collage { transition: none; }
}

/* [hidden] must always win — author display rules (.btn etc.) would otherwise
   override the UA's [hidden]{display:none} and leak owner controls into the
   read-only shared view. Blanket guard for all Phase 3 views. */
#playlist-view [hidden],
#playlists-view [hidden],
#history-view [hidden] { display: none !important; }
