:root {
  --bg: #111318;
  --panel: #1b1e26;
  --panel-2: #242833;
  --border: #323746;
  --text: #eef1f6;
  --muted: #9aa3b2;
  --accent: #ffb547;      /* sounding keys */
  --accent-soft: #ffd38f;
  --accent-ink: #2a1a00;
  --select: #8f7cff;      /* selected keys (Select mode) */
  --select-soft: #d2caff;
  --felt: #7a2230;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

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

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- Toolbar ---------- */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 4px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-right: auto;
  font-weight: 700;
  letter-spacing: 0.02em;
}

select, .btn {
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
  color: var(--text);
  font: inherit;
  font-size: 15px;
  touch-action: manipulation;
  cursor: pointer;
}
.btn:hover:not(:disabled), select:hover { background: #2c3140; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: var(--accent-soft); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-2);
}
.segmented button {
  height: 32px;
  padding: 0 14px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  touch-action: manipulation;
  cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--text); color: var(--bg); font-weight: 600; }

/* ---------- Display ---------- */
.display {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  min-height: 64px;
  padding: 6px 14px 8px;
}
.display-text { min-width: 0; }
.display-main {
  font-size: clamp(26px, 4.5vw, 44px);
  font-weight: 700;
  line-height: 1.1;
  color: var(--accent);
  /* one line only: a wrapping name would resize the keyboard while playing */
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.display.idle .display-main { color: var(--text); }
.display-main .alt { color: var(--muted); font-size: 0.55em; font-weight: 600; }
.display-sub {
  margin-top: 2px;
  overflow: hidden;
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.pills { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.pill:empty { display: none; }
.pill.warn { border-color: #6b4a1a; color: var(--accent-soft); }
.pill {
  padding: 3px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--panel);
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

/* ---------- Chord bar (Select mode) ---------- */
.chord-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 0 12px 8px;
}
.chord-bar[hidden] { display: none; }
.tempo { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; }
.tempo input { width: 110px; accent-color: var(--accent); }
.tempo output { min-width: 3ch; color: var(--text); text-align: right; font-variant-numeric: tabular-nums; }
.sel-count { color: var(--select-soft); font-size: 13px; }

/* ---------- Stage: octave buttons + keyboard ---------- */
.stage {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) 56px;
  gap: 8px;
  padding: 4px 10px 10px;
}
.stage.vertical {
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: 46px minmax(0, 1fr) 46px;
}
.stage.vertical #oct-up { order: -1; }  /* higher notes on top… */
.stage.vertical #oct-down { order: 1; } /* …lower notes at the bottom */

.oct-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 20px;
  touch-action: manipulation;
  cursor: pointer;
}
.oct-btn small { color: var(--muted); font-size: 11px; }
.oct-btn:hover:not(:disabled) { background: var(--panel-2); }
.oct-btn:disabled { opacity: 0.35; cursor: default; }
.stage.vertical .oct-btn { flex-direction: row; gap: 10px; }

/* ---------- Keyboard ---------- */
.keyboard {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  border-top: 6px solid var(--felt);
  border-radius: 10px;
  background: #0a0b0e;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.keyboard.vertical { border-top: 0; border-left: 6px solid var(--felt); }

.key { position: absolute; display: flex; cursor: pointer; }

/* Horizontal (default) */
.key.white {
  top: 0;
  bottom: 0;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
  padding-bottom: 10px;
  border-right: 1px solid #b9b9b4;
  border-radius: 0 0 6px 6px;
  background: linear-gradient(to bottom, #dcdcd7 0, #fbfbf9 6%, #f2f2ee 92%, #d6d6d0 100%);
}
.key.black {
  top: 0;
  height: 62%;
  z-index: 2;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding-bottom: 8px;
  border-radius: 0 0 5px 5px;
  background: linear-gradient(to bottom, #111216 0, #2c2e36 85%, #3a3c45 100%);
  box-shadow: 0 3px 4px rgba(0, 0, 0, 0.45);
}

/* Vertical: low notes at the bottom, black keys on the left */
.keyboard.vertical .key.white {
  top: auto;
  left: 0;
  right: 0;
  bottom: auto;
  flex-direction: row;
  justify-content: flex-end;
  padding: 0 12px 0 0;
  border-right: 0;
  border-top: 1px solid #b9b9b4;
  border-radius: 0 6px 6px 0;
  background: linear-gradient(to right, #dcdcd7 0, #fbfbf9 6%, #f2f2ee 92%, #d6d6d0 100%);
}
.keyboard.vertical .key.black {
  top: auto;
  left: 0;
  width: 62%;
  height: auto;
  flex-direction: row;
  justify-content: flex-end;
  padding: 0 8px 0 0;
  border-radius: 0 5px 5px 0;
  background: linear-gradient(to right, #111216 0, #2c2e36 85%, #3a3c45 100%);
  box-shadow: 3px 0 4px rgba(0, 0, 0, 0.45);
}

.label { color: #77787d; font-size: 12px; pointer-events: none; }
.key.is-c .label { color: #25262b; font-weight: 700; }
.hint {
  color: #a3a4aa;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  pointer-events: none;
}
.key.black .hint { color: #8d8f99; }

/* Selected keys (only visible in Select mode) */
.keyboard.selecting .key.white.selected { background: var(--select-soft); box-shadow: inset 0 0 0 3px var(--select); }
.keyboard.selecting .key.black.selected { background: var(--select); }

/* Sounding keys — listed after .selected so they win */
.keyboard .key.white.active,
.keyboard.selecting .key.white.active { background: var(--accent-soft); }
.keyboard .key.black.active,
.keyboard.selecting .key.black.active { background: var(--accent); }
.key.active .label, .key.active .hint { color: var(--accent-ink); }

/* Notes outside the instrument's range */
.key.disabled { cursor: not-allowed; }
.key.white.disabled, .keyboard.vertical .key.white.disabled { background: #5d6068; }
.key.black.disabled, .keyboard.vertical .key.black.disabled { background: #1c1d22; }
.key.disabled .label { color: #3a3c43; }

/* ---------- Small screens ---------- */
@media (max-width: 600px) {
  .toolbar { gap: 6px; padding: 8px 8px 2px; }
  .brand span { display: none; }
  .brand { margin-right: 0; }
  #instrument { flex: 1; min-width: 0; }
  .display { min-height: 52px; padding: 4px 10px 6px; }
  .display-sub { font-size: 12px; }
  .pill { font-size: 11px; padding: 2px 8px; }
  #view-range { display: none; } /* the octave buttons already show the visible range */
  .chord-bar { gap: 6px; padding: 0 8px 6px; }
  .chord-bar .btn, .chord-bar select { height: 36px; font-size: 14px; padding: 0 10px; }
  .tempo input { width: 90px; }
  .stage { padding: 2px 6px 6px; gap: 6px; }
}
@media (max-height: 480px) and (orientation: landscape) {
  /* Phone in landscape: squeeze the chrome to give the keys height */
  .toolbar { padding: 4px 10px 0; }
  .toolbar select, .toolbar .btn { height: 34px; }
  .segmented button { height: 28px; }
  .display { min-height: 0; padding: 2px 14px 4px; }
  .display-main { font-size: 24px; }
  .pills { flex-direction: row; }
  .chord-bar { padding-bottom: 4px; }
  .chord-bar .btn, .chord-bar select { height: 32px; }
  .stage { padding: 2px 8px 6px; grid-template-columns: 46px minmax(0, 1fr) 46px; }
}
