/* ══════════════════════════════════════════════════════════════
   Affordability Hub: page-specific styles (loaded only on
   /discover/affordability/). Uses the Discover design tokens.
   ══════════════════════════════════════════════════════════════ */

.discover-note { color: var(--slate); font-size: 0.95rem; }

/* ── Category-group colours (chart/popup segments + legend + table dots) ── */
.seg-housing { background: #1a5f7a; }
.seg-utilities { background: #e0a32e; }
.seg-food { background: #5a9e6f; }
.seg-healthcare { background: #c25b6a; }
.seg-transport { background: #d97642; }
.seg-education { background: #8275b0; }
.seg-other { background: #6b7280; }

.afford-dot {
  width: 11px; height: 11px; border-radius: 50%;
  display: inline-block; margin-right: 0.35rem; vertical-align: middle; flex: 0 0 auto;
}

/* ── Profile selector ───────────────────────────────────────── */
.afford-profiles { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 1.25rem 0 0.25rem; }
.afford-profile-btn {
  font: inherit; cursor: pointer; padding: 0.45rem 1.05rem;
  border: 1px solid var(--line); background: var(--card); color: var(--ink); border-radius: 999px;
}
.afford-profile-btn:hover { border-color: var(--accent); }
.afford-profile-btn.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ── Cost gradient legend ───────────────────────────────────── */
.afford-gradient { display: flex; align-items: center; gap: 0.6rem; margin: 0.75rem 0 0.5rem; font-size: 0.85rem; color: var(--slate); }
.afford-grad-bar {
  flex: 1 1 auto; max-width: 340px; height: 12px; border-radius: 6px;
  background: linear-gradient(90deg, #fff7bc, #fec44f, #d95f0e, #8c1414);
  border: 1px solid var(--line);
}
.afford-grad-label b { color: var(--ink); }

/* ── Map ────────────────────────────────────────────────────── */
#afford-map {
  height: 480px; margin: 0.4rem 0 0.85rem;
  border: 1px solid var(--line); border-radius: 10px; z-index: 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.06);
}
@media (max-width: 640px) { #afford-map { height: 380px; } }
/* Region spotlight: polygons dissolve their shade in/out as groups toggle */
#afford-map path.leaflet-interactive { transition: fill-opacity 0.35s ease; }
@media (prefers-reduced-motion: reduce) {
  #afford-map path.leaflet-interactive, .afford-rank-collapse, .afford-rank-chevron { transition: none; }
}

/* ── Map + region filter panel ──────────────────────────────── */
.afford-mapwrap { display: flex; gap: 0.75rem; align-items: stretch; margin: 0.4rem 0 0.85rem; }
.afford-mapwrap > #afford-map { flex: 1 1 auto; margin: 0; }
.afford-filters {
  flex: 0 0 196px; display: flex; flex-direction: column; gap: 0.45rem; overflow: auto;
  border: 1px solid var(--line); border-radius: 10px; padding: 0.7rem;
}
.afford-filters:empty { display: none; }
.afford-filters-head { margin: 0; font-size: 0.74rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--slate); }
.afford-filters-list { display: flex; flex-direction: column; gap: 0.3rem; }
.afford-filter-chip {
  font: inherit; font-size: 0.86rem; cursor: pointer; text-align: left;
  padding: 0.35rem 0.6rem; border: 1px solid var(--line); border-radius: 8px; background: var(--card); color: var(--ink);
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}
.afford-filter-chip:hover { border-color: var(--accent); }
.afford-filter-chip.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.afford-filters-clear { font: inherit; font-size: 0.82rem; cursor: pointer; color: var(--accent); background: none; border: 0; padding: 0.1rem 0; text-align: left; }
.afford-filters-toggle { display: none; }
@media (max-width: 760px) {
  .afford-mapwrap { flex-direction: column; gap: 0.5rem; }
  .afford-mapwrap > #afford-map { width: 100%; }
  .afford-filters { flex: 1 1 auto; display: none; }
  .afford-filters.is-shown { display: flex; }
  .afford-filters-toggle {
    display: inline-flex; align-items: center; gap: 0.4rem; align-self: flex-start;
    font: inherit; font-size: 0.9rem; cursor: pointer; padding: 0.4rem 0.9rem;
    border: 1px solid var(--line); border-radius: 999px; background: var(--card); color: var(--ink);
  }
  .afford-filters-toggle::before { content: "▾"; font-size: 0.8em; }
  .afford-filters-toggle[aria-expanded="true"]::before { content: "▴"; }
}

/* ── Category legend ────────────────────────────────────────── */
.afford-legend { display: flex; flex-wrap: wrap; gap: 0.4rem 1rem; margin: 0.5rem 0 0.25rem; font-size: 0.85rem; }
.afford-legend span { display: inline-flex; align-items: center; }

/* ── Click popup: cylinder + ranked breakdown ───────────────── */
.afford-popup .leaflet-popup-content-wrapper { border-radius: 12px; box-shadow: 0 6px 22px rgba(0, 0, 0, 0.22); }
.afford-popup .leaflet-popup-content { margin: 0; }
.afford-pop {
  font-family: 'Instrument Sans', system-ui, sans-serif; color: var(--ink);
  padding: 0.85rem 0.95rem; min-width: 230px;
}
.afford-pop-head { border-bottom: 1px solid var(--line); padding-bottom: 0.45rem; margin-bottom: 0.55rem; }
.afford-pop-head strong { font-family: 'Newsreader', Georgia, serif; font-size: 1.12rem; }
.afford-pop-total { font-size: 1.35rem; font-weight: 700; color: var(--accent); line-height: 1.2; }
.afford-pop-sub { font-size: 0.8rem; color: var(--slate); }
.afford-pop-body { display: flex; gap: 0.85rem; align-items: stretch; }
.afford-pop-cyl {
  position: relative; flex: 0 0 26px; width: 26px; min-height: 132px;
  display: flex; flex-direction: column-reverse;
  border-radius: 7px 7px 2px 2px; overflow: hidden; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.afford-pop-cyl::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  background: linear-gradient(90deg, rgba(0,0,0,0.2), rgba(255,255,255,0.24) 46%, rgba(0,0,0,0.16));
}
.afford-pop-seg { width: 100%; }
.afford-pop-list { flex: 1 1 auto; display: flex; flex-direction: column; gap: 0.18rem; font-size: 0.85rem; }
.afford-pop-row { display: flex; align-items: center; gap: 0.4rem; }
.afford-pop-row span { flex: 1 1 auto; color: var(--slate); }
.afford-pop-row strong { font-variant-numeric: tabular-nums; }
.afford-pop-warn { font-size: 0.76rem; color: var(--wine); margin-top: 0.5rem; }
.afford-pop-link { display: inline-block; margin-top: 0.6rem; font-weight: 600; font-size: 0.88rem; }
.afford-pop-rent { font-size: 0.76rem; color: var(--slate); margin-top: 0.55rem; border-top: 1px solid var(--line); padding-top: 0.4rem; }

/* ── Chart click → cost popup (modal, reuses the map popup content) ── */
body.afford-modal-open { overflow: hidden; }
.afford-modal[hidden] { display: none; }
.afford-modal { position: fixed; inset: 0; z-index: 1200; display: flex; align-items: center; justify-content: center; padding: 1rem; }
.afford-modal__backdrop { position: absolute; inset: 0; background: rgba(15, 23, 42, 0.5); }
.afford-modal__card { position: relative; background: #fff; border-radius: 14px; box-shadow: 0 14px 44px rgba(0, 0, 0, 0.28); width: 100%; max-width: 340px; max-height: 85vh; overflow: auto; }
.afford-modal__card .afford-pop { min-width: 0; padding: 1rem 1.05rem; }
.afford-modal__card .afford-pop-head { padding-right: 1.5rem; }
.afford-modal__close { position: absolute; top: 0.35rem; right: 0.45rem; z-index: 1; font: inherit; font-size: 1.5rem; line-height: 1; cursor: pointer; background: none; border: 0; color: var(--slate); padding: 0.1rem 0.4rem; }
.afford-modal__close:hover { color: var(--ink); }

/* Source visibility */
.afford-rentnote { background: var(--muted); border-left: 3px solid var(--accent); padding: 0.55rem 0.8rem; border-radius: 0 6px 6px 0; margin-top: 0.65rem; }
.afford-src { font-size: 0.85rem; line-height: 1.35; }

/* ── Tables ─────────────────────────────────────────────────── */
.afford-table { width: 100%; border-collapse: collapse; margin: 0.75rem 0; font-size: 0.97rem; }
.afford-table th, .afford-table td {
  text-align: left; padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--line); vertical-align: top;
}
.afford-table th {
  font-family: 'Instrument Sans', system-ui, sans-serif; font-weight: 700;
  font-size: 0.82rem; text-transform: uppercase; letter-spacing: 0.03em;
  color: var(--slate); border-bottom: 2px solid var(--line);
}
.afford-table tbody tr:hover { background: var(--muted); }
.afford-table td:not(:first-child) { white-space: nowrap; }
.afford-ratio { display: block; font-size: 0.78rem; color: var(--slate); }
.afford-na { color: #b8b2a8; }
.afford-meta { font-size: 0.82rem; color: var(--slate); }
.afford-col-active { background: var(--accent-light); font-weight: 700; }

.tag {
  display: inline-block; font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.03em;
  background: var(--muted); color: var(--slate); padding: 0.05rem 0.4rem; border-radius: 3px; vertical-align: middle;
}

/* ── Sources ────────────────────────────────────────────────── */
.afford-sources { font-size: 0.9rem; color: var(--slate); padding-left: 1.1rem; }
.afford-sources li { margin-bottom: 0.5rem; }

/* ── Search toolbar ─────────────────────────────────────────── */
.afford-toolbar { display: flex; align-items: center; gap: 0.75rem; flex-wrap: wrap; margin: 0.25rem 0 1.1rem; }
.afford-search {
  display: flex; align-items: center; gap: 0.5rem; flex: 1 1 260px; max-width: 420px;
  border: 1px solid var(--line); border-radius: 999px; padding: 0.5rem 0.95rem; background: var(--card);
}
.afford-search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-light); }
.afford-search svg { width: 18px; height: 18px; color: var(--slate); flex: 0 0 auto; }
.afford-search input { flex: 1 1 auto; min-width: 0; border: 0; outline: 0; background: transparent; font: inherit; color: var(--ink); }
.afford-count { font-size: 0.85rem; color: var(--slate); white-space: nowrap; }

/* ── Ranked cost bar chart (collapsible region groups) ──────── */
.afford-rank { margin: 0; padding: 0; display: flex; flex-direction: column; gap: 0.7rem; }
.afford-rank-group { display: flex; flex-direction: column; }
.afford-rank-grouphead {
  display: flex; align-items: center; gap: 0.55rem; width: 100%;
  font: inherit; text-align: left; cursor: pointer; color: inherit;
  background: none; border: 0; border-bottom: 1px solid var(--line); padding: 0.1rem 0 0.35rem;
}
.afford-rank-grouphead:hover .afford-rank-region { color: var(--accent); }
.afford-rank-chevron {
  flex: 0 0 auto; width: 0; height: 0;
  border-top: 5px solid transparent; border-bottom: 5px solid transparent; border-left: 6px solid var(--slate);
  transition: transform 0.25s ease;
}
.afford-rank-group.is-open .afford-rank-chevron { transform: rotate(90deg); }
.afford-rank-region { font-family: 'Newsreader', Georgia, serif; font-size: 1.05rem; font-weight: 600; color: var(--ink); }
.afford-rank-ct { font-size: 0.72rem; color: var(--slate); background: var(--muted); border-radius: 999px; padding: 0.05rem 0.5rem; }
.afford-rank-avg { margin-left: auto; font-size: 0.78rem; color: var(--slate); font-variant-numeric: tabular-nums; white-space: nowrap; }
/* Collapsible body: grid-rows 0fr→1fr animates height cheaply (no JS) */
.afford-rank-collapse { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 0.3s ease; }
.afford-rank-group.is-open .afford-rank-collapse { grid-template-rows: 1fr; }
.afford-rank-clip { overflow: hidden; min-height: 0; }
.afford-rank-list { list-style: none; margin: 0; padding: 0.45rem 0 0.2rem; display: flex; flex-direction: column; gap: 0.3rem; }
.afford-rank-row { display: grid; grid-template-columns: 150px 1fr minmax(140px, auto); align-items: center; gap: 0.65rem; }
.afford-rank-row.is-limited { opacity: 0.72; }
.afford-rank-name {
  font-size: 0.92rem; font-weight: 600; color: var(--ink); text-decoration: none;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.afford-rank-name:hover { color: var(--accent); text-decoration: underline; }
.afford-rank-track { background: var(--muted); border-radius: 6px; height: 22px; overflow: hidden; }
.afford-rank-bar { height: 100%; border-radius: 6px; min-width: 5px; transition: width 0.35s ease, background 0.35s ease; }
.afford-rank-meta { font-size: 0.82rem; color: var(--slate); text-align: right; white-space: nowrap; }
.afford-rank-meta strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.afford-rank-empty { margin-top: 0.5rem; }

/* ── Sortable table headers ─────────────────────────────────── */
.afford-sort {
  font: inherit; cursor: pointer; background: none; border: 0; padding: 0; color: inherit;
  letter-spacing: inherit; text-transform: inherit; display: inline-flex; align-items: center; gap: 0.25rem;
}
.afford-sort::after { content: "↕"; opacity: 0.4; font-size: 0.9em; }
.afford-sort:hover { color: var(--accent); }
.afford-sort.is-sorted { color: var(--accent); }
.afford-sort.is-sorted::after { content: "↑"; opacity: 1; }
.afford-sort.is-sorted.is-desc::after { content: "↓"; }

@media (max-width: 640px) {
  .afford-toolbar { gap: 0.5rem; }
  .afford-search { max-width: none; flex-basis: 100%; }
  .afford-rank-row { grid-template-columns: 92px 1fr; row-gap: 0.15rem; }
  .afford-rank-name { grid-column: 1; grid-row: 1; }
  .afford-rank-track { grid-column: 2; grid-row: 1; }
  .afford-rank-meta { grid-column: 2; grid-row: 2; text-align: left; }
  /* Wide tables scroll horizontally rather than overflowing the page */
  .afford-table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; font-size: 0.86rem; }
  .afford-table th, .afford-table td { padding: 0.45rem 0.5rem; }
  #afford-map { height: 340px; }
  /* Gradient legend stacks: labels on top, full-width bar below */
  .afford-gradient { flex-wrap: wrap; }
  .afford-grad-bar { order: 3; flex-basis: 100%; max-width: none; }
  .afford-profiles { gap: 0.35rem; }
  .afford-profile-btn { padding: 0.4rem 0.85rem; font-size: 0.92rem; }
  .afford-legend { font-size: 0.8rem; gap: 0.35rem 0.7rem; }
  .afford-pop { min-width: 0; padding: 0.7rem 0.75rem; }
  .afford-pop-total { font-size: 1.2rem; }
  .afford-rentnote { font-size: 0.86rem; }
}
