:root {
    --bg: #f4f6fb;
    --surface: #ffffff;
    --surface-2: #f0f3f9;
    --border: #e2e7f0;
    --text: #1d2433;
    --muted: #6b7686;
    --primary: #2f6df6;
    --primary-ink: #ffffff;
    --ok: #1f9d55;
    --bad: #d64545;
    --warn: #c98a00;
    --warn-bg: #fff6df;
    --offline: #8a94a6;
    --radius: 10px;
    --shadow: 0 1px 3px rgba(20, 30, 60, .08), 0 6px 20px rgba(20, 30, 60, .05);
    font-size: 14px;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #11151c;
        --surface: #182029;
        --surface-2: #1f2a36;
        --border: #2a3644;
        --text: #e7ecf3;
        --muted: #9aa7b8;
        --primary: #4d87ff;
        --ok: #37c172;
        --bad: #ff6b6b;
        --warn: #ffbe3d;
        --warn-bg: #3a2f10;
        --offline: #697585;
        --shadow: 0 1px 3px rgba(0, 0, 0, .3), 0 8px 24px rgba(0, 0, 0, .25);
    }
}

* { box-sizing: border-box; }

body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
    line-height: 1.45;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.4rem; margin: 0 0 .6rem; }

.topbar {
    display: flex;
    align-items: center;
    gap: 1.2rem;
    padding: .7rem 1.2rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    position: sticky;
    top: 0;
    z-index: 10;
}
.topbar .brand { font-weight: 800; letter-spacing: .3px; color: var(--primary); }
.topbar nav { display: flex; gap: .4rem; }
.topbar nav a {
    padding: .35rem .7rem;
    border-radius: 8px;
    color: var(--text);
}
.topbar nav a.active, .topbar nav a:hover { background: var(--surface-2); text-decoration: none; }
.topbar .user { margin-left: auto; display: flex; align-items: center; gap: .7rem; color: var(--muted); }

.container { max-width: 1280px; margin: 0 auto; padding: 1.2rem; }

.btn {
    display: inline-flex;
    align-items: center;
    gap: .35rem;
    padding: .42rem .8rem;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 8px;
    cursor: pointer;
    font-size: .9rem;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--primary); color: var(--primary-ink); border-color: transparent; }
/* Важливо: тримаємо фон/колір і на hover, інакше загальне .btn:hover
   перекриває фон на світлий, а білий текст primary стає невидимим. */
.btn-primary:hover { background: var(--primary); color: var(--primary-ink); filter: brightness(1.08); }
.btn-danger { color: var(--bad); border-color: var(--bad); }
.btn-danger:hover { background: var(--bad); color: #fff; }
.btn-ghost { border-color: transparent; background: transparent; }

/* --- Вхід --- */
.auth-card {
    max-width: 360px;
    margin: 8vh auto;
    background: var(--surface);
    padding: 1.6rem;
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
}
.auth-card h1 { text-align: center; }
.auth-card label { display: block; margin: .8rem 0 .2rem; color: var(--muted); }
.auth-card input {
    width: 100%;
    padding: .6rem .7rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface-2);
    color: var(--text);
}
.auth-card .btn { width: 100%; justify-content: center; margin-top: 1.2rem; }
.alert { background: var(--bad); color: #fff; padding: .5rem .7rem; border-radius: 8px; margin-bottom: .6rem; }

/* --- Dashboard --- */
.dash-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; }
.dash-tools { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; }
.dash-tools input[type=search] {
    padding: .45rem .7rem; border: 1px solid var(--border);
    border-radius: 8px; background: var(--surface); color: var(--text); min-width: 220px;
}
.switch { display: inline-flex; align-items: center; gap: .4rem; color: var(--muted); cursor: pointer; }
.totals { color: var(--muted); }
.totals b.ok { color: var(--ok); }
.totals b.bad { color: var(--bad); }

.table-wrap { overflow-x: auto; margin-top: 1rem; border-radius: var(--radius); box-shadow: var(--shadow); }
table.devices { width: 100%; border-collapse: collapse; background: var(--surface); min-width: 980px; }
table.devices th, table.devices td { padding: .6rem .7rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
table.devices thead th { background: var(--surface-2); color: var(--muted); font-weight: 600; font-size: .82rem; text-transform: uppercase; letter-spacing: .4px; position: sticky; top: 0; }
table.devices tbody tr:hover { background: var(--surface-2); }
.col-chk, .col-ctl { width: 1%; white-space: nowrap; }

.status-dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: .4rem; vertical-align: middle; }
.st-running { background: var(--ok); animation: pulse 2.2s infinite; }
.st-idle { background: var(--muted); }
.st-warn { background: var(--warn); box-shadow: 0 0 0 3px rgba(201, 138, 0, .18); }
.st-offline { background: var(--offline); }
@keyframes pulse {
    0%   { box-shadow: 0 0 0 0 rgba(31, 157, 85, .4); }
    70%  { box-shadow: 0 0 0 6px rgba(31, 157, 85, 0); }
    100% { box-shadow: 0 0 0 0 rgba(31, 157, 85, 0); }
}
@media (prefers-reduced-motion: reduce) { .st-running { animation: none; box-shadow: 0 0 0 3px rgba(31,157,85,.18); } }
tr.row-offline { opacity: .62; }
tr.row-warn td { background: var(--warn-bg); }

.dev-id { font-weight: 600; }
.dev-sub { color: var(--muted); font-size: .82rem; }
.uptime { color: var(--ok); font-size: .82rem; }

.apps { display: flex; flex-wrap: wrap; gap: .3rem; max-width: 260px; }
.app-badge {
    display: inline-flex; align-items: center; gap: .28rem;
    font-size: .72rem; padding: .12rem .45rem .12rem .3rem; border-radius: 7px;
    background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
}
.app-badge .ico { display: block; border-radius: 3px; flex: none; }
.app-badge .ico-emoji { font-size: 13px; line-height: 1; display: inline-flex; width: 15px; justify-content: center; flex: none; }
.app-badge .lbl { line-height: 1; }
/* вкл — иконка в фирменном цвете; выкл — приглушённо (чётко видно, что отключено) */
.app-badge.off { opacity: .5; filter: grayscale(.9); }
.app-badge.clickable { cursor: pointer; user-select: none; transition: filter .12s, outline-color .12s, opacity .12s; }
.app-badge.clickable:hover { outline: 1px solid var(--muted); outline-offset: 1px; opacity: 1; }
.app-badge.pending { outline: 1px dashed var(--primary); outline-offset: 1px; }

.counters .ok, .counters .bad {
    display: inline-flex; align-items: center; gap: .2rem;
    padding: .06rem .45rem; border-radius: 999px; font-weight: 600; font-size: .82rem;
}
.counters .ok { color: var(--ok); background: rgba(31, 157, 85, .14); }
.counters .bad { color: var(--bad); background: rgba(214, 69, 69, .14); }
.counters br { line-height: .4; }

.err { color: var(--bad); font-size: .82rem; white-space: pre-wrap; word-break: break-word; max-width: 320px; }
.info-cell { max-width: 340px; font-size: .84rem; color: var(--muted); }
.info-cell .pause-warn { color: var(--warn); font-weight: 600; }

.ctl { display: inline-flex; gap: .3rem; }
.ctl button {
    width: 32px; height: 32px; display: inline-flex; align-items: center; justify-content: center;
    border: 1px solid var(--border); background: var(--surface); border-radius: 8px; cursor: pointer;
    font-size: .95rem; color: var(--muted); transition: background .12s, color .12s;
}
.ctl button:hover { background: var(--surface-2); color: var(--text); }
.ctl .play:hover { color: var(--ok); background: rgba(31, 157, 85, .14); border-color: transparent; }
.ctl .stop:hover { color: var(--bad); background: rgba(214, 69, 69, .14); border-color: transparent; }
.ctl .del:hover { color: var(--bad); background: rgba(214, 69, 69, .14); border-color: transparent; }

.bulk-bar { display: flex; align-items: center; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }
.bulk-bar span { color: var(--muted); }

/* --- Журнал --- */
.filters { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-bottom: 1rem; }
.filters input, .filters select {
    padding: .45rem .6rem; border: 1px solid var(--border); border-radius: 8px;
    background: var(--surface); color: var(--text);
}
.filters .inline { display: inline-flex; gap: .3rem; align-items: center; color: var(--muted); }
.log-totals { display: flex; gap: .6rem; align-items: center; margin-bottom: 1rem; }
.pill { padding: .2rem .6rem; border-radius: 999px; font-size: .82rem; }
.pill-ok { background: rgba(31, 157, 85, .15); color: var(--ok); }
.pill-bad { background: rgba(214, 69, 69, .15); color: var(--bad); }
.log-list { display: flex; flex-direction: column; gap: .5rem; }
.log-item { background: var(--surface); border: 1px solid var(--border); border-left: 3px solid var(--muted); border-radius: 8px; padding: .6rem .8rem; }
.log-item.log-ok { border-left-color: var(--ok); }
.log-item.log-bad { border-left-color: var(--bad); }
.log-meta { display: flex; gap: .6rem; align-items: baseline; flex-wrap: wrap; }
.log-type { font-weight: 700; text-transform: uppercase; font-size: .75rem; }
.log-ok .log-type { color: var(--ok); }
.log-bad .log-type { color: var(--bad); }
.log-user { font-weight: 600; }
.log-history { background: var(--surface-2); padding: .6rem; border-radius: 6px; margin: .5rem 0 0; font-size: .8rem; white-space: pre-wrap; word-break: break-word; max-height: 260px; overflow: auto; }

.muted { color: var(--muted); }
.footer { text-align: center; color: var(--muted); padding: 2rem 1rem; }

/* --- Доведення (focus, виділення, скрол, дрібниці) --- */
.btn:active { transform: translateY(1px); }
a:focus-visible, .btn:focus-visible, input:focus-visible, select:focus-visible,
input[type=checkbox]:focus-visible {
    outline: 2px solid var(--primary); outline-offset: 2px;
}
.auth-card input:focus, .dash-tools input:focus, .filters input:focus, .filters select:focus {
    outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px rgba(47, 109, 246, .18);
}
/* виділений рядок не губиться при оновленні і підсвічується */
table.devices tbody tr:has(.rowchk:checked) { background: rgba(47, 109, 246, .10); }
table.devices tbody tr:has(.rowchk:checked):hover { background: rgba(47, 109, 246, .14); }
/* групу/зону/версію не переносимо */
table.devices td:nth-child(3), table.devices td:nth-child(4), table.devices td:nth-child(5) { white-space: nowrap; }

.table-wrap::-webkit-scrollbar, .log-history::-webkit-scrollbar { height: 10px; width: 10px; }
.table-wrap::-webkit-scrollbar-thumb, .log-history::-webkit-scrollbar-thumb {
    background: var(--border); border-radius: 8px;
}
.table-wrap::-webkit-scrollbar-track { background: transparent; }

@media (max-width: 640px) {
    .dash-tools { width: 100%; }
    .dash-tools input[type=search] { min-width: 0; flex: 1; }
    .dash-head { align-items: flex-start; }
    .container { padding: .8rem; }
}
