.drums-stage {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 16px 16px;
  gap: 10px;
  /* Allow this flex child to shrink inside .instrument-body so the grid
   * inside us can use min-height: 0 and not blow out the page. */
  min-height: 0;
  width: 100%;
}

.drum-pads {
  flex: 1 1 auto;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 12px;
  width: 100%;
  max-width: 720px;
  /* Let the grid fill the remaining vertical space inside `.drums-stage`.
   * The cap keeps pads from looking absurdly tall on very large desktop
   * windows, but on phones (portrait or landscape) it grows to fill. */
  max-height: min(720px, 100%);
  min-height: 0;
}

.drum-pad {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  /* Touch target — generous on phones. */
  min-height: 56px;
  background: linear-gradient(160deg, rgba(30, 41, 59, 0.85), rgba(15, 23, 42, 0.85));
  border: 1px solid var(--border);
  border-radius: 16px;
  color: var(--fg);
  font-weight: 600;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition:
    transform 90ms ease,
    border-color 120ms ease,
    box-shadow 120ms ease,
    background 120ms ease;
  overflow: hidden;
  /* Color-coded family accent set per-pad as a CSS var. */
  --pad-accent: var(--accent);
}

.drum-pad::before {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 30%, var(--pad-accent), transparent 65%);
  opacity: 0.18;
  pointer-events: none;
  transition: opacity 200ms ease;
}

.drum-pad:hover {
  border-color: var(--pad-accent);
  box-shadow: 0 12px 28px -16px var(--pad-accent);
}

.drum-pad.active {
  transform: translateY(2px) scale(0.98);
  background: linear-gradient(160deg, rgba(30, 41, 59, 0.95), rgba(15, 23, 42, 0.95));
  border-color: var(--pad-accent);
  box-shadow:
    inset 0 0 0 1px var(--pad-accent),
    0 0 30px var(--pad-accent);
}

.drum-pad.active::before {
  opacity: 0.55;
}

.drum-pad-emoji {
  font-size: clamp(34px, 7vw, 52px);
  line-height: 1;
  filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.4));
}

.drum-pad-name {
  font-size: clamp(12px, 2vw, 14px);
  letter-spacing: 0.04em;
  text-align: center;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.5);
}

.drum-pad-key {
  position: absolute;
  top: 8px;
  right: 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--fg-dim);
  background: rgba(15, 23, 42, 0.6);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 6px;
}

/* QWERTY hint is irrelevant on touch devices and on phone-sized screens
 * (narrow portrait or short landscape) where the user almost certainly
 * isn't on a physical keyboard. The pad emoji + label is enough. */
@media (hover: none) and (pointer: coarse),
  (max-width: 640px),
  (max-height: 480px) {
  .drum-pad-key {
    display: none;
  }
}

.drum-pad.active .drum-pad-key {
  color: #fff;
  border-color: var(--pad-accent);
}

@media (prefers-reduced-motion: reduce) {
  .drum-pad {
    transition: none !important;
  }
  .drum-pad.active {
    transform: none !important;
  }
}

@media (max-width: 480px) {
  .drum-pads {
    gap: 8px;
  }
  .drums-stage {
    padding: 6px 10px 12px;
  }
}

/* Phone-sized screens: chunkier loop buttons. */
@media (max-width: 640px) {
  .loop-btn {
    padding: 9px 14px;
    font-size: 14px;
    min-height: 38px;
  }
  .loop-btn-dot {
    width: 11px;
    height: 11px;
  }
}

/* Narrow portrait phones: stack the loop row below volume/kit so the
 * buttons get their full width. */
@media (max-width: 480px) {
  .control-loop {
    width: 100%;
    justify-content: center;
  }
  .loop-status {
    flex-basis: 100%;
    text-align: center;
    min-width: 0;
  }
}

/* Landscape phones: vertical pixels are precious. Hide control labels,
 * shrink the slider, and squeeze padding so the controls fit on ONE
 * row — leaves three full pad rows visible on phones as small as 568×320. */
@media (max-height: 480px) {
  .instrument-body {
    padding-bottom: 8px;
  }
  .instrument-controls {
    padding: 4px 12px 6px;
    gap: 6px 14px;
    flex-wrap: nowrap;
    overflow: hidden;
  }
  .control > label,
  .control-loop > .loop-label-text {
    display: none;
  }
  .control input[type='range'] {
    width: 80px;
  }
  .loop-btn {
    padding: 7px 11px;
    font-size: 13px;
    min-height: 32px;
  }
  .loop-status {
    font-size: 11px;
    min-width: 0;
    /* Truncate long status strings instead of wrapping. */
    max-width: 110px;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
  .drums-stage {
    padding: 4px 10px 6px;
    gap: 4px;
  }
  .drum-pads {
    gap: 8px;
    max-height: none;
  }
  .drum-pad {
    min-height: 0;
    gap: 2px;
  }
  .drum-pad-emoji {
    font-size: clamp(24px, 5.5vh, 38px);
  }
  .drum-pad-name {
    font-size: clamp(11px, 2.5vh, 13px);
  }
}

/* =========================================================================
 * Loop controls (record / play / clear) + position bar
 * ========================================================================= */

.control-loop {
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.loop-label-text {
  text-transform: uppercase;
  font-size: 11px;
  letter-spacing: 0.1em;
  color: var(--fg-dim);
}

.loop-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(30, 41, 59, 0.85);
  color: var(--fg);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition:
    border-color 120ms ease,
    background 120ms ease,
    color 120ms ease,
    box-shadow 120ms ease;
}

.loop-btn:hover:not(:disabled) {
  border-color: var(--accent);
}

.loop-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.loop-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.loop-btn-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #ef4444;
  box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.18);
  flex-shrink: 0;
}

.loop-btn-icon {
  font-size: 12px;
  line-height: 1;
  transform: translateY(-0.5px);
}

/* Armed: rec is "waiting for first hit" — pulse softly */
.loop-record.armed {
  border-color: #ef4444;
  color: #fecaca;
}

.loop-record.armed .loop-btn-dot {
  animation: loop-rec-pulse 0.9s ease-in-out infinite;
}

/* Recording / overdubbing — solid red glow */
.loop-record.recording,
.loop-record.overdubbing {
  border-color: #ef4444;
  background: rgba(239, 68, 68, 0.18);
  color: #fff;
  box-shadow: 0 0 18px -2px rgba(239, 68, 68, 0.55);
}

.loop-record.recording .loop-btn-dot,
.loop-record.overdubbing .loop-btn-dot {
  animation: loop-rec-pulse 0.7s ease-in-out infinite;
}

.loop-play.playing {
  border-color: var(--accent-3);
  background: rgba(52, 211, 153, 0.16);
  color: #ecfdf5;
  box-shadow: 0 0 18px -4px rgba(52, 211, 153, 0.5);
}

@keyframes loop-rec-pulse {
  0%,
  100% {
    box-shadow: 0 0 0 2px rgba(239, 68, 68, 0.18);
    transform: scale(1);
  }
  50% {
    box-shadow: 0 0 0 6px rgba(239, 68, 68, 0);
    transform: scale(1.18);
  }
}

.loop-status {
  min-width: 96px;
  font-size: 12px;
  color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
  font-feature-settings: 'tnum';
  letter-spacing: 0.01em;
}

.loop-status.has-loop {
  color: var(--fg);
}

.loop-status.recording,
.loop-status.armed {
  color: #fca5a5;
}

.loop-status.playing,
.loop-status.overdubbing {
  color: var(--accent-3);
}

/* Position bar — sits just above the pads when there's a loop. */
.loop-bar {
  width: min(100%, 720px);
  height: 4px;
  margin: 0 auto 10px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.18);
  overflow: hidden;
  opacity: 0;
  transition: opacity 200ms ease;
}

.loop-bar.visible {
  opacity: 1;
}

.loop-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  border-radius: inherit;
  transition: width 60ms linear;
}

.loop-bar.recording .loop-bar-fill {
  background: linear-gradient(90deg, #ef4444, #f97316);
}

.loop-bar.overdubbing .loop-bar-fill {
  background: linear-gradient(90deg, var(--accent-3), #ef4444);
}

@media (prefers-reduced-motion: reduce) {
  .loop-record.armed .loop-btn-dot,
  .loop-record.recording .loop-btn-dot,
  .loop-record.overdubbing .loop-btn-dot {
    animation: none !important;
  }
}
