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

:root {
  --bg: #f5f5f5;
  --surface: #fff;
  --border: #e0e0e0;
  --primary: #1a5276;
  --new-bg: #eaf6ff;
  --new-border: #3498db;
  --rm-color: #e74c3c;
  --zp-color: #8e44ad;
  --text: #222;
  --muted: #666;
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Header ---- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 20px;
  background: var(--primary);
  color: #fff;
  flex-shrink: 0;
  flex-wrap: wrap;
}

header h1 { font-size: 1.2rem; font-weight: 700; }

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

.count-badge {
  background: rgba(255,255,255,0.2);
  border-radius: 12px;
  padding: 2px 10px;
  font-size: 0.8rem;
}

.filters { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.filters select {
  padding: 5px 8px;
  border-radius: 6px;
  border: none;
  font-size: 0.85rem;
  background: rgba(255,255,255,0.15);
  color: #fff;
  cursor: pointer;
}

.filters select option { background: var(--primary); }

.toggle-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.85rem;
  cursor: pointer;
  white-space: nowrap;
}

.scrape-controls { display: flex; align-items: center; gap: 8px; }

#last-scrape { font-size: 0.75rem; opacity: 0.8; white-space: nowrap; }

#scrape-btn {
  padding: 6px 14px;
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.5);
  background: transparent;
  color: #fff;
  cursor: pointer;
  font-size: 0.85rem;
  white-space: nowrap;
  transition: background 0.15s;
}
#scrape-btn:hover { background: rgba(255,255,255,0.15); }
#scrape-btn:disabled { opacity: 0.5; cursor: default; }

/* ---- View toggle ---- */
.view-toggle {
  display: flex;
  gap: 0;
  padding: 8px 20px;
  background: #fff;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.view-btn {
  padding: 5px 16px;
  border: 1px solid var(--border);
  background: #fff;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--muted);
}
.view-btn:first-child { border-radius: 6px 0 0 6px; }
.view-btn:last-child { border-radius: 0 6px 6px 0; }
.view-btn + .view-btn { border-left: none; }
.view-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ---- Main layout ---- */
#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

#cards-panel {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

#map-panel {
  width: 100%;
  display: none;
}

#map { width: 100%; height: 100%; }

/* View modes */
body.view-cards #cards-panel { display: block; }
body.view-cards #map-panel { display: none; }

body.view-map #cards-panel { display: none; }
body.view-map #map-panel { display: flex; flex: 1; }

body.view-split #cards-panel { flex: 0 0 55%; }
body.view-split #map-panel { display: flex; flex: 1; border-left: 1px solid var(--border); }

/* Default: cards view */
body { }
#cards-panel { display: block; }

/* ---- Sections ---- */
.section { margin-bottom: 24px; }
.section-heading {
  font-size: 0.9rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  margin-bottom: 12px;
}

/* ---- Card grid ---- */
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 14px;
}

/* ---- Property card ---- */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: box-shadow 0.15s;
  position: relative;
}
.card:hover { box-shadow: 0 4px 16px rgba(0,0,0,0.1); }
.card.is-new { border-color: var(--new-border); background: var(--new-bg); }
.card.highlighted { box-shadow: 0 0 0 3px var(--new-border); }

.card-img-wrapper {
  position: relative;
}

.sold-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(231, 76, 60, 0.78);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-shadow: 0 2px 6px rgba(0,0,0,0.35);
  pointer-events: none;
}

.card-img {
  width: 100%;
  height: 170px;
  object-fit: cover;
  background: #ddd;
  display: block;
}
.card-img-placeholder {
  width: 100%;
  height: 170px;
  background: #e8e8e8;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
  font-size: 0.8rem;
}

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

.card-badges {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  position: absolute;
  top: 8px;
  left: 8px;
}

.badge {
  font-size: 0.7rem;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.badge-new { background: var(--new-border); color: #fff; }
.badge-rm { background: var(--rm-color); color: #fff; }
.badge-zp { background: var(--zp-color); color: #fff; }
.badge-freehold { background: #27ae60; color: #fff; }
.badge-leasehold { background: #e67e22; color: #fff; }
.badge-both { background: #2c3e50; color: #fff; }

.card-price {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--primary);
}

.card-title {
  font-size: 0.9rem;
  color: var(--text);
  line-height: 1.3;
  font-weight: 500;
}

.card-meta {
  display: flex;
  gap: 10px;
  font-size: 0.8rem;
  color: var(--muted);
  flex-wrap: wrap;
}

.card-address {
  font-size: 0.8rem;
  color: var(--muted);
  line-height: 1.3;
}

.card-actions {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.btn-view {
  flex: 1;
  padding: 6px;
  border-radius: 5px;
  border: 1px solid var(--primary);
  background: transparent;
  color: var(--primary);
  font-size: 0.8rem;
  cursor: pointer;
  text-align: center;
  text-decoration: none;
}
.btn-view:hover { background: var(--primary); color: #fff; }

.btn-dismiss {
  padding: 6px 10px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  cursor: pointer;
}
.btn-dismiss:hover { background: #fee; border-color: #e74c3c; color: #e74c3c; }

.btn-undismiss {
  padding: 6px 10px;
  border-radius: 5px;
  border: 1px solid #27ae60;
  background: transparent;
  color: #27ae60;
  font-size: 0.8rem;
  cursor: pointer;
}
.btn-undismiss:hover { background: #27ae60; color: #fff; }

.btn-favourite {
  padding: 6px 10px;
  border-radius: 5px;
  border: 1px solid var(--border);
  background: transparent;
  color: #ccc;
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
  transition: color 0.15s, border-color 0.15s;
}
.btn-favourite:hover { color: #f1c40f; border-color: #f1c40f; }
.btn-favourite.active { color: #f1c40f; border-color: #f1c40f; }

#favourites-section .section-heading { color: #b8860b; }

.card-note-section { margin-top: 2px; }

.note-text {
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
  border: 1px solid transparent;
  white-space: pre-wrap;
  word-break: break-word;
}
.note-text:hover { border-color: var(--border); background: #fafafa; }

.btn-add-note {
  font-size: 0.75rem;
  color: #aaa;
  background: transparent;
  border: 1px dashed #ccc;
  border-radius: 4px;
  padding: 4px 8px;
  cursor: pointer;
  width: 100%;
  text-align: left;
  transition: color 0.15s, border-color 0.15s;
}
.btn-add-note:hover { color: var(--primary); border-color: var(--primary); }

.card-notes {
  width: 100%;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 6px 8px;
  font-size: 0.78rem;
  font-family: inherit;
  color: var(--text);
  background: transparent;
  min-height: 60px;
  line-height: 1.4;
  transition: border-color 0.15s;
  display: block;
}
.card-notes:focus { outline: none; border-color: var(--primary); }

.btn-save-note {
  margin-top: 5px;
  padding: 4px 12px;
  font-size: 0.78rem;
  border-radius: 4px;
  border: 1px solid var(--primary);
  background: var(--primary);
  color: #fff;
  cursor: pointer;
}

/* ---- Empty state ---- */
#empty-state {
  text-align: center;
  padding: 60px 20px;
  color: var(--muted);
}

/* ---- Map popup ---- */
.map-popup { font-size: 0.85rem; min-width: 200px; }
.map-popup strong { display: block; margin-bottom: 4px; }
.map-popup a { color: var(--primary); }
.map-popup-img { width: 100%; height: 130px; object-fit: cover; border-radius: 4px; margin-bottom: 8px; display: block; }
.map-popup-actions { display: flex; align-items: center; justify-content: space-between; margin-top: 6px; gap: 8px; }
.popup-dismiss-btn { font-size: 0.78rem; padding: 2px 8px; border: 1px solid #ccc; border-radius: 4px; background: #f5f5f5; cursor: pointer; color: #555; }
.popup-dismiss-btn:hover { background: #e0e0e0; }

/* ---- Mobile split: stack vertically ---- */
@media (max-width: 768px) {
  body.view-split #main { flex-direction: column; }
  body.view-split #cards-panel { flex: 0 0 50%; overflow-y: auto; }
  body.view-split #map-panel { border-left: none; border-top: 1px solid var(--border); flex: 1; }

  #cards-panel {
    padding-bottom: calc(100px + env(safe-area-inset-bottom, 0px));
  }
}

/* ---- Undo toast ---- */
#undo-toast {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%) translateY(16px);
  background: #333;
  color: #fff;
  padding: 11px 18px;
  border-radius: 8px;
  font-size: 0.875rem;
  display: flex;
  align-items: center;
  gap: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.35);
  opacity: 0;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 9999;
  pointer-events: none;
}
#undo-toast.visible {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
  pointer-events: auto;
}
.toast-undo-btn {
  background: transparent;
  border: 1px solid rgba(255,255,255,0.45);
  color: #fff;
  border-radius: 5px;
  padding: 3px 12px;
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  white-space: nowrap;
}
.toast-undo-btn:hover { background: rgba(255,255,255,0.15); }

/* ---- Spinning scrape button ---- */
@keyframes spin { to { transform: rotate(360deg); } }
.spinning::after {
  content: ' ⟳';
  display: inline-block;
  animation: spin 1s linear infinite;
}
