:root {
  --bg: #0d0e12;
  --panel: #171920;
  --panel-2: #20232c;
  --text: #e9eaf0;
  --muted: #8b90a0;
  --accent: #ff3d6e;
  --accent-2: #ffb03d;
  --bar-h: 52px;
  --gap: 6px;
  --label-size: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 system-ui, "Segoe UI", sans-serif;
  overflow: hidden;
}

button, input, textarea { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid #2e3240;
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { border-color: #4a5063; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { color: #ff7a7a; }
button[aria-pressed="true"] { background: var(--accent-2); border-color: var(--accent-2); color: #111; }

/* ---------- toolbar ---------- */
#bar {
  height: var(--bar-h);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 14px;
  background: var(--panel);
  border-bottom: 1px solid #23262f;
}
.brand { font-weight: 700; letter-spacing: .5px; color: var(--accent); }
.now {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}
#nowText { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
body.playing #nowText { color: var(--text); }
.ctl { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.ctl input { width: 110px; accent-color: var(--accent); }
.ctl output { min-width: 3.2em; color: var(--text); font-variant-numeric: tabular-nums; }

.scrub {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}
.scrub input { width: 220px; accent-color: var(--accent); }
.scrub input:disabled { opacity: .35; }
.scrub span { min-width: 3.4em; }
#curTime { text-align: right; }

.eq { display: inline-flex; align-items: flex-end; gap: 2px; height: 14px; }
.eq i { width: 3px; height: 3px; background: var(--muted); border-radius: 1px; }
body.playing .eq i { background: var(--accent); animation: eq .9s ease-in-out infinite; }
body.playing .eq i:nth-child(2) { animation-delay: -.3s; }
body.playing .eq i:nth-child(3) { animation-delay: -.6s; }
@keyframes eq { 0%, 100% { height: 3px; } 50% { height: 14px; } }

#showBar {
  position: fixed;
  top: 8px;
  right: 8px;
  z-index: 10;
  display: none;
  opacity: 0;
  transition: opacity .2s;
}
#showBar:hover, #showBar:focus-visible { opacity: 1; }
body.bar-hidden { --bar-h: 0px; }
body.bar-hidden #bar { display: none; }
body.bar-hidden #showBar { display: block; }

/* ---------- grid ---------- */
main {
  height: calc(100vh - var(--bar-h));
  display: grid;
  place-items: center;
  padding: 12px;
}
.grid {
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  gap: var(--gap);
  /* Largest 16:9 grid that fits both the width and the height of the window */
  width: min(100%, calc(
    (100vh - var(--bar-h) - 24px - (var(--rows) - 1) * var(--gap)) * 16 / 9 * var(--cols) / var(--rows)
    + (var(--cols) - 1) * var(--gap)
  ));
}

.tile {
  --level: 0;
  position: relative;
  aspect-ratio: 16 / 9;
  padding: 0;
  border: 0;
  border-radius: 6px;
  overflow: hidden;
  background: #1a1c23 center / cover no-repeat;
  outline: 2px solid transparent;
  outline-offset: 1px;
  transition: transform .15s, outline-color .15s, filter .2s;
}
.tile:hover { transform: scale(1.03); z-index: 1; }
.tile::before {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(transparent 40%, rgb(0 0 0 / .55));
}
.tile.empty { background: transparent; border: 1px dashed #262933; cursor: default; }
.tile.empty::before { display: none; }
.tile.empty:hover { transform: none; }

.label {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 4px 6px;
  font-size: var(--label-size);
  font-weight: 800;
  line-height: 1.1;
  text-align: center;
  color: #fff;
  text-shadow: 0 1px 3px #000, 0 0 10px rgb(0 0 0 / .8);
  overflow-wrap: anywhere;
}

/* volume level bar, animates during crossfades */
.level {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 4px;
  width: calc(var(--level) * 100%);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}

.tile.active { outline-color: var(--accent); box-shadow: 0 0 18px rgb(255 61 110 / .55); }
.tile.loading { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { outline-color: rgb(255 61 110 / .25); } }

.badge {
  position: absolute;
  top: 4px;
  right: 4px;
  display: none;
  padding: 1px 6px;
  border-radius: 4px;
  background: rgb(0 0 0 / .75);
  font-size: 11px;
  color: #ffb3b3;
}
.tile.error { filter: grayscale(1) brightness(.6); }
.tile.error .badge { display: block; }

/* edit mode */
body.editing .tile { cursor: pointer; }
body.editing .tile:not(.empty) { outline: 1px dashed var(--accent-2); }
body.editing .tile.empty { cursor: pointer; border-color: #3a3f4f; }
body.editing .tile.empty::after {
  content: "+";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: #4a5063;
  font-size: 22px;
}
.tile.drop-target { outline: 2px solid var(--accent-2) !important; }

/* hidden player stack: kept rendered (some browsers stop hidden/zero-size iframes) */
#players {
  position: fixed;
  right: 0;
  bottom: 0;
  width: 320px;
  height: 180px;
  opacity: 0;
  pointer-events: none;
  z-index: -1;
}
#players > div { position: absolute; inset: 0; }

/* ---------- dialogs ---------- */
dialog {
  width: min(640px, calc(100vw - 32px));
  background: var(--panel);
  color: var(--text);
  border: 1px solid #2e3240;
  border-radius: 10px;
  padding: 20px;
}
dialog::backdrop { background: rgb(0 0 0 / .6); }
dialog h2 { margin: 0 0 14px; font-size: 18px; }
label.block { display: block; margin-bottom: 10px; color: var(--muted); }
label.block textarea, label.block input {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 8px;
  background: var(--bg);
  border: 1px solid #2e3240;
  border-radius: 6px;
}
textarea { resize: vertical; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; margin-top: 16px; }
.spacer { flex: 1; }
.msg { min-height: 1.2em; margin: 4px 0 10px; color: var(--muted); font-size: 12px; }
.msg.warn { color: var(--accent-2); }
hr { border: 0; border-top: 1px solid #2e3240; margin: 16px 0; }
.grid-opts { display: flex; flex-wrap: wrap; gap: 12px 18px; }
.grid-opts label { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.grid-opts input[type="number"] {
  width: 64px;
  padding: 4px 6px;
  background: var(--bg);
  border: 1px solid #2e3240;
  border-radius: 6px;
}
.grid-opts .check { width: 100%; }
code { background: var(--bg); padding: 0 4px; border-radius: 3px; }
