/* ── Devolution Explorer ─────────────────────────────────────────────────
   Clean, light map explorer inspired by the ONS "Explore local statistics"
   service. Palette follows the GOV.UK Analysis Function categorical colours. */

* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --ink:        #0b0c0c;   /* GOV.UK near-black text            */
  --ink-soft:   #505a5f;   /* GOV.UK secondary text            */
  --line:       #e2e4e7;
  --panel-bg:   #ffffff;
  --header-bg:  #ffffff;
  --focus:      #ffdd00;   /* GOV.UK focus yellow              */

  /* Devolution tier colours (GOV.UK Analysis Function categorical set) */
  --c-established: #12436d;
  --c-mayoral:     #28a197;
  --c-foundation:  #f46a25;
  --c-proposed:    #a285d1;
}

body {
  font-family: -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 14px;
  color: var(--ink);
  /* --vh100 is the real visible height, set from JS (window.innerHeight) so the
     layout doesn't spill below the toolbar on Android/TV Chrome where 100vh is
     taller than the visible viewport. Falls back to 100vh if JS doesn't run. */
  height: 100vh;
  height: var(--vh100, 100vh);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: var(--vh100, 100vh);
}

/* ── Header ───────────────────────────────────────────────────────────── */
#header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  background: var(--header-bg);
  border-bottom: 1px solid var(--line);
  padding: 0 22px;
  height: 56px;
  flex-shrink: 0;
  z-index: 1000;
}

/* Menu button + wrapper: on wide screens the wrapper is transparent to layout
   (display:contents), so the two control groups sit inline in the header exactly
   as before. On mobile they collapse into a dropdown behind the menu button. */
#menu-btn { display: none; }
#header-menu { display: contents; }

#header::before {
  /* GOV.UK-style accent bar */
  content: "";
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 4px;
  background: var(--c-established);
}

#header-title { display: flex; align-items: baseline; gap: 12px; }

#header-main {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.01em;
  white-space: nowrap;
}

#header-sub {
  font-size: 13px;
  color: var(--ink-soft);
  white-space: nowrap;
}

#header-controls { display: flex; align-items: center; gap: 10px; }

.fs-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.fs-btn:hover { border-color: #b6bcc2; background: #f8f9fa; }
.fs-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 0; }
.fs-ic { display: block; flex-shrink: 0; }
.fs-btn .fs-exit { display: none; }
.fs-btn.is-fullscreen .fs-enter { display: none; }
.fs-btn.is-fullscreen .fs-exit { display: block; }

/* Icon-only button (re-centre) — matches the fullscreen button */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
}
.icon-btn:hover { border-color: #b6bcc2; background: #f8f9fa; }
.icon-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 0; }

/* Highlight tools (top bar) */
#highlight-tools { display: flex; align-items: center; gap: 8px; }
.tools-label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
}
.hl-btn {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 13px;
  cursor: pointer;
  white-space: nowrap;
}
.hl-btn:hover { border-color: #b6bcc2; }
.hl-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 0; }
.hl-btn.active {
  background: var(--c-established);
  border-color: var(--c-established);
  color: #fff;
}

#area-select {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 7px 30px 7px 12px;
  min-width: 240px;
  cursor: pointer;
  appearance: none;
  /* chevron */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23505a5f' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 11px center;
}
#area-select:hover { border-color: #b6bcc2; }
#area-select:focus-visible { outline: 3px solid var(--focus); outline-offset: 0; }

.visually-hidden {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ── Map container ────────────────────────────────────────────────────── */
#map-container {
  position: relative;
  flex: 1;
  overflow: hidden;
}

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

/* ── Legend ───────────────────────────────────────────────────────────── */
#legend {
  position: absolute;
  bottom: 24px;
  left: 16px;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px 14px;
  box-shadow: 0 1px 6px rgba(11, 12, 12, 0.12);
  z-index: 500;
  max-width: 230px;
}

.legend-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  margin-bottom: 9px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 9px;
  font-size: 13px;
  margin-bottom: 6px;
  color: var(--ink);
}

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

.legend-note {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-size: 11px;
  line-height: 1.4;
  color: var(--ink-soft);
}

/* ── Side panel ───────────────────────────────────────────────────────── */
#panel {
  position: absolute;
  top: 16px;
  right: 16px;
  bottom: 16px;
  width: 360px;
  background: var(--panel-bg);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(11, 12, 12, 0.14);
  z-index: 600;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

#panel-default {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 36px 28px;
  text-align: center;
  color: var(--ink-soft);
}

#panel-icon { font-size: 36px; margin-bottom: 16px; opacity: 0.75; }
#panel-default p { font-size: 14px; line-height: 1.55; margin-bottom: 10px; }
.panel-hint { font-size: 12.5px; color: #8a9196; }

#panel-content.hidden, #panel-default.hidden { display: none; }
#panel-content { display: flex; flex-direction: column; height: 100%; }

#panel-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 28px;
  height: 28px;
  border: none;
  background: rgba(255, 255, 255, 0.2);
  color: #fff;
  font-size: 22px;
  line-height: 1;
  border-radius: 4px;
  cursor: pointer;
  z-index: 2;
}
#panel-close:hover { background: rgba(255, 255, 255, 0.35); }

#panel-header {
  padding: 18px 20px 16px;
  color: #fff;
  flex-shrink: 0;
  /* background colour set per-tier from JS */
  background: var(--c-established);
}

#panel-name {
  font-size: 24px;
  font-weight: 700;
  line-height: 1.25;
  margin-bottom: 10px;
  padding-right: 26px;
}

#panel-badges { display: flex; gap: 6px; flex-wrap: wrap; }

.badge {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 3px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  background: rgba(255, 255, 255, 0.22);
  color: #fff;
}
.badge.hidden { display: none; }

#panel-body {
  padding: 4px 20px 20px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;   /* momentum scroll on touch devices */
  overscroll-behavior: contain;        /* don't chain scroll to the map/page */
  touch-action: pan-y;                 /* allow vertical touch scrolling */
  flex: 1;
  min-height: 0;                       /* let the flex child shrink so it can scroll */
}

.panel-section { padding: 16px 0; border-bottom: 1px solid var(--line); }
.panel-section:last-child { border-bottom: none; }

.panel-section-title {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-soft);
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.coming-soon {
  font-size: 9.5px;
  background: #fff3d6;
  color: #7a5900;
  padding: 1px 6px;
  border-radius: 3px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: none;
}

.fact-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 6px 0;
}
.fact-label { color: var(--ink-soft); font-size: 13px; flex-shrink: 0; }
.fact-value { font-size: 13px; font-weight: 500; text-align: right; }
.fact-value.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }

.lad-list {
  list-style: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.lad-list li {
  font-size: 12px;
  background: #f3f5f7;
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 3px 9px;
  border-radius: 12px;
}

/* Loading placeholder for the qualitative content still to be added */
.placeholder-row {
  height: 10px;
  background: linear-gradient(90deg, #eef1f4 25%, #e2e6ea 50%, #eef1f4 75%);
  background-size: 200% 100%;
  animation: shimmer 1.6s infinite;
  border-radius: 4px;
  margin-bottom: 10px;
}
.placeholder-row.short { width: 55%; }
@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── Populated panel content ──────────────────────────────────────────── */
.panel-website {
  display: inline-block;
  margin-top: 10px;
  font-size: 12px;
  font-weight: 600;
  color: #fff;
  background: rgba(255, 255, 255, 0.18);
  padding: 4px 10px;
  border-radius: 4px;
  text-decoration: none;
}
.panel-website:hover { background: rgba(255, 255, 255, 0.30); }
.panel-website.hidden { display: none; }

.kv {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 5px 0;
}
.kv-label { color: var(--ink-soft); font-size: 12.5px; flex-shrink: 0; }
.kv-value { font-size: 12.5px; font-weight: 500; text-align: right; }

.sub-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--ink-soft);
  margin: 12px 0 5px;
}
.panel-section .sub-label:first-child { margin-top: 0; }

.prose {
  white-space: pre-line;   /* preserve the source's line breaks / numbered lists */
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--ink);
  margin-bottom: 4px;
}
.prose em { color: var(--ink-soft); }

.chips { list-style: none; display: flex; flex-wrap: wrap; gap: 6px; }
.chips li {
  font-size: 12px;
  background: #f3f5f7;
  border: 1px solid var(--line);
  color: var(--ink);
  padding: 3px 9px;
  border-radius: 12px;
}

.tier-line {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  margin-bottom: 8px;
}
.tier-dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }

/* Powers — per-competency detail (competency underlined, comment beneath) */
.power-item {
  padding: 10px 0 10px 10px;
  margin-bottom: 2px;
  border-left: 3px solid var(--line);
}
.power-item.devolved { border-left-color: var(--c-mayoral); background: rgba(40, 161, 151, 0.06); }
.power-item.partial  { border-left-color: var(--c-foundation); background: rgba(244, 106, 37, 0.05); }
.power-item.na       { border-left-color: #e2e4e7; }

.power-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 5px;
}
.power-name {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--ink);
  text-decoration: underline;
  text-decoration-color: #c7ccd1;
  text-underline-offset: 3px;
}
.ptag {
  flex-shrink: 0;
  font-size: 9px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 2px 6px;
  border-radius: 3px;
  white-space: nowrap;
}
.ptag.devolved     { background: #d9efe9; color: #14675b; }
.ptag.partial      { background: #ffe6d3; color: #99490f; }
.ptag.not-devolved { background: #eceef0; color: #59626a; }
.ptag.na           { background: #f0f2f4; color: #8a9196; }
.power-empty { font-size: 12px; color: #8a9196; font-style: italic; }

.section-empty { font-size: 12.5px; color: #8a9196; font-style: italic; }

.panel-footer { padding: 14px 0 4px; display: flex; flex-direction: column; gap: 6px; }
.review-badge {
  align-self: flex-start;
  font-size: 10.5px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 3px;
  background: #e8f0e9;
  color: #285c33;
}
.review-badge.web { background: #fff3d6; color: #7a5900; }
.sources { font-size: 11px; color: var(--ink-soft); }
.sources a { color: var(--c-mayoral); text-decoration: none; }
.sources a:hover { text-decoration: underline; }

/* ── Leaflet overrides ────────────────────────────────────────────────── */
.leaflet-container { background: #f4f6f8; font: inherit; }

/* No focus box around a clicked area — emphasis is handled by the casing/halo. */
.leaflet-interactive:focus { outline: none; }
path.leaflet-interactive:focus { outline: none; }

.leaflet-tooltip.msa-tooltip {
  background: var(--ink);
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 12.5px;
  font-weight: 600;
  box-shadow: 0 2px 8px rgba(11, 12, 12, 0.3);
}
.leaflet-tooltip.msa-tooltip::before { display: none; }
.leaflet-tooltip.msa-tooltip .tt-sub {
  display: block;
  font-weight: 400;
  font-size: 11px;
  opacity: 0.75;
  margin-top: 1px;
}

.leaflet-control-zoom { border: none !important; box-shadow: 0 1px 6px rgba(11,12,12,0.18) !important; }
.leaflet-control-zoom a {
  background: #fff !important;
  color: var(--ink) !important;
  border-color: var(--line) !important;
}
.leaflet-control-zoom a:hover { background: #f3f5f7 !important; }
.leaflet-control-attribution {
  background: rgba(255, 255, 255, 0.8) !important;
  font-size: 10px !important;
}

/* ── Responsive ───────────────────────────────────────────────────────── */
@media (max-width: 760px) {
  #header { padding: 0 14px; }
  #header-sub { display: none; }             /* keep the title short */
  #header-title { min-width: 0; }            /* allow the title to shrink… */
  #header-main { font-size: 17px; overflow: hidden; text-overflow: ellipsis; } /* …and truncate */
  #menu-btn { display: inline-flex; flex-shrink: 0; }   /* reveal the toggle, never shrink it */

  /* Controls collapse into a dropdown behind the menu button */
  #header-menu {
    display: none;
    position: absolute;
    top: calc(100% + 6px);
    left: 10px;
    right: 10px;
    flex-direction: column;
    align-items: stretch;
    gap: 16px;
    background: #fff;
    border: 1px solid var(--line);
    border-radius: 10px;
    box-shadow: 0 12px 32px rgba(11, 12, 12, 0.20);
    padding: 16px;
    z-index: 1200;
  }
  #header-menu.open { display: flex; }
  #highlight-tools { flex-wrap: wrap; }
  #header-controls { flex-wrap: wrap; gap: 10px; }
  #area-select { flex: 1 1 100%; min-width: 0; }
  #fullscreen-btn { flex: 1; justify-content: center; }

  /* Info panel becomes a scrollable bottom sheet, shown only when an area is
     selected (JS toggles .open) so the map is unobstructed at rest. */
  #panel {
    display: none;
    top: auto; left: 0; right: 0; bottom: 0;
    width: 100%;
    height: 53vh;
    height: 53dvh;
    border-radius: 14px 14px 0 0;
  }
  /* Set child divs in #panel-header (except '#panel-name') to organise in two columns instead of two rows */
  #panel-header {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: center;
    column-gap: 10px;
    row-gap: 8px;
    padding: 14px 16px 13px;
  }
  #panel-name {
    grid-column: 1 / -1;
    margin-bottom: 0;
    font-size: 22px;
    line-height: 1.2;
  }
  #panel-badges {
    grid-column: 1;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    align-items: center;
    gap: 6px;
    min-width: 0;
  }
  /*
   * Only style visible badges as flex elements.
   * Using :not(.hidden) avoids overriding the hidden state.
   */
  #panel-badges .badge:not(.hidden),
  #panel-website {
    display: inline-flex;
    align-items: center;
    min-height: 26px;
    box-sizing: border-box;
    padding: 4px 10px;
    font-size: 12px;
    line-height: 1.2;
    border-radius: 4px;
  }

  /* Defensive rule because the ID selector otherwise has high specificity */
  #panel-badges .badge.hidden {
    display: none;
  }

  #panel-website {
    grid-column: 2;
    align-self: center;
    margin-top: 0;
    white-space: nowrap;
  }

  #panel.open { display: flex; }
  #legend { display: none; }                 /* reclaim space; tiers are in the panel */
}
