:root {
    color-scheme: light dark;
    --bg: #eef2f6;
    --panel: #ffffff;
    --text: #17212b;
    --muted: #667085;
    --border: #c8d2df;
    --blue: #1f4d78;
    --blue-soft: #e8eef5;
    --green: #14a44d;
    --red: #d0342c;
    --yellow: #b7791f;
    --terminal: #071019;
    --terminal-text: #d7f7dd;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    height: 100vh;
    background: var(--bg);
    color: var(--text);
    font-family: "Segoe UI", Arial, sans-serif;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

button, input, select {
    font: inherit;
}

button {
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    padding: 0.45rem 0.7rem;
    cursor: pointer;
}

button:hover {
    border-color: var(--blue);
}

button.danger {
    color: var(--red);
}

.topbar {
    display: flex;
    flex: 0 0 auto;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: 1rem 1.2rem;
    background: var(--blue);
    color: #fff;
}

.topbar h1 {
    margin: 0;
    font-size: 1.35rem;
}

.topbar p {
    margin: 0.25rem 0 0;
    opacity: 0.85;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.topbar-actions select {
    border: 1px solid rgba(255, 255, 255, 0.45);
    border-radius: 6px;
    background: #fff;
    color: var(--text);
    padding: 0.45rem 0.55rem;
}

.layout {
    display: grid;
    grid-template-columns: 280px minmax(360px, 1fr) 360px;
    grid-template-rows: minmax(0, 1fr);
    flex: 1 1 auto;
    min-height: 0;
    gap: 1rem;
    padding: 1rem;
    overflow: hidden;
}

.panel {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    min-width: 0;
    min-height: 0;
    box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08);
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 1rem;
    padding: 0.8rem 0.9rem;
    border-bottom: 1px solid var(--border);
}

.panel-header h2 {
    margin: 0;
    font-size: 1rem;
}

.muted {
    color: var(--muted);
    margin: 0.2rem 0 0;
    font-size: 0.85rem;
}

.badge {
    min-width: 1.6rem;
    border-radius: 999px;
    background: var(--blue-soft);
    color: var(--blue);
    text-align: center;
    padding: 0.2rem 0.45rem;
    font-weight: 700;
}

.badge.alarm {
    color: var(--red);
}

.devices-list, .alarms-list, .rules-list {
    padding: 0.7rem;
}

.devices-panel {
    overflow: auto;
}

.device {
    display: grid;
    grid-template-columns: 14px minmax(0, 1fr) 28px;
    align-items: start;
    width: 100%;
    gap: 0.6rem;
    margin-bottom: 0.5rem;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.45rem;
}

.device.selected {
    border-color: var(--blue);
    background: var(--blue-soft);
}

.device-main {
    min-width: 0;
    border: 0;
    background: transparent;
    padding: 0;
    text-align: left;
}

.device-main:hover {
    border-color: transparent;
}

.device-delete, .rule-delete {
    width: 28px;
    height: 28px;
    padding: 0;
    border-radius: 4px;
    color: var(--muted);
    line-height: 1;
}

.device-delete:hover, .rule-delete:hover {
    border-color: var(--red);
    color: var(--red);
}

.device small {
    display: block;
    margin-top: 0.2rem;
    color: var(--muted);
    line-height: 1.35;
}

.status {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    margin-top: 0.3rem;
    background: var(--red);
}

.status.online {
    background: var(--green);
}

.status.ready {
    background: #f5b301;
}

.terminal-panel {
    display: flex;
    flex-direction: column;
    min-height: 0;
    height: 100%;
    overflow: hidden;
}

.terminal-header {
    align-items: stretch;
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    gap: 0.65rem;
}

.filters {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.9rem;
}

.filters label {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    white-space: nowrap;
    line-height: 1;
}

.filters input[type="checkbox"] {
    margin: 0;
}

.filters button {
    margin-left: 0.2rem;
    white-space: nowrap;
}

.terminal-actions {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    flex-wrap: wrap;
    justify-content: flex-end;
    margin-left: auto;
}

.terminal {
    flex: 1;
    min-height: 0;
    margin: 0;
    padding: 0.9rem;
    background: var(--terminal);
    color: var(--terminal-text);
    overflow: auto;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    font-family: Consolas, "Courier New", monospace;
    font-size: 0.9rem;
    line-height: 1.45;
    border-radius: 0;
}

.command-form {
    display: grid;
    grid-template-columns: minmax(160px, 1fr) auto auto auto auto auto;
    gap: 0.5rem;
    align-items: center;
    padding: 0.65rem;
    border-top: 1px solid var(--border);
    background: #fff;
}

.command-form input[type="text"] {
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.45rem;
    color: var(--text);
    background: #fff;
}

.command-form label {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    white-space: nowrap;
}

.connection-form {
    display: grid;
    grid-template-columns: 150px 120px 110px minmax(130px, 1fr) 95px auto;
    gap: 0.5rem;
    align-items: center;
    padding: 0 0.65rem 0.65rem;
    background: #fff;
    border-radius: 0 0 8px 8px;
}

.connection-form input,
.connection-form select {
    min-width: 0;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.45rem;
    color: var(--text);
    background: #fff;
}

.side-panel {
    padding-bottom: 0.7rem;
    overflow: auto;
}

.settings-overlay {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: grid;
    place-items: center;
    padding: 1rem;
    background: rgba(15, 23, 42, 0.45);
}

.settings-overlay[hidden] {
    display: none;
}

.settings-dialog {
    width: min(760px, 100%);
    max-height: min(92vh, 980px);
    overflow: auto;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    box-shadow: 0 20px 50px rgba(15, 23, 42, 0.28);
}

.settings-form {
    display: grid;
    gap: 0.8rem;
    padding: 0.8rem;
    border-bottom: 1px solid var(--border);
}

.settings-form fieldset {
    display: grid;
    gap: 0.55rem;
    margin: 0;
    padding: 0.75rem;
    border: 1px solid var(--border);
    border-radius: 7px;
}

.settings-form legend {
    padding: 0 0.25rem;
    font-weight: 700;
}

.settings-form label {
    display: grid;
    gap: 0.25rem;
    color: var(--muted);
    font-size: 0.85rem;
}

.settings-form input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.45rem;
    color: var(--text);
    background: #fff;
}

.settings-actions {
    display: flex;
    justify-content: flex-end;
}

.alarm-card, .rule-card {
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 0.65rem;
    margin-bottom: 0.55rem;
    background: #fff;
}

.rule-card {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 0.7rem;
}

.rule-card-main {
    min-width: 0;
    flex: 1 1 auto;
    border: 0;
    background: transparent;
    padding: 0;
    text-align: left;
    color: inherit;
}

.rule-card-main:hover {
    border-color: transparent;
}

.rule-actions {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.alarm-card small, .rule-card small {
    display: block;
    color: var(--muted);
    margin-top: 0.15rem;
}

.alarm-card p {
    margin: 0.45rem 0;
}

.rules-section {
    border-top: 1px solid var(--border);
}

.rule-form {
    display: grid;
    gap: 0.55rem;
    padding: 0.7rem;
}

.rule-form label {
    display: grid;
    gap: 0.25rem;
    color: var(--muted);
    font-size: 0.85rem;
}

.rule-form input, .rule-form select {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.45rem;
    color: var(--text);
    background: #fff;
}

.checks {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.45rem 0.8rem;
    margin-top: 0.2rem;
}

.checks label {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    color: var(--text);
    font-size: 0.9rem;
    line-height: 1.2;
}

.checks input[type="checkbox"] {
    width: auto;
    margin: 0;
    flex: 0 0 auto;
}

.toast {
    position: fixed;
    right: 1rem;
    bottom: 1rem;
    max-width: 420px;
    background: #111827;
    color: #fff;
    border-radius: 8px;
    padding: 0.75rem 0.9rem;
    box-shadow: 0 12px 30px rgba(0, 0, 0, 0.22);
}

.install {
    max-width: 760px;
    margin: 3rem auto;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 1.4rem;
}

.status-ok {
    color: var(--green);
    font-weight: 700;
}

.status-error {
    color: var(--red);
    font-weight: 700;
}

@media (max-width: 1120px) {
    body {
        overflow: auto;
        height: 100vh;
        min-height: 100vh;
    }

    .layout {
        grid-template-columns: 260px 1fr;
        overflow: auto;
    }

    .side-panel {
        grid-column: 1 / -1;
        overflow: visible;
    }
}

@media (max-width: 760px) {
    .topbar {
        align-items: flex-start;
        flex-direction: column;
    }

    .layout {
        display: flex;
        flex-direction: column;
        grid-template-columns: none;
        padding: 0.6rem;
        overflow: visible;
    }

    .devices-panel {
        flex: 0 0 auto;
        overflow: hidden;
    }

    .devices-list {
        display: flex;
        gap: 0.55rem;
        overflow-x: auto;
        padding: 0.65rem;
        -webkit-overflow-scrolling: touch;
        scroll-snap-type: x proximity;
    }

    .device {
        flex: 0 0 min(86vw, 280px);
        margin-bottom: 0;
        scroll-snap-align: start;
    }

    .terminal-panel {
        display: flex;
        flex-direction: column;
        flex: 0 0 auto;
        height: auto;
        min-height: 0;
        max-height: none;
        overflow: hidden;
    }

    .terminal {
        flex: 0 0 auto;
        height: clamp(420px, 68svh, 760px);
        min-height: 0;
        max-height: none;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .terminal-header {
        gap: 0.75rem;
    }

    .filters {
        justify-content: flex-start;
        flex-wrap: wrap;
        column-gap: 0.38rem;
        row-gap: 0.45rem;
        font-size: 0.86rem;
    }

    .terminal-actions {
        justify-content: flex-start;
        margin-left: 0;
    }

    .command-form {
        grid-template-columns: repeat(4, auto);
        justify-content: stretch;
        align-items: center;
        width: 100%;
        flex: 0 0 auto;
        position: relative;
        z-index: 1;
    }

    .command-form input[type="text"],
    .command-form button {
        grid-column: 1 / -1;
        width: 100%;
        min-width: 0;
    }

    .command-form label {
        min-width: 0;
        margin-right: 0.7rem;
    }

    .connection-form {
        grid-template-columns: minmax(0, 3fr) minmax(82px, 1fr);
        flex: 0 0 auto;
    }

    .connection-form select,
    .connection-form button {
        grid-column: 1 / -1;
    }

    .side-panel {
        display: block;
        flex: 0 0 auto;
        grid-column: auto;
        overflow: visible;
        margin-top: 0;
        padding-bottom: calc(1rem + env(safe-area-inset-bottom, 0px));
    }

    .side-panel .panel-header {
        align-items: center;
    }

    .alarms-list {
        padding: 0.75rem;
    }
}

@media (max-width: 760px) and (max-height: 720px) {
    .terminal {
        height: clamp(360px, 60svh, 620px);
    }
}

@media (max-width: 760px) and (display-mode: standalone) {
    .terminal {
        height: clamp(360px, 56svh, 640px);
    }
}
