:root {
  --bg: #0c0f0c;
  --surface: #141814;
  --surface-panel: #141814;
  --surface-active: rgba(28, 48, 36, 0.96);
  --border: #2a332c;
  --border-active: #5ecf8a;
  --text: #d8eadf;
  --text-muted: #7a9484;
  --brand: #6ecf96;
  --danger-border: #a85a52;
  --danger-bg: rgba(56, 24, 22, 0.96);
  --danger-text: #ffd4cc;
  --beacon-border: #3d9a62;
  --beacon-bg: rgba(22, 52, 34, 0.98);
  --beacon-text: #c8f5d4;
  --beacon-glow: rgba(62, 168, 98, 0.28);
  --beacon-running-border: #c8a038;
  --beacon-running-bg: rgba(56, 44, 14, 0.98);
  --beacon-running-text: #fff0c8;
  --beacon-running-glow: rgba(220, 168, 48, 0.45);
  --toolbar-gap: 10px;
  --radius: 4px;
  --dock-padding-x: 12px;
  --dock-padding-bottom: max(10px, env(safe-area-inset-bottom));
  --header-padding-top: max(8px, env(safe-area-inset-top));
  --toolbar-2col-width: 292px;
  --control-h: 34px;
  --stepper-side: 34px;
  --toolbar-grid-gap: 6px;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  -webkit-user-select: none;
  user-select: none;
}

.mobile-unsupported {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(24px, env(safe-area-inset-top)) max(24px, env(safe-area-inset-right))
    max(24px, env(safe-area-inset-bottom)) max(24px, env(safe-area-inset-left));
  box-sizing: border-box;
  background: var(--bg);
}

.mobile-unsupported-card {
  max-width: 22rem;
  text-align: center;
}

.mobile-unsupported-title {
  margin: 0 0 12px;
  color: var(--brand);
  font: 700 18px/1.3 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  letter-spacing: 0.04em;
}

.mobile-unsupported-message {
  margin: 0;
  color: var(--text-muted);
  font: 500 15px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

body.mobile-unsupported-active #app-canvas,
body.mobile-unsupported-active #top-toolbar,
body.mobile-unsupported-active #bottom-toolbar,
body.mobile-unsupported-active #selection-controls,
body.mobile-unsupported-active #propagation-stats-hud,
body.mobile-unsupported-active #controls-help,
body.mobile-unsupported-active #node-ids-layer,
body.mobile-unsupported-active #node-stats-layer {
  display: none !important;
}

@media (min-width: 641px) {
  #mobile-unsupported {
    display: none !important;
  }

  body.mobile-unsupported-active #app-canvas,
  body.mobile-unsupported-active #top-toolbar,
  body.mobile-unsupported-active #bottom-toolbar,
  body.mobile-unsupported-active #selection-controls,
  body.mobile-unsupported-active #propagation-stats-hud,
  body.mobile-unsupported-active #controls-help,
  body.mobile-unsupported-active #node-ids-layer,
  body.mobile-unsupported-active #node-stats-layer {
    display: revert !important;
  }
}

.page-intro {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
  pointer-events: none;
}

#app-canvas {
  display: block;
  width: 100vw;
  height: 100vh;
  outline: none;
  touch-action: none;
}

.brand-row {
  display: flex;
  justify-content: center;
  align-self: start;
  pointer-events: none;
}

.brand-title {
  color: var(--brand);
  font: 700 16px/1.2 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  letter-spacing: 0.06em;
  white-space: nowrap;
}

#top-toolbar {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  padding: var(--header-padding-top) var(--dock-padding-x) 8px;
  padding-right: max(var(--dock-padding-x), env(safe-area-inset-right));
  padding-left: max(var(--dock-padding-x), env(safe-area-inset-left));
  pointer-events: none;
  box-sizing: border-box;
}

#top-toolbar > .brand-row {
  position: absolute;
  left: 50%;
  top: var(--header-padding-top);
  transform: translateX(-50%);
  z-index: 0;
  padding-bottom: 0;
}

.hud-panel {
  pointer-events: auto;
  box-sizing: border-box;
  border: 1px solid var(--border);
  background: rgba(20, 24, 20, 0.94);
  border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(8px);
}

.top-toolbar-main {
  position: relative;
  z-index: 1;
  width: max-content;
  max-width: 100%;
  padding: 10px 12px;
}

.hud-panel--bottom {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  padding: 10px 12px;
}

.hud-panel--help {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px;
  width: 100%;
}

.toolbar-groups {
  display: flex;
  flex: 1 1 auto;
  flex-wrap: wrap;
  gap: var(--toolbar-gap);
  min-width: 0;
}

.toolbar-groups--stacked {
  flex: 0 0 auto;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  gap: 12px;
  width: var(--toolbar-2col-width);
}

.toolbar-group--radio .toolbar-buttons,
.toolbar-group--view .toolbar-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--toolbar-grid-gap);
  align-items: end;
}

.toolbar-groups--stacked .toolbar-buttons > .stepper,
.toolbar-groups--stacked .toolbar-buttons > .toolbar-chip,
.toolbar-groups--stacked .toolbar-buttons > .touch-control {
  height: var(--control-h);
  min-height: var(--control-h);
  max-height: var(--control-h);
  box-sizing: border-box;
}

.toolbar-groups--stacked .toolbar-buttons > .touch-control {
  padding: 0 12px;
  line-height: 1;
  font: 600 13px/1 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.toolbar-group--view .stepper,
.toolbar-group--radio .stepper {
  width: 100%;
}

.toolbar-stepper-field--grid {
  grid-column: 1 / -1;
}

.toolbar-buttons--grid-toggles {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: var(--toolbar-grid-gap);
  width: 100%;
}

.toolbar-buttons--grid-toggles > .toolbar-chip {
  width: 100%;
}

.toolbar-stepper-field--chip .toolbar-chip {
  width: 100%;
}

.toolbar-group {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.toolbar-label {
  color: var(--text-muted);
  font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding-left: 2px;
}

.toolbar-stepper-field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.stepper-field-label {
  color: var(--text-muted);
  font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  padding-left: 2px;
}

.toolbar-group--radio .toolbar-buttons > .toolbar-stepper-field,
.toolbar-group--view .toolbar-buttons > .toolbar-stepper-field {
  width: 100%;
}

.toolbar-stepper-field .stepper {
  width: 100%;
}

.toolbar-buttons {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

#beacon-button,
#clear-reset-button,
.touch-control,
.propagation-stat-nav {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 6px 10px;
  border-radius: var(--radius);
  font: 600 13px/1.2 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  cursor: pointer;
  touch-action: manipulation;
  box-sizing: border-box;
}

#top-toolbar button:focus-visible,
#bottom-toolbar button:focus-visible,
#selection-controls button:focus-visible,
#propagation-stats-hud button:focus-visible,
#controls-help button:focus-visible {
  outline: none;
}

.toolbar-chip,
.touch-control.toolbar-chip {
  height: var(--control-h);
  min-height: var(--control-h);
  max-height: var(--control-h);
  padding: 0 12px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  box-sizing: border-box;
}

.toolbar-stepper-field--select {
  min-width: 7.5rem;
}

.toolbar-select {
  height: var(--control-h);
  min-height: var(--control-h);
  max-height: var(--control-h);
  padding: 0 28px 0 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font: 600 13px/1.2 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  cursor: pointer;
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--text-muted) 50%),
    linear-gradient(135deg, var(--text-muted) 50%, transparent 50%);
  background-position:
    calc(100% - 16px) calc(50% + 2px),
    calc(100% - 11px) calc(50% + 2px);
  background-size:
    5px 5px,
    5px 5px;
  background-repeat: no-repeat;
}

.toolbar-select:focus-visible {
  outline: 2px solid var(--border-active);
  outline-offset: 1px;
}

.toolbar-groups--stacked .toolbar-chip {
  width: 100%;
}

.stepper-side {
  cursor: pointer;
  touch-action: manipulation;
}

.stepper {
  display: flex;
  align-items: stretch;
  height: var(--control-h);
  min-height: var(--control-h);
  max-height: var(--control-h);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface);
  box-sizing: border-box;
}

.stepper[hidden],
#touch-range-controls[hidden],
#touch-mode-toggle-button[hidden] {
  display: none !important;
}

.stepper > * {
  border: none;
  border-radius: 0;
  margin: 0;
  box-shadow: none;
  box-sizing: border-box;
}

.stepper > *:not(:last-child) {
  border-right: 1px solid var(--border);
}

.stepper > .stepper-side {
  flex: 0 0 var(--stepper-side);
  width: var(--stepper-side);
  min-width: var(--stepper-side);
  height: 100%;
  padding: 0;
  line-height: 1;
  font-size: 15px;
  font-weight: 600;
  background: var(--surface);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.stepper-readout,
.stepper-value {
  flex: 1 1 0;
  min-width: 3.25rem;
  height: 100%;
  padding: 0 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font: 600 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  background: rgba(8, 14, 10, 0.65);
  white-space: nowrap;
}

.stepper-value:disabled {
  opacity: 1;
  cursor: default;
}

.stepper-caption {
  flex: 0 0 auto;
  height: 100%;
  padding: 0 8px;
  display: flex;
  align-items: center;
  color: var(--text-muted);
  font: 600 11px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  background: rgba(8, 14, 10, 0.4);
}

.touch-control-active {
  border-color: var(--border-active);
  background: var(--surface-active);
  color: var(--text);
}

#labels-toggle-button.touch-control-active,
#cad-toggle-button.touch-control-active,
#touch-mode-toggle-button.touch-control-active {
  border-color: var(--border-active);
  background: var(--surface-active);
  color: var(--text);
}

#beacon-button.btn-beacon {
  padding: 10px 22px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.04em;
  border-color: var(--beacon-border);
  background: var(--beacon-bg);
  color: var(--beacon-text);
  box-shadow: 0 0 0 1px var(--beacon-glow);
}

#beacon-button.btn-beacon:hover:not(:disabled) {
  filter: brightness(1.08);
}

#beacon-button.beacon-button-running:disabled {
  border-color: var(--beacon-running-border);
  background: var(--beacon-running-bg);
  color: var(--beacon-running-text);
  opacity: 1;
  cursor: wait;
  animation: beacon-running-pulse 1.1s ease-in-out infinite;
}

@keyframes beacon-running-pulse {
  0%,
  100% {
    box-shadow:
      0 0 0 1px var(--beacon-running-glow),
      0 0 10px rgba(220, 168, 48, 0.2);
  }
  50% {
    box-shadow:
      0 0 0 2px var(--beacon-running-glow),
      0 0 18px rgba(220, 168, 48, 0.42);
  }
}

#bottom-toolbar #beacon-button,
#bottom-toolbar #import-scene-button,
#bottom-toolbar #export-scene-button,
#bottom-toolbar #clear-reset-button {
  padding: 10px 22px;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.2;
  box-sizing: border-box;
}

#bottom-toolbar #import-scene-button,
#bottom-toolbar #export-scene-button {
  color: var(--text);
  border-color: var(--border);
  background: var(--surface);
}

#bottom-toolbar #import-scene-button:hover,
#bottom-toolbar #export-scene-button:hover {
  color: var(--text);
  border-color: var(--border-active);
  background: var(--surface-active);
}

.btn-muted {
  color: var(--text-muted);
  border-color: #243028;
  background: rgba(14, 20, 16, 0.92);
}

.btn-muted:hover {
  color: var(--text);
  border-color: var(--border);
}

#touch-reset-links-button:disabled {
  cursor: default;
  pointer-events: none;
}

#clear-reset-button.reset-hold-active {
  border-color: var(--danger-border);
  background: var(--danger-bg);
  color: var(--danger-text);
}

.btn-danger {
  border-color: #6a3a3a;
}

.btn-danger:hover {
  border-color: var(--danger-border);
  background: rgba(48, 22, 22, 0.96);
}

#bottom-toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px var(--dock-padding-x);
  padding-bottom: var(--dock-padding-bottom);
  padding-left: max(var(--dock-padding-x), env(safe-area-inset-left));
  pointer-events: none;
  box-sizing: border-box;
}

#bottom-toolbar .hud-panel--bottom > * {
  pointer-events: auto;
}

#selection-controls {
  position: fixed;
  left: 50%;
  bottom: var(--dock-padding-bottom);
  transform: translateX(-50%);
  z-index: 11;
  max-width: min(95vw, 720px);
  pointer-events: auto;
}

#selection-controls[hidden] {
  display: none;
}

#selection-controls .selection-toolbar {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: max-content;
  max-width: min(95vw, 720px);
  padding: 10px 12px;
}

#selection-controls .selection-toolbar .toolbar-label {
  line-height: 1;
  padding: 0 2px 2px;
}

#selection-controls .selection-toolbar .toolbar-buttons {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

#selection-controls .selection-toolbar .toolbar-buttons > .stepper,
#selection-controls .selection-toolbar .toolbar-buttons > .toolbar-chip,
#selection-controls .selection-toolbar .toolbar-buttons > .touch-control {
  height: var(--control-h);
  min-height: var(--control-h);
  max-height: var(--control-h);
  box-sizing: border-box;
}

#selection-controls .selection-toolbar .toolbar-buttons > .touch-control {
  padding: 0 12px;
  line-height: 1;
  font: 600 13px/1 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

#controls-help {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  right: max(var(--dock-padding-x), env(safe-area-inset-right));
  z-index: 10;
  width: min(220px, 42vw);
  pointer-events: none;
}

#controls-help .hud-panel--help {
  pointer-events: auto;
}

.controls-help-toggle {
  width: 100%;
  box-sizing: border-box;
  pointer-events: auto;
  height: var(--control-h);
  min-height: var(--control-h);
  max-height: var(--control-h);
  padding: 0 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  border-radius: var(--radius);
  font: 600 13px/1.2 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  cursor: pointer;
  touch-action: manipulation;
}

.controls-help-toggle:hover {
  color: var(--text);
  border-color: var(--border-active);
}

.controls-help-panel {
  width: 100%;
  box-sizing: border-box;
  pointer-events: auto;
  padding: 8px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: rgba(14, 18, 14, 0.88);
  color: var(--text-muted);
  font: 500 11px/1.45 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

#controls-help-panel[hidden] {
  display: none;
}

.controls-help-heading {
  margin: 0 0 6px;
  font: 600 10px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--brand);
}

.controls-help-section {
  margin: 0;
}

.controls-help-section + .controls-help-section {
  margin-top: 6px;
}

.controls-help-label {
  margin: 0 0 3px;
  font: 600 9px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.controls-help-section ul {
  margin: 0;
  padding: 0;
  list-style: none;
}

.controls-help-section li {
  margin: 0;
  padding: 1px 0;
}

.controls-help-section kbd {
  display: inline-block;
  min-width: 1.1em;
  padding: 1px 4px;
  border-radius: 3px;
  border: 1px solid var(--border);
  background: rgba(16, 22, 18, 0.85);
  color: var(--text);
  font: 600 10px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-align: center;
}

.controls-help-canvas,
.controls-help-touch {
  display: none;
}

@media (pointer: coarse) {
  .controls-help-mouse {
    display: none;
  }

  .controls-help-canvas {
    display: block;
  }
}

@media (any-pointer: coarse) {
  .controls-help-touch {
    display: block;
  }
}

#propagation-stats-hud {
  position: fixed;
  right: max(var(--dock-padding-x), env(safe-area-inset-right));
  bottom: var(--dock-padding-bottom);
  z-index: 10;
  pointer-events: auto;
}

#propagation-stats-hud[hidden] {
  display: none;
}

.propagation-stats-panel {
  display: flex;
  align-items: stretch;
  gap: 4px;
  padding: 6px;
}

.propagation-stat-nav {
  width: 28px;
  flex: 0 0 28px;
  padding: 0;
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
}

.propagation-stat-nav:disabled {
  opacity: 0.4;
  cursor: default;
}

#propagation-stat-card {
  border: none;
  background: transparent;
  color: var(--text);
  width: 272px;
  min-width: 0;
  flex: 1 1 272px;
  max-width: min(272px, calc(100vw - 96px));
  box-sizing: border-box;
  padding: 8px 10px;
  border-radius: var(--radius);
  font: 600 12px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-variant-numeric: tabular-nums;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  text-align: left;
}

#node-ids-layer {
  position: fixed;
  inset: 0;
  z-index: 9;
  pointer-events: none;
}

.node-id-label {
  position: fixed;
  color: #d4ead8;
  font: 700 12px/1 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  transform: translate(-50%, -50%);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.85);
  white-space: nowrap;
}

#node-stats-layer {
  position: fixed;
  inset: 0;
  z-index: 8;
  pointer-events: none;
}

.node-stat {
  position: fixed;
  border: 1px solid #344a3c;
  background: rgba(12, 18, 14, 0.92);
  color: #c8e6d0;
  padding: 2px 5px;
  border-radius: 3px;
  font: 600 11px/1.2 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre;
}

@media (max-width: 720px) {
  #top-toolbar > .brand-row {
    position: static;
    transform: none;
    justify-content: center;
    padding-bottom: 6px;
  }

  .toolbar-groups--stacked {
    width: 100%;
  }

  #bottom-toolbar {
    flex-wrap: wrap;
  }

  #bottom-toolbar #beacon-button,
  #bottom-toolbar #import-scene-button,
  #bottom-toolbar #export-scene-button,
  #bottom-toolbar #clear-reset-button {
    flex: 1 1 auto;
    text-align: center;
  }

  #selection-controls {
    left: var(--dock-padding-x);
    right: var(--dock-padding-x);
    transform: none;
    max-width: none;
  }

  #propagation-stats-hud {
    left: var(--dock-padding-x);
    right: var(--dock-padding-x);
  }

  .propagation-stats-panel {
    width: 100%;
  }

  #propagation-stat-card {
    flex: 1 1 auto;
    width: auto;
    min-width: 0;
  }
}
