/* Calgary Housing Affordability Index — module, charts and table controls.
 *
 * Everything is namespaced afd-* so it cannot reach the shared nav, footer or
 * theme styles. No element or bare-class selectors outside .afd-* / #afford-*.
 *
 * Colour notes: brand teal #48A3A6 is 2.97:1 on white and FAILS WCAG AA for
 * text, so it is used only for rules, fills and borders. Teal *text* on a light
 * ground is #2E7D82 (4.6:1). Body text is #012F38 on #fff (14.8:1) and the
 * muted grey is #5a5a5a (7.0:1).
 */

#afford-tool,
#afford-chart-income,
#afford-chart-bands,
#afford-chart-types,
#afford-chart-time,
#afford-table {
  --afd-ink: #012F38;
  --afd-green: #6ABA5A;
  --afd-lime: #A2C243;
  --afd-teal: #48A3A6;
  --afd-teal-text: #2E7D82;   /* 4.80:1 on white — AA for text */
  --afd-teal-deep: #2C787D;   /* 4.79:1 on the off-white ground, where
                                 #2E7D82 slips to 4.48:1 and fails AA */
  --afd-bg: #F8F7F4;
  --afd-line: #e4e0db;
  --afd-line-soft: #f1efec;
  --afd-muted: #5a5a5a;
  --afd-radius: 4px;
  --afd-sans: 'DM Sans', system-ui, -apple-system, sans-serif;
  max-width: 100%;
  min-width: 0;                 /* grid/flex children default to min-width:auto,
                                   which stops the table below from scrolling
                                   and pushes the page sideways instead */
}

/* Screen-reader-only. Used for the data table beside every chart, so the
   numbers live in the HTML and are never trapped in a graphic. */
.afd-sr {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

/* Anything the builder marked replaceable, once we have replaced it. */
.afd-retired { display: none !important; }

/* =======================================================  income tool  === */

.afd-tool {
  font-family: var(--afd-sans);
  color: var(--afd-ink);
  min-width: 0;
}

.afd-picker {
  background: var(--afd-bg);
  border: 1px solid var(--afd-line);
  border-top: 3px solid var(--afd-teal);
  border-radius: var(--afd-radius);
  padding: 1.25rem 1.35rem 1.4rem;
}

.afd-picker-legend {
  margin: 0 0 .75rem;
  font-size: .7rem;
  letter-spacing: .14em;
  text-transform: uppercase;
  font-weight: 600;
  color: var(--afd-teal-deep);
}

.afd-presets {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
}

.afd-preset {
  font-family: inherit;
  font-size: .875rem;
  font-weight: 500;
  line-height: 1;
  color: var(--afd-ink);
  background: #fff;
  border: 1px solid var(--afd-line);
  border-radius: var(--afd-radius);
  padding: .68rem 1rem;
  cursor: pointer;
  transition: border-color .15s, background .15s, color .15s;
}
.afd-preset:hover { border-color: var(--afd-teal); }
.afd-preset:focus-visible,
.afd-custom-input:focus-visible,
.afd-ctrl-select:focus-visible,
.afd-sort:focus-visible,
.afd-reset:focus-visible {
  outline: 2px solid var(--afd-teal-text);
  outline-offset: 2px;
}
.afd-preset[aria-pressed="true"] {
  background: var(--afd-ink);
  border-color: var(--afd-ink);
  color: #fff;
}

.afd-custom {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: .6rem;
  margin-top: .95rem;
  padding-top: .95rem;
  border-top: 1px solid var(--afd-line);
}
.afd-custom-label {
  font-size: .78rem;
  color: var(--afd-muted);
  font-weight: 500;
}
.afd-custom-box {
  display: inline-flex;
  align-items: center;
  background: #fff;
  border: 1px solid var(--afd-line);
  border-radius: var(--afd-radius);
  padding: 0 .3rem 0 .7rem;
}
.afd-custom-prefix { font-size: .875rem; color: var(--afd-muted); }
.afd-custom-input {
  font-family: inherit;
  font-size: .875rem;
  color: var(--afd-ink);
  border: 0;
  background: transparent;
  padding: .6rem .5rem;
  width: 8.5rem;
  min-width: 0;
}

/* --- headline figures --- */

.afd-stats {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 1px;
  background: var(--afd-line);
  border: 1px solid var(--afd-line);
  border-top: 0;
  margin-top: -1px;
}

.afd-stat {
  background: #fff;
  padding: 1.05rem 1.15rem 1.15rem;
  display: flex;
  flex-direction: column;
  gap: .3rem;
  min-width: 0;
}
.afd-stat.is-lead { background: #fbfbf9; }

.afd-stat-label {
  font-size: .7rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--afd-muted);
  font-weight: 600;
  line-height: 1.4;
}
.afd-stat-value {
  font-family: var(--afd-sans);
  font-weight: 400;                 /* headline weight: DM Sans Regular */
  letter-spacing: -0.02em;
  font-size: 1.7rem;
  line-height: 1.1;
  color: var(--afd-ink);
  font-variant-numeric: tabular-nums;
}
.afd-stat.is-lead .afd-stat-value { font-size: 2.05rem; }
.afd-stat-sub {
  font-size: .76rem;
  line-height: 1.5;
  color: var(--afd-muted);
  font-weight: 300;
}

.afd-range {
  margin: 1rem 0 0;
  font-size: .9rem;
  line-height: 1.65;
  color: var(--afd-ink);
  font-weight: 300;
  border-left: 3px solid var(--afd-lime);
  padding: .1rem 0 .1rem .85rem;
}

.afd-basis {
  margin: .85rem 0 0;
  font-size: .76rem;
  line-height: 1.6;
  color: var(--afd-muted);
  font-weight: 300;
}

/* --- communities in range --- */

.afd-comm {
  margin-top: 1.75rem;
  border-top: 1px solid var(--afd-line);
  padding-top: 1.25rem;
}
.afd-comm-head {
  font-family: var(--afd-sans);
  font-weight: 400;
  letter-spacing: -0.02em;
  font-size: 1.15rem;
  color: var(--afd-ink);
  margin: 0 0 .4rem;
}
.afd-comm-count {
  margin: 0 0 .9rem;
  font-size: .9rem;
  line-height: 1.65;
  color: var(--afd-muted);
  font-weight: 300;
}
.afd-comm-n {
  font-weight: 400;
  color: var(--afd-ink);
  font-size: 1.05rem;
  letter-spacing: -0.02em;
}
.afd-comm-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(215px, 1fr));
  gap: 1px;
  background: var(--afd-line-soft);
  border: 1px solid var(--afd-line-soft);
}
.afd-comm-item {
  background: #fff;
  padding: .6rem .75rem;
  display: flex;
  flex-direction: column;
  gap: .12rem;
  min-width: 0;
}
.afd-comm-link {
  font-size: .875rem;
  font-weight: 500;
  color: var(--afd-teal-text);
  text-decoration: none;
  overflow-wrap: anywhere;
}
.afd-comm-link:hover { text-decoration: underline; }
.afd-comm-meta {
  font-size: .74rem;
  color: var(--afd-muted);
  font-weight: 300;
  font-variant-numeric: tabular-nums;
}
.afd-comm-more {
  margin: .8rem 0 0;
  font-size: .78rem;
  line-height: 1.6;
  color: var(--afd-muted);
  font-weight: 300;
}

/* ============================================================  charts  === */

.afd-fig {
  margin: 0;
  font-family: var(--afd-sans);
  min-width: 0;
}
.afd-fig-cap {
  display: block;
  margin-bottom: 1rem;
}
.afd-fig-title {
  display: block;
  font-family: var(--afd-sans);
  font-weight: 400;
  letter-spacing: -0.02em;
  font-size: 1.15rem;
  line-height: 1.25;
  color: var(--afd-ink);
}
.afd-fig-note {
  display: block;
  margin-top: .3rem;
  font-size: .8rem;
  line-height: 1.6;
  color: var(--afd-muted);
  font-weight: 300;
}

.afd-bars {
  display: flex;
  flex-direction: column;
  gap: .1rem;
}

.afd-bar-row {
  display: grid;
  grid-template-columns: minmax(88px, 130px) 1fr minmax(84px, auto);
  align-items: center;
  gap: .75rem;
  padding: .42rem 0;
  border-top: 1px solid var(--afd-line-soft);
  min-width: 0;
}
.afd-bar-row:first-child { border-top: 0; }

.afd-bar-label {
  font-size: .8rem;
  font-weight: 500;
  color: var(--afd-ink);
  line-height: 1.35;
  overflow-wrap: anywhere;
}
.afd-bar-track {
  display: block;
  height: 16px;
  background: #f1efec;
  border-radius: 2px;
  min-width: 0;
  overflow: hidden;
}
.afd-bar-fill {
  display: block;
  height: 100%;
  background: var(--afd-teal);
  border-radius: 2px;
}
.afd-bar-row:nth-child(even) .afd-bar-fill { background: #55acaf; }
.afd-bar-row.is-accent .afd-bar-fill { background: var(--afd-green); }
.afd-bar-value {
  font-size: .82rem;
  font-weight: 500;
  color: var(--afd-ink);
  text-align: right;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.afd-bar-sub {
  grid-column: 2 / -1;
  font-size: .72rem;
  color: var(--afd-muted);
  font-weight: 300;
  font-variant-numeric: tabular-nums;
  margin-top: -.15rem;
}

/* --- time series --- */

.afd-svg-wrap {
  width: 100%;
  min-width: 0;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.afd-svg {
  display: block;
  width: 100%;
  min-width: 460px;
  height: auto;
}
.afd-grid { stroke: #eceae6; stroke-width: 1; }
.afd-axis {
  font-family: var(--afd-sans);
  font-size: 11px;
  fill: #5a5a5a;
}
.afd-area { fill: rgba(72, 163, 166, .13); stroke: none; }
.afd-line {
  fill: none;
  stroke: var(--afd-teal-text);
  stroke-width: 2;
  stroke-linejoin: round;
  stroke-linecap: round;
}
.afd-dot { fill: #fff; stroke: var(--afd-teal-text); stroke-width: 2; }

/* ==================================================  table controls  ==== */

.afd-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: .75rem;
  padding: .95rem 1rem;
  background: var(--afd-bg);
  border: 1px solid var(--afd-line);
  border-radius: var(--afd-radius) var(--afd-radius) 0 0;
  font-family: var(--afd-sans);
}
.afd-ctrl {
  display: flex;
  flex-direction: column;
  gap: .25rem;
  min-width: 0;
  flex: 1 1 148px;
}
.afd-ctrl-reset { flex: 0 0 auto; align-self: flex-end; }
.afd-ctrl-label {
  font-size: .66rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  font-weight: 600;
  color: var(--afd-muted);
}
.afd-ctrl-select {
  font-family: inherit;
  font-size: .82rem;
  color: var(--afd-ink);
  background: #fff;
  border: 1px solid var(--afd-line);
  border-radius: var(--afd-radius);
  padding: .5rem .55rem;
  width: 100%;
  min-width: 0;
}
.afd-reset {
  font-family: inherit;
  font-size: .72rem;
  font-weight: 600;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: #fff;
  background: var(--afd-ink);
  border: 1px solid var(--afd-ink);
  border-radius: var(--afd-radius);
  padding: .58rem 1rem;
  cursor: pointer;
}
.afd-reset:hover { background: #02414d; }

.afd-status {
  margin: 0;
  padding: .55rem 1rem;
  font-family: var(--afd-sans);
  font-size: .76rem;
  color: var(--afd-muted);
  font-weight: 300;
  background: #fff;
  border: 1px solid var(--afd-line);
  border-top: 0;
}

.afd-empty {
  margin: 0;
  padding: 1.25rem 1rem;
  font-family: var(--afd-sans);
  font-size: .85rem;
  color: var(--afd-muted);
  text-align: center;
  border: 1px solid var(--afd-line);
  border-top: 0;
}

/* Sort buttons sit inside the server-rendered <th>. */
.afd-sort {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: .3rem;
  text-align: left;
}
.afd-sort-mark {
  width: 0; height: 0;
  border-left: 3.5px solid transparent;
  border-right: 3.5px solid transparent;
  border-top: 4px solid rgba(1, 47, 56, .3);
  flex: 0 0 auto;
}
[aria-sort="ascending"] .afd-sort-mark {
  border-top: 0;
  border-bottom: 4px solid var(--afd-teal-text);
}
[aria-sort="descending"] .afd-sort-mark {
  border-top: 4px solid var(--afd-teal-text);
}

/* Filtered-out rows are hidden, never removed: the full table is in the HTML
   on first paint and stays there. */
.afd-row[hidden] { display: none !important; }

/* ============================================================  mobile  === */

@media (max-width: 900px) {
  .afd-stat.is-lead .afd-stat-value { font-size: 1.85rem; }
}

@media (max-width: 640px) {
  .afd-picker { padding: 1rem 1rem 1.15rem; }
  .afd-preset { flex: 1 1 auto; padding: .68rem .7rem; }
  .afd-custom { flex-direction: column; align-items: stretch; }
  .afd-custom-box { width: 100%; }
  .afd-custom-input { width: 100%; }

  .afd-stats { grid-template-columns: 1fr 1fr; }
  .afd-stat { padding: .85rem .9rem .95rem; }
  .afd-stat-value { font-size: 1.35rem; }
  .afd-stat.is-lead {
    grid-column: 1 / -1;
  }
  .afd-stat.is-lead .afd-stat-value { font-size: 1.9rem; }

  .afd-comm-list { grid-template-columns: 1fr; }

  .afd-bar-row {
    grid-template-columns: 1fr auto;
    grid-template-areas:
      "label value"
      "track track"
      "sub   sub";
    gap: .3rem .6rem;
    padding: .6rem 0;
  }
  .afd-bar-label { grid-area: label; }
  .afd-bar-value { grid-area: value; }
  .afd-bar-track { grid-area: track; }
  .afd-bar-sub { grid-area: sub; margin-top: 0; }

  .afd-controls { padding: .8rem .8rem; }
  .afd-ctrl { flex: 1 1 100%; }
  .afd-ctrl-reset { align-self: stretch; }
  .afd-reset { width: 100%; }

  /* The community table becomes a stack of cards. Only applied once JS has
     copied each column heading onto its cell as data-label, so a no-JS page
     keeps a normal (horizontally scrolling) table instead of an unlabelled
     pile of values. */
  /* An author min-width on the wide table would keep it wide in card mode
     and push the page sideways, so it is dropped here explicitly. */
  .afd-table-cards { border: 0; min-width: 0 !important; max-width: 100%; }
  .afd-table-cards thead {
    position: absolute;
    width: 1px; height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
  }
  /* The community column may be a <th scope="row">, so every cell type is
     covered, not just td. */
  .afd-table-cards,
  .afd-table-cards tbody,
  .afd-table-cards tr,
  .afd-table-cards tbody th,
  .afd-table-cards td { display: block; width: auto; }
  .afd-table-cards tr {
    border: 1px solid var(--afd-line);
    border-top: 0;
    padding: .55rem .8rem .7rem;
    background: #fff;
  }
  .afd-table-cards tbody th,
  .afd-table-cards td {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: .75rem;
    align-items: baseline;
    padding: .3rem 0;
    border: 0;
    border-top: 1px solid var(--afd-line-soft);
    font-size: .84rem;
    text-align: left;
    font-variant-numeric: tabular-nums;
  }
  .afd-table-cards tbody th::before,
  .afd-table-cards td::before {
    content: attr(data-label);
    font-size: .68rem;
    letter-spacing: .08em;
    text-transform: uppercase;
    font-weight: 600;
    color: var(--afd-muted);
    white-space: nowrap;
  }
  /* The first cell is the card's title: no label, full width, larger. */
  .afd-table-cards tr > :first-child {
    border-top: 0;
    font-size: .98rem;
    font-weight: 600;
    padding-bottom: .45rem;
    grid-template-columns: 1fr;
  }
  .afd-table-cards tr > :first-child::before { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .afd-preset { transition: none; }
}

@media print {
  .afd-controls, .afd-picker, .afd-status { display: none !important; }
  .afd-row[hidden] { display: table-row !important; }
}
