:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --surface-2: #f0f3f7;
    --border: #d9dee6;
    --text: #1d2430;
    --muted: #667085;
    --accent: #1f6feb;
    --accent-contrast: #ffffff;
    --ok: #1a7f37;
    --ok-bg: #e6f4ea;
    --warn: #b42318;
    --warn-bg: #fdecea;
    --caution: #c4560a;
    --neutral-bg: #eceff3;
    --chart-bar: #3b82f6;
    --chart-line: #0f766e;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
    color-scheme: light;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0f1419;
        --surface: #171d24;
        --surface-2: #1e252e;
        --border: #2d3640;
        --text: #e6edf3;
        --muted: #8b98a5;
        --accent: #4c9aff;
        --accent-contrast: #0f1419;
        --ok: #4ac26b;
        --ok-bg: #12261a;
        --warn: #ff7b72;
        --warn-bg: #2d1515;
        --caution: #f0a04b;
        --neutral-bg: #232b35;
        --chart-bar: #4c9aff;
        --chart-line: #2dd4bf;
        --shadow: none;
        color-scheme: dark;
    }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 17px; margin: 24px 0 10px; }
h3 { font-size: 15px; margin: 18px 0 8px; }

.muted { color: var(--muted); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; }

/* шапка */
.topbar {
    display: flex;
    align-items: center;
    gap: 20px;
    flex-wrap: wrap;
    padding: 10px 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--text); font-size: 15px; }
.brand:hover { text-decoration: none; }
.topnav { display: flex; gap: 4px; flex-wrap: wrap; }
.topnav a {
    color: var(--muted);
    padding: 6px 10px;
    border-radius: 6px;
}
.topnav a:hover, .topnav a.active { background: var(--surface-2); color: var(--text); text-decoration: none; }
.userbox { margin-left: auto; display: flex; gap: 12px; align-items: center; color: var(--muted); flex-wrap: wrap; }
.userbox form { margin: 0; }

.container { max-width: 1280px; margin: 0 auto; padding: 20px 16px 48px; }

.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.breadcrumbs { color: var(--muted); font-size: 13px; margin-bottom: 4px; }
.breadcrumbs a { color: var(--muted); }

/* блоки */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 16px;
    margin-bottom: 16px;
}
.card > h2:first-child, .card > h3:first-child { margin-top: 0; }

.grid-cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 12px; }
.group-card { display: block; color: var(--text); }
.group-card:hover { text-decoration: none; border-color: var(--accent); }
.group-card .name { font-weight: 600; font-size: 15px; margin-bottom: 6px; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.tile .label { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
.tile .value { font-size: 20px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.tile .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.cell-sub { color: var(--muted); font-size: 12px; white-space: nowrap; }

/* статусы */
.badge {
    display: inline-block;
    padding: 1px 8px;
    border-radius: 999px;
    font-size: 12px;
    font-weight: 500;
    background: var(--neutral-bg);
    color: var(--muted);
    white-space: nowrap;
}
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.text-ok { color: var(--ok); }
.text-warn { color: var(--warn); }
.text-caution { color: var(--caution); }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }

/* таблицы */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.data { width: 100%; border-collapse: collapse; }
table.data th, table.data td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.data thead th {
    position: sticky; top: 0;
    background: var(--surface-2);
    font-weight: 600; font-size: 12px; color: var(--muted);
    text-transform: uppercase; letter-spacing: .03em;
    white-space: nowrap;
}
table.data tbody tr:hover { background: var(--surface-2); }
table.data tbody tr:last-child td { border-bottom: none; }
table.data tfoot td, table.data tfoot th { background: var(--surface-2); font-weight: 600; border-top: 1px solid var(--border); border-bottom: none; }
table.data tr.subtotal td { background: var(--surface-2); font-weight: 600; }
table.data pre { margin: 0; font: inherit; white-space: pre-wrap; }
table.kv th { width: 45%; font-weight: 500; color: var(--muted); }

table.sortable thead th[data-sort] { cursor: pointer; user-select: none; }
table.sortable thead th[data-sort]::after { content: " ↕"; opacity: .35; }
table.sortable thead th.sorted-asc::after { content: " ↑"; opacity: 1; }
table.sortable thead th.sorted-desc::after { content: " ↓"; opacity: 1; }

/* лог обмена: моноширинный, переносы и отступы дампов сохраняются */
table.data.log td { padding: 3px 10px; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12px; line-height: 18px; }
table.data.log pre.log-text { margin: 0; font: inherit; white-space: pre-wrap; overflow-wrap: anywhere; }
table.data.log details > pre.log-text { margin-top: 4px; padding: 6px 10px; background: var(--surface-2); border-radius: 6px; }
table.data.log summary { cursor: pointer; list-style: none; }
table.data.log summary::-webkit-details-marker { display: none; }
table.data.log summary::before { content: "▸ "; color: var(--accent); }
table.data.log details[open] > summary::before { content: "▾ "; }
table.data.log details[open] > summary .log-preview { color: var(--muted); }
.log-preview { white-space: pre-wrap; overflow-wrap: anywhere; }

/* формы */
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 12px; }
.toolbar .spacer { flex: 1; }
.form-row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field label { font-size: 12px; color: var(--muted); }
.form-narrow { max-width: 420px; }

input[type=text], input[type=password], input[type=date], input[type=search], input[type=number], input[type=time], select, textarea {
    font: inherit;
    color: var(--text);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 6px 10px;
    min-height: 34px;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
textarea { min-height: 0; resize: vertical; width: 100%; }
textarea.code { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 13px; line-height: 1.5; tab-size: 2; white-space: pre; }
textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.chips label { display: inline-flex; gap: 6px; align-items: center; }
.pre-line { white-space: pre-line; }
details > summary.btn { list-style: none; cursor: pointer; }
details > summary.btn::-webkit-details-marker { display: none; }
td pre.mono { margin: 6px 0 0; white-space: pre-wrap; }
table.data td > pre.mono:first-child { margin-top: 0; }

/* правила: редактор JSON и справка рядом */
.rules-json { display: grid; grid-template-columns: minmax(0, 1fr) 300px; gap: 16px; align-items: start; }
.rules-json .card p { margin: 0 0 8px; }
@media (max-width: 960px) { .rules-json { grid-template-columns: minmax(0, 1fr); } }

/* правила на странице прибора */
table.rules-meter td:first-child { font-weight: 600; white-space: nowrap; width: 1%; }

.btn {
    display: inline-flex; align-items: center; gap: 6px;
    font: inherit; font-weight: 500;
    padding: 6px 14px; min-height: 34px;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    white-space: nowrap;
}
.btn:hover { background: var(--surface-2); text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--warn); }
.btn.small { padding: 2px 10px; min-height: 26px; font-size: 13px; }
.btn.link { border: none; background: none; padding: 0; min-height: 0; color: var(--accent); }
form.inline { display: inline; margin: 0; }

.checklist { max-height: 260px; overflow: auto; border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px; background: var(--surface); }
.checklist label { display: block; padding: 3px 0; }

.alert { padding: 10px 14px; border-radius: var(--radius); margin-bottom: 16px; border: 1px solid var(--border); background: var(--surface); }
.alert.ok { background: var(--ok-bg); border-color: transparent; color: var(--ok); }
.alert.error { background: var(--warn-bg); border-color: transparent; color: var(--warn); }
.secret { font-family: ui-monospace, Consolas, monospace; word-break: break-all; background: var(--surface); color: var(--text); padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border); display: block; margin: 8px 0; }

/* вкладки */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs a {
    padding: 8px 14px;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    white-space: nowrap;
}
.tabs a:hover { color: var(--text); text-decoration: none; }
.tabs a.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

.presets { display: flex; gap: 6px; flex-wrap: wrap; }
.field.presets { flex-direction: row; }

/* графики */
.chart { width: 100%; height: auto; display: block; }
.chart .axis { stroke: var(--border); stroke-width: 1; }
.chart .grid { stroke: var(--border); stroke-width: 1; stroke-dasharray: 3 4; }
.chart .lbl { fill: var(--muted); font-size: 12px; }
.chart .bar { fill: var(--chart-bar); }
.chart .bar:hover { opacity: .75; }
.chart .bar.neg { fill: var(--warn); }
.chart .line { fill: none; stroke: var(--chart-line); stroke-width: 2; }
.chart .dot { fill: var(--chart-line); }

/* вход */
.login-wrap { min-height: 80vh; display: flex; align-items: center; justify-content: center; }
.login-card { width: 100%; max-width: 360px; }

@media (max-width: 640px) {
    .container { padding: 12px 10px 32px; }
    .topbar { padding: 10px 12px; gap: 10px; }
    .userbox { margin-left: 0; }
    .tile .value { font-size: 17px; }
}
