:root {
  --bg: #f3f5f1;
  --ink: #202522;
  --muted: #68716b;
  --panel: #ffffff;
  --line: #d7ddd7;
  --accent: #0f766e;
  --accent-2: #c2410c;
  --steel: #35566b;
  --cab: #edf2f7;
  --part: #f8fafc;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: grid;
  grid-template-columns: 300px 1fr;
  color: var(--ink);
  background: var(--bg);
  font-family: Inter, Segoe UI, Arial, sans-serif;
}

button,
input {
  font: inherit;
}

.sidebar {
  min-height: 100vh;
  padding: 18px;
  background: #ffffff;
  border-right: 1px solid var(--line);
  overflow: auto;
  overscroll-behavior: contain;
}

.brand {
  margin-bottom: 18px;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  letter-spacing: 0;
}

.brand p {
  margin: 5px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.file-picker {
  display: grid;
  gap: 7px;
  margin-bottom: 14px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
}

.file-picker input {
  width: 100%;
  color: var(--ink);
  text-transform: none;
  font-weight: 500;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  margin-bottom: 16px;
  background: #eef1ee;
  border: 1px solid var(--line);
}

.tab,
.tools button {
  min-height: 34px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.tab.active,
.tools button.active {
  background: var(--panel);
  border-color: var(--line);
  color: var(--accent);
  font-weight: 700;
}

.panel {
  margin-top: 16px;
}

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

.panel-actions {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.panel-title {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: .04em;
  text-transform: uppercase;
}

.panel-action {
  flex-shrink: 0;
  min-height: 26px;
  padding: 0 8px;
  border: 1px solid var(--line);
  background: #fbfcfb;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
}

.panel-action:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.panel-action.active {
  border-color: var(--accent);
  color: var(--accent);
  background: #ecfeff;
}

.list {
  display: grid;
  gap: 6px;
}

.list button {
  width: 100%;
  min-height: 38px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  background: #fbfcfb;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.layer-list {
  max-height: min(72vh, calc(100vh - 220px));
  overflow-y: auto;
  overscroll-behavior: contain;
}

.layer-row {
  display: block;
}

.layer-row-suppressed .layer-item {
  border-style: dashed;
  background: #f8faf8;
}

.layer-list .layer-item {
  position: relative;
  display: grid;
  grid-template-columns: 14px 1fr;
  align-items: center;
  gap: 9px;
  width: 100%;
  min-height: 38px;
  padding: 8px 10px 8px 10px;
  border: 1px solid var(--line);
  background: #fbfcfb;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.layer-hide {
  position: absolute;
  top: 6px;
  right: 8px;
  z-index: 1;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  margin: 0;
  padding: 2px 4px;
  border-radius: 4px;
  background: rgba(255, 255, 255, .92);
  color: var(--muted);
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
}

.layer-hide:hover {
  color: var(--accent);
}

.layer-hide-input {
  width: 12px;
  height: 12px;
  margin: 0;
  cursor: pointer;
}

.layer-hide-label {
  user-select: none;
}

.layer-copy {
  min-width: 0;
  padding-right: 52px;
}

.list button.active,
.layer-list .layer-item.active {
  border-color: var(--accent);
  box-shadow: inset 3px 0 0 var(--accent);
}

.list button:not(.active),
.layer-list .layer-item:not(.active) {
  opacity: .45;
}

.list button.selected,
.layer-list .layer-item.selected {
  background: #ecfeff;
}

.swatch {
  width: 12px;
  height: 24px;
  border: 1px solid rgba(0, 0, 0, .18);
}

.item-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
  font-size: 13px;
}

.item-subtitle {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.workspace {
  min-width: 0;
  min-height: 100vh;
  display: grid;
  grid-template-rows: 58px 1fr;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  background: #fbfcfb;
  border-bottom: 1px solid var(--line);
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.elevation-nav {
  display: flex;
  align-items: center;
  gap: 4px;
}

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

.toolbar-actions button {
  min-width: 54px;
  min-height: 34px;
  padding: 0 10px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
}

.toolbar-actions button.active {
  background: var(--panel);
  border-color: var(--line);
  color: var(--accent);
  font-weight: 700;
}

.toolbar-help {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 12px;
  margin-left: 4px;
  padding-left: 12px;
  border-left: 1px solid var(--line);
}

.help-link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 13px;
  font-weight: 600;
  text-align: left;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

.help-link:hover {
  color: #0d5c56;
}

.help-modal {
  width: min(520px, calc(100vw - 32px));
  max-height: min(85vh, 640px);
  margin: auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 18px 48px rgba(32, 37, 34, .22);
}

.help-modal::backdrop {
  background: rgba(32, 37, 34, .45);
}

.help-modal-inner {
  position: relative;
  padding: 22px 22px 20px;
}

.help-modal-close {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfb;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
}

.help-modal-close:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.help-modal-title {
  margin: 0 36px 12px 0;
  font-size: 18px;
  line-height: 1.3;
}

.help-modal-body {
  font-size: 14px;
  line-height: 1.55;
  color: var(--ink);
}

.help-modal-body p {
  margin: 0 0 12px;
}

.help-modal-body p:last-child {
  margin-bottom: 0;
}

.help-modal-body strong {
  color: var(--accent);
}

.icon-btn {
  min-width: 36px;
  min-height: 34px;
  padding: 0 8px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}

.icon-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.tools {
  display: flex;
  gap: 6px;
}

.tools button {
  min-width: 54px;
  padding: 0 10px;
  border-color: var(--line);
  background: var(--panel);
}

.canvas-wrap {
  position: relative;
  min-height: 0;
  overflow: hidden;
  touch-action: none;
  background:
    linear-gradient(#e4e9e4 1px, transparent 1px),
    linear-gradient(90deg, #e4e9e4 1px, transparent 1px);
  background-size: 32px 32px;
}

#drawing {
  position: relative;
  z-index: 1;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  user-select: none;
}

.gl-canvas {
  position: absolute;
  inset: 0;
  z-index: 0;
  width: 100%;
  height: 100%;
  display: none;
  pointer-events: none;
}

#drawing.dragging {
  cursor: grabbing;
}

#drawing.dragging.orbit-pan {
  cursor: move;
}

#drawing.measure-mode {
  cursor: crosshair;
}

#drawing.measure-mode.dragging {
  cursor: crosshair;
}

.measure-overlay {
  pointer-events: none;
}

.measure-point-mark {
  pointer-events: none;
}

.measure-point-circle {
  fill: none;
  stroke: #dc2626;
  stroke-width: 0.55px;
  vector-effect: non-scaling-stroke;
}

.measure-point-cross {
  fill: none;
  stroke: #dc2626;
  stroke-width: 0.48px;
  stroke-linecap: round;
  vector-effect: non-scaling-stroke;
}

.measure-line {
  stroke: #c2410c;
  stroke-width: 0.85;
  vector-effect: non-scaling-stroke;
  stroke-dasharray: 5 3;
}

.measure-label {
  fill: #0f172a;
  font-size: 2.1px;
  font-weight: 700;
  paint-order: stroke;
  stroke: #ffffff;
  stroke-width: 0.32px;
  stroke-linejoin: round;
}

.wall-line {
  stroke: var(--steel);
  stroke-width: 16;
  stroke-linecap: square;
}

.wall-thin {
  stroke: #718096;
  stroke-width: 3;
  stroke-dasharray: 14 8;
}

.cabinet-footprint,
.cabinet-elevation {
  fill: var(--cab);
  stroke: #334155;
  stroke-width: 2;
}

.cabinet-footprint.selected,
.cabinet-elevation.selected {
  fill: #ccfbf1;
  stroke: var(--accent);
  stroke-width: 4;
}

.part {
  fill: var(--part);
  stroke: #64748b;
  stroke-width: 1.5;
}

.face {
  fill: #fff7ed;
  stroke: var(--accent-2);
  stroke-width: 1.5;
}

.shape-line {
  fill: none;
  stroke: #7c2d12;
  stroke-width: 1.5;
}

.label {
  fill: #1f2937;
  font-size: 28px;
  paint-order: stroke;
  stroke: #ffffff;
  stroke-width: 5px;
  stroke-linejoin: round;
}

.dxf-line {
  fill: none;
  stroke-width: .55;
  vector-effect: non-scaling-stroke;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: .86;
}

.dxf-line.selected-layer {
  stroke-width: 1.1;
  opacity: 1;
}

.dxf-text-group {
  pointer-events: none;
}

.dxf-text {
  fill: #0f172a;
  font-size: 2.8px;
  font-weight: 700;
  paint-order: stroke;
  stroke: #ffffff;
  stroke-width: 0.45px;
  stroke-linejoin: round;
}

.joint-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  font-size: 10px;
  font-weight: 800;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  vertical-align: middle;
  background: #e0f2fe;
  color: #0369a1;
  border-radius: 4px;
}

.axis-line {
  stroke: #111827;
  stroke-width: 1.4;
  vector-effect: non-scaling-stroke;
}

.axis-label {
  fill: #111827;
  font-size: 2.4px;
  paint-order: stroke;
  stroke: #ffffff;
  stroke-width: .35px;
  stroke-linejoin: round;
}

.empty-state {
  fill: #475569;
  font-size: 30px;
}

@media (max-width: 860px) {
  body {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    max-height: 44vh;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .layer-list {
    max-height: min(34vh, calc(44vh - 200px));
  }

  .workspace {
    min-height: 56vh;
  }
}
