:root {
  --sidebar-width: 240px;
  --accent: #2dd4a8;
  --accent-dim: rgba(45, 212, 168, 0.20);
  --accent-glow: rgba(45, 212, 168, 0.10);
  --bg: #13151a;
  --surface: #1a1d23;
  --surface-2: #22262e;
  --surface-3: #1e2128;
  --border: #2a2e36;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --subtle: #64748b;
  --green: #22c55e;
  --yellow: #eab308;
  --red: #ef4444;
  --blue: #38bdf8;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  overflow-x: hidden;
}

a { color: var(--accent); text-decoration: none; }
code { color: #cbd5e1; background: #12141a; padding: 1px 5px; border-radius: 4px; font-size: 0.85em; }

.app-shell { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 20px 14px;
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.sidebar-brand {
  display: flex; align-items: center; gap: 12px;
  margin-bottom: 24px;
}
.brand-logo { width: 36px; height: 36px; object-fit: contain; }
.brand-name { font-weight: 700; color: var(--accent); letter-spacing: 0.2px; }
.brand-sub { font-size: 0.75rem; color: var(--muted); }

.sidebar .nav-link {
  color: var(--muted);
  border-radius: 8px;
  padding: 10px 12px;
  display: flex; align-items: center; gap: 10px;
  transition: background .15s, color .15s;
  font-size: 0.92rem;
}
.sidebar .nav-link:hover { color: var(--text); background: var(--surface-2); }
.sidebar .nav-link.active { color: #0f1115; background: var(--accent); font-weight: 600; }

.sidebar-foot { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--border); }
.connection-status { font-size: 0.8rem; color: var(--subtle); margin-bottom: 10px; display: flex; gap: 6px; align-items: center; }

/* Main */
.main {
  flex: 1;
  padding: 28px 32px 80px;
  max-width: 100%;
  position: relative;
}

.page-header {
  display: flex; align-items: flex-end; justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
}
.page-header h2 { margin: 0 0 4px; font-weight: 600; letter-spacing: 0.2px; font-size: 1.5rem; }
.subtle { color: var(--subtle); font-size: 0.88rem; }
.page-actions { display: flex; align-items: center; gap: 10px; }
.filter-group { min-width: 280px; }

/* Stat row */
.stat-row {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}
.stat-card {
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
}
.stat-label { color: var(--muted); font-size: 0.78rem; letter-spacing: 0.5px; text-transform: uppercase; }
.stat-value { font-size: 1.7rem; font-weight: 600; margin-top: 4px; }
.text-green { color: var(--green) !important; }
.text-yellow { color: var(--yellow) !important; }
.text-red { color: var(--red) !important; }
.text-blue { color: var(--blue) !important; }

/* Station grid */
.station-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
}
.station-card {
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  cursor: pointer;
  transition: border-color .15s, transform .1s, box-shadow .15s;
  position: relative;
}
.station-card:hover {
  border-color: var(--accent);
  box-shadow: 0 6px 20px var(--accent-glow);
}
.station-card .title-row {
  display: flex; justify-content: space-between; align-items: flex-start; gap: 10px;
}
.station-card .st-name { font-weight: 600; font-size: 1.05rem; }
.station-card .st-host { color: var(--muted); font-size: 0.82rem; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.station-card .st-ip { color: var(--subtle); font-size: 0.78rem; margin-top: 4px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }

.badge-dot {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 0.75rem; padding: 3px 8px; border-radius: 20px;
  background: var(--surface-2); color: var(--muted);
  border: 1px solid var(--border);
}
.badge-dot .dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--subtle);
}
.badge-dot.online .dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
.badge-dot.offline .dot { background: var(--red); }
.badge-dot.ok { color: var(--green); border-color: rgba(34,197,94,.35); }
.badge-dot.warning { color: var(--yellow); border-color: rgba(234,179,8,.35); }
.badge-dot.critical { color: var(--red); border-color: rgba(239,68,68,.45); }

.health-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.meter-row {
  margin-top: 12px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
}
.meter-label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.meter-bar {
  height: 6px; border-radius: 10px; background: var(--surface-2); overflow: hidden; margin-top: 4px;
}
.meter-fill { height: 100%; background: var(--accent); border-radius: 10px; transition: width .3s; }
.meter-fill.warn { background: var(--yellow); }
.meter-fill.crit { background: var(--red); }

.pair-bars {
  display: flex; gap: 4px; margin-top: 12px;
}
.pair-seg { flex: 1; height: 6px; border-radius: 3px; background: var(--surface-2); }
.pair-seg.ok { background: var(--green); }
.pair-seg.warning { background: var(--yellow); }
.pair-seg.critical { background: var(--red); }
.pair-seg.syncing { background: var(--blue); }

/* Activity list */
.activity-list {
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.activity-row {
  display: grid;
  grid-template-columns: 160px 120px 1fr;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.activity-row:last-child { border-bottom: none; }
.activity-row .kind {
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: .5px; color: var(--muted);
  align-self: center;
}
.activity-row .kind.connect { color: var(--green); }
.activity-row .kind.disconnect { color: var(--red); }
.activity-row .kind.command { color: var(--blue); }
.activity-row .ts { color: var(--subtle); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 0.82rem; }

/* Fleet */
.fleet-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 14px;
}
.card-panel {
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px 20px;
}
.card-panel h5 { margin: 0 0 6px; }
.card-panel.card-danger { border-color: rgba(239,68,68,.5); }

/* Buttons */
.btn-accent { background: var(--accent); border: none; color: #0f1115; font-weight: 600; }
.btn-accent:hover { background: #5fe8c4; color: #0f1115; }
.btn-outline-accent { background: transparent; border: 1px solid var(--accent); color: var(--accent); }
.btn-outline-accent:hover { background: var(--accent-dim); color: var(--accent); }

/* Detail drawer */
.detail-drawer {
  position: fixed;
  top: 0; right: 0;
  width: min(560px, 100%);
  height: 100vh;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: -20px 0 60px rgba(0,0,0,.45);
  display: flex; flex-direction: column;
  z-index: 200;
  animation: slideIn .18s ease-out;
}
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } to { transform: translateX(0); opacity: 1; } }
.detail-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 16px 20px; border-bottom: 1px solid var(--border);
}
.detail-title { font-weight: 600; font-size: 1.05rem; }
.icon-btn { background: transparent; border: none; color: var(--muted); font-size: 1.1rem; cursor: pointer; }
.icon-btn:hover { color: var(--text); }
.detail-tabs { display: flex; gap: 2px; padding: 0 14px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.tab-btn {
  background: transparent; border: none; color: var(--muted);
  padding: 10px 14px; font-size: 0.88rem; cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--accent); border-bottom-color: var(--accent); }
.detail-body { flex: 1; overflow: auto; padding: 18px 20px; }
.detail-tab .kv { display: grid; grid-template-columns: 160px 1fr; gap: 6px 14px; font-size: 0.88rem; margin-bottom: 14px; }
.detail-tab .kv dt { color: var(--muted); }
.detail-tab .kv dd { margin: 0; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.detail-tab h6 { margin: 18px 0 8px; color: var(--muted); text-transform: uppercase; font-size: 0.75rem; letter-spacing: .5px; }

.pair-list { display: grid; gap: 10px; }
.pair-item {
  background: var(--surface-3); border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px;
}
.pair-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.pair-name { font-weight: 600; }
.pair-meta { display: grid; grid-template-columns: repeat(2, 1fr); gap: 6px 14px; margin-top: 8px; font-size: 0.82rem; color: var(--muted); }
.pair-meta span { color: var(--text); }

.cmd-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; margin-bottom: 18px; }
.cmd-btn {
  background: var(--surface-3); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; text-align: left;
  color: var(--text); font-size: 0.9rem; cursor: pointer;
  display: flex; justify-content: space-between; align-items: center; gap: 6px;
}
.cmd-btn:hover { border-color: var(--accent); color: var(--accent); }
.cmd-btn.danger:hover { border-color: var(--red); color: var(--red); }
.cmd-btn:disabled { opacity: .5; cursor: not-allowed; }

.cmd-log {
  font-size: 0.8rem;
}
.cmd-log-item {
  padding: 8px 0; border-bottom: 1px dashed var(--border);
}
.cmd-log-item:last-child { border-bottom: none; }
.cmd-log-item .type { color: var(--accent); font-weight: 600; }
.cmd-log-item .status { font-size: 0.72rem; padding: 2px 6px; border-radius: 4px; margin-left: 8px; }
.cmd-log-item .status.acked { background: rgba(34,197,94,.2); color: var(--green); }
.cmd-log-item .status.sent { background: rgba(56,189,248,.2); color: var(--blue); }
.cmd-log-item .status.timeout, .cmd-log-item .status.send_failed { background: rgba(239,68,68,.2); color: var(--red); }
.cmd-log-item pre {
  background: #0f1115; border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 8px; color: #cbd5e1; max-height: 160px; overflow: auto;
  font-size: 0.78rem;
}

/* Modal */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 500; }
.modal-card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 22px 24px; width: min(420px, 90%); }
.modal-card h5 { margin: 0 0 6px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }
#loginError { min-height: 18px; }

/* Toast */
.toast-host { position: fixed; top: 20px; right: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 600; }
.toast {
  background: var(--surface-2); border: 1px solid var(--border); border-left: 3px solid var(--accent);
  border-radius: 8px; padding: 10px 14px; font-size: 0.88rem; min-width: 260px; box-shadow: 0 6px 20px rgba(0,0,0,.4);
  animation: toastIn .2s ease-out;
}
.toast.err { border-left-color: var(--red); }
.toast.warn { border-left-color: var(--yellow); }
@keyframes toastIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: translateY(0); } }

@media (max-width: 760px) {
  .sidebar { display: none; }
  .main { padding: 16px; }
  .stat-row { grid-template-columns: repeat(2, 1fr); }
  .detail-drawer { width: 100%; }
}
