:root {
  --bg: var(--tg-theme-bg-color, #0b0d0f);
  --surface: var(--tg-theme-secondary-bg-color, #161a1e);
  --text: var(--tg-theme-text-color, #eef2f5);
  --muted: var(--tg-theme-hint-color, #8b97a3);
  --accent: var(--tg-theme-button-color, #e8a33d);
  --accent-text: var(--tg-theme-button-text-color, #0b0d0f);
  --danger: #ef6b6b;
  --good: #5ec98a;
  --line: rgba(127, 140, 152, .22);
  --field: color-mix(in srgb, var(--text) 7%, transparent);
  --radius: 16px;
  --sheet-radius: 22px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
html, body { margin: 0; height: 100%; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 16px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* Fields show focus with their border; a second ring reads as a double box. */
input:focus-visible, textarea:focus-visible { outline: none; }

/* ── app frame ──────────────────────────────────────────────────────── */
.app {
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.home {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
  /* Fixed padding, never a viewport unit: the keyboard must not move the
     title or the field, only the space below them. */
  padding: calc(env(safe-area-inset-top, 0px) + 40px) 20px
           calc(env(safe-area-inset-bottom, 0px) + 24px);
  display: flex;
  flex-direction: column;
}

/* ── search ─────────────────────────────────────────────────────────── */
.hero-title {
  margin: 0 0 22px;
  font-size: clamp(30px, 9.5vw, 40px);
  line-height: 1.08;
  font-weight: 700;
  letter-spacing: -.025em;
}

.search {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.search-input {
  width: 100%;
  height: 56px;
  padding: 0 18px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--text);
  font-size: 17px;              /* 16px+ stops iOS from zooming on focus */
  appearance: none;
  outline: none;
  transition: border-color .15s ease, background .15s ease;
}

.search-input:focus {
  border-color: color-mix(in srgb, var(--accent) 60%, transparent);
  background: color-mix(in srgb, var(--accent) 7%, var(--field));
}

.search-input.invalid {
  border-color: color-mix(in srgb, var(--danger) 70%, transparent);
}

.search-input::placeholder {
  color: color-mix(in srgb, var(--muted) 70%, transparent);
}

.search-submit {
  height: 54px;
  border: 0;
  border-radius: var(--radius);
  background: var(--accent);
  color: var(--accent-text);
  font-size: 17px;
  font-weight: 700;
  transition: transform .08s ease, opacity .15s ease;
}

.search-submit:active { transform: scale(.985); }
.search-submit:disabled { opacity: .55; }

/* ── compact now playing ────────────────────────────────────────────── */
.now-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 26px;
  padding: 8px 8px 8px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--field);
}

.now-bar-main {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 0;
  border: 0;
  background: none;
  text-align: left;
}

.now-bar-dot {
  flex: none;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 10px var(--accent);
}

.now-bar-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
  font-weight: 600;
}

.now-bar-pct {
  flex: none;
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.now-bar-stop {
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  background: transparent;
  color: var(--danger);
  font-size: 11px;
  line-height: 1;
}

.now-bar-stop:active { transform: scale(.94); }

/* ── now playing ────────────────────────────────────────────────────── */
.eyebrow {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .16em;
  text-transform: uppercase;
  color: var(--accent);
}

.now-title {
  margin: 0;
  font-size: 24px;
  line-height: 1.25;
  font-weight: 700;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.progress { margin-top: 22px; }

.track {
  height: 6px;
  border-radius: 999px;
  background: var(--line);
  overflow: hidden;
}

.track-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width .4s ease;
}

.progress-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 8px;
  font-size: 12.5px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 20px;
}

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
}

.stat-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
}

.stat-value {
  margin-top: 3px;
  font-size: 14px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.stat-value.good { color: var(--good); }

/* ── buttons ────────────────────────────────────────────────────────── */
.primary, .danger, .ghost {
  width: 100%;
  height: 54px;
  border-radius: 14px;
  border: 1px solid transparent;
  font-size: 16px;
  font-weight: 650;
  transition: transform .08s ease, opacity .15s ease;
}

.primary {
  background: var(--accent);
  color: var(--accent-text);
}

.danger {
  margin-top: 22px;
  background: transparent;
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
  color: var(--danger);
}

.ghost {
  margin-top: 8px;
  background: transparent;
  color: var(--muted);
}

.primary:active, .danger:active, .ghost:active { transform: scale(.99); }
.primary:disabled, .danger:disabled { opacity: .55; }

/* ── session ────────────────────────────────────────────────────────── */
.session {
  margin-top: 24px;
  padding: 16px;
  border: 1px solid color-mix(in srgb, var(--danger) 28%, transparent);
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--danger) 5%, transparent);
}

.session-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 14px;
}

.pill {
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}

.pill.bad {
  color: var(--danger);
  border-color: color-mix(in srgb, var(--danger) 45%, transparent);
}

.field { display: block; margin-top: 12px; }

.field-label {
  display: block;
  margin-bottom: 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--muted);
}

.input {
  width: 100%;
  padding: 11px 12px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--bg);
  color: var(--text);
  font-size: 16px;
  resize: vertical;
}

.input:focus {
  outline: none;
  border-color: color-mix(in srgb, var(--accent) 55%, transparent);
}

.input::placeholder { color: color-mix(in srgb, var(--muted) 75%, transparent); }

.hint {
  margin: 12px 0 0;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--muted);
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  word-break: break-all;
  opacity: .8;
}

.session .primary { margin-top: 12px; height: 48px; font-size: 15px; }

/* ── sheets ─────────────────────────────────────────────────────────── */
.sheet-layer {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  animation: fade-in .18s ease;
}

.sheet {
  position: relative;
  width: 100%;
  max-width: 560px;
  max-height: 92dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  border: 1px solid var(--line);
  border-bottom: 0;
  border-radius: var(--sheet-radius) var(--sheet-radius) 0 0;
  box-shadow: 0 -18px 60px rgba(0, 0, 0, .45);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  animation: sheet-in .22s cubic-bezier(.2, .8, .2, 1);
}

.sheet-tall { height: 92dvh; }

/* The sheet follows the finger with an inline transform; this is the release
   animation when the pull was too short to dismiss. */
.sheet.snapping { transition: transform .18s cubic-bezier(.2, .8, .2, 1); }

.sheet-layer.closing .sheet { animation: sheet-out .16s ease forwards; }
.sheet-layer.closing .backdrop { animation: fade-out .16s ease forwards; }

@keyframes sheet-in { from { transform: translateY(100%); } to { transform: none; } }
@keyframes sheet-out { to { transform: translateY(100%); } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes fade-out { to { opacity: 0; } }

@media (prefers-reduced-motion: reduce) {
  .sheet, .backdrop, .sheet-layer.closing .sheet, .sheet-layer.closing .backdrop {
    animation: none;
  }
  .track-fill, .cover, .search-input { transition: none; }
}

.grip {
  flex: none;
  width: 38px;
  height: 4px;
  margin: 9px auto 0;
  border-radius: 999px;
  background: rgba(127, 140, 152, .4);
}

.sheet-head {
  flex: none;
  padding: 14px 20px 10px;
}

.sheet-title {
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}

.sheet-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  -webkit-overflow-scrolling: touch;
  padding: 0 20px 20px;
}

.sheet-foot {
  flex: none;
  padding: 12px 20px 14px;
  border-top: 1px solid var(--line);
  background: var(--bg);
}

.sheet-foot .primary { margin: 0; }

/* ── results ────────────────────────────────────────────────────────── */
.list { display: flex; flex-direction: column; gap: 8px; }

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 12px 13px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  text-align: left;
}

.row:active { transform: scale(.99); }
.row.current { border-color: color-mix(in srgb, var(--accent) 55%, transparent); }

.row-main { flex: 1; min-width: 0; }

.row-title {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.32;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.badges {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}

.badge {
  font-size: 10px;
  font-weight: 650;
  letter-spacing: .03em;
  padding: 2px 7px;
  border-radius: 6px;
  background: color-mix(in srgb, var(--muted) 16%, transparent);
  color: var(--muted);
}

.badge.q {
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 16%, transparent);
}

.badge.seeds-badge { color: var(--good); }
.badge.leech-badge { color: var(--danger); }

.row-stats {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 7px;
  font-size: 12px;
  color: var(--muted);
}

.seeds { color: var(--good); font-weight: 650; }
.leech { color: var(--danger); }

.row-go {
  flex: none;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  padding-left: 2px;
}

.row-skeleton {
  height: 78px;
  border-radius: 14px;
  background: var(--field);
  animation: pulse 1.1s ease-in-out infinite;
}

.empty {
  padding: 44px 16px;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

/* ── title page ─────────────────────────────────────────────────────── */
.detail { padding-top: 8px; }

.hero {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

.cover-wrap {
  position: relative;
  flex: none;
  width: 104px;
  aspect-ratio: 2 / 3;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--field);
}

.cover-wrap.loading::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 20%,
              rgba(255, 255, 255, .07) 45%, transparent 70%);
  animation: shimmer 1.2s ease-in-out infinite;
}

.cover {
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  transition: opacity .2s ease;
}

.cover.loaded { opacity: 1; }

.cover-fallback {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 34px;
  font-weight: 700;
  color: var(--muted);
  background: linear-gradient(160deg,
              color-mix(in srgb, var(--accent) 14%, transparent), transparent 70%);
}

.hero-body { flex: 1; min-width: 0; }

.detail-title {
  margin: 0;
  font-size: 19px;
  line-height: 1.3;
  font-weight: 700;
}

.detail-subtitle {
  margin-top: 7px;
  font-size: 12.5px;
  color: var(--muted);
}

.detail-subtitle:empty { display: none; }

.description {
  margin: 18px 0 0;
  font-size: 14.5px;
  line-height: 1.62;
  color: color-mix(in srgb, var(--text) 84%, var(--muted));
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.skeleton {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 18px;
}

.skeleton span {
  height: 13px;
  border-radius: 6px;
  background: var(--field);
  animation: pulse 1.1s ease-in-out infinite;
}

.skeleton span:nth-child(2) { width: 92%; }
.skeleton span:nth-child(3) { width: 66%; }

.detail-error {
  margin-top: 18px;
  font-size: 13.5px;
  line-height: 1.6;
  color: var(--muted);
}

.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 650;
  text-decoration: underline;
  text-underline-offset: 2px;
}

@keyframes shimmer { from { transform: translateX(-100%); } to { transform: translateX(100%); } }
@keyframes pulse { 0%, 100% { opacity: .5; } 50% { opacity: 1; } }

/* ── misc ───────────────────────────────────────────────────────────── */
.spinner {
  display: inline-block;
  width: 17px;
  height: 17px;
  border: 2px solid rgba(0, 0, 0, .25);
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -3px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  z-index: 50;
  max-width: 88vw;
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .35);
}

@keyframes spin { to { transform: rotate(360deg); } }
