/* Health Monitor — dense operations dashboard.
   Theme is driven by data-theme on <html>; "auto" follows the OS. */

:root {
  --ok: #16a34a;
  --warn: #d97706;
  --down: #dc2626;
  --unknown: #6b7280;
  --paused: #94a3b8;

  --bg: #f4f5f7;
  --panel: #ffffff;
  --panel-alt: #fafbfc;
  --header: #ffffff;
  --border: #dfe3e8;
  --border-soft: #eceff2;
  --text: #14181f;
  --text-dim: #667085;
  --text-faint: #98a2b3;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --chip: #f1f3f6;
  --flash: #fff3c4;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);

  --row-h: 24px;
  --fs: 12px;
  --fs-sm: 11px;
  --fs-xs: 10px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;

  /* Tells the browser to theme scrollbars, form controls and autofill for us. */
  color-scheme: light;
  accent-color: var(--accent);
}

html[data-theme="dark"] {
  color-scheme: dark;
  --ok: #22c55e;
  --warn: #f59e0b;
  --down: #f04438;
  --unknown: #7d8590;
  --paused: #5b6472;

  --bg: #0d1117;
  --panel: #141920;
  --panel-alt: #171d26;
  --header: #111620;
  --border: #232b36;
  --border-soft: #1c232d;
  --text: #dde3ea;
  --text-dim: #8b96a7;
  --text-faint: #6b7688;
  --accent: #4d8dff;
  --accent-soft: #16294a;
  --chip: #1b222c;
  --flash: #3a3418;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

@media (prefers-color-scheme: dark) {
  html[data-theme="auto"] {
    color-scheme: dark;
    --ok: #22c55e; --warn: #f59e0b; --down: #f04438; --unknown: #7d8590; --paused: #5b6472;
    --bg: #0d1117; --panel: #141920; --panel-alt: #171d26; --header: #111620;
    --border: #232b36; --border-soft: #1c232d;
    --text: #dde3ea; --text-dim: #8b96a7; --text-faint: #6b7688;
    --accent: #4d8dff; --accent-soft: #16294a; --chip: #1b222c; --flash: #3a3418;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }

html {
  scrollbar-color: var(--border) transparent;
  scrollbar-width: thin;
}
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: var(--fs);
  line-height: 1.35;
  -webkit-font-smoothing: antialiased;
}

html[data-density="cozy"] { --row-h: 30px; --fs: 13px; --fs-sm: 12px; }
html[data-density="micro"] { --row-h: 20px; --fs: 11px; --fs-sm: 10px; --fs-xs: 9px; }

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
button, input, select, textarea { font-family: inherit; font-size: var(--fs); color: inherit; }

/* ---------- header ---------- */

header.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  background: var(--header);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  display: flex;
  align-items: center;
  gap: 7px;
  font-weight: 620;
  font-size: 13px;
  letter-spacing: -.01em;
  white-space: nowrap;
}

.brand .logo { width: 14px; height: 14px; }

.summary { display: flex; gap: 5px; align-items: center; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 7px;
  border-radius: 999px;
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  background: var(--chip);
  border: 1px solid transparent;
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
.pill:hover { border-color: var(--border); }
.pill[aria-pressed="true"] { border-color: var(--accent); background: var(--accent-soft); }
.pill .dot { width: 7px; height: 7px; }
.pill.ok { color: var(--ok); }
.pill.warn { color: var(--warn); }
.pill.down { color: var(--down); }
.pill.unknown { color: var(--unknown); }

.spacer { flex: 1 1 auto; }

.toolbar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }

.search {
  width: 190px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--panel);
  font-size: var(--fs-sm);
}
.search:focus { outline: none; border-color: var(--accent); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--panel);
  cursor: pointer;
  font-size: var(--fs-sm);
  white-space: nowrap;
}
.btn:hover { border-color: var(--accent); color: var(--accent); }
.btn[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); color: #fff; }
.btn.danger { color: var(--down); }
.btn.danger:hover { border-color: var(--down); }
.btn.icon { padding: 3px 6px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.meta {
  font-size: var(--fs-xs);
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* ---------- layout ---------- */

main { padding: 8px 12px 40px; }

.group {
  margin-bottom: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 7px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.group-head {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 8px;
  background: var(--panel-alt);
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
  user-select: none;
  min-height: var(--row-h);
}
.group-head:hover { background: var(--chip); }
.group.collapsed .group-head { border-bottom: none; }
.group.collapsed .rows { display: none; }

.group-name { font-weight: 600; font-size: var(--fs); letter-spacing: -.01em; }
.group-counts { display: flex; gap: 4px; margin-left: auto; }

.count {
  font-size: var(--fs-xs);
  font-variant-numeric: tabular-nums;
  padding: 0 5px;
  border-radius: 3px;
  background: var(--chip);
}
.count.ok { color: var(--ok); }
.count.warn { color: var(--warn); }
.count.down { color: var(--down); }
.count.zero { opacity: .35; }

/* ---------- rows ---------- */

.row {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: var(--row-h);
  padding: 0 8px;
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
  position: relative;
}
.svc:last-child .row, .svc:last-child .detail { border-bottom: none; }
.row:hover { background: var(--panel-alt); }
.row.status-down { background: color-mix(in srgb, var(--down) 7%, transparent); }
.row.status-warn { background: color-mix(in srgb, var(--warn) 7%, transparent); }
.row.status-down:hover { background: color-mix(in srgb, var(--down) 12%, transparent); }
.row.status-warn:hover { background: color-mix(in srgb, var(--warn) 12%, transparent); }

.chev {
  width: 12px;
  flex: 0 0 12px;
  color: var(--text-faint);
  font-size: 9px;
  transition: transform .12s ease;
  text-align: center;
}
.expanded > .chev { transform: rotate(90deg); }

.dot {
  width: 8px;
  height: 8px;
  flex: 0 0 8px;
  border-radius: 50%;
  background: var(--unknown);
}
.dot.ok { background: var(--ok); }
.dot.warn { background: var(--warn); }
.dot.down { background: var(--down); box-shadow: 0 0 0 3px color-mix(in srgb, var(--down) 22%, transparent); }
.dot.unknown { background: var(--unknown); }
.dot.paused { background: var(--paused); }

.row-name {
  flex: 0 0 auto;
  min-width: 130px;
  max-width: 240px;
  font-weight: 550;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-type {
  flex: 0 0 auto;
  font-size: var(--fs-xs);
  color: var(--text-faint);
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  white-space: nowrap;
}

.row-target {
  flex: 0 1 auto;
  min-width: 0;
  color: var(--text-dim);
  font-family: var(--mono);
  font-size: var(--fs-sm);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.row-metrics {
  flex: 1 1 auto;
  display: flex;
  gap: 5px;
  justify-content: flex-end;
  overflow: hidden;
  white-space: nowrap;
  min-width: 0;
}

.m {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
  font-size: var(--fs-sm);
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  padding: 0 4px;
  border-radius: 3px;
  background: var(--chip);
  white-space: nowrap;
}
.m .k { color: var(--text-faint); font-size: var(--fs-xs); }
.m .v { color: var(--text); }
.m.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); }
.m.warn .v { color: var(--warn); }
.m.down { background: color-mix(in srgb, var(--down) 18%, transparent); }
.m.down .v { color: var(--down); }
.m.err { background: color-mix(in srgb, var(--down) 15%, transparent); color: var(--down); font-family: var(--sans); }
.m.msg { background: color-mix(in srgb, var(--warn) 15%, transparent); color: var(--warn); font-family: var(--sans); max-width: 320px; overflow: hidden; text-overflow: ellipsis; }

.lat {
  flex: 0 0 54px;
  text-align: right;
  font-family: var(--mono);
  font-size: var(--fs-sm);
  font-variant-numeric: tabular-nums;
  color: var(--text-dim);
}

.spark { flex: 0 0 56px; height: 14px; display: block; }

.ago {
  flex: 0 0 46px;
  text-align: right;
  font-size: var(--fs-xs);
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

/* value changed since the last poll */
@keyframes flashfade { from { background: var(--flash); } to { background: transparent; } }
.flash { animation: flashfade .9s ease-out; border-radius: 3px; }

/* ---------- expanded detail ---------- */

.detail {
  display: none;
  padding: 8px 10px 10px 27px;
  background: var(--panel-alt);
  border-bottom: 1px solid var(--border-soft);
  font-size: var(--fs-sm);
}
.detail.open { display: block; }

.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 3px 14px;
  margin-bottom: 8px;
}

.kv { display: flex; gap: 6px; align-items: baseline; min-width: 0; }
.kv .k { color: var(--text-faint); font-size: var(--fs-xs); white-space: nowrap; }
.kv .v {
  font-family: var(--mono);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.kv .v.warn { color: var(--warn); }
.kv .v.down { color: var(--down); }
.kv .v.ok { color: var(--ok); }

.detail h4 {
  margin: 8px 0 4px;
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
  font-weight: 600;
}

.subchecks { display: flex; flex-wrap: wrap; gap: 4px; }
.subcheck {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 7px;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--panel);
  font-size: var(--fs-sm);
}
.subcheck .name { font-weight: 550; }
.subcheck .ms { font-family: var(--mono); color: var(--text-dim); font-size: var(--fs-xs); }
.subcheck .sub { color: var(--text-faint); font-size: var(--fs-xs); }

.errbox {
  padding: 5px 8px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--down) 12%, transparent);
  color: var(--down);
  font-family: var(--mono);
  font-size: var(--fs-sm);
  margin-bottom: 6px;
  word-break: break-word;
}
.msgbox {
  padding: 5px 8px;
  border-radius: 4px;
  background: color-mix(in srgb, var(--warn) 12%, transparent);
  color: var(--warn);
  font-size: var(--fs-sm);
  margin-bottom: 6px;
}

.detail-actions { display: flex; gap: 6px; margin-top: 8px; align-items: center; }

.raw {
  margin-top: 6px;
  max-height: 320px;
  overflow: auto;
  padding: 7px 9px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  font-family: var(--mono);
  font-size: var(--fs-xs);
  white-space: pre;
  tab-size: 2;
}

.bigspark { width: 100%; height: 34px; display: block; margin: 4px 0 2px; }

/* ---------- misc ---------- */

.empty {
  padding: 40px 12px;
  text-align: center;
  color: var(--text-faint);
}

.toast {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 100;
  padding: 7px 12px;
  border-radius: 6px;
  background: var(--panel);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: var(--fs-sm);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .18s, transform .18s;
  pointer-events: none;
  max-width: 380px;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.error { border-color: var(--down); color: var(--down); }
.toast.ok { border-color: var(--ok); color: var(--ok); }

.offline {
  background: var(--down);
  color: #fff;
  text-align: center;
  padding: 2px;
  font-size: var(--fs-sm);
}

/* ---------- login ---------- */

.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

/* The login page has no toolbar, so its theme toggle sits in the corner —
   the choice can be made before signing in and carries into the app. */
.theme-floating {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 10;
  font-size: 13px;
  line-height: 1;
  padding: 5px 8px;
}
.login-card {
  width: 100%;
  max-width: 320px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 22px;
  box-shadow: var(--shadow);
}
.login-card h1 { margin: 0 0 4px; font-size: 16px; display: flex; align-items: center; gap: 8px; }
.login-card p.sub { margin: 0 0 16px; color: var(--text-dim); font-size: var(--fs-sm); }
.field { margin-bottom: 10px; }
.field label { display: block; font-size: var(--fs-xs); color: var(--text-dim); margin-bottom: 3px; }
.field input, .field select, .field textarea {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: var(--bg);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--accent); }
.field .help { font-size: var(--fs-xs); color: var(--text-faint); margin-top: 2px; }
.error-text { color: var(--down); font-size: var(--fs-sm); min-height: 16px; margin-bottom: 6px; }

/* ---------- admin ---------- */

.admin-layout { display: grid; grid-template-columns: 300px 1fr; gap: 10px; padding: 10px 12px 40px; align-items: start; }
@media (max-width: 900px) { .admin-layout { grid-template-columns: 1fr; } }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 7px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.card > h3 {
  margin: 0;
  padding: 6px 10px;
  font-size: var(--fs-sm);
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-dim);
  background: var(--panel-alt);
  border-bottom: 1px solid var(--border-soft);
  display: flex;
  align-items: center;
  gap: 8px;
}
.card > h3 .spacer { flex: 1; }
.card-body { padding: 10px; }

.list-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
}
.list-item:hover { background: var(--panel-alt); }
.list-item.active { background: var(--accent-soft); }
.list-item .nm { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list-item .ty { font-size: var(--fs-xs); color: var(--text-faint); }
/* Per-row delete in the admin service list: visible on hover or keyboard focus
   so the list stays quiet, but reachable without selecting the service first. */
.row-del {
  flex: 0 0 auto;
  border: 1px solid transparent;
  background: none;
  color: var(--text-faint);
  cursor: pointer;
  border-radius: 4px;
  line-height: 1;
  padding: 2px 5px;
  font-size: var(--fs-sm);
  opacity: 0;
  transition: opacity .1s ease;
}
.list-item:hover .row-del,
.list-item.active .row-del,
.row-del:focus-visible { opacity: 1; }
.row-del:hover { color: var(--down); border-color: var(--down); background: color-mix(in srgb, var(--down) 12%, transparent); }

.list-item.group-label {
  background: var(--panel-alt);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-faint);
  cursor: default;
}

.form-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px 10px; }
@media (max-width: 1100px) { .form-grid { grid-template-columns: repeat(2, 1fr); } }
.form-grid .w1 { grid-column: span 1; }
.form-grid .w2 { grid-column: span 2; }
.form-grid .w3 { grid-column: span 3; }
.form-grid .w4 { grid-column: span 4; }
@media (max-width: 1100px) {
  .form-grid .w3, .form-grid .w4 { grid-column: span 2; }
}

.section-title {
  grid-column: 1 / -1;
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--text-faint);
  border-bottom: 1px solid var(--border-soft);
  padding-bottom: 3px;
  margin-top: 6px;
  font-weight: 600;
}

.checkbox { display: flex; align-items: center; gap: 6px; padding-top: 16px; }
.checkbox input { width: auto; }

.form-actions {
  display: flex;
  gap: 7px;
  align-items: center;
  padding: 10px;
  border-top: 1px solid var(--border-soft);
  background: var(--panel-alt);
  position: sticky;
  bottom: 0;
}

table.grid { width: 100%; border-collapse: collapse; font-size: var(--fs-sm); }
table.grid th, table.grid td { text-align: left; padding: 4px 8px; border-bottom: 1px solid var(--border-soft); }
table.grid th { color: var(--text-faint); font-size: var(--fs-xs); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
table.grid tr:hover td { background: var(--panel-alt); }

.tabs { display: flex; gap: 2px; }
.tab {
  padding: 4px 10px;
  border: 1px solid transparent;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-size: var(--fs-sm);
  color: var(--text-dim);
}
.tab.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.test-result {
  margin-top: 8px;
  padding: 8px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: var(--fs-sm);
}
.hidden { display: none !important; }
