:root {
  --bg: #0b1120;
  --surface: #111827;
  --surface-2: #1e293b;
  --border: #263349;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --radius: 10px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft JhengHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 12.5px; }
.center { text-align: center; }
code {
  background: rgba(148, 163, 184, 0.14);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}

/* Login */
.login-wrapper {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.45);
}
.brand-logo { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.brand-logo h1 { font-size: 20px; font-weight: 700; }
.subtitle { color: var(--muted); margin-bottom: 24px; font-size: 13px; }
.login-card label { display: block; margin-bottom: 14px; }
.login-card label span { display: block; margin-bottom: 6px; color: var(--muted); font-size: 12.5px; }
.hint { margin-top: 16px; font-size: 12px; color: var(--muted); text-align: center; }

.dot-live {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--success);
  box-shadow: 0 0 10px var(--success);
  display: inline-block;
  flex-shrink: 0;
}

/* Inputs & Buttons */
input[type="text"], input[type="password"], input[type="number"], select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13.5px;
  outline: none;
}
input:focus, select:focus { border-color: var(--primary); }
.login-card input, .modal-body input { width: 100%; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}
.btn-full { width: 100%; padding: 10px; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11.5px; border-radius: 6px; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-hover); }
.btn-secondary { background: var(--surface-2); color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #293548; }
.btn-danger { background: rgba(239, 68, 68, 0.15); color: #fca5a5; border-color: rgba(239, 68, 68, 0.35); }
.btn-danger:hover { background: rgba(239, 68, 68, 0.28); }
.btn-success { background: rgba(34, 197, 94, 0.15); color: #86efac; border-color: rgba(34, 197, 94, 0.35); }
.btn-success:hover { background: rgba(34, 197, 94, 0.28); }
.btn-warning { background: rgba(245, 158, 11, 0.15); color: #fcd34d; border-color: rgba(245, 158, 11, 0.35); }
.btn-warning:hover { background: rgba(245, 158, 11, 0.28); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); background: rgba(148, 163, 184, 0.1); }

/* Layout */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
  width: 248px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px;
  flex-shrink: 0;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.sidebar-header { display: flex; align-items: center; gap: 10px; padding: 4px 8px 18px; border-bottom: 1px solid var(--border); }
.brand-title { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: var(--muted); }

.nav-menu { display: flex; flex-direction: column; gap: 4px; margin-top: 14px; }
.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 13.5px;
  cursor: pointer;
  text-align: left;
  transition: 0.15s;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active { background: rgba(59, 130, 246, 0.18); color: #93c5fd; font-weight: 600; }

.sidebar-divider {
  margin: 22px 10px 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #64748b;
}
.ext-links { display: flex; flex-direction: column; gap: 4px; }
.ext-links a {
  padding: 7px 12px;
  color: var(--muted);
  text-decoration: none;
  font-size: 12.5px;
  border-radius: 6px;
}
.ext-links a:hover { background: var(--surface-2); color: var(--text); }

.sidebar-footer {
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.sidebar-footer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.sidebar-footer-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.ip-info-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(15, 23, 42, 0.65);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 12.5px;
}
.ip-rules-textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12.5px;
  line-height: 1.55;
  resize: vertical;
  outline: none;
}
.ip-rules-textarea:focus { border-color: var(--primary); }
.user-info { font-size: 13px; font-weight: 600; color: var(--text); }

.main-content { flex: 1; padding: 22px 28px; overflow-y: auto; min-width: 0; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 22px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.topbar h2 { font-size: 20px; font-weight: 700; }
.topbar-actions { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
.topbar-divider {
  width: 1px;
  height: 18px;
  background: var(--border);
  margin: 0 2px;
}
.user-pill {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
}

/* KPI Cards */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}
.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.kpi-label { font-size: 12.5px; color: var(--muted); }
.kpi-value { font-size: 30px; font-weight: 700; margin: 4px 0; }
.kpi-sub { font-size: 12px; color: var(--muted); }

/* Host Cards */
.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 12px;
}
.section-header h3 { font-size: 15.5px; font-weight: 600; }

.host-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(355px, 1fr));
  gap: 16px;
}
.host-group {
  margin-bottom: 20px;
}
.host-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  margin-bottom: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s ease;
}
.host-group-header:hover {
  background: var(--surface-2);
}
.host-group-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 700;
}
.host-group-arrow {
  display: inline-block;
  width: 14px;
  color: var(--muted);
  font-size: 11px;
}
.host-group-stats {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}
.host-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 11px;
}
.host-card.offline { border-color: rgba(239, 68, 68, 0.55); }
.host-card-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.host-title-wrap { min-width: 0; flex: 1; }
.host-name {
  font-size: 16px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  line-height: 1.3;
}
.host-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.host-badges {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  justify-content: flex-end;
  flex-shrink: 0;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}
.badge-success { background: rgba(34, 197, 94, 0.18); color: #86efac; }
.badge-danger { background: rgba(239, 68, 68, 0.2); color: #fca5a5; }
.badge-warning { background: rgba(245, 158, 11, 0.2); color: #fcd34d; }
.badge-neutral { background: rgba(148, 163, 184, 0.18); color: #cbd5e1; }

.metric-row { display: flex; flex-direction: column; gap: 4px; }
.metric-label { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); }
.progress-track { height: 7px; background: var(--bg); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; border-radius: 999px; transition: width 0.3s ease; }
.fill-ok { background: var(--primary); }
.fill-warn { background: var(--warning); }
.fill-crit { background: var(--danger); }

.host-card-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding-top: 10px;
  margin-top: auto;
  border-top: 1px solid var(--border);
}
.host-card-actions .btn {
  padding: 5px 4px;
  white-space: nowrap;
}
.host-quick-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 12px;
  padding: 9px 11px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid rgba(38, 51, 73, 0.7);
  border-radius: 8px;
}
.qs-item {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.qs-label {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.2;
}
.qs-val {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.host-sub-warn {
  font-size: 11.5px;
  color: #fcd34d;
  background: rgba(245, 158, 11, 0.12);
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: 6px;
  padding: 4px 8px;
}
.badge-clickable { cursor: pointer; }
.badge-clickable:hover { filter: brightness(1.15); }
.host-detail-body { max-height: 75vh; overflow-y: auto; display: flex; flex-direction: column; gap: 16px; }
.detail-section h4 {
  font-size: 13px;
  font-weight: 700;
  color: #93c5fd;
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 8px 12px;
}
.detail-kv {
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
}
.detail-kv .k { font-size: 11px; color: var(--muted); }
.detail-kv .v { font-size: 13px; font-weight: 600; color: var(--text); margin-top: 2px; word-break: break-word; }

/* Tables & Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.table-wrap { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; }
.data-table th, .data-table td {
  padding: 11px 14px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.data-table th { color: var(--muted); font-weight: 600; background: rgba(15, 23, 42, 0.6); }
.data-table tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: rgba(30, 41, 59, 0.45); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}
.toolbar-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.inline-label { color: var(--muted); font-size: 12.5px; }
.checkbox-inline { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); cursor: pointer; }

/* Log Console */
.log-console {
  padding: 14px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  line-height: 1.55;
  max-height: 68vh;
  overflow: auto;
  background: #070b14;
  white-space: pre-wrap;
  word-break: break-all;
}
.log-line { padding: 3px 6px; border-radius: 4px; }
.log-line:hover { background: rgba(148, 163, 184, 0.08); }
.log-ts { color: #64748b; margin-right: 8px; }
.log-host { color: #60a5fa; margin-right: 8px; font-weight: 600; }
.log-unit { color: #a78bfa; margin-right: 8px; }
.log-err { color: #fca5a5; }
.log-warn { color: #fcd34d; }

/* Alerts & SSH Banner */
.stack-list { display: flex; flex-direction: column; gap: 10px; }
.alert-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--warning);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 14px;
}
.alert-item.critical { border-left-color: var(--danger); }
.ssh-banner { padding: 16px; }
.ssh-banner-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; margin-bottom: 10px; }
.code-box {
  background: #070b14;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  color: #93c5fd;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal-card {
  width: 100%;
  max-width: 580px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.modal-wide { max-width: 860px; }
.modal-journal {
  width: min(92vw, 1600px);
  max-width: none;
  height: 85vh;
  display: flex;
  flex-direction: column;
  transition: width 0.15s ease, height 0.15s ease;
}
.modal-journal.modal-fullscreen {
  width: 98vw;
  height: 95vh;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.modal-header-actions select {
  padding: 4px 8px;
  font-size: 12px;
}
.modal-body { padding: 18px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 14px; }
.form-grid label span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px; }
.modal-log {
  flex: 1;
  min-height: 0;
  max-height: none;
  margin: 0;
  border-radius: 0;
  overflow: auto;
}

.error-box {
  background: rgba(239, 68, 68, 0.15);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fca5a5;
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 12.5px;
  margin-bottom: 12px;
}

/* Toast */
.toast-container {
  position: fixed;
  right: 20px;
  bottom: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 200;
}
.toast {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
}
.toast.ok { border-left: 4px solid var(--success); }
.toast.err { border-left: 4px solid var(--danger); }

@media (max-width: 820px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; position: static; height: auto; border-right: none; border-bottom: 1px solid var(--border); }
  .form-grid { grid-template-columns: 1fr; }
  .modal-journal { width: 96vw; height: 90vh; }
}
