:root {
  --c1: #5b3cc4;
  --c2: #c43c7a;
  --c3: #1f6fd1;
  --c4: #2a2350;
  --fg: #fff;
  --dim: rgba(255, 255, 255, 0.55);
  --line-dim: rgba(255, 255, 255, 0.3);
  --ease: cubic-bezier(0.22, 0.9, 0.25, 1);
  --safe-top: max(env(safe-area-inset-top, 0px), calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px)));
  --safe-bottom: max(env(safe-area-inset-bottom, 0px), var(--tg-safe-area-inset-bottom, 0px));
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: #0b0b10; color: var(--fg);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Inter, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased; user-select: none; -webkit-user-select: none;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
svg.fill { fill: currentColor; stroke: none; }
.hidden { display: none !important; }

/* ---------- Animated, cover-tinted background ---------- */
#bg { position: fixed; inset: 0; overflow: hidden; z-index: 0; background: var(--c4); transition: background 1.2s; }
#bg::after { content: ""; position: absolute; inset: 0; background: linear-gradient(180deg, rgba(0,0,0,.25), rgba(0,0,0,.55)); }
.blob {
  position: absolute; width: 85vmax; height: 85vmax; border-radius: 50%;
  filter: blur(70px); opacity: .75; transition: background 1.2s;
  animation: drift 24s ease-in-out infinite alternate; will-change: transform;
}
.b1 { background: var(--c1); top: -35vmax; left: -30vmax; }
.b2 { background: var(--c2); bottom: -40vmax; right: -30vmax; animation-duration: 30s; animation-delay: -8s; }
.b3 { background: var(--c3); top: 20vh; left: 30vw; width: 60vmax; height: 60vmax; animation-duration: 36s; animation-delay: -15s; }
.b4 { background: var(--c1); bottom: -20vmax; left: -25vmax; width: 55vmax; height: 55vmax; opacity: .5; animation-duration: 28s; animation-delay: -4s; }
body.paused .blob { animation-play-state: paused; }
@keyframes drift {
  0%   { transform: translate(0, 0) rotate(0) scale(1); }
  50%  { transform: translate(12vw, 8vh) rotate(120deg) scale(1.15); }
  100% { transform: translate(-8vw, 14vh) rotate(240deg) scale(.95); }
}
.grain { position: absolute; inset: 0; z-index: 1; opacity: .06; mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"); }

/* ---------- Layout ---------- */
#app { position: relative; z-index: 1; height: 100%; display: flex; flex-direction: column; padding-top: var(--safe-top); }
.top { display: flex; align-items: center; gap: 12px; padding: 14px 16px 8px; transition: opacity .4s; }
.meta { flex: 1; min-width: 0; }
.title { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.artist { color: var(--dim); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 4px 8px; border-radius: 99px; background: rgba(255,255,255,.16); }

.cover { background: rgba(255,255,255,.08) center/cover no-repeat; border-radius: 8px; flex: none; box-shadow: 0 8px 30px rgba(0,0,0,.35); position: relative; overflow: hidden; }
.cover.placeholder::before { content: "♪"; position: absolute; inset: 0; display: grid; place-items: center; font-size: 45%; color: rgba(255,255,255,.7);
  background: linear-gradient(135deg, var(--c1), var(--c2)); }
.cover-sm { width: 48px; height: 48px; font-size: 48px; border-radius: 6px; }

.view { position: absolute; inset: 0; top: calc(var(--safe-top) + 70px); bottom: calc(150px + var(--safe-bottom)); transition: opacity .5s var(--ease), transform .6s var(--ease); }
#coverView { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 28px; padding: 0 28px; }
.cover-big { width: min(78vw, 46vh, 420px); aspect-ratio: 1; font-size: min(78vw, 46vh, 420px); border-radius: 14px; box-shadow: 0 30px 80px rgba(0,0,0,.5);
  transition: transform .6s var(--ease); }
body.paused .cover-big { transform: scale(.86); }
.big-meta { width: min(78vw, 420px); }
.title-big { font-size: 24px; font-weight: 800; letter-spacing: -.01em; }
.artist-big { font-size: 18px; color: var(--dim); }

.mode-lyrics #coverView { opacity: 0; transform: scale(.94); pointer-events: none; }
.mode-cover #lyricsView { opacity: 0; transform: translateY(30px); pointer-events: none; }
.mode-cover .top .meta, .mode-cover .top .cover-sm { opacity: 0; }

/* ---------- Lyrics ---------- */
#lyricsView { overflow: hidden; touch-action: none;
  -webkit-mask-image: linear-gradient(180deg, transparent 0, #000 9%, #000 78%, transparent 100%);
          mask-image: linear-gradient(180deg, transparent 0, #000 9%, #000 78%, transparent 100%); }
.lyrics { position: relative; padding: 0 26px; }
.line {
  --d: 0;
  padding: 12px 0; cursor: pointer; will-change: transform, filter;
  font-size: clamp(26px, 7.2vw, 40px); font-weight: 800; line-height: 1.18; letter-spacing: -.015em;
  color: var(--fg); opacity: .32;
  filter: blur(calc(min(var(--d), 5) * .55px));
  transition: transform .75s var(--ease), opacity .45s ease, filter .45s ease;
}
.line .inner { display: block; transform-origin: left center; transform: scale(.955); transition: transform .6s var(--ease), filter .6s; }
.line.active { opacity: 1; filter: none; }
.line.active .inner { transform: scale(1); filter: drop-shadow(0 0 18px rgba(255,255,255,.28)); }
.line.past { opacity: .32; }
/* Karaoke sweep: an inline span lays its background across wrapped rows in reading order. */
.line.active .txt {
  color: transparent;
  background: linear-gradient(90deg, #fff calc(var(--p, 0%) - 4%), rgba(255,255,255,.42) calc(var(--p, 0%) + 4%));
  -webkit-background-clip: text; background-clip: text;
}
.line.ended .txt { color: #fff; background: none; }
.lyrics.free .line { filter: none; opacity: .55; transition-delay: 0ms !important; }
.lyrics.free .line.active { opacity: 1; }
.lyrics.dragging .line { transition: opacity .3s, filter .3s; }
.line:active .inner { transform: scale(.97); }

/* Instrumental dots */
.line.dots .inner { display: flex; gap: 12px; height: 1.1em; align-items: center; transform: scale(.5); opacity: 0; transition: transform .6s var(--ease), opacity .4s; }
.line.dots.active .inner { transform: scale(1); opacity: 1; animation: breathe 2.6s ease-in-out infinite; }
.dot { width: .38em; height: .38em; border-radius: 50%; background: #fff; opacity: .3; }
@keyframes breathe { 50% { transform: scale(1.12); } }

.plain { white-space: pre-wrap; font-size: 22px; font-weight: 700; line-height: 1.45; padding: 30px 26px 120px; overflow-y: auto; height: 100%; touch-action: pan-y; user-select: text; }
.plain-note { font-size: 13px; color: var(--dim); font-weight: 500; margin-bottom: 18px; }
.status { position: absolute; inset: 0; display: grid; place-items: center; align-content: center; text-align: center; color: var(--dim); font-size: 17px; padding: 30px; line-height: 1.5; }
.status b { color: #fff; display: block; font-size: 20px; margin-bottom: 6px; }

/* ---------- Controls ---------- */
.controls { position: absolute; left: 0; right: 0; bottom: 0; padding: 0 24px calc(22px + var(--safe-bottom)); }
.progress input {
  --v: 0%; width: 100%; height: 22px; margin: 0; background: transparent; -webkit-appearance: none; appearance: none; cursor: pointer;
}
.progress input::-webkit-slider-runnable-track { height: 5px; border-radius: 3px; background: linear-gradient(90deg, rgba(255,255,255,.95) var(--v), rgba(255,255,255,.22) var(--v)); transition: height .2s; }
.progress input::-moz-range-track { height: 5px; border-radius: 3px; background: linear-gradient(90deg, rgba(255,255,255,.95) var(--v), rgba(255,255,255,.22) var(--v)); }
.progress input::-webkit-slider-thumb { -webkit-appearance: none; width: 0; height: 0; margin-top: -3px; border-radius: 50%; background: #fff; transition: width .2s, height .2s, margin .2s; }
.progress input::-moz-range-thumb { width: 0; height: 0; border: 0; background: #fff; }
.progress input:active::-webkit-slider-thumb, .progress:hover input::-webkit-slider-thumb { width: 13px; height: 13px; margin-top: -4px; }
.times { display: flex; justify-content: space-between; font-size: 12px; color: var(--dim); font-variant-numeric: tabular-nums; margin-top: 2px; }
.buttons { display: flex; align-items: center; justify-content: space-between; margin-top: 10px; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border-radius: 50%; color: rgba(255,255,255,.9); transition: transform .15s, background .2s, color .2s; position: relative; }
.icon-btn:active { transform: scale(.88); background: rgba(255,255,255,.1); }
.icon-btn svg { width: 26px; height: 26px; }
.icon-btn.small svg { width: 22px; height: 22px; }
.icon-btn.on { color: #fff; background: rgba(255,255,255,.18); }
.icon-btn:disabled { opacity: .35; }
.play { width: 70px; height: 70px; border-radius: 50%; background: #fff; color: #111; display: grid; place-items: center; box-shadow: 0 10px 30px rgba(0,0,0,.3); transition: transform .15s; }
.play:active { transform: scale(.9); }
.play svg { width: 32px; height: 32px; grid-area: 1/1; transition: opacity .2s, transform .25s var(--ease); }
.play .i-pause, body.playing .play .i-play { opacity: 0; transform: scale(.5); }
body.playing .play .i-pause { opacity: 1; transform: scale(1); }

/* Save-for-offline button: arrow -> progress ring -> check */
#btnSave .ring { position: absolute; inset: 4px; width: 36px; height: 36px; transform: rotate(-90deg); opacity: 0; }
#btnSave .ring circle { stroke-dasharray: 100.5; stroke-dashoffset: calc(100.5 - var(--dl, 0) * 100.5); transition: stroke-dashoffset .2s; }
#btnSave.loading .ring { opacity: 1; }
#btnSave.saved { color: #7dffb0; }

/* ---------- Library sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 10; pointer-events: none; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.45); opacity: 0; transition: opacity .35s; }
.sheet-panel {
  position: absolute; left: 0; right: 0; bottom: 0; max-height: 78vh; display: flex; flex-direction: column;
  background: rgba(24, 22, 34, .82); backdrop-filter: blur(30px) saturate(1.4); -webkit-backdrop-filter: blur(30px) saturate(1.4);
  border-radius: 20px 20px 0 0; padding: 8px 0 calc(12px + var(--safe-bottom));
  transform: translateY(105%); transition: transform .45s var(--ease);
}
.sheet.open { pointer-events: auto; }
.sheet.open .sheet-backdrop { opacity: 1; }
.sheet.open .sheet-panel { transform: none; }
.grabber { width: 40px; height: 5px; border-radius: 3px; background: rgba(255,255,255,.3); margin: 0 auto 8px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 0 18px 10px; }
.sheet-head h2 { margin: 0; font-size: 22px; }
.toggle { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--dim); cursor: pointer; }
.toggle input { accent-color: #fff; width: 16px; height: 16px; }
.lib-list { list-style: none; margin: 0; padding: 0 8px; overflow-y: auto; }
.lib-list li { display: flex; align-items: center; gap: 12px; padding: 8px 10px; border-radius: 12px; cursor: pointer; }
.lib-list li:active { background: rgba(255,255,255,.08); }
.lib-list li.current { background: rgba(255,255,255,.12); }
.lib-list li.unavailable { opacity: .35; }
.lib-list .cover { width: 46px; height: 46px; font-size: 46px; border-radius: 6px; }
.lib-list .m { flex: 1; min-width: 0; }
.lib-list .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-list .a { font-size: 13px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-list .badge { font-size: 14px; color: #7dffb0; }
.lib-list .del { width: 34px; height: 34px; color: var(--dim); display: grid; place-items: center; border-radius: 50%; }
.lib-list .del svg { width: 18px; height: 18px; }
.lib-empty { padding: 30px 20px; text-align: center; color: var(--dim); }

/* Search */
.search { display: flex; align-items: center; gap: 8px; margin: 0 16px 10px; padding: 0 12px; height: 42px; border-radius: 12px; background: rgba(255,255,255,.1); }
.search svg { width: 18px; height: 18px; color: var(--dim); flex: none; }
.search input { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: none; color: #fff; font: inherit; font-size: 16px; user-select: text; -webkit-user-select: text; }
.search input::placeholder { color: var(--dim); }
.search:focus-within { background: rgba(255,255,255,.16); }
.lib-list .src { font-size: 11px; font-weight: 700; letter-spacing: .02em; padding: 2px 6px; border-radius: 6px; background: rgba(255,255,255,.14); margin-right: 6px; }
.lib-list .src.full { background: rgba(125,255,176,.18); color: #7dffb0; }
.lib-list .act { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 50%; color: #fff; flex: none; }
.lib-list .act svg { width: 20px; height: 20px; }
.lib-list li.busy .act svg { display: none; }
.lib-list li.busy .act::after { content: ""; width: 18px; height: 18px; border-radius: 50%; border: 2px solid rgba(255,255,255,.25); border-top-color: #fff; animation: spin .8s linear infinite; }
.lib-list li.busy .a::after { content: " · Downloading…"; }
@keyframes spin { to { transform: rotate(360deg); } }
.lib-list .cover.wide { background-size: cover; }
.storage { font-size: 12px; color: var(--dim); text-align: center; padding: 10px 18px 0; }

.toast { position: fixed; left: 50%; top: calc(var(--safe-top) + 16px); z-index: 20; transform: translate(-50%, -20px); opacity: 0; pointer-events: none;
  background: rgba(30,30,40,.9); backdrop-filter: blur(20px); padding: 10px 16px; border-radius: 99px; font-size: 14px; font-weight: 600;
  transition: opacity .3s, transform .4s var(--ease); max-width: calc(100vw - 32px); text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 900px) {
  .lyrics { max-width: 760px; margin: 0 auto; }
  .controls { max-width: 760px; margin: 0 auto; }
}
@media (prefers-reduced-motion: reduce) {
  .blob { animation: none; }
  .line { transition-duration: .2s; transition-delay: 0ms !important; }
}

/* ---------- Lyrics sheet ---------- */
.sheet-panel.tall { height: 86vh; max-height: 86vh; }
.tabs { display: flex; gap: 4px; padding: 3px; border-radius: 10px; background: rgba(255,255,255,.1); }
.tab { padding: 6px 14px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--dim); }
.tab.on { background: rgba(255,255,255,.2); color: #fff; }
.tab-panel { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.lyr-form { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 0 16px 10px; }
.lyr-form .btn { grid-column: 1 / -1; }
.lyr-form input, .draft-row select, #lyrText {
  font: inherit; font-size: 16px; color: #fff; background: rgba(255,255,255,.1); border: 1px solid transparent;
  border-radius: 12px; padding: 10px 12px; outline: 0; min-width: 0; user-select: text; -webkit-user-select: text;
}
.lyr-form input:focus, #lyrText:focus, .draft-row select:focus { border-color: rgba(255,255,255,.35); }
.draft-row select option { color: #000; }
.btn { font-weight: 600; font-size: 15px; padding: 11px 16px; border-radius: 12px; background: rgba(255,255,255,.14); color: #fff; transition: transform .12s, opacity .2s; }
.btn:active { transform: scale(.97); }
.btn.primary { background: #fff; color: #111; }
.btn.ghost { background: none; color: var(--dim); }
.btn:disabled { opacity: .45; }
.draft-row { display: flex; gap: 8px; padding: 0 16px 8px; }
.draft-row select { flex: 1; }
.hint { font-size: 13px; color: var(--dim); padding: 0 18px 8px; }
#lyrText { flex: 1; min-height: 160px; margin: 0 16px; resize: none; line-height: 1.5; font-size: 15px; white-space: pre; overflow: auto; }
.toggle.share { padding: 10px 18px 4px; }
.btn-row { display: flex; gap: 8px; padding: 10px 16px 4px; }
.btn-row .primary { flex: 1; }
.lib-list .lyr-prev { font-size: 12px; color: var(--dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status .actions { display: flex; gap: 8px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.status .actions .btn { font-size: 14px; padding: 9px 14px; }

/* ---------- Tap-to-sync ---------- */
.sync { position: fixed; inset: 0; z-index: 30; display: flex; flex-direction: column; padding: calc(var(--safe-top) + 12px) 20px calc(var(--safe-bottom) + 20px);
  background: rgba(10,10,16,.92); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px); }
.sync-top { display: flex; align-items: center; justify-content: space-between; font-weight: 700; font-variant-numeric: tabular-nums; }
.sync-lines { flex: 1; display: flex; flex-direction: column; justify-content: center; gap: 14px; text-align: left; }
.sync-prev, .sync-next { font-size: 20px; font-weight: 700; color: rgba(255,255,255,.35); min-height: 1.3em; }
.sync-label { font-size: 12px; font-weight: 700; letter-spacing: .06em; text-transform: uppercase; color: #7dffb0; }
.sync-cur { font-size: clamp(28px, 8vw, 40px); font-weight: 800; line-height: 1.2; }
.sync-cur.flash { animation: flash .35s ease-out; }
@keyframes flash { from { color: #7dffb0; transform: translateY(-4px); } }
.sync-time { text-align: center; color: var(--dim); font-variant-numeric: tabular-nums; margin-bottom: 12px; }
.sync-tap { height: 34vh; max-height: 260px; border-radius: 24px; background: #fff; color: #111; font-size: 28px; font-weight: 800; letter-spacing: .1em; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.sync-tap:active { transform: scale(.98); background: #e6e6e6; }
