* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: #24272c; color: #dfe3ea;
  font: 13px/1.35 "Segoe UI", system-ui, -apple-system, Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
kbd { float: right; margin-left: 18px; color: #8d95a1; font: 11px "Segoe UI", system-ui, sans-serif; }

#app { display: grid; height: 100vh; height: 100dvh;
  grid-template-columns: 80px minmax(0, 1fr) 350px; grid-template-rows: 34px minmax(0, 1fr) 24px;
  grid-template-areas: "top top top" "tools view side" "status status status"; }

/* ---------- top bar & menus */
#topbar { grid-area: top; display: flex; align-items: center; gap: 14px; background: #1b1d21;
  border-bottom: 1px solid #30343a; padding: 0 10px; z-index: 20; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; letter-spacing: .2px; }
.shop-link { margin-left: 6px; font-weight: 400; font-size: 12px; color: #9fb8d8; text-decoration: none; padding: 3px 8px; border: 1px solid #3d424a; border-radius: 4px; white-space: nowrap; }
.shop-link:hover { color: #fff; border-color: #6e96c8; }
.menus { display: flex; }
.menu { position: relative; }
.menu-btn { background: none; border: 0; padding: 6px 10px; border-radius: 4px; cursor: pointer; }
.menu-btn:hover, .menu.open .menu-btn { background: #3a5f8f; }
.menu-list { display: none; position: absolute; top: 100%; left: 0; min-width: 270px; background: #2b2e34;
  border: 1px solid #3d424a; border-radius: 6px; padding: 4px; box-shadow: 0 8px 24px rgba(0,0,0,.45); }
.menu.open .menu-list { display: block; }
.menu-list button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 6px 10px;
  border-radius: 4px; cursor: pointer; }
.menu-list button:hover { background: #3a5f8f; }
.menu-list hr, #tools hr { border: 0; border-top: 1px solid #3d424a; margin: 4px 2px; }
.menu-list .check { padding: 6px 10px; }

.viewbar { margin-left: auto; display: flex; align-items: center; gap: 8px; min-width: 0; }
.vb-icon { width: 20px; height: 20px; flex: none; }
.seg { display: flex; border: 1px solid #3d424a; border-radius: 6px; overflow: hidden; }
.seg button { background: #2a2d33; border: 0; border-right: 1px solid #3d424a; padding: 3px 10px; cursor: pointer; font-size: 12px; color: #cfd5de; }
.seg button:last-child { border-right: 0; }
.seg button:hover { background: #3a3f47; color: #fff; }
.seg button.active { background: #3a5f8f; color: #fff; }
.vb-fit { display: flex; align-items: center; gap: 4px; background: #2a2d33; border: 1px solid #3d424a; border-radius: 6px; padding: 2px 9px 2px 6px; cursor: pointer; font-size: 12px; color: #cfd5de; }
.vb-fit:hover { background: #3a3f47; color: #fff; }
.vb-fit .st { stroke-width: 3; }
.vb-proj { min-width: 104px; justify-content: flex-start; }
.vb-proj.on { background: #3a5f8f; border-color: #5a86bf; color: #fff; }

/* ---------- left tools */
#tools { grid-area: tools; background: #1b1d21; display: flex; flex-direction: column; gap: 2px; padding: 5px 5px;
  overflow-y: auto; border-right: 1px solid #30343a; }
.tool { display: flex; flex-direction: column; align-items: center; gap: 0; width: 100%; padding: 3px 2px 2px;
  background: none; border: 1px solid transparent; border-radius: 7px; cursor: pointer; font-size: 11.5px; }
.tool svg { width: 25px; height: 25px; }
.tool:hover { background: #33373e; border-color: #444a53; }
.tool.active { background: #3a5f8f; border-color: #5a86bf; }
.tool:disabled { opacity: .4; cursor: default; }
.st { stroke: #e1e6ee; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }
.st:not([fill]) { fill: none; }
.st.thin { stroke-width: 1.6; }
.tool-wrap { position: relative; }
.popup { display: none; position: fixed; left: 90px; top: 90px; z-index: 100; background: #2b2e34; border: 1px solid #4a5059;
  border-radius: 8px; padding: 8px; box-shadow: 0 10px 30px rgba(0,0,0,.55); }
.popup.open { display: block; }
.popup-title { font-size: 12px; color: #9fc2ef; font-weight: 600; margin: 0 2px 6px; }
.shape-grid { display: grid; grid-template-columns: repeat(2, 120px); gap: 4px; }
.shape-btn { display: flex; align-items: center; gap: 8px; background: none; border: 1px solid transparent; border-radius: 6px;
  padding: 5px 8px; cursor: pointer; color: #dfe3ea; text-align: left; font-size: 12.5px; }
.shape-btn:hover, .shape-btn:focus-visible { background: #3a5f8f; border-color: #5a86bf; outline: none; }
.shape-btn svg { flex: none; }

/* ---------- 3D view */
#viewwrap { grid-area: view; position: relative; overflow: hidden;
  background: linear-gradient(to bottom, #4d525c 0%, #2f3238 55%, #1c1d21 100%); }
#gl, #overlay { position: absolute; inset: 0; display: block; }
#overlay { touch-action: none; }
#hud { position: absolute; left: 12px; top: 12px; background: rgba(20,22,26,.78); border-radius: 6px; padding: 6px 10px;
  pointer-events: none; max-width: calc(100% - 24px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#dropzone { position: absolute; inset: 10px; border: 3px dashed #6ea8f0; border-radius: 12px; background: rgba(40,80,130,.35);
  display: none; align-items: center; justify-content: center; font-size: 20px; pointer-events: none; }
#viewwrap.dragging #dropzone { display: flex; }
#busy { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.35); font-size: 18px; }
#busy[hidden] { display: none; }

/* ---------- right side */
#side { grid-area: side; overflow-y: auto; padding: 4px 8px 10px; background: #24272c; border-left: 1px solid #30343a; }
.group { border: 1px solid #3a3f47; border-radius: 7px; background: #2a2d33; padding: 8px; margin: 10px 0 0; }
.group h2 { font-size: 12.5px; margin: 0 0 8px; color: #9fc2ef; font-weight: 600; }
.objlist { list-style: none; margin: 0 0 8px; padding: 2px; min-height: 92px; max-height: 160px; overflow-y: auto;
  background: #1c1e22; border: 1px solid #3d424a; border-radius: 5px; }
.objlist li { display: flex; align-items: center; gap: 7px; padding: 4px 6px; border-radius: 4px; cursor: pointer; user-select: none; }
.objlist li:hover { background: #2c3036; }
.objlist li.sel { background: #3a5f8f; color: #fff; }
.objlist .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.objlist .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.objlist .tris { color: #9aa3ae; font-size: 11px; }
.objlist li.sel .tris { color: #d6e4f5; }
.objlist input[type=checkbox] { margin: 0; }
.objlist .empty { color: #8b929c; cursor: default; padding: 10px; }
/* anchor box: a pin toggle at the end of each row */
.objlist .anchor { display: inline-flex; align-items: center; gap: 2px; padding: 1px 4px; border-radius: 4px; cursor: pointer; color: #7d8590; font-size: 12px; flex: none; }
.objlist .anchor:hover { background: rgba(255,255,255,.08); color: #ffc85a; }
.objlist .anchor.on { color: #ffc85a; background: rgba(255,200,90,.14); }
.objlist .anchor input { margin: 0; width: 12px; height: 12px; accent-color: #e0a63a; }
.objlist li.pinned .name::after { content: " · pinned"; color: #ffc85a; font-size: 11px; }
.group button, #side > .panel button { background: #383d45; border: 1px solid #4a5059; border-radius: 5px; padding: 5px 6px;
  cursor: pointer; min-width: 0; }
.group button:hover { background: #444a54; }
.group button:active { background: #2c4a70; }
.group button:disabled { color: #6b717a; background: #2e3137; cursor: default; }
button.primary { background: #3a6ea8 !important; border-color: #5a8fd0 !important; font-weight: 600; color: #fff; }
button.primary:hover { background: #4680c0 !important; }
button.raise { background: #2f6b45 !important; border-color: #4a9a66 !important; font-weight: 600; }
button.lower { background: #7a3a32 !important; border-color: #a85448 !important; font-weight: 600; }
button.wide { display: block; width: 100%; margin-top: 6px; }
.row2, .row3 { display: grid; gap: 6px; margin-top: 6px; }
.row2 { grid-template-columns: 1fr 1fr; }
.row3 { grid-template-columns: 1fr 1fr 1fr; }
.row2.align { align-items: center; }
.align-grid { display: grid; grid-template-columns: 22px 1fr 1fr 1fr; gap: 5px; align-items: center; margin-top: 7px; }
.align-grid.wide-label { grid-template-columns: 92px 1fr 1fr 1fr; }
.align-grid span { color: #aab2bf; }
.xyz { display: grid; grid-template-columns: auto 1fr auto 1fr auto 1fr; gap: 5px; align-items: center; }
.xyz span { color: #aab2bf; }
input[type=number], select { background: #1c1e22; border: 1px solid #454b54; border-radius: 4px; padding: 3px 5px; min-width: 0; width: 100%; }
input[type=number]:focus, select:focus { outline: 1px solid #5a86bf; border-color: #5a86bf; }
input:disabled { opacity: .5; }
.field { display: grid; grid-template-columns: 76px 1fr auto; gap: 6px; align-items: center; margin-top: 6px; }
.row2 .field { grid-template-columns: auto 1fr auto; margin: 0; }
#panelDensity .field { grid-template-columns: 104px 1fr auto; }
.slider { display: grid; grid-template-columns: 100px 1fr 58px 26px; gap: 6px; align-items: center; margin-bottom: 6px; }
input[type=range] { width: 100%; accent-color: #6e9fd8; }
.check { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.hint { color: #aab2bf; margin: 8px 2px 0; font-size: 12px; }
.panel[hidden] { display: none; }

#status { grid-area: status; display: flex; justify-content: space-between; gap: 10px; align-items: center; padding: 0 10px;
  background: #1b1d21; color: #aab2bf; border-top: 1px solid #30343a; font-size: 12px; white-space: nowrap; overflow: hidden; }
#statusMsg { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
#status .stats { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; color: #c3cad4; }
#status .stats b { color: #e6eaf0; font-weight: 600; }
#status .stats .over { color: #ffb060; font-weight: 600; }
#statusRight { flex: none; }
@media (max-width: 1000px) { #statusRight { display: none; } }

.badge { background: none; border: 0; color: inherit; padding: 0 2px; cursor: pointer; font: inherit; text-decoration: underline dotted; }
.badge.on { color: #8fd18f; }
.axes { display: grid; grid-template-columns: 34px 1fr; gap: 3px 8px; align-items: center; margin: 10px 0 4px; font-size: 12px; color: #aab2bf; }
.axes .bar { position: relative; height: 8px; background: #1c1e22; border-radius: 4px; overflow: hidden; }
.axes .bar i { position: absolute; top: 0; bottom: 0; left: 50%; background: #6e9fd8; }
#smDlg { width: 460px; }
#smDlg button { background: #383d45; border-color: #4a5059; color: #dfe3ea; }
#smDlg button:disabled { opacity: .45; cursor: default; }
#smDlg .row2 { margin-bottom: 6px; }
dialog { background: #2b2e34; color: #dfe3ea; border: 1px solid #4a5059; border-radius: 10px; max-width: 520px; }
dialog::backdrop { background: rgba(0,0,0,.5); }
dialog h3 { margin-top: 0; }
dialog li { margin-bottom: 6px; }
dialog form { text-align: right; }
dialog button { border-radius: 5px; padding: 6px 18px; border: 1px solid; cursor: pointer; }

/* ---------- narrower screens */
@media (max-width: 1000px) {
  #app { grid-template-columns: 72px minmax(0, 1fr) 300px; }
  .slider { grid-template-columns: 78px 1fr 52px 22px; }
}
@media (max-width: 1100px) {
  .vb-icon { display: none; }
  .seg button { padding: 3px 7px; }
}
@media (max-width: 900px) {
  .seg button[data-view="back"], .seg button[data-view="left"], .seg button[data-view="bottom"] { display: none; }
}
@media (max-width: 760px) {
  .viewbar { gap: 4px; }
  .seg button[data-view="front"], .seg button[data-view="right"] { display: none; }
  .vb-fit { padding: 2px 6px; }
  #app { height: auto; min-height: 100vh; grid-template-columns: minmax(0, 1fr); grid-template-rows: 34px auto 58vh auto 24px;
    grid-template-areas: "top" "tools" "view" "side" "status"; }
  #side, #tools, #viewwrap, #topbar { min-width: 0; }
  #tools { flex-direction: row; overflow-x: auto; overflow-y: hidden; border-right: 0; border-bottom: 1px solid #30343a; padding: 4px;
    scrollbar-width: thin; }
  #tools hr { border-top: 0; border-left: 1px solid #3d424a; margin: 4px 2px; }
  .tool { width: auto; min-width: 60px; }
  .tool-wrap { display: flex; }
  #side { border-left: 0; overflow: visible; }
  .menu-list { min-width: 230px; }
  .brand span, .shop-link { display: none; }
}
@media (max-width: 420px) {
  #topbar { gap: 4px; padding: 0 4px; }
  .menu-btn { padding: 6px 7px; }
  .menu:nth-child(n+3) .menu-list { left: auto; right: 0; }
}

/* ---------- text tool */
#panelText textarea { width: 100%; min-height: 46px; resize: vertical; background: #1c1e22; border: 1px solid #454b54; border-radius: 4px;
  padding: 5px 6px; font: 14px "Segoe UI", system-ui, sans-serif; color: #eef1f5; }
#panelText textarea:focus, #panelText input[type=text]:focus { outline: 1px solid #5a86bf; border-color: #5a86bf; }
#panelText input[type=text] { background: #1c1e22; border: 1px solid #454b54; border-radius: 4px; padding: 3px 5px; min-width: 0; width: 100%; }
#panelText .field { grid-template-columns: 90px 1fr auto; }
#panelText .row2 { margin-top: 8px; }
/* ---------- font picker */
.font-btn { display: flex; align-items: center; justify-content: space-between; gap: 6px; width: 100%; min-width: 0;
  background: #1c1e22 !important; border: 1px solid #454b54 !important; border-radius: 4px; padding: 3px 8px !important; cursor: pointer;
  color: #eef1f5; font-size: 14px; text-align: left; }
.font-btn:hover { border-color: #5a86bf !important; }
.font-btn span:first-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.font-btn .caret { font-family: "Segoe UI", system-ui, sans-serif; font-size: 11px; color: #9aa3ae; flex: none; }
.font-pop { width: 300px; padding: 8px; }
.font-pop input[type=search], .font-pop input[type=text] { width: 100%; background: #1c1e22; border: 1px solid #454b54; border-radius: 4px;
  padding: 5px 7px; color: #eef1f5; font: 13px "Segoe UI", system-ui, sans-serif; }
.font-list { margin-top: 6px; max-height: 320px; overflow-y: auto; border: 1px solid #3d424a; border-radius: 5px; background: #1c1e22; }
.font-item { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 5px 9px; color: #e6eaf0; font-size: 16px;
  cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.font-item:hover, .font-item:focus-visible { background: #3a5f8f; outline: none; }
.font-item.current { background: #2c4a70; }
.font-foot { display: flex; align-items: center; gap: 8px; margin-top: 6px; font-size: 11.5px; color: #9aa3ae; white-space: nowrap; }
.font-custom { display: flex; align-items: center; gap: 5px; flex: 1; min-width: 0; }
.font-custom input { padding: 3px 6px !important; }

/* ---------- pencil */
.curvelist { min-height: 60px; max-height: 140px; }
.curvelist .swatch { background: #ffb347; border-radius: 6px; height: 4px; width: 14px; }
/* ---------- units switch */
.units-seg { margin-left: auto; }
.units-seg + .viewbar { margin-left: 10px; }
.units-seg button { padding: 3px 9px; }
@media (max-width: 760px) { .units-seg button { padding: 3px 6px; } }
.curvelist .swatch.closed { height: 9px; width: 12px; border-radius: 3px; background: none; border: 2px solid #ffb347; }

/* ---------- polygons & booleans */
.poly-grid { grid-template-columns: repeat(2, 1fr); }
.poly-grid .shape-btn { padding: 4px 8px; }
.poly-grid .shape-btn.active { background: #3a5f8f; border-color: #5a86bf; }
#panelPolygon .field { grid-template-columns: 96px 1fr auto; }
.objlist li.neg .name { color: #f39a95; }
.objlist li.neg .swatch { background: #f06a63 !important; opacity: .7; }

/* ---------- cut view */
#panelClip .field { grid-template-columns: 84px 1fr auto; }
#panelClip .seg button { flex: 1; padding: 4px 0; }
#panelClip input[type=range] { margin-top: 4px; }
#clipInfo b { color: #ffdb6a; font-weight: 600; }