:root {
  --bg: #01010a;
  --text: #e8ecf4;
  --muted: #6b758c;
  --cyan: #5eb8d4;
  --violet: #9b7fd4;
  --gold: #d4a85e;
  --mint: #5ed4a8;
  --mag: #c45a7a;
  --rose: #d47898;
  --mono: ui-monospace, "SF Mono", "Menlo", "Monaco", "Consolas", "Liberation Mono", "Courier New", monospace;
  --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);
}

*, *::before, *::after { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--mono);
  color: var(--text);
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#boot-loading {
  position: fixed;
  z-index: 98;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  color: var(--cyan);
  font-size: 0.72rem;
  letter-spacing: 0.2em;
  text-transform: lowercase;
  opacity: 0.7;
  pointer-events: none;
  animation: brand-pulse 2s ease-in-out infinite;
}

#boot-loading.is-done {
  display: none;
}

#boot-error {
  position: fixed;
  z-index: 99;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: min(28rem, calc(100vw - 2rem));
  padding: 1rem 1.2rem;
  border: 1px solid rgba(196, 90, 122, 0.55);
  border-radius: 0.5rem;
  background: rgba(20, 8, 16, 0.92);
  color: #e88aa8;
  font-size: 0.75rem;
  line-height: 1.5;
  text-align: center;
  pointer-events: none;
}

#canvas-3d {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  z-index: 0;
  touch-action: none;
}

.ui {
  position: fixed;
  z-index: 2;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding:
    calc(clamp(0.75rem, 3vw, 1.5rem) + var(--safe-top))
    calc(clamp(0.75rem, 3vw, 1.5rem) + var(--safe-right))
    calc(clamp(0.75rem, 3vw, 1.5rem) + var(--safe-bottom))
    calc(clamp(0.75rem, 3vw, 1.5rem) + var(--safe-left));
  pointer-events: none;
}

.ui-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  width: 100%;
  pointer-events: none;
}

.render-swap {
  pointer-events: auto;
  flex: 0 0 auto;
  margin-top: 0.15rem;
  border: 1px solid rgba(94, 212, 168, 0.42);
  background: linear-gradient(135deg, rgba(94, 184, 212, 0.18), rgba(94, 212, 168, 0.14));
  color: var(--mint);
  font-family: var(--mono);
  font-size: clamp(0.58rem, 2.6vw, 0.68rem);
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: lowercase;
  padding: 0.42rem 0.78rem;
  min-height: 2rem;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.2s, box-shadow 0.2s, transform 0.2s, border-color 0.2s;
}

.render-swap:hover,
.render-swap:focus-visible {
  color: #9ef0d0;
  border-color: rgba(94, 212, 168, 0.62);
  box-shadow: 0 0 18px rgba(94, 212, 168, 0.22);
  transform: translateY(-1px);
  outline: none;
}

.render-swap.is-fluids {
  border-color: rgba(155, 127, 212, 0.45);
  background: linear-gradient(135deg, rgba(155, 127, 212, 0.2), rgba(94, 184, 212, 0.12));
  color: var(--violet);
}

.render-swap.is-fluids:hover,
.render-swap.is-fluids:focus-visible {
  color: #c4b0e8;
  border-color: rgba(155, 127, 212, 0.62);
  box-shadow: 0 0 18px rgba(155, 127, 212, 0.24);
}

.brand {
  margin: 0;
  font-size: clamp(1.2rem, 5vw, 2rem);
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: lowercase;
  background: linear-gradient(115deg, var(--cyan) 0%, var(--violet) 38%, var(--mag) 68%, var(--gold) 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 24px rgba(94, 184, 212, 0.18));
  animation: brand-pulse 4s ease-in-out infinite;
}

@keyframes brand-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.88; }
}

.ui-bottom {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  pointer-events: none;
  max-width: 100%;
}

.formula-dock {
  pointer-events: auto;
  width: 100%;
  overflow: hidden;
  -webkit-mask-image: linear-gradient(90deg, transparent, #000 5%, #000 95%, transparent);
  mask-image: linear-gradient(90deg, transparent, #000 5%, #000 95%, transparent);
}

.formula-dock__track {
  display: flex;
  gap: 0.45rem;
  overflow-x: auto;
  padding: 0.35rem 0.5rem 0.5rem;
  scrollbar-width: none;
  -ms-overflow-style: none;
  -webkit-overflow-scrolling: touch;
  scroll-snap-type: x proximity;
}

.formula-dock__track::-webkit-scrollbar {
  display: none;
}

.formula-chip {
  flex: 0 0 auto;
  scroll-snap-align: center;
  border: 1px solid rgba(94, 184, 212, 0.22);
  background: linear-gradient(135deg, rgba(94, 184, 212, 0.1), rgba(196, 90, 122, 0.08));
  color: var(--muted);
  font-family: var(--mono);
  font-size: clamp(0.52rem, 2.8vw, 0.62rem);
  letter-spacing: 0.1em;
  text-transform: lowercase;
  padding: 0.42rem 0.68rem;
  min-height: 2rem;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.2s, border-color 0.2s, background 0.2s, box-shadow 0.2s;
  white-space: nowrap;
}

.formula-chip:hover,
.formula-chip:focus-visible {
  color: var(--text);
  outline: none;
}

.formula-chip.is-active {
  font-weight: 500;
}

.formula-editor {
  pointer-events: auto;
  max-width: min(36rem, 100%);
  padding: 0.75rem 0.9rem;
  border: 1px solid rgba(94, 184, 212, 0.2);
  border-radius: 0.5rem;
  background: rgba(6, 10, 20, 0.88);
}

@supports (backdrop-filter: blur(8px)) {
  .formula-editor {
    backdrop-filter: blur(8px);
  }
}

.formula-editor__expr {
  margin: 0 0 0.65rem;
  font-size: clamp(0.62rem, 2.8vw, 0.68rem);
  line-height: 1.55;
  background: linear-gradient(90deg, var(--cyan), var(--mint), var(--violet), var(--gold));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  letter-spacing: 0.04em;
  word-break: break-word;
}

.formula-editor__params {
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
  max-height: min(38vh, 16rem);
  overflow-y: auto;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.param-section {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.param-section__title {
  margin: 0;
  font-size: 0.56rem;
  letter-spacing: 0.16em;
  text-transform: lowercase;
  color: rgba(94, 184, 212, 0.55);
}

.param-row {
  display: grid;
  grid-template-columns: minmax(2.2rem, 2.5rem) 1fr minmax(2rem, 2.5rem);
  align-items: center;
  gap: 0.5rem;
}

.param-row label {
  font-size: 0.62rem;
  color: var(--muted);
  text-transform: lowercase;
}

.param-row input[type="range"] {
  width: 100%;
  min-height: 1.75rem;
  accent-color: var(--cyan);
}

.param-row output {
  font-size: 0.62rem;
  color: var(--text);
  text-align: right;
}

.ui-actions {
  pointer-events: auto;
  display: none;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
}

.ui-actions.is-visible {
  display: flex;
}

.much-wow,
.have-fun {
  pointer-events: auto;
  border: 1px solid rgba(196, 90, 122, 0.45);
  background: linear-gradient(135deg, rgba(196, 90, 122, 0.22), rgba(155, 127, 212, 0.14));
  color: var(--mag);
  font-family: var(--mono);
  font-size: clamp(0.66rem, 3vw, 0.72rem);
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: lowercase;
  padding: 0.5rem 1rem;
  min-height: 2.25rem;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.2s, box-shadow 0.2s, transform 0.2s;
}

.have-fun {
  border: 1px solid rgba(94, 184, 212, 0.45);
  background: linear-gradient(135deg, rgba(94, 184, 212, 0.2), rgba(94, 212, 168, 0.14));
  color: var(--cyan);
}

.have-fun:hover,
.have-fun:focus-visible {
  color: #8ee0f0;
  box-shadow: 0 0 20px rgba(94, 184, 212, 0.25);
  transform: translateY(-1px);
  outline: none;
}

.much-wow:hover,
.much-wow:focus-visible {
  color: #e88aa8;
  box-shadow: 0 0 20px rgba(196, 90, 122, 0.25);
  transform: translateY(-1px);
  outline: none;
}

.links {
  display: flex;
  flex-wrap: wrap;
  gap: 0.85rem 1.25rem;
  align-items: center;
  pointer-events: auto;
}

.links a {
  font-size: clamp(0.68rem, 3.2vw, 0.78rem);
  letter-spacing: 0.12em;
  text-transform: lowercase;
  text-decoration: none;
  transition: color 0.2s, text-shadow 0.2s, transform 0.2s;
  min-height: 2rem;
  display: inline-flex;
  align-items: center;
}

.link-break {
  background: linear-gradient(90deg, var(--mag), var(--rose), var(--gold));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  font-weight: 600;
}

.link-break:hover,
.link-break:focus-visible {
  filter: drop-shadow(0 0 14px rgba(196, 90, 122, 0.35));
  transform: translateY(-2px);
  outline: none;
}

.link-x {
  background: linear-gradient(90deg, var(--muted), var(--cyan), var(--mint));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.link-x:hover,
.link-x:focus-visible {
  filter: drop-shadow(0 0 12px rgba(94, 184, 212, 0.28));
  transform: translateY(-2px);
  outline: none;
}

@media (max-width: 768px) {
  .ui-bottom {
    gap: 0.5rem;
  }

  .formula-chip {
    padding: 0.38rem 0.58rem;
  }
}

@media (max-width: 500px) {
  .formula-editor {
    padding: 0.6rem 0.7rem;
  }

  .formula-editor__params {
    max-height: min(32vh, 12rem);
  }

  .links {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.55rem;
  }
}

@media (max-height: 500px) and (orientation: landscape) {
  .ui {
    padding-top: calc(0.5rem + var(--safe-top));
    padding-bottom: calc(0.5rem + var(--safe-bottom));
  }

  .brand {
    font-size: clamp(1rem, 4vh, 1.35rem);
  }

  .formula-editor__params {
    max-height: 28vh;
  }

  .ui-bottom {
    gap: 0.35rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .brand,
  #boot-loading {
    animation: none;
  }

  .formula-chip,
  .much-wow,
  .have-fun,
  .render-swap,
  .links a {
    transition: none;
  }
}