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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #f0f2f5;
  color: #1a1a2e;
  min-height: 100vh;
}

/* ── Header ── */
header {
  background: #1a1a2e;
  color: #fff;
  padding: 14px 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 2px 8px rgba(0,0,0,.4);
}
header h1 { font-size: 1.1rem; font-weight: 700; letter-spacing: .5px; }
header h1 span { color: #4ade80; }
#global-stats { font-size: .78rem; color: #94a3b8; margin-left: auto; white-space: nowrap; }

.header-nav { display: flex; gap: 4px; margin-left: 20px; }
.nav-link {
  padding: 5px 12px; border-radius: 6px;
  color: #94a3b8; text-decoration: none;
  font-size: .8rem; font-weight: 500; transition: all .15s;
}
.nav-link:hover { color: #fff; background: rgba(255,255,255,.1); }
.nav-link.active { color: #fff; background: rgba(255,255,255,.15); }

/* ── Controls ── */
.controls {
  background: #fff;
  border-bottom: 1px solid #e2e8f0;
  padding: 12px 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
  z-index: 9000;
}

.chips { display: flex; flex-wrap: wrap; gap: 5px; }
.chip {
  padding: 4px 12px;
  border-radius: 999px;
  border: 1.5px solid #d1d5db;
  background: #fff;
  font-size: .78rem;
  cursor: pointer;
  transition: all .15s;
  color: #374151;
  font-weight: 500;
}
.chip:hover { border-color: #6366f1; color: #6366f1; }
.chip.active { background: #6366f1; border-color: #6366f1; color: #fff; }

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
}
.filter-group {
  display: flex;
  align-items: center;
  gap: 4px;
}
.filter-label {
  font-size: .72rem;
  color: #6b7280;
  white-space: nowrap;
}
.filters input[type=text],
.filters input[type=number],
.filters select {
  padding: 5px 8px;
  border: 1.5px solid #d1d5db;
  border-radius: 7px;
  font-size: .78rem;
  background: #fff;
  outline: none;
  color: #1a1a2e;
}
.filters input[type=text] { min-width: 160px; flex: 1; }
.filters input[type=number] { width: 80px; }
.filters input:focus, .filters select:focus { border-color: #6366f1; }
.filters select { cursor: pointer; }
.filters label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: .78rem;
  color: #6b7280;
  cursor: pointer;
  white-space: nowrap;
}
.sep { color: #d1d5db; font-size: .8rem; }

.room-chips { display: flex; gap: 3px; }
.room-chip {
  padding: 4px 9px;
  border: 1.5px solid #e5e7eb; border-radius: 6px;
  background: #f3f4f6; cursor: pointer;
  font-size: .78rem; color: #9ca3af; font-weight: 500;
  transition: all .12s;
}
.room-chip:hover { border-color: #6366f1; color: #6366f1; background: #f5f3ff; }
.room-chip.active {
  background: #6366f1; border-color: #6366f1;
  color: #fff; font-weight: 700;
  box-shadow: 0 1px 4px rgba(99,102,241,.35);
}

/* ── Location multiselect ── */
.ms-wrap { position: relative; }
.ms-btn {
  padding: 5px 10px;
  border: 1.5px solid #d1d5db; border-radius: 7px;
  background: #fff; cursor: pointer;
  font-size: .78rem; color: #1a1a2e;
  display: flex; align-items: center; gap: 6px;
  white-space: nowrap; min-width: 160px;
}
.ms-btn:hover { border-color: #6366f1; }
.ms-btn.has-selection { border-color: #6366f1; color: #6366f1; font-weight: 600; }
.ms-btn .ms-caret { margin-left: auto; color: #9ca3af; font-size: .65rem; }
.ms-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 9999;
  background: #fff;
  border: 1.5px solid #d1d5db;
  border-radius: 9px;
  box-shadow: 0 8px 24px rgba(0,0,0,.12);
  width: 260px;
  max-height: 320px;
  overflow: hidden;
  display: none;
  flex-direction: column;
}
.ms-dropdown.open { display: flex; }
.ms-search {
  padding: 8px 10px;
  border: none;
  border-bottom: 1px solid #f0f2f5;
  font-size: .78rem;
  outline: none;
  width: 100%;
}
.ms-actions {
  display: flex; gap: 8px;
  padding: 5px 10px;
  border-bottom: 1px solid #f0f2f5;
}
.ms-actions button {
  font-size: .7rem; color: #6366f1;
  background: none; border: none;
  cursor: pointer; padding: 0;
  font-weight: 600;
}
.ms-actions button:hover { text-decoration: underline; }
.ms-list {
  overflow-y: auto;
  max-height: 240px;
  padding: 4px 0;
}
.ms-item {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 12px;
  font-size: .78rem; cursor: pointer;
  color: #374151;
}
.ms-item:hover { background: #f5f3ff; }
.ms-item input[type=checkbox] { accent-color: #6366f1; cursor: pointer; flex-shrink: 0; }
.ms-empty { padding: 12px; font-size: .78rem; color: #9ca3af; text-align: center; }

/* ── Stats bar ── */
.stats-bar {
  padding: 7px 24px;
  font-size: .77rem;
  color: #6b7280;
  background: #f0f2f5;
}

/* ── Grid — 5 columns ── */
.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  padding: 0 24px 24px;
}
@media (max-width: 1400px) { .grid { grid-template-columns: repeat(4, 1fr); } }
@media (max-width: 1000px) { .grid { grid-template-columns: repeat(3, 1fr); } }
@media (max-width: 680px)  { .grid { grid-template-columns: repeat(2, 1fr); padding: 0 12px 16px; } }
@media (max-width: 420px)  { .grid { grid-template-columns: 1fr; } }

/* ── Card ── */
.card {
  background: #fff;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
  transition: transform .15s, box-shadow .15s;
  display: flex;
  flex-direction: column;
}
.card:hover { transform: translateY(-2px); box-shadow: 0 4px 14px rgba(0,0,0,.12); }
.card.deleted { opacity: .5; }

.card-photo-wrap {
  position: relative;
  aspect-ratio: 4/3;
  overflow: hidden;
  background: #e5e7eb;
  display: block;
}
.card-photo-wrap img {
  width: 100%; height: 100%;
  object-fit: cover;
  transition: transform .2s;
}
.card:hover .card-photo-wrap img { transform: scale(1.04); }
.card-photo-placeholder {
  width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  background: #e5e7eb; color: #9ca3af; font-size: 1.8rem;
}

.card-body { padding: 8px 10px 6px; flex: 1; display: flex; flex-direction: column; gap: 3px; }

.card-price-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.card-price { font-size: 1.05rem; font-weight: 700; color: #1a1a2e; }
.price-drop { font-size: .66rem; font-weight: 600; color: #16a34a; background: #dcfce7; padding: 1px 5px; border-radius: 999px; }
.price-rise { font-size: .66rem; font-weight: 600; color: #dc2626; background: #fee2e2; padding: 1px 5px; border-radius: 999px; }

.card-unit-price { font-size: .7rem; color: #6366f1; font-weight: 600; }

.card-location { font-size: .78rem; color: #6b7280; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-transport { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.card-walk  { font-size: .73rem; color: #3b82f6; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.card-metro { font-size: .73rem; color: #ef4444; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.card-details  { font-size: .75rem; color: #9ca3af; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-date     { font-size: .75rem; color: #9ca3af; }

.card-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
.card-badges { display: flex; flex-wrap: wrap; gap: 3px; }
.badge {
  font-size: .62rem; font-weight: 600;
  padding: 1px 6px; border-radius: 999px;
}
.badge-score   { font-weight: 700; }
.score-high    { background: #dcfce7; color: #15803d; }
.score-mid     { background: #fef9c3; color: #854d0e; }
.score-low     { background: #fee2e2; color: #dc2626; }
.badge-score-lg { font-size: .78rem; padding: 3px 10px; }
.badge-repair  { background: #ede9fe; color: #7c3aed; }
.badge-agency  { background: #fef3c7; color: #b45309; }
.badge-owner   { background: #dcfce7; color: #15803d; }
.badge-deleted { background: #f3f4f6; color: #9ca3af; }
.badge-cat     { background: #e0f2fe; color: #0369a1; }

.card-footer {
  display: flex; gap: 6px;
  padding: 4px 10px 6px;
  border-top: 1px solid #f3f4f6;
  margin-top: auto;
}
.card-footer a {
  font-size: .72rem; color: #9ca3af; font-weight: 500;
  text-decoration: none; display: flex; align-items: center; gap: 2px;
  padding: 2px 5px; border-radius: 4px; transition: color .12s;
}
.card-footer a:hover { color: #6366f1; }

/* ── Pagination ── */
.pagination {
  display: flex; align-items: center; justify-content: center;
  gap: 12px; padding: 16px 24px 32px;
}
.pagination button {
  padding: 7px 18px;
  border: 1.5px solid #d1d5db; border-radius: 8px;
  background: #fff; cursor: pointer;
  font-size: .82rem; font-weight: 500; transition: all .15s;
}
.pagination button:hover:not(:disabled) { border-color: #6366f1; color: #6366f1; }
.pagination button:disabled { opacity: .4; cursor: default; }
.pagination span { font-size: .82rem; color: #6b7280; }

/* ── States ── */
.state-msg {
  grid-column: 1 / -1; text-align: center;
  padding: 60px 20px; color: #9ca3af; font-size: .9rem;
}
.spinner {
  width: 32px; height: 32px;
  border: 3px solid #e5e7eb; border-top-color: #6366f1;
  border-radius: 50%; animation: spin .7s linear infinite;
  margin: 0 auto 14px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Tab bar ── */
.tab-bar {
  display: flex;
  gap: 0;
  background: #fff;
  border-bottom: 2px solid #e2e8f0;
  padding: 0 24px;
  position: sticky;
  top: 52px;
  z-index: 90;
}
.tab {
  padding: 10px 20px;
  border: none; border-bottom: 2px solid transparent;
  background: none; cursor: pointer;
  font-size: .85rem; font-weight: 500; color: #6b7280;
  margin-bottom: -2px;
  transition: all .15s;
}
.tab:hover { color: #6366f1; }
.tab.active { color: #6366f1; border-bottom-color: #6366f1; }

.tab-content { }
.tab-content.active { }

/* ── Analytics tab ── */
.page-content { padding: 20px 24px; }

.stat-tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin-bottom: 20px; }
@media (max-width: 900px) { .stat-tiles { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 500px) { .stat-tiles { grid-template-columns: 1fr; } }
.stat-tile {
  background: #fff; border-radius: 10px;
  padding: 18px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.stat-tile .label { font-size: .75rem; color: #6b7280; margin-bottom: 6px; }
.stat-tile .value { font-size: 1.4rem; font-weight: 700; color: #1a1a2e; }

.charts-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px; }
@media (max-width: 800px) { .charts-grid { grid-template-columns: 1fr; } }
.chart-card {
  background: #fff; border-radius: 10px;
  padding: 18px 20px;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.chart-card h3 { font-size: .88rem; font-weight: 600; color: #374151; margin-bottom: 14px; }
.chart-card.full-width { grid-column: 1 / -1; }
.chart-wrap { position: relative; }
.no-data { text-align: center; padding: 40px 0; color: #9ca3af; font-size: .85rem; }

/* ── Map tab ── */
.map-tab-content {
  display: flex;
  flex-direction: column;
  height: calc(100vh - 140px);
}
.map-tab-controls {
  background: #fff;
  border-bottom: 1px solid #e2e8f0;
  padding: 8px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
}
.map-stats { font-size: .78rem; color: #6b7280; white-space: nowrap; }
.view-toggle { display: flex; gap: 4px; }
.view-btn {
  padding: 5px 14px;
  border: 1.5px solid #d1d5db; border-radius: 7px;
  background: #fff; cursor: pointer;
  font-size: .78rem; font-weight: 500; color: #374151;
  transition: all .15s;
}
.view-btn:hover { border-color: #6366f1; color: #6366f1; }
.view-btn.active { background: #6366f1; border-color: #6366f1; color: #fff; }

.transport-toggle {
  display: flex; align-items: center; gap: 4px;
  border-left: 1px solid #e5e7eb; padding-left: 12px;
}
.transport-label { font-size: .72rem; color: #9ca3af; white-space: nowrap; margin-right: 2px; }
.transport-btn {
  padding: 4px 10px;
  border: 1.5px solid #d1d5db; border-radius: 7px;
  background: #fff; cursor: pointer;
  font-size: .75rem; font-weight: 500; color: #374151;
  transition: all .15s; white-space: nowrap;
}
.transport-btn:hover { border-color: #3b82f6; color: #3b82f6; }
#tb-bus.active   { background: #3b82f6; border-color: #3b82f6; color: #fff; }
#tb-metro.active { background: #ef4444; border-color: #ef4444; color: #fff; }
#tb-train.active { background: #10b981; border-color: #10b981; color: #fff; }

#map { flex: 1; width: 100%; min-height: 400px; }

.map-legend {
  position: absolute; bottom: 24px; right: 10px; z-index: 1000;
  background: #fff; border-radius: 8px; padding: 10px 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
  font-size: .75rem; color: #374151;
}
.legend-item { display: flex; align-items: center; gap: 7px; margin-bottom: 5px; }
.legend-item:last-child { margin-bottom: 0; }
.legend-dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.transport-dot { width: 9px; height: 9px; }
.legend-sep { border-top: 1px solid #e5e7eb; margin-top: 6px; padding-top: 6px; }

.choropleth-legend {
  position: absolute; bottom: 24px; right: 10px; z-index: 1000;
  background: #fff; border-radius: 8px; padding: 10px 14px;
  box-shadow: 0 2px 8px rgba(0,0,0,.12);
  font-size: .75rem; color: #374151;
  display: none;
}
.choropleth-legend.visible { display: block; }
.choropleth-legend-title { font-weight: 700; margin-bottom: 6px; }
.choro-item { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; }
.choro-swatch { width: 20px; height: 12px; border-radius: 2px; flex-shrink: 0; border: 1px solid rgba(0,0,0,.1); }

/* ── Rayon chips row ── */
.rayon-chips-row { display: flex; flex-wrap: nowrap; gap: 4px; align-items: center; overflow-x: auto; }
.rayon-item {
  display: flex; align-items: center; gap: 5px;
  padding: 3px 8px; border-radius: 999px;
  border: 1.5px solid #e5e7eb; background: #f3f4f6;
  cursor: pointer; font-size: .73rem; color: #6b7280;
  user-select: none; transition: all .12s;
  white-space: nowrap;
}
.rayon-item:hover { border-color: #6366f1; color: #6366f1; }
.rayon-item.active { background: #fff; border-color: currentColor; color: #374151; font-weight: 600; }
.rayon-swatch {
  width: 9px; height: 9px; border-radius: 2px;
  flex-shrink: 0; border: 1px solid rgba(0,0,0,.1);
}

.district-tooltip {
  background: rgba(26,26,46,.88); color: #fff; border: none;
  border-radius: 6px; padding: 6px 10px; font-size: .78rem;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* Popup */
.popup-photo { width: 100%; border-radius: 6px; margin-bottom: 6px; display: block; }
.popup-price { font-size: 1rem; font-weight: 700; color: #1a1a2e; }
.popup-ppm2  { font-size: .75rem; color: #6366f1; font-weight: 600; margin-bottom: 2px; }
.popup-loc   { font-size: .8rem; color: #6b7280; }
.popup-det   { font-size: .78rem; color: #9ca3af; margin-bottom: 4px; }
.popup-walk  { font-size: .78rem; color: #3b82f6; font-weight: 500; margin-top: 3px; }
.popup-link  { display: inline-block; margin-top: 6px; font-size: .8rem; color: #6366f1; text-decoration: none; font-weight: 600; }
.popup-link:hover { text-decoration: underline; }
.popup-badge { display: inline-block; font-size: .68rem; font-weight: 600; padding: 1px 7px; border-radius: 999px; margin-top: 4px; }
.popup-agency { background: #fef3c7; color: #b45309; }
.popup-owner  { background: #dcfce7; color: #15803d; }

/* ── Card clickable ── */
.card { cursor: pointer; }

/* ── Detail modal ── */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.55);
  z-index: 10000;
  display: flex; align-items: center; justify-content: center;
  padding: 16px;
  animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-box {
  background: #fff;
  border-radius: 14px;
  width: 100%; max-width: 760px;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 20px 60px rgba(0,0,0,.3);
  animation: slideUp .18s ease;
}
@keyframes slideUp { from { transform: translateY(24px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.modal-close {
  position: sticky; top: 12px; float: right; margin: 12px 12px 0 0;
  width: 32px; height: 32px;
  border: none; border-radius: 50%;
  background: rgba(0,0,0,.06); color: #374151;
  font-size: .9rem; cursor: pointer; z-index: 1;
  display: flex; align-items: center; justify-content: center;
  transition: background .12s;
}
.modal-close:hover { background: rgba(0,0,0,.14); }

.modal-inner { padding: 0 20px 24px; }

.modal-photo {
  width: 100%; max-height: 340px;
  object-fit: cover;
  border-radius: 10px;
  margin-bottom: 16px;
  display: block;
}
.modal-photo-placeholder {
  width: 100%; height: 180px;
  background: #e5e7eb; border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 3rem; color: #9ca3af;
  margin-bottom: 16px;
}

.modal-title {
  font-size: 1rem; font-weight: 700; color: #1a1a2e;
  margin-bottom: 12px; line-height: 1.4;
}

.modal-price-row {
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap;
  margin-bottom: 4px;
}
.modal-price { font-size: 1.5rem; font-weight: 800; color: #1a1a2e; }
.modal-ppm2  { font-size: .88rem; color: #6366f1; font-weight: 600; }
.modal-price-drop { font-size: .78rem; font-weight: 600; color: #16a34a; background: #dcfce7; padding: 2px 8px; border-radius: 999px; }
.modal-price-rise { font-size: .78rem; font-weight: 600; color: #dc2626; background: #fee2e2; padding: 2px 8px; border-radius: 999px; }

.modal-specs {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin: 12px 0;
}
.modal-spec {
  background: #f0f2f5; border-radius: 7px;
  padding: 5px 10px; font-size: .8rem; color: #374151; font-weight: 500;
}

.modal-badges { display: flex; flex-wrap: wrap; gap: 5px; margin-bottom: 12px; }

.modal-location {
  font-size: .88rem; color: #6b7280;
  margin-bottom: 6px;
}
.modal-transport {
  display: flex; flex-wrap: wrap; align-items: center; gap: 6px;
  font-size: .8rem; color: #3b82f6;
  margin-bottom: 10px;
}
.modal-transport-sep { color: #d1d5db; }

.modal-description {
  font-size: .82rem; color: #4b5563; line-height: 1.6;
  background: #f9fafb; border-radius: 8px;
  padding: 10px 12px; margin: 12px 0;
  white-space: pre-line;
}

.modal-meta {
  display: flex; flex-wrap: wrap; gap: 6px 16px;
  font-size: .77rem; color: #9ca3af;
  margin: 10px 0;
}

.modal-history-title {
  font-size: .85rem; font-weight: 600; color: #374151;
  margin: 16px 0 8px;
}
.modal-history-chart { height: 160px; position: relative; }
.modal-history-table {
  width: 100%; border-collapse: collapse;
  font-size: .78rem; margin-top: 8px;
}
.modal-history-table th {
  text-align: left; padding: 4px 8px;
  color: #6b7280; font-weight: 500;
  border-bottom: 1px solid #e5e7eb;
}
.modal-history-table td { padding: 5px 8px; border-bottom: 1px solid #f3f4f6; color: #374151; }
.modal-history-table tr:last-child td { border-bottom: none; }
.modal-history-table .change-down { color: #16a34a; font-weight: 600; }
.modal-history-table .change-up   { color: #dc2626; font-weight: 600; }

.modal-links {
  display: flex; gap: 10px; margin-top: 18px; flex-wrap: wrap;
}
.modal-link {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 8px 16px; border-radius: 8px;
  font-size: .82rem; font-weight: 600; text-decoration: none;
  transition: opacity .12s;
}
.modal-link:hover { opacity: .82; }
.modal-link-primary { background: #6366f1; color: #fff; }
.modal-link-secondary { background: #f0f2f5; color: #374151; }
