* { box-sizing: border-box; }

:root {
  --bg: #040205;
  --panel: rgba(20, 14, 28, 0.92);
  --panel-soft: rgba(30, 22, 42, 0.75);
  --text: #f6f2fb;
  --muted: rgba(246, 242, 251, 0.72);
  --muted-2: rgba(246, 242, 251, 0.5);
  --stroke: rgba(255, 233, 142, 0.82);
  --stroke-soft: rgba(255, 233, 142, 0.45);
  --divider: rgba(255,255,255,0.08);
  --accent: #6f2dbd;
  --accent-2: #b388eb;
  --shadow: 0 22px 52px rgba(0, 0, 0, 0.35);
  --radius: 10px;
  --font: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
}

body {
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

button, select, input { font: inherit; }
button { cursor: pointer; }

#app {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background:
    radial-gradient(circle at 18% 16%, rgba(125, 52, 202, 0.22), transparent 26%),
    radial-gradient(circle at 80% 24%, rgba(158, 72, 186, 0.12), transparent 24%),
    linear-gradient(180deg, #050307 0%, #050307 100%);
}

#map-wrap {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1;
  pointer-events: auto;
}

#map {
  width: 100%;
  height: 100%;
  display: block;
  background: transparent;
  user-select: none;
  touch-action: none;
}

.country {
  cursor: pointer;
  transition: opacity 120ms ease, fill 120ms ease, stroke-width 120ms ease;
}

.country.no-data {
  fill: rgba(255,255,255,0.06);
}

.country.active {
  stroke-width: 1.6px;
}

.country.outside-selection {
  opacity: 0.2;
}

.boundary-outline {
  fill: none;
  stroke: rgba(255,233,142,0.2);
  stroke-width: 0.6px;
  pointer-events: none;
}

.glass-card {
  position: absolute;
  z-index: 30;
  border-radius: var(--radius);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

#title-card {
  top: 18px;
  left: 18px;
  padding: 14px 16px 12px;
  min-width: 210px;
  max-width: min(420px, calc(100vw - 36px));
  background: linear-gradient(135deg, rgba(255,255,255,0.70), rgba(255,255,255,0.15));
  color: #1f0937;
  box-shadow: var(--shadow);
  pointer-events: none;
}

#title-card h1 {
  margin: 0;
  font-size: clamp(1.05rem, 1.8vw, 1.45rem);
  line-height: 1.08;
  letter-spacing: -0.02em;
}

#title-card p {
  margin: 4px 0 0;
  font-size: 0.78rem;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  opacity: 0.82;
}

.panel {
  position: absolute;
  inset: 18px 18px 18px auto;
  width: min(370px, calc(100vw - 36px));
  display: flex;
  z-index: 35;
  transition: transform 320ms cubic-bezier(.2,.8,.2,1);
}

.panel.collapsed {
  transform: translateX(calc(100% - 14px));
}

.panel-handle {
  width: 14px;
  border: 0;
  border-radius: 10px 0 0 10px;
  background: rgba(173, 173, 173, 0.72);
  padding: 0;
  margin: 24px 0 24px 0;
  box-shadow: inset 0 0 0 1px rgba(255,255,255,0.08);
}

.panel-handle:hover { background: rgba(190, 190, 190, 0.9); }

.panel-inner {
  flex: 1;
  min-height: 0;
  background: var(--panel);
  border-radius: 0 var(--radius) var(--radius) 0;
  padding: 20px 18px 18px;
  box-shadow: var(--shadow);
  overflow: auto;
  scrollbar-width: thin;
}

.panel-inner::-webkit-scrollbar,
.modal-body::-webkit-scrollbar { width: 10px; }
.panel-inner::-webkit-scrollbar-thumb,
.modal-body::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
  border-radius: 999px;
}

.panel-section { display: block; }
.panel-spacer { flex: 1 0 20px; min-height: 20px; }
.divider { height: 1px; background: var(--divider); margin: 16px 0; }

.eyebrow {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--muted-2);
  margin-bottom: 6px;
}

.section-title {
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: -0.015em;
}

.section-copy {
  margin-top: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.45;
}

.control-label {
  display: block;
  margin: 12px 0 8px;
  color: var(--muted);
  font-size: 0.83rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.control-input {
  width: 100%;
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius);
  padding: 11px 12px;
  background: rgba(255,255,255,0.06);
  color: var(--text);
  outline: none;
}

.control-input:focus {
  border-color: rgba(179,136,235,0.8);
  box-shadow: 0 0 0 3px rgba(179,136,235,0.14);
}

select.control-input {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, rgba(255,255,255,0.85) 50%), linear-gradient(135deg, rgba(255,255,255,0.85) 50%, transparent 50%);
  background-position: calc(100% - 18px) calc(50% - 3px), calc(100% - 12px) calc(50% - 3px);
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
}

.segmented {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.segmented button {
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: var(--radius);
  background: rgba(255,255,255,0.05);
  color: var(--text);
  padding: 9px 12px;
  min-height: 40px;
  font-weight: 600;
}

.segmented button.active,
.segmented button[aria-pressed="true"] {
  background: linear-gradient(180deg, rgba(179,136,235,0.26), rgba(111,45,189,0.34));
  border-color: rgba(179,136,235,0.6);
  color: #fff;
}

.segmented button:disabled {
  opacity: 0.55;
  cursor: default;
}

.range-readout {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 6px;
  color: var(--muted-2);
  font-size: 0.79rem;
}

.range-readout strong {
  color: var(--text);
  font-size: 0.9rem;
}

.legend-header-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.legend-title { margin: 0; }
.legend-note {
  margin-top: 4px;
  color: var(--muted-2);
  font-size: 0.78rem;
  line-height: 1.35;
}

.legend {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.legend-item {
  display: grid;
  grid-template-columns: 22px 1fr;
  gap: 10px;
  align-items: center;
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 8px 10px;
  background: rgba(255,255,255,0.03);
}

.legend-item.interactive { cursor: pointer; }
.legend-item.selected {
  border-color: rgba(179,136,235,0.65);
  background: rgba(179,136,235,0.12);
}
.legend-item.dimmed { opacity: 0.58; }

.legend-swatch {
  width: 22px;
  height: 22px;
  border-radius: 7px;
  border: 1px solid rgba(255,255,255,0.18);
}

.legend-label {
  font-size: 0.85rem;
  color: var(--text);
  line-height: 1.35;
}

.legend-gradient {
  border-radius: var(--radius);
  border: 1px solid rgba(255,255,255,0.12);
  height: 16px;
}

.legend-gradient-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 0.78rem;
  color: var(--muted);
}

.primary-button,
.ghost-button,
.icon-button {
  border: 0;
  border-radius: var(--radius);
}

.primary-button {
  width: 100%;
  padding: 12px 14px;
  background: linear-gradient(180deg, #8e5bd9, #6f2dbd);
  color: white;
  font-weight: 700;
  box-shadow: 0 8px 24px rgba(111,45,189,0.28);
}

.ghost-button {
  padding: 8px 10px;
  background: rgba(255,255,255,0.06);
  color: var(--text);
  font-weight: 600;
}

.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(20, 14, 28, 0.88);
  color: var(--text);
  width: 46px;
  height: 46px;
  box-shadow: var(--shadow);
}

.icon-button.small {
  width: 36px;
  height: 36px;
  box-shadow: none;
  background: rgba(255,255,255,0.06);
}

#mobile-legend-button {
  position: absolute;
  right: 18px;
  bottom: 18px;
  z-index: 40;
}

#sources-fab {
  position: absolute;
  right: 18px;
  bottom: 74px;
  z-index: 40;
}

.bottom-sheet {
  position: absolute;
  inset: auto 12px 12px;
  z-index: 41;
  border-radius: 14px;
  background: rgba(20, 14, 28, 0.96);
  box-shadow: var(--shadow);
  transform: translateY(calc(100% + 20px));
  transition: transform 260ms ease;
}

.bottom-sheet.open {
  transform: translateY(0);
}

.sheet-handle {
  width: 56px;
  height: 4px;
  border-radius: 999px;
  background: rgba(255,255,255,0.28);
  margin: 10px auto 0;
}

.sheet-content {
  max-height: min(52vh, 340px);
  overflow: auto;
  padding: 12px 14px 14px;
}

.legend-mobile-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.mobile-scheme-wrap { margin: 12px 0 2px; }

.overlay {
  position: absolute;
  inset: 0;
  z-index: 90;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(4, 2, 5, 0.68);
}

.overlay.visible {
  display: flex;
}

.loading-card,
.orientation-card {
  width: min(340px, calc(100vw - 32px));
  border-radius: 14px;
  background: rgba(20, 14, 28, 0.94);
  box-shadow: var(--shadow);
  padding: 20px 18px;
  text-align: center;
}

.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  margin: 0 auto 12px;
  border: 3px solid rgba(255,255,255,0.14);
  border-top-color: #b388eb;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-text { color: var(--muted); }

.orientation-icon {
  font-size: 1.5rem;
  margin-bottom: 6px;
}

.popup {
  position: absolute;
  z-index: 50;
  min-width: 220px;
  max-width: min(360px, calc(100vw - 28px));
  border-radius: var(--radius);
  border: 0.5px solid rgba(255,255,255,0.5);
  padding: 12px 12px 10px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.popup.hidden { display: none; }

.popup-title-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
}

.popup h3 {
  margin: 0;
  font-size: 1.02rem;
  line-height: 1.15;
}

.popup-year {
  font-size: 0.82rem;
  font-weight: 700;
  opacity: 0.86;
  white-space: nowrap;
}

.popup-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 7px;
  margin-top: 10px;
}

.popup-line {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 0.88rem;
}

.popup-line small {
  display: block;
  opacity: 0.76;
  font-size: 0.75rem;
  margin-top: 3px;
}

.popup-year-buttons {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.popup-year-buttons button {
  padding: 8px 10px;
  border: 1px solid rgba(255,255,255,0.22);
  border-radius: 9px;
  background: rgba(255,255,255,0.12);
  color: inherit;
  font-weight: 700;
}

.popup-year-buttons button:disabled {
  opacity: 0.5;
  cursor: default;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(4, 2, 5, 0.48);
  padding: 14px;
}

.modal-backdrop.hidden { display: none; }

.modal {
  width: min(720px, calc(100vw - 28px));
  max-height: min(76vh, 680px);
  overflow: hidden;
  border-radius: 14px;
  background: rgba(18, 12, 25, 0.96);
  box-shadow: var(--shadow);
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 18px 14px;
  border-bottom: 1px solid var(--divider);
}

.modal-header h2 {
  margin: 4px 0 0;
  font-size: 1.2rem;
}

.modal-body {
  max-height: calc(76vh - 74px);
  overflow: auto;
  padding: 16px 18px 18px;
}

.source-item + .source-item {
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--divider);
}

.source-label {
  color: var(--muted-2);
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.7rem;
  margin-bottom: 5px;
}

.source-value {
  color: var(--text);
  line-height: 1.5;
}

.source-value a {
  color: #d7befc;
  text-decoration-color: rgba(215,190,252,0.45);
}

.attribution {
  position: absolute;
  left: 18px;
  bottom: 16px;
  z-index: 12;
  padding: 8px 10px;
  border-radius: 999px;
  background: rgba(10, 8, 12, 0.55);
  color: rgba(255,255,255,0.66);
  font-size: 0.72rem;
  pointer-events: none;
}

.mobile-only { display: none; }

@media (max-width: 900px) {
  .panel { display: none; }
  .mobile-only { display: inline-flex; }
  .bottom-sheet.mobile-only,
  #orientation-warning.mobile-only { display: flex; }
  #title-card {
    left: 0;
    right: 0;
    top: 0;
    min-width: 0;
    max-width: none;
    border-radius: 0 0 var(--radius) var(--radius);
    padding: calc(env(safe-area-inset-top, 0px) + 8px) 14px 10px;
    background: linear-gradient(180deg, rgba(255,255,255,0.18), rgba(255,255,255,0.04));
    color: #ffffff;
  }
  #title-card h1 {
    font-size: 0.98rem;
    text-align: center;
  }
  #title-card p {
    text-align: center;
    font-size: 0.67rem;
    opacity: 0.7;
  }
  .attribution {
    left: 12px;
    right: 12px;
    bottom: 12px;
    text-align: center;
    border-radius: 12px;
  }
  .popup {
    max-width: min(320px, calc(100vw - 22px));
    min-width: 0;
  }
}

@media (max-width: 900px) and (orientation: portrait) {
  #orientation-warning { display: flex; }
}

@media (max-width: 900px) and (orientation: landscape) {
  #map-wrap {
    align-items: center;
    justify-content: center;
  }
}

@media (prefers-reduced-motion: reduce) {
  .panel,
  .bottom-sheet,
  .country,
  .spinner {
    transition: none !important;
    animation: none !important;
  }
}
