:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --surface-hover: #f5f5f3;
  --border: #e3e2de;
  --border-strong: #cfcdc7;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7c7b76;
  --accent: #2a78d6;
  --accent-hover: #256abf;
  --accent-soft: #e6f0fc;
  --accent-text: #1c5cab;
  --on-accent: #ffffff;
  --danger: #c53333;
  --danger-soft: #fbe9e9;
  --up: #006300;
  --down: #c53333;
  --good: #0ca30c;
  --warning: #fab219;
  --critical: #d03b3b;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --shadow: 0 1px 2px rgb(0 0 0 / .04), 0 4px 16px rgb(0 0 0 / .06);
  --shadow-lg: 0 8px 40px rgb(0 0 0 / .18);
  --radius: 10px;
  --radius-sm: 6px;
  --font: "Inter", "Vazirmatn", system-ui, -apple-system, "Segoe UI", Tahoma, Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #232321;
    --surface-hover: #212120;
    --border: #2e2e2b;
    --border-strong: #3f3e3a;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --text-3: #8f8e86;
    --accent: #3987e5;
    --accent-hover: #5598e7;
    --accent-soft: #17283d;
    --accent-text: #86b6ef;
    --danger: #e66767;
    --danger-soft: #3a1d1d;
    --up: #4cc44c;
    --down: #e66767;
    --series-1: #3987e5;
    --series-2: #d95926;
    --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 16px rgb(0 0 0 / .25);
    --shadow-lg: 0 8px 40px rgb(0 0 0 / .5);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211;
  --surface: #1a1a19;
  --surface-2: #232321;
  --surface-hover: #212120;
  --border: #2e2e2b;
  --border-strong: #3f3e3a;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --text-3: #8f8e86;
  --accent: #3987e5;
  --accent-hover: #5598e7;
  --accent-soft: #17283d;
  --accent-text: #86b6ef;
  --danger: #e66767;
  --danger-soft: #3a1d1d;
  --up: #4cc44c;
  --down: #e66767;
  --series-1: #3987e5;
  --series-2: #d95926;
  --shadow: 0 1px 2px rgb(0 0 0 / .3), 0 4px 16px rgb(0 0 0 / .25);
  --shadow-lg: 0 8px 40px rgb(0 0 0 / .5);
}

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

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

body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent-text); text-decoration: none; }
a:hover { text-decoration: underline; }

button, input, select { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

svg { display: block; }

.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- Shell ---------- */

.shell {
  display: grid;
  grid-template-columns: 232px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 20px 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 6px;
  color: var(--text);
}
.brand:hover { text-decoration: none; }
.brand-mark svg { width: 32px; height: 32px; }
.brand-mark rect { fill: var(--accent); }
.brand-mark path { fill: none; stroke: #fff; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 14px; }
.brand-text small { color: var(--text-3); font-size: 12px; }

.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  color: var(--text-2);
  font-weight: 500;
}
.nav a:hover { background: var(--surface-hover); color: var(--text); text-decoration: none; }
.nav a.active { background: var(--accent-soft); color: var(--accent-text); }
.nav svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.nav-badge {
  margin-left: auto;
  min-width: 20px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 11px;
  font-weight: 600;
  line-height: 18px;
  text-align: center;
}

.sidebar-footer { margin-top: auto; display: flex; flex-direction: column; gap: 12px; }

.conn {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 6px;
  color: var(--text-2);
  font-size: 12.5px;
}
.conn-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); flex: none; }
.conn[data-state="open"] .conn-dot { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent); }
.conn[data-state="connecting"] .conn-dot { background: var(--warning); animation: pulse 1.2s ease-in-out infinite; }
.conn[data-state="closed"] .conn-dot { background: var(--critical); }
@keyframes pulse { 50% { opacity: .35; } }

.toggles { display: flex; align-items: center; gap: 8px; }

/* ---------- Controls ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 34px;
  padding: 0 14px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
}
.btn:hover { background: var(--surface-hover); }
.btn:disabled { opacity: .55; cursor: default; }
.btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(1.08); background: var(--danger); }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-sm { height: 28px; padding: 0 10px; font-size: 13px; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-2);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.sidebar .icon-btn { border-color: var(--border); }

.segmented {
  display: inline-flex;
  padding: 2px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
}
.segmented button {
  height: 26px;
  padding: 0 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--text-2);
  font-size: 12.5px;
  font-weight: 500;
  cursor: pointer;
}
.segmented button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgb(0 0 0 / .12); }

.input, .field input, .field select {
  width: 100%;
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: var(--surface);
}
.input:focus, .field input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }

.search {
  position: relative;
  flex: 1 1 260px;
  max-width: 380px;
}
.search svg {
  position: absolute; left: 10px; top: 50%; transform: translateY(-50%);
  width: 16px; height: 16px; fill: none; stroke: var(--text-3); stroke-width: 2; stroke-linecap: round;
}
.search .input { padding-left: 34px; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: var(--surface);
  color: var(--text-2);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.chip:hover { border-color: var(--border-strong); color: var(--text); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: transparent; color: var(--accent-text); }
.chip .count { color: var(--text-3); font-variant-numeric: tabular-nums; }
.chip[aria-pressed="true"] .count { color: inherit; opacity: .75; }

/* ---------- Page ---------- */

.main { min-width: 0; padding: 28px 32px 48px; outline: none; }

.page { max-width: 1240px; margin: 0 auto; display: flex; flex-direction: column; gap: 20px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; }
.page-head h1 { margin: 0; font-size: 22px; line-height: 1.25; letter-spacing: -.01em; }
.page-head .sub { margin: 4px 0 0; color: var(--text-2); }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.crumbs { display: flex; gap: 6px; align-items: center; color: var(--text-3); font-size: 13px; margin-bottom: 6px; }
.crumbs a { color: var(--text-2); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.card-head h2 { margin: 0; font-size: 15px; }
.card-body { padding: 18px; }

.toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* ---------- Table ---------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: middle; }
th {
  position: sticky; top: 0;
  background: var(--surface);
  color: var(--text-3);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
  white-space: nowrap;
}
th.r, td.r { text-align: right; }
tbody tr:hover { background: var(--surface-hover); }
tbody tr:last-child td { border-bottom: 0; }
td.actions { width: 1%; white-space: nowrap; text-align: right; }
td.actions .icon-btn { opacity: .6; }
tr:hover td.actions .icon-btn, td.actions .icon-btn:focus-visible { opacity: 1; }
th button.sort {
  all: unset;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
th button.sort:hover { color: var(--text); }
th button.sort:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
th[aria-sort] { color: var(--text); }

.sym { font-weight: 600; color: var(--text); }
.name { max-width: 340px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.badge {
  display: inline-flex;
  align-items: center;
  height: 22px;
  padding: 0 8px;
  border-radius: 11px;
  background: var(--surface-2);
  color: var(--text-2);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.flash { animation: flash 1.4s ease-out; }
@keyframes flash { 0% { background: var(--accent-soft); } 100% { background: transparent; } }

.delta { font-weight: 500; }
.delta.up { color: var(--up); }
.delta.down { color: var(--down); }

.pager {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  color: var(--text-2);
  font-size: 13px;
}
.pager .btns { display: flex; gap: 6px; }

.empty {
  padding: 48px 20px;
  text-align: center;
  color: var(--text-2);
}
.empty strong { display: block; color: var(--text); font-size: 15px; margin-bottom: 4px; }

.skeleton { padding: 48px; text-align: center; color: var(--text-3); }

.error-box {
  padding: 14px 16px;
  border: 1px solid color-mix(in srgb, var(--danger) 35%, transparent);
  border-radius: var(--radius-sm);
  background: var(--danger-soft);
  color: var(--danger);
}

/* ---------- Asset detail ---------- */

.asset-title { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.asset-title .name-fa { font-size: 15px; color: var(--text-2); font-weight: 400; }

.stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  overflow: hidden;
}
/* Dividers drawn by each tile, so a partly filled last row leaves no gray gap. */
.stat { padding: 16px 18px; box-shadow: 1px 0 0 var(--border), 0 1px 0 var(--border); }
.stat .label { color: var(--text-3); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.stat .value { margin-top: 4px; font-size: 22px; font-weight: 600; letter-spacing: -.01em; }
.stat .value small { font-size: 13px; font-weight: 500; color: var(--text-3); margin-left: 4px; }
.stat .meta { margin-top: 2px; font-size: 13px; color: var(--text-2); }

.meta-list { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; margin: 0; font-size: 13px; }
.meta-list dt { color: var(--text-3); }
.meta-list dd { margin: 0; }

/* ---------- Chart ---------- */

.chart { position: relative; height: 280px; touch-action: pan-y; }
.chart svg { width: 100%; height: 100%; overflow: visible; }
.chart .grid line { stroke: var(--border); stroke-width: 1; }
.chart .axis text { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .area { opacity: .08; }
.chart .crosshair { stroke: var(--text-3); stroke-width: 1; stroke-dasharray: 3 3; }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 4px; }

.legend { display: flex; gap: 16px; font-size: 13px; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 14px; height: 2px; border-radius: 1px; }

.tooltip {
  position: absolute;
  top: 0;
  z-index: 2;
  min-width: 150px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow);
  font-size: 12.5px;
  pointer-events: none;
}
.tooltip .t-date { color: var(--text-2); margin-bottom: 4px; }
.tooltip .t-row { display: flex; align-items: center; gap: 8px; }
.tooltip .t-row i { width: 12px; height: 2px; border-radius: 1px; flex: none; }
.tooltip .t-row strong { font-variant-numeric: tabular-nums; }
.tooltip .t-row span { color: var(--text-3); margin-left: auto; padding-left: 10px; }

/* ---------- Activity ---------- */

.feed { list-style: none; margin: 0; padding: 0; }
.feed li {
  display: grid;
  grid-template-columns: 76px 120px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  padding: 9px 18px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.feed li:last-child { border-bottom: 0; }
.feed .time { color: var(--text-3); font-variant-numeric: tabular-nums; }
.feed .what { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge.ev-asset { background: var(--accent-soft); color: var(--accent-text); }
.badge.ev-created { background: color-mix(in srgb, var(--good) 14%, transparent); color: var(--up); }

/* ---------- Dialog ---------- */

dialog {
  width: min(480px, calc(100vw - 32px));
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog form { margin: 0; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 0; }
.dialog-head h2 { margin: 0; font-size: 16px; }
.dialog-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 14px; }
.dialog-body p { margin: 0; color: var(--text-2); }
.dialog-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 14px 20px; border-top: 1px solid var(--border); background: var(--surface-2); border-radius: 0 0 12px 12px; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 13px; font-weight: 500; }
.field .hint { font-size: 12px; color: var(--text-3); }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-error { font-size: 13px; }

/* ---------- Toasts ---------- */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: calc(100vw - 32px);
}
.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 240px;
  max-width: 420px;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--good);
  border-radius: var(--radius-sm);
  background: var(--surface);
  box-shadow: var(--shadow-lg);
  animation: toast-in .18s ease-out;
}
.toast.error { border-left-color: var(--critical); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } }

/* ---------- Login ---------- */

.login-body { display: grid; place-items: center; min-height: 100vh; padding: 24px 16px; }
.login { width: min(380px, 100%); display: flex; flex-direction: column; gap: 20px; }
.login-brand { justify-content: center; }
.login-card { display: flex; flex-direction: column; gap: 14px; padding: 24px; }
.login-card h1 { margin: 0; font-size: 20px; }
.login-card .muted { margin: -8px 0 4px; }
.login-submit { height: 38px; margin-top: 4px; }

.check { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--text-2); cursor: pointer; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }

.field select { appearance: auto; }

/* ---------- Signed-in user ---------- */

.user-box {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 6px 0;
  border-top: 1px solid var(--border);
}
.avatar {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
}
.user-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 500; font-size: 13px; }

.code {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  font: 12.5px/1.6 var(--mono);
  white-space: pre;
}
.help { display: flex; flex-direction: column; gap: 10px; color: var(--text-2); }
.help p { margin: 0; }
.help code { padding: 1px 5px; border-radius: 4px; background: var(--surface-2); font: 12.5px var(--mono); }
.input-row { display: flex; gap: 8px; }
.input-row input { flex: 1; min-width: 0; }

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; }
  .sidebar {
    position: sticky;
    z-index: 10;
    height: auto;
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 12px;
    padding: 10px 16px;
    border-right: 0;
    border-bottom: 1px solid var(--border);
  }
  .brand-text small { display: none; }
  .nav { flex-direction: row; order: 3; width: 100%; overflow-x: auto; }
  .nav a { padding: 6px 10px; white-space: nowrap; }
  .sidebar-footer { margin: 0 0 0 auto; flex-direction: row; align-items: center; }
  .conn-label { display: none; }
  .user-box { padding: 0; border: 0; }
  .user-name { display: none; }
  .main { padding: 20px 16px 40px; }
  .field-row { grid-template-columns: 1fr; }
  .feed li { grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; padding: 9px 16px; }
  .feed li .time { grid-column: 1; grid-row: 1; }
  .feed li > span:nth-child(2) { grid-column: 2; grid-row: 1; justify-self: end; }
  .feed li .what { grid-column: 1; grid-row: 2; }
  .feed li .val { grid-column: 2; grid-row: 2; justify-self: end; }
  .hide-sm { display: none; }
  th, td { padding: 10px; }
  .name { max-width: 140px; }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .stat { padding: 12px 14px; }
  .stat .value { font-size: 18px; }
}
