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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #1a1a2e;
  color: #e0e0e0;
  overflow: hidden;
  height: 100vh;
}

#header {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 20px;
  background: rgba(26, 26, 46, 0.9);
  border-bottom: 1px solid #333;
}

#header h1 {
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  cursor: pointer;
  user-select: none;
}

#navbar-country {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 10px 2px 6px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid #444;
  border-radius: 4px;
}

#navbar-country.hidden {
  display: none;
}

#navbar-country img {
  width: 24px;
  height: 16px;
  object-fit: contain;
  border-radius: 2px;
  flex-shrink: 0;
}

#navbar-country span {
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
}

#direction-toggle.hidden {
  display: none;
}

#direction-toggle {
  padding: 4px 12px;
  font-size: 13px;
  font-weight: 600;
  color: #e0e0e0;
  background: #2a2a4a;
  border: 1px solid #555;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

#direction-toggle:hover {
  background: #3a3a5a;
  border-color: #777;
}

#direction-toggle.incoming {
  background: #3a2a4a;
  border-color: #886;
}

#map {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

#legend {
  position: absolute;
  bottom: 30px;
  right: 0;
  z-index: 1000;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  font-size: 13px;
}

#legend-toggle {
  writing-mode: vertical-rl;
  transform: rotate(180deg);
  background: rgba(26, 26, 46, 0.92);
  border: 1px solid #444;
  border-right: none;
  border-radius: 0 6px 6px 0;
  color: #aaa;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  cursor: pointer;
  padding: 6px;
  transition: color 0.15s, background 0.15s;
  flex-shrink: 0;
  align-self: stretch;
  display: flex;
  align-items: center;
  justify-content: center;
}

#legend-toggle:hover {
  background: #3a3a5a;
  color: #fff;
}

#legend-content {
  background: rgba(26, 26, 46, 0.92);
  border: 1px solid #444;
  border-right: none;
  padding: 12px 16px;
  overflow: hidden;
  transition: width 0.2s ease, padding 0.2s ease, opacity 0.2s ease, height 0.2s ease;
  width: 160px;
  opacity: 1;
}

#legend.collapsed #legend-content {
  width: 0;
  padding-left: 0;
  padding-right: 0;
  opacity: 0;
  pointer-events: none;
}

#legend.collapsed #legend-toggle {
  border-right: 1px solid #444;
  border-radius: 0 6px 6px 0;
}

#legend-content h3 {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
  color: #ccc;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  white-space: nowrap;
}

.swatch {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 3px;
}

#explore-prompt {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 500;
  color: #aaa;
  background: rgba(26, 26, 46, 0.85);
  border: 1px solid #444;
  border-radius: 6px;
  padding: 10px 18px;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: 0.5px;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#explore-prompt.hidden {
  opacity: 0;
}

/* Country Panel */
#country-panel {
  position: absolute;
  top: 48px;
  left: 0;
  bottom: 0;
  width: 300px;
  z-index: 1000;
  background: rgba(26, 26, 46, 0.95);
  border-right: 1px solid #444;
  display: flex;
  flex-direction: column;
  transition: transform 0.2s ease;
}

#country-panel.hidden {
  transform: translateX(-100%);
  pointer-events: none;
}

#country-panel.collapsed {
  transform: translateX(-100%);
  pointer-events: none;
}

#panel-toggle {
  position: absolute;
  right: -24px;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  height: 48px;
  background: rgba(26, 26, 46, 0.95);
  border: 1px solid #444;
  border-left: none;
  border-radius: 0 4px 4px 0;
  color: #aaa;
  font-size: 16px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  transition: color 0.15s, background 0.15s;
  z-index: 1001;
}

#panel-toggle:hover {
  background: #3a3a5a;
  color: #fff;
}

#country-panel.hidden #panel-toggle {
  display: none;
}

#mobile-panel-toggle {
  display: none;
}

#country-panel-close {
  position: absolute;
  top: 8px;
  right: 8px;
  background: none;
  border: none;
  color: #aaa;
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
  padding: 4px 8px;
}

#country-panel-close:hover {
  color: #fff;
}

.country-panel-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px 16px 12px;
}

#country-flag {
  width: 48px;
  height: 32px;
  object-fit: contain;
  border-radius: 2px;
  flex-shrink: 0;
}

#country-panel-name {
  font-size: 18px;
  font-weight: 600;
  color: #fff;
  margin: 0;
}

.country-panel-tabs {
  display: flex;
  border-bottom: 1px solid #444;
  padding: 0 8px;
}

.country-panel-tabs .tab {
  flex: 1;
  padding: 8px 4px;
  font-size: 12px;
  font-weight: 600;
  color: #888;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  text-align: center;
  transition: color 0.15s, border-color 0.15s;
}

.country-panel-tabs .tab:hover {
  color: #ccc;
}

.country-panel-tabs .tab.active {
  color: #fff;
  border-bottom-color: #4a90d9;
}

.country-panel-tabs .tab.active[data-level="1"] {
  border-bottom-color: #e07b9b;
}

.country-panel-tabs .tab.active[data-level="0"] {
  border-bottom-color: #999;
}

#country-list {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 8px 0;
}

#country-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 16px;
  font-size: 13px;
  color: #ddd;
  cursor: default;
}

#country-list li.clickable-country {
  cursor: pointer;
}

#country-list li:hover {
  background: rgba(255, 255, 255, 0.05);
}

#country-list li img {
  width: 24px;
  height: 16px;
  object-fit: contain;
  border-radius: 1px;
  flex-shrink: 0;
}

#country-list .empty-message {
  color: #666;
  font-style: italic;
  padding: 16px;
  text-align: center;
}

/* Country search */
#country-search-wrapper {
  position: relative;
  margin-left: auto;
}

#search-icon-btn {
  display: none;
}

#country-search {
  padding: 4px 10px;
  font-size: 13px;
  color: #e0e0e0;
  background: #2a2a4a;
  border: 1px solid #555;
  border-radius: 4px;
  outline: none;
  width: 180px;
  transition: border-color 0.15s, width 0.2s;
}

#country-search::placeholder {
  color: #777;
}

#country-search:focus {
  border-color: #4a90d9;
  width: 220px;
}

#country-search-results {
  display: none;
  position: absolute;
  top: calc(100% + 4px);
  right: 0;
  min-width: 220px;
  background: rgba(26, 26, 46, 0.98);
  border: 1px solid #444;
  border-radius: 4px;
  list-style: none;
  z-index: 2000;
  overflow: hidden;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
}

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

#country-search-results li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 12px;
  font-size: 13px;
  color: #ddd;
  cursor: pointer;
}

#country-search-results li:hover {
  background: rgba(74, 144, 217, 0.2);
  color: #fff;
}

#country-search-results li.no-match {
  color: #666;
  font-style: italic;
  cursor: default;
}

#country-search-results li img {
  width: 22px;
  height: 15px;
  object-fit: contain;
  border-radius: 1px;
  flex-shrink: 0;
}

/* Override Leaflet background for dark theme */
.leaflet-container {
  background: #1a1a2e;
}

/* Zoom controls */
.leaflet-top.leaflet-right {
  top: 52px;
}

.leaflet-control-zoom {
  border: 1px solid #555 !important;
  border-radius: 4px !important;
  overflow: hidden;
}

.leaflet-control-zoom a {
  background: rgba(26, 26, 46, 0.92) !important;
  color: #e0e0e0 !important;
  border-color: #555 !important;
  width: 30px !important;
  height: 30px !important;
  line-height: 30px !important;
  font-size: 16px !important;
}

.leaflet-control-zoom a:hover {
  background: #3a3a5a !important;
  color: #fff !important;
}

/* ========================
   Mobile drag handle
   ======================== */

#panel-drag-handle {
  display: none;
}

/* ========================
   Mobile / small screen
   ======================== */

@media (max-width: 640px) {
  /* Header: compact single row */
  #header {
    gap: 6px;
    padding: 8px 12px;
  }

  #app-title {
    font-size: 17px;
    white-space: nowrap;
  }

  /* Country pill: visible in header when panel is collapsed */
  #navbar-country {
    max-width: 110px;
    flex-shrink: 1;
  }

  #navbar-country span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  #direction-toggle {
    font-size: 12px;
    padding: 4px 8px;
    white-space: nowrap;
  }

  /* Search: collapsed to icon by default, expands on tap */
  #country-search-wrapper {
    flex: none;
    margin-left: auto;
    min-width: 0;
  }

  #country-search-wrapper.expanded {
    flex: 1;
    margin-left: 0;
  }

  #search-icon-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: #2a2a4a;
    border: 1px solid #555;
    border-radius: 4px;
    color: #e0e0e0;
    cursor: pointer;
    flex-shrink: 0;
  }

  #search-icon-btn:hover {
    background: #3a3a5a;
    border-color: #777;
  }

  /* font-size >= 16px prevents iOS from zooming on input focus */
  #country-search {
    display: none;
    width: 100%;
    font-size: 16px;
  }

  #country-search-wrapper.expanded #country-search {
    display: block;
  }

  #country-search-wrapper.expanded #search-icon-btn {
    display: none;
  }

  #country-search:focus {
    width: 100%;
  }

  /* Dropdown spans full wrapper width */
  #country-search-results {
    left: 0;
    right: 0;
    min-width: 0;
  }

  #country-search-results li {
    padding: 10px 12px;
    font-size: 14px;
  }

  /* Drag handle visible on mobile — full-width chevron tap target */
  #panel-drag-handle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 48px;
    flex-shrink: 0;
    cursor: pointer;
    touch-action: none;
  }

  #mobile-panel-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    background: none;
    border: none;
    color: #aaa;
    font-size: 28px;
    line-height: 1;
    padding: 0;
    cursor: pointer;
    touch-action: manipulation;
    pointer-events: auto;
  }

  #country-panel-close {
    display: none;
  }

  /* Country panel: bottom sheet instead of left sidebar */
  #country-panel {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: 62vh;
    border-right: none;
    border-top: 1px solid #555;
    border-radius: 14px 14px 0 0;
    transform: translateY(0);
    transition: transform 0.25s ease;
  }

  #country-panel.hidden {
    transform: translateY(100%);
    pointer-events: none;
  }

  /* Collapsed: peek at bottom with drag handle visible for re-expanding */
  #country-panel.collapsed {
    transform: translateY(calc(100% - 52px));
    pointer-events: none;
  }

  #country-panel.collapsed #panel-drag-handle {
    pointer-events: auto;
    cursor: pointer;
  }

  #country-panel.collapsed #mobile-panel-toggle {
    pointer-events: auto;
  }

  /* Side collapse tab not applicable on mobile */
  #panel-toggle {
    display: none !important;
  }

  .country-panel-header {
    touch-action: none;
    padding: 8px 16px;
  }

  #panel-toggle-slot {
    margin-left: auto;
    flex-shrink: 0;
  }

  #panel-toggle-slot #direction-toggle {
    font-size: 12px;
    padding: 4px 10px;
  }

  #country-flag {
    width: 36px;
    height: 24px;
  }

  #country-panel-name {
    font-size: 16px;
  }

  /* Larger touch targets in country list */
  #country-list li {
    padding: 10px 16px;
    font-size: 14px;
  }

  #country-list li img {
    width: 26px;
    height: 18px;
  }

  /* Legend: position below zoom controls on the right */
  #legend {
    bottom: auto;
    top: 140px;
  }

  /* Explore prompt: nudge down slightly to clear the taller mobile header */
  #explore-prompt {
    top: 54%;
  }
}
