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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

#map {
  width: 100vw;
  height: 100vh;
}

/* ── Title overlay ── */
#title-overlay {
  position: absolute;
  top: 16px;
  left: 16px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  padding: 14px 20px;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
  z-index: 10;
  pointer-events: none;
  transition: background 0.3s, color 0.3s, box-shadow 0.3s;
}

#title-overlay h1 {
  font-size: 20px;
  font-weight: 700;
  color: #0D47A1;
  letter-spacing: -0.3px;
  margin-bottom: 2px;
  transition: color 0.3s;
}

#title-overlay p {
  font-size: 12px;
  color: #5c7a9c;
  font-weight: 400;
  transition: color 0.3s;
}

/* ── Continent quick-jump bar ── */
#continent-bar {
  position: absolute;
  top: 90px;
  left: 16px;
  z-index: 10;
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  max-width: 320px;
}

#continent-bar button {
  background: rgba(255, 255, 255, 0.88);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(13, 71, 161, 0.15);
  border-radius: 6px;
  padding: 4px 10px;
  font-size: 11px;
  font-weight: 600;
  color: #1565C0;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}

#continent-bar button:hover {
  background: #e3f2fd;
  border-color: #1565C0;
  transform: translateY(-1px);
}

#continent-bar button:active {
  transform: translateY(0);
}

/* ── Search panel ── */
#search-panel {
  position: absolute;
  top: 16px;
  right: 56px;
  z-index: 20;
  width: 300px;
}

#search-row {
  display: flex;
  gap: 6px;
  align-items: stretch;
}

#search-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(8px);
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
  padding: 0 12px;
  height: 38px;
  gap: 8px;
  transition: box-shadow 0.15s, background 0.3s;
}

#search-wrap:focus-within {
  box-shadow: 0 2px 16px rgba(13, 71, 161, 0.25);
}

#search-icon {
  color: #78909c;
  flex-shrink: 0;
  transition: color 0.3s;
}

#search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  color: #1a1a1a;
  font-family: inherit;
  transition: color 0.3s;
}

#search-input::placeholder {
  color: #b0bec5;
}

#search-hint {
  font-family: 'SF Mono', 'Consolas', monospace;
  font-size: 11px;
  color: #90a4ae;
  background: #eceff1;
  border-radius: 3px;
  padding: 1px 5px;
  border: 1px solid #cfd8dc;
  flex-shrink: 0;
  transition: background 0.3s, color 0.3s, border-color 0.3s;
}

#search-hint.hidden {
  display: none;
}

/* ── Random river button ── */
#random-btn {
  width: 38px;
  height: 38px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  backdrop-filter: blur(8px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #1565C0;
  transition: all 0.2s;
  flex-shrink: 0;
}

#random-btn:hover {
  background: #e3f2fd;
  transform: rotate(15deg);
}

#random-btn:active {
  transform: rotate(30deg) scale(0.95);
}

#search-results {
  list-style: none;
  background: rgba(255, 255, 255, 0.96);
  backdrop-filter: blur(8px);
  border-radius: 0 0 8px 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
  max-height: 280px;
  overflow-y: auto;
  display: none;
  margin-top: 2px;
  transition: background 0.3s;
}

#search-results.open {
  display: block;
}

#search-results li {
  padding: 10px 14px;
  cursor: pointer;
  font-size: 13px;
  color: #37474f;
  border-bottom: 1px solid #f0f0f0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  transition: color 0.3s;
}

#search-results li:last-child {
  border-bottom: none;
}

#search-results li:hover,
#search-results li.active {
  background: #e3f2fd;
}

#search-results li .river-name {
  font-weight: 600;
  color: #0D47A1;
}

#search-results li .river-class {
  font-size: 11px;
  color: #90a4ae;
  white-space: nowrap;
}

#search-results .no-results {
  padding: 12px 14px;
  color: #90a4ae;
  font-size: 13px;
  font-style: italic;
  cursor: default;
}

#search-results .no-results:hover {
  background: transparent;
}

/* ── Dark mode toggle ── */
#dark-toggle {
  position: absolute;
  top: 110px;
  right: 10px;
  z-index: 20;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(8px);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #5c7a9c;
  transition: all 0.3s;
}

#dark-toggle:hover {
  background: #e3f2fd;
  color: #0D47A1;
}

/* ── Loading bar ── */
#loading-bar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: rgba(21, 101, 192, 0.15);
  z-index: 30;
  overflow: hidden;
}

#loading-bar.hidden {
  display: none;
}

#loading-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #1565C0, #42A5F5, #1565C0);
  background-size: 200% 100%;
  animation: loading-shimmer 1.5s ease-in-out infinite;
  transition: width 0.3s;
}

@keyframes loading-shimmer {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

#loading-text {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 11px;
  color: #1565C0;
  background: rgba(255, 255, 255, 0.85);
  padding: 2px 10px;
  border-radius: 4px;
  white-space: nowrap;
}

/* ── Legend ── */
#legend {
  position: absolute;
  bottom: 32px;
  left: 16px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(8px);
  padding: 12px 16px;
  border-radius: 10px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.12);
  z-index: 10;
  transition: background 0.3s, box-shadow 0.3s;
}

#legend-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

#legend h3 {
  font-size: 11px;
  font-weight: 600;
  color: #5c7a9c;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  transition: color 0.3s;
}

#legend-toggle {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 10px;
  color: #90a4ae;
  padding: 2px;
  line-height: 1;
  transition: transform 0.2s;
  display: none;
}

#legend-body {
  margin-top: 8px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0;
  margin-bottom: 5px;
  font-size: 12px;
  color: #37474f;
  transition: color 0.3s;
}

.legend-item:last-child {
  margin-bottom: 0;
}

.legend-filter {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
}

.legend-filter input[type="checkbox"] {
  width: 14px;
  height: 14px;
  accent-color: #1565C0;
  cursor: pointer;
  margin: 0;
}

.legend-line {
  display: inline-block;
  height: 3px;
  border-radius: 2px;
  background: #1565C0;
  transition: background 0.3s;
}

.legend-line.major { width: 32px; height: 5px; }
.legend-line.large { width: 26px; height: 3.5px; }
.legend-line.medium { width: 20px; height: 2px; }
.legend-line.small { width: 16px; height: 1px; }

#river-count {
  font-size: 11px;
  color: #90a4ae;
  margin-top: 8px;
  padding-top: 6px;
  border-top: 1px solid #e0e0e0;
  transition: color 0.3s, border-color 0.3s;
}

/* MapLibre controls */
.maplibregl-ctrl-bottom-left {
  bottom: 0;
  left: 0;
}

/* ── Popup styling ── */
.maplibregl-popup {
  animation: popup-fade 0.15s ease-out;
}

@keyframes popup-fade {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.maplibregl-popup-content {
  padding: 0;
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.18);
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  overflow: hidden;
  min-width: 200px;
  max-width: 280px;
}

.maplibregl-popup-close-button {
  font-size: 18px;
  color: #fff;
  right: 8px;
  top: 6px;
  opacity: 0.7;
}

.maplibregl-popup-close-button:hover {
  opacity: 1;
  background: none;
}

.river-popup-header {
  background: linear-gradient(135deg, #0D47A1, #1565C0);
  padding: 12px 16px;
  color: #fff;
}

.river-popup-header h3 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.2px;
}

.river-popup-header .river-class-badge {
  display: inline-block;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  background: rgba(255, 255, 255, 0.2);
  padding: 2px 8px;
  border-radius: 4px;
  margin-top: 4px;
}

.river-popup-body {
  padding: 10px 16px 14px;
}

.river-popup-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  padding: 4px 0;
  font-size: 12px;
}

.river-popup-label {
  color: #78909c;
  font-weight: 500;
}

.river-popup-value {
  color: #37474f;
  font-weight: 600;
  text-align: right;
}

.river-popup-names {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid #eceff1;
}

.river-popup-names .name-row {
  display: flex;
  gap: 8px;
  align-items: baseline;
  padding: 2px 0;
  font-size: 11px;
}

.river-popup-names .name-lang {
  color: #b0bec5;
  font-size: 10px;
  text-transform: uppercase;
  min-width: 22px;
}

.river-popup-names .name-text {
  color: #546e7a;
}

.river-popup-link {
  display: block;
  text-align: center;
  padding: 8px;
  background: #f5f7fa;
  font-size: 11px;
  color: #1565C0;
  text-decoration: none;
  font-weight: 600;
  letter-spacing: 0.3px;
  transition: background 0.15s;
}

.river-popup-link:hover {
  background: #e3f2fd;
}

/* ══════════════════════════════════════════
   DARK MODE
   ══════════════════════════════════════════ */
body.dark-mode #title-overlay {
  background: rgba(10, 22, 40, 0.92);
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.4);
}

body.dark-mode #title-overlay h1 {
  color: #64B5F6;
}

body.dark-mode #title-overlay p {
  color: #78909c;
}

body.dark-mode #continent-bar button {
  background: rgba(10, 22, 40, 0.85);
  border-color: rgba(100, 181, 246, 0.2);
  color: #64B5F6;
}

body.dark-mode #continent-bar button:hover {
  background: rgba(21, 101, 192, 0.3);
  border-color: #64B5F6;
}

body.dark-mode #search-wrap {
  background: rgba(10, 22, 40, 0.9);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

body.dark-mode #search-wrap:focus-within {
  box-shadow: 0 2px 20px rgba(100, 181, 246, 0.3);
}

body.dark-mode #search-icon { color: #546e7a; }
body.dark-mode #search-input { color: #e0e0e0; }
body.dark-mode #search-input::placeholder { color: #546e7a; }

body.dark-mode #search-hint {
  background: rgba(30, 50, 80, 0.6);
  color: #546e7a;
  border-color: #2a3f5f;
}

body.dark-mode #random-btn {
  background: rgba(10, 22, 40, 0.9);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
  color: #64B5F6;
}

body.dark-mode #random-btn:hover {
  background: rgba(21, 101, 192, 0.3);
}

body.dark-mode #dark-toggle {
  background: rgba(10, 22, 40, 0.9);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  color: #FFC107;
}

body.dark-mode #dark-toggle:hover {
  background: rgba(255, 193, 7, 0.15);
}

body.dark-mode #search-results {
  background: rgba(10, 22, 40, 0.96);
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
}

body.dark-mode #search-results li {
  color: #b0bec5;
  border-bottom-color: rgba(255, 255, 255, 0.06);
}

body.dark-mode #search-results li:hover,
body.dark-mode #search-results li.active {
  background: rgba(21, 101, 192, 0.2);
}

body.dark-mode #search-results li .river-name {
  color: #64B5F6;
}

body.dark-mode #legend {
  background: rgba(10, 22, 40, 0.92);
  box-shadow: 0 2px 16px rgba(0, 0, 0, 0.4);
}

body.dark-mode #legend h3 {
  color: #64B5F6;
}

body.dark-mode .legend-item {
  color: #b0bec5;
}

body.dark-mode .legend-line {
  background: #42A5F5;
}

body.dark-mode #river-count {
  color: #546e7a;
  border-top-color: rgba(255, 255, 255, 0.08);
}

body.dark-mode .legend-filter input[type="checkbox"] {
  accent-color: #42A5F5;
}

body.dark-mode .maplibregl-popup-content {
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.5);
}

body.dark-mode .river-popup-header {
  background: linear-gradient(135deg, #0a1628, #1a3a5c);
}

body.dark-mode .river-popup-body {
  background: #0d1b2a;
}

body.dark-mode .river-popup-label { color: #546e7a; }
body.dark-mode .river-popup-value { color: #b0bec5; }
body.dark-mode .river-popup-names { border-top-color: rgba(255,255,255,0.08); }
body.dark-mode .river-popup-names .name-text { color: #78909c; }

body.dark-mode .river-popup-link {
  background: #0a1628;
  color: #42A5F5;
}

body.dark-mode .river-popup-link:hover {
  background: rgba(21, 101, 192, 0.2);
}

body.dark-mode .maplibregl-popup-close-button { color: #90a4ae; }

body.dark-mode #loading-text {
  color: #42A5F5;
  background: rgba(10, 22, 40, 0.9);
}

/* MapLibre attribution in dark mode */
body.dark-mode .maplibregl-ctrl-attrib {
  background: rgba(10, 22, 40, 0.7) !important;
  color: #546e7a !important;
}

body.dark-mode .maplibregl-ctrl-attrib a {
  color: #42A5F5 !important;
}

body.dark-mode .maplibregl-ctrl-group {
  background: rgba(10, 22, 40, 0.9) !important;
  box-shadow: 0 2px 8px rgba(0,0,0,0.4) !important;
}

body.dark-mode .maplibregl-ctrl-group button {
  background: transparent !important;
}

body.dark-mode .maplibregl-ctrl-group button span {
  filter: invert(0.7) !important;
}

/* ── Responsive ── */
@media (max-width: 640px) {
  #title-overlay {
    top: 10px;
    left: 10px;
    padding: 10px 14px;
  }

  #title-overlay h1 {
    font-size: 16px;
  }

  #continent-bar {
    top: 70px;
    left: 10px;
    max-width: calc(100vw - 60px);
  }

  #continent-bar button {
    padding: 3px 7px;
    font-size: 10px;
  }

  #search-panel {
    top: auto;
    bottom: 130px;
    right: 10px;
    left: 10px;
    width: auto;
  }

  #dark-toggle {
    top: 10px;
    right: 56px;
  }

  #legend {
    bottom: 24px;
    left: 10px;
    padding: 10px 12px;
  }

  #legend-toggle {
    display: block;
  }

  #legend-body.collapsed {
    display: none;
  }

  #legend-toggle.collapsed {
    transform: rotate(-90deg);
  }
}

@media (max-width: 480px) {
  #title-overlay h1 {
    font-size: 14px;
  }

  #title-overlay p {
    display: none;
  }

  #continent-bar {
    top: 52px;
  }
}
