:root {
  --bg: #0e1218;
  --bg2: #151b24;
  --bg3: #1d2632;
  --line: #2b3746;
  --text: #e4e9ef;
  --muted: #91a0b2;
  --accent: #f5ad45;
  --accent2: #58aefa;
  --ok: #58c98b;
  --warn: #ffb03a;
  --err: #ff6a5e;
  --radius: 8px;
  font-size: 14px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: "Inter", "Segoe UI", system-ui, sans-serif;
  background: radial-gradient(circle at 55% 0, #172231 0, var(--bg) 45%); color: var(--text);
  overflow: hidden;
}
#app { display: flex; flex-direction: column; height: 100vh; }
#app-loader {
  position: fixed; inset: 0; z-index: 1000; display: flex; align-items: center; justify-content: center; gap: 12px;
  background: radial-gradient(circle at 50% 20%, #1c2a3b, var(--bg) 52%); color: var(--text); font-size: 16px;
  transition: opacity .2s ease;
}
#app-loader.done { opacity: 0; pointer-events: none; }
.loader-logo { width: 72px; height: 72px; object-fit: contain; }
.loader-spinner { width: 20px; height: 20px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: loader-spin .8s linear infinite; }
@keyframes loader-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .loader-spinner { animation: none; } }

button {
  background: linear-gradient(#222c39, #1b232e); color: var(--text); border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 10px; cursor: pointer; font-size: 12.5px;
  white-space: nowrap;
}
button:hover { border-color: var(--accent); color: #fff; }
button.active { background: var(--accent); color: #191919; border-color: var(--accent); font-weight: 600; }
.panel-toggle { display: none; }
input { background: var(--bg2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; padding: 6px 8px; }
input:focus { outline: none; border-color: var(--accent2); }

#topbar {
  display: flex; align-items: center; gap: 14px; padding: 8px 12px;
  background: rgba(21,27,36,.96); border-bottom: 1px solid var(--line); flex-wrap: wrap;
  box-shadow: 0 6px 24px rgba(0,0,0,.22); z-index: 20;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.brand .logo { width: 30px; height: 30px; object-fit: contain; }
.brand .thin { color: var(--muted); font-weight: 500; }
.tool-group, .top-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.shortcut-key { text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; }
.language-picker { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 11px; }
.language-picker select { background: var(--bg2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; padding: 5px; }
.split-tool { display: flex; }
.split-tool > .tool { border-radius: 6px 0 0 6px; }
.tool-menu-toggle {
  display: block; padding: 6px 7px; background: linear-gradient(#222c39, #1b232e); color: var(--text);
  border: 1px solid var(--line); border-left: 0;
  border-radius: 0 6px 6px 0; cursor: pointer; user-select: none;
}
.menu-anchor[open] .tool-menu-toggle, .tool-menu-toggle:hover { border-color: var(--accent); color: #fff; }
.split-tool .drop-menu { left: 0; right: auto; }
.v-sep { width: 1px; height: 22px; background: var(--line); display: inline-block; margin: 0 4px; }

#workspace { flex: 1; display: flex; min-height: 0; }
aside { background: linear-gradient(180deg, #171e28, #121820); overflow-y: auto; padding: 10px; }
#palette { border-right: 1px solid var(--line); }
#inspector { border-left: 1px solid var(--line); width: 280px; }
.panel-title { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 10px 2px 6px; font-weight: 700; }
.panel-title:first-child { margin-top: 0; }
.panel-title .hint { text-transform: none; letter-spacing: 0; color: var(--muted); font-weight: 400; }
.panel-note { font-size: 11px; color: var(--muted); margin-top: 8px; line-height: 1.45; }
.badge { background: var(--accent); color: #222; border-radius: 10px; padding: 0 7px; font-size: 10.5px; font-weight: 700; }

#palette-search-wrap { margin-bottom: 8px; }
#stock-controls { display: flex; align-items: center; gap: 5px; margin-bottom: 8px; color: var(--muted); font-size: 11px; }
#stock-controls span { margin-right: auto; }
#stock-controls button { padding: 2px 7px; font-size: 10px; }
#palette-search { width: 100%; }
#palette-tree .cat { margin-bottom: 4px; }
#palette-tree .cat-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 7px 9px; background: #1b2430; border: 1px solid transparent; border-radius: 7px; cursor: pointer;
  font-weight: 600; font-size: 12.5px;
}
#palette-tree .cat-head .n { color: var(--muted); font-weight: 400; font-size: 11px; }
#palette-tree .cat-tools { display: flex; align-items: center; gap: 7px; }
#palette-tree .cat-stock { margin: 0; padding: 0; accent-color: var(--ok); }
#palette-tree .cat-head:hover { color: var(--accent); border-color: #35465a; }
#palette-tree .items { padding: 4px 0 7px; }
#palette-tree .item {
  display: grid; grid-template-columns: minmax(0, 1fr) 44px 18px;
  align-items: center; gap: 6px; min-height: 30px;
  padding: 4px 7px; border-radius: 6px; cursor: pointer; font-size: 12px;
  border: 1px solid transparent;
}
#palette-tree .item-pick {
  display: grid; grid-template-columns: minmax(62px, auto) minmax(0, 1fr); align-items: center; gap: 6px;
  min-width: 0; padding: 0; border: 0; background: transparent; color: inherit; font: inherit; text-align: left;
}
#palette-tree .item-pick:focus-visible { outline: 2px solid var(--accent2); outline-offset: 2px; }
#palette-tree .item:hover { background: #202b38; border-color: #314154; }
#palette-tree .item.armed { background: var(--accent); color: #191919; }
#palette-tree .item .sub { color: var(--muted); font-size: 10.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#palette-tree .item.armed .sub { color: #333; }
#palette-tree .item.out-of-stock > span:not(.stock-toggle) { opacity: .42; }
.stock-toggle, .stock-max { display: flex; align-items: center; justify-content: center; margin: 0; }
.stock-toggle input { accent-color: var(--ok); padding: 0; }
.stock-max input { width: 42px; height: 22px; padding: 1px 3px; font-size: 10px; text-align: center; }

#catalogue-preview {
  position: fixed; z-index: 160; width: 282px; overflow: hidden;
  background: rgba(17,23,31,.97); border: 1px solid #40516a; border-radius: 10px;
  box-shadow: 0 16px 48px rgba(0,0,0,.55); backdrop-filter: blur(8px);
}
#catalogue-preview[hidden] { display: none; }
#catalogue-preview .preview-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 7px 9px; border-bottom: 1px solid var(--line); font-size: 11px; }
#catalogue-preview .preview-head b { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#catalogue-preview .preview-head button { padding: 0; width: 22px; height: 22px; font-size: 16px; line-height: 18px; }
#catalogue-preview canvas { display: block; width: 270px; height: 170px; margin: 6px; border-radius: 7px; background: radial-gradient(circle, #263243, #11171f 72%); }
#catalogue-preview .preview-status { padding: 0 9px 8px; color: var(--muted); font-size: 10px; }

#viewport-wrap { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#viewport { position: relative; flex: 1; min-height: 0; }
#c3d { width: 100%; height: 100%; display: block; }
#main2d { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
#c3d.flat-main { opacity: 0; }
#labels { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.lbl {
  position: absolute; transform: translate(-50%, -50%);
  background: rgba(23,25,29,.82); border: 1px solid var(--line); color: var(--text);
  font-size: 10.5px; padding: 1px 6px; border-radius: 4px; white-space: nowrap;
}
.lbl.angle { color: var(--accent); border-color: rgba(255,176,58,.4); }
.lbl.measure { color: var(--accent2); border-color: rgba(74,168,255,.4); }
.lbl.piece { opacity: .9; }
.lbl.warn { color: var(--err); border-color: rgba(255,106,94,.4); }
#hud-corner { position: absolute; top: 8px; right: 8px; }
#bypass-badge {
  position: absolute; right: 8px; top: 40px; padding: 4px 8px; border-radius: 6px;
  color: var(--err); border: 1px solid var(--err); background: rgba(35,18,20,.9);
  font-size: 10.5px; font-weight: 700; letter-spacing: .06em;
}
#stats-mini { font-size: 11px; color: var(--muted); background: rgba(23,25,29,.7); padding: 4px 8px; border-radius: 6px; }
#view-buttons { position: absolute; left: 8px; top: 8px; display: flex; gap: 4px; }
#view-buttons button { padding: 4px 8px; font-size: 11px; }

#statusbar {
  display: flex; gap: 16px; align-items: center; padding: 5px 12px;
  background: var(--bg2); border-top: 1px solid var(--line);
  font-size: 11.5px; color: var(--muted); flex-wrap: wrap;
}
#statusbar b { color: var(--text); font-weight: 600; }

#bottombar {
  height: 200px; min-height: 60px; display: flex; gap: 6px; padding: 6px;
  background: var(--bg2); border-top: 1px solid var(--line);
}
.view2d { flex: 1; display: flex; flex-direction: column; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; min-width: 0; }
.v2d-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 2px 8px; background: var(--bg3); font-size: 10.5px; color: var(--muted);
  text-transform: uppercase; letter-spacing: .06em;
}
.v2d-head button { padding: 1px 7px; font-size: 10px; border-radius: 4px; }
.view2d canvas { flex: 1; width: 100%; min-height: 0; background: #14161a; cursor: crosshair; }

.card {
  background: var(--bg3); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 10px; margin-bottom: 6px; font-size: 12.5px; line-height: 1.5;
}
.card table { width: 100%; border-collapse: collapse; font-size: 12px; }
.card th { text-align: left; color: var(--muted); font-weight: 600; padding: 2px 4px; border-bottom: 1px solid var(--line); }
.card td { padding: 2px 4px; border-bottom: 1px solid rgba(51,55,63,.4); }
.card td.num, .card th.num { text-align: right; font-variant-numeric: tabular-nums; }
.card .row { display: flex; justify-content: space-between; gap: 8px; }
.card .btn-row { display: flex; gap: 4px; flex-wrap: wrap; margin-top: 6px; }
.card .btn-row button { padding: 4px 8px; font-size: 11px; }
.muted { color: var(--muted); }
.kv { display: flex; justify-content: space-between; gap: 10px; }
.kv span:first-child { color: var(--muted); }
.wline { display: flex; gap: 6px; align-items: baseline; margin: 2px 0; font-size: 12px; }
.wline .tag { font-size: 10px; border-radius: 4px; padding: 0 6px; font-weight: 700; flex-shrink: 0; }
.wline .tag.warn { background: rgba(255,176,58,.15); color: var(--warn); }
.wline .tag.err { background: rgba(255,106,94,.15); color: var(--err); }
.wline .tag.info { background: rgba(74,168,255,.15); color: var(--accent2); }
.wline button { padding: 0 6px; font-size: 10.5px; }

#status-hint {
  padding: 4px 12px; font-size: 11px; color: var(--muted);
  background: var(--bg); border-top: 1px solid var(--line); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
#status-hint b { color: var(--text); }

#toast-wrap { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; z-index: 100; pointer-events: none; }
.toast {
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 14px; font-size: 12.5px; box-shadow: 0 4px 18px rgba(0,0,0,.5);
  animation: fadein .15s ease;
}
.toast.err { border-color: var(--err); color: var(--err); }
.toast.ok { border-color: var(--ok); color: var(--ok); }
@keyframes fadein { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; } }

#menu-layer .ctx-menu {
  position: absolute; z-index: 90; background: var(--bg2); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px; min-width: 180px; max-height: 300px; overflow-y: auto;
  box-shadow: 0 8px 30px rgba(0,0,0,.6); font-size: 12.5px;
}
.ctx-menu .mi { padding: 5px 10px; border-radius: 5px; cursor: pointer; display: flex; justify-content: space-between; gap: 10px; }
.ctx-menu .mi:hover { background: var(--bg3); }
.ctx-menu .mi .sub { color: var(--muted); font-size: 11px; }
.ctx-menu .head { padding: 4px 10px; font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.ctx-menu .mi.ok { color: var(--ok); }

::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #4a4f58; }

dialog#preset-dialog {
  background: var(--bg2); color: var(--text); border: 1px solid var(--line);
  border-radius: 10px; padding: 16px 20px; min-width: 340px;
}
dialog#preset-dialog::backdrop { background: rgba(0,0,0,.6); }
dialog#preset-dialog h3 { margin: 0 0 10px; }
dialog#preset-dialog label { display: flex; justify-content: space-between; gap: 10px; margin: 8px 0; align-items: center; }
dialog#preset-dialog input, dialog#preset-dialog select { width: 140px; }
dialog#preset-dialog .btn-row { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
dialog#preset-dialog button.primary { background: var(--accent); color: #191919; font-weight: 700; border-color: var(--accent); }
.webgl-fail { padding: 30px; color: var(--err); font-size: 14px; }

/* ---------- dropdown menus ---------- */
.menu-anchor { position: relative; }
.menu-anchor summary { list-style: none; }
.menu-anchor summary::-webkit-details-marker { display: none; }
.menu-btn {
  display: inline-flex; align-items: center; gap: 6px; background: var(--bg3);
  border: 1px solid var(--line); border-radius: 6px; padding: 6px 10px;
  cursor: pointer; font-size: 12.5px; user-select: none;
}
.menu-anchor[open] .menu-btn, .menu-btn:hover { border-color: var(--accent); color: #fff; }
.menu-btn .ic { width: 13px; height: 13px; }
.menu-anchor .drop-menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 95;
  background: var(--bg2); border: 1px solid var(--line); border-radius: 8px;
  padding: 4px; min-width: 210px;
  box-shadow: 0 8px 30px rgba(0,0,0,.6); font-size: 12.5px;
}
.menu-anchor .drop-menu button {
  display: flex; justify-content: space-between; gap: 18px; width: 100%;
  border: 0; background: transparent; padding: 7px 10px; text-align: left;
}
.menu-anchor .drop-menu button:hover { background: var(--bg3); }
.menu-anchor .drop-menu button span { color: var(--muted); font-size: 10.5px; }

/* ---------- projection badge & buttons ---------- */
.proj-badge {
  position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
  background: rgba(74,168,255,.16); border: 1px solid rgba(74,168,255,.5);
  color: var(--accent2); font-size: 10.5px; font-weight: 700; letter-spacing: .08em;
  padding: 3px 10px; border-radius: 6px; pointer-events: none; text-transform: uppercase;
}
.view2d.active-proj .v2d-head { color: var(--accent2); }
.view2d.active-proj { border-color: rgba(74,168,255,.55); }
.v2d-actions { display: flex; gap: 4px; align-items: center; }

/* ---------- resize handles ---------- */
.drag-h.col { width: 5px; margin: 0 -2px; cursor: col-resize; background: transparent; z-index: 5; position: relative; }
.drag-h.col:hover, .drag-h.col.dragging { background: rgba(255,176,58,.45); }
.drag-h.row { height: 7px; flex: 0 0 7px; cursor: row-resize; background: transparent; z-index: 5; position: relative; }
.drag-h.row:hover, .drag-h.row.dragging { background: rgba(255,176,58,.45); }
.split { display: flex; min-width: 0; }
#palette { width: 250px; flex-shrink: 0; }
#inspector { width: 280px; flex-shrink: 0; }

@media (max-width: 900px) {
  .panel-toggle { display: inline-block; }
  #split-left, #inspector, .drag-h.col { display: none; }
  #app.palette-open #split-left { display: flex; }
  #app.inspector-open #inspector { display: block; }
}

/* ---------- metadata card ---------- */
.meta-grid { display: grid; grid-template-columns: 1fr; gap: 4px; }
.meta-grid label { display: flex; flex-direction: column; gap: 2px; font-size: 11px; color: var(--muted); }
.meta-grid input { font-size: 12px; padding: 4px 6px; }

/* Optional accounts and project history. */
#account-dialog { width: min(660px, calc(100vw - 32px)); max-height: 85vh; overflow: auto; background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
#account-dialog::backdrop { background: #0009; }
#account-dialog [hidden] { display: none !important; }
#account-dialog fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
#account-dialog form, #account-dialog label { display: grid; gap: 8px; margin: 12px 0; }
#account-dialog input { width: 100%; min-width: 0; box-sizing: border-box; }
#account-dialog summary { cursor: pointer; padding: 8px 0; }
#account-dialog h2 { font-size: 18px; margin: 0; }
#account-dialog p, #account-dialog small { color: var(--muted); }
#account-dialog a { color: var(--accent); display: inline-block; padding: 10px 0; }
#account-status { min-height: 2em; overflow-wrap: anywhere; }
.account-header, .account-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.account-header { justify-content: space-between; }
.cloud-row { border-top: 1px solid var(--line); padding: 12px 0; overflow-wrap: anywhere; }
.cloud-row strong, .cloud-row small { display: block; margin-bottom: 6px; }
.cloud-row button { margin: 2px 6px 2px 0; }
