:root {
  color-scheme: dark;
  --bg: #0e0e10;
  --surface: #1c1c1f;
  --surface-2: #2a2a2e;
  --text: #f4f4f6;
  --muted: #9a9aa2;
  --accent: #1ed760;
  --accent-text: #06210f;
  --heart: #ff375f;
  --star: #ffcc00;
  --danger: #ff6b6b;
  --tabs-h: 56px;
  --mini-h: 64px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html { background: var(--bg); }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  padding: 0 env(safe-area-inset-right) calc(var(--tabs-h) + var(--safe-b) + 24px) env(safe-area-inset-left);
  -webkit-user-select: none;
  user-select: none;
}
body.has-player { padding-bottom: calc(var(--tabs-h) + var(--mini-h) + var(--safe-b) + 32px); }
body.noscroll { overflow: hidden; }

button { font: inherit; color: inherit; }
button:disabled { opacity: 0.35; }
input, select { font: inherit; font-size: 16px; /* >=16px evita lo zoom automatico su iOS */ }

.icon { display: inline-flex; width: 24px; height: 24px; flex: none; }
.icon svg { width: 100%; height: 100%; fill: currentColor; }

/* ---------- barra superiore ---------- */

.bar {
  position: sticky;
  top: 0;
  z-index: 5;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: rgba(14, 14, 16, 0.92);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}
.bar-row { display: flex; gap: 8px; align-items: center; }
.bar-row + .bar-row { margin-top: 8px; }
.bar h1 { margin: 0; font-size: 28px; letter-spacing: -0.02em; }

#search, #sort, .inline-form input {
  padding: 9px 12px;
  border: 0;
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}
#search { flex: 1; min-width: 0; }
#sort { flex: none; }

main { padding: 8px 16px 0; }

.empty { color: var(--muted); text-align: center; padding: 40px 16px; }
.meta { color: var(--muted); font-size: 13px; }

/* ---------- griglia video ---------- */

.grid {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 18px 12px;
}

.card { position: relative; }
.card-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.card-main .title, .row-text .title {
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding-right: 28px;
}
.card.playing .title, .row-wrap.playing .title { color: var(--accent); }
.card-more { position: absolute; right: -6px; bottom: 14px; color: var(--muted); }

.thumb, .row-thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: 8px;
  background: var(--surface) center / cover no-repeat;
  display: grid;
  place-items: center;
  color: var(--muted);
  overflow: hidden;
  margin-bottom: 6px;
}
.progress { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--accent); }
.badge-heart {
  position: absolute;
  top: 6px;
  right: 6px;
  color: var(--heart);
  filter: drop-shadow(0 1px 2px rgba(0, 0, 0, 0.6));
}
.badge-heart .icon { width: 18px; height: 18px; }

/* ---------- righe (playlist, coda) ---------- */

.rows { list-style: none; margin: 12px 0 0; padding: 0; }
.rows li, .row-wrap { display: flex; align-items: center; }
.row {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.row.compact { padding: 10px 0; }
.row-thumb { width: 96px; flex: none; margin: 0; border-radius: 6px; }
.row-thumb .badge-heart, .row-thumb .progress { display: none; }
.row-text { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.row-text .title { padding-right: 0; }
.row .check { color: var(--accent); }

/* ---------- pulsanti ---------- */

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  cursor: pointer;
  flex: none;
}
.icon-btn-spacer { width: 44px; }

.pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 8px 14px;
  border: 0;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.pill .icon { width: 20px; height: 20px; }
.pill.primary { background: var(--accent); color: var(--accent-text); }
.pill.danger { color: var(--danger); }
.pill.on { color: var(--heart); }
.pill.wide { width: 100%; margin-top: 8px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }

.back {
  display: inline-flex;
  align-items: center;
  margin: 4px 0 0 -8px;
  padding: 6px 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 17px;
}
.detail-title { margin: 4px 0 0; font-size: 26px; }

/* ---------- stelle e cuore ---------- */

.stars { display: flex; }
.star {
  width: 36px;
  height: 40px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--surface-2);
  cursor: pointer;
}
.star .icon { width: 28px; height: 28px; }
.star.on { color: var(--star); }
.heart.on { color: var(--heart); }

/* ---------- player ---------- */

.player {
  position: fixed;
  z-index: 10;
  left: 8px;
  right: 8px;
  bottom: calc(var(--tabs-h) + var(--safe-b) + 8px);
  height: var(--mini-h);
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 6px 6px 6px 6px;
  border-radius: 10px;
  background: var(--surface-2);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  overflow: hidden;
}
.player .full-only { display: none; }

.video-wrap { background: #000; border-radius: 6px; overflow: hidden; cursor: pointer; }
.player:not(.expanded) .video-wrap { width: 92px; height: 52px; }
#video { display: block; width: 100%; height: 100%; object-fit: contain; background: #000; }

.mini-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}
.mini-title, .mini-sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.mini-title { font-size: 15px; font-weight: 600; }
.mini-sub { font-size: 13px; color: var(--muted); }
.mini-controls { display: flex; }
.mini-progress { position: absolute; left: 10px; right: 10px; bottom: 0; height: 2px; background: rgba(255, 255, 255, 0.15); }
#mini-progress-bar { height: 100%; width: 0; background: var(--text); }

.player.expanded {
  inset: 0;
  height: auto;
  display: block;
  padding: calc(env(safe-area-inset-top) + 4px) calc(env(safe-area-inset-right) + 16px) calc(var(--safe-b) + 24px) calc(env(safe-area-inset-left) + 16px);
  border-radius: 0;
  background: linear-gradient(180deg, #23232a 0%, var(--bg) 55%);
  box-shadow: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  z-index: 30;
}
.player.expanded .mini-only { display: none; }
.player.expanded .full-only { display: block; }
.player.expanded .full-head { display: flex; }
.player.expanded .video-wrap { width: 100%; aspect-ratio: 16 / 9; border-radius: 10px; cursor: default; }

.full-head { align-items: center; justify-content: space-between; margin: 0 -8px 8px; }
.full-context { font-size: 13px; color: var(--muted); text-align: center; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.full-title-row { display: flex; align-items: center; gap: 8px; margin-top: 18px; }
.full-titles { flex: 1; min-width: 0; }
.full-titles h2 { margin: 0; font-size: 22px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.full-titles p { margin: 2px 0 0; color: var(--muted); font-size: 14px; }
.heart .icon { width: 28px; height: 28px; }

.rating-row { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; }

.transport { display: flex; align-items: center; justify-content: space-between; margin: 18px 0 8px; }
.transport .big .icon { width: 36px; height: 36px; }
.transport .toggle { color: var(--muted); }
.transport .toggle.on { color: var(--accent); }
.play-btn { width: 68px; height: 68px; background: var(--text); color: var(--bg); }
.play-btn .icon { width: 36px; height: 36px; }

.view-modes { display: flex; gap: 8px; justify-content: center; margin: 12px 0 4px; }

.queue-title { margin: 24px 0 4px; font-size: 17px; }
.queue { list-style: none; margin: 0; padding: 0; }
.queue li { display: flex; }

/* ---------- tab bar ---------- */

.tabs {
  position: fixed;
  z-index: 9;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  padding: 0 env(safe-area-inset-right) var(--safe-b) env(safe-area-inset-left);
  background: rgba(14, 14, 16, 0.96);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}
.tabs button {
  flex: 1;
  height: var(--tabs-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 11px;
  cursor: pointer;
}
.tabs button.active { color: var(--text); }

/* ---------- sheet ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: flex-end;
  background: rgba(0, 0, 0, 0.55);
}
.sheet {
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 8px 16px calc(var(--safe-b) + 20px);
  border-radius: 16px 16px 0 0;
  background: var(--surface);
}
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.sheet-head h2 { margin: 0; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet h3 { margin: 18px 0 0; font-size: 15px; color: var(--muted); font-weight: 600; }
.sheet-actions { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.inline-form { display: flex; gap: 8px; margin-top: 12px; }
.inline-form input { flex: 1; min-width: 0; background: var(--surface-2); -webkit-user-select: text; user-select: text; }

/* ---------- altro / diagnostica ---------- */

.settings h2 { font-size: 18px; margin: 20px 0 8px; }
.info { display: grid; grid-template-columns: 1fr auto; gap: 6px 12px; margin: 0 0 12px; font-size: 14px; }
.info dt { color: var(--muted); }
.info dd { margin: 0; text-align: right; }
.log {
  max-height: 50vh;
  overflow: auto;
  padding: 10px;
  border-radius: 8px;
  background: var(--surface);
  font: 12px/1.5 ui-monospace, Menlo, monospace;
  white-space: pre-wrap;
  -webkit-user-select: text;
  user-select: text;
}

/* ---------- toast ---------- */

.toast {
  position: fixed;
  z-index: 50;
  left: 16px;
  right: 16px;
  bottom: calc(var(--tabs-h) + var(--mini-h) + var(--safe-b) + 24px);
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}
/* Con sheet o player completo aperti il toast va in alto per non coprire i controlli. */
.toast.top { bottom: auto; top: calc(env(safe-area-inset-top) + 12px); }
