:root {
    --bg: #11161d;
    --panel: #1a222c;
    --panel-light: #222c38;
    --line: #2d3a44;
    --text: #e6edf3;
    --muted: #8d9aa8;
    --accent: #4da3ff;
    --ok: #4fd28b;
    --stale: #d2a34f;
    --off: #7c8894;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font: 14px/1.45 "Segoe UI", Roboto, system-ui, sans-serif;
}

header {
    display: flex;
    align-items: center;
    gap: 24px;
    padding: 10px 16px;
    background: var(--panel);
    border-bottom: 1px solid #2d3a44;
}

header h1 {
    font-size: 16px;
    font-weight: 600;
    margin: 0;
    white-space: nowrap;
}

.stats {
    display: flex;
    gap: 18px;
    color: var(--muted);
    font-size: 13px;
    flex: 1;
    flex-wrap: wrap;
}

.stats b { color: var(--text); font-weight: 600; }

.controls {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--muted);
}

select, input[type="search"], button {
    background: var(--panel-light);
    color: var(--text);
    border: 1px solid #2d3a44;
    border-radius: 6px;
    padding: 6px 9px;
    font: inherit;
}

button { cursor: pointer; }
button.toggle.active { border-color: var(--accent); color: var(--accent); }

.connection { font-size: 12px; }
.connection.online { color: var(--ok); }
.connection.offline { color: #e06c75; }

main {
    display: flex;
    height: calc(100% - 53px);
}

aside {
    width: 300px;
    min-width: 240px;
    background: var(--panel);
    border-right: 1px solid #2d3a44;
    display: flex;
    flex-direction: column;
}

aside input[type="search"] { margin: 10px 10px 0; }
aside #group-filter { margin: 8px 10px 10px; }

#devices {
    list-style: none;
    margin: 0;
    padding: 0 0 10px;
    overflow-y: auto;
    flex: 1;
}

#devices li {
    padding: 9px 12px;
    border-bottom: 1px solid #222c38;
    cursor: pointer;
}

#devices li:hover { background: var(--panel-light); }
#devices li.selected { background: #243447; box-shadow: inset 3px 0 0 var(--accent); }

.device-title { display: flex; align-items: center; gap: 7px; font-weight: 600; }

.dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: var(--off);
    flex: none;
}

.dot.online { background: var(--ok); }
.dot.stale { background: var(--stale); }

.device-meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
.device-meta.fleet { color: var(--accent); }
.device-meta.sensors { color: #c9d4a8; }

.workspace { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
#map { flex: 1; min-height: 240px; background: #0d1117; }

/* Всплывающие события над картой: Leaflet держит z-index до 1000. */
.toasts {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 1100;
    display: flex;
    flex-direction: column;
    gap: 8px;
    max-width: 320px;
    pointer-events: none;
}

.toast {
    pointer-events: auto;
    cursor: pointer;
    background: var(--panel);
    border: 1px solid var(--stale);
    border-left: 3px solid var(--stale);
    border-radius: 6px;
    padding: 8px 12px;
    display: flex;
    flex-direction: column;
    gap: 2px;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.45);
}

.toast-time { color: var(--muted); font-size: 12px; }

.log {
    height: 250px;
    background: var(--panel);
    border-top: 1px solid #2d3a44;
    display: flex;
    flex-direction: column;
}

.log-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 8px 12px;
    border-bottom: 1px solid #222c38;
    font-weight: 600;
}

.table-wrap { overflow: auto; flex: 1; }

table { width: 100%; border-collapse: collapse; font-size: 13px; }

th {
    position: sticky;
    top: 0;
    background: var(--panel-light);
    text-align: left;
    padding: 6px 10px;
    font-weight: 600;
    color: var(--muted);
    white-space: nowrap;
}

td {
    padding: 5px 10px;
    border-bottom: 1px solid #1f2731;
    white-space: nowrap;
}

td.params {
    white-space: normal;
    color: var(--muted);
    font-size: 12px;
    max-width: 420px;
}

tr.invalid td { color: #b06a6a; }

.empty { padding: 14px; color: var(--muted); }

.leaflet-popup-content-wrapper,
.leaflet-popup-tip { background: var(--panel-light); color: var(--text); }

.leaflet-popup-content { margin: 10px 12px; }
.leaflet-popup-content b { color: var(--accent); }

.marker-label {
    background: rgba(26, 34, 44, .85);
    border: 1px solid #3a4756;
    border-radius: 4px;
    padding: 1px 5px;
    color: var(--text);
    font-size: 11px;
    white-space: nowrap;
}

/* --- общие элементы форм, вход и админпанель --- */

input[type="text"], input[type="password"], input[type="number"], textarea {
    background: var(--panel-light);
    color: var(--text);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 6px 9px;
    font: inherit;
}

textarea { resize: vertical; }

button.primary { border-color: var(--accent); color: var(--accent); }
button.danger { border-color: #7a3b3b; color: #e08a8a; }
button:disabled { opacity: .55; cursor: default; }

.hint { color: var(--muted); font-size: 12px; margin: 4px 0 10px; }
.hint code { color: var(--text); }

.error {
    color: #e06c75;
    background: #2b1d1f;
    border: 1px solid #5c2f33;
    border-radius: 6px;
    padding: 8px 10px;
    margin: 0 0 12px;
}

body.centered {
    display: flex;
    align-items: center;
    justify-content: center;
}

.card {
    width: 320px;
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 22px 24px;
    display: flex;
    flex-direction: column;
}

.card h1 { font-size: 18px; margin: 0; }
.card label { color: var(--muted); font-size: 12px; margin: 10px 0 4px; }
.card button { margin-top: 16px; padding: 8px; }

.tabs { display: flex; gap: 6px; }
.tab.active { border-color: var(--accent); color: var(--accent); }

header .spacer { flex: 1; }
header .link { color: var(--accent); text-decoration: none; white-space: nowrap; }
header .whoami { color: var(--muted); font-size: 13px; white-space: nowrap; }

main.admin {
    display: block;
    height: calc(100% - 53px);
    overflow-y: auto;
    padding: 16px;
}

.device-modal { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .58); padding: 0; overflow-y: auto; }
.device-dialog { width: min(100%, 980px); min-height: 560px; margin: 0 auto; background: #f4f6f8; color: #172333; border: 1px solid #8799a8; box-shadow: 0 12px 38px rgba(0,0,0,.45); }
.device-dialog-header { height: 34px; display: flex; align-items: center; justify-content: space-between; padding: 0 10px 0 16px; background: #0799df; color: white; }
.device-dialog-header h2 { margin: 0; font-size: 16px; font-weight: 500; }
.device-dialog-header button { border: 0; background: transparent; color: white; font-size: 24px; line-height: 1; padding: 0 2px; }
.device-dialog-body { padding: 0 14px 12px; background: #fff; }
.device-dialog .row button.primary { background: #078ee0; color: #fff; border: 1px solid #067ac0; border-radius: 2px; }
.device-dialog-body #device-options-form > .row { position: absolute; bottom: 8px; right: 44px; }
.device-dialog { position: relative; padding-bottom: 46px; }
.device-dialog-body #device-options-form > .device-tab-content { min-height: 370px; }
.device-dialog-extra { padding: 8px 14px; background: #fff; }
.device-dialog-extra h2 { color: #243548; }
.device-dialog-extra .panel { border: 0; border-radius: 0; border-top: 1px solid #bac3cc; color: #243548; background: #fff; }
.device-dialog-extra .hint { color: #53667b; }
.device-dialog-extra input, .device-dialog-extra select, .device-dialog-extra textarea { border-radius: 2px; background: #fff; color: #172333; border-color: #aab5bf; }
.device-dialog-extra button { border-radius: 2px; }
.device-dialog-extra { width: min(100%, 980px); margin: 0 auto; }
.device-dialog-extra .panel { background: var(--panel); }

.panel {
    background: var(--panel);
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 14px 16px;
    margin-bottom: 16px;
}

.panel h2 { font-size: 15px; margin: 0 0 2px; }
.panel .counter { color: var(--muted); font-weight: 400; }
.panel .table-wrap { max-height: 60vh; }

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 8px; }
.row input { flex: 1 1 180px; }
.row textarea { flex: 1 1 100%; }

td.actions { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
td input[type="text"] { width: 100%; min-width: 120px; }

.check { display: flex; align-items: center; gap: 5px; white-space: nowrap; }

.checks {
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-height: 110px;
    overflow-y: auto;
    margin-bottom: 6px;
}

/* Карточка объекта: настройки блоками, датчики узкими колонками. */

.block {
    border-top: 1px solid var(--line);
    padding-top: 6px;
    margin-top: 12px;
}

.block:first-child { border-top: 0; margin-top: 0; }
.block h3 { font-size: 13px; color: var(--accent); margin: 0; }
.block .row label { flex: 0 0 220px; color: var(--muted); }
.block .row .hint { flex: 1 1 100%; margin: 0; }

.device-tabs { display: flex; flex-wrap: wrap; gap: 3px; margin: 0; padding-top: 8px; border-bottom: 1px solid #9ba6b0; }
.device-tab { border-radius: 0; border: 1px solid #c4cbd1; margin: 0; padding: 6px 12px; background: #e8ebef; color: #314255; font-size: 13px; }
.device-tab.active { background: #fff; color: #233247; border-color: #aeb8c1; border-bottom-color: #fff; }
.device-tab-content { min-height: 285px; padding: 10px 5px; background: #fff; border-bottom: 1px solid #c4cbd1; }
.device-tab-content > .device-tab-panel { min-height: 265px; }
.device-dialog-extra[hidden] { display: none; }
.device-dialog-extra .panel:first-child { padding-top: 0; }
.device-dialog-extra .panel:last-child { padding-bottom: 0; }
.device-tab-panel[hidden] { display: none; }
.device-tab-panel .block { border: 0; margin: 0; padding: 0; }
.device-tab-panel .block .row { display: grid; grid-template-columns: 215px minmax(180px, 310px); justify-content: start; gap: 6px 10px; margin: 5px 0; }
.device-tab-panel .block .row input,
.device-tab-panel .block .row select { width: 100%; min-width: 0; background: #fff; color: #172333; border: 1px solid #9ca9b5; border-radius: 0; padding: 3px 6px; }
.device-tab-panel .block .row label { color: #26384c; font-weight: 400; font-size: 12px; }
.device-tab-panel .block .row .hint { grid-column: 2; color: #617286; font-size: 11px; }
.device-dialog-body .row { align-items: start; }
.device-dialog-body #device-options-form > .device-tab-content + .row { padding: 7px 2px; }
@media (max-width: 720px) { .device-tab-panel .block .row { grid-template-columns: 1fr; gap: 4px; } .device-tab { padding: 6px 8px; font-size: 12px; } }

 td.pair { display: flex; gap: 4px; }
td input.narrow { width: 80px; min-width: 0; }
td textarea { width: 130px; min-width: 0; font-size: 12px; }
