/* Rental Market Insights dashboard — same cream/brick theme as the rest of
   the site (see style.css :root), plus a small data-viz palette for charts.
   Categorical order (brick, blue, green, gold) validated for CVD-safe
   adjacent pairs at n<=4 — see the dataviz skill's color-formula checks. */

.insights-page {
  --data-brick: var(--brick);
  --data-blue: #2166a3;
  --data-green: #4d7c3a;
  --data-gold: #b8860b;
  --data-muted: rgba(46, 36, 32, 0.28);
  --data-grid: var(--line);
  padding: 32px 0 72px;
}

.insights-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 28px;
}

.insights-header h1 { font-size: clamp(1.7rem, 3.4vw, 2.3rem); margin: 6px 0 6px; }
.insights-intro { color: var(--muted); font-size: 0.95rem; max-width: 640px; line-height: 1.6; margin-bottom: 6px; }
.insights-sub { color: var(--muted); font-size: 0.9rem; }

.insights-header-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }

/* ---------- Layout ---------- */
.insights-layout {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  gap: 28px;
  align-items: start;
}

.insights-sidebar { position: sticky; top: 20px; display: flex; flex-direction: column; gap: 12px; }

.sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: var(--card);
  border: none;
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: 0 4px 18px rgba(70, 45, 30, 0.05);
  cursor: pointer;
  color: var(--ink);
  font-family: var(--font-body);
}
.sidebar-toggle-icon { width: 18px; height: 18px; color: var(--muted); transition: transform 0.2s ease; flex-shrink: 0; }
.sidebar-toggle[aria-expanded="false"] .sidebar-toggle-icon { transform: rotate(-90deg); }

.sidebar-panel.is-collapsed { display: none; }

.filters-card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: 0 4px 18px rgba(70, 45, 30, 0.05);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.sidebar-title { font-size: 1rem; font-family: var(--font-body); font-weight: 600; }

.field { display: flex; flex-direction: column; gap: 6px; font-size: 0.82rem; color: var(--muted); }
.field select,
.field input {
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--cream);
  font-family: var(--font-body);
  font-size: 0.88rem;
  color: var(--ink);
  outline: none;
  width: 100%;
}
.field select:focus, .field input:focus { border-color: var(--brick); }

.insights-apply { width: 100%; text-align: center; border: none; }
.insights-reset { text-align: center; font-size: 0.8rem; color: var(--muted); }
.insights-reset:hover { color: var(--brick); }

/* ---------- Stat strip ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.stat-tile {
  background: var(--card);
  border-radius: var(--radius);
  padding: 14px 12px;
  box-shadow: 0 4px 18px rgba(70, 45, 30, 0.05);
  display: flex;
  gap: 10px;
  align-items: flex-start;
  min-width: 0;
  border-top: 3px solid var(--stat-tone);
}
.stat-tile--brick { --stat-tone: var(--data-brick); }
.stat-tile--blue { --stat-tone: var(--data-blue); }
.stat-tile--green { --stat-tone: var(--data-green); }
.stat-tile--gold { --stat-tone: var(--data-gold); }

.stat-icon {
  flex-shrink: 0;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--stat-tone) 14%, transparent);
  color: var(--stat-tone);
}
.stat-icon-svg { display: block; width: 17px; height: 17px; }
.stat-icon-svg svg { width: 100%; height: 100%; }

.stat-body { min-width: 0; }
.stat-label { font-size: 0.68rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.stat-value {
  font-size: 1.25rem; font-weight: 600; margin-top: 3px; font-variant-numeric: proportional-nums;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.stat-note { font-size: 0.72rem; color: var(--muted); margin-top: 3px; line-height: 1.3; }

/* ---------- Panels ---------- */
.panel {
  background: var(--card);
  border-radius: var(--radius);
  padding: 24px 26px;
  box-shadow: 0 4px 18px rgba(70, 45, 30, 0.05);
  margin-bottom: 24px;
}
.panel-head { margin-bottom: 18px; }
.panel-head h2 { font-size: 1.15rem; font-family: var(--font-body); font-weight: 600; display: inline-flex; align-items: center; gap: 8px; }
.panel-sub { font-size: 0.82rem; color: var(--muted); margin-top: 4px; }
.panel-link-more { font-size: 0.82rem; font-weight: 500; color: var(--brick); white-space: nowrap; flex-shrink: 0; }
.panel-link-more:hover { text-decoration: underline; }

/* Shareable-anchor icon injected next to each panel heading (see
   wirePanelLinks() in insights.js) — invisible until hovered/focused so it
   doesn't compete with the heading text at rest. */
.panel-anchor {
  opacity: 0;
  color: var(--muted);
  font-size: 0.85em;
  text-decoration: none;
  transition: opacity 0.15s ease;
}
.panel-head h2:hover .panel-anchor,
.panel-anchor:focus { opacity: 1; }
.panel-anchor.copied { opacity: 1; color: var(--sage); }

.stat-grid-head { margin-bottom: 10px; }
.stat-grid-head h2 {
  font-size: 0.8rem; font-family: var(--font-body); font-weight: 600;
  color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em;
}

.chart-skeleton {
  height: 160px;
  border-radius: 10px;
  background: linear-gradient(90deg, var(--cream) 25%, #f3ece2 37%, var(--cream) 63%);
  background-size: 400% 100%;
  animation: insights-shimmer 1.4s ease infinite;
}
@keyframes insights-shimmer { 0% { background-position: 100% 0; } 100% { background-position: 0 0; } }

.panel-empty { color: var(--muted); font-size: 0.86rem; padding: 24px 0; text-align: center; }

/* ---------- Global rental map ---------- */
.rent-map {
  height: 440px;
  width: 100%;
  border-radius: 10px;
  background: var(--cream);
  z-index: 0;
}
.map-popup { font-family: var(--font-body); font-size: 0.85rem; line-height: 1.5; color: var(--ink); }
.map-popup strong { display: block; font-size: 0.9rem; margin-bottom: 2px; }
.map-popup-meta { color: var(--muted); font-size: 0.78rem; }
.map-popup-see-more { display: inline-block; margin-top: 6px; font-size: 0.8rem; font-weight: 500; color: var(--brick); }
.map-popup-see-more:hover { text-decoration: underline; }

/* ---------- Ranked bar chart (Top 10 Locations) ---------- */
.bench-list { display: flex; flex-direction: column; gap: 10px; }
.bench-row { display: grid; grid-template-columns: 180px 1fr 90px; align-items: center; gap: 12px; }
.bench-name { font-size: 0.85rem; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bench-track { position: relative; height: 22px; background: var(--cream); border-radius: 6px; overflow: hidden; }
.bench-fill {
  position: absolute; inset: 0 auto 0 0; height: 100%;
  background: var(--data-brick); border-radius: 6px;
  transition: width 0.4s ease;
}
.bench-track:hover .bench-fill { background: var(--brick-dark); }
.bench-value { font-size: 0.82rem; text-align: right; font-variant-numeric: tabular-nums; color: var(--ink); }
.bench-meta { font-size: 0.72rem; color: var(--muted); grid-column: 1 / -1; margin-top: -6px; padding-left: 192px; }

/* ---------- Line / scatter charts (SVG) ---------- */
.viz-svg { width: 100%; height: auto; aspect-ratio: 640 / 240; display: block; overflow: visible; }

/* The deals scatter's height is set inline by JS to match however tall
   the deal-callouts column next to it is (see renderDeals), rather than
   the fixed 640:240 every other chart uses — a short chart with a lot of
   empty space below it otherwise, next to a taller card stack. The two
   overrides below hand control of sizing to that inline height: min-height
   is the floor for when there are few/no callouts, and overflow is a
   safety net rather than a normal state — this box is sized to exactly
   fit its svg child, so scrolling here would mean the height match
   itself came out wrong. */
.deals-chart { min-width: 0; min-height: 220px; overflow: auto; }
.deals-chart .viz-svg { height: 100%; aspect-ratio: auto; }
.viz-legend { display: flex; gap: 16px; margin-top: 10px; flex-wrap: wrap; }
.viz-legend-item { display: flex; align-items: center; gap: 6px; font-size: 0.78rem; color: var(--muted); }
.viz-legend-swatch { width: 14px; height: 2px; border-radius: 2px; display: inline-block; }

.viz-tooltip {
  position: fixed;
  pointer-events: none;
  background: var(--ink);
  color: #fff;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 0.78rem;
  line-height: 1.4;
  z-index: 40;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.18);
  transform: translate(-50%, -110%);
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.1s ease;
}
.viz-tooltip.visible { opacity: 1; }
.viz-tooltip strong { font-weight: 600; }

.deals-layout { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 20px; align-items: start; }
.deal-callouts { display: flex; flex-direction: column; gap: 10px; }
.deal-callout {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.deal-callout a { font-weight: 500; }
.deal-callout a:hover { color: var(--brick); }
.deal-discount { color: var(--sage); font-weight: 600; }
.deal-meta { color: var(--muted); font-size: 0.76rem; }

/* ---------- Dispersion range chart ---------- */
.dispersion-list { display: flex; flex-direction: column; gap: 16px; }
.dispersion-row { display: grid; grid-template-columns: 120px 1fr 160px; align-items: center; gap: 14px; }
.dispersion-name { font-size: 0.85rem; font-weight: 500; }
.dispersion-track { position: relative; height: 26px; }
.dispersion-whisker { position: absolute; top: 50%; height: 2px; background: var(--data-grid); transform: translateY(-50%); }
.dispersion-box { position: absolute; top: 5px; bottom: 5px; background: var(--data-brick); opacity: 0.55; border-radius: 4px; }
.dispersion-median { position: absolute; top: 2px; bottom: 2px; width: 2px; background: var(--ink); }
.dispersion-figures { font-size: 0.76rem; color: var(--muted); text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Compare matrix ---------- */
.compare-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 16px; }
.compare-card { border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; }
.compare-card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.compare-card h3 { font-size: 1.02rem; font-family: var(--font-body); margin: 0; }
.compare-remove {
  border: 1px solid var(--line); background: var(--cream); color: var(--muted);
  width: 24px; height: 24px; border-radius: 50%; font-size: 1rem; line-height: 1;
  cursor: pointer; flex-shrink: 0;
}
.compare-remove:hover { border-color: var(--brick); color: var(--brick); }
#compare-add-select {
  padding: 9px 14px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--cream); font-family: var(--font-body); font-size: 0.85rem; color: var(--ink); outline: none;
}
.compare-row { display: flex; justify-content: space-between; font-size: 0.83rem; padding: 6px 0; border-top: 1px solid var(--line); }
.compare-row:first-of-type { border-top: none; }
.compare-row span:first-child { color: var(--muted); }
.compare-row span:last-child { font-weight: 500; }

/* ---------- Ledger table ---------- */
.panel-head-ledger { display: flex; justify-content: space-between; align-items: flex-start; flex-wrap: wrap; gap: 14px; }
.ledger-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.ledger-controls input, .ledger-controls select {
  padding: 9px 14px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--cream); font-family: var(--font-body); font-size: 0.85rem; color: var(--ink); outline: none;
}
.ledger-controls input:focus, .ledger-controls select:focus { border-color: var(--brick); }

.table-scroll { overflow-x: auto; }
.ledger-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; min-width: 720px; }
.ledger-table th {
  text-align: left; font-size: 0.72rem; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--line);
}
.ledger-table td { padding: 12px; border-bottom: 1px solid var(--line); vertical-align: top; }
.ledger-table tr:last-child td { border-bottom: none; }
.ledger-property { font-weight: 500; }
.ledger-location { color: var(--muted); font-size: 0.78rem; }
.ledger-price { font-variant-numeric: tabular-nums; font-weight: 500; }
.ledger-local { color: var(--muted); font-size: 0.76rem; }
.ledger-table a.pill { padding: 5px 14px; font-size: 0.78rem; }

/* ---------- Responsive ---------- */
@media (max-width: 980px) {
  /* minmax(0, 1fr), not a bare 1fr: a bare 1fr track still carries an
     "auto" minimum, which lets a wide child (here, a <select> whose
     longest option is a full country name) force the track — and the
     whole page — wider than the viewport. The explicit 0 minimum is what
     actually makes the column shrink to the available width. */
  .insights-layout { grid-template-columns: minmax(0, 1fr); }
  .insights-sidebar { position: static; }
  .deals-layout { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 600px) {
  .insights-header { flex-direction: column; align-items: flex-start; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  /* A 2-column tile is too narrow for a 6-figure KES value at the desktop
     font size — wrap instead of the ellipsis truncating it to "KES 227,…". */
  .stat-value { font-size: 1.05rem; white-space: normal; overflow: visible; text-overflow: clip; line-height: 1.25; }
  .stat-icon { width: 28px; height: 28px; }
  .stat-icon-svg { width: 15px; height: 15px; }
  .bench-row { grid-template-columns: 96px 1fr 70px; }
  .bench-meta { padding-left: 108px; }
  .dispersion-row { grid-template-columns: 90px 1fr; }
  .dispersion-figures { grid-column: 1 / -1; text-align: left; }
  .panel { padding: 18px; }
  .rent-map { height: 320px; }
}

/* ---------- All Locations page ---------- */
.locations-back { display: block; font-size: 0.82rem; color: var(--muted); margin-bottom: 10px; }
.locations-back:hover { color: var(--brick); }
.locations-page-size { display: flex; align-items: center; gap: 8px; font-size: 0.82rem; color: var(--muted); }
.locations-page-size select {
  padding: 8px 12px; border: 1px solid var(--line); border-radius: 999px;
  background: var(--cream); font-family: var(--font-body); font-size: 0.85rem; color: var(--ink); outline: none;
}
.locations-table td, .locations-table th { white-space: nowrap; }
.locations-table .locations-rank { color: var(--muted); font-variant-numeric: tabular-nums; }
.locations-bar-cell { display: flex; align-items: center; gap: 10px; min-width: 220px; }
.locations-bar-track { position: relative; flex: 1; height: 16px; background: var(--cream); border-radius: 5px; overflow: hidden; }
.locations-bar-fill { position: absolute; inset: 0 auto 0 0; height: 100%; background: var(--data-brick); border-radius: 5px; }
.locations-bar-value { font-variant-numeric: tabular-nums; font-weight: 500; white-space: nowrap; }

/* ---------- Sign-in gate: locked listing links & gated pages ---------- */
.auth-locked { color: var(--muted); cursor: pointer; }
.auth-locked:hover { color: var(--brick); }
.auth-locked svg { vertical-align: -2px; margin-right: 4px; }

.auth-required-panel { text-align: center; padding: 56px 24px; }
.auth-required-panel h2 { font-size: 1.2rem; margin-bottom: 8px; }
.auth-required-panel p { color: var(--muted); font-size: 0.9rem; max-width: 440px; margin: 0 auto 20px; }
.auth-required-panel .auth-modal-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

/* ---------- Browse Rental Markets (internal links to per-location pages) ---------- */
.browse-locations { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.browse-locations-towns { margin-top: 10px; padding-top: 14px; border-top: 1px solid var(--line); }
.browse-locations .pill span { color: var(--muted); font-size: 0.78rem; }
.browse-locations .pill.active { color: #fff; }
.browse-locations .pill.active span { color: rgba(255, 255, 255, 0.75); }
