/* =========================================================
   IoT Sensor Dashboard - Dark monitoring theme
   ========================================================= */

:root {
    --bg: #0f1923;
    --surface: #1a2736;
    --surface-hover: #223344;
    --border: #2a3a4a;
    --text: #d8dee9;
    --text-muted: #7a8a9a;
    --accent: #5e9ed6;
    --accent-hover: #7ab4e8;
    --temp-color: #e5736a;
    --humidity-color: #5eb8c4;
    --success: #6abf69;
    --warning: #e5a84b;
    --danger: #d9534f;
    --radius: 8px;
    --gap: 1rem;
}

*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
}

/* ---- Header & Nav ---- */

header {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 1.5rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 0.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
}

header h1 {
    font-size: 1.1rem;
    font-weight: 600;
    white-space: nowrap;
}

nav {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.tab {
    background: none;
    border: none;
    color: var(--text-muted);
    padding: 0.5rem 1rem;
    cursor: pointer;
    border-radius: var(--radius);
    font-size: 0.875rem;
    transition: all 0.2s;
}

.tab:hover {
    color: var(--text);
    background: var(--surface-hover);
}

.tab.active {
    color: var(--accent);
    background: var(--surface-hover);
    font-weight: 600;
}

.refresh-btn {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    background: none;
    border: 1px solid var(--border);
    color: var(--success);
    padding: 0.35rem 0.75rem;
    border-radius: var(--radius);
    cursor: pointer;
    font-size: 0.8rem;
    margin-left: 0.5rem;
    transition: all 0.2s;
}

.refresh-btn.paused {
    color: var(--text-muted);
}

.pulse-dot {
    width: 8px;
    height: 8px;
    background: var(--success);
    border-radius: 50%;
    display: inline-block;
    animation: pulse 2s ease-in-out infinite;
}

.refresh-btn.paused .pulse-dot {
    background: var(--text-muted);
    animation: none;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.3; }
}

/* ---- Main content ---- */

main {
    max-width: 1200px;
    margin: 0 auto;
    padding: var(--gap);
}

.tab-content {
    display: none;
}

.tab-content.active {
    display: block;
}

/* ---- Cards ---- */

.cards-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: var(--gap);
    margin-bottom: var(--gap);
}

.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.25rem;
}

.card h3 {
    font-size: 0.9rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.card h3 small {
    font-weight: 400;
    text-transform: none;
    letter-spacing: normal;
}

.stat-row {
    display: flex;
    justify-content: space-between;
    padding: 0.35rem 0;
    border-bottom: 1px solid var(--border);
}

.stat-row:last-child {
    border-bottom: none;
}

.stat-label {
    color: var(--text-muted);
    font-size: 0.875rem;
}

.stat-value {
    font-weight: 600;
    font-size: 0.875rem;
}

.stat-value.ok {
    color: var(--success);
}

/* ---- Section titles ---- */

.section-title {
    font-size: 1rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

/* ---- Latest reading cards ---- */

.readings-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: var(--gap);
}

.reading-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem 1.25rem;
}

.reading-card .device-name {
    font-weight: 600;
    font-size: 0.95rem;
    margin-bottom: 0.25rem;
}

.reading-card .device-location {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.75rem;
}

.reading-values {
    display: flex;
    gap: 1.5rem;
    margin-bottom: 0.5rem;
}

.reading-val {
    display: flex;
    flex-direction: column;
}

.reading-val .label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.reading-val .value {
    font-size: 1.5rem;
    font-weight: 700;
}

.reading-val .value.temp {
    color: var(--temp-color);
}

.reading-val .value.humidity {
    color: var(--humidity-color);
}

.reading-card .timestamp {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ---- Stats card content ---- */

.stats-grid {
    display: grid;
    gap: 0.5rem;
}

.stats-device {
    padding: 0.5rem 0;
    border-bottom: 1px solid var(--border);
}

.stats-device:last-child {
    border-bottom: none;
}

.stats-device .name {
    font-weight: 600;
    font-size: 0.85rem;
    margin-bottom: 0.25rem;
}

.stats-device .values {
    display: flex;
    gap: 1rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    flex-wrap: wrap;
}

.stats-device .values span {
    white-space: nowrap;
}

/* ---- Charts ---- */

.chart-controls {
    display: flex;
    gap: 1rem;
    margin-bottom: 1rem;
    flex-wrap: wrap;
}

.chart-controls label {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.chart-controls select {
    background: var(--surface);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.5rem 0.75rem;
    font-size: 0.875rem;
    cursor: pointer;
}

.chart-controls select:focus {
    outline: none;
    border-color: var(--accent);
}

.chart-container {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1rem;
    margin-bottom: var(--gap);
}

.chart-container h3 {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.chart-container canvas {
    width: 100% !important;
    max-height: 300px;
}

/* ---- Tables ---- */

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    font-size: 0.875rem;
}

thead {
    background: var(--surface-hover);
}

th {
    text-align: left;
    padding: 0.75rem 1rem;
    font-weight: 600;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

td {
    padding: 0.75rem 1rem;
    border-top: 1px solid var(--border);
}

tr:hover td {
    background: var(--surface-hover);
}

.status-badge {
    display: inline-block;
    padding: 0.15rem 0.5rem;
    border-radius: 999px;
    font-size: 0.75rem;
    font-weight: 600;
}

.status-badge.active {
    background: rgba(106, 191, 105, 0.15);
    color: var(--success);
}

.status-badge.inactive {
    background: rgba(122, 138, 154, 0.15);
    color: var(--text-muted);
}

/* ---- Buttons ---- */

.btn {
    padding: 0.5rem 1rem;
    border-radius: var(--radius);
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    cursor: pointer;
    font-size: 0.875rem;
    transition: all 0.2s;
}

.btn:hover {
    background: var(--surface-hover);
}

.btn-primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
}

.btn-primary:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
}

.btn-sm {
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem;
}

.btn-danger {
    color: var(--danger);
    border-color: var(--danger);
    background: transparent;
}

.btn-danger:hover {
    background: rgba(217, 83, 79, 0.15);
}

.actions-cell {
    display: flex;
    gap: 0.4rem;
}

/* ---- Modal ---- */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}

.modal {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 1.5rem;
    width: 90%;
    max-width: 420px;
}

.modal h3 {
    margin-bottom: 1rem;
    font-size: 1.1rem;
}

.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.form-group input {
    width: 100%;
    padding: 0.5rem 0.75rem;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    color: var(--text);
    font-size: 0.875rem;
}

.form-group input:focus {
    outline: none;
    border-color: var(--accent);
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 1.25rem;
}

/* ---- Empty states ---- */

.empty-msg {
    color: var(--text-muted);
    font-size: 0.875rem;
    padding: 1rem 0;
}

/* ---- Toast notifications ---- */

.toast {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.75rem 1.25rem;
    font-size: 0.875rem;
    z-index: 300;
    animation: slideIn 0.3s ease;
    max-width: 340px;
}

.toast.error {
    border-color: var(--danger);
    color: var(--danger);
}

.toast.success {
    border-color: var(--success);
    color: var(--success);
}

@keyframes slideIn {
    from { transform: translateY(1rem); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

/* ---- Responsive ---- */

@media (max-width: 640px) {
    header {
        padding: 0.5rem 1rem;
    }

    header h1 {
        font-size: 0.95rem;
    }

    .tab {
        padding: 0.4rem 0.6rem;
        font-size: 0.8rem;
    }

    main {
        padding: 0.75rem;
    }

    .cards-row {
        grid-template-columns: 1fr;
    }

    .readings-grid {
        grid-template-columns: 1fr;
    }

    /* Table to card layout on mobile */
    #devices-table thead {
        display: none;
    }

    #devices-table tbody tr {
        display: block;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: var(--radius);
        margin-bottom: 0.5rem;
        padding: 0.75rem;
    }

    #devices-table td {
        display: flex;
        justify-content: space-between;
        padding: 0.3rem 0;
        border: none;
    }

    #devices-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-muted);
        font-size: 0.8rem;
    }

    .actions-cell {
        justify-content: flex-end;
    }

    .chart-container canvas {
        max-height: 220px;
    }
}
