:root {
    --bg: #0b1020;
    --bg-soft: #11182c;
    --panel: #151d33;
    --panel-2: #1a2440;
    --border: #263355;
    --text: #e8edf7;
    --muted: #8c9ab8;
    --accent: #4f8cff;
    --accent-2: #22d3a6;
    --danger: #ff6b7a;
    --warn: #ffc857;
    --radius: 14px;
    --shadow: 0 12px 34px rgba(0, 0, 0, .28);
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
    background: radial-gradient(circle at 12% -10%, #1b2a55 0, transparent 42%),
                radial-gradient(circle at 95% 0%, #16274a 0, transparent 35%), var(--bg);
    color: var(--text);
    font: 14px/1.5 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
button, input, select { font: inherit; }
button { cursor: pointer; }

.boot { display: grid; place-items: center; min-height: 100vh; color: var(--muted); }

/* ---------- Auth ---------- */
.auth-screen {
    min-height: 100vh;
    display: grid;
    place-items: center;
    padding: 24px;
}
.auth-card {
    width: min(430px, 100%);
    background: linear-gradient(160deg, rgba(30, 42, 76, .96), rgba(17, 24, 44, .96));
    border: 1px solid var(--border);
    border-radius: 20px;
    box-shadow: var(--shadow);
    padding: 30px;
}
.auth-logo { margin-bottom: 8px; }
.auth-logo-img { height: 44px; width: auto; display: block; max-width: 100%; }
.auth-sub { color: var(--muted); margin: 0 0 24px; font-size: 13px; }
.auth-tabs { display: flex; gap: 6px; background: #0f162b; padding: 4px; border-radius: 10px; margin-bottom: 20px; }
.auth-tabs button {
    flex: 1; border: 0; background: transparent; color: var(--muted);
    padding: 9px; border-radius: 8px; font-weight: 600;
}
.auth-tabs button.active { background: var(--panel-2); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.25); }
.auth-card .field { margin-bottom: 14px; }
.auth-card .btn { width: 100%; margin-top: 8px; }

/* ---------- Layout ---------- */
.app-shell { display: flex; min-height: 100vh; }
.sidebar {
    width: 238px; flex: 0 0 238px; background: rgba(12, 18, 36, .92);
    border-right: 1px solid var(--border); padding: 22px 14px;
    display: flex; flex-direction: column; gap: 6px;
    position: sticky; top: 0; height: 100vh;
}
.brand {
    display: flex; align-items: center; padding: 2px 6px 20px;
}
.brand-logo { height: 38px; width: auto; display: block; max-width: 100%; }
.nav-label { color: #61709a; font-size: 11px; text-transform: uppercase; letter-spacing: .1em; margin: 14px 10px 6px; }
.nav-item {
    --nav-color: #4f8cff; --nav-rgb: 79,140,255;
    display: flex; align-items: center; gap: 11px; padding: 10px 12px;
    color: var(--muted); border-radius: 10px; font-weight: 600; font-size: 13px; transition: .15s;
}
.nav-item:hover { background: rgba(var(--nav-rgb), .09); color: var(--text); }
.nav-item.active { background: rgba(var(--nav-rgb), .16); color: #e8f0ff; box-shadow: inset 3px 0 0 var(--nav-color); }
.nav-item .ico {
    width: 28px; height: 28px; border-radius: 8px; display: inline-flex; align-items: center; justify-content: center;
    background: rgba(var(--nav-rgb), .12); color: var(--nav-color); flex: 0 0 28px;
}
.nav-item .ico svg { width: 16px; height: 16px; display: block; }
.nav-item.active .ico { background: rgba(var(--nav-rgb), .22); box-shadow: 0 0 0 1px rgba(var(--nav-rgb), .18) inset; }
.nav-blue { --nav-color:#5b93ff; --nav-rgb:91,147,255; }
.nav-cyan { --nav-color:#22d3ee; --nav-rgb:34,211,238; }
.nav-green { --nav-color:#34d399; --nav-rgb:52,211,153; }
.nav-orange { --nav-color:#fb923c; --nav-rgb:251,146,60; }
.nav-violet { --nav-color:#a78bfa; --nav-rgb:167,139,250; }
.nav-amber { --nav-color:#fbbf24; --nav-rgb:251,191,36; }
.nav-slate { --nav-color:#94a3b8; --nav-rgb:148,163,184; }
.nav-rose { --nav-color:#fb7185; --nav-rgb:251,113,133; }
.sidebar-foot { margin-top: auto; border-top: 1px solid var(--border); padding: 14px 10px 0; }
.sidebar-foot .user-name { font-weight: 700; font-size: 13px; }
.sidebar-foot .user-role { color: var(--muted); font-size: 11px; }

.main { flex: 1; min-width: 0; }
.topbar {
    height: 64px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between;
    padding: 0 28px; background: rgba(11, 16, 32, .6); backdrop-filter: blur(10px);
    position: sticky; top: 0; z-index: 5;
}
.topbar h1 { font-size: 17px; margin: 0; letter-spacing: -.2px; }
.topbar .pill {
    background: var(--panel); border: 1px solid var(--border); color: var(--muted);
    padding: 6px 12px; border-radius: 999px; font-size: 12px;
}
.topbar .pill b { color: var(--accent-2); }
.content { padding: 26px 28px 50px; max-width: 1300px; }

/* ---------- Cards & grids ---------- */
.grid { display: grid; gap: 16px; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.stats-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stats-grid-5 { grid-template-columns: repeat(5, minmax(0, 1fr)); }
.card {
    background: linear-gradient(160deg, rgba(24, 33, 60, .95), rgba(18, 25, 45, .95));
    border: 1px solid var(--border); border-radius: var(--radius);
    box-shadow: var(--shadow); overflow: hidden;
}
.card-head {
    padding: 15px 18px; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.card-head h2 { font-size: 14px; margin: 0; }
.card-head .hint { color: var(--muted); font-size: 12px; }
.card-body { padding: 18px; }
.stack { display: grid; gap: 16px; }

.stat-card { padding: 18px; position: relative; overflow: hidden; }
.stat-card::after {
    content: ''; position: absolute; right: -22px; top: -22px;
    width: 80px; height: 80px; border-radius: 50%; background: rgba(79,140,255,.08);
}
.stat-label { color: var(--muted); font-size: 12px; }
.stat-value { font-size: 25px; font-weight: 800; margin-top: 6px; letter-spacing: -.6px; }
.stat-sub { color: var(--muted); font-size: 11px; margin-top: 4px; }
.color-green { color: var(--accent-2); }
.color-blue { color: var(--accent); }
.color-warn { color: var(--warn); }
.color-danger { color: var(--danger); }

/* ---------- Forms ---------- */
.form-row { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.field { display: grid; gap: 6px; min-width: 0; }
.field.grow { flex: 1; }
.label { color: var(--muted); font-size: 12px; font-weight: 600; }
.input, .select {
    background: #0e1529; border: 1px solid var(--border); color: var(--text);
    border-radius: 9px; padding: 9px 11px; outline: none; min-height: 38px;
    width: 100%;
}
.input:focus, .select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(79,140,255,.13); }
.input::placeholder { color: #5d6b90; }
.select { appearance: auto; }

.btn {
    border: 1px solid transparent; border-radius: 9px; padding: 9px 14px;
    font-weight: 700; font-size: 13px; background: var(--panel-2); color: var(--text);
    transition: transform .12s ease, background .12s ease, border-color .12s ease;
    min-height: 38px;
}
.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(0); }
.btn-primary { background: linear-gradient(135deg, #4f8cff, #6f6bff); color: #fff; }
.btn-green { background: rgba(34,211,166,.16); color: #7ff0cf; border-color: rgba(34,211,166,.35); }
.btn-danger { background: rgba(255,107,122,.13); color: #ff9aa5; border-color: rgba(255,107,122,.3); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--muted); }
.btn-sm { padding: 5px 9px; min-height: 28px; font-size: 11px; }
.btn:disabled { opacity: .55; cursor: not-allowed; transform: none; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
.table-wrap-limit { overflow-x: auto; overflow-y: auto; max-height: calc(2.8rem * 15 + 3rem); }
.table-wrap-limit thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; box-shadow: 0 1px 0 var(--border); }
.live-table-wrap { overflow-x: auto; overflow-y: auto; max-height: calc(2.8rem * 10 + 3rem); }
.live-table-wrap thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; box-shadow: 0 1px 0 var(--border); }
.table-wrap-limit-15 { overflow-x: auto; overflow-y: auto; max-height: calc(2.8rem * 15 + 3rem); }
.table-wrap-limit-15 thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; box-shadow: 0 1px 0 var(--border); }
.table-wrap-limit-20 { overflow-x: auto; overflow-y: auto; max-height: calc(2.8rem * 20 + 3rem); }
.table-wrap-limit-20 thead th { position: sticky; top: 0; background: var(--panel); z-index: 1; box-shadow: 0 1px 0 var(--border); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th {
    text-align: left; color: #7d8bb0; font-size: 11px; text-transform: uppercase;
    letter-spacing: .05em; padding: 11px 14px; border-bottom: 1px solid var(--border);
    white-space: nowrap;
}
td { padding: 11px 14px; border-bottom: 1px solid rgba(38,51,85,.55); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: rgba(79,140,255,.04); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
.muted { color: var(--muted); }
.small { font-size: 11px; }
.nowrap { white-space: nowrap; }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }

.badge {
    display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px;
    border-radius: 999px; font-size: 11px; font-weight: 700;
    background: rgba(140,154,184,.12); color: var(--muted);
}
.badge-green { background: rgba(34,211,166,.13); color: #71e6c2; }
.badge-red { background: rgba(255,107,122,.13); color: #ff9aa5; }
.badge-blue { background: rgba(79,140,255,.15); color: #a9c8ff; }
.badge-warn { background: rgba(255,200,87,.14); color: #ffd98a; }

.empty { padding: 34px 16px; text-align: center; color: var(--muted); }
.empty .big { font-size: 26px; display: block; margin-bottom: 8px; opacity: .7; }

/* ---------- Chart ---------- */
.chart { display: grid; gap: 9px; }
.chart-row { display: grid; grid-template-columns: 68px 1fr 92px; gap: 10px; align-items: center; }
.chart-row-5 { grid-template-columns: 68px minmax(120px,1fr) 92px 92px 92px; }
.chart-label { color: var(--muted); font-size: 11px; }
.chart-track {
    height: 9px; background: #0d1428; border-radius: 99px; overflow: hidden;
    display: flex; border: 1px solid rgba(38,51,85,.7);
}
.chart-bar-proxy { background: linear-gradient(90deg, #4f8cff, #8b6bff); }
.chart-bar-direct { background: linear-gradient(90deg, #22d3a6, #4de0b8); }
.chart-value { text-align: right; font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; }
.chart-head-row .chart-label, .chart-head-row .chart-value { color: var(--muted); font-weight: 700; font-size: 10px; text-transform: uppercase; letter-spacing: .4px; }

/* System metrics */
.sys-grid { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 14px; }
.sys-item { background: rgba(17,26,49,.55); border: 1px solid var(--border); border-radius: 14px; padding: 14px; }
.sys-top { display: flex; justify-content: space-between; gap: 12px; align-items: flex-start; margin-bottom: 10px; }
.sys-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; font-weight: 700; }
.sys-value { font-size: 28px; font-weight: 800; line-height: 1.1; margin-top: 4px; }
.sys-sub { font-size: 12px; color: var(--muted); font-family: var(--mono); text-align: right; }
.sys-meter { height: 10px; background: rgba(143,161,199,.12); border-radius: 999px; overflow: hidden; border: 1px solid rgba(143,161,199,.12); }
.sys-meter-bar { height: 100%; border-radius: 999px; transition: width .35s ease; }
.sys-meter-blue { background: linear-gradient(90deg, #4f8cff, #7ab0ff); }
.sys-meter-green { background: linear-gradient(90deg, #22d3a6, #66e3c2); }
.sys-meter-amber { background: linear-gradient(90deg, #ffc857, #ffde8a); }
.sys-meter-red { background: linear-gradient(90deg, #ff6b7a, #ff9aa5); }
.sys-meter-purple { background: linear-gradient(90deg, #a78bfa, #c4b5fd); }
.sys-foot { margin-top: 10px; font-size: 12px; color: var(--muted); }
.color-amber { color: var(--warn); }
.color-red { color: var(--danger); }
.color-purple { color: #c4b5fd; }
@media (max-width: 1100px) { .sys-grid { grid-template-columns: 1fr; } }
.sort-btn { padding: 2px 6px; min-height: 0; font-size: 12px; line-height: 1.2; color: var(--text); }
.sort-btn:hover { background: rgba(79,140,255,.12); }

.rule-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.rule-chip, .rule-add-btn {
  border: 1px solid var(--border); border-radius: 999px; padding: 5px 10px;
  font-size: 12px; line-height: 1; cursor: pointer; transition: .18s ease;
  display: inline-flex; align-items: center; gap: 6px; background: #111a31; color: var(--text);
}
.rule-chip-has {
  background: rgba(34,211,166,.1); border-color: rgba(34,211,166,.28); color: #8ff0d3;
  cursor: default; font-weight: 600;
}
.rule-chip-remove {
  background: rgba(255,107,122,.08); border-color: rgba(255,107,122,.28); color: #ffb3bc; font-weight: 600;
}
.rule-chip-remove:hover {
  background: rgba(255,107,122,.18); border-color: rgba(255,107,122,.45); color: #ffd0d6;
  transform: translateY(-1px); box-shadow: 0 6px 16px rgba(255,107,122,.14);
}
.rule-more {
  min-width: 24px; height: 24px; padding: 0 7px; border-radius: 999px; font-size: 11px; font-weight: 700;
  display: inline-flex; align-items: center; justify-content: center;
  background: rgba(143,161,199,.1); border: 1px solid rgba(143,161,199,.25); color: var(--muted);
}
.rule-chip-missing { background: rgba(255,200,87,.08); border-color: rgba(255,200,87,.25); color: #ffd98a; cursor: default; }
.rule-add-btn {
  width: 30px; height: 30px; padding: 0; justify-content: center; font-size: 18px; font-weight: 700;
  background: rgba(79,140,255,.12); border-color: rgba(79,140,255,.35); color: #a9c8ff;
}
.rule-add-btn:hover { background: rgba(79,140,255,.22); transform: translateY(-1px); box-shadow: 0 6px 16px rgba(79,140,255,.18); }
@media (max-width: 900px) {
  .chart-row-5 { grid-template-columns: 56px minmax(80px,1fr) 72px 72px 72px; gap: 6px; }
}
.chart-legend { display: flex; gap: 16px; font-size: 11px; color: var(--muted); margin-bottom: 6px; }
.legend-dot { display: inline-block; width: 9px; height: 9px; border-radius: 3px; margin-right: 6px; }

/* ---------- Modal / toast ---------- */
.modal {
    position: fixed; inset: 0; background: rgba(4, 8, 18, .76); z-index: 50;
    display: grid; place-items: center; padding: 20px; backdrop-filter: blur(4px);
}
.modal-box {
    background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
    box-shadow: var(--shadow); padding: 22px; width: min(520px, 100%);
}
.modal-box h3 { margin: 0 0 10px; font-size: 16px; }
.modal-box p { color: var(--muted); margin: 0 0 16px; }
.code-box {
    background: #090e1c; border: 1px solid var(--border); border-radius: 9px;
    padding: 12px 14px; font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 12px; line-height: 1.65; white-space: pre-wrap; word-break: break-word; color: #bcd4ff; overflow-x: auto;
}
.api-doc { display: grid; gap: 18px; }
.api-section { padding: 16px 0; border-top: 1px solid rgba(38,50,82,.7); }
.api-section:first-child { padding-top: 0; border-top: 0; }
.api-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.api-title { font-size: 14px; font-weight: 700; color: var(--text); }
.api-path { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; color: #bcd4ff; background: rgba(79,140,255,.08); border: 1px solid rgba(79,140,255,.18); padding: 3px 8px; border-radius: 7px; }
.method { display: inline-flex; align-items: center; justify-content: center; min-width: 56px; padding: 3px 10px; border-radius: 999px; font-size: 11px; font-weight: 800; letter-spacing: .04em; }
.method-get { background: rgba(34,211,166,.14); color: #71e6c2; border: 1px solid rgba(34,211,166,.25); }
.method-post { background: rgba(79,140,255,.15); color: #a9c8ff; border: 1px solid rgba(79,140,255,.28); }
.method-delete { background: rgba(255,107,122,.14); color: #ffb3ba; border: 1px solid rgba(255,107,122,.25); }
.api-desc { color: var(--muted); font-size: 13px; margin-bottom: 10px; line-height: 1.6; }
.api-sub { font-size: 12px; font-weight: 700; color: #9fb0d0; margin: 12px 0 6px; text-transform: uppercase; letter-spacing: .04em; }
.api-note { font-size: 12px; color: var(--muted); background: rgba(79,140,255,.06); border: 1px solid rgba(79,140,255,.14); border-left: 3px solid var(--accent); padding: 9px 11px; border-radius: 8px; margin-top: 10px; line-height: 1.6; }
.api-note.warn { background: rgba(251,191,36,.06); border-color: rgba(251,191,36,.18); border-left-color: var(--warn); }
.api-table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 10px; }
.api-table th, .api-table td { padding: 8px 10px; border-bottom: 1px solid rgba(38,50,82,.6); text-align: left; vertical-align: top; }
.api-table th { color: #8ea0c2; font-weight: 700; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; background: rgba(255,255,255,.01); }
.api-table code { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 11px; color: #bcd4ff; background: rgba(79,140,255,.08); padding: 1px 5px; border-radius: 4px; }
#toast-root { position: fixed; right: 22px; bottom: 22px; z-index: 90; display: grid; gap: 8px; }
.toast {
    background: #1b2745; border: 1px solid var(--border); color: var(--text);
    padding: 11px 15px; border-radius: 10px; box-shadow: var(--shadow); font-size: 13px;
    animation: slide .2s ease;
}
.toast.error { border-color: rgba(255,107,122,.5); color: #ffb3ba; }
.toast.success { border-color: rgba(34,211,166,.5); color: #8bf0d1; }
@keyframes slide { from { transform: translateY(8px); opacity: 0; } }

/* ---------- Proxy input / output ---------- */
.proxy-io {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}
.proxy-pane { display: flex; flex-direction: column; min-width: 0; }
.pane-head {
    display: flex; justify-content: space-between; align-items: center;
    gap: 12px; margin-bottom: 8px;
}
.proxy-textarea {
    width: 100%; min-height: 320px; max-height: calc(1.6em * 15 + 20px); resize: vertical; overflow: auto;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 12.5px; line-height: 1.6; white-space: pre;
}
.proxy-textarea[readonly] { background: #0b1122; color: #bcd4ff; }
.pattern-textarea {
    width: 100%; min-height: 200px; max-height: calc(1.7em * 15 + 20px); resize: vertical; overflow: auto;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 13px; line-height: 1.7; white-space: pre;
}
.pattern-list { max-height: 150px; overflow: auto; line-height: 1.7; }
.pane-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 8px; }

/* Số thứ tự cho ô proxy đầu vào / đầu ra */
.textarea-wrap { display: flex; align-items: stretch; min-width: 0; }
.line-numbers {
    flex: 0 0 42px; width: 42px; margin: 0; box-sizing: border-box;
    padding: 9px 8px 9px 0; overflow: hidden; user-select: none;
    font-family: ui-monospace, Menlo, Consolas, monospace;
    font-size: 12.5px; line-height: 1.6; text-align: right;
    color: var(--muted); background: #0b1122;
    border: 1px solid var(--border); border-right: 0;
    border-radius: 9px 0 0 9px; white-space: pre;
    max-height: calc(1.6em * 15 + 20px);
}
.textarea-wrap > .input { border-radius: 0 9px 9px 0; min-width: 0; }
.textarea-wrap > .proxy-textarea[readonly] { border-left: 1px solid var(--border); }

@media (max-width: 1000px) {
    .stats-grid, .stats-grid-5 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
    .grid-2, .grid-3, .proxy-io { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
    .sidebar { width: 64px; flex-basis: 64px; padding: 18px 8px; }
    .brand { justify-content: center; padding-left: 0; padding-right: 0; }
    .brand-logo { width: 38px; height: 38px; object-fit: cover; object-position: left center; }
    .nav-text, .nav-label, .sidebar-foot .user-name, .sidebar-foot .user-role { display: none; }
    .nav-item { justify-content: center; padding: 10px; }
    .content { padding: 18px 14px 40px; }
    .topbar { padding: 0 14px; }
    .stats-grid, .stats-grid-5 { grid-template-columns: 1fr 1fr; }
}
