:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #e3e6eb;
  --text: #1c2330;
  --muted: #6b7484;
  --accent: #2563eb;
  --accent-soft: #e8efff;
  --good: #15803d;
  --good-soft: #e7f6ec;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --bad: #c62828;
  --bad-soft: #fdecec;
  --info-soft: #eef2f7;
  --sidebar: #141a24;
  --sidebar-text: #c6ccd6;
  --sidebar-active: #243045;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f131a;
    --surface: #171c25;
    --surface-2: #1c222d;
    --border: #2a3140;
    --text: #e4e8ef;
    --muted: #8b94a5;
    --accent: #5b8cff;
    --accent-soft: #1d2a47;
    --good: #4ade80;
    --good-soft: #13291c;
    --warn: #fbbf24;
    --warn-soft: #2d2412;
    --bad: #f87171;
    --bad-soft: #321718;
    --info-soft: #1f2632;
    --sidebar: #0b0e13;
    --sidebar-text: #a9b1bf;
    --sidebar-active: #1d2636;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 24px; margin: 0 0 16px; font-weight: 650; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h2 { font-size: 15px; margin: 0; font-weight: 600; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
small { font-size: 13px; font-weight: 400; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.lead { margin: -8px 0 18px; max-width: 760px; }
.good { color: var(--good); }
.bad { color: var(--bad); }
.warn-text { color: var(--warn); }

/* Каркас */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 220px; flex: 0 0 220px; background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column; padding: 18px 12px; position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 17px; padding: 4px 10px 18px; }
.brand img { width: 26px; height: 26px; }
.brand:hover { text-decoration: none; }
.brand.big { color: var(--text); font-size: 22px; padding: 0 0 4px; }
.brand.big img { width: 32px; height: 32px; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.sidebar nav a, .sidebar-foot a {
  color: var(--sidebar-text); padding: 8px 12px; border-radius: 8px; font-size: 14px;
}
.sidebar nav a:hover, .sidebar-foot a:hover { background: var(--sidebar-active); text-decoration: none; color: #fff; }
.sidebar a.active { background: var(--sidebar-active); color: #fff; font-weight: 600; }
.sidebar-foot { margin-top: auto; display: flex; flex-direction: column; gap: 2px; border-top: 1px solid #ffffff14; padding-top: 12px; }
.sidebar-foot .link { color: var(--sidebar-text); padding: 8px 12px; font-size: 14px; }
.content { flex: 1; min-width: 0; padding: 28px 32px 48px; max-width: 1280px; }

/* Картки й сітки */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px 18px; box-shadow: var(--shadow); margin-bottom: 16px; min-width: 0;
}
.card.flush { padding: 0; overflow-x: auto; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head.pad { padding: 14px 18px 0; }
.grid { display: grid; gap: 16px; margin-bottom: 16px; }
.grid > .card { margin-bottom: 0; }
.kpis { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.systems { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.label { color: var(--muted); font-size: 13px; }
.value { font-size: 26px; font-weight: 650; margin: 2px 0 8px; font-variant-numeric: tabular-nums; }
.value.sm { font-size: 17px; margin-bottom: 0; }
.value small { color: var(--muted); }
.sub { color: var(--muted); font-size: 13px; }

.meter { display: block; width: 100%; height: 6px; }
.meter .track { fill: var(--border); }
.meter .fill { fill: var(--accent); }
.meter.warn .fill { fill: var(--warn); }
.meter.over .fill { fill: var(--bad); }

.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); display: inline-block; flex: none; }
.dot.ok { background: var(--good); box-shadow: 0 0 0 3px var(--good-soft); }
.dot.bad { background: var(--bad); box-shadow: 0 0 0 3px var(--bad-soft); }
.dot.off { background: var(--border); }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 14px; margin: 0; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* Графік */
.chart { width: 100%; height: 140px; display: block; }
.chart .bar { fill: var(--accent); }
.chart .bar:hover { fill: var(--warn); }
.chart .axis { stroke: var(--border); stroke-width: .4; vector-effect: non-scaling-stroke; }
.chart-x { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 4px; }

/* Таблиці */
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
.card:not(.flush) th:first-child, .card:not(.flush) td:first-child { padding-left: 0; }
.card:not(.flush) th:last-child, .card:not(.flush) td:last-child { padding-right: 0; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover td { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.nowrap { white-space: nowrap; }
.clip { max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wrap { overflow-wrap: anywhere; }
.empty { text-align: center; color: var(--muted); padding: 22px; }
table.compact td { padding: 7px 8px; }

.badge { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 500; white-space: nowrap; background: var(--info-soft); color: var(--muted); }
.st-done { background: var(--good-soft); color: var(--good); }
.st-error { background: var(--bad-soft); color: var(--bad); }
.st-blocked { background: var(--warn-soft); color: var(--warn); }
.st-running { background: var(--accent-soft); color: var(--accent); }
.rt-local { background: var(--good-soft); color: var(--good); }
.rt-cloud, .rt-local-cloud { background: var(--accent-soft); color: var(--accent); }

/* Повідомлення */
.flash { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; border: 1px solid transparent; }
.flash.ok { background: var(--good-soft); color: var(--good); }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.flash.info { background: var(--info-soft); color: var(--text); }

/* Форми */
.form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 500; margin-bottom: 14px; }
.form .field { min-width: 0; }
.form .field label { margin-bottom: 6px; }
label.check { flex-direction: row !important; align-items: center; gap: 8px !important; font-weight: 400 !important; }
label.check.small { font-size: 13px; color: var(--muted); }
.hint { color: var(--muted); font-size: 12px; font-weight: 400; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0;
}
input[type=checkbox] { width: 16px; height: 16px; accent-color: var(--accent); flex: none; }
textarea { resize: vertical; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 13px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent-soft); border-color: var(--accent); }
.row { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 16px; }
.row.three { grid-template-columns: repeat(3, minmax(0, 1fr)); }
details { margin-bottom: 14px; }
summary { cursor: pointer; color: var(--accent); font-size: 14px; margin-bottom: 8px; }
.narrow { max-width: 460px; }
.actions { display: flex; justify-content: flex-end; gap: 10px; flex-wrap: wrap; }
.actions.left { justify-content: flex-start; }
.actions.sticky { position: sticky; bottom: 0; padding: 12px 0; background: linear-gradient(transparent, var(--bg) 35%); }
.filters { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; }
.filters select { width: auto; }
.pager { display: flex; align-items: center; justify-content: center; gap: 14px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-size: 14px; font-weight: 500; padding: 8px 14px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn:disabled { opacity: .7; cursor: progress; }
.btn.wide { width: 100%; }
button.link { background: none; border: 0; cursor: pointer; font: inherit; text-align: left; width: 100%; border-radius: 8px; }
button.link:hover { background: var(--sidebar-active); color: #fff; }

pre.text {
  margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 8px; padding: 12px 14px; font: 13px/1.55 ui-monospace, "Cascadia Mono", Consolas, monospace; max-height: 520px; overflow: auto;
}

/* Агенти */
.agents { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 16px; }
.agents .card { margin: 0; display: flex; flex-direction: column; gap: 10px; }
.agents .card p { margin: 0; }
.agents .card .btn { align-self: flex-start; margin-top: auto; }
.agent.is-off { opacity: .85; }
.switch {
  display: inline-flex; align-items: center; gap: 8px; border: 0; background: none; cursor: pointer;
  font: inherit; font-size: 13px; color: var(--muted); padding: 0;
}
.switch span { width: 34px; height: 20px; border-radius: 999px; background: var(--border); position: relative; transition: background .15s; }
.switch span::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: left .15s; }
.switch.on { color: var(--good); }
.switch.on span { background: var(--good); }
.switch.on span::after { left: 16px; }

/* Вхід */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login { width: 100%; max-width: 360px; padding: 28px; }
.login p { margin: 0 0 18px; }
.login label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; font-weight: 500; margin-bottom: 14px; }

/* Мобільні */
@media (max-width: 1000px) {
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .systems, .two { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; flex: none; height: auto; position: static; padding: 10px 12px; }
  .brand { padding-bottom: 8px; }
  .sidebar nav { flex-direction: row; overflow-x: auto; }
  .sidebar nav a { white-space: nowrap; }
  .sidebar-foot { flex-direction: row; justify-content: space-between; margin-top: 6px; padding-top: 6px; }
  .sidebar-foot .link { width: auto; }
  .content { padding: 18px 16px 40px; }
  .row, .row.three { grid-template-columns: 1fr; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .value { font-size: 21px; }
  .clip { max-width: 160px; }
}
