:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e1e4ea;
  --text: #1a1d23;
  --muted: #646b78;
  --accent: #2f6fdf;
  --accent-text: #ffffff;
  --up: #1f9d55;
  --warn: #b97800;
  --down: #d63b3b;
  --unknown: #8a909c;
  --bar: #e6e9ef;
  --grid: #e9ebef;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --code-bg: #0f1218;
  --code-text: #d7dbe2;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1e222b;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --muted: #9aa1ad;
    --accent: #5b8ff0;
    --up: #34c172;
    --warn: #f0a92a;
    --down: #f06262;
    --unknown: #7d8491;
    --bar: #2a2f3a;
    --grid: #252a33;
    --series-1: #3987e5;
    --series-2: #d95926;
    --code-bg: #0a0c10;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115; --surface: #171a21; --surface-2: #1e222b; --border: #2a2f3a; --text: #e6e8ec; --muted: #9aa1ad;
  --accent: #5b8ff0; --up: #34c172; --warn: #f0a92a; --down: #f06262; --unknown: #7d8491; --bar: #2a2f3a; --grid: #252a33;
  --series-1: #3987e5; --series-2: #d95926; --code-bg: #0a0c10; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
h1 { font-size: 18px; }
h2 { font-size: 16px; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
h3.mt { margin-top: 16px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--down); margin: 0; }
.down-text { color: var(--down); }
code, .path { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.path { word-break: break-all; }
[hidden] { display: none !important; }

input[type="search"], input:not([type]), input[type="password"], select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 10px;
  min-width: 0;
}
input:focus-visible, select:focus-visible, .btn:focus-visible, [role=tab]:focus-visible, summary:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

.btn {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 5px 11px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: .5; cursor: progress; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.danger { color: var(--down); }
.btn.small-btn { padding: 2px 8px; font-size: 12px; }

/* ---------- Login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 360px; display: grid; gap: 14px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 28px; box-shadow: var(--shadow);
}
.login-card label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
.login-card p { margin: 0; }
.enroll { display: grid; gap: 10px; justify-items: center; text-align: center; }
.enroll img { background: #fff; border-radius: 8px; padding: 6px; }
.secret, .backup-codes {
  display: block; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  word-break: break-all; user-select: all;
}
.backup-codes { margin: 0; columns: 2; text-align: center; line-height: 1.9; }

/* ---------- Layout ---------- */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 24px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: baseline; gap: 10px; min-width: 0; flex: none; }
.topbar-right { display: flex; align-items: center; gap: 8px; margin-left: auto; flex: none; }

.tabs { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; min-width: 0; }
.tabs::-webkit-scrollbar { display: none; }
.tabs [role=tab] {
  font: inherit; font-size: 13px; color: var(--muted); background: none; border: 0;
  padding: 7px 12px; border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.tabs [role=tab]:hover { color: var(--text); background: var(--surface-2); }
.tabs [role=tab][aria-selected="true"] { color: var(--text); background: var(--surface-2); font-weight: 600; }

main { max-width: 1320px; margin: 0 auto; padding: 20px 24px 48px; }
.panel { display: grid; gap: 20px; }

.toolbar { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; }
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; }
.seg button { font: inherit; font-size: 13px; color: var(--muted); background: none; border: 0; padding: 4px 10px; border-radius: 6px; cursor: pointer; }
.seg button.on { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: var(--shadow); }

.box {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; box-shadow: var(--shadow); display: grid; gap: 10px; align-content: start; min-width: 0;
}
.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 480px), 1fr)); gap: 16px; }

/* ---------- Stats ---------- */
.host-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); gap: 12px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; box-shadow: var(--shadow); display: grid; gap: 6px; align-content: start; min-width: 0;
}
.stat.stat-warn { border-color: color-mix(in srgb, var(--warn) 55%, var(--border)); }
.stat-label { font-size: 12px; color: var(--muted); }
.stat-value { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stat-sub { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.meter { height: 6px; background: var(--bar); border-radius: 3px; overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 3px; transition: width .4s; }
.meter.warn > span { background: var(--warn); }
.meter.down > span { background: var(--down); }

/* ---------- Servicios ---------- */
.summary-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; }
.summary { display: flex; flex-wrap: wrap; gap: 14px; font-size: 13px; }
.summary span { display: inline-flex; align-items: center; gap: 6px; }
.filters { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }

#groups { display: grid; gap: 24px; }
.group { display: grid; gap: 10px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr)); gap: 12px; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 14px 16px; box-shadow: var(--shadow); display: grid; gap: 10px; min-width: 0;
}
.card.st-down { border-color: color-mix(in srgb, var(--down) 55%, var(--border)); }
.card.st-warn { border-color: color-mix(in srgb, var(--warn) 55%, var(--border)); }
.card-head { display: flex; align-items: flex-start; gap: 10px; }
.card-title { min-width: 0; flex: 1; }
.card-title strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-title .target { font-size: 12px; color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

.dot { width: 10px; height: 10px; border-radius: 50%; margin-top: 5px; flex: none; background: var(--unknown); }
.dot.up { background: var(--up); box-shadow: 0 0 0 3px color-mix(in srgb, var(--up) 20%, transparent); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warn) 22%, transparent); }
.dot.down { background: var(--down); box-shadow: 0 0 0 3px color-mix(in srgb, var(--down) 22%, transparent); }

.badge {
  font-size: 11px; padding: 2px 7px; border-radius: 999px; white-space: nowrap;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); text-decoration: none;
}
.badge.up { color: var(--up); }
.badge.warn { color: var(--warn); }
.badge.down { color: var(--down); }
.uptime { font-size: 12px; font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.uptime.up { color: var(--up); }
.uptime.warn { color: var(--warn); }
.uptime.down { color: var(--down); }

.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; font-size: 12px; margin: 0; }
.metrics div { display: grid; gap: 1px; min-width: 0; }
.metrics dt { color: var(--muted); }
.metrics dd { margin: 0; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.checks { display: flex; flex-wrap: wrap; gap: 6px; font-size: 12px; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; border-top: 1px solid var(--border); padding-top: 10px; }
.actions .spacer { flex: 1; }
.actions .btn { padding: 4px 9px; font-size: 12px; }

/* ---------- Gráficas ---------- */
.chart-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 420px), 1fr)); gap: 16px; }
.chart-grid:empty { display: none; }
.chart { margin: 0; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px 6px; box-shadow: var(--shadow); min-width: 0; }
.box .chart { border: 0; box-shadow: none; padding: 0; }
.chart figcaption { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; margin-bottom: 6px; }
.chart-title { font-weight: 600; font-size: 13px; }
.chart-latest { margin-left: auto; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.legend { display: inline-flex; gap: 10px; font-size: 12px; color: var(--muted); }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.plot { width: 100%; min-width: 0; }
.chart-slot { min-width: 0; }
.uplot { font-family: inherit; }
.u-over { cursor: crosshair; }

.tooltip {
  position: fixed; z-index: 40; pointer-events: none;
  background: var(--surface); color: var(--text); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18); padding: 8px 10px; font-size: 12px; display: grid; gap: 3px; min-width: 150px;
}
.tip-title { color: var(--muted); margin-bottom: 2px; }
.tip-row { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: center; font-variant-numeric: tabular-nums; }
.tip-swatch { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }

/* ---------- Tablas ---------- */
.table-wrap { overflow-x: auto; }
.table-wrap.tall { max-height: 420px; overflow-y: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 13px; }
.table th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 6px 8px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); }
.table td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; white-space: nowrap; }
.table td .small, .table td.empty { white-space: normal; min-width: 140px; display: inline-block; }
.table tr:last-child td { border-bottom: 0; }
.table .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.table td.empty { text-align: center; padding: 16px; }
.proc { display: inline-block; max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

/* ---------- Disco ---------- */
.tree { display: grid; gap: 2px; font-size: 13px; }
.tree details > summary { list-style: none; cursor: pointer; }
.tree details > summary::-webkit-details-marker { display: none; }
.tree details > summary .tree-path::before { content: '▸ '; color: var(--muted); }
.tree details[open] > summary .tree-path::before { content: '▾ '; }
.tree-leaf .tree-path::before { content: '  '; white-space: pre; }
.tree-children { padding-left: 16px; display: grid; gap: 2px; }
.tree-row { display: grid; grid-template-columns: minmax(0, 1fr) 30% 72px; gap: 10px; align-items: center; padding: 3px 4px; border-radius: 6px; }
.tree-row:hover { background: var(--surface-2); }
.tree-path { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.tree-bar { height: 8px; background: var(--bar); border-radius: 4px; overflow: hidden; }
.tree-bar > span { display: block; height: 100%; background: var(--series-1); border-radius: 4px; }
.tree-size { text-align: right; font-variant-numeric: tabular-nums; font-size: 12px; }
.cleanups { display: grid; gap: 8px; }
.cleanup { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; background: var(--surface-2); border-radius: 8px; }

/* ---------- Sistema / Seguridad ---------- */
.channels { display: grid; gap: 8px; }
.channel { display: grid; grid-template-columns: auto auto 1fr; gap: 8px; align-items: center; }
.row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { font-size: 12px; padding: 3px 9px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* ---------- Terminal ---------- */
.gate { max-width: 460px; }
.inline-form { display: flex; gap: 8px; }
.inline-form input { flex: 1; }
#term-wrap { display: grid; gap: 10px; }
.term { height: calc(100vh - 210px); min-height: 320px; background: var(--code-bg); border-radius: 10px; padding: 8px; overflow: hidden; }

/* ---------- Logs ---------- */
.drawer {
  position: fixed; inset: 0 0 0 auto; width: min(760px, 100vw); z-index: 20;
  background: var(--surface); border-left: 1px solid var(--border);
  display: grid; grid-template-rows: auto auto 1fr; box-shadow: -8px 0 24px rgba(0, 0, 0, .15);
}
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; padding: 14px 16px; border-bottom: 1px solid var(--border); }
.drawer-tools { display: flex; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); }
.drawer-tools input { flex: 1; }
#logs-body {
  margin: 0; overflow: auto; padding: 12px 16px;
  background: var(--code-bg); color: var(--code-text);
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap; word-break: break-all;
}
#logs-body .err { color: #ff8080; }
#logs-body .wrn { color: #f5c26b; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 30;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  font-size: 13px; max-width: calc(100vw - 32px); box-shadow: var(--shadow);
}
.toast.bad { background: var(--down); color: #fff; }

@media (max-width: 860px) {
  .topbar { flex-wrap: wrap; row-gap: 6px; padding-left: 16px; padding-right: 16px; }
  .tabs { order: 3; width: 100%; margin: 0 -4px; }
  #hostname, #updated { display: none; }
}
@media (max-width: 640px) {
  main { padding: 16px 16px 40px; }
  .filters, .filters input[type="search"] { width: 100%; }
  .host-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .stat { padding: 12px; }
  .stat-value { font-size: 19px; }
  .tree-row { grid-template-columns: minmax(0, 1fr) 64px; }
  .tree-bar { display: none; }
  .term { height: calc(100vh - 230px); }
}
