:root {
  --primary: #5b2c91;
  --primary-light: #8e44ad;
  --primary-lighter: #a569bd;
  --primary-dark: #3f1d66;
  --bg: #f7f5fb;
  --surface: #ffffff;
  --surface-2: #f1ecf9;
  --text: #2b2b2b;
  --text-muted: #767676;
  --border: #e6def2;
  --success: #1e8449;
  --danger: #c0392b;
  --warning: #b7950b;
  --shadow: 0 4px 20px rgba(94, 44, 145, 0.08);
  --radius: 12px;
  --sidebar-w: 260px;
  --header-h: 64px;
}

[data-theme="dark"] {
  --primary: #a569bd;
  --primary-light: #8e44ad;
  --primary-lighter: #c39bd3;
  --primary-dark: #6c3b9e;
  --bg: #17141d;
  --surface: #201c29;
  --surface-2: #2a2433;
  --text: #eee7f5;
  --text-muted: #a89bb8;
  --border: #362d45;
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: Tahoma, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  direction: rtl;
  transition: background .25s ease, color .25s ease;
}

a { text-decoration: none; color: inherit; }
button { font-family: inherit; cursor: pointer; }
ul { list-style: none; }

.btn-action-edit { background: #ffc107; color: #212529; }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 11px 22px;
  border-radius: 9px;
  font-size: 14px;
  font-weight: 600;
  transition: background .2s;
}
.btn-primary:hover { background: var(--primary-dark); }
.btn-block { width: 100%; }

.btn-outline {
  background: transparent;
  border: 1.5px solid var(--primary);
  color: var(--primary);
  padding: 9px 18px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
}
.btn-outline:hover { background: var(--surface-2); }

.btn-danger {
  background: var(--danger);
  color: #fff;
  border: none;
  padding: 9px 18px;
  border-radius: 9px;
  font-size: 13px;
}

.btn-sm { padding: 6px 12px; font-size: 12px; border-radius: 7px; }

.login-page {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--bg), var(--surface-2));
}
.login-wrap { width: 100%; max-width: 420px; padding: 20px; }
.login-card {
  background: var(--surface);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 40px 32px;
}
.login-logo { text-align: center; margin-bottom: 28px; }
.logo-circle {
  width: 64px; height: 64px;
  background: linear-gradient(135deg, var(--primary-light), var(--primary));
  color: #fff;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 28px; font-weight: bold;
  margin: 0 auto 14px;
}
.login-logo h1 { font-size: 18px; color: var(--text); margin-bottom: 6px; }
.login-logo p { font-size: 13px; color: var(--text-muted); }

.form-group { margin-bottom: 18px; }
.form-group label { display: block; font-size: 13px; margin-bottom: 6px; color: var(--text); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%;
  padding: 11px 14px;
  border: 1.5px solid var(--border);
  border-radius: 9px;
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none;
  border-color: var(--primary-light);
}

.login-note { text-align: center; font-size: 12px; color: var(--text-muted); margin-top: 18px; }

.alert { padding: 12px 14px; border-radius: 9px; font-size: 13px; margin-bottom: 18px; }
.alert-error { background: #fdecea; color: var(--danger); }
[data-theme="dark"] .alert-error { background: #3a1c1c; }
.alert-success { background: #eafaf1; color: var(--success); }
[data-theme="dark"] .alert-success { background: #16301f; }

.theme-toggle {
  position: fixed; bottom: 22px; left: 22px;
  width: 46px; height: 46px;
  border-radius: 50%;
  background: var(--surface);
  border: 1.5px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 18px;
  z-index: 1001;
}

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: var(--sidebar-w);
  background: var(--surface);
  border-left: 1px solid var(--border);
  padding: 20px 0;
  position: fixed;
  right: 0; top: 0; bottom: 0;
  overflow-y: auto;
  transition: transform .25s ease;
  z-index: 3000;
}
.sidebar .brand {
  display: flex; align-items: center; gap: 10px;
  padding: 0 20px 20px; border-bottom: 1px solid var(--border); margin-bottom: 14px;
}
.sidebar .brand .logo-circle { width: 40px; height: 40px; font-size: 18px; margin: 0; }
.sidebar .brand span { font-size: 16px; font-weight: bold; color: var(--primary); }

.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 22px;
  color: var(--text-muted);
  font-size: 14px;
  border-right: 3px solid transparent;
}
.nav-item:hover { background: var(--surface-2); color: var(--text); }
.nav-item.active {
  background: var(--surface-2);
  color: var(--primary);
  border-right-color: var(--primary);
  font-weight: 600;
}

.main-content {
  margin-right: var(--sidebar-w);
  flex: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  position: sticky; top: 0; 
  z-index: 2000;
}
.topbar .page-title { font-size: 16px; font-weight: 600; }
.topbar .user-menu { display: flex; align-items: center; gap: 14px; }
.topbar .user-badge {
  font-size: 13px; color: var(--text-muted);
  display: flex; align-items: center; gap: 8px;
}
.role-pill {
  font-size: 11px; padding: 3px 10px; border-radius: 20px;
  background: var(--surface-2); color: var(--primary);
}

.content-area { padding: 24px; flex: 1; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}
.card h3 { font-size: 15px; margin-bottom: 14px; color: var(--text); }

.grid-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}
.stat-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  border-right: 4px solid var(--primary-light);
}
.stat-box .num { font-size: 26px; font-weight: bold; color: var(--primary); }
.stat-box .label { font-size: 13px; color: var(--text-muted); margin-top: 4px; }

table.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data-table th, table.data-table td {
  padding: 10px 12px; text-align: right; border-bottom: 1px solid var(--border);
}
table.data-table th { background: var(--surface-2); color: var(--text-muted); font-weight: 600; }
table.data-table tr:hover td { background: var(--surface-2); }

.badge { padding: 3px 10px; border-radius: 20px; font-size: 11px; }
.badge-success { background: #eafaf1; color: var(--success); }
.badge-danger { background: #fdecea; color: var(--danger); }
[data-theme="dark"] .badge-success { background: #16301f; }
[data-theme="dark"] .badge-danger { background: #3a1c1c; }

/* ==========================================================
   نقشه و کنترل‌های ثابت
   ========================================================== */
#map-container { 
  position: relative; 
  height: calc(100vh - var(--header-h) - 48px); 
  border-radius: var(--radius); 
  overflow: hidden; 
  box-shadow: var(--shadow); 
}
#map { width: 100%; height: 100%; background: #ddd; z-index: 1; }

.leaflet-control-zoom {
  border: 1px solid var(--border) !important;
  box-shadow: var(--shadow) !important;
  border-radius: 8px !important;
  overflow: hidden;
  margin-top: 14px !important;
  margin-left: 14px !important;
}
.leaflet-control-zoom a {
  background-color: var(--surface) !important;
  color: var(--text) !important;
  border-bottom: 1px solid var(--border) !important;
}

/* ==========================================================
   کنترل‌های نقشه (دکمه لایه‌ها + کادر جستجو)
   ========================================================== */

.layers-toggle-btn {
  position: absolute;
  top: 14px;
  left: 60px;
  z-index: 1600;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1.5px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.map-controls {
  position: absolute; 
  top: 58px; 
  left: 60px; 
  z-index: 1650;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  width: 240px;
  max-height: calc(100% - 80px);
  overflow-y: auto;
  border: 1px solid var(--border);
}
.map-controls h4 { font-size: 13px; margin-bottom: 10px; color: var(--primary); }
.layer-toggle { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.layer-toggle:last-child { border-bottom: none; }
.layer-toggle .layer-name { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.layer-dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }

.switch { position: relative; width: 40px; height: 22px; display:inline-block; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch .slider { position: absolute; inset: 0; background: var(--border); border-radius: 20px; transition: .2s; cursor:pointer; }
.switch .slider:before { content: ''; position: absolute; width: 16px; height: 16px; right: 3px; top: 3px; background: #fff; border-radius: 50%; transition: .2s; }
.switch input:checked + .slider { background: var(--primary); }
.switch input:checked + .slider:before { transform: translateX(-18px); }

/* کادر جستجو */
.map-search-box {
  position: absolute; 
  top: 14px; 
  right: 14px; 
  z-index: 1700;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  width: 280px;
  max-width: calc(100% - 360px);
  border: 1px solid var(--border);
}

.search-input-wrapper {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.map-search-box input { 
  width: 100%; 
  padding: 8px 12px; 
  border: 1.5px solid var(--border); 
  border-radius: 8px; 
  background: var(--surface); 
  color: var(--text); 
  font-size: 13px; 
  outline: none;
}
.map-search-box input:focus { border-color: var(--primary-light); }

.search-results { 
  max-height: 200px; 
  overflow-y: auto; 
  margin-top: 6px; 
  border-top: 1px solid var(--border);
  padding-top: 4px;
}

/* سوییچر نقشه در گوشه پایین راست */
.map-source-switch {
  position: absolute; 
  bottom: 14px; 
  right: 14px; 
  z-index: 1500;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 6px;
  display: flex; 
  gap: 6px;
  border: 1px solid var(--border);
}
.map-source-switch button {
  border: none; background: transparent; padding: 6px 10px; border-radius: 7px; font-size: 12px; color: var(--text-muted);
}
.map-source-switch button.active { background: var(--primary); color: #fff; }

.offline-indicator {
  position: absolute; bottom: 14px; left: 14px; z-index: 1500;
  background: var(--warning); color: #fff;
  padding: 7px 14px; border-radius: 8px; font-size: 12px;
  display: none;
}
.offline-indicator.show { display: block; }

/* ==========================================================
   مودال‌ها و پاپ‌آپ‌ها
   ========================================================== */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  display: none; align-items: center; justify-content: center; z-index: 4000;
}
.modal-overlay.show { display: flex; }
.modal-box {
  background: var(--surface);
  border-radius: 16px;
  width: 100%; max-width: 480px;
  max-height: 90vh; overflow-y: auto;
  padding: 26px;
  position: relative;
}
.modal-box h3 { color: var(--primary); margin-bottom: 18px; font-size: 16px; }
.modal-close { position: absolute; top: 14px; left: 14px; font-size: 20px; color: var(--text-muted); background:none; border:none; }

.route-buttons { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.route-buttons a {
  flex: 1; text-align: center;
  padding: 8px; border-radius: 8px; font-size: 12px;
  background: var(--surface-2); color: var(--primary);
  border: 1px solid var(--border);
  min-width: 80px;
}

.leaflet-popup-content-wrapper { background: var(--surface) !important; color: var(--text) !important; border-radius: 10px !important; }
.leaflet-popup-tip { background: var(--surface) !important; }
.custom-popup { font-size: 13px; }
.custom-popup table { width:100%; }
.custom-popup td { padding: 3px 0; }
.custom-popup .popup-title { font-weight:bold; color: var(--primary); margin-bottom:6px; font-size:14px; }
.popup-actions { margin-top:10px; display:flex; gap:6px; flex-wrap:wrap; }
.popup-actions button, .popup-actions a {
  flex:1; text-align:center; font-size:11px; padding:6px 8px; border-radius:6px;
  background: var(--primary); color:#fff; border:none; min-width:70px;
}

.file-hint { font-size:11px; color:var(--text-muted); margin-top:4px; }

#btnLocation.active, #btnAddPoint.active {
  background: var(--primary) !important;
  color: #ffffff !important;
}

.user-location-marker-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}
.user-location-marker-icon div { position: relative; }
.user-location-marker-icon div::after {
  content: '';
  position: absolute;
  top: -5px; left: -5px; right: -5px; bottom: -5px;
  border-radius: 50%;
  border: 2px solid #2196F3;
  animation: pulse-location 1.8s infinite ease-in-out;
}

@keyframes pulse-location {
  0% { transform: scale(0.8); opacity: 1; }
  100% { transform: scale(2.4); opacity: 0; }
}

.zones-section {
  margin-top: 10px; padding-top: 10px;
  border-top: 1px dashed var(--border);
}
.zones-section h4 { font-size: 13px; margin-bottom: 10px; color: var(--primary); }
.zone-toggle { display: flex; align-items: center; justify-content: space-between; padding: 8px 0; border-bottom: 1px solid var(--border); }
.zone-toggle:last-child { border-bottom: none; }
.zone-toggle .zone-name { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.zone-dot { width: 12px; height: 12px; border-radius: 50%; display: inline-block; }

#zoomHint {
  position: absolute; top: 14px; left: 50%; transform: translateX(-50%);
  z-index: 1600; background: var(--surface); color: var(--text);
  padding: 8px 16px; border-radius: 8px; box-shadow: var(--shadow);
  font-size: 12px; display: none; pointer-events: none;
}

.bottom-actions-bar {
  position: absolute; bottom: 14px; left: 14px; z-index: 1500;
  display: flex; flex-direction: column; gap: 8px;
}
.bottom-action-btn {
  display: flex; align-items: center; gap: 6px;
  background: var(--surface); border: 1.5px solid var(--border);
  color: var(--text); padding: 8px 14px; border-radius: 9px;
  font-size: 13px; font-weight: 600; box-shadow: var(--shadow);
}

.finish-polygon-btn { position: absolute; bottom: 70px; left: 14px; z-index: 2000; display: none; }

.mobile-panel-close {
  display: none;
  position: absolute;
  top: 10px;
  left: 10px;
  background: none;
  border: none;
  font-size: 18px;
  color: var(--text-muted);
}

.mobile-panel-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 2800;
}
.mobile-panel-backdrop.show { display: block; }

/* ==========================================================
   طراحی ریسپانسیو (موبایل و تبلت)
   ========================================================== */
@media (max-width: 900px) {
  .sidebar { transform: translateX(100%); }
  .sidebar.open { transform: translateX(0); }
  .main-content { margin-right: 0; }
  .hamburger { display: block; background: none; border: none; font-size: 22px; color: var(--text); }
}

@media (max-width: 780px) {
  #map-container { height: calc(100vh - var(--header-h) - 24px); }

  .layers-toggle-btn { top: 12px; left: 12px; }

  .map-search-box {
    top: 12px; right: 12px; left: 110px;
    width: auto; max-width: none;
  }

  .map-controls {
    display: none; position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 82%; max-width: 320px; max-height: 100vh;
    border-radius: 0; z-index: 2900;
  }
  .map-controls.show { display: block; }
  .mobile-panel-close { display: block; }

  .map-source-switch { bottom: 70px; right: 12px; }

  .bottom-actions-bar {
    left: 0; right: 0; bottom: 0;
    flex-direction: row; justify-content: space-around;
    background: var(--surface); padding: 8px 4px;
    border-top: 1px solid var(--border);
  }
  .bottom-action-btn { flex-direction: column; border: none; box-shadow: none; font-size: 10px; padding: 4px; }
}

.map-point-label {
    background: rgba(255, 255, 255, 0.9) !important;
    border: 1px solid var(--primary) !important;
    border-radius: 4px !important;
    padding: 1px 5px !important;
    font-size: 11px !important;
    font-weight: bold !important;
    color: var(--text) !important;
}
[data-theme="dark"] .map-point-label { background: rgba(32, 28, 41, 0.9) !important; color: #fff !important; }
.map-point-label::before { display: none !important; }