/*
 * Centro de mando — estilos.
 *
 * Se usa en dos sitios muy distintos: una pantalla grande en la oficina y un teléfono a las 3 de la
 * mañana. De ahí dos decisiones:
 *  - El menú se colapsa en cualquier tamaño (botón ☰). En teléfono entra como panel sobre el
 *    contenido, con fondo oscurecido; en pantalla grande simplemente deja de ocupar espacio.
 *  - Nada se desborda: las tablas scrollean dentro de su caja y las rejillas bajan a una columna.
 */

:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2330;
  --panel-3: #222b38;
  --border: #2b3542;
  --border-soft: #212a36;
  --text: #e6edf3;
  --muted: #8b98a5;
  --ok: #3fb950;
  --warn: #d29922;
  --crit: #f85149;
  --info: #4c8eda;
  --none: #6e7681;
  --sidebar-w: 236px;
  --radius: 10px;
  --shadow: 0 8px 28px rgba(0, 0, 0, .45);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  -webkit-text-size-adjust: 100%;
}

a { color: var(--info); }

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

.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;
}

/* ── Armazón ─────────────────────────────────────────────────────────────── */

/*
 * Nada de rejilla acá: el menú va `position: fixed` (así, al colapsarlo, se desplaza fuera de la
 * pantalla sin generar scroll horizontal), y un elemento fijo SALE del flujo. Con `display: grid`
 * el contenido pasaba a ocupar la primera columna y se dibujaba debajo del menú. El sitio se
 * reserva con el margen de `.main`.
 */
.app { min-height: 100%; }

.main {
  min-width: 0;
  display: flex;
  flex-direction: column;
  margin-left: var(--sidebar-w);
  transition: margin-left .18s ease;
}

.app.nav-hidden .main { margin-left: 0; }

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 0 16px;
  overflow: hidden auto;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  width: var(--sidebar-w);
  z-index: 9;
  transition: transform .18s ease;
}

.app.nav-hidden .sidebar { transform: translateX(-100%); }

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 18px 18px;
  font-weight: 650;
  font-size: 15px;
  white-space: nowrap;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 28px; height: 28px;
  flex: 0 0 28px;
  border-radius: 8px;
  background: linear-gradient(140deg, var(--info), #2f6fb4);
  color: #fff;
  font-weight: 700;
}

.brand-text span {
  display: block;
  font-weight: 400;
  font-size: 11.5px;
  color: var(--muted);
}

nav { display: flex; flex-direction: column; gap: 1px; }

nav a {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 18px;
  color: var(--text);
  text-decoration: none;
  border-left: 3px solid transparent;
  white-space: nowrap;
}

nav a:hover { background: var(--panel-2); }
nav a.active { background: var(--panel-2); border-left-color: var(--info); font-weight: 600; }

.user {
  margin-top: auto;
  padding: 14px 18px 0;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  word-break: break-word;
}

.user button { margin-top: 8px; }

.backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .55);
  z-index: 8;
  border: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(22, 27, 34, .92);
  backdrop-filter: blur(6px);
  position: sticky;
  top: 0;
  z-index: 4;
}

.topbar h1 {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-right { display: flex; gap: 10px; align-items: center; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 34px; height: 34px;
  flex: 0 0 34px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
}

.icon-btn:hover { background: var(--panel-2); border-color: var(--info); }

.chip {
  font-size: 11.5px;
  color: var(--muted);
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: 999px;
  padding: 3px 10px;
  white-space: nowrap;
}

.view { padding: 18px 20px 56px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }

h2 { font-size: 14.5px; margin: 24px 0 10px; letter-spacing: .01em; }
h2:first-child { margin-top: 0; }

/* ── Controles ───────────────────────────────────────────────────────────── */

.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 13px;
  cursor: pointer;
  font: inherit;
  white-space: nowrap;
}

.btn:hover { border-color: var(--info); background: var(--panel-3); }
.btn.primary { background: var(--info); border-color: var(--info); color: #fff; }
.btn.primary:hover { background: #5b9ce8; }
.btn.danger { border-color: var(--crit); color: var(--crit); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }

input, select, textarea {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
  font: inherit;
  max-width: 100%;
}

input[type="checkbox"] { width: 16px; height: 16px; accent-color: var(--info); }

label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
label.inline { flex-direction: row; align-items: center; gap: 7px; color: var(--text); font-size: 13px; }

.form-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; margin-bottom: 12px; }

.tabs { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 14px; }
.tabs .btn.active { border-color: var(--info); background: #1b3250; font-weight: 600; }

/* ── Tarjetas ────────────────────────────────────────────────────────────── */

.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(255px, 1fr)); }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 15px;
  min-width: 0;
}

.card .label { color: var(--muted); font-size: 12px; margin-bottom: 5px; display: flex; align-items: center; gap: 2px; }
.card .value { font-size: 23px; font-weight: 650; letter-spacing: -.01em; }
.card .sub { color: var(--muted); font-size: 11.5px; margin-top: 2px; }
.card .sub.hint { color: var(--none); }
.card.lvl-warn { border-color: var(--warn); }
.card.lvl-crit { border-color: var(--crit); box-shadow: inset 0 0 0 1px rgba(248, 81, 73, .18); }
.lvl-ok .value, .t-ok { color: var(--ok); }
.lvl-warn .value, .t-warn { color: var(--warn); }
.lvl-crit .value, .t-crit { color: var(--crit); }
.lvl-none .value { color: var(--none); }

button.card {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
  position: relative;
}

button.card:hover, button.card:focus-visible { border-color: var(--info); }

.card-more {
  position: absolute;
  top: 11px; right: 13px;
  font-size: 11px;
  color: var(--info);
  opacity: 0;
  transition: opacity .12s;
}

button.card:hover .card-more, button.card:focus-visible .card-more { opacity: 1; }
/* Se reserva el sitio del "Ver logs →" siempre: en teléfono está visible y el título es largo. */
button.card .label { padding-right: 68px; }

/* ── Señales de estado ───────────────────────────────────────────────────── */

.dot {
  display: inline-block;
  width: 9px; height: 9px;
  border-radius: 50%;
  margin-right: 7px;
  flex: 0 0 9px;
  background: var(--none);
}

.dot.up, .dot.OK { background: var(--ok); box-shadow: 0 0 0 3px rgba(63, 185, 80, .16); }
.dot.down, .dot.ALARM { background: var(--crit); box-shadow: 0 0 0 3px rgba(248, 81, 73, .18); }
.dot.unknown, .dot.INSUFFICIENT_DATA { background: var(--warn); box-shadow: 0 0 0 3px rgba(210, 153, 34, .16); }

.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: .03em;
  border: 1px solid currentColor;
  white-space: nowrap;
}

.badge.CRITICA { color: var(--crit); }
.badge.ALTA { color: var(--warn); }
.badge.MEDIA { color: var(--info); }

.count { background: var(--crit); color: #fff; border-radius: 999px; padding: 1px 8px; font-size: 11px; font-weight: 700; }

/* ── Tablas ──────────────────────────────────────────────────────────────── */

.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
  -webkit-overflow-scrolling: touch;
}

table { width: 100%; border-collapse: collapse; min-width: 520px; }
th, td { text-align: left; padding: 9px 11px; border-bottom: 1px solid var(--border-soft); vertical-align: top; }
th { color: var(--muted); font-weight: 500; font-size: 11.5px; background: var(--panel-2); }
tr:last-child td { border-bottom: 0; }
td.nowrap, th.nowrap { white-space: nowrap; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
tfoot th { background: var(--panel-2); border-top: 1px solid var(--border); color: var(--text); font-weight: 650; }
tr.row-click { cursor: pointer; }
tr.row-click:hover td { background: var(--panel-2); }
td.spark { width: 110px; padding: 4px 10px; }
td.spark svg.chart { height: 28px; margin: 0; }

pre.log {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font: 12px/1.5 ui-monospace, SFMono-Regular, Consolas, monospace;
  max-height: 8.5em;
  overflow: hidden;
  cursor: pointer;
}

pre.log.open { max-height: none; }
mark { background: #9e6a03; color: #fff; border-radius: 2px; padding: 0 1px; }

/* ── Avisos ──────────────────────────────────────────────────────────────── */

.empty {
  color: var(--muted);
  padding: 22px;
  text-align: center;
  background: var(--panel);
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.error {
  color: #ffb3ae;
  background: #2b1416;
  border: 1px solid var(--crit);
  border-radius: 8px;
  padding: 10px 13px;
  margin-bottom: 12px;
}

.notice {
  color: var(--muted);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 13px;
  margin-bottom: 12px;
}

.notice b { color: var(--text); }
.notice p { margin: 6px 0 0; }

/* ── Panel lateral de logs ───────────────────────────────────────────────── */

.drawer {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  width: min(920px, 94vw);
  background: var(--panel);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  z-index: 10;
  display: flex;
  flex-direction: column;
}

.drawer header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.drawer header .grow { flex: 1; min-width: 180px; }
.drawer > .muted { padding: 8px 16px 0; }
.drawer .body-note { padding: 0 16px; }
.drawer .body-note .notice { margin: 8px 0 0; }
.drawer .body { overflow: auto; padding: 10px 16px 24px; flex: 1; }
.drawer .line { padding: 8px 0; border-bottom: 1px solid var(--border-soft); }
.drawer .line-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 4px; }

/* ── Contexto de una línea ───────────────────────────────────────────────── */

.ctx { margin: 10px 0 4px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg); overflow: hidden; }

.ctx-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  padding: 8px 12px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--border);
}

.ctx-head .grow { min-width: 0; flex: 1; }
.ctx-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.ctx-controls .tabs { margin: 0; }
.ctx-body { max-height: 420px; overflow: auto; padding: 4px 0; }
.ctx-line { display: grid; grid-template-columns: 150px minmax(0, 1fr); gap: 10px; padding: 3px 12px; }
.ctx-line.hit { background: #2d2410; box-shadow: inset 3px 0 0 var(--warn); }
.ctx-line pre.log { max-height: none; cursor: default; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.nowrap { white-space: nowrap; }

/* ── Gráficas ────────────────────────────────────────────────────────────── */

svg.chart { width: 100%; height: 64px; display: block; margin-top: 10px; }
svg.chart .area { fill: rgba(76, 142, 218, .15); }
svg.chart .line { fill: none; stroke: var(--info); stroke-width: 1.5; }
svg.chart .warn { stroke: var(--warn); stroke-dasharray: 3 3; stroke-width: 1; }
svg.chart .crit { stroke: var(--crit); stroke-dasharray: 3 3; stroke-width: 1; }

.login { display: flex; align-items: center; justify-content: center; min-height: 100vh; flex-direction: column; gap: 16px; padding: 24px; text-align: center; }

/* ── Wiki ────────────────────────────────────────────────────────────────── */

.wiki-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 18px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.wiki-bar input[type="search"] { flex: 1; min-width: 200px; }
.wiki-content { min-width: 0; }
.wiki-head { margin-bottom: 16px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.wiki-head h2 { margin: 0 0 3px; font-size: 20px; }

.wiki-results { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }

.wiki-result {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  cursor: pointer;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 13px;
}

.wiki-result:hover, .wiki-result:focus-visible { border-color: var(--info); }
.wiki-result-title { font-weight: 600; margin-bottom: 2px; }

/* ── Markdown renderizado ───────────────────────────────────────────────── */

.md { max-width: 86ch; line-height: 1.65; }
.md .md-h { margin: 24px 0 8px; line-height: 1.3; }
.md h2.md-h { font-size: 18px; border-bottom: 1px solid var(--border); padding-bottom: 5px; }
.md h3.md-h { font-size: 15.5px; }
.md h4.md-h, .md h5.md-h, .md h6.md-h { font-size: 14px; color: var(--muted); }
.md p { margin: 10px 0; }
.md ul, .md ol { margin: 10px 0; padding-left: 22px; }
.md li { margin: 4px 0; }

.md code {
  background: var(--panel-2);
  border: 1px solid var(--border-soft);
  border-radius: 4px;
  padding: 1px 4px;
  font: 12.5px/1.4 ui-monospace, SFMono-Regular, Consolas, monospace;
  word-break: break-word;
}

.md .md-code { margin: 13px 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--panel); }
.md .md-code-lang { padding: 5px 11px; font-size: 11px; color: var(--muted); background: var(--panel-2); border-bottom: 1px solid var(--border); }
.md .md-code pre { margin: 0; padding: 11px 13px; overflow: auto; }
.md .md-code code { background: none; border: 0; padding: 0; white-space: pre; word-break: normal; }
.md blockquote { margin: 13px 0; padding: 6px 14px; border-left: 3px solid var(--border); color: var(--muted); }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 22px 0; }
.md .md-table { margin: 14px 0; overflow-x: auto; display: block; border: 1px solid var(--border); border-radius: 8px; }
.md .md-table th, .md .md-table td { white-space: normal; }
.md .md-img, .md .md-ref { color: var(--muted); font-size: 12px; }

/* ── Teléfonos y tablets ─────────────────────────────────────────────────── */

@media (max-width: 860px) {
  :root { font-size: 15px; }

  /* El menú pasa a ser un panel sobre el contenido: no reserva espacio. */
  .main, .app.nav-hidden .main { margin-left: 0; }

  .sidebar {
    width: min(86vw, 300px);
    box-shadow: var(--shadow);
    padding-top: max(18px, env(safe-area-inset-top));
  }

  nav a { padding: 13px 18px; }

  .topbar { padding: 9px 12px; padding-top: max(9px, env(safe-area-inset-top)); }
  .topbar h1 { font-size: 15.5px; }
  .chip { display: none; }
  .btn-text { display: none; }

  .view { padding: 14px 12px 64px; }
  .grid { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .card .value { font-size: 21px; }
  .card-more { opacity: 1; }

  .form-row { gap: 10px; }
  .form-row > label { flex: 1 1 100%; }
  .form-row > label.inline { flex: 0 0 auto; }

  .drawer { width: 100vw; border-left: 0; }
  .drawer header { padding: 10px 12px; padding-top: max(10px, env(safe-area-inset-top)); }
  .drawer .body { padding: 8px 12px 32px; }

  table { min-width: 560px; }
  .md { max-width: none; }

  .ctx-line { grid-template-columns: minmax(0, 1fr); gap: 0; }
  .ctx-body { max-height: 60vh; }
}

@media (max-width: 420px) {
  .view { padding: 12px 10px 64px; }
  .topbar-right { gap: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
