:root {
  --bg: #0c0d11;
  --surface: #15171d;
  --surface-2: #1c1f27;
  --surface-3: #252934;
  --line: #262a34;
  --text: #eceef3;
  --muted: #8b91a1;
  --faint: #5b6172;
  --accent: #8f7dff;
  --accent-soft: rgba(143, 125, 255, 0.16);
  --play: #3fd99a;
  --play-soft: rgba(63, 217, 154, 0.15);
  --queued: #ffb547;
  --queued-soft: rgba(255, 181, 71, 0.16);
  --danger: #ff6b6b;
  --radius: 18px;
  --radius-sm: 10px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; border: 0; background: none; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 20px;
  padding: 12px max(20px, env(safe-area-inset-left));
  background: rgba(12, 13, 17, 0.85);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -0.01em; font-size: 17px; }
.transport { display: flex; align-items: center; gap: 18px; margin: 0 auto; }
.transport .field { flex-direction: row; align-items: center; gap: 10px; }
.transport .field label { margin: 0; }
.play {
  width: 52px; height: 52px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--accent); color: #fff;
  box-shadow: 0 6px 24px rgba(143, 125, 255, 0.35);
  transition: transform .12s, background .2s;
}
.play:active { transform: scale(.94); }
.play svg { width: 24px; height: 24px; fill: currentColor; }
.play .i-stop, .running .play .i-play { display: none; }
.running .play .i-stop { display: block; }
.running .play { background: var(--surface-3); box-shadow: none; }
.bar-count { display: flex; flex-direction: column; align-items: center; min-width: 36px; font-variant-numeric: tabular-nums; font-weight: 600; font-size: 18px; line-height: 1.1; }
.bar-count small { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-weight: 500; }
.master { display: flex; align-items: center; gap: 8px; color: var(--muted); }
.master svg { width: 20px; height: 20px; fill: currentColor; flex: none; }
.master input { width: 110px; }

/* ---------- Layout ---------- */
.grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.15fr);
  gap: 16px;
  padding: 16px max(20px, env(safe-area-inset-left)) 8px;
  max-width: 1280px; margin: 0 auto;
}
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px 20px 20px;
  display: flex; flex-direction: column; gap: 18px;
  min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; font-size: 13px; font-weight: 600; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.head-tools { display: flex; align-items: center; gap: 10px; }

.field { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.field label { font-size: 12px; color: var(--muted); font-weight: 500; display: flex; gap: 8px; align-items: baseline; }
.field label small { color: var(--faint); font-weight: 400; }
.field output { margin-left: auto; color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- Controls ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: var(--radius-sm); padding: 3px; gap: 2px; }
.seg button { padding: 7px 12px; border-radius: 8px; color: var(--muted); font-size: 13px; font-weight: 500; min-width: 40px; transition: background .15s, color .15s; }
.seg button:hover { color: var(--text); }
.seg button.on { background: var(--surface-3); color: var(--text); box-shadow: 0 1px 0 rgba(255,255,255,.04) inset; }

select {
  background: var(--surface-2); border: 0; border-radius: 8px; padding: 6px 28px 6px 10px; font-size: 13px;
  appearance: none; -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238b91a1' stroke-width='1.5' fill='none'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}

input[type=range] {
  margin: 0;
  -webkit-appearance: none; appearance: none; background: transparent; height: 24px; width: 100%; cursor: pointer;
  --pct: 50%;
}
input[type=range]::-webkit-slider-runnable-track {
  height: 4px; border-radius: 4px;
  background: linear-gradient(to right, var(--accent) var(--pct), var(--surface-3) var(--pct));
}
input[type=range]::-moz-range-track { height: 4px; border-radius: 4px; background: var(--surface-3); }
input[type=range]::-moz-range-progress { height: 4px; border-radius: 4px; background: var(--accent); }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: #fff; margin-top: -6px;
  box-shadow: 0 1px 4px rgba(0,0,0,.4);
}
input[type=range]::-moz-range-thumb { width: 16px; height: 16px; border: 0; border-radius: 50%; background: #fff; }
input.mini { width: 84px; }

.btn, .ghost {
  padding: 7px 12px; border-radius: 9px; font-size: 13px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.btn { background: var(--surface-2); }
.btn:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent); color: #fff; }
.ghost { color: var(--muted); }
.ghost:hover { color: var(--text); background: var(--surface-2); }

.round {
  width: 40px; height: 40px; border-radius: 50%; flex: none;
  background: var(--surface-2); font-size: 20px; line-height: 1;
}
.round:hover { background: var(--surface-3); }
.round:active, .tap:active { transform: scale(.94); }

/* ---------- Metronome ---------- */
.dial-wrap { position: relative; width: min(100%, 300px); aspect-ratio: 1; margin: 0 auto; }
.dial { width: 100%; height: 100%; display: block; }
.dial .seg-arc { fill: none; stroke: var(--surface-3); stroke-linecap: round; transition: stroke .25s ease-out; }
.dial .seg-arc.accent { stroke: #343948; }
.dial .seg-arc.mute { stroke: var(--surface-2); stroke-dasharray: 2 6; }
.dial .seg-arc.hit { stroke: var(--accent); transition: none; }
.dial .seg-arc.hit.accent { stroke: #fff; }
.dial-center {
  position: absolute; inset: 22%; border-radius: 50%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 35%, #1f2230, var(--surface-2));
  cursor: ns-resize; user-select: none; touch-action: none;
  transition: transform .08s;
}
.dial-center.pulse { transform: scale(1.035); }
.bpm { font-size: clamp(48px, 9vw, 68px); font-weight: 700; letter-spacing: -0.04em; line-height: 1; font-variant-numeric: tabular-nums; }
.bpm-label { margin-top: 6px; font-size: 12px; color: var(--muted); letter-spacing: .04em; }

.tempo-row { display: flex; align-items: center; gap: 12px; }
.tap {
  height: 40px; padding: 0 18px; border-radius: 20px; flex: none;
  background: var(--accent-soft); color: var(--accent); font-weight: 600;
}
.tap.flash { background: var(--accent); color: #fff; }

.metro-opts { display: flex; gap: 20px; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; }
.stepper { display: inline-flex; align-items: center; background: var(--surface-2); border-radius: var(--radius-sm); padding: 3px; }
.stepper button { width: 34px; height: 32px; border-radius: 8px; font-size: 18px; }
.stepper button:hover { background: var(--surface-3); }
.stepper span { min-width: 44px; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }

.accents { display: flex; gap: 6px; height: 58px; }
.accents button {
  flex: 1; min-width: 0; border-radius: 8px; background: var(--surface-2);
  display: flex; flex-direction: column; justify-content: flex-end; align-items: center; padding: 6px 0 5px; gap: 4px;
  position: relative;
}
.accents button i { display: block; width: 60%; max-width: 26px; border-radius: 3px; background: var(--faint); height: 40%; transition: background .2s; }
.accents button[data-a="2"] i { height: 72%; background: var(--text); }
.accents button[data-a="0"] i { height: 3px; background: var(--surface-3); }
.accents button span { font-size: 10px; color: var(--faint); font-variant-numeric: tabular-nums; }
.accents button.hit i { background: var(--accent); transition: none; }

/* ---------- Pads ---------- */
.keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.key {
  position: relative; aspect-ratio: 1.45; border-radius: 14px;
  background: var(--surface-2); border: 1px solid transparent;
  font-size: clamp(20px, 3vw, 28px); font-weight: 600; letter-spacing: -0.02em;
  display: grid; place-items: center; user-select: none; touch-action: none;
  transition: background .25s, border-color .25s, transform .08s;
}
.key:hover { background: var(--surface-3); }
.key:active { transform: scale(.97); }
.key small { position: absolute; left: 10px; bottom: 7px; font-size: 10px; font-weight: 500; color: var(--faint); letter-spacing: .04em; }
.key.on {
  background: linear-gradient(160deg, rgba(143,125,255,.38), rgba(143,125,255,.12));
  border-color: rgba(143,125,255,.6);
  box-shadow: 0 0 32px rgba(143,125,255,.25);
}
.key.on::after {
  content: ""; position: absolute; right: 10px; top: 10px; width: 7px; height: 7px; border-radius: 50%;
  background: var(--accent); animation: breathe 2.4s ease-in-out infinite;
}
.key.queued { border-color: var(--queued); animation: blink .5s steps(2) infinite; }
.pad-opts { grid-auto-flow: row dense; }
.pad-opts .wide { grid-column: 1 / -1; }
.pad-opts .wide .seg { display: flex; }
.pad-opts .wide .seg button { flex: 1; }
.loading { margin-left: 8px; font-weight: 500; letter-spacing: 0; text-transform: none; color: var(--queued); }
.loading[hidden] { display: none; }
.credit { margin: 0; font-size: 10.5px; color: var(--faint); }
.credit a { color: inherit; }
.pad-opts { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 16px 20px; }

/* ---------- Clips ---------- */
.clip-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.clip-grid:empty { display: none; }
.clip {
  position: relative; overflow: hidden; border-radius: 14px; min-height: 92px;
  background: var(--surface-2); border: 1px solid transparent; text-align: left;
  padding: 12px 12px 10px; display: flex; flex-direction: column; justify-content: space-between; gap: 8px;
  user-select: none;
}
.clip:hover { background: var(--surface-3); }
.clip .name { font-weight: 600; font-size: 14px; line-height: 1.25; word-break: break-word; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.clip .meta { display: flex; align-items: center; justify-content: space-between; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.clip .tools { display: flex; gap: 2px; }
.clip .tools span { padding: 3px 6px; border-radius: 6px; color: var(--faint); font-size: 12px; }
.clip .tools span:hover { background: rgba(255,255,255,.06); color: var(--text); }
.clip .tools .loop.on { color: var(--play); }
.clip .prog { position: absolute; left: 0; bottom: 0; height: 3px; background: var(--play); width: 0; }
.clip.playing { background: var(--play-soft); border-color: rgba(63,217,154,.5); }
.clip.queued { background: var(--queued-soft); border-color: var(--queued); animation: blink .5s steps(2) infinite; }
.clips.drag { border-color: var(--accent); background: var(--accent-soft); }
.empty { margin: 0; color: var(--faint); font-size: 13px; text-align: center; padding: 18px 12px; border: 1px dashed var(--line); border-radius: 14px; }

/* ---------- Setlist ---------- */
.song-form { display: flex; gap: 8px; }
.song-form[hidden] { display: none; }
.song-form input { flex: 1; min-width: 0; background: var(--surface-2); border: 1px solid var(--line); border-radius: 9px; padding: 7px 10px; }
.songs { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; counter-reset: song; }
.songs:empty { display: none; }
.song {
  display: flex; align-items: center; gap: 12px; padding: 10px 10px 10px 14px; border-radius: 12px;
  background: var(--surface-2); border: 1px solid transparent; cursor: pointer; counter-increment: song;
}
.song::before { content: counter(song); color: var(--faint); font-size: 12px; width: 14px; font-variant-numeric: tabular-nums; }
.song:hover { background: var(--surface-3); }
.song .title { flex: 1; min-width: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.song .info { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.song .del { color: var(--faint); padding: 2px 8px; border-radius: 6px; }
.song .del:hover { color: var(--danger); background: rgba(255,107,107,.1); }
.song.current { border-color: rgba(143,125,255,.5); background: var(--accent-soft); }
.song.queued { border-color: var(--queued); animation: blink .5s steps(2) infinite; }

/* ---------- Footer ---------- */
.foot { display: flex; gap: 18px; justify-content: center; flex-wrap: wrap; padding: 14px 20px 28px; color: var(--faint); font-size: 12px; }
kbd { font: 11px Inter, sans-serif; padding: 2px 6px; border-radius: 5px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted); margin-right: 4px; }

@keyframes blink { 50% { border-color: transparent; } }
@keyframes breathe { 50% { opacity: .3; } }

/* ---------- Single-screen landscape (iPad landscape, laptops) ----------
   Three columns that fill the viewport: metronome | pads | cues over setlist.
   Nothing scrolls except the cue and setlist lists inside their cards. */
@media (min-width: 1000px) and (min-height: 600px) and (min-aspect-ratio: 5/4) {
  body { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
  .topbar { position: static; flex: none; padding-top: 8px; padding-bottom: 8px; }
  .play { width: 46px; height: 46px; }
  .foot { display: none; }
  .grid {
    flex: 1; min-height: 0; width: 100%; max-width: 1600px;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.25fr) minmax(0, 0.95fr);
    grid-template-rows: minmax(0, 1fr) minmax(0, 1fr);
    gap: 12px; padding: 12px max(16px, env(safe-area-inset-left)) max(12px, env(safe-area-inset-bottom));
  }
  .card { min-height: 0; padding: 14px 16px; gap: 12px; overflow: hidden; }
  .metro { grid-column: 1; grid-row: 1 / 3; justify-content: space-between; }
  .pads { grid-column: 2; grid-row: 1 / 3; }
  .clips { grid-column: 3; grid-row: 1; }
  .setlist { grid-column: 3; grid-row: 2; }

  /* Dial shrinks with screen height so the controls below always fit. */
  .dial-wrap { width: min(100%, 320px, calc(100dvh - 420px)); min-width: 150px; container-type: inline-size; }
  .bpm { font-size: 24cqi; }
  .bpm-label { font-size: max(10px, 4.4cqi); margin-top: 2px; }
  .metro-opts { gap: 12px; flex-wrap: nowrap; }
  .metro-opts .seg button { min-width: 32px; padding: 6px 8px; }
  .stepper span { min-width: 36px; }
  .accents { height: 50px; }

  /* Keys stretch to fill whatever height the pad card has left. */
  .keys { flex: 1; min-height: 0; grid-auto-rows: minmax(0, 1fr); gap: 8px; }
  .key { aspect-ratio: auto; min-height: 44px; }
  .pad-opts { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px 16px; flex: none; }
  .pad-opts .seg { display: flex; }
  .pad-opts .seg button { flex: 1; padding: 6px 4px; min-width: 0; }

  .clip-grid, .songs { flex: 1; min-height: 0; overflow-y: auto; align-content: start; overscroll-behavior: contain; }
  .clip-grid { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
  .clip { min-height: 74px; padding: 10px; }
  .empty { padding: 12px 10px; font-size: 12px; }
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .transport .field label { display: none; }
  .foot { display: none; }
}
@media (max-width: 600px) {
  .topbar { flex-wrap: wrap; gap: 10px 14px; padding: 10px 16px; }
  .brand { font-size: 15px; }
  .transport { order: 3; width: 100%; justify-content: space-between; margin: 0; }
  .master input { width: 80px; }
  .master { margin-left: auto; }
  .grid { padding: 12px 16px; gap: 12px; }
  .card { padding: 16px; gap: 16px; }
  .keys { grid-template-columns: repeat(3, 1fr); gap: 8px; }
  .tempo-row { gap: 8px; }
  .tap { padding: 0 14px; }
  input.mini { width: 64px; }
}
