:root {
  --ink: #1a1a2e;
  --muted: #6b7280;
  --line: #e5e7eb;
  --accent: #6a3d9a;
  --bg: #f7f7fb;
}
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
}
header {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.9rem 1.2rem; background: #fff; border-bottom: 1px solid var(--line);
}
header h1 { margin: 0; font-size: 1.35rem; letter-spacing: -0.01em; }
.subtitle { margin: 0.15rem 0 0; color: var(--muted); font-size: 0.9rem; }
.badge {
  flex: none; background: var(--accent); color: #fff; padding: 0.4rem 0.7rem;
  border-radius: 999px; font-size: 0.8rem; white-space: nowrap;
}
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }

#map { position: relative; width: 100%; height: 68vh; min-height: 420px; }

.panel {
  position: absolute; z-index: 2; top: 12px; right: 12px;
  background: rgba(255,255,255,0.96); border: 1px solid var(--line);
  border-radius: 10px; padding: 0.7rem 0.8rem; max-width: 280px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.08); font-size: 0.82rem;
}
.legend-title { font-weight: 600; margin-bottom: 0.45rem; line-height: 1.15; }
.panel ul { list-style: none; margin: 0; padding: 0; }
.panel li { display: flex; align-items: center; gap: 0.45rem; margin: 0.2rem 0; }
.sw { width: 14px; height: 14px; border-radius: 3px; flex: none; border: 1px solid rgba(0,0,0,0.15); }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; border: 1px solid rgba(0,0,0,0.3); }
.dot.ring { background: #000; box-shadow: 0 0 0 2px #facc15; }
.toggle { display: flex; align-items: center; gap: 0.4rem; margin-top: 0.55rem;
  padding-top: 0.55rem; border-top: 1px solid var(--line); cursor: pointer; }
.vac-key { margin-top: 0.4rem; }

/* Collapsible legend panel */
.panel { max-height: calc(68vh - 28px); overflow-y: auto; }
.legend-title { padding-right: 1.3rem; }
.panel-close { position: absolute; top: 4px; right: 6px; width: 26px; height: 26px; border: 0;
  background: transparent; color: var(--muted); font-size: 1.25rem; line-height: 1; cursor: pointer; border-radius: 6px; }
.panel-close:hover { background: #f0f0f5; color: var(--ink); }
.legend-open { position: absolute; z-index: 2; top: 12px; right: 12px;
  background: rgba(255,255,255,0.96); border: 1px solid var(--line); border-radius: 999px;
  padding: 0.5rem 0.85rem; font: inherit; font-size: 0.82rem; font-weight: 500; cursor: pointer;
  box-shadow: 0 2px 10px rgba(0,0,0,0.08); }
.legend-open:hover { background: #fff; }
.info { cursor: help; }

/* "Back to Hungary" button — appears when the country scrolls out of view */
.recenter { position: absolute; z-index: 3; left: 50%; transform: translateX(-50%); bottom: 16px;
  background: var(--accent); color: #fff; border: 0; border-radius: 999px;
  padding: 0.55rem 1.05rem; font: inherit; font-weight: 600; cursor: pointer;
  box-shadow: 0 3px 14px rgba(0,0,0,0.28); }
.recenter:hover { filter: brightness(1.07); }
.recenter[hidden] { display: none; }

/* Tooltips */
.tooltip {
  position: fixed; z-index: 9999; max-width: 280px;
  background: #1a1a2e; color: #fff; padding: 0.5rem 0.65rem; border-radius: 8px;
  font-size: 0.78rem; line-height: 1.35; box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  pointer-events: none;
}
.tooltip::after {
  content: ""; position: absolute; left: 50%; transform: translateX(-50%);
  bottom: -5px; border: 5px solid transparent; border-top-color: #1a1a2e; border-bottom: 0;
}
.tooltip.below::after { bottom: auto; top: -5px; border-top: 0; border-bottom-color: #1a1a2e; }
[data-tip] { cursor: help; }
.info { color: var(--accent); font-size: 0.85em; vertical-align: 0.05em; }
.toggle .info, .legend-title .info { opacity: 0.8; }

/* No-WebGL Leaflet fallback. Leaflet's panes/controls sit at z-index 400-1000, so the legend
   and recenter button (z-index 2/3) must be lifted above them; the container gets the same
   no-data background as the MapLibre 'bg' layer. */
.fallback-2d .leaflet-container { background: #eaeaf2; }
.fallback-2d .panel, .fallback-2d .legend-open { z-index: 1001; }
.fallback-2d .recenter { z-index: 1002; }
.leaflet-popup-content { margin: 0.7rem 0.8rem; }
.leaflet-popup-content .popup h3 { margin-top: 0; }

.maplibregl-popup-content, .leaflet-popup-content { font-size: 0.85rem; }
.maplibregl-popup-content { padding: 0.7rem 0.8rem; border-radius: 8px; }
.popup .k[data-tip] { border-bottom: 1px dotted #b9b9c6; }
.popup .tag[data-tip] { cursor: help; }
.popup h3 { margin: 0 0 0.3rem; font-size: 0.95rem; }
.popup table { border-collapse: collapse; }
.popup td { padding: 1px 0; vertical-align: top; }
.popup td.k { color: var(--muted); padding-right: 0.6rem; white-space: nowrap; }
.popup .tag { display: inline-block; padding: 1px 7px; border-radius: 999px; color: #fff; font-size: 0.75rem; }

#worst { max-width: 1100px; margin: 1.4rem auto; padding: 0 1.2rem; }
#worst h2 { font-size: 1.1rem; margin-bottom: 0.2rem; }

.table-controls { display: flex; align-items: center; gap: 0.5rem; margin: 0.6rem 0 0.7rem; font-size: 0.85rem; }
.table-controls .ctl-label { color: var(--muted); }
.dropdown { position: relative; }
.dropdown-btn { display: inline-flex; align-items: center; justify-content: space-between; gap: 0.5rem;
  min-width: 190px; padding: 0.4rem 0.6rem; border: 1px solid var(--line); border-radius: 8px;
  background: #fff; font: inherit; color: var(--ink); cursor: pointer; }
.dropdown-btn:hover { background: #f7f7fb; }
.dropdown-btn .caret { color: var(--muted); font-size: 0.8em; }
.dropdown-menu { position: absolute; z-index: 50; top: calc(100% + 4px); left: 0; min-width: 240px;
  max-height: 60vh; overflow-y: auto; margin: 0; padding: 0.25rem; list-style: none; background: #fff;
  border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,0.16); }
.dropdown-menu[hidden] { display: none; }
.dropdown-menu li { padding: 0.5rem 0.6rem; border-radius: 7px; cursor: pointer; white-space: nowrap; }
.dropdown-menu li:hover { background: #f0f0f5; }
.dropdown-menu li[aria-selected="true"] { background: rgba(106, 61, 154, 0.1); font-weight: 600; }
.dropdown-menu li[aria-selected="true"]::before { content: "✓ "; color: var(--accent); }
.sort-dir { width: 2rem; height: 2rem; flex: none; border: 1px solid var(--line); border-radius: 8px; background: #fff; cursor: pointer; }
.sort-dir:hover { background: #f0f0f5; }

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: #fff; }
table#worst100 { border-collapse: collapse; width: 100%; font-size: 0.85rem; }
#worst100 th, #worst100 td { padding: 0.45rem 0.55rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; }
#worst100 th { position: sticky; top: 0; background: #fafafa; cursor: pointer; user-select: none;
  white-space: normal; line-height: 1.2; vertical-align: bottom; font-size: 0.8rem; }
#worst100 th:hover { background: #f0f0f5; }
#worst100 th[aria-sort="ascending"]::after { content: " ▲"; color: var(--muted); }
#worst100 th[aria-sort="descending"]::after { content: " ▼"; color: var(--muted); }
#worst100 td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
#worst100 tbody tr:hover { background: #fafaff; cursor: pointer; }
#worst100 .desert td:first-child { box-shadow: inset 3px 0 0 var(--accent); }

/* Mobile: turn each row into a card (no horizontal scroll) — label: value pairs. */
@media (max-width: 760px) {
  .table-wrap { overflow: visible; border: 0; background: transparent; }
  #worst100 thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  #worst100, #worst100 tbody, #worst100 tr, #worst100 td { display: block; width: 100%; }
  #worst100 tr { border: 1px solid var(--line); border-radius: 10px; background: #fff;
    margin-bottom: 0.6rem; padding: 0.3rem 0.7rem; }
  #worst100 tr:hover { background: #fff; }
  #worst100 td { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem;
    border: 0; border-bottom: 1px solid #f0f0f5; padding: 0.4rem 0; text-align: right; white-space: normal; }
  #worst100 td:last-child { border-bottom: 0; }
  #worst100 td::before { content: attr(data-label); color: var(--muted); text-align: left;
    font-weight: 500; flex: 0 0 auto; max-width: 60%; }
  #worst100 td.num { text-align: right; }
  #worst100 .desert { box-shadow: inset 4px 0 0 var(--accent); }
  #worst100 .desert td:first-child { box-shadow: none; }
}

/* Mobile: collapse the worst-100 list to the first 5 cards with a "show all / show less" toggle.
   The .cards-collapsed class lives on #worst100 (which render() preserves), so the visible 5 always
   track the current sort. Desktop ignores the class (rule is inside the media query). The desktop/
   mobile intro hints (.hint-*) swap here too. :not([hidden]) lets the button's hidden attribute win. */
.cards-expander { display: none; }
.cards-expander:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.hint-mobile { display: none; }

@media (max-width: 760px) {
  #worst100.cards-collapsed tbody tr:nth-child(n+6) { display: none; }
  .cards-expander:not([hidden]) {
    display: block; width: 100%; margin: 0.2rem 0 0.8rem;
    padding: 0.6rem 0.8rem; border: 1px solid var(--line); border-radius: 10px;
    background: #fff; font: inherit; font-size: 0.85rem; font-weight: 500;
    color: var(--accent); cursor: pointer; text-align: center;
  }
  .cards-expander:hover { background: #f7f7fb; }
  .hint-desktop { display: none; }
  .hint-mobile { display: inline; }
}

footer { max-width: 1100px; margin: 1.5rem auto 3rem; padding: 0 1.2rem; color: var(--muted); font-size: 0.82rem; }
footer a { color: var(--accent); }

/* Touch: bigger tap targets + readable tooltips */
@media (max-width: 760px) {
  .info { display: inline-block; min-width: 1.6rem; min-height: 1.6rem; line-height: 1.6rem;
    text-align: center; border-radius: 50%; background: rgba(106,61,154,0.1); }
  .tooltip { max-width: 80vw; font-size: 0.82rem; }
  .legend-title, .toggle, .panel li { padding-top: 0.35rem; padding-bottom: 0.35rem; }
}

@media (max-width: 640px) {
  header { flex-direction: column; align-items: flex-start; gap: 0.4rem; }
  #map { height: 62vh; }
  .panel { max-width: min(80vw, 260px); font-size: 0.8rem; max-height: calc(62vh - 28px); }
}
