:root {
  --bg: #080a10;
  --panel: rgba(10, 14, 22, 0.9);
  --border: rgba(120, 140, 180, 0.2);
  --text: #e8ecf4;
  --muted: #8b95a8;
  --accent: #5b9dff;
  --accent2: #7c5cff;
  --danger: #ff5c6a;
  --lab-red: #e10600;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --hud-fade: rgba(8, 10, 16, 0.94);
  --btn-bg: rgba(255, 255, 255, 0.04);
  --log: #a8b4cc;
  --toast-bg: rgba(12, 16, 26, 0.92);
  --dock-bg: rgba(10, 12, 18, 0.95);
  --loader-bg: radial-gradient(circle at 50% 40%, #121826, #06080c);
  --dock-h: 4.25rem;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: "Space Grotesk", "Segoe UI", system-ui, sans-serif;
}

html.theme-light {
  --bg: #e8eef6;
  --panel: rgba(255, 255, 255, 0.82);
  --border: rgba(70, 90, 120, 0.18);
  --text: #1a2433;
  --muted: #5a6a7e;
  --accent: #3b7ddd;
  --accent2: #6b5ce0;
  --danger: #d64555;
  --shadow: 0 12px 36px rgba(40, 60, 90, 0.12);
  --hud-fade: rgba(232, 238, 246, 0.92);
  --btn-bg: rgba(30, 50, 80, 0.05);
  --log: #4a5a6e;
  --toast-bg: rgba(255, 255, 255, 0.94);
  --dock-bg: rgba(255, 255, 255, 0.94);
  --loader-bg: radial-gradient(circle at 50% 40%, #f2f6fb, #dce5f0);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#canvas {
  display: block;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  touch-action: none;
}

.hud-top {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: calc(0.55rem + var(--safe-t)) 0.85rem 0.55rem;
  background: linear-gradient(180deg, var(--hud-fade), transparent);
  pointer-events: none;
  z-index: 10;
}

.hud-left {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  min-width: 0;
  pointer-events: auto;
}

.hud-right {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  pointer-events: auto;
  flex: 0 0 auto;
}

.theme-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.28rem 0.45rem 0.28rem 0.3rem;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--btn-bg);
  color: var(--muted);
  font: inherit;
  font-size: 0.68rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s;
}

.theme-toggle:hover {
  color: var(--text);
  border-color: rgba(91, 157, 255, 0.4);
  background: rgba(91, 157, 255, 0.08);
}

.theme-toggle__track {
  position: relative;
  width: 2.1rem;
  height: 1.15rem;
  border-radius: 999px;
  background: rgba(120, 140, 180, 0.28);
  flex: 0 0 auto;
  transition: background 0.2s;
}

html.theme-light .theme-toggle__track {
  background: rgba(59, 125, 221, 0.35);
}

.theme-toggle__thumb {
  position: absolute;
  top: 0.14rem;
  left: 0.14rem;
  width: 0.87rem;
  height: 0.87rem;
  border-radius: 50%;
  background: #f2f5fa;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.25);
  transition: transform 0.2s ease;
}

.theme-toggle.is-light .theme-toggle__thumb {
  transform: translateX(0.92rem);
  background: #fff;
}

.theme-toggle__label {
  min-width: 2.4rem;
  text-align: left;
}

.back-lab {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  flex: 0 0 auto;
  padding: 0.42rem 0.7rem;
  border: 1px solid rgba(225, 6, 0, 0.45);
  border-radius: 999px;
  background: rgba(225, 6, 0, 0.12);
  color: #ffd6d2;
  text-decoration: none;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

html.theme-light .back-lab {
  color: #9a1a12;
  background: rgba(225, 6, 0, 0.1);
  border-color: rgba(225, 6, 0, 0.35);
}

.back-lab:hover {
  background: rgba(225, 6, 0, 0.22);
  border-color: rgba(225, 6, 0, 0.75);
}

.back-lab:active { transform: scale(0.97); }

.back-lab__arrow { font-size: 0.85rem; line-height: 1; }

.brand {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
  pointer-events: none;
}

.brand-dot {
  flex: 0 0 auto;
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent2));
  box-shadow: 0 0 16px rgba(91, 157, 255, 0.6);
}

.brand strong {
  display: block;
  font-size: 0.88rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand small { color: var(--muted); font-size: 0.66rem; }

.stats {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.4rem 0.6rem;
  font-size: 0.68rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  pointer-events: none;
}

.panel {
  position: fixed;
  top: 4rem;
  width: min(270px, calc(100vw - 1.4rem));
  max-height: calc(100vh - 5rem);
  max-height: calc(100dvh - 5rem);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  backdrop-filter: blur(14px);
  box-shadow: var(--shadow);
  z-index: 12;
  contain: layout style;
}

.panel-left { left: 0.7rem; }
.panel-physics {
  left: 0.7rem;
  top: auto;
  bottom: 0.7rem;
  max-height: min(300px, calc(100dvh - 5rem));
}
.panel-right {
  right: 0.7rem;
  width: min(290px, calc(100vw - 1.4rem));
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  margin: 0;
  padding: 0.7rem 0.85rem;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-size: 0.7rem;
  font-weight: 650;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  cursor: pointer;
  text-align: left;
}

.panel-head:hover {
  background: rgba(255, 255, 255, 0.04);
}

html.theme-light .panel-head:hover {
  background: rgba(30, 50, 80, 0.05);
}

.chevron {
  width: 0.5rem;
  height: 0.5rem;
  border-right: 2px solid var(--muted);
  border-bottom: 2px solid var(--muted);
  transform: rotate(45deg);
  transition: transform 0.2s ease;
  flex: 0 0 auto;
}

.panel.collapsed .chevron { transform: rotate(-45deg); }
.panel.collapsed .panel-body { display: none; }
.panel.collapsed { max-height: none; }

.panel-body {
  padding: 0 0.85rem 0.85rem;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
}

button {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--btn-bg);
  color: var(--text);
  padding: 0.55rem 0.45rem;
  font: inherit;
  font-size: 0.76rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.1s;
}

button:hover {
  background: rgba(91, 157, 255, 0.12);
  border-color: rgba(91, 157, 255, 0.45);
}

button:active { transform: scale(0.97); }

button.ghost { background: transparent; }
button.danger {
  border-color: rgba(255, 92, 106, 0.45);
  color: #ffb3ba;
}

html.theme-light button.danger {
  color: #b03040;
  border-color: rgba(214, 69, 85, 0.4);
}

.btn-row {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.3rem;
}

.btn-row button { flex: 1; }

label {
  display: block;
  margin: 0.5rem 0;
  font-size: 0.72rem;
  color: var(--muted);
}

input[type="range"] {
  width: 100%;
  margin-top: 0.3rem;
  accent-color: var(--accent);
  height: 1.35rem;
}

.tool-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.4rem;
}

.tool.active {
  background: linear-gradient(135deg, rgba(91, 157, 255, 0.22), rgba(124, 92, 255, 0.18));
  border-color: rgba(91, 157, 255, 0.55);
}

.hint {
  margin: 0.7rem 0 0;
  font-size: 0.7rem;
  line-height: 1.45;
  color: var(--muted);
}

.log {
  margin-top: 0.7rem;
  max-height: 120px;
  overflow: auto;
  font-size: 0.66rem;
  line-height: 1.35;
  color: var(--log);
  font-family: ui-monospace, Consolas, monospace;
}

.log div {
  padding: 0.15rem 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

html.theme-light .log div {
  border-bottom-color: rgba(30, 50, 80, 0.06);
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%) translateY(0);
  z-index: 50;
  max-width: min(90vw, 360px);
  padding: 0.65rem 1rem;
  border-radius: 12px;
  border: 1px solid rgba(91, 157, 255, 0.35);
  background: var(--toast-bg);
  color: var(--text);
  font-size: 0.78rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
  transition: opacity 0.25s, transform 0.25s;
}

.toast[hidden] {
  opacity: 0;
  transform: translateX(-50%) translateY(12px);
  pointer-events: none;
}

.mobile-dock,
.sheet-backdrop,
.mobile-only { display: none; }

.loader {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--loader-bg);
  z-index: 100;
  transition: opacity 0.45s, visibility 0.45s;
}

.loader.hidden {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.loader-inner { text-align: center; }

.loader-ring {
  width: 48px;
  height: 48px;
  margin: 0 auto 1rem;
  border: 3px solid rgba(91, 157, 255, 0.15);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.85s linear infinite;
}

.loader-back {
  display: inline-block;
  margin-top: 1.1rem;
  color: var(--muted);
  font-size: 0.78rem;
  text-decoration: none;
}
.loader-back:hover { color: var(--text); }

@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 820px) {
  .desktop-only { display: none !important; }
  .mobile-only { display: block; }

  .back-lab__text { display: none; }
  .back-lab { padding: 0.45rem 0.6rem; }
  .brand-sub { display: none; }
  .theme-toggle__label { display: none; }
  .stats span:nth-child(3) { display: none; }

  .panel {
    top: auto;
    left: 0.65rem;
    right: 0.65rem;
    bottom: calc(var(--dock-h) + var(--safe-b) + 0.55rem);
    width: auto;
    max-height: min(56dvh, 400px);
    border-radius: 18px 18px 14px 14px;
    transform: translateY(110%);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.28s ease, opacity 0.2s ease;
    z-index: 30;
  }

  .panel.sheet-open {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  .panel.collapsed .panel-body { display: block; }
  .panel .chevron { display: none; }
  .panel-head { pointer-events: none; padding-bottom: 0.3rem; }

  .btn-grid button,
  .tool-row button {
    min-height: 2.7rem;
    font-size: 0.84rem;
  }

  .sheet-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 25;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
  }
  html.theme-light .sheet-backdrop { background: rgba(30, 50, 80, 0.28); }
  .sheet-backdrop.show {
    opacity: 1;
    pointer-events: auto;
  }

  .toast { bottom: calc(var(--dock-h) + var(--safe-b) + 0.85rem); }

  .mobile-dock {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 40;
    height: calc(var(--dock-h) + var(--safe-b));
    padding: 0.3rem 0.3rem calc(0.3rem + var(--safe-b));
    gap: 0.2rem;
    background: var(--dock-bg);
    border-top: 1px solid var(--border);
    backdrop-filter: blur(16px);
  }

  .mobile-dock button,
  .mobile-dock a.dock-home {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.1rem;
    min-height: 3.1rem;
    padding: 0.2rem 0.1rem;
    border-radius: 12px;
    font-size: 1rem;
    line-height: 1;
    text-decoration: none;
    color: var(--text);
    border: 1px solid var(--border);
    background: var(--btn-bg);
  }

  .mobile-dock a.dock-home {
    border-color: rgba(225, 6, 0, 0.4);
    background: rgba(225, 6, 0, 0.12);
    color: #ffd6d2;
  }

  html.theme-light .mobile-dock a.dock-home {
    color: #9a1a12;
  }

  .mobile-dock button span,
  .mobile-dock a.dock-home span {
    font-size: 0.58rem;
    color: var(--muted);
    letter-spacing: 0.02em;
  }

  .mobile-dock a.dock-home span { color: #ffb3ba; }
  html.theme-light .mobile-dock a.dock-home span { color: #9a1a12; }

  .mobile-dock button.active {
    background: rgba(91, 157, 255, 0.16);
    border-color: rgba(91, 157, 255, 0.45);
  }

  .mobile-dock button.active span { color: var(--text); }
}

@media (min-width: 821px) {
  .panel-physics {
    left: 0.7rem;
    bottom: 0.7rem;
    top: auto;
    width: min(270px, calc(100vw - 1.4rem));
  }
}
