:root {
  --bg: #0f1115;
  --surface: #171a21;
  --surface-2: #1e222b;
  --line: #2a2f3a;
  --ink: #e8eaf0;
  --ink-dim: #9aa3b2;
  --accent: #6c8cff;
  --accent-ink: #0f1115;
  --good: #45c08a;
  --warn: #f0b24a;
  --hot: #ff6b6b;
  --radius: 10px;
  --shadow: 0 8px 28px rgba(0, 0, 0, .38);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fa;
    --surface: #ffffff;
    --surface-2: #f0f2f7;
    --line: #dfe3ec;
    --ink: #171a21;
    --ink-dim: #5f6878;
    --accent: #3355ee;
    --accent-ink: #ffffff;
    --shadow: 0 6px 20px rgba(20, 30, 60, .10);
  }
}

* { box-sizing: border-box; }

/* Overlays below set `display: flex`, which would otherwise beat the `hidden` attribute. */
[hidden] { display: none !important; }

/* ---------- freshness scale ----------
   One ramp shared by cards, map pins and the legend. Deliberately hue-ordered
   (green -> amber -> grey) so age reads without consulting the legend. All six take
   dark text, which keeps contrast legible in either theme. */
:root {
  --age-0: #22c55e;
  --age-1: #84cc16;
  --age-2: #eab308;
  --age-3: #f59e0b;
  --age-4: #fb7185;
  --age-5: #94a3b8;
  --age-ink: #10131a;
}

.age-0 { --age: var(--age-0); }
.age-1 { --age: var(--age-1); }
.age-2 { --age: var(--age-2); }
.age-3 { --age: var(--age-3); }
.age-4 { --age: var(--age-4); }
.age-5 { --age: var(--age-5); }

/* colour stripe down the leading edge of a card or building row */
.age-edge { border-left: 3px solid var(--age); }

.pill.age-pill {
  background: color-mix(in srgb, var(--age) 20%, transparent);
  border-color: color-mix(in srgb, var(--age) 55%, var(--line));
  color: color-mix(in srgb, var(--age) 72%, var(--ink));
}

.age-dot {
  display: inline-block; width: 9px; height: 9px; border-radius: 50%;
  background: var(--age);
}

.legend {
  position: absolute; left: 10px; bottom: 22px; z-index: 500;
  display: flex; flex-direction: column; gap: 3px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 9px; font-size: 10px; color: var(--ink-dim);
  box-shadow: var(--shadow); pointer-events: none;
}
.legend-row { display: flex; align-items: center; gap: 5px; }
.legend-title { margin-right: 3px; font-weight: 600; color: var(--ink); }
.legend-swatch {
  width: 11px; height: 11px; border-radius: 3px; background: var(--age);
  margin-left: 3px;
}
.legend-note { padding-top: 3px; border-top: 1px solid var(--line); }
.legend-badge {
  background: var(--age-5); color: var(--age-ink); margin-left: 0;
}

body {
  margin: 0;
  font: 14px/1.5 "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--ink);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

h2 { font-size: 16px; margin: 0 0 4px; }
h3 { font-size: 15px; margin: 0; }
p { margin: 0 0 10px; }
.muted { color: var(--ink-dim); font-weight: 400; }
.hint { color: var(--ink-dim); font-size: 12px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 10px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark { font-size: 22px; color: var(--accent); }
.brand strong { display: block; letter-spacing: -.2px; }
.brand small { color: var(--ink-dim); font-size: 11px; }

.tabs { display: flex; gap: 2px; margin-left: auto; }
.tab {
  background: transparent; border: 0; color: var(--ink-dim);
  padding: 7px 13px; border-radius: 7px; cursor: pointer; font: inherit;
}
.tab:hover { background: var(--surface-2); color: var(--ink); }
.tab.is-active { background: var(--accent); color: var(--accent-ink); }

.topbar-actions { display: flex; align-items: center; gap: 10px; }
.topbar-actions .muted { font-size: 12px; }

/* ---------- buttons & fields ---------- */
.btn {
  font: inherit; border-radius: 8px; padding: 7px 13px;
  border: 1px solid var(--line); background: var(--surface-2);
  color: var(--ink); cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn-ghost { background: transparent; }
.btn:disabled { opacity: .5; cursor: progress; }

.field { display: block; margin-bottom: 12px; border: 0; padding: 0; }
.field > span, .field > legend {
  display: block; font-size: 11px; text-transform: uppercase;
  letter-spacing: .05em; color: var(--ink-dim); margin-bottom: 5px; padding: 0;
}
.field > legend em { text-transform: none; letter-spacing: 0; opacity: .7; }
.field input[type=search], .field input[type=number], .field input[type=text],
.field input[type=tel], .field select {
  width: 100%; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--bg); color: var(--ink); font: inherit;
}
.field input[type=range] { width: 100%; accent-color: var(--accent); }
.field-row { display: flex; gap: 10px; }
.field-row > .field { flex: 1; }

.check { display: flex; align-items: center; gap: 8px; margin-bottom: 9px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 15px; height: 15px; }
.inline { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--ink-dim); }
.inline select {
  padding: 5px 8px; border-radius: 7px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); font: inherit;
}

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  padding: 5px 11px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--bg); color: var(--ink-dim); cursor: pointer; font-size: 12px;
  user-select: none;
}
.chip:hover { border-color: var(--accent); color: var(--ink); }
.chip.is-on { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* ---------- views ---------- */
.view { display: none; flex: 1 1 auto; min-height: 0; overflow: hidden; }
.view.is-active { display: flex; }
#view-buildings.is-active, #view-alerts.is-active, #view-settings.is-active {
  display: block; overflow-y: auto; padding: 18px;
}

/* Explore = filters + one or both content panes.
   Which panes show is driven by .pane-split / .pane-list / .pane-map on the container,
   so the map is always reachable — never hidden by viewport width alone. */
#view-explore { display: none; }
#view-explore.is-active { display: grid; grid-template-columns: 290px 1fr; }

/* Toolbar on top, panes below — so the toolbar is never a casualty of a pane switch. */
.content { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.panes {
  flex: 1 1 auto; min-height: 0;
  display: grid; grid-template-columns: minmax(360px, 1fr) 1fr;
}
#view-explore.pane-list .panes,
#view-explore.pane-map .panes { grid-template-columns: 1fr; }
#view-explore.pane-list .mapwrap { display: none; }
#view-explore.pane-map .results { display: none; }

.filters {
  border-right: 1px solid var(--line); background: var(--surface);
  display: flex; flex-direction: column; min-height: 0;
}
.filters-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 11px 14px; border-bottom: 1px solid var(--line);
}
.filters-backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 899; }

/* Shown only in the narrow layout; see the media query at the bottom. */
.narrow-only { display: none; }
.filter-scroll { overflow-y: auto; padding: 14px; flex: 1 1 auto; }
.filter-actions {
  display: flex; gap: 8px; padding: 12px 14px;
  border-top: 1px solid var(--line); flex-wrap: wrap;
}
.filter-actions .btn { flex: 1; white-space: nowrap; }

.results { display: flex; flex-direction: column; min-height: 0; min-width: 0; border-right: 1px solid var(--line); }
#view-explore.pane-list .results { border-right: 0; }
.results-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 11px 14px; border-bottom: 1px solid var(--line); background: var(--surface);
  flex: 0 0 auto; flex-wrap: wrap;
}
.cards { overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
/* These are flex items in a scrolling column. `overflow: hidden` on .group-card would
   otherwise zero its automatic minimum size and let it collapse to nothing. */
.cards > * { flex: 0 0 auto; }

.results-controls { display: flex; align-items: center; gap: 14px; }

/* segmented Listings / By building toggle */
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 2px; }
.seg-btn {
  font: inherit; font-size: 12px; border: 0; background: transparent; color: var(--ink-dim);
  padding: 4px 11px; border-radius: 6px; cursor: pointer; white-space: nowrap;
}
.seg-btn:hover { color: var(--ink); }
.seg-btn.is-on { background: var(--accent); color: var(--accent-ink); }

/* ---------- building group card ---------- */
.group-card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); overflow: hidden;
}
.group-card:hover { border-color: var(--accent); }
.group-head {
  display: flex; align-items: flex-start; gap: 10px; width: 100%;
  padding: 11px 12px; background: transparent; border: 0;
  color: inherit; font: inherit; text-align: left; cursor: pointer;
}
.group-caret { color: var(--ink-dim); flex: 0 0 12px; line-height: 1.4; }
.group-main { flex: 1 1 auto; min-width: 0; }
.group-name {
  display: block; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.group-loc {
  display: block; color: var(--ink-dim); font-size: 12px; margin-bottom: 5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.group-right { display: flex; flex-direction: column; align-items: flex-end; gap: 5px; flex: 0 0 auto; }
.group-range { font-size: 15px; font-weight: 650; letter-spacing: -.2px; white-space: nowrap; }
.group-headline { display: block; font-size: 12px; margin-bottom: 5px; color: var(--ink-dim); }
.group-headline b { color: var(--ink); }

/* the cheapest unit in the building, surfaced without expanding */
.group-cheapest {
  display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap;
  padding: 7px 12px 9px 34px; border-top: 1px dashed var(--line); font-size: 12px;
}
.cheapest-tag {
  font-size: 10px; text-transform: uppercase; letter-spacing: .05em;
  color: var(--good); font-weight: 700;
}
.group-cheapest strong { font-size: 14px; }

.group-units { border-top: 1px solid var(--line); padding: 4px 12px 10px; overflow-x: auto; }
.group-foot {
  padding-top: 8px; display: flex; gap: 12px; align-items: center;
  justify-content: space-between; flex-wrap: wrap; font-size: 11px;
}
.unit-table.compact th, .unit-table.compact td { padding: 6px 8px; font-size: 12px; }
/* context rows — present for comparison, but visually subordinate to the matches */
.unit-table tr.is-context td { opacity: .62; }
.unit-table tr.is-match td { font-weight: 500; }
.tag {
  font-size: 9px; text-transform: uppercase; letter-spacing: .04em;
  padding: 1px 5px; border-radius: 4px; margin-left: 5px; vertical-align: 1px;
}
.tag-match { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--accent); }

/* ---------- listing card ---------- */
.card {
  display: grid; grid-template-columns: 108px 1fr; gap: 12px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 10px; cursor: pointer;
}
.card:hover, .card.is-active { border-color: var(--accent); }
.card-img {
  width: 108px; height: 92px; border-radius: 8px; object-fit: cover;
  background: var(--surface-2); display: block;
}
.card-body { min-width: 0; }
.card-top { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.card-rent { font-size: 17px; font-weight: 650; letter-spacing: -.3px; }
.card-type { color: var(--ink-dim); }
.card-title {
  font-weight: 550; margin: 3px 0 1px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card-loc {
  color: var(--ink-dim); font-size: 12px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.card-meta {
  display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px;
  font-size: 11px; color: var(--ink-dim);
}
.pill {
  background: var(--surface-2); border-radius: 999px; padding: 2px 8px;
  border: 1px solid var(--line);
}
.pill.fresh { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, var(--line)); }
.pill.new { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.pill.near { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, var(--line)); }
.pill.link { cursor: pointer; }
.pill.link:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- map ---------- */
.mapwrap { position: relative; min-height: 0; }
#map { position: absolute; inset: 0; background: var(--surface-2); }
.leaflet-container { font: inherit; background: var(--surface-2); }
.marker-pin {
  background: var(--accent); color: var(--accent-ink); border-radius: 999px;
  padding: 3px 8px; font-size: 11px; font-weight: 650; white-space: nowrap;
  border: 2px solid var(--surface); box-shadow: var(--shadow);
}
.marker-pin { background: var(--age, var(--accent)); color: var(--age-ink); }
.marker-pin.is-active { outline: 2px solid var(--hot); transform: scale(1.12); }
/* several units at one address collapse into a single pin carrying a count */
.marker-pin.is-multi { padding-right: 3px; }
.marker-count {
  display: inline-block; margin-left: 4px; padding: 0 5px; border-radius: 999px;
  background: rgba(255, 255, 255, .9); color: #1a1d24; font-size: 10px; font-weight: 700;
}
.marker-office {
  background: var(--hot); color: #fff; border-radius: 8px; padding: 4px 9px;
  font-size: 11px; font-weight: 700; border: 2px solid #fff; box-shadow: var(--shadow);
}
/* device location, shown only while searching around "my location" */
.marker-me {
  background: #2563eb; color: #fff; border-radius: 999px; padding: 4px 10px;
  font-size: 11px; font-weight: 700; border: 2px solid #fff; box-shadow: var(--shadow);
}

/* ---------- panels ---------- */
.panel {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 16px; margin: 0 auto 16px;
  max-width: 900px;
}
.panel.wide { max-width: 1200px; }
.panel-head {
  display: flex; justify-content: space-between; align-items: flex-start;
  gap: 16px; margin-bottom: 12px;
}

/* ---------- buildings ---------- */
.building-list { display: flex; flex-direction: column; gap: 10px; }
.building {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; background: var(--bg); cursor: pointer;
}
.building:hover { border-color: var(--accent); }
.building-top { display: flex; justify-content: space-between; gap: 12px; align-items: baseline; }
.building-name { font-weight: 600; }
.building-stats { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 7px; font-size: 12px; }
.stat b { display: block; font-size: 14px; font-weight: 650; }
.stat span { color: var(--ink-dim); font-size: 11px; }

/* unit comparison table */
.unit-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.unit-table th, .unit-table td {
  text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line);
}
.unit-table th { font-size: 11px; text-transform: uppercase; color: var(--ink-dim); font-weight: 500; }
.unit-table tbody tr:hover { background: var(--surface-2); }
.unit-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.unit-table .best { color: var(--good); font-weight: 600; }

/* ---------- watches ---------- */
.watch-list { display: flex; flex-direction: column; gap: 10px; }
.watch {
  border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; background: var(--bg);
  display: flex; justify-content: space-between; gap: 14px; align-items: flex-start;
}
.watch-crit { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 7px; }
.watch-actions { display: flex; gap: 6px; flex-shrink: 0; }
.log { display: flex; flex-direction: column; gap: 6px; font-size: 12px; }
.log-row {
  display: flex; gap: 10px; padding: 7px 10px; border-radius: 7px;
  background: var(--bg); border: 1px solid var(--line);
}
.log-row.err { border-color: color-mix(in srgb, var(--hot) 45%, var(--line)); }

.chip-list { list-style: none; display: flex; flex-wrap: wrap; gap: 6px; padding: 0; margin: 8px 0; }
.chip-list li {
  display: flex; align-items: center; gap: 6px; padding: 4px 6px 4px 11px;
  border: 1px solid var(--line); border-radius: 999px; font-size: 12px; background: var(--bg);
}
.chip-list button {
  border: 0; background: transparent; color: var(--ink-dim);
  cursor: pointer; font-size: 15px; line-height: 1; padding: 0 3px;
}
.chip-list button:hover { color: var(--hot); }

.suggest { display: flex; flex-direction: column; gap: 2px; margin-bottom: 8px; }
.suggest button {
  text-align: left; background: var(--bg); border: 1px solid var(--line);
  border-radius: 7px; padding: 7px 10px; color: var(--ink); cursor: pointer; font: inherit;
}
.suggest button:hover { border-color: var(--accent); }

/* ---------- drawer & modal ---------- */
.drawer, .modal {
  position: fixed; inset: 0; background: rgba(0, 0, 0, .5);
  display: flex; z-index: 1000;
}
.drawer { justify-content: flex-end; }
.modal { align-items: center; justify-content: center; padding: 20px; }
.drawer-panel {
  width: min(760px, 94vw); background: var(--surface);
  border-left: 1px solid var(--line); display: flex; flex-direction: column;
}
.modal-panel {
  width: min(620px, 100%); max-height: 90vh; background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius);
  display: flex; flex-direction: column; box-shadow: var(--shadow);
}
.drawer-head, .modal-head, .modal-foot {
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.modal-foot { border-bottom: 0; border-top: 1px solid var(--line); }
.drawer-body, .modal-body { overflow-y: auto; padding: 16px; }

.preview {
  margin-top: 12px; padding: 10px; border-radius: 8px;
  background: var(--bg); border: 1px solid var(--line); font-size: 12px;
}

.empty { padding: 34px 16px; text-align: center; color: var(--ink-dim); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--surface); border: 1px solid var(--accent);
  padding: 11px 18px; border-radius: 9px; box-shadow: var(--shadow); z-index: 2000;
  max-width: 80vw;
}
.toast.err { border-color: var(--hot); }

.spinner { display: inline-block; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- narrow layout ----------
   One content pane at a time (List or Map — the user picks), and the filter sidebar
   becomes an off-canvas drawer instead of disappearing. */
@media (max-width: 1100px) {
  .narrow-only { display: inline-flex; }
  .wide-only { display: none; }

  #view-explore.is-active { grid-template-columns: 1fr; }
  .panes { grid-template-columns: 1fr; }

  /* Split is unavailable here, so it falls back to the list. */
  #view-explore.pane-split .mapwrap { display: none; }

  .filters {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 900;
    width: min(330px, 88vw); border-right: 1px solid var(--line);
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: var(--shadow);
  }
  .filters.is-open { transform: none; }

  .results-head { flex-wrap: wrap; gap: 8px; }
  .results-controls { flex-wrap: wrap; gap: 8px; }
  .legend { left: 8px; bottom: 8px; }
}

@media (max-width: 760px) {
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 8px 12px; }
  .tabs { margin-left: 0; order: 3; width: 100%; overflow-x: auto; scrollbar-width: none; }
  .tabs::-webkit-scrollbar { display: none; }
  .tab { white-space: nowrap; }
  .topbar-actions { margin-left: auto; }
  .brand small { display: none; }
  .btn { white-space: nowrap; }

  #view-buildings.is-active, #view-alerts.is-active, #view-settings.is-active { padding: 10px; }
  .panel { padding: 13px; }
  .panel-head { flex-direction: column; align-items: stretch; gap: 10px; }
  .field-row { flex-direction: column; gap: 0; }

  .card { grid-template-columns: 84px 1fr; gap: 10px; }
  .card-img { width: 84px; height: 76px; }
  .card-rent { font-size: 16px; }

  /* Building rows: let the name take the width and drop the price block underneath. */
  .group-head { flex-wrap: wrap; }
  .group-main { flex: 1 1 100%; order: 2; }
  .group-right { flex-direction: row; align-items: center; gap: 8px; order: 3; flex: 1 1 100%; }
  .building-stats { gap: 10px; }
  .group-cheapest { padding-left: 12px; }

  /* Tables stay readable by scrolling inside their own container, never the page. */
  .unit-table { min-width: 560px; }
  .drawer-body, .group-units { overflow-x: auto; }

  .modal { padding: 0; }
  .modal-panel { max-height: 100vh; height: 100%; border-radius: 0; border: 0; }
  .drawer-panel { width: 100vw; }

  .legend { font-size: 9px; padding: 4px 7px; gap: 2px; }
  .legend-note { display: none; }
}

/* Very small phones: the freshness legend is the first thing that can go. */
@media (max-width: 400px) {
  .results-head { padding: 9px 10px; }
  .cards { padding: 9px; }
  .card-meta { gap: 4px; }
}
