/** seconds → m:ss.d */
export function mmss(t: number, decimals = 1): string {
  if (!isFinite(t)) return '0:00'
  const m = Math.floor(t / 60)
  const s = t - m * 60
  // "SS.d" is 3 chars + the decimals, not 4 — the old `4 + decimals` padded a
  // spurious zero onto every sub-10s time ("0:002.2"). Caught by the set-judge
  // smoke test reading the transport clock back.
  return `${m}:${s.toFixed(decimals).padStart(decimals ? 3 + decimals : 2, '0')}`
}

/** dB value → 0..1 across a loudness window (default -40..-3 LUFS) */
export function loudFrac(db: number, lo = -40, hi = -3): number {
  if (!isFinite(db)) return 0
  return Math.max(0, Math.min(1, (db - lo) / (hi - lo)))
}

export function fmtDb(v: number | null | undefined, unit = ''): string {
  return v == null ? '—' : `${v > 0 ? '+' : ''}${v.toFixed(1)}${unit}`
}
