/* ---- themes ----
   :root holds the default DARK theme. [data-theme="light"] and
   [data-theme="paper"] (set on <html> by the app) override the same vars. */
:root {
  --bg: #14171c;
  --bg2: #1b1f26;
  --bg3: #232833;
  --line: #2c333f;
  --text: #d7dce4;
  --muted: #8a93a3;
  --accent: #4f9eff;
  --accent2: #2f7fe0;
  --sel: #ff9d3b;
  --ok: #4fd07a;
  --warn: #e0b341;
  --bad: #ef5b6e;
  --dim: #e0b341;

  /* scrollbars */
  --scrollbar: #3a4351;
  --scrollbar-hover: #4a5566;

  /* 2D sketch canvas */
  --grid: #20262f;
  --axis-x: #4a3030;
  --axis-y: #2f4a32;
  --entity: #cfd6e0;
  --entity-curve: #b9c7ff;
  --entity-hi: #fff;
  --handle-fill: #11141a;
  --badge: #9fb2d8;
  --badge-geo: #74e0c2;

  /* 3D viewer (read by JS via getComputedStyle) */
  --bg3d: #1b1e24;
  --grid3d: #44505f;
  --grid3d-minor: #2a313b;

  color-scheme: dark; /* native scrollbars & form controls */
}

/* ---- light theme ---- */
:root[data-theme="light"] {
  --bg: #ffffff;
  --bg2: #f1f4f8;
  --bg3: #e3e8ef;
  --line: #d2d8e0;
  --text: #1f2733;
  --muted: #616b7a;
  --accent: #2f7fe0;
  --accent2: #2563c4;
  --sel: #e8730f;
  --ok: #1c9e57;
  --warn: #b8860b;
  --bad: #d23048;
  --dim: #b07d0a;

  --scrollbar: #c2c9d3;
  --scrollbar-hover: #aab2bf;

  --grid: #e9edf3;
  --axis-x: #e7b9b9;
  --axis-y: #bcdcc1;
  --entity: #2a3340;
  --entity-curve: #2f5bd0;
  --entity-hi: #000;
  --handle-fill: #ffffff;
  --badge: #54627e;
  --badge-geo: #1f9e7a;

  --bg3d: #eef1f5;
  --grid3d: #c2cad4;
  --grid3d-minor: #dde2e9;

  color-scheme: light;
}

/* ---- paper theme (warm sepia) ---- */
:root[data-theme="paper"] {
  --bg: #f5edda;
  --bg2: #ece2c9;
  --bg3: #e0d4b6;
  --line: #cdbf9c;
  --text: #43381f;
  --muted: #8a7a57;
  --accent: #1f6f6b;
  --accent2: #1a5d59;
  --sel: #c0561f;
  --ok: #4a7c2f;
  --warn: #a9791f;
  --bad: #b23a2e;
  --dim: #8a5a1f;

  --scrollbar: #cdbf9c;
  --scrollbar-hover: #b9a87f;

  --grid: #e4d8bb;
  --axis-x: #d9b8a0;
  --axis-y: #b8c79c;
  --entity: #3b2f17;
  --entity-curve: #2f4a7a;
  --entity-hi: #000;
  --handle-fill: #fbf5e6;
  --badge: #6b5a38;
  --badge-geo: #3f7a52;

  --bg3d: #ded2b4;
  --grid3d: #b9a87f;
  --grid3d-minor: #d0c4a4;

  color-scheme: light;
}

* { box-sizing: border-box; }

/* scrollbars matched to the active theme */
* { scrollbar-width: thin; scrollbar-color: var(--scrollbar) var(--bg2); }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--bg2); }
::-webkit-scrollbar-thumb { background: var(--scrollbar); border-radius: 6px; border: 2px solid var(--bg2); }
::-webkit-scrollbar-thumb:hover { background: var(--scrollbar-hover); }

html, body { margin: 0; height: 100%; }
body {
  font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text); overflow: hidden;
}

#app {
  display: grid;
  grid-template-columns: 1fr 330px;
  grid-template-rows: 46px 1fr 26px;
  height: 100vh;
}

/* ---- header ---- */
header {
  grid-column: 1 / 3;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 12px; background: var(--bg2); border-bottom: 1px solid var(--line);
}
header { gap: 14px; }
.brand { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; letter-spacing: .2px; flex: 0 0 auto; }
.brand-mark { display: block; width: 20px; height: 20px; border-radius: 5px; }
.header-spacer { flex: 1; }
.brand-tag { color: var(--muted); font-size: 11px; flex: 0 0 auto; }
.file-ops { display: flex; gap: 6px; }

/* ---- traditional menu bar ---- */
.menubar { display: flex; align-items: stretch; align-self: stretch; }
.menu { position: relative; display: flex; }
.menu-label { background: none; border: none; color: var(--text); padding: 0 12px; font-size: 13px; cursor: pointer; border-radius: 0; }
.menu-label:hover, .menu.open .menu-label { background: var(--bg3); }
.menu-drop { position: absolute; top: 100%; left: 0; min-width: 210px; background: var(--bg2); border: 1px solid var(--line); border-radius: 0 0 8px 8px; box-shadow: 0 10px 30px rgba(0,0,0,.5); padding: 4px; display: none; z-index: 1001; }
.menu.open .menu-drop { display: block; }
.menu-item { display: flex; justify-content: space-between; align-items: center; gap: 24px; padding: 6px 10px; border-radius: 5px; font-size: 12px; cursor: pointer; white-space: nowrap; }
.menu-item:hover { background: var(--accent2); color: #fff; }
.menu-sc { color: var(--muted); font-size: 11px; }
.menu-item:hover .menu-sc { color: #dbe7ff; }
.menu-sep { height: 1px; background: var(--line); margin: 4px 2px; }

/* ---- info dialog (Help) ---- */
.info-body { font-size: 13px; line-height: 1.55; max-width: 460px; }
.info-body h4 { margin: 12px 0 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.info-body ul { margin: 0; padding-left: 18px; }
.info-body code { background: var(--bg); padding: 1px 5px; border-radius: 4px; color: #b9c7ff; font-size: 12px; }
.info-body kbd { background: var(--bg3); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 1px 6px; font-family: monospace; font-size: 11px; }

button {
  background: var(--bg3); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 10px; cursor: pointer; font-size: 12px;
}
button:hover { background: #2c333f; border-color: #3a4351; }
button:active { transform: translateY(1px); }
button:disabled { opacity: .32; cursor: default; }
button:disabled:hover { background: var(--bg3); border-color: var(--line); }
button:disabled:active { transform: none; }
button.mini { padding: 1px 7px; font-size: 11px; margin-left: 6px; }

/* ---- left toolbar ---- */
.toolbar {
  grid-row: 2; grid-column: 1;
  display: flex; flex-direction: column; gap: 4px; padding: 6px 5px;
  background: var(--bg2); border-right: 1px solid var(--line);
}
.toolbar button {
  width: 36px; height: 36px; font-size: 17px; padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.toolbar button.active { background: var(--accent2); border-color: var(--accent); color: #fff; }
.tb-head { font-size: 9px; font-weight: 700; letter-spacing: .5px; color: var(--muted); text-align: center; padding: 1px 0 3px; border-bottom: 1px solid var(--line); margin-bottom: 3px; }

/* ---- in-pane toolboxes (2D tools live in the 2D pane, 3D tools in the 3D pane) ---- */
.pane-body { flex: 1; display: flex; min-height: 0; min-width: 0; }
.toolbox { flex: 0 0 auto; background: var(--bg2); border-right: 1px solid var(--line); overflow-y: auto; overflow-x: hidden; }
.toolbox-2d { display: grid; grid-template-columns: repeat(2, 28px); gap: 3px; padding: 5px 5px 8px; align-content: start; }
.toolbox-3d { display: flex; flex-direction: column; gap: 3px; padding: 5px; }
.toolbox button { width: 28px; height: 28px; padding: 0; font-size: 14px; line-height: 1; display: flex; align-items: center; justify-content: center; }
.toolbox button svg { width: 18px; height: 18px; display: block; }
.toolbox button.active { background: var(--accent2); border-color: var(--accent); color: #fff; }
.toolbox .tb-sep { grid-column: 1 / -1; height: 1px; background: var(--line); margin: 4px 1px; }
.toolbox-3d .tb-sep { width: 100%; }

/* ---- center ---- */
main { grid-row: 2; grid-column: 1; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.constraint-bar {
  display: flex; align-items: center; gap: 5px; flex-wrap: wrap;
  padding: 6px 8px; background: var(--bg2); border-bottom: 1px solid var(--line);
}
.constraint-bar .group-label { color: var(--muted); margin-right: 2px; }
.constraint-bar button { min-width: 30px; height: 28px; padding: 0 8px; }
.constraint-bar .divider { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.chk { color: var(--muted); display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.sel-readout { color: var(--muted); }
.sel-readout b { color: var(--accent); }

.split { flex: 1; display: flex; min-height: 0; }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
.pane-2d { flex: 1.25; }
.pane-3d { flex: 1; }

/* draggable divider (split view only) */
.splitter { flex: 0 0 5px; background: var(--line); cursor: col-resize; }
.splitter:hover { background: var(--accent); }

/* view modes (class on <main>) */
main.view-2d .pane-3d, main.view-2d .splitter { display: none; }
main.view-3d .pane-2d, main.view-3d .splitter { display: none; }

/* view + panel controls in the constraint bar */
.view-group { display: inline-flex; gap: 3px; margin-left: 10px; }
.view-group button { min-width: 28px; height: 24px; padding: 0 7px; display: inline-flex; align-items: center; justify-content: center; }
.view-group button.active { background: var(--accent2); border-color: var(--accent); color: #fff; }
.view-group button svg { display: block; width: 16px; height: 16px; }

/* right-panel toggle (class on #app) */
#app.panel-hidden { grid-template-columns: 1fr 0; }
#app.panel-hidden .panel { display: none; }
.pane-title {
  font-size: 11px; color: var(--muted); padding: 3px 8px; background: var(--bg2);
  border-bottom: 1px solid var(--line); text-transform: uppercase; letter-spacing: .5px;
  display: flex; align-items: center; justify-content: space-between;
}
#sketch { flex: 1; min-width: 0; min-height: 0; background: var(--bg); display: block; cursor: default; touch-action: none; user-select: none; -webkit-user-select: none; }
#viewer { flex: 1; min-width: 0; min-height: 0; }
#viewer canvas { display: block; }

/* ---- SVG sketch styling ---- */
.grid-line { stroke: var(--grid); stroke-width: 1; }
.axis-x { stroke: var(--axis-x); stroke-width: 1.2; }
.axis-y { stroke: var(--axis-y); stroke-width: 1.2; }
.entity { fill: none; stroke: var(--entity); stroke-width: 1.7; }
.entity.line { stroke-linecap: round; }
.entity.circle, .entity.arc { stroke: var(--entity-curve); }
.entity.sel { stroke: var(--sel); stroke-width: 2.4; }
.entity:hover { stroke: var(--entity-hi); }
.handle { fill: var(--handle-fill); stroke: var(--entity); stroke-width: 1.4; }
.handle.sel { fill: var(--sel); stroke: var(--entity-hi); }
.handle.fixed { fill: var(--ok); stroke: var(--handle-fill); }
.snap { fill: none; stroke: var(--accent); stroke-width: 1.5; }
.snap.on-point { stroke: var(--ok); stroke-width: 2; }
.preview { fill: rgba(79,158,255,0.08); stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 5 4; }
.preview.thin { fill: none; stroke-width: 1.2; }
.badge { fill: var(--badge); font-size: 11px; font-family: monospace; user-select: none; }
.badge.geo { fill: var(--badge-geo); }
.dim-ext { stroke: #6a5a30; stroke-width: 1; }
.dim-line { stroke: var(--dim); stroke-width: 1; }
.dim-label { fill: var(--dim); font-size: 12px; font-family: monospace; }
.dim-label:hover { fill: #fff; }

/* ---- right panel ---- */
.panel { grid-row: 2; grid-column: 2; background: var(--bg2); border-left: 1px solid var(--line); overflow-y: auto; display: flex; flex-direction: column; }
.panel section { padding: 9px 11px; border-bottom: 1px solid var(--line); }
.panel section.grow { flex: 1; }
.panel h3 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); display: flex; align-items: center; }
.panel input { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 5px; padding: 4px 6px; font-size: 12px; width: 100%; font-family: monospace; }
.panel input:focus { outline: none; border-color: var(--accent); }

.param-head, .param-row { display: grid; grid-template-columns: 1fr 1.2fr 56px 22px; gap: 5px; align-items: center; margin-bottom: 5px; }
.param-head { color: var(--muted); font-size: 11px; margin-bottom: 4px; }
.param-val { color: var(--ok); font-family: monospace; font-size: 11px; }
.param-val.bad { color: var(--bad); }
.icon-btn { padding: 2px 6px; line-height: 1; color: var(--muted); }
.icon-btn:hover { color: var(--bad); }

.cons-row { display: flex; align-items: center; gap: 6px; padding: 3px 0; border-bottom: 1px dashed #232833; font-size: 12px; }
.cons-type { color: var(--accent); font-weight: 600; }
.cons-refs { color: var(--muted); font-family: monospace; font-size: 11px; }
.cons-row input.expr { flex: 1; min-width: 40px; }
.cons-row .param-val { margin-left: auto; }
.cons-row .icon-btn { margin-left: auto; }
.cons-row input.expr ~ .icon-btn { margin-left: 0; }

.field { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; color: var(--muted); }
.field input { flex: 1; }
.row { display: flex; align-items: center; gap: 10px; }
#btn-export-stl { background: var(--accent2); border-color: var(--accent); color: #fff; }

.help ol { margin: 0; padding-left: 18px; color: var(--muted); }
.help li { margin-bottom: 5px; }
.help code { background: var(--bg); padding: 1px 4px; border-radius: 3px; color: #b9c7ff; }

/* ---- footer ---- */
footer {
  grid-column: 1 / 3; display: flex; align-items: center; gap: 18px;
  padding: 0 12px; background: var(--bg2); border-top: 1px solid var(--line);
  font-size: 11px; color: var(--muted);
}
footer > span { flex: 0 0 auto; }
/* fixed-width, tabular coords so the rest of the status bar doesn't jump as the cursor moves */
#cursor { display: inline-block; min-width: 106px; font-variant-numeric: tabular-nums; }
#solver-state.ok { color: var(--ok); }
#solver-state.warn { color: var(--warn); }
#solver-state.bad { color: var(--bad); }
#profile-note { color: var(--warn); }
#flash.ok { color: var(--ok); }
#flash.bad { color: var(--bad); }

/* ---- dialog ---- */
dialog { background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: 10px; padding: 16px; min-width: 320px; }
dialog::backdrop { background: rgba(0,0,0,0.5); }
dialog p { margin: 0 0 10px; }
dialog input { width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 7px 8px; font-family: monospace; }
dialog menu { display: flex; flex-direction: row-reverse; gap: 8px; padding: 0; margin: 14px 0 0; }
#dlg-ok { background: var(--accent2); border-color: var(--accent); color: #fff; }

/* ---- welcome modal ---- */
#welcome-dlg { min-width: 430px; max-width: 470px; }
.welcome-head { display: flex; align-items: center; gap: 12px; margin-bottom: 16px; }
.welcome-mark { display: block; border-radius: 9px; flex: 0 0 auto; }
.welcome-head .dlg-title { margin: 0 0 2px; font-size: 16px; }
.welcome-sub { margin: 0; color: var(--muted); font-size: 12px; line-height: 1.45; }
.welcome-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 14px; }
.welcome-card { display: flex; flex-direction: column; align-items: flex-start; gap: 1px; text-align: left;
  padding: 12px; border: 1px solid var(--line); border-radius: 9px; background: var(--bg3); }
.welcome-card:hover { border-color: var(--accent); background: var(--bg); }
.wc-icon { line-height: 0; color: var(--accent); margin-bottom: 7px; }
.wc-icon svg { width: 22px; height: 22px; display: block; }
.wc-label { font-size: 13px; font-weight: 600; color: var(--text); }
.wc-desc { font-size: 11px; color: var(--muted); }
.welcome-examples { border-top: 1px solid var(--line); padding-top: 12px; }
.we-head { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin-bottom: 7px; }
#welcome-examples-list { display: flex; flex-direction: column; gap: 5px; }
.welcome-ex { text-align: left; padding: 8px 10px; border: 1px solid var(--line); border-radius: 7px;
  background: var(--bg3); font-size: 12px; color: var(--text); }
.welcome-ex:hover { border-color: var(--accent); background: var(--bg); }
#welcome-dlg menu { justify-content: space-between; align-items: center; margin-top: 16px; }
.welcome-dontshow { font-size: 11px; }
.welcome-dontshow input { width: auto; }

/* ---- header divider ---- */
.file-ops .divider { width: 1px; height: 22px; background: var(--line); margin: 0 3px; }

/* ---- sketch tabs ---- */
.pane-2d .pane-title { padding: 0; }
.tabs { display: flex; align-items: stretch; gap: 0; flex-wrap: wrap; width: 100%; }
.tab {
  display: flex; align-items: center; gap: 5px; padding: 4px 9px; cursor: pointer;
  border-right: 1px solid var(--line); color: var(--muted); font-size: 12px;
  text-transform: none; letter-spacing: 0; border-bottom: 2px solid transparent;
}
.tab:hover { background: var(--bg3); }
.tab.active { color: var(--text); background: var(--bg); border-bottom-color: var(--accent); }
.tab-x { background: none; border: none; color: var(--muted); padding: 0 2px; font-size: 13px; }
.tab-x:hover { color: var(--bad); }
.tab-add { background: none; border: none; color: var(--muted); padding: 2px 10px; font-size: 15px; }
.tab-add:hover { color: var(--accent); }

/* ---- features ---- */
h3 .spacer { flex: 1; }
h3 .muted-note { font-weight: 400; text-transform: none; letter-spacing: 0; color: #5d6675; margin-left: 6px; }
.panel select { background: var(--bg); color: var(--text); border: 1px solid var(--line); border-radius: 5px; padding: 3px 5px; font-size: 12px; }
.panel select:focus { outline: none; border-color: var(--accent); }
.feat-row { border: 1px solid var(--line); border-radius: 7px; padding: 6px 7px; margin-bottom: 7px; background: var(--bg); }
.feat-head { display: flex; align-items: center; gap: 6px; margin-bottom: 5px; }
.feat-head input.feat-name { flex: 1; background: transparent; border: none; color: var(--text); font-weight: 600; padding: 2px; }
.feat-head input.feat-name:focus { outline: none; border-bottom: 1px solid var(--accent); }
.op-sel { color: var(--accent); }
.base-tag { font-size: 10px; color: var(--ok); border: 1px solid var(--ok); border-radius: 4px; padding: 0 5px; }
.feat-body { display: grid; grid-template-columns: 1fr 1fr; gap: 5px 8px; margin-bottom: 5px; }
.feat-field { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 11px; }
.feat-field span { min-width: 44px; }
.feat-field input, .feat-field select { flex: 1; min-width: 0; }
.feat-tools { display: flex; gap: 4px; justify-content: flex-end; }
.feat-tools .icon-btn { color: var(--muted); }
.feat-tools .icon-btn:hover { color: var(--accent); }
.export-row { margin-top: 4px; }
#btn-export-stl { background: var(--accent2); border-color: var(--accent); color: #fff; }
.constraint-bar #btn-fillet, .constraint-bar #btn-chamfer, .constraint-bar #btn-mirror,
.constraint-bar #btn-pattern, .constraint-bar #btn-construction { color: #ffd28a; }

/* ---- misc utilities ---- */
.hidden { display: none !important; }

/* ---- 2D plane select & 3D transform header ---- */
.pane-2d .pane-title { display: flex; align-items: stretch; }
.tabs { flex: 1; }
.plane-sel { align-self: center; margin: 0 6px; background: var(--bg3); color: var(--text); border: 1px solid var(--line); border-radius: 5px; font-size: 11px; padding: 2px 4px; }
.pane-3d .pane-title { display: flex; align-items: center; gap: 4px; }
.pane-3d .pane-title .spacer { flex: 1; }
.pane-3d .pane-title button.active { background: var(--accent2); border-color: var(--accent); color: #fff; }

/* ---- construction geometry (2D) ---- */
.entity.construction { stroke: #7d6a3a; stroke-dasharray: 6 4; stroke-width: 1.2; }
.entity.construction.sel { stroke: var(--sel); }

/* ---- external / reference geometry (2D) ---- */
.entity.external { stroke: #d9a441; stroke-dasharray: 3 3; stroke-width: 1.4; }
.entity.external.sel { stroke: var(--sel); }
.handle.external { fill: #d9a441; stroke: #4a3a12; }

/* ---- marquee box-select ---- */
.marquee { fill: rgba(79,158,255,0.10); stroke: var(--accent); stroke-width: 1; stroke-dasharray: 4 3; }

/* ---- external-geometry picker ---- */
.dlg-title { margin: 0 0 10px; font-weight: 600; }
.ext-list { max-height: 320px; overflow-y: auto; min-width: 340px; border: 1px solid var(--line); border-radius: 6px; padding: 4px; }
.ext-sketch { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .5px; padding: 6px 8px 3px; }
.ext-item { padding: 5px 10px; border-radius: 5px; font-family: monospace; font-size: 12px; cursor: pointer; }
.ext-item:hover { background: var(--accent2); color: #fff; }
.constraint-bar #btn-external { color: #d9a441; }

/* ---- feature colour + transform fields ---- */
.feat-color { width: 22px; height: 20px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: none; cursor: pointer; }
.feat-xyz { display: flex; align-items: center; gap: 4px; margin-top: 4px; }
.feat-xyz span { min-width: 30px; color: var(--muted); font-size: 11px; }
.feat-xyz input.num { width: 100%; flex: 1; min-width: 0; background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: 4px; padding: 2px 4px; font-family: monospace; font-size: 11px; }

/* ---- 2D context menu ---- */
.ctx { position: fixed; z-index: 1000; min-width: 170px; background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 4px; box-shadow: 0 8px 30px rgba(0,0,0,0.45); }
.ctx-item { padding: 6px 10px; border-radius: 5px; font-size: 12px; cursor: pointer; color: var(--text); }
.ctx-item:hover { background: var(--accent2); color: #fff; }
.ctx-item.disabled { color: #515a68; pointer-events: none; }
.ctx-sep { height: 1px; background: var(--line); margin: 4px 2px; }
.ctx-head { padding: 5px 10px 6px; color: var(--accent); font-size: 11px; font-family: monospace; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
