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

:root {
  --bg: #111827;
  --surface: #1f2937;
  --surface2: #374151;
  --accent: #f97316;
  --accent-glow: rgba(249,115,22,0.4);
  --blue: #3b82f6;
  --purple: #a855f7;
  --text: #f9fafb;
  --muted: #6b7280;
  --radius: 14px;
}

html, body { height: 100%; touch-action: manipulation; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  user-select: none;
  -webkit-user-select: none;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 10px 12px;
  gap: 10px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
}

.logo {
  font-size: 1.1rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  color: var(--accent);
}

/* ─── Beat dots ─── */
.beat-dots { display: flex; gap: 7px; align-items: center; }
.beat-dot {
  width: 9px; height: 9px;
  border-radius: 50%;
  background: var(--surface2);
  transition: background 0.05s, transform 0.05s;
}
.beat-dot.on { background: var(--accent); transform: scale(1.5); }
.beat-dot.sub { width: 6px; height: 6px; opacity: 0.5; }

/* ─── Controls ─── */
.controls {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 10px 12px;
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
  flex-shrink: 0;
}

.ctrl-group { display: flex; flex-direction: column; gap: 4px; }
.ctrl-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
}

select {
  background: var(--surface2);
  color: var(--text);
  border: 1.5px solid rgba(255,255,255,0.08);
  border-radius: 9px;
  padding: 7px 10px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  min-width: 64px;
  text-align: center;
}

.bpm-row { display: flex; align-items: center; gap: 5px; }
.bpm-btn {
  width: 34px; height: 34px;
  border-radius: 8px; border: none;
  background: var(--surface2); color: var(--text);
  font-size: 1.1rem; font-weight: 700; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.bpm-btn:active { background: var(--accent); }
#bpmVal { min-width: 38px; text-align: center; font-size: 1.05rem; font-weight: 700; }

/* ─── Waveform selector ─── */
.wave-group { display: flex; gap: 4px; }
.wave-btn {
  padding: 7px 8px;
  background: var(--surface2);
  border: 1.5px solid transparent;
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.5px;
  cursor: pointer;
  transition: border-color 0.12s, color 0.12s, background 0.12s;
  -webkit-tap-highlight-color: transparent;
}
.wave-btn.on { border-color: var(--accent); color: var(--accent); background: rgba(249,115,22,0.12); }
.wave-btn:active:not(.on) { background: var(--accent); color: #fff; }

/* ─── SF2 controls ─── */
.sf2-load-row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.sf2-load-btn {
  background: var(--surface2);
  color: var(--text);
  border: 1.5px solid rgba(255,255,255,0.08);
  border-radius: 9px;
  padding: 7px 10px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
  transition: background 0.1s;
}
.sf2-load-btn:active  { background: var(--accent); }
.sf2-load-btn:disabled { opacity: 0.5; cursor: default; }

.sf2-status {
  font-size: 0.68rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 90px;
}

.sf2-preset-select {
  min-width: 130px;
  max-width: 200px;
  font-size: 0.82rem;
  text-align: left;
}

/* ─── Play button ─── */
.play-btn {
  margin-left: auto;
  background: var(--accent); color: white;
  border: none; border-radius: 10px;
  padding: 9px 20px;
  font-size: 1rem; font-weight: 800;
  cursor: pointer; letter-spacing: 0.3px;
  transition: background 0.15s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
}
.play-btn:active { transform: scale(0.95); }
.play-btn.playing { background: #22c55e; }

/* ─── Chord section ─── */
.chord-section {
  flex: 1;
  display: flex; flex-direction: column;
  gap: 8px; min-height: 0;
}

.section-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  flex-shrink: 0;
}

.chord-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  flex: 1; min-height: 0;
  align-content: start;
}

.chord-btn {
  background: var(--surface);
  border: 2px solid var(--surface2);
  border-radius: var(--radius); color: var(--text);
  font-size: clamp(1rem, 5vw, 1.35rem); font-weight: 800;
  cursor: pointer; padding: 6px 4px; min-height: 56px;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 2px;
  transition: border-color 0.1s, background 0.1s, transform 0.08s, box-shadow 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.chord-btn .qlabel {
  font-size: 0.6rem; font-weight: 500; opacity: 0.6; letter-spacing: 0.3px;
}

.chord-btn.maj { border-color: var(--blue); color: var(--blue); }
.chord-btn.min { border-color: var(--purple); color: var(--purple); }
.chord-btn.dim { border-color: var(--muted); color: var(--muted); }
.chord-btn.custom { border-color: #f59e0b; color: #f59e0b; }
.chord-btn.remove-mode { border-color: #ef4444 !important; }

.chord-btn.active {
  background: var(--accent) !important;
  border-color: var(--accent) !important;
  color: #fff !important;
  box-shadow: 0 0 18px var(--accent-glow);
  transform: scale(1.04);
}
.chord-btn.active .qlabel { opacity: 0.85; }
.chord-btn:active:not(.active) { transform: scale(0.94); }
.chord-btn.active:active { transform: scale(1.01); }

/* ─── Piano ─── */
.piano-section {
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.piano-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.octave-ctrl {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
}

.oct-btn {
  background: var(--surface2);
  border: none;
  border-radius: 6px;
  color: var(--text);
  font-size: 0.7rem;
  width: 26px; height: 22px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.oct-btn:active { background: var(--accent); }
#octLabel { min-width: 24px; text-align: center; font-size: 0.75rem; font-weight: 600; }

.piano-keys {
  position: relative;
  height: 64px;
  display: flex;
  max-width: 460px;
}

.piano-key-white {
  flex: 1;
  background: #d0d0d0;
  border: 1px solid #777;
  border-top: none;
  border-radius: 0 0 5px 5px;
  cursor: pointer;
  transition: background 0.05s;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}
.piano-key-white + .piano-key-white { margin-left: 1px; }
.piano-key-white.pressed { background: var(--accent) !important; }

.piano-key-black {
  position: absolute;
  top: 0;
  height: 62%;
  width: 8.5%;
  background: #1a1a2a;
  border: 1px solid #000;
  border-top: none;
  border-radius: 0 0 4px 4px;
  cursor: pointer;
  z-index: 2;
  transition: background 0.05s;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}
.piano-key-black.pressed { background: var(--accent) !important; }

/* ─── Note picker ─── */
.note-picker {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 5px;
  flex-shrink: 0;
}
.note-btn {
  background: var(--surface2);
  border: 1.5px solid transparent;
  border-radius: 8px;
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 700;
  padding: 7px 2px;
  cursor: pointer;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
  font-family: monospace;
  transition: border-color 0.1s, color 0.1s, background 0.1s;
}
.note-btn.on      { border-color: var(--accent); color: var(--accent); background: rgba(249,115,22,0.12); }
.note-btn.bass-on { border-color: var(--blue);   color: var(--blue);   background: rgba(59,130,246,0.12); }
.note-btn:active  { background: var(--accent); color: #fff; }

/* ─── Suffix chips ─── */
.suffix-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  flex-shrink: 0;
}
.suffix-chip {
  background: var(--surface2);
  border: 1.5px solid transparent;
  border-radius: 7px;
  color: var(--text);
  font-size: 0.78rem;
  font-weight: 700;
  padding: 5px 10px;
  cursor: pointer;
  font-family: monospace;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.1s, color 0.1s, background 0.1s;
}
.suffix-chip.on       { border-color: var(--accent); color: var(--accent); background: rgba(249,115,22,0.12); }
.suffix-chip.slash-on { border-color: var(--blue);   color: var(--blue);   background: rgba(59,130,246,0.12); }
.suffix-chip:active   { background: var(--accent); color: #fff; }

/* ─── Chord display ─── */
.custom-row { display: flex; gap: 7px; flex-shrink: 0; }

.chord-display {
  flex: 1; min-width: 0;
  background: var(--surface);
  border: 1.5px solid var(--surface2);
  border-radius: 9px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 40px;
  transition: border-color 0.15s;
}
.chord-display.ready     { border-color: #f59e0b; }
.chord-display.bass-mode { border-color: var(--blue); }

#chordDisplayText {
  flex: 1;
  font-size: 1rem;
  font-weight: 700;
  font-family: monospace;
  color: var(--muted);
}
.chord-display.ready     #chordDisplayText,
.chord-display.bass-mode #chordDisplayText { color: var(--text); }

.chord-clear-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
  padding: 2px 4px;
  -webkit-tap-highlight-color: transparent;
}
.chord-clear-btn:active { color: #ef4444; }

.add-btn {
  background: var(--surface2); color: var(--text);
  border: none; border-radius: 9px;
  font-size: 0.9rem; font-weight: 700; padding: 8px 14px;
  cursor: pointer; white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}
.add-btn:active { background: var(--accent); }

/* ─── Landscape ─── */
@media (orientation: landscape) and (max-height: 480px) {
  .app { flex-direction: row; flex-wrap: nowrap; padding: 8px; gap: 8px; align-items: stretch; }
  header { display: none; }
  .controls {
    flex-direction: column; align-items: stretch;
    flex-wrap: nowrap; width: 180px; flex-shrink: 0; gap: 8px;
  }
  .play-btn { margin-left: 0; width: 100%; }
  .bpm-row { justify-content: center; }
  .wave-group { flex-wrap: wrap; }
  .chord-section { flex: 1; }
  .piano-section { display: none; }
}
