/* Dark, 3ds-Max-like theme. Colors are tokens so a light theme can be added
   later by redefining them. */
:root {
  --bg: #26282c;
  --panel: #383b40;
  --panel-2: #44474d;
  --panel-3: #50545b;
  --field: #232528;
  --border: #1c1d20;
  --text: #dcdee1;
  --text-dim: #9ca1a9;
  --accent: #5aaee0;
  --accent-ink: #0d1820;
  --active-vp: #e8c24e;
  --danger: #e0715d;
  --ok: #7cc46b;
  --x: #e0605a;
  --y: #74c25a;
  --z: #5a8de0;
  --radius: 6px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  display: grid;
  grid-template-columns: 46px 1fr;
  background: var(--bg);
  color: var(--text);
  font: 12px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

button { font: inherit; color: inherit; }

/* Containers for floating UI; fixed so they take no space in the layout,
   and stacked above the stage (whose own z-indexes stay inside it). */
#popups, #menus { position: fixed; top: 0; left: 0; width: 0; height: 0; }
#popups { z-index: 100; }
#menus { z-index: 1000; }

/* --- toolbar ------------------------------------------------------------- */

#toolbar {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 4px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  scrollbar-width: none;
  z-index: 10;
}

.tb-btn {
  position: relative;
  flex: none;
  width: 36px;
  height: 34px;
  border: 0;
  border-radius: var(--radius);
  background: transparent;
  font-size: 15px;
  cursor: pointer;
}
.tb-btn:hover { background: var(--panel-2); }
.tb-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.tb-btn.active { background: var(--accent); color: var(--accent-ink); }
.tb-btn:disabled { opacity: 0.35; cursor: default; background: transparent; }
.tb-btn.has-menu::after {
  content: "";
  position: absolute;
  right: 3px;
  bottom: 3px;
  border: 3px solid transparent;
  border-right-color: currentColor;
  border-bottom-color: currentColor;
  opacity: 0.6;
}
.tb-sep { flex: none; width: 28px; height: 1px; margin: 5px 0; background: var(--border); }

/* --- viewports ----------------------------------------------------------- */

#stage { position: relative; overflow: hidden; min-width: 0; isolation: isolate; }

#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }

#viewports {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 3px;
}
#viewports.maximized { grid-template-columns: 1fr; grid-template-rows: 1fr; }
#viewports.maximized .vp:not(.max) { display: none; }

.vp { position: relative; min-width: 0; min-height: 0; }
.vp.active { outline: 2px solid var(--active-vp); outline-offset: -2px; z-index: 1; }
.vp-input { position: absolute; inset: 0; touch-action: none; }
.vp-input.crosshair { cursor: crosshair; }

.vp-label {
  position: absolute;
  top: 5px;
  left: 6px;
  display: flex;
  gap: 3px;
  z-index: 2;
}
.vp-label button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 0;
  border-radius: 4px;
  padding: 2px 8px;
  background: rgba(18, 19, 22, 0.55);
  cursor: pointer;
}
.vp-label button:hover { background: rgba(18, 19, 22, 0.85); }
.vp-label .vp-max { padding: 2px 6px; color: var(--text-dim); }
.vp-label .vp-max:hover { color: var(--text); }
.vp-label .dim { color: var(--text-dim); }

.marquee {
  position: absolute;
  border: 1px dashed #fff;
  background: rgba(255, 255, 255, 0.06);
  pointer-events: none;
  z-index: 2;
}

#hint {
  position: absolute;
  left: 50%;
  bottom: 8px;
  transform: translateX(-50%);
  padding: 3px 10px;
  border-radius: 12px;
  background: rgba(18, 19, 22, 0.7);
  color: var(--text);
  pointer-events: none;
  z-index: 3;
  white-space: nowrap;
}
#hint:empty { display: none; }

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

.menu {
  position: fixed;
  min-width: 190px;
  padding: 4px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.5);
  z-index: 1000;
}
.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 5px 8px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--panel-2); outline: none; }
.menu-item:disabled { opacity: 0.4; cursor: default; background: transparent; }
.menu-item .ico { width: 16px; text-align: center; color: var(--text-dim); }
.menu-item .chk { width: 12px; color: var(--accent); }
.menu-item kbd { margin-left: auto; color: var(--text-dim); font: inherit; font-size: 11px; }
.menu-sep { height: 1px; margin: 4px 2px; background: var(--border); }
.menu-title {
  padding: 4px 8px 2px;
  color: var(--text-dim);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

/* --- popups -------------------------------------------------------------- */

.popup {
  position: fixed;
  display: flex;
  flex-direction: column;
  width: 270px;
  max-height: calc(100vh - 16px);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.5);
  z-index: 100;
}
.popup[hidden] { display: none; }
.popup.wide { width: 340px; }
.popup-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 6px 5px 10px;
  background: var(--panel-2);
  border-radius: 8px 8px 0 0;
  cursor: move;
  touch-action: none;
}
.popup-head .ico { color: var(--text-dim); }
.popup-head h2 { flex: 1; margin: 0; font-size: 12px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.popup-close {
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
}
.popup-close:hover { background: var(--panel-3); color: var(--text); }
.popup-body { padding: 8px 10px 10px; overflow: auto; user-select: text; -webkit-user-select: text; }

/* --- form widgets -------------------------------------------------------- */

.row {
  display: grid;
  grid-template-columns: 1fr 120px;
  align-items: center;
  gap: 6px;
  margin: 3px 0;
}
.row > label, .lbl { color: var(--text-dim); }
.lbl-row { display: flex; justify-content: space-between; align-items: baseline; }
.link-btn { padding: 0; border: 0; background: none; color: var(--accent); font-size: 11px; cursor: pointer; }
.link-btn:hover { text-decoration: underline; }
.link-btn[hidden] { display: none; }
.lock-btn { margin-left: 4px; padding: 0 3px; border: 0; background: none; color: var(--text-dim); font-size: 10px; cursor: pointer; }
.lock-btn:hover { color: var(--text); }
.lock-btn[aria-pressed="true"] { color: var(--accent); }
.row.full { grid-template-columns: 1fr; }
.row.choice { grid-template-columns: 64px 1fr; }
.row.choice .seg > .btn { padding: 4px 2px; min-width: 0; }

input[type="number"], input[type="text"], select, textarea {
  width: 100%;
  padding: 3px 6px;
  background: var(--field);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  font: inherit;
}
input[type="number"] { font-variant-numeric: tabular-nums; }
input:focus, select:focus, textarea:focus { outline: 1px solid var(--accent); outline-offset: 0; }
textarea { resize: vertical; min-height: 54px; }
input[type="color"] {
  width: 34px;
  height: 22px;
  padding: 0 2px;
  background: var(--field);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}
input[type="checkbox"] { accent-color: var(--accent); }
input[type="range"] { flex: 1; min-width: 0; accent-color: var(--accent); }

.scrub { cursor: ew-resize; }
.scrub:hover { color: var(--text); }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 4px 9px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--panel-3); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.danger:hover { background: var(--danger); color: #1b0d0a; }
.btn-row { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }
.btn-row.fill > .btn { flex: 1; }
.op-row { align-items: center; flex-wrap: nowrap; margin: 4px 0; }
.op-row > .btn { flex: none; }
.op-amount { width: 52px !important; flex: none; }
.op-field { display: flex; align-items: center; gap: 4px; color: var(--text-dim); font-size: 11px; }
.op-field > span { cursor: ew-resize; }
.op-row > .op-field { flex: 1 1 0; min-width: 0; }
.op-field > .op-amount { flex: 1 1 auto; min-width: 0; width: auto !important; }
.op-main { min-width: 80px; justify-content: center; }
.op-field > .op-amount { appearance: textfield; -moz-appearance: textfield; }
.op-field > .op-amount::-webkit-inner-spin-button, .op-field > .op-amount::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.op-mode { margin: -1px 0 8px; }
.op-mode > .btn { padding: 2px 4px; min-width: 0; font-size: 11px; }
.seg { display: flex; margin: 4px 0; }
.seg > .btn { flex: 1; border-radius: 0; }
.seg > .btn:first-child { border-radius: 4px 0 0 4px; }
.seg > .btn:last-child { border-radius: 0 4px 4px 0; }
.seg > .btn + .btn { border-left: 0; }

.section {
  margin: 10px -10px 0;
  padding: 8px 10px 0;
  border-top: 1px solid var(--border);
}
.section-title {
  margin: 0 0 4px;
  color: var(--text-dim);
  font-size: 10px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.muted { color: var(--text-dim); }
.small { font-size: 11px; }

.vec3 { display: grid; grid-template-columns: repeat(3, 12px 1fr); gap: 3px 4px; align-items: center; }
.ax-x { color: var(--x); font-weight: 600; }
.ax-y { color: var(--y); font-weight: 600; }
.ax-z { color: var(--z); font-weight: 600; }

.name-row { display: flex; gap: 6px; align-items: center; }
.modifier { margin: 0 0 8px; padding: 4px 8px 6px; background: var(--panel-2); border-radius: var(--radius); }
.modifier-head { display: flex; align-items: center; gap: 6px; margin-bottom: 2px; }
.modifier-head strong { flex: 1; font-weight: 600; }
.name-row input[type="text"] { flex: 1; }
.type-tag { color: var(--text-dim); font-size: 11px; margin-top: 2px; }

/* --- scene list ----------------------------------------------------------- */

.scene-list { max-height: min(55vh, 520px); overflow-y: auto; margin: 6px -4px 0; }
.list-row {
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  padding: 3px 6px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.list-row:hover { background: var(--panel-2); }
.list-row.on { background: var(--accent); color: var(--accent-ink); }
.list-row .ico { color: inherit; opacity: 0.75; }
.list-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-type { color: var(--text-dim); font-size: 11px; }
.list-row.on .list-type { color: inherit; opacity: 0.7; }
.swatch { flex: none; width: 10px; height: 10px; border-radius: 2px; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.4); }

/* --- AI ------------------------------------------------------------------ */

.chat { display: flex; flex-direction: column; gap: 6px; max-height: 280px; overflow-y: auto; margin-bottom: 8px; }
.chat:empty { display: none; }
.msg { max-width: 92%; padding: 5px 8px; border-radius: 8px; white-space: pre-wrap; }
.msg.user { align-self: flex-end; background: #2e4a5e; }
.msg.assistant { align-self: flex-start; background: var(--panel-2); }
.msg.question { border-left: 3px solid var(--active-vp); }
.msg.error { align-self: flex-start; background: #4a2a26; }
.msg .meta { display: block; margin-top: 3px; color: var(--text-dim); font-size: 10px; }
.msg ul { margin: 4px 0 0; padding-left: 16px; }
.ai-status { display: flex; align-items: center; gap: 8px; margin-top: 6px; color: var(--text-dim); }
.ai-status:empty { display: none; }

.spin { display: inline-block; width: 12px; height: 12px; border: 2px solid var(--text-dim); border-right-color: transparent; border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* --- toasts -------------------------------------------------------------- */

#toasts {
  position: fixed;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  z-index: 2000;
  pointer-events: none;
}
.toast {
  padding: 6px 12px;
  background: #1d1f22;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  pointer-events: auto;
}
.toast.error { border-color: var(--danger); }
.toast.ok { border-color: var(--ok); }
.toast button { margin-left: 10px; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: var(--panel-3); border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

@media (prefers-reduced-motion: reduce) {
  .spin { animation-duration: 3s; }
}
