/* Lienzo · estilos */
:root {
  --bg: #0b0f19;
  --bg-2: #121829;
  --panel: #161d31;
  --panel-2: #1c2540;
  --line: #253052;
  --txt: #e8ecf7;
  --muted: #8590b0;
  --accent: #6d8cff;
  --accent-2: #8b5cf6;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --topbar-h: 56px;
  --side-w: 288px;
  --radius: 12px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--txt);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  overscroll-behavior: none;
}

body { overflow: hidden; }

/* ─────────────── Topbar */
#topbar {
  position: fixed; inset: 0 0 auto 0; height: var(--topbar-h);
  display: flex; align-items: center; gap: 10px;
  padding: 0 12px; padding-top: env(safe-area-inset-top);
  background: color-mix(in srgb, var(--bg-2) 92%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  z-index: 40;
}
.spacer { flex: 1; }

#space-name-wrap { display: flex; flex-direction: column; min-width: 0; }
#space-name {
  background: transparent; border: none; color: var(--txt);
  font-size: 16px; font-weight: 650; padding: 2px 4px; border-radius: 6px;
  width: 190px; max-width: 42vw;
}
#space-name:focus { outline: 2px solid var(--accent); background: var(--panel); }
#space-id { font-size: 11px; padding-left: 5px; }

.btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--txt);
  padding: 8px 13px; border-radius: 9px; font-size: 14px; font-weight: 600;
  cursor: pointer; white-space: nowrap; transition: .12s;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.accent { background: var(--ok); border-color: var(--ok); color: #04140a; }
.btn.accent:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger { background: transparent; border-color: #7f2b2b; color: #ff9b9b; }
.btn.danger:hover { background: #3a1414; }
.btn.wide { width: 100%; margin-top: 6px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.icon-btn {
  background: transparent; border: none; color: var(--txt);
  font-size: 19px; cursor: pointer; padding: 6px 9px; border-radius: 8px;
}
.icon-btn:hover { background: var(--panel-2); }

.pill {
  background: var(--panel); border: 1px solid var(--line);
  padding: 5px 11px; border-radius: 20px; font-size: 13px; font-weight: 600;
  color: var(--muted); white-space: nowrap;
}
.pill.over { color: #ffb4b4; border-color: #7f2b2b; }

/* ─────────────── Sidebar */
#sidebar {
  position: fixed; top: var(--topbar-h); bottom: 0; left: 0; width: var(--side-w);
  background: var(--bg-2); border-right: 1px solid var(--line);
  padding: 12px; overflow-y: auto; z-index: 30;
  transition: transform .22s ease;
}
.side-sec { margin-bottom: 22px; }
.side-sec h3 {
  margin: 0 0 8px; font-size: 11px; text-transform: uppercase;
  letter-spacing: .09em; color: var(--muted); font-weight: 700;
}
.hint { margin: -4px 0 8px; font-size: 12px; color: var(--muted); }

.space-list, .type-list, .event-list { list-style: none; margin: 0; padding: 0; }

.space-list li {
  padding: 9px 11px; border-radius: 9px; cursor: pointer;
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  border: 1px solid transparent;
}
.space-list li:hover { background: var(--panel); }
.space-list li.active { background: var(--panel-2); border-color: var(--accent); }
.space-list .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.space-list .ct { font-size: 11px; color: var(--muted); }

.type-list li {
  display: flex; align-items: center; gap: 9px;
  padding: 9px 11px; border-radius: 9px; cursor: pointer;
  border: 1px solid transparent;
}
.type-list li:hover { background: var(--panel); border-color: var(--line); }
.type-list .ic {
  width: 26px; height: 26px; flex: none; border-radius: 7px;
  display: grid; place-items: center; font-size: 13px;
  background: var(--panel-2);
}
.type-list .tx { min-width: 0; }
.type-list .tx b { display: block; font-size: 13.5px; font-weight: 600; }
.type-list .tx span { display: block; font-size: 11.5px; color: var(--muted);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.event-list li { padding: 6px 2px; font-size: 12px; border-bottom: 1px solid var(--line); }
.event-list li:last-child { border-bottom: none; }
.event-list .ts { color: var(--muted); font-size: 11px; }
.event-list .kind-err { color: #ff9b9b; }
.event-list .kind-ok { color: #86efac; }

#budget .bar { height: 7px; background: var(--panel-2); border-radius: 4px; overflow: hidden; margin: 6px 0; }
#budget .bar i { display: block; height: 100%; background: var(--ok); }
#budget .bar.warn i { background: var(--warn); }
#budget .bar.over i { background: var(--err); }
#budget .nums { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; }

/* ─────────────── Escenario */
#stage {
  position: fixed; inset: var(--topbar-h) 0 0 0;
  overflow: hidden; touch-action: none;
  background:
    radial-gradient(circle at 1px 1px, #1b2440 1px, transparent 0) 0 0 / 26px 26px,
    var(--bg);
}
#stage.panning { cursor: grabbing; }
#stage.wiring { cursor: crosshair; }

#wires { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 1; }
#wires path { fill: none; stroke: #4a5c96; stroke-width: 2.5; }
#wires path.text-edge { stroke: #64748b; stroke-dasharray: 5 4; }
#wires path.image-edge { stroke: #8b5cf6; }
#wires path.video-edge { stroke: #ec4899; }
#wires path.pending { stroke: var(--accent); stroke-dasharray: 4 4; }
#wires circle { fill: var(--bg-2); stroke: #4a5c96; stroke-width: 2; }

#nodes { position: absolute; inset: 0; z-index: 2; }

/* ─────────────── Nodo */
.node {
  position: absolute; width: 230px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: 0 8px 26px rgba(0,0,0,.42);
  user-select: none;
}
.node.selected { border-color: var(--accent); box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 45%, transparent), 0 8px 26px rgba(0,0,0,.5); }
.node.running { border-color: var(--warn); }
.node.error { border-color: var(--err); }

.node-head {
  display: flex; align-items: center; gap: 7px;
  padding: 8px 10px; border-bottom: 1px solid var(--line);
  cursor: grab; border-radius: var(--radius) var(--radius) 0 0;
}
.node-head:active { cursor: grabbing; }
.node-ic { width: 21px; height: 21px; border-radius: 6px; display: grid; place-items: center; font-size: 11px; flex: none; }
.node-title { flex: 1; font-size: 13px; font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-type { font-size: 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; }
.node-badge { font-size: 10px; padding: 1px 6px; border-radius: 10px; background: var(--panel-2); color: var(--muted); }
.node-badge.ok { background: #14351f; color: #86efac; }
.node-badge.err { background: #3a1414; color: #ff9b9b; }
.node-badge.run { background: #3a2c0e; color: #fcd34d; }

.node-body { padding: 9px 10px; font-size: 12.5px; }
.node-body .prompt {
  color: var(--muted); max-height: 54px; overflow: hidden;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  word-break: break-word;
}
.node-body .model { font-size: 10.5px; color: #6b7899; margin-top: 5px; font-family: ui-monospace, monospace; }
.node-body .thumb {
  width: 100%; border-radius: 8px; display: block; margin-bottom: 7px;
  border: 1px solid var(--line); cursor: zoom-in; background: #0a0e18;
}
.node-body video.thumb { cursor: pointer; }
.node-body .empty-out {
  border: 1px dashed var(--line); border-radius: 8px; padding: 11px;
  text-align: center; color: var(--muted); font-size: 11.5px;
}

.port {
  position: absolute; width: 13px; height: 13px; border-radius: 50%;
  background: var(--panel-2); border: 2px solid #4a5c96; cursor: crosshair;
  top: 50%; transform: translateY(-50%);
}
.port:hover { border-color: var(--accent); transform: translateY(-50%) scale(1.25); }
.port.out { right: -7px; }
.port.in { left: -7px; }
.port.text { border-color: #64748b; }
.port.image { border-color: #8b5cf6; }
.port.video { border-color: #ec4899; }

/* ─────────────── Modales */
.modal {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(4,7,14,.68); backdrop-filter: blur(4px);
  display: grid; place-items: center; padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--bg-2); border: 1px solid var(--line);
  border-radius: 15px; width: 100%; max-width: 520px;
  max-height: 88vh; display: flex; flex-direction: column;
  box-shadow: 0 26px 70px rgba(0,0,0,.6);
}
.modal-head {
  display: flex; align-items: center; padding: 13px 15px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 16px; flex: 1; }
.modal-body { padding: 15px; overflow-y: auto; }
.modal-foot {
  display: flex; gap: 8px; padding: 12px 15px;
  border-top: 1px solid var(--line);
}

.field { margin-bottom: 13px; }
.field label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
.field input[type=text], .field input[type=number], .field textarea, .field select {
  width: 100%; background: var(--panel); border: 1px solid var(--line);
  color: var(--txt); border-radius: 9px; padding: 9px 11px; font-size: 14px;
  font-family: inherit;
}
.field textarea { min-height: 92px; resize: vertical; line-height: 1.5; }
.field select { appearance: none; background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%); background-position: right 15px center, right 10px center; background-size: 5px 5px, 5px 5px; background-repeat: no-repeat; }
.field input:focus, .field textarea:focus, .field select:focus { outline: none; border-color: var(--accent); }

.versions { display: flex; flex-direction: column; gap: 7px; }
.version {
  display: flex; gap: 10px; align-items: center; padding: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 9px;
}
.version img, .version video { width: 52px; height: 52px; object-fit: cover; border-radius: 6px; flex: none; }
.version .vi { flex: 1; min-width: 0; font-size: 12px; }
.version .vi b { display: block; }
.version .vi span { color: var(--muted); font-size: 11px; }

.run-step { display: flex; gap: 9px; align-items: flex-start; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.run-step:last-child { border-bottom: none; }
.run-step .st { width: 17px; height: 17px; border-radius: 50%; display: grid; place-items: center; font-size: 10px; flex: none; margin-top: 1px; }
.run-step .st.ok { background: #14351f; color: #86efac; }
.run-step .st.err { background: #3a1414; color: #ff9b9b; }
.run-step .st.wait { background: var(--panel-2); color: var(--muted); }
.run-step .st.run { background: #3a2c0e; color: #fcd34d; }
.run-step .tx { flex: 1; min-width: 0; }
.run-step .tx em { display: block; font-style: normal; color: var(--muted); font-size: 11.5px; word-break: break-word; }

/* ─────────────── Varios */
.muted { color: var(--muted); font-size: 11px; }
.empty-hint {
  position: absolute; inset: 0; display: grid; place-content: center;
  text-align: center; color: var(--muted); pointer-events: none; z-index: 0;
}
.empty-hint h2 { color: var(--txt); font-size: 19px; margin: 0 0 6px; }
.empty-hint p { margin: 0; font-size: 13.5px; max-width: 300px; }

#scrim { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 25; display: none; }
#scrim.on { display: block; }

.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--line); color: var(--txt);
  padding: 11px 17px; border-radius: 10px; z-index: 90; font-size: 13.5px;
  box-shadow: 0 10px 34px rgba(0,0,0,.5); max-width: 90vw;
}
.toast.err { border-color: #7f2b2b; color: #ffb4b4; }

.lightbox {
  position: fixed; inset: 0; background: rgba(2,4,9,.93); z-index: 100;
  display: grid; place-items: center; padding: 20px; cursor: zoom-out;
}
.lightbox img, .lightbox video { max-width: 100%; max-height: 100%; border-radius: 8px; }

/* ─────────────── Portón de acceso */
.gate {
  position: fixed; inset: 0; z-index: 200;
  background: radial-gradient(120% 90% at 50% 0%, #16203a 0%, var(--bg) 62%);
  display: grid; place-items: center; padding: 24px;
}
.gate[hidden] { display: none; }
.gate-card {
  width: 100%; max-width: 330px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 18px; padding: 30px 26px 26px;
  box-shadow: 0 24px 60px rgba(0,0,0,.5);
  text-align: center;
}
.gate-logo { font-size: 44px; line-height: 1; margin-bottom: 10px; }
.gate-card h1 { margin: 0 0 6px; font-size: 23px; letter-spacing: -.4px; }
.gate-sub { margin: 0 0 20px; color: var(--muted); font-size: 13.5px; }
.gate-card input {
  width: 100%; background: var(--bg-2); border: 1px solid var(--line);
  color: var(--txt); border-radius: 10px; padding: 12px 14px;
  font-size: 16px; margin-bottom: 12px; text-align: center;
}
.gate-card input:focus { outline: none; border-color: var(--accent); }
.btn.wide { width: 100%; padding: 12px; font-size: 15px; }
.gate-err { color: #ffb4b4; font-size: 13px; margin: 12px 0 0; }
.gate-err[hidden] { display: none; }

/* ─────────────── Móvil */
@media (max-width: 820px) {
  #sidebar { transform: translateX(-100%); box-shadow: 12px 0 40px rgba(0,0,0,.55); }
  #sidebar.open { transform: translateX(0); }
  #space-name { width: 130px; }
  .node { width: 200px; }
  #btn-add { display: none; }
  /* En móvil el lienzo no puede empezar pegado al borde: deja margen para
     que los puertos (círculos de conexión) no queden bajo el borde del dedo. */
  #nodes { padding-left: 12px; }
  #topbar { gap: 6px; padding: 0 8px; }
  #space-name { width: 110px; font-size: 15px; }
  .pill { padding: 4px 9px; font-size: 12px; }
  .btn { padding: 7px 11px; font-size: 13.5px; }
}
@media (min-width: 821px) {
  #btn-menu { display: none; }
  /* #stage ya está desplazado; #wires y #nodes son hijos suyos y heredan el offset.
     NO hay que volverles a poner left: var(--side-w) (producía un doble desplazamiento). */
  #stage { left: var(--side-w); }
}
