:root {
  --bg: #0a0a0a;
  --card: #1f1f1f;
  --text: #fff;
  --text-dim: rgba(255, 255, 255, 0.38);
  --accent: rgb(71, 184, 255);
  --play: rgb(56, 133, 245);
  --save: rgb(56, 158, 97);
  --library-border: rgba(255, 165, 0, 0.55);
  --cell-off: #333;
  --cell-off-muted: #242424;
  --row-gap: 4px;
  --col-gap: 3px;
  --divider-w: 5px;
  --label-w: 88px;
  --mute-w: 36px;
}

/* Drums — teal (wie App) */
[data-kit="0"] {
  --accent: rgb(61, 184, 168);
}
/* Organic — violett (wie App) */
[data-kit="1"] {
  --accent: rgb(184, 132, 250);
}

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

html {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.app {
  min-height: 100%;
  padding: 12px 10px 24px;
  padding-left: max(10px, env(safe-area-inset-left));
  padding-right: max(10px, env(safe-area-inset-right));
  padding-bottom: max(24px, env(safe-area-inset-bottom));
  max-width: 1100px;
  margin: 0 auto;
}

.top-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  margin-bottom: 14px;
  padding: 0 8px;
}

.top-bar__title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
}

.kit-row {
  display: flex;
  gap: 4px;
  margin-bottom: 14px;
  padding: 0 8px;
}

.kit-btn {
  flex: 1;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 9px 6px;
  font-size: 12px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.58);
  background: rgba(255, 255, 255, 0.04);
  cursor: pointer;
  min-height: 44px;
}

.kit-btn[aria-selected="true"] {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
  border-width: 2px;
  border-color: color-mix(in srgb, var(--accent) 100%, transparent);
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--accent) 32%, transparent);
}

.studio-card {
  background: var(--card);
  border-radius: 12px;
  padding: 10px;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}

.toolbar__pattern-title {
  font: 600 13px ui-monospace, monospace;
  color: var(--accent);
  width: 100px;
  min-width: 72px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar__steps {
  display: flex;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.15);
}

.seg {
  width: 48px;
  min-height: 36px;
  border: none;
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.55);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.seg--active {
  background: rgba(255, 255, 255, 0.18);
  color: #fff;
}

.bpm-block {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 5px 12px;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 6px;
  user-select: none;
  touch-action: pan-y;
}

.bpm-block__label {
  font-size: 9px;
  font-weight: 500;
  color: var(--text-dim);
  min-width: 28px;
}

.bpm-arrow {
  width: 34px;
  height: 32px;
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.92);
  font-size: 18px;
  font-weight: 600;
  cursor: pointer;
  line-height: 1;
  padding: 0;
}

.bpm-arrow:disabled {
  color: rgba(255, 255, 255, 0.3);
  cursor: default;
}

.bpm-block__value {
  font: 700 16px ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  width: 52px;
  text-align: center;
}

.toolbar__grow {
  flex: 1;
  min-width: 8px;
}

.toolbar__grow--sm {
  flex: 0;
  min-width: 4px;
}

.btn-play {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.25);
  background: var(--play);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.btn-play__icon {
  font-size: 11px;
  margin-left: 2px;
}

.btn-clear {
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  padding: 7px 12px;
  min-height: 36px;
  cursor: pointer;
  white-space: nowrap;
  background: transparent;
  color: rgba(255, 255, 255, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.28);
}

.btn-clear:hover {
  border-color: rgba(255, 120, 120, 0.55);
  color: rgb(255, 170, 170);
}

.btn-save,
.btn-export,
.btn-library {
  font-size: 12px;
  font-weight: 700;
  border: none;
  border-radius: 999px;
  padding: 7px 14px;
  min-height: 36px;
  cursor: pointer;
  white-space: nowrap;
}

.btn-save {
  background: var(--save);
  color: #fff;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.22);
}

.btn-export {
  background: rgb(45, 55, 72);
  color: rgba(255, 255, 255, 0.92);
  box-shadow: inset 0 0 0 1px rgba(120, 160, 255, 0.45);
}

.btn-library {
  background: rgb(51, 51, 51);
  color: rgba(255, 255, 255, 0.92);
  box-shadow: inset 0 0 0 1.2px var(--library-border);
}

.export-modal__hint code {
  font-size: 11px;
  opacity: 0.9;
}

.export-modal__actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.btn-export-choice {
  width: 100%;
  text-align: center;
  font-size: 14px;
  font-weight: 600;
  padding: 12px 14px;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
}

.btn-export-choice:hover {
  background: rgba(255, 255, 255, 0.12);
}

.grid-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.sequencer {
  padding: 4px 0;
  min-width: min-content;
}

.seq-row {
  display: flex;
  align-items: center;
  gap: var(--col-gap);
  margin-bottom: var(--row-gap);
}

.seq-row--header {
  margin-bottom: var(--row-gap);
}

.seq-label {
  width: var(--label-w);
  min-width: var(--label-w);
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  color: var(--accent);
  line-height: 1.15;
  word-break: break-word;
  flex-shrink: 0;
}

.seq-label--muted {
  opacity: 0.4;
}

.seq-label--header {
  height: 12px;
  min-height: 12px;
}

.seq-step-num {
  width: var(--cell, 24px);
  min-width: var(--cell, 24px);
  height: 12px;
  font-size: 9px;
  font-weight: 500;
  font-family: ui-monospace, monospace;
  color: var(--text-dim);
  text-align: center;
  line-height: 12px;
  flex-shrink: 0;
}

.seq-divider {
  width: var(--divider-w);
  min-width: var(--divider-w);
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  align-self: stretch;
}

.seq-divider::after {
  content: "";
  width: 1.5px;
  background: rgba(255, 255, 255, 0.78);
  border-radius: 1px;
  margin: 1px 0;
}

.seq-divider--header {
  height: 12px;
  align-self: auto;
}

.seq-divider--header::after {
  margin: 0;
  height: 10px;
}

.seq-cell {
  width: var(--cell, 24px);
  min-width: var(--cell, 24px);
  height: var(--cell, 24px);
  border-radius: 5px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: var(--cell-off);
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
  -webkit-appearance: none;
  appearance: none;
}

.seq-cell--track-muted:not(.seq-cell--on) {
  background: var(--cell-off-muted);
}

/* Velocity: leise / normal / Akzent — wie App (Füllung + Rahmen) */
.seq-cell--on.seq-cell--vel-soft {
  background: color-mix(in srgb, var(--accent) 45%, transparent);
  border-color: rgba(255, 255, 255, 0.4);
  border-width: 1px;
}

.seq-cell--on.seq-cell--vel-normal {
  background: color-mix(in srgb, var(--accent) 82%, transparent);
  border-color: rgba(255, 255, 255, 0.85);
  border-width: 1.5px;
}

.seq-cell--on.seq-cell--vel-accent {
  background: color-mix(in srgb, var(--accent) 82%, transparent);
  border-color: rgb(255, 149, 0);
  border-width: 2.5px;
}

.seq-cell--on.seq-cell--track-muted.seq-cell--vel-soft {
  background: color-mix(in srgb, var(--accent) 20%, transparent);
  border-color: rgba(255, 255, 255, 0.22);
}

.seq-cell--on.seq-cell--track-muted.seq-cell--vel-normal {
  background: color-mix(in srgb, var(--accent) 37%, transparent);
  border-color: rgba(255, 255, 255, 0.45);
}

.seq-cell--on.seq-cell--track-muted.seq-cell--vel-accent {
  background: color-mix(in srgb, var(--accent) 37%, transparent);
  border-color: rgba(255, 149, 0, 0.55);
}

.seq-cell--playhead {
  border-width: 2.5px !important;
  border-color: #fff !important;
}

.seq-mute-head {
  width: var(--mute-w);
  min-width: var(--mute-w);
  font-size: 7px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.25);
  text-align: center;
  flex-shrink: 0;
}

.seq-mute {
  width: var(--mute-w);
  min-width: var(--mute-w);
  height: var(--cell, 24px);
  border: none;
  background: transparent;
  color: rgba(255, 255, 255, 0.5);
  cursor: pointer;
  font-size: 14px;
  flex-shrink: 0;
  padding: 0;
  line-height: 1;
}

.seq-mute--active {
  color: rgba(255, 80, 80, 0.95);
}

/* ——— Standardbeats ——— */
.factory-beats {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.factory-beats__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  margin-bottom: 12px;
}

.factory-beats__title {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: rgba(255, 255, 255, 0.88);
}

.factory-beats__actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-left: auto;
}

.btn-slot-save,
.btn-factory-export,
.btn-factory-import {
  font-size: 12px;
  font-weight: 600;
  border-radius: 999px;
  padding: 7px 12px;
  min-height: 34px;
  cursor: pointer;
  white-space: nowrap;
  border: 1px solid rgba(255, 255, 255, 0.22);
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.88);
}

.btn-slot-save:disabled {
  opacity: 0.35;
  cursor: default;
}

.btn-slot-save:not(:disabled) {
  border-color: rgba(56, 158, 97, 0.55);
  background: rgba(56, 158, 97, 0.22);
  color: #fff;
}

.factory-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 10px 12px;
}

.factory-col {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.factory-col__head {
  text-align: center;
  padding: 4px 2px 6px;
}

.factory-col__kit {
  display: block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.factory-col--drums .factory-col__kit {
  color: rgb(61, 184, 168);
}

.factory-col--organic .factory-col__kit {
  color: rgb(184, 132, 250);
}

.factory-col__meter {
  display: block;
  margin-top: 2px;
  font-size: 10px;
  font-weight: 600;
  color: var(--text-dim);
}

.factory-slot {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 48px;
  border-radius: 10px;
  border: 1.5px solid rgba(255, 255, 255, 0.16);
  background: rgba(255, 255, 255, 0.04);
  color: #fff;
  cursor: pointer;
  padding: 6px 4px;
  -webkit-appearance: none;
  appearance: none;
}

.factory-col--drums .factory-slot {
  border-color: rgba(61, 184, 168, 0.35);
}

.factory-col--organic .factory-slot {
  border-color: rgba(184, 132, 250, 0.35);
}

.factory-slot__name {
  font-size: 14px;
  font-weight: 700;
  line-height: 1.1;
}

.factory-slot__bpm {
  font-size: 10px;
  font-weight: 500;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.factory-slot--active {
  background: rgba(255, 255, 255, 0.1);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.2);
}

.factory-col--drums .factory-slot--active {
  border-color: rgb(61, 184, 168);
  box-shadow: 0 0 0 1px rgba(61, 184, 168, 0.35);
}

.factory-col--organic .factory-slot--active {
  border-color: rgb(184, 132, 250);
  box-shadow: 0 0 0 1px rgba(184, 132, 250, 0.35);
}

.factory-slot--edited .factory-slot__name::after {
  content: " ·";
  color: rgb(255, 165, 0);
}

@media (max-width: 640px) {
  .factory-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px 10px;
  }
}

/* Modal */
.modal[hidden] {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
}

.modal__panel {
  position: relative;
  background: #2a2a2a;
  border-radius: 14px;
  padding: 20px;
  width: 100%;
  max-width: 340px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

.modal__panel--wide {
  max-width: 420px;
  max-height: min(80vh, 560px);
  display: flex;
  flex-direction: column;
}

.modal__head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.modal__head h2 {
  margin: 0;
  font-size: 17px;
}

.btn-close-lib {
  border: none;
  background: transparent;
  color: #fff;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}

.modal__hint {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--text-dim);
}

.modal__input {
  width: 100%;
  font: 600 18px ui-monospace, monospace;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: #1a1a1a;
  color: #fff;
  margin-bottom: 16px;
}

.modal__actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

.btn-ghost {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 15px;
  cursor: pointer;
  padding: 8px 12px;
}

.btn-primary {
  background: var(--save);
  border: none;
  color: #fff;
  font-weight: 600;
  padding: 8px 18px;
  border-radius: 8px;
  cursor: pointer;
}

.library-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.library-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.library-item__main {
  flex: 1;
  min-width: 0;
  text-align: left;
  background: none;
  border: none;
  color: inherit;
  cursor: pointer;
  padding: 0;
}

.library-item__title {
  font-size: 16px;
  font-weight: 600;
  display: block;
}

.library-item__sub {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 4px;
}

.library-item__check {
  color: #ff9500;
  font-size: 18px;
}

.library-item__del {
  border: none;
  background: rgba(255, 59, 48, 0.2);
  color: #ff6b6b;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  flex-shrink: 0;
}

.library-empty {
  text-align: center;
  padding: 24px 12px;
  color: var(--text-dim);
}

.library-empty__title {
  font-size: 16px;
  font-weight: 600;
  color: #fff;
  margin: 0 0 8px;
}

.library-empty__hint {
  margin: 0;
  font-size: 14px;
  line-height: 1.4;
}

/* Speichern-Feedback */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  z-index: 200;
  padding: 12px 18px;
  border-radius: 10px;
  background: rgba(28, 28, 28, 0.96);
  border: 1px solid rgba(56, 158, 97, 0.65);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.toast.toast--show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.toast[hidden] {
  display: block !important;
  visibility: hidden;
}

.btn-slot-save.btn-slot-save--ok,
.btn-save.btn-save--ok {
  border-color: rgba(56, 158, 97, 0.85);
  background: rgb(56, 158, 97);
  color: #fff;
}

/* Smartphone quer: kompaktere Toolbar, kleinere Zellen wenn nötig */
@media (max-height: 480px) and (orientation: landscape) {
  .app {
    padding-top: 6px;
    padding-bottom: 12px;
  }

  .top-bar {
    margin-bottom: 8px;
  }

  .top-bar__title {
    font-size: 16px;
  }

  .kit-row {
    margin-bottom: 8px;
  }

  .kit-btn {
    padding: 6px 4px;
    min-height: 40px;
    font-size: 11px;
  }

  .studio-card {
    padding: 8px;
  }

  .toolbar__pattern-title {
    width: 72px;
    font-size: 11px;
  }

  .seg {
    width: 40px;
    min-height: 32px;
    font-size: 12px;
  }

  .bpm-block__value {
    font-size: 14px;
    width: 46px;
  }

  :root {
    --label-w: 72px;
    --mute-w: 32px;
  }

  .seq-label {
    font-size: 10px;
  }
}

@media (max-width: 520px) {
  .toolbar {
    gap: 6px;
  }

  .toolbar__grow {
    flex-basis: 100%;
    height: 0;
    min-height: 0;
  }

  .toolbar__grow--sm {
    display: none;
  }
}
