:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e2e4e9;
  --text: #171a21;
  --muted: #6b7280;
  --primary: #2563eb;
  --primary-text: #ffffff;
  --good: #16a34a;
  --good-bg: #dcfce7;
  --avg: #d97706;
  --avg-bg: #fef3c7;
  --poor: #dc2626;
  --poor-bg: #fee2e2;
  --unknown: #9ca3af;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05);
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b22;
    --surface-2: #20242d;
    --border: #2b303b;
    --text: #e6e8ee;
    --muted: #9aa3b2;
    --good-bg: rgba(22, 163, 74, 0.18);
    --avg-bg: rgba(217, 119, 6, 0.18);
    --poor-bg: rgba(220, 38, 38, 0.18);
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; }
h2 { font-size: 16px; margin: 0; }
p { margin: 0; }
#app { max-width: 1280px; margin: 0 auto; padding: 24px 16px 60px; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.ok { color: var(--good); }
.err { color: var(--poor); }
.mono { font-variant-numeric: tabular-nums; }

/* top bar */
.topbar, .site-head {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start;
  gap: 12px; margin-bottom: 20px;
}
.toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.toolbar input[type="search"] {
  min-width: 220px;
}

button, input, select {
  font: inherit; color: inherit;
}
input, select {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: 0; border-color: transparent; }
button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
  padding: 7px 12px; cursor: pointer; white-space: nowrap;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }
button.primary:hover { filter: brightness(1.08); }
button.danger { color: var(--poor); }
button.danger:hover { background: var(--poor-bg); }
button.small { padding: 4px 8px; font-size: 12px; }
button:disabled { opacity: 0.5; cursor: default; }
button.active { background: var(--primary); color: var(--primary-text); border-color: var(--primary); }

/* stats */
.stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; margin-bottom: 20px;
}
.tile {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; box-shadow: var(--shadow);
}
.tile .label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.tile .value { font-size: 24px; font-weight: 600; line-height: 1.2; margin-top: 2px; }
.tile .value.good { color: var(--good); }
.tile .value.avg { color: var(--avg); }
.tile .value.poor { color: var(--poor); }
.tile .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* server groups */
.server {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  margin-bottom: 16px; box-shadow: var(--shadow); overflow: hidden;
}
.server.server-down { border-color: var(--poor); }
.server-head {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px;
  padding: 12px 16px; background: var(--surface-2); border-bottom: 1px solid var(--border);
}
.server.server-down .server-head { background: var(--poor-bg); }
.server-head .host { margin-left: 8px; }
.badges { display: flex; flex-wrap: wrap; gap: 6px; }
.badge {
  font-size: 12px; padding: 2px 8px; border-radius: 999px; background: var(--surface); border: 1px solid var(--border);
}
.badge.green { background: var(--good-bg); color: var(--good); border-color: transparent; }
.badge.red { background: var(--poor-bg); color: var(--poor); border-color: transparent; }
.badge.red.strong { background: var(--poor); color: #fff; font-weight: 600; }
.badge.orange { background: var(--avg-bg); color: var(--avg); border-color: transparent; }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-top: 1px solid var(--border); vertical-align: middle; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; border-top: 0; }
tbody tr:hover { background: var(--surface-2); }
td.num, th.num { text-align: right; }
.table-wrap { overflow-x: auto; }
.site-name { font-weight: 600; color: var(--text); }
.row-actions { white-space: nowrap; text-align: right; }
.row-actions button { margin-left: 4px; }
tr.disabled td { opacity: 0.55; }

/* dot */
.dot {
  display: inline-block; width: 12px; height: 12px; border-radius: 50%; background: var(--unknown); vertical-align: middle;
  box-shadow: 0 0 0 3px color-mix(in srgb, currentColor 0%, transparent);
}
.dot.up { background: var(--good); box-shadow: 0 0 0 3px var(--good-bg); }
.dot.down { background: var(--poor); box-shadow: 0 0 0 3px var(--poor-bg); }
.dot.unknown { background: var(--unknown); }
.dot.pulse { animation: pulse 1.2s ease-in-out infinite; }
.dot.big { width: 16px; height: 16px; margin-right: 6px; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* scores */
.score {
  display: inline-flex; align-items: center; justify-content: center; min-width: 38px; height: 26px; padding: 0 6px;
  border-radius: 6px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.score.good { background: var(--good-bg); color: var(--good); }
.score.avg { background: var(--avg-bg); color: var(--avg); }
.score.poor { background: var(--poor-bg); color: var(--poor); }
.score.none { background: var(--surface-2); color: var(--muted); font-weight: 400; }
.ring {
  display: flex; flex-direction: column; align-items: center; gap: 6px; min-width: 80px;
}
.ring .circle {
  width: 64px; height: 64px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700; border: 4px solid;
}
.ring .circle.good { color: var(--good); border-color: var(--good); background: var(--good-bg); }
.ring .circle.avg { color: var(--avg); border-color: var(--avg); background: var(--avg-bg); }
.ring .circle.poor { color: var(--poor); border-color: var(--poor); background: var(--poor-bg); }
.ring .circle.none { color: var(--muted); border-color: var(--border); }
.ring .name { font-size: 12px; color: var(--muted); text-align: center; }

/* cards */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.card.flush { padding: 0; }
.card.flush .card-head { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.card-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 12px; }
.card.flush .card-head { margin-bottom: 0; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; margin-bottom: 16px; }
.cards .card { margin-bottom: 0; }
.rings { display: flex; flex-wrap: wrap; gap: 16px; justify-content: space-around; margin: 12px 0 16px; }
.metrics { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; }
.metric { background: var(--surface-2); border-radius: 8px; padding: 8px 10px; }
.metric .label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.metric .value { font-weight: 600; font-variant-numeric: tabular-nums; }
.metric .value.good { color: var(--good); }
.metric .value.avg { color: var(--avg); }
.metric .value.poor { color: var(--poor); }
.chip { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12px; }
.back { display: inline-block; margin-bottom: 12px; }
.range button { margin-left: 4px; }
.chart-wrap { position: relative; height: 260px; }
.empty { padding: 24px; text-align: center; color: var(--muted); }
.error-text { color: var(--poor); font-size: 12px; }
.incident { padding: 8px 0; border-top: 1px solid var(--border); display: flex; gap: 12px; flex-wrap: wrap; }
.incident:first-child { border-top: 0; }

/* dialogs */
dialog {
  border: 1px solid var(--border); border-radius: 12px; padding: 0; background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px)); box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}
dialog#server-dialog { width: min(820px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0, 0, 0, 0.45); }
dialog form, dialog .dialog-body { padding: 20px; display: flex; flex-direction: column; gap: 12px; }
dialog .dialog-body form { padding: 0; }
dialog label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }
dialog label.check { flex-direction: row; align-items: center; gap: 8px; }
dialog label.check input { width: auto; }
.grid-3 { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 10px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.dialog-actions.left { justify-content: flex-start; }
.form-error { color: var(--poor); background: var(--poor-bg); padding: 8px 10px; border-radius: 8px; }
#server-list table { font-size: 13px; }

#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
  z-index: 100; max-width: calc(100vw - 32px);
}
#toast.error { background: var(--poor); color: #fff; }

@media (max-width: 720px) {
  #app { padding: 16px 12px 40px; }
  th:nth-child(4), td:nth-child(4) { display: none; }
  .row-actions button span { display: none; }
}
