:root {
  --bg: #0a0e14;
  --bg2: #0f141d;
  --panel: #121822;
  --panel2: #18202c;
  --line: #232c3a;
  --txt: #dfe6f0;
  --mut: #8a96a8;
  --accent: #35e0c8;
  --accent2: #4a9eff;
  --danger: #ff5277;
  --warn: #ffb454;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--txt);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
}
.hidden { display: none !important; }

/* ---------- GATE ---------- */
.gate {
  position: fixed; inset: 0; z-index: 100;
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 50% 30%, #131c2a, #070a0f);
}
.gate-card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 34px 30px; width: 320px; text-align: center;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.gate-logo { font-size: 46px; color: var(--accent); }
.gate-title { font-size: 26px; font-weight: 800; letter-spacing: 3px; margin-top: 6px; }
.gate-sub { color: var(--mut); font-size: 12px; margin: 6px 0 18px; }
.gate-input {
  width: 100%; padding: 12px; border-radius: 10px; text-align: center; font-size: 18px;
  background: var(--bg2); border: 1px solid var(--line); color: var(--txt);
}
.gate-btn {
  width: 100%; margin-top: 14px; padding: 12px; border: 0; border-radius: 10px;
  background: var(--accent); color: #04201c; font-weight: 700; font-size: 15px; cursor: pointer;
}
.gate-err { color: var(--danger); font-size: 12px; height: 16px; margin-top: 8px; }

/* ---------- APP ---------- */
.app { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  background: var(--bg2); border-bottom: 1px solid var(--line); flex: 0 0 auto;
}
.brand { font-weight: 800; letter-spacing: 2px; display: flex; align-items: center; gap: 6px; }
.brand-logo { color: var(--accent); font-size: 18px; }
.piece-name {
  background: var(--panel); border: 1px solid var(--line); color: var(--txt);
  border-radius: 8px; padding: 6px 10px; width: 200px;
}
.spacer { flex: 1; }
.sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }
.btn {
  background: var(--panel); border: 1px solid var(--line); color: var(--txt);
  border-radius: 8px; padding: 7px 11px; cursor: pointer; font-size: 13px; white-space: nowrap;
}
.btn:hover { background: var(--panel2); }
.btn.primary { background: var(--accent); color: #04201c; border-color: var(--accent); font-weight: 700; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.select { padding-right: 6px; }

/* ---------- LAYOUT ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.pane { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--bg); }
.pane-head {
  flex: 0 0 auto; padding: 8px 12px; font-size: 12px; color: var(--mut);
  background: var(--bg2); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
}
.pane-head .hint { color: var(--accent); }
.viewer-pane { flex: 1; }

/* ---------- PANEL LATERAL CON PESTAÑAS ---------- */
.side-pane { width: 360px; flex: 0 0 auto; border-right: 1px solid var(--line); }
.tabbar { flex: 0 0 auto; display: flex; background: var(--bg2); border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; padding: 10px 8px; background: transparent; border: 0;
  border-bottom: 2px solid transparent; color: var(--mut);
  cursor: pointer; font-size: 13px; font-weight: 600; white-space: nowrap;
}
.tab:hover { color: var(--txt); background: var(--panel); }
.tab.on { color: var(--txt); background: var(--panel); border-bottom-color: var(--accent); }
.tab .hint { color: var(--accent); font-weight: 400; }
.tab-body { flex: 1; display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-sub { flex: 0 0 auto; padding: 6px 8px; border-bottom: 1px solid var(--line); background: var(--bg2); }
#tab-code { min-width: 0; }
#tab-code .CodeMirror { flex: 1; height: auto; font-size: 13px; }

.splitter { width: 5px; cursor: col-resize; background: var(--line); flex: 0 0 auto; }
.splitter:hover { background: var(--accent); }

/* ---------- CHAT ---------- */
.chat-log { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.msg { padding: 9px 12px; border-radius: 12px; max-width: 92%; white-space: pre-wrap; word-wrap: break-word; }
.msg.bot { background: var(--panel); border: 1px solid var(--line); align-self: flex-start; }
.msg.user { background: var(--accent2); color: #021428; align-self: flex-end; }
.msg.err { background: #2a151c; border: 1px solid var(--danger); color: #ffd1da; align-self: flex-start; }
.msg em { color: var(--accent); font-style: normal; }
.msg a { color: var(--accent2); text-decoration: underline; word-break: break-all; }
.prog-label { color: var(--mut); font-size: 13px; }
.progress { height: 4px; background: var(--panel2); border-radius: 3px; overflow: hidden; margin-top: 7px; }
.progress::before { content: ""; display: block; height: 100%; width: 38%; border-radius: 3px; background: var(--accent); animation: prog 1.1s ease-in-out infinite; }
@keyframes prog { 0% { transform: translateX(-110%); } 100% { transform: translateX(300%); } }
.chat-attach { flex: 0 0 auto; display: flex; align-items: center; gap: 8px; padding: 6px 10px; border-top: 1px solid var(--line); background: var(--bg2); }
.chat-attach img { height: 42px; width: 42px; object-fit: cover; border-radius: 6px; border: 1px solid var(--line); }
.chat-attach .cap { color: var(--mut); font-size: 12px; flex: 1; }
.chat-attach .rm { background: none; border: 0; color: var(--danger); cursor: pointer; font-size: 15px; }
.tab-body.dragging { outline: 2px dashed var(--accent); outline-offset: -6px; }

/* Barra de herramientas flotante del visor 3D */
.viewer-tools { position: absolute; top: 8px; left: 8px; z-index: 8; display: flex; gap: 4px; flex-wrap: wrap; align-items: center; max-width: 76%; }
.viewer-tools .tool { padding: 6px 9px; font-size: 14px; line-height: 1; background: rgba(18,24,34,.9); }
.viewer-tools .tool.on { background: var(--accent); color: #04201c; border-color: var(--accent); }
.viewer-tools .tool.danger { color: var(--danger); }
.viewer-tools .tsep { width: 1px; height: 20px; background: var(--line); margin: 0 2px; }
.ops-body { padding: 6px 14px 12px; overflow-y: auto; }
.ops-row { padding: 10px 0; border-bottom: 1px solid var(--line); }
.ops-row b { display: block; margin-bottom: 6px; font-size: 13px; }
.ops-fields { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ops-fields label { color: var(--mut); font-size: 12px; display: flex; align-items: center; gap: 4px; }
.ops-fields input { width: 64px; padding: 5px 7px; background: var(--bg2); border: 1px solid var(--line); border-radius: 6px; color: var(--txt); }
.ops-fields select { padding: 5px 7px; background: var(--bg2); border: 1px solid var(--line); border-radius: 6px; color: var(--txt); }
.ops-fields .btn { margin-left: auto; }

/* Barra de croquis (estilo SolidWorks) */
.sketch-bar { position: absolute; top: 8px; left: 50%; transform: translateX(-50%); z-index: 9; display: flex; gap: 4px; align-items: center; background: rgba(10,14,20,.9); padding: 5px 7px; border: 1px solid var(--line); border-radius: 10px; flex-wrap: wrap; max-width: 92%; }
.sketch-bar .sk-title { color: var(--accent); font-size: 12px; font-weight: 700; margin-right: 4px; }
.sketch-bar .tool { padding: 6px 9px; }
.sketch-bar .tool.on { background: var(--accent); color: #04201c; border-color: var(--accent); }
.sketch-bar .tool.sk-ok { color: var(--accent); }

/* Iconos SVG inline (reemplazan emojis) */
.ic { width: 1.05em; height: 1.05em; display: inline-block; vertical-align: -0.16em; stroke: currentColor; fill: none; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: 0 0 auto; }
.brand .ic { color: var(--accent); width: 1.2em; height: 1.2em; }
.gate-logo .ic { width: 46px; height: 46px; color: var(--accent); }
.viewer-tools .tool .ic, #btn-undo .ic, #btn-redo .ic, .modal-head .x .ic { width: 18px; height: 18px; }
.chat-input { flex: 0 0 auto; display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); background: var(--bg2); }
.chat-input textarea {
  flex: 1; resize: none; height: 46px; background: var(--panel); color: var(--txt);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; font-family: inherit;
}
.chat-input .btn { align-self: stretch; }
.typing { color: var(--mut); font-style: italic; }

/* ---------- VIEWER ---------- */
.lbl-toggles { display: flex; gap: 10px; font-size: 11px; }
.lbl-toggles label { display: flex; align-items: center; gap: 3px; cursor: pointer; color: var(--txt); }
.viewer { flex: 1; position: relative; overflow: hidden; }
.viewer canvas { display: block; }
.tooltip {
  position: absolute; pointer-events: none; z-index: 10;
  background: rgba(10,14,20,.94); border: 1px solid var(--accent);
  color: var(--txt); padding: 5px 9px; border-radius: 8px; font-size: 12px;
  transform: translate(12px, 12px); box-shadow: 0 6px 18px rgba(0,0,0,.5); max-width: 240px;
}
.tooltip b { color: var(--accent); }
.hud {
  position: absolute; left: 10px; bottom: 10px; z-index: 9;
  background: rgba(10,14,20,.7); border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 10px; font-size: 11px; color: var(--mut); line-height: 1.6;
}
.hud b { color: var(--txt); }
.run-status {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%); z-index: 9;
  background: rgba(10,14,20,.9); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 14px; font-size: 12px;
}
.run-status.ok { border-color: var(--accent); }
.run-status.err { border-color: var(--danger); color: #ffd1da; }
.label-tag {
  position: absolute; pointer-events: none; z-index: 8; font-size: 11px; font-weight: 700;
  padding: 1px 5px; border-radius: 5px; transform: translate(-50%, -50%);
  background: rgba(10,14,20,.82); border: 1px solid var(--line); white-space: nowrap;
}
.label-tag.face { color: var(--accent); }
.label-tag.edge { color: var(--warn); }
.label-tag.vert { color: var(--accent2); }

/* ---------- CODE ---------- */
#tab-code .CodeMirror { flex: 1; height: auto; font-size: 13px; }

/* ---------- MODAL ---------- */
.modal { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.6); }
.modal-card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; width: 420px; max-height: 70vh; display: flex; flex-direction: column; }
.modal-head { padding: 12px 16px; border-bottom: 1px solid var(--line); font-weight: 700; display: flex; justify-content: space-between; align-items: center; }
.modal-head .x { background: none; border: 0; color: var(--mut); cursor: pointer; font-size: 16px; }
.pieces-list { overflow-y: auto; padding: 8px; }
.piece-row { display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: 8px; cursor: pointer; }
.piece-row:hover { background: var(--panel2); }
.piece-row.active { background: var(--panel2); }
.piece-row .nm { flex: 1; min-width: 0; }
.piece-row .nm .title { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.piece-row .nm .meta { display: block; color: var(--mut); font-size: 11px; margin-top: 2px; }
.piece-row .del { color: var(--danger); background: none; border: 0; cursor: pointer; font-size: 15px; }

/* Panel de Chats (cajón lateral estilo ChatGPT) */
.modal.drawer { justify-content: flex-start; align-items: stretch; }
.modal.drawer .modal-card { width: 300px; max-width: 86vw; height: 100%; max-height: 100%; border-radius: 0; border-top: 0; border-bottom: 0; border-left: 0; }
.chats-new { margin: 10px; padding: 11px; border-radius: 9px; background: var(--accent); color: #04201c; font-weight: 700; border: 0; cursor: pointer; font-size: 14px; }
.chats-foot { padding: 10px 14px; border-top: 1px solid var(--line); color: var(--mut); font-size: 11px; }

@media (max-width: 820px) {
  /* En movil: el VISOR 3D arriba (primero) y el panel Chat/Codigo debajo. */
  .layout { flex-direction: column-reverse; }
  .viewer-pane { flex: 0 0 48vh; min-height: 240px; }
  .side-pane { width: auto; flex: 1; min-height: 0; border-right: 0; border-top: 1px solid var(--line); }
  .splitter { display: none; }
}

/* ===== Imagen -> 3D (modal) ===== */
.img3d-body { padding: 16px; display: flex; flex-direction: column; gap: 12px; }
#modeSeg.seg { display: flex; gap: 6px; background: #10161f; padding: 4px; border-radius: 10px; }
#modeSeg.seg button {
  flex: 1; padding: 9px 8px; border: 0; border-radius: 7px; cursor: pointer;
  background: transparent; color: #9fb0c4; font-size: 13px; font-weight: 600;
}
#modeSeg.seg button.on { background: #2b6cff; color: #fff; }
.mode-pane { display: block; }
.drop {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 84px; padding: 14px; text-align: center; cursor: pointer;
  border: 1.5px dashed #2c3a4e; border-radius: 10px; color: #9fb0c4; font-size: 12.5px;
  background: #0d131c; transition: border-color .15s, background .15s;
}
.drop:hover { border-color: #2b6cff; background: #101a28; }
.drop b { color: #d6e0ec; font-size: 13px; }
.mv-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.img3d-go { width: 100%; padding: 11px; font-size: 14px; }
.img3d-hint { color: #7f8da0; font-size: 11.5px; line-height: 1.5; }
@media (max-width: 560px) { .mv-grid { grid-template-columns: 1fr; } }

/* ===== Conectar Claude Code (MCP) ===== */
.mcp-btn { padding: 4px 9px; font-size: 12px; }
.mcp-body { padding: 16px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; }
.mcp-lead { margin: 0; color: var(--mut); font-size: 12.5px; line-height: 1.5; }
.mcp-lead b { color: var(--txt); }
.mcp-field { display: flex; flex-direction: column; gap: 5px; }
.mcp-field > label { font-size: 11.5px; color: var(--mut); }
.mcp-field label .hint { color: var(--accent); }
.mcp-copyrow { display: flex; gap: 6px; align-items: stretch; }
.mcp-code {
  flex: 1; background: #0d131c; border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; color: var(--accent); font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 12px; word-break: break-all; user-select: all; display: block;
}
.mcp-code-block { white-space: pre; overflow-x: auto; color: var(--txt); }
.mcp-copy { flex: 0 0 auto; align-self: stretch; }
.mcp-copy.ok { background: var(--accent); color: #04201c; border-color: var(--accent); }
.mcp-details { border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; }
.mcp-details summary { cursor: pointer; color: var(--mut); font-size: 12px; }
.mcp-details .mcp-copyrow { margin-top: 8px; }
.mcp-hint { color: #7f8da0; font-size: 11.5px; line-height: 1.5; }
.msg .inline-mcp {
  margin-top: 8px; background: var(--accent); color: #04201c; border: 0;
  border-radius: 8px; padding: 6px 11px; cursor: pointer; font-weight: 700; font-size: 12.5px;
}
