@font-face {
  font-family: "Barlow";
  src: url("../fonts/barlow-400.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Barlow";
  src: url("../fonts/barlow-500.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Barlow";
  src: url("../fonts/barlow-600.woff2") format("woff2");
  font-weight: 600;
  font-style: normal;
  font-display: swap;
}
@font-face {
  font-family: "Barlow Condensed";
  src: url("../fonts/barlow-condensed-500.woff2") format("woff2");
  font-weight: 500;
  font-style: normal;
  font-display: swap;
}

:root {
  --bg: #111111;
  --panel: #1b1b1b;
  --panel-2: #242424;
  --inset: #0e0e0e;
  --stage: #161616;
  --line: #343434;
  --text: #ededed;
  --muted: #8d8d8d;
  --faint: #666666;
  --accent: #ff764d;
  --ink: #1a0e0a;
  --down: #ffc44d;
  --down-ink: #1a1203;
  --quarter: #5c3a1e;
  --sub: #2c261e;
  --count: #2a2118;
  --count-down: #ffb15a;
  --led-off: #2c2c2c;
  --danger: #e25b4a;
  color-scheme: dark;
}

html[data-theme="light"] {
  --bg: #cfcfcf;
  --panel: #e8e8e8;
  --panel-2: #f5f5f5;
  --inset: #f7f7f7;
  --stage: #f2f2f2;
  --line: #b3b3b3;
  --text: #1a1a1a;
  --muted: #555555;
  --faint: #6a6a6a;
  --accent: #ef5b2a;
  --ink: #1a0e0a;
  --down: #ffb703;
  --down-ink: #1a1203;
  --quarter: #f0c48a;
  --sub: #e6d5ba;
  --count: #f6e3d0;
  --count-down: #ffb703;
  --led-off: #cfcfcf;
  --danger: #c13b2e;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: "Barlow", "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 16px;
  line-height: 1.3;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button {
  cursor: pointer;
  touch-action: manipulation;
  border-radius: 0;
}
button:disabled { opacity: 0.35; cursor: default; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
input { border-radius: 0; }

.app {
  height: 100dvh;
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top"
    "banner"
    "stage"
    "list"
    "transport";
  background: var(--bg);
}
.topbar { grid-area: top; }
.banners { grid-area: banner; }
.performance {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--bg);
}
.list-pane {
  grid-area: list;
  min-height: 0;
  overflow: auto;
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.transport { grid-area: transport; }

.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  padding-top: calc(6px + env(safe-area-inset-top));
  min-height: calc(48px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: auto;
  min-width: 0;
}
.brand-mark {
  width: 14px;
  height: 14px;
  background: var(--accent);
  flex: none;
}
.brand-mark.is-down { background: var(--down); }
.brand-name {
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  font-size: 14px;
}
.mode-switch { display: flex; border: 1px solid var(--line); flex: none; }
.mode-switch button,
.ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  height: 32px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.mode-switch button { border: 0; }
.mode-switch button[aria-selected="true"] {
  background: var(--accent);
  color: var(--ink);
}
.ghost { display: inline-flex; align-items: center; gap: 6px; }
.share-icon { width: 14px; height: 14px; display: block; }

.banner {
  margin: 0;
  padding: 8px 12px;
  background: #3a2a12;
  color: #ffd48a;
  font-size: 14px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}
html[data-theme="light"] .banner { background: #ffe7c2; color: #5a3208; }
.banner[hidden] { display: none; }
.banner button {
  background: transparent;
  color: inherit;
  border: 1px solid currentColor;
  height: 32px;
  padding: 0 10px;
  flex: none;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-size: 12px;
  font-weight: 600;
}

.stage {
  position: relative;
  min-height: 30dvh;
  max-height: 40dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  padding: 32px 16px 12px;
  background: var(--stage);
  color: var(--text);
  border-bottom: 1px solid var(--line);
  overflow: hidden;
  user-select: none;
  transition: background-color 120ms linear, color 120ms linear;
}
body[data-mode="edit"] .stage { min-height: 20dvh; }
.stage.flash { transition: none; }
.stage.flash-down { background: var(--down); color: var(--down-ink); }
.stage.flash-quarter { background: var(--quarter); color: var(--text); }
.stage.flash-sub { background: var(--sub); color: var(--text); }
.stage.flash-count { background: var(--count); color: var(--text); box-shadow: inset 0 0 0 4px var(--accent); }
.stage.flash-count-down { background: var(--count-down); color: var(--down-ink); box-shadow: inset 0 0 0 6px var(--accent); }

.stage-top {
  position: absolute;
  top: 8px;
  left: 10px;
  right: 10px;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.75;
}
.beat-num {
  font-family: "Barlow Condensed", "Barlow", sans-serif;
  font-weight: 500;
  font-size: clamp(104px, 30vw, 260px);
  line-height: 0.8;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.03em;
}
body[data-mode="edit"] .beat-num { font-size: clamp(72px, 20vw, 150px); }
.beat-num.is-tempo { font-size: clamp(80px, 22vw, 200px); }
.beat-small {
  min-height: 1.1em;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.bar-label {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  max-width: 92%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.leds { display: flex; gap: 6px; margin-bottom: 4px; }
.led {
  width: 12px;
  height: 12px;
  background: var(--led-off);
  border: 1px solid var(--line);
}
.led.on { background: var(--accent); border-color: var(--accent); }
.led.down { background: var(--down); border-color: var(--down); }
.stage.flash-down .led { background: rgba(0, 0, 0, 0.22); border-color: transparent; }
.stage.flash-down .led.on,
.stage.flash-down .led.down { background: #1a1203; }

.now { padding: 8px 12px 4px; }
.now-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 10px; }
.now h1 {
  margin: 0;
  min-width: 0;
  font-size: clamp(22px, 5vw, 34px);
  font-weight: 500;
  letter-spacing: -0.02em;
  line-height: 1.05;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.np-meta {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.vol {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  flex: none;
}
.vol input { width: 88px; accent-color: var(--accent); }

.transport {
  padding: 8px 10px calc(8px + env(safe-area-inset-bottom));
  display: grid;
  gap: 8px;
  background: var(--bg);
  border-top: 1px solid var(--line);
}
.play {
  height: 64px;
  background: var(--accent);
  color: var(--ink);
  border: 1px solid var(--accent);
  font-size: 18px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}
.play.is-on {
  background: transparent;
  color: var(--accent);
}
.nav { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.nav button {
  height: 52px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--line);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.nav button:active, .play:active, .song-row:active { filter: brightness(1.12); }

.keys {
  display: none;
  margin: 0;
  padding: 8px 12px;
  color: var(--faint);
  font-size: 12px;
  letter-spacing: 0.04em;
}

.song-row {
  width: 100%;
  min-height: 64px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 8px 12px;
  background: var(--panel);
  border: 0;
  border-bottom: 1px solid var(--line);
  text-align: left;
}
.song-row.is-current { box-shadow: inset 3px 0 0 var(--accent); background: var(--panel-2); }
.idx {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.06em;
}
.row-title {
  font-size: 17px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.row-meta {
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  font-variant-numeric: tabular-nums;
}
.song-row.is-playing .idx { color: var(--accent); }

.card {
  padding: 10px 12px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.card.is-current { box-shadow: inset 3px 0 0 var(--accent); }
.card-top, .bpm-line, .chip-block { display: flex; align-items: center; gap: 6px; }
.card-top { margin-bottom: 8px; }
.title, .bpm {
  background: var(--inset);
  border: 1px solid var(--line);
  height: 40px;
  padding: 0 10px;
  font-size: 16px;
}
.title { flex: 1; min-width: 0; }
.bpm { width: 5.2rem; text-align: center; font-variant-numeric: tabular-nums; font-weight: 600; }
.card-ops { display: flex; gap: 4px; flex: none; }
.card-ops button, .step, .tap, .preview {
  background: var(--panel-2);
  border: 1px solid var(--line);
  height: 40px;
  min-width: 44px;
  padding: 0 8px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
}
.del { color: var(--danger); }
.bpm-line { margin-bottom: 8px; }
.lbl {
  color: var(--muted);
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  min-width: 52px;
}
.tap { margin-left: auto; letter-spacing: 0.1em; text-transform: uppercase; }
.chip-block { margin-bottom: 6px; align-items: stretch; }
.chips {
  display: flex;
  gap: 4px;
  flex: 1;
  min-width: 0;
  overflow-x: auto;
  scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto;
  height: 36px;
  padding: 0 10px;
  background: var(--inset);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--ink);
}
.preview { width: 100%; margin-top: 4px; letter-spacing: 0.12em; text-transform: uppercase; }
.preview.is-on { background: var(--accent); color: var(--ink); border-color: var(--accent); }

.add-row {
  position: sticky;
  bottom: 0;
  padding: 10px;
  background: var(--bg);
  border-top: 1px solid var(--line);
}
#add {
  width: 100%;
  height: 48px;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}
body[data-mode="run"] #editor,
body[data-mode="run"] #add-row,
body[data-mode="edit"] #runner { display: none; }

.toast {
  position: fixed;
  z-index: 5;
  top: calc(58px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  background: var(--down);
  color: var(--down-ink);
  padding: 8px 12px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.04em;
  max-width: calc(100% - 24px);
}
.toast[hidden] { display: none; }
.nosleep {
  position: fixed;
  width: 2px;
  height: 2px;
  right: 0;
  bottom: 0;
  opacity: 0;
  pointer-events: none;
}

@media (min-width: 900px) {
  .app {
    grid-template-columns: minmax(360px, 440px) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "top top"
      "banner banner"
      "list stage"
      "list transport";
  }
  .list-pane { border-top: 0; border-right: 1px solid var(--line); }
  .performance { height: 100%; min-height: 0; }
  .stage { flex: 1; min-height: 0; max-height: none; border-bottom: 0; }
  body[data-mode="edit"] .stage { min-height: 0; max-height: none; }
  .transport { padding-bottom: 16px; }
  .keys { display: block; }
  .beat-num { font-size: clamp(140px, 18vw, 280px); }
}

@media (max-width: 380px) {
  .share-label { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .mode-switch button, .ghost { padding: 0 8px; }
}

@media (max-height: 520px) and (orientation: landscape) {
  .stage, body[data-mode="edit"] .stage { min-height: 0; }
  .beat-num, body[data-mode="edit"] .beat-num { font-size: 72px; }
  .play { height: 48px; }
  .nav button { height: 40px; }
}

@media (prefers-reduced-motion: reduce) {
  .stage { transition: none; }
}
