/* === Reset & Base === */
*,
*::before,
*::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg-primary: #000000;
    --bg-secondary: #0a0a0a;
    --bg-tertiary: #141414;
    --bg-row-hover: #1e1e1e;
    --border: #2d2d2d;
    --text-primary: #f8f8f2;
    --text-secondary: #a9a9a9;
    --text-muted: #75715e;
    --accent: #66d9ef;
    --green: #a6e22e;
    --yellow: #e6db74;
    --red: #f92672;
    --orange: #fd971f;
    --font-mono: 'JetBrains Mono', 'Fira Code', 'Cascadia Code', 'Consolas', monospace;
    --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
}

body {
    background: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-sans);
    font-size: 13px;
    line-height: 1.4;
    overflow-x: hidden;
}

.sticky {
    position: sticky;
    top: 0;
    z-index: 100;
}

/* === Header === */
.header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 16px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
}

.header-left {
    display: flex;
    align-items: center;
    gap: 16px;
}

.header h1 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-primary);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.connection-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--red);
    transition: background 0.3s;
}

.connection-dot.connected {
    background: var(--green);
    box-shadow: 0 0 6px var(--green);
}

/* === Controls bar === */
.controls {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 8px 16px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    flex-wrap: wrap;
}

.btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 4px 12px;
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s;
}

.btn:hover {
    background: var(--bg-row-hover);
    color: var(--text-primary);
    border-color: var(--text-muted);
}

.filter-input {
    padding: 4px 10px;
    background: var(--bg-tertiary);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12px;
    outline: none;
    width: 200px;
    transition: border-color 0.15s;
}

.filter-input:focus {
    border-color: var(--accent);
}

.filter-input::placeholder {
    color: var(--text-muted);
}

.meta-info {
    color: var(--text-muted);
    font-size: 11px;
    margin-left: auto;
}

/* === Summary bar === */
.summary {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
    padding: 6px 16px;
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border);
    font-size: 11px;
    color: var(--text-secondary);
}

.summary span {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.summary .label {
    color: var(--text-muted);
}

/* === Table === */
.table-wrapper {
    position: relative;
    overflow-x: auto;
    overflow-y: auto;
    max-width: 100%;
    /* Height is set dynamically by JS to enable sticky thead + tfoot within this container */
}

table {
    width: 100%;
    border-collapse: collapse;
    font-family: var(--font-mono);
    font-size: 12px;
    white-space: nowrap;
}

thead {
    /* We apply sticky to the th elements instead of the thead to prevent overlap bugs */
    z-index: 10;
}

th {
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    font-weight: 500;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    padding: 6px 10px;
    text-align: left;
    /* Removed normal border as it gets hidden behind scrolling rows when sticky */
    border: none;
    /* Use an inset box-shadow instead so the line renders visually on top of everything */
    box-shadow: inset 0 -2px 0 var(--border);
    cursor: pointer;
    user-select: none;
    transition: color 0.15s;
    /* Make the individual cells sticky */
    position: sticky;
    top: 0;
    z-index: 10;
    /* Force background color so rows behind it don't show through */
    background-color: var(--bg-secondary);
}

th:hover {
    color: var(--text-primary);
}

th.sorted-asc::after,
th.sorted-desc::after {
    position: absolute;
    right: 4px;
    color: var(--accent);
    font-size: 10px;
}

th.sorted-asc::after {
    content: '▲';
}

th.sorted-desc::after {
    content: '▼';
}

td {
    padding: 5px 10px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
}

tr:nth-child(even) {
    background: var(--bg-secondary);
}

tr:nth-child(odd) {
    background: var(--bg-primary);
}

tr:hover {
    background: var(--bg-row-hover) !important;
}

tr.offline {
    background: rgba(249, 38, 114, 0.15) !important;
    opacity: 0.6;
}

/* === Totals row === */
.totals-row {
    background: var(--bg-tertiary) !important;
    position: sticky;
    bottom: -1px;
    z-index: 5;
}

.totals-row td {
    border-bottom: none;
    color: var(--text-primary);
    font-size: 12px;
    /* Use an inset box-shadow instead of border-top on the tr so it renders visually when sticky */
    box-shadow: inset 0 2px 0 var(--accent);
}

/* === Status badge === */
.status-badge {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 6px;
    vertical-align: middle;
}

.status-badge.online {
    background: var(--green);
    box-shadow: 0 0 4px var(--green);
}

.status-badge.offline {
    background: var(--red);
    box-shadow: 0 0 4px var(--red);
}

/* === Color-coded values === */
.val-good {
    color: var(--green);
}

.val-warn {
    color: var(--yellow);
}

.val-danger {
    color: var(--red);
}

.val-muted {
    color: var(--text-muted);
}

.server-name {
    color: var(--accent);
    font-weight: 500;
    font-family: var(--font-sans);
}

.cpu-model {
    color: var(--text-secondary);
    font-family: var(--font-sans);
    font-size: 11px;
    max-width: 280px;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* === Empty state === */
.empty-state {
    text-align: center;
    padding: 80px 20px;
    color: var(--text-muted);
}

.empty-state h2 {
    font-size: 18px;
    margin-bottom: 8px;
    color: var(--text-secondary);
}

.empty-state p {
    font-size: 13px;
}

/* === Pulse animation for live values === */
@keyframes pulse {

    0%,
    100% {
        opacity: 1;
    }

    50% {
        opacity: 0.6;
    }
}

.updating {
    animation: pulse 0.5s ease-in-out;
}

/* === Scrollbar === */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* === Responsive Grid Layout === */
@media screen and (max-width: 1024px) {

    /* Hide the normal table header */
    table thead {
        display: none;
    }

    table,
    tbody,
    tr,
    td {
        display: block;
        width: 100%;
    }

    .table-wrapper {
        padding: 10px;
        max-width: 100vw;
        overflow-x: auto;
        box-sizing: border-box;
    }

    /* Make rows look like cards */
    tr {
        margin-bottom: 16px;
        border: 1px solid var(--border);
        border-radius: 8px;
        background: var(--bg-secondary) !important;
        display: grid;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        /* 2 columns grid for data */
        gap: 8px;
        padding: 12px;
        box-sizing: border-box;
    }

    tr.totals-row {
        position: static;
        grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
        background: var(--bg-tertiary) !important;
    }

    /* The first TD (name/status) can span full width */
    td:first-child {
        grid-column: 1 / -1;
        font-size: 14px;
        font-weight: bold;
        border-bottom: 1px solid var(--border);
        padding-bottom: 8px;
        margin-bottom: 4px;
        display: flex;
        flex-direction: row;
        align-items: center;
        text-align: left;
    }

    td:first-child::before {
        content: none;
    }

    /* Style the other TDs to show their data-labels */
    td {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: flex-start;
        padding: 4px 8px;
        border: none;
        text-align: left;
        white-space: normal;
        overflow-wrap: anywhere;
        min-width: 0;
        box-sizing: border-box;
    }

    td::before {
        content: attr(data-label);
        font-weight: 600;
        text-transform: uppercase;
        font-size: 10px;
        color: var(--text-muted);
        margin-bottom: 2px;
    }

    /* Hide empty TDs like the spacer ones in the totals row */
    td.hidden-mobile {
        display: none !important;
    }

    th {
        display: none;
    }
}

@media screen and (max-width: 600px) {
    tr {
        grid-template-columns: minmax(0, 1fr);
        /* single column on narrow mobile */
    }

    tr.totals-row {
        grid-template-columns: minmax(0, 1fr);
    }
}