/* ── Variables ───────────────────────────────────────────────────────────── */
:root {
    --primary:       #1976D2;
    --primary-dark:  #1565C0;
    --primary-light: #BBDEFB;
    --accent:        #0288D1;
    --bg:            #F0F2F5;
    --surface:       #FFFFFF;
    --surface-2:     #F7F9FC;
    --text:          #212121;
    --text-muted:    #757575;
    --border:        #E0E0E0;
    --navbar-bg:     #1565C0;
    --navbar-text:   #FFFFFF;
    --success:       #4CAF50;
    --error:         #F44336;
    --warning:       #FF9800;
    --sidebar-w:     280px;
    --navbar-h:      52px;
    --detail-w:      300px;
    --radius:        8px;
}

/* ── Reset ───────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
    height: 100%;
    font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
    font-size: 14px;
    color: var(--text);
    background: var(--bg);
}

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

/* ── Navbar ──────────────────────────────────────────────────────────────── */
.navbar {
    height: var(--navbar-h);
    background: var(--navbar-bg);
    color: var(--navbar-text);
    display: flex;
    align-items: center;
    padding: 0 1.25rem;
    gap: 1.5rem;
    position: sticky;
    top: 0;
    z-index: 100;
    box-shadow: 0 2px 4px rgba(0,0,0,0.2);
    flex-shrink: 0;
}

.navbar-brand {
    color: var(--navbar-text);
    font-size: 1.1rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
}

.navbar-links {
    display: flex;
    align-items: center;
    gap: 0.25rem;
    margin-left: auto;
}

.nav-link {
    color: rgba(255,255,255,0.85);
    padding: 0.35rem 0.75rem;
    border-radius: 4px;
    font-size: 0.9rem;
    transition: background 0.15s;
    text-decoration: none;
}
.nav-link:hover, .nav-link.active {
    background: rgba(255,255,255,0.15);
    color: #fff;
    text-decoration: none;
}

.nav-clock {
    font-family: 'Consolas', 'Menlo', monospace;
    font-size: 1rem;
    font-weight: 600;
    color: rgba(255,255,255,0.9);
    letter-spacing: 1px;
    margin-left: 0.75rem;
}

.nav-user {
    color: rgba(255,255,255,0.7);
    font-size: 0.85rem;
    padding: 0 0.5rem;
    border-left: 1px solid rgba(255,255,255,0.2);
    border-right: 1px solid rgba(255,255,255,0.2);
    margin: 0 0.25rem;
    text-decoration: none;
}
.nav-user:hover { color: #fff; }

/* ── Profile page ────────────────────────────────────────────────────────── */
.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

.profile-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 1.5rem;
    padding: 0.75rem 0;
    margin: 0.5rem 0 1rem;
    border-top: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    font-size: 0.85rem;
    color: var(--text-muted);
}

.form-actions {
    display: flex;
    justify-content: flex-end;
    padding-top: 0.5rem;
}

.required {
    color: var(--danger, #E53935);
}

.profile-stats {
    display: flex;
    gap: 1rem;
    flex-wrap: wrap;
}

.stat-box {
    flex: 1;
    min-width: 100px;
    background: var(--bg, #F5F5F5);
    border: 1px solid var(--border, #E0E0E0);
    border-radius: 6px;
    padding: 0.75rem 1rem;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-value {
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--primary, #1565C0);
    line-height: 1.2;
}

.stat-label {
    font-size: 0.78rem;
    color: var(--text-muted, #757575);
    margin-top: 0.2rem;
}

.fetch-ok  { color: #2E7D32; }
.fetch-err { color: #C62828; }

/* ── Flash messages ──────────────────────────────────────────────────────── */
.flash-container {
    padding: 0.5rem 1.25rem;
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.flash {
    padding: 0.6rem 1rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    border-left: 4px solid;
}
.flash-success  { background: #E8F5E9; border-color: var(--success); color: #2E7D32; }
.flash-error    { background: #FFEBEE; border-color: var(--error);   color: #C62828; }
.flash-warning  { background: #FFF8E1; border-color: var(--warning); color: #E65100; }
.flash-info     { background: #E3F2FD; border-color: var(--primary); color: #0D47A1; }

/* ── Auth pages ──────────────────────────────────────────────────────────── */
.auth-page { display: flex; flex-direction: column; }

.auth-container {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background: linear-gradient(135deg, #1565C0 0%, #0288D1 100%);
}

.auth-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
    width: 100%;
    max-width: 520px;
}

.auth-wrapper .auth-card {
    width: 100%;
    max-width: 380px;
}

.stats-strip {
    width: 100%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
}

.stats-strip-item {
    background: rgba(255,255,255,0.12);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 8px;
    padding: 0.65rem 0.75rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.stats-strip-value {
    font-size: 1.25rem;
    font-weight: 700;
    color: #fff;
    line-height: 1.2;
}

.stats-strip-label {
    font-size: 0.7rem;
    color: rgba(255,255,255,0.75);
    margin-top: 0.2rem;
    text-transform: uppercase;
    letter-spacing: 0.03em;
}

.auth-card {
    background: var(--surface);
    border-radius: 12px;
    padding: 2.5rem;
    width: 100%;
    max-width: 380px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.15);
}

.auth-title {
    text-align: center;
    font-size: 1.6rem;
    font-weight: 700;
    color: var(--primary);
    margin-bottom: 0.25rem;
}

.auth-subtitle {
    text-align: center;
    font-size: 1rem;
    color: var(--text-muted);
    margin-bottom: 1.75rem;
    font-weight: 400;
}

.auth-link {
    text-align: center;
    margin-top: 1rem;
    font-size: 0.875rem;
    color: var(--text-muted);
}

.auth-note {
    text-align: center;
    margin-top: 0.75rem;
    font-size: 0.8rem;
    color: var(--text-muted);
    background: #FFF8E1;
    padding: 0.5rem;
    border-radius: 4px;
}

/* ── Form elements ───────────────────────────────────────────────────────── */
.form-group {
    margin-bottom: 1rem;
}

.form-group label {
    display: block;
    font-size: 0.8rem;
    font-weight: 600;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-bottom: 0.35rem;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 0.55rem 0.75rem;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 0.9rem;
    color: var(--text);
    background: var(--surface);
    transition: border-color 0.15s, box-shadow 0.15s;
    outline: none;
}

.form-group input:focus,
.form-group select:focus {
    border-color: var(--primary);
    box-shadow: 0 0 0 3px var(--primary-light);
}

.input-sm {
    width: 100%;
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.85rem;
    color: var(--text);
    background: var(--surface);
    outline: none;
}
.input-sm:focus { border-color: var(--primary); }

.select-sm {
    padding: 0.3rem 0.5rem;
    border: 1px solid var(--border);
    border-radius: 4px;
    font-size: 0.85rem;
    background: var(--surface);
    outline: none;
    cursor: pointer;
}

.form-row {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem 1.1rem;
    border: none;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s, opacity 0.15s;
    text-decoration: none;
    gap: 0.4rem;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.6; cursor: default; }

.btn-primary  { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-dark); text-decoration: none; }

.btn-secondary { background: #E3F2FD; color: var(--primary); }
.btn-secondary:hover:not(:disabled) { background: var(--primary-light); text-decoration: none; }

.btn-ghost    { background: transparent; color: var(--text-muted); border: 1px solid var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); text-decoration: none; }

.btn-danger   { background: #FFEBEE; color: var(--error); }
.btn-danger:hover:not(:disabled) { background: #FFCDD2; text-decoration: none; }

.btn-warning  { background: #FFF8E1; color: #E65100; }
.btn-warning:hover:not(:disabled) { background: #FFE082; text-decoration: none; }

.btn-success  { background: #E8F5E9; color: #2E7D32; }
.btn-success:hover:not(:disabled) { background: #C8E6C9; text-decoration: none; }

.btn-full     { width: 100%; }
.btn-sm       { padding: 0.3rem 0.7rem; font-size: 0.8rem; }

/* ── Map layout ──────────────────────────────────────────────────────────── */
body.map-page {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.map-layout {
    flex: 1;
    display: flex;
    overflow: hidden;
    position: relative;
}

.sidebar {
    width: var(--sidebar-w);
    background: var(--surface);
    border-right: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
    display: flex;
    flex-direction: column;
}

.sidebar-fab,
.sidebar-backdrop,
.sidebar-mobile-header {
    display: none;
}

.sidebar-section {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.sidebar-title {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.radio-group {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
}

.quick-range-group {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 0.4rem;
}

.quick-range-btn.active {
    background: var(--primary);
    color: #fff;
    border-color: var(--primary);
}

.radio-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    cursor: pointer;
}

.device-item {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.875rem;
    cursor: pointer;
    padding: 0.2rem 0;
}

.device-color {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}

.status-text {
    font-size: 0.8rem;
    color: var(--text-muted);
    min-height: 1.2em;
}

#map {
    flex: 1;
    height: 100%;
    background: #E8EDF2;
    position: relative;
}

.map-warning {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #FFF8E1;
    border: 1px solid var(--warning);
    border-radius: var(--radius);
    padding: 1rem 1.5rem;
    text-align: center;
    max-width: 400px;
    color: #5D4037;
}

.detail-panel {
    width: var(--detail-w);
    background: var(--surface);
    border-left: 1px solid var(--border);
    overflow-y: auto;
    flex-shrink: 0;
}

.detail-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface-2);
}

.device-badge {
    color: white;
    padding: 0.25rem 0.6rem;
    border-radius: 999px;
    font-size: 0.8rem;
    font-weight: 600;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1rem;
    cursor: pointer;
    color: var(--text-muted);
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    line-height: 1;
}
.close-btn:hover { background: var(--surface); }

.detail-content { padding: 0.75rem 1rem; }

.detail-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding: 0.4rem 0;
    border-bottom: 1px solid var(--surface-2);
    font-size: 0.875rem;
}
.detail-row:last-child { border-bottom: none; }
.detail-row > span:first-child { color: var(--text-muted); font-size: 0.8rem; }
.detail-row > span:last-child  { font-weight: 500; }

.detail-section-label {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--primary);
    padding: 0.6rem 0 0.2rem;
    border-bottom: 1px solid var(--primary-light);
    margin-bottom: 0.2rem;
}

/* ── Dashboard ───────────────────────────────────────────────────────────── */
body.dashboard-page { display: flex; flex-direction: column; }

.dashboard-container {
    flex: 1;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    max-width: 1400px;
    width: 100%;
    margin: 0 auto;
}

.page-title {
    font-size: 1.4rem;
    font-weight: 600;
    color: var(--text);
}

.card {
    background: var(--surface);
    border-radius: var(--radius);
    border: 1px solid var(--border);
    padding: 1.25rem;
    box-shadow: 0 1px 3px rgba(0,0,0,0.06);
}

.card-title {
    font-size: 0.95rem;
    font-weight: 600;
    margin-bottom: 1rem;
    color: var(--text);
}

.card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 1rem;
}

.charts-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 1.5rem;
}

.filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    align-items: flex-end;
}

.filter-bar .form-group {
    margin-bottom: 0;
    min-width: 160px;
}

.filter-bar .filter-actions {
    display: flex;
    gap: 0.5rem;
    min-width: unset;
}

.charts-grid-single {
    grid-template-columns: 1fr;
}

.chart-wrapper {
    position: relative;
    height: 520px;
}

.chart-wrapper-compact {
    height: 320px;
}

.filter-bar input.input-sm {
    min-width: 0;
    width: 100%;
}

.mono {
    font-family: 'Consolas', 'Menlo', monospace;
    font-size: 0.82rem;
}
.mono.small { font-size: 0.75rem; color: var(--text-muted); }

.ago-badge {
    display: inline-block;
    padding: 0.2rem 0.55rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    white-space: nowrap;
}
.ago-ok   { background: #E8F5E9; color: #2E7D32; }
.ago-warn { background: #FFF8E1; color: #E65100; }
.ago-old  { background: #FFEBEE; color: #C62828; }
.env-temperature { background: #FFF3E0; color: #E65100; }
.env-light { background: #FFFDE7; color: #827717; }

.environment-values {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.35rem;
}

.environment-values > div {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    gap: 0.15rem;
    width: 100%;
}

.environment-values > div > span:first-child {
    color: var(--text-muted);
    font-size: 0.7rem;
}

.summary-stacked-values {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.summary-stacked-values > div {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
}

.summary-stacked-values span {
    color: var(--text-muted);
    font-size: 0.7rem;
}

.summary-stacked-values strong {
    font-size: 0.82rem;
    font-weight: 600;
}

.summary-time-footer {
    margin-top: 0.35rem;
}

.radio-values,
.lora-configuration-values {
    min-width: 110px;
}

.color-picker {
    position: relative;
}

.device-name-editor {
    display: flex;
    flex-direction: column;
    gap: 0.4rem;
    margin-bottom: 0.35rem;
}

.device-name-editor > div,
.device-name-controls {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.device-name-label {
    color: var(--text-muted);
    font-size: 0.68rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.device-name-controls input {
    width: min(180px, 100%);
    padding: 0.3rem 0.45rem;
    border: 1px solid var(--border);
    border-radius: 5px;
    font: inherit;
    font-size: 0.82rem;
}

.color-picker > summary {
    list-style: none;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.18rem 0.45rem;
    background: #fff;
}

.color-picker > summary::-webkit-details-marker {
    display: none;
}

.device-color-dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.18);
    display: inline-block;
}

.token-device-picker {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
    gap: 0.6rem;
}

.token-device-picker.is-disabled {
    opacity: 0.65;
}

.token-device-option {
    display: block;
    cursor: pointer;
}

.token-device-option input[type="checkbox"] {
    position: absolute;
    opacity: 0;
    pointer-events: none;
}

.token-device-option-body {
    display: flex;
    align-items: center;
    gap: 0.55rem;
    padding: 0.7rem 0.8rem;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #fff;
    min-height: 52px;
    transition: border-color 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.token-device-option-text {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    min-width: 0;
    flex: 1;
}

.token-device-option input[type="checkbox"]:checked + .token-device-option-body {
    border-color: var(--primary);
    background: var(--surface-2);
    box-shadow: 0 0 0 2px rgba(30, 136, 229, 0.12);
}

.token-device-option input[type="checkbox"]:focus-visible + .token-device-option-body {
    outline: 2px solid rgba(30, 136, 229, 0.45);
    outline-offset: 2px;
}

.color-grid {
    margin-top: 0.35rem;
    padding: 0.5rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fff;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.12);
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 0.35rem;
    width: 180px;
}

.color-chip {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.24);
    cursor: pointer;
}

.color-chip.selected {
    outline: 2px solid #263238;
    outline-offset: 1px;
}

/* ── Content pages (datasources) ─────────────────────────────────────────── */
body.content-page { display: flex; flex-direction: column; }

.content-container {
    flex: 1;
    padding: 1.5rem;
    max-width: 960px;
    width: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.content-container .card { margin-bottom: 0; }

/* ── Tables ──────────────────────────────────────────────────────────────── */
.table-wrapper { overflow-x: auto; }

.pagination-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    margin-bottom: 0.9rem;
    flex-wrap: wrap;
}

.pagination-controls {
    display: flex;
    align-items: center;
    gap: 0.6rem;
}

.device-summary-cards {
    display: none;
}

.data-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.875rem;
}

.data-table th {
    text-align: left;
    padding: 0.6rem 0.75rem;
    background: var(--surface-2);
    border-bottom: 2px solid var(--border);
    font-size: 0.78rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    white-space: nowrap;
}

.data-table td {
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid var(--border);
    vertical-align: middle;
}

.table-device-cell {
    display: flex;
    align-items: flex-start;
    gap: 0.6rem;
    min-width: 150px;
}

.table-device-meta {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    min-width: 0;
}

.table-device-board {
    color: var(--text-muted);
    font-size: 0.76rem;
    line-height: 1.2;
}

.table-location-cell {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.table-battery-cell {
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    align-items: flex-start;
}

.table-battery-voltage {
    color: var(--text-muted);
    font-size: 0.76rem;
    line-height: 1.2;
}

.table-environment-cell {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    font-size: 0.76rem;
    line-height: 1.2;
}

.table-status-cell {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    font-size: 0.76rem;
    line-height: 1.2;
}

.table-radio-cell {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    font-size: 0.76rem;
    line-height: 1.2;
}

.table-gateway-cell {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.table-lora-cell {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
    font-size: 0.76rem;
    line-height: 1.2;
}

.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--surface-2); }

.url-cell {
    max-width: 300px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-family: monospace;
    font-size: 0.8rem;
    color: var(--text-muted);
}

.action-cell { white-space: nowrap; display: flex; gap: 0.5rem; align-items: center; }

.row-disabled td { opacity: 0.5; }
.row-disabled td.action-cell { opacity: 1; }

.status-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    margin-right: 0.35rem;
    vertical-align: middle;
}
.dot-on  { background: var(--success); box-shadow: 0 0 0 2px #C8E6C9; }
.dot-off { background: #BDBDBD;        box-shadow: 0 0 0 2px #EEEEEE; }
.status-label { font-size: 0.8rem; color: var(--text-muted); vertical-align: middle; }

.label-hint { font-weight: 400; font-size: 0.78rem; color: var(--text-muted); text-transform: none; letter-spacing: 0; }

/* ── Modal ───────────────────────────────────────────────────────────────── */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 500;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal-card {
    background: var(--surface);
    border-radius: 10px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    width: 100%;
    max-width: 560px;
    overflow: hidden;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    background: var(--surface-2);
    border-bottom: 1px solid var(--border);
}

.modal-title { font-size: 1rem; font-weight: 600; }

.modal-card form { padding: 1.25rem; }

.modal-actions {
    display: flex;
    gap: 0.75rem;
    margin-top: 1.25rem;
}

/* ── Misc helpers ────────────────────────────────────────────────────────── */
.muted { color: var(--text-muted); font-size: 0.875rem; }

/* ── Toast notifications ─────────────────────────────────────────────────── */
.toast {
    position: fixed;
    bottom: 1.5rem;
    right: 1.5rem;
    padding: 0.75rem 1.25rem;
    border-radius: var(--radius);
    font-size: 0.875rem;
    font-weight: 500;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 9999;
    animation: slideIn 0.2s ease;
}
.toast.success { background: #E8F5E9; color: #2E7D32; border-left: 4px solid var(--success); }
.toast.error   { background: #FFEBEE; color: #C62828; border-left: 4px solid var(--error); }

@keyframes slideIn {
    from { transform: translateX(100%); opacity: 0; }
    to   { transform: translateX(0);   opacity: 1; }
}

/* ── Footer ──────────────────────────────────────────────────────────────── */
.site-footer {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.6rem 1rem;
    font-size: 0.75rem;
    color: var(--text-muted, #888);
    border-top: 1px solid var(--border, #e0e0e0);
    background: var(--surface, #fff);
}
.footer-version {
    font-family: monospace;
    background: var(--bg, #f5f5f5);
    border: 1px solid var(--border, #e0e0e0);
    border-radius: 4px;
    padding: 0 0.4rem;
    font-size: 0.7rem;
    color: var(--text-muted, #888);
}
.footer-sep  { color: var(--border, #ccc); }
.footer-license { opacity: 0.7; }

/* ── Tracker page ────────────────────────────────────────────────────────── */
body.tracker-page {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.tracker-layout {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

.tracker-filters {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    padding: 0.45rem 0.75rem;
    border-bottom: 1px solid var(--border);
    background: var(--surface);
}

.tracker-filter-label {
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    flex: 0 0 auto;
}

.tracker-device-filters {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.55rem;
}

.tracker-filter-item {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    font-size: 0.8rem;
    color: var(--text);
}

.tracker-filter-item input[type="checkbox"] {
    margin: 0;
}

/* Map takes top portion — fixed height so info panel is always visible */
.tracker-map-wrap {
    flex: 0 0 52%;
    position: relative;
    min-height: 200px;
}

#tracker-map {
    width: 100%;
    height: 100%;
}

/* Device chips bar overlaid on map bottom edge */
.device-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    padding: 0.4rem 0.6rem;
    background: linear-gradient(transparent, rgba(0,0,0,0.35));
    pointer-events: none;  /* clicks pass through to map */
}

.dev-chip {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    padding: 0.2rem 0.55rem;
    border-radius: 20px;
    border: 1.5px solid;
    background: rgba(255,255,255,0.92);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    pointer-events: all;
    white-space: nowrap;
    transition: transform 0.1s;
}
.dev-chip:active { transform: scale(0.95); }
.dev-chip-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    flex-shrink: 0;
}

/* Info panel — scrollable bottom area */
.tracker-info-wrap {
    flex: 1;
    overflow-y: auto;
    background: var(--bg);
    border-top: 2px solid var(--border);
    -webkit-overflow-scrolling: touch;
}

.tracker-info-placeholder {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    min-height: 80px;
}
.ti-hint {
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* Detail content */
.ti-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
    padding: 0.75rem 1rem 0.5rem;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 1;
}
.ti-badge {
    color: #fff;
    font-size: 0.8rem;
    font-weight: 700;
    padding: 0.25rem 0.65rem;
    border-radius: 20px;
}
.ti-time {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-left: auto;
}

/* 2-column card grid, collapses to 1 col on narrow screens */
.ti-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.6rem;
    padding: 0.75rem;
}

.ti-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 0.65rem 0.75rem;
}
.ti-card-title {
    font-size: 0.7rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.7px;
    color: var(--text-muted);
    margin-bottom: 0.45rem;
}
.ti-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
    padding: 0.18rem 0;
    border-bottom: 1px solid var(--bg);
    font-size: 0.82rem;
}
.ti-row:last-child { border-bottom: none; }
.ti-label { color: var(--text-muted); white-space: nowrap; }
.ti-value { font-weight: 600; text-align: right; }
.ti-unit  { font-weight: 400; color: var(--text-muted); font-size: 0.75rem; }

/* ── Responsive — tablet (≤ 900 px) ─────────────────────────────────────── */
@media (max-width: 900px) {
    .charts-grid { grid-template-columns: 1fr; }
    :root { --sidebar-w: 220px; --detail-w: 240px; }
}

/* ── Responsive — mobile (≤ 768 px) ─────────────────────────────────────── */
@media (max-width: 768px) {

    /* Two-row navbar: brand+clock on top, links scroll horizontally below */
    :root { --navbar-h: 88px; }  /* 52px top row + 36px links row */

    .navbar {
        flex-wrap: wrap;
        height: auto;
        padding: 0;
        gap: 0;
    }

    /* Top row: brand + clock */
    .navbar-brand {
        height: 52px;
        padding: 0 1rem;
    }
    .nav-clock {
        height: 52px;
        display: flex;
        align-items: center;
        padding-right: 1rem;
        margin-left: auto;
    }

    /* Links row: full-width strip, horizontally scrollable, no wrap */
    .navbar-links {
        display: flex;
        width: 100%;
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        background: rgba(0,0,0,0.18);
        border-top: 1px solid rgba(255,255,255,0.12);
        gap: 0;
        margin-left: 0;
        padding: 0 0.25rem;
        height: 36px;
    }
    .navbar-links::-webkit-scrollbar { display: none; }

    .nav-link, .nav-user {
        flex-shrink: 0;
        padding: 0 0.85rem;
        height: 36px;
        line-height: 36px;
        font-size: 0.82rem;
        border-radius: 0;
        border: none;
        margin: 0;
        white-space: nowrap;
    }
    .nav-user {
        border-left: 1px solid rgba(255,255,255,0.2);
        margin-left: auto;
        color: rgba(255,255,255,0.85);
        padding-right: 1rem;
    }

    /* Map sidebar: slide in as overlay, offset by two-row navbar */
    .sidebar {
        position: fixed;
        top: var(--navbar-h);
        left: 0;
        bottom: 0;
        z-index: 200;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        box-shadow: 4px 0 16px rgba(0,0,0,0.2);
        width: min(var(--sidebar-w), 85vw);
    }
    .sidebar.sidebar-open { transform: translateX(0); }

    .sidebar-backdrop {
        display: none;
        position: fixed;
        inset: 0;
        top: var(--navbar-h);
        background: rgba(0,0,0,0.4);
        z-index: 199;
    }
    .sidebar-backdrop.backdrop-visible { display: block; }

    .sidebar-fab {
        display: flex;
        align-items: center;
        justify-content: center;
        position: absolute;
        bottom: calc(0.75rem + env(safe-area-inset-bottom));
        left: 0.75rem;
        z-index: 100;
        height: 40px;
        padding: 0 0.9rem;
        gap: 0.45rem;
        background: var(--surface);
        border: 1px solid var(--border);
        border-radius: 999px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.18);
        color: var(--text);
        font-size: 0.9rem;
        font-weight: 600;
        cursor: pointer;
    }

    .sidebar-mobile-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        min-height: 48px;
        padding: 0.65rem 1rem;
        border-bottom: 1px solid var(--border);
        background: var(--surface-2);
    }

    /* Detail panel full-width on mobile */
    .detail-panel {
        position: fixed !important;
        bottom: 0; left: 0; right: 0;
        top: auto !important;
        width: 100% !important;
        max-height: 55vh;
        overflow-y: auto;
        border-radius: 12px 12px 0 0;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
        z-index: 300;
    }

    /* Dashboard / content pages */
    .dashboard-container { padding: 0.75rem; gap: 0.75rem; }
    .card { padding: 0.9rem; }
    .page-title { font-size: 1.15rem; }

    /* Form rows collapse to single column */
    .form-row { grid-template-columns: 1fr; gap: 0; }

    /* Filter bar: full-width inputs */
    .filter-bar .form-group { min-width: 0; flex: 1 1 100%; }
    .filter-bar .filter-actions { flex: 1 1 100%; }

    /* Action buttons in tables: wrap nicely */
    .action-cell { flex-wrap: wrap; gap: 0.35rem; }

    /* Datasource table: hide secondary columns */
    .data-table .col-hide-mobile { display: none; }

    /* Device Summary uses expandable cards instead of a wide table. */
    .device-summary-wrapper {
        display: none;
    }
    .device-summary-cards {
        display: grid;
        gap: 0.75rem;
    }
    .device-summary-card {
        border: 1px solid var(--border);
        border-radius: var(--radius);
        padding: 0.85rem;
        background: var(--surface);
    }
    .device-summary-card-header {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;
        gap: 0.75rem;
        margin-bottom: 0.8rem;
    }
    .device-summary-name {
        display: block;
        margin-bottom: 0.35rem;
        overflow-wrap: anywhere;
    }
    .device-summary-primary {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
        margin-bottom: 0.75rem;
    }
    .device-summary-primary > div,
    .device-summary-detail-grid > div {
        display: flex;
        flex-direction: column;
        gap: 0.2rem;
        min-width: 0;
    }
    .device-summary-primary > div > span,
    .device-summary-detail-grid > div > span {
        color: var(--text-muted);
        font-size: 0.72rem;
    }
    .device-summary-primary > div > strong,
    .device-summary-detail-grid > div > strong {
        font-size: 0.85rem;
        overflow-wrap: anywhere;
    }
    .device-map-link {
        width: 100%;
        margin-bottom: 0.7rem;
    }
    .device-summary-details {
        border-top: 1px solid var(--border);
        padding-top: 0.65rem;
    }
    .device-summary-details > summary {
        color: var(--primary);
        cursor: pointer;
        font-size: 0.82rem;
        font-weight: 600;
    }
    .device-summary-detail-grid {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.75rem 0.5rem;
        padding-top: 0.75rem;
    }

    /* Modal fills screen */
    .modal-overlay { padding: 0; align-items: flex-end; }
    .modal-card {
        max-width: 100%;
        border-radius: 12px 12px 0 0;
        max-height: 92vh;
        overflow-y: auto;
    }

    /* Auth pages */
    .auth-container { padding: 1rem; }
    .auth-card { padding: 1.75rem 1.25rem; }
    .stats-strip { grid-template-columns: repeat(2, 1fr); }

    /* Profile stats row */
    .profile-stats { gap: 0.6rem; }
    .stat-box { padding: 0.6rem 0.75rem; }
    .stat-value { font-size: 1.3rem; }
}

/* ── Responsive — small phones (≤ 480 px) ───────────────────────────────── */
@media (max-width: 480px) {
    .stats-strip { grid-template-columns: 1fr 1fr; }
    .charts-grid { grid-template-columns: 1fr; }
    .chart-wrapper { height: 200px; }
}

/* Tracker: single-column cards on narrow screens */
@media (max-width: 500px) {
    .ti-grid { grid-template-columns: 1fr; }
    .tracker-map-wrap { flex-basis: 45%; }
}
