:root {
  --bg: #0b0b10;
  --surface: #17171f;
  --surface-2: #22222d;
  --text: #f4f4f8;
  --muted: #8a8a99;
  --dim: #4a4a58;
  --accent: #ff4f7b;
  --ok: #3ddc84;
  --warn: #ffb020;
  --error: #ff5a5a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Display", "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 0;
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
}
button.primary { background: var(--accent); color: #fff; font-weight: 600; }
button.big { font-size: 1.15rem; padding: 16px 28px; border-radius: 16px; }

/* Top bar */
.topbar {
  padding: calc(var(--safe-top) + 10px) 16px 8px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.status { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; font-size: 0.85rem; color: var(--muted); }
#status-text { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--dim); flex-shrink: 0; }
.dot[data-state="idle"] { background: var(--muted); }
.dot[data-state="active"] { background: var(--ok); }
.dot[data-state="busy"] { background: var(--warn); animation: pulse 0.8s infinite alternate; }
.dot[data-state="error"] { background: var(--error); }
@keyframes pulse { to { opacity: 0.3; } }

.meter { position: relative; width: 70px; height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; }
.meter-fill { height: 100%; width: 0; background: var(--ok); transition: width 80ms linear; }
.meter-threshold { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text); opacity: 0.6; }

/* Now playing */
.now { display: flex; align-items: center; gap: 12px; padding: 4px 16px 12px; }
.now[hidden] { display: none; }
.artwork { width: 56px; height: 56px; border-radius: 10px; object-fit: cover; background: var(--surface); flex-shrink: 0; }
.artwork[hidden] { display: none; }
.meta { flex: 1; min-width: 0; }
.song-title { font-weight: 700; font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.song-artist { color: var(--muted); font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.progress { margin-top: 8px; height: 3px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.progress-fill { height: 100%; width: 0; background: var(--text); }

/* Lyrics */
.lyrics {
  flex: 1;
  overflow-y: auto;
  padding: 0 20px;
  -webkit-overflow-scrolling: touch;
  mask-image: linear-gradient(transparent 0, #000 8%, #000 85%, transparent 100%);
  -webkit-mask-image: linear-gradient(transparent 0, #000 8%, #000 85%, transparent 100%);
}
.spacer { height: 30vh; }
.line {
  font-size: clamp(1.5rem, 6.5vw, 2.4rem);
  font-weight: 700;
  line-height: 1.25;
  margin: 0 0 0.55em;
  color: var(--dim);
  transition: color 0.3s, transform 0.3s, opacity 0.3s;
  transform-origin: left center;
  cursor: pointer;
}
.line.past { color: var(--dim); opacity: 0.55; }
.line.active { color: var(--muted); transform: scale(1.02); }
.line.instrumental { font-size: 1.6rem; }
.line.active.instrumental { color: var(--text); }

.word {
  --p: 0%;
  background: linear-gradient(90deg, var(--text) var(--p), currentColor var(--p));
  -webkit-background-clip: text;
  background-clip: text;
}
.line.active .word { -webkit-text-fill-color: transparent; }
.line.active .word.sung { -webkit-text-fill-color: var(--text); }

.plain { white-space: pre-wrap; font-size: 1.2rem; line-height: 1.5; color: var(--text); padding-bottom: 40px; }
.message { color: var(--muted); text-align: center; margin: 32px 0 16px; line-height: 1.4; }

h2 { font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); margin: 24px 0 8px; }

.history { list-style: none; padding: 0; margin: 0 0 24px; }
.history li {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "title time" "artist time";
  gap: 2px 12px;
  padding: 12px 4px;
  border-bottom: 1px solid var(--surface-2);
  cursor: pointer;
}
.history li.message { display: block; border: 0; }
.h-title { grid-area: title; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.h-artist { grid-area: artist; color: var(--muted); font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.h-time { grid-area: time; align-self: center; color: var(--muted); font-size: 0.8rem; }

/* Toolbar */
.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px calc(var(--safe-bottom) + 10px);
  background: var(--bg);
}
.toolbar #toggle-btn { min-width: 84px; }
.toolbar #toggle-btn.on { background: var(--surface-2); color: var(--accent); }
.offset { display: flex; align-items: center; gap: 4px; margin-left: auto; background: var(--surface); border-radius: 12px; }
.offset button { background: transparent; font-size: 1.2rem; padding: 8px 14px; }
#offset-label { min-width: 46px; text-align: center; font-variant-numeric: tabular-nums; font-size: 0.85rem; color: var(--muted); }
.toolbar svg { display: block; }

/* Overlay + dialogs */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(11, 11, 16, 0.96);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  z-index: 10;
}
.overlay[hidden] { display: none; }
.overlay-card { max-width: 380px; text-align: center; }
.overlay-card h1 { font-size: 2rem; margin: 0 0 12px; }
.overlay-card p { color: var(--muted); line-height: 1.45; }
.hint { font-size: 0.85rem; color: var(--muted); line-height: 1.4; }
.error { color: var(--error); min-height: 1.2em; }

dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100dvh - 64px);
  border: 0;
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  padding: 20px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.6); }
dialog h2 { margin-top: 0; }
dialog label { display: block; font-size: 0.85rem; color: var(--muted); margin-bottom: 14px; }
dialog input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 12px;
  font-size: 16px; /* prevents iOS zoom on focus */
  color: var(--text);
  background: var(--surface-2);
  border: 0;
  border-radius: 10px;
}
#search-results { max-height: 50dvh; overflow-y: auto; margin-top: 12px; }
.actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 12px; }
