:root {
  --primary: #1a7a4c;
  --primary-dark: #145c3a;
  --accent: #2f80ed;
  --bg: #f5f7f6;
  --card: #ffffff;
  --border: #e0e4e2;
  --text: #1c1f1e;
  --text-muted: #6b7570;
  --danger: #d64545;
  --radius: 10px;
  --sidebar-w: 340px;
  --topbar-h: 56px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
}

#app {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  grid-template-rows: var(--topbar-h) 1fr;
  grid-template-areas:
    "topbar topbar"
    "sidebar map";
  height: 100vh;
  height: 100dvh; /* коректна височина на мобилен Safari при показване/скриване на адресната лента */
  width: 100vw;
  overflow: hidden;
}

#topbar {
  grid-area: topbar;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  background: var(--primary);
  color: white;
  z-index: 20;
}
#topbar h1 {
  font-size: 17px;
  font-weight: 600;
  margin: 0;
  flex: 1;
  min-width: 0; /* без това flex елементът отказва да се свие и чупи topbar-а на тесни екрани */
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#menuToggle {
  display: none;
  background: none;
  border: none;
  color: white;
  font-size: 22px;
  line-height: 1;
  padding: 10px;
  margin: -10px;
  cursor: pointer;
  flex-shrink: 0;
}
.add-buttons { display: flex; gap: 8px; flex-shrink: 0; }
.primary-btn {
  background: white;
  color: var(--primary-dark);
  border: none;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
  white-space: nowrap;
}
.primary-btn:hover { background: #eafaf1; }
.primary-btn-alt { background: #eafaf1; color: var(--primary-dark); }
.primary-btn-alt:hover { background: white; }

#sidebar {
  grid-area: sidebar;
  background: var(--card);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  z-index: 10;
}
.sidebar-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}
.sidebar-section.list-section {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.section-label {
  display: block;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  margin-bottom: 8px;
  font-weight: 600;
}

.type-tabs {
  display: flex;
  gap: 6px;
}
.type-tab {
  flex: 1;
  padding: 8px 4px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
.type-tab.active {
  background: var(--primary);
  border-color: var(--primary);
  color: white;
}

#searchInput {
  width: 100%;
  padding: 9px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  font-size: 16px; /* <16px кара iOS Safari автоматично да увеличи мащаба при фокус */
}
#searchInput:focus { outline: 2px solid var(--accent); }

.chip-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.chip {
  padding: 5px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 12px;
  cursor: pointer;
  color: var(--text-muted);
  white-space: nowrap;
}
.chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.results-list {
  overflow-y: auto;
}
.result-item {
  padding: 10px 8px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 4px;
}
.result-item:hover { background: var(--bg); }
.result-item .rt {
  font-weight: 600;
  font-size: 13.5px;
  display: flex;
  align-items: center;
  gap: 6px;
}
.result-item .rs {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
}
.dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; display: inline-block; }
.dot-obekti { background: #1a7a4c; }
.dot-kvartiri { background: #2f80ed; }

#sidebarOverlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 15;
}

#mapWrap { grid-area: map; position: relative; }
#map { width: 100%; height: 100%; }

.legend {
  position: absolute;
  right: 12px;
  bottom: 24px;
  background: white;
  border-radius: 10px;
  padding: 10px 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.18);
  font-size: 12.5px;
  z-index: 5;
  max-width: 220px;
  max-height: min(60vh, 320px);
  overflow-y: auto;
}
.legend-header { display: flex; align-items: center; justify-content: space-between; gap: 10px; cursor: pointer; }
.legend-title { font-weight: 700; font-size: 12px; }
.legend-toggle {
  background: none;
  border: none;
  padding: 4px;
  margin: -4px;
  font-size: 11px;
  color: var(--text-muted);
  cursor: pointer;
}
.legend-content { margin-top: 8px; }
.legend-row { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; border: 2px solid white; box-shadow: 0 0 0 1px rgba(0,0,0,0.15); }
.legend-note { margin-top: 6px; padding-top: 6px; border-top: 1px solid var(--border); color: var(--text-muted); font-size: 11.5px; line-height: 1.4; }
.legend.collapsed .legend-content { display: none; }
.legend.collapsed .legend-header { justify-content: flex-start; }

@media (max-width: 860px) {
  .legend { left: 12px; right: 12px; bottom: 12px; max-width: none; }
  .legend.collapsed { padding: 8px 12px; width: auto; display: inline-flex; flex-direction: column; }
}

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal-overlay.hidden { display: none; }
.modal {
  background: white;
  border-radius: 14px;
  width: min(520px, 100%);
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-wide { width: min(920px, 100%); }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: white;
}
.modal-header h2 { margin: 0; font-size: 18px; }
.icon-btn {
  background: none;
  border: none;
  font-size: 18px;
  line-height: 1;
  padding: 10px;
  margin: -10px;
  cursor: pointer;
  color: var(--text-muted);
  flex-shrink: 0;
}
.modal-body { padding: 20px; }

.form-row { margin-bottom: 14px; }
.form-row label {
  display: block;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 5px;
}
.form-row input, .form-row select, .form-row textarea {
  width: 100%;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 16px; /* <16px кара iOS Safari автоматично да увеличи мащаба при фокус */
  font-family: inherit;
}
.form-row textarea { resize: vertical; min-height: 60px; }
.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0 12px;
}

.detail-grid { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: 14px; }
.detail-grid dt { color: var(--text-muted); font-weight: 600; }
.detail-grid dd { margin: 0; }

.status-badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
}
.status-green { background: #dcf5e6; color: #1a7a4c; }
.status-grey { background: #eceeed; color: #5b6560; }
.status-yellow { background: #fff3cd; color: #8a6300; }
.status-red { background: #fbdede; color: #b23030; }
.status-blue { background: #dce9fb; color: #2f80ed; }
.status-purple { background: #ede2f5; color: #8e44ad; }

.maps-link {
  display: inline-block;
  margin-top: 4px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--accent);
  text-decoration: none;
}
.maps-link:hover { text-decoration: underline; }

.map-pick-hint {
  background: #eafaf1;
  border: 1px dashed var(--primary);
  color: var(--primary-dark);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 12px;
}

.form-actions {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  margin-top: 16px;
}
.btn {
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  font-weight: 600;
  cursor: pointer;
  font-size: 14px;
}
.btn-primary { background: var(--primary); border-color: var(--primary); color: white; }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }

.gm-style-iw { font-family: inherit; }

.map-pick-banner {
  position: fixed;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary-dark);
  color: white;
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  z-index: 200;
  box-shadow: 0 6px 20px rgba(0,0,0,0.25);
}

/* Mobile */
@media (max-width: 860px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-areas:
      "topbar"
      "map";
  }
  #menuToggle { display: block; }
  #sidebar {
    position: fixed;
    top: var(--topbar-h);
    left: 0;
    bottom: 0;
    width: min(86vw, 360px);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 20px rgba(0,0,0,0.15);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebarOverlay.show { display: block; }
  #topbar h1 { font-size: 15px; }
  .primary-btn { padding: 7px 10px; font-size: 12.5px; }
}

/* Много тесни телефони — бутоните стават само с икона, за да не се чупи topbar-ът */
@media (max-width: 400px) {
  #topbar { gap: 8px; padding: 0 10px; }
  #topbar h1 { display: none; }
  .add-buttons { gap: 6px; }
  .primary-btn { padding: 8px 10px; }
  .btn-text { display: none; }
}

.list-tabs { display: flex; gap: 6px; margin-bottom: 14px; }
.list-tabs .type-tab { flex: none; padding: 7px 16px; }
.list-table-wrap { overflow-x: auto; }
.list-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.list-table th {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 2px solid var(--border);
  color: var(--text-muted);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  white-space: nowrap;
}
.list-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.list-table tr.clickable { cursor: pointer; }
.list-table tr.clickable:hover td { background: var(--bg); }
.list-empty { padding: 24px; text-align: center; color: var(--text-muted); }
