:root {
  --bg: #1a1d24;
  --panel: #252a33;
  --text: #e8eaef;
  --muted: #8b93a5;
  --accent: #c9a227;
  --bar: #e6c02a;
  --bar-text: #1a1a1a;
  --hit-line: #f5f0ff;
  --white-key: #f4f2ee;
  --white-border: #c8c4bc;
  --black-key: #1c1e22;
  --key-active: #7cb87c;
  --error-bg: #4a2020;
  --error-text: #ffb4b4;
  --font: "Segoe UI", system-ui, sans-serif;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  min-height: 100dvh;
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.app-shell {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-height: 100dvh;
  min-height: -webkit-fill-available;
  width: 100%;
}

/* Centered 4:3 frame only on typical desktops (hover). Tablets/phones use full width. */
@media (min-width: 1024px) and (hover: hover) {
  body {
    align-items: center;
    justify-content: center;
    padding: max(0.5rem, env(safe-area-inset-top)) max(0.5rem, env(safe-area-inset-right)) max(0.5rem, env(safe-area-inset-bottom))
      max(0.5rem, env(safe-area-inset-left));
  }

  /* Whole app (toolbar + content) in the largest 4:3 that fits the viewport */
  .app-shell {
    flex: 0 0 auto;
    aspect-ratio: 4 / 3;
    width: min(calc(100vw - 1rem), calc((100dvh - 1rem) * 4 / 3));
    max-width: calc(100vw - 1rem);
    max-height: calc(100dvh - 1rem);
    min-height: 0;
    height: auto;
    overflow: hidden;
    border-radius: 12px;
    box-shadow: 0 8px 40px rgba(0, 0, 0, 0.35);
  }
}

.toolbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.75rem 1.25rem;
  padding: 0.75rem 1rem;
  background: var(--panel);
  border-bottom: 1px solid #333842;
  flex-shrink: 0;
}

.portrait-prompt {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20000;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: max(1.5rem, env(safe-area-inset-top)) max(1.5rem, env(safe-area-inset-right))
    max(1.5rem, env(safe-area-inset-bottom)) max(1.5rem, env(safe-area-inset-left));
  text-align: center;
  gap: 0.75rem;
  background: var(--bg);
  color: var(--text);
}

.portrait-prompt-icon {
  color: var(--accent);
  opacity: 0.9;
}

.portrait-prompt-title {
  margin: 0;
  font-size: 1.25rem;
  font-weight: 600;
}

.portrait-prompt-text {
  margin: 0;
  font-size: 0.95rem;
  line-height: 1.45;
  color: var(--muted);
  max-width: 22rem;
}

@media (orientation: portrait) and (max-width: 1024px) {
  .portrait-prompt {
    display: flex;
  }

  .app-shell {
    display: none;
  }
}

.toolbar-left {
  justify-self: start;
  flex-shrink: 0;
}

.toolbar-center {
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  justify-content: center;
  gap: 0.5rem 0.75rem;
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

.toolbar-right {
  justify-self: end;
  justify-content: flex-end;
  text-align: right;
  flex-shrink: 0;
}

.toolbar-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.75rem;
}

.btn {
  font: inherit;
  padding: 0.35rem 0.75rem;
  border-radius: 6px;
  border: 1px solid #4a5160;
  background: #363d4a;
  color: var(--text);
  cursor: pointer;
}

a.btn {
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.btn:hover {
  background: #424a5a;
}

.btn.primary {
  background: #3d5a80;
  border-color: #4a6fa5;
}

.btn.primary:hover {
  background: #4a7ab0;
}

.inline {
  font-size: 0.875rem;
  color: var(--muted);
}

.inline.check {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  cursor: pointer;
  color: var(--text);
}

.muted {
  font-size: 0.8rem;
  color: var(--muted);
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.song-name-label {
  max-width: min(24rem, 42vw);
}

.bpm-readout {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  min-width: 4.25rem;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.35rem 0.5rem;
  min-width: 2.25rem;
}

.btn-icon svg {
  display: block;
  width: 1.15rem;
  height: 1.15rem;
}

#btn-play.btn-icon svg {
  width: 1.25rem;
  height: 1.25rem;
}

/* Only the active control icon is shown (play when idle, pause while playing). */
#btn-play.btn-icon .btn-play-icon-pause {
  display: none;
}

#btn-play.btn-icon.is-playing .btn-play-icon-play {
  display: none;
}

#btn-play.btn-icon.is-playing .btn-play-icon-pause {
  display: block;
}

.section-nav-group {
  display: flex;
  align-items: center;
}

.section-readout-box {
  box-sizing: border-box;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.35rem 0.5rem;
  min-height: 2.05rem;
  padding: 0.35rem 0.55rem 0.35rem 0.65rem;
  background: #2a3038;
  border: 1px solid #4a5160;
  border-radius: 10px;
}

.section-readout-label {
  flex: 0 1 auto;
  font-size: 0.8125rem;
  color: var(--muted);
  line-height: 1.2;
  text-align: left;
  white-space: nowrap;
  margin-right: 0.15rem;
  max-width: 10.5rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.section-nav-chevron {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.65rem;
  height: 1.65rem;
  padding: 0;
  margin: 0;
  border: none;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  cursor: pointer;
}

.section-nav-chevron:hover {
  background: rgba(255, 255, 255, 0.18);
}

.section-nav-chevron:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.section-nav-chevron svg {
  display: block;
  width: 1.1rem;
  height: 1.1rem;
}

.section-readout-box input {
  flex: 0 0 auto;
  width: 5.25rem;
  min-width: 5.25rem;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
  text-align: center;
  padding: 0 0.1rem;
  cursor: default;
  user-select: none;
  pointer-events: none;
}

.section-readout-box.section-readout-box--disabled {
  opacity: 0.48;
  pointer-events: none;
}

.section-readout-box.section-readout-box--disabled .section-nav-chevron {
  cursor: not-allowed;
}

.toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  font-size: 0.875rem;
  color: var(--text);
  user-select: none;
}

.toggle input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-ui {
  width: 2.5rem;
  height: 1.35rem;
  border-radius: 999px;
  background: #4a5160;
  position: relative;
  flex-shrink: 0;
  transition: background 0.2s;
}

.toggle-ui::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: calc(1.35rem - 4px);
  height: calc(1.35rem - 4px);
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.35);
}

.toggle input:checked + .toggle-ui {
  background: #3d6a8c;
}

.toggle input:checked + .toggle-ui::after {
  transform: translateX(1.15rem);
}

.toggle input:focus-visible + .toggle-ui {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.toggle.toggle--disabled {
  opacity: 0.48;
  cursor: not-allowed;
  pointer-events: none;
}

/* Phones & tablets landscape: smaller controls so the ribbon fits on one row */
@media (orientation: landscape) and (max-width: 1366px) {
  .toolbar {
    padding: 0.4rem 0.55rem;
    gap: 0.35rem 0.5rem;
  }

  .toolbar-center {
    flex-wrap: nowrap;
    gap: 0.28rem 0.4rem;
    min-width: 0;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 1px;
    justify-content: center;
  }

  .toolbar-group {
    gap: 0.35rem 0.45rem;
  }

  .btn {
    font-size: 0.78rem;
    padding: 0.2rem 0.45rem;
    border-radius: 5px;
  }

  .btn-icon {
    padding: 0.2rem 0.35rem;
    min-width: 1.85rem;
  }

  .btn-icon svg {
    width: 1rem;
    height: 1rem;
  }

  #btn-play.btn-icon svg {
    width: 1.05rem;
    height: 1.05rem;
  }

  .toolbar .toggle {
    font-size: 0.76rem;
    gap: 0.3rem;
  }

  .toolbar .toggle-ui {
    width: 2.1rem;
    height: 1.12rem;
  }

  .toolbar .toggle-ui::after {
    width: calc(1.12rem - 4px);
    height: calc(1.12rem - 4px);
    top: 2px;
    left: 2px;
  }

  .toolbar .toggle input:checked + .toggle-ui::after {
    transform: translateX(0.98rem);
  }

  .section-readout-box {
    gap: 0.22rem 0.35rem;
    min-height: 1.72rem;
    padding: 0.22rem 0.4rem 0.22rem 0.45rem;
    border-radius: 8px;
  }

  .section-readout-label {
    font-size: 0.72rem;
    max-width: 7.5rem;
    margin-right: 0.05rem;
  }

  .section-readout-box input {
    width: 4.5rem;
    min-width: 4.5rem;
    font-size: 0.78rem;
  }

  .section-nav-chevron {
    width: 1.4rem;
    height: 1.4rem;
    border-radius: 3px;
  }

  .section-nav-chevron svg {
    width: 0.95rem;
    height: 0.95rem;
  }

  .muted {
    font-size: 0.72rem;
  }

  .song-name-label {
    max-width: min(14rem, 28vw);
  }

  .bpm-readout {
    font-size: 0.72rem;
    min-width: 3.5rem;
  }
}

select,
input[type="number"] {
  font: inherit;
  padding: 0.25rem 0.4rem;
  border-radius: 4px;
  border: 1px solid #4a5160;
  background: var(--bg);
  color: var(--text);
}

input[type="number"] {
  width: 4.5rem;
}

#error-banner {
  margin: 0;
  padding: 0.5rem 1rem;
  background: var(--error-bg);
  color: var(--error-text);
  font-size: 0.875rem;
  flex-shrink: 0;
}

.stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0.65rem 0.85rem;
  max-width: min(1200px, 100%);
  margin: 0 auto;
  width: 100%;
}

@media (min-width: 1024px) {
  .stage {
    max-width: none;
    margin: 0;
    width: 100%;
    flex: 1 1 auto;
    min-height: 0;
    padding-left: 0.65rem;
    padding-right: 0.65rem;
  }
}

.fall-wrap {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid #333842;
  background: linear-gradient(180deg, #0f1116 0%, #1a1d24 45%);
}

.fall-area {
  position: relative;
  flex: 2 1 0;
  min-height: 0;
  height: auto;
  overflow: hidden;
  cursor: pointer;
}

.bar-rail {
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 2.25rem;
  z-index: 4;
  pointer-events: none;
  border-right: 1px solid rgba(255, 255, 255, 0.08);
}

.bar-rail-label {
  position: absolute;
  left: 0.2rem;
  transform: translateY(-100%);
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  padding-bottom: 2px;
}

.bar-lines {
  position: absolute;
  left: 2.25rem;
  right: 0;
  top: 0;
  bottom: 0;
  z-index: 1;
  pointer-events: none;
}

.bar-line-h {
  position: absolute;
  left: 0;
  right: 0;
  height: 1px;
  background: rgba(255, 255, 255, 0.022);
}

.bars-layer {
  position: absolute;
  left: 2.25rem;
  right: 0;
  top: 0;
  bottom: 0;
  pointer-events: none;
  z-index: 2;
}

.prep-bar-strip {
  position: absolute;
  left: 0;
  right: 0;
  pointer-events: none;
  z-index: 1;
  border-radius: 6px;
  box-sizing: border-box;
  opacity: 0.42;
  background: repeating-linear-gradient(
    -45deg,
    rgba(255, 255, 255, 0.06),
    rgba(255, 255, 255, 0.06) 5px,
    rgba(255, 255, 255, 0.12) 5px,
    rgba(255, 255, 255, 0.12) 10px
  );
}

.fall-bar-band {
  position: absolute;
  left: 0;
  right: 0;
  pointer-events: none;
  z-index: 0;
  box-sizing: border-box;
}

.fall-bar-band--striped {
  opacity: 0.42;
  background: repeating-linear-gradient(
    -45deg,
    rgba(255, 255, 255, 0.06),
    rgba(255, 255, 255, 0.06) 5px,
    rgba(255, 255, 255, 0.12) 5px,
    rgba(255, 255, 255, 0.12) 10px
  );
}

.note-bar {
  position: absolute;
  z-index: 2;
  border-radius: 6px;
  background: var(--bar);
  color: var(--bar-text);
  font-weight: 700;
  font-size: clamp(0.65rem, 2vw, 0.95rem);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);
  border: 1px solid rgba(0, 0, 0, 0.12);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  padding: 0 2px;
}

.note-bar--label-bottom {
  align-items: flex-end;
  padding-bottom: 8px;
}

.hit-line {
  position: absolute;
  left: 2.25rem;
  right: 0;
  bottom: 0;
  height: 3px;
  background: var(--hit-line);
  box-shadow: 0 0 12px rgba(255, 240, 255, 0.35);
  z-index: 3;
  pointer-events: none;
}

.keyboard-wrap {
  position: relative;
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: #12141a;
  padding: 0.35rem 0 0.5rem 2.25rem;
  overflow-x: auto;
  overflow-y: hidden;
}

.keyboard {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  height: 100%;
  width: 100%;
  min-width: 0;
  margin: 0;
}

.key-white {
  float: left;
  width: var(--w, 10%);
  height: 100%;
  background: linear-gradient(180deg, var(--white-key) 0%, #dcd8d0 100%);
  border: 1px solid var(--white-border);
  border-radius: 0 0 6px 6px;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.06);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 0.5rem;
  font-weight: 700;
  font-size: clamp(0.7rem, 1.5vw, 0.9rem);
  color: #333;
  user-select: none;
}

.key-white.active {
  background: linear-gradient(180deg, var(--key-active) 0%, #5a9a5a 100%);
  color: #fff;
}

@keyframes keyRepeatFlashWhite {
  0% {
    filter: brightness(2.1);
    box-shadow: inset 0 0 18px rgba(255, 255, 255, 0.95);
  }
  100% {
    filter: brightness(1);
    box-shadow: none;
  }
}

.key-white.key-repeat-flash {
  animation: keyRepeatFlashWhite 0.32s ease-out;
  z-index: 3;
}

.key-black {
  position: absolute;
  top: 0;
  width: var(--bw, 5%);
  height: 58%;
  background: linear-gradient(180deg, #2a2d34 0%, var(--black-key) 40%);
  border-radius: 0 0 4px 4px;
  border: 1px solid #000;
  box-shadow: 2px 4px 6px rgba(0, 0, 0, 0.45);
  z-index: 1;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 0.2rem;
  font-size: 0.55rem;
  color: #aaa;
  user-select: none;
}

.key-black.active {
  background: linear-gradient(180deg, #4a7a4a 0%, #3d6a3d 100%);
  color: #fff;
}

@keyframes keyRepeatFlashBlack {
  0% {
    filter: brightness(2.4);
    box-shadow: inset 0 0 14px rgba(255, 255, 255, 0.55);
  }
  100% {
    filter: brightness(1);
    box-shadow: none;
  }
}

.key-black.key-repeat-flash {
  animation: keyRepeatFlashBlack 0.32s ease-out;
  z-index: 4;
}

.keyboard::after {
  content: "";
  display: table;
  clear: both;
}

/* Song picker modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  cursor: pointer;
}

.modal-dialog {
  position: relative;
  width: min(100%, 26rem);
  max-height: min(85vh, 32rem);
  display: flex;
  flex-direction: column;
  padding: 1.25rem 1.35rem;
  background: var(--panel);
  border: 1px solid #3d4450;
  border-radius: 12px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
}

.modal-dialog h2 {
  margin: 0 0 0.75rem;
  font-size: 1.15rem;
  font-weight: 600;
}

.modal-status {
  margin: 0 0 0.75rem;
  min-height: 1.25rem;
  font-size: 0.85rem;
}

.song-modal-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
  min-height: 0;
}

.song-modal-list li {
  margin: 0;
}

.song-modal-item {
  display: block;
  width: 100%;
  margin: 0 0 0.35rem;
  padding: 0.55rem 0.75rem;
  font: inherit;
  text-align: left;
  color: var(--text);
  background: #363d4a;
  border: 1px solid #4a5160;
  border-radius: 8px;
  cursor: pointer;
}

.song-modal-item:hover {
  background: #424a5a;
  border-color: #5a6375;
}

.modal-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 1rem;
  padding-top: 0.75rem;
  border-top: 1px solid #333842;
}
