File size: 21,244 Bytes
3c56493
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
<script lang="ts">
	import { browser } from '$app/environment';
	import { onDestroy } from 'svelte';
	import { currentTime, playerStore } from '$lib/stores/player';
	import { lyricsStore } from '$lib/stores/lyrics';
	import { formatArtists } from '$lib/utils';
	import { isSonglinkTrack } from '$lib/types';
	import { Maximize2, Minimize2, RefreshCw, X } from 'lucide-svelte';

	const COMPONENT_MODULE_URL =
		'https://cdn.jsdelivr.net/npm/@uimaxbai/am-lyrics/dist/src/am-lyrics.min.js';
	const SEEK_FORCE_THRESHOLD_MS = 220;

	type LyricsMetadata = {
		title: string;
		artist: string;
		album?: string;
		query: string;
		durationMs?: number;
		isrc?: string;
	};

	type AmLyricsElement = HTMLElement & {
		currentTime: number;
		scrollToActiveLine?: () => void;
		scrollToInstrumental?: (index: number) => void;
		activeLineIndices?: number[];
		shadowRoot: ShadowRoot | null;
		updateComplete?: Promise<unknown>;
		__tidalScrollPatched?: boolean;
	};

	let amLyricsElement = $state<AmLyricsElement | null>(null);
	let scriptStatus = $state<'idle' | 'loading' | 'ready' | 'error'>('idle');
	let scriptError = $state<string | null>(null);
	let pendingLoad: Promise<void> | null = null;
	let hasEscapeListener = false;

	let baseTimeMs = $state(0);
	let lyricsKey = $state('0:none');
	let metadata = $state<LyricsMetadata | null>(null);
	let animationFrameId: number | null = null;
	let lastBaseTimestamp = 0;
	let scrollPatchFrame: number | null = null;
	let lastRefreshedTrackId = $state<number | string | null>(null);

	$effect(() => {
		const seconds = $currentTime ?? 0;
		const playing = $playerStore.isPlaying;
		const nextMs = Number.isFinite(seconds) ? Math.max(0, seconds * 1000) : 0;
		baseTimeMs = nextMs;
		if (browser) {
			lastBaseTimestamp = performance.now();
		}
		if (scriptStatus === 'ready' && amLyricsElement) {
			const current = Number(amLyricsElement.currentTime ?? 0);
			const delta = Math.abs(current - nextMs);
			if (!playing || delta > SEEK_FORCE_THRESHOLD_MS) {
				amLyricsElement.currentTime = nextMs;
			}
		}
	});

	$effect(() => {
		lyricsKey = `${$lyricsStore.refreshToken}:${$lyricsStore.track?.id ?? 'none'}`;
	});

	$effect(() => {
		if ($lyricsStore.open && browser) {
			void ensureComponentLoaded();
		}
		attachEscapeListener($lyricsStore.open);
	});

	onDestroy(() => {
		attachEscapeListener(false);
	});

	async function ensureComponentLoaded() {
		if (scriptStatus === 'ready') {
			return;
		}
		if (typeof customElements !== 'undefined' && customElements.get('am-lyrics')) {
			scriptStatus = 'ready';
			scriptError = null;
			return;
		}
		if (pendingLoad) {
			scriptStatus = 'loading';
			try {
				await pendingLoad;
			} catch {
				// handled when the original promise settles
			}
			return;
		}
		if (!browser) return;

		scriptStatus = 'loading';
		scriptError = null;

		pendingLoad = loadComponentScript()
			.then(() => {
				scriptStatus = 'ready';
				scriptError = null;
				if (amLyricsElement) {
					amLyricsElement.currentTime = baseTimeMs;
				}
			})
			.catch((error) => {
				console.error('Failed to load Apple Music lyrics component', error);
				scriptStatus = 'error';
				scriptError = error instanceof Error ? error.message : 'Unable to load lyrics component.';
			})
			.finally(() => {
				pendingLoad = null;
			});

		await pendingLoad;
	}

	function loadComponentScript(): Promise<void> {
		return new Promise((resolve, reject) => {
			if (!browser) {
				resolve();
				return;
			}

			const waitForDefinition = () => {
				if (typeof customElements !== 'undefined' && 'whenDefined' in customElements) {
					customElements
						.whenDefined('am-lyrics')
						.then(() => resolve())
						.catch(reject);
				} else {
					resolve();
				}
			};

			if (typeof customElements !== 'undefined' && customElements.get('am-lyrics')) {
				resolve();
				return;
			}

			const existing = document.querySelector<HTMLScriptElement>('script[data-am-lyrics]');
			if (existing) {
				if (existing.dataset.loaded === 'true') {
					waitForDefinition();
					return;
				}
				const handleLoad = () => {
					existing.dataset.loaded = 'true';
					waitForDefinition();
				};
				const handleError = () => {
					existing.removeEventListener('load', handleLoad);
					existing.removeEventListener('error', handleError);
					existing.remove();
					reject(new Error('Failed to load lyrics component.'));
				};
				existing.addEventListener('load', handleLoad, { once: true });
				existing.addEventListener('error', handleError, { once: true });
				return;
			}

			const script = document.createElement('script');
			script.type = 'module';
			script.src = COMPONENT_MODULE_URL;
			script.dataset.amLyrics = 'true';

			const handleLoad = () => {
				script.dataset.loaded = 'true';
				waitForDefinition();
			};

			const handleError = () => {
				script.removeEventListener('load', handleLoad);
				script.removeEventListener('error', handleError);
				script.remove();
				reject(new Error('Failed to load lyrics component.'));
			};

			script.addEventListener('load', handleLoad, { once: true });
			script.addEventListener('error', handleError, { once: true });
			document.head.append(script);
		});
	}

	function handleOverlayClick(event: MouseEvent) {
		if (event.target === event.currentTarget) {
			lyricsStore.close();
		}
	}

	function handleOverlayKeydown(event: KeyboardEvent) {
		if (event.target !== event.currentTarget) return;
		if (event.key === 'Enter' || event.key === ' ') {
			event.preventDefault();
			lyricsStore.close();
		}
	}

	function handleEscape(event: KeyboardEvent) {
		if (event.key === 'Escape' && $lyricsStore.open) {
			event.preventDefault();
			lyricsStore.close();
		}
	}

	function attachEscapeListener(open: boolean) {
		if (!browser) return;
		if (open && !hasEscapeListener) {
			window.addEventListener('keydown', handleEscape);
			hasEscapeListener = true;
		} else if (!open && hasEscapeListener) {
			window.removeEventListener('keydown', handleEscape);
			hasEscapeListener = false;
		}
	}

	function stopAnimation() {
		if (animationFrameId !== null) {
			cancelAnimationFrame(animationFrameId);
			animationFrameId = null;
		}
	}

	function handleRefresh() {
		if ($lyricsStore.track) {
			lyricsStore.refresh();
		}
		if (scriptStatus !== 'ready' && browser) {
			scriptStatus = 'idle';
			scriptError = null;
			void ensureComponentLoaded();
		}
	}

	function handleRetry() {
		scriptStatus = 'idle';
		scriptError = null;
		if (browser) {
			void ensureComponentLoaded();
		}
	}

	function handleLineClick(event: Event) {
		const detail = (event as CustomEvent<{ timestamp: number }>).detail;
		if (!detail) return;
		const timeSeconds = detail.timestamp / 1000;
		playerStore.play();
		window.dispatchEvent(new CustomEvent('lyrics:seek', { detail: { timeSeconds } }));
	}

	function patchLyricsAutoscroll(element: AmLyricsElement, attempt = 0) {
		if (!browser || !element || element.__tidalScrollPatched) {
			return;
		}

		const container = element.shadowRoot?.querySelector<HTMLElement>('.lyrics-container');
		if (!container) {
			if (attempt > 8) return;
			if (scrollPatchFrame !== null) {
				cancelAnimationFrame(scrollPatchFrame);
			}
			scrollPatchFrame = requestAnimationFrame(() => {
				patchLyricsAutoscroll(element, attempt + 1);
			});
			return;
		}

		scrollPatchFrame = null;

		element.__tidalScrollPatched = true;

		const styles = getComputedStyle(container);
		const parsedPaddingTop = parseFloat(styles.paddingTop || '0');
		const parsedPaddingBottom = parseFloat(styles.paddingBottom || '0');
		const preferredFraction = 0.32;
		const comfortTopFraction = 0.18;
		const comfortBottomFraction = 0.22;

		const computeScrollTarget = (line: HTMLElement) => {
			const containerRect = container.getBoundingClientRect();
			const lineRect = line.getBoundingClientRect();
			const lineTop = lineRect.top - containerRect.top + container.scrollTop;
			const lineBottom = lineTop + lineRect.height;
			const viewportStart = container.scrollTop;
			const viewportEnd = viewportStart + container.clientHeight;
			const comfortStart = viewportStart + parsedPaddingTop + container.clientHeight * comfortTopFraction;
			const comfortEnd = viewportEnd - parsedPaddingBottom - container.clientHeight * comfortBottomFraction;

			if (lineTop >= comfortStart && lineBottom <= comfortEnd) {
				return null;
			}

			const backgroundBefore = line.querySelector<HTMLElement>('.background-text.before');
			const backgroundOffset = backgroundBefore
				? Math.min(backgroundBefore.clientHeight / 2, lineRect.height * 0.6)
				: 0;

			const desiredTop =
				lineTop - parsedPaddingTop - container.clientHeight * preferredFraction - backgroundOffset;
			const maxScroll = container.scrollHeight - container.clientHeight;

			return Math.max(0, Math.min(maxScroll, desiredTop));
		};

		const applyScroll = (target: number | null) => {
			if (target === null) return;
			const delta = Math.abs(container.scrollTop - target);
			if (delta < 1) return;
			container.scrollTo({ top: target, behavior: 'smooth' });
		};

		const originalActiveScroll = element.scrollToActiveLine?.bind(element);
		element.scrollToActiveLine = function () {
			const indices = Array.isArray(element.activeLineIndices)
				? (element.activeLineIndices as number[])
				: [];
			if (!indices.length) {
				originalActiveScroll?.();
				return;
			}

			const targetIndex = Math.min(...indices);
			const line = container.querySelector<HTMLElement>(
				`.lyrics-line:nth-child(${targetIndex + 1})`
			);
			if (!line) {
				originalActiveScroll?.();
				return;
			}

			const target = computeScrollTarget(line);
			if (target === null) return;
			applyScroll(target);
		};

		const originalInstrumentalScroll = element.scrollToInstrumental?.bind(element);
		element.scrollToInstrumental = function (index: number) {
			const line = container.querySelector<HTMLElement>(`.lyrics-line:nth-child(${index + 1})`);
			if (!line) {
				originalInstrumentalScroll?.(index);
				return;
			}

			const target = computeScrollTarget(line);
			if (target === null) return;
			applyScroll(target);
		};
	}

	$effect(() => {
		if (!amLyricsElement) {
			return;
		}

		const listener = (event: Event) => handleLineClick(event);
		amLyricsElement.addEventListener('line-click', listener as EventListener);
		return () => {
			amLyricsElement?.removeEventListener('line-click', listener as EventListener);
		};
	});

	$effect(() => {
		if (!browser) return;
		const element = amLyricsElement as AmLyricsElement | null;
		if (!element || scriptStatus !== 'ready') return;
		patchLyricsAutoscroll(element);
	});

	$effect(() => {
		const open = $lyricsStore.open;
		const trackId = $lyricsStore.track?.id ?? null;

		if (!open || !trackId) {
			lastRefreshedTrackId = open ? trackId : null;
			return;
		}

		if (trackId !== lastRefreshedTrackId) {
			lastRefreshedTrackId = trackId;
			lyricsStore.refresh();
		}
	});

	$effect(() => {
		if (!browser || !amLyricsElement) {
			stopAnimation();
			return;
		}

		if (scriptStatus !== 'ready' || !$lyricsStore.open) {
			stopAnimation();
			amLyricsElement.currentTime = baseTimeMs;
			return;
		}

		if (!$playerStore.isPlaying) {
			stopAnimation();
			amLyricsElement.currentTime = baseTimeMs;
			return;
		}

		const element = amLyricsElement;
		const originBase = baseTimeMs;
		const nowTimestamp = performance.now();
		const originTimestamp =
			lastBaseTimestamp && Math.abs(nowTimestamp - lastBaseTimestamp) < 1200
				? lastBaseTimestamp
				: nowTimestamp;

		const tick = (now: number) => {
			const elapsed = now - originTimestamp;
			const nextMs = originBase + elapsed;
			element.currentTime = nextMs;
			animationFrameId = requestAnimationFrame(tick);
		};

		animationFrameId = requestAnimationFrame(tick);
		return () => {
			stopAnimation();
		};
	});

	$effect(() => {
		const track = $lyricsStore.track;
		if (!track) {
			metadata = null;
			return;
		}

		let title: string;
		let artist: string;
		let album: string | undefined;
		let isrc: string | undefined;

		if (isSonglinkTrack(track)) {
			title = track.title;
			artist = track.artistName;
			album = undefined;
			isrc = undefined;
		} else {
			title = track.title;
			artist = formatArtists(track.artists);
			album = track.album?.title;
			isrc = track.isrc;
		}

		const durationMs =
			typeof track.duration === 'number'
				? Math.max(0, Math.round(track.duration * 1000))
				: undefined;

		metadata = {
			title,
			artist,
			album,
			query: `${title} ${artist}`.trim(),
			durationMs,
			isrc: isrc ?? ''
		};
	});
</script>

{#if $lyricsStore.open}
	<div
		class="lyrics-overlay"
		role="presentation"
		onclick={handleOverlayClick}
		onkeydown={handleOverlayKeydown}
		tabindex="-1"
	>
		<div
			class={`lyrics-panel ${$lyricsStore.maximized ? 'lyrics-panel--maximized' : ''}`}
			role="dialog"
			aria-modal="true"
			aria-label="Lyrics"
		>
			<header class="lyrics-header">
				<div class="lyrics-heading">
					<h2 class="lyrics-title">Lyrics</h2>
					{#if metadata}
						<p class="lyrics-subtitle">{metadata.title} • {metadata.artist}</p>
						{#if metadata.album}
							<p class="lyrics-album">{metadata.album}</p>
						{/if}
					{:else}
						<p class="lyrics-subtitle">Start playback to load synced lyrics.</p>
					{/if}
				</div>
				<div class="lyrics-header-actions">
					<button
						type="button"
						class="lyrics-icon-button"
						onclick={handleRefresh}
						aria-label="Refresh lyrics"
						title="Refresh lyrics"
						disabled={!metadata || scriptStatus === 'loading'}
					>
						<RefreshCw size={18} class={scriptStatus === 'loading' ? 'animate-spin' : ''} />
					</button>
					<button
						type="button"
						class="lyrics-icon-button lyrics-maximize-button"
						onclick={() => lyricsStore.toggleMaximize()}
						aria-label={$lyricsStore.maximized ? 'Restore window' : 'Maximize window'}
						title={$lyricsStore.maximized ? 'Restore window' : 'Maximize window'}
					>
						{#if $lyricsStore.maximized}
							<Minimize2 size={18} />
						{:else}
							<Maximize2 size={18} />
						{/if}
					</button>
					<button
						type="button"
						class="lyrics-icon-button"
						onclick={() => lyricsStore.close()}
						aria-label="Close lyrics"
						title="Close lyrics"
					>
						<X size={18} />
					</button>
				</div>
			</header>

			<div class="lyrics-body">
				{#if scriptStatus === 'error'}
					<div class="lyrics-placeholder">
						<p class="lyrics-message">{scriptError ?? 'Unable to load lyrics right now.'}</p>
						<button type="button" class="lyrics-retry" onclick={handleRetry}> Try again </button>
					</div>
				{:else if !metadata}
					<div class="lyrics-placeholder">
						<p class="lyrics-message">Press play to fetch lyrics.</p>
					</div>
				{:else if scriptStatus === 'loading' || scriptStatus === 'idle'}
					<div class="lyrics-placeholder">
						<span class="spinner" aria-hidden="true"></span>
						Loading lyrics…
					</div>
				{:else}
					<div class="lyrics-component-wrapper">
						{#key lyricsKey}
							<am-lyrics
								bind:this={amLyricsElement}
								class="am-lyrics-element"
								song-title={metadata.title}
								song-artist={metadata.artist}
								song-album={metadata.album || undefined}
								song-duration={metadata.durationMs}
								query={metadata.query}
								isrc={metadata.isrc || undefined}
								highlight-color="#fff"
								hover-background-color="rgba(59, 130, 246, 0.14)"
								font-family="'Figtree', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif"
								autoscroll
								interpolate
							></am-lyrics>
						{/key}
					</div>
				{/if}
			</div>
		</div>
	</div>
{/if}

<style>
	.lyrics-overlay {
		position: fixed;
		inset: 0;
		bottom: var(--player-height, 120px);
		display: flex;
		align-items: center;
		justify-content: center;
		padding: 1rem;
		padding-bottom: 0.5rem;
		background: transparent;
		backdrop-filter: blur(var(--perf-blur-low, 12px)) saturate(120%);
		-webkit-backdrop-filter: blur(var(--perf-blur-low, 12px)) saturate(120%);
		z-index: 60;
		pointer-events: none;
	}

	.lyrics-panel {
		width: min(960px, 100%);
		height: clamp(380px, 72vh, 780px);
		display: flex;
		flex-direction: column;
		border-radius: 1.25rem;
		background: transparent;
		border: 1px solid rgba(148, 163, 184, 0.25);
		backdrop-filter: blur(var(--perf-blur-high, 32px)) saturate(var(--perf-saturate, 160%));
		-webkit-backdrop-filter: blur(var(--perf-blur-high, 32px)) saturate(var(--perf-saturate, 160%));
		box-shadow: 
			0 30px 80px rgba(2, 6, 23, 0.6),
			0 4px 18px rgba(15, 23, 42, 0.45),
			inset 0 1px 0 rgba(255, 255, 255, 0.06);
		overflow: hidden;
		pointer-events: auto;
		transition:
			border-color 1.2s cubic-bezier(0.4, 0, 0.2, 1),
			box-shadow 0.3s ease;
	}

	.lyrics-header {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		gap: 1rem;
		padding: 1.25rem 1.5rem 1rem;
		border-bottom: 1px solid rgba(71, 85, 105, 0.45);
	}

	.lyrics-heading {
		flex: 1;
	}

	.lyrics-title {
		margin: 0;
		font-size: 1.25rem;
		font-weight: 600;
		color: #f8fafc;
	}

	.lyrics-subtitle {
		margin: 0.35rem 0 0;
		font-size: 0.95rem;
		color: #cbd5f5;
	}

	.lyrics-album {
		margin: 0.2rem 0 0;
		font-size: 0.8rem;
		color: #94a3b8;
	}

	.lyrics-header-actions {
		display: flex;
		align-items: center;
		gap: 0.5rem;
	}

	.lyrics-icon-button {
		display: inline-flex;
		align-items: center;
		justify-content: center;
		padding: 0.4rem;
		border-radius: 9999px;
		border: 1px solid rgba(148, 163, 184, 0.3);
		background: transparent;
		backdrop-filter: blur(16px) saturate(140%);
		-webkit-backdrop-filter: blur(16px) saturate(140%);
		color: #e2e8f0;
		transition:
			background 160ms ease,
			border-color 160ms ease,
			transform 160ms ease,
			box-shadow 160ms ease;
	}

	.lyrics-icon-button[disabled] {
		opacity: 0.5;
		cursor: not-allowed;
	}

	.lyrics-icon-button:not([disabled]):hover {
		border-color: var(--bloom-accent, rgba(96, 165, 250, 0.7));
		box-shadow: inset 0 0 20px rgba(96, 165, 250, 0.12);
		transform: translateY(-1px);
	}

	.animate-spin {
		animation: spin 0.8s linear infinite;
	}

	@keyframes spin {
		0% {
			transform: rotate(0deg);
		}
		100% {
			transform: rotate(360deg);
		}
	}

	.lyrics-body {
		flex: 1;
		padding: 1rem 1.5rem 1.5rem;
		display: flex;
		overflow: hidden;
	}

	.lyrics-component-wrapper {
		flex: 1;
		display: flex;
		align-items: stretch;
		justify-content: stretch;
		border-radius: 1rem;
		background: rgba(15, 23, 42, 0.65);
		border: 1px solid rgba(59, 73, 99, 0.5);
		overflow: hidden;
	}

	.am-lyrics-element {
		flex: 1;
		display: block;
		width: 100%;
		height: 100%;
		overflow-y: auto;
		overflow-x: hidden;
		overscroll-behavior: contain;
		color: inherit;
	}

	.am-lyrics-element::part(container) {
		padding: 1.25rem;
		padding-block: 2rem;
		box-sizing: border-box;
	}

	.am-lyrics-element::part(line) {
		scroll-margin-block-start: min(32vh, 9rem);
		scroll-margin-block-end: min(28vh, 7rem);
	}

	@media (min-width: 640px) {
		.lyrics-panel--maximized .am-lyrics-element {
			font-size: clamp(1.05rem, 0.85rem + 1.2vw, 1.85rem);
			line-height: 1.6;
			letter-spacing: 0.01em;
		}

		.lyrics-panel--maximized .am-lyrics-element::part(line) {
			padding-block: clamp(0.65rem, 0.45rem + 0.8vw, 1.2rem);
		}
	}

	.lyrics-placeholder {
		flex: 1;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		gap: 1rem;
		text-align: center;
		color: #cbd5f5;
		padding: 1.5rem;
	}

	.lyrics-message {
		margin: 0;
		font-size: 0.95rem;
	}

	.lyrics-retry {
		border: 1px solid var(--bloom-accent, rgba(96, 165, 250, 0.7));
		background: transparent;
		backdrop-filter: blur(16px) saturate(140%);
		-webkit-backdrop-filter: blur(16px) saturate(140%);
		color: #f0f9ff;
		border-radius: 9999px;
		padding: 0.45rem 1.25rem;
		font-size: 0.85rem;
		font-weight: 500;
		box-shadow: inset 0 0 20px rgba(96, 165, 250, 0.1);
		transition:
			border-color 160ms ease,
			box-shadow 160ms ease;
	}

	.lyrics-retry:hover {
		border-color: var(--bloom-accent, rgba(191, 219, 254, 0.9));
		box-shadow: inset 0 0 30px rgba(96, 165, 250, 0.18);
	}

	.spinner {
		width: 1.25rem;
		height: 1.25rem;
		border-radius: 9999px;
		border: 2px solid rgba(148, 163, 184, 0.35);
		border-top-color: rgba(148, 163, 184, 0.95);
		animation: spin 0.85s linear infinite;
	}

	@media (max-width: 640px) {
		.lyrics-overlay {
			padding: 0;
			align-items: stretch;
			justify-content: stretch;
			bottom: 0; /* Override player height on mobile for full screen */
		}

		.lyrics-panel {
			border-radius: 0;
			border: none;
			width: 100vw;
			height: 100vh;
			max-height: 100vh;
		}

		.lyrics-header {
			flex-direction: column;
			align-items: flex-start;
		}

		.lyrics-header-actions {
			align-self: flex-end;
		}

		.lyrics-body {
			padding: 1rem;
		}

		.lyrics-maximize-button {
			display: none;
		}

		.am-lyrics-element::part(container) {
			padding: 1.5rem;
			padding-block: 2.5rem;
		}

		.am-lyrics-element::part(line) {
			scroll-margin-block-start: min(40vh, 9rem);
			scroll-margin-block-end: min(28vh, 6.5rem);
		}
	}
</style>