:root {
  --bg: #0e1115;
  --surface: #15191e;
  --surface-2: #1a1f25;
  --surface-3: #222931;
  --surface-4: #2b333d;
  --border: #313943;
  --border-soft: #242b33;
  --text: #edf2f7;
  --muted: #97a2ae;
  --muted-2: #6f7b87;
  --accent: #f7ad28;
  --accent-2: #ffbf4f;
  --blue: #2e92ff;
  --danger: #ef6666;
  --focus: rgba(46, 146, 255, .35);
  --panel-width: 280px;
  --right-panel-width: 300px;
  --toolbar-h: 44px;
  --status-h: 30px;
  --grid-minor: rgba(255,255,255,.035);
  --grid-major: rgba(255,255,255,.07);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
}

button, input, select, textarea { font: inherit; }
button { color: inherit; }

.app-shell {
  width: 100vw;
  height: 100vh;
  display: grid;
  grid-template-rows: 52px minmax(0, 1fr);
  background: var(--bg);
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(160px, 1fr) auto auto;
  align-items: center;
  gap: 18px;
  min-width: 0;
  padding: 0 14px;
  background: linear-gradient(180deg, #171b20 0%, #12161a 100%);
  border-bottom: 1px solid var(--border-soft);
}

.app-page-nav {
  display: flex;
  align-items: center;
  gap: 3px;
  padding: 3px;
  border: 1px solid var(--border-soft);
  border-radius: 7px;
  background: #0e1216;
}

.app-page-link {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  padding: 0 11px;
  border-radius: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
}

.app-page-link:hover { color: var(--text); background: var(--surface-2); }
.app-page-link.active { color: #171109; background: var(--accent); }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 135px;
  letter-spacing: .01em;
}

.brand-mark {
  position: relative;
  width: 24px;
  height: 24px;
}

.brand-mark span {
  position: absolute;
  width: 9px;
  height: 9px;
  background: var(--accent);
  border-radius: 3px;
  transform: rotate(45deg);
}
.brand-mark span:nth-child(1) { left: 8px; top: 1px; }
.brand-mark span:nth-child(2) { left: 2px; top: 12px; }
.brand-mark span:nth-child(3) { left: 14px; top: 12px; }

.project-title-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.project-name {
  min-width: 140px;
  max-width: 340px;
  width: min(340px, 45vw);
  padding: 7px 10px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 5px;
  color: var(--text);
  font-weight: 600;
  outline: none;
}
.project-name:hover,
.project-name:focus {
  background: var(--surface-2);
  border-color: var(--border);
}

.save-state {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}
.save-state.dirty { color: var(--accent-2); }

.top-actions { display: flex; gap: 8px; align-items: center; }

.button,
.tool-button,
.icon-button,
.zoom-readout {
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 5px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease, color .12s ease, transform .06s ease;
}
.button:hover,
.tool-button:hover,
.icon-button:hover,
.zoom-readout:hover { background: var(--surface-3); border-color: #46515e; }
.button:active,
.tool-button:active { transform: translateY(1px); }
.button:focus-visible,
.tool-button:focus-visible,
.icon-button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}

.button {
  min-height: 32px;
  padding: 6px 11px;
  font-size: 12px;
  font-weight: 600;
}
.button.primary {
  border-color: #1479d8;
  background: #167edc;
  color: white;
}
.button.primary:hover { background: #218ae7; }
.button.subtle { background: #20262d; }
.button.full { width: 100%; }
.button.grow { flex: 1; }

.workspace {
  min-height: 0;
  display: grid;
  grid-template-columns: var(--panel-width) minmax(0, 1fr) var(--right-panel-width);
}

.panel {
  min-height: 0;
  background: var(--surface);
  overflow: hidden;
}
.left-panel { border-right: 1px solid var(--border-soft); }
.right-panel { border-left: 1px solid var(--border-soft); }

.panel-tabs {
  height: 40px;
  display: flex;
  align-items: stretch;
  border-bottom: 1px solid var(--border);
  background: #161a1f;
}
.panel-tab {
  flex: 1;
  min-width: 0;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
}
.panel-tab:hover { color: var(--text); background: rgba(255,255,255,.025); }
.panel-tab.active { color: var(--text); border-bottom-color: var(--blue); }

.tab-page { display: none; height: calc(100% - 40px); min-height: 0; }
.tab-page.active { display: block; }

.search-wrap { padding: 10px; border-bottom: 1px solid var(--border-soft); }
.search-wrap input {
  width: 100%;
  height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #0f1317;
  color: var(--text);
  outline: none;
}
.search-wrap input::placeholder { color: var(--muted-2); }

.library-list {
  height: calc(100% - 53px);
  overflow: auto;
  padding-bottom: 18px;
}
.library-category { border-bottom: 1px solid rgba(255,255,255,.035); }
.library-category > summary {
  list-style: none;
  cursor: pointer;
  padding: 10px 12px;
  color: #dfe6ed;
  font-size: 12px;
  font-weight: 700;
  user-select: none;
}
.library-category > summary::-webkit-details-marker { display: none; }
.library-category > summary::before {
  content: "▾";
  display: inline-block;
  width: 16px;
  color: var(--muted);
  transition: transform .12s ease;
}
.library-category:not([open]) > summary::before { transform: rotate(-90deg); }

.library-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 4px;
  padding: 0 7px 10px;
}
.library-item {
  min-width: 0;
  min-height: 76px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 7px 4px;
  border: 1px solid transparent;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  cursor: grab;
  text-align: center;
  user-select: none;
}
.library-item:hover { border-color: var(--border); background: rgba(255,255,255,.035); color: var(--text); }
.library-item:active { cursor: grabbing; }
.library-item-icon {
  width: 42px;
  height: 34px;
  display: grid;
  place-items: center;
  border: 1px solid #697582;
  border-radius: 4px;
  color: var(--icon-accent, #d3dae1);
  font-size: 17px;
  font-weight: 700;
  background: rgba(255,255,255,.015);
}
.library-item-icon.compact-icon { font-size: 11px; letter-spacing: .02em; }
.library-item-name {
  width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 10px;
  line-height: 1.2;
}
.library-empty { padding: 20px; color: var(--muted); text-align: center; font-size: 12px; }

.layers-header {
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 10px 0 12px;
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .08em;
  border-bottom: 1px solid var(--border-soft);
}
.icon-button {
  width: 25px;
  height: 25px;
  display: grid;
  place-items: center;
  padding: 0;
}
.layers-list { height: calc(100% - 40px); overflow: auto; padding: 6px; }
.layer-row {
  display: grid;
  grid-template-columns: 20px minmax(0, 1fr) auto;
  align-items: center;
  gap: 7px;
  min-height: 34px;
  padding: 0 7px;
  border: 1px solid transparent;
  border-radius: 4px;
  cursor: pointer;
  color: #c7d0d9;
  font-size: 11px;
}
.layer-row:hover { background: rgba(255,255,255,.03); }
.layer-row.selected { border-color: rgba(46,146,255,.55); background: rgba(46,146,255,.08); }
.layer-swatch { width: 11px; height: 11px; border-radius: 3px; border: 1px solid rgba(255,255,255,.3); }
.layer-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-type { color: var(--muted-2); font-size: 9px; text-transform: uppercase; }

.editor-column {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: var(--toolbar-h) minmax(0, 1fr) var(--status-h);
  background: #0f1317;
}
.toolbar {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 0 8px;
  overflow-x: auto;
  overflow-y: hidden;
  background: #15191e;
  border-bottom: 1px solid var(--border-soft);
}
.toolbar-group { display: flex; align-items: center; gap: 4px; }
.toolbar-separator { width: 1px; height: 22px; background: var(--border); margin: 0 4px; flex: 0 0 auto; }
.tool-button,
.zoom-readout {
  min-width: 30px;
  height: 30px;
  padding: 0 7px;
  display: grid;
  place-items: center;
  font-size: 13px;
}
.tool-button.active { border-color: #46617b; background: #263342; color: white; }
.tool-button.danger:hover { color: #ff8b8b; border-color: #794348; }
.tool-button.wide { min-width: 40px; font-size: 11px; }
.zoom-readout { min-width: 52px; font-size: 11px; }
.toolbar-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
  user-select: none;
}
.toolbar-toggle.standalone { min-height: 32px; }
.toolbar-toggle input { accent-color: var(--blue); }
.toolbar-select {
  height: 29px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--muted);
  padding: 0 6px;
  font-size: 11px;
}

.viewport {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #0c1014;
  cursor: default;
  outline: none;
}
.viewport.pan-mode,
.viewport.panning { cursor: grab; }
.viewport.panning { cursor: grabbing; }
.viewport.hide-grid .world { background-image: none; }

.world {
  position: absolute;
  left: 0;
  top: 0;
  width: 6000px;
  height: 4000px;
  transform-origin: 0 0;
  background-color: #11161b;
  background-image:
    var(--custom-background, none),
    linear-gradient(to right, var(--grid-major) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-major) 1px, transparent 1px),
    linear-gradient(to right, var(--grid-minor) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-minor) 1px, transparent 1px);
  background-size: cover, 100px 100px, 100px 100px, 20px 20px, 20px 20px;
  background-repeat: no-repeat, repeat, repeat, repeat, repeat;
}
.connection-layer,
.node-layer {
  position: absolute;
  inset: 0;
  width: 6000px;
  height: 4000px;
}
.connection-layer { overflow: visible; pointer-events: none; }
.node-layer { pointer-events: none; }

.viewport.floor-plan-mode .connection-layer,
.viewport.floor-plan-mode .ports { display: none; }

.viewport.floor-plan-mode .canvas-node:not(.area-node):not(.label-node) {
  box-shadow: 0 4px 12px rgba(0,0,0,.16);
}

.connection-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 14;
  pointer-events: stroke;
  cursor: pointer;
}
.connection {
  fill: none;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}
.connection.selected { filter: drop-shadow(0 0 4px rgba(255,255,255,.75)); stroke-width: 3.4; }
.connection.preview { stroke: #c8d1da; stroke-dasharray: 6 5; opacity: .85; }
.connection-label-bg { fill: #0e1216; stroke: #303844; stroke-width: 1; rx: 4; }
.connection-label { fill: #dce4ec; font-size: 11px; text-anchor: middle; dominant-baseline: middle; pointer-events: none; }

.canvas-node {
  position: absolute;
  min-width: 40px;
  min-height: 30px;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  gap: 9px;
  padding: 10px 12px;
  border: 1px solid var(--node-stroke, #596574);
  border-radius: 6px;
  background: var(--node-fill, #20262d);
  color: #f2f6f9;
  box-shadow: 0 7px 18px rgba(0,0,0,.18);
  pointer-events: auto;
  cursor: move;
  user-select: none;
  touch-action: none;
}
.canvas-node.area-node {
  display: block;
  padding: 0;
  background: color-mix(in srgb, var(--node-fill, #20262d) 58%, transparent);
  box-shadow: none;
  border-style: dashed;
}
.canvas-node.area-node .node-content {
  position: absolute;
  left: 10px;
  top: 8px;
}
.canvas-node.area-node .node-icon { display: none; }
.canvas-node.shape-node .node-subtitle,
.canvas-node.label-node .node-subtitle { display: none; }
.canvas-node.label-node {
  min-width: 100px;
  min-height: 34px;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
  padding: 4px 6px;
  cursor: move;
}
.canvas-node.label-node .node-icon,
.canvas-node.label-node .node-topline { display: none; }
.canvas-node.label-node .node-title { font-size: 16px; }
.canvas-node.selected {
  outline: 2px solid var(--blue);
  outline-offset: 2px;
  box-shadow: 0 0 0 1px rgba(46,146,255,.25), 0 7px 18px rgba(0,0,0,.22);
}
.canvas-node.locked { cursor: not-allowed; opacity: .82; }
.node-topline {
  position: absolute;
  left: 8px;
  right: 8px;
  top: 0;
  height: 2px;
  border-radius: 0 0 2px 2px;
  background: var(--node-accent, var(--accent));
  opacity: .8;
}
.node-icon {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid rgba(255,255,255,.12);
  border-radius: 5px;
  background: rgba(255,255,255,.035);
  color: var(--icon-accent, #dfe6ec);
  font-size: 15px;
  font-weight: 700;
}
.node-icon.compact-icon { font-size: 10px; letter-spacing: .02em; }
.node-content { min-width: 0; pointer-events: none; }
.node-title {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-weight: 700;
  font-size: 12px;
  line-height: 1.2;
}
.node-subtitle {
  min-width: 0;
  margin-top: 3px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  color: #9ba7b3;
  font-size: 9px;
  line-height: 1.1;
  text-transform: uppercase;
  letter-spacing: .08em;
}
.resize-handle {
  position: absolute;
  right: -5px;
  bottom: -5px;
  width: 10px;
  height: 10px;
  border: 1px solid white;
  border-radius: 2px;
  background: var(--blue);
  cursor: nwse-resize;
  opacity: 0;
  pointer-events: none;
}
.canvas-node.selected .resize-handle { opacity: 1; pointer-events: auto; }

.ports {
  position: absolute;
  top: 8px;
  bottom: 8px;
  width: 12px;
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  pointer-events: none;
}
.ports-in { left: -7px; }
.ports-out { right: -7px; }
.port {
  position: relative;
  width: 12px;
  height: 12px;
  border: 2px solid #11161b;
  border-radius: 50%;
  background: var(--port-color, #9aa6b2);
  box-shadow: 0 0 0 1px rgba(255,255,255,.25);
  pointer-events: auto;
  cursor: crosshair;
}
.port:hover,
.port.armed { transform: scale(1.25); box-shadow: 0 0 0 2px white; }
.port::after {
  content: attr(data-port-label);
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  max-width: 130px;
  padding: 4px 6px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #0c1014;
  color: #e8edf1;
  font-size: 9px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  z-index: 10000;
}
.ports-in .port::after { left: 17px; }
.ports-out .port::after { right: 17px; }
.port:hover::after { opacity: 1; }

.selection-box {
  position: absolute;
  z-index: 30;
  border: 1px solid rgba(46, 146, 255, 0.95);
  background: rgba(46, 146, 255, 0.12);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.14);
  pointer-events: none;
}

.suggestion-panel {
  position: absolute;
  z-index: 35;
  width: 280px;
  max-height: calc(100% - 24px);
  display: flex;
  flex-direction: column;
  padding: 10px;
  border: 1px solid rgba(130, 152, 171, 0.45);
  border-radius: 12px;
  background: rgba(15, 19, 23, 0.96);
  box-shadow: 0 18px 34px rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(8px);
  pointer-events: auto;
  transform-origin: top left;
  animation: suggestion-in 120ms ease-out;
}

.suggestion-panel[hidden] { display: none; }

.suggestion-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 8px;
  color: #dfe7f0;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
}

.suggestion-header small {
  overflow: hidden;
  color: var(--muted);
  font-size: 9px;
  font-weight: 600;
  letter-spacing: 0;
  text-overflow: ellipsis;
  text-transform: none;
  white-space: nowrap;
}

.suggestion-search {
  height: 34px;
  display: grid;
  grid-template-columns: 18px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  flex: 0 0 auto;
  padding: 0 9px;
  border: 1px solid rgba(122, 135, 150, 0.42);
  border-radius: 7px;
  background: #0c1014;
  color: var(--muted);
}

.suggestion-search:focus-within {
  border-color: var(--blue);
  box-shadow: 0 0 0 1px rgba(46, 146, 255, .22);
}

.suggestion-search svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-width: 1.8;
}

.suggestion-search input {
  min-width: 0;
  height: 100%;
  padding: 0;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: 11px;
}

.suggestion-search input::placeholder { color: var(--muted-2); }
.suggestion-search input::-webkit-search-cancel-button { filter: invert(.75); }

.suggestion-results {
  min-height: 0;
  margin-top: 4px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.suggestion-item {
  width: 100%;
  display: grid;
  grid-template-columns: 26px minmax(0,1fr);
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  margin-top: 6px;
  border: 1px solid rgba(122, 135, 150, 0.3);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.02);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}

.suggestion-empty {
  padding: 18px 8px 12px;
  color: var(--muted);
  font-size: 10px;
  text-align: center;
}

.suggestion-item:hover {
  background: rgba(46, 146, 255, 0.08);
  border-color: rgba(46, 146, 255, 0.42);
}

.suggestion-item:focus-visible {
  outline: 2px solid var(--blue);
  outline-offset: 1px;
}

.suggestion-item-icon {
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.06);
  color: var(--icon-accent, inherit);
  font-size: 12px;
  font-weight: 700;
}
.suggestion-item-icon.compact-icon { font-size: 8px; letter-spacing: 0; }

.suggestion-item strong {
  display: block;
  font-size: 11px;
  line-height: 1.3;
}

.suggestion-item small {
  display: block;
  color: var(--muted);
  font-size: 9px;
  line-height: 1.3;
}

@keyframes suggestion-in {
  from { opacity: 0; transform: translateY(-4px) scale(.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (prefers-reduced-motion: reduce) {
  .suggestion-panel { animation: none; }
}

.canvas-hint {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  padding: 7px 11px;
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 5px;
  background: rgba(12,16,20,.78);
  backdrop-filter: blur(8px);
  color: var(--muted);
  font-size: 10px;
  pointer-events: none;
  transition: opacity .2s ease;
}
.canvas-hint.hidden { opacity: 0; }

.statusbar {
  display: grid;
  grid-template-columns: 150px minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  padding: 0 10px;
  background: #11151a;
  border-top: 1px solid var(--border-soft);
  color: var(--muted-2);
  font-size: 10px;
}
#statusSelection { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); }
.status-right { display: flex; gap: 15px; }

.inspector-page { overflow: auto; padding-bottom: 20px; }
.empty-inspector {
  display: grid;
  justify-items: center;
  gap: 7px;
  padding: 60px 28px 20px;
  text-align: center;
  color: var(--muted);
}
.empty-inspector h3 { margin: 0; font-size: 13px; color: #dbe3ea; }
.empty-inspector p { margin: 0; font-size: 11px; line-height: 1.5; }
.empty-icon { font-size: 30px; color: var(--muted-2); }
.inspector-section { padding: 12px; border-bottom: 1px solid var(--border-soft); }
.inspector-section.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.inspector-actions { display: flex; gap: 8px; }
.section-title { margin: 0 0 12px; font-size: 12px; }
.field-label {
  display: block;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .06em;
}
.field-input {
  width: 100%;
  min-height: 32px;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: #101419;
  color: var(--text);
  outline: none;
  font-size: 11px;
}
.field-textarea { resize: vertical; line-height: 1.45; }
.color-input { padding: 3px; }
.data-preview {
  min-height: 180px;
  margin: 0;
  padding: 10px;
  overflow: auto;
  border: 1px solid var(--border-soft);
  border-radius: 4px;
  background: #0d1115;
  color: #aeb9c4;
  font: 10px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}
.signal-legend { display: grid; gap: 7px; }
.signal-legend-row { display: grid; grid-template-columns: 12px 1fr; align-items: center; gap: 8px; color: #c7d0d8; font-size: 10px; }
.signal-dot { width: 10px; height: 10px; border-radius: 50%; }

.stage-calc-button {
  display: none;
  width: auto;
  min-width: 92px;
  grid-auto-flow: column;
  gap: 6px;
  padding: 0 9px;
  color: #f0c95d;
}

.stage-calculator-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(5, 8, 11, .76);
  backdrop-filter: blur(5px);
}

.stage-calculator-modal[hidden] { display: none; }

.stage-calculator {
  width: min(1080px, calc(100vw - 40px));
  max-height: min(760px, calc(100vh - 40px));
  overflow: hidden;
  border: 1px solid #3d4752;
  border-radius: 12px;
  background: #11161b;
  box-shadow: 0 30px 80px rgba(0,0,0,.55);
}

.stage-calculator-header {
  min-height: 76px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  background: #151a1f;
}

.stage-calculator-header h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1.25;
}

.stage-calculator-header p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 11px;
}

.stage-calculator-close {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  font-size: 18px;
}

.stage-calculator-body {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 265px;
}

body.page-stage-calculator {
  height: auto;
  min-height: 100%;
  overflow: auto;
}

body.page-stage-calculator .app-shell {
  height: 52px;
  display: block;
}

body.page-stage-calculator .topbar { height: 52px; }
body.page-stage-calculator .workspace { display: none; }

body.page-stage-calculator .stage-calculator-modal {
  position: relative;
  inset: auto;
  min-height: calc(100vh - 52px);
  padding: 28px;
  background: #0d1115;
  backdrop-filter: none;
}

body.page-stage-calculator .stage-calculator {
  width: min(1180px, calc(100vw - 56px));
  max-height: none;
  box-shadow: 0 20px 55px rgba(0,0,0,.35);
}

body.page-stage-calculator .stage-calculator-close { display: none; }
body.page-stage-calculator .stage-calculator-body { min-height: min(620px, calc(100vh - 184px)); }
body.page-stage-calculator .stage-grid-scroll { max-height: min(560px, calc(100vh - 250px)); }

.stage-layout-editor {
  min-width: 0;
  min-height: 0;
  padding: 16px 18px 18px;
  border-right: 1px solid var(--border-soft);
}

.stage-layout-controls {
  display: grid;
  gap: 8px;
  margin-bottom: 14px;
}

.stage-control-cluster {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: 8px;
}

.stage-add-decks,
.stage-selected-controls {
  padding-top: 8px;
  border-top: 1px solid var(--border-soft);
}

.stage-control-label {
  align-self: center;
  min-width: 64px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}

.stage-layout-controls label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 9px;
  font-weight: 700;
  letter-spacing: .06em;
  text-transform: uppercase;
}

.stage-layout-controls input,
.stage-layout-controls select {
  width: 72px;
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  outline: 0;
  background: #0c1014;
  color: var(--text);
  font-size: 11px;
}

.stage-layout-controls select { width: 132px; }
.stage-selected-controls .button:disabled,
.stage-layout-controls select:disabled { opacity: .42; cursor: not-allowed; }

.stage-selection-hint {
  align-self: center;
  color: var(--muted-2);
  font-size: 9px;
}

.stage-grid-scroll {
  max-height: min(500px, calc(100vh - 225px));
  overflow: auto;
  border: 1px solid #303944;
  border-radius: 8px;
  background-color: #0d1216;
  background-image:
    linear-gradient(rgba(69, 82, 95, .18) 1px, transparent 1px),
    linear-gradient(90deg, rgba(69, 82, 95, .18) 1px, transparent 1px);
  background-size: 16px 16px;
}

.stage-grid-wrap {
  position: relative;
  width: max-content;
  min-width: 100%;
  padding: 22px;
}

.stage-grid {
  position: relative;
  z-index: 1;
  background-color: rgba(10, 15, 19, .32);
  background-image:
    linear-gradient(rgba(83, 98, 111, .22) 1px, transparent 1px),
    linear-gradient(90deg, rgba(83, 98, 111, .22) 1px, transparent 1px);
  background-size: 48px 48px;
}

.stage-deck-slot {
  position: absolute;
  display: grid;
  place-items: center;
  overflow: hidden;
  border: 1px solid #a88626;
  border-radius: 3px;
  background: rgba(72, 60, 20, .42);
  box-shadow: inset 0 0 0 1px rgba(247, 173, 40, .08);
  cursor: grab;
  touch-action: none;
  user-select: none;
}

.stage-deck-slot:active { cursor: grabbing; }
.stage-deck-slot.selected { border-color: #35a3ff; box-shadow: inset 0 0 0 1px #35a3ff, 0 0 0 2px rgba(53,163,255,.18); }

.stage-add-deck {
  width: 100%;
  height: 100%;
  border: 0;
  background: transparent;
  color: var(--muted-2);
  cursor: pointer;
  font-size: 10px;
}

.stage-add-deck:hover { color: var(--text); background: rgba(255,255,255,.025); }

.stage-deck-label {
  position: absolute;
  left: 6px;
  top: 5px;
  max-width: calc(100% - 30px);
  overflow: hidden;
  color: #c7ced5;
  font-size: 8px;
  font-weight: 700;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage-remove-deck {
  position: absolute;
  right: 5px;
  top: 3px;
  z-index: 3;
  width: 21px;
  height: 21px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.stage-remove-deck:hover { background: rgba(255,98,93,.12); color: #ff8b87; }

.stage-rotate-deck {
  position: absolute;
  right: 4px;
  bottom: 3px;
  z-index: 3;
  width: 21px;
  height: 21px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: rgba(17,22,27,.72);
  color: #b7c1cb;
  cursor: pointer;
  font-size: 12px;
}
.stage-rotate-deck:hover { color: white; background: #29323b; }

.stage-leg-toggle {
  position: relative;
  z-index: 3;
  display: grid;
  grid-template-columns: 1fr 1fr;
  padding: 2px;
  border: 1px solid #4a5561;
  border-radius: 6px;
  background: #11161b;
}

.stage-leg-toggle button {
  min-width: 29px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 11px;
  font-weight: 800;
}

.stage-leg-toggle button.active {
  background: #b48216;
  color: white;
}

.stage-support-layer {
  position: absolute;
  left: 22px;
  top: 22px;
  z-index: 4;
  pointer-events: none;
}

.stage-support-point {
  position: absolute;
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  transform: translate(-50%, -50%);
  border: 2px solid #ff3c3c;
  border-radius: 2px;
  background: rgba(20, 11, 12, .94);
  color: #ff9c9c;
  box-shadow: 0 0 0 1px rgba(0,0,0,.7);
  font-size: 8px;
  font-weight: 800;
}

.stage-grid-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 18px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 9px;
}

.stage-grid-legend span { display: inline-flex; align-items: center; gap: 6px; }
.stage-support-swatch { width: 12px; height: 12px; border: 2px solid #ff3c3c; border-radius: 2px; }

.stage-material-summary {
  padding: 18px;
  background: #0e1317;
}

.stage-material-summary h3 { margin: 0 0 14px; font-size: 13px; }

.stage-material-primary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
}

.stage-material-primary > div {
  min-width: 0;
  padding: 12px 6px 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #151a1f;
  text-align: center;
}

.stage-material-primary strong { display: block; color: white; font-size: 22px; line-height: 1; }
.stage-material-primary span { display: block; margin-top: 6px; color: var(--muted); font-size: 9px; text-transform: uppercase; }
.stage-material-primary .wood-total { border-color: #805052; background: #211719; }
.stage-material-primary .wood-total strong { color: #ff716d; }

.stage-material-breakdown { margin: 16px 0 0; }
.stage-material-breakdown > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 34px;
  border-bottom: 1px solid var(--border-soft);
}
.stage-material-breakdown dt { color: var(--muted); font-size: 10px; }
.stage-material-breakdown dd { margin: 0; color: #e5ebf0; font-size: 11px; font-weight: 800; }
.stage-material-breakdown .saved dd { color: #61c954; }

.stage-calculation-note {
  margin-top: 16px;
  padding: 11px;
  border: 1px solid #303a45;
  border-radius: 7px;
  background: #141a20;
  color: #9ca8b4;
  font-size: 9px;
  line-height: 1.5;
}

@media (max-width: 1050px) {
  :root { --panel-width: 240px; --right-panel-width: 260px; }
  .top-actions .subtle:nth-of-type(2),
  .top-actions .subtle:nth-of-type(4) { display: none; }
}

@media (max-width: 1180px) {
  .topbar { gap: 8px; padding-inline: 9px; }
  .project-name { max-width: 190px; }
  .save-state { display: none; }
  .app-page-link { padding-inline: 8px; }
}

@media (max-width: 760px) {
  .stage-calculator-body {
    grid-template-columns: 1fr;
    max-height: calc(100vh - 116px);
    overflow: auto;
  }
  .stage-layout-editor { border-right: 0; border-bottom: 1px solid var(--border-soft); }
  .stage-material-summary { min-height: 280px; }
  .stage-layout-controls { flex-wrap: wrap; }
  body.page-stage-calculator .stage-calculator-modal { padding: 14px; }
  body.page-stage-calculator .stage-calculator { width: calc(100vw - 28px); }
  .brand { min-width: 24px; }
  .brand strong, .project-title-wrap { display: none; }
  .top-actions .button:not(#saveBtn) { display: none; }
  .top-actions #saveBtn { display: inline-flex; }
  .topbar { grid-template-columns: auto auto auto; overflow-x: auto; }
}

@media (max-width: 820px) {
  :root { --panel-width: 205px; --right-panel-width: 230px; }
  .workspace { grid-template-columns: var(--panel-width) minmax(0, 1fr); }
  .right-panel { display: none; }
  .save-state { display: none; }
}

@media print {
  .topbar, .left-panel, .right-panel, .toolbar, .statusbar, .canvas-hint { display: none !important; }
  .workspace { display: block; }
  .editor-column { display: block; }
  .viewport { overflow: visible; background: white; }
  .world { transform: none !important; background: white !important; }
}
