:root {
  --bg: #121212;
  --head: #161616;
  --card: #1c1c1c;
  --card2: #262626;
  --line: #2c2c2c;
  --text: #fff;
  --muted: #a7a7a7;
  --green: #1ed760;
  --green-bg: #173a24;
  --red: #f3727f;
  --red-bg: #3a1a1e;
  --radius: 22px;
  --mood: #3a2626; /* tint taken from the current album cover (setMood) */

  /* Motion: quick and decelerating. Exits are faster than entrances. No bounce. */
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);   /* quint */
  --ease-expo: cubic-bezier(0.16, 1, 0.3, 1);
  --ease-in: cubic-bezier(0.55, 0, 1, 0.45);
  --fast: 140ms;
  --med: 240ms;

  /* Layers */
  --z-overlay: 15;
  --z-modal: 20;
  --z-toast: 30;
}

* { box-sizing: border-box; margin: 0; }
/* Thin dark scroll bars instead of the browser's chunky white ones */
* { scrollbar-width: thin; scrollbar-color: #3a3a3a transparent; }
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: #4a4a4a; }
html, body { height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
/* Soft wash of the album cover's colour from the top of the screen, fading between songs */
body::before {
  content: ''; position: fixed; inset: 0; z-index: -1; pointer-events: none;
  background-color: var(--mood);
  -webkit-mask-image: radial-gradient(130% 60% at 50% 0%, #000 0%, transparent 70%);
  mask-image: radial-gradient(130% 60% at 50% 0%, #000 0%, transparent 70%);
  opacity: .55;
  transition: background-color 600ms var(--ease-out);
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
button:disabled { opacity: .35; cursor: default; }
:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
a { color: inherit; }
.hidden { display: none !important; }

/* Header */
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: calc(14px + env(safe-area-inset-top)) 16px 14px;
  background: rgba(22, 22, 22, .82);
  border-bottom: 1px solid #1f1f1f;
}
.titles { flex: 1; min-width: 0; }
.topbar h1 { font-size: 26px; font-weight: 800; letter-spacing: -.02em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar p { color: var(--muted); font-size: 13px; margin-top: 2px; font-variant-numeric: tabular-nums; }
.icon { width: 44px; height: 44px; border-radius: 50%; background: var(--card2); display: grid; place-items: center; flex: none; transition: transform var(--fast) var(--ease-out), background-color var(--fast); }
.icon svg { width: 20px; height: 20px; }
.icon:active { transform: scale(.9); }

main { flex: 1; display: flex; flex-direction: column; width: 100%; max-width: 520px; margin: 0 auto; padding: 14px 16px 16px; min-height: 0; }

/* Generic */
.center { flex: 1; display: grid; place-items: center; text-align: center; }
.hero { display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 340px; width: 100%; }
.hero h2 { font-size: 30px; font-weight: 800; letter-spacing: -.02em; text-wrap: balance; }
.hero p { color: var(--muted); text-wrap: pretty; }
.logo { width: 72px; height: 72px; border-radius: 22px; background: var(--green-bg); display: grid; place-items: center; color: var(--green); }
.logo svg { width: 36px; height: 36px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; min-height: 52px; padding: 0 24px;
  border-radius: 999px; font-weight: 700; text-decoration: none; width: 100%;
  transition: transform var(--fast) var(--ease-out), background-color var(--fast), filter var(--fast);
}
.btn:active:not(:disabled) { transform: scale(.97); }
.btn.primary { background: var(--green); color: #000; }
.btn.ghost { background: var(--card2); }
.btn.text { background: none; color: var(--muted); min-height: 44px; font-weight: 600; }
@media (hover: hover) {
  .btn.primary:hover { filter: brightness(1.08); }
  .btn.ghost:hover, .icon:hover { background: #2e2e2e; }
  .btn.text:hover { color: var(--text); }
}
.muted { color: var(--muted); }
.small { font-size: 13px; }
.err { color: var(--red); font-size: 14px; }
.box { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }

/* Loading placeholders: shapes of what's coming, with a gentle shimmer */
.skel { background: var(--card2); position: relative; overflow: hidden; }
.skel::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,.05), transparent);
  transform: translateX(-100%); animation: shimmer 1.2s var(--ease-out) infinite;
}
@keyframes shimmer { to { transform: translateX(100%); } }
.skel-row { display: flex; align-items: center; gap: 14px; padding: 12px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; }
.skel-row .skel:first-child { width: 56px; height: 56px; border-radius: 10px; flex: none; }
.skel-lines { flex: 1; display: grid; gap: 8px; }
.skel-lines .skel { height: 12px; border-radius: 6px; }
.skel-lines .skel:last-child { width: 40%; }
.skel-card { flex: 1; border-radius: var(--radius); min-height: 300px; }
.saving { color: var(--green); animation: beat 900ms var(--ease-out) infinite; }
.saving svg { width: 40px; height: 40px; }
@keyframes beat { 0%, 100% { transform: scale(1); } 30% { transform: scale(1.14); } }

/* Playlist list */
.list { display: flex; flex-direction: column; gap: 10px; }
.list .note { margin-bottom: 2px; }
.pl { display: flex; align-items: center; gap: 14px; padding: 12px; text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: 18px; width: 100%; transition: transform var(--fast) var(--ease-out), background-color var(--fast); }
.pl:active { transform: scale(.985); }
@media (hover: hover) { .pl:hover { background: #222; } }
.pl img, .pl .ph { width: 56px; height: 56px; border-radius: 10px; object-fit: cover; flex: none; background: var(--card2); }
.pl div { min-width: 0; display: flex; flex-direction: column; }
.pl strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl span { color: var(--muted); font-size: 14px; }
.pl .tag { color: var(--green); }
/* Playlist row: the box is the row, so the reset icon sits inside it on the right */
.row { position: relative; display: flex; align-items: center; background: var(--card); border: 1px solid var(--line); border-radius: 18px; overflow: hidden; }
.row .pl { flex: 1; min-width: 0; background: none; border: 0; }
.row .reset { width: 40px; height: 40px; margin-right: 10px; background: none; color: var(--muted); }
.row .reset svg { width: 18px; height: 18px; }
.row .reset:active { color: var(--text); }
/* How far through a started playlist you are: thin bar along the bottom of its row */
.row .prog { position: absolute; left: 0; right: 0; bottom: 0; height: 3px; background: rgba(255,255,255,.06); pointer-events: none; }
.row .prog::after { content: ''; position: absolute; inset: 0; background: var(--green); transform-origin: left; transform: scaleX(var(--p, 0)); }
/* Rows arrive one after another, quickly (capped so long lists don't drag) */
.list.enter > * { animation: rise 320ms var(--ease-out) backwards; animation-delay: calc(min(var(--i, 0), 12) * 28ms); }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } }

/* Swipe */
.swipe { flex: 1; display: flex; flex-direction: column; gap: 14px; min-height: 0; }
.bar { height: 4px; background: var(--card2); border-radius: 4px; overflow: hidden; flex: none; }
.bar div { height: 100%; background: var(--green); transform-origin: left; transform: scaleX(0); transition: transform 360ms var(--ease-out); }
.deck { position: relative; flex: 1; min-height: 300px; }
.card {
  position: absolute; inset: 0;
  border-radius: var(--radius); overflow: hidden;
  background: var(--card); border: 1px solid var(--line);
  box-shadow: 0 18px 40px rgba(0,0,0,.45);
  touch-action: none; user-select: none; -webkit-user-select: none;
  transform-origin: 50% 110%; /* tilt pivots low, like holding a card at the bottom */
  transition: transform 320ms var(--ease-expo);
  --keep: 0; --cut: 0;
}
.card.top { cursor: grab; }
.card.top:active { cursor: grabbing; }
.card.under { transform: scale(.94) translateY(14px); opacity: .6; transition: transform var(--med) var(--ease-out), opacity var(--med) var(--ease-out); }
/* While the top card flies off, the one underneath rises into its place */
.card.under.rising { transform: none; opacity: 1; }
.card.under.appear { animation: appear 260ms var(--ease-out) backwards; }
@keyframes appear { from { opacity: 0; transform: scale(.9) translateY(24px); } }
/* Undo: the card comes back from the side it left */
.card.top.from-right { animation: back-right 380ms var(--ease-expo); }
.card.top.from-left { animation: back-left 380ms var(--ease-expo); }
@keyframes back-right { from { transform: translateX(120%) rotate(18deg); } }
@keyframes back-left { from { transform: translateX(-120%) rotate(-18deg); } }
.card img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
.card img.bg { object-fit: cover; filter: blur(40px) brightness(.55); transform: scale(1.3); }
.card img.art { object-fit: contain; object-position: center top; }
.card .meta {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 90px 20px 20px;
  background: linear-gradient(to top, rgba(0,0,0,.92) 0%, rgba(0,0,0,.6) 55%, transparent 100%);
}
.card h2 {
  font-size: 30px; font-weight: 800; letter-spacing: -.025em; line-height: 1.1; text-wrap: balance;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.card p { color: #d0d0d0; margin-top: 6px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* KEEP / CUT stamps grow in and settle as you drag */
.stamp {
  position: absolute; top: 28px; padding: 6px 14px; border: 4px solid; border-radius: 12px;
  font-size: 30px; font-weight: 900; letter-spacing: .06em; background: rgba(0,0,0,.35);
  pointer-events: none;
}
.stamp.keep { left: 22px; color: var(--green); opacity: var(--keep); transform: rotate(-14deg) scale(calc(1.5 - .5 * var(--keep))); }
.stamp.cut { right: 22px; color: var(--red); opacity: var(--cut); transform: rotate(14deg) scale(calc(1.5 - .5 * var(--cut))); }

.controls { display: flex; align-items: center; justify-content: center; gap: 14px; flex: none; padding-bottom: env(safe-area-inset-bottom); }
.ctl { position: relative; border-radius: 50%; display: grid; place-items: center; flex: none; background: var(--card); border: 1px solid var(--line); transition: transform var(--fast) var(--ease-out), background-color var(--fast), opacity var(--fast); }
.ctl svg { width: 45%; height: 45%; }
.ctl.small { width: 44px; height: 44px; color: var(--muted); }
.ctl.big { width: 56px; height: 56px; border-width: 2px; }
.ctl.play { width: 62px; height: 62px; background: #fff; border-color: #fff; color: #000; }
.ctl.cut { color: var(--red); border-color: var(--red); background: var(--red-bg); }
.ctl.keep { color: var(--green); border-color: var(--green); background: var(--green-bg); }
.ctl:active:not(:disabled) { transform: scale(.9); }
@media (hover: hover) { .ctl:not(:disabled):hover { transform: scale(1.05); } }
/* Keep: the heart pops. Cut: the X gives a quick shake. */
.ctl.keep.pop svg { animation: pop 260ms var(--ease-out); }
@keyframes pop { 40% { transform: scale(1.35); } }
.ctl.cut.shake svg { animation: shake 260ms var(--ease-out); }
@keyframes shake { 20% { transform: translateX(-4px) rotate(-8deg); } 50% { transform: translateX(4px) rotate(8deg); } 80% { transform: translateX(-2px); } }
/* Play and pause morph into each other */
.ctl.play svg { position: absolute; transition: opacity 180ms var(--ease-out), transform 220ms var(--ease-out); }
.ctl.play .i-pause { opacity: 0; transform: scale(.5) rotate(-90deg); }
.ctl.play.is-playing .i-play { opacity: 0; transform: scale(.5) rotate(90deg); }
.ctl.play.is-playing .i-pause { opacity: 1; transform: none; }

/* Done screen */
.done .logo { position: relative; animation: land 420ms var(--ease-expo) backwards; }
@keyframes land { from { transform: scale(.4) rotate(-12deg); opacity: 0; } }
.done h2 { animation: rise 320ms var(--ease-out) 80ms backwards; }
.done .stat { font-size: 17px; color: var(--text); animation: rise 320ms var(--ease-out) 140ms backwards; }
.done .stat b { font-variant-numeric: tabular-nums; color: var(--green); }
.done .actions { width: 100%; display: grid; gap: 10px; margin-top: 6px; animation: rise 320ms var(--ease-out) 200ms backwards; }
.done .pair { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.done .pair .btn { min-height: 46px; padding: 0 12px; }
/* Short burst of confetti dots from the heart */
.burst {
  position: absolute; left: 50%; top: 50%; width: var(--s, 10px); height: var(--s, 10px); margin: calc(var(--s, 10px) / -2);
  border-radius: 3px; pointer-events: none;
  animation: burst 900ms var(--ease-expo) forwards, burst-fade 900ms linear forwards;
}
@keyframes burst {
  from { transform: translate(0, 0) rotate(0) scale(1); }
  to { transform: translate(var(--x), var(--y)) rotate(var(--r, 180deg)) scale(.7); }
}
@keyframes burst-fade { 0%, 55% { opacity: 1; } 100% { opacity: 0; } }

/* Pop-up box */
.sheet-wrap {
  position: fixed; inset: 0; z-index: var(--z-modal); background: rgba(0,0,0,.65);
  display: flex; align-items: center; justify-content: center;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  animation: fade 180ms var(--ease-out);
}
.sheet {
  width: 100%; max-width: 420px; max-height: 100%;
  display: flex; flex-direction: column; gap: 12px;
  background: var(--head); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 20px 16px 16px;
  box-shadow: 0 24px 60px rgba(0,0,0,.6);
  animation: box-in 240ms var(--ease-out);
}
@keyframes fade { from { opacity: 0; } }
@keyframes box-in { from { opacity: 0; transform: translateY(10px) scale(.96); } }
.sheet-wrap.out { animation: fade-out 150ms var(--ease-in) forwards; pointer-events: none; }
.sheet-wrap.out .sheet { animation: box-out 150ms var(--ease-in) forwards; }
@keyframes fade-out { to { opacity: 0; } }
@keyframes box-out { to { opacity: 0; transform: translateY(6px) scale(.97); } }
.sheet h3 { font-size: 20px; font-weight: 800; letter-spacing: -.02em; line-height: 1.2; overflow-wrap: anywhere; text-wrap: balance; }
.field {
  width: 100%; min-height: 52px; padding: 0 16px; flex: none;
  border-radius: 14px; background: var(--card); border: 1px solid var(--line);
  color: var(--text); font: inherit; font-size: 16px;
  transition: border-color var(--fast);
}
.field:focus { outline: 2px solid var(--green); outline-offset: -1px; }
.sheet .list { overflow-y: auto; min-height: 0; }
.sheet .btn { flex: none; }

/* Debug box */
.debug-box { overflow-y: auto; }
.debug-box section { flex: none; }
.debug-box h4 { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin-bottom: 6px; }
.dbg-row { padding: 8px 10px; margin-bottom: 6px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; color: var(--text); font-size: 14px; }
.dbg-row span { color: var(--muted); }
.dbg-row code { display: block; margin-top: 2px; font-size: 11px; color: #777; word-break: break-all; }
#dbgLog {
  max-height: 180px; overflow: auto; margin: 0; padding: 8px;
  background: #0b0b0b; border: 1px solid var(--line); border-radius: 10px;
  color: #9fe7b4; font: 11px/1.35 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap;
}
.dbg-btns { display: flex; gap: 8px; flex: none; }
.dbg-btns .btn { flex: 1; width: auto; padding: 0 12px; }
.debug-box .btn { min-height: 46px; }
.toggle { display: flex; align-items: center; gap: 10px; font-size: 14px; flex: none; cursor: pointer; }
.toggle input { width: 20px; height: 20px; accent-color: var(--green); }
.ok { color: var(--green); }

/* On-screen log overlay, pinned under the header so it doesn't cover the buttons */
#debug {
  position: fixed; left: 8px; right: 8px; top: calc(78px + env(safe-area-inset-top)); z-index: var(--z-overlay); /* under pop-ups */
  max-height: 32dvh; overflow: hidden; margin: 0; padding: 8px;
  background: rgba(0,0,0,.82); border: 1px solid #333; border-radius: 10px;
  color: #9fe7b4; font: 11px/1.35 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap;
  pointer-events: none;
}

/* Toast: slides up from the bottom */
.toast {
  position: fixed; left: 50%; z-index: var(--z-toast);
  bottom: calc(24px + env(safe-area-inset-bottom));
  width: max-content; max-width: calc(100% - 32px);
  background: #2e2e2e; border: 1px solid #3a3a3a; border-radius: 14px;
  padding: 12px 16px; font-size: 14px;
  box-shadow: 0 10px 30px rgba(0,0,0,.5);
  transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  transition: transform var(--med) var(--ease-out), opacity 180ms var(--ease-out);
}
.toast.show { transform: translate(-50%, 0); opacity: 1; }

/* Reduced motion: no movement, just quick fades */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 1ms !important;
    animation-delay: 0ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }
  .toast { transform: translate(-50%, 0); transition: opacity 120ms !important; }
  .burst { display: none; }
}
