:root {
  --bg: #f4f7f7;
  --surface: #ffffff;
  --surface-2: #eef3f3;
  --text: #17272b;
  --muted: #566a70;
  --line: #d9e2e4;
  --primary: #0f766e;
  --primary-hover: #0b5d57;
  --on-primary: #ffffff;
  --danger: #b42318;
  --danger-bg: #fdecea;
  --ok: #067647;
  --ok-bg: #e5f5ec;
  --warn: #8a5a00;
  --warn-bg: #fff4d6;
  --focus: #0b6bcb;
  --shadow: 0 1px 2px rgba(20, 40, 45, 0.06), 0 6px 20px rgba(20, 40, 45, 0.06);
  --radius: 10px;
  --sidebar: 248px;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1517;
    --surface: #162125;
    --surface-2: #1c2b30;
    --text: #e5eff1;
    --muted: #9db2b8;
    --line: #2a3b41;
    --primary: #2dd4bf;
    --primary-hover: #5eead4;
    --on-primary: #04211e;
    --danger: #ff8a7d;
    --danger-bg: #3a1c19;
    --ok: #5fd39a;
    --ok-bg: #12301f;
    --warn: #f2c35b;
    --warn-bg: #33290f;
    --focus: #7ab8ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.3);
  }
}

* { 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, "Noto Sans", sans-serif;
  min-height: 100vh;
}
h1, h2, h3 { line-height: 1.25; margin: 0 0 12px; }
h1 { font-size: 22px; }
h2 { font-size: 17px; }
h3 { font-size: 15px; }
p { margin: 0 0 12px; }
a { color: var(--primary); }
code, .mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 13px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
[hidden] { display: none !important; }

/* Иконки */
.icon { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Каркас */
.app { display: grid; grid-template-columns: var(--sidebar) minmax(0, 1fr); min-height: 100vh; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 16px; font-weight: 650; font-size: 16px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--primary); color: var(--on-primary); display: grid; place-items: center; }
.nav { display: flex; flex-direction: column; gap: 2px; }
.nav a {
  display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 8px;
  color: var(--text); text-decoration: none;
}
.nav a:hover { background: var(--surface-2); }
.nav a[aria-current="page"] { background: var(--surface-2); color: var(--primary); font-weight: 600; }
.nav-title { margin: 14px 10px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
.sidebar-foot { margin-top: auto; padding-top: 16px; border-top: 1px solid var(--line); font-size: 13px; color: var(--muted); }
.sidebar-foot .who { color: var(--text); font-weight: 600; overflow-wrap: anywhere; }
.main { padding: 24px 24px 48px; min-width: 0; max-width: 1180px; width: 100%; }
.topbar { display: none; }
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head p { color: var(--muted); margin: 0; max-width: 62ch; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.toolbar { margin-bottom: 12px; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; justify-content: space-between; }

@media (max-width: 860px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .sidebar { position: fixed; inset: 0 auto 0 0; width: min(82vw, 300px); z-index: 20; transform: translateX(-102%); transition: transform 0.18s ease; box-shadow: var(--shadow); }
  .sidebar.open { transform: none; }
  .scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 10; }
  .topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; font-weight: 650; }
  .main { padding: 16px 16px 40px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation-duration: 0.01ms !important; } }

/* Карточки и таблицы */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow); margin-bottom: 16px; }
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
table { border-collapse: collapse; width: 100%; min-width: 560px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); background: var(--surface-2); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; }
.cell-actions { white-space: nowrap; text-align: right; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

/* Бейджи */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--danger-bg); color: var(--danger); }

/* Кнопки */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 38px; padding: 7px 14px; border-radius: 8px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  position: relative;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--on-primary); }
.btn.primary:hover { background: var(--primary-hover); border-color: var(--primary-hover); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.danger:hover { background: var(--danger-bg); }
.btn.small { min-height: 30px; padding: 3px 10px; font-size: 13px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn[disabled], .btn[aria-busy="true"] { opacity: 0.65; cursor: default; pointer-events: none; }
.btn[aria-busy="true"]::after {
  content: ""; width: 14px; height: 14px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Формы */
.form { display: grid; gap: 14px; }
.form.cols { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.field { display: grid; gap: 4px; min-width: 0; }
.field > label, .field > .label { font-weight: 600; font-size: 14px; }
.field .req { color: var(--danger); }
.hint { color: var(--muted); font-size: 13px; }
input, select, textarea {
  width: 100%; min-height: 40px; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--text); font: inherit;
}
input:disabled { background: var(--surface-2); }
input[aria-invalid="true"] { border-color: var(--danger); }
.input-row { display: flex; gap: 6px; }
.input-row input { flex: 1; min-width: 0; }
.error-line { color: var(--danger); background: var(--danger-bg); border-radius: 8px; padding: 8px 10px; font-size: 14px; }
.note { background: var(--surface-2); border-radius: 8px; padding: 10px 12px; font-size: 14px; }
.note.warn { background: var(--warn-bg); color: var(--warn); }
.checks { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.checks label { display: inline-flex; align-items: center; gap: 6px; font-weight: 400; }
.checks input { width: auto; min-height: 0; }

/* Блок с копируемым значением */
.copybox { display: flex; gap: 8px; align-items: stretch; background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 6px 6px 10px; }
.copybox code { flex: 1; min-width: 0; overflow-wrap: anywhere; align-self: center; user-select: all; }

/* Вход */
.auth-wrap { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth-card { width: min(100%, 400px); background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 24px; box-shadow: var(--shadow); }
.auth-card .brand { padding: 0 0 16px; }

/* Диалог */
dialog { border: 1px solid var(--line); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text); width: min(560px, calc(100vw - 32px)); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0, 0, 0, 0.5); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.dlg-head h2 { margin: 0; }
.dlg-body { padding: 16px; max-height: min(70vh, 640px); overflow-y: auto; }
.dlg-foot { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; padding: 12px 16px; border-top: 1px solid var(--line); }

/* Тосты */
.toasts { position: fixed; right: 16px; bottom: 16px; display: grid; gap: 8px; z-index: 50; max-width: min(380px, calc(100vw - 32px)); }
.toast { background: var(--text); color: var(--bg); border-radius: 10px; padding: 10px 14px; box-shadow: var(--shadow); display: flex; gap: 8px; align-items: flex-start; }
.toast.bad { background: var(--danger); color: #fff; }
@media (prefers-color-scheme: dark) { .toast.bad { color: #2a0b07; } }

/* Вкладки */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 14px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer; white-space: nowrap; }
.tabs button[aria-selected="true"] { color: var(--primary); border-bottom-color: var(--primary); }

.kv { display: grid; grid-template-columns: max-content 1fr; gap: 6px 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.json { white-space: pre-wrap; overflow-wrap: anywhere; background: var(--surface-2); border-radius: 8px; padding: 10px; max-height: 320px; overflow: auto; }
.loading { padding: 32px; color: var(--muted); text-align: center; }
