:root {
  --bg: #f3f2f1;
  --panel: #ffffff;
  --ink: #252423;
  --ink-2: #605e5c;
  --ink-3: #8a8886;
  --line: #e1dfdd;
  --line-2: #edebe9;
  --accent: #6b4fbb;
  --accent-soft: #f7f3ff;
  --accent-ink: #4b3591;
  --ok: #107c10;
  --err: #c4314b;
  --warn: #986f0b;
  --red: #d64550;
  --sidebar: #faf9f8;
  --radius: 6px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 6px rgba(0,0,0,.05);
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  font-size: 13px;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent-ink); }

/* ---------- верхняя панель ---------- */
.topbar {
  height: 42px; display: flex; align-items: center; gap: 16px;
  padding: 0 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.topbar .brand { font-weight: 600; font-size: 15px; white-space: nowrap; }
.topbar .brand small { color: var(--ink-3); font-weight: 400; margin-left: 6px; }
.topbar nav { display: flex; gap: 2px; }
.topbar nav a {
  padding: 6px 12px; border-radius: var(--radius); text-decoration: none; color: var(--ink-2);
}
.topbar nav a.active { background: var(--accent-soft); color: var(--accent-ink); font-weight: 600; }
.topbar nav a:hover { background: var(--line-2); }
.topbar .spacer { flex: 1; }
.topbar .actuality { color: var(--ink-2); white-space: nowrap; font-size: 12px; }
.topbar .actuality b { color: var(--ink); font-weight: 600; }
.linkbtn { background: none; border: 0; color: var(--ink-2); cursor: pointer; padding: 6px 8px; border-radius: var(--radius); }
.linkbtn:hover { background: var(--line-2); }

/* ---------- раскладка дашборда ---------- */
.layout { display: grid; grid-template-columns: 268px 1fr; min-height: calc(100vh - 42px); }
.sidebar {
  background: var(--sidebar); border-right: 1px solid var(--line); padding: 14px 14px 24px;
  display: flex; flex-direction: column; gap: 16px; position: sticky; top: 42px;
  height: calc(100vh - 42px); overflow: auto;
}
.slicer h3 { margin: 0 0 6px; font-size: 12px; font-weight: 600; color: var(--ink-2); text-transform: none; }
.slicer .dates { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.slicer input[type=date], .slicer input[type=search], .slicer select {
  width: 100%; padding: 5px 7px; border: 1px solid var(--line); border-radius: 4px; background: #fff;
}
.checklist { border: 1px solid var(--line); border-radius: 4px; background: #fff; max-height: 330px; overflow: auto; }
.checklist.short { max-height: none; }
.checklist label {
  display: flex; gap: 7px; align-items: flex-start; padding: 4px 8px; cursor: pointer; line-height: 1.3;
}
.checklist label:hover { background: var(--accent-soft); }
.checklist input { margin-top: 2px; accent-color: var(--accent); }
.checklist .empty { padding: 8px; color: var(--ink-3); }
.slicer .search { margin-bottom: 6px; }
.slicer .hint { color: var(--ink-3); font-size: 11px; margin-top: 4px; }
.sidebar .actions { display: flex; gap: 6px; margin-top: auto; }

.btn {
  border: 1px solid var(--line); background: #fff; border-radius: 4px; padding: 5px 10px; cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--line-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-ink); }
.btn:disabled { opacity: .5; cursor: default; }

.main { padding: 14px 16px 16px; min-width: 0; display: flex; flex-direction: column; gap: 12px; }

.cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
.card {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 16px;
  min-height: 84px; display: flex; flex-direction: column; justify-content: center;
}
.card .value { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; }
.card .label { color: var(--ink-2); font-size: 12px; margin-top: 2px; }

/* ---------- матрица ---------- */
.matrix-wrap {
  background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; min-height: 0; flex: 1;
}
.matrix-toolbar { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--line-2); }
.matrix-toolbar .status { color: var(--ink-3); margin-left: auto; font-size: 12px; }
.matrix-scroll { overflow: auto; max-height: calc(100vh - 250px); }
table.matrix { border-collapse: separate; border-spacing: 0; width: max-content; min-width: 100%; }
table.matrix th, table.matrix td {
  padding: 4px 8px; border-bottom: 2px solid #fff; white-space: nowrap; font-variant-numeric: tabular-nums;
}
table.matrix thead th {
  position: sticky; top: 0; z-index: 3; background: var(--accent-soft); font-weight: 600; font-size: 12px;
  text-align: center; white-space: normal; min-width: 88px; max-width: 130px; line-height: 1.2;
  cursor: pointer; user-select: none; border-bottom: 1px solid var(--line);
}
table.matrix thead th .sort { color: var(--accent); font-size: 10px; margin-left: 2px; }
table.matrix thead th:first-child { left: 0; z-index: 4; text-align: left; min-width: 320px; max-width: 420px; }
table.matrix td { text-align: right; color: #595858; position: relative; }
table.matrix td.rowhead {
  position: sticky; left: 0; background: #fff; z-index: 2; text-align: left; color: var(--ink);
  max-width: 420px; overflow: hidden; text-overflow: ellipsis;
}
table.matrix tr:hover td { background-color: #faf8ff; }
table.matrix tr:hover td.rowhead { background: #faf8ff; }
table.matrix tr.lvl1 td.rowhead { font-weight: 600; }
table.matrix tr.total td { font-weight: 700; color: #4b3591; background: #f7f3ff; border-top: 1px solid var(--line); position: sticky; bottom: 0; z-index: 2; }
table.matrix tr.total td.rowhead { z-index: 3; background: #f7f3ff; }
.toggle {
  display: inline-grid; place-items: center; width: 16px; height: 16px; margin-right: 6px; border: 1px solid #a19f9d;
  border-radius: 2px; font-size: 12px; line-height: 1; cursor: pointer; color: var(--ink-2); background: #fff;
  vertical-align: -3px;
}
.toggle.leaf { visibility: hidden; }
td .bar { position: absolute; top: 3px; bottom: 3px; left: 4px; border-radius: 2px; z-index: 0; }
td .v { position: relative; z-index: 1; }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--red); margin-right: 5px; vertical-align: 0; }
.blank-label { color: var(--ink-3); font-style: italic; }
.path-link { color: inherit; text-decoration: none; }
.path-link:hover { text-decoration: underline; }

.footer-actions { display: flex; justify-content: flex-end; }

.loading { opacity: .55; pointer-events: none; transition: opacity .15s; }
.error-box { background: #fde7e9; color: #a4262c; border-radius: var(--radius); padding: 10px 12px; white-space: pre-wrap; }

/* ---------- логин ---------- */
.login-page { display: grid; place-items: center; min-height: 100vh; }
.login {
  background: var(--panel); padding: 28px 28px 24px; border-radius: 10px; box-shadow: var(--shadow); width: 340px;
  display: flex; flex-direction: column; gap: 12px;
}
.login h1 { margin: 0; font-size: 18px; }
.login p { margin: 0; color: var(--ink-2); }
.login input { padding: 8px 10px; border: 1px solid var(--line); border-radius: 4px; }
.login .error { color: var(--err); min-height: 1em; }

/* ---------- консоль ---------- */
.console { padding: 14px 16px; display: flex; flex-direction: column; gap: 14px; max-width: 1600px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); }
.tabs button {
  border: 0; background: none; padding: 8px 14px; cursor: pointer; color: var(--ink-2);
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button.active { color: var(--accent-ink); border-bottom-color: var(--accent); font-weight: 600; }
.panel { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }
.panel h2 { margin: 0 0 10px; font-size: 14px; }
.panel .sub { color: var(--ink-2); margin: -4px 0 10px; }
.grid-conn { display: grid; grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 10px; }
.conn { border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; display: flex; flex-direction: column; gap: 4px; }
.conn .title { font-weight: 600; }
.conn .target { font-family: var(--mono); font-size: 11px; color: var(--ink-2); word-break: break-all; }
.conn .state { font-size: 12px; }
.badge { display: inline-block; padding: 1px 8px; border-radius: 10px; font-size: 11px; font-weight: 600; }
.badge.ok { background: #dff6dd; color: var(--ok); }
.badge.err { background: #fde7e9; color: var(--err); }
.badge.off { background: var(--line-2); color: var(--ink-2); }
.badge.run { background: #fff4ce; color: var(--warn); }
.editor-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; }
.editor-row select { padding: 5px 7px; border: 1px solid var(--line); border-radius: 4px; background: #fff; min-width: 280px; }
textarea.code {
  width: 100%; min-height: 160px; font-family: var(--mono); font-size: 12.5px; padding: 10px; resize: vertical;
  border: 1px solid var(--line); border-radius: 4px; background: #fcfcfc; line-height: 1.45; tab-size: 2;
}
input.code { width: 100%; font-family: var(--mono); font-size: 12.5px; padding: 7px 9px; border: 1px solid var(--line); border-radius: 4px; }
.result-meta { color: var(--ink-2); margin: 8px 0; font-size: 12px; }
.result { overflow: auto; max-height: 60vh; border: 1px solid var(--line); border-radius: 4px; }
table.grid { border-collapse: collapse; font-size: 12px; font-family: var(--mono); }
table.grid th, table.grid td { border-bottom: 1px solid var(--line-2); border-right: 1px solid var(--line-2); padding: 3px 8px; text-align: left; white-space: nowrap; max-width: 520px; overflow: hidden; text-overflow: ellipsis; }
table.grid th { position: sticky; top: 0; background: #f8f8f8; font-weight: 600; }
table.grid td.null { color: var(--ink-3); font-style: italic; }
table.grid td.num { text-align: right; }
table.grid tr.bad td { background: #fff5f5; }
pre.json { margin: 0; padding: 10px; font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-word; }
.kbd { font-family: var(--mono); font-size: 11px; background: var(--line-2); border-radius: 3px; padding: 1px 4px; }
.row-flex { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.muted { color: var(--ink-3); }
details.log summary { cursor: pointer; }
details.log pre { max-height: 300px; overflow: auto; background: #fbfbfb; border: 1px solid var(--line-2); padding: 8px; font-family: var(--mono); font-size: 11.5px; }

@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .cards { grid-template-columns: 1fr; }
  .topbar .actuality { display: none; }
  .matrix-scroll { max-height: none; }
}
