/* ---------- App tokens ---------- */
:root {
  /* Palette lives in global-style.css */
  /* spacing scale for quick tweaks */
  --space-1: 4px;
  --space-2: 6px;
  --space-3: 8px;
  --space-4: 12px;

  /* viewport metrics (updated via JS on touch devices) */
  --app-vh: 100vh;
  --hud-height: 0px;
  --stage-available: 100vh;
  --keyboard-h: 128px;
  --stage-pad: var(--space-4);
  --stage-gap: var(--space-4);
}

@supports (height: 100svh) {
  :root {
    --app-vh: 100svh;
    --stage-available: 100svh;
  }
}

@supports (height: 100dvh) {
  :root {
    --app-vh: 100dvh;
    --stage-available: 100dvh;
  }
}

:root[data-active-player="1"] {
  --accent: var(--teal-600);
}

:root[data-active-player="2"] {
  --accent: var(--gold-900);
}

/* ---------- Resets ---------- */
* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--teal-100);
  color: var(--mist-050);
  font: 16px/1.4 system-ui, Segoe UI, Roboto, Helvetica, Arial;
}

body {
  min-height: var(--app-vh);
  display: flex;
  flex-direction: column;
}

/* ---------- Layout regions ---------- */
.nav-left{
  display: flex;
  align-items: center;
  padding: 12px;
}

.logo img {
  width: 40px;
  height: 40px;
}

#stage {
  display: flex;
  flex-direction: column;
  gap: var(--stage-gap);
  padding: var(--stage-pad);
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  max-height: var(--stage-available, var(--app-vh));
  overflow: auto;
  padding-bottom: calc(var(--stage-pad) + env(safe-area-inset-bottom, 0px));
}

#hud {
  position: sticky;
  top: 0;
  padding: var(--space-4);
  background: linear-gradient(180deg, var(--fx-midnight-veil-strong), var(--fx-midnight-veil-soft) 60%, transparent);
  z-index: 1;
}

.hud-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
}

/* ---------- Pane look (shared surface) ----------
   Apply the same surface look to HUD boxes, control row, and piano-roll.
   Then override per-component differences below. */
.hud-box,
.controls-row,
#piano-roll {
  background: var(--fx-glass);
  backdrop-filter: blur(6px);
  border: 1px solid var(--neutral-300);
  border-radius: 12px;
}

/* HUD boxes: small, flexible chips */
.hud-box {
  position: relative;
  width: fit-content;
  display: flex;
  padding: var(--space-3) var(--space-4);
  align-items: center;
  gap: var(--space-1);
}

.hud-box.chord {
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-2);
}

.hud-box.chord .instrument-select {
  width: 100%;
  padding: 2px 6px;
  border-radius: 6px;
  border: 1px solid var(--fx-shadow-strong);
  background: var(--fx-ink-mid);
  color: inherit;
}

.hud-box.chord .chord-labels {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  width: 100%;
}

.hud-box.mic {
  gap: var(--space-3);
}

.hud-box.mic .ctl {
  min-width: 110px;
}

.hud-box.mic .mic-readout {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: var(--space-1);
}

.hud-box.mic .mic-note {
  font-size: 1.1rem;
  font-weight: 700;
  min-width: 2.4ch;
}

.hud-box .info-tip,
#piano-roll>.info-tip,
#keyboard .info-tip {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 120;
}

#piano-roll>.info-tip {
  right: 8px;
  left: auto;
  transform: none;
}

.info-tip {
  max-width: 240px;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--fx-blue-pop);
  color: var(--mist-050);
  box-shadow: 0 12px 24px var(--fx-shadow-strong);
  backdrop-filter: blur(6px);
  display: flex;
  gap: 10px;
  pointer-events: auto;
  z-index: 20;
}

.info-anchor {
  position: absolute;
  top: 8px;
  left: 8px;
  width: auto;
  height: 0;
  pointer-events: none;
  z-index: 1;
}

.info-anchor--pattern .info-tip {
  top: 8px;
  left: 8px;
  right: auto;
  transform: none;
}

.info-tip.hidden {
  display: none;
}

.info-tip__text {
  margin: 0;
  font-size: 0.78rem;
  line-height: 1.35;
}

.info-tip__close {
  appearance: none;
  border: none;
  background: transparent;
  color: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  padding: 0;
  align-self: flex-start;
}

.info-tip__close:hover,
.info-tip__close:focus-visible {
  color: var(--accent, var(--teal-600));
}

.info-toggle {
  align-self: center;
  padding-inline: 14px;
  background-color: var(--blue-300) !important;
}

.info-toggle.active {
  background-color: var(--red-300) !important;
  /* color: var(--red-300); */
}

.hud-box .box-heading {
  font-weight: 600;
  font-size: 0.95rem;
}



.hud-box.arranger .rows>.hud-box {
  justify-content: center;
  margin-inline: auto;
}

.hud-box.arranger:not(.collapsed)>.box-heading {
  display: none;
}

.hud-box.arranger .rows>.hud-box .ctl-group {
  display: flex;
  /* flex-direction: column; */
  align-items: center;
  gap: var(--space-2);
}

.hud-box.arranger .rows>.hud-box .ctl-group label {
  text-align: center;
  font-weight: 600;
}

.hud-box.collapsed .rows {
  display: none;
}

/* Score box: compact vs expanded */
.hud-box.score {
  min-width: 220px;
  justify-content: space-evenly;
}

.hud-box.score:not(.collapsed) .value {
  display: none;
}

/* by default hide P2 compact values; show only if P2 enabled AND box collapsed */
#score2,
#rounds2 {
  display: none;
}

.hud-box.score.collapsed #score2,
.hud-box.score.collapsed #rounds2 {
  display: none;
}

body.player2-enabled .hud-box.score.collapsed #score2,
body.player2-enabled .hud-box.score.collapsed #rounds2 {
  display: inline-block;
}

.hud-box.score .players {
  display: grid;
  gap: .5rem;
  margin-top: .5rem;
}

.hud-box .value {
  font-size: 1rem;
  font-weight: 700;
}

/* Arranger/Mixer toggle anchors */
.hud-box.arranger,
.hud-box.mixer {
  position: relative;
}

/* Mixer internals */
.hud-box.mixer .mix {
  display: grid;
  gap: var(--space-2);
}

.hud-box.mixer .row {
  display: grid;
  grid-template-columns: 70px 1fr auto;
  align-items: center;
  gap: var(--space-3);
}

.hud-box.mixer input[type="range"] {
  width: 100%;
}

.hud-box.mixer .mutebtn {
  padding: 4px 8px;
}

.hud-box.mixer .mutebtn.muted {
  opacity: .6;
}

/* ---------- Buttons ---------- */
.ctl,
.mixer-toggle {
  appearance: none;
  border: 1px solid var(--neutral-300);
  background: var(--neutral-500);
  color: var(--mist-050);
  border-radius: 10px;
  font-weight: 600;
  cursor: pointer;
}

.ctl {
  padding: var(--space-3) var(--space-4);
}

.ctl:disabled {
  opacity: .5;
  cursor: not-allowed;
}

.mixer-toggle {
  position: absolute;
  right: -8px;
  bottom: -8px;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  color: var(--mist-050);
  font-weight: 800;
  line-height: 1;
  display: grid;
  place-items: center;
  background-color: var(--blue-300) !important;
  z-index: 3;
}

/* Controls bar */
.controls-row {
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
}

/* Transport status label */
.status {
  display: flex;
  align-items: center;
  margin-left: auto;
  opacity: .8;
}

/* ---------- Score delta badge ---------- */
.delta {
  margin-left: var(--space-3);
  font-weight: 700;
  transition: transform .2s, opacity .2s;
}

.delta.show {
  opacity: 1;
  transform: translateY(-4px);
}

.delta.hidden {
  opacity: 0;
}

.delta.positive {
  color: var(--green-300);
}

.delta.negative {
  color: var(--red-300);
}

/* ---------- Beat/Chord HUD ---------- */
#beat-label {
  color: var(--green-300);
  font-size: 1.4em;
  font-weight: 700;
}

#curr {
  color: var(--green-300);
}

/* ---------- A11y ---------- */
.sr-only {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* ---------- Piano-roll surface ---------- */
#piano-roll {
  position: relative;
  flex: 1 1 auto;
  min-height: max(180px,
      min(40vh,
        calc(var(--stage-available, var(--app-vh)) - (var(--keyboard-h) + 2 * var(--stage-pad) + var(--stage-gap) + env(safe-area-inset-bottom, 0px)))));
  border-radius: 14px;
  /* preserve larger corner here */
  overflow: hidden;
  background: var(--neutral-500);
  border: 1px solid var(--neutral-300);
  /* keep edge line */
}

#roll-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 1;
}

/* Click zone overlay */
#click-zone {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: calc(var(--keyboard-h) - 8px);
  /* runtime adjusted to align with keyboard */
  background: transparent;
  outline: 2px dashed var(--fx-outline-mid);
  pointer-events: none;
  z-index: 4;

  /* center content (single row of pills + label) */
  display: flex;
  align-items: center;
  justify-content: center;
}

#click-zone .cz-pills {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 3;
}

#click-zone .cz-pill {
  position: absolute;
  top: 50%;
  bottom: auto;
  margin: 0;
  height: 14px;
  width: 14px;
  /* set inline as well for centering */
  border-radius: 50%;
  background: var(--fx-teal-flash);
  box-shadow: 0 0 0 1px var(--fx-shadow-subtle) inset;
  transform: translateY(-50%);
}

#click-zone .cz-pill--chromatic {
  background: var(--fx-gold-flash);
}

#click-zone .cz-label {
  background: var(--fx-ink-soft);
  padding: 4px 8px;
  border-radius: 8px;
  font-size: .8rem;
  color: var(--mist-400);
}

/* ---------- Keyboard ---------- */
#keyboard {
  margin-top: auto;
  position: relative;
  display: grid;
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(0, auto);
  gap: var(--space-2);
}

.piano {
  position: relative;
  height: var(--keyboard-h);
  user-select: none;
}

.keys-white {
  position: absolute;
  inset: 0;
  display: flex;
}

.keys-black {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

/* shared key traits */
.key.white,
.key.black {
  border-radius: 6px;
  position: relative;
  cursor: pointer;
}

/* white keys */
.key.white {
  flex: 1;
  background: var(--mist-050);
  border: 1px solid var(--mist-200);
  border-bottom: 6px solid var(--mist-200);
  margin: 0 1px;
}

.key.white.active {
  background: var(--blue-100);
  border-bottom-color: var(--mist-300);
}

.key.white.key-match {
  background: var(--green-100);
  border-color: var(--green-300);
  border-bottom-color: var(--green-600);
}

.key.white.key-neutral {
  background: var(--gold-100);
  border-color: var(--gold-300);
  border-bottom-color: var(--gold-600);
}

.key.white.key-miss {
  background: var(--red-50);
  border-color: var(--red-300);
  border-bottom-color: var(--red-300);
}

/* black keys */
.key.black {
  background: var(--neutral-700);
  border: 1px solid var(--neutral-300);
  pointer-events: auto;
  /* overlay above whites */
  z-index: 2;
}

.key.black.active {
  background: var(--mist-400);
}

.key.black.key-match {
  background: var(--green-900);
  border-color: var(--teal-900);
}

.key.black.key-neutral {
  background: var(--brown-200);
  border-color: var(--brown-900);
}

.key.black.key-miss {
  background: var(--red-300);
  border-color: var(--red-900);
}

/* key labels */
.key label {
  position: absolute;
  bottom: 6px;
  left: 3px;
  font-size: .7rem;
  color: var(--fx-ink-strong);
}

.key.black label {
  color: var(--fx-white-soft);
}

#key-select {
  font-size: 1rem;
  padding: 6px 12px;
  /* min-width: 120px; */
  border-radius: 8px;
  border: 1px solid var(--neutral-300);
  background: var(--neutral-500);
  color: var(--mist-050);
}

.key-bpm-container {
  display: flex;
  flex-direction: row;
}

/* ---------- Score flash (global target) ---------- */
.flash--pos {
  animation: scoreFlashPos 350ms ease;
}

.flash--neg {
  animation: scoreFlashNeg 350ms ease;
}

#score,
#score2 {
  border-radius: 6px;
  padding: 0 4px;
}

@keyframes scoreFlashPos {
  0% {
    box-shadow: 0 0 0 transparent;
    background-color: transparent;
  }

  20% {
    box-shadow: 0 0 18px var(--fx-teal-burst);
    background-color: var(--fx-teal-wash);
  }

  100% {
    box-shadow: 0 0 0 transparent;
    background-color: transparent;
  }
}

@keyframes scoreFlashNeg {
  0% {
    box-shadow: 0 0 0 transparent;
    background-color: transparent;
  }

  20% {
    box-shadow: 0 0 18px var(--fx-red-burst);
    background-color: var(--fx-red-wash);
  }

  100% {
    box-shadow: 0 0 0 transparent;
    background-color: transparent;
  }
}

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .hud-row {
    display: flex;
    flex-wrap: wrap;
  }
}

/* ================== iPad/mobile layout tweaks ================== */
/* On short screens, don't force the keyboard to the bottom so the HUD stays visible. */
@media (max-height: 740px) {
  :root {
    --stage-gap: var(--space-3);
    --stage-pad: var(--space-3);
    --keyboard-h: 112px;
  }

  #keyboard {
    margin-top: 0;
  }

  /* let it flow in normal document order */
}

/* On narrow screens, reduce paddings/gaps a bit to gain vertical space */
@media (max-width: 900px) {
  :root {
    --stage-gap: var(--space-3);
    --stage-pad: var(--space-3);
  }

  #hud {
    padding: var(--space-3);
  }

  .controls-row {
    padding: var(--space-2) var(--space-3);
    gap: var(--space-2);
  }

  .hud-row {
    gap: var(--space-2);
  }

  .hud-box {
    padding: var(--space-2) var(--space-3);
  }

  .hud-box.score {
    min-width: 200px;
  }

  /* slightly smaller compact score */
  .hud-box.arranger .rows {
    width: 100%;
  }

  .hud-box.arranger .rows>.hud-box {
    width: min(260px, 100%);
  }

  #key-select {
    width: 100%;
    font-size: 1.05rem;
  }
}

/* If the header still steals too much vertical space on very short screens,
   disable sticky and let the page scroll naturally. */
@media (max-height: 640px) {
  #hud {
    position: static;
  }

  /* header scrolls with content */
}

/* On phone-sized screens hide the piano roll and lower manual to save space */
@media (max-height: 640px) {
  :root {
    --stage-gap: var(--space-2);
    --stage-pad: var(--space-2);
  }

  #piano-roll {
    display: none;
  }

  #keyboard .piano[data-row="lower"] {
    display: none;
  }
}
