* {
  box-sizing: border-box;
}

/* Remove browser focus ring on Leaflet SVG polygons when clicked */
.leaflet-interactive:focus {
  outline: none;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden; /* prevent any scrolling beyond the viewport — the whole
                       UI is absolutely/fixed positioned inside #app */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

#app {
  position: relative;
  /* 100dvh tracks the real visible area as the browser chrome shows/hides.
     Keep the vh fallback for browsers that don't understand dvh. */
  height: 100vh;
  height: 100dvh;
  width: 100vw;
  overflow: hidden; /* belt-and-suspenders: nothing should scroll outside #app */
}

#map {
  position: absolute;
  inset: 0;
  z-index: 1;
}

/* ---------------- Back link ---------------- */

/* Floating pill, top-left, mirrors #page-title's styling so it reads as
   part of the same control row rather than a separate element. */
#back-link {
  position: absolute;
  top: 10px;
  left: 12px;
  z-index: 1000;
  padding: 5px 14px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  font-size: 13px;
  font-weight: 600;
  color: #0D6E6E;
  text-decoration: none;
  white-space: nowrap;
}

#back-link:hover {
  text-decoration: underline;
}

/* ---------------- Page title ---------------- */

/* Centred, compact title pill (Phase 8 item 6) — sits above the search/
   filter bar, which is pushed down to make room (see #top-bar top below)
   so the two never overlap. */
#page-title {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  margin: 0;
  padding: 5px 16px;
  background: white;
  border-radius: 20px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  font-size: 14px;
  font-weight: 700;
  color: #222;
  white-space: nowrap;
  pointer-events: none;
}

/* ---------------- Top bar ---------------- */

/* Search and the Metric/Period/Apply group are two separate floating boxes
   again (disconnected per Luca's request, 2026-06-24, reverting the brief
   single-bar grouping from earlier the same day) — the search box is a
   live-as-you-type autocomplete with no Apply gate, so bundling it visually
   with controls that DO require an explicit Apply step read as misleading. */
#top-bar {
  position: absolute;
  top: 52px; /* below #page-title */
  left: 12px;
  right: 12px;
  z-index: 999;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 12px;
  flex-wrap: wrap;
  pointer-events: none; /* let map drag through gaps */
}

#top-bar > * {
  pointer-events: auto;
}

#search-container {
  position: relative;
  width: 260px;
}

#search-input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid #d0d0d0;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  font-size: 14px;
}

#search-results {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.2);
  margin-top: 4px;
  overflow: hidden;
  display: none;
}

#search-results.visible {
  display: block;
}

.search-result-item {
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
  border-bottom: 1px solid #f0f0f0;
}

.search-result-item:last-child {
  border-bottom: none;
}

.search-result-item:hover {
  background: #f5f5f5;
}

#filter-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: white;
  padding: 8px 12px;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  flex-wrap: wrap;
  margin: 0;
}

.filter-field {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  color: #555;
  gap: 2px;
}

.filter-field select {
  font-size: 13px;
  padding: 4px 6px;
  border-radius: 4px;
  border: 1px solid #ccc;
}

/* Metric/Period changes only take effect once this is clicked (or Enter is
   pressed while a select is focused, standard form-submit behaviour) —
   Luca's request, 2026-06-24, replacing the previous immediate-update-on-
   change behaviour. */
#apply-filters {
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 600;
  color: white;
  background: #0D6E6E;
  border: none;
  border-radius: 6px;
  cursor: pointer;
}

#apply-filters:hover {
  background: #0A5050;
}

/* ---------------- Legend ---------------- */

/* Discrete colour-band swatches rather than a continuous gradient bar
   (Luca's request, 2026-06-24: on the old smooth gradient, a $700k suburb
   and a $1M suburb looked almost the same shade — adjacent bands now use
   visually distinct ColorBrewer RdYlGn colours instead of a blend). */
#legend {
  position: absolute;
  bottom: 24px;
  left: 12px;
  z-index: 1000;
  background: white;
  padding: 8px 12px;
  border-radius: 8px;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  font-size: 11px;
  max-height: 40vh;
  overflow-y: auto;
}

#legend .legend-title {
  font-weight: 600;
  margin-bottom: 4px;
  font-size: 12px;
}

#legend .legend-row {
  display: flex;
  align-items: center;
  gap: 6px;
  line-height: 1.5;
}

#legend .legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex: 0 0 auto;
}

#legend .legend-range {
  color: #444;
  white-space: nowrap;
}

/* ---------------- Detail panel ---------------- */

#detail-panel {
  position: absolute;
  top: 0;
  right: 0;
  height: 100%;
  width: 360px;
  background: white;
  z-index: 1001;
  box-shadow: -4px 0 12px rgba(0,0,0,0.2);
  padding: 20px;
  overflow-y: auto;
  transition: transform 0.2s ease;
}

#detail-panel.hidden {
  transform: translateX(100%);
}

#close-panel {
  position: absolute;
  top: 12px;
  right: 14px;
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: #888;
}

#panel-suburb-name {
  margin: 0 30px 16px 0;
  font-size: 20px;
}

#panel-metrics {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 20px;
}

.metric-card {
  background: #f7f7f7;
  border-radius: 8px;
  padding: 10px 12px;
}

.metric-card .label {
  font-size: 11px;
  color: #777;
  margin-bottom: 2px;
}

.metric-card .value {
  font-size: 17px;
  font-weight: 600;
}

/* ---------------- Loading overlay ---------------- */

#loading {
  position: absolute;
  inset: 0;
  z-index: 2000;
  background: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 14px;
  color: #555;
}

#loading.hidden {
  display: none;
}

#loading-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#loading-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #e0e0e0;
  border-top-color: #0D6E6E;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ---------------- Error banner ---------------- */

#error-banner {
  position: absolute;
  bottom: 70px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1500;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 8px;
  padding: 10px 40px 10px 14px;
  font-size: 13px;
  color: #991b1b;
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
  white-space: nowrap;
  max-width: calc(100% - 24px);
}

#error-banner.hidden {
  display: none;
}

#error-dismiss {
  position: absolute;
  top: 50%;
  right: 10px;
  transform: translateY(-50%);
  background: none;
  border: none;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  color: #991b1b;
  padding: 0 2px;
}

/* Panel chart error fallback */
#panel-chart-error {
  font-size: 13px;
  color: #888;
  text-align: center;
  margin: 8px 0 0;
}
#panel-chart-error.hidden {
  display: none;
}

/* ---------------- Tooltip ---------------- */

.suburb-tooltip {
  font-size: 13px;
  line-height: 1.4;
}

.suburb-tooltip .tooltip-name {
  font-weight: 600;
}

/* ---------------- Mobile (Phase 8 item 5) ---------------- */

@media (max-width: 640px) {
  #back-link {
    font-size: 12px;
    padding: 4px 10px;
  }

  /* On mobile the centered title overlaps the left-aligned back link.
     Pin it to the right edge instead so both elements have clear space. */
  #page-title {
    left: auto;
    right: 12px;
    transform: none;
    font-size: 13px;
    padding: 4px 12px;
  }

  #top-bar {
    top: 46px;
    max-width: none;
  }

  #search-container {
    width: 100%;
  }

  /* Compact filter form: all selects + Apply on a single row so the
     controls don't eat up two rows of vertical space on narrow screens. */
  #filter-form {
    width: 100%;
    flex-wrap: nowrap;
    padding: 5px 8px;
    gap: 4px;
    align-items: center;
  }

  .filter-field {
    flex: 1 1 0;
    min-width: 0;
    font-size: 10px;
    gap: 1px;
  }

  .filter-field select {
    font-size: 11px;
    padding: 3px 2px;
    width: 100%;
    min-width: 0;
  }

  #apply-filters {
    flex-shrink: 0;
    padding: 5px 10px;
    font-size: 12px;
    align-self: flex-end;
  }

  #legend {
    max-height: 26vh;
    font-size: 10px;
  }

  /* Detail panel bottom sheet — kept shallow so a good portion of the map
     remains visible above it. Chart is still accessible by scrolling inside
     the panel (overflow-y: auto on the base rule). */
  #detail-panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: auto;
    max-height: 38vh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -6px 20px rgba(0,0,0,0.25);
    padding: 12px 14px;
    transform: translateY(0);
  }

  #detail-panel.hidden {
    transform: translateY(100%);
  }

  #panel-suburb-name {
    font-size: 15px;
    margin: 0 28px 6px 0;
  }

  #panel-metrics {
    gap: 6px;
    margin-bottom: 8px;
  }

  .metric-card {
    padding: 5px 8px;
  }

  .metric-card .label {
    font-size: 10px;
    margin-bottom: 1px;
  }

  .metric-card .value {
    font-size: 14px;
  }

  #panel-chart {
    max-height: 80px;
  }
}
