:root {
    color-scheme: light dark;
    --bg: #f4f5f7;
    --surface: #ffffff;
    --surface-2: #f0f1f3;
    --surface-hover: #e9eaec;
    --border: #dcdfe3;
    --text: #1b1f23;
    --muted: #62676d;
    --accent: #2563eb;
    --accent-text: #ffffff;
    --ok: #1a7f37;
    --ok-bg: rgba(26, 127, 55, 0.12);
    --bad: #c0392b;
    --bad-bg: rgba(192, 57, 43, 0.12);
    --warn: #a05a00;
    --warn-bg: rgba(184, 134, 11, 0.15);
    --unknown: #6b7280;
    --unknown-bg: rgba(107, 114, 128, 0.14);
    --info: #2563eb;
    --info-bg: rgba(37, 99, 235, 0.12);
    --radius: 8px;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #121316;
        --surface: #1a1c20;
        --surface-2: #222429;
        --surface-hover: #2a2d33;
        --border: #33363c;
        --text: #e7e9ea;
        --muted: #9aa0a6;
        --accent: #4f8bff;
        --accent-text: #0b1220;
        --ok: #3fb950;
        --ok-bg: rgba(63, 185, 80, 0.15);
        --bad: #f0554a;
        --bad-bg: rgba(240, 85, 74, 0.15);
        --warn: #d29922;
        --warn-bg: rgba(210, 153, 34, 0.15);
        --unknown: #8b949e;
        --unknown-bg: rgba(139, 148, 158, 0.15);
        --info: #4f8bff;
        --info-bg: rgba(79, 139, 255, 0.15);
        --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    }
}

* { box-sizing: border-box; }

html, body { max-width: 100%; overflow-x: hidden; }

body {
    font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
    margin: 0;
    padding: 0 1.5rem 3rem;
    background: var(--bg);
    color: var(--text);
}

.page-shell { max-width: 1360px; margin-inline: auto; }

a { color: var(--accent); }

header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1.1rem 0;
    border-bottom: 1px solid var(--border);
    max-width: 1360px;
    margin-inline: auto;
}

h1 { font-size: 1.15rem; margin: 0; font-weight: 600; }
h2 { font-size: 1rem; font-weight: 600; margin: 0 0 0.9rem; }
h3 { font-size: 0.9rem; font-weight: 600; margin: 0 0 0.5rem; }

/* ---------- layout ---------- */

.grid { display: grid; gap: 1rem; }
.grid-5 { grid-template-columns: repeat(5, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-main { display: grid; grid-template-columns: 2fr 1fr; gap: 1rem; align-items: start; }

@media (max-width: 1100px) {
    .grid-5, .grid-4 { grid-template-columns: repeat(2, 1fr); }
    .grid-3 { grid-template-columns: repeat(2, 1fr); }
    .grid-main { grid-template-columns: 1fr; }
}
@media (max-width: 640px) {
    .grid-5, .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }
    body { padding: 0 0.85rem 2rem; }
}

/* ---------- card ---------- */

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.15rem;
    box-shadow: var(--shadow);
}
.card-row { display: flex; align-items: center; justify-content: space-between; gap: 0.75rem; margin-bottom: 0.75rem; }
.card-row h2 { margin: 0; }
.card-actions { display: flex; gap: 0.5rem; flex-wrap: wrap; }
.card-footnote { color: var(--muted); font-size: 0.78rem; margin: 0.6rem 0 0; }
.section { margin-top: 1.25rem; }
.section:first-child { margin-top: 0; }

/* ---------- stat card (dashboard top row) ---------- */

.stat-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.9rem 1rem;
    box-shadow: var(--shadow);
    cursor: default;
    display: flex;
    flex-direction: column;
    gap: 0.3rem;
}
.stat-card.clickable { cursor: pointer; }
.stat-card.clickable:hover { background: var(--surface-hover); }
.stat-label { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 1.5rem; font-weight: 650; line-height: 1.15; }
.stat-value.ok { color: var(--ok); }
.stat-value.bad { color: var(--bad); }
.stat-value.warn { color: var(--warn); }
.stat-sub { font-size: 0.78rem; color: var(--muted); }
.stat-sub .up { color: var(--ok); }
.stat-sub .down { color: var(--info); }

/* ---------- definition lists ---------- */

dl { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 0.75rem 1.5rem; margin: 0 0 1rem; }
dl > div { display: flex; flex-direction: column; gap: 0.15rem; }
dt { font-size: 0.72rem; color: var(--muted); text-transform: uppercase; letter-spacing: 0.03em; }
dd { margin: 0; font-size: 0.95rem; }

/* ---------- table ---------- */

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.84rem; }
th, td { text-align: left; padding: 0.5rem 0.6rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.03em; }
tbody tr:hover { background: var(--surface-2); }
td.wrap, th.wrap { white-space: normal; }
.mono { font-variant-numeric: tabular-nums; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.truncate { max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; vertical-align: bottom; }

/* ---------- badges ---------- */

.badge {
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    padding: 0.15rem 0.55rem;
    border-radius: 999px;
    font-size: 0.72rem;
    font-weight: 600;
    line-height: 1.5;
    white-space: nowrap;
}
.badge-ok { color: var(--ok); background: var(--ok-bg); }
.badge-bad { color: var(--bad); background: var(--bad-bg); }
.badge-warn { color: var(--warn); background: var(--warn-bg); }
.badge-unknown { color: var(--unknown); background: var(--unknown-bg); }
.badge-info { color: var(--info); background: var(--info-bg); }

.dot { display: inline-block; width: 0.5rem; height: 0.5rem; border-radius: 50%; background: var(--unknown); flex: none; }
.dot.ok { background: var(--ok); }
.dot.bad { background: var(--bad); }
.dot.warn { background: var(--warn); }

/* ---------- buttons ---------- */

button, .btn {
    font: inherit;
    padding: 0.45rem 0.85rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    color: var(--text);
    cursor: pointer;
    line-height: 1.3;
}
button:hover, .btn:hover { background: var(--surface-hover); }
button:disabled, .btn:disabled { opacity: 0.55; cursor: not-allowed; }
button:disabled:hover { background: var(--surface-2); }

.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { filter: brightness(1.08); }
.btn-warn { color: var(--warn); border-color: var(--warn); background: var(--warn-bg); }
.btn-danger { color: var(--bad); border-color: var(--bad); background: var(--bad-bg); }
.btn-sm { padding: 0.25rem 0.6rem; font-size: 0.78rem; }
.btn-icon { padding: 0.35rem 0.5rem; }

.action-button { padding: 0.25rem 0.6rem; font-size: 0.8rem; margin-right: 0.25rem; }

/* action menu (dropdown) */
.action-menu { position: relative; display: inline-block; }
.action-menu-list {
    position: absolute;
    right: 0;
    top: calc(100% + 0.25rem);
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 6px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
    min-width: 170px;
    z-index: 30;
    padding: 0.3rem;
    display: none;
}
.action-menu.open .action-menu-list { display: block; }
.action-menu-list button {
    display: block;
    width: 100%;
    text-align: left;
    background: none;
    border: none;
    padding: 0.4rem 0.6rem;
    border-radius: 4px;
    font-size: 0.85rem;
}
.action-menu-list button:hover { background: var(--surface-hover); }
.action-menu-list button.danger { color: var(--bad); }
.action-menu-list button.warn { color: var(--warn); }
.action-menu-list hr { border: none; border-top: 1px solid var(--border); margin: 0.3rem 0; }

/* ---------- forms ---------- */

form { display: flex; flex-direction: column; gap: 0.75rem; }
form.form-narrow { max-width: 340px; }
label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.85rem; }
input, select { font: inherit; padding: 0.4rem 0.55rem; border-radius: 6px; border: 1px solid var(--border); background: var(--surface-2); color: var(--text); }
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field-row { display: flex; gap: 0.75rem; flex-wrap: wrap; }
.field-row > label { flex: 1; min-width: 160px; }
.form-actions { display: flex; gap: 0.6rem; justify-content: flex-end; margin-top: 0.25rem; }

.error { color: var(--bad); font-size: 0.85rem; }
.status-ok { color: var(--ok); }
.status-bad { color: var(--bad); }
.status-warn { color: var(--warn); }
.status-unknown { color: var(--unknown); }

.hint { color: var(--muted); font-size: 0.78rem; margin-top: 0.5rem; }

/* ---------- nav / tabs ---------- */

.tabs { display: flex; gap: 0.25rem; margin-top: 1.1rem; max-width: 1360px; margin-inline: auto; flex-wrap: wrap; }
.tab-button {
    background: none;
    border: 1px solid transparent;
    border-bottom: none;
    border-radius: 6px 6px 0 0;
    padding: 0.55rem 1rem;
    color: var(--muted);
}
.tab-button:hover { background: var(--surface-2); color: var(--text); }
.tab-button.active { background: var(--surface); font-weight: 600; color: var(--text); border-color: var(--border); }
.tab-panel { margin-top: -1px; padding-top: 1.1rem; border-top: 1px solid var(--border); max-width: 1360px; margin-inline: auto; }

.page-header { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 1rem; flex-wrap: wrap; gap: 0.5rem; }
.page-header .updated-at { color: var(--muted); font-size: 0.78rem; }

/* in-page sub-tabs (Connections: Active/Recent/Failed) */
.subtabs { display: flex; gap: 0.4rem; margin-bottom: 0.9rem; border-bottom: 1px solid var(--border); }
.subtab-button { background: none; border: none; border-bottom: 2px solid transparent; border-radius: 0; padding: 0.5rem 0.2rem; margin-right: 1rem; color: var(--muted); }
.subtab-button:hover { background: none; color: var(--text); }
.subtab-button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

/* ---------- empty state ---------- */

.empty-state {
    text-align: center;
    padding: 2.25rem 1rem;
    color: var(--muted);
}
.empty-state .empty-title { color: var(--text); font-weight: 600; margin-bottom: 0.3rem; }
.empty-state .empty-detail { font-size: 0.85rem; margin-bottom: 0.9rem; }

.stale-banner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    background: var(--warn-bg);
    color: var(--warn);
    border: 1px solid var(--warn);
    border-radius: 6px;
    padding: 0.5rem 0.75rem;
    font-size: 0.82rem;
    margin-bottom: 0.9rem;
}

/* ---------- modal ---------- */

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding: 4vh 1rem;
    z-index: 100;
    overflow-y: auto;
}
.modal-backdrop[hidden] { display: none; }
.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35);
    width: 100%;
    max-width: 460px;
    padding: 1.25rem;
}
.modal.modal-wide { max-width: 720px; }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 0.9rem; }
.modal-header h2 { margin: 0; }
.modal-close { border: none; background: none; font-size: 1.1rem; padding: 0.2rem 0.5rem; color: var(--muted); }
.modal-close:hover { color: var(--text); background: var(--surface-2); }
.modal-body { max-height: 70vh; overflow-y: auto; }

/* one-time secret reveal */
.secret-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 0.75rem; margin: 0.6rem 0; }
.secret-row { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; padding: 0.3rem 0; }
.secret-row .mono { word-break: break-all; }
.secret-warning { color: var(--warn); font-size: 0.8rem; margin-top: 0.5rem; }

/* ---------- drawer (details panel) ---------- */

.drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    z-index: 90;
}
.drawer-backdrop[hidden] { display: none; }
.drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(560px, 100vw);
    background: var(--surface);
    border-left: 1px solid var(--border);
    box-shadow: -8px 0 30px rgba(0, 0, 0, 0.3);
    z-index: 95;
    overflow-y: auto;
    padding: 1.25rem;
}
.drawer-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1rem; }
.drawer-tabs { display: flex; gap: 0.4rem; border-bottom: 1px solid var(--border); margin: 1rem 0 0.9rem; }

/* ---------- toast ---------- */

.toast-container {
    position: fixed;
    bottom: 1.25rem;
    right: 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    z-index: 200;
    max-width: 360px;
}
.toast {
    background: var(--surface);
    border: 1px solid var(--border);
    border-left: 4px solid var(--info);
    border-radius: 6px;
    padding: 0.6rem 0.85rem;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
    font-size: 0.85rem;
}
.toast.ok { border-left-color: var(--ok); }
.toast.bad { border-left-color: var(--bad); }
.toast.warn { border-left-color: var(--warn); }

/* ---------- device / user summary cards ---------- */

.device-card { display: flex; flex-direction: column; gap: 0.6rem; }
.device-card .device-title { display: flex; align-items: center; justify-content: space-between; }
.device-card .device-name { font-weight: 600; }
.mini-dl { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 0.5rem 1rem; font-size: 0.82rem; }
.mini-dl dt { font-size: 0.68rem; }

.device-groups { display: flex; flex-direction: column; gap: 0.3rem; }
.device-group-title {
    font-size: 0.68rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--muted);
    margin: 0.55rem 0 0.3rem;
    padding-top: 0.4rem;
    border-top: 1px solid var(--border);
}
.device-group-title:first-child { margin-top: 0; padding-top: 0; border-top: none; }
.data-source-note { color: var(--muted); font-size: 0.7rem; }

/* stale/annotated value inline hint, e.g. WAN IP carried forward */
.stale-note { color: var(--warn); font-size: 0.72rem; display: block; margin-top: 0.1rem; }

/* ---------- trend chart (inline SVG) ---------- */

.chart-wrap { width: 100%; overflow-x: auto; }
.chart-legend { display: flex; gap: 1rem; font-size: 0.78rem; color: var(--muted); margin-bottom: 0.4rem; flex-wrap: wrap; }
.chart-legend .legend-item { display: flex; align-items: center; gap: 0.35rem; }
.legend-swatch { width: 0.7rem; height: 0.7rem; border-radius: 2px; display: inline-block; }

.loading-inline { color: var(--muted); font-size: 0.85rem; }

/* ---------- Dashboard V2: alerts + mini charts ---------- */

.alert-list { display: flex; flex-direction: column; gap: 0.5rem; }
.alert-row {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    padding: 0.6rem 0.75rem;
    border-radius: 6px;
    border: 1px solid var(--border);
    background: var(--surface-2);
    font-size: 0.85rem;
}
.alert-row.bad { border-color: var(--bad); background: var(--bad-bg); }
.alert-row.warn { border-color: var(--warn); background: var(--warn-bg); }
.alert-row .alert-body { flex: 1; min-width: 0; }
.alert-row .alert-category { font-weight: 600; }
.alert-row .alert-message { color: var(--muted); margin-top: 0.1rem; }
.alert-row .alert-time { color: var(--muted); font-size: 0.72rem; white-space: nowrap; }

.mini-charts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; }
.mini-chart h3 { margin-bottom: 0.3rem; }
@media (max-width: 900px) {
    .mini-charts { grid-template-columns: 1fr; }
}

.devices-overview-card { display: flex; flex-direction: column; gap: 0.5rem; padding: 0.9rem 1rem; cursor: pointer; }
.devices-overview-card:hover { background: var(--surface-hover); }
.devices-overview-card .device-title { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }

/* ---------- Hub / port cards ---------- */

.port-card { display: flex; flex-direction: column; gap: 0.6rem; }
.port-card .device-title { display: flex; align-items: center; justify-content: space-between; }
.port-card select { max-width: 220px; }

/* ---------- operation log details ---------- */

.log-detail-pre {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.75rem;
    font-size: 0.78rem;
    white-space: pre-wrap;
    word-break: break-word;
    max-height: 40vh;
    overflow-y: auto;
    margin: 0;
}
.summary-cell { max-width: 360px; white-space: normal; }

@media (max-width: 640px) {
    table.responsive-stack thead { display: none; }
    table.responsive-stack, table.responsive-stack tbody, table.responsive-stack tr, table.responsive-stack td { display: block; width: 100%; }
    table.responsive-stack tr { border: 1px solid var(--border); border-radius: 6px; margin-bottom: 0.6rem; padding: 0.4rem 0.6rem; }
    table.responsive-stack td { border-bottom: none; padding: 0.25rem 0; white-space: normal; }
    table.responsive-stack td::before { content: attr(data-label); display: block; font-size: 0.68rem; color: var(--muted); text-transform: uppercase; }
    .modal { max-width: 100%; }
    .drawer { width: 100vw; }
}
