:root {
  --bg-deep: #0b1412;
  --card-bg: rgba(12, 23, 19, 0.82);
  --card-border: rgba(148, 184, 168, 0.2);
  --text-main: #edf4f0;
  --text-muted: #9cb2a8;
  --accent-emerald: #18b26b;
  --accent-amber: #f59e0b;
  --accent-danger: #e5484d;
  --accent-gold: #e2b049;
  --focus-ring: #38bdf8;
}

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

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg-deep);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 13px;
  line-height: 1.4;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

#app {
  position: relative;
  width: 100vw;
  height: 100dvh;
  overflow: hidden;
}

#viewport {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#viewport:active {
  cursor: grabbing;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* Top-Left Ultra-Compact Live Telemetry Pill */
.hud-top-left {
  position: absolute;
  top: 10px;
  left: 10px;
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  border: 1px solid var(--card-border);
  z-index: 20;
  pointer-events: none;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  max-width: calc(100vw - 170px);
}

.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent-emerald);
  box-shadow: 0 0 8px var(--accent-emerald);
  flex-shrink: 0;
}

.stat-item strong,
.stat-item b {
  color: #fff;
  font-weight: 700;
}

.stat-item small {
  color: var(--text-muted);
  font-size: 11px;
}

.stat-sep {
  color: rgba(255, 255, 255, 0.25);
}

.stat-ok {
  color: #4ade80;
  font-weight: 700;
  font-size: 11.5px;
}

/* Top-Right Quick Actions */
.hud-top-right {
  position: absolute;
  top: 10px;
  right: 10px;
  display: flex;
  align-items: center;
  gap: 5px;
  z-index: 25;
}

.pill-btn {
  appearance: none;
  display: inline-grid;
  place-items: center;
  min-width: 34px;
  height: 34px;
  padding: 0 9px;
  border-radius: 999px;
  background: var(--card-bg);
  backdrop-filter: blur(12px);
  border: 1px solid var(--card-border);
  color: var(--text-main);
  font-size: 12.5px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.pill-btn:hover {
  background: rgba(255, 255, 255, 0.14);
}

.pill-btn.active,
.pill-btn[aria-expanded="true"],
.pill-btn[aria-pressed="true"] {
  background: rgba(226, 176, 73, 0.22);
  border-color: var(--accent-gold);
  color: #fde68a;
}

/* Bottom Touch-Friendly Camera View Bar */
.camera-bar {
  position: absolute;
  bottom: max(12px, env(safe-area-inset-bottom, 12px));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 8px;
  background: var(--card-bg);
  backdrop-filter: blur(14px);
  border: 1px solid var(--card-border);
  border-radius: 999px;
  z-index: 20;
  max-width: calc(100vw - 20px);
  overflow-x: auto;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.camera-bar::-webkit-scrollbar {
  display: none;
}

.cam-btn {
  appearance: none;
  border: 1px solid transparent;
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
  padding: 6px 11px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background 0.15s, border-color 0.15s;
}

.cam-btn:hover {
  background: rgba(255, 255, 255, 0.12);
}

.cam-btn.active {
  background: rgba(226, 176, 73, 0.22);
  border-color: var(--accent-gold);
  color: #fde68a;
}

/* On-Demand Engineering Drawer (Closed by Default) */
.eng-drawer {
  position: absolute;
  top: 52px;
  right: 10px;
  width: min(290px, calc(100vw - 20px));
  max-height: calc(100dvh - 115px);
  overflow-y: auto;
  z-index: 30;
}

.card {
  background: rgba(11, 21, 18, 0.94);
  backdrop-filter: blur(16px);
  border: 1px solid var(--card-border);
  border-radius: 14px;
  padding: 12px 14px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.45);
}

.eyebrow {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--accent-gold);
}

.form-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.stage-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 10px;
}

.stage-pills span {
  font-size: 10.5px;
  padding: 2px 6px;
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.06);
  color: var(--text-muted);
}

.stage-pills b {
  color: var(--text-main);
}

fieldset {
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 7px 9px 9px;
  margin: 0 0 8px;
}

legend {
  padding: 0 5px;
  font-size: 10.5px;
  font-weight: 700;
  color: var(--accent-gold);
}

.injector-buttons {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  margin-bottom: 6px;
}

.btn-inject {
  position: relative;
  text-align: center;
  padding: 6px 4px 6px 14px;
  border-radius: 7px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text-main);
  font-size: 11px;
  font-weight: 600;
  cursor: pointer;
}

.dot {
  position: absolute;
  left: 6px;
  top: 50%;
  transform: translateY(-50%);
  width: 6px;
  height: 6px;
  border-radius: 50%;
}

.dot.ok {
  background: var(--accent-emerald);
}
.dot.danger {
  background: var(--accent-danger);
}
.dot.warn {
  background: var(--accent-amber);
}

.batch-actions {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  gap: 5px;
}

.field {
  margin-bottom: 8px;
}

.field:last-child {
  margin-bottom: 0;
}

.field label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11.5px;
  font-weight: 500;
  margin-bottom: 2px;
}

.field output {
  font-weight: 700;
  color: #fde68a;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.field input[type="range"] {
  width: 100%;
  accent-color: var(--accent-gold);
  cursor: pointer;
}

.radio-grid,
.checkbox-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
}

.radio-option,
.check-option {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  cursor: pointer;
}

.form-footer {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.btn-primary,
.btn-secondary,
.btn-outline,
.btn-icon {
  appearance: none;
  border-radius: 7px;
  font-size: 11.5px;
  font-weight: 600;
  padding: 6px 10px;
  cursor: pointer;
  border: 1px solid transparent;
}

.btn-primary {
  background: var(--accent-emerald);
  color: #062214;
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.18);
  color: var(--text-main);
}

.btn-outline {
  background: transparent;
  border-color: rgba(226, 176, 73, 0.5);
  color: #fde68a;
}

.btn-icon {
  padding: 2px 7px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
}

/* 3D World Labels */
#world-labels {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 10;
}

.wlabel {
  position: absolute;
  transform: translate(-50%, -100%);
  padding: 2px 7px;
  border-radius: 5px;
  background: rgba(11, 20, 18, 0.78);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #e2e8f0;
  font-size: 10px;
  font-weight: 600;
  white-space: nowrap;
}

.wlabel.highlight {
  border-color: var(--accent-gold);
  color: #fde68a;
}

.wlabel.ok {
  border-color: var(--accent-emerald);
  color: #6ee7b7;
}

.wlabel.warn {
  border-color: var(--accent-amber);
  color: #fcd34d;
}

/* Modal Dialogs (<dialog closedby="any">) */
dialog.modal-dialog {
  width: min(540px, calc(100vw - 24px));
  max-height: calc(100dvh - 80px);
  overflow-y: auto;
  background: rgba(12, 22, 19, 0.96);
  backdrop-filter: blur(18px);
  border: 1px solid var(--accent-gold);
  border-radius: 14px;
  padding: 16px 18px;
  color: var(--text-main);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.72);
}

dialog.modal-dialog::backdrop {
  background-color: rgba(4, 10, 8, 0.62);
  backdrop-filter: blur(3px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.modal-header h2 {
  margin: 0;
  font-size: 15px;
  color: #fde68a;
}

.modal-body p,
.modal-body li {
  font-size: 12.5px;
  line-height: 1.5;
  color: #d8e6df;
}

.modal-body ol {
  padding-left: 18px;
  margin: 8px 0;
}

.modal-body li {
  margin-bottom: 6px;
}

/* Loading Overlay */
#loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(11, 20, 18, 0.94);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
  text-align: center;
  padding: 20px;
}

.spinner {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: 3px solid rgba(226, 176, 73, 0.25);
  border-top-color: var(--accent-gold);
  animation: spin 0.85s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 600px) {
  .hud-top-left {
    top: 8px;
    left: 8px;
    padding: 5px 10px;
    font-size: 11px;
    gap: 5px;
    max-width: calc(100vw - 154px);
  }
  .kpi-pack-row {
    display: none;
  }
  .hud-top-right {
    top: 8px;
    right: 8px;
    gap: 4px;
  }
  .pill-btn {
    min-width: 32px;
    height: 32px;
    padding: 0 8px;
    font-size: 11.5px;
  }
  .camera-bar {
    bottom: max(10px, env(safe-area-inset-bottom, 10px));
    padding: 4px 6px;
    gap: 4px;
  }
  .cam-btn {
    padding: 5px 9px;
    font-size: 11px;
  }
}
