:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --grid: #e9e8e4;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --good: #0ca30c;
  --good-text: #006300;
  --warning: #fab219;
  --critical: #d03b3b;
  --accent: #2a78d6;
  --banner-bg: #fff4e0;
  --banner-border: #fab219;
  --radius: 10px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans Arabic", sans-serif;
  font-size: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface-1: #1a1a19;
    --surface-2: #232321;
    --border: #2e2e2b;
    --grid: #292926;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #8e8d85;
    --series-1: #3987e5;
    --series-2: #d95926;
    --good-text: #4cc94c;
    --accent: #3987e5;
    --banner-bg: #2b2412;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface-1: #1a1a19; --surface-2: #232321; --border: #2e2e2b; --grid: #292926;
  --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #8e8d85;
  --series-1: #3987e5; --series-2: #d95926; --good-text: #4cc94c; --accent: #3987e5; --banner-bg: #2b2412;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; background: var(--bg); color: var(--text-primary); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 15px; }
h3 { font-size: 13px; margin-top: 18px; }
.muted { color: var(--text-muted); font-weight: 400; }
.small { font-size: 12px; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* top bar */
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 20px;
  background: var(--surface-1); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 5; flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; }
.health { display: flex; gap: 8px; flex: 1; flex-wrap: wrap; }
.user { display: flex; align-items: center; gap: 10px; color: var(--text-secondary); }
.pill {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-secondary); font-size: 12px; border: 1px solid var(--border);
}
.pill-btn { cursor: pointer; font: inherit; font-size: 12px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); }
.pill.ok .dot { background: var(--good); }
.pill.warn .dot { background: var(--warning); }
.pill.bad .dot { background: var(--critical); }

.btn { font: inherit; border-radius: 8px; padding: 7px 14px; cursor: pointer; border: 1px solid var(--border); background: var(--surface-1); color: var(--text-primary); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn:focus-visible, button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.banner { padding: 10px 20px; background: var(--banner-bg); border-bottom: 1px solid var(--banner-border); color: var(--text-primary); }
.banner::before { content: "⚠ "; }

.wrap { max-width: 1440px; margin: 0 auto; padding: 20px; display: grid; gap: 16px; }

/* KPI tiles */
.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; }
.kpi { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.kpi-label { color: var(--text-secondary); font-size: 12px; display: flex; align-items: center; gap: 6px; }
.kpi-value { font-size: 28px; font-weight: 650; margin-top: 6px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em; }
.kpi-sub { font-size: 15px; color: var(--text-muted); font-weight: 500; margin-left: 4px; }
.kpi-foot { color: var(--text-muted); font-size: 12px; margin-top: 4px; min-height: 16px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; }
.swatch.s1 { background: var(--series-1); }
.swatch.s2 { background: var(--series-2); }

/* cards */
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.legend { display: flex; gap: 14px; color: var(--text-secondary); font-size: 12px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }

/* chart */
.chart { position: relative; height: 220px; }
.chart.small { height: 160px; margin-top: 8px; }
.d-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-top: 18px; }
.d-head h3 { margin: 0; }
.drawer .feed { max-height: 240px; }
.feed .sub { color: var(--text-muted); }
.chain { margin: 0 0 12px; font-size: 13px; }
.chain.ok { color: var(--good-text); }
.chain.bad { color: var(--critical); font-weight: 600; }
.chart svg { width: 100%; height: 100%; display: block; overflow: visible; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .axis-label { fill: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.s1 { stroke: var(--series-1); }
.chart .line.s2 { stroke: var(--series-2); }
.chart .area.s1 { fill: var(--series-1); opacity: 0.10; }
.chart .area.s2 { fill: var(--series-2); opacity: 0.08; }
.chart .crosshair { stroke: var(--text-muted); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .marker { stroke: var(--surface-1); stroke-width: 2; }
.chart .marker.s1 { fill: var(--series-1); }
.chart .marker.s2 { fill: var(--series-2); }
.tooltip {
  position: absolute; pointer-events: none; background: var(--surface-1); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; font-size: 12px; box-shadow: 0 4px 16px rgb(0 0 0 / 0.12); min-width: 150px;
}
.tooltip .t { color: var(--text-muted); margin-bottom: 4px; }
.tooltip .r { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.tooltip .r span:first-child { display: inline-flex; align-items: center; gap: 6px; color: var(--text-secondary); }

/* layout */
.grid { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
.side { display: grid; gap: 16px; }

/* filters */
.filters { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { font: inherit; font-size: 12px; padding: 6px 12px; border: 0; background: transparent; color: var(--text-secondary); cursor: pointer; }
.seg button.on { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
input[type=search], textarea, .login-card input {
  font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1); color: var(--text-primary);
}
input[type=search] { width: 220px; }

/* clients table */
.table-wrap { overflow-x: auto; }
table.clients { width: 100%; border-collapse: collapse; }
.clients th { text-align: left; font-weight: 500; color: var(--text-muted); font-size: 12px; padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.clients td { padding: 9px 10px; border-bottom: 1px solid var(--grid); white-space: nowrap; vertical-align: middle; }
.clients tbody tr { cursor: pointer; }
.clients tbody tr:hover { background: var(--surface-2); }
.clients .name { font-weight: 600; }
.clients .sub { color: var(--text-muted); font-size: 12px; }
.status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
.status .ic { width: 16px; height: 16px; border-radius: 50%; display: inline-grid; place-items: center; font-size: 10px; color: #fff; }
.status.online { color: var(--good-text); }
.status.online .ic { background: var(--good); }
.status.offline { color: var(--text-secondary); }
.status.offline .ic { background: var(--text-muted); }
.status.unknown { color: var(--text-secondary); }
.status.unknown .ic { background: var(--warning); color: #000; }
.tag { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--text-secondary); margin-left: 6px; font-weight: 500; }
.empty { color: var(--text-muted); text-align: center; padding: 18px 0; margin: 0; }

/* feed */
.feed { list-style: none; margin: 0; padding: 0; max-height: 360px; overflow-y: auto; }
.feed li { display: grid; grid-template-columns: 14px 1fr auto; gap: 8px; padding: 7px 0; border-bottom: 1px solid var(--grid); align-items: baseline; }
.feed .k { width: 8px; height: 8px; border-radius: 50%; background: var(--text-muted); margin-top: 4px; }
.feed .k.connect { background: var(--good); }
.feed .k.disconnect { background: var(--critical); }
.feed .k.circuit { background: var(--series-1); }
.feed .k.auth { background: var(--series-2); }
.feed time { color: var(--text-muted); font-size: 11px; font-variant-numeric: tabular-nums; }

.routers { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.routers li { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.routers .sub { color: var(--text-muted); font-size: 12px; }

/* dialogs */
.drawer {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-1); color: var(--text-primary);
  width: min(560px, calc(100vw - 32px)); padding: 18px 20px; max-height: calc(100vh - 64px);
}
.drawer::backdrop { background: rgb(0 0 0 / 0.35); }
.drawer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.facts { display: grid; grid-template-columns: 150px 1fr; gap: 8px 12px; margin: 0; }
.facts dt { color: var(--text-muted); }
.facts dd { margin: 0; word-break: break-word; }
.fp { display: block; font-size: 11px; padding: 8px; background: var(--surface-2); border-radius: 6px; word-break: break-all; margin: 6px 0 10px; }
textarea { width: 100%; font-family: ui-monospace, Consolas, monospace; font-size: 12px; margin-bottom: 10px; }
.error { color: var(--critical); margin: 0 0 10px; }

/* login */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 28px; width: min(380px, 100%); }
.login-card h1 { font-size: 20px; margin: 18px 0 16px; }
.login-card form { display: grid; gap: 14px; }
.login-card label { display: grid; gap: 6px; color: var(--text-secondary); font-size: 13px; }
.login-card .btn { margin-top: 4px; padding: 9px; }

@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 640px) {
  .topbar { padding: 10px 16px; }
  .wrap { padding: 16px; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  input[type=search] { width: 100%; }
  .facts { grid-template-columns: 1fr; }
}

/* ---------- admin shell ---------- */
.shell { display: grid; grid-template-columns: 210px minmax(0, 1fr); min-height: calc(100vh - 53px); }
.sidenav { background: var(--surface-1); border-right: 1px solid var(--border); padding: 12px 10px; position: sticky; top: 53px; align-self: start; max-height: calc(100vh - 53px); overflow-y: auto; }
.sidenav a { display: block; padding: 6px 10px; border-radius: 6px; color: var(--text-secondary); text-decoration: none; font-size: 13px; }
.sidenav a:hover { background: var(--surface-2); color: var(--text-primary); }
.sidenav a.on { background: var(--surface-2); color: var(--text-primary); font-weight: 600; box-shadow: inset 3px 0 0 var(--accent); }
.nav-group { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); padding: 14px 10px 4px; }
.nav-group:first-child { padding-top: 2px; }
.content { min-width: 0; }

.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 20px; }
.page-head p { margin: 4px 0 0; max-width: 720px; }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.btn.danger { color: var(--critical); border-color: var(--critical); background: transparent; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.pager { display: flex; justify-content: flex-end; align-items: center; gap: 8px; margin-top: 10px; }
.pager .muted { margin-right: auto; }

table.admin td { white-space: normal; }
table.admin tr.static { cursor: default; }
table.admin .sel { width: 28px; }
.row-menu { width: 36px; text-align: right; }
.menu { position: relative; display: inline-block; }
.menu summary { list-style: none; cursor: pointer; padding: 2px 8px; border-radius: 6px; font-size: 18px; line-height: 1; color: var(--text-secondary); }
.menu summary::-webkit-details-marker { display: none; }
.menu summary:hover { background: var(--surface-2); }
.menu-list { position: absolute; right: 0; top: 100%; z-index: 10; min-width: 190px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; box-shadow: 0 6px 20px rgb(0 0 0 / 0.18); padding: 4px; display: grid; }
.menu-list button { font: inherit; text-align: left; background: transparent; border: 0; padding: 7px 10px; border-radius: 6px; color: var(--text-primary); cursor: pointer; }
.menu-list button:hover { background: var(--surface-2); }
.menu-list button.danger { color: var(--critical); }

.chips { display: inline-flex; flex-wrap: wrap; gap: 4px; }
.chip { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--text-secondary); }
.chip button { border: 0; background: transparent; color: var(--text-muted); cursor: pointer; font-size: 14px; line-height: 1; padding: 0 0 0 2px; }
.tag.warn { background: var(--banner-bg); color: var(--text-primary); }
code.clip { display: inline-block; max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; vertical-align: bottom; font-size: 12px; color: var(--text-secondary); }

/* forms */
.drawer.wide { width: min(720px, calc(100vw - 32px)); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.field { display: grid; gap: 6px; align-content: start; }
.field.wide, .field:has(.chip-input), .field:has(textarea) { grid-column: 1 / -1; }
.field > label { font-size: 13px; color: var(--text-secondary); }
.field.bool { grid-column: 1 / -1; }
.field.bool label { display: flex; align-items: center; gap: 8px; color: var(--text-primary); }
.field .help { font-size: 12px; color: var(--text-muted); }
.field input[type=text], .field input[type=number], .field input[type=password], .field select, .field textarea {
  font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1); color: var(--text-primary); width: 100%;
}
textarea.mono, #token-text { font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.chip-input { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1); min-height: 38px; }
.chip-input input { flex: 1; min-width: 160px; border: 0; outline: 0; background: transparent; color: var(--text-primary); font: inherit; padding: 2px; }
.chip-input:focus-within { outline: 2px solid var(--accent); outline-offset: 1px; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.qr-wrap { display: grid; place-items: center; margin: 8px 0 12px; }
.qr-wrap svg { max-width: 100%; height: auto; border-radius: 6px; }

.toast { position: fixed; right: 20px; bottom: 20px; z-index: 100; background: var(--surface-1); border: 1px solid var(--border); border-left: 4px solid var(--good); border-radius: 8px; padding: 10px 14px; box-shadow: 0 6px 20px rgb(0 0 0 / 0.2); max-width: min(480px, calc(100vw - 40px)); }
.toast.bad { border-left-color: var(--critical); }

.attr-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; }
.attr-list { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.linkish { font: inherit; background: var(--surface-2); border: 1px solid var(--border); border-radius: 999px; padding: 2px 10px; color: var(--accent); cursor: pointer; }

@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr; }
  .sidenav { position: static; max-height: none; display: flex; overflow-x: auto; gap: 4px; border-right: 0; border-bottom: 1px solid var(--border); padding: 8px 16px; }
  .sidenav a { white-space: nowrap; }
  .nav-group { display: none; }
  .form-grid { grid-template-columns: 1fr; }
}

/* ---------- users & people ---------- */
.user-link { color: var(--text-secondary); text-decoration: none; }
.user-link:hover { color: var(--text-primary); text-decoration: underline; }
.owner { color: var(--text-secondary); font-weight: 600; }
.fp.big { font-size: 18px; letter-spacing: 0.06em; text-align: center; padding: 14px; user-select: all; }
.inline-form { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.inline-form input { font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-1); color: var(--text-primary); min-width: 200px; }
table.roles td:not(:first-child), table.roles th:not(:first-child) { text-align: center; width: 110px; }
.page-head a { color: var(--accent); text-decoration: none; }
.page-head a:hover { text-decoration: underline; }
