:root {
  --bg: #19191b;
  --panel: #242427;
  --panel-2: #2b2b2f;
  --panel-3: #323237;
  --row-hover: #35353b;
  --border: #111113;
  --line: #3a3a40;
  --text: #d8d8dc;
  --text-dim: #8e8e96;
  --text-faint: #65656c;
  --accent: #4c82d8;
  --accent-2: #3b6bb5;
  --select: #ffa033;
  --select-bg: rgba(255, 160, 51, 0.16);
  --input: #1c1c1f;
  --danger: #e0625a;
  --x: #f5364f;
  --y: #8bdb00;
  --z: #2b8fff;
  --radius: 5px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 12px/1.35 var(--font); overflow: hidden; }
button, input, select { font: inherit; color: inherit; }
kbd {
  display: inline-block; padding: 0 4px; min-width: 16px; text-align: center; border-radius: 3px;
  background: var(--panel-3); border: 1px solid #45454c; border-bottom-width: 2px; font: 11px/15px var(--font);
}
.icon { width: 16px; height: 16px; flex: none; display: block; }
.grow { flex: 1; }
.hint { color: var(--text-dim); font-size: 11px; line-height: 1.45; }

#app {
  display: grid;
  grid-template-columns: 252px minmax(0, 1fr) 268px;
  grid-template-rows: 40px minmax(0, 1fr) 24px;
  height: 100vh;
}

/* ------------------------------------------------------------ top bar */

#topbar {
  grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; padding: 0 8px;
  background: var(--panel-2); border-bottom: 1px solid var(--border);
}
#topbar .home {
  display: grid; place-items: center; width: 26px; height: 26px; border-radius: 6px; text-decoration: none;
  background: linear-gradient(135deg, #ffa033, #e0625a); color: #1b1b1d; font-weight: 800; font-size: 11px;
}
.app-title { font-weight: 600; color: #ececf0; margin-right: 4px; white-space: nowrap; }
.menubar { display: flex; }
.menubar button {
  background: none; border: 0; padding: 5px 9px; border-radius: 4px; cursor: pointer; color: var(--text);
}
.menubar button:hover, .menubar button.open { background: var(--panel-3); }
.doc-box { display: flex; align-items: center; gap: 8px; margin-left: 6px; min-width: 0; }
.doc-name {
  background: transparent; border: 1px solid transparent; border-radius: 4px; padding: 4px 6px; width: 180px;
  color: #f2f2f4; font-weight: 500;
}
.doc-name:hover { border-color: var(--line); }
.doc-name:focus { outline: none; border-color: var(--accent); background: var(--input); }
.save-state { color: var(--text-dim); font-size: 11px; white-space: nowrap; }
.save-state.dirty { color: #e8b36a; }
.res { display: flex; align-items: center; gap: 6px; color: var(--text-dim); }

.icon-btn {
  display: inline-grid; place-items: center; width: 26px; height: 26px; border: 0; border-radius: 4px;
  background: none; color: var(--text); cursor: pointer; padding: 0; flex: none;
}
.icon-btn:hover:not(:disabled) { background: var(--panel-3); }
.icon-btn:disabled { opacity: 0.35; cursor: default; }
.icon-btn.danger:hover { color: var(--danger); }

.select {
  background: var(--input); border: 1px solid var(--line); border-radius: 4px; padding: 3px 6px; height: 24px;
}
.select:focus { outline: none; border-color: var(--accent); }
.text-input {
  background: var(--input); border: 1px solid var(--line); border-radius: 4px; padding: 4px 6px; width: 100%; min-width: 0;
}
.text-input:focus { outline: none; border-color: var(--accent); }
.text-input.wide { width: 320px; max-width: 100%; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 26px; padding: 0 10px;
  background: var(--panel-3); border: 1px solid #44444b; border-radius: 4px; cursor: pointer; white-space: nowrap;
}
.btn:hover { background: #3c3c43; }
.btn.primary { background: var(--accent-2); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent); }
.btn.danger { color: #ffb3ad; }
.btn.danger:hover { background: #5a2b29; border-color: var(--danger); }
.btn.small { height: 22px; font-size: 11px; margin: 6px; }

.segmented { display: flex; background: var(--input); border: 1px solid var(--line); border-radius: 5px; padding: 1px; gap: 1px; }
.segmented button {
  flex: 1; display: flex; align-items: center; justify-content: center; gap: 5px; border: 0; background: none;
  padding: 3px 8px; border-radius: 4px; cursor: pointer; color: var(--text-dim); white-space: nowrap; min-width: 0;
}
.segmented button:hover { color: var(--text); background: var(--panel-3); }
.segmented button.active { background: var(--accent-2); color: #fff; }
.segmented.mode button { padding: 3px 12px; }
.segmented.ops { display: grid; grid-template-columns: 1fr 1fr; }
.segmented.ops button { padding: 4px 4px; font-size: 11.5px; justify-content: flex-start; padding-left: 10px; }
.segmented.ops button .icon { width: 13px; height: 13px; }

/* ------------------------------------------------------------ panels */

#left, #right { display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
#left { border-right: 1px solid var(--border); }
#right { border-left: 1px solid var(--border); }
.panel { display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.panel-head {
  display: flex; align-items: center; gap: 6px; height: 32px; padding: 0 6px 0 10px; flex: none;
  background: var(--panel-2); border-bottom: 1px solid var(--border); color: #e6e6ea;
}
.panel-head.clickable { cursor: pointer; user-select: none; }
.panel-head.clickable:hover { background: #303035; }
.panel-head .icon { opacity: 0.8; }
.panel-head .chev .icon { width: 12px; height: 12px; }
.panel-title { font-weight: 600; }
.panel-head .count { color: var(--text-faint); font-size: 11px; }
.panel-scroll { overflow-y: auto; min-height: 0; flex: 1; }

#tools { flex: 1 1 auto; min-height: 120px; }
#tools.disabled .panel-scroll { opacity: 0.45; pointer-events: none; }
#history { flex: 0 0 auto; max-height: 45%; border-top: 1px solid var(--border); }
#history.collapsed { max-height: 32px; }
#history.collapsed .history-list { display: none; }

.section { padding: 8px 10px 10px; border-bottom: 1px solid #1e1e21; }
.section-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); margin-bottom: 7px; font-weight: 600; }
.section-body { display: flex; flex-direction: column; gap: 6px; }

.tool-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.tool {
  display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 7px 2px 5px; border: 1px solid transparent;
  background: var(--panel-2); border-radius: 5px; cursor: pointer; color: var(--text-dim); font-size: 10.5px;
}
.tool .icon { width: 20px; height: 20px; color: #cfd2d8; }
.tool:hover:not(:disabled) { background: var(--panel-3); color: var(--text); }
.tool.active { background: var(--accent-2); border-color: var(--accent); color: #fff; }
.tool.active .icon { color: #fff; }
.tool:disabled { opacity: 0.4; cursor: default; }
.tool-hint { padding: 10px; color: var(--text-dim); font-size: 11px; line-height: 1.5; }

.with-key { display: flex; gap: 5px; align-items: center; }
.with-key .num { flex: 1; }
.key-chip {
  width: 24px; height: 22px; flex: none; border-radius: 4px; background: var(--panel-3); border: 1px solid #47474e;
  border-bottom-width: 2px; font: 600 11px var(--font); cursor: pointer; padding: 0;
}
.key-chip:hover:not(:disabled) { border-color: var(--accent); }
.key-chip:disabled { cursor: default; opacity: 0.8; }

.toggle-row { display: flex; gap: 4px; align-items: center; }
.mini-label { color: var(--text-dim); margin-right: 4px; min-width: 60px; }
.toggle {
  flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 4px; height: 24px;
  background: var(--input); border: 1px solid var(--line); border-radius: 4px; cursor: pointer; font-weight: 600; color: var(--text-dim);
}
.toggle:hover { color: var(--text); border-color: #55555c; }
.toggle.active { color: #fff; background: #3a3a41; }
.toggle.axis-x.active { background: rgba(245, 54, 79, 0.35); border-color: var(--x); }
.toggle.axis-y.active { background: rgba(139, 219, 0, 0.25); border-color: var(--y); }
.toggle.axis-z.active { background: rgba(43, 143, 255, 0.3); border-color: var(--z); }

.check { display: flex; align-items: center; gap: 7px; cursor: pointer; user-select: none; }
.check input { accent-color: var(--accent); margin: 0; width: 14px; height: 14px; }
.check.disabled { opacity: 0.45; cursor: default; }

.prop-row { display: grid; grid-template-columns: 62px minmax(0, 1fr); align-items: center; gap: 8px; }
.prop-label { color: var(--text-dim); }

/* number field */
.num {
  position: relative; display: flex; align-items: center; justify-content: space-between; gap: 6px; height: 24px;
  padding: 0 8px; background: var(--input); border: 1px solid var(--line); border-radius: 4px; cursor: ew-resize;
  user-select: none; min-width: 0; overflow: hidden;
}
.num:hover { border-color: #55555c; background: #202024; }
.num.scrubbing { border-color: var(--accent); }
.num.editing { cursor: text; border-color: var(--accent); padding: 0; }
.num-label { color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.num-value { font-variant-numeric: tabular-nums; white-space: nowrap; }
.num-input { width: 100%; height: 100%; background: transparent; border: 0; padding: 0 8px; outline: none; }
.vec { display: grid; grid-template-columns: 1fr; gap: 2px; margin-bottom: 4px; }
.vec .num { border-radius: 0; }
.vec .num:first-child { border-radius: 4px 4px 0 0; }
.vec .num:last-child { border-radius: 0 0 4px 4px; }
.num.axis-x .num-label { color: #ff7d8f; }
.num.axis-y .num-label { color: #b4ec5c; }
.num.axis-z .num-label { color: #74b6ff; }
.vec-label { color: var(--text-dim); font-size: 11px; margin: 2px 0 1px; }

/* color */
.color-field { display: flex; flex-direction: column; gap: 6px; }
.color-row { display: flex; gap: 6px; }
.color-input {
  width: 44px; height: 26px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: var(--input); cursor: pointer;
}
.color-input::-webkit-color-swatch-wrapper { padding: 2px; }
.color-input::-webkit-color-swatch { border: 0; border-radius: 3px; }
.color-input::-moz-color-swatch { border: 0; border-radius: 3px; }
.color-hex { flex: 1; background: var(--input); border: 1px solid var(--line); border-radius: 4px; padding: 0 8px; font-variant-numeric: tabular-nums; min-width: 0; }
.color-hex:focus { outline: none; border-color: var(--accent); }
.swatches { display: grid; grid-template-columns: repeat(10, 1fr); gap: 3px; }
.swatch { aspect-ratio: 1; border: 1px solid rgba(0, 0, 0, 0.5); border-radius: 3px; cursor: pointer; padding: 0; }
.swatch:hover { transform: scale(1.15); }
.swatch.active { outline: 2px solid #fff; outline-offset: 0; }

/* ------------------------------------------------------------ outliner */

#outliner { flex: 1; }
.outliner-list { flex: 1; overflow: auto; padding: 4px 0 40px; min-height: 0; }
.outliner-list.drop-root { box-shadow: inset 0 -2px 0 var(--accent); }
.outliner-empty { color: var(--text-dim); padding: 18px 14px; line-height: 1.6; text-align: center; }
.row {
  position: relative; display: flex; align-items: center; gap: 4px; height: 24px; padding-right: 4px; cursor: pointer;
  white-space: nowrap; user-select: none;
}
.row:hover, .row.hover { background: var(--row-hover); }
.row.selected { background: var(--select-bg); box-shadow: inset 2px 0 0 var(--select); }
.row.selected .name { color: #ffd29a; }
.row.is-hidden { opacity: 0.45; }
.row.dragging { opacity: 0.4; }
.row.drop-before { box-shadow: inset 0 2px 0 var(--accent); }
.row.drop-after { box-shadow: inset 0 -2px 0 var(--accent); }
.row.drop-inside { background: rgba(76, 130, 216, 0.25); outline: 1px solid var(--accent); outline-offset: -1px; }
.twisty { width: 14px; height: 14px; display: grid; place-items: center; color: var(--text-dim); flex: none; }
.twisty .icon { width: 11px; height: 11px; }
.twisty.leaf { visibility: hidden; }
.row .type { color: #b8c4d6; flex: none; }
.row .type .icon { width: 14px; height: 14px; }
.op { width: 14px; height: 14px; display: grid; place-items: center; font-weight: 700; font-size: 13px; line-height: 1; flex: none; border-radius: 3px; }
.op .icon { width: 11px; height: 11px; }
.op-add { color: #8fd48f; }
.op-subtract { color: #ff8a80; }
.op-intersect { color: #c3a6ff; }
.op-paint { color: #ffd166; }
.dot { width: 9px; height: 9px; border-radius: 50%; flex: none; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5); }
.row .name { flex: 1; overflow: hidden; text-overflow: ellipsis; padding-left: 2px; }
.badges { display: flex; gap: 3px; color: var(--text-dim); }
.badge { display: flex; align-items: center; gap: 1px; font-size: 9px; }
.badge .icon { width: 12px; height: 12px; }
.badge.bone { color: #9fd0ff; }
.vis { width: 20px; height: 20px; border: 0; background: none; color: var(--text-dim); cursor: pointer; display: grid; place-items: center; padding: 0; opacity: 0.55; }
.row:hover .vis, .row.is-hidden .vis { opacity: 1; }
.vis:hover { color: #fff; }
.vis .icon { width: 14px; height: 14px; }
.rename { flex: 1; min-width: 0; background: var(--input); border: 1px solid var(--accent); border-radius: 3px; padding: 1px 4px; outline: none; }

/* ------------------------------------------------------------ history */

.history-list { overflow-y: auto; min-height: 0; padding: 2px 0 6px; }
.hrow { display: flex; align-items: center; height: 22px; padding-right: 8px; cursor: pointer; gap: 4px; }
.hrow:hover { background: var(--row-hover); }
.hrow.current { background: rgba(76, 130, 216, 0.2); }
.hrow.current .hlabel { color: #fff; font-weight: 600; }
.hrow.off-path .hlabel { color: var(--text-faint); font-style: italic; }
.graph { flex: none; display: block; height: 22px; }
.graph svg { display: block; }
.graph line, .graph path { stroke: #5e6a7c; stroke-width: 1.5; }
.graph circle { fill: #7d8a9e; }
.graph circle.past { fill: #9eb3d1; }
.graph circle.other { fill: #555a63; }
.graph circle.cur { fill: var(--select); stroke: #fff; stroke-width: 1.2; }
.hlabel { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.htime { color: var(--text-faint); font-size: 10.5px; white-space: nowrap; }

/* ------------------------------------------------------------ viewport */

#viewport { position: relative; overflow: hidden; overflow: clip; background: #202124; min-width: 0; }
#viewport canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#viewport canvas.overlay { outline: none; touch-action: none; }
.busy {
  position: absolute; left: 14px; bottom: 12px; width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.15); border-top-color: var(--select); opacity: 0; transition: opacity 0.2s;
  pointer-events: none; animation: spin 0.8s linear infinite;
}
.busy.on { opacity: 1; transition-delay: 0.15s; }
@keyframes spin { to { transform: rotate(360deg); } }

.props {
  position: absolute; top: 0; right: 0; bottom: 0; width: 292px; z-index: 5; border-left: 1px solid var(--border);
  box-shadow: -12px 0 24px rgba(0, 0, 0, 0.28); transform: translateX(105%); visibility: hidden;
  transition: transform 0.18s ease-out, visibility 0s linear 0.18s;
  background: rgba(36, 36, 39, 0.97);
}
.props.open { transform: none; visibility: visible; transition: transform 0.18s ease-out, visibility 0s; }
.props-title { display: flex; align-items: center; gap: 6px; font-weight: 600; }
.props-body { overflow-y: auto; flex: 1; min-height: 0; padding-bottom: 30px; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; padding: 0 6px; }

/* ------------------------------------------------------------ status bar */

#statusbar {
  grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 0 10px;
  background: var(--panel-2); border-top: 1px solid var(--border); color: var(--text-dim); font-size: 11px; white-space: nowrap;
}
#status-left { overflow: hidden; text-overflow: ellipsis; }
#status-right { font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ menus */

.menu-popup {
  position: fixed; z-index: 100; min-width: 200px; padding: 4px; background: #2a2a2e; border: 1px solid #111;
  border-radius: 6px; box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}
.menu-item {
  display: grid; grid-template-columns: 18px 1fr auto; align-items: center; gap: 8px; width: 100%; height: 26px;
  padding: 0 8px; border: 0; background: none; border-radius: 4px; cursor: pointer; text-align: left;
}
.menu-item:hover:not(.disabled) { background: var(--accent-2); color: #fff; }
.menu-item.disabled { opacity: 0.4; cursor: default; }
.menu-check { display: grid; place-items: center; color: var(--select); }
.menu-check .icon { width: 14px; height: 14px; color: var(--text-dim); }
.menu-item:hover .menu-check .icon { color: #fff; }
.menu-shortcut { color: var(--text-faint); font-size: 11px; }
.menu-item:hover .menu-shortcut { color: #dbe6ff; }
.menu-sep { height: 1px; background: #3a3a40; margin: 4px 2px; }
.menu-header { padding: 5px 8px 3px; font-size: 11px; color: var(--text-dim); font-weight: 600; }

/* ------------------------------------------------------------ dialogs */

.dialog-backdrop { position: fixed; inset: 0; z-index: 200; display: grid; place-items: center; background: rgba(0, 0, 0, 0.5); padding: 20px; }
.dialog {
  display: flex; flex-direction: column; max-height: 100%; min-width: 320px; max-width: 520px; background: var(--panel);
  border: 1px solid #0d0d0e; border-radius: 8px; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.6);
}
.dialog.wide { width: 860px; max-width: 100%; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 10px 16px; border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin: 0; font-size: 14px; font-weight: 600; }
.dialog-body { padding: 14px 16px; overflow: auto; min-height: 0; }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 10px 16px 14px; }
.dialog-text { margin: 0; line-height: 1.5; }
.field-block { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field-block > span { color: var(--text-dim); }
.form .select { height: 28px; }

.tabs { display: flex; gap: 4px; margin-bottom: 12px; }
.tab { border: 0; background: none; padding: 6px 12px; border-radius: 5px; cursor: pointer; color: var(--text-dim); font-weight: 600; }
.tab:hover { color: var(--text); background: var(--panel-3); }
.tab.active { background: var(--accent-2); color: #fff; }
.library { min-height: 200px; }
.library-empty { color: var(--text-dim); text-align: center; padding: 50px 20px; line-height: 1.7; }
.card-group { margin: 14px 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); }
.card-group:first-child { margin-top: 0; }
.card-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.card {
  position: relative; display: flex; flex-direction: column; background: var(--panel-2); border: 1px solid #1a1a1c;
  border-radius: 7px; overflow: hidden; cursor: pointer; transition: border-color 0.12s, transform 0.12s;
}
.card:hover, .card:focus { border-color: var(--accent); outline: none; transform: translateY(-1px); }
.card.current { border-color: var(--select); }
.thumb { aspect-ratio: 4 / 3; background: #202124; display: grid; place-items: center; overflow: hidden; }
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb-empty .icon { width: 40px; height: 40px; color: #55555d; }
.card-info { padding: 7px 9px 9px; }
.card-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { color: var(--text-dim); font-size: 11px; margin-top: 2px; }
.card-actions { position: absolute; top: 6px; right: 6px; display: flex; gap: 2px; opacity: 0; transition: opacity 0.12s; }
.card:hover .card-actions { opacity: 1; }
.card-actions .icon-btn { background: rgba(20, 20, 22, 0.85); width: 24px; height: 24px; }

.help { display: grid; grid-template-columns: repeat(auto-fit, minmax(360px, 1fr)); gap: 6px 24px; }
.help h3 { margin: 6px 0 6px; font-size: 12px; color: var(--select); }
.help table { border-collapse: collapse; width: 100%; }
.help td { padding: 3px 0; vertical-align: top; }
.help td.keys { color: #e7e7ea; font-weight: 600; width: 46%; padding-right: 10px; }
.help-group { margin-bottom: 8px; }
.about { line-height: 1.6; max-width: 720px; }
.about li { margin-bottom: 4px; }

#toast {
  position: fixed; left: 50%; bottom: 38px; transform: translate(-50%, 20px); z-index: 300; padding: 8px 14px;
  background: #2f3136; border: 1px solid #45474e; border-radius: 6px; color: #eee; opacity: 0; pointer-events: none;
  transition: opacity 0.2s, transform 0.2s; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4); max-width: 80vw;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { border-color: var(--danger); color: #ffc9c4; }

.fatal { padding: 40px; max-width: 600px; margin: 0 auto; grid-column: 1 / -1; }

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #3b3b41; border-radius: 5px; border: 2px solid var(--panel); }
::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 1100px) {
  #app { grid-template-columns: 220px minmax(0, 1fr) 230px; }
  .app-title { display: none; }
  .doc-name { width: 130px; }
}
