@import url('https://fonts.googleapis.com/css2?family=Cairo:wght@300;400;600;700;900&family=Tajawal:wght@400;500;700&display=swap');

:root {
  --bg-primary: #0a0e17;
  --bg-secondary: #111827;
  --bg-card: #1a2233;
  --bg-card-hover: #1f2b3f;
  --border: #2a3a52;
  --text-primary: #e8edf5;
  --text-secondary: #8899b0;
  --text-muted: #5a6a80;
  --accent: #f59e0b;
  --danger: #ef4444;
  --danger-glow: rgba(239,68,68,0.3);
  --warning: #f97316;
  --warning-glow: rgba(249,115,22,0.25);
  --info: #3b82f6;
  --success: #10b981;
  --ap-color: #ef4444;
  --at-color: #f59e0b;
  --mixed-color: #f97316;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
  --transition: all 0.3s cubic-bezier(0.4,0,0.2,1);
}

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

html { font-size: 14px; }

body {
  font-family: 'Cairo', 'Tajawal', sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  direction: rtl;
  overflow: hidden;
  height: 100vh;
  width: 100vw;
}

/* ═══════ HEADER ═══════ */
#app-header {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 56px;
  background: linear-gradient(135deg, var(--bg-secondary) 0%, #0f1629 100%);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 1000;
  backdrop-filter: blur(12px);
}

.header-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.header-brand .brand-icon {
  width: 36px; height: 36px;
  background: linear-gradient(135deg, var(--danger), var(--warning));
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  box-shadow: 0 0 20px var(--danger-glow);
  animation: pulse-glow 2s ease-in-out infinite;
}

@keyframes pulse-glow {
  0%, 100% { box-shadow: 0 0 20px var(--danger-glow); }
  50% { box-shadow: 0 0 35px var(--danger-glow), 0 0 60px rgba(239,68,68,0.15); }
}

.header-brand h1 {
  font-size: 1.15rem;
  font-weight: 700;
  background: linear-gradient(90deg, var(--text-primary), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: -0.3px;
}

.header-stats {
  display: flex;
  gap: 20px;
  align-items: center;
}

.stat-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(255,255,255,0.05);
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  border: 1px solid rgba(255,255,255,0.08);
  transition: var(--transition);
}

.stat-chip:hover {
  background: rgba(255,255,255,0.1);
  transform: translateY(-1px);
}

.stat-chip .stat-value {
  font-weight: 700;
  color: var(--accent);
}

.stat-chip .stat-label { color: var(--text-secondary); }

.export-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 18px;
  background: linear-gradient(135deg, #10b981, #059669);
  color: #fff;
  border: none;
  border-radius: 20px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  box-shadow: 0 2px 12px rgba(16,185,129,0.3);
}

.export-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 20px rgba(16,185,129,0.5);
  background: linear-gradient(135deg, #34d399, #10b981);
}

.export-btn:active { transform: translateY(0); }

/* ═══════ SIDEBAR ═══════ */
#sidebar {
  position: fixed;
  top: 56px;
  right: 0;
  width: 340px;
  height: calc(100vh - 56px);
  background: var(--bg-secondary);
  border-left: 1px solid var(--border);
  z-index: 900;
  display: flex;
  flex-direction: column;
  transition: transform 0.4s cubic-bezier(0.4,0,0.2,1);
  overflow: hidden;
}

#sidebar.collapsed { transform: translateX(100%); }

.sidebar-toggle {
  position: fixed;
  top: 70px;
  right: 340px;
  width: 32px; height: 32px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-right: none;
  border-radius: 8px 0 0 8px;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 901;
  transition: var(--transition);
  font-size: 14px;
}

.sidebar-toggle:hover {
  background: var(--bg-card-hover);
  color: var(--text-primary);
}

#sidebar.collapsed ~ .sidebar-toggle { right: 0; }

/* Search */
.sidebar-search {
  padding: 14px;
  border-bottom: 1px solid var(--border);
}

.search-input-wrap {
  position: relative;
}

.search-input-wrap input {
  width: 100%;
  padding: 10px 14px 10px 36px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 0.9rem;
  outline: none;
  transition: var(--transition);
}

.search-input-wrap input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(245,158,11,0.15);
}

.search-input-wrap input::placeholder { color: var(--text-muted); }

.search-input-wrap .search-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  font-size: 14px;
}

/* Filters */
.sidebar-filters {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.filter-btn {
  padding: 5px 12px;
  border-radius: 16px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-secondary);
  font-family: inherit;
  font-size: 0.78rem;
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
}

.filter-btn:hover { border-color: var(--accent); color: var(--accent); }

.filter-btn.active {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
  font-weight: 600;
}

.filter-btn.type-ap.active { background: var(--ap-color); border-color: var(--ap-color); color: #fff; }
.filter-btn.type-at.active { background: var(--at-color); border-color: var(--at-color); color: #000; }
.filter-btn.type-mixed.active { background: var(--mixed-color); border-color: var(--mixed-color); color: #fff; }

/* Field List */
.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.sidebar-list::-webkit-scrollbar { width: 5px; }
.sidebar-list::-webkit-scrollbar-track { background: transparent; }
.sidebar-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 10px; }

.field-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: var(--transition);
  position: relative;
  overflow: hidden;
}

.field-card::before {
  content: '';
  position: absolute;
  top: 0; right: 0;
  width: 4px; height: 100%;
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
}

.field-card.type-ap::before { background: var(--ap-color); }
.field-card.type-at::before { background: var(--at-color); }
.field-card.type-mixed::before { background: var(--mixed-color); }

.field-card:hover {
  background: var(--bg-card-hover);
  border-color: rgba(245,158,11,0.3);
  transform: translateX(-3px);
}

.field-card-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 6px;
}

.field-card-id {
  font-size: 0.75rem;
  color: var(--text-muted);
  background: rgba(255,255,255,0.05);
  padding: 2px 8px;
  border-radius: 10px;
}

.field-card-type {
  font-size: 0.72rem;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
}

.field-card-type.ap { background: rgba(239,68,68,0.15); color: var(--ap-color); }
.field-card-type.at { background: rgba(245,158,11,0.15); color: var(--at-color); }
.field-card-type.mixed { background: rgba(249,115,22,0.15); color: var(--mixed-color); }

.field-card-location {
  font-size: 0.85rem;
  color: var(--text-primary);
  font-weight: 500;
  margin-bottom: 4px;
  line-height: 1.4;
}

.field-card-meta {
  display: flex;
  gap: 12px;
  font-size: 0.75rem;
  color: var(--text-secondary);
}

.field-card-meta span { display: flex; align-items: center; gap: 4px; }

/* ═══════ MAP ═══════ */
#map-container {
  position: fixed;
  top: 56px;
  left: 0;
  right: 340px;
  bottom: 0;
  transition: right 0.4s cubic-bezier(0.4,0,0.2,1);
}

#sidebar.collapsed ~ #map-container { right: 0; }

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

/* Leaflet Popup Custom */
.leaflet-popup-content-wrapper {
  background: var(--bg-card) !important;
  color: var(--text-primary) !important;
  border-radius: var(--radius) !important;
  border: 1px solid var(--border) !important;
  box-shadow: var(--shadow) !important;
  font-family: 'Cairo', sans-serif !important;
  direction: rtl !important;
  min-width: 280px;
}

.leaflet-popup-tip { background: var(--bg-card) !important; }

.leaflet-popup-content { margin: 0 !important; padding: 0 !important; }

.popup-content { padding: 16px; }

.popup-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid var(--border);
}

.popup-title {
  font-size: 1rem;
  font-weight: 700;
}

.popup-badge {
  font-size: 0.75rem;
  padding: 3px 10px;
  border-radius: 12px;
  font-weight: 600;
}

.popup-badge.ap { background: rgba(239,68,68,0.2); color: var(--ap-color); }
.popup-badge.at { background: rgba(245,158,11,0.2); color: var(--at-color); }
.popup-badge.mixed { background: rgba(249,115,22,0.2); color: var(--mixed-color); }

.popup-detail {
  display: flex;
  justify-content: space-between;
  padding: 6px 0;
  font-size: 0.85rem;
  border-bottom: 1px solid rgba(255,255,255,0.04);
}

.popup-detail:last-child { border: none; }
.popup-detail .label { color: var(--text-secondary); }
.popup-detail .value { color: var(--text-primary); font-weight: 600; }

.popup-danger {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  font-size: 0.8rem;
  text-align: center;
  font-weight: 600;
}

.popup-danger.high { background: rgba(239,68,68,0.15); color: var(--ap-color); border: 1px solid rgba(239,68,68,0.3); }
.popup-danger.medium { background: rgba(249,115,22,0.15); color: var(--mixed-color); border: 1px solid rgba(249,115,22,0.3); }

/* Custom Markers */
.mine-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 2px 10px rgba(0,0,0,0.5);
  font-size: 12px;
  transition: transform 0.2s;
}

.mine-marker:hover { transform: scale(1.3); }

.mine-marker.ap { background: var(--ap-color); }
.mine-marker.at { background: var(--at-color); }
.mine-marker.mixed { background: var(--mixed-color); }

/* Legend */
#map-legend {
  position: fixed;
  bottom: 20px;
  left: 20px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  z-index: 800;
  min-width: 200px;
  backdrop-filter: blur(12px);
}

.legend-title {
  font-size: 0.85rem;
  font-weight: 700;
  margin-bottom: 10px;
  color: var(--text-primary);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.legend-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.3);
  flex-shrink: 0;
}

.legend-dot.ap { background: var(--ap-color); }
.legend-dot.at { background: var(--at-color); }
.legend-dot.mixed { background: var(--mixed-color); }

/* Region Stats Panel */
#region-stats {
  position: fixed;
  bottom: 20px;
  left: 240px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 18px;
  z-index: 800;
  display: flex;
  gap: 16px;
  backdrop-filter: blur(12px);
}

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

.region-stat-value {
  font-size: 1.2rem;
  font-weight: 900;
  color: var(--accent);
  line-height: 1;
}

.region-stat-label {
  font-size: 0.7rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Cluster styles */
.marker-cluster-small, .marker-cluster-medium, .marker-cluster-large {
  background: rgba(239,68,68,0.3) !important;
}

.marker-cluster-small div, .marker-cluster-medium div, .marker-cluster-large div {
  background: rgba(239,68,68,0.7) !important;
  color: #fff !important;
  font-family: 'Cairo', sans-serif !important;
  font-weight: 700 !important;
}

/* ═══════ RESPONSIVE ═══════ */
@media (max-width: 768px) {
  #sidebar { width: 100%; }
  #map-container { right: 0; }
  #sidebar:not(.collapsed) ~ #map-container { display: none; }
  .sidebar-toggle { right: 0; }
  #sidebar:not(.collapsed) ~ .sidebar-toggle { right: calc(100% - 32px); }
  .header-stats { display: none; }
  #region-stats { left: 20px; bottom: 70px; flex-wrap: wrap; gap: 10px; }
  #map-legend { bottom: 150px; }
  .stat-chip { padding: 4px 10px; font-size: 0.75rem; }
}

/* ═══════ ANIMATIONS ═══════ */
@keyframes fadeInUp {
  from { opacity: 0; transform: translateY(15px); }
  to { opacity: 1; transform: translateY(0); }
}

.field-card { animation: fadeInUp 0.4s ease-out backwards; }

.field-card:nth-child(1) { animation-delay: 0.02s; }
.field-card:nth-child(2) { animation-delay: 0.04s; }
.field-card:nth-child(3) { animation-delay: 0.06s; }
.field-card:nth-child(4) { animation-delay: 0.08s; }
.field-card:nth-child(5) { animation-delay: 0.1s; }

/* Loading overlay */
#loading-overlay {
  position: fixed;
  inset: 0;
  background: var(--bg-primary);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  transition: opacity 0.6s ease;
}

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

.loading-icon {
  font-size: 48px;
  animation: pulse-glow 1.5s ease-in-out infinite;
  margin-bottom: 16px;
}

.loading-text {
  font-size: 1.1rem;
  color: var(--text-secondary);
  animation: fadeInUp 0.8s ease;
}

.loading-bar {
  width: 200px;
  height: 3px;
  background: var(--border);
  border-radius: 4px;
  margin-top: 16px;
  overflow: hidden;
}

.loading-bar-fill {
  width: 0%;
  height: 100%;
  background: linear-gradient(90deg, var(--danger), var(--accent));
  border-radius: 4px;
  animation: loadBar 1.5s ease-out forwards;
}

@keyframes loadBar {
  to { width: 100%; }
}
