:root {
  --bg: #0b0d12;
  --bg-elev: #10131a;
  --panel: #13171f;
  --panel2: #1a1f29;
  --panel3: #222835;
  --border: #262c38;
  --border-strong: #353d4d;
  --text: #e2e6ee;
  --muted: #8f98a9;
  --accent: #6aa9ff;
  --accent-strong: #3d86f5;
  --accent-bg: #1b3252;
  --accent-glow: rgba(106, 169, 255, .18);
  --ok: #4cc38a;
  --warn: #e5b567;
  --err: #f07178;
  --violet: #b794f6;
  --mono: ui-monospace, SFMono-Regular, "Cascadia Code", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: "Segoe UI Variable", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --side-w: 224px;
  --panel-h: 320px;
  --radius: 8px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

/* Necesario: las reglas display de los componentes anularían el atributo hidden. */
[hidden] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 var(--sans); -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.mono { font-family: var(--mono); }
.pad { padding: 8px 14px; margin: 0; }
.spacer { flex: 1; }
.error { color: var(--err); min-height: 1.4em; margin: 0; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

input, select, textarea, .btn {
  font: inherit; color: var(--text); background: var(--panel2);
  border: 1px solid var(--border); border-radius: 7px; padding: 8px 12px;
  transition: background .12s, border-color .12s;
}
input:focus-visible, select:focus-visible, textarea:focus-visible { border-color: var(--accent); outline-offset: 0; }
.btn { cursor: pointer; min-height: 36px; display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
.btn:hover:not(:disabled) { background: var(--panel3); border-color: var(--border-strong); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--accent-strong); border-color: var(--accent-strong); color: #fff; font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: #5499ff; border-color: #5499ff; }
.btn.danger { background: #4a1f25; border-color: var(--err); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; min-height: 32px; }

/* Acceso */
.login {
  min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(900px 500px at 50% -10%, var(--accent-glow), transparent 70%), var(--bg);
}
.login-card {
  width: min(440px, 100%); display: flex; flex-direction: column; gap: 12px;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 32px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .5);
}
.login-card h1 { font-size: 26px; letter-spacing: -.01em; }
.login-card p { margin: 0; }
.login-card input { width: 100%; min-height: 44px; font-size: 16px; }
.login-card label { font-size: 13px; }

/* Registro y recuperación de cuenta */
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-field { display: flex; flex-direction: column; gap: 6px; }
.auth-card .muted, .login-card .muted { color: var(--muted, #8b949e); }
.auth-card .small, .login-card .small { font-size: 12.5px; line-height: 1.45; }
.auth-card .error, .login-card .error { color: var(--err, #f85149); font-size: 13px; min-height: 1.2em; }
.auth-notice { color: var(--ok, #3fb950); font-size: 13px; }
.auth-notice:empty { display: none; }
.auth-links { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 16px; margin-top: 4px; }
.link-btn {
  background: none; border: 0; padding: 4px 0; min-height: 32px; font: inherit; font-size: 13px;
  color: var(--accent, #3b82f6); cursor: pointer; text-decoration: underline; text-underline-offset: 3px;
}
.link-btn:hover { filter: brightness(1.2); }
.link-btn:focus-visible { outline: 2px solid var(--accent, #3b82f6); outline-offset: 2px; border-radius: 4px; }
.auth-check { display: flex; align-items: center; gap: 8px; font-size: 14px; cursor: pointer; }
.auth-check input { width: 18px; min-height: 18px; height: 18px; }
/* Campo trampa anti-bots: fuera de pantalla pero presente en el DOM */
.hp { position: absolute; left: -10000px; top: auto; width: 1px; height: 1px; overflow: hidden; }
.codes-panel { display: flex; flex-direction: column; gap: 10px; }
.codes-list {
  list-style: none; margin: 0; padding: 12px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px;
  background: var(--bg, #0d1117); border: 1px solid var(--border); border-radius: 8px;
}
.codes-list code { font-family: ui-monospace, Consolas, monospace; font-size: 13px; letter-spacing: .04em; user-select: all; }
.codes-actions { display: flex; flex-wrap: wrap; gap: 8px; }
@media (max-width: 420px) {
  .login-card { padding: 22px 18px; }
  .codes-list { grid-template-columns: 1fr; }
}

/* Estructura */
.app {
  display: grid; height: 100vh; height: 100dvh;
  grid-template-columns: var(--side-w) minmax(0, 1fr);
  grid-template-rows: 48px minmax(0, 1fr) 26px;
  grid-template-areas: "top top" "side main" "status status";
}
.topbar {
  grid-area: top; display: flex; align-items: center; gap: 16px;
  padding: 0 16px; background: var(--bg-elev); border-bottom: 1px solid var(--border);
}
.brand {
  font-weight: 700; letter-spacing: .03em; font-size: 16px;
  background: linear-gradient(90deg, var(--accent), var(--violet));
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
#ctx { font-size: 14px; padding: 3px 10px; background: var(--panel2); border: 1px solid var(--border); border-radius: 999px; }
.conn { font-size: 13px; color: var(--ok); display: inline-flex; align-items: center; gap: 6px; }
.conn::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: currentColor; box-shadow: 0 0 8px currentColor; }
.conn[data-state="down"] { color: var(--err); }

.sidebar {
  grid-area: side; display: flex; flex-direction: column; min-height: 0; position: relative;
  background: var(--panel); border-inline-end: 1px solid var(--border);
}
.side-head { padding: 14px; border-bottom: 1px solid var(--border); display: grid; gap: 10px; }
.toolbar { display: flex; flex-wrap: wrap; gap: 6px; }
.toolbar .btn { flex: 1 1 auto; }
.tree { flex: 1; overflow: auto; padding: 8px 0 24px; }
.tree ul { list-style: none; margin: 0; padding: 0; }
.row {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: start;
  min-height: 32px; padding: 4px 12px; padding-inline-start: calc(12px + var(--depth, 0) * 16px);
  background: none; border: 0; border-radius: 0; color: var(--text); font: inherit; cursor: pointer;
}
.row:hover { background: var(--panel2); }
.row.selected { background: var(--accent-bg); box-shadow: inset 2px 0 0 var(--accent); }
.row.disabled { color: var(--muted); cursor: default; }
.row .ico { width: 14px; flex: none; color: var(--muted); font-size: 10px; transition: transform .1s; }
.row[aria-expanded="true"] .ico { transform: rotate(90deg); }
.row .lbl { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row[data-kind="client"] .lbl { font-weight: 600; }
.row[data-kind="dir"] .lbl, .row[data-kind="project"] .lbl { color: #cdd7ea; }
.note { padding: 3px 14px 3px 44px; font-size: 13px; }

.main { grid-area: main; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.tabs { display: flex; overflow-x: auto; background: var(--bg-elev); border-bottom: 1px solid var(--border); min-height: 42px; scrollbar-width: thin; }
.tab { display: flex; align-items: center; border-inline-end: 1px solid var(--border); flex: none; }
.tab.active { background: var(--bg); box-shadow: inset 0 2px 0 var(--accent); }
.tab-main, .tab-close { background: none; border: 0; color: var(--muted); font: inherit; cursor: pointer; padding: 10px 10px; }
.tab-main { padding-inline-end: 4px; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab:hover .tab-main { color: var(--text); }
.tab.active .tab-main { color: var(--text); }
.tab-close { border-radius: 6px; margin-inline-end: 4px; padding: 4px 8px; font-size: 16px; line-height: 1; }
.tab-close:hover { color: var(--err); background: var(--panel2); }
.dot { color: var(--warn); margin-inline-start: 6px; font-size: 11px; }

.editor-area { flex: 1; min-height: 140px; position: relative; background: var(--bg); }

/* Pantalla de bienvenida */
.empty { height: 100%; overflow: auto; display: flex; flex-direction: column; align-items: center; text-align: center; color: var(--muted); padding: 24px; }
.empty p { margin: 4px 0; }
.empty h1 { color: var(--text); font-size: 30px; letter-spacing: -.01em; margin-bottom: 6px; }
.welcome { max-width: 760px; width: 100%; margin: auto; display: grid; gap: 22px; justify-items: center; }
.welcome-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 12px; width: 100%; text-align: start; }
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 16px;
  display: grid; gap: 6px; align-content: start;
}
.card h3 { font-size: 15px; color: var(--text); }
.card p { margin: 0; font-size: 13px; color: var(--muted); }
.card .tag { justify-self: start; font-size: 11px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border-strong); color: var(--warn); }
.card .tag.ok { color: var(--ok); }
.welcome-actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }

.editor { display: flex; height: 100%; background: var(--bg); font: 14px/1.6 var(--mono); }
.gutter {
  flex: none; min-width: 4.2ch; padding: 10px 12px 40px 10px; text-align: end; color: #566070;
  overflow: hidden; white-space: pre; user-select: none; border-inline-end: 1px solid var(--border);
}
.code { position: relative; flex: 1; min-width: 0; }
.hl, .ta {
  position: absolute; inset: 0; margin: 0; padding: 10px 24px 20px 14px;
  font: inherit; line-height: inherit; tab-size: 4; white-space: pre;
  overflow-wrap: normal; word-wrap: normal; border: 0; border-radius: 0; letter-spacing: normal;
}
/* Relleno extra: compensa las barras de desplazamiento del textarea para que ambas capas lleguen al final a la vez. */
.hl { overflow: hidden; pointer-events: none; color: var(--text); padding-bottom: 40px; padding-right: 40px; }
.ta { overflow: auto; resize: none; background: transparent; color: transparent; caret-color: #fff; outline: none; }
.ta::selection { background: rgba(106, 169, 255, .35); }
.t-keyword { color: #c792ea; }
.t-string { color: #a5d6a7; }
.t-comment { color: #6b7686; font-style: italic; }
.t-number { color: #f2a65a; }
.t-tag { color: #7fb7ff; }

/* Paneles inferiores */
.panel { height: var(--panel-h); display: flex; flex-direction: column; border-top: 1px solid var(--border); background: var(--panel); min-height: 0; position: relative; }
.panel-tabs { display: flex; border-bottom: 1px solid var(--border); background: var(--bg-elev); overflow-x: auto; }
.ptab { background: none; border: 0; border-radius: 0; color: var(--muted); padding: 10px 18px; cursor: pointer; font: inherit; white-space: nowrap; }
.ptab:hover { color: var(--text); }
.ptab[aria-selected="true"] { color: var(--text); box-shadow: inset 0 -2px 0 var(--accent); }
.pane { flex: 1; overflow: auto; min-height: 0; }
.inline-form { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 12px 14px; }
.inline-form label { color: var(--muted); font-size: 13px; }
.inline-form .check { display: flex; align-items: center; gap: 6px; }
.inline-form .check input { padding: 0; }
.mem-form { display: grid; gap: 6px; padding: 0 14px 12px; }
.mem-form textarea { resize: vertical; font-family: var(--sans); }
.mem-form label { color: var(--muted); font-size: 13px; }
.mem-form .btn { justify-self: start; }
.list { list-style: none; margin: 0; padding: 0 0 8px; }
.list > li { padding: 10px 14px; border-top: 1px solid var(--border); }
.list > li:has(> .list-item) { padding: 0; }
.list-item { display: grid; gap: 2px; width: 100%; padding: 8px 14px; background: none; border: 0; border-radius: 0; color: var(--text); font: inherit; text-align: start; cursor: pointer; }
.list-item:hover { background: var(--panel2); }
.list-item:focus-visible { outline-offset: -2px; }
.list-item > span { overflow: hidden; text-overflow: ellipsis; white-space: pre; }
.list-item mark { background: var(--accent-bg); color: var(--text); border-radius: 3px; padding: 0 2px; box-shadow: 0 0 0 1px var(--accent); }
.task summary { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; cursor: pointer; }
.task-body { padding: 10px 0 4px; display: grid; gap: 8px; }
.task-body pre { margin: 0; padding: 10px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; overflow: auto; font: 13px/1.5 var(--mono); max-height: 200px; }
.badge { padding: 1px 10px; border-radius: 999px; font-size: 12px; background: var(--panel2); border: 1px solid var(--border); }
.s-done { color: var(--ok); border-color: var(--ok); }
.s-failed { color: var(--err); border-color: var(--err); }
.s-running { color: var(--accent); border-color: var(--accent); }
.s-cancelled { color: var(--muted); }
.s-queued { color: var(--warn); border-color: var(--warn); }
.mem-meta { color: var(--muted); font-size: 13px; display: flex; flex-wrap: wrap; gap: 10px; }
.mem-text { white-space: pre-wrap; overflow-wrap: anywhere; margin: 4px 0 0; }
.chip { background: var(--panel2); border: 1px solid var(--border); border-radius: 999px; padding: 0 10px; }

.statusbar {
  grid-area: status; display: flex; gap: 18px; align-items: center; padding: 0 14px;
  background: #123660; color: #dbe9ff; font-size: 13px;
}
#status-msg[data-kind="error"] { color: #ffb3b8; font-weight: 600; }
#status-msg[data-kind="warn"] { color: #ffe0a3; }
#status-msg[data-kind="ok"] { color: #b6f0d2; }

/* Diálogos */
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--border-strong); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 24px)); box-shadow: 0 30px 80px rgba(0, 0, 0, .6); }
dialog::backdrop { background: rgba(0, 0, 0, .65); backdrop-filter: blur(2px); }
.dlg-body { display: flex; flex-direction: column; gap: 12px; padding: 24px; margin: 0; }
.dlg-body p { margin: 0; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }
.trash-item { display: flex; justify-content: space-between; align-items: center; gap: 12px; }

/* Navegación por secciones */
.nav { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; min-width: 0; }
.navbtn {
  background: none; border: 0; border-radius: 7px; color: var(--muted); font: inherit;
  padding: 6px 12px; cursor: pointer; white-space: nowrap; min-height: 34px;
}
.navbtn:hover { color: var(--text); background: var(--panel2); }
.navbtn[aria-current="page"] { color: var(--text); background: var(--accent-bg); box-shadow: inset 0 -2px 0 var(--accent); }

/* Vistas: panel del jefe, cerebro, planificador y creaciones */
.view { flex: 1; min-height: 0; overflow: auto; padding: 28px 32px 40px; display: grid; gap: 20px; align-content: start; }
.view-head { display: grid; gap: 4px; }
.view-head h1 { font-size: 24px; letter-spacing: -.01em; }
.view-head p { margin: 0; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; display: grid; gap: 2px; }
.stat b { font-size: 30px; line-height: 1.15; font-variant-numeric: tabular-nums; }
.cols { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 12px; align-items: start; }
.card textarea { resize: vertical; min-height: 90px; font-family: var(--sans); }
.welcome-start p { margin: 0; color: var(--text); }
.steps { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.steps li {
  display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; gap: 14px; align-items: center;
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px;
}
.steps p { margin: 2px 0 0; }
.step-n { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--panel2); border: 1px solid var(--border-strong); font-weight: 600; }
.dropzone {
  display: grid; gap: 8px; justify-items: start; padding: 28px; border: 1px dashed var(--border-strong);
  border-radius: 14px; background: var(--panel);
}
.dropzone p { margin: 0; color: var(--muted); max-width: 62ch; }
.tag { justify-self: start; font-size: 11px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border-strong); color: var(--warn); }
.tag.ok { color: var(--ok); }

@media (max-width: 900px) {
  .view { padding: 18px 16px 32px; }
  .steps li { grid-template-columns: 32px minmax(0, 1fr); }
  .steps li .badge { grid-column: 2; justify-self: start; }
  .topbar { gap: 8px; padding: 0 10px; }
}

/* Asas para redimensionar */
.resizer { position: absolute; z-index: 5; background: transparent; transition: background .12s; touch-action: none; }
.resizer:hover, .resizer.dragging { background: var(--accent); }
.resizer.v { top: 0; bottom: 0; inset-inline-end: -3px; width: 6px; cursor: col-resize; }
.resizer.h { left: 0; right: 0; top: -3px; height: 6px; cursor: row-resize; }
body.resizing { user-select: none; }

@media (max-width: 900px) {
  .app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: 48px minmax(0, 38vh) minmax(0, 1fr) 26px;
    grid-template-areas: "top" "side" "main" "status";
  }
  .sidebar { border-inline-end: 0; border-bottom: 1px solid var(--border); }
  .panel { height: min(var(--panel-h), 40vh); }
  #ctx { display: none; }
  .resizer.v { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .row .ico, .btn, .resizer { transition: none; }
}
