/* ====== TOKENS ====== */
:root, [data-theme="light"] {
  --color-bg: #f7f6f2;
  --color-surface: #ffffff;
  --color-surface-2: #f9f8f5;
  --color-border: #d4d1ca;
  --color-text: #28251d;
  --color-text-muted: #7a7974;
  --color-text-faint: #bab9b4;
  --color-primary: #01696f;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.08);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.12);
  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 14px;
  --font-body: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;

  /* Stage colors */
  --stage-prospect: #6366f1;
  --stage-contact: #f59e0b;
  --stage-tasting: #8b5cf6;
  --stage-no-order: #ef4444;
  --stage-won: #10b981;
}

[data-theme="dark"] {
  --color-bg: #171614;
  --color-surface: #1e1d1b;
  --color-surface-2: #252422;
  --color-border: #393836;
  --color-text: #e0dfdd;
  --color-text-muted: #8a8985;
  --color-text-faint: #5a5957;
  --color-primary: #4f98a3;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 16px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);

  --stage-prospect: #818cf8;
  --stage-contact: #fbbf24;
  --stage-tasting: #a78bfa;
  --stage-no-order: #f87171;
  --stage-won: #34d399;
}

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

html, body {
  height: 100%;
  font-family: var(--font-body);
  font-size: 14px;
  color: var(--color-text);
  background: var(--color-bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

/* ====== HEADER ====== */
.header {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo {
  color: var(--color-primary);
  flex-shrink: 0;
}

.header h1 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.02em;
  line-height: 1.2;
}

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

.deal-count {
  font-size: 12px;
  font-weight: 500;
  color: var(--color-text-muted);
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  padding: 4px 10px;
  border-radius: 999px;
}

/* ====== OWNER FILTER ====== */
.owner-filter {
  position: relative;
}

.owner-filter select {
  appearance: none;
  -webkit-appearance: none;
  background: var(--color-surface-2);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 5px 28px 5px 10px;
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font-body);
  color: var(--color-text);
  cursor: pointer;
  transition: all 0.15s ease;
  background-image: url("data:image/svg+xml,%3Csvg width='10' height='6' viewBox='0 0 10 6' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1L5 5L9 1' stroke='%237a7974' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}

.owner-filter select:hover {
  border-color: var(--color-primary);
}

.owner-filter select:focus {
  outline: 2px solid var(--color-primary);
  outline-offset: 1px;
}

[data-theme-toggle] {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 999px;
  background: none;
  border: 1px solid var(--color-border);
  color: var(--color-text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

[data-theme-toggle]:hover {
  background: var(--color-surface-2);
  color: var(--color-text);
}

/* ====== MAP ====== */
#map {
  position: fixed;
  top: 49px;
  left: 0;
  right: 0;
  bottom: 0;
}

/* ====== LEGEND ====== */
.legend {
  position: fixed;
  bottom: 24px;
  left: 16px;
  z-index: 1000;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 12px 16px;
  min-width: 220px;
  transition: all 0.2s ease;
}

.legend.collapsed {
  min-width: auto;
  padding: 8px 12px;
}

.legend.collapsed .legend-items {
  display: none;
}

.legend-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: none;
  color: var(--color-text-muted);
  cursor: pointer;
  padding: 2px;
  transition: transform 0.2s ease;
  position: absolute;
  top: 10px;
  right: 10px;
  width: 20px;
  height: 20px;
}

.legend.collapsed .legend-toggle {
  position: static;
  transform: rotate(180deg);
}

.legend-items {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  padding: 3px 4px;
  border-radius: var(--radius-sm);
  transition: opacity 0.15s ease, background 0.15s ease;
  user-select: none;
}

.legend-item:hover {
  background: var(--color-surface-2);
}

.legend-item.dimmed {
  opacity: 0.3;
}

.legend-item .dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 2px solid transparent;
}

.legend-item .count {
  margin-left: auto;
  color: var(--color-text-faint);
  font-size: 11px;
  font-weight: 400;
  padding-right: 14px;
}

/* ====== SEARCH ====== */
.search-box {
  position: fixed;
  top: 60px;
  right: 16px;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  padding: 8px 12px;
  box-shadow: var(--shadow-md);
  width: 260px;
  transition: width 0.2s ease;
}

.search-box:focus-within {
  border-color: var(--color-primary);
  width: 320px;
}

.search-box svg {
  flex-shrink: 0;
  color: var(--color-text-faint);
}

.search-box input {
  border: none;
  background: none;
  outline: none;
  font-size: 13px;
  width: 100%;
  color: var(--color-text);
}

.search-box input::placeholder {
  color: var(--color-text-faint);
}

.search-results {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  max-height: 280px;
  overflow-y: auto;
}

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

.search-result-item {
  padding: 8px 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  transition: background 0.1s ease;
  border-bottom: 1px solid var(--color-border);
}

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

.search-result-item:hover {
  background: var(--color-surface-2);
}

.search-result-item .result-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.search-result-item .result-name {
  font-weight: 500;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.search-result-item .result-stage {
  font-size: 11px;
  color: var(--color-text-muted);
  flex-shrink: 0;
}

/* ====== POPUP ====== */
.leaflet-popup-content-wrapper {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  border-radius: var(--radius-lg) !important;
  box-shadow: var(--shadow-lg) !important;
  border: 1px solid var(--color-border) !important;
  padding: 0 !important;
}

.leaflet-popup-content {
  margin: 0 !important;
  font-family: var(--font-body) !important;
  min-width: 200px !important;
}

.leaflet-popup-tip {
  background: var(--color-surface) !important;
  border: 1px solid var(--color-border) !important;
  box-shadow: none !important;
}

.popup-inner {
  padding: 14px 16px;
}

.popup-stage {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 999px;
  margin-bottom: 8px;
}

.popup-name {
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
  margin-bottom: 4px;
}

.popup-address {
  font-size: 12px;
  color: var(--color-text-muted);
  line-height: 1.4;
}

.popup-value {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid var(--color-border);
}

.popup-value strong {
  color: var(--color-text);
}

.popup-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--color-border);
}

.popup-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 12px;
  border-radius: var(--radius-md);
  font-size: 12px;
  font-weight: 600;
  font-family: var(--font-body);
  text-decoration: none;
  transition: all 0.15s ease;
  flex: 1;
  justify-content: center;
  white-space: nowrap;
  min-height: 36px;
}

.popup-btn svg {
  flex-shrink: 0;
}

.popup-btn-maps {
  background: #1a73e8;
  color: #ffffff;
}

.popup-btn-maps:hover {
  background: #1557b0;
}

.popup-btn-maps svg {
  stroke: #ffffff;
}

.popup-btn-pipedrive {
  background: var(--color-surface-2);
  color: var(--color-text);
  border: 1px solid var(--color-border);
}

.popup-btn-pipedrive:hover {
  background: var(--color-surface-dynamic, var(--color-surface-2));
}

/* ====== CUSTOM MARKERS ====== */
.custom-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 2.5px solid #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,0.25);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.custom-marker:hover {
  transform: scale(1.3);
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  z-index: 10000 !important;
}

/* ====== CLUSTER MARKERS ====== */
.marker-cluster {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--color-surface);
  border: 2px solid var(--color-border);
  box-shadow: var(--shadow-md);
  font-size: 12px;
  font-weight: 700;
  color: var(--color-text);
  font-family: var(--font-body);
}

/* ====== LEAFLET OVERRIDES ====== */
.leaflet-control-zoom {
  border: 1px solid var(--color-border) !important;
  border-radius: var(--radius-md) !important;
  overflow: hidden;
  box-shadow: var(--shadow-md) !important;
}

.leaflet-control-zoom a {
  background: var(--color-surface) !important;
  color: var(--color-text) !important;
  border-color: var(--color-border) !important;
  width: 34px !important;
  height: 34px !important;
  line-height: 34px !important;
  font-size: 16px !important;
}

.leaflet-control-zoom a:hover {
  background: var(--color-surface-2) !important;
}

.leaflet-control-attribution {
  background: var(--color-surface) !important;
  color: var(--color-text-faint) !important;
  font-size: 10px !important;
  padding: 2px 6px !important;
}

.leaflet-control-attribution a {
  color: var(--color-text-muted) !important;
}

/* ====== RESPONSIVE ====== */
@media (max-width: 600px) {
  .header {
    padding: 8px 12px;
  }
  .header h1 { font-size: 14px; }
  .deal-count { display: none; }

  .owner-filter select {
    font-size: 13px;
    padding: 6px 30px 6px 12px;
    min-height: 34px;
  }

  /* Search - full width on mobile */
  .search-box {
    right: 10px;
    left: 10px;
    width: auto;
    top: 56px;
  }
  .search-box:focus-within {
    width: auto;
  }

  /* Map offset for search bar */
  #map {
    top: 92px;
  }

  /* Legend - compact bottom bar */
  .legend {
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    padding: 10px 14px;
    min-width: auto;
    max-height: 50vh;
    overflow-y: auto;
  }

  .legend.collapsed {
    padding: 8px 14px;
  }

  .legend-items {
    gap: 4px;
  }

  .legend-item {
    padding: 6px 4px;
    font-size: 13px;
  }

  /* Bigger markers on mobile for easier tap targets */
  .custom-marker {
    min-width: 14px;
    min-height: 14px;
  }

  /* Popup - wider and better tap targets */
  .leaflet-popup-content {
    min-width: 260px !important;
  }

  .popup-inner {
    padding: 14px 14px;
  }

  .popup-btn {
    padding: 10px 12px;
    font-size: 13px;
    min-height: 42px;
  }

  .popup-name {
    font-size: 16px;
  }

  .popup-address {
    font-size: 13px;
  }

  /* Search results - larger tap targets */
  .search-result-item {
    padding: 12px;
    font-size: 14px;
  }

  /* Leaflet zoom controls */
  .leaflet-control-zoom a {
    width: 40px !important;
    height: 40px !important;
    line-height: 40px !important;
    font-size: 18px !important;
  }

  .leaflet-control-zoom {
    margin-top: 100px !important;
  }
}
