:root {
    --bg: #f4f6f8;
    --surface: #ffffff;
    --border: #e5e8ec;
    --text: #1c2126;
    --text-muted: #6b7280;
    --accent: #2563eb;
    --accent-hover: #1d4ed8;
    --danger: #dc2626;
    --danger-bg: #fee2e2;
    --radius: 10px;
    --shadow: 0 8px 24px rgba(16, 24, 40, 0.08);
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
    margin: 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    font-size: 14px;
    color: var(--text);
    background: var(--bg);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* --- Top bar (≈5% of viewport height) --- */

#topbar {
    flex: none;
    height: 5vh;
    min-height: 52px;
    max-height: 64px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 0 20px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    position: relative;
    z-index: 30;
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 16px;
    letter-spacing: 0.2px;
    color: var(--text);
}

.brand svg { color: var(--accent); }

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 10px;
}

.topbar-actions .btn {
    flex: none;
}

.user-chip {
    font-size: 13px;
    color: var(--text-muted);
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.poll-countdown {
    font-size: 13px;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

/* --- Buttons --- */

.btn {
    appearance: none;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    line-height: 1;
    white-space: nowrap;
    transition: background 0.15s ease, border-color 0.15s ease;
}

.btn:hover { background: #f3f4f6; }

.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }

.btn-danger { background: var(--surface); border-color: #fca5a5; color: var(--danger); }
.btn-danger:hover { background: var(--danger-bg); border-color: var(--danger); }

.btn-small { padding: 6px 10px; font-size: 12px; }
.btn-block { width: 100%; }
.btn-icon { padding: 6px 8px; font-size: 12px; line-height: 1; }

/* --- Map area / content --- */

#map {
    position: relative;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    background:
        linear-gradient(#e8edf2 1px, transparent 1px),
        linear-gradient(90deg, #e8edf2 1px, transparent 1px),
        #f0f3f6;
    background-size: 44px 44px;
}

#maplibre-map {
    position: absolute;
    inset: 0;
    z-index: 0;
    background: #e8edf2;
}

/* --- Position marker --- */

.pos-marker-dot {
    width: 18px;
    height: 18px;
    background: var(--accent);
    border: 3px solid #fff;
    border-radius: 50%;
    box-shadow: 0 1px 6px rgba(15, 23, 42, 0.45);
}

/* --- Time-travel slider --- */

.time-travel {
    position: absolute;
    left: 50%;
    bottom: 20px;
    transform: translateX(-50%);
    z-index: 10;
    width: min(480px, calc(100vw - 40px));
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 10px 14px 12px;
}

.time-travel-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 8px;
}

.time-label {
    font-size: 12.5px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#time-slider {
    width: 100%;
    accent-color: var(--accent);
    cursor: pointer;
}

.time-jumps {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
}

.time-jumps .btn {
    padding: 4px 9px;
    font-size: 11.5px;
    min-width: 40px;
}

.time-jumps-spacer {
    flex: 1;
}

/* --- Trip info card (overlay on the map) --- */

.trip-info {
    position: absolute;
    top: 20px;
    left: 20px;
    width: min(320px, calc(100vw - 40px));
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 18px;
    z-index: 10;
}

.trip-info-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}

.trip-info-title { margin: 0; font-size: 17px; }

.trip-info.collapsed {
    width: auto;
    max-width: min(320px, calc(100vw - 40px));
    padding: 8px 10px 8px 14px;
}

.trip-info.collapsed .trip-info-title {
    font-size: 14px;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.trip-info-desc { margin: 8px 0 12px; font-size: 13px; color: var(--text-muted); line-height: 1.5; }

.trip-info-stats { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.trip-info-stats .stat-group { display: flex; flex-direction: row; gap: 24px; }
.trip-info-stats > div, .trip-info-stats .stat-group > div { display: flex; flex-direction: column; }
.stat-num { font-size: 18px; font-weight: 700; }
.stat-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); }

/* --- Slide-over panel --- */

.panel {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(420px, 92vw);
    background: var(--surface);
    border-right: 1px solid var(--border);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    transform: translateX(-105%);
    visibility: hidden;
    transition: transform 0.25s ease, visibility 0s 0.25s;
    z-index: 20;
}

.panel.open { transform: none; visibility: visible; transition: transform 0.25s ease; }

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 18px;
    border-bottom: 1px solid var(--border);
}

.panel-header h1 { margin: 0; font-size: 16px; }
.panel-header-actions { display: flex; gap: 8px; }

.panel-list {
    overflow-y: auto;
    max-height: 38%;
    border-bottom: 1px solid var(--border);
    padding: 8px;
}

.panel-detail {
    flex: 1;
    overflow-y: auto;
    padding: 16px 18px;
}

.panel-empty {
    padding: 24px 12px;
    text-align: center;
    color: var(--text-muted);
    font-size: 13px;
}

.trip-row {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    border: 1px solid transparent;
    border-radius: 8px;
    background: none;
    cursor: pointer;
    margin-bottom: 2px;
    font-family: inherit;
}

.trip-row:hover { background: #f6f7f9; }
.trip-row.selected { background: #eff6ff; border-color: #bfdbfe; }
.trip-row-name { display: block; font-size: 13.5px; font-weight: 600; margin-bottom: 3px; }
.trip-row-meta { display: flex; gap: 8px; align-items: center; font-size: 11.5px; color: var(--text-muted); }

/* --- Panel detail sections --- */

.detail-section {
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px;
    margin-bottom: 14px;
}

.detail-section h2 { margin: 0 0 6px; font-size: 15px; }
.detail-section h3 { margin: 0 0 12px; font-size: 12.5px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--text-muted); }

.detail-meta { margin: 0 0 8px; font-size: 12px; color: var(--text-muted); display: flex; gap: 8px; align-items: center; }
.detail-desc { margin: 0 0 4px; font-size: 13px; color: var(--text-muted); }
.count { font-weight: 600; color: var(--accent); }

label { display: block; font-size: 12.5px; font-weight: 600; margin: 10px 0 4px; }

.input {
    width: 100%;
    padding: 8px 10px;
    font-size: 13px;
    font-family: inherit;
    color: var(--text);
    border: 1px solid #d7dbe1;
    border-radius: 8px;
    background: #fff;
}

.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12); }
textarea.input { resize: vertical; }

.route-list, .device-list, .grant-list { list-style: none; margin: 0 0 10px; padding: 0; }
.route-list li, .device-list li, .grant-list li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 7px 0;
    border-bottom: 1px dashed #edf0f3;
    font-size: 13px;
}

.muted { color: var(--text-muted); }

.inline-form { display: flex; gap: 8px; }
.inline-form .input { flex: 1; }

.file-label {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    border: 1.5px dashed #cdd5dd;
    border-radius: 8px;
    padding: 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    margin: 0;
}

.file-label:hover { border-color: var(--accent); color: var(--accent); }
.file-label input { display: none; }

.detail-section.danger { border-color: #fecaca; }
.detail-section.danger h3 { color: var(--danger); }

.hint { font-size: 12px; color: var(--text-muted); margin: 8px 0 0; line-height: 1.4; }
.share-box { display: flex; gap: 8px; }
.share-box .input { flex: 1; font-size: 12px; }

/* --- Badges --- */

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.badge-private { background: #f1f3f5; color: #4b5563; }
.badge-registered { background: #dbeafe; color: #1d4ed8; }
.badge-public { background: #dcfce7; color: #15803d; }
.badge-active { background: #dcfce7; color: #15803d; }
.badge-inactive { background: #f1f3f5; color: #9ca3af; }

label.check {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
    margin-top: 12px;
    cursor: pointer;
}
label.check input { width: auto; margin: 0; }

/* --- Modal --- */

.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(15, 23, 42, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 60;
}

.modal {
    background: var(--surface);
    border-radius: 14px;
    box-shadow: 0 20px 50px rgba(15, 23, 42, 0.25);
    padding: 22px;
    width: 100%;
    max-width: 430px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal h3 { margin: 0 0 4px; font-size: 17px; }
.modal-message { font-size: 13.5px; color: var(--text-muted); line-height: 1.5; margin: 8px 0 18px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 18px; }

.notif-section { margin-top: 16px; }
.notif-section .modal-actions { justify-content: flex-start; }
.notif-section code {
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    background: rgba(15, 23, 42, 0.06);
    padding: 1px 5px;
    border-radius: 4px;
    font-size: 12px;
    word-break: break-all;
}
.notif-section a { color: var(--accent); word-break: break-all; }

/* --- Toasts --- */

.toasts {
    position: fixed;
    right: 18px;
    bottom: 18px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 80;
}

.toast {
    background: #1f2429;
    color: #fff;
    padding: 10px 14px;
    border-radius: 8px;
    font-size: 13px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
    max-width: 340px;
    animation: toast-in 0.2s ease;
}

.toast-error { background: #b91c1c; }
.toast.out { opacity: 0; transform: translateY(4px); transition: all 0.3s; }

@keyframes toast-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: none; }
}

/* --- Responsive / mobile --- */

@media (max-width: 768px) {
    #topbar {
        padding: 0 12px;
        gap: 10px;
    }

    .brand { font-size: 14px; gap: 8px; }
    .brand svg { width: 16px; height: 16px; }

    .topbar-actions {
        flex: 1;
        min-width: 0;
        gap: 6px;
        overflow-x: auto;
        scrollbar-width: none;
    }
    .topbar-actions::-webkit-scrollbar { display: none; }

    .btn { padding: 6px 10px; font-size: 12px; border-radius: 7px; }
    .btn-small { padding: 5px 9px; font-size: 11.5px; }
    .btn-icon { padding: 5px 7px; }

    .user-chip { max-width: 110px; font-size: 12px; }
    .poll-countdown { font-size: 12px; }

    .trip-info {
        top: 12px;
        left: 12px;
        right: auto;
        width: auto;
        max-width: calc(100vw - 72px);
        padding: 12px 14px;
    }

    .trip-info.collapsed {
        right: auto;
        width: auto;
        max-width: calc(100vw - 24px);
        padding: 6px 8px 6px 12px;
    }

    .trip-info-title { font-size: 15px; }
    .trip-info.collapsed .trip-info-title { font-size: 13px; }
    .trip-info-desc { font-size: 12px; }
    .trip-info .stat-num { font-size: 15px; }
    .trip-info .stat-label { font-size: 10px; }

    .time-travel { bottom: 12px; width: calc(100vw - 24px); }

    .modal-overlay { padding: 12px; }
    .modal { padding: 18px; }

    .toasts { left: 12px; right: 12px; bottom: 12px; align-items: stretch; }
    .toast { max-width: none; }
}

@media (max-width: 480px) {
    .brand span { display: none; }
    .brand svg { width: 20px; height: 20px; }
    .topbar-actions { gap: 4px; }
    .btn { padding: 6px 9px; }
    .poll-countdown { display: none; }
    .user-chip { display: none; }

    .trip-info-stats { gap: 16px; }

    .inline-form { flex-direction: column; align-items: stretch; }
    .inline-form .btn { width: 100%; }

    .modal-actions { flex-wrap: wrap; }
}
