:root {
  --bg: #f4f3f0; --surface: #ffffff; --surface-2: #f0efeb; --line: #dedcd6; --line-strong: #c9c6bf;
  --text: #1d1d1b; --muted: #6d6b66; --accent: #2f6fed; --accent-soft: #e3ecfd; --ok: #1f9d66;
  --warn: #b7791f; --danger: #c93c3c; --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 16px rgba(0,0,0,.05);
  --radius: 14px; --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121315; --surface: #1b1c1f; --surface-2: #232428; --line: #2e3035; --line-strong: #3b3e44;
    --text: #ececea; --muted: #9c9ca0; --accent: #5b8ff5; --accent-soft: #1e2a44; --ok: #3ecf8e;
    --warn: #e0a84a; --danger: #ff6b6b; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 16px rgba(0,0,0,.3);
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 var(--font); }
a { color: inherit; }
h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -.01em; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .07em; color: var(--muted); margin: 0 0 10px; font-weight: 600; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- barra superior ---------- */
.topbar { display: flex; align-items: center; gap: 16px; padding: 12px 24px; border-bottom: 1px solid var(--line); background: var(--surface); position: sticky; top: 0; z-index: 10; }
.brand { font-weight: 700; text-decoration: none; letter-spacing: -.01em; }
.crumbs { color: var(--muted); font-size: 14px; display: flex; gap: 8px; align-items: center; min-width: 0; }
.crumbs a { text-decoration: none; }
.crumbs span { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .spacer { flex: 1; }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; background: var(--muted); margin-right: 6px; }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.err { background: var(--danger); }
.status-pill { font-size: 12px; color: var(--muted); display: flex; align-items: center; }
.status-pill a { color: var(--accent); margin-left: 6px; }

main.page { max-width: 1240px; margin: 0 auto; padding: 28px 24px 60px; }

/* ---------- controles ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; border: 1px solid var(--line-strong); background: var(--surface); color: var(--text);
  padding: 9px 14px; border-radius: 10px; font: inherit; font-size: 14px; cursor: pointer; text-decoration: none; white-space: nowrap; }
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.07); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--danger); }
.btn.icon { padding: 8px; width: 38px; height: 38px; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
input[type=text], input[type=password], textarea, select { width: 100%; font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--line-strong); border-radius: 10px; padding: 9px 11px; }
textarea { resize: vertical; min-height: 84px; }
input:focus, textarea:focus, select:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
label.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.error-box { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--line); border-radius: 11px; padding: 3px; gap: 3px; }
.seg button { border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; font: inherit; font-size: 13px; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

/* ---------- login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 24px; }
.login { width: min(380px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 16px; }
.login h1 { text-align: center; margin-bottom: 4px; }

/* ---------- lista de espacios ---------- */
.head-row { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 18px; }
.space-card { overflow: hidden; text-decoration: none; display: flex; flex-direction: column; transition: transform .12s; }
.space-card:hover { transform: translateY(-2px); }
.space-card .thumb { aspect-ratio: 4/3; background: var(--surface-2) center/cover no-repeat; }
.space-card .meta { padding: 12px 14px; display: flex; flex-direction: column; gap: 2px; }
.space-card .name { font-weight: 600; }
.new-card { border: 2px dashed var(--line-strong); background: transparent; box-shadow: none; min-height: 240px; display: grid; place-items: center; color: var(--muted); cursor: pointer; font: inherit; }
.new-card:hover { border-color: var(--accent); color: var(--accent); }
dialog { border: 0; border-radius: var(--radius); padding: 0; background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0,0,0,.3); width: min(440px, 94vw); }
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog form { padding: 24px; display: flex; flex-direction: column; gap: 14px; }
.drop-zone { border: 2px dashed var(--line-strong); border-radius: 12px; padding: 18px; text-align: center; color: var(--muted); cursor: pointer; position: relative; }
.drop-zone img { max-width: 100%; max-height: 200px; border-radius: 8px; display: block; margin: 0 auto; }
.drop-zone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }

/* ---------- espacio: pipeline ---------- */
.pipeline { display: grid; grid-template-columns: minmax(0, 1fr) 40px minmax(0, 1.15fr) 40px minmax(0, 1fr); align-items: start; gap: 0; }
.pipe-col { display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.pipe-col .label { display: flex; align-items: center; gap: 8px; color: var(--muted); font-size: 13px; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; }
.badge-num { width: 22px; height: 22px; border-radius: 50%; border: 1.5px solid currentColor; display: inline-grid; place-items: center; font-size: 12px; }
.connector { align-self: center; height: 2px; background: var(--line-strong); position: relative; margin-top: 30px; }
.connector::before, .connector::after { content: ""; position: absolute; top: -4px; width: 10px; height: 10px; border-radius: 50%; background: var(--surface); border: 2px solid var(--line-strong); }
.connector::before { left: -5px; } .connector::after { right: -5px; }
.img-frame { position: relative; border-radius: 12px; overflow: hidden; background: var(--surface-2); border: 1px solid var(--line); }
.img-frame img { display: block; width: 100%; height: auto; }
.img-frame .mask-overlay { position: absolute; inset: 0; width: 100%; height: 100%; opacity: .5; mix-blend-mode: normal; pointer-events: none; }
.node { padding: 16px; display: flex; flex-direction: column; gap: 14px; }
.node-title { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.node-title strong { font-size: 16px; }
.node .mask-preview { display: grid; grid-template-columns: 96px 1fr; gap: 12px; align-items: center; }
.node .mask-preview .img-frame { width: 96px; }
.refs-strip { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.refs-strip img { width: 44px; height: 44px; object-fit: cover; border-radius: 8px; border: 1px solid var(--line); }
.play { width: 100%; padding: 12px 16px; font-size: 15px; font-weight: 600; }
.play .cost { font-weight: 400; opacity: .85; }
.results { display: flex; flex-direction: column; gap: 12px; }
.result { padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.result .info { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; color: var(--muted); flex-wrap: wrap; }
.result .actions { display: flex; gap: 6px; flex-wrap: wrap; }
.result .actions .btn { padding: 5px 9px; font-size: 12px; }
.result.pending .placeholder { aspect-ratio: 4/3; display: grid; place-items: center; background: var(--surface-2); border-radius: 10px; color: var(--muted); font-size: 14px; text-align: center; padding: 12px; }
.spinner { width: 22px; height: 22px; border: 3px solid var(--line-strong); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; margin: 0 auto 8px; }
@keyframes spin { to { transform: rotate(360deg); } }
.empty { color: var(--muted); font-size: 14px; padding: 18px; text-align: center; border: 1px dashed var(--line-strong); border-radius: 12px; }
@media (max-width: 960px) {
  .pipeline { grid-template-columns: 1fr; gap: 18px; }
  .connector { display: none; }
}

/* ---------- visor ---------- */
.viewer { position: fixed; inset: 0; background: rgba(10,10,12,.92); display: none; z-index: 50; overflow: hidden; user-select: none; }
.viewer.open { display: block; }
.viewer-stage { position: absolute; inset: 0; cursor: grab; touch-action: none; }
.viewer-stage.dragging { cursor: grabbing; }
.viewer-stage img { position: absolute; left: 0; top: 0; transform-origin: 0 0; max-width: none; border-radius: 4px;
  box-shadow: 0 10px 40px rgba(0,0,0,.5); image-rendering: auto; }
.viewer-bar { position: absolute; left: 50%; bottom: 44px; transform: translateX(-50%); display: flex; align-items: center; gap: 4px;
  background: rgba(30,30,34,.9); border: 1px solid rgba(255,255,255,.12); border-radius: 12px; padding: 6px; color: #eee; }
.vbtn { min-width: 34px; height: 34px; border: 0; border-radius: 8px; background: transparent; color: #eee; font: inherit; font-size: 18px; cursor: pointer; }
.vbtn.wide { font-size: 13px; padding: 0 10px; }
.vbtn:hover:not(:disabled) { background: rgba(255,255,255,.12); }
.vbtn:disabled { opacity: .3; cursor: default; }
.vsep { width: 1px; height: 22px; background: rgba(255,255,255,.15); margin: 0 4px; }
.viewer-label { font-size: 13px; min-width: 120px; text-align: center; }
.viewer-zoom { font-size: 12px; min-width: 46px; text-align: center; color: #bbb; }
.viewer-close { position: absolute; top: 14px; right: 14px; font-size: 24px; background: rgba(30,30,34,.9); }
.viewer-hint { position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); font-size: 12px; color: #999; white-space: nowrap; }

/* ---------- referencias ---------- */
.refs-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 24px; align-items: start; }
.selected-list { display: flex; flex-direction: column; gap: 12px; }
.sel-item { display: grid; grid-template-columns: 140px 1fr; gap: 12px; padding: 10px; }
.sel-item .pic { position: relative; }
.sel-item .pic img { width: 140px; height: 105px; object-fit: cover; border-radius: 9px; display: block; }
.sel-item .remove { position: absolute; right: 6px; bottom: 6px; width: 28px; height: 28px; border-radius: 50%; border: 0; background: rgba(0,0,0,.65); color: #fff; cursor: pointer; font-size: 16px; line-height: 1; }
.sel-item textarea { min-height: 105px; height: 105px; font-size: 14px; }
.carousel-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 30px 0 12px; flex-wrap: wrap; }
.carousel-head .row select { width: auto; max-width: 320px; }
.carousel { display: flex; gap: 12px; overflow-x: auto; padding: 4px 2px 14px; scroll-snap-type: x proximity; }
.carousel .tile { flex: 0 0 auto; width: 180px; position: relative; scroll-snap-align: start; cursor: pointer; border-radius: 12px; overflow: hidden; border: 2px solid transparent; background: var(--surface-2); }
.carousel .tile img { width: 180px; height: 135px; object-fit: cover; display: block; }
.carousel .tile .check { position: absolute; right: 8px; bottom: 8px; width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; background: rgba(0,0,0,.35); display: grid; place-items: center; color: #fff; font-size: 14px; }
.carousel .tile.on { border-color: var(--accent); }
.carousel .tile.on .check { background: var(--accent); border-color: var(--accent); }
.carousel .more { flex: 0 0 auto; width: 120px; display: grid; place-items: center; }
@media (max-width: 900px) { .refs-layout { grid-template-columns: 1fr; } }

/* ---------- canvas ---------- */
body.canvas-page { height: 100vh; height: 100dvh; overflow: hidden; display: flex; flex-direction: column; }
#viewport { position: relative; flex: 1; overflow: hidden; cursor: default; touch-action: none;
  background-color: var(--bg); background-image: radial-gradient(var(--line-strong) 1px, transparent 1px); background-size: 24px 24px; }
#viewport.panning, #viewport.pan-ready { cursor: grab; }
#viewport.panning { cursor: grabbing; }
#world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
#edges { position: absolute; left: 0; top: 0; overflow: visible; pointer-events: none; }
#edges path.edge { fill: none; stroke-width: 3; }
#edges path.hit { fill: none; stroke: transparent; stroke-width: 18; pointer-events: stroke; cursor: pointer; }
#edges path.hit:hover + path.edge, #edges path.edge.hover { stroke-dasharray: 8 5; }
#edges path.temp { fill: none; stroke: var(--accent); stroke-width: 3; stroke-dasharray: 6 5; }
.canvas-toolbar { position: absolute; left: 16px; top: 16px; display: flex; align-items: center; gap: 6px; padding: 6px; z-index: 5; }
.canvas-toolbar .sep { width: 1px; height: 24px; background: var(--line); margin: 0 4px; }
.canvas-toolbar #zoom-val { min-width: 48px; text-align: center; font-size: 12px; color: var(--muted); }
.menu { position: relative; }
.menu > summary { list-style: none; cursor: pointer; }
.menu > summary::-webkit-details-marker { display: none; }
.menu-pop { position: absolute; top: calc(100% + 8px); left: 0; min-width: 190px; padding: 6px; z-index: 20; }
.menu-item { display: block; width: 100%; text-align: left; border: 0; background: transparent; font: inherit; font-size: 14px;
  color: var(--text); padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.menu-item:hover { background: var(--surface-2); }
.menu-item.danger { color: var(--danger); }
.canvas-hint { position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); font-size: 12px; color: var(--muted); background: var(--surface); border: 1px solid var(--line); padding: 6px 12px; border-radius: 999px; z-index: 5; white-space: nowrap; max-width: calc(100% - 32px); overflow: hidden; text-overflow: ellipsis; }

.cnode { position: absolute; background: var(--surface); border: 2px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); user-select: none; }
.cnode.image { border-color: var(--c); }
.cnode.dragging { box-shadow: 0 12px 32px rgba(0,0,0,.25); z-index: 3; }
.cnode .head { display: flex; align-items: center; gap: 8px; padding: 8px 10px; cursor: grab; border-radius: 12px 12px 0 0; }
.cnode.image .head { background: var(--c); color: #fff; }
.cnode.transform .head { background: var(--surface-2); border-bottom: 1px solid var(--line); }
.cnode .head .title { font-weight: 600; font-size: 13px; white-space: nowrap; }
.cnode .head .sub { font-size: 12px; opacity: .85; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.cnode .head .x { border: 0; background: transparent; color: inherit; font-size: 18px; line-height: 1; cursor: pointer; opacity: .7; padding: 0 2px; }
.cnode .head .x:hover { opacity: 1; }
.cnode .body { padding: 10px; display: flex; flex-direction: column; gap: 10px; }
.cnode.image .body { padding: 0; }
.cnode.image .body img { display: block; width: 100%; height: auto; cursor: zoom-in; }
.cnode .foot { display: flex; gap: 6px; padding: 8px 10px; }
.cnode .foot .btn, .cnode .body .btn { padding: 5px 9px; font-size: 12px; }
.cnode textarea { min-height: 70px; font-size: 13px; padding: 7px 9px; cursor: text; user-select: text; }
.cnode .seg button { padding: 5px 9px; font-size: 12px; }
.cnode .model-select { width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 9px; background: var(--surface-2); color: var(--text); font: inherit; font-size: 12px; }
.cnode .play { padding: 9px 12px; font-size: 14px; }
.cnode .preview { position: relative; border-radius: 10px; overflow: hidden; border: 2px solid var(--c, var(--line)); }
.cnode .preview img { display: block; width: 100%; height: auto; }
.cnode .preview .tint { position: absolute; inset: 0; background: rgba(255,70,70,.55); -webkit-mask-size: 100% 100%; mask-size: 100% 100%; }
.cnode .hint { font-size: 13px; color: var(--muted); border: 1px dashed var(--line-strong); border-radius: 10px; padding: 14px; text-align: center; }
.cnode .refs-strip img { width: 36px; height: 36px; }
.cnode .job { font-size: 12px; display: flex; align-items: center; gap: 8px; color: var(--muted); }
.cnode .job .spinner { width: 16px; height: 16px; border-width: 2px; margin: 0; }
.cnode .job.err { color: var(--danger); }
.port { position: absolute; top: 50%; width: var(--port, 18px); height: var(--port, 18px); margin-top: calc(var(--port, 18px) / -2); border-radius: 50%; background: var(--surface); border: 3px solid var(--c, var(--muted)); z-index: 2; }
.port.out { right: calc(var(--port, 18px) / -2 - 2px); cursor: crosshair; }
.port.in { left: calc(var(--port, 18px) / -2 - 2px); }
.port.passive { cursor: default; opacity: .8; }
.port.out:not(.passive):hover { transform: scale(1.25); }
.cnode.drop-target { outline: 3px dashed var(--accent); outline-offset: 4px; }
.resize { position: absolute; right: 2px; bottom: 2px; width: var(--grip, 16px); height: var(--grip, 16px); cursor: nwse-resize; z-index: 2;
  border-right: 3px solid var(--line-strong); border-bottom: 3px solid var(--line-strong); border-radius: 0 0 10px 0; }
.resize:hover { border-color: var(--accent); }
.picker-grid.busy { opacity: .5; pointer-events: none; }

.picker { width: min(980px, 94vw); padding: 20px; display: flex; flex-direction: column; gap: 14px; max-height: 86vh; }
dialog#picker { width: auto; max-width: none; }
.picker-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.picker-head h1 { margin: 0; }
.picker-head select { width: auto; max-width: 320px; }
.picker-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; overflow-y: auto; padding: 2px; }
.picker-grid .tile { border-radius: 10px; overflow: hidden; cursor: pointer; border: 2px solid transparent; background: var(--surface-2); }
.picker-grid .tile:hover { border-color: var(--accent); }
.picker-grid .tile img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; }

/* ---------- celular ---------- */
/* textos que cambian según haya mouse o pantalla táctil */
.touch-only { display: none; }
@media (pointer: coarse) { .touch-only { display: inline; } .mouse-only { display: none !important; } }
button, .btn, a, summary, .tile { touch-action: manipulation; }   /* sin espera de doble toque */
/* iPhone agranda la página al enfocar un campo con letra de menos de 16 px y no la vuelve a achicar */
@media (pointer: coarse) { input[type=text], input[type=password], textarea, select, .cnode textarea, .cnode .model-select { font-size: 16px; } }
@media (max-width: 640px) {
  .topbar { gap: 10px; padding: 10px 12px; }
  .crumbs { flex: 1; font-size: 13px; gap: 5px; overflow: hidden; }
  .crumbs span { min-width: 0; }
  .topbar .spacer { display: none; }
  .status-pill > span:not(.dot) { display: none; }   /* queda solo el punto de color */
  .status-pill a { display: inline; }
  main.page { padding: 18px 14px 40px; }
  .canvas-toolbar { left: 8px; right: 8px; top: 8px; flex-wrap: wrap; gap: 5px; }
  .canvas-toolbar .sep { display: none; }
  .canvas-toolbar #zoom-val { min-width: 38px; }
  .canvas-toolbar .btn { padding: 8px 10px; flex: 0 0 auto; }
  .canvas-toolbar .btn.icon { width: 38px; }
  .long-label { display: none; }
  .menu-pop { position: fixed; top: 64px; left: auto; right: 12px; }
  .canvas-hint { white-space: normal; text-align: center; bottom: 8px; border-radius: 12px; }
  .canvas-hint.gone { opacity: 0; pointer-events: none; transition: opacity .4s; }
  .crumbs a, .crumbs span { white-space: nowrap; }
  .viewer-bar { left: 8px; right: 8px; transform: none; bottom: 12px; justify-content: center; flex-wrap: wrap; }
  .viewer-label { min-width: 0; flex: 1 1 100%; order: -1; padding: 2px 0 4px; }
  .vsep, .viewer-hint, #v-full { display: none; }
  .vbtn { min-width: 42px; height: 42px; }
  .viewer-close { width: 44px; height: 44px; top: 10px; right: 10px; }
  .sel-item { grid-template-columns: 96px 1fr; }
  .sel-item .pic img { width: 96px; height: 96px; }
  .sel-item textarea { min-height: 96px; height: 96px; }
  .carousel .tile, .carousel .tile img { width: 140px; }
  .carousel .tile img { height: 105px; }
}

/* ---------- toast ---------- */
#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: 14px; opacity: 0; transition: opacity .2s; pointer-events: none; z-index: 60; max-width: 90vw; }
#toast.show { opacity: 1; }
#toast.err { background: var(--danger); color: #fff; }
