/* Each colour has a light and a dark value. The page follows the system until
   the script sets data-theme from the chosen map: light map, light page. */
:root {
  --bg: light-dark(#e9edf1, #0a0e13);
  --panel: light-dark(rgba(255, 255, 255, 0.95), rgba(13, 18, 25, 0.94));
  --pop: light-dark(#ffffff, #121922);
  --well: light-dark(#f2f4f7, #0f151c);
  --raised: light-dark(rgba(15, 23, 32, 0.05), rgba(255, 255, 255, 0.05));
  --pressed: light-dark(rgba(15, 23, 32, 0.11), rgba(255, 255, 255, 0.12));
  --track: light-dark(rgba(15, 23, 32, 0.2), rgba(255, 255, 255, 0.14));
  --line: light-dark(rgba(15, 23, 32, 0.13), rgba(255, 255, 255, 0.09));
  --shadow: light-dark(rgba(20, 30, 45, 0.2), rgba(0, 0, 0, 0.5));
  --text: light-dark(#17202b, #e7edf3);
  --muted: light-dark(#596675, #8a97a6);
  --cams: light-dark(#a35f00, #ffb224);
  --live: light-dark(#cf2f2f, #ff5d5d);
  --flights: light-dark(#0a739a, #4cc9f0);
  --ships: light-dark(#0f7a50, #5ee0a0);
  --media: #05080b;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --panel-width: 304px;
  color-scheme: light dark;
}

:root[data-theme="light"] { color-scheme: light; }
:root[data-theme="dark"] { color-scheme: dark; }

* { box-sizing: border-box; }

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

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }

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

/* ---- Panel ---------------------------------------------------------------- */

.panel {
  position: absolute;
  top: 12px;
  left: 12px;
  bottom: 12px;
  width: var(--panel-width);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  z-index: 5;
  transition: transform 0.22s ease;
}

body.panel-closed .panel { transform: translateX(calc(-100% - 24px)); }

.panel-open {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 4;
  width: 38px;
  height: 38px;
  background: var(--panel);
  border: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 10px 14px;
  border-bottom: 1px solid var(--line);
}

.brand-home {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  padding: 0;
  background: none;
  border: 0;
  text-align: left;
}

.brand-home strong {
  display: block;
  font-size: 15px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.brand-home small { color: var(--muted); font-size: 11px; letter-spacing: 0.04em; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 8px;
  color: var(--muted);
}

.icon-btn:hover { background: var(--raised); color: var(--text); }
.icon-btn:disabled { opacity: 0.35; background: none; color: var(--muted); cursor: default; }

.panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px 14px;
  scrollbar-width: thin;
  scrollbar-color: var(--track) transparent;
}

.panel section { margin-top: 16px; }

.panel h2 {
  margin: 0 0 8px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--muted);
}

/* ---- Search --------------------------------------------------------------- */

.search { position: relative; }

.search input {
  width: 100%;
  height: 36px;
  padding: 0 12px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 9px;
  outline: none;
}

.search input:focus { border-color: rgba(255, 178, 36, 0.6); }
.search input::placeholder { color: var(--muted); }

.results {
  position: absolute;
  left: 0;
  right: 0;
  top: 40px;
  margin: 0;
  padding: 4px;
  list-style: none;
  max-height: 320px;
  overflow-y: auto;
  background: var(--pop);
  border: 1px solid var(--line);
  border-radius: 9px;
  box-shadow: 0 12px 30px var(--shadow);
  z-index: 2;
}

.results button {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 7px 8px;
  background: none;
  border: 0;
  border-radius: 6px;
  text-align: left;
}

.results button:hover, .results button:focus-visible { background: var(--raised); outline: none; }
.results .dot { flex: none; width: 8px; height: 8px; border-radius: 50%; }
.results .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.results .kind { color: var(--muted); font-size: 11px; }
.results .empty { padding: 8px; color: var(--muted); }

/* ---- Layers --------------------------------------------------------------- */

.layer {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  margin-bottom: 6px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
}

.layer input { position: absolute; opacity: 0; pointer-events: none; }
.layer .swatch { flex: none; width: 10px; height: 10px; border-radius: 50%; }
.layer[data-layer="cams"] .swatch { background: var(--cams); }
.layer[data-layer="flights"] .swatch { background: var(--flights); }
.layer[data-layer="ships"] .swatch { background: var(--ships); }
.layer-text { flex: 1; min-width: 0; }
.layer-text b { display: block; font-weight: 600; }

.layer-text small {
  display: block;
  color: var(--muted);
  font-size: 11.5px;
  overflow-wrap: anywhere;
}

.switch {
  flex: none;
  position: relative;
  width: 34px;
  height: 20px;
  background: var(--track);
  border-radius: 10px;
  transition: background 0.15s;
}

.switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}

.layer input:checked + .switch { background: #2f9e6b; }
.layer input:checked + .switch::after { transform: translateX(14px); }
.layer input:focus-visible + .switch { outline: 2px solid var(--cams); outline-offset: 2px; }
.layer:has(input:not(:checked)) .swatch { opacity: 0.35; }

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 10px;
  margin: -1px 2px 9px;
  color: var(--muted);
  font-size: 11px;
}

.legend span { display: inline-flex; align-items: center; gap: 4px; }
.legend i { width: 8px; height: 8px; border-radius: 2px; }
/* What the colours stand for, on a line of its own. */
.legend b { flex-basis: 100%; font-weight: 500; opacity: 0.75; }

.segmented {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 2px;
  padding: 2px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 9px;
}

.segmented button {
  height: 30px;
  background: none;
  border: 0;
  border-radius: 7px;
  color: var(--muted);
}

.segmented button[aria-pressed="true"] { background: var(--pressed); color: var(--text); }

/* A heading with a control on the same line. */
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.section-head h2 { margin: 0; }
.head-tools { display: flex; align-items: center; gap: 6px; }

.segmented.icons { grid-auto-columns: 36px; }
.segmented.icons button { display: grid; place-items: center; height: 26px; padding: 0; }
.segmented.icons button:hover { color: var(--text); }

.check { display: flex; align-items: center; gap: 8px; margin-top: 10px; cursor: pointer; }
.check input { width: 15px; height: 15px; accent-color: var(--cams); }

.sources, .sources ul { margin: 0; padding: 0; list-style: none; }
.sources ul { margin: 0 0 6px 10px; padding-left: 6px; border-left: 1px solid var(--line); }

.sources summary {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 7px;
  cursor: pointer;
  list-style: none;
}

.sources summary::-webkit-details-marker { display: none; }
.sources summary::before { content: "▸"; color: var(--muted); font-size: 10px; }
.sources details[open] > summary::before { content: "▾"; }
.sources summary:hover { background: var(--raised); }
.sources summary .name { font-weight: 600; }

.sources button {
  display: flex;
  align-items: baseline;
  gap: 8px;
  width: 100%;
  padding: 6px 8px;
  background: none;
  border: 0;
  border-radius: 7px;
  text-align: left;
}

.sources button:hover { background: var(--raised); }
.sources .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sources .count { color: var(--muted); font-family: var(--mono); font-size: 11.5px; }
.sources .bad .count { color: var(--live); }

.fine { margin: 18px 0 0; color: var(--muted); font-size: 11px; }

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

.detail {
  position: absolute;
  top: 12px;
  right: 12px;
  width: min(360px, calc(100% - 24px));
  max-height: calc(100% - 24px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  overflow: hidden;
  z-index: 6;
}

.detail-head {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 12px 10px 10px 14px;
}

.detail-head .titles { flex: 1; min-width: 0; }
.detail-nav { display: flex; gap: 2px; }
.detail-head h3 { margin: 2px 0 0; font-size: 16px; line-height: 1.25; overflow-wrap: anywhere; }
.detail-head p { margin: 2px 0 0; color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }

.tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 1px 7px;
  border-radius: 20px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  background: var(--raised);
  color: var(--muted);
}

.tag.cams { color: var(--cams); background: color-mix(in srgb, var(--cams) 13%, transparent); }
.tag.flights { color: var(--flights); background: color-mix(in srgb, var(--flights) 13%, transparent); }
.tag.ships { color: var(--ships); background: color-mix(in srgb, var(--ships) 13%, transparent); }
.tag.live { color: #fff; background: #d83a3a; }
.tag.live::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: #fff; animation: blink 1.4s infinite; }
.tag.alert { color: #fff; background: #d83a3a; }

@keyframes blink { 50% { opacity: 0.25; } }

.detail-body { overflow-y: auto; padding: 0 14px 14px; scrollbar-width: thin; }

.media {
  position: relative;
  aspect-ratio: 16 / 9;
  background: var(--media);
  border: 1px solid var(--line);
  border-radius: 9px;
  overflow: hidden;
  color-scheme: dark; /* a picture area is dark in both themes */
}

.media img, .media video { display: block; width: 100%; height: 100%; object-fit: contain; background: var(--media); }

.media .cam-status {
  position: absolute;
  left: 6px;
  bottom: 6px;
  padding: 1px 7px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.62);
  color: #dfe6ee;
  font: 10.5px var(--mono);
  pointer-events: none;
}

.media .cam-status:empty { display: none; }

.cam-fail, .cam-wait {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px;
  color: var(--muted);
  text-align: center;
  font-size: 12px;
}

.views { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

.btn {
  height: 30px;
  padding: 0 11px;
  background: var(--raised);
  border: 1px solid var(--line);
  border-radius: 8px;
  text-decoration: none;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
}

.btn:hover { background: var(--pressed); }
.btn[aria-pressed="true"] { border-color: var(--cams); color: var(--cams); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.small { height: 26px; padding: 0 9px; font-size: 12px; }

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin: 10px 0 0;
  background: var(--well);
  border: 1px solid var(--line);
  border-radius: 9px;
  overflow: hidden;
}

/* Rows can be hidden, so each cell draws its own hairline. */
.stats > div { padding: 8px 10px; min-width: 0; box-shadow: 0 0 0 0.5px var(--line); }
.stats dt { color: var(--muted); font-size: 10.5px; letter-spacing: 0.06em; text-transform: uppercase; }
.stats dd { margin: 2px 0 0; font: 13px var(--mono); overflow-wrap: anywhere; }
.stats dd small { color: var(--muted); font-size: 11px; }
.stats .wide { grid-column: 1 / -1; }

.photo {
  position: relative;
  display: block;
  aspect-ratio: 16 / 9;
  margin-bottom: 10px;
  background: var(--media);
  border: 1px solid var(--line);
  border-radius: 9px;
  overflow: hidden;
}

.photo img { display: block; width: 100%; height: 100%; object-fit: cover; }

.photo span {
  position: absolute;
  right: 6px;
  bottom: 6px;
  max-width: calc(100% - 12px);
  padding: 1px 7px;
  border-radius: 20px;
  background: rgba(0, 0, 0, 0.62);
  color: #dfe6ee;
  font-size: 10.5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.route {
  padding: 10px 12px;
  background: var(--well);
  border: 1px solid var(--line);
  border-radius: 9px;
}

.route-ends { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 12px; }
.route-ends b { display: block; font: 600 19px var(--mono); letter-spacing: 0.04em; }
.route-ends span { display: block; max-width: 110px; color: var(--muted); font-size: 11.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.route-ends .right { text-align: right; }
.route-bar { height: 3px; background: var(--track); border-radius: 2px; overflow: hidden; }
.route-bar i { display: block; height: 100%; background: var(--flights); border-radius: 2px; }
.route-note { margin: 8px 0 0; color: var(--muted); font-size: 11.5px; }

.actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.credit { margin: 10px 0 0; color: var(--muted); font-size: 11px; }

/* ---- Monitor -------------------------------------------------------------- */

.fab {
  position: absolute;
  left: calc(var(--panel-width) + 24px);
  top: 12px;
  height: 38px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  z-index: 4;
  transition: left 0.22s ease;
}

.fab:hover { border-color: var(--cams); color: var(--cams); }
body.panel-closed .fab { left: 60px; }

/* The monitor is dark in both themes, like the pictures it shows. */
.wall {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: rgba(6, 9, 13, 0.97);
  color: var(--text);
  color-scheme: dark;
  z-index: 20;
}

.wall header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px 8px;
  padding: 8px 10px 8px 14px;
  border-bottom: 1px solid var(--line);
}

.wall header strong { letter-spacing: 0.12em; text-transform: uppercase; }

/* The script sets the columns and rows of the chosen layout. */
.wall-grid {
  flex: 1;
  display: grid;
  grid-template-columns: repeat(var(--cols, 3), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows, 3), minmax(0, 1fr));
  gap: 8px;
  padding: 10px;
  min-height: 0;
}

.wall-grid > .tile:first-child { grid-column: span var(--big, 1); grid-row: span var(--big, 1); }

.tile {
  position: relative;
  min-height: 0;
  padding: 0;
  background: var(--media);
  border: 1px solid var(--line);
  border-radius: 9px;
  overflow: hidden;
  text-align: left;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.tile:hover { border-color: var(--cams); }
.tile img { -webkit-user-drag: none; }
.tile.drop { outline: 2px solid var(--cams); outline-offset: -2px; }

.tile.dragging {
  z-index: 2;
  border-color: var(--cams);
  box-shadow: 0 16px 44px rgba(0, 0, 0, 0.65);
  opacity: 0.9;
  cursor: grabbing;
}
.tile .media { position: absolute; inset: 0; aspect-ratio: auto; border: 0; border-radius: 0; }
.tile .media img, .tile .media video { object-fit: cover; }

.tile .caption {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  padding: 6px 9px 14px;
  background: linear-gradient(rgba(0, 0, 0, 0.72), transparent);
  color: #fff;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  pointer-events: none;
}

.wall-empty { grid-column: 1 / -1; grid-row: 1 / -1; display: grid; place-items: center; color: var(--muted); text-align: center; }

/* ---- Status and toast ------------------------------------------------------ */

.statusbar {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  padding: 3px 11px;
  background: light-dark(rgba(255, 255, 255, 0.85), rgba(10, 14, 19, 0.78));
  border: 1px solid var(--line);
  border-radius: 20px;
  color: var(--muted);
  font: 11px var(--mono);
  white-space: nowrap;
  pointer-events: none;
  z-index: 3;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 44px;
  transform: translateX(-50%);
  max-width: min(90vw, 520px);
  padding: 9px 14px;
  background: light-dark(#ffffff, #1a222c);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 30px var(--shadow);
  z-index: 30;
}

/* ---- MapLibre overrides ---------------------------------------------------- */

.maplibregl-popup.tip .maplibregl-popup-content {
  padding: 4px 9px;
  background: var(--pop);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  font: 12px/1.3 system-ui, sans-serif;
  box-shadow: 0 6px 18px var(--shadow);
  pointer-events: none;
}

.maplibregl-popup.tip .maplibregl-popup-tip { display: none; }

.compass {
  display: block;
  width: 46px;
  height: 46px;
  padding: 0;
  background: none;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}

.compass svg { display: block; }
.compass .rim { fill: var(--pop); stroke: var(--line); }
.compass:hover .rim { stroke: var(--muted); }
.compass .needle { fill: #e5483d; }
.compass .south { fill: var(--muted); }
.compass .arm { fill: var(--track); }
.compass text { fill: var(--muted); font: 700 7.5px system-ui, sans-serif; text-anchor: middle; dominant-baseline: central; }
.compass text.north { fill: var(--live); }
.maplibregl-ctrl-group { background: var(--pop); border: 1px solid var(--line); }
.maplibregl-ctrl-group button + button { border-top-color: var(--line); }
.maplibregl-ctrl-scale { background: light-dark(rgba(255, 255, 255, 0.7), rgba(10, 14, 19, 0.6)); color: var(--muted); border-color: var(--muted); }
.maplibregl-ctrl-attrib { background: light-dark(rgba(255, 255, 255, 0.8), rgba(10, 14, 19, 0.7)) !important; color: var(--muted); }
.maplibregl-ctrl-attrib-inner { color: var(--muted); }
.maplibregl-ctrl-attrib a { color: light-dark(#3d4a59, #b9c4d0); }
/* The icons of the map library are dark drawings. */
:root:not([data-theme="light"]) .maplibregl-ctrl-group button .maplibregl-ctrl-icon { filter: invert(1) brightness(1.6); }
:root:not([data-theme="light"]) .maplibregl-ctrl-attrib-button { filter: invert(1); }
.maplibregl-ctrl-bottom-left { left: calc(var(--panel-width) + 16px); transition: left 0.22s ease; }
body.panel-closed .maplibregl-ctrl-bottom-left { left: 0; }

/* ---- Small screens --------------------------------------------------------- */

/* Tablets: the card leaves room for the map. */
@media (max-width: 1023px) {
  .detail { width: min(340px, calc(100% - 24px)); }
  .statusbar { display: none; } /* it would run into the map credits */
}

/* Phones: the panel is a drawer and the card is a sheet at the bottom. */
@media (max-width: 760px) {
  :root { --panel-width: min(86vw, 320px); }
  .panel {
    top: 0;
    left: 0;
    bottom: 0;
    padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom) env(safe-area-inset-left);
    border-radius: 0 var(--radius) var(--radius) 0;
  }
  .panel-open { top: calc(12px + env(safe-area-inset-top)); left: calc(12px + env(safe-area-inset-left)); }
  .fab { top: calc(12px + env(safe-area-inset-top)); left: calc(60px + env(safe-area-inset-left)); }
  body.panel-closed .fab { left: calc(60px + env(safe-area-inset-left)); }
  .maplibregl-ctrl-bottom-left { left: 0; }
  .detail {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: auto;
    max-height: 58%;
    padding-bottom: env(safe-area-inset-bottom);
    border-radius: var(--radius) var(--radius) 0 0;
  }
  /* Monitor: the title and the page arrows on one line, the layouts on the next. */
  .wall header strong { order: 1; }
  .wall header .spacer { order: 2; }
  #wall-prev { order: 3; }
  #wall-next { order: 4; }
  #wall-close { order: 5; }
  #wall-layout { order: 6; }
  #wall-reset { order: 7; }
  #wall-info { order: 8; flex: 1; min-width: 0; overflow: hidden; text-align: right; text-overflow: ellipsis; white-space: nowrap; }
  .wall-grid { gap: 5px; padding: 6px; }
}

/* Fingers need larger targets than a mouse pointer. */
@media (pointer: coarse) {
  .icon-btn { width: 38px; height: 38px; }
  .segmented.icons { grid-auto-columns: 40px; }
  .segmented.icons button { height: 34px; }
  .btn { height: 36px; }
  .btn.small { height: 32px; }
  .search input { height: 42px; font-size: 16px; } /* 16px stops the zoom on focus in iOS */
  .sources summary, .sources button { padding-top: 9px; padding-bottom: 9px; }
}

@media (prefers-reduced-motion: reduce) {
  .panel, .fab, .switch, .switch::after { transition: none; }
  .tag.live::before { animation: none; }
}
