# Now playing

A music player for a fictional album: cover art drawn from tokens, a seek bar that advances while the track plays, shuffle and repeat toggles, volume, and a queue you can jump into. The controls are `Button`s with `aria-pressed` for the toggles; the rest is app-owned markup.

**Example: Low Tide Signals** (`examples/recipes/now-playing.tsx`)

```tsx
'use client'

import { Button, Heading, Stack, Text } from '@blocks/design-system'
import { Pause, Play, Repeat, Shuffle, SkipBack, SkipForward, Volume2 } from 'lucide-react'
import { type CSSProperties, useEffect, useId, useState } from 'react'
import styles from './now-playing.module.css'

interface Track {
  title: string
  artist: string
  seconds: number
  /** The cover's two token colors: generated art, no image files. */
  cover: [string, string]
}

const album = 'Low Tide Signals'
const tracks: [Track, ...Track[]] = [
  { title: 'Harbor Lights', artist: 'The Quiet Cartographers', seconds: 214, cover: ['--ds-color-accent', '--ds-color-warning'] },
  { title: 'Paper Lanterns', artist: 'The Quiet Cartographers', seconds: 187, cover: ['--ds-color-warning', '--ds-color-danger'] },
  { title: 'Salt and Static', artist: 'The Quiet Cartographers', seconds: 243, cover: ['--ds-color-success', '--ds-color-info'] },
  { title: 'Northbound', artist: 'The Quiet Cartographers', seconds: 199, cover: ['--ds-color-info', '--ds-color-accent'] },
  { title: 'Fog Horn Lullaby', artist: 'The Quiet Cartographers', seconds: 262, cover: ['--ds-color-danger', '--ds-color-accent'] },
]

const clock = (seconds: number) => `${Math.floor(seconds / 60)}:${String(Math.floor(seconds % 60)).padStart(2, '0')}`

function Cover({ track, size }: { track: Track; size: 'lg' | 'sm' }) {
  const style = { '--from': `var(${track.cover[0]})`, '--to': `var(${track.cover[1]})` } as CSSProperties
  return <span className={size === 'lg' ? styles.cover : styles.thumb} style={style} aria-hidden />
}

export default function NowPlaying() {
  const [current, setCurrent] = useState(0)
  const [elapsed, setElapsed] = useState(62)
  const [playing, setPlaying] = useState(false)
  const [shuffle, setShuffle] = useState(false)
  const [repeat, setRepeat] = useState(false)
  const [volume, setVolume] = useState(70)
  const id = useId()
  const track = tracks[current] ?? tracks[0]

  function skip(by: number) {
    setCurrent((index) => (index + by + tracks.length) % tracks.length)
    setElapsed(0)
  }

  // Advance one second at a time while playing; at the end, repeat or go on.
  useEffect(() => {
    if (!playing) return
    const timer = setInterval(() => {
      setElapsed((seconds) => {
        if (seconds + 1 < track.seconds) return seconds + 1
        if (!repeat) setCurrent((index) => (index + 1) % tracks.length)
        return 0
      })
    }, 1000)
    return () => clearInterval(timer)
  }, [playing, repeat, track.seconds])

  const queue = tracks.map((each, index) => ({ each, index })).filter(({ index }) => index !== current)

  return (
    <section className={styles.player} aria-labelledby={`${id}-title`}>
      <div className={styles.now}>
        <Cover track={track} size="lg" />
        <Stack gap="md" dangerouslySetClassName={styles.controls}>
          <Stack gap="xxs">
            <Stack direction="row" gap="md" align="center">
              {/* Bars that dance while the track plays: decoration only. */}
              <span className={playing ? `${styles.bars} ${styles.dancing}` : styles.bars} aria-hidden>
                <span />
                <span />
                <span />
                <span />
              </span>
              <Text fontSize="xs" color="text-muted" dangerouslySetClassName={styles.eyebrow}>
                {playing ? 'Now playing' : 'Paused'} · {album}
              </Text>
            </Stack>
            <Heading as="h3" fontSize="xl" id={`${id}-title`} margin="none">
              {track.title}
            </Heading>
            <Text color="text-muted">{track.artist}</Text>
          </Stack>

          <div className={styles.scrubber}>
            <input
              type="range"
              className={styles.range}
              min={0}
              max={track.seconds}
              value={elapsed}
              onChange={(event) => setElapsed(Number(event.target.value))}
              aria-label="Seek"
              aria-valuetext={`${clock(elapsed)} of ${clock(track.seconds)}`}
              style={{ '--fill': `${(elapsed / track.seconds) * 100}%` } as CSSProperties}
            />
            <Stack direction="row" justify="space-between">
              <Text fontSize="xs" color="text-muted" dangerouslySetClassName={styles.time}>
                {clock(elapsed)}
              </Text>
              <Text fontSize="xs" color="text-muted" dangerouslySetClassName={styles.time}>
                −{clock(track.seconds - elapsed)}
              </Text>
            </Stack>
          </div>

          <Stack direction="row" align="center" justify="space-between" gap="xs">
            <Button variant="ghost" aria-label="Shuffle" aria-pressed={shuffle} onClick={() => setShuffle(!shuffle)} dangerouslySetClassName={styles.transport}>
              <Shuffle aria-hidden size={18} />
            </Button>
            <Button variant="ghost" aria-label="Previous track" onClick={() => skip(-1)} dangerouslySetClassName={styles.transport}>
              <SkipBack aria-hidden size={20} />
            </Button>
            <Button variant="primary" borderRadius="circle" aria-label={playing ? 'Pause' : 'Play'} onClick={() => setPlaying(!playing)} dangerouslySetClassName={styles.play}>
              {playing ? <Pause aria-hidden size={22} /> : <Play aria-hidden size={22} />}
            </Button>
            <Button variant="ghost" aria-label="Next track" onClick={() => skip(1)} dangerouslySetClassName={styles.transport}>
              <SkipForward aria-hidden size={20} />
            </Button>
            <Button variant="ghost" aria-label="Repeat track" aria-pressed={repeat} onClick={() => setRepeat(!repeat)} dangerouslySetClassName={styles.transport}>
              <Repeat aria-hidden size={18} />
            </Button>
          </Stack>

          <label className={styles.volume}>
            <Volume2 aria-hidden size={16} />
            <span className={styles.visuallyHidden}>Volume</span>
            <input
              type="range"
              className={styles.range}
              min={0}
              max={100}
              value={volume}
              onChange={(event) => setVolume(Number(event.target.value))}
              aria-valuetext={`${volume}%`}
              style={{ '--fill': `${volume}%` } as CSSProperties}
            />
          </label>
        </Stack>
      </div>

      <div className={styles.queue}>
        <Heading as="h4" fontSize="sm" margin="none" id={`${id}-queue`}>
          Up next
        </Heading>
        <ol aria-labelledby={`${id}-queue`} className={styles.list}>
          {queue.map(({ each, index }) => (
            <li key={each.title}>
              <button
                type="button"
                className={styles.item}
                onClick={() => {
                  setCurrent(index)
                  setElapsed(0)
                  setPlaying(true)
                }}
              >
                <Cover track={each} size="sm" />
                <span className={styles.itemText}>
                  <span className={styles.itemTitle}>{each.title}</span>
                  <span className={styles.itemMeta}>{clock(each.seconds)}</span>
                </span>
                <span className={styles.visuallyHidden}>, play now</span>
              </button>
            </li>
          ))}
        </ol>
      </div>

      <span className={styles.visuallyHidden} role="status">
        {playing ? `Playing ${track.title}` : `Paused`}
      </span>
    </section>
  )
}
```

## How it works

- Playback is one interval, started and cleared by an effect while `playing` is true; at the end of a track it repeats or moves on.
- The seek and volume bars are native range inputs, so they work with arrow keys and screen readers. `aria-valuetext` reads "1:02 of 3:34" rather than a bare number.
- Every cover is the same CSS drawing with two color tokens in custom properties, so the art follows light and dark themes with no images.
- A visually hidden status says "Playing Harbor Lights" or "Paused" when playback changes. The dancing bars are decoration and hold still under `prefers-reduced-motion`.

## App-owned CSS

`examples/recipes/now-playing.module.css`:

```css
/* A music player: generated cover art, token-colored range inputs and a queue. */

.player {
  container-type: inline-size;
  display: grid;
  gap: var(--ds-space-lg);
  max-width: 36rem;
  padding: clamp(var(--ds-space-md), 4cqi, var(--ds-space-lg));
  border: 1px solid var(--ds-color-border-subtle);
  border-radius: var(--ds-border-radius-xxl);
  background: linear-gradient(160deg, var(--ds-color-surface), var(--ds-color-bg));
  box-shadow: var(--ds-shadow-md);
}

.now {
  display: grid;
  grid-template-columns: minmax(0, 13rem) minmax(0, 1fr);
  gap: var(--ds-space-lg);
  align-items: center;
}

/* Cover art from two tokens: a soft sunset of circles over a horizon. */
.cover,
.thumb {
  display: block;
  aspect-ratio: 1;
  border-radius: var(--ds-border-radius-xl);
  background:
    /* The sun stays light in both themes: a fixed step of the slate scale. */
    radial-gradient(circle at 70% 32%, color-mix(in srgb, var(--ds-slate-1) 90%, transparent) 0 11%, transparent 11.5%),
    radial-gradient(circle at 70% 32%, color-mix(in srgb, var(--ds-slate-1) 30%, transparent) 0 19%, transparent 19.5%),
    linear-gradient(to bottom, transparent 62%, color-mix(in srgb, var(--to) 70%, var(--ds-color-text)) 62% 66%, transparent 66%),
    linear-gradient(155deg, var(--from), var(--to));
  box-shadow: var(--ds-shadow-md);
}

.thumb {
  width: 2.5rem;
  flex: none;
  border-radius: var(--ds-border-radius-sm);
  box-shadow: none;
}

.controls {
  min-width: 0;
}

.eyebrow {
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.time {
  font-variant-numeric: tabular-nums;
}

.scrubber {
  display: grid;
  gap: var(--ds-space-xxs);
}

/* A range input drawn from tokens: filled track to the thumb. */
.range {
  --fill: 0%;
  appearance: none;
  width: 100%;
  height: 1rem;
  margin: 0;
  background: transparent;
  cursor: pointer;
}

.range::-webkit-slider-runnable-track {
  height: 4px;
  border-radius: var(--ds-border-radius-pill);
  background: linear-gradient(to right, var(--ds-color-accent) var(--fill), var(--ds-color-border) var(--fill));
}

.range::-moz-range-track {
  height: 4px;
  border-radius: var(--ds-border-radius-pill);
  background: linear-gradient(to right, var(--ds-color-accent) var(--fill), var(--ds-color-border) var(--fill));
}

.range::-webkit-slider-thumb {
  appearance: none;
  width: 14px;
  height: 14px;
  margin-top: -5px;
  border-radius: 50%;
  background: var(--ds-color-text);
  box-shadow: var(--ds-shadow-sm);
}

.range::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border: 0;
  border-radius: 50%;
  background: var(--ds-color-text);
}

.range:focus-visible {
  outline: 2px solid var(--ds-color-focus-ring);
  outline-offset: 4px;
  border-radius: var(--ds-border-radius-sm);
}

/* Borderless icon buttons of one size around the filled play button. */
.transport {
  width: 2.5rem;
  height: 2.5rem;
  padding: 0;
  justify-content: center;
  border-color: transparent;
  color: var(--ds-color-text-muted);
}

.transport:hover {
  color: var(--ds-color-text);
}

.transport[aria-pressed='true'] {
  color: var(--ds-color-accent-text);
  background: var(--ds-color-accent-subtle);
}

.play {
  width: 3.25rem;
  height: 3.25rem;
  padding: 0;
  justify-content: center;
}

.volume {
  display: flex;
  align-items: center;
  gap: var(--ds-space-sm);
  color: var(--ds-color-text-muted);
}

.queue {
  display: grid;
  gap: var(--ds-space-xs);
  padding-top: var(--ds-space-md);
  border-top: 1px solid var(--ds-color-border-subtle);
}

.list {
  display: grid;
  gap: 2px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.item {
  display: flex;
  align-items: center;
  gap: var(--ds-space-md);
  width: 100%;
  padding: var(--ds-space-xs);
  border: 0;
  border-radius: var(--ds-border-radius-md);
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: start;
  cursor: pointer;
}

.item:hover {
  background: var(--ds-color-surface-hover);
}

.item:focus-visible {
  outline: 2px solid var(--ds-color-focus-ring);
  outline-offset: -2px;
}

.itemText {
  display: flex;
  flex: 1;
  justify-content: space-between;
  gap: var(--ds-space-sm);
  min-width: 0;
}

.itemTitle {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: var(--ds-font-weight-medium);
}

.itemMeta {
  color: var(--ds-color-text-muted);
  font-size: var(--ds-font-size-sm);
  font-variant-numeric: tabular-nums;
}

/* The equalizer beside the eyebrow. */
.bars {
  display: flex;
  flex: none;
  align-items: flex-end;
  gap: 2px;
  height: 12px;
}

.bars span {
  width: 2px;
  height: 30%;
  border-radius: 2px;
  background: var(--ds-color-accent);
  transition: height 200ms;
}

.dancing span {
  animation: dance 900ms ease-in-out infinite alternate;
}

.dancing span:nth-child(2) {
  animation-delay: -300ms;
}

.dancing span:nth-child(3) {
  animation-delay: -600ms;
}

.dancing span:nth-child(4) {
  animation-delay: -150ms;
}

@keyframes dance {
  from {
    height: 20%;
  }
  to {
    height: 100%;
  }
}

@media (prefers-reduced-motion: reduce) {
  .dancing span {
    animation: none;
    height: 70%;
  }
}

.visuallyHidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* A phone: the cover sits above the controls. */
@container (max-width: 30rem) {
  .now {
    grid-template-columns: minmax(0, 1fr);
  }

  .cover {
    width: min(100%, 15rem);
    justify-self: center;
  }
}
```
