:root {
  --bg: #111418;
  --surface: #1b2027;
  --surface-2: #252c35;
  --border: #323a45;
  --text: #eef1f4;
  --muted: #9aa5b1;
  --accent: #f5a524;
  --accent-text: #1a1204;
  --go: #2fbf71;
  --stop: #e5484d;
  --danger: #ff6b6b;
  --radius: 12px;
  --tab-h: 64px;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #eceff3;
    --border: #d5dae1;
    --text: #15191e;
    --muted: #5d6875;
    --accent: #d98500;
    --accent-text: #ffffff;
    --danger: #c62828;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none; /* no pull-to-refresh reloads */
}

button, input, select, textarea { font: inherit; color: inherit; }
button { touch-action: manipulation; cursor: pointer; }
[hidden] { display: none !important; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* Layout ------------------------------------------------------------------ */

.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 6px) max(16px, env(safe-area-inset-right)) 6px max(16px, env(safe-area-inset-left));
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}
.brand { margin: 0; font-size: 1.1rem; letter-spacing: .02em; }

main {
  max-width: 720px;
  margin: 0 auto;
  padding: 0 max(16px, env(safe-area-inset-right)) calc(var(--tab-h) + env(safe-area-inset-bottom) + 24px) max(16px, env(safe-area-inset-left));
}

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(3, 1fr);
  height: calc(var(--tab-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.tabbar button {
  background: none; border: 0; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-size: 1.3rem; gap: 2px;
}
.tabbar button span { font-size: .75rem; }
.tabbar button.active { color: var(--accent); }

.toolbar { display: grid; gap: 8px; padding: 12px 0; background: var(--bg); }
.toolbar.sticky { position: sticky; top: calc(env(safe-area-inset-top) + 45px); z-index: 4; }
.row { display: flex; align-items: center; gap: 8px; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.view-title { margin: 0; flex: 1; font-size: 1.2rem; }
.count { color: var(--muted); font-size: .85rem; flex: 1; }

/* Controls ---------------------------------------------------------------- */

input[type="search"], input:not([type]), input[type="number"], input[type="password"], select, textarea, .title-input {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 16px; /* prevents iOS zoom on focus */
}
textarea { resize: vertical; }
#sync-token { min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 0; }
.sort select { width: auto; max-width: 100%; }
.sort.grow select { width: 100%; }
.title-input { font-weight: 600; flex: 1; }

.btn {
  min-height: 44px;
  padding: 8px 14px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  white-space: nowrap;
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; font-weight: 600; }
.btn.ghost { background: transparent; }
.btn.danger, .icon-btn.danger { color: var(--danger); }
.btn:disabled, .icon-btn:disabled { opacity: .35; cursor: default; }

.icon-btn {
  min-width: 44px; min-height: 44px;
  background: none; border: 0; border-radius: var(--radius);
  font-size: 1.15rem; color: var(--text);
}
.icon-btn:active, .btn:active { filter: brightness(1.2); }

.link-btn { background: none; border: 0; color: var(--accent); text-decoration: underline; padding: 4px; }

.check { display: flex; align-items: center; gap: 10px; min-height: 40px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

/* Lists ------------------------------------------------------------------- */

.list { list-style: none; margin: 0; padding: 0; }
.item {
  display: flex; align-items: center; gap: 4px;
  min-height: 60px;
  border-bottom: 1px solid var(--border);
}
.item-main {
  flex: 1; min-width: 0;
  display: flex; flex-direction: column; align-items: flex-start;
  padding: 8px 4px;
  background: none; border: 0; text-align: left;
}
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.item-sub { color: var(--muted); font-size: .85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.badge {
  min-width: 48px; padding: 4px 8px; text-align: center;
  border-radius: 999px; background: var(--surface-2);
  font-variant-numeric: tabular-nums; font-weight: 600; font-size: .9rem;
}
.badge.subtle { min-width: auto; color: var(--muted); background: none; }
.num { width: 28px; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.chev { color: var(--muted); font-size: 1.5rem; padding: 0 8px; }
.item.previewing .badge { background: var(--accent); color: var(--accent-text); }
.item.active-set .item-sub { color: var(--accent); }
.item.in-set .item-title { color: var(--muted); }
.list.compact .item { min-height: 48px; }
.item.current { background: var(--surface-2); border-radius: var(--radius); }
.item.current .num, .item.current .item-title { color: var(--accent); }
.item.done .item-title { color: var(--muted); }

.empty { color: var(--muted); text-align: center; padding: 32px 16px; }

/* Play -------------------------------------------------------------------- */

.play-head { display: flex; align-items: center; gap: 12px; padding: 12px 0; }
.position { font-variant-numeric: tabular-nums; color: var(--muted); font-weight: 600; white-space: nowrap; }

.song-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 20px 16px;
  text-align: center;
  user-select: none;
  -webkit-user-select: none;
}
.now-title { font-size: clamp(1.6rem, 7vw, 2.4rem); font-weight: 700; line-height: 1.15; overflow-wrap: anywhere; }
.now-artist { color: var(--muted); min-height: 1.4em; }
.now-meta { display: flex; justify-content: center; align-items: center; gap: 24px; margin-top: 12px; flex-wrap: wrap; }
.tempo { display: flex; align-items: center; gap: 10px; }
.bpm { font-size: clamp(2.6rem, 14vw, 4rem); font-weight: 800; font-variant-numeric: tabular-nums; line-height: 1; }
.bpm.changed { color: var(--accent); }
.unit { display: block; font-size: .75rem; color: var(--muted); letter-spacing: .1em; }
.nudge {
  width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--border); background: var(--surface-2); font-size: 1.4rem;
}
.sig { font-size: 2rem; font-weight: 700; padding: 4px 14px; border: 2px solid var(--border); border-radius: var(--radius); }
.now-notes { color: var(--muted); margin-top: 8px; white-space: pre-wrap; }
.now-notes:empty { display: none; }

.beats { display: flex; justify-content: center; flex-wrap: wrap; gap: 10px; margin-top: 16px; }
.dot { width: 22px; height: 22px; border-radius: 50%; background: var(--surface-2); border: 2px solid var(--border); transition: background 40ms; }
.dot.accent { border-color: var(--accent); }
.dot.lit { background: var(--go); border-color: var(--go); }
.dot.accent.lit { background: var(--accent); border-color: var(--accent); }

.transport { display: grid; grid-template-columns: 1fr 1.4fr 1fr; gap: 10px; margin-top: 16px; }
.btn.big { min-height: 72px; font-size: 1.3rem; font-weight: 700; }
.btn.play { background: var(--go); color: #fff; border-color: transparent; }
.btn.play.on { background: var(--stop); }
.up-next { color: var(--muted); text-align: center; margin: 12px 0 8px; font-size: .9rem; }

/* Dialogs ----------------------------------------------------------------- */

dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 48px);
  padding: 20px;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
}
dialog::backdrop { background: rgb(0 0 0 / .6); }
dialog h2 { margin: 0 0 12px; font-size: 1.2rem; }
dialog h3 { margin: 20px 0 8px; font-size: 1rem; color: var(--muted); }
dialog form { display: grid; gap: 12px; }
dialog label { display: grid; gap: 4px; font-size: .9rem; color: var(--muted); }
dialog label.check { display: flex; color: var(--text); font-size: 1rem; }
dialog label input, dialog label select, dialog label textarea { color: var(--text); }
.grid3 { display: grid; grid-template-columns: 2fr 1fr 1fr; gap: 8px; }
.dialog-actions { display: flex; gap: 8px; margin-top: 8px; }
.hint { color: var(--muted); font-size: .85rem; margin: 4px 0; }
.hint.error { color: var(--danger); white-space: pre-line; }
.help summary { cursor: pointer; color: var(--accent); font-size: .9rem; }
.help[open] summary { margin-bottom: 4px; }
ol.hint { padding-left: 20px; display: grid; gap: 4px; }
code { background: var(--surface-2); padding: 1px 6px; border-radius: 6px; }

dialog.full {
  width: 100vw; max-width: 100vw;
  height: 100dvh; max-height: 100dvh;
  margin: 0; border-radius: 0; border: 0;
  padding: env(safe-area-inset-top) max(16px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(16px, env(safe-area-inset-left));
}
.picker .toolbar { position: sticky; top: 0; z-index: 2; background: var(--surface); }
.picker h2 { margin: 0; }
.picker select { width: auto; }

/* Toast ------------------------------------------------------------------- */

.toast {
  position: fixed; inset: auto; margin: 0; border: 0; left: 50%; bottom: calc(var(--tab-h) + env(safe-area-inset-bottom) + 16px);
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  padding: 10px 16px; border-radius: 999px;
  background: var(--text); color: var(--bg);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
  z-index: 100;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

a { color: var(--accent); }

/* Muted looping video that keeps the screen on where Wake Lock isn't available. */
.keep-awake { position: fixed; left: 0; top: 0; width: 1px; height: 1px; opacity: .01; pointer-events: none; }

/* Landscape ---------------------------------------------------------------- */

/* Wide enough for two columns: song card + transport on the left (pinned),
   up-next and the set list on the right. */
@media (orientation: landscape) and (min-width: 640px) {
  main { max-width: 1100px; }
  #player {
    display: grid;
    grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr);
    gap: 16px;
    align-items: start;
  }
  .player-main { position: sticky; top: calc(env(safe-area-inset-top) + 64px); }
  .up-next { margin-top: 0; text-align: left; }
}

/* Phone on its side: move the tabs (and settings) into a left rail so the
   whole height goes to the song, and scale the card to the short screen. */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --rail-w: 72px; }

  .tabbar {
    top: 0; right: auto;
    width: calc(var(--rail-w) + env(safe-area-inset-left));
    height: auto;
    padding: 8px 0 0 env(safe-area-inset-left);
    grid-template-columns: 1fr;
    grid-auto-rows: 64px;
    align-content: start;
    border-top: 0;
    border-right: 1px solid var(--border);
  }
  .topbar {
    position: fixed; top: auto; left: 0; bottom: 0; z-index: 11;
    width: calc(var(--rail-w) + env(safe-area-inset-left));
    padding: 0 0 calc(env(safe-area-inset-bottom) + 8px) env(safe-area-inset-left);
    justify-content: center;
    background: transparent;
    border: 0;
  }
  .brand { display: none; }

  main {
    max-width: none;
    padding: 0 max(16px, env(safe-area-inset-right)) calc(env(safe-area-inset-bottom) + 16px)
      calc(var(--rail-w) + env(safe-area-inset-left) + 16px);
  }
  .toolbar.sticky { top: 0; }
  .toast { bottom: calc(env(safe-area-inset-bottom) + 16px); }

  .play-head { padding: 8px 0; }
  .player-main { top: 8px; }
  .song-card { padding: 12px 16px; }
  .now-title { font-size: clamp(1.3rem, 7vh, 2.2rem); }
  .bpm { font-size: clamp(2.4rem, 17vh, 4.5rem); }
  #player { grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); }
  .now-meta { margin-top: 6px; gap: 14px; flex-wrap: nowrap; }
  .nudge { width: 40px; height: 40px; }
  .sig { font-size: 1.5rem; padding: 2px 10px; }
  .beats { margin-top: 10px; }
  .now-notes { max-height: 3em; overflow: auto; }
  .transport { margin-top: 10px; }
  .btn.big { min-height: 56px; }
}
