:root {
    --bg: #f4f6f9;
    --surface: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --muted: #64748b;
    --primary: #0f766e;
    --primary-hover: #115e59;
    --hot: #dc2626;
    --cold: #0d9488;
    --air: #2563eb;
    --danger: #dc2626;
    --danger-bg: #fef2f2;
    --success: #15803d;
    --success-bg: #f0fdf4;
    --warn: #b45309;
    --warn-bg: #fffbeb;
    --radius: 12px;
    --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
}

*, *::before, *::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

a {
    color: var(--primary);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

.container {
    width: 100%;
    max-width: 1220px;
    margin: 0 auto;
    padding: 0 16px;
}

/* Topbar */
.topbar {
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 20;
}

.topbar-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    min-height: 64px;
    flex-wrap: wrap;
    padding-top: 8px;
    padding-bottom: 8px;
}

.brand-logo {
    height: 36px;
    width: auto;
    display: block;
}

.nav {
    display: flex;
    gap: 8px;
    align-items: center;
}

.nav-link {
    padding: 8px 14px;
    border-radius: 8px;
    color: var(--muted);
    font-weight: 500;
    font-size: 0.95rem;
}

.nav-link:hover {
    background: var(--bg);
    color: var(--text);
    text-decoration: none;
}

.nav-link.active {
    background: #ccfbf1;
    color: var(--primary-hover);
}

.user-bar {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 0.9rem;
}

.user-name {
    font-weight: 600;
    color: var(--text);
}

/* Main & Footer */
.main-content {
    flex: 1;
    padding-top: 24px;
    padding-bottom: 40px;
}

.footer {
    border-top: 1px solid var(--border);
    background: var(--surface);
    padding: 16px 0;
    font-size: 0.85rem;
    color: var(--muted);
    text-align: center;
}

/* Typography & Layout */
.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
    margin-bottom: 20px;
}

.page-title {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 700;
}

.page-subtitle {
    margin: 4px 0 0;
    color: var(--muted);
    font-size: 0.92rem;
}

.actions-row {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
    align-items: center;
}

/* Cards */
.card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 20px;
    margin-bottom: 20px;
}

.card-title {
    margin: 0 0 14px;
    font-size: 1.1rem;
    font-weight: 600;
}

/* Alerts */
.alert {
    padding: 12px 16px;
    border-radius: 10px;
    margin-bottom: 16px;
    font-size: 0.95rem;
    border: 1px solid transparent;
}

.alert-success {
    background: var(--success-bg);
    color: var(--success);
    border-color: #bbf7d0;
}

.alert-error {
    background: var(--danger-bg);
    color: var(--danger);
    border-color: #fecaca;
}

.alert-warning {
    background: var(--warn-bg);
    color: var(--warn);
    border-color: #fde68a;
}

/* Badges */
.badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 3px 10px;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    white-space: nowrap;
}

.badge-online {
    background: #dcfce7;
    color: #166534;
}

.badge-online::before {
    content: "";
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #22c55e;
}

.badge-offline {
    background: #fee2e2;
    color: #991b1b;
}

.badge-offline::before {
    content: "";
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #ef4444;
}

.badge-admin {
    background: #e0e7ff;
    color: #3730a3;
}

.badge-muted {
    background: #f1f5f9;
    color: #475569;
}

/* Buttons */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 9px 16px;
    border-radius: 8px;
    font-size: 0.92rem;
    font-weight: 600;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    text-decoration: none;
}

.btn:hover {
    text-decoration: none;
}

.btn-primary {
    background: var(--primary);
    color: #ffffff;
}

.btn-primary:hover {
    background: var(--primary-hover);
}

.btn-outline {
    background: var(--surface);
    color: var(--text);
    border-color: var(--border);
}

.btn-outline:hover {
    background: var(--bg);
}

.btn-danger {
    background: var(--danger);
    color: #ffffff;
}

.btn-danger:hover {
    background: #b91c1c;
}

.btn-sm {
    padding: 6px 11px;
    font-size: 0.83rem;
}

/* Forms */
.form-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 14px;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin-bottom: 12px;
}

.form-group label {
    font-size: 0.86rem;
    font-weight: 600;
    color: var(--text);
}

.form-control {
    width: 100%;
    padding: 9px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font-size: 0.95rem;
    background: var(--surface);
    color: var(--text);
}

.form-control:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(15, 118, 110, 0.14);
}

.checkbox-row {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.9rem;
    margin: 10px 0 16px;
}

/* Auth box */
.auth-wrapper {
    max-width: 420px;
    margin: 40px auto;
}

.auth-wrapper h1 {
    margin: 0 0 4px;
    font-size: 1.45rem;
}

.auth-wrapper p {
    margin: 0 0 20px;
    color: var(--muted);
    font-size: 0.92rem;
}

/* Device list */
.location-group {
    margin-bottom: 24px;
}

.city-heading {
    font-size: 1.15rem;
    font-weight: 700;
    margin: 0 0 10px;
    padding-bottom: 6px;
    border-bottom: 2px solid var(--border);
}

.district-heading {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    margin: 14px 0 10px;
}

.device-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
    gap: 16px;
}

.device-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    gap: 12px;
    transition: transform 0.12s, border-color 0.12s;
    color: inherit;
}

.device-card:hover {
    transform: translateY(-2px);
    border-color: var(--primary);
    text-decoration: none;
}

.device-card-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
}

.device-address {
    font-weight: 700;
    font-size: 1.05rem;
    color: var(--text);
}

.device-name {
    font-size: 0.84rem;
    color: var(--muted);
}

.mini-temps {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.mini-temp {
    background: var(--bg);
    border-radius: 8px;
    padding: 8px;
    font-size: 0.8rem;
}

.mini-temp-label {
    color: var(--muted);
    font-size: 0.72rem;
    display: block;
}

.mini-temp-val {
    font-weight: 700;
    font-size: 0.92rem;
}

.t-hot { color: var(--hot); }
.t-cold { color: var(--cold); }
.t-air { color: var(--air); }

.device-card-foot {
    display: flex;
    justify-content: space-between;
    font-size: 0.8rem;
    color: var(--muted);
    border-top: 1px solid var(--border);
    padding-top: 8px;
}

/* Reading page */
.summary-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    gap: 14px;
    margin-bottom: 20px;
}

.metric-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    gap: 14px;
}

.metric-icon {
    width: 38px;
    height: 38px;
    flex-shrink: 0;
}

.metric-label {
    font-size: 0.82rem;
    color: var(--muted);
}

.metric-value {
    font-size: 1.45rem;
    font-weight: 700;
    line-height: 1.2;
}

.metric-sub {
    font-size: 0.78rem;
    color: var(--muted);
    margin-top: 2px;
}

.range-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    align-items: center;
    margin-bottom: 20px;
}

.range-pill {
    padding: 7px 14px;
    border-radius: 999px;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    font-weight: 600;
    font-size: 0.86rem;
}

.range-pill:hover {
    background: var(--bg);
    text-decoration: none;
}

.range-pill.active {
    background: var(--primary);
    color: #ffffff;
    border-color: var(--primary);
}

.chart-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    margin-bottom: 20px;
}

.chart-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 12px;
}

.chart-header h2 {
    margin: 0;
    font-size: 1.08rem;
}

.chart-stats {
    font-size: 0.83rem;
    color: var(--muted);
}

.chart-canvas-wrap {
    position: relative;
    height: 300px;
    width: 100%;
}

/* Tabs & Tables (Admin) */
.tabs {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    border-bottom: 1px solid var(--border);
    margin-bottom: 20px;
}

.tab-link {
    padding: 10px 16px;
    font-weight: 600;
    font-size: 0.92rem;
    color: var(--muted);
    border-bottom: 2px solid transparent;
    margin-bottom: -1px;
}

.tab-link:hover {
    color: var(--text);
    text-decoration: none;
}

.tab-link.active {
    color: var(--primary);
    border-bottom-color: var(--primary);
}

.table-wrap {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.9rem;
}

th, td {
    text-align: left;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

th {
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: var(--muted);
    background: #f8fafc;
}

.code-inline {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.8rem;
    background: #f1f5f9;
    padding: 3px 6px;
    border-radius: 5px;
    word-break: break-all;
}

.code-block {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 0.82rem;
    background: #0f172a;
    color: #e2e8f0;
    padding: 14px;
    border-radius: 8px;
    overflow-x: auto;
    white-space: pre;
}

.search-input {
    max-width: 320px;
}

@media (max-width: 640px) {
    .chart-canvas-wrap {
        height: 240px;
    }
    .user-name {
        display: none;
    }
}
