* { box-sizing: border-box; }
html, body, #app { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, sans-serif;
  color: #1c1d1f;
  background: #0f1115;
}

#app { display: flex; flex-direction: column; height: 100vh; }

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  background: #11151c;
  color: #e6e9ef;
  border-bottom: 1px solid #1f242c;
}
#topbar .title strong { font-size: 16px; letter-spacing: 0.01em; }
#topbar .subtitle { color: #8a93a3; margin-left: 10px; font-size: 12px; }
#topbar button {
  background: #1d2330;
  color: #e6e9ef;
  border: 1px solid #2a3242;
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  min-height: 36px;
  touch-action: manipulation;
}
#topbar button:hover { background: #28304a; }
#topbar button:active { background: #324063; }

.topbar-search {
  position: relative;
  flex: 1;
  max-width: 320px;
  margin: 0 16px;
}
.topbar-search input {
  width: 100%;
  background: #1d2330;
  color: #e6e9ef;
  border: 1px solid #2a3242;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 13px;
  outline: none;
  min-height: 36px;
}
.topbar-search input::placeholder { color: #6c7587; }
.topbar-search input:focus { border-color: #5d8ab8; background: #232a3a; }

#search-results {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: #ffffff;
  color: #1c1d1f;
  border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  max-height: 320px;
  overflow-y: auto;
  z-index: 8;
  padding: 4px;
}
.search-result {
  padding: 8px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
}
.search-result:hover, .search-result.active { background: #f1f5fb; }
.search-result-name { font-weight: 600; }
.search-result-meta { color: #6b7280; font-size: 11px; margin-top: 2px; }
.search-empty { padding: 12px; color: #888; font-size: 12px; text-align: center; }

main {
  flex: 1;
  position: relative;
  overflow: hidden;
}

#map { position: absolute; inset: 0; }

#sidepanel {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 380px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
  background: #ffffff;
  color: #1c1d1f;
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.25);
  padding: 18px 20px;
  z-index: 5;
}
#sidepanel.hidden { display: none; }
#close-panel {
  position: absolute;
  right: 8px;
  top: 6px;
  background: transparent;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: #555;
}
#sidepanel h3 {
  margin: 0 0 4px;
  font-size: 16px;
}
#sidepanel .meta { color: #6b7280; font-size: 12px; margin-bottom: 8px; }

.panel-head-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 8px;
  padding-right: 22px; /* leave room for the absolute close button */
}
.panel-head-text { flex: 1; min-width: 0; }
.copy-link-btn {
  flex-shrink: 0;
  background: #f4f6fa;
  color: #1c1d1f;
  border: 1px solid #d8dde6;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 11px;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  touch-action: manipulation;
  transition: background 120ms;
}
.copy-link-btn:hover { background: #e9eef7; }
.copy-link-btn.copied { background: #ecf8ee; color: #1f5d2a; border-color: #c1e0c5; }
.copy-link-icon { font-size: 13px; }
#sidepanel .pred-box {
  background: #f6f8fb;
  border-radius: 8px;
  padding: 10px 12px;
  margin: 10px 0 14px;
}
#sidepanel .pred-row { display: flex; justify-content: space-between; padding: 2px 0; font-size: 13px; }
#sidepanel .pred-row .v { font-weight: 600; }
#sidepanel .pred-row.headline .v { font-size: 22px; color: #0a4f0a; }
#sidepanel .interval { color: #6b7280; font-size: 11px; }

#sidepanel table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  margin-top: 6px;
}
#sidepanel th, #sidepanel td {
  text-align: left;
  padding: 6px 4px;
  border-bottom: 1px solid #eee;
}
#sidepanel th { color: #6b7280; font-weight: 500; font-size: 11px; }
#sidepanel .price { text-align: right; font-variant-numeric: tabular-nums; }

#legend {
  position: absolute;
  bottom: 18px;
  left: 18px;
  background: rgba(255,255,255,0.95);
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
  z-index: 4;
  width: 240px;
  font-size: 12px;
}
.legend-title { font-weight: 600; margin-bottom: 6px; }
.legend-bar {
  height: 10px;
  border-radius: 2px;
  background: linear-gradient(to right, #5b3a86, #5d8ab8, #b9d2c0, #f1c987, #c54040);
  margin-bottom: 4px;
}
.legend-labels {
  display: flex;
  justify-content: space-between;
  color: #555;
  font-variant-numeric: tabular-nums;
}
.legend-note { color: #777; margin-top: 6px; font-size: 11px; }

#status-bar {
  position: absolute;
  bottom: 12px;
  right: 18px;
  background: rgba(15,17,21,0.85);
  color: #cbd2dc;
  padding: 4px 10px;
  border-radius: 6px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  z-index: 4;
}

#toppanel {
  position: absolute;
  top: 12px;
  left: 12px;
  width: 380px;
  max-height: calc(100vh - 100px);
  overflow-y: auto;
  background: #ffffff;
  border-radius: 10px;
  box-shadow: 0 6px 24px rgba(0,0,0,0.25);
  z-index: 5;
  padding: 0;
}
#toppanel.hidden { display: none; }
.toppanel-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px 8px;
  border-bottom: 1px solid #eee;
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 1;
}
.toppanel-head h3 { margin: 0; font-size: 14px; }
.toppanel-controls { display: flex; gap: 8px; align-items: center; }
.toppanel-controls select {
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 4px;
  border: 1px solid #ccc;
}
#close-toppanel {
  background: transparent;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: #555;
}
#toppanel-list { padding: 4px 8px 12px; }
.top-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  border-radius: 6px;
}
.top-item:hover { background: #f5f8fb; }
.top-rank {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #c54040;
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
  flex-shrink: 0;
}
.top-rank.r2 { background: #e87a32; }
.top-rank.r3 { background: #f1c987; color: #333; }
.top-rank.rN { background: #5d8ab8; }
.top-info { flex: 1; min-width: 0; }
.top-info-name { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.top-info-meta { font-size: 11px; color: #777; }
.top-cagr { text-align: right; flex-shrink: 0; }
.top-cagr-val { font-size: 14px; font-weight: 600; color: #0a4f0a; }
.top-cagr-int { font-size: 10px; color: #888; }

.shap-section { margin-top: 16px; }
.shap-section h4 {
  font-size: 13px;
  margin: 0 0 6px;
  color: #333;
}
.shap-row {
  display: grid;
  grid-template-columns: 16px 1fr 70px;
  gap: 8px;
  align-items: center;
  padding: 4px 0;
  font-size: 11px;
  border-bottom: 1px solid #f3f3f3;
}
.shap-arrow { font-size: 14px; line-height: 1; }
.shap-arrow.up { color: #0a7d20; }
.shap-arrow.down { color: #b03030; }
.shap-feat { color: #333; line-height: 1.2; }
.shap-feat-val { font-size: 10px; color: #888; display: block; }
.shap-bar {
  height: 14px;
  border-radius: 2px;
  position: relative;
  background: #f0f0f0;
  overflow: hidden;
}
.shap-bar-inner {
  position: absolute;
  top: 0;
  bottom: 0;
  background: rgba(10, 125, 32, 0.5);
}
.shap-bar-inner.down { background: rgba(176, 48, 48, 0.5); }
.shap-pp {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: #444;
}

/* Shared modal frame — used by #metrics-modal and #howitworks-modal */
#metrics-modal,
#howitworks-modal {
  position: absolute;
  inset: 0;
  background: rgba(8, 10, 15, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}
#metrics-modal.hidden,
#howitworks-modal.hidden { display: none; }
.modal-card {
  background: #fff;
  border-radius: 14px;
  padding: 28px 30px 24px;
  width: min(680px, 92vw);
  max-height: 86vh;
  overflow-y: auto;
  position: relative;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.28);
}
.modal-card h2 {
  margin: 0 0 18px;
  font-size: 20px;
  font-weight: 600;
  color: #111827;
  letter-spacing: -0.01em;
}
#close-metrics,
#close-howitworks {
  position: absolute;
  right: 14px;
  top: 12px;
  background: transparent;
  border: none;
  font-size: 24px;
  color: #6b7280;
  cursor: pointer;
  line-height: 1;
  padding: 4px 10px;
  border-radius: 6px;
  transition: background 0.15s;
}
#close-metrics:hover,
#close-howitworks:hover { background: #f3f4f6; color: #111827; }

@media (max-width: 768px) {
  .modal-card { padding: 22px 22px 18px; border-radius: 12px; }
  .modal-card h2 { font-size: 18px; margin-bottom: 14px; }
}
.metric-section { margin-bottom: 22px; }
.metric-section h3 {
  font-size: 12px;
  color: #6b7280;
  margin: 18px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-weight: 600;
}
.metric-section h4 { font-size: 12px; margin: 0 0 6px; color: #444; font-weight: 600; }
.metric-intro { font-size: 12px; color: #555; margin: 0 0 8px; line-height: 1.45; }
.metric-fineprint { font-size: 11px; color: #777; margin: 6px 0 0; line-height: 1.4; }
.metric-table { width: 100%; border-collapse: collapse; font-size: 12px; }
.metric-table th, .metric-table td { padding: 5px 8px; text-align: left; border-bottom: 1px solid #eee; }
.metric-table th { color: #6b7280; font-weight: 500; font-size: 11px; }
.metric-table.small th, .metric-table.small td { padding: 3px 6px; font-size: 11px; }
.metric-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.threshold-pass { color: #0a7d20; font-weight: 600; }
.threshold-fail { color: #b03030; font-weight: 600; }

/* ----- Stat cards (top of modal) ----- */
.metric-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}
.stat-card {
  background: linear-gradient(180deg, #f7fafd, #eef2f8);
  border: 1px solid #dde3ec;
  border-radius: 10px;
  padding: 12px 14px;
  min-height: 88px;
}
.stat-card-label {
  font-size: 10.5px;
  color: #5b6477;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}
.stat-card-value {
  font-size: 26px;
  font-weight: 700;
  color: #11151c;
  margin-top: 2px;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.stat-card-unit {
  font-size: 13px;
  font-weight: 500;
  color: #5b6477;
  margin-left: 2px;
}
.stat-card-hint {
  font-size: 10.5px;
  color: #6c7587;
  margin-top: 4px;
  line-height: 1.3;
}

/* ----- Methodology cards (training / testing) ----- */
.method-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin: 12px 0 6px;
}
.method-card {
  background: #fbfcfe;
  border: 1px solid #e3e7ee;
  border-radius: 10px;
  padding: 12px 14px;
}
.method-card-title {
  font-size: 13px;
  font-weight: 600;
  color: #11151c;
  margin-bottom: 8px;
}
.method-list { margin: 0; padding-left: 18px; font-size: 12px; line-height: 1.55; color: #333; }
.method-list li { margin: 4px 0; }
.method-list b { color: #11151c; }

/* ----- Feature-importance bars ----- */
.fi-table { display: flex; flex-direction: column; gap: 4px; }
.fi-row {
  display: grid;
  grid-template-columns: 1fr 100px 40px;
  gap: 8px;
  align-items: center;
  font-size: 12px;
  padding: 4px 0;
  border-bottom: 1px solid #f3f3f3;
}
.fi-label { color: #333; line-height: 1.3; }
.fi-bar {
  height: 12px;
  background: #f0f0f0;
  border-radius: 2px;
  overflow: hidden;
}
.fi-bar-inner {
  height: 100%;
  background: linear-gradient(90deg, #5d8ab8, #b9d2c0);
}
.fi-pct {
  text-align: right;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: #444;
  font-size: 11px;
}

/* ----- Slice grid ----- */
.slice-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 12px;
}

/* ----- Caveat box ----- */
.caveat-box {
  background: #fffaf0;
  border: 1px solid #f3e0bd;
  border-radius: 10px;
  padding: 12px 16px 12px 14px;
}
.caveat-box ul {
  margin: 4px 0 0;
  padding-left: 4px;
  list-style: none;
  font-size: 12px;
  line-height: 1.5;
  color: #333;
}
.caveat-box li { margin: 6px 0; }

/* mobile: collapse grids to single column */
@media (max-width: 768px) {
  .metric-cards { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .stat-card-value { font-size: 22px; }
  .method-grid { grid-template-columns: 1fr; }
  .slice-grid { grid-template-columns: 1fr; }
  .fi-row { grid-template-columns: 1fr 70px 36px; }
}
.banner-fail {
  background: #fff4ec;
  border: 1px solid #f1cdb3;
  color: #9a4d18;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12px;
  margin-bottom: 12px;
}
.banner-pass {
  background: #ecf8ee;
  border: 1px solid #c1e0c5;
  color: #1f5d2a;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 12px;
  margin-bottom: 12px;
}
.hidden { display: none !important; }

/* Mobile drag-handle for the side panel bottom-sheet (visible only ≤768) */
.sidepanel-handle {
  display: none;
  width: 36px;
  height: 4px;
  border-radius: 2px;
  background: #cfd4dc;
  margin: 6px auto 8px;
}

/* ============================================================
   Mobile (≤ 768px) — bottom sheet panels, compact topbar
   ============================================================ */
@media (max-width: 768px) {
  /* Topbar: respect notch via safe-area-top, compact rows */
  #topbar {
    padding: max(10px, env(safe-area-inset-top)) 12px 10px;
    gap: 8px;
    flex-wrap: wrap;
  }
  #topbar .title strong { font-size: 14px; }
  #topbar .subtitle { display: none; }
  .topbar-actions { display: flex; gap: 6px; flex-shrink: 0; }
  /* 44px tap target (Apple HIG). Slightly more padding so labels don't crowd. */
  #topbar button {
    padding: 10px 12px;
    font-size: 12px;
    min-height: 44px;
    border-radius: 8px;
  }
  /* Search takes full width on its own row, below the title.
     16px font-size prevents iOS Safari from zooming on focus. */
  .topbar-search { order: 3; flex-basis: 100%; max-width: none; margin: 4px 0 0; }
  .topbar-search input {
    font-size: 16px;
    min-height: 44px;
    padding-left: 14px;
  }
  #search-results { max-height: 55vh; }

  /* Side panel becomes a bottom sheet — uses dvh so it doesn't jump
     when iOS Safari shows/hides its address bar. */
  #sidepanel {
    top: auto;
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-height: 78vh;
    max-height: 78dvh;
    border-radius: 18px 18px 0 0;
    padding: 4px 18px 24px;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.28);
  }
  .sidepanel-handle { display: block; }
  /* Bigger close hit-target (44px) on the corner */
  #close-panel {
    top: 6px;
    right: 8px;
    font-size: 26px;
    padding: 8px 12px;
    min-width: 44px;
    min-height: 44px;
  }

  /* Top panel: same dvh treatment */
  #toppanel {
    top: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: 80vh;
    max-height: 80dvh;
    border-radius: 0 0 18px 18px;
  }
  .toppanel-head { padding: 14px 16px 10px; }
  #close-toppanel {
    min-width: 44px;
    min-height: 44px;
    padding: 8px 12px;
    font-size: 24px;
  }

  /* Legend chip — keep visible but smaller */
  #legend {
    bottom: 12px;
    left: 12px;
    width: 150px;
    padding: 8px 10px;
    font-size: 11px;
    border-radius: 10px;
  }
  .legend-note { display: none; }

  /* Hide tiny status bar on mobile — irrelevant for casual users */
  #status-bar { display: none; }

  /* Larger top-item rows for fingers */
  .top-item { padding: 13px 12px; }
  .top-rank { width: 32px; height: 32px; font-size: 13px; }
  .top-info-name { font-size: 14px; }
  .top-info-meta { font-size: 12px; }
  .top-cagr-val { font-size: 16px; }

  /* Mapbox controls — 44px tap targets */
  .mapboxgl-ctrl-group button { width: 44px !important; height: 44px !important; }

  /* Make the share button tappable */
  .copy-link-btn {
    min-height: 40px;
    padding: 6px 12px;
  }

  /* Money headline padding tightens on small screens (already in money block) */
}

/* Very narrow phones (iPhone SE @ 320px / Mini @ 375px) */
@media (max-width: 480px) {
  #topbar button { padding: 10px 10px; font-size: 11.5px; }
  .topbar-actions { gap: 4px; }
  #topbar .title strong { font-size: 13px; }
}

/* iOS safe-area: keep bottom sheet above the home indicator */
@supports (padding: max(0px)) {
  @media (max-width: 768px) {
    #sidepanel { padding-bottom: max(24px, env(safe-area-inset-bottom)); }
    #toppanel { padding-bottom: max(8px, env(safe-area-inset-bottom)); }
  }
}

/* ============================================================
   Money headline (£ today → £ in 3y) on the LSOA panel
   ============================================================ */
.money-headline {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 10px;
  align-items: center;
  background: linear-gradient(180deg, #f7f9fc 0%, #f0f4fa 100%);
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  padding: 14px 14px 12px;
  margin: 12px 0 6px;
}
.money-now, .money-future {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}
.money-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: #6b7280;
}
.money-val {
  font-size: 18px;
  font-weight: 600;
  color: #1f2937;
  line-height: 1.2;
}
.money-mid {
  font-size: 11px;
  color: #6b7280;
  margin-top: 1px;
}
.money-arrow {
  color: #9aa4b1;
  font-size: 20px;
  font-weight: 300;
  text-align: center;
  flex: 0 0 auto;
}
.money-foot {
  font-size: 11px;
  color: #6b7280;
  margin: 0 2px 14px;
  line-height: 1.5;
}

.rank-badge {
  display: inline-block;
  margin-left: 6px;
  padding: 2px 7px;
  border-radius: 999px;
  background: #eef2f7;
  color: #475569;
  font-size: 11px;
  font-weight: 500;
  white-space: nowrap;
}

.pred-row.pred-foot {
  display: block;
  font-size: 11px;
  color: #6b7280;
  line-height: 1.5;
  border-top: 1px solid #e6eaf0;
  padding-top: 8px;
  margin-top: 6px;
}
.pred-row.pred-foot em {
  color: #1f2937;
  font-style: italic;
}

/* ============================================================
   "How it works" modal — shell shared with #metrics-modal above.
   Below: only the inner-content styling specific to this modal.
   ============================================================ */
.hiw-section { margin-bottom: 18px; }
.hiw-section h3 {
  font-size: 12px;
  color: #6b7280;
  margin: 18px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  font-weight: 600;
}
.hiw-section p {
  font-size: 13px;
  line-height: 1.55;
  color: #333;
  margin: 6px 0;
}
.hiw-section ul {
  margin: 4px 0 0;
  padding-left: 18px;
  font-size: 13px;
  line-height: 1.55;
  color: #333;
}
.hiw-section li { margin: 4px 0; }
.hiw-callout {
  background: #fffaf0;
  border: 1px solid #f6dbb1;
  border-radius: 8px;
  padding: 12px 14px;
}
.hiw-callout h3 { color: #92400e; margin-top: 0; }
.hiw-callout em { font-style: italic; }
.hiw-quiet { font-size: 12px; color: #6b7280; }
.hiw-links {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  border-top: 1px solid #e6eaf0;
  padding-top: 12px;
}
.hiw-links a {
  font-size: 12px;
  color: #2563eb;
  text-decoration: none;
}
.hiw-links a:hover { text-decoration: underline; }
.hiw-footer {
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid #e6eaf0;
  padding-top: 14px;
  margin-top: 6px;
}
#hiw-got-it.primary {
  background: #111827;
  color: #fff;
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  min-height: 40px;
}
#hiw-got-it.primary:hover { background: #1f2937; }

@media (max-width: 768px) {
  .money-headline {
    grid-template-columns: 1fr;
    gap: 4px;
    padding: 12px;
  }
  .money-arrow { display: none; }
  .money-future { border-top: 1px dashed #d4dae3; padding-top: 8px; margin-top: 4px; }
  .money-val { font-size: 17px; }
}
