/* UMPAD — design system
   Tokens follow shadcn/ui's naming and neutral scale (background/foreground/
   muted/border/ring/primary, --radius), ported to vanilla CSS because this
   project has no build step. Pure greyscale: every hue is 0, saturation 0.
   Motion and material rules come from the apple-design skill (§ refs inline). */

:root {
  color-scheme: light dark;

  /* shadcn neutral — light */
  --background: 0 0% 100%;
  --foreground: 0 0% 3.9%;
  --card: 0 0% 100%;
  --muted: 0 0% 96.1%;
  --muted-foreground: 0 0% 45.1%;
  --subtle-foreground: 0 0% 63.9%;
  --border: 0 0% 89.8%;
  --input: 0 0% 89.8%;
  --ring: 0 0% 3.9%;
  --primary: 0 0% 9%;
  --primary-foreground: 0 0% 98%;
  --destructive: 0 72% 42%;

  --radius: 0.625rem;
  --radius-sm: calc(var(--radius) - 4px);
  --radius-lg: calc(var(--radius) + 6px);
  --radius-xl: calc(var(--radius) + 12px);

  /* Motion — critically damped by default (apple-design §4) */
  --spring: cubic-bezier(.32, .72, 0, 1);
  --fast: 140ms;
  --base: 260ms;
  --slow: 420ms;

  --shadow-xs: 0 1px 2px hsl(var(--foreground) / .06);
  --shadow-sm: 0 1px 3px hsl(var(--foreground) / .08), 0 1px 2px hsl(var(--foreground) / .04);
  --shadow-md: 0 4px 16px hsl(var(--foreground) / .07), 0 1px 4px hsl(var(--foreground) / .04);
  --shadow-lg: 0 16px 48px hsl(var(--foreground) / .12), 0 4px 12px hsl(var(--foreground) / .06);

  --dock-h: 132px;
  --tile-opacity: .82;

  /* Canvas cannot read hsl(var(--x)) itself, so the waveform gets fully
     resolved colours it can hand straight to fillStyle. */
  --wave-played: hsl(var(--foreground));
  --wave-idle: hsl(var(--foreground) / .2);
}

@media (prefers-color-scheme: dark) {
  :root {
    --background: 0 0% 3.9%;
    --foreground: 0 0% 98%;
    --card: 0 0% 6.5%;
    --muted: 0 0% 14.9%;
    --muted-foreground: 0 0% 63.9%;
    --subtle-foreground: 0 0% 45.1%;
    --border: 0 0% 14.9%;
    --input: 0 0% 18%;
    --ring: 0 0% 83.1%;
    --primary: 0 0% 98%;
    --primary-foreground: 0 0% 9%;
    --destructive: 0 70% 62%;

    --shadow-xs: 0 1px 2px hsl(0 0% 0% / .5);
    --shadow-sm: 0 1px 3px hsl(0 0% 0% / .6);
    --shadow-md: 0 4px 18px hsl(0 0% 0% / .65);
    --shadow-lg: 0 18px 50px hsl(0 0% 0% / .75);

    --tile-opacity: .78;
  }
}

/* Manual toggle wins over the system preference. */
:root[data-theme="light"] {
  --background: 0 0% 100%; --foreground: 0 0% 3.9%; --card: 0 0% 100%;
  --muted: 0 0% 96.1%; --muted-foreground: 0 0% 45.1%; --subtle-foreground: 0 0% 63.9%;
  --border: 0 0% 89.8%; --input: 0 0% 89.8%; --ring: 0 0% 3.9%;
  --primary: 0 0% 9%; --primary-foreground: 0 0% 98%; --destructive: 0 72% 42%;
  --tile-opacity: .82;
}
:root[data-theme="dark"] {
  --background: 0 0% 3.9%; --foreground: 0 0% 98%; --card: 0 0% 6.5%;
  --muted: 0 0% 14.9%; --muted-foreground: 0 0% 63.9%; --subtle-foreground: 0 0% 45.1%;
  --border: 0 0% 14.9%; --input: 0 0% 18%; --ring: 0 0% 83.1%;
  --primary: 0 0% 98%; --primary-foreground: 0 0% 9%; --destructive: 0 70% 62%;
  --tile-opacity: .78;
}

*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-optical-sizing: auto;
  font-size: 15px;
  line-height: 1.5;
  color: hsl(var(--foreground));
  background: hsl(var(--background));
  min-height: 100vh;
  padding-bottom: calc(var(--dock-h) + env(safe-area-inset-bottom));
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, select { font: inherit; color: inherit; }
button { background: none; border: 0; cursor: pointer; }

:where(a, button, input, select, [tabindex]):focus-visible {
  outline: 2px solid hsl(var(--ring));
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ── Backdrop: scattered platform tiles ────────────────────────
   Decorative only. Masked away behind the content column so the
   interface stays the thing you look at. */
.backdrop {
  position: fixed; inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
  /* Sized in px against the 720px content column, not in %, so the clear
     zone tracks the interface rather than the viewport. */
  -webkit-mask-image: radial-gradient(ellipse 660px 540px at 50% 40%, transparent 0%, transparent 62%, #000 100%);
  mask-image: radial-gradient(ellipse 660px 540px at 50% 40%, transparent 0%, transparent 62%, #000 100%);
}

/* Two layers on purpose: the outer element owns the drift animation, the
   inner one owns the tilt. A single element cannot do both — the animation's
   transform would overwrite the rotation. */
.tile {
  position: absolute;
  animation: tile-drift var(--dur, 26s) ease-in-out var(--delay, 0s) infinite;
  will-change: transform;
}
.tile-box {
  width: 100%; height: 100%;
  display: grid; place-items: center;
  border-radius: 23%;
  background: var(--tile-color);
  transform: rotate(var(--rot, 0deg));
  opacity: var(--tile-opacity);
  box-shadow: 0 14px 36px hsl(0 0% 0% / .28);
}
.tile-box svg {
  width: 56%; height: 56%;
  color: var(--tile-ink, #fff);
}

@keyframes tile-drift {
  0%, 100% { transform: translate3d(0, 0, 0); }
  50%      { transform: translate3d(0, var(--drift, -14px), 0); }
}

.chrome, .page, .dock { position: relative; z-index: 1; }

/* ── Chrome ────────────────────────────────────────────────────
   Translucent layer; content scrolls under it (apple-design §12). */
.chrome {
  position: sticky; top: 0; z-index: 60;
  background: hsl(var(--background) / .72);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-bottom: 1px solid hsl(var(--border));
}
.chrome-inner {
  max-width: 720px; margin: 0 auto;
  padding: 11px max(16px, env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}

.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; color: inherit; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 7px; flex-shrink: 0;
  color: hsl(var(--foreground));
  --mark-ink: hsl(var(--background));
}
.brand-name {
  font-size: 15px; font-weight: 620;
  letter-spacing: .02em;            /* small caps-ish wordmark reads tighter set wide */
}

.chrome-actions { display: flex; align-items: center; gap: 2px; }

.icon-btn {
  width: 34px; height: 34px; border-radius: var(--radius-sm);
  display: grid; place-items: center;
  color: hsl(var(--muted-foreground));
  transition: background var(--fast) ease, color var(--fast) ease, transform var(--fast) var(--spring);
}
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:hover { background: hsl(var(--muted)); color: hsl(var(--foreground)); }
.icon-btn:active { transform: scale(.93); }   /* feedback on press (§1) */

/* ── Menu ──────────────────────────────────────────────────────── */
.menu-wrap { position: relative; }
.menu {
  position: absolute; top: calc(100% + 8px); right: 0;
  width: 278px; padding: 6px;
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 70;

  transform-origin: top right;      /* anchored to its trigger (§7) */
  opacity: 0; transform: scale(.95) translateY(-6px);
  visibility: hidden;
  transition: opacity var(--fast) ease, transform var(--base) var(--spring), visibility var(--base);
}
.menu.open { opacity: 1; transform: none; visibility: visible; }

.menu-row {
  display: flex; align-items: center; gap: 10px;
  padding: 7px 8px; border-radius: var(--radius-sm);
  font-size: 13px; color: hsl(var(--muted-foreground));
}
.menu-row label { flex: 1; }
.menu-row select {
  background: hsl(var(--background));
  border: 1px solid hsl(var(--input));
  border-radius: var(--radius-sm);
  padding: 5px 8px; font-size: 13px; cursor: pointer;
  color: hsl(var(--foreground));
}
.menu-value {
  font-variant-numeric: tabular-nums; width: 38px; text-align: right;
  color: hsl(var(--subtle-foreground));
}
.menu-note {
  padding: 6px 8px 7px; margin-top: 2px;
  border-top: 1px solid hsl(var(--border));
  font-size: 11.5px; line-height: 1.45; color: hsl(var(--subtle-foreground));
}

input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 92px; height: 20px; background: none; cursor: pointer;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 999px; background: hsl(var(--input));
}
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 13px; height: 13px; margin-top: -4.5px;
  border-radius: 50%;
  background: hsl(var(--foreground));
  box-shadow: var(--shadow-xs);
}

/* ── Page ──────────────────────────────────────────────────────── */
.page {
  max-width: 720px; margin: 0 auto;
  padding: 44px max(16px, env(safe-area-inset-left)) 32px;
  display: flex; flex-direction: column; gap: 22px;
}

.hero { text-align: center; }
.hero-title {
  font-size: clamp(26px, 4.6vw, 40px);
  line-height: 1.06;                /* tight leading on display type (§15) */
  letter-spacing: -.032em;          /* negative tracking as it grows */
  font-weight: 660;
  max-width: 16ch;
  margin: 0 auto;
  text-wrap: balance;
}
.hero-abbr { color: hsl(var(--muted-foreground)); font-weight: 560; }
.hero-sub {
  margin: 14px auto 0; max-width: 46ch;
  font-size: 14.5px; line-height: 1.55;
  letter-spacing: 0;                /* body sits near zero */
  color: hsl(var(--muted-foreground));
}

/* ── Composer ──────────────────────────────────────────────────── */
.composer { display: flex; flex-direction: column; gap: 10px; }

.field {
  display: flex; align-items: center; gap: 6px;
  padding: 5px;
  background: hsl(var(--card));
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  transition: border-color var(--base) ease, box-shadow var(--base) ease;
}
.field:focus-within {
  border-color: hsl(var(--ring));
  box-shadow: var(--shadow-md), 0 0 0 3px hsl(var(--ring) / .12);
}

.platform-select {
  flex-shrink: 0;
  background: hsl(var(--muted));
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 8px; font-size: 13px;
  color: hsl(var(--muted-foreground));
  cursor: pointer;
  transition: color var(--fast) ease;
}
.platform-select:hover { color: hsl(var(--foreground)); }

.url-input {
  flex: 1; min-width: 0;
  background: none; border: 0; outline: none;
  padding: 10px 6px; font-size: 15px;
}
.url-input::placeholder { color: hsl(var(--subtle-foreground)); }

.btn-primary {
  flex-shrink: 0;
  background: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
  font-size: 14px; font-weight: 570; letter-spacing: -.005em;
  padding: 10px 18px; border-radius: var(--radius-sm);
  box-shadow: var(--shadow-xs);
  transition: opacity var(--fast) ease, transform var(--fast) var(--spring);
}
.btn-primary:hover { opacity: .88; }
.btn-primary:active { transform: scale(.965); }
.btn-primary:disabled { opacity: .5; cursor: default; transform: none; }
.btn-primary.busy { animation: breathe 1.4s ease-in-out infinite; }
@keyframes breathe { 0%,100% { opacity: 1 } 50% { opacity: .55 } }

.dropzone {
  display: none;
  flex-direction: column; gap: 6px; align-items: center;
  padding: 22px 16px;
  border: 1px dashed hsl(var(--input));
  border-radius: var(--radius-lg);
  background: hsl(var(--muted) / .5);
  text-align: center;
}
.dropzone.show { display: flex; }
.dropzone label {
  display: inline-flex; align-items: center; gap: 8px;
  color: hsl(var(--foreground)); font-weight: 560; font-size: 14px; cursor: pointer;
}
.dropzone label svg { width: 16px; height: 16px; }
.dropzone input { display: none; }
.dropzone-hint { font-size: 12.5px; color: hsl(var(--subtle-foreground)); }

.status { min-height: 18px; font-size: 13px; color: hsl(var(--destructive)); text-align: center; }
.status.info { color: hsl(var(--muted-foreground)); }

/* ── Results ───────────────────────────────────────────────────── */
.results { display: none; flex-direction: column; gap: 6px; }
.results.show { display: flex; }
.results-head { padding: 0 2px 2px; }
.results-count {
  font-size: 11px; font-weight: 600; letter-spacing: .07em; text-transform: uppercase;
  color: hsl(var(--subtle-foreground));
}

.track-list {
  display: flex; flex-direction: column;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius);
  background: hsl(var(--card));
  overflow: hidden;
}

.track-row {
  position: relative;
  display: flex; align-items: center; gap: 11px;
  padding: 10px 12px;
  cursor: pointer;
  border-bottom: 1px solid hsl(var(--border));
  transition: background var(--fast) ease;
}
.track-row:last-child { border-bottom: 0; }
.track-row:hover { background: hsl(var(--muted) / .6); }
.track-row.active { background: hsl(var(--muted)); }

/* Playback position of the active row, fed by --progress from app.js. */
.track-row.active::after {
  content: ''; position: absolute; left: 0; bottom: 0;
  height: 2px; width: var(--progress, 0%);
  background: hsl(var(--foreground));
  transition: width .25s linear;
}

.track-thumb {
  width: 36px; height: 36px; border-radius: var(--radius-sm); object-fit: cover;
  background: hsl(var(--muted)); flex-shrink: 0;
  border: 1px solid hsl(var(--border));
}
.track-info { flex: 1; min-width: 0; }
.track-title {
  font-size: 13.5px; font-weight: 520; letter-spacing: -.006em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.track-artist {
  font-size: 12.5px; color: hsl(var(--muted-foreground)); margin-top: 1px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.track-dur {
  font-size: 12px; color: hsl(var(--subtle-foreground)); flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}

/* ── Empty state ───────────────────────────────────────────────── */
.empty { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 24px 0 8px; text-align: center; }
.empty.hide { display: none; }
.empty-mark {
  width: 36px; height: 36px; border-radius: 10px; margin-bottom: 8px; opacity: .18;
  color: hsl(var(--foreground)); --mark-ink: hsl(var(--background));
}
.empty-title { font-size: 14px; font-weight: 560; }
.empty-sub { font-size: 13px; color: hsl(var(--subtle-foreground)); }

/* ── Player dock ───────────────────────────────────────────────── */
.dock {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  background: hsl(var(--background) / .78);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid hsl(var(--border));
  padding-bottom: env(safe-area-inset-bottom);

  transform: translateY(calc(100% + 20px));
  transition: transform var(--slow) var(--spring);
}
.dock.show { transform: none; }

.dock-inner {
  max-width: 720px; margin: 0 auto;
  padding: 11px max(16px, env(safe-area-inset-left)) 13px;
  display: flex; flex-direction: column; gap: 9px;
}

.np { display: flex; align-items: center; gap: 11px; }
.np-art { position: relative; width: 40px; height: 40px; flex-shrink: 0; }
.np-art img, .np-art-fallback {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  border-radius: var(--radius-sm); object-fit: cover;
  border: 1px solid hsl(var(--border));
}
.np-art-fallback { color: hsl(var(--foreground)); --mark-ink: hsl(var(--background)); }
.np-art-fallback.hide { display: none; }

.np-meta { flex: 1; min-width: 0; }
.np-title {
  font-size: 14px; font-weight: 590; letter-spacing: -.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.np-artist {
  font-size: 12.5px; color: hsl(var(--muted-foreground)); margin-top: 1px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.btn-ghost {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 13px; border-radius: var(--radius-sm);
  background: hsl(var(--background));
  border: 1px solid hsl(var(--border));
  font-size: 13px; font-weight: 550;
  color: hsl(var(--foreground));
  transition: background var(--fast) ease, color var(--fast) ease, transform var(--fast) var(--spring);
}
.btn-ghost svg { width: 15px; height: 15px; }
.btn-ghost:hover { background: hsl(var(--primary)); color: hsl(var(--primary-foreground)); border-color: hsl(var(--primary)); }
.btn-ghost:active { transform: scale(.96); }

/* ── Transport + waveform ──────────────────────────────────────── */
.transport { display: flex; align-items: center; gap: 11px; }

.play-btn {
  width: 36px; height: 36px; flex-shrink: 0;
  border-radius: 50%;
  background: hsl(var(--primary)); color: hsl(var(--primary-foreground));
  display: grid; place-items: center;
  box-shadow: var(--shadow-xs);
  transition: transform var(--fast) var(--spring), opacity var(--fast) ease;
}
.play-btn svg { width: 16px; height: 16px; }
.play-btn:hover { opacity: .88; }
.play-btn:active { transform: scale(.9); }
.play-btn .i-pause { display: none; }
.play-btn.playing .i-play { display: none; }
.play-btn.playing .i-pause { display: block; }

.t-time {
  font-size: 11.5px; color: hsl(var(--subtle-foreground));
  font-variant-numeric: tabular-nums;
  width: 36px; flex-shrink: 0;
}
.t-time:last-child { text-align: right; }

.wave {
  position: relative; flex: 1; min-width: 0;
  height: 40px;
  cursor: pointer;
  touch-action: none;              /* we own the gesture (§2) */
}
.wave canvas { display: block; width: 100%; height: 100%; }

.wave-skeleton {
  position: absolute; inset: 0;
  border-radius: var(--radius-sm);
  background: linear-gradient(90deg, transparent, hsl(var(--muted)) 45%, transparent);
  background-size: 200% 100%;
  opacity: 0;
  transition: opacity var(--base) ease;
  pointer-events: none;
}
/* Only animate while visible — an infinite animation on a hidden element
   keeps the page awake for nothing. */
.wave-skeleton.show { opacity: 1; animation: shimmer 1.3s linear infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ── Narrow screens ────────────────────────────────────────────── */
@media (max-width: 560px) {
  :root { --dock-h: 150px; --tile-opacity: .7; }
  .page { padding-top: 30px; gap: 18px; }
  .hero-title { max-width: 14ch; }

  .field { flex-wrap: wrap; }
  .url-input { flex: 1 1 100%; order: -1; font-size: 16px; padding: 11px 10px; }
  .platform-select { flex: 1 1 auto; }
  .btn-primary { flex: 1 1 auto; }

  .np-art { width: 36px; height: 36px; }
  .dl-btn span { display: none; }
  .dl-btn { padding: 9px 11px; }
  .t-time { width: 32px; }
}

/* Below this the waveform is squeezed to nothing between the transport
   controls, so it gets a row of its own. */
@media (max-width: 400px) {
  :root { --dock-h: 168px; }
  .transport { flex-wrap: wrap; row-gap: 4px; }
  .wave { order: 3; flex-basis: 100%; height: 34px; }
  .t-time { flex: 1; }
  .t-time:last-of-type { text-align: right; }
}

/* ── Accessibility preferences (§14) ───────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
  .tile { animation: none; }
  .dock { transform: none; opacity: 0; visibility: hidden; }
  .dock.show { opacity: 1; visibility: visible; }
  .menu { transform: none; }
}

@media (prefers-reduced-transparency: reduce) {
  .chrome, .dock {
    background: hsl(var(--background));
    backdrop-filter: none; -webkit-backdrop-filter: none;
  }
  .backdrop { display: none; }
}

@media (prefers-contrast: more) {
  :root { --border: 0 0% 55%; --muted-foreground: 0 0% 25%; --subtle-foreground: 0 0% 32%; }
  .chrome, .dock { background: hsl(var(--background)); backdrop-filter: none; }
  .backdrop { display: none; }
}
