:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #e3e6eb;
  --text: #1b1f24;
  --muted: #69717d;
  --accent: #2f6fde;
  --accent-soft: #e6eefc;
  --ok: #1f8a4c;
  --warn: #b26a00;
  --warn-soft: #fff4e0;
  --bad: #c0362c;
  --bad-soft: #fdecea;
  --test: #7a3fd1;
  --test-soft: #f1eafc;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --panel: #181c21;
    --line: #2a3038;
    --text: #e4e7eb;
    --muted: #8b939e;
    --accent: #6c9dff;
    --accent-soft: #1d2a42;
    --ok: #4cc283;
    --warn: #e0a243;
    --warn-soft: #33270f;
    --bad: #ff7a6e;
    --bad-soft: #3a1c1a;
    --test: #b48cff;
    --test-soft: #2a2140;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex;
  flex-direction: column;
}
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button {
  border: 1px solid var(--line);
  background: var(--panel);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.ghost { background: transparent; }
button.danger { color: var(--bad); }
button.icon { padding: 2px 8px; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: 12px; }

.top {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 5;
}
.brand { font-weight: 700; font-size: 16px; }
.status { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.dirty { color: var(--warn); }
.actions { display: flex; gap: 8px; }

.layout { flex: 1; display: grid; grid-template-columns: 340px minmax(0, 1fr); min-height: 0; }
.hosts {
  border-right: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: calc(100vh - 53px);
  position: sticky;
  top: 53px;
}
.hosts-head { padding: 12px; border-bottom: 1px solid var(--line); display: grid; gap: 8px; }
.hint { margin: 0; font-size: 12px; color: var(--muted); }
.hint.active { color: var(--accent); }
.seg { display: flex; }
.seg button { border-radius: 0; flex: 1; padding: 4px 6px; font-size: 12px; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button + button { margin-left: -1px; }
.seg button.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); position: relative; }

.host-list { list-style: none; margin: 0; padding: 4px 0; overflow: auto; flex: 1; }
.host {
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
  display: grid;
  gap: 2px;
}
.host:hover { background: var(--accent-soft); }
.host.bad .host-name { color: var(--muted); text-decoration: line-through; }
.host-name { font-weight: 500; word-break: break-word; }
.host-meta { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; font-size: 12px; color: var(--muted); }

.badge {
  display: inline-block;
  font-size: 11px;
  padding: 0 6px;
  border-radius: 10px;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.badge.bad { color: var(--bad); background: var(--bad-soft); border-color: transparent; }
.badge.warn { color: var(--warn); background: var(--warn-soft); border-color: transparent; }
.badge.grp { color: var(--accent); background: var(--accent-soft); border-color: transparent; }
.badge.fb { color: var(--test); background: var(--test-soft); border-color: transparent; }

.main { padding: 0 20px 40px; min-width: 0; }
.tabs {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  padding: 12px 0;
  position: sticky;
  top: 53px;
  background: var(--bg);
  z-index: 4;
}
.tabs button { border-radius: 16px; }
.tabs button.on { background: var(--text); color: var(--bg); border-color: var(--text); }
.tabs button.test { border-style: dashed; }
.tabs button.test.on { background: var(--test); border-color: var(--test); color: #fff; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 12px;
}
.card.variant-head { border-color: var(--test); background: var(--test-soft); }
.banner { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 12px; font-size: 13px; }
.banner.warn { background: var(--warn-soft); color: var(--warn); }
.banner.info { background: var(--accent-soft); color: var(--accent); }

.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row + .row { margin-top: 8px; }
.grow { flex: 1; }
label.field { display: grid; gap: 4px; font-size: 12px; color: var(--muted); }
label.field input, label.field select { color: var(--text); font-size: 14px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px; }

.group-title { font-weight: 600; font-size: 15px; flex: 1; }
.tier {
  display: grid;
  grid-template-columns: 64px 1fr auto;
  gap: 8px;
  align-items: start;
  padding: 6px;
  border-radius: 6px;
  border: 1px solid transparent;
  cursor: pointer;
}
.tier:hover { background: var(--bg); }
.tier.active { border-color: var(--accent); background: var(--accent-soft); }
.tier-label { font-size: 12px; color: var(--muted); padding-top: 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 2px 4px 2px 10px;
  font-size: 13px;
}
.chip button { border: none; background: none; padding: 0 4px; color: var(--muted); line-height: 1; }
.chip button:hover { color: var(--bad); }
.chip.user .st { font-size: 11px; }
.chip input { border: none; padding: 2px 4px; width: 160px; background: transparent; }
.chip-add { width: 180px; max-width: 100%; padding: 3px 8px; font-size: 13px; border-radius: 14px; }

.members { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line); font-size: 13px; }
.members .tierline { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 4px; align-items: baseline; }
.members .t { color: var(--muted); font-size: 12px; min-width: 48px; }
.members .n { white-space: nowrap; }
.members .n.off { color: var(--muted); text-decoration: line-through; }
.members .empty { color: var(--bad); }

details.adv summary { cursor: pointer; color: var(--muted); font-size: 12px; margin-top: 8px; }
details.adv[open] summary { margin-bottom: 8px; }

.toggle { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; cursor: pointer; }

.toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: var(--radius);
  max-width: min(640px, calc(100vw - 32px));
  z-index: 20;
  white-space: pre-wrap;
}
.toast.err { background: var(--bad); color: #fff; }

dialog {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  max-width: min(560px, calc(100vw - 32px));
  width: 100%;
}
dialog::backdrop { background: rgba(0, 0, 0, .4); }
dialog h3 { margin: 0 0 10px; }
.confirm-body { margin-bottom: 14px; font-size: 13px; }
.confirm-body ul { margin: 4px 0; padding-left: 18px; }

@media (max-width: 860px) {
  body { display: block; }
  .layout { display: block; }
  .hosts { position: static; height: 45vh; max-height: none; border-right: none; border-bottom: 1px solid var(--line); }
  .top { flex-wrap: wrap; position: static; }
  .actions { flex-wrap: wrap; }
  .status { order: 3; flex-basis: 100%; }
  .main { padding: 0 16px 40px; }
  .tabs { top: 0; position: static; }
  .tier { grid-template-columns: 1fr auto; }
  .tier-label { grid-column: 1 / -1; }
}

/* ---- сайдбар: разделы, кластеры, drag-n-drop ---- */
.host-tools { display: grid; gap: 6px; }
.host-tools select { font-size: 13px; padding: 4px 6px; }
.row.tight { gap: 6px; flex-wrap: nowrap; }
.row.tight + .row.tight { margin-top: 0; }
.small { font-size: 12px; }
.banner.small { padding: 6px 10px; margin: 0; font-size: 12px; }
button.link { border: none; background: none; padding: 0; color: var(--accent); text-decoration: underline; font-size: inherit; }
button.ghost.small { padding: 3px 10px; font-size: 12px; }
.host[draggable="true"] { cursor: grab; }
.host.cluster { background: var(--bg); }
.host.cluster:hover { background: var(--accent-soft); }
.host.nested { padding-left: 34px; }
.caret { padding: 0 6px; font-size: 12px; line-height: 1.6; }
.tier.drop { border-color: var(--accent); border-style: dashed; background: var(--accent-soft); }
.chip.zero { border-color: var(--bad); color: var(--bad); }
.chip .cnt {
  font-size: 11px;
  background: var(--accent-soft);
  color: var(--accent);
  border-radius: 8px;
  padding: 0 5px;
}
.chip .cnt.none { background: var(--bad-soft); color: var(--bad); }
.card.collapsed { padding: 8px 14px; }
.group-title.static { cursor: pointer; flex: 0 1 auto; font-weight: 600; }
.members.inline { margin: 0 0 0 auto; padding: 0; border: none; }
.card.flash { outline: 2px solid var(--bad); outline-offset: 2px; transition: outline-color .3s; }

/* ---- директ ---- */
.dir-grid { display: grid; grid-template-columns: minmax(0, 3fr) minmax(0, 2fr); gap: 12px; }
.dir-grid .card { margin-bottom: 0; display: flex; flex-direction: column; gap: 6px; }
.dir-area {
  width: 100%;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.5;
  resize: vertical;
  flex: 1;
}
.dir-err {
  background: var(--bad-soft);
  color: var(--bad);
  border-radius: var(--radius);
  padding: 10px 14px;
  margin: 12px 0 0;
  white-space: pre-wrap;
  font-family: var(--mono);
  font-size: 12px;
}
.dir-err.hidden { display: none; }
@media (max-width: 860px) { .dir-grid { grid-template-columns: 1fr; } }

/* ---- вход ---- */
[hidden] { display: none !important; }
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card {
  width: min(400px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 24px;
}
.auth-card h2 { margin: 12px 0 16px; font-size: 18px; }
.auth-form { display: grid; gap: 12px; }
.auth-form.inline { max-width: 360px; margin-top: 10px; }
.auth-err { color: var(--bad); font-size: 13px; min-height: 1em; }
.qr { display: block; margin: 12px auto; image-rendering: pixelated; background: #fff; padding: 8px; border-radius: 8px; }
.secret { text-align: center; margin-bottom: 12px; word-break: break-all; }
.secret code { font-family: var(--mono); font-size: 13px; }
.whoami { display: inline-flex; gap: 8px; align-items: center; margin-left: 8px; font-size: 13px; }
.user-row { padding: 6px 0; border-top: 1px solid var(--line); }
pre.mono { background: var(--bg); padding: 8px; border-radius: 6px; white-space: pre-wrap; }

.ok-line { color: var(--ok); }
.auth-card .auth-form input { font-size: 15px; padding: 9px 10px; }
.auth-card .auth-form button.primary { font-size: 16px; padding: 8px; }
