:root {
  --background: #f7f8fa;
  --surface: #ffffff;
  --foreground: #18212b;
  --muted: #65717e;
  --border: #dce1e6;
  --border-strong: #c4cbd2;
  --map-accent: #087f78;
  --area-accent: #a64b16;
  --selected: #e7f4f2;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  min-width: 320px;
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  background: var(--background);
  color: var(--foreground);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button, input { font: inherit; }

.map-app {
  display: grid;
  grid-template-rows: 72px minmax(0, 1fr);
  height: 100dvh;
}

.map-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 0 24px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  z-index: 500;
}

.map-heading {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.back-link {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  background: var(--surface);
  color: var(--foreground);
  font-size: 21px;
  font-weight: 600;
  line-height: 1;
  text-decoration: none;
}

.back-link:hover { border-color: var(--map-accent); color: var(--map-accent); }
.back-link:focus-visible { outline: 3px solid rgb(8 127 120 / 18%); outline-offset: 2px; }

.map-title { min-width: 0; }
.map-title h1 { margin: 0; font-size: 22px; font-weight: 650; letter-spacing: 0; }
.map-title p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }

.map-stats { display: flex; align-items: center; gap: 22px; flex: 0 0 auto; }
.map-stat { display: grid; gap: 2px; min-width: 72px; }
.map-stat strong { font-size: 18px; font-weight: 650; font-variant-numeric: tabular-nums; }
.map-stat span { color: var(--muted); font-size: 12px; }

.map-workspace {
  display: grid;
  grid-template-columns: minmax(310px, 360px) minmax(0, 1fr);
  min-height: 0;
}

.premises-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  border-right: 1px solid var(--border);
  background: var(--surface);
  z-index: 400;
}

.panel-search { padding: 16px; border-bottom: 1px solid var(--border); }
.panel-search label { display: block; margin-bottom: 7px; color: var(--muted); font-size: 12px; font-weight: 600; }
.panel-search input {
  width: 100%;
  height: 40px;
  padding: 0 12px;
  border: 1px solid var(--border-strong);
  border-radius: 5px;
  outline: none;
  background: var(--surface);
  color: var(--foreground);
}
.panel-search input:focus { border-color: var(--map-accent); box-shadow: 0 0 0 3px rgb(8 127 120 / 15%); }

.building-list { min-height: 0; margin: 0; padding: 0; overflow-y: auto; list-style: none; }
.building-item { border-bottom: 1px solid var(--border); }
.building-button {
  display: block;
  width: 100%;
  padding: 16px;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
}
.building-button:hover { background: #f4f7f7; }
.building-button.is-selected { background: var(--selected); box-shadow: inset 3px 0 0 var(--map-accent); }
.building-heading { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.building-address { margin: 0; font-size: 14px; font-weight: 650; line-height: 1.35; }
.building-count {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 25px;
  height: 25px;
  padding: 0 7px;
  border-radius: 4px;
  background: var(--map-accent);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
}
.building-number { margin: 7px 0 12px; color: var(--muted); font: 12px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.premise-lines { display: grid; gap: 8px; }
.premise-line { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px; align-items: baseline; }
.premise-line code { overflow: hidden; color: var(--foreground); font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
.premise-area { color: var(--area-accent); font-size: 12px; font-weight: 700; white-space: nowrap; }
.empty-list { padding: 24px 16px; color: var(--muted); font-size: 14px; }

.map-stage, .leaflet-map { min-width: 0; min-height: 0; width: 100%; height: 100%; }
.leaflet-container { background: #e8ecef; font-family: inherit; }
.leaflet-control-zoom a { color: var(--foreground); }
.premises-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 3px solid #fff;
  border-radius: 50% 50% 50% 8px;
  background: var(--map-accent);
  box-shadow: 0 3px 10px rgb(24 33 43 / 28%);
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  transform: rotate(-45deg);
}
.premises-marker span { transform: rotate(45deg); }
.premises-marker.is-selected { background: var(--area-accent); }
.leaflet-popup-content-wrapper { border-radius: 6px; box-shadow: 0 8px 28px rgb(24 33 43 / 22%); }
.leaflet-popup-content { width: 320px !important; margin: 16px; }
.popup-title { margin: 0 0 5px; font-size: 15px; font-weight: 700; line-height: 1.35; }
.popup-building-number, .popup-coordinates { margin: 0; color: var(--muted); font: 11px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.popup-coordinates { margin-top: 4px; }
.popup-premises { display: grid; gap: 10px; margin-top: 13px; padding-top: 12px; border-top: 1px solid var(--border); }
.popup-premise { display: grid; gap: 4px; }
.popup-premise-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.popup-premise code { font-size: 12px; }
.popup-premise-area { color: var(--area-accent); font-size: 12px; font-weight: 700; white-space: nowrap; }
.popup-premise-meta { color: var(--muted); font-size: 11px; }

@media (max-width: 760px) {
  .map-app { grid-template-rows: auto minmax(0, 1fr); }
  .map-header { align-items: flex-start; padding: 14px 16px; }
  .map-heading { align-items: flex-start; gap: 9px; }
  .back-link { width: 34px; height: 34px; }
  .map-title h1 { font-size: 18px; }
  .map-title p { max-width: 230px; }
  .map-stats { gap: 12px; }
  .map-stat { min-width: 48px; }
  .map-stat strong { font-size: 16px; }
  .map-stat span { font-size: 10px; }
  .map-workspace { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) 220px; }
  .map-stage { grid-row: 1; }
  .premises-panel { grid-row: 2; border-top: 1px solid var(--border); border-right: 0; }
  .panel-search { display: none; }
  .building-list { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(280px, 86vw); overflow-x: auto; overflow-y: hidden; scroll-snap-type: x mandatory; }
  .building-item { border-right: 1px solid var(--border); border-bottom: 0; scroll-snap-align: start; }
  .building-button { height: 100%; overflow-y: auto; }
  .leaflet-popup-content { width: min(270px, 70vw) !important; }
}
