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

        body {
            width: 100vw;
            height: 100dvh;
            overflow: hidden;
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
        }

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

        /* Top bar */
        #topbar {
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            min-height: 52px;
            background: rgba(15, 23, 42, 0.92);
            backdrop-filter: blur(10px);
            display: flex;
            flex-direction: row;
            justify-content: space-between;
            align-items: center;
            padding: 6px 14px;
            gap: 12px;
            z-index: 100;
            border-bottom: 1px solid rgba(255, 255, 255, 0.08);
        }

        #topbar-left {
            display: flex;
            flex-direction: column;
            gap: 8px;
            flex: 1;
            min-width: 0;
        }

        .topbar-row {
            display: flex;
            align-items: center;
            width: 100%;
            gap: 10px;
        }

        #topbar .logo {
            font-weight: 700;
            font-size: 15px;
            color: #fff;
            white-space: nowrap;
        }

        #topbar .user-info {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 13px;
            font-weight: 600;
        }

        #topbar .user-badge {
            background: #0ea5e9;
            color: #fff;
            padding: 2px 8px;
            border-radius: 99px;
            font-size: 11px;
            white-space: nowrap;
        }

        #stats-bar {
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: flex-end;
            gap: 4px;
            font-size: 11px;
            color: rgba(255, 255, 255, 0.6);
            border-left: 1px solid rgba(255, 255, 255, 0.1);
            padding-left: 14px;
            height: 100%;
        }

        #stats-bar span {
            white-space: nowrap;
        }

        #stats-bar strong {
            color: #7dd3fc;
        }

        /* ============================================================
           CABLE DRAWING
           ============================================================ */

        /* Trigger button — right side, below locate */
        #cable-draw-btn {
            position: absolute;
            bottom: 186px;
            right: 12px;
            width: 44px;
            height: 44px;
            border-radius: 10px;
            background: #fff;
            color: #000;
            border: none;
            font-size: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
            z-index: 100;
            transition: transform 0.2s, background 0.2s;
        }

        #cable-draw-btn:hover {
            transform: scale(1.1);
        }

        #cable-draw-btn.active {
            background: #dc2626;
            color: #fff;
            box-shadow: 0 2px 10px rgba(220, 38, 38, 0.5);
        }

        /* Marker creation button */
        #marker-create-btn {
            position: absolute;
            bottom: 240px;
            right: 12px;
            width: 44px;
            height: 44px;
            border-radius: 10px;
            background: #fff;
            color: #000;
            border: none;
            font-size: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
            z-index: 100;
            transition: transform 0.2s, background 0.2s;
        }

        #marker-create-btn.active {
            background: #fef3c7;
            border: 2px solid #d97706;
        }

        #marker-create-toolbar {
            position: fixed;
            bottom: 15px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 1200;
            background: #fff;
            border-radius: 16px;
            box-shadow: 0 4px 32px rgba(0, 0, 0, 0.25);
            padding: 14px;
            display: none;
            flex-direction: column;
            gap: 12px;
            width: 90vw;
            max-width: 450px;
            border: 2px solid #d97706;
        }

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

        #dynamic-fields-container {
            display: flex;
            flex-direction: column;
            gap: 8px;
            width: 100%;
            border-top: 1px solid #f1f5f9;
            padding-top: 8px;
        }

        .dynamic-field-group {
            display: flex;
            gap: 8px;
            width: 100%;
        }

        .dynamic-input {
            flex: 1;
            padding: 8px 12px;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            font-size: 13px;
            background: #f8fafc;
            outline: none;
            transition: all 0.2s;
        }

        .dynamic-input:focus {
            border-color: #d97706;
            background: #fff;
            box-shadow: 0 0 0 2px rgba(217, 119, 6, 0.1);
        }

        /* Drawing toolbar — bottom center */
        #cable-draw-toolbar {
            position: fixed;
            bottom: 10px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 1200;
            background: #fff;
            border-radius: 14px;
            box-shadow: 0 4px 24px rgba(0, 0, 0, 0.22);
            padding: 10px 12px;
            display: none;
            /* shown via JS */
            align-items: center;
            justify-content: center;
            gap: 10px;
            flex-wrap: wrap;
            /* allow wrapping on narrow screens */
            width: max-content;
            max-width: 95vw;
            border: 2px solid #0369a1;
        }

        .cable-toolbar-label {
            font-weight: 700;
            font-size: 13px;
            color: #0369a1;
            white-space: nowrap;
        }

        .cable-type-select {
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 6px 10px;
            font-size: 13px;
            background: #f8fafc;
            outline: none;
            cursor: pointer;
        }

        .cable-type-select:focus {
            border-color: #0369a1;
        }

        .cable-description-input {
            border: 1px solid #e0e0e0;
            border-radius: 8px;
            padding: 6px 10px;
            font-size: 13px;
            background: #f8fafc;
            outline: none;
            min-width: 120px;
            flex: 1;
            /* allow growth and shrink */
            max-width: 240px;
            font-family: inherit;
            transition: border-color 0.15s;
        }

        .cable-description-input:focus {
            border-color: #0369a1;
        }

        .cable-point-count {
            font-size: 12px;
            font-weight: 600;
            color: #64748b;
            white-space: nowrap;
            min-width: 52px;
            text-align: center;
        }

        .cable-btn {
            border: none;
            border-radius: 8px;
            padding: 6px 12px;
            font-size: 13px;
            font-weight: 600;
            cursor: pointer;
            white-space: nowrap;
            transition: all 0.15s;
        }

        .cable-btn-undo {
            background: #fef3c7;
            color: #92400e;
        }

        .cable-btn-undo:hover {
            background: #fbbf24;
            color: #000;
        }

        .cable-btn-save {
            background: #0369a1;
            color: #fff;
        }

        .cable-btn-save:hover {
            background: #0284c7;
        }

        .cable-btn-save:disabled {
            background: #cbd5e1;
            color: #64748b;
            cursor: default;
        }

        .cable-btn-cancel {
            background: #fee2e2;
            color: #dc2626;
        }

        .cable-btn-cancel:hover {
            background: #dc2626;
            color: #fff;
        }


        /* Mobile: collapse topbar */
        @media (max-width: 600px) {
            #topbar {
                padding: 6px 10px;
            }

            /* Hide button text on mobile, keep emojis */
            .tb-btn-text {
                display: none;
            }

            .tb-btn {
                padding: 6px;
                font-size: 16px;
                /* Larger icon size since no text */
                min-width: unset;
            }

            #brigade-filter-panel {
                left: 4px;
            }

            #routes-panel {
                left: 4px;
            }

            #markers-panel {
                left: 4px;
            }

            #search-box {
                width: 120px;
                font-size: 12px;
            }
        }

        /* Search — bottom center */
        #search-container {
            position: fixed;
            bottom: 28px;
            left: 50%;
            transform: translateX(-50%);
            z-index: 200;
            width: min(500px, calc(100vw - 100px));
        }

        #search-wrap {
            position: relative;
        }

        #search-box {
            width: 100%;
            background: rgba(15, 23, 42, 0.88);
            backdrop-filter: blur(8px);
            border: 1px solid rgba(255, 255, 255, 0.18);
            color: #fff;
            font-size: 14px;
            padding: 10px 64px 10px 14px;
            border-radius: 12px;
            outline: none;
            box-sizing: border-box;
            transition: border-color .15s, background .15s;
        }

        #search-box::placeholder {
            color: rgba(255, 255, 255, 0.45);
        }

        #search-box:focus {
            border-color: rgba(14, 165, 233, 0.7);
            background: rgba(15, 23, 42, 0.95);
        }

        #search-count {
            position: absolute;
            right: 36px;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.45);
            font-size: 12px;
            pointer-events: none;
        }

        #search-clear {
            position: absolute;
            right: 10px;
            top: 50%;
            transform: translateY(-50%);
            color: rgba(255, 255, 255, 0.5);
            font-size: 16px;
            cursor: pointer;
            display: none;
        }

        #search-clear:hover {
            color: #fff;
        }

        #search-results {
            display: none;
            position: absolute;
            bottom: calc(100% + 6px);
            left: 0;
            right: 0;
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 -4px 32px rgba(0, 0, 0, 0.22);
            z-index: 300;
            max-height: 360px;
            overflow-y: auto;
        }

        #search-results.visible {
            display: block;
        }

        .search-result-item {
            padding: 9px 14px;
            cursor: pointer;
            border-bottom: 1px solid #f1f5f9;
            transition: background .1s;
        }

        .search-result-item:last-child {
            border-bottom: none;
        }

        .search-result-item:hover {
            background: #f0f9ff;
        }

        .search-result-header {
            display: flex;
            justify-content: space-between;
            align-items: baseline;
            gap: 8px;
            margin-bottom: 2px;
        }

        .search-result-title {
            font-weight: 600;
            font-size: 13px;
            color: #1e293b;
        }

        .search-result-coords {
            font-size: 11px;
            color: #94a3b8;
            white-space: nowrap;
        }

        .search-result-message {
            font-size: 12px;
            color: #4b5563;
            display: -webkit-box;
            -webkit-line-clamp: 2;
            line-clamp: 2;
            -webkit-box-orient: vertical;
            overflow: hidden;
        }

        .search-result-date {
            font-size: 11px;
            color: #94a3b8;
            margin-top: 2px;
        }

        .search-match {
            background: #fef08a;
            border-radius: 2px;
            padding: 0 1px;
        }

        /* Popup Pinning */
        .pin-btn {
            background: none;
            border: none;
            font-size: 16px;
            cursor: pointer;
            padding: 2px 6px;
            border-radius: 4px;
            transition: all 0.2s;
            opacity: 0.6;
            margin-left: 8px;
        }

        .pin-btn:hover {
            background: #f3f4f6;
            opacity: 1;
        }

        .pin-btn.active {
            opacity: 1;
            filter: drop-shadow(0 0 2px #f59e0b);
        }

        .pinned-popup .maplibregl-popup-tip {
            display: none;
            /* Hide default tip when pinned, we use SVG lines */
        }

        .pinned-popup .maplibregl-popup-content {
            border: 2px solid #3b82f6;
            /* The drag offset is applied to the content, preventing conflicts with MapLibre's positioning */
            transform: translate(var(--drag-x, 0px), var(--drag-y, 0px));
            transition: box-shadow 0.2s;
        }

        .pinned-popup.dragging .maplibregl-popup-content {
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2) !important;
            z-index: 1000;
        }

        .pin-btn.active {
            color: #3b82f6;
            transform: scale(1.2);
            filter: drop-shadow(0 0 4px rgba(59, 130, 246, 0.5));
        }

        .popup-header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 4px;
        }

        .pinned-popup .popup-header {
            cursor: grab;
        }

        .pinned-popup .popup-header:active {
            cursor: grabbing;
        }

        .leader-line-path {
            stroke: #3b82f6;
            stroke-width: 3;
            /* Solid line, slightly thicker */
            fill: none;
            opacity: 0.8;
        }

        /* Marker Nudge D-Pad */
        .nudge-controls {
            display: flex;
            gap: 4px;
            margin: 6px auto;
            justify-content: center;
            align-items: center;
        }

        .nudge-btn {
            background: #f3f4f6;
            border: 1px solid #d1d5db;
            border-radius: 4px;
            width: 24px;
            height: 24px;
            display: flex;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            font-size: 14px;
            color: #4b5563;
        }

        .nudge-btn:hover {
            background: #e5e7eb;
        }

        .nudge-btn:active {
            background: #d1d5db;
        }

        /* Grid areas no longer used for flex layout */

        .btn-center:hover {
            background: #1d4ed8;
        }

        /* Refresh button */
        #refresh-btn {
            position: absolute;
            bottom: 78px;
            right: 12px;
            width: 44px;
            height: 44px;
            background: #10b981;
            color: white;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
            z-index: 100;
            transition: transform .15s, background .15s;
        }

        #refresh-btn:hover {
            background: #059669;
        }

        /* Brigade Orders Panel - Top Center */
        #orders-panel {
            position: absolute;
            top: 80px;
            left: 50%;
            transform: translateX(-50%);
            width: 90%;
            max-width: 500px;
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(10px);
            border-radius: 12px;
            box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
            z-index: 1000;
            display: none;
            flex-direction: column;
            max-height: 700px;
            overflow: hidden;
            border: 1px solid rgba(0, 0, 0, 0.1);
        }

        .orders-header {
            padding: 10px 15px;
            background: #1e293b;
            color: #fff;
            display: flex;
            justify-content: space-between;
            align-items: center;
            font-size: 14px;
            font-weight: 600;
        }

        .orders-content {
            padding: 10px;
            overflow-y: auto;
            font-size: 13px;
        }

        .order-item {
            padding: 8px;
            border-bottom: 1px solid #eee;
        }

        .order-item:last-child {
            border-bottom: none;
        }

        .crm-login-form {
            padding: 15px;
            display: flex;
            flex-direction: column;
            gap: 10px;
        }

        .crm-input {
            padding: 8px 12px;
            border: 1px solid #cbd5e1;
            border-radius: 6px;
            font-size: 14px;
        }

        #refresh-btn:active {
            transform: scale(0.92);
        }

        /* Fiat Doblo (Brigade) button - Center Right */
        #fiat-btn {
            position: absolute;
            top: 50%;
            right: 12px;
            transform: translateY(-50%);
            width: 44px;
            height: 44px;
            background: #ffffff;
            color: #1e293b;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
            z-index: 100;
            transition: transform 0.2s, background 0.2s;
            font-size: 24px;
        }

        #fiat-btn:hover {
            transform: translateY(-50%) scale(1.1);
            background: #f1f5f9;
        }

        #fiat-btn:active {
            transform: translateY(-50%) scale(0.92);
        }

        @keyframes rotate {
            from {
                transform: rotate(0deg);
            }

            to {
                transform: rotate(360deg);
            }
        }

        /* Locate button */
        #locate-btn {
            position: absolute;
            bottom: 132px;
            right: 12px;
            width: 44px;
            height: 44px;
            background: #fff;
            border: none;
            border-radius: 10px;
            cursor: pointer;
            font-size: 20px;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
            z-index: 100;
            transition: transform .15s;
        }

        #locate-btn:active {
            transform: scale(0.92);
        }

        /* Popup */
        .maplibregl-popup-content {
            border-radius: 12px !important;
            padding: 14px 16px !important;
            min-width: 200px;
            max-width: 320px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18) !important;
            font-size: 13px;
            line-height: 1.5;
        }

        .popup-title {
            font-weight: 700;
            font-size: 15px;
            margin-bottom: 6px;
        }

        .popup-meta {
            color: #6b7280;
            font-size: 11px;
            margin-top: 4px;
        }

        .popup-coords {
            font-family: monospace;
            font-size: 11px;
            background: #f3f4f6;
            border-radius: 6px;
            padding: 4px 8px;
            margin-top: 6px;
            cursor: pointer;
        }

        .popup-coords:hover {
            background: #e5e7eb;
        }

        .cable-tag {
            display: inline-block;
            padding: 2px 8px;
            border-radius: 99px;
            font-size: 11px;
            font-weight: 700;
            color: #fff;
            margin-bottom: 4px;
        }

        /* Loading overlay */
        #loading {
            position: absolute;
            inset: 0;
            background: #0f172a;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            z-index: 999;
            color: #fff;
            gap: 16px;
            transition: opacity .4s;
        }

        #loading.hidden {
            opacity: 0;
            pointer-events: none;
        }

        .spinner {
            width: 40px;
            height: 40px;
            border: 3px solid rgba(255, 255, 255, 0.15);
            border-top-color: #0ea5e9;
            border-radius: 50%;
            animation: spin .8s linear infinite;
        }

        @keyframes spin {
            to {
                transform: rotate(360deg);
            }
        }

        #loading-text {
            font-size: 15px;
            font-weight: 500;
            color: rgba(255, 255, 255, 0.85);
            margin-top: 10px;
        }

        /* Topbar buttons (brigade filter, routes, markers) */
        .tb-btn {
            background: rgba(255, 255, 255, 0.12);
            border: 1px solid rgba(255, 255, 255, 0.18);
            color: #fff;
            font-size: 13px;
            font-weight: 600;
            padding: 5px 12px;
            border-radius: 8px;
            cursor: pointer;
            white-space: nowrap;
            transition: background .15s;
        }

        .tb-btn:hover {
            background: rgba(255, 255, 255, 0.2);
        }

        .tb-btn.active {
            background: rgba(14, 165, 233, 0.35);
            border-color: rgba(14, 165, 233, 0.5);
        }

        .tb-btn.off {
            background: rgba(255, 255, 255, 0.06);
            color: rgba(255, 255, 255, 0.45);
        }


        /* Shared dropdown panel */
        .tb-dropdown {
            display: none;
            position: absolute;
            background: #fff;
            border-radius: 12px;
            box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
            padding: 10px 0;
            z-index: 200;
            min-width: 190px;
            max-height: 420px;
            overflow-y: auto;
        }

        .tb-dropdown.open {
            display: block;
        }

        #brigade-filter-panel {
            left: 105px;
        }

        #routes-panel {
            left: 214px;
        }

        #markers-panel {
            left: 320px;
        }

        .bf-header {
            font-size: 11px;
            font-weight: 700;
            color: #0369a1;
            text-transform: uppercase;
            letter-spacing: 0.06em;
            padding: 2px 14px 6px;
        }

        .bf-item {
            display: flex;
            align-items: center;
            gap: 8px;
            padding: 5px 14px;
            cursor: pointer;
            font-size: 13px;
            color: #1e293b;
            transition: background .1s;
        }

        .bf-item:hover {
            background: #f1f5f9;
        }

        .bf-item input[type=checkbox] {
            accent-color: #0ea5e9;
            width: 15px;
            height: 15px;
            cursor: pointer;
            flex-shrink: 0;
        }

        .bf-separator {
            border: none;
            border-top: 1px solid #e5e7eb;
            margin: 6px 0;
        }

        .bf-toggle-all {
            font-size: 11px;
            color: #0ea5e9;
            cursor: pointer;
            padding: 2px 14px 8px;
            display: block;
        }

        .bf-toggle-all:hover {
            text-decoration: underline;
        }

        /* Mobile search specific */
        @media (max-width: 600px) {
            #search-container {
                width: calc(100vw - 16px);
                bottom: max(16px, env(safe-area-inset-bottom) + 16px);
                z-index: 100;
            }

            #search-box {
                font-size: 16px;
                /* Prevents iOS auto-zoom */
            }

            #search-results {
                max-height: 250px;
                /* Limit height on mobile so it fits in the space above keyboard */
                z-index: 100;
            }

            #refresh-btn,
            #locate-btn,
            #fiat-btn,
            #cable-draw-btn {
                z-index: 100;
            }
        }

        /* ── Custom Modals (to avoid Telegram WebView blocking native prompts) ── */
        .custom-modal-backdrop {
            position: fixed;
            inset: 0;
            background: rgba(0, 0, 0, 0.5);
            z-index: 10000;
            display: none;
            align-items: center;
            justify-content: center;
            backdrop-filter: blur(2px);
        }

        .custom-modal {
            background: #fff;
            border-radius: 12px;
            padding: 20px;
            width: 90%;
            max-width: 340px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .custom-modal h3 {
            margin: 0;
            font-size: 16px;
            color: #111;
        }

        .custom-modal label {
            font-size: 12px;
            color: #666;
            margin-bottom: -6px;
        }

        .custom-modal input,
        .custom-modal select,
        .custom-modal textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ccc;
            border-radius: 8px;
            font-size: 14px;
            box-sizing: border-box;
            font-family: inherit;
        }

        .custom-modal textarea {
            resize: vertical;
            min-height: 60px;
        }

        .custom-modal-actions {
            display: flex;
            gap: 8px;
            margin-top: 8px;
        }

        .custom-modal-btn {
            flex: 1;
            padding: 10px;
            border: none;
            border-radius: 8px;
            font-weight: 600;
            cursor: pointer;
            font-size: 14px;
        }

        .btn-cancel {
            background: #f3f4f6;
            color: #374151;
        }

        .btn-primary {
            background: #0ea5e9;
            color: #fff;
        }

        .btn-danger {
            background: #ef4444;
            color: #fff;
        }

        /* User Location Dot */
        .user-location-dot {
            width: 14px;
            height: 14px;
            background-color: #0ea5e9;
            border: 2px solid #fff;
            border-radius: 50%;
            box-shadow: 0 0 5px rgba(0, 0, 0, 0.5);
            position: relative;
        }

        .user-location-dot::after {
            content: '';
            position: absolute;
            top: 50%;
            left: 50%;
            width: 100%;
            height: 100%;
            background-color: #0ea5e9;
            border-radius: 50%;
            transform: translate(-50%, -50%);
            animation: ripple 2s infinite;
            z-index: -1;
        }

        @keyframes ripple {
            0% {
                width: 100%;
                height: 100%;
                opacity: 0.6;
            }

            100% {
                width: 400%;
                height: 400%;
                opacity: 0;
            }
        }
