
:root {
  --bg:       #0d0f14;
  --surface:  #141720;
  --surface2: #1c2030;
  --border:   #252b3b;
  --text:     #e2e8f0;
  --muted:    #64748b;
  --accent:   #3b82f6;
  --accent-h: #2563eb;
  --green:    #22c55e;
  --red:      #ef4444;
  --yellow:   #f59e0b;
  --purple:   #a78bfa;
  --mono:     'IBM Plex Mono', monospace;
  --sans:     'IBM Plex Sans', sans-serif;
  --radius:   8px;
  --sidebar:  240px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ── LOGIN ─────────────────────────────────────────── */
.login-screen {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh;
  background: radial-gradient(ellipse at 30% 40%, #1a2040 0%, var(--bg) 70%);
}

.login-card {
  width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 40px;
  box-shadow: 0 25px 60px rgba(0,0,0,0.5);
}

.login-logo {
  display: flex; justify-content: center;
  margin-bottom: 20px;
}

.login-title {
  font-size: 22px; font-weight: 600;
  text-align: center; margin-bottom: 4px;
}

.login-sub {
  color: var(--muted); text-align: center;
  font-size: 13px; margin-bottom: 32px;
}

.login-form { display: flex; flex-direction: column; gap: 16px; }

.field-group { display: flex; flex-direction: column; gap: 6px; }
.field-group label { font-size: 12px; font-weight: 500; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; }

input[type="text"], input[type="password"], input[type="date"],
select, .search-input {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  padding: 10px 14px;
  font-family: var(--sans);
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s;
  width: 100%;
}
input:focus, select:focus { border-color: var(--accent); }

.login-error {
  background: rgba(239,68,68,0.1);
  border: 1px solid rgba(239,68,68,0.3);
  color: var(--red);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 13px;
}

.btn-login {
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius);
  padding: 12px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  font-family: var(--sans);
}
.btn-login:hover { background: var(--accent-h); }
.btn-login:active { transform: scale(0.98); }

.login-version {
  text-align: center; color: var(--muted);
  font-size: 11px; font-family: var(--mono);
  margin-top: 24px;
}

/* ── LAYOUT ──────────────────────────────────────────── */
.app { display: flex; height: 100vh; overflow: hidden; }

/* ── SIDEBAR ─────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar);
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  display: flex; align-items: center; gap: 12px;
  padding: 20px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-brand {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  overflow-y: auto;
}

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px;
  border-radius: var(--radius);
  color: var(--muted);
  text-decoration: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: background 0.1s, color 0.1s;
  user-select: none;
  position: relative;
}
.nav-item svg { width: 16px; height: 16px; flex-shrink: 0; }
.nav-item:hover { background: var(--surface2); color: var(--text); }
.nav-item.active { background: rgba(59,130,246,0.15); color: var(--accent); }

.ws-dot {
  width: 7px; height: 7px; border-radius: 50%;
  margin-left: auto;
  flex-shrink: 0;
}
.ws-dot.online  { background: var(--green); box-shadow: 0 0 6px var(--green); }
.ws-dot.offline { background: var(--muted); }
.ws-dot.connecting { background: var(--yellow); animation: pulse 1s infinite; }

@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:0.3} }

.badge {
  background: var(--red);
  color: white;
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  margin-left: auto;
  font-family: var(--mono);
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
}
.sidebar-user {
  flex: 1; font-size: 12px; color: var(--muted);
  font-family: var(--mono);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.btn-logout {
  background: none; border: none; cursor: pointer;
  color: var(--muted); padding: 4px;
  display: flex; align-items: center;
  transition: color 0.15s;
}
.btn-logout svg { width: 15px; height: 15px; }
.btn-logout:hover { color: var(--red); }

/* ── MAIN ────────────────────────────────────────────── */
.main {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* ── PAGES ───────────────────────────────────────────── */
.page {
  display: none;
  padding: 24px;
  flex-direction: column;
  gap: 20px;
  min-height: 0;       /* allow flex child to shrink below content size */
}
.page.active { display: flex; }

.page-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 4px;
}
.page-header h2 {
  font-size: 20px; font-weight: 600;
  letter-spacing: -0.02em;
}

.header-actions { display: flex; align-items: center; gap: 10px; }
.muted-text { font-size: 12px; color: var(--muted); font-family: var(--mono); }

/* ── KPI CARDS ───────────────────────────────────────── */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
}
.kpi-grid-4 { grid-template-columns: repeat(4, 1fr); }
.kpi-grid-3 { grid-template-columns: repeat(3, 1fr); }

@media (max-width: 1100px) {
  .kpi-grid { grid-template-columns: repeat(2, 1fr); }
  .kpi-grid-4 { grid-template-columns: repeat(2, 1fr); }
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  align-items: center;
  gap: 14px;
}

.kpi-icon {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.kpi-icon svg { width: 20px; height: 20px; }
.kpi-icon.sent     { background: rgba(34,197,94,0.15);  color: var(--green); }
.kpi-icon.total    { background: rgba(59,130,246,0.15); color: var(--accent); }
.kpi-icon.failed   { background: rgba(239,68,68,0.15);  color: var(--red); }
.kpi-icon.deferred { background: rgba(245,158,11,0.15); color: var(--yellow); }
.kpi-icon.queue    { background: rgba(167,139,250,0.15);color: var(--purple); }
.kpi-icon.week     { background: rgba(59,130,246,0.12); color: var(--accent); }

.kpi-label { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.05em; font-weight: 500; }
.kpi-value { font-size: 26px; font-weight: 600; font-family: var(--mono); letter-spacing: -0.02em; margin-top: 2px; }

/* ── CARDS ───────────────────────────────────────────── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  /* overflow:hidden clips table content and pagination — use visible instead,
     border-radius still works on the background */
}

.card-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.card-header h3 { font-size: 13px; font-weight: 600; letter-spacing: 0.01em; }

/* ── CHARTS ──────────────────────────────────────────── */
.chart-wrap { padding: 16px 18px; height: 260px; position: relative; }
.chart-wrap-sm { height: 200px; position: relative; }
.chart-wrap canvas, .chart-wrap-sm canvas { max-height: 100%; }

/* ── TOPS GRID ───────────────────────────────────────── */
.tops-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 16px;
}
@media (max-width: 900px) { .tops-grid { grid-template-columns: 1fr; } }

.top-table-wrap { padding: 0 0 4px; }
.top-table { width: 100%; border-collapse: collapse; }
.top-table tr { border-bottom: 1px solid var(--border); }
.top-table tr:last-child { border-bottom: none; }
.top-table td {
  padding: 9px 18px;
  font-size: 12px;
}
.top-table td:first-child {
  font-family: var(--mono);
  color: var(--text);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px;
}
.top-table td:last-child {
  text-align: right;
  font-family: var(--mono);
  font-weight: 600;
  color: var(--accent);
  white-space: nowrap;
}
.top-bar-wrap { display: flex; align-items: center; gap: 8px; }
.top-bar {
  height: 4px; border-radius: 2px;
  background: var(--accent); opacity: 0.6;
  min-width: 4px; flex-shrink: 0;
}

/* ── LOG TABLE ───────────────────────────────────────── */
.log-table-wrap { overflow-x: auto; overflow-y: visible; }
.log-table {
  width: 100%; border-collapse: collapse;
  font-size: 12px;
}
.log-table thead th {
  padding: 10px 14px;
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  background: var(--surface2);
}
.log-table tbody tr {
  border-bottom: 1px solid rgba(37,43,59,0.6);
  transition: background 0.1s;
}
.log-table tbody tr:hover { background: var(--surface2); }
.log-table tbody td {
  padding: 9px 14px;
  font-family: var(--mono);
  font-size: 11.5px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.log-table tbody td.td-date { color: var(--muted); white-space: nowrap; }
.log-table tbody td.td-status {}

.status-badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px;
  border-radius: 20px;
  font-size: 10.5px; font-weight: 600;
  text-transform: uppercase; letter-spacing: 0.04em;
}
.status-sent     { background: rgba(34,197,94,0.15);  color: var(--green); }
.status-bounced  { background: rgba(239,68,68,0.15);  color: var(--red); }
.status-deferred { background: rgba(245,158,11,0.15); color: var(--yellow); }
.status-unknown  { background: rgba(100,116,139,0.15);color: var(--muted); }

/* ── PAGINATION ──────────────────────────────────────── */
.pagination {
  display: flex; align-items: center; gap: 6px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  justify-content: flex-end;
}
.pagination .page-info {
  font-size: 12px; color: var(--muted); font-family: var(--mono);
  margin-right: auto;
}
.page-btn {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
  font-family: var(--mono);
  transition: background 0.1s, border-color 0.1s;
}
.page-btn:hover { background: var(--border); }
.page-btn.active { background: var(--accent); border-color: var(--accent); color: white; }
.page-btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── TERMINAL ────────────────────────────────────────── */
.card-terminal {
  display: flex;
  flex-direction: column;
  flex: 1;             /* fill the remaining height of the live page */
  min-height: 0;       /* allow shrinking inside flex parent */
  overflow: hidden;
}

.terminal-statusbar {
  display: flex; align-items: center; gap: 6px;
  padding: 10px 16px;
  background: var(--surface2);
  border-bottom: 1px solid var(--border);
}
.terminal-dot {
  width: 11px; height: 11px; border-radius: 50%;
}
.terminal-dot.red    { background: #ff5f57; }
.terminal-dot.yellow { background: #febc2e; }
.terminal-dot.green  { background: #28c840; }
.terminal-status {
  margin-left: 8px;
  font-size: 12px; font-family: var(--mono);
  color: var(--muted);
}
.terminal-count {
  margin-left: auto;
  font-size: 11px; font-family: var(--mono);
  color: var(--muted);
}

.terminal-output {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 12px 16px;
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.7;
  background: #080a0f;
  min-height: 0;       /* critical: allows flex child to scroll instead of grow */
}
.log-line { display: block; white-space: pre-wrap; word-break: break-all; }
.log-line.ll-warn  { color: var(--yellow); }
.log-line.ll-error { color: var(--red); }
.log-line.ll-sent  { color: var(--green); }
.log-line.ll-info  { color: #94a3b8; }
.log-line.ll-ts    { color: var(--muted); }

/* ── QUEUE PAGE ──────────────────────────────────────── */
.queue-actions { display: flex; gap: 6px; }
.btn-sm {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: 7px 12px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  font-family: var(--sans);
  transition: background 0.1s, border-color 0.1s;
}
.btn-sm svg { width: 14px; height: 14px; }
.btn-sm:hover { background: var(--border); }
.btn-warning { border-color: rgba(245,158,11,0.4); color: var(--yellow); }
.btn-warning:hover { background: rgba(245,158,11,0.1); }
.btn-danger  { border-color: rgba(239,68,68,0.4); color: var(--red); }
.btn-danger:hover  { background: rgba(239,68,68,0.1); }
.btn-success { border-color: rgba(34,197,94,0.4); color: var(--green); }
.btn-success:hover { background: rgba(34,197,94,0.1); }

.btn-icon {
  background: none;
  border: 1px solid var(--border);
  color: var(--muted);
  border-radius: var(--radius);
  padding: 7px;
  cursor: pointer;
  display: flex; align-items: center;
  transition: color 0.1s, background 0.1s;
}
.btn-icon svg { width: 15px; height: 15px; }
.btn-icon:hover { color: var(--text); background: var(--surface2); }

.action-btn {
  background: none; border: none; cursor: pointer;
  color: var(--muted); padding: 3px 6px;
  border-radius: 4px;
  font-size: 11px;
  font-family: var(--mono);
  transition: color 0.1s, background 0.1s;
}
.action-btn:hover { color: var(--text); background: var(--surface2); }
.action-btn.del:hover { color: var(--red); }
.action-btn.hold:hover { color: var(--yellow); }
.action-btn.release:hover { color: var(--green); }

.empty-state {
  display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 12px;
  padding: 48px;
  color: var(--muted);
  font-size: 13px;
}
.empty-state svg { width: 64px; height: 64px; opacity: 0.4; }

/* ── CONTROLS ────────────────────────────────────────── */
.select-sm {
  padding: 7px 10px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  width: auto;
}
.search-input { width: 240px; padding: 7px 12px; font-size: 13px; }
.date-picker { width: auto; padding: 6px 10px; font-size: 12px; }

.toggle-label {
  display: flex; align-items: center; gap: 6px;
  font-size: 12px; color: var(--muted);
  cursor: pointer;
  user-select: none;
}
.toggle-label input { cursor: pointer; accent-color: var(--accent); }

/* ── TOAST ───────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 24px; right: 24px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 18px;
  font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
  z-index: 9999;
  animation: slideIn 0.2s ease;
  max-width: 360px;
}
.toast.toast-ok  { border-color: rgba(34,197,94,0.4); }
.toast.toast-err { border-color: rgba(239,68,68,0.4); color: var(--red); }
@keyframes slideIn { from{transform:translateY(8px);opacity:0} to{transform:translateY(0);opacity:1} }

/* ── SCROLLBAR ───────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* ── LIVE PAGE — fixed height terminal ─────────────────────────── */
#page-live {
  /* Use flex-grow + calc to fill remaining viewport without overflow:hidden
     which would break scroll on other pages when switching back */
  flex: 1;
  min-height: 0;
}

/* ── SETTINGS PAGE ───────────────────────────────────────────── */
.settings-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  background: var(--border);
}
@media (max-width: 900px) { .settings-grid { grid-template-columns: repeat(2, 1fr); } }

.stat-item {
  background: var(--surface);
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.stat-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stat-value {
  font-family: var(--mono);
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.settings-section { padding: 20px; display: flex; flex-direction: column; gap: 20px; }
.settings-desc { font-size: 13px; color: var(--muted); line-height: 1.6; }
.settings-desc-inline { font-size: 12px; color: var(--muted); }

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 0;
  border-top: 1px solid var(--border);
}
.settings-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  flex-shrink: 0;
}
.settings-control {
  display: flex;
  align-items: center;
  gap: 10px;
}

.nrcpt-badge {
  display: inline-block;
  background: rgba(167,139,250,0.15);
  color: var(--purple);
  font-family: var(--mono);
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
}

/* ── TREND ARROWS ─────────────────────────────────────────────── */
.kpi-trend {
  font-size: 11px;
  font-family: var(--mono);
  margin-top: 2px;
  min-height: 16px;
}
.trend-up   { color: var(--green); }
.trend-down { color: var(--red); }
.trend-flat { color: var(--muted); }

/* ── ANALYTICS BOTTOM GRID ───────────────────────────────────── */
.analytics-bottom-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}
@media (max-width: 1000px) { .analytics-bottom-grid { grid-template-columns: 1fr; } }

/* ── HEATMAP ─────────────────────────────────────────────────── */
.heatmap-container {
  padding: 16px;
  overflow-x: auto;
}
.heatmap-grid {
  display: grid;
  grid-template-columns: 32px repeat(24, 1fr);
  gap: 2px;
  min-width: 580px;
}
.heatmap-label {
  font-size: 9px;
  font-family: var(--mono);
  color: var(--muted);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 4px;
  white-space: nowrap;
}
.heatmap-hour-label {
  font-size: 9px;
  font-family: var(--mono);
  color: var(--muted);
  text-align: center;
  padding-bottom: 2px;
}
.heatmap-cell {
  aspect-ratio: 1;
  border-radius: 2px;
  background: var(--surface2);
  cursor: default;
  transition: opacity 0.1s;
  position: relative;
}
.heatmap-cell:hover { opacity: 0.8; }
.heatmap-legend {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px 0 0;
  font-size: 10px;
  color: var(--muted);
  font-family: var(--mono);
}
.heatmap-legend-scale {
  display: flex;
  gap: 2px;
}
.heatmap-legend-cell {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

/* ── BOUNCE TABLE ────────────────────────────────────────────── */
.bounce-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}
.bounce-table thead th {
  padding: 8px 14px;
  text-align: left;
  font-size: 10px;
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
}
.bounce-table tbody tr {
  border-bottom: 1px solid rgba(37,43,59,0.5);
}
.bounce-table tbody tr:hover { background: var(--surface2); }
.bounce-table tbody td {
  padding: 8px 14px;
  font-family: var(--mono);
  font-size: 11px;
}
.bounce-table td:first-child {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.bounce-rate-bar {
  display: flex;
  align-items: center;
  gap: 6px;
}
.bounce-rate-fill {
  height: 4px;
  border-radius: 2px;
  min-width: 2px;
}
.rate-ok   { background: var(--green); }
.rate-warn { background: var(--yellow); }
.rate-bad  { background: var(--red); }
