:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --critical: #d03b3b;
  --good: #0ca30c;
  --good-text: #006300;
  --warn-bg: #fff4d6;
  --grid: #e8e7e3;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 4px 16px rgba(0, 0, 0, .06);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211;
    --surface: #1a1a19;
    --surface-2: #242422;
    --border: #33332f;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #8f8e86;
    --accent: #3987e5;
    --series-1: #3987e5;
    --critical: #e66767;
    --good: #0ca30c;
    --good-text: #4cc94c;
    --warn-bg: #3a3014;
    --grid: #2a2a27;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
  --text: #ffffff; --text-2: #c3c2b7; --muted: #8f8e86; --accent: #3987e5; --series-1: #3987e5;
  --critical: #e66767; --good: #0ca30c; --good-text: #4cc94c; --warn-bg: #3a3014; --grid: #2a2a27;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--critical); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; }
h1 { font-size: 22px; margin: 0 0 16px; }
h2 { font-size: 16px; margin: 0 0 12px; }
a { color: var(--accent); }

button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  min-width: 0;
}
textarea { width: 100%; resize: vertical; }
textarea.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; min-height: 140px; }
textarea.prompt { min-height: 320px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover { background: var(--surface-2); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
button.primary:hover { filter: brightness(1.05); }
button.danger { color: var(--critical); }
button.link { border: 0; background: none; color: var(--accent); padding: 0; }
button:disabled { opacity: .5; cursor: default; }

/* ---------------------------------------------------------------- вход --- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: 100%; max-width: 340px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 24px; box-shadow: var(--shadow); display: grid; gap: 12px;
}
.login-card h1 { margin: 0; }
.login-card p { margin: 0; }
.login-card label { display: grid; gap: 4px; color: var(--text-2); }

/* -------------------------------------------------------------- каркас --- */
.shell { display: grid; grid-template-columns: 200px 1fr; min-height: 100vh; }
.nav {
  background: var(--surface); border-right: 1px solid var(--border);
  padding: 16px 12px; display: flex; flex-direction: column; gap: 2px;
  position: sticky; top: 0; height: 100vh;
}
.brand { font-weight: 600; padding: 4px 8px 16px; display: grid; }
.brand .muted { font-weight: 400; font-size: 12px; }
.nav a { padding: 7px 10px; border-radius: 6px; color: var(--text-2); text-decoration: none; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--surface-2); color: var(--text); font-weight: 600; }
.nav-foot { margin-top: auto; padding: 8px; display: grid; gap: 4px; font-size: 12px; justify-items: start; }
.view { padding: 24px; min-width: 0; max-width: 1280px; }

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .nav { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; border-right: 0; border-bottom: 1px solid var(--border); padding: 8px 16px; }
  .brand { padding: 4px 8px; width: 100%; }
  .nav-foot { margin: 0 0 0 auto; padding: 4px; grid-auto-flow: column; gap: 12px; }
  .view { padding: 16px; }
}

/* -------------------------------------------------------------- блоки --- */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 16px; margin-bottom: 16px; min-width: 0;
}
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 16px; }
.head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.head h1 { margin: 0; }

.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 14px 16px; }
.tile .label { color: var(--text-2); font-size: 12px; }
.tile .value { font-size: 26px; font-weight: 600; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .sub { color: var(--muted); font-size: 12px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { color: var(--text-2); font-weight: 500; font-size: 12px; white-space: nowrap; }
tbody tr:last-child td { border-bottom: 0; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
td.err { color: var(--critical); max-width: 380px; overflow-wrap: anywhere; font-size: 12px; }

.badge {
  display: inline-flex; align-items: center; gap: 4px; font-size: 12px;
  padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); white-space: nowrap;
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--muted); }
.badge.on { color: var(--good-text); }
.badge.on::before { background: var(--good); }
.badge.off { color: var(--critical); }
.badge.off::before { background: var(--critical); }
.badge.plain::before { display: none; }

.form { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; }
.form label, .field { display: grid; gap: 4px; color: var(--text-2); font-size: 12px; }
.form label input, .form label select, .form label textarea { font-size: 14px; color: var(--text); }
.form .wide { grid-column: 1 / -1; }
.form label.check { display: flex; gap: 8px; align-items: flex-start; font-size: 14px; color: var(--text); }
.form label.check input { margin-top: 3px; }
.actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; margin-top: 12px; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0 0; }
.warn { background: var(--warn-bg); border-radius: 8px; padding: 10px 12px; margin-bottom: 12px; }

.keycard { border: 1px solid var(--border); border-radius: 8px; padding: 12px; margin-bottom: 12px; }
.keycard.disabled { opacity: .75; }
.keycard .form { grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); }

/* ---------------------------------------------------------------- график --- */
.chart-box { width: 100%; min-height: 180px; }
.chart { display: block; max-width: 100%; }
.chart .grid line { stroke: var(--grid); }
.chart .axis text { fill: var(--muted); font-size: 11px; }
.chart .bar { fill: var(--series-1); }
.chart .bar.crit { fill: var(--critical); }
.chart .hit { fill: transparent; }
.chart .hit:hover + .bar, .chart g.col:hover .bar { filter: brightness(1.15); }
.tip {
  position: fixed; pointer-events: none; z-index: 30; background: var(--surface); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px; font-size: 12px; box-shadow: var(--shadow);
}

/* ---------------------------------------------------------------- модалка --- */
.modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-card {
  background: var(--surface); border-radius: 12px; padding: 20px; width: 100%; max-width: 560px;
  max-height: calc(100vh - 32px); overflow: auto; box-shadow: var(--shadow);
}
.keybox {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 18px; letter-spacing: .5px;
  background: var(--surface-2); border-radius: 8px; padding: 12px; text-align: center; user-select: all;
  overflow-wrap: anywhere;
}
.toast {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 40;
  background: var(--text); color: var(--surface); padding: 8px 14px; border-radius: 8px; font-size: 13px;
}
.toast.bad { background: var(--critical); color: #fff; }
