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

:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

body {
  /* default background */
  background: #000;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  overflow: hidden;
  cursor: crosshair;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

canvas {
  display: block;
  cursor: crosshair;
  touch-action: none;
}

/* Control Panel Styles */
.controls-panel {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 1000;
  width: 320px;
  background: rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(20px) saturate(180%);
  -webkit-backdrop-filter: blur(20px) saturate(180%);
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4), 
              0 0 0 1px rgba(255, 255, 255, 0.1) inset;
  color: #fff;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  font-size: 14px;
  cursor: move;
  user-select: none;
}

.controls-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  background: rgba(255, 255, 255, 0.12);
  border-radius: 16px 16px 0 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  cursor: move;
  backdrop-filter: blur(10px);
}

.controls-title {
  font-weight: 600;
  font-size: 15px;
}

.toggle-btn {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  width: 28px;
  height: 28px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
  transition: all 0.2s;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.toggle-btn:hover {
  background: rgba(255, 255, 255, 0.25);
  transform: scale(1.05);
}

.controls-content {
  max-height: 70vh;
  max-height: 70svh;
  overflow-y: auto;
  padding: 16px;
  transition: max-height 0.3s ease, padding 0.3s ease;
  -webkit-overflow-scrolling: touch;
}

.controls-content.collapsed {
  max-height: 0;
  padding: 0 16px;
  overflow: hidden;
}

.control-section {
  margin-bottom: 20px;
  padding-bottom: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.15);
  backdrop-filter: blur(5px);
}

.control-section:last-child {
  border-bottom: none;
  margin-bottom: 0;
  padding-bottom: 0;
}

.control-section h3 {
  margin: 0 0 12px 0;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: rgba(255, 255, 255, 0.85);
  font-weight: 600;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
}

.control-section label {
  display: block;
  margin-bottom: 8px;
  cursor: pointer;
}

.control-section input[type="range"] {
  width: 100%;
  margin-top: 4px;
  cursor: pointer;
}

.control-section input[type="checkbox"] {
  margin-right: 8px;
  cursor: pointer;
}

.control-section input[type="file"] {
  display: block;
  width: 100%;
  padding: 6px;
  margin: 8px 0;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  backdrop-filter: blur(5px);
}

.control-section input[type="file"]::file-selector-button {
  background: rgba(255, 255, 255, 0.15);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #fff;
  padding: 4px 12px;
  border-radius: 6px;
  margin-right: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.control-section input[type="file"]::file-selector-button:hover {
  background: rgba(255, 255, 255, 0.25);
}

.control-section button {
  background: rgba(255, 80, 80, 0.7);
  border: 1px solid rgba(255, 100, 100, 0.3);
  color: #fff;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(5px);
}

.control-section button:hover {
  background: rgba(255, 80, 80, 0.9);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(255, 80, 80, 0.3);
}

.reset-btn {
  width: 100%;
  background: rgba(255, 165, 0, 0.7) !important;
  border: 1px solid rgba(255, 165, 0, 0.4) !important;
  padding: 10px !important;
  font-size: 14px !important;
  font-weight: 600;
}

.reset-btn:hover {
  background: rgba(255, 165, 0, 0.9) !important;
  box-shadow: 0 4px 12px rgba(255, 165, 0, 0.4) !important;
}

.control-section select {
  width: 100%;
  padding: 6px;
  margin-top: 4px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  border-radius: 8px;
  color: #fff;
  font-size: 13px;
  cursor: pointer;
  backdrop-filter: blur(5px);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}

.control-section select option {
  background: rgba(26, 26, 46, 0.98);
  color: #fff;
  backdrop-filter: blur(20px);
}

#color-palette {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.color-picker {
  width: 50px;
  height: 50px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.color-picker:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.3);
  border-color: rgba(255, 255, 255, 0.5);
}

#color-palette-section {
  margin-top: 8px;
}

/* Custom color pickers styling */
.custom-color-picker {
  width: 50px;
  height: 50px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
  background: transparent;
  overflow: hidden;
}

.custom-color-picker::-webkit-color-swatch-wrapper {
  padding: 0;
}

.custom-color-picker::-webkit-color-swatch {
  border: none;
  border-radius: 8px;
}

.custom-color-picker::-moz-color-swatch {
  border: none;
  border-radius: 8px;
}

.custom-color-picker:hover {
  transform: scale(1.1);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
  border-color: rgba(255, 255, 255, 0.5);
}

#custom-colors-section {
  background: rgba(255, 255, 255, 0.05);
  padding: 12px;
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(5px);
}

#custom-color-palette {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: 10px;
  justify-content: flex-start;
}

/* Scrollbar styling */
.controls-content::-webkit-scrollbar {
  width: 6px;
}

.controls-content::-webkit-scrollbar-track {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 3px;
}

.controls-content::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 3px;
}

.controls-content::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Value display styling */
label span {
  font-weight: 600;
  color: #ffa500;
  min-width: 45px;
  display: inline-block;
}

.trig-panel {
  border-color: rgba(120, 210, 255, 0.24);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4),
              0 0 0 1px rgba(120, 210, 255, 0.12) inset;
}

.trig-panel .controls-header {
  background: rgba(120, 210, 255, 0.12);
}

.trig-note,
.trig-footnote {
  margin: 0 0 12px;
  font-size: 12px;
  line-height: 1.5;
  color: rgba(255, 255, 255, 0.78);
}

.trig-footnote {
  margin: 12px 0 0;
}

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

.trig-metric {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.trig-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: rgba(255, 255, 255, 0.62);
}

.trig-value {
  color: #9de5ff;
  font-size: 15px;
  font-weight: 700;
}

.trig-formula-list {
  display: grid;
  gap: 8px;
}

.trig-formula {
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(8, 18, 32, 0.45);
  border: 1px solid rgba(120, 210, 255, 0.16);
  color: rgba(255, 255, 255, 0.9);
  font-size: 12px;
  line-height: 1.45;
}

#bg-controls button:hover {
  background: rgba(255, 255, 255, 0.22);
}

@media (max-width: 900px), (pointer: coarse) {
  body,
  canvas {
    cursor: default;
  }

  .controls-panel {
    width: min(92vw, 340px);
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    backdrop-filter: blur(8px) saturate(120%);
    -webkit-backdrop-filter: blur(8px) saturate(120%);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
    font-size: 15px;
  }

  .controls-header {
    padding: 12px 14px;
  }

  .toggle-btn {
    width: 44px;
    height: 44px;
    font-size: 22px;
    border-radius: 10px;
  }

  .control-section button,
  .control-section select,
  .control-section input[type="file"] {
    font-size: 16px;
    padding: 10px 12px;
  }

  .control-section input[type="checkbox"] {
    transform: scale(1.2);
    transform-origin: left center;
  }

  .control-section input[type="range"] {
    height: 32px;
  }

  .control-section input[type="range"]::-webkit-slider-runnable-track {
    height: 8px;
    border-radius: 999px;
  }

  .control-section input[type="range"]::-webkit-slider-thumb {
    width: 24px;
    height: 24px;
    margin-top: -8px;
  }

  .control-section input[type="range"]::-moz-range-track {
    height: 8px;
    border-radius: 999px;
  }

  .control-section input[type="range"]::-moz-range-thumb {
    width: 24px;
    height: 24px;
    border-radius: 999px;
    border: none;
  }

  .controls-header {
    backdrop-filter: none;
  }

  .control-section,
  .control-section input[type="file"],
  .control-section select,
  #custom-colors-section {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }

  #presets-panel {
    width: min(92vw, 340px) !important;
    left: calc(12px + var(--safe-left)) !important;
    right: auto !important;
    top: auto !important;
    bottom: calc(12px + var(--safe-bottom));
  }

  #trig-panel {
    width: min(92vw, 340px) !important;
    left: auto !important;
    right: calc(12px + var(--safe-right)) !important;
    top: auto !important;
    bottom: calc(12px + var(--safe-bottom));
  }
}
