:root {
  --bg: #0f1115;
  --bg2: #171a21;
  --tile: #1e2330;
  --tile-active: #2b3550;
  --text: #e8eaf0;
  --muted: #8b93a7;
  --accent: #5c86ff;
  --danger: #ff5c6c;
  --radius: 14px;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}
body { display: flex; flex-direction: column; }
button, a, input, select { touch-action: manipulation; }
[hidden] { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Topbar ---------- */
#topbar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--bg2);
}
.searchwrap {
  flex: 1; min-width: 0; display: flex; align-items: center; gap: 6px;
  background: var(--tile); border-radius: 999px; padding: 6px 12px;
}
.searchwrap:focus-within { outline: 2px solid var(--accent); }
.searchwrap .icon { width: 18px; height: 18px; fill: var(--muted); flex: none; }
#search {
  flex: 1; min-width: 0; background: none; border: none; outline: none;
  color: var(--text); font-size: 16px;
}
#search::-webkit-search-cancel-button { display: none; }
button.ghost {
  background: none; border: none; color: var(--text);
  padding: 10px; border-radius: 10px; cursor: pointer; line-height: 0;
  font-size: 20px;
}
button.ghost:active { background: var(--tile); }
.icon { width: 22px; height: 22px; fill: currentColor; }
#btnStop { color: var(--danger); }
#clearSearch { font-size: 18px; padding: 8px 10px; margin: -6px -4px; color: var(--muted); }

button:focus-visible, .tile:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

/* Schmale Screens (iPhone): Buttons kompakter, damit die Leiste nie overflowed
   (mit Studio- + Stop-Button sind es bis zu 5 Icons neben der Suche) */
@media (max-width: 480px) {
  #topbar { gap: 0; padding-left: 8px; padding-right: 4px; }
  #topbar button.ghost { padding: 9px 5px; }
  #topbar button.ghost .icon { width: 21px; height: 21px; }
  .searchwrap { padding: 6px 10px; }
  #search { font-size: 16px; } /* nicht kleiner: <16px zoomt iOS beim Fokus rein */
}
@media (max-width: 350px) {
  #topbar button.ghost { padding: 9px 3px; }
  #topbar button.ghost .icon { width: 19px; height: 19px; }
}

/* ---------- Board / Seiten ---------- */
#board {
  flex: 1; display: flex; overflow-x: auto; overflow-y: hidden;
  scroll-snap-type: x mandatory; scroll-behavior: smooth;
  scrollbar-width: none;
}
#board::-webkit-scrollbar { display: none; }
.page {
  flex: none; width: 100%; height: 100%;
  scroll-snap-align: start; scroll-snap-stop: always;
  display: grid;
  gap: var(--gap, 8px); padding: var(--gap, 8px);
  grid-template-columns: repeat(var(--cols), 1fr);
  grid-template-rows: repeat(var(--rows), 1fr);
  align-content: stretch;
}
.tile {
  border: none; border-radius: var(--radius);
  background: var(--tile); color: var(--text);
  font-size: var(--tilefont, 14px); font-weight: 600;
  padding: 2px 4px; cursor: pointer;
  display: flex; align-items: center; justify-content: center; text-align: center;
  overflow: hidden; word-break: break-word; hyphens: auto;
  position: relative; user-select: none; -webkit-user-select: none;
  transition: transform .08s ease, background .15s ease, filter .15s ease;
  touch-action: pan-x;
}
.tile:active { transform: scale(.94); }
.tile.playing {
  background: var(--tile-active);
  outline: 2px solid var(--accent);
  animation: pulse 1s ease-in-out infinite;
}
.tile.flash { outline: 3px solid var(--accent); animation: pulse .8s ease-in-out 4; }
@keyframes pulse { 50% { outline-color: transparent; } }
.tile .label { position: relative; z-index: 1; }
.tile .fav {
  position: absolute; top: 4px; right: 6px; font-size: 11px; opacity: .9;
  color: inherit;
}
.tile .dur {
  position: absolute; bottom: 4px; left: 7px;
  font-size: 9px; font-weight: 400; color: inherit; opacity: .6;
}
.tile .prog {
  position: absolute; left: 0; bottom: 0; height: 3px; width: 0;
  background: var(--accent); border-radius: 0 2px 2px 0;
  pointer-events: none;
}
.tile.playing .prog { box-shadow: 0 0 8px var(--accent); }
.tile .eq {
  position: absolute; top: 5px; left: 7px; display: none;
  align-items: flex-end; gap: 2px; height: 12px;
}
.tile.playing .eq { display: flex; }
.tile .eq i {
  width: 3px; background: var(--accent); border-radius: 1px;
  animation: eqb .7s ease-in-out infinite;
}
.tile .eq i:nth-child(1) { height: 60%; animation-delay: 0s; }
.tile .eq i:nth-child(2) { height: 100%; animation-delay: .2s; }
.tile .eq i:nth-child(3) { height: 40%; animation-delay: .4s; }
@keyframes eqb { 50% { transform: scaleY(.35); } }
.ripple {
  position: absolute; border-radius: 50%;
  background: rgba(255,255,255,.25); pointer-events: none;
  transform: scale(0); animation: rip .45s ease-out forwards;
}
@keyframes rip { to { transform: scale(1); opacity: 0; } }
/* Dichte Grids: Badges ausblenden */
body[data-dense] .tile .dur, body[data-dense] .tile .eq, body[data-dense] .tile .fav { display: none; }
.tile.user::after {
  content: "eigen"; position: absolute; bottom: 3px; right: 6px;
  font-size: 9px; font-weight: 400; color: inherit; opacity: .55;
}
body[data-dense] .tile.user::after { display: none; }

/* Leer-/Lade-Zustand */
.boardstate {
  flex: none; width: 100%; height: 100%;
  display: flex; flex-direction: column; gap: 10px;
  align-items: center; justify-content: center;
  color: var(--muted); font-size: 15px; text-align: center; padding: 24px;
}
.boardstate b { color: var(--text); }

/* ---------- Seiten-Punkte ---------- */
#pagedots {
  display: flex; justify-content: center; align-items: center; gap: 0;
  padding: 0 0 calc(env(safe-area-inset-bottom) + 6px);
  background: var(--bg); min-height: 34px;
}
#pagedots button.dot {
  width: 9px; height: 9px; border-radius: 50%;
  background: #4a5470; border: none; cursor: pointer; padding: 0;
  box-sizing: content-box; background-clip: content-box;
  padding: 12px 5px; transition: all .2s;
}
#pagedots button.dot.on { background-color: var(--accent); width: 20px; border-radius: 5px; }
#pagedots .pagetext { color: var(--muted); font-size: 13px; padding: 8px 12px; }

/* ---------- Desktop-Pfeile ---------- */
.navarrow {
  position: fixed; top: 50%; transform: translateY(-50%);
  width: 40px; height: 64px; border: none; border-radius: 12px;
  background: rgba(30,35,48,.7); color: var(--text);
  font-size: 28px; cursor: pointer; z-index: 4;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
#navPrev { left: 6px; }
#navNext { right: 6px; }
@media (hover: hover) and (pointer: fine) {
  body:hover .navarrow { opacity: .8; pointer-events: auto; }
  .navarrow:hover { background: var(--tile-active); }
  .tile:hover { filter: brightness(1.18); }
  button.ghost:hover { background: var(--tile); }
  #btnAdd:hover { filter: brightness(1.12); }
  #pagedots button.dot:hover { background-color: var(--muted); }
  #pagedots button.dot.on:hover { background-color: var(--accent); }
}

/* ---------- FAB ---------- */
#btnAdd {
  position: fixed; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 38px);
  width: 54px; height: 54px; border-radius: 50%;
  border: none; background: var(--accent); color: #fff;
  font-size: 30px; line-height: 0; cursor: pointer;
  box-shadow: 0 4px 16px rgba(0,0,0,.45);
  z-index: 5;
}
#btnAdd:active { transform: scale(.92); }

/* ---------- Sheets ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: flex; align-items: flex-end; justify-content: center; z-index: 10;
}
.sheet {
  background: var(--bg2); width: 100%; max-width: 480px;
  border-radius: 18px 18px 0 0;
  padding: 18px 18px calc(env(safe-area-inset-bottom) + 18px);
  max-height: 85vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 14px;
}
.sheet.tall { height: 85vh; }
/* Desktop/Tablet: Modals mittig statt als Bottom-Sheet */
@media (min-width: 600px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 18px; max-height: 80vh; }
  .sheet.tall { height: auto; max-height: 80vh; }
}
.sheet h2 { margin: 0; font-size: 18px; }
.sheet label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.sheet label.row { flex-direction: row; align-items: center; gap: 10px; color: var(--text); }
.sheet input[type=range] { width: 100%; accent-color: var(--accent); }
.sheet input[type=checkbox] { accent-color: var(--accent); width: 20px; height: 20px; }
.sheet input[type=text], .sheet input[type=color], .sheet input[type=number], .sheet select {
  background: var(--tile); border: none; border-radius: 8px;
  color: var(--text); padding: 10px; font-size: 16px; width: 100%;
}
.sheet input[type=color] { height: 44px; padding: 4px; }
.sliderrow { display: flex; align-items: center; gap: 10px; }
.sliderrow input[type=number] { width: 72px; flex: none; text-align: center; }
.sheet button {
  background: var(--tile); color: var(--text); border: none;
  border-radius: 10px; padding: 12px; font-size: 15px; cursor: pointer;
}
.sheet button.primary { background: var(--accent); color: #fff; font-weight: 600; }
.sheet button.danger { background: var(--danger); color: #fff; }
.sheet b { color: var(--text); }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.detailbtns { display: flex; gap: 8px; }
.detailbtns button { flex: 1; }

/* ---------- Export-Liste ---------- */
.exportactions { display: flex; align-items: center; gap: 12px; }
#exportProgress { color: var(--muted); font-size: 13px; }
#exportList {
  list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
}
#exportList li {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--tile); border-radius: 8px; padding: 4px 4px 4px 12px; font-size: 14px;
  gap: 8px;
}
#exportList li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#exportList a {
  color: var(--accent); text-decoration: none; font-weight: 600; flex: none;
  padding: 10px 16px; border-radius: 8px;
}
#exportList a:active { background: var(--tile-active); }

/* ---------- Studio ---------- */
#studio {
  position: fixed; inset: 0; z-index: 20; background: var(--bg);
  display: flex; flex-direction: column;
}
.studiobar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--bg2);
}
.studiobar h2 { margin: 0; font-size: 17px; }
.studiobody {
  flex: 1; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 14px;
  max-width: 720px; width: 100%; margin: 0 auto;
}
.urlrow { display: flex; gap: 8px; }
.urlrow input {
  flex: 1; min-width: 0; background: var(--tile); border: none; border-radius: 10px;
  color: var(--text); padding: 12px; font-size: 16px;
}
.urlrow button, .studiorow button {
  background: var(--tile); color: var(--text); border: none;
  border-radius: 10px; padding: 12px 16px; font-size: 15px; cursor: pointer;
}
.urlrow button.primary, .studiorow button.primary { background: var(--accent); color: #fff; font-weight: 600; }
#studioEditor { display: flex; flex-direction: column; gap: 14px; }
#studioEditor h3 { margin: 0; font-size: 15px; color: var(--muted); font-weight: 500; }
#studioEditor label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
#studioEditor input[type=text] {
  background: var(--tile); border: none; border-radius: 8px;
  color: var(--text); padding: 10px; font-size: 16px;
}
#studioEditor input[type=range] { accent-color: var(--accent); }
#studioEditor b { color: var(--text); }
#waveWrap {
  position: relative; height: 140px; background: var(--bg2);
  border-radius: 12px; overflow: hidden; touch-action: none;
}
#wave { position: absolute; inset: 0; width: 100%; height: 100%; }
#selBox {
  position: absolute; top: 0; bottom: 0;
  background: rgba(92,134,255,.18);
  border-left: 1px solid var(--accent); border-right: 1px solid var(--accent);
  pointer-events: none;
}
.handle {
  position: absolute; top: 0; bottom: 0; width: 28px; margin-left: -14px;
  cursor: ew-resize; touch-action: none;
}
.handle::after {
  content: ""; position: absolute; left: 50%; top: 0; bottom: 0;
  width: 4px; margin-left: -2px; background: var(--accent); border-radius: 2px;
}
.handle::before {
  content: ""; position: absolute; left: 50%; top: 50%;
  width: 16px; height: 34px; margin: -17px 0 0 -8px;
  background: var(--accent); border-radius: 8px; opacity: .95;
}
#playhead {
  position: absolute; top: 0; bottom: 0; width: 2px;
  background: #fff; opacity: .9; display: none; pointer-events: none;
}
.studiorow { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.studiorow button { flex: none; }
.zoomrow { justify-content: flex-start; flex-wrap: wrap; }
.zoomrow button { min-width: 44px; padding: 10px 12px; }
.timefields { display: flex; align-items: center; gap: 6px; color: var(--muted); flex-wrap: wrap; }
.timefields input {
  width: 84px; background: var(--tile); border: none; border-radius: 8px;
  color: var(--text); padding: 10px 8px; font-size: 16px; text-align: center;
}
.handle:focus-visible { outline: 2px solid #fff; outline-offset: -2px; border-radius: 6px; }

/* ---------- Toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 90px);
  transform: translate(-50%, 20px); z-index: 30;
  background: var(--bg2); color: var(--text);
  border: 1px solid var(--tile-active); border-radius: 12px;
  padding: 12px 18px; font-size: 14px; max-width: 90vw;
  opacity: 0; pointer-events: none;
  transition: opacity .25s, transform .25s;
  box-shadow: 0 6px 24px rgba(0,0,0,.5);
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Reduzierte Bewegung ---------- */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
  #board { scroll-behavior: auto; }
}
