:root {
  --font: "Roboto", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Arial,
    sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;

  --h1: clamp(20px, 2vw, 24px);
  --h2: 16px;
  --label: 12px;
  --body: 14px;

  --bg-0: #04163b;
  --bg-1: #072147;
  --bg-2: #0d3664;
  --line: #1d4f80;

  --text-1: #f2f6ff;
  --text-2: #a8bfd7;
  --text-3: #7f8fb0;

  --brand: #3b82f6;
  --brand-600: #2563eb;
  --success: #22c55e;
  --warn: #f59e0b;
  --danger: #ef4444;
  --info: #38bdf8;

  --r-sm: 6px;
  --r-md: 10px;

  --s-1: 8px;
  --s-2: 16px;
  --s-3: 24px;

  --col-gap: 16px;
  --row-gap: 16px;
  --maxw: 1440px;

  --focus: #3dabff;

  /* extras */
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, 0.35);
  --easing: cubic-bezier(0.4, 0, 0.2, 1);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}
html {
  color-scheme: dark;
}

body {
  margin: 0;
  font: 400 var(--body) / 1.5 var(--font);
  color: var(--text-1);
  background: var(--bg-0);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  font-synthesis-weight: none;
}

.app {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: auto 1fr;
  min-height: 100vh;
}

/* ——— Header ——— */
.header {
  grid-column: 1/-1;
  position: sticky;
  top: 0;
  z-index: 10;
  background: linear-gradient(
    180deg,
    rgba(4, 22, 59, 0.95) 0%,
    rgba(5, 30, 70, 0.92) 100%
  );
  border-bottom: 1px solid var(--line);
  backdrop-filter: saturate(120%) blur(6px);
  -webkit-backdrop-filter: saturate(120%) blur(6px);
}
.container {
  max-width: var(--maxw);
  margin-inline: auto;
  padding-inline: var(--s-2);
}
.header-row {
  display: flex;
  align-items: center;
  gap: 12px;
  height: 56px;
}
h1 {
  font-size: var(--h1);
  margin: 0;
}
.badge {
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--bg-2);
  color: var(--text-2);
  border: 1px solid var(--line);
}
.spacer {
  flex: 1;
}

/* ——— Stage ——— */
.merged {
  position: relative;
  width: 100%;
  height: calc(100vh - 56px);
  overflow: hidden;
}
@supports (height: 100dvh) {
  .merged {
    height: calc(100dvh - 56px);
  }
}

.stage {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

/* SVG overlay */
.stage svg,
.mesh-overlay {
  background: transparent;
}
.stage svg {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

/* GL layer */
#glWrap {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(
      55% 48% at 32% 38%,
      rgba(33, 169, 255, 0.78),
      rgba(33, 169, 255, 0) 70%
    ),
    radial-gradient(
      48% 45% at 62% 62%,
      rgba(159, 236, 120, 0.7),
      rgba(159, 236, 120, 0) 72%
    ),
    radial-gradient(
      120% 95% at 50% 120%,
      rgba(5, 25, 58, 0.9),
      rgba(5, 25, 58, 0)
    ),
    linear-gradient(130deg, #04163b 0%, #073158 45%, #04163b 100%);
}
#gl {
  width: 100%;
  height: 100%;
  display: block;
  background: transparent; /* ensure canvas alpha shows through */
}

/* Mesh overlay container */
.mesh-overlay {
  pointer-events: none;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.mesh-overlay * {
  pointer-events: auto;
}

.mesh-overlay #svgWrap {
  background: transparent;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: grab;
}
.mesh-overlay #svgWrap:active {
  cursor: grabbing;
}

/* ——— GUI ——— */
.mesh-gui {
  position: fixed;
  top: 72px;
  right: 16px;
  z-index: 30;
  color: var(--text-1);
  font-family: var(--font);
  background: rgba(17, 22, 30, 0.95);
  border-radius: var(--r-md);
  border: 1px solid var(--line);
  box-shadow: var(--shadow-lg);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  max-height: calc(100vh - 120px);
  overflow: hidden;
}
.mesh-gui .title {
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.mesh-gui .children {
  max-height: calc(100vh - 160px);
  overflow-y: auto;
}

/* ——— Panels & bits ——— */
.section {
  padding: var(--s-2);
}
.section.is-tight {
  position: absolute;
  right: 8px;
  top: 8px;
  z-index: 2;
}
.well {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  padding: var(--s-2);
}
.label {
  font-size: var(--label);
  text-transform: uppercase;
  color: var(--text-3);
  letter-spacing: 0.08em;
}

.mt-12 {
  margin-top: 12px;
}
.mb-4 {
  margin-bottom: 4px;
}
.w-140 {
  width: 140px;
}
.h-32 {
  height: 32px;
}
.fs-12 {
  font-size: 12px;
}

/* ——— Buttons ——— */
.btn {
  height: 36px;
  padding: 0 14px;
  border-radius: var(--r-md);
  background: var(--brand);
  color: #fff;
  border: 1px solid var(--brand-600);
  font-weight: 600;
  cursor: pointer;
  transition: filter 0.2s var(--easing), transform 0.08s var(--easing);
}
.btn.secondary {
  background: var(--bg-2);
  color: var(--text-1);
  border: 1px solid var(--line);
}
.btn.ghost {
  background: transparent;
  color: var(--text-1);
  border: 1px solid var(--line);
}
@media (hover: hover) {
  .btn:hover {
    filter: brightness(1.05);
  }
}
.btn:active {
  transform: translateY(1px);
}

/* ——— Inputs ——— */
.input,
.select {
  width: 100%;
  height: 36px;
  border-radius: 8px;
  background: var(--bg-1);
  color: var(--text-1);
  border: 1px solid var(--line);
  padding: 0 10px;
  accent-color: var(--brand);
}
.input[type="number"] {
  width: 90px;
}

.row {
  display: flex;
  gap: var(--s-1);
  align-items: center;
}
.range {
  width: 100%;
}

:where(input, button, select):focus {
  outline: none;
}
:where(input, button, select):focus-visible {
  outline: none;
}

/* ——— Toggle ——— */
.toggle {
  position: relative;
  width: 48px;
  height: 24px;
  border-radius: 24px;
  background: #0a2d4e;
  transition: background 0.3s var(--easing);
  cursor: pointer;
}
.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}
.toggle .knob {
  position: absolute;
  inset-block-start: 3px;
  inset-inline-start: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #94a3b8;
  transition: all 0.3s var(--easing);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
}
@media (hover: hover) {
  .toggle:hover .knob {
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  }
}
.toggle input:checked + .knob {
  transform: translateX(24px);
  background: #4ade80;
  box-shadow: 0 2px 8px rgba(74, 222, 128, 0.4);
}
.toggle:active .knob {
  width: 22px;
}

/* ——— File list ——— */
.files {
  overflow: auto;
  padding: var(--s-1) var(--s-1) var(--s-2);
}
.file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}
@media (hover: hover) {
  .file:hover {
    background: rgba(255, 255, 255, 0.03);
  }
}
.file.active {
  background: rgba(59, 130, 246, 0.15);
  outline: 1px solid rgba(59, 130, 246, 0.35);
}
.file .name {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ——— HUD ——— */
.hud {
  position: absolute;
  bottom: 8px;
  left: 8px;
  font: 12px/1.4 var(--mono);
  color: var(--text-3);
  background: var(--bg-2);
  border: 1px solid var(--line);
  padding: 6px 8px;
  border-radius: var(--r-sm);
}
.hud.is-right {
  right: 8px;
  left: auto;
}
.hud.hud-pan {
  bottom: 38px;
  left: 8px;
}

/* ——— Mesh visuals ——— */
.mesh-line {
  stroke: #43536c;
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
}
.mesh-line.u {
  stroke: #1fa9ff;
}
.mesh-line.v {
  stroke: #9bec78;
}

.mesh-point {
  fill: #e6eefb;
  stroke: #0a0e13;
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
}
.mesh-point.sel {
  fill: #3db8ff;
}

.handle {
  fill: #0a0f16;
  stroke: #7191be;
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}
.handle.u {
  stroke: #1fa9ff;
}
.handle.v {
  stroke: #9bec78;
}
.handle-line {
  stroke: #4a678b;
  stroke-dasharray: 2 2;
  stroke-width: 1;
  vector-effect: non-scaling-stroke;
}
.handle-line.u {
  stroke: #1fa9ff;
}
.handle-line.v {
  stroke: #9bec78;
}

.handle-hover {
  fill: none;
  stroke: #ff6161;
  stroke-width: 1.3;
  stroke-opacity: 0;
  opacity: 0;
  vector-effect: non-scaling-stroke;
  transform-origin: center;
  transform-box: fill-box;
  transition: opacity 0.18s var(--easing);
}

.handle-group:hover .handle-hover {
  opacity: 1;
  animation: handle-hover-pulse 1.2s ease-in-out infinite;
}

@keyframes handle-hover-pulse {
  0% {
    transform: scale(0.88);
    stroke-opacity: 0.18;
  }
  50% {
    transform: scale(1.05);
    stroke-opacity: 0.85;
  }
  100% {
    transform: scale(0.88);
    stroke-opacity: 0.18;
  }
}

/* ——— Swatches ——— */
.swatch {
  width: 18px;
  height: 18px;
  border-radius: 4px;
  border: 1px solid var(--line);
  cursor: pointer;
}
.swatch-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ——— A11y / Motion ——— */
@media (prefers-reduced-motion: reduce) {
  .btn,
  .toggle .knob,
  .toggle {
    transition: none !important;
  }
  .btn:active {
    transform: none;
  }
  .handle-group:hover .handle-hover {
    animation: none !important;
    opacity: 1;
    stroke-opacity: 0.6;
  }
}

#animToggleBtn {
  visibility: hidden;
  display: none;
}
