:root {
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  --bg: #ffffff;
  --canvas-bg: #f6f8fa;
  --surface: rgba(255, 255, 255, 0.9);
  --surface-hover: rgba(31, 35, 40, 0.06);
  --text: #1f2328;
  --muted: #656d76;
  --border: rgba(31, 35, 40, 0.14);
  --divider: rgba(31, 35, 40, 0.1);
  --accent: #0969da;
  --accent-soft: rgba(9, 105, 218, 0.12);
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.08);
  --axis-x: #cf222e;
  --axis-y: #1a7f37;
  --axis-z: #0969da;
  --triad-wire: rgba(31, 35, 40, 0.28);
  --triad-wire-hover: rgba(31, 35, 40, 0.55);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1117;
    --canvas-bg: #10151c;
    --surface: rgba(17, 22, 29, 0.88);
    --surface-hover: rgba(230, 237, 243, 0.07);
    --text: #e6edf3;
    --muted: #8b949e;
    --border: rgba(230, 237, 243, 0.12);
    --divider: rgba(230, 237, 243, 0.08);
    --accent: #58a6ff;
    --accent-soft: rgba(88, 166, 255, 0.15);
    --shadow: 0 8px 24px rgba(0, 0, 0, 0.36);
    --axis-x: #f85149;
    --axis-y: #3fb950;
    --axis-z: #58a6ff;
    --triad-wire: rgba(230, 237, 243, 0.22);
    --triad-wire-hover: rgba(230, 237, 243, 0.52);
  }
}

:root[data-theme="dark"] {
  --bg: #0d1117;
  --canvas-bg: #10151c;
  --surface: rgba(17, 22, 29, 0.88);
  --surface-hover: rgba(230, 237, 243, 0.07);
  --text: #e6edf3;
  --muted: #8b949e;
  --border: rgba(230, 237, 243, 0.12);
  --divider: rgba(230, 237, 243, 0.08);
  --accent: #58a6ff;
  --accent-soft: rgba(88, 166, 255, 0.15);
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.36);
  --axis-x: #f85149;
  --axis-y: #3fb950;
  --axis-z: #58a6ff;
  --triad-wire: rgba(230, 237, 243, 0.22);
  --triad-wire-hover: rgba(230, 237, 243, 0.52);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--canvas-bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
}

#viewport {
  position: relative;
  width: 100%;
  height: 100%;
  touch-action: none;
  background-color: var(--canvas-bg);
}

#viewport canvas.webgl {
  display: block;
  width: 100%;
  height: 100%;
  outline: none;
}

/* Status / Loading Overlay */
#status {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  background: var(--canvas-bg);
  z-index: 30;
}

#status.hidden {
  display: none;
}

#status.error {
  color: var(--axis-x);
}

/* Subtle Auto-Fading Hint */
#hint {
  position: absolute;
  top: 9px;
  left: 10px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1;
  color: var(--muted);
  opacity: 0.58;
  pointer-events: none;
  z-index: 12;
  transition: opacity 0.28s ease;
}

#hint.dismissed {
  opacity: 0;
}

@media (max-width: 480px) {
  #hint {
    display: none;
  }
}

/* Top-right Docked Control Bar */
.controls {
  position: absolute;
  top: 0;
  right: 0;
  display: inline-flex;
  align-items: center;
  gap: 0;
  padding: 0;
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid var(--border);
  border-left: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 15;
}

.ctrl-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  height: 27px;
  padding: 0 9px;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 500;
  line-height: 1;
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s;
}

.ctrl-btn.ctrl-icon {
  width: 28px;
  padding: 0;
}

.ctrl-btn:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.ctrl-btn:focus-visible {
  outline: 1.5px solid var(--accent);
  outline-offset: -1.5px;
}

.ctrl-btn.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.ctrl-btn.hidden {
  display: none;
}

.ctrl-sep {
  width: 1px;
  height: 27px;
  margin: 0;
  background: var(--border);
  flex-shrink: 0;
}

#btn-fullscreen .icon-compress {
  display: none;
}

#btn-fullscreen.active .icon-expand {
  display: none;
}

#btn-fullscreen.active .icon-compress {
  display: block;
}

/* Parts dropdown (multi-part assemblies only) */
#parts-panel {
  position: absolute;
  top: 28px;
  right: 0;
  width: 210px;
  max-height: 55%;
  overflow-y: auto;
  padding: 4px;
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid var(--border);
  border-left: 1px solid var(--border);
  border-radius: 0;
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: none;
  z-index: 16;
  font-family: var(--mono);
  font-size: 11px;
}

#parts-panel.hidden {
  display: none;
}

.part-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 0;
  cursor: pointer;
}

.part-row:hover {
  background: var(--surface-hover);
}

.part-swatch {
  width: 9px;
  height: 9px;
  border-radius: 0;
  border: 1px solid rgba(128, 128, 128, 0.35);
  flex-shrink: 0;
}

.part-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Bottom-left Wireframe Orientation Triad */
.bottom-left {
  position: absolute;
  left: 10px;
  bottom: 10px;
  display: flex;
  align-items: flex-end;
  pointer-events: none;
  z-index: 12;
}

#triad {
  width: 68px;
  height: 68px;
  pointer-events: auto;
  cursor: pointer;
  flex-shrink: 0;
}

/* Bottom-right Docked Stack (Hover readout + Measurement Inspector) */
.bottom-right {
  position: absolute;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0;
  max-width: min(210px, calc(100% - 88px));
  z-index: 20;
  pointer-events: none;
}

#hover-pill {
  display: none;
  padding: 2px 7px;
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
  border-right: 0;
  border-bottom: 0;
  border-radius: 0;
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#hover-pill.visible {
  display: block;
}

/* Bottom-right Docked Measurement Readout */
#measure-card {
  min-width: 148px;
  max-height: calc(100vh - 58px);
  overflow-y: auto;
  padding: 4px 7px 5px;
  border-top: 1px solid var(--border);
  border-left: 1px solid var(--border);
  border-right: 0;
  border-bottom: 0;
  border-radius: 0;
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: none;
  font-family: var(--mono);
  font-size: 10px;
  line-height: 1.25;
  font-variant-numeric: tabular-nums;
  pointer-events: auto;
  -webkit-user-select: text;
  user-select: text;
}

#measure-card.hidden {
  display: none;
}

.mc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-bottom: 2px;
}

.mc-title {
  font-size: 9px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.mc-close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  margin-right: -2px;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
  color: var(--muted);
  font-family: inherit;
  font-size: 12px;
  line-height: 1;
  cursor: pointer;
  transition: background-color 0.12s, color 0.12s;
}

.mc-close:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.mc-section {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.mc-section-sep {
  margin-top: 3px;
  padding-top: 3px;
  border-top: 1px solid var(--divider);
}

.mc-primary {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}

.mc-primary-label {
  font-size: 10px;
  font-weight: 500;
  color: var(--muted);
  white-space: nowrap;
}

.mc-primary-value {
  font-size: 10.5px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.mc-sublabel {
  font-weight: 500;
  color: var(--text);
}

.mc-subval {
  font-weight: 600;
}

.mc-rows {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.mc-rows:not(:empty) {
  margin-top: 3px;
  padding-top: 3px;
  border-top: 1px solid var(--divider);
}

.mc-rows > .mc-section-sep:first-child {
  margin-top: 0;
  padding-top: 0;
  border-top: 0;
}

.mc-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}

.mc-deltas-block {
  display: flex;
  flex-direction: column;
  gap: 0;
  padding: 1px 0 0 6px;
  font-size: 9.5px;
  line-height: 1.2;
}

.mc-deltas-block .mc-val {
  font-size: 9.5px;
  color: var(--muted);
}

.mc-hoverable {
  margin-left: -4px;
  margin-right: -4px;
  padding-left: 4px;
  padding-right: 4px;
  border-radius: 0;
  cursor: pointer;
}

.mc-deltas-block.mc-hoverable {
  padding-left: 10px;
}

.mc-hoverable:hover {
  background: var(--accent-soft);
}

.mc-key {
  color: var(--muted);
  white-space: nowrap;
}

.mc-val {
  font-size: 10px;
  color: var(--text);
  text-align: right;
  white-space: nowrap;
}

.axis-x {
  color: var(--axis-x);
}

.axis-y {
  color: var(--axis-y);
}

.axis-z {
  color: var(--axis-z);
}
