:root {
    --brand: #2196F3;
    --brand-dark: #1976D2;
    --ink: #14283d;
    --muted: #6b7a8c;
    --line: #e6ebf2;
    --bg: #f3f6fb;
    --card: #ffffff;
    --green: #2e9e5b;
    --red: #e5484d;
    --orange: #f59f00;
    --radius: 14px;
    --shadow: 0 1px 2px rgba(20, 40, 61, 0.06), 0 6px 18px rgba(20, 40, 61, 0.05);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Inter', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}

/* ---------- Navbar ---------- */
.navbar {
    background: linear-gradient(90deg, var(--brand-dark), var(--brand));
    padding: 0 28px;
    height: 60px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 2px 10px rgba(25, 118, 210, 0.25);
    position: sticky;
    top: 0;
    z-index: 500;
}

.nav-brand {
    color: white;
    font-size: 19px;
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
    text-decoration: none;
}

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

.nav-links a {
    color: rgba(255, 255, 255, 0.85);
    text-decoration: none;
    margin-left: 0;
    font-weight: 500;
    font-size: 14px;
    padding: 20px 14px 17px;
    border-bottom: 3px solid transparent;
}

.nav-links a:hover {
    color: white;
    text-decoration: none;
}

.nav-links a.active {
    color: white;
    border-bottom-color: white;
}

.nav-sep {
    width: 1px;
    height: 22px;
    background: rgba(255, 255, 255, 0.3);
    margin: 0 10px;
}

.nav-user {
    color: white;
    font-size: 14px;
    font-weight: 500;
    padding: 0 6px;
}

/* ---------- Page ---------- */
.container {
    max-width: 1320px;
    margin: 28px auto;
    padding: 0 24px;
}

.page-header {
    margin-bottom: 20px;
}

.page-header h1 {
    font-size: 28px;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.page-header p {
    color: var(--muted);
    font-size: 14px;
    margin-top: 4px;
}

/* ---------- Stats ---------- */
.stats-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 16px;
    margin-bottom: 20px;
}

.stat-card {
    background: var(--card);
    border-radius: var(--radius);
    padding: 18px 20px;
    display: flex;
    align-items: center;
    gap: 16px;
    box-shadow: var(--shadow);
}

.stat-icon {
    width: 46px;
    height: 46px;
    border-radius: 12px;
    background: #e8f3fe;
    color: var(--brand-dark);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    font-size: 22px;
}

.stat-icon.on {
    background: #e4f6ec;
    color: var(--green);
}

.stat-info p {
    font-size: 13px;
    color: var(--muted);
    margin-bottom: 2px;
}

.stat-info h3 {
    font-size: 26px;
    font-weight: 700;
    color: var(--ink);
    letter-spacing: -0.02em;
    line-height: 1.15;
}

.stat-info h3#last-update {
    font-size: 22px;
}

.stat-info h3.ws-on { color: var(--green); }
.stat-info h3.ws-off { color: var(--red); }

.hint {
    font-size: 11px;
    background: #eef2f7;
    color: var(--muted);
    border-radius: 10px;
    padding: 1px 7px;
    margin-left: 4px;
    cursor: help;
}

/* ---------- Cards ---------- */
.card {
    background: var(--card);
    border-radius: var(--radius);
    margin-bottom: 20px;
    box-shadow: var(--shadow);
    overflow: hidden;
}

.card-header {
    padding: 14px 20px;
    border-bottom: 1px solid var(--line);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.card-header.wrap {
    flex-wrap: wrap;
}

.card-header h2 {
    font-size: 17px;
    font-weight: 600;
    white-space: nowrap;
}

.card-tools {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: wrap;
}

.live-badge {
    background: var(--green);
    color: white;
    padding: 4px 12px 4px 22px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
    position: relative;
}

.live-badge::before {
    content: '';
    position: absolute;
    left: 10px;
    top: 50%;
    width: 7px;
    height: 7px;
    margin-top: -3.5px;
    border-radius: 50%;
    background: white;
    animation: pulse 1.5s infinite;
}

.live-badge.off {
    background: #9aa7b6;
}

.live-badge.off::before {
    animation: none;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.35; }
}

/* ---------- Map + device list ---------- */
.dash-grid {
    display: grid;
    grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr);
    gap: 20px;
    margin-bottom: 20px;
}

.dash-grid .card {
    margin-bottom: 0;
    display: flex;
    flex-direction: column;
}

#map {
    height: 470px;
    width: 100%;
    flex: none;
    background: #e8eef5;
    isolation: isolate;
}

.dev-marker-wrap {
    background: transparent;
    border: 0;
}

.dev-marker {
    display: block;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: var(--c, #1E88E5);
    border: 3px solid white;
    box-shadow: 0 1px 6px rgba(0, 0, 0, 0.4);
}

.dev-marker.stale {
    opacity: 0.45;
    filter: grayscale(0.6);
}

.leaflet-popup-content {
    font-size: 13px;
    line-height: 1.5;
}

.side-card .device-list {
    overflow-y: auto;
    max-height: 470px;
    flex: 1;
}

.count-pill {
    background: #e8f3fe;
    color: var(--brand-dark);
    font-weight: 600;
    font-size: 13px;
    border-radius: 12px;
    padding: 2px 10px;
}

.device-item {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 20px;
    background: none;
    border: 0;
    border-bottom: 1px solid var(--line);
    text-align: left;
    cursor: pointer;
    font: inherit;
    color: inherit;
}

.device-item:hover {
    background: #f6f9fd;
}

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

.device-main {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.device-route {
    font-size: 13.5px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.device-id {
    font-size: 12px;
    color: var(--muted);
    font-family: ui-monospace, Consolas, monospace;
}

.device-time {
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
}

.device-time.live {
    color: var(--green);
    font-weight: 600;
}

.empty-note {
    padding: 36px 24px;
    text-align: center;
    color: var(--muted);
    font-size: 14px;
    line-height: 1.6;
}

/* ---------- Controls ---------- */
.control,
.search-box input {
    height: 36px;
    border: 1px solid #d5dde8;
    border-radius: 8px;
    background: white;
    font: inherit;
    font-size: 13.5px;
    color: var(--ink);
    padding: 0 10px;
}

.control:focus,
.search-box input:focus,
.form-group input:focus,
.form-group select:focus {
    outline: 2px solid rgba(33, 150, 243, 0.35);
    border-color: var(--brand);
}

.search-box {
    position: relative;
}

.search-box svg {
    position: absolute;
    left: 10px;
    top: 10px;
    color: var(--muted);
    pointer-events: none;
}

.search-box input {
    padding-left: 32px;
    width: 240px;
}

/* ---------- Table ---------- */
.table-container {
    overflow-x: auto;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 13px 16px;
    text-align: left;
    border-bottom: 1px solid var(--line);
    font-size: 13.5px;
}

th {
    background: #f8fafc;
    font-weight: 600;
    font-size: 12.5px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.03em;
    white-space: nowrap;
}

tbody tr:hover {
    background: #f8fafd;
}

td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
td.mono { font-family: ui-monospace, Consolas, monospace; font-size: 12.5px; }
td.nowrap { white-space: nowrap; }
td.route-cell { max-width: 320px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.actions-col { text-align: right; white-space: nowrap; }
.muted { color: var(--muted); }

/* ---------- Pagination ---------- */
.pagination {
    padding: 14px 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
    border-top: 1px solid var(--line);
}

#page-info {
    font-size: 13.5px;
    color: var(--muted);
}

/* ---------- Buttons ---------- */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    height: 36px;
    padding: 0 16px;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font: inherit;
    font-size: 13.5px;
    font-weight: 600;
    text-decoration: none;
    transition: background 0.15s, opacity 0.15s;
}

.btn:hover { opacity: 0.9; }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }

.btn-sm { height: 32px; padding: 0 12px; font-size: 13px; }

.btn-primary { background: var(--brand); color: white; }
.btn-danger { background: var(--red); color: white; }
.btn-secondary { background: #eef2f7; color: var(--ink); }
.btn-warning { background: #FF9800; color: white; }
.btn-outline { background: white; color: var(--brand-dark); border: 1px solid #b9d9f7; }
.btn-outline:hover { background: #f1f8ff; opacity: 1; }
.btn-full { width: 100%; padding: 12px; height: auto; }

.btn-icon {
    width: 32px;
    height: 32px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: #e8f3fe;
    color: var(--brand-dark);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    margin-left: 4px;
}

.btn-icon:hover { background: #d4e9fc; }
.btn-icon.danger { background: #fdecec; color: var(--red); }
.btn-icon.danger:hover { background: #fad7d8; }

/* ---------- Status badges ---------- */
.badge {
    display: inline-block;
    padding: 3px 11px;
    border-radius: 20px;
    font-size: 12px;
    font-weight: 600;
    text-transform: capitalize;
}

.badge-active { background: #e4f6ec; color: #1f7a45; }
.badge-inactive { background: #fdecec; color: #b42318; }

/* ---------- Modal ---------- */
.modal {
    display: none;
    position: fixed;
    top: 0; left: 0;
    width: 100%; height: 100%;
    background: rgba(15, 30, 50, 0.5);
    z-index: 1000;
    justify-content: center;
    align-items: center;
}

.modal.show { display: flex; }

.modal-content {
    background: white;
    border-radius: var(--radius);
    width: 480px;
    max-width: 92%;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}

.modal-header {
    padding: 16px 24px;
    border-bottom: 1px solid var(--line);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 { font-size: 18px; }

.close {
    font-size: 26px;
    cursor: pointer;
    color: #9aa7b6;
    line-height: 1;
}

.modal-body { padding: 24px; }

.modal-footer {
    padding: 16px 24px;
    border-top: 1px solid var(--line);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

/* ---------- Forms ---------- */
.form-group {
    margin-bottom: 16px;
}

.form-group label {
    display: block;
    font-size: 13.5px;
    font-weight: 600;
    color: #44566a;
    margin-bottom: 6px;
}

.form-group input,
.form-group select {
    width: 100%;
    padding: 10px 12px;
    border: 1px solid #d5dde8;
    border-radius: 8px;
    font: inherit;
    font-size: 14px;
}

/* ---------- Login ---------- */
.login-page {
    background: linear-gradient(135deg, var(--brand-dark), var(--brand));
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 24px;
}

.login-card {
    background: white;
    border-radius: 16px;
    padding: 32px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
}

.login-header {
    text-align: center;
    margin-bottom: 24px;
}

.login-header h1 {
    font-size: 28px;
    color: var(--brand);
    margin-bottom: 8px;
}

.login-header p {
    color: var(--muted);
    font-size: 14px;
}

.alert {
    padding: 12px;
    border-radius: 8px;
    margin-bottom: 16px;
    font-size: 14px;
}

.alert-error {
    background: #FFEBEE;
    color: #C62828;
    border: 1px solid #FFCDD2;
}

.text-center { text-align: center; }

/* ---------- Small screens ---------- */
@media (max-width: 1000px) {
    .stats-grid { grid-template-columns: repeat(2, 1fr); }
    .dash-grid { grid-template-columns: 1fr; }
    .side-card .device-list { max-height: 320px; }
}

@media (max-width: 600px) {
    .navbar { padding: 0 14px; }
    .nav-brand span { display: none; }
    .nav-user, .nav-sep { display: none; }
    .nav-links a { padding: 20px 8px 17px; font-size: 13px; }
    .container { padding: 0 12px; margin-top: 18px; }
    .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 10px; }
    .stat-card { padding: 14px; }
    .stat-icon { display: none; }
    .stat-info h3 { font-size: 22px; }
    .stat-info h3#last-update { font-size: 18px; }
    #map { height: 340px; }
    .search-box input { width: 100%; }
    .search-box { flex: 1 1 100%; }
}