:root {
  --bg: #0f1419;
  --surface: #1a2332;
  --surface2: #243044;
  --border: #2f3f56;
  --text: #e8eef7;
  --muted: #8fa3bf;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #ef4444;
  --radius: 10px;
  font-family: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--err); font-size: 0.9rem; }
.ok { color: var(--ok); }
.warn { color: var(--warn); }

.view { min-height: 100vh; }
.login-card {
  max-width: 380px;
  margin: 12vh auto;
  padding: 2rem;
}
.login-card h1 { margin: 0 0 0.25rem; }

#app-view {
  display: grid;
  grid-template-columns: 220px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 1.25rem 1rem;
  display: flex;
  flex-direction: column;
}
.brand { font-weight: 700; font-size: 1.1rem; margin-bottom: 1.5rem; }
.nav {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  color: var(--muted);
  padding: 0.65rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 0.25rem;
}
.nav:hover, .nav.active {
  background: var(--surface2);
  color: var(--text);
}
.sidebar-foot {
  margin-top: auto;
  padding-top: 1rem;
  font-size: 0.85rem;
  color: var(--muted);
}
.topo-intro { margin: 0 0 1rem; max-width: 52rem; line-height: 1.5; }
.topo-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1.25rem;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
  font-size: 0.85rem;
}
.topo-legend-item { display: inline-flex; align-items: center; gap: 0.35rem; color: var(--muted); }
.topo-dot {
  width: 10px; height: 10px; border-radius: 3px; flex-shrink: 0;
}
.topo-dot.server { background: #3b82f6; }
.topo-dot.domain { background: #6366f1; }
.topo-dot.proxy { background: #f59e0b; }
.topo-dot.app { background: #22c55e; }
.topo-dot.database { background: #a855f7; }
.topo-dot.site { background: #14b8a6; }

.topo-servers {
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  margin-bottom: 1.25rem;
}
.topo-server-pill {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.85rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
}
.topo-server-pill.online { border-color: rgba(34, 197, 94, 0.35); }
.topo-server-pill.offline { opacity: 0.75; }
.topo-pill-name { font-weight: 600; }
.topo-pill-ip { color: var(--muted); font-size: 0.82rem; font-family: ui-monospace, monospace; }

.topo-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 0.85rem;
}
.topo-domain-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
}
.topo-domain-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  margin-bottom: 0.65rem;
  padding-bottom: 0.55rem;
  border-bottom: 1px solid var(--border);
}
.topo-domain-head h3 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 600;
  word-break: break-word;
}
.topo-flow { display: flex; flex-direction: column; gap: 0.45rem; }
.topo-flow-row {
  display: grid;
  grid-template-columns: 8.5rem 1fr;
  gap: 0.5rem;
  align-items: baseline;
  font-size: 0.88rem;
}
.topo-flow-key { color: var(--muted); }
.topo-flow-val { word-break: break-word; }
.topo-meta {
  display: block;
  margin-top: 0.15rem;
  font-size: 0.78rem;
  color: var(--muted);
  font-family: ui-monospace, monospace;
}
.topo-warn { margin: 0.5rem 0 0; font-size: 0.8rem; }

.sites-filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.65rem 1rem;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
}
.sites-filter-label {
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}
.sites-filter-hint { font-size: 0.85rem; margin-left: auto; }
.sites-bulk-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}
.sites-table .col-check { width: 2.25rem; text-align: center; }
.sites-table .col-check input { cursor: pointer; }
.sites-table .col-backup { white-space: nowrap; }
.sites-table tr.site-backup-off td { opacity: 0.72; }
.backup-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
  user-select: none;
}
.backup-toggle input { cursor: pointer; accent-color: var(--ok); }
.backup-toggle-label {
  font-size: 0.85rem;
  min-width: 1.5rem;
  color: var(--muted);
}
.backup-toggle input:checked + .backup-toggle-label { color: var(--ok); }
.filter-pills { display: flex; flex-wrap: wrap; gap: 0.4rem; }
.filter-pill {
  font: inherit;
  font-size: 0.85rem;
  padding: 0.35rem 0.7rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  cursor: pointer;
}
.filter-pill:hover { border-color: var(--accent); }
.backups-sort-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1rem;
  padding: 0.75rem 1rem;
}
.backups-sort-label { font-weight: 500; color: var(--muted); }
.backups-sort-hint { font-size: 0.85rem; margin-left: auto; }
.sort-pill.active { font-weight: 600; }
.backups-server-filter {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.backups-server-filter select,
.certs-server-filter select {
  min-width: 12rem;
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.certs-filter-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.65rem 1rem;
  padding: 0.75rem 1rem;
  margin-bottom: 1rem;
}
.certs-server-filter {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
tr.cert-row-expired td { background: rgba(239, 68, 68, 0.06); }
tr.cert-row-expiring td { background: rgba(245, 158, 11, 0.06); }
.site-path {
  max-width: 18rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: ui-monospace, monospace;
  font-size: 0.82rem;
}
.backups-bulk-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem;
}
.backups-table .col-check {
  width: 2.25rem;
  text-align: center;
}
.backups-table .col-check input { cursor: pointer; }

.filter-pill.active {
  background: rgba(59, 130, 246, 0.2);
  border-color: var(--accent);
  color: var(--text);
}
.topo-ip {
  font-family: ui-monospace, monospace;
  font-size: 0.82rem;
  color: var(--text);
  background: var(--surface2);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

.link {
  background: none;
  border: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
  margin-top: 0.5rem;
}

main {
  padding: 1.5rem 2rem;
  overflow: auto;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.25rem;
  gap: 1rem;
  flex-wrap: wrap;
}
.page-head h1 { margin: 0; font-size: 1.5rem; }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
}
button.stat {
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: inherit;
  width: 100%;
  transition: border-color 0.15s, background 0.15s;
}
button.stat:hover {
  border-color: var(--accent);
  background: var(--surface2);
}
button.stat:hover .lbl { color: var(--text); }
.stat .val { font-size: 1.75rem; font-weight: 700; }
.stat .lbl { color: var(--muted); font-size: 0.85rem; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
th, td {
  text-align: left;
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--border);
}
th { color: var(--muted); font-weight: 500; }
tr:hover td { background: rgba(255,255,255,0.02); }

.badge {
  display: inline-block;
  padding: 0.15rem 0.5rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}
.badge.ok { background: rgba(34,197,94,0.15); color: var(--ok); }
.badge.off { background: rgba(143,163,191,0.15); color: var(--muted); }
.badge.run { background: rgba(59,130,246,0.15); color: var(--accent); }
.badge.fail { background: rgba(239,68,68,0.15); color: var(--err); }
.badge.warn { background: rgba(245,158,11,0.15); color: #f59e0b; }
.section-title { margin: 1.5rem 0 0.75rem; font-size: 1.1rem; font-weight: 600; }

button, .btn {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 0.55rem 1rem;
  cursor: pointer;
  font-size: 0.9rem;
}
button:hover { background: var(--accent-hover); }
button.secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
button.danger { background: var(--err); }
button.sm { padding: 0.35rem 0.65rem; font-size: 0.8rem; }
.actions { display: flex; gap: 0.4rem; flex-wrap: wrap; }

label {
  display: block;
  margin-bottom: 0.85rem;
  font-size: 0.85rem;
  color: var(--muted);
}
input, select, textarea {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.55rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
textarea { min-height: 72px; resize: vertical; }

dialog {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  padding: 0;
  max-width: 520px;
  width: calc(100% - 2rem);
}
dialog::backdrop { background: rgba(0,0,0,0.55); }
#modal-form { padding: 1.25rem 1.5rem; }
#modal-form h2 { margin: 0 0 1rem; font-size: 1.2rem; }
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  margin-top: 1rem;
}

.token-box {
  font-family: monospace;
  font-size: 0.8rem;
  word-break: break-all;
  background: var(--bg);
  padding: 0.5rem;
  border-radius: 6px;
  margin-top: 0.35rem;
}

.card.table-wide { overflow-x: auto; }

table.servers-table .col-metrics {
  min-width: 28rem;
  width: 50%;
}
table.servers-table .col-actions {
  width: 1%;
  white-space: nowrap;
}

.metrics-cell { min-width: 26rem; }
.metrics-toggle {
  display: flex;
  align-items: flex-start;
  gap: 0.35rem;
  background: none;
  border: none;
  color: inherit;
  padding: 0;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.metrics-toggle:hover { color: var(--accent); background: none; }
.metrics-chevron {
  flex-shrink: 0;
  width: 1em;
  color: var(--muted);
}
.metrics-summary { line-height: 1.4; }
.metrics-detail {
  margin-top: 0.35rem;
  padding-left: 1.35rem;
  font-size: 0.8rem;
  line-height: 1.6;
  max-height: 14rem;
  overflow: auto;
}
.metrics-detail > div {
  white-space: nowrap;
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.78rem;
}

textarea.nginx-editor,
pre.nginx-view {
  width: 100%;
  min-width: min(52rem, 90vw);
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.82rem;
  line-height: 1.45;
}
textarea.nginx-editor {
  min-height: 22rem;
  resize: vertical;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.75rem;
}
pre.nginx-view {
  margin: 0;
  padding: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  max-height: 70vh;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.nginx-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  align-items: center;
  margin-bottom: 1rem;
}
.nginx-toolbar select {
  min-width: 14rem;
}

.cert-job-detail { max-width: 28rem; line-height: 1.45; }
.cert-job-err { color: var(--err); display: inline-block; }
.backup-job-err,
.cert-job-err {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--err);
  font-size: 0.85rem;
  margin-top: 0.35rem;
  max-width: 22rem;
  line-height: 1.45;
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
  text-underline-offset: 2px;
}
.backup-job-err:hover,
.cert-job-err:hover {
  color: #f87171;
}
.error-detail {
  margin: 0.75rem 0 0;
  padding: 0.75rem;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-family: ui-monospace, "Cascadia Code", monospace;
  font-size: 0.82rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 50vh;
  overflow: auto;
}

@media (max-width: 768px) {
  #app-view { grid-template-columns: 1fr; }
  .sidebar { flex-direction: row; flex-wrap: wrap; align-items: center; }
  .sidebar nav { display: flex; flex-wrap: wrap; gap: 0.25rem; }
  .sidebar-foot { width: 100%; }
}
