:root {
  --bg: #f4f3f1;
  --surface: #ffffff;
  --surface-2: #f8f7f5;
  --border: #dedbd6;
  --text: #1f1e1c;
  --muted: #6d6a65;
  --accent: #d9480f;
  --accent-text: #ffffff;
  --accent-soft: rgba(217, 72, 15, 0.12);
  --danger: #c92a2a;
  --ok: #2b8a3e;
  --warn: #b7791f;
  /* canvas */
  --stage: #e7e5e1;
  --material: #ffffff;
  --grid: rgba(0, 0, 0, 0.06);
  --grid-major: rgba(0, 0, 0, 0.13);
  --ink: #1f1e1c;
  --ink-fill: rgba(31, 30, 28, 0.10);
  --sel: #d9480f;
  --travel: rgba(25, 113, 194, 0.7);
  --ruler: #f8f7f5;
  --radius: 6px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #181817; --surface: #222220; --surface-2: #2a2a28; --border: #3a3936;
    --text: #ecebe8; --muted: #a19e98; --accent: #ff7a3d; --accent-text: #1a1a1a; --accent-soft: rgba(255, 122, 61, 0.16);
    --danger: #ff6b6b; --ok: #69db7c; --warn: #f0b847;
    --stage: #121211; --material: #262624; --grid: rgba(255, 255, 255, 0.05); --grid-major: rgba(255, 255, 255, 0.11);
    --ink: #ecebe8; --ink-fill: rgba(236, 235, 232, 0.10); --sel: #ff7a3d; --travel: rgba(116, 192, 252, 0.75); --ruler: #1e1e1d;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #181817; --surface: #222220; --surface-2: #2a2a28; --border: #3a3936;
  --text: #ecebe8; --muted: #a19e98; --accent: #ff7a3d; --accent-text: #1a1a1a; --accent-soft: rgba(255, 122, 61, 0.16);
  --danger: #ff6b6b; --ok: #69db7c; --warn: #f0b847;
  --stage: #121211; --material: #262624; --grid: rgba(255, 255, 255, 0.05); --grid-major: rgba(255, 255, 255, 0.11);
  --ink: #ecebe8; --ink-fill: rgba(236, 235, 232, 0.10); --sel: #ff7a3d; --travel: rgba(116, 192, 252, 0.75); --ruler: #1e1e1d;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 11.5px; }
.spacer { flex: 1; }

button {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 5px 10px; cursor: pointer; white-space: nowrap;
  display: inline-flex; align-items: center; gap: 6px; justify-content: center;
}
button:hover { background: var(--surface-2); border-color: color-mix(in srgb, var(--border) 60%, var(--text)); }
button:active { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: default; transform: none; }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; padding: 6px 16px; }
button.primary:hover { filter: brightness(1.07); background: var(--accent); }
button.danger { color: var(--danger); }
.icon-btn { padding: 5px 7px; }
.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 5px 7px; width: 100%; min-width: 0;
}
input[type=checkbox] { width: auto; accent-color: var(--accent); margin: 0; }
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
textarea { resize: vertical; font-size: 15px; }
select[size] { padding: 2px; }

/* ---------- top bar */
.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex: none; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; color: var(--accent); }
.brand span { color: var(--text); }
.group { display: flex; gap: 4px; }
.switch { display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
.chip {
  padding: 4px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface-2);
  font-size: 12px; display: flex; align-items: center; gap: 6px; max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.chip::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.chip.ok::before { background: var(--ok); }
.chip.warn::before { background: var(--warn); }
.chip.bad::before { background: var(--danger); }

/* ---------- layout */
.main { flex: 1; display: flex; min-height: 0; }
.toolbar {
  width: 48px; flex: none; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 8px 0; overflow-y: auto;
}
.toolbar button { width: 36px; height: 36px; padding: 0; border-color: transparent; background: transparent; }
.toolbar button:hover { background: var(--surface-2); border-color: var(--border); }
.toolbar button.active { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.toolbar hr { width: 28px; border: 0; border-top: 1px solid var(--border); margin: 4px 0; }

.stage { flex: 1; position: relative; min-width: 0; background: var(--stage); overflow: hidden; }
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.stage.drop::after { content: "Drop to import"; position: absolute; inset: 12px; border: 2px dashed var(--accent); border-radius: 12px; display: grid; place-items: center; font-size: 18px; color: var(--accent); background: var(--accent-soft); pointer-events: none; }
.empty-hint {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); text-align: center; pointer-events: none;
  display: flex; flex-direction: column; gap: 4px; color: var(--muted);
}
.empty-hint strong { color: var(--text); font-size: 15px; font-weight: 600; }
.banner a { color: var(--accent); font-weight: 600; }
.banner {
  position: absolute; left: 50%; top: 32px; transform: translateX(-50%); max-width: min(560px, 90%);
  background: var(--surface); border: 1px solid var(--warn); border-left-width: 4px; border-radius: var(--radius);
  padding: 8px 12px; box-shadow: 0 4px 16px rgba(0,0,0,.12); font-size: 12.5px;
}

.panel {
  width: 300px; flex: none; background: var(--surface); border-left: 1px solid var(--border); overflow-y: auto;
}
.panel details { border-bottom: 1px solid var(--border); }
.panel summary {
  padding: 10px 14px; font-weight: 600; cursor: pointer; user-select: none; list-style: none; display: flex; align-items: center; gap: 6px;
}
.panel summary::-webkit-details-marker { display: none; }
.panel summary::before { content: "›"; display: inline-block; width: 10px; transition: transform .15s; color: var(--muted); font-size: 15px; }
.panel details[open] > summary::before { transform: rotate(90deg); }
.sec-body { padding: 0 14px 14px; display: flex; flex-direction: column; gap: 9px; }
.sec-body label, .dialog-body label { display: block; font-size: 12px; color: var(--muted); }
.sec-body label > input, .sec-body label > select, .sec-body label > textarea, .sec-body label > .row, .sec-body label > button,
.dialog-body label > input, .dialog-body label > select { display: block; margin-top: 3px; }
.sec-body label > .row { display: flex; }
.grid2 { align-items: end; }
.sec-body label.check { display: flex; flex-direction: row; align-items: center; gap: 7px; color: var(--text); font-size: 12.5px; cursor: pointer; }
.unit { font-size: 11px; opacity: .8; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.row { display: flex; gap: 6px; align-items: flex-end; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: 8px 10px 10px; margin: 0; display: flex; flex-direction: column; gap: 8px; }
legend { font-size: 12px; font-weight: 600; padding: 0 4px; }
.est { margin: 0; padding: 8px 10px; background: var(--surface-2); border-radius: var(--radius); font-variant-numeric: tabular-nums; }
.est:empty { display: none; }
p { margin: 0; }

.jog { display: flex; gap: 12px; align-items: center; }
.pad { display: grid; grid-template-columns: repeat(3, 34px); grid-template-rows: repeat(3, 34px); gap: 3px; }
.pad button { padding: 0; }
.pad button:nth-child(1) { grid-area: 1 / 2; }
.pad button:nth-child(2) { grid-area: 2 / 1; }
.pad button:nth-child(3) { grid-area: 2 / 2; }
.pad button:nth-child(4) { grid-area: 2 / 3; }
.pad button:nth-child(5) { grid-area: 3 / 2; }
.jog-side { flex: 1; display: flex; flex-direction: column; gap: 6px; }

/* ---------- status bar */
.status {
  display: flex; gap: 16px; padding: 4px 12px; font-size: 12px; background: var(--surface);
  border-top: 1px solid var(--border); flex: none; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden;
}

/* ---------- modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.35); display: grid; place-items: center; z-index: 50; padding: 16px; }
.dialog {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; width: min(560px, 100%);
  max-height: calc(100vh - 32px); display: flex; flex-direction: column; box-shadow: 0 16px 48px rgba(0,0,0,.25);
}
.dialog.wide { width: min(860px, 100%); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin: 0; font-size: 15px; }
.dialog-body { padding: 14px 16px; overflow-y: auto; display: flex; flex-direction: column; gap: 10px; }
.dialog-body label.check { display: flex; flex-direction: row; align-items: center; gap: 7px; color: var(--text); font-size: 13px; }
.dialog-foot { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--border); }
.dialog-foot:empty { display: none; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 16px; font-variant-numeric: tabular-nums; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.warnlist { margin: 0; padding: 8px 10px 8px 26px; border-radius: var(--radius); background: color-mix(in srgb, var(--warn) 12%, transparent); border: 1px solid color-mix(in srgb, var(--warn) 45%, transparent); }
.progress { height: 8px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.trace-wrap { display: grid; grid-template-columns: 1fr 240px; gap: 14px; }
.trace-wrap canvas { width: 100%; height: auto; max-height: 60vh; object-fit: contain; background: repeating-conic-gradient(var(--surface-2) 0 25%, var(--surface) 0 50%) 0 0 / 16px 16px; border: 1px solid var(--border); border-radius: var(--radius); }
.trace-side { display: flex; flex-direction: column; gap: 10px; }
.hpgl-view { width: 100%; height: 50vh; font: 12px/1.4 ui-monospace, Consolas, monospace; }

.toasts { position: fixed; right: 16px; bottom: 40px; display: flex; flex-direction: column; gap: 8px; z-index: 60; max-width: min(420px, calc(100vw - 32px)); }
.toast { background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent); border-radius: var(--radius); padding: 8px 12px; box-shadow: 0 6px 20px rgba(0,0,0,.15); animation: tin .15s ease-out; }
.toast.err { border-left-color: var(--danger); }
.toast.ok { border-left-color: var(--ok); }
@keyframes tin { from { opacity: 0; transform: translateY(6px); } }

@media (max-width: 900px) {
  .panel { width: 250px; }
  .topbar .group:nth-of-type(2) { display: none; }
}
@media (max-width: 640px) {
  .main { flex-direction: column; }
  .toolbar { width: auto; flex-direction: row; height: 48px; border-right: 0; border-bottom: 1px solid var(--border); padding: 0 8px; overflow-x: auto; }
  .toolbar hr { width: 0; height: 24px; border-top: 0; border-left: 1px solid var(--border); }
  .panel { width: auto; max-height: 40vh; border-left: 0; border-top: 1px solid var(--border); }
  .trace-wrap { grid-template-columns: 1fr; }
}
