:root {
  color-scheme: dark;
  --bg: #0b0d10;
  --tile-bg: #16191d;
  --tile-border: #2a2f36;
  --accent: #3ddc97;
  --accent-dim: #1f6b4d;
  --text: #eef1f4;
  --text-dim: #8b939d;
  --danger: #e5484d;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
}

body {
  display: flex;
  flex-direction: column;
}

#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  height: clamp(38px, 5.5vh, 54px);
  background: #101317;
  border-bottom: 1px solid var(--tile-border);
}

#brand {
  font-weight: 600;
  font-size: clamp(13px, 1.9vh, 18px);
  color: var(--text-dim);
  letter-spacing: 0.02em;
}

#topbar button {
  margin-left: auto;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.09);
  color: var(--text);
  font: inherit;
  font-size: clamp(12px, 1.8vh, 16px);
  padding: 7px 16px;
  cursor: pointer;
}

#topbar button:hover {
  background: rgba(255, 255, 255, 0.16);
}

#grid {
  flex: 1 1 0;
  min-height: 0;
  display: grid;
  width: 100%;
  gap: 6px;
  padding: 6px;
}

.grid-message {
  align-self: center;
  justify-self: center;
  max-width: 40ch;
  text-align: center;
  color: var(--text-dim);
  font-size: clamp(13px, 2vh, 18px);
  line-height: 1.5;
}

.tile {
  --tile-hue: 210;
  background: hsl(var(--tile-hue) 55% 26%);
  border: 1px solid hsl(var(--tile-hue) 65% 45%);
  box-shadow: 0 0 26px -8px hsl(var(--tile-hue) 80% 55%);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  padding: clamp(6px, 1vh, 14px);
  gap: clamp(4px, 0.8vh, 10px);
}

.tile--0 { --tile-hue: 210; }
.tile--1 { --tile-hue: 285; }
.tile--2 { --tile-hue: 150; }
.tile--3 { --tile-hue: 32; }
.tile--4 { --tile-hue: 345; }
.tile--5 { --tile-hue: 190; }
.tile--6 { --tile-hue: 55; }
.tile--7 { --tile-hue: 250; }

.tile-body {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: stretch;
  gap: clamp(6px, 1vw, 14px);
}

.tile-main {
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: clamp(4px, 0.8vh, 10px);
}

.title {
  flex: 0 0 auto;
  width: 100%;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: clamp(14px, 2.4vh, 30px);
  font-weight: 600;
  text-align: center;
  padding: 4px 6px;
  border-radius: 6px;
  user-select: text;
}

.title:focus {
  background: rgba(255, 255, 255, 0.12);
}

.wave-row {
  flex: 1 1 0;
  min-height: 0;
  display: flex;
  align-items: stretch;
  gap: clamp(4px, 0.7vw, 10px);
}

.waveform-wrap {
  position: relative;
  flex: 1 1 0;
  min-width: 0;
  min-height: 0;
  background: #0e1013;
  border-radius: 8px;
  overflow: hidden;
}

.waveform {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
  touch-action: none;
}

.time {
  position: absolute;
  top: 6px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  padding: 0 10px;
  font-size: clamp(10px, 1.4vh, 16px);
  color: var(--text-dim);
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}

.loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: clamp(11px, 1.6vh, 16px);
  background: #0e1013;
}

.loading.hidden {
  display: none;
}

/* Solange das Stück nicht vollständig geladen ist, bleibt es unbedienbar */
.tile.is-loading .btn,
.tile.is-loading .fader {
  opacity: 0.4;
  pointer-events: none;
}

.controls {
  flex: 1.6 1 0;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: clamp(4px, 0.8vw, 12px);
}

.btn {
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  font-size: clamp(20px, 5vh, 56px);
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-height: 0;
  min-width: 0;
}

.btn:active {
  background: rgba(255, 255, 255, 0.24);
}

.btn-playpause {
  background: hsl(var(--tile-hue) 60% 42%);
  border-color: hsl(var(--tile-hue) 85% 68%);
  box-shadow: 0 0 0 2px hsl(var(--tile-hue) 85% 68% / 0.4), 0 0 22px -4px hsl(var(--tile-hue) 90% 55%);
  color: #ffffff;
  font-size: clamp(28px, 8vh, 80px);
}

.btn-playpause:active {
  background: hsl(var(--tile-hue) 70% 55%);
  color: #06110c;
}

.btn-stop:active {
  background: var(--danger);
  border-color: var(--danger);
}

/* Fade-Buttons links/rechts der Wellenform */
.btn-fade {
  flex: 0 0 auto;
  width: clamp(30px, 3.4vw, 54px);
  font-size: clamp(14px, 2.6vh, 30px);
  color: var(--text-dim);
  background: rgba(255, 255, 255, 0.07);
}

.btn-fade.is-fading {
  color: #06110c;
  background: hsl(var(--tile-hue) 70% 58%);
  border-color: hsl(var(--tile-hue) 85% 72%);
  box-shadow: 0 0 20px -4px hsl(var(--tile-hue) 90% 60%);
}

/* Vertikaler Fader am rechten Rand der Kachel */
.fader {
  /* Spaltenbreite; Thumb und Sprossen leiten sich davon ab */
  --fader-w: clamp(38px, 4.2vw, 66px);
  /* Thumb-Grafik ist 119x176 px */
  --thumb-w: calc(var(--fader-w) * 0.68);
  --thumb-h: calc(var(--thumb-w) * 1.479);
  /* 0 = leise (unten), 1 = laut (oben) */
  --value: 1;

  position: relative;
  flex: 0 0 auto;
  width: var(--fader-w);
  align-self: stretch;
  border-radius: 8px;
  background-color: #232527;
  background-image:
    /* Mittellinie */
    linear-gradient(#8e8e8e, #8e8e8e),
    /* Sprossen */
    repeating-linear-gradient(to bottom, #6f6f6f 0 2px, transparent 2px 4.8%);
  background-repeat: no-repeat;
  background-size:
    max(2px, 6%) 100%,
    86% 100%;
  background-position: center, center;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.5);
  cursor: pointer;
  touch-action: none;
  overflow: hidden;
}

.fader:focus-visible {
  outline: 2px solid hsl(var(--tile-hue) 85% 68%);
  outline-offset: 2px;
}

.fader-thumb {
  position: absolute;
  left: 50%;
  width: var(--thumb-w);
  height: var(--thumb-h);
  /* Thumb-Mitte wandert zwischen unterem und oberem Anschlag */
  top: calc((1 - var(--value)) * (100% - var(--thumb-h)));
  transform: translateX(-50%);
  background-image: url("../img/schieber.webp");
  background-size: 100% 100%;
  background-repeat: no-repeat;
  pointer-events: none;
}

/* Nur für Sprünge; während des Fades wird --value pro Frame gesetzt */
.fader--glide .fader-thumb {
  transition: top 0.28s ease-out;
}

/* ------------------------------------------------------------------------- */
/*  Konfigurationsfenster                                                     */
/* ------------------------------------------------------------------------- */

#config-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.68);
}

#config-overlay[hidden] {
  display: none;
}

#config-box {
  display: flex;
  flex-direction: column;
  width: min(680px, 100%);
  max-height: min(88vh, 900px);
  background: #14181d;
  border: 1px solid var(--tile-border);
  border-radius: 14px;
  box-shadow: 0 24px 70px -12px rgba(0, 0, 0, 0.8);
  overflow: hidden;
}

#config-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--tile-border);
}

#config-head h2 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

#config-close {
  margin-left: auto;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-dim);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
}

#config-close:hover {
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
}

#config-body {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 4px 18px 18px;
}

#config-note {
  margin: 14px 0 0;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid #6b5a1f;
  background: #2a2410;
  color: #e8d9a0;
  font-size: 13px;
  line-height: 1.5;
}

.cfg-section {
  margin-top: 22px;
}

.cfg-section h3 {
  margin: 0 0 4px;
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.cfg-hint {
  margin: 0 0 10px;
  font-size: 12.5px;
  color: var(--text-dim);
  line-height: 1.5;
}

/* --- Liste der Stücke --- */

.cfg-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.07);
}

.cfg-active {
  flex: 0 0 auto;
  width: 20px;
  height: 20px;
  accent-color: var(--accent);
  cursor: pointer;
}

.cfg-move {
  flex: 0 0 auto;
  display: flex;
  gap: 2px;
}

.cfg-move button {
  width: 30px;
  height: 30px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-dim);
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
}

.cfg-move button:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.16);
  color: var(--text);
}

.cfg-move button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.cfg-move button:disabled {
  opacity: 0.25;
  cursor: default;
}

.cfg-name {
  flex: 1 1 auto;
  min-width: 0;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  padding: 6px 8px;
  color: var(--text);
  font: inherit;
  font-size: 14px;
  user-select: text;
}

.cfg-name:hover:not(:disabled) {
  border-color: rgba(255, 255, 255, 0.16);
}

.cfg-name:focus {
  outline: none;
  border-color: var(--accent);
  background: rgba(255, 255, 255, 0.06);
}

.cfg-name:disabled {
  color: var(--text-dim);
}

.cfg-size {
  flex: 0 0 auto;
  font-size: 12px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.cfg-del {
  flex: 0 0 auto;
  width: 32px;
  height: 32px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text-dim);
  font-size: 15px;
  cursor: pointer;
}

.cfg-del:hover:not(:disabled) {
  background: rgba(229, 72, 77, 0.18);
  border-color: var(--danger);
  color: #ff9a9d;
}

.cfg-del:disabled {
  opacity: 0.3;
  cursor: default;
}

/* --- Upload --- */

#cfg-drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 26px 16px;
  border: 2px dashed rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  color: var(--text-dim);
  font-size: 13px;
  text-align: center;
  cursor: pointer;
}

#cfg-drop strong {
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

#cfg-drop:hover,
#cfg-drop:focus-visible {
  border-color: rgba(255, 255, 255, 0.35);
  outline: none;
}

#cfg-drop.is-over {
  border-color: var(--accent);
  background: rgba(61, 220, 151, 0.1);
}

#cfg-drop.is-disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

#cfg-upload-status {
  min-height: 20px;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

#cfg-upload-status.is-error {
  color: #ff9a9d;
}

/* --- Einstellungen --- */

.cfg-row-setting {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 0;
  font-size: 14px;
}

.cfg-row-setting > span {
  flex: 0 0 130px;
}

.cfg-row-setting input[type="range"] {
  flex: 1 1 auto;
  min-width: 0;
  accent-color: var(--accent);
}

.cfg-row-setting output {
  flex: 0 0 48px;
  text-align: right;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

@media (max-width: 700px) {
  .title { font-size: clamp(12px, 3vh, 22px); }
  .btn { font-size: clamp(18px, 6vh, 40px); }
  .btn-fade { width: clamp(26px, 8vw, 40px); font-size: clamp(13px, 3vh, 22px); }
  .fader { --fader-w: clamp(32px, 9vw, 48px); }
  .cfg-row-setting > span { flex: 0 0 100px; font-size: 13px; }
  .cfg-size { display: none; }
}
