:root { /* Root element with global styles */
  color-scheme: dark;
  font-family: system-ui, sans-serif;
  background: #000;
  color: #aaa;
  -webkit-tap-highlight-color: transparent;
}

body { margin: 0; overflow: hidden; }

#canvas { /* Canvas element */
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.intro, .noscript { /* Intro and noscript elements */
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  font-size: 20px;
  font-weight: 300;
  letter-spacing: 0.15em;
  pointer-events: none;
}

.visually-hidden { /* Visually hidden elements for accessibility */
  position: fixed;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
  pointer-events: none;
}

.settings-menu { /* Settings menu container */
  position: fixed;
  z-index: 1;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
}

.settings-menu > summary { /* Summary element for the settings menu */
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 10px;
  background: #111e;
  color: #ddd;
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.settings-menu > summary::-webkit-details-marker { /* Hide the default details marker */
  display: none;
}

.settings-menu > summary:focus-visible { /* Focus styles for the summary element */
  outline: 2px solid #ddd;
  outline-offset: 3px;
}

.settings-panel {
  box-sizing: border-box;
  width: min(320px, calc(100vw - 48px));
  max-height: 70vh;
  overflow-y: auto;
  margin-top: 8px;
  padding: 22px;
  border: 1px solid #ffffff18;
  border-radius: 18px;
  background: #151519f2;
  box-shadow: 0 16px 48px #0008;
  color: #ddd;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 13px;
  line-height: 1.5;
}

.panel-heading {
  margin-bottom: 22px;
}

.panel-heading h1 {
  margin: 0;
  color: #f4f4f5;
  font-size: 21px;
  font-weight: 500;
  letter-spacing: -0.04em;
}

.animated-title {
  position: relative;
}

.title-filter {
  position: absolute;
  pointer-events: none;
}

.animated-title .title-text {
  display: inline-block;
  transform-origin: center;
}

.title-particles {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.title-particle {
  position: absolute;
  left: var(--origin-x);
  top: var(--origin-y);
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #eee;
  margin: -1.5px 0 0 -1.5px;
  opacity: 0;
  pointer-events: none;
}

.settings-menu[open] .particles-ready .title-text {
  animation: title-disappear 5s linear infinite;
}

.settings-menu[open] .particles-ready .title-particle {
  animation:
    title-explode 5s cubic-bezier(0.45, 0, 0.55, 1) infinite,
    title-particle-wobble 5s ease-in-out infinite;
}

@keyframes title-disappear {
  0%, 100% {
    opacity: 1;
    filter: url(#title-distortion) blur(0);
    transform: translate(0, 0) rotate(0) skewX(0) scale(1, 1);
  }
  12%, 32% {
    opacity: 1;
    filter: url(#title-distortion) blur(0);
    transform: translate(0, -1px) rotate(-0.5deg) skewX(-2deg) scale(1.01, 0.97);
  }
  22%, 40% {
    opacity: 1;
    filter: url(#title-distortion) blur(0);
    transform: translate(0, 1px) rotate(0.5deg) skewX(2deg) scale(0.99, 1.04);
  }
  44% {
    opacity: 1;
    filter: url(#title-distortion) blur(0);
    transform: translate(0, -1px) rotate(-1deg) skewX(-5deg) scale(1.03, 0.95);
  }
  50% {
    opacity: 0.6;
    filter: url(#title-distortion) blur(0.5px);
    transform: translate(0, 0) rotate(0) skewX(0) scale(1, 1);
  }
  56%, 82% {
    opacity: 0;
    filter: url(#title-distortion) blur(1px);
    transform: translate(0, 0) rotate(0) skewX(0) scale(1, 1);
  }
  87% {
    opacity: 0.5;
    filter: url(#title-distortion) blur(1px);
    transform: translate(0, 0) rotate(0) skewX(0) scale(1, 1);
  }
  92% {
    opacity: 1;
    filter: url(#title-distortion) blur(0);
    transform: translate(0, 0) rotate(0) skewX(0) scale(1, 1);
  }
}

@keyframes title-explode {
  0%, 44% {
    opacity: 0;
    transform: translate(0, 0) rotate(0deg) scale(1, 1);
  }
  50% {
    opacity: 1;
    transform: translate(0, 0) rotate(0deg) scale(1, 1);
  }
  68% {
    opacity: 1;
    transform: translate(var(--travel-x), var(--travel-y)) rotate(25deg) scale(0.85, 0.65);
  }
  86% {
    opacity: 1;
    transform: translate(0, 0) rotate(0deg) scale(1, 1);
  }
  92%, 100% {
    opacity: 0;
    transform: translate(0, 0) rotate(0deg) scale(1, 1);
  }
}

@keyframes title-particle-wobble {
  0%, 50%, 86%, 100% {
    translate: 0 0;
    border-radius: 50%;
  }
  59%, 77% {
    translate: var(--wobble-x) var(--wobble-y);
    border-radius: 40% 60% 45% 55%;
  }
  68% {
    translate: calc(0px - var(--wobble-x)) calc(0px - var(--wobble-y));
    border-radius: 60% 40% 55% 45%;
  }
}

#load-sound,
#edit-region {
  width: 100%;
  min-height: 44px;
  border: 1px solid #ffffff20;
  border-radius: 10px;
  background: #ffffff0a;
  color: #eeeef2;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
}

#load-sound:disabled,
#edit-region:disabled {
  opacity: 0.5;
  cursor: wait;
}

#load-sound:focus-visible,
.slider-control input:focus-visible {
  outline: 2px solid #c4b5fd;
  outline-offset: 4px;
}

.audio-controls p {
  margin: 8px 0 0;
  color: #a4a4b2;
  font-size: 11px;
}

.audio-controls p:empty {
  display: none;
}

.waveform {
  position: fixed;
  left: 0;
  top: 50%;
  width: 100%;
  height: 160px;
  transform: translateY(-50%);
  opacity: 0.4;
  pointer-events: none; /* touches must pass through to the main canvas */
}

.slider-control {
  margin-top: 24px;
}

.slider-control label {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  font-size: 12px;
}

.slider-control output {
  color: #bcb0e4;
  font-family: ui-monospace, monospace;
  font-variant-numeric: tabular-nums;
}

.slider-control input[type="range"] {
  display: block;
  width: 100%;
  min-height: 32px;
  margin: 4px 0 0;
  accent-color: #c4b5fd;
}

#load-sound:not(:disabled) {
  animation: load-pulse 1s ease-in-out infinite;
}

@keyframes load-pulse {
  0%, 100% {
    transform: scale(1);
    background-color: #ffffff0a;
  }
  50% {
    transform: scale(1.02);
    background-color: #ffffff16;
  }
}

#menu-guide {
  position: fixed;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  opacity: 1;
  transition: opacity 0.6s ease;
}

#menu-guide.dismissed {
  opacity: 0;
}

.guide-particle {
  position: absolute;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #eee;
  box-shadow: 0 0 4px #ffffff40;
  transform: translate(-50%, -50%);
}

@media (prefers-reduced-motion: reduce) {
  .settings-menu[open] .particles-ready .title-text,
  .settings-menu[open] .particles-ready .title-particle,
  #load-sound:not(:disabled) {
    animation: none;
  }

  #menu-guide {
    transition: none;
  }
}

#edit-region {
  margin-top: 10px;
}

[hidden] {
  display: none !important;
}

.region-editor {
  position: fixed;
  inset: 0;
  pointer-events: none; /* only the handles and OK button take touches */
}

.region-shade {
  position: absolute;
  top: calc(50% - 80px);
  height: 160px;
  background: #000a;
}

#shade-start { left: 0; }
#shade-end { right: 0; }

.region-handle {
  position: absolute;
  top: calc(50% - 120px);
  width: 44px;
  height: 240px;
  margin-left: -22px;
  pointer-events: auto;
  touch-action: none;
  cursor: ew-resize;
}

.region-handle::before {
  content: "";
  position: absolute;
  left: 21px;
  top: 0;
  width: 2px;
  height: 100%;
  background: #c4b5fd;
}

.region-handle::after {
  content: "";
  position: absolute;
  left: 14px;
  top: calc(100% - 36px);
  width: 16px;
  height: 32px;
  border-radius: 8px;
  background: #c4b5fd;
}

.region-actions {
  position: absolute;
  left: 50%;
  bottom: max(32px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  width: calc(100vw - 24px);
}

#region-ok,
#reverse-toggle,
#loop-toggle {
  min-width: 96px;
  min-height: 44px;
  padding: 0 16px;
  border: 1px solid #ffffff30;
  border-radius: 10px;
  background: #ffffff14;
  color: #eee;
  font: inherit;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  pointer-events: auto;
}

#reverse-toggle[aria-pressed="true"],
#loop-toggle[aria-pressed="true"] {
  border-color: #c4b5fd;
  color: #c4b5fd;
}

.fade-ramp {
  position: absolute;
  top: calc(50% - 80px);
  width: 0;
  height: 160px;
  pointer-events: none;
}

.fade-line {
  fill: none;
  stroke: #c4b5fd;
  stroke-width: 1.5;
  vector-effect: non-scaling-stroke;
}

.fade-grip {
  position: absolute;
  top: calc(50% - 80px);
  width: 32px;
  height: 32px;
  margin: -16px 0 0 -16px;
  pointer-events: auto;
  touch-action: none;
  cursor: ew-resize;
}

.fade-grip::after {
  content: "";
  position: absolute;
  inset: 8px;
  border: 2px solid #c4b5fd;
  border-radius: 50%;
  background: #0d0d10;
}

.fade-grip::before {
  content: attr(data-label);
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  color: #bcb0e4;
  font: 11px ui-monospace, monospace;
  white-space: nowrap;
  pointer-events: none;
}