:root {
  --bg: #12151b;
  --panel: #1a1f27;
  --panel-2: #222833;
  --line: #2d3542;
  --text: #d7dde6;
  --muted: #8692a3;
  --accent: #3d8bfd;
  --accent-2: #2b6fd6;
  --hole: #ff6b6b;
  --intersect: #3ccf7e;
  --warn: #ffb020;
  --radius: 6px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--font);
  overflow: hidden;
}

button, select, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: #46526a; background: #283040; }
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--accent-2); border-color: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent); }
button.toggle.on { background: #1f3a63; border-color: var(--accent); color: #fff; }
button.danger:hover { border-color: var(--hole); color: var(--hole); }
select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 6px;
}

/* ---------------------------------------------------------------- toolbar */
.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { font-size: 16px; letter-spacing: 0.02em; color: var(--muted); }
.brand b { color: var(--text); }
.proj-name {
  width: 120px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 4px 6px;
}
.proj-name:hover, .proj-name:focus { border-color: var(--line); background: var(--bg); outline: none; }
.group { display: flex; gap: 4px; align-items: center; }
.toolbar button { padding: 5px 8px; }
#addButtons { gap: 3px; }
#addButtons button { padding: 5px 7px; }
.toolbar button.icon { font-size: 16px; line-height: 1; padding: 4px 9px 6px; }
.toolbar .export { margin-left: auto; }

/* ---------------------------------------------------------------- layout */
main {
  display: grid;
  grid-template-columns: 230px 1fr 290px;
  min-height: 0;
}
.panel {
  background: var(--panel);
  overflow-y: auto;
  min-height: 0;
}
#treePanel { border-right: 1px solid var(--line); padding: 10px; }
#propsPanel { border-left: 1px solid var(--line); }
h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin: 2px 0 8px;
  font-weight: 600;
}
h2 small { text-transform: none; letter-spacing: 0; font-weight: 400; margin-left: 4px; }
.hint { color: var(--muted); font-size: 12px; margin: 10px 2px; }

/* ---------------------------------------------------------------- tree */
#tree { list-style: none; margin: 0; padding: 0; }
#tree li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 6px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
}
#tree li:hover { background: var(--panel-2); }
#tree li.sel { background: #1f3a63; border-color: var(--accent); }
#tree li.hidden .name { opacity: 0.45; text-decoration: line-through; }
#tree .ico {
  width: 18px; height: 18px; flex: none;
  display: grid; place-items: center;
  border-radius: 4px;
  font-size: 11px;
  background: #3a4558;
}
#tree .ico.hole { background: transparent; border: 1.5px dashed var(--hole); color: var(--hole); }
#tree .ico.intersect { background: transparent; border: 1.5px solid var(--intersect); color: var(--intersect); }
#tree .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tree .err { color: var(--warn); font-weight: bold; }
#tree .acts { display: flex; gap: 2px; opacity: 0; }
#tree li:hover .acts, #tree li.sel .acts { opacity: 1; }
#tree .acts button { padding: 0 5px; font-size: 11px; line-height: 18px; background: transparent; border-color: transparent; }
#tree .acts button:hover { border-color: var(--line); }
#tree .empty { color: var(--muted); cursor: default; }

/* ---------------------------------------------------------------- props */
#props .section { padding: 10px 12px; border-bottom: 1px solid var(--line); }
#props .placeholder { padding: 16px 12px; color: var(--muted); }
#props .placeholder kbd {
  font: 11px var(--mono);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 0 4px;
}
#props .placeholder ul { padding-left: 16px; }
.row { display: grid; grid-template-columns: 1fr 120px; align-items: center; gap: 8px; margin: 5px 0; }
.row > label { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row3 { display: grid; grid-template-columns: 22px repeat(3, 1fr); gap: 4px; align-items: center; margin: 5px 0; }
.row3 > label { color: var(--muted); }
.row3 .axis { text-align: center; font-size: 11px; color: var(--muted); }
.unit-wrap { position: relative; }
.unit-wrap .unit {
  position: absolute; right: 7px; top: 50%; transform: translateY(-50%);
  color: var(--muted); font-size: 11px; pointer-events: none;
}
input.num, input.txt {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px 26px 4px 7px;
  font-family: var(--mono);
  font-size: 12px;
}
input.txt { font-family: var(--font); padding-right: 7px; font-size: 13px; }
input.num:focus, input.txt:focus { outline: none; border-color: var(--accent); }
input.num.bad { border-color: var(--hole); background: #2a1719; }
input[type=checkbox] { accent-color: var(--accent); width: 16px; height: 16px; justify-self: start; }
.seg { display: flex; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.seg button { flex: 1; border: 0; border-radius: 0; background: transparent; padding: 5px 4px; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent-2); color: #fff; }
.seg button.on.hole { background: #8a2d2d; }
.seg button.on.intersect { background: #1d7048; }
.origin-hint { color: var(--muted); font-size: 11.5px; margin: 2px 0 6px; }
.readout { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.error-box {
  margin: 8px 12px 0;
  padding: 7px 9px;
  border: 1px solid #6a4a12;
  background: #2b2312;
  color: var(--warn);
  border-radius: var(--radius);
}
.pts { width: 100%; border-collapse: collapse; margin-top: 4px; }
.pts td { padding: 2px; }
.pts td:first-child { color: var(--muted); width: 22px; font-size: 11px; }
.pts td:last-child { width: 24px; }
.pts input.num { padding-right: 7px; }
.pts button { padding: 1px 6px; }
.btn-row { display: flex; gap: 6px; margin-top: 6px; }
.btn-row button { flex: 1; }

/* ---------------------------------------------------------------- viewport */
#viewport { position: relative; min-width: 0; min-height: 0; overflow: hidden; }
#viewport canvas { display: block; touch-action: none; }
.view-bar {
  position: absolute; top: 8px; left: 8px; right: 8px;
  display: flex; gap: 10px; flex-wrap: wrap; z-index: 2;
  pointer-events: none;
}
.view-bar > * { pointer-events: auto; }
.view-bar button { background: rgba(34, 40, 51, 0.88); backdrop-filter: blur(4px); }
.view-bar .snap {
  display: flex; align-items: center; gap: 5px;
  color: var(--muted);
  background: rgba(34, 40, 51, 0.88);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1px 2px 1px 8px;
}
.view-bar .snap select { border: 0; background: transparent; }
.measure-label {
  position: absolute;
  display: none;
  transform: translate(-50%, -130%);
  background: rgba(20, 22, 28, 0.92);
  border: 1px solid var(--warn);
  color: var(--text);
  padding: 4px 8px;
  border-radius: var(--radius);
  pointer-events: none;
  font-family: var(--mono);
  font-size: 12px;
  text-align: center;
  white-space: nowrap;
}
.measure-label b { color: var(--warn); display: block; font-size: 13px; }
.measure-label span { color: var(--muted); font-size: 11px; }

#loading {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  background: rgba(18, 21, 27, 0.85);
  z-index: 5;
  color: var(--muted);
}
#loading.gone { display: none; }
.spinner {
  width: 22px; height: 22px;
  border: 3px solid var(--line);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------- status */
#status {
  display: flex; gap: 18px;
  padding: 4px 12px;
  background: var(--panel);
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-family: var(--mono);
  font-size: 11.5px;
  white-space: nowrap;
  overflow: hidden;
}
#stMsg { margin-left: auto; color: var(--text); }
#stMsg.err { color: var(--warn); }
#stKernel.busy::before { content: "● "; color: var(--warn); }
#stKernel.ok::before { content: "● "; color: var(--intersect); }

@media (max-width: 900px) {
  main { grid-template-columns: 1fr; grid-template-rows: 1fr auto auto; }
  #treePanel, #propsPanel { border: 0; border-top: 1px solid var(--line); max-height: 32vh; }
  #viewport { order: -1; min-height: 50vh; }
}

/* ---------------------------------------------------------------- tools */
.place-label {
  position: absolute;
  display: none;
  background: rgba(20, 22, 28, 0.92);
  border: 1px solid var(--accent);
  color: var(--text);
  padding: 3px 8px;
  border-radius: var(--radius);
  pointer-events: none;
  font-family: var(--mono);
  font-size: 12px;
  white-space: nowrap;
  z-index: 3;
}
.place-label b { display: block; color: #8fc1ff; font-size: 14px; }
.place-label span { color: var(--muted); font-size: 11px; }
.tool-hint {
  position: absolute;
  display: none;
  left: 50%;
  bottom: 14px;
  transform: translateX(-50%);
  background: rgba(31, 58, 99, 0.95);
  border: 1px solid var(--accent);
  color: #fff;
  padding: 6px 12px;
  border-radius: var(--radius);
  pointer-events: none;
  z-index: 3;
  white-space: nowrap;
}
.edge-count { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px; }
.edge-count b { font-family: var(--mono); }
.preset-note { color: var(--muted); font-size: 11.5px; margin-top: 6px; }
#tree .ico.op { background: transparent; border: 1.5px solid #8fc1ff; color: #8fc1ff; }
#addButtons button.on { background: #1f3a63; border-color: var(--accent); color: #fff; }
.add-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; margin-right: 3px; }
