:root {
  --bg: #070b14;
  --panel: #101828;
  --panel-2: #172238;
  --line: #24314c;
  --text: #eef3ff;
  --muted: #8b9bb9;
  --ice: #7cc4ff;
  --red: #e8343b;
  --gold: #f5b82e;
  --green: #22c06a;
  --purple: #8f63ff;
  --radius: 18px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
body { position: fixed; inset: 0; overflow: hidden; }

button {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
[hidden] { display: none !important; }

/* ---------- Game screen ---------- */
#game {
  height: 100%;
  display: grid;
  grid-template-areas: "now" "prog" "transport" "board";
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  gap: 12px;
  padding:
    calc(env(safe-area-inset-top) + 12px)
    calc(env(safe-area-inset-right) + 16px)
    calc(env(safe-area-inset-bottom) + 16px)
    calc(env(safe-area-inset-left) + 16px);
  user-select: none;
  -webkit-user-select: none;
}

.now { grid-area: now; display: flex; align-items: flex-start; gap: 12px; min-width: 0; }
.now-text { flex: 1; min-width: 0; }
.now-label {
  font-size: 12px; font-weight: 700; letter-spacing: .12em; text-transform: uppercase;
  color: var(--ice);
}
.now-label.live { color: var(--green); }
.now-title {
  font-size: clamp(20px, 6vw, 30px); font-weight: 800; line-height: 1.15;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.now-meta { font-size: 14px; color: var(--muted); margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.icon-btn {
  flex: none; width: 46px; height: 46px; border-radius: 14px;
  display: grid; place-items: center; background: var(--panel); color: var(--muted);
  border: 1px solid var(--line);
}

.progress { grid-area: prog; }
.progress-bar {
  height: 6px; border-radius: 3px; background: var(--panel-2); position: relative; overflow: hidden;
}
.progress-bar::after {
  content: ""; position: absolute; inset: 0; width: var(--p, 0%); background: var(--ice); border-radius: 3px;
}
.progress-bar.board-src::after { background: var(--gold); }
.progress-times {
  display: flex; justify-content: space-between; font-size: 12px; color: var(--muted);
  font-variant-numeric: tabular-nums; height: 16px; margin-top: 4px;
}

.transport {
  grid-area: transport;
  display: flex; align-items: center; justify-content: center;
  gap: clamp(10px, 3vw, 32px); min-height: 0;
}
.play {
  width: min(calc(100vw - 150px), 40dvh, 380px);
  aspect-ratio: 1; border-radius: 50%;
  display: grid; place-items: center;
  background: radial-gradient(circle at 35% 30%, #3aa0ff, #1660c9 70%);
  color: #fff;
  box-shadow: 0 0 0 6px rgba(124,196,255,.12), 0 18px 40px rgba(0,0,0,.5);
  transition: transform .08s ease, background .2s ease;
}
.play:active { transform: scale(.97); }
.play svg { width: 46%; height: 46%; }
.play .i-pause { display: none; }
.play.playing { background: radial-gradient(circle at 35% 30%, #35d985, #14894a 70%); box-shadow: 0 0 0 6px rgba(34,192,106,.18), 0 18px 40px rgba(0,0,0,.5); }
.play.playing .i-play { display: none; }
.play.playing .i-pause { display: block; }
.play.no-song { background: var(--panel-2); color: var(--muted); box-shadow: none; }

.skip {
  flex: none; width: 46px; height: 46px; border-radius: 50%;
  display: grid; place-items: center; color: var(--muted);
  background: var(--panel); border: 1px solid var(--line);
}
.skip svg { width: 24px; height: 24px; }
.skip:active { background: var(--panel-2); }

.board {
  grid-area: board;
  display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr;
  gap: 10px; height: min(30dvh, 280px);
}
.pad {
  position: relative; border-radius: var(--radius);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  padding: 8px; min-width: 0; overflow: hidden;
  color: #fff; text-align: center;
  box-shadow: inset 0 -4px 0 rgba(0,0,0,.25);
  transition: transform .08s ease, filter .15s ease;
}
.pad:active { transform: scale(.97); filter: brightness(1.15); }
.pad-name { font-weight: 800; font-size: clamp(16px, 4.6vw, 24px); letter-spacing: .02em; text-transform: uppercase; }
.pad-sub { font-size: 12px; opacity: .8; max-width: 100%; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pad-horn  { background: linear-gradient(160deg, #ff5258, #b81d24); }
.pad-pp    { background: linear-gradient(160deg, #ffc94d, #d48b00); color: #1d1300; }
.pad-organ { background: linear-gradient(160deg, #a37bff, #5a2fd1); }
.pad-inter { background: linear-gradient(160deg, #2f8f7a, #175c4d); }
.pad.unset { filter: saturate(.35) brightness(.7); }
.pad.active { box-shadow: 0 0 0 4px #fff, 0 0 26px rgba(255,255,255,.45); animation: pulse 1s ease-in-out infinite; }
.pad.active .pad-sub::before { content: "■ "; }
@keyframes pulse { 50% { filter: brightness(1.25); } }

/* Phone or tablet held sideways: soundboard on the right */
@media (orientation: landscape) {
  #game {
    grid-template-areas: "now board" "prog board" "transport board";
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr);
    column-gap: 20px;
  }
  .board { height: auto; }
  .play { width: min(36vw, calc(100dvh - 170px), 380px); }
}

/* ---------- Editor ---------- */
.editor {
  position: fixed; inset: 0; z-index: 10; background: var(--bg);
  display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
.ed-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; }
.ed-head h1 { margin: 0; font-size: 22px; }
.tabs { display: flex; gap: 6px; padding: 0 16px 10px; }
.tabs button {
  flex: 1; padding: 10px 6px; border-radius: 12px; background: var(--panel);
  color: var(--muted); font-weight: 600; font-size: 14px; border: 1px solid var(--line);
}
.tabs button[aria-selected="true"] { background: var(--ice); color: #04223f; border-color: var(--ice); }
.ed-body { flex: 1; overflow-y: auto; padding: 4px 16px 16px; -webkit-overflow-scrolling: touch; }
.ed-foot {
  padding: 10px 16px calc(env(safe-area-inset-bottom) + 10px);
  font-size: 12px; color: var(--muted); border-top: 1px solid var(--line);
}
.ed-hint { font-size: 13px; color: var(--muted); margin: 4px 0 12px; line-height: 1.4; }

.btn {
  padding: 12px 16px; border-radius: 12px; background: var(--panel-2); font-weight: 600; font-size: 15px;
  border: 1px solid var(--line);
}
.btn:active { filter: brightness(1.2); }
.btn-primary { background: var(--ice); color: #04223f; border-color: var(--ice); }
.btn-accent { background: var(--gold); color: #241800; border-color: var(--gold); }
.btn-danger { background: transparent; color: #ff7b80; border-color: #5a2430; }
.btn-block { width: 100%; }

.list { list-style: none; margin: 12px 0 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.row {
  display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 6px 6px 6px 12px;
}
.row.queued { border-color: var(--ice); }
.row-num { width: 22px; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; flex: none; }
.row-main { flex: 1; min-width: 0; text-align: left; padding: 6px 0; }
.row-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.chip { display: inline-block; font-size: 10px; font-weight: 800; letter-spacing: .08em; padding: 2px 6px; border-radius: 6px; background: var(--ice); color: #04223f; margin-right: 6px; vertical-align: 1px; }
.row-btn {
  flex: none; width: 40px; height: 40px; border-radius: 10px; display: grid; place-items: center;
  background: var(--panel-2); color: var(--muted);
}
.row-btn:disabled { opacity: .3; }
.empty { text-align: center; color: var(--muted); padding: 28px 12px; border: 1px dashed var(--line); border-radius: 14px; margin-top: 12px; }

.slot { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 14px; margin-bottom: 12px; }
.slot-title { font-weight: 800; text-transform: uppercase; letter-spacing: .06em; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.slot-dot { width: 10px; height: 10px; border-radius: 50%; }
.slot-file { margin: 8px 0 12px; color: var(--muted); font-size: 14px; }
.slot-file b { color: var(--text); font-weight: 600; }
.slot-actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- Song sheet ---------- */
.sheet-wrap {
  position: fixed; inset: 0; z-index: 20; background: rgba(0,0,0,.6);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 560px; background: var(--panel); border-radius: 22px 22px 0 0;
  padding: 18px 16px calc(env(safe-area-inset-bottom) + 16px);
  display: flex; flex-direction: column; gap: 12px; border-top: 1px solid var(--line);
}
.sh-name {
  width: 100%; font: inherit; font-size: 18px; font-weight: 700; color: var(--text);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
}
.sh-time { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; margin-bottom: -6px; }
#shScrub { width: 100%; accent-color: var(--ice); height: 28px; }
.sh-row { display: flex; gap: 8px; }
.sh-row .btn { flex: 1; }
.sh-pp { flex: 2 !important; }
.sh-start {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--panel-2); border-radius: 14px; padding: 10px 12px; border: 1px solid var(--line);
}
.sh-start-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.sh-start-input {
  width: 6.5ch; font: inherit; font-size: 24px; font-weight: 800; color: var(--gold);
  background: transparent; border: 0; padding: 0; font-variant-numeric: tabular-nums;
}
.sh-start-input:focus { outline: none; border-bottom: 2px solid var(--gold); }

.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); transform: translateX(-50%);
  background: #000c; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 30;
  max-width: calc(100% - 32px); text-align: center;
}

/* ---------- Download sheet ---------- */
.dl-title { margin: 0; font-size: 20px; }
.dl-text { margin: 0; color: var(--muted); font-size: 15px; line-height: 1.45; }
.ed-foot { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.ed-foot .btn { flex: none; padding: 8px 12px; font-size: 13px; }
.btn:disabled { opacity: .7; }
