:root {
  --bg: #14151a;
  --surface: #1e2028;
  --text: #f1f2f6;
  --muted: #8a8fa3;
  --radius: 16px;
  --gap: 16px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  display: flex;
  justify-content: center;
}

.sampler {
  width: 100%;
  max-width: 880px;
  padding: 32px 24px calc(32px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.sampler__header {
  position: relative;
  text-align: center;
}

/* Переключатель языка в правом верхнем углу шапки */
.lang {
  position: absolute;
  top: 0;
  right: 0;
  padding: 6px 10px;
  border: 1px solid rgb(255 255 255 / 12%);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  /* Тёмный выпадающий список у нативного select */
  color-scheme: dark;
}

.lang:hover {
  border-color: rgb(255 255 255 / 25%);
}

.lang:focus-visible {
  outline: 2px solid var(--text);
  outline-offset: 2px;
}

.sampler__title {
  margin: 0 0 6px;
  font-size: 28px;
  letter-spacing: 0.02em;
}

.sampler__hint {
  margin: 0;
  color: var(--muted);
  font-size: 14px;
}

/* ---------- Пэды ---------- */

.pads {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--gap);
}

.pad {
  --pad-color: #888;
  position: relative;
  aspect-ratio: 1;
  border: none;
  border-radius: var(--radius);
  background: var(--pad-color);
  color: #fff;
  cursor: pointer;
  user-select: none;
  touch-action: manipulation;
  box-shadow: 0 6px 0 rgb(0 0 0 / 35%), inset 0 -40px 60px rgb(0 0 0 / 18%);
  transition: transform 60ms ease, box-shadow 60ms ease, filter 120ms ease;
  font: inherit;
  padding: 0;
  outline: none;
}

/* Пэд с открытым меню поднимается над соседями, чтобы меню их не перекрывали */
.pad:has(.menu:not([hidden])) {
  z-index: 20;
}

.pad:focus-visible {
  box-shadow: 0 6px 0 rgb(0 0 0 / 35%), 0 0 0 3px var(--text);
}

.pad:hover {
  filter: brightness(1.08);
}

.pad.is-active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 rgb(0 0 0 / 35%), 0 0 28px var(--pad-color);
  filter: brightness(1.25);
}

.pad__key {
  position: absolute;
  left: 12px;
  bottom: 10px;
  font-size: 13px;
  font-weight: 700;
  opacity: 0.85;
}

.pad__name {
  position: absolute;
  left: 12px;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  text-shadow: 0 1px 2px rgb(0 0 0 / 40%);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pad.is-loading .pad__name {
  opacity: 0.6;
}

.pad.is-binding {
  animation: pad-binding 1s ease-in-out infinite;
}

@keyframes pad-binding {
  50% {
    box-shadow: 0 6px 0 rgb(0 0 0 / 35%), 0 0 0 4px var(--text), 0 0 24px var(--pad-color);
  }
}

.pad__more {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: rgb(0 0 0 / 20%);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  touch-action: manipulation;
}

.pad__more:hover,
.pad__more[aria-expanded="true"] {
  background: rgb(0 0 0 / 38%);
}

.pad__more svg {
  width: 20px;
  height: 20px;
  fill: currentColor;
}

/* ---------- Выпадающее меню ---------- */

.menu {
  position: absolute;
  top: 46px;
  right: 6px;
  z-index: 10;
  min-width: 180px;
  margin: 0;
  padding: 6px;
  list-style: none;
  background: var(--surface);
  border: 1px solid rgb(255 255 255 / 8%);
  border-radius: 12px;
  box-shadow: 0 12px 32px rgb(0 0 0 / 45%);
  text-align: left;
}

.menu[hidden] {
  display: none;
}

.menu__item {
  display: block;
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

.menu__item:hover,
.menu__item:focus-visible {
  background: rgb(255 255 255 / 8%);
  outline: none;
}

/* ---------- Слайдер скорости ---------- */

.speed {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px 20px 14px;
}

.speed__label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 15px;
  margin-bottom: 12px;
}

.speed__value {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
}

.speed__slider {
  width: 100%;
  height: 28px;
  margin: 0;
  accent-color: #7c5cff;
  cursor: pointer;
}

.speed__scale {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: 12px;
  margin-top: 4px;
}

/* ---------- Осциллограмма ---------- */

/* Есть выделение: пэды ждут нажатия, чтобы принять фрагмент */
.pads.is-armed .pad {
  outline: 2px dashed rgb(255 255 255 / 70%);
  outline-offset: 3px;
}

.wave {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
}

.wave__view {
  position: relative;
  height: 150px;
  border: 2px dashed rgb(255 255 255 / 14%);
  border-radius: 12px;
  background: #121318;
  overflow: hidden;
  transition: border-color 120ms ease, background-color 120ms ease;
}

.wave.has-buffer .wave__view {
  border-style: solid;
  border-color: transparent;
}

.wave.is-dragover .wave__view {
  border-style: dashed;
  border-color: #ff8a3d;
  background: #1f1712;
}

.wave__canvas {
  display: block;
  width: 100%;
  height: 100%;
  /* Все жесты обрабатываем сами: один палец — выделение, два — зум и прокрутка */
  touch-action: none;
  user-select: none;
}

.wave__placeholder {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  color: var(--muted);
  font-size: 14px;
  text-align: center;
  pointer-events: none;
}

.wave.has-buffer:not(.is-dragover):not(.is-loading) .wave__placeholder {
  display: none;
}

/* Поверх загруженной волны подсказку при перетаскивании нужно отделить от фона */
.wave.has-buffer .wave__placeholder {
  background: rgb(18 19 24 / 85%);
  color: var(--text);
}

.wave__pick {
  border: none;
  background: none;
  color: #ff9a52;
  font: inherit;
  text-decoration: underline;
  cursor: pointer;
  pointer-events: auto;
}

.wave.is-loading .wave__pick,
.wave.is-dragover .wave__pick {
  display: none;
}

.wave__play {
  position: absolute;
  left: 6px;
  bottom: 6px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: rgb(0 0 0 / 35%);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  touch-action: manipulation;
}

.wave__play:hover {
  background: rgb(0 0 0 / 55%);
}

.wave__play:disabled {
  display: none;
}

.wave__play svg {
  width: 16px;
  height: 16px;
  fill: currentColor;
}

.wave__time {
  position: absolute;
  right: 10px;
  bottom: 8px;
  color: rgb(255 255 255 / 75%);
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgb(0 0 0 / 60%);
  pointer-events: none;
}

/* Полоса прокрутки при зуме. Своя, а не нативная: на мобильных нативная
   скрыта или слишком тонкая для пальца */
.wave__scroll {
  position: relative;
  height: 12px;
  margin-top: 8px;
  border-radius: 6px;
  background: rgb(255 255 255 / 6%);
  cursor: pointer;
  touch-action: none;
}

.wave__scroll[hidden] {
  display: none;
}

.wave__thumb {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  border-radius: 6px;
  background: rgb(255 154 82 / 55%);
}

.wave__scroll:hover .wave__thumb,
.wave__scroll:active .wave__thumb {
  background: rgb(255 154 82 / 85%);
}

.wave__replace {
  display: none;
  margin: 6px 2px 0;
  padding: 0;
  border: none;
  background: none;
  color: #ff9a52;
  font: inherit;
  font-size: 13px;
  text-decoration: underline;
  cursor: pointer;
}

.wave.has-buffer .wave__replace {
  display: block;
}

.wave__replace:hover {
  color: #ffb47e;
}

.wave__info {
  margin: 10px 2px 0;
  min-height: 1.4em;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

/* ---------- Мобильная версия ---------- */

@media (max-width: 600px) {
  :root {
    --gap: 12px;
    --radius: 14px;
  }

  .sampler {
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom));
    gap: 20px;
  }

  .sampler__title {
    font-size: 22px;
  }

  .pads {
    grid-template-columns: repeat(2, 1fr);
  }

  .wave__view {
    height: 120px;
  }

  .wave__scroll {
    height: 18px;
    border-radius: 9px;
  }

  .wave__thumb {
    border-radius: 9px;
  }

  /* На тач-устройствах физической клавиатуры обычно нет */
  .sampler__hint .keys-only,
  .pad__key {
    display: none;
  }
}

@media (max-width: 600px) and (orientation: landscape) {
  .pads {
    grid-template-columns: repeat(4, 1fr);
  }

  .pad {
    aspect-ratio: 4 / 3;
  }
}
