* { -webkit-tap-highlight-color: transparent; }
:root{
    --brand:#11325C;--brand-light:#1a4a87;
    --accent:#e8a020;--text:#1a1a2e;--muted:#6b7280;
    --border:#e2e8f0;--card-bg:#fff;--radius:12px;
    --shadow:0 1px 6px rgba(0,0,0,.12);--ff:'Figtree',sans-serif;
    --top-h:56px; /* top bar height */
}

/* ── OUTER WRAP ── */
#crm-map-wrap{
    display:flex;flex-direction:column;
    height:100vh;min-height:600px;max-height:1200px;
    font-family:var(--ff);
    background:#f8f9fa;
    overflow:hidden;
}

/* ══════════════════════════════
   TOP BAR — search + pill filters
   ══════════════════════════════ */
#crm-top-bar{
    height:var(--top-h);
    background:#fff;
    border-bottom:1px solid var(--border);
    display:flex;align-items:center;
    gap:8px;padding:0 16px;
    flex-shrink:0;
    z-index:100;
    box-shadow:0 1px 4px rgba(0,0,0,.06);
}

/* Search input */
.crm-search-wrap{
    display:flex;align-items:center;
    border:1px solid var(--border);
    border-radius:24px;
    padding:0 14px;
    height:36px;
    min-width:220px;max-width:320px;flex:1;
    background:#fff;
    transition:border-color .2s,box-shadow .2s;
    gap:8px;
}
.crm-search-wrap:focus-within{
    border-color:var(--brand);
    box-shadow:0 0 0 2px rgba(17,50,92,.08);
}
.crm-search-wrap svg{opacity:.4;flex-shrink:0;}
#crm-search-input{
    border:none;outline:none;font-family:var(--ff);
    font-size:13px;color:var(--text);background:transparent;
    width:100%;
}
#crm-search-input::placeholder{color:var(--muted);}
/* AI filter pills strip — shown when [listing_map] receives URL params from [listing_ai_search] */
#crm-ai-pills{
    display:none;
    position:absolute;left:0;width:62%;
    top:calc(var(--top-h) + 4px);z-index:99;
    padding:6px 16px;
    gap:6px;flex-wrap:wrap;align-items:center;
    background:#fff;border-bottom:0.5px solid var(--border);
}
#crm-ai-pills.visible{display:flex;}
.crm-ai-pill{
    display:inline-flex;align-items:center;gap:5px;
    background:#e8eef6;color:#11325C;
    font-size:11px;font-weight:600;
    padding:4px 10px;border-radius:20px;
    border:0.5px solid #b5cce0;
    font-family:var(--ff);
}
.crm-ai-pill-key{opacity:.55;font-weight:400;}
#crm-ai-clear{
    font-size:11px;color:var(--muted);cursor:pointer;
    background:none;border:none;font-family:var(--ff);
    padding:4px 8px;margin-left:4px;
    transition:color .15s;
}
#crm-ai-clear:hover{color:#d63638;}

/* Pill filters */
.crm-pill{
    display:inline-flex;align-items:center;gap:5px;
    border:1px solid var(--border);
    border-radius:20px;
    padding:0 14px;height:36px;
    font-size:13px;font-weight:500;font-family:var(--ff);
    color:var(--text);background:#fff;
    cursor:pointer;white-space:nowrap;
    transition:all .15s;
    position:relative;
}
.crm-pill:hover{border-color:#aaa;}
.crm-pill.active{
    background:var(--brand);color:#fff;border-color:var(--brand);
}
.crm-pill svg{opacity:.6;}
.crm-pill.active svg{opacity:1;filter:brightness(10);}

/* Clear Filters button */
#crm-btn-clear{
    display:inline-flex;align-items:center;gap:5px;
    border:1px solid var(--border);border-radius:20px;
    padding:0 14px;height:36px;
    font-size:13px;font-weight:500;font-family:var(--ff);
    color:var(--muted);background:#fff;cursor:pointer;
    white-space:nowrap;transition:all .15s;
}
#crm-btn-clear:hover{border-color:#d63638;color:#d63638;}

/* Pill dropdown */
.crm-pill-dropdown{
    position:absolute;top:calc(100% + 6px);left:0;
    background:#fff;border:1px solid var(--border);
    border-radius:12px;padding:10px;
    box-shadow:0 4px 24px rgba(0,0,0,.12);
    z-index:1000;min-width:240px;
    display:none;
}
.crm-pill-dropdown.open{display:block;}
.crm-pill-dropdown label{
    display:flex;align-items:center;gap:10px;
    padding:8px 10px;border-radius:8px;cursor:pointer;
    font-size:13px;color:var(--text);
    transition:background .15s;
}
.crm-pill-dropdown label:hover{background:#f4f6f9;}
.crm-pill-dropdown input[type=number],
.crm-pill-dropdown input[type=text]{
    width:100%;border:1px solid var(--border);border-radius:8px;
    padding:10px 12px;font-size:14px;font-family:var(--ff);
    outline:none;color:var(--text);
    transition:border-color .2s;
    min-width:0; /* allow inputs to shrink inside flex row */
}
.crm-pill-dropdown input:focus{border-color:var(--brand);}
.crm-pill-dropdown .crm-dd-row{display:flex;gap:10px;padding:8px 6px 4px;}
.crm-pill-dropdown .crm-dd-row input{flex:1;}
.crm-pill-dropdown .crm-dd-apply{
    width:calc(100% - 12px);margin:8px 6px 4px;
    background:var(--brand);color:#fff;border:none;border-radius:8px;
    padding:11px;font-size:14px;font-weight:700;font-family:var(--ff);
    cursor:pointer;transition:background .2s;
}
.crm-pill-dropdown .crm-dd-apply:hover{background:var(--brand-light);}
/* Secondary action inside a dropdown (like the neighborhood Clear button) */
.crm-pill-dropdown .crm-dd-secondary{
    width:calc(100% - 12px);margin:6px 6px 0;
    background:transparent;color:var(--muted);
    border:1px solid var(--border);border-radius:8px;
    padding:9px;font-size:13px;font-weight:600;font-family:var(--ff);
    cursor:pointer;transition:all .15s;
}
.crm-pill-dropdown .crm-dd-secondary:hover{
    background:#f4f6f9;color:var(--text);border-color:#cbd5e1;
}

/* Right side controls */
.crm-top-right{
    margin-left:auto;display:flex;align-items:center;gap:8px;
}
/* View toggle: hidden on desktop, only useful on mobile */
#crm-view-toggle{
    display:none; /* hidden by default */
    border:1px solid var(--border);border-radius:8px;overflow:hidden;
}
.crm-view-btn{
    padding:6px 12px;font-size:12px;font-weight:600;font-family:var(--ff);
    border:none;background:#fff;color:var(--muted);cursor:pointer;
    transition:all .15s;display:flex;align-items:center;gap:5px;
}
.crm-view-btn.active{background:var(--brand);color:#fff;}

/* Draw button */
#btn-draw{
    display:inline-flex;align-items:center;gap:6px;
    border:1px solid var(--border);
    border-radius:20px 0 0 20px; /* left pill shape */
    padding:0 14px;height:36px;
    font-size:13px;font-weight:500;font-family:var(--ff);
    color:var(--text);background:#fff;cursor:pointer;
    white-space:nowrap;transition:all .15s;
    border-right:none; /* merges with clear button */
}
#btn-draw:only-child{ border-radius:20px; border-right:1px solid var(--border); }
#btn-draw:hover,#btn-draw.active{background:var(--brand);color:#fff;border-color:var(--brand);}
#btn-draw-clear{
    display:none; /* shown via JS when drawing exists */
    align-items:center;justify-content:center;
    border:1px solid var(--border);
    border-left:1px solid rgba(255,255,255,.3);
    border-radius:0 20px 20px 0; /* right pill shape */
    padding:0 11px;height:36px;
    font-size:13px;font-weight:600;font-family:var(--ff);
    color:#fff;background:#d63638;
    border-color:#d63638;
    cursor:pointer;transition:all .15s;
    white-space:nowrap;
}
#btn-draw-clear:hover{background:#b91c1c;border-color:#b91c1c;}
/* When draw active, clear button border matches */
#btn-draw.active + #btn-draw-clear{
    border-left-color:rgba(255,255,255,.25);
}

/* ══════════════════════════════
   BODY — listings left + map right
   ══════════════════════════════ */
#crm-body{
    display:flex;flex:1;overflow:hidden;
    position:relative;
}

/* ── LISTINGS PANEL (left, scrollable) ── */
#crm-listings-panel{
    flex:0 0 42%;
    height:100%;overflow-y:auto;
    background:#f8f9fa;
    display:flex;flex-direction:column;
}
#crm-listings-header{
    padding:12px 16px 8px;
    flex-shrink:0;
}
#crm-results-count{
    font-size:13px;font-weight:600;color:var(--text);
    font-family:var(--ff);
}
#crm-results-sub{
    font-size:12px;color:var(--muted);margin-top:1px;
}
#crm-results-grid{
    padding:0 12px 12px;
    display:grid;grid-template-columns:1fr 1fr;gap:12px;
}
#crm-empty{
    grid-column:1/-1;text-align:center;
    padding:48px 20px;color:var(--muted);font-size:15px;
}

/* ── CARD ── */
.crm-card{
    background:#fff;border-radius:var(--radius);
    overflow:hidden;box-shadow:var(--shadow);
    cursor:pointer;transition:box-shadow .2s,transform .15s;
    border:2px solid transparent;
    display:block;color:inherit;text-decoration:none;
}
.crm-card:hover{box-shadow:0 4px 20px rgba(0,0,0,.16);transform:translateY(-2px);}
.crm-card.active{border-color:var(--brand);}
.crm-card-img{
    width:100%;aspect-ratio:4/3;
    background:#dde3ec;position:relative;
    overflow:hidden;
}
.crm-card-img img{
    position:absolute !important;
    top:0 !important; left:0 !important;
    width:100% !important; height:100% !important;
    max-width:none !important;
    object-fit:cover !important;
    object-position:center !important;
    display:block !important;
    margin:0 !important;
    transition:transform .3s;
}
.crm-card:hover .crm-card-img img{transform:scale(1.04);}
.crm-card-img .no-photo{
    width:100%;height:100%;display:flex;
    align-items:center;justify-content:center;
    color:#a0aec0;font-size:11px;flex-direction:column;gap:4px;
}
.crm-card-badge{
    position:absolute;top:8px;left:8px;
    background:rgba(0,0,0,.55);backdrop-filter:blur(4px);
    color:#fff;font-size:9px;font-weight:700;
    padding:3px 8px;border-radius:4px;
    text-transform:uppercase;letter-spacing:.5px;
}
.crm-card-badge.rent{background:rgba(232,160,32,.9);}
.crm-card-body{padding:10px 11px 12px;}
.crm-card-price{
    font-size:16px;font-weight:800;color:var(--text);
    font-family:var(--ff);margin-bottom:3px;
}
.crm-card-title{
    font-size:12px;color:var(--muted);
    font-family:var(--ff);margin-bottom:5px;
    white-space:nowrap;overflow:hidden;text-overflow:ellipsis;
}
.crm-card-stats{
    font-size:11px;color:var(--muted);
    display:flex;align-items:center;gap:6px;flex-wrap:wrap;
}
.crm-card-stats .sep{opacity:.3;}

/* ── MAP (right, fixed height) ── */
#crm-map{
    flex:1;height:100%;position:relative;z-index:1;
}
.leaflet-interactive:focus{outline:none !important;}
path.leaflet-interactive:focus{outline:none !important;}

/* ── POLYGON STYLES ── */
/* Regions: grey outline, no fill */
/* Hoods: grey outline, no fill */
/* Hover/active: cyan */

/* ── PIN ── */
.crm-pin{
    background:#1a1a2e;color:#fff;
    font-size:11px;font-weight:700;font-family:var(--ff);
    padding:4px 9px;border-radius:20px;white-space:nowrap;
    display:inline-block;width:auto !important;
    box-shadow:0 2px 8px rgba(0,0,0,.3);border:2px solid #fff;
    cursor:pointer;transition:background .15s,transform .15s;line-height:1.2;
}
.crm-pin:hover,.crm-pin.active{background:var(--brand);transform:scale(1.1);}
.leaflet-div-icon{background:transparent !important;border:none !important;width:auto !important;height:auto !important;overflow:visible !important;}

/* ── POPUP ── */
.crm-popup{min-width:200px;font-family:var(--ff);}
.crm-popup-img{
    width:100% !important;
    height:130px !important;
    max-width:none !important;
    object-fit:cover !important;
    object-position:center !important;
    display:block !important;
    border-radius:6px;
    margin-bottom:8px;
}
.crm-popup-price{font-size:16px;font-weight:800;color:var(--text);margin-bottom:3px;}
.crm-popup-title{font-size:12px;color:var(--muted);margin-bottom:5px;}
.crm-popup-meta{font-size:11px;color:var(--muted);margin-bottom:8px;display:flex;gap:8px;flex-wrap:wrap;}
.crm-popup-link{
    display:block;background:var(--brand);color:#fff;text-align:center;
    padding:8px 12px;border-radius:8px;font-size:12px;font-weight:700;
    text-decoration:none;transition:background .2s;
}
.crm-popup-link:hover{background:var(--brand-light);}

/* ── MAP DRAWING ── */
#crm-map.drawing{cursor:crosshair !important;}

/* ── SCROLLBAR ── */
#crm-listings-panel::-webkit-scrollbar{width:4px;}
#crm-listings-panel::-webkit-scrollbar-thumb{background:var(--border);border-radius:2px;}

/* ── RESPONSIVE ── */
@media(max-width:1024px){
    #crm-results-grid{grid-template-columns:1fr 1fr;}
    #crm-listings-panel{flex:0 0 48%;}
}
@media(max-width:768px){
    #crm-body{
        flex-direction:column-reverse; /* map on top, listings below on mobile */
        /* --crm-map-vh drives both panes together — set by the split resizer JS */
        --crm-map-vh:35;
    }
    #crm-listings-panel{
        flex:0 0 auto;
        height:calc((100 - var(--crm-map-vh)) * 1dvh - 4px);
        overflow-y:auto;
    }
    #crm-map{
        flex:0 0 auto;
        height:calc(var(--crm-map-vh) * 1dvh - 4px);
    }
    #crm-split-handle{ display:flex; }
    #crm-results-grid{grid-template-columns:1fr 1fr;}
    .crm-search-wrap{min-width:140px;}
    .crm-pill span.crm-pill-label{display:none;} /* hide pill labels on mobile */
    #crm-view-toggle{display:flex;} /* show List/Map toggle on mobile only */
}
@media(max-width:480px){
    #crm-results-grid{grid-template-columns:1fr;}
    #crm-top-bar{gap:6px;padding:0 10px;}
    .crm-pill{padding:0 10px;}
}
/* ── Region property count circles ── */
.crm-region-count {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #00459B;
    color: #fff;
    font-family: 'Montserrat', sans-serif;
    font-size: 12px;
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(0,69,155,0.35);
    border: 2px solid #fff;
    /* Clickable — drills into the region. Was pointer-events:none, which
       (together with the marker's interactive:false) meant clicks on the
       badge were swallowed and never reached the region polygon. */
    pointer-events: auto;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}
.crm-region-count:hover {
    transform: scale(1.12);
    box-shadow: 0 4px 14px rgba(0,69,155,0.5);
}

/* ══════════════════════════════════════════════════
   MOBILE FILTER DRAWER — accordion of pills, bottom sheet
   ══════════════════════════════════════════════════ */

/* Trigger button — hidden on desktop, shown on mobile */
#crm-mf-trigger{
    display:none;
    align-items:center;gap:6px;
    height:36px;padding:0 14px;
    border:1px solid var(--border);border-radius:20px;
    background:#fff;color:var(--text);
    font-family:var(--ff);font-size:13px;font-weight:600;
    cursor:pointer;position:relative;flex-shrink:0;
}
#crm-mf-badge{
    display:none;
    align-items:center;justify-content:center;
    min-width:18px;height:18px;padding:0 4px;
    border-radius:50%;
    background:var(--brand);color:#fff;
    font-size:10px;font-weight:700;
}

/* Overlay */
#crm-mf-overlay{
    display:none;
    position:fixed;inset:0;
    background:rgba(0,0,0,.4);
    z-index:998;
    opacity:0;transition:opacity .25s;
}
#crm-mf-overlay.open{ display:block; opacity:1; }

/* Drawer — bottom sheet */
#crm-mf-drawer{
    display:none;
    position:fixed;left:0;right:0;bottom:0;
    max-height:85dvh;
    background:#fff;
    border-radius:18px 18px 0 0;
    box-shadow:0 -4px 32px rgba(0,0,0,.18);
    z-index:999;
    flex-direction:column;
    transform:translateY(100%);
    transition:transform .3s cubic-bezier(.4,0,.2,1);
}
#crm-mf-drawer.open{ display:flex; transform:translateY(0); }

#crm-mf-drawer-handle{
    width:40px;height:4px;border-radius:2px;
    background:#d1d5db;
    margin:10px auto 0;
    flex-shrink:0;
}
#crm-mf-drawer-header{
    display:flex;align-items:center;justify-content:space-between;
    padding:12px 20px;
    border-bottom:1px solid var(--border);
    flex-shrink:0;
}
#crm-mf-drawer-header span{
    font-family:var(--ff);font-size:16px;font-weight:700;color:var(--text);
}
#crm-mf-close{
    width:32px;height:32px;border-radius:50%;
    border:none;background:#f3f4f6;color:var(--text);
    display:flex;align-items:center;justify-content:center;
    cursor:pointer;
}

#crm-mf-drawer-body{
    flex:1;overflow-y:auto;
    -webkit-overflow-scrolling:touch;
    overscroll-behavior:contain;
    padding:8px 20px 16px;
}

#crm-mf-drawer-footer{
    display:flex;gap:10px;
    padding:14px 20px;
    padding-bottom:max(14px, env(safe-area-inset-bottom));
    border-top:1px solid var(--border);
    flex-shrink:0;
}
#crm-mf-clear{
    flex:0 0 auto;
    padding:0 18px;height:44px;
    border:1.5px solid var(--border);border-radius:10px;
    background:#fff;color:var(--text);
    font-family:var(--ff);font-size:14px;font-weight:600;
    cursor:pointer;
}
#crm-mf-apply{
    flex:1;height:44px;
    border:none;border-radius:10px;
    background:var(--brand);color:#fff;
    font-family:var(--ff);font-size:14px;font-weight:700;
    cursor:pointer;
}

/* ── Pills reparented into the drawer become accordion sections ── */
#crm-mf-drawer-body .crm-pill{
    display:flex !important; /* override any inline display:none (e.g. hood pill) */
    width:100%;
    height:auto;
    border:none;
    border-bottom:1px solid var(--border);
    border-radius:0;
    padding:14px 0;
    justify-content:space-between;
    background:transparent;
}
#crm-mf-drawer-body .crm-pill span.crm-pill-label{
    display:inline !important; /* re-show labels that the 768px rule hides elsewhere */
    font-size:14px;font-weight:600;
}
#crm-mf-drawer-body .crm-pill-dropdown{
    position:static !important;
    display:none;
    width:100%;
    box-shadow:none;border:none;
    padding:10px 0 4px;
    margin-top:0;
}
#crm-mf-drawer-body .crm-pill-dropdown.open{ display:block; }
#crm-mf-drawer-body .crm-dd-apply{ width:100%; }

@media(max-width:768px){
    #crm-mf-trigger{ display:inline-flex; }
    /* Hide the individual pills + inline Clear Filters button from the
       top bar on mobile — they live inside the drawer instead. */
    #crm-top-bar > .crm-pill,
    #crm-btn-clear{ display:none !important; }
    /* Draw area: shrink padding on mobile to save space */
    #btn-draw{ padding:0 10px; font-size:12px; }
}

/* ══════════════════════════════════════════════════
   SPLIT RESIZER HANDLE — mobile only, between map & listings
   Drag to rescale, tap-and-hold-drag for smooth resizing.
   ══════════════════════════════════════════════════ */
#crm-split-handle{
    display:none; /* hidden on desktop, shown by mobile media query */
    align-items:center;justify-content:center;
    flex:0 0 8px;
    width:100%;
    background:#fff;
    border-top:1px solid var(--border);
    border-bottom:1px solid var(--border);
    cursor:ns-resize;
    touch-action:none; /* prevent scroll while dragging */
    z-index:5;
    position:relative;
}
#crm-split-handle:hover,
#crm-split-handle.is-dragging{
    background:#f3f4f6;
}
.crm-split-grip{
    display:block;
    width:36px;height:4px;
    border-radius:2px;
    background:#cbd5e1;
    transition:background .15s;
}
#crm-split-handle:hover .crm-split-grip,
#crm-split-handle.is-dragging .crm-split-grip{
    background:var(--brand);
}
/* Extended invisible tap target so it's easy to grab on touchscreens
   without making the visible bar bigger. */
#crm-split-handle::before{
    content:'';
    position:absolute;
    top:-10px;bottom:-10px;left:0;right:0;
}
/* Disable text selection during drag anywhere in the map wrap. */
#crm-map-wrap.is-resizing,
#crm-map-wrap.is-resizing *{
    user-select:none !important;
    -webkit-user-select:none !important;
}

/* ══════════════════════════════════════════════════
   CLUSTERED PINS — multiple units at one address
   ══════════════════════════════════════════════════ */
.crm-pin-cluster {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 0 10px 0 4px;
    height: 26px;
    background: #0b1f3a;
    border: 2px solid #fff;
    border-radius: 999px;
    box-shadow: 0 2px 10px rgba(0,0,0,.3);
    cursor: pointer;
    white-space: nowrap;
    transition: transform .15s, box-shadow .15s;
}
.crm-pin-cluster:hover {
    transform: scale(1.08);
    box-shadow: 0 4px 16px rgba(0,0,0,.4);
}
.crm-pin-count {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 18px;
    height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--brand);
    color: #fff;
    font-family: var(--ff);
    font-size: 11px;
    font-weight: 800;
}
.crm-pin-range {
    color: #fff;
    font-family: var(--ff);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: .2px;
}

/* ── Cluster popup ── */
.crm-cluster-popup { font-family: var(--ff); }
.crm-cluster-head {
    font-size: 12px;
    font-weight: 700;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: .4px;
    padding: 2px 2px 8px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 6px;
}
.crm-cluster-list {
    max-height: 280px;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.crm-cluster-list::-webkit-scrollbar { width: 4px; }
.crm-cluster-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.crm-cluster-row {
    display: flex;
    gap: 10px;
    align-items: center;
    padding: 7px 6px;
    border-radius: 8px;
    text-decoration: none;
    transition: background .15s;
}
.crm-cluster-row:hover { background: #f4f6f9; }
.crm-cluster-row.is-target {
    background: #eaf1fb;
    box-shadow: inset 0 0 0 1.5px var(--brand);
}
.crm-cluster-row img,
.crm-cluster-nophoto {
    width: 52px;
    height: 44px;
    flex-shrink: 0;
    border-radius: 6px;
    object-fit: cover;
    background: #dde3ec;
    display: block;
}
.crm-cluster-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
    gap: 1px;
}
.crm-cluster-price {
    font-size: 13px;
    font-weight: 800;
    color: var(--text);
    line-height: 1.2;
}
.crm-cluster-title {
    font-size: 11px;
    color: var(--text);
    line-height: 1.3;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 190px;
}
.crm-cluster-meta {
    font-size: 10px;
    color: var(--muted);
}
