:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1a1d21;
  --muted: #5c6470;
  --accent: #0a7d34;
  --accent-ink: #ffffff;
  --border: #dce0e6;
  --focus: #1d6fe0;
  --shadow: 0 1px 4px rgba(0, 0, 0, .12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14171a;
    --panel: #1e2227;
    --text: #e8eaed;
    --muted: #9aa4b0;
    --accent: #2fae5c;
    --accent-ink: #06210f;
    --border: #2e343c;
    --focus: #5aa0ff;
    --shadow: 0 1px 4px rgba(0, 0, 0, .5);
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
  line-height: 1.45;
}

a { color: var(--focus); }

/* Keyboard focus visibility everywhere */
:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--accent);
  color: var(--accent-ink);
  padding: 10px 14px;
  z-index: 2000;
  border-radius: 0 0 6px 0;
}
.skip-link:focus { left: 0; }

.app-header {
  padding: 14px 18px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.app-header h1 { margin: 0; font-size: 1.25rem; }
.tagline { margin: 4px 0 0; color: var(--muted); font-size: .9rem; }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 280px 1fr 320px;
  grid-template-areas: "controls map list";
  min-height: 0;
}

.controls   { grid-area: controls; }
.map-wrap   { grid-area: map; position: relative; }
.list-panel { grid-area: list; }

.controls, .list-panel {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
}
.list-panel { border-right: none; border-left: 1px solid var(--border); }

.control-group { margin-bottom: 18px; }
.control-group > label,
legend { font-weight: 600; font-size: .9rem; display: block; margin-bottom: 6px; padding: 0; }

fieldset.control-group { border: 0; margin: 0 0 18px; padding: 0; }

.radio { display: block; font-weight: 400; margin: 6px 0; cursor: pointer; font-size: .9rem; }
.radio input { margin-right: 6px; }
.hint { color: var(--muted); font-size: .8rem; }

select, input[type="search"] {
  width: 100%;
  padding: 8px 10px;
  font-size: .95rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
}

.status {
  font-size: .88rem;
  color: var(--muted);
  border-left: 3px solid var(--accent);
  padding: 6px 10px;
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  border-radius: 0 6px 6px 0;
}
.status.error { border-left-color: #c0392b; color: #c0392b; }

.meta { margin-top: 20px; font-size: .85rem; color: var(--muted); }
.meta summary { cursor: pointer; font-weight: 600; color: var(--text); }
.meta p { margin: 8px 0; }

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

.list-panel h2 { margin: 0 0 10px; font-size: 1rem; }
.results-list { list-style: none; margin: 0; padding: 0; }
.results-list li {
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
  font-size: .85rem;
  background: var(--bg);
}
.results-list li button {
  all: unset;
  cursor: pointer;
  display: block;
  width: 100%;
}
.results-list li button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.results-list .r-title { font-weight: 600; color: var(--accent); }
.results-list .r-sub { color: var(--muted); }
.list-note { color: var(--muted); font-size: .8rem; margin: 4px 0 10px; }

.app-footer {
  padding: 10px 18px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  font-size: .8rem;
  color: var(--muted);
}
.app-footer a { color: var(--focus); }

/* Popup content — shared by Leaflet popups and Google InfoWindows (.map-popup) */
.map-popup { font-size: .88rem; line-height: 1.5; color: var(--text); }
.map-popup .p-title { font-weight: 700; color: var(--accent); display: block; margin-bottom: 4px; }
.map-popup dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 2px 8px; }
.map-popup dt { color: var(--muted); }
.map-popup dd { margin: 0; }
.map-popup .p-id { color: var(--muted); font-size: .78rem; margin-top: 6px; display: block; }

/* Inline link-styled button (e.g. discarded count) */
.linklike {
  background: none;
  border: none;
  padding: 0;
  font: inherit;
  color: var(--focus);
  text-decoration: underline;
  cursor: pointer;
}
.linklike:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* Discarded-records dialog */
.discard-dialog {
  max-width: 420px;
  width: calc(100vw - 32px);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px 18px;
  background: var(--panel);
  color: var(--text);
  box-shadow: 0 8px 30px rgba(0, 0, 0, .3);
}
.discard-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.discard-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 0; }
.discard-head h2 { margin: 0; font-size: 1.05rem; }
.discard-dialog p { font-size: .88rem; margin: 10px 0; }
.discard-dialog code { background: var(--bg); padding: 1px 4px; border-radius: 4px; font-size: .85em; }
.discard-list { list-style: none; margin: 6px 0 0; padding: 0; }
.discard-list li {
  font-size: .84rem;
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 6px;
  background: var(--bg);
}
.discard-list .d-id { color: var(--muted); display: block; font-size: .78rem; }

/* BiciMAD layer controls */
.check { display: block; font-weight: 400; margin: 6px 0; cursor: pointer; font-size: .9rem; }
.check input { margin-right: 6px; }
.check.sub { margin-left: 20px; }
.check input:disabled + * , .check:has(input:disabled) { color: var(--muted); cursor: default; }
#bicimad-live { margin: 8px 0 0; }
#bicimad-refresh {
  cursor: pointer;
  font-size: .82rem;
  padding: 5px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
#bicimad-refresh:hover { border-color: var(--focus); }
#bicimad-refresh:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
#bicimad-updated { margin-left: 8px; }
.bm-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; margin-top: 8px; font-size: .78rem; color: var(--muted); }
.bm-legend span { display: inline-flex; align-items: center; gap: 4px; }
.bm-legend i { width: 11px; height: 11px; border-radius: 50%; border: 1px solid #fff; box-shadow: 0 0 0 1px var(--border); }
#bicimad-msg:empty { display: none; }
#bicimad-msg.error { color: #c0392b; }

/* Google Maps key box */
.key-box {
  margin-top: 10px;
  padding: 10px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  background: var(--bg);
}
.key-box[hidden] { display: none; }
.key-box label { display: block; font-size: .82rem; font-weight: 600; margin-bottom: 4px; }
.key-box input { margin-bottom: 8px; }
.key-actions { display: flex; align-items: center; gap: 10px; }
.key-actions button {
  cursor: pointer;
  font-size: .85rem;
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 600;
}
.key-actions button:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.key-actions .link-btn {
  background: none;
  border: none;
  color: var(--focus);
  padding: 4px;
  font-weight: 400;
  text-decoration: underline;
}
.key-actions .link-btn[hidden] { display: none; }
.key-box .hint { margin: 8px 0 0; }

/* List panel header + collapse controls */
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.icon-btn {
  all: unset;
  cursor: pointer;
  line-height: 1;
  font-size: 1.3rem;
  color: var(--muted);
  padding: 2px 8px;
  border-radius: 6px;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
.icon-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.reopen-btn {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 1000;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.reopen-btn:hover { border-color: var(--focus); }
.reopen-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.reopen-btn[hidden] { display: none; }

/* "Locate me" button — top-left, below the zoom control (both engines) */
.locate-btn {
  position: absolute;
  top: 84px;
  left: 12px;
  z-index: 1000;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  line-height: 1;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.locate-btn:hover { border-color: var(--focus); }
.locate-btn:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.locate-btn:disabled { opacity: .6; cursor: default; }
.locate-btn.loading { animation: locate-pulse 1s ease-in-out infinite; }
@keyframes locate-pulse { 50% { opacity: .45; } }

/* Collapsed state: drop the list column and give its space to the map */
.layout.list-collapsed {
  grid-template-columns: 280px 1fr;
  grid-template-areas: "controls map";
}
.layout.list-collapsed .list-panel { display: none; }

/* Responsive: stack and let the PAGE scroll on narrow screens.
   (Desktop keeps the fixed-height 3-column grid above.) Using block flow here
   avoids squeezing the controls into a tiny internal-scroll box. */
@media (max-width: 900px) {
  .layout { display: block; }

  /* Controls take their natural height — every option is visible. */
  .controls {
    overflow: visible;
    border: none;
    border-bottom: 1px solid var(--border);
  }

  /* Map gets a usable fixed slice of the viewport. */
  .map-wrap { height: 65vh; }

  /* Results flow below the map; long lists scroll within the panel, not the page. */
  .list-panel {
    border: none;
    border-top: 1px solid var(--border);
    max-height: 75vh;
    overflow-y: auto;
  }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
