/* === page-map.css — Split-view map with filter bar + area list === */

/* Hide chat icon on map page */
.map-page ~ .chat-toggle,
body:has(.map-page) .chat-toggle { display: none; }

/* ── Full-width override ── */
.container:has(.map-page) {
  width: 100%; max-width: 100%; margin: 0; padding: 0;
}
.page-content:has(.map-page) {
  margin-top: 0; margin-bottom: 0;
}
/* Hide footer on map page */
body:has(.map-page) .site-footer { display: none; }
body:has(.map-page) {
  overflow: hidden;
  padding-bottom: 0;
}
html:has(.map-page) {
  overflow: hidden;
}

@supports not selector(:has(*)) {
  .map-page { margin: 0 calc(-50vw + 50%); width: 100vw; }
}

/* ── Page wrapper ── */
.map-page {
  position: relative;
  height: calc(100vh - 60px - 40px);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ================================================================
   FILTER BAR
   ================================================================ */
.map-filter-bar {
  flex-shrink: 0;
  background: var(--surface-card);
  border-bottom: 1px solid var(--neutral-200);
  padding: var(--space-2) var(--space-4);
  z-index: 100;
}

.filter-bar-inner {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  flex-wrap: wrap;
  max-width: 100%;
}

/* Filter groups */
.filter-group { flex-shrink: 0; }
.filter-time { order: 1; }
.filter-search { order: 4; flex: 1; min-width: 180px; }
.filter-stats { order: 5; }

/* Time pills */
.time-pills {
  display: flex;
  gap: 4px;
  background: var(--surface-elevated);
  border-radius: var(--radius-full);
  padding: 3px;
}

.pill-btn {
  padding: 5px 14px;
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  font-weight: 600;
  border: none;
  border-radius: var(--radius-full);
  background: transparent;
  color: var(--neutral-400);
  cursor: pointer;
  transition: all var(--duration-fast) var(--ease-out);
  min-height: unset;
}

.pill-btn:hover {
  color: var(--neutral-700);
  background: rgba(0,0,0,0.04);
}

.pill-btn.active {
  background: var(--primary-500);
  color: #fff;
  box-shadow: var(--glow-blue);
}

/* Dropdowns in filter bar */
.filter-bar-inner select {
  padding: 6px 10px 6px 28px;
  width: auto;
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  font-weight: 600;
  border: 1.5px solid var(--neutral-200);
  border-radius: var(--radius-full);
  background: var(--surface-card);
  color: var(--neutral-500);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%2378819F' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: left 10px center;
  min-height: 34px;
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}

.filter-bar-inner select:focus {
  outline: none;
  border-color: var(--primary-400);
  box-shadow: 0 0 0 3px rgba(5,28,201,0.12);
}

/* Search */
.search-wrapper {
  position: relative;
  display: flex;
  align-items: center;
}

.search-wrapper .search-icon {
  position: absolute;
  right: 10px;
  color: var(--neutral-300);
  pointer-events: none;
  z-index: 2;
}

.search-wrapper input {
  width: 100%;
  padding: 6px 34px 6px 12px;
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  border: 1.5px solid var(--neutral-200);
  border-radius: var(--radius-full);
  background: var(--surface-card);
  color: var(--neutral-600);
  min-height: 34px;
  transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
}

.search-wrapper input:focus {
  outline: none;
  border-color: var(--primary-400);
  box-shadow: 0 0 0 3px rgba(5,28,201,0.12);
}

.search-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  left: 0;
  background: var(--surface-card);
  border: 1px solid var(--neutral-200);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  max-height: 260px;
  overflow-y: auto;
  z-index: 200;
  direction: rtl;
}

.search-dropdown.hidden { display: none; }

.search-dropdown-item {
  padding: 8px 14px;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--neutral-600);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background var(--duration-fast);
}

.search-dropdown-item:hover,
.search-dropdown-item.active {
  background: var(--primary-50);
  color: var(--primary-500);
}

.search-dropdown-item .search-gov {
  font-size: var(--text-xs);
  font-weight: 400;
  color: var(--neutral-400);
}

/* Results badge */
.results-badge {
  display: inline-flex;
  align-items: center;
  padding: 4px 12px;
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--neutral-400);
  background: var(--surface-elevated);
  border-radius: var(--radius-full);
  white-space: nowrap;
}

/* ================================================================
   SPLIT VIEW
   ================================================================ */
.map-split {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

/* ── List panel ── */
.map-list-panel {
  width: 350px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface-base);
  border-left: 1px solid var(--neutral-200);
  z-index: 10;
}

.list-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: var(--space-3) var(--space-4);
  background: var(--surface-card);
  border-bottom: 1px solid var(--neutral-200);
  flex-shrink: 0;
}

.list-title {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: 700;
  color: var(--neutral-700);
}

.list-sort-btn {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  font-family: var(--font-primary);
  font-size: 0.65rem;
  font-weight: 600;
  color: var(--neutral-400);
  background: var(--surface-elevated);
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  min-height: unset;
  transition: color var(--duration-fast);
}

.list-sort-btn:hover { color: var(--neutral-600); }

/* Area list scroll */
.area-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-2);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

/* ── Area card ── */
.area-card {
  position: relative;
  background: var(--surface-card);
  border: 1px solid var(--neutral-200);
  border-radius: var(--radius-md);
  padding: 14px 16px 12px;
  cursor: pointer;
  transition: box-shadow var(--duration-fast) var(--ease-out),
              border-color var(--duration-fast) var(--ease-out),
              transform var(--duration-fast) var(--ease-out);
  direction: rtl;
  overflow: hidden;
  flex-shrink: 0;
}

/* Governorate color stripe (right edge in RTL) */
.area-card::before {
  content: '';
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: 2px;
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
}

/* Governorate colors */
.area-card[data-gov="العاصمة"]::before { background: #051CC9; }
.area-card[data-gov="حولي"]::before { background: #EB4A6C; }
.area-card[data-gov="الفروانية"]::before { background: #16A34A; }
.area-card[data-gov="الأحمدي"]::before { background: #AD15FF; }
.area-card[data-gov="الجهراء"]::before { background: #FF9600; }
.area-card[data-gov="مبارك الكبير"]::before { background: #0EA5E9; }

.area-card:hover {
  border-color: var(--primary-300);
  box-shadow: 0 2px 8px rgba(5,28,201,0.1);
  transform: translateY(-1px);
}

.area-card.highlighted {
  border-color: var(--primary-400);
  box-shadow: 0 0 0 2px rgba(5,28,201,0.15), var(--shadow-md);
  background: rgba(5,28,201,0.03);
}

.area-card.selected {
  border-color: var(--primary-500);
  box-shadow: 0 0 0 2px rgba(5,28,201,0.22), var(--shadow-md);
  background: rgba(5,28,201,0.04);
}

.area-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-3);
}

.area-card-name {
  font-size: var(--text-lg);
  font-weight: 700;
  color: var(--neutral-900);
  line-height: 1.3;
}

.area-card-cat {
  font-size: var(--text-xs);
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: var(--primary-50);
  color: var(--primary-700);
  white-space: nowrap;
}

.area-card-gov {
  font-size: var(--text-xs);
  font-weight: 700;
  padding: 2px 9px;
  border-radius: var(--radius-full);
  background: var(--neutral-200);
  color: var(--neutral-700);
  white-space: nowrap;
  letter-spacing: 0.01em;
}

/* Stats row — three equal columns */
.area-card-stats {
  display: flex;
  gap: 0;
  background: var(--neutral-50);
  border-radius: var(--radius-md);
  padding: 8px 0;
  border: 1px solid var(--neutral-200);
}

.area-card-stat {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  position: relative;
}

/* Vertical dividers between stats */
.area-card-stat + .area-card-stat::before {
  content: '';
  position: absolute;
  right: 0;
  top: 4px;
  bottom: 4px;
  width: 1px;
  background: var(--neutral-300);
}

.area-card-stat-value {
  font-size: var(--text-base);
  font-weight: 800;
  font-family: var(--font-mono);
  line-height: 1.2;
}

.area-card-stat-label {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--neutral-400);
  letter-spacing: 0.02em;
}

/* Stat colors — WCAG AA compliant on #F8F9FB background */
.area-card-stat.deals .area-card-stat-value { color: #15803D; }
.area-card-stat.avg .area-card-stat-value   { color: #1E40AF; }
.area-card-stat.total .area-card-stat-value  { color: #7C3AED; }

/* ── No results ── */
.area-list-empty {
  padding: var(--space-8) var(--space-4);
  text-align: center;
  color: var(--neutral-400);
  font-size: var(--text-sm);
}

/* ================================================================
   MAP CANVAS
   ================================================================ */
.map-canvas-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

#mapContainer {
  position: absolute;
  inset: 0;
  z-index: 1;
}

/* ── Loading overlay ── */
.map-loading-overlay {
  position: absolute;
  inset: 0;
  z-index: 500;
  background: rgba(255,255,255,0.5);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--duration-normal) var(--ease-out);
}

.map-loading-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.map-loading-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
}

.map-spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--neutral-200);
  border-top-color: var(--primary-400);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.map-loading-text {
  font-family: var(--font-primary);
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--neutral-700);
  background: rgba(255,255,255,0.9);
  padding: var(--space-1) var(--space-3);
  border-radius: var(--radius-sm);
}

/* ================================================================
   LEAFLET OVERRIDES
   ================================================================ */

/* Suppress focus outline on polygons */
.leaflet-interactive:focus { outline: none; }

/* Popup RTL */
.leaflet-popup-content-wrapper {
  direction: rtl;
  text-align: right;
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--neutral-200);
  background: var(--surface-card);
  color: var(--neutral-700);
}

.leaflet-popup-content {
  margin: var(--space-3) var(--space-4);
  font-family: var(--font-primary);
  font-size: var(--text-sm);
  line-height: 1.6;
}

/* Styled popup */
.map-styled-popup .leaflet-popup-content-wrapper {
  padding: 0;
  overflow: hidden;
}

.map-styled-popup .leaflet-popup-content {
  margin: 0;
  width: 240px !important;
}

.popup-header {
  background: var(--brand-gradient);
  padding: var(--space-3) var(--space-4);
  position: relative;
  overflow: hidden;
}

.popup-header::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
  pointer-events: none;
}

.popup-title {
  font-weight: 700;
  font-size: var(--text-sm);
  color: #fff;
  margin-bottom: 2px;
  position: relative;
}

.popup-gov {
  font-size: var(--text-xs);
  color: rgba(255,255,255,0.6);
  position: relative;
}

.popup-body {
  padding: var(--space-3) var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.popup-stat {
  display: flex;
  justify-content: space-between;
  gap: var(--space-3);
}

.popup-stat.popup-empty {
  justify-content: center;
  color: var(--neutral-400);
}

.popup-stat-label {
  color: var(--neutral-400);
  font-size: var(--text-xs);
}

.popup-stat-value {
  font-weight: 700;
  color: var(--neutral-700);
  font-size: var(--text-xs);
  font-family: var(--font-mono);
}

.popup-stat-value.deals { color: var(--success); }
.popup-stat-value.total { color: var(--primary-400); }
.popup-stat-value.avg   { color: var(--accent-500); }

/* ── Popup transactions ── */
.popup-transactions {
  border-top: 1px solid var(--neutral-200);
  padding: var(--space-2) var(--space-4) var(--space-3);
}

.popup-tx-title {
  font-size: var(--text-xs);
  font-weight: 700;
  color: var(--neutral-500);
  margin-bottom: var(--space-1);
}

.popup-tx-loading {
  font-size: var(--text-xs);
  color: var(--neutral-400);
  text-align: center;
  padding: var(--space-1) 0;
}

.popup-tx-card {
  padding: 4px 0;
  border-bottom: 1px solid var(--neutral-100);
  font-size: var(--text-xs);
}

.popup-tx-card:last-of-type {
  border-bottom: none;
}

.popup-tx-meta {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 2px;
}

.popup-tx-badge {
  display: inline-block;
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: 600;
  max-width: 70px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.popup-tx-badge.desc {
  background: #dbeafe;
  color: #1e40af;
}

.popup-tx-badge.cat {
  background: #fef3c7;
  color: #92400e;
}

.popup-tx-date {
  color: var(--neutral-400);
  font-weight: 500;
  font-size: 10px;
  margin-inline-start: auto;
}

.popup-tx-values {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
}

.popup-tx-price {
  color: var(--neutral-700);
  font-weight: 700;
}

.popup-tx-ratio {
  color: var(--neutral-400);
  font-size: 10px;
  font-weight: 500;
}

.popup-tx-area {
  color: var(--neutral-500);
  font-weight: 500;
}

.popup-tx-scrollable {
  max-height: 280px;
  overflow-y: auto;
  scrollbar-width: thin;
}

.popup-tx-scrollable::-webkit-scrollbar {
  width: 4px;
}

.popup-tx-scrollable::-webkit-scrollbar-thumb {
  background: var(--neutral-300);
  border-radius: 2px;
}

.popup-tx-show-all {
  display: block;
  text-align: center;
  color: #2563eb;
  font-size: var(--text-xs);
  font-weight: 600;
  cursor: pointer;
  padding: 4px 0 2px;
}

.popup-tx-show-all:hover {
  text-decoration: underline;
}

/* ── Legend ── */
.map-legend {
  background: var(--surface-card);
  border: 1px solid var(--neutral-200);
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  direction: rtl;
  font-family: var(--font-primary);
  font-size: var(--text-xs);
  line-height: 1.6;
  color: var(--neutral-500);
}

.map-legend .legend-title {
  font-weight: 700;
  margin-bottom: var(--space-1);
  color: var(--neutral-700);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-1);
  padding: 1px 0;
}

.legend-swatch {
  display: inline-block;
  width: 16px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.08);
}

.legend-label-text {
  font-size: var(--text-xs);
  color: var(--neutral-400);
}

/* ── Zone tooltip ── */
.zone-tooltip {
  background: var(--surface-elevated) !important;
  color: var(--neutral-800) !important;
  border: 1px solid var(--neutral-200) !important;
  border-radius: var(--radius-sm) !important;
  padding: var(--space-2) var(--space-3) !important;
  font-family: var(--font-primary) !important;
  font-size: var(--text-xs) !important;
  font-weight: 600 !important;
  box-shadow: var(--shadow-md) !important;
  direction: rtl !important;
}

.zone-tooltip::before {
  border-top-color: var(--surface-elevated) !important;
}

/* ── Area labels ── */
.area-label {
  background: none !important;
  border: none !important;
  box-shadow: none !important;
  transform: translateX(-50%);
}

.area-label-text {
  font-family: var(--font-primary);
  font-size: 9px; /* default; overridden by JS zoom-responsive scaling */
  font-weight: 600;
  color: var(--neutral-800);
  text-shadow:
    -1px -1px 0 rgba(255,255,255,0.85),
     1px -1px 0 rgba(255,255,255,0.85),
    -1px  1px 0 rgba(255,255,255,0.85),
     1px  1px 0 rgba(255,255,255,0.85),
     0   -1px 0 rgba(255,255,255,0.85),
     0    1px 0 rgba(255,255,255,0.85),
    -1px  0   0 rgba(255,255,255,0.85),
     1px  0   0 rgba(255,255,255,0.85);
  white-space: nowrap;
  pointer-events: none;
}

/* ── Cluster icons ── */
.cluster-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: rgba(5,28,201,0.85);
  color: #fff;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  font-weight: 700;
  border: 3px solid rgba(255,255,255,0.8);
  box-shadow: var(--shadow-md);
}

/* Fullscreen overrides */
.leaflet-fullscreen-on .map-canvas-wrap {
  position: fixed;
  inset: 0;
  z-index: 10000;
}

/* ================================================================
   MOBILE BOTTOM SHEET (hidden on desktop)
   ================================================================ */
.mobile-sheet {
  display: none;
}

/* ================================================================
   SKELETON LOADING in area list
   ================================================================ */
.area-list .skeleton-card {
  height: 88px;
  border-radius: var(--radius-md);
}

/* ================================================================
   RESPONSIVE
   ================================================================ */

/* Tablets and smaller */
@media (max-width: 900px) {
  .map-list-panel { width: 280px; }
  .filter-search { min-width: 140px; }
}

/* Mobile */
@media (max-width: 768px) {
  .map-page {
    height: calc(100vh - 60px - 40px);
  }

  /* Filter bar horizontal scroll */
  .map-filter-bar {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding: var(--space-2) var(--space-3);
  }

  .filter-bar-inner {
    flex-wrap: nowrap;
    min-width: max-content;
  }

  /* Hide search + stats on mobile */
  .filter-search { display: none; }
  .filter-stats { display: none; }

  /* Stacked layout */
  .map-split {
    flex-direction: column;
  }

  /* Hide desktop list panel on mobile */
  .map-list-panel {
    display: none;
  }

  /* Map takes most space */
  .map-canvas-wrap {
    flex: 1;
    min-height: 55vh;
  }

  /* Show mobile bottom sheet */
  .mobile-sheet {
    display: flex;
    flex-direction: column;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--surface-card);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: 0 -4px 24px rgba(0,0,0,0.12);
    z-index: 900;
    max-height: 55vh;
    transform: translateY(calc(100% - 68px));
    transition: transform 0.3s var(--ease-out);
    will-change: transform;
    touch-action: none;
  }

  .mobile-sheet.dragging {
    transition: none;
  }

  .mobile-sheet.expanded {
    transform: translateY(0);
  }

  /* Legend above mobile bottom sheet */
  .map-legend {
    z-index: 1000;
    margin-bottom: 74px !important;
    margin-right: 6px !important;
  }

  /* Map popup width on mobile */
  .map-styled-popup .leaflet-popup-content {
    width: min(280px, calc(100vw - 60px)) !important;
  }

  .popup-tx-meta {
    flex-wrap: wrap;
  }

  /* Mobile search inside bottom sheet */
  .sheet-search-input {
    width: 100%;
    padding: 8px 12px;
    font-family: var(--font-primary);
    font-size: 16px;
    border: 1.5px solid var(--neutral-200);
    border-radius: var(--radius-full);
    background: var(--surface-elevated);
    color: var(--neutral-600);
    direction: rtl;
    margin-bottom: var(--space-2);
    min-height: 40px;
    transition: border-color var(--duration-fast), box-shadow var(--duration-fast);
  }

  .sheet-search-input:focus {
    outline: none;
    border-color: var(--primary-400);
    box-shadow: 0 0 0 3px rgba(5,28,201,0.12);
  }

  .sheet-handle {
    padding: var(--space-3) var(--space-4);
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-2);
    flex-shrink: 0;
  }

  .handle-bar {
    width: 36px;
    height: 4px;
    background: var(--neutral-300);
    border-radius: 2px;
  }

  .sheet-peek-stats {
    display: flex;
    align-items: center;
    gap: var(--space-4);
  }

  .peek-stat {
    font-family: var(--font-primary);
    font-size: var(--text-xs);
    color: var(--neutral-400);
  }

  .peek-value {
    font-weight: 700;
    color: var(--primary-400);
    font-size: var(--text-sm);
    font-family: var(--font-mono);
    margin-inline-end: 3px;
  }

  .peek-divider {
    width: 1px;
    height: 14px;
    background: var(--neutral-200);
  }

  .sheet-body {
    flex: 1;
    overflow-y: auto;
    padding: 0 var(--space-3) var(--space-4);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
  }

}

@media (max-width: 480px) {
  .pill-btn {
    padding: 4px 10px;
    font-size: 0.6rem;
  }
}
