/* ============================================================
   free3d — UI styles
   A clean, CAD-ish dark interface. Light theme via prefers-color-scheme.
   ============================================================ */

:root {
  --bg:          #1b1d21;
  --bg-panel:    #24272c;
  --bg-elev:     #2c3036;
  --bg-hover:    #343941;
  --border:      #383d45;
  --text:        #e6e8ea;
  --text-dim:    #9aa1ab;
  --accent:      #4a9eff;
  --accent-dim:  #2f6fb8;
  --toolbar-h:   48px;
  --panel-w:     260px;
  --radius:      6px;
  --shadow:      0 6px 20px rgba(0,0,0,0.35);
  --font:        system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: var(--font);
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  user-select: none;
  -webkit-user-select: none;
}
/* Vertical stack: toolbar, toggle strip, then the workspace fills the rest. A
   flex column (rather than an absolutely-placed workspace) keeps the rows from
   overlapping and lets the toolbar collapse and the viewport reflow cleanly. */
body { display: flex; flex-direction: column; }

/* ===================== Toolbar ===================== */
#toolbar {
  height: var(--toolbar-h);
  flex: none;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  z-index: 20;
  position: relative;
}
#toolbar.collapsed { display: none; }

/* Floating tab that brings the toolbar back when it's collapsed. */
.expand-toolbar {
  position: fixed;
  /* Sit clear of the toggle-strip's own collapse button (at the strip's far right,
     ~right:12–36px): when only the toolbar is collapsed the strip slides to the top,
     and a pill at right:10 would cover — and intercept clicks on — that button. */
  top: 6px; right: 46px;
  z-index: 30;
  height: 26px;
  padding: 0 10px 0 7px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 13px;
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
}
.expand-toolbar:hover { color: var(--text); }
.expand-toolbar svg { width: 15px; height: 15px; }
.expand-toolbar[hidden] { display: none; } /* class display: would otherwise beat the hidden attr */

.toolbar-groups {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: 1;
  min-width: 0;
  /* NB: no overflow here — an overflow value would clip the dropdowns,
     which are absolutely positioned below the toolbar. */
}

.tool-group { position: relative; }

/* A group trigger button (may open a dropdown) */
.group-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 34px;
  padding: 0 10px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius);
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
}
.group-btn:hover { background: var(--bg-hover); }
.group-btn.active { background: var(--accent-dim); border-color: var(--accent); }
.group-btn .caret { color: var(--text-dim); font-size: 9px; margin-left: 1px; }
.group-btn .ico { width: 16px; height: 16px; display: block; }

/* Dropdown menu of sub-tools */
.dropdown {
  position: absolute;
  top: calc(100% + 5px);
  left: 0;
  min-width: 172px; /* a floor; items use white-space:nowrap so the menu grows to fit its longest label */
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 5px;
  display: none;
  flex-direction: column;
  gap: 2px;
  z-index: 40;
}
.tool-group.open .dropdown { display: flex; }
/* Transparent bridge across the trigger→menu gap so hover isn't dropped. */
.dropdown::before { content: ''; position: absolute; left: 0; right: 0; top: -7px; height: 7px; }

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 9px;
  /* Extra right padding reserves a fixed slot for the caret / check mark, which
     are positioned into it — so the key chip is the only auto-margin element and
     lands in the same column on every row (whether or not the row has a caret). */
  position: relative;
  padding: 7px 22px 7px 9px;
  border-radius: 4px;
  background: transparent;
  border: none;
  color: var(--text);
  cursor: pointer;
  text-align: left;
  font-size: 13px;
  width: 100%;
  white-space: nowrap; /* keep multi-word labels ("Save checkpoint", "Reduce triangles…") on one line; the menu auto-grows to fit */
}
.dropdown-item:hover { background: var(--bg-hover); }
.dropdown-item.active { background: var(--accent-dim); }
.dropdown-item .ico { width: 16px; height: 16px; flex: none; color: var(--text-dim); }
/* Icon rows (Primitives): larger 3D shapes. */
.dropdown-item.iconrow { padding: 8px 22px 8px 12px; }
.dropdown-item.iconrow .ico { width: auto; height: 26px; color: var(--text); }
/* Cylinder / Cone rows: three individually-clickable 3D parts. */
.dropdown-item.parts { gap: 2px; }
.dropdown-item.parts:hover { background: transparent; }
/* The parts row never fills as a whole (the active tool is shown by the active
   sub-shape's .part.active highlight instead — see toolbar.js reflectParts). */
.dropdown-item.parts.active { background: transparent; }
.dropdown-item.parts .part { display: inline-flex; align-items: center; padding: 2px 5px; border-radius: 5px; cursor: pointer; }
.dropdown-item.parts .part:hover { background: var(--bg-hover); }
.dropdown-item.parts .part:focus-visible { outline: 1px solid var(--accent); outline-offset: -1px; } /* keyboard-focusable (role=button) */
.dropdown-item.active .ico { color: var(--text); }
/* Placement-anchor toggle: highlight the active of the three anchor icons. */
.dropdown-item.parts .part.active { background: var(--accent-dim); }
.dropdown-item.parts .part.active .ico { color: var(--text); }
.dropdown-item .key {
  margin-left: auto;
  color: var(--text-dim);
  font-size: 11px;
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  line-height: 15px;
}
.dropdown-sep { height: 1px; background: var(--border); margin: 4px 6px; }

/* ---- Always-visible global toggle strip (under the toolbar) ---- */
.toggle-bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 19; /* below the toolbar (20) so its dropdowns still overlay the strip */
}
.toggle-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 10px 0 5px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 13px;
  color: var(--text-dim);
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.toggle-chip:hover { background: var(--bg-hover); color: var(--text); }
.toggle-chip .key {
  font-size: 10px;
  line-height: 15px;
  min-width: 15px;
  text-align: center;
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 3px;
}
.toggle-bar.collapsed { display: none; }
/* Collapse handle at the right end of the strip. */
.toggle-collapse {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px; height: 24px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-dim);
  cursor: pointer;
}
.toggle-collapse:hover { background: var(--bg-hover); color: var(--text); }
.toggle-collapse svg { width: 15px; height: 15px; }
/* Tab that restores the strip, hanging from the top of the viewport. Offset right so
   it clears the left tree-panel's own restore tab (.expand-tab, at the left edge)
   when both are collapsed at once. */
.expand-toggles {
  position: absolute;
  top: 0; left: 44px;
  z-index: 10;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 20px;
  padding: 0 9px 0 6px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  color: var(--text-dim);
  font-size: 11px;
  cursor: pointer;
}
.expand-toggles:hover { color: var(--text); }
.expand-toggles svg { width: 13px; height: 13px; }
.expand-toggles[hidden] { display: none; } /* class display: would beat the hidden attr */
/* Caret and check live in the reserved far-right slot (see .dropdown-item),
   clear of the key column. */
.dropdown-item .check,
.dropdown-item .seg-caret {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
}
.dropdown-item .check { color: var(--accent); font-size: 12px; }
.dropdown-item .seg-caret { color: var(--text-dim); font-size: 10px; }

/* "Sides" flyout for radial shapes (Cylinder / Sphere / Cone) */
.dropdown-item-wrap { position: relative; }
.seg-flyout {
  display: none;
  position: absolute;
  left: 100%;
  top: -5px;
  min-width: 176px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 10px 11px;
  z-index: 50;
}
.dropdown-item-wrap:hover .seg-flyout,
.seg-flyout:hover { display: block; }
.seg-head { font-size: 12px; color: var(--text-dim); margin-bottom: 8px; }
.seg-slider + .seg-head { margin-top: 12px; } /* space between stacked sliders */
.seg-head .seg-val { color: var(--text); font-weight: 600; }
.seg-slider { width: 100%; margin: 0; accent-color: var(--accent); cursor: pointer; }

/* Segmented button row (e.g. Sculpt mode: Drag / Pull / Push) */
.seg-choice { display: flex; gap: 4px; }
.seg-choice-btn {
  flex: 1; padding: 5px 4px; font: inherit; font-size: 12px; cursor: pointer;
  color: var(--text-dim); background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.seg-choice-btn:hover { color: var(--text); border-color: var(--text-dim); }
.seg-choice-btn.active { color: #fff; background: var(--accent); border-color: var(--accent); }

.toolbar-right { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.hist-buttons { display: flex; gap: 2px; }
.hist-btn {
  display: flex; align-items: center; justify-content: center;
  width: 32px; height: 32px;
  background: transparent; border: 1px solid transparent; border-radius: var(--radius);
  color: var(--text); cursor: pointer;
}
a.hist-btn { text-decoration: none; }
.hist-btn svg { width: 18px; height: 18px; }
.hist-btn:hover:not(:disabled) { background: var(--bg-hover); }
.hist-btn:disabled { color: var(--text-dim); opacity: 0.4; cursor: default; }
.hint {
  color: var(--text-dim);
  font-size: 12px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- Snapping panel ---- */
.snap-dropdown { min-width: 236px; }
.snap-check { justify-content: space-between; }
.snap-hdr {
  font-size: 11px; color: var(--text-dim); text-transform: uppercase;
  letter-spacing: 0.4px; padding: 4px 9px 2px;
}
.snap-list { display: flex; flex-direction: column; gap: 1px; }
.snap-row {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 8px 5px 6px; border-radius: 4px; cursor: pointer;
  color: var(--text); background: transparent;
}
.snap-row:hover { background: var(--bg-hover); }
.snap-row.dragging { opacity: 0.5; background: var(--accent-dim); }
.snap-row.off .snap-name { color: var(--text-dim); }
.snap-grip { color: var(--text-dim); font-size: 12px; cursor: grab; line-height: 1; }
.snap-name { flex: 1; font-size: 13px; }
/* On/off tick box — a bordered square that's filled + ticked when enabled and an
   empty outline when disabled, so its state (and hit target) reads in both. */
.snap-box {
  flex: none; width: 16px; height: 16px; border-radius: 3px;
  border: 1px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  font-size: 11px; line-height: 1; color: #fff;
}
.snap-row:not(.off) .snap-box { background: var(--accent); border-color: var(--accent); }
.snap-field {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 5px 9px; font-size: 13px; color: var(--text);
}
.snap-field-col { padding: 6px 11px 8px; }
.snap-rot { padding: 2px 9px; }
.snap-rot .snap-check { flex: 1; padding: 5px 0; }
/* The rotation row nests its select in a .snap-field; drop that inner padding so
   the select lines up with the World-grid-step select in the same column. */
.snap-rot .snap-field { flex: none; padding: 0; }
.snap-select {
  width: 72px; box-sizing: border-box;
  background: var(--bg-panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 4px;
  padding: 3px 6px; font-size: 12px; cursor: pointer;
}
.snap-select:focus { outline: 1px solid var(--accent); }
.snap-select:disabled { opacity: 0.4; cursor: default; }
/* Keyboard focus ring for the snap-priority rows (they're role=button, tabindex 0). */
.snap-row:focus-visible { outline: 1px solid var(--accent); outline-offset: -1px; }

/* ===================== Workspace ===================== */
/* Fills the height left by the toolbar + toggle strip (flex column on <body>).
   position:relative anchors the panels' absolute expand-tabs. */
#workspace {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
}

/* ---- Left panel ---- */
#left-panel {
  width: var(--panel-w);
  flex: none;
  background: var(--bg-panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left 0.18s ease;
}
#left-panel.collapsed { margin-left: calc(-1 * var(--panel-w)); }

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 12px;
  font-weight: 600;
  font-size: 12px;
  letter-spacing: 0.4px;
  text-transform: uppercase;
  color: var(--text-dim);
  border-bottom: 1px solid var(--border);
}

.panel-actions { display: flex; align-items: center; gap: 2px; }

/* Geometry (boolean) operations — a distinct row below the group buttons. */
.panel-ops { display: flex; gap: 4px; padding: 5px 8px; border-bottom: 1px solid var(--border); }
.op-btn {
  flex: 1; padding: 4px 2px; font-size: 11px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 4px;
  color: var(--text); cursor: pointer;
}
.op-btn:hover:not(:disabled) { background: var(--bg-hover); }
.op-btn:disabled { opacity: 0.35; cursor: default; }

.hierarchy {
  flex: 1;
  overflow-y: auto;
  padding: 6px 4px;
}
.hierarchy.drop-root { outline: 2px dashed var(--accent); outline-offset: -4px; border-radius: var(--radius); }

.expand-tab {
  position: absolute;
  left: 0;
  top: 10px;
  width: 22px;
  height: 34px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-left: none;
  border-radius: 0 var(--radius) var(--radius) 0;
  color: var(--text-dim);
  cursor: pointer;
  z-index: 10;
}
.expand-tab:hover { color: var(--text); }
.expand-tab-right {
  left: auto; right: 0;
  border-left: 1px solid var(--border); border-right: none;
  border-radius: var(--radius) 0 0 var(--radius);
}

/* ---- Right panel (object properties / inspector) ---- */
#right-panel {
  width: var(--panel-w);
  flex: none;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-right 0.18s ease;
}
#right-panel.collapsed { margin-right: calc(-1 * var(--panel-w)); }

.inspector { flex: 1; overflow-y: auto; padding: 10px 12px; }
.insp-empty { color: var(--text-dim); font-size: 12px; padding: 14px 4px; text-align: center; line-height: 1.5; }
.insp-name {
  font-size: 13px; font-weight: 600; color: var(--text);
  margin-bottom: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.insp-kind { font-size: 11px; color: var(--text-dim); margin-bottom: 12px; }

/* Live relative-displacement readout (shown only during a gizmo drag). */
.insp-relative {
  margin-bottom: 12px; padding: 8px 9px;
  background: var(--accent-dim); border: 1px solid var(--accent);
  border-radius: 6px;
}
.insp-rel-title {
  font-size: 11px; letter-spacing: 0.3px; text-transform: uppercase;
  /* --text, not --accent: accent-on-accent-dim is ~1.9:1 in dark, below WCAG AA. */
  color: var(--text); margin-bottom: 6px; font-weight: 600;
}
.insp-rel-row { display: flex; flex-wrap: wrap; gap: 5px; min-height: 16px; }
.insp-rel-chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 7px; border-radius: 10px;
  background: var(--bg-elev); border: 1px solid var(--border);
  font-size: 12px; color: var(--text); font-variant-numeric: tabular-nums;
}
.insp-rel-axis { font-size: 10px; font-weight: 600; color: var(--text-dim); }
.insp-group { margin-bottom: 12px; }
.insp-group > .insp-label {
  font-size: 11px; letter-spacing: 0.3px; text-transform: uppercase;
  color: var(--text-dim); margin-bottom: 4px;
}
.insp-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 5px; }
.insp-field { display: flex; align-items: center; gap: 3px; }
.insp-field > span { font-size: 11px; color: var(--text-dim); width: 10px; }
.insp-field > input {
  width: 100%; min-width: 0; padding: 4px 5px;
  background: var(--bg-elev); border: 1px solid var(--border); border-radius: 4px;
  color: var(--text); font-size: 12px; font-variant-numeric: tabular-nums;
}
.insp-field > input:focus { outline: none; border-color: var(--accent); }
/* Trim the number spinners so the fields stay compact. */
.insp-field > input::-webkit-outer-spin-button,
.insp-field > input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
.insp-field > input[type=number] { -moz-appearance: textfield; }

/* ---- Hierarchy tree ---- */
.tree-node {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--text);
}
/* .hover mirrors the viewport hover; :hover covers the pointer being over the row. */
.tree-node:hover,
.tree-node.hover { background: var(--bg-hover); }
.tree-node.selected { background: var(--accent-dim); }
.tree-node:focus-visible { outline: 1px solid var(--accent); outline-offset: -1px; } /* keyboard-focusable tree items */
.tree-node.drop-into { background: var(--accent-dim); outline: 1px dashed var(--accent); outline-offset: -2px; }
/* Sibling-reorder insertion line (drop above / below this row). */
.tree-node.drop-before { box-shadow: inset 0 2px 0 0 var(--accent); }
.tree-node.drop-after { box-shadow: inset 0 -2px 0 0 var(--accent); }
.tree-node .node-caret {
  width: 12px; flex: none; text-align: center;
  color: var(--text-dim); font-size: 9px; cursor: pointer; user-select: none;
}
.tree-node .node-caret.leaf { visibility: hidden; cursor: default; }
.tree-node .node-ico { width: 14px; height: 14px; color: var(--text-dim); flex: none; }
.tree-node.selected .node-ico { color: var(--text); }
.tree-node .node-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-node .vis-toggle,
.tree-node .del-toggle {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 12px; padding: 0 2px; opacity: 0.6;
}
.tree-node .vis-toggle:hover { opacity: 1; }
/* The delete button stays discreet until you hover the row, then the icon itself. */
.tree-node .del-toggle { opacity: 0; font-size: 11px; }
.tree-node:hover .del-toggle,
.tree-node.hover .del-toggle { opacity: 0.55; }
.tree-node .del-toggle:hover { opacity: 1; filter: saturate(1.4) brightness(1.15); }
.tree-empty { color: var(--text-dim); font-size: 12px; padding: 14px 12px; text-align: center; line-height: 1.5; }

/* ===================== Viewport ===================== */
#viewport { position: relative; flex: 1; min-width: 0; }
#canvas { display: block; width: 100%; height: 100%; }

/* Bottom status bar: tips on the left, live coordinates on the right. */
#statusbar {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 12px;
  background: color-mix(in srgb, var(--bg-panel) 82%, transparent);
  border-top: 1px solid var(--border);
  backdrop-filter: blur(6px);
  pointer-events: none;
}

.coords {
  font-family: ui-monospace, "Cascadia Code", Consolas, monospace;
  font-size: 11px;
  color: var(--text-dim);
  white-space: nowrap;
}

/* ===================== Buttons ===================== */
.icon-btn {
  background: none; border: none; color: var(--text-dim);
  cursor: pointer; font-size: 16px; line-height: 1; padding: 2px 6px; border-radius: 4px;
}
.icon-btn:hover:not(:disabled) { color: var(--text); background: var(--bg-hover); }
.icon-btn:disabled { opacity: 0.32; cursor: default; }

/* ===================== Light theme =====================
   Applied when the user explicitly picks light (data-theme="light"), or — absent
   any explicit choice (:not([data-theme])) — when the OS prefers light. The theme
   toggle in the toolbar stamps data-theme on <html>, so an explicit pick always
   wins over the OS preference. */
:root[data-theme="light"] {
  --bg:          #eef0f3;
  --bg-panel:    #f7f8fa;
  --bg-elev:     #ffffff;
  --bg-hover:    #e6e9ee;
  --border:      #d4d8de;
  --text:        #1c2024;
  --text-dim:    #6b7280;
  --accent:      #2f6fb8;
  --accent-dim:  #cfe0f5;
  --shadow:      0 6px 20px rgba(0,0,0,0.15);
}
:root[data-theme="light"] .group-btn.active,
:root[data-theme="light"] .dropdown-item.active { color: #1c2024; }

@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg:          #eef0f3;
    --bg-panel:    #f7f8fa;
    --bg-elev:     #ffffff;
    --bg-hover:    #e6e9ee;
    --border:      #d4d8de;
    --text:        #1c2024;
    --text-dim:    #6b7280;
    --accent:      #2f6fb8;
    --accent-dim:  #cfe0f5;
    --shadow:      0 6px 20px rgba(0,0,0,0.15);
  }
  :root:not([data-theme]) .group-btn.active,
  :root:not([data-theme]) .dropdown-item.active { color: #1c2024; }
}
