* {
  box-sizing: border-box;
}

:root {
  --bg: #0f1419;
  --card: rgba(20, 26, 33, 0.94);
  --line: rgba(255, 255, 255, 0.08);
  --hover: rgba(255, 255, 255, 0.06);
  --field: #1c242d;
  --popup: #141a21;
  --scrollbar: rgba(255, 255, 255, 0.2);
  --storm: #ffd60a;
  --text: #e6e9ed;
  --muted: #9aa5b1;
  /* Highlight colour (selected tab, switches, focus ring) and the text colour that sits on it. */
  --accent: #ffffff;
  --on-accent: #10151a;
  --gap: 16px;
  /* Makes the browser's own scrollbars, checkboxes and sliders match the theme. */
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #dfe3e8;
  --card: rgba(255, 255, 255, 0.95);
  --line: rgba(15, 23, 42, 0.12);
  --text: #17202a;
  --muted: #5b6673;
  --hover: rgba(15, 23, 42, 0.06);
  --field: #f1f3f5;
  --popup: #ffffff;
  --scrollbar: rgba(15, 23, 42, 0.25);
  --storm: #e08a00;
  /* White would vanish on the light panels, so the highlight turns dark here. */
  --accent: #17202a;
  --on-accent: #ffffff;
  color-scheme: light;
}

html,
body {
  height: 100%;
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}

button,
select,
input {
  font: inherit;
  color: inherit;
  touch-action: manipulation;
}

/* Keyboard focus: one clearly visible ring everywhere. */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#map {
  position: absolute;
  inset: 0;
  background: var(--bg);
}

/* Cards */

/* Above the map's own controls (zoom, attribution), which sit at 1000. */
.card {
  position: absolute;
  z-index: 1100;
  display: flex;
  flex-direction: column;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(6px);
}

.card-head {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 12px 16px;
  border: 0;
  background: none;
  font-size: 15px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-align: left;
  cursor: pointer;
}

.grabber {
  width: 40px;
  height: 4px;
  border-radius: 2px;
  background: var(--muted);
}

/* On wide screens the ranking card is always open and has no header.
   Phones keep the handle as the open/close button of the bottom sheet. */
@media (min-width: 641px) {
  #ranking .card-head {
    display: none;
  }

  .card.collapsed .card-body {
    display: flex;
  }
}

.card-body {
    display: flex;
  }
}

.card-head:hover .chevron {
  border-color: var(--text);
}

.card.collapsed .chevron {
  transform: translateY(2px) rotate(-135deg);
}

.card-body {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0 16px 14px;
}

.card.collapsed .card-body {
  display: none;
}

#ranking {
  top: var(--gap);
  left: var(--gap);
  width: 400px;
  max-height: calc(100% - 2 * var(--gap));
}

/* Airport details: their own panel on the right, so the list on the left stays visible.
   It stops short of the bottom to leave the map's zoom buttons free. */

#detail {
  top: var(--gap);
  right: calc(var(--gap) + env(safe-area-inset-right));
  width: 400px;
  max-height: calc(100% - 2 * var(--gap) - 84px);
  padding: 16px 0 14px;
}

#detail-view {
  min-height: 0;
  padding: 0 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
}

/* Layer bar: one quiet row of switches, centred over the free part of the map. */

#layerbar {
  position: absolute;
  top: calc(var(--gap) + env(safe-area-inset-top));
  left: calc(400px + 2 * var(--gap));
  right: var(--gap);
  z-index: 1050;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 2px 4px;
  width: fit-content;
  margin: 0 auto;
  padding: 4px 10px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--card) 72%, transparent);
  border: 1px solid var(--line);
  backdrop-filter: blur(6px);
  font-size: 12.5px;
  color: var(--muted);
}

/* With the details open, the bar centres between the two panels. */
.detail-open #layerbar {
  right: calc(400px + 2 * var(--gap));
}

#layers {
  display: contents;
}

.layer,
.bar-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 999px;
  white-space: nowrap;
  user-select: none;
}

.layer {
  cursor: pointer;
}

.layer:hover,
.layer:has(input:checked) {
  color: var(--text);
}

.layer:hover {
  background: var(--hover);
}

.layer.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Round switches, drawn here instead of by the browser: a ring, with a filled circle inside when on. */
.layer input {
  appearance: none;
  display: grid;
  place-content: center;
  flex: none;
  width: 14px;
  height: 14px;
  margin: 0;
  border: 1.5px solid var(--muted);
  border-radius: 50%;
  background: none;
  cursor: inherit;
}

.layer input:checked {
  border-color: var(--accent);
}

.layer input:checked::after {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
}

.bar-opacity {
  margin-left: 4px;
  padding-left: 12px;
  border-left: 1px solid var(--line);
  border-radius: 0;
}

.bar-opacity input {
  width: 72px;
  accent-color: var(--accent);
}

/* Ranking and routes */

[hidden] {
  display: none !important;
}

.logo {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
  height: 44px;
  margin: 14px 16px 12px;
  border: 1px dashed var(--muted);
  border-radius: 8px;
  font-size: 11px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

#main-view,
.tab-panel {
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  flex: none;
  gap: 4px;
  margin-bottom: 12px;
  padding: 4px;
  border-radius: 8px;
  background: var(--hover);
}

.tab {
  padding: 6px 10px;
  border: 0;
  border-radius: 6px;
  background: none;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}

.tab:hover {
  color: var(--text);
}

.tab[aria-selected="true"] {
  background: var(--accent);
  color: var(--on-accent);
}

.scroll-list,
#detail-view {
  scrollbar-width: thin;
  scrollbar-color: var(--scrollbar) transparent;
}

.searching .tabs,
.searching .tab-panel {
  display: none;
}

#airport-search {
  flex: none;
  width: 100%;
  margin-bottom: 12px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--field);
  font-size: 14px;
  color: var(--text);
}

/* No built-in clear button; Esc or deleting the text clears the search. */
#airport-search::-webkit-search-cancel-button {
  display: none;
}

#airport-search::placeholder {
  color: var(--muted);
}

.filters {
  display: grid;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
}

.filters label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.filters select {
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--field);
  color: var(--text);
}

.filters input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
}

.pills {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.pill {
  padding: 3px 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--hover);
  font-size: 12px;
  color: var(--muted);
  cursor: pointer;
}

.pill:hover {
  color: var(--text);
}

.pill[aria-pressed="true"] {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--on-accent);
}

.filters output {
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.scroll-list {
  flex: 1;
  min-height: 0;
  margin: 0 -16px;
  padding: 4px 0;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.scroll-list .empty {
  padding: 16px;
  font-size: 13px;
  color: var(--muted);
}

.airport {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 10px;
  width: 100%;
  padding: 9px 16px;
  border: 0;
  background: none;
  text-align: left;
  cursor: pointer;
}

.airport:hover,
.airport:focus-visible {
  background: var(--hover);
}

/* Rows fill the list edge to edge, so their focus ring sits inside. */
.airport:focus-visible,
.route:focus-visible {
  outline-offset: -2px;
}

/* Airport rows: the code on its own line, name and city underneath. */
.airport {
  align-items: center;
}

.airport .code,
.route .code {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.2;
}

.airport .where,
.route .where {
  grid-column: 1;
  min-width: 0;
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.airport .reasons,
.route .reasons,
#detail-view .reasons {
  grid-column: 1;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 3px;
  font-size: 12px;
  line-height: 1.4;
  color: var(--muted);
}

/* --chip is the reason's severity colour; the bubble is a soft tint of it. */
.chip {
  padding: 1px 8px;
  border: 1px solid color-mix(in srgb, var(--chip, var(--muted)) 55%, transparent);
  border-radius: 999px;
  background: color-mix(in srgb, var(--chip, var(--muted)) 18%, transparent);
  font-size: 11.5px;
  color: var(--text);
  white-space: nowrap;
}

.route {
  display: grid;
  gap: 8px;
  width: 100%;
  padding: 11px 16px;
  border: 0;
  border-left: 3px solid transparent;
  background: none;
  text-align: left;
  cursor: pointer;
}

.route:hover,
.route:focus-visible {
  background: var(--hover);
}

.route.active {
  border-left-color: var(--accent);
  background: var(--hover);
}

.route-end {
  display: grid;
  grid-template-columns: 34px 1fr auto;
  gap: 2px 8px;
  align-items: baseline;
}

.route-label {
  font-size: 11px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}

.route .where,
.route .reasons {
  grid-column: 2;
}

.route-distance {
  padding-left: 42px;
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.score {
  min-width: 34px;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
  color: #10151a;
  font-variant-numeric: tabular-nums;
}

/* Time slider: centred along the bottom of the free map area, beside the ranking card. */

#timebar {
  position: absolute;
  left: calc(50% + 208px);
  bottom: var(--gap);
  transform: translateX(-50%);
  z-index: 1100;
  display: flex;
  align-items: center;
  gap: 14px;
  width: min(440px, calc(100% - 416px - 2 * var(--gap) - 120px));
  min-width: 300px;
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--card);
  border: 1px solid var(--line);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(6px);
}

#time-now {
  flex: none;
  padding: 6px 12px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  font-size: 13px;
  font-weight: 600;
  color: var(--on-accent);
  cursor: pointer;
}

#time-now:hover:not(:disabled),
#banner:hover {
  filter: brightness(1.12);
}

#time-now:disabled {
  background: var(--hover);
  color: var(--muted);
  cursor: default;
}

.time-main {
  flex: 1;
  min-width: 0;
}

.time-label {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
}

.time-label strong {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

#time-note {
  font-size: 11px;
  text-align: right;
  color: var(--muted);
}

#time-offset {
  display: block;
  width: 100%;
  margin: 6px 0 2px;
  accent-color: var(--accent);
}

.time-scale {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--muted);
}

/* Banner */

/* Makes room for the time slider while it is shown. */
body:has(#timebar:not([hidden])) #banner {
  bottom: 110px;
}

#banner {
  position: absolute;
  left: 50%;
  bottom: 28px;
  transform: translateX(-50%);
  z-index: 1200;
  width: max-content;
  max-width: min(520px, calc(100% - 2 * var(--gap)));
  padding: 12px 16px;
  border: 0;
  border-radius: 8px;
  background: #b3382c;
  color: #fff;
  font-size: 14px;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
}

#banner code {
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.25);
}

/* Data credits: a small "©" that unfolds to the full line. */

.leaflet-container .leaflet-control-attribution {
  max-width: 22px;
  height: 22px;
  margin: 0 var(--gap) var(--gap) 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: color-mix(in srgb, var(--card) 72%, transparent);
  backdrop-filter: blur(6px);
  font-size: 11px;
  line-height: 20px;
  white-space: nowrap;
  color: var(--muted);
  cursor: pointer;
  transition: max-width 0.2s;
}

.leaflet-container .leaflet-control-attribution::before {
  content: "©";
  display: inline-block;
  width: 20px;
  text-align: center;
}

.leaflet-container .leaflet-control-attribution:hover,
.leaflet-container .leaflet-control-attribution:focus-within,
.leaflet-container .leaflet-control-attribution:focus-visible,
.leaflet-container .leaflet-control-attribution.open {
  max-width: calc(100vw - 2 * var(--gap));
  height: auto;
  padding-right: 10px;
  border-radius: 11px;
  white-space: normal;
  color: var(--text);
}

.leaflet-container .leaflet-control-attribution a {
  color: inherit;
  text-decoration: underline;
}

/* Zoom buttons keep the same distance from the window edge as the panels. */
.leaflet-container .leaflet-control-zoom {
  margin: 0 var(--gap) var(--gap) 0;
}

/* With the credits shown underneath, they only need a small gap to them. */
.leaflet-bottom:has(.leaflet-control-attribution:not([hidden])) .leaflet-control-zoom {
  margin-bottom: 8px;
}

@media (prefers-reduced-motion: reduce) {
  .leaflet-container .leaflet-control-attribution {
    transition: none;
  }
}

/* Map markers and popups */

/* Thunderstorm: a yellow dot that flickers like lightning and sends out a fading ring. */

.storm-flash span {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
}

.storm-flash span::before,
.storm-flash span::after {
  content: "";
  position: absolute;
  border-radius: 50%;
  animation: 2.6s infinite var(--delay, 0s);
}

.storm-flash span::before {
  inset: 9px;
  background: var(--storm);
  box-shadow: 0 0 10px 3px var(--storm);
  animation-name: storm-flicker;
}

.storm-flash span::after {
  inset: 0;
  border: 2px solid var(--storm);
  animation-name: storm-ring;
  animation-timing-function: ease-out;
}

@keyframes storm-flicker {
  0%, 9%, 100% { opacity: 0.45; }
  3%, 12% { opacity: 1; }
  6% { opacity: 0.3; }
  30% { opacity: 0.6; }
}

@keyframes storm-ring {
  0% { transform: scale(0.3); opacity: 0.9; }
  45%, 100% { transform: scale(1.5); opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .storm-flash span::before,
  .storm-flash span::after {
    animation: none;
  }

  .storm-flash span::after {
    opacity: 0.5;
  }
}

.leaflet-popup-content-wrapper,
.leaflet-popup-tip {
  background: var(--popup);
  color: var(--text);
}

.leaflet-popup-content {
  margin: 12px 14px;
  font-size: 13px;
  line-height: 1.5;
}

.leaflet-popup-content .facts {
  margin: 8px 0;
}

/* Airport detail */

.detail-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

#detail-view h2 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: 0.04em;
  line-height: 1.1;
}

.detail-head .note {
  margin-top: 4px;
  text-wrap: balance;
}

#detail-view h3 {
  margin: 16px 0 6px;
  padding-top: 12px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted);
}

.note {
  margin: 2px 0 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.note code {
  color: var(--text);
}

/* All reasons are shown here, slightly larger than in the lists. */
#detail-view .reasons {
  gap: 6px;
  margin-top: 8px;
}

#detail-view .chip {
  padding: 3px 10px;
  font-size: 12.5px;
  color: var(--text);
}

.facts {
  display: grid;
  grid-template-columns: 92px 1fr;
  gap: 5px 10px;
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
}

.facts dt {
  color: var(--muted);
}

.facts dd {
  margin: 0;
}

.forecast-when {
  margin: 0 0 4px;
  font-size: 13px;
  color: var(--muted);
}

.forecast-when strong {
  color: var(--text);
}

.raw-label {
  margin: 8px 0 3px;
  font-size: 11px;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.raw {
  margin: 0;
  padding: 8px;
  border-radius: 6px;
  background: var(--hover);
  font-family: ui-monospace, Consolas, monospace;
  font-size: 11.5px;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

/* Phones: layers shrink to a button, the ranking becomes a bottom sheet. */

@media (max-width: 640px) {
  :root {
    --gap: 10px;
  }

  /* The layer bar becomes one row that scrolls sideways. */
  #layerbar {
    left: var(--gap);
    right: var(--gap);
    flex-wrap: nowrap;
    justify-content: flex-start;
    width: auto;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .detail-open #layerbar {
    right: var(--gap);
  }

  /* Airport details slide over the ranking sheet as a sheet of their own. */
  #detail {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1150;
    width: auto;
    max-height: 65dvh;
    border-radius: 14px 14px 0 0;
    border-width: 1px 0 0;
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }

  #ranking {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 65dvh;
    border-radius: 14px 14px 0 0;
    border-width: 1px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .airport {
    padding-top: 11px;
    padding-bottom: 11px;
  }

  #ranking.collapsed .logo {
    display: none;
  }

  .logo {
    margin-top: 2px;
  }

  .leaflet-control-zoom {
    display: none;
  }

  .leaflet-bottom {
    bottom: calc(30px + env(safe-area-inset-bottom));
  }

  body:has(#timebar:not([hidden])) .leaflet-bottom {
    bottom: calc(138px + env(safe-area-inset-bottom));
  }

  /* Sits just above the collapsed ranking sheet; the open sheet covers it. */
  #timebar {
    left: var(--gap);
    right: var(--gap);
    bottom: calc(56px + env(safe-area-inset-bottom));
    transform: none;
    z-index: 1050;
    width: auto;
    min-width: 0;
  }

  #banner {
    top: 64px;
    bottom: auto;
  }
}
