:root {
  --bg: #121826;
  --surface: #1a1f2e;
  --surface-hover: #222940;
  --fg: #e2e8f0;
  --fg-muted: #8892b0;
  --border: #2d3548;
  --accent: #00b894;
  --accent-hover: #00ce9e;
  --success: #4ade80;
  --danger: #ef4444;
  --warning: #f59e0b;
  --down: #4ade80;
  --up: #60a5fa;
  --font-display: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  --font-body: -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  --font-mono: ui-monospace, Menlo, monospace;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { width: 100%; min-height: 100vh; background: var(--bg); color: var(--fg); font: 15px/1.5 var(--font-body); -webkit-font-smoothing: antialiased; }
body { display: flex; }

.sidebar { width: 240px; background: var(--surface); border-right: 1px solid var(--border); display: flex; flex-direction: column; flex-shrink: 0; }
.logo { padding: 20px 24px; border-bottom: 1px solid var(--border); font: 600 16px/1.2 var(--font-display); }
.logo-icon { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; background: var(--accent); border-radius: 6px; margin-right: 10px; vertical-align: middle; }
.logo-icon svg { width: 16px; height: 16px; fill: none; stroke: #fff; stroke-width: 2; }
.nav { flex: 1; padding: 12px; }
.nav-item { display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-radius: 8px; color: var(--fg-muted); text-decoration: none; font: 500 14px/1.4 var(--font-body); transition: all .15s; margin-bottom: 2px; }
.nav-item:hover { background: var(--surface-hover); color: var(--fg); }
.nav-item.active { background: var(--accent); color: #fff; }
.nav-item svg { width: 18px; height: 18px; opacity: .7; fill: none; stroke: currentColor; stroke-width: 2; }
.nav-item.active svg { opacity: 1; }
.nav-section { padding: 16px 16px 8px; font: 600 11px/1.3 var(--font-mono); color: var(--fg-muted); text-transform: uppercase; letter-spacing: .08em; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.header { height: 60px; background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; padding: 0 24px; flex-shrink: 0; }
.header-title { font: 600 18px/1.3 var(--font-display); }
.header-meta { display: flex; align-items: center; gap: 20px; font: 13px var(--font-mono); color: var(--fg-muted); }
.status-dot { display: inline-block; width: 8px; height: 8px; background: var(--success); border-radius: 50%; animation: pulse 2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .5; } }
.content { flex: 1; padding: 24px; overflow-y: auto; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; margin-bottom: 20px; overflow: hidden; }
.card-header { padding: 16px 20px; border-bottom: 1px solid var(--border); display: flex; align-items: center; justify-content: space-between; }
.card-title { font: 600 15px/1.4 var(--font-display); }
.card-body { padding: 0; }

.stats-grid { display: grid; gap: 16px; margin-bottom: 24px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.stats-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.stats-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.stats-grid.cols-5 { grid-template-columns: repeat(5, 1fr); }
.stats-grid.cols-6 { grid-template-columns: repeat(6, 1fr); }
.stat-card { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 20px; }
.stat-label { font: 11px/1.3 var(--font-mono); color: var(--fg-muted); text-transform: uppercase; letter-spacing: .05em; margin-bottom: 8px; }
.stat-value { font: 28px/1.1 var(--font-mono); font-weight: 600; letter-spacing: -.02em; }
.stat-value.dl { color: var(--down); }
.stat-value.ul { color: var(--up); }
.stat-value.tot { color: var(--fg); }
.stat-value.danger { color: var(--danger); }
.stat-value.warning { color: var(--warning); }
.stat-value.success { color: var(--success); }
.stat-sub { font: 12px/1.4 var(--font-mono); color: var(--fg-muted); margin-top: 4px; }
.ul { color: var(--up); }
.dl { color: var(--down); }

.data-table { width: 100%; border-collapse: collapse; font: 13px/1.5 var(--font-mono); }
.data-table th { text-align: left; padding: 12px 16px; white-space: nowrap; background: var(--surface-hover); font: 600 11px/1.3 var(--font-mono); color: var(--fg-muted); text-transform: uppercase; letter-spacing: .05em; border-bottom: 1px solid var(--border); }
.data-table td { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: var(--surface-hover); }
.data-table .muted { color: var(--fg-muted); font-size: 11px; }
.data-table .danger { color: var(--danger); font-weight: 600; }
.data-table .warning { color: var(--warning); font-weight: 600; }
.data-table .info { color: var(--up); }
.data-table .dl { color: var(--down); }
.data-table .ul { color: var(--up); }
.table-wrap { overflow-x: auto; }
.total-row { background: var(--surface-hover); font-weight: 600; }

.pagination { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-top: 1px solid var(--border); }
.pagination-info { font: 13px var(--font-mono); color: var(--fg-muted); }
.pagination-buttons { display: flex; gap: 8px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 16px; border-radius: 6px; font: 500 13px/1.3 var(--font-body); border: 1px solid var(--border); background: var(--surface); color: var(--fg); cursor: pointer; transition: all .15s; text-decoration: none; }
.btn:hover { background: var(--surface-hover); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-sm { padding: 6px 12px; font: 12px var(--font-mono); }
.btn[disabled] { opacity: .45; cursor: default; }

.tag { display: inline-flex; align-items: center; white-space: nowrap; padding: 3px 8px; border-radius: 4px; font: 11px/1.3 var(--font-mono); font-weight: 500; }
.tag-danger { background: rgba(239, 68, 68, .15); color: var(--danger); }
.tag-success { background: rgba(74, 222, 128, .15); color: var(--success); }
.tag-warning { background: rgba(245, 158, 11, .15); color: var(--warning); }
.tag-info { background: rgba(96, 165, 250, .15); color: var(--up); }

.section-title { font: 600 13px/1.4 var(--font-display); color: var(--fg-muted); margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.section-title::before { content: ""; width: 3px; height: 14px; background: var(--accent); border-radius: 2px; }

.fail2ban-status { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 20px; margin-bottom: 24px; }
.fail2ban-title { font: 600 14px/1.4 var(--font-display); margin-bottom: 16px; display: flex; align-items: center; gap: 8px; }
.fail2ban-title::before { content: ""; width: 4px; height: 16px; background: var(--warning); border-radius: 2px; }
.fail2ban-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 20px; }
.fail2ban-stat { display: flex; align-items: baseline; gap: 8px; }
.fail2ban-stat .label { font: 12px var(--font-mono); color: var(--fg-muted); }
.fail2ban-stat .value { font: 20px/1 var(--font-mono); font-weight: 600; }
.fail2ban-stat .value.failed { color: var(--danger); }
.fail2ban-stat .value.banned { color: var(--warning); }
.fail2ban-stat .value.jail { color: var(--success); }

.realtime-section { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 20px; margin-bottom: 24px; }
.realtime-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }
.realtime-title { font: 600 14px/1.4 var(--font-display); display: flex; align-items: center; gap: 8px; }
.realtime-title::before { content: ""; width: 4px; height: 16px; background: var(--success); border-radius: 2px; }
.live-badge { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; background: rgba(74, 222, 128, .12); border-radius: 20px; font: 11px/1 var(--font-mono); color: var(--success); }
.live-dot { width: 6px; height: 6px; background: var(--success); border-radius: 50%; animation: pulse 1.5s infinite; }
.realtime-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
.realtime-stat { display: flex; flex-direction: column; gap: 4px; }
.realtime-stat .process-name { font: 13px var(--font-mono); color: var(--fg-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 200px; }
.realtime-stat .bandwidth { display: flex; gap: 16px; font: 14px var(--font-mono); }
.realtime-stat .bandwidth .down-color { color: var(--down); }
.realtime-stat .bandwidth .up-color { color: var(--up); }
.realtime-stat .bandwidth .label { color: var(--fg-muted); font-size: 12px; }
.data-source { font: 11px var(--font-mono); color: var(--fg-muted); margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.data-source a { color: var(--accent); cursor: pointer; }

.chart { display: flex; align-items: flex-end; gap: 4px; height: 160px; padding: 16px 20px 8px; }
.chart-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; min-width: 0; }
.chart-bar { background: linear-gradient(180deg, var(--accent) 0%, #0a8f74 100%); border-radius: 3px 3px 0 0; min-height: 2px; }
.chart-bar.over { background: linear-gradient(180deg, var(--warning) 0%, #b45309 100%); }
.chart-x { font: 9px/1.4 var(--font-mono); color: var(--fg-muted); text-align: center; margin-top: 4px; height: 14px; overflow: hidden; white-space: nowrap; }

.toolbar { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--border); }
.toolbar input { flex: 1; max-width: 320px; padding: 8px 12px; border-radius: 6px; border: 1px solid var(--border); background: var(--bg); color: var(--fg); font: 13px var(--font-mono); }
.toolbar input.short { max-width: 140px; }
.toolbar .hint { font: 12px var(--font-mono); }
.card-note { font: 11px var(--font-mono); color: var(--fg-muted); }
.auth-hint { color: var(--fg-muted); font: 13px var(--font-mono); margin-bottom: 12px; }
.card-body.pad { padding: 20px; }
.toolbar.flush { padding: 0; border: 0; }
.toolbar input:focus { outline: none; border-color: var(--accent); }
.empty { text-align: center; color: var(--fg-muted); padding: 24px; font: 13px var(--font-mono); }
.error { color: var(--danger); text-align: center; padding: 24px; font: 13px var(--font-mono); }
.loading { color: var(--fg-muted); text-align: center; padding: 24px; font: 13px var(--font-mono); }

/* locked: no valid token yet - hide the panel chrome entirely */
body.auth-locked .sidebar,
body.auth-locked .header { display: none; }
body.auth-locked .main { align-items: center; justify-content: center; }
body.auth-locked .content {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 24px;
}
body.auth-locked .content .card { width: 100%; max-width: 440px; margin: 0; }

::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--fg-muted); }

@media (max-width: 1200px) {
  .stats-grid.cols-6, .stats-grid.cols-4 { grid-template-columns: repeat(3, 1fr); }
  .realtime-grid { grid-template-columns: 1fr; }
}
@media (max-width: 900px) {
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 768px) {
  .sidebar { display: none; }
  .stats-grid.cols-6, .stats-grid.cols-4, .stats-grid.cols-3 { grid-template-columns: repeat(2, 1fr); }
  .content { padding: 16px; }
}

/* ---------------------------------------------------------------- rules page */
/* keep scrolling inside the editor: the page itself must not grow with the file */
body[data-page="rules"] { height: 100vh; overflow: hidden; }
body[data-page="rules"] .main { min-height: 0; }
body[data-page="rules"] .content { display: flex; flex-direction: column; height: calc(100vh - 60px); min-height: 0; overflow: hidden; }
body[data-page="rules"] .rules-grid { flex: 1; min-height: 0; display: flex; gap: 20px; align-items: stretch; }
body[data-page="rules"] .rules-grid > .card:first-child { flex: 1; min-width: 0; display: flex; flex-direction: column; min-height: 0; margin-bottom: 0; overflow: hidden; }
body[data-page="rules"] .rules-grid > .card:last-child { flex: none; width: 300px; align-self: flex-start; }
.rules-grid { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 20px; align-items: start; }
.card-path { font: 11px var(--font-mono); color: var(--fg-muted); margin-top: 3px; }
.file-meta { display: flex; gap: 20px; }
.file-meta .meta-item { text-align: right; }
.file-meta dt { font: 10px var(--font-mono); color: var(--fg-muted); text-transform: uppercase; letter-spacing: .05em; }
.file-meta dd { font: 12px var(--font-mono); color: var(--fg); margin-top: 2px; }
.unsaved-state { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; font: 12px var(--font-mono); color: var(--warning); opacity: 0; transition: opacity .15s; }
.unsaved-state.show { opacity: 1; }
.unsaved-state .state-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--warning); }
.confirm-strip { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 12px 20px; background: rgba(245, 158, 11, .08); border-top: 1px solid var(--border); border-bottom: 1px solid var(--border); }
.confirm-strip[hidden] { display: none; }
.confirm-title { font: 600 13px var(--font-display); color: var(--warning); }
.confirm-text { font: 12px var(--font-mono); color: var(--fg-muted); margin-top: 3px; }
.confirm-actions { display: flex; gap: 8px; }
.editor { display: flex; flex: 1; min-height: 0; background: #0d1220; }
.line-gutter { padding: 12px 8px 12px 12px; text-align: right; user-select: none; overflow: hidden; border-right: 1px solid var(--border); background: rgba(255, 255, 255, .02); }
.line-numbers { font: 12px/20px var(--font-mono); color: var(--fg-muted); opacity: .6; white-space: pre; }
.code-area { flex: 1; height: 100%; min-height: 0; padding: 12px 16px; border: 0; background: transparent; color: var(--fg); font: 12px/20px var(--font-mono); resize: none; outline: none; white-space: pre; overflow: auto; tab-size: 2; }
.code-area:focus { box-shadow: inset 0 0 0 1px var(--accent); }
.error-bar { display: none; padding: 10px 20px; background: rgba(239, 68, 68, .1); border-top: 1px solid var(--border); color: var(--danger); font: 12px/1.6 var(--font-mono); white-space: pre-wrap; }
.error-bar.show { display: block; }
.rule-types { display: flex; flex-wrap: wrap; gap: 6px; padding: 16px 20px; }
.rule-types code { font: 11px var(--font-mono); color: var(--fg); background: var(--surface-hover); border: 1px solid var(--border); border-radius: 4px; padding: 2px 6px; }
.rule-hint { padding: 0 20px 16px; font: 12px/1.7 var(--font-mono); color: var(--fg-muted); }
.rule-hint strong { color: var(--warning); font-weight: 600; }
.toast { position: fixed; right: 24px; bottom: 24px; padding: 10px 16px; border-radius: 8px; background: var(--surface-hover); border: 1px solid var(--border); color: var(--fg); font: 12px var(--font-mono); opacity: 0; transform: translateY(8px); transition: all .2s; pointer-events: none; z-index: 20; }
.toast.show { opacity: 1; transform: none; }
.toast.err { border-color: var(--danger); color: var(--danger); }
@media (max-width: 1100px) {
  .rules-grid { grid-template-columns: 1fr; }
  /* narrow screens: give scrolling back to the page, editor keeps a fixed height */
  body[data-page="rules"] { height: auto; overflow: auto; }
  body[data-page="rules"] .content { height: auto; overflow: visible; }
  body[data-page="rules"] .rules-grid { display: block; }
  body[data-page="rules"] .rules-grid > .card:last-child { width: auto; margin-top: 20px; }
  body[data-page="rules"] .editor { flex: none; height: 60vh; }
}
