/* Cerebro Central - hoja de estilos unica.
   Tema oscuro, una escala de espaciado, tipografia del sistema.
   Sin recursos externos: todo se sirve desde este mismo origen. */

:root {
  /* escala de espaciado: todo multiplo de 4 */
  --s1: 4px;  --s2: 8px;  --s3: 12px; --s4: 16px;
  --s5: 24px; --s6: 32px; --s7: 48px;

  --bg:        #0d1319;
  --bg-sunk:   #090e13;
  --surface:   #141c25;
  --surface-2: #1a2430;
  --surface-3: #212d3b;
  --border:    #25323f;
  --border-st: #33455a;

  --ink:       #e7eef6;
  --ink-2:     #b6c4d4;
  --mut:       #8798a9;
  --mut-2:     #6b7c8e;

  --acc:       #4a9eff;
  --acc-ink:   #06203d;
  --acc-soft:  #16304d;

  --ok:        #35c08b;
  --warn:      #e3a94b;
  --bad:       #f2675f;

  /* Paleta de estado. Reservada para salud y capacidad: nunca se reusa como
     color de serie. Pasa el piso de contraste contra la superficie y el piso
     de vision normal; el par verde/ambar queda en la banda limite para
     protanopia, por eso cada estado va SIEMPRE acompanado de su nombre
     escrito y, en los medidores, del numero. El color no es nunca el unico
     portador del estado. */
  --health-green:  #35c08b;
  --health-yellow: #e3a94b;
  --health-red:    #f2675f;
  --health-down:   #7d8898;

  --radius:    6px;
  --radius-sm: 4px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --shadow: 0 10px 30px rgba(0,0,0,.45);
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }

:focus-visible {
  outline: 2px solid var(--acc);
  outline-offset: 2px;
  border-radius: 2px;
}

.skiplink {
  position: fixed; top: var(--s2); left: var(--s2); z-index: 60;
  background: var(--acc); color: var(--acc-ink);
  padding: var(--s2) var(--s3); border-radius: var(--radius-sm);
  font-weight: 650; text-decoration: none;
  transform: translateY(-200%);
}
.skiplink:focus { transform: none; }

/* ---------------------------------------------------------------- arranque */
.boot {
  position: fixed; inset: 0; display: grid; place-items: center;
  background: var(--bg); padding: var(--s4); z-index: 50;
}
.boot-card {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius); padding: var(--s5) var(--s6); text-align: center;
  max-width: 28rem;
}
.boot-title { margin: 0 0 var(--s2); font-weight: 650; letter-spacing: .01em; }
.boot-note { margin: 0; color: var(--mut); font-size: .9rem; }

/* ------------------------------------------------------------------ ingreso */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: var(--s5) var(--s4); }
.login-card {
  width: 100%; max-width: 24rem;
  border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius); padding: var(--s6) var(--s5);
}
.login-card h1 { font-size: 1.05rem; margin: 0 0 var(--s1); font-weight: 650; }
.login-card .sub-note { margin: 0 0 var(--s5); color: var(--mut); font-size: .86rem; }

/* -------------------------------------------------------------------- shell */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }

.sidebar {
  background: var(--bg-sunk); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand { padding: var(--s4); border-bottom: 1px solid var(--border); }
.brand-name { font-weight: 650; font-size: .95rem; letter-spacing: .01em; display: block; }
.brand-env {
  display: inline-block; margin-top: var(--s2);
  font-size: .64rem; text-transform: uppercase; letter-spacing: .14em;
  color: var(--mut); border: 1px solid var(--border-st);
  padding: 1px var(--s2); border-radius: 3px;
}
.sidenav { flex: 1; overflow-y: auto; padding: var(--s3) var(--s2); }
.sidenav-group { margin: 0 0 var(--s4); }
.sidenav-label {
  font-size: .66rem; text-transform: uppercase; letter-spacing: .13em;
  color: var(--mut-2); padding: 0 var(--s3) var(--s2);
}
.sidenav a {
  display: block; padding: var(--s2) var(--s3); margin-bottom: 2px;
  color: var(--ink-2); text-decoration: none; border-radius: var(--radius-sm);
  font-size: .89rem; border-left: 2px solid transparent;
}
.sidenav a:hover { background: var(--surface-2); color: var(--ink); }
.sidenav a[aria-current="page"] {
  background: var(--acc-soft); color: #cfe4ff; border-left-color: var(--acc); font-weight: 600;
}
.sidebar-foot {
  border-top: 1px solid var(--border); padding: var(--s3) var(--s4);
  font-size: .78rem; color: var(--mut);
}

.mainarea { display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: var(--s3);
  padding: var(--s3) var(--s5); border-bottom: 1px solid var(--border);
  background: var(--bg); position: sticky; top: 0; z-index: 20;
}
.topbar h1 { font-size: 1rem; margin: 0; font-weight: 650; }
.topbar .grow { flex: 1; }
.userbox { display: flex; align-items: center; gap: var(--s3); font-size: .85rem; }
.userbox .uname { color: var(--ink); font-weight: 600; }
.userbox .urole { color: var(--mut); font-size: .78rem; }
.navtoggle { display: none; }

main { padding: var(--s5); flex: 1; min-width: 0; }
main:focus { outline: none; }

.page-head { margin-bottom: var(--s5); }
.page-head h2 { font-size: 1.1rem; margin: 0 0 var(--s2); font-weight: 650; }
.page-head p { margin: 0; color: var(--mut); font-size: .87rem; max-width: 62ch; }
.page-actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s4); }

/* --------------------------------------------------------------- superficies */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); margin-bottom: var(--s4);
}
.card-head {
  padding: var(--s3) var(--s4); border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap;
}
.card-head h3 { margin: 0; font-size: .9rem; font-weight: 650; }
.card-head .grow { flex: 1; }
.card-head .hint { color: var(--mut); font-size: .78rem; }
.card-body { padding: var(--s4); }
.card-body.flush { padding: 0; }

.grid { display: grid; gap: var(--s4); }
.grid.cols-2 { grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }
.grid.cols-4 { grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); }

/* --------------------------------------------------------------- indicadores */
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: var(--s4);
}
.stat-label {
  font-size: .7rem; text-transform: uppercase; letter-spacing: .1em;
  color: var(--mut); margin: 0 0 var(--s2);
}
.stat-value { font-size: 1.7rem; line-height: 1.1; font-weight: 650; margin: 0; font-variant-numeric: tabular-nums; }
.stat-note { margin: var(--s2) 0 0; font-size: .78rem; color: var(--mut); }
.stat.is-bad .stat-value { color: var(--bad); }
.stat.is-warn .stat-value { color: var(--warn); }
.stat.is-ok .stat-value { color: var(--ok); }

.badge {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: .74rem; font-weight: 600; letter-spacing: .02em;
  padding: 2px var(--s2); border-radius: 999px;
  border: 1px solid var(--border-st); background: var(--surface-2); color: var(--ink-2);
  white-space: nowrap;
}
.badge::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; flex: none; }
.badge.plain::before { display: none; }
.badge.green  { color: #8ff0c6; border-color: #1f6a4e; background: #10281f; }
.badge.yellow { color: #f6d79a; border-color: #6d5522; background: #2a2114; }
.badge.red    { color: #ffc0bb; border-color: #7a322d; background: #2c1715; }
.badge.down   { color: #c2ccd8; border-color: #3b4654; background: #1d242c; }
.badge.ok     { color: #8ff0c6; border-color: #1f6a4e; background: #10281f; }
.badge.near   { color: #f6d79a; border-color: #6d5522; background: #2a2114; }
.badge.over   { color: #ffc0bb; border-color: #7a322d; background: #2c1715; }
.badge.blocked{ color: #e7b7ff; border-color: #5c3573; background: #241a2d; }
.badge.info   { color: #bcdcff; border-color: #2a4e78; background: #122132; }
.badge.unknown{ color: var(--mut); }

.meter-track {
  height: 6px; border-radius: 3px; background: var(--surface-3);
  overflow: hidden; margin-top: 6px; min-width: 7rem;
}
.meter-fill { height: 100%; background: var(--ok); border-radius: 3px; }
.meter-fill.near { background: var(--warn); }
.meter-fill.over { background: var(--bad); }
.meter-fill.blocked { background: #a855f7; }
.meter-fill.unknown { background: var(--mut-2); }
.meter-text { font-size: .8rem; font-variant-numeric: tabular-nums; color: var(--ink-2); }

/* -------------------------------------------------------------------- tablas */
.table-scroll { overflow-x: auto; }
table.data { width: 100%; border-collapse: collapse; font-size: .87rem; }
table.data th, table.data td {
  text-align: left; padding: var(--s3) var(--s4);
  border-bottom: 1px solid var(--border); vertical-align: top;
}
table.data thead th {
  background: var(--surface-2);
  font-size: .72rem; text-transform: uppercase; letter-spacing: .08em;
  color: var(--mut); font-weight: 650; white-space: nowrap;
}
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr:last-child td { border-bottom: 0; }
table.data td.num, table.data th.num { text-align: right; font-variant-numeric: tabular-nums; }
table.data td.nowrap { white-space: nowrap; }
th.sortable { padding: 0; }
th.sortable button {
  all: unset; display: flex; align-items: center; gap: 6px; cursor: pointer;
  padding: var(--s3) var(--s4); width: 100%; box-sizing: border-box;
  font: inherit; color: inherit; text-transform: inherit; letter-spacing: inherit;
}
th.sortable button:hover { color: var(--ink); }
th.sortable .arrow { font-size: .7rem; color: var(--acc); }
th.num.sortable button { justify-content: flex-end; }

.rowlink { color: var(--acc); text-decoration: none; font-weight: 600; }
.rowlink:hover { text-decoration: underline; }
.sub { color: var(--mut); font-size: .78rem; display: block; margin-top: 2px; }
.mono { font-family: var(--mono); font-size: .82rem; }

.pager {
  display: flex; align-items: center; gap: var(--s3); flex-wrap: wrap;
  padding: var(--s3) var(--s4); border-top: 1px solid var(--border);
  font-size: .82rem; color: var(--mut);
}
.pager .grow { flex: 1; }

/* ------------------------------------------------------------------- filtros */
.filters {
  display: flex; flex-wrap: wrap; gap: var(--s3); align-items: flex-end;
  padding: var(--s4); border-bottom: 1px solid var(--border);
}
.filters .field { margin: 0; min-width: 9rem; }
.filters .field.wide { min-width: 15rem; flex: 1; }
.filters .filter-actions { display: flex; gap: var(--s2); }

/* ---------------------------------------------------------------- formularios */
.field { margin-bottom: var(--s4); }
.field > label, .field > .label {
  display: block; font-size: .78rem; color: var(--ink-2);
  margin-bottom: var(--s2); font-weight: 600;
}
.field .req { color: var(--bad); }
.field-hint { font-size: .76rem; color: var(--mut); margin-top: var(--s2); }
.field-error { font-size: .78rem; color: var(--bad); margin-top: var(--s2); }

input[type=text], input[type=password], input[type=number], input[type=search],
input[type=date], input[type=tel], select, textarea {
  width: 100%; padding: var(--s2) var(--s3);
  background: var(--bg-sunk); color: var(--ink);
  border: 1px solid var(--border-st); border-radius: var(--radius-sm);
  font: inherit; font-size: .88rem;
}
textarea { min-height: 5rem; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--acc); box-shadow: 0 0 0 2px var(--acc-soft); }
input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] { border-color: var(--bad); }
input:disabled, select:disabled, textarea:disabled { opacity: .55; cursor: not-allowed; }
input::placeholder, textarea::placeholder { color: var(--mut-2); }

.check { display: flex; align-items: flex-start; gap: var(--s2); font-size: .88rem; }
.check input { width: auto; margin: 3px 0 0; accent-color: var(--acc); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: var(--s2) var(--s4); border-radius: var(--radius-sm);
  background: var(--acc); color: var(--acc-ink); border: 1px solid transparent;
  font: inherit; font-size: .86rem; font-weight: 650; cursor: pointer; white-space: nowrap;
  text-decoration: none;
}
.btn:hover:not(:disabled) { filter: brightness(1.1); }
.btn:disabled { opacity: .5; cursor: not-allowed; filter: none; }
.btn.ghost { background: transparent; color: var(--ink-2); border-color: var(--border-st); font-weight: 600; }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); color: var(--ink); filter: none; }
.btn.danger { background: #45201d; color: #ffd0cc; border-color: #7a322d; }
.btn.danger:hover:not(:disabled) { background: #572824; filter: none; }
.btn.sm { padding: 3px var(--s3); font-size: .78rem; }
.btn.link { all: unset; color: var(--acc); cursor: pointer; font: inherit; font-size: .84rem; text-decoration: underline; }
.btn.link:focus-visible { outline: 2px solid var(--acc); outline-offset: 2px; }

.actions { display: flex; flex-wrap: wrap; gap: var(--s2); }
.actions.end { justify-content: flex-end; }
.form-foot { display: flex; gap: var(--s2); justify-content: flex-end; margin-top: var(--s5); }

/* --------------------------------------------------------------- estados UI */
.state {
  border: 1px solid var(--border); background: var(--surface);
  border-radius: var(--radius); padding: var(--s6) var(--s5);
  text-align: center; max-width: 46rem; margin: 0 auto;
}
.state-kicker {
  font-size: .68rem; text-transform: uppercase; letter-spacing: .14em;
  color: var(--mut); margin: 0 0 var(--s3);
}
.state h3 { margin: 0 0 var(--s3); font-size: 1rem; font-weight: 650; }
.state p { margin: 0 auto var(--s3); color: var(--ink-2); font-size: .89rem; max-width: 54ch; }
.state .reqid {
  display: inline-block; margin-top: var(--s2); font-family: var(--mono);
  font-size: .76rem; color: var(--mut); background: var(--bg-sunk);
  border: 1px solid var(--border); border-radius: var(--radius-sm); padding: var(--s1) var(--s2);
  user-select: all; word-break: break-all;
}
.state .state-actions { display: flex; gap: var(--s2); justify-content: center; margin-top: var(--s4); flex-wrap: wrap; }
.state.err { border-color: #5c2a26; }
.state.err h3 { color: #ffbdb7; }
.state.pending { border-color: #5a4a1f; }
.state.pending h3 { color: #f6d79a; }
.state.inline { padding: var(--s5) var(--s4); margin: 0; border: 0; background: transparent; }

.loading { display: flex; align-items: center; gap: var(--s3); padding: var(--s6); justify-content: center; color: var(--mut); font-size: .88rem; }
.spinner {
  width: 16px; height: 16px; border-radius: 50%; flex: none;
  border: 2px solid var(--border-st); border-top-color: var(--acc);
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

.banner {
  border: 1px solid var(--border); border-left: 3px solid var(--acc);
  background: var(--surface-2); border-radius: var(--radius-sm);
  padding: var(--s3) var(--s4); font-size: .85rem; margin-bottom: var(--s4);
}
.banner.err { border-left-color: var(--bad); color: #ffcdc9; }
.banner.warn { border-left-color: var(--warn); color: #f6dfb4; }
.banner.ok { border-left-color: var(--ok); color: #bdf0da; }
.banner .reqid { display: block; margin-top: var(--s2); font-family: var(--mono); font-size: .74rem; color: var(--mut); user-select: all; }

/* ------------------------------------------------------------- definiciones */
dl.defs { display: grid; grid-template-columns: minmax(8rem, auto) 1fr; gap: var(--s2) var(--s4); margin: 0; font-size: .87rem; }
dl.defs dt { color: var(--mut); }
dl.defs dd { margin: 0; overflow-wrap: anywhere; }
.empty-val { color: var(--mut-2); }

/* ------------------------------------------------------------ ciclo de vida */
.lifecycle { list-style: none; margin: 0; padding: 0; }
.lifecycle li {
  display: flex; gap: var(--s3); padding: var(--s3) 0;
  border-bottom: 1px solid var(--border); align-items: flex-start;
}
.lifecycle li:last-child { border-bottom: 0; }
.lifecycle .mark {
  flex: none; width: 20px; height: 20px; border-radius: 50%;
  border: 1px solid var(--border-st); display: grid; place-items: center;
  font-size: .7rem; color: var(--mut); margin-top: 1px;
}
.lifecycle li.done .mark { background: #10281f; border-color: #1f6a4e; color: #8ff0c6; }
.lifecycle li.next .mark { background: var(--acc-soft); border-color: var(--acc); color: #cfe4ff; }
.lifecycle .step-name { font-weight: 600; font-size: .87rem; }
.lifecycle .step-note { color: var(--mut); font-size: .79rem; margin-top: 2px; }
.lifecycle li.next .step-name { color: #cfe4ff; }
.lifecycle .step-body { flex: 1; min-width: 0; }

/* -------------------------------------------------------------------- hilos */
.thread { display: flex; flex-direction: column; gap: var(--s3); padding: var(--s4); }
.msg { max-width: 46rem; border: 1px solid var(--border); border-radius: var(--radius); padding: var(--s3) var(--s4); background: var(--surface-2); }
.msg.out { align-self: flex-end; background: #14263c; border-color: #27456b; }
.msg.internal { align-self: center; width: 100%; max-width: none; background: #241f14; border-color: #5a4a1f; border-style: dashed; }
.msg-meta { display: flex; gap: var(--s3); flex-wrap: wrap; align-items: center; font-size: .72rem; color: var(--mut); margin-bottom: var(--s2); }
.msg-body { white-space: pre-wrap; overflow-wrap: anywhere; font-size: .88rem; }
.msg-body.none { color: var(--mut-2); font-style: italic; }

/* ------------------------------------------------------------------ dialogos */
dialog.modal {
  border: 1px solid var(--border-st); background: var(--surface);
  color: var(--ink); border-radius: var(--radius); padding: 0;
  width: min(100% - 2rem, 34rem); box-shadow: var(--shadow);
}
dialog.modal.wide { width: min(100% - 2rem, 52rem); }
dialog.modal::backdrop { background: rgba(4,8,12,.72); }
.modal-head { padding: var(--s4); border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: .98rem; font-weight: 650; }
.modal-head p { margin: var(--s2) 0 0; color: var(--mut); font-size: .83rem; }
.modal-body { padding: var(--s4); max-height: min(60vh, 32rem); overflow-y: auto; }
.modal-foot { padding: var(--s3) var(--s4); border-top: 1px solid var(--border); display: flex; gap: var(--s2); justify-content: flex-end; flex-wrap: wrap; }

.secret-reveal { display: flex; gap: var(--s2); align-items: stretch; margin-top: var(--s3); }
.secret-reveal input { font-family: var(--mono); font-size: .95rem; letter-spacing: .02em; }

/* ------------------------------------------------------------------- avisos */
.toasts {
  position: fixed; right: var(--s4); bottom: var(--s4); z-index: 70;
  display: flex; flex-direction: column; gap: var(--s2); max-width: 24rem;
}
.toast {
  border: 1px solid var(--border-st); background: var(--surface-3);
  border-radius: var(--radius-sm); padding: var(--s3) var(--s4);
  font-size: .85rem; box-shadow: var(--shadow);
}
.toast.ok { border-color: #1f6a4e; background: #10281f; color: #bdf0da; }
.toast.err { border-color: #7a322d; background: #2c1715; color: #ffcdc9; }

.chiprow { display: flex; flex-wrap: wrap; gap: var(--s2); align-items: center; }
.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.detail-row td { background: var(--bg-sunk); }
.detail-row pre {
  margin: 0; font-family: var(--mono); font-size: .78rem; color: var(--ink-2);
  white-space: pre-wrap; overflow-wrap: anywhere; max-height: 22rem; overflow: auto;
}

/* ---------------------------------------------------------------- responsive */
@media (max-width: 1100px) {
  main { padding: var(--s4); }
  .topbar { padding: var(--s3) var(--s4); }
}
@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: 250px; z-index: 40;
    transform: translateX(-100%); transition: transform .16s ease-out;
    box-shadow: var(--shadow);
  }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .nav-scrim {
    position: fixed; inset: 0; background: rgba(4,8,12,.6); z-index: 30; display: block;
  }
  .nav-scrim { display: none; }
  .navtoggle { display: inline-flex; }
  .userbox .urole { display: none; }
  table.data th, table.data td { padding: var(--s2) var(--s3); }
  dl.defs { grid-template-columns: 1fr; gap: var(--s1) 0; }
  dl.defs dd { margin-bottom: var(--s2); }
}
@media (prefers-reduced-motion: reduce) { .sidebar { transition: none; } }
