@import url('https://fonts.googleapis.com/css2?family=DM+Mono:ital,wght@0,300;0,400;0,500;1,300&family=Unbounded:wght@400;700;900&display=swap');

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

:root {
  --bg: #080a0e;
  --s1: #0f1218;
  --s2: #161b24;
  --s3: #1e2535;
  --b1: #1e2535;
  --b2: #2a3448;
  --b3: #3a4860;
  --tx: #c8d4e8;
  --tx2: #7890b0;
  --tx3: #e0f2ff;
  --acc: #00e5ff;
  --acc2: #ff6b35;
  --panel: 380px;
  --head: 62px;
}

html, body { height: 100%; overflow: hidden; background: var(--bg); }

body {
  font-family: 'DM Mono', monospace;
  color: var(--tx);
  display: flex; flex-direction: column;
}

/* ─── VISIBILITY HELPERS ──────────────────────────────────────────────── */
.mobile-only  { display: none !important; }
.desktop-only { display: flex; }

/* ─── HEADER ──────────────────────────────────────────────────────────── */
header {
  height: var(--head);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0;
  border-bottom: 1px solid var(--b1);
  background: var(--bg);
  z-index: 30;
  overflow: hidden;
}

.logo-block {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 24px;
  border-right: 1px solid var(--b1);
  gap: 10px;
  flex-shrink: 0;
}
.logo-text {
  font-family: 'Unbounded', sans-serif;
  font-weight: 900;
  font-size: 1.1rem;
  letter-spacing: -0.02em;
  line-height: 1;
}
.logo-text span { color: var(--acc); }
.logo-sub {
  font-size: 0.48rem;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: var(--tx3);
  line-height: 1;
  margin-top: 3px;
}

.ctrl-block {
  display: flex;
  align-items: center;
  height: 100%;
  padding: 0 20px;
  border-right: 1px solid var(--b1);
  gap: 14px;
  flex-shrink: 0;
}
.ctrl-lbl {
  font-size: 0.5rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--tx3);
  margin-bottom: 4px;
  display: block;
}

/* Capital */
.cap-wrap {
  display: flex; align-items: center; gap: 6px;
  background: var(--s2); border: 1px solid var(--b2);
  padding: 6px 12px; border-radius: 3px;
  transition: border-color 0.2s;
}
.cap-wrap:focus-within { border-color: var(--acc); }
.cap-sym { color: var(--acc); font-weight: 500; font-size: 0.85rem; }
#capital {
  background: none; border: none; outline: none;
  color: var(--tx); font-family: 'DM Mono', monospace;
  font-size: 0.9rem; font-weight: 500; width: 100px;
}

/* SWR */
.swr-row { display: flex; align-items: center; gap: 8px; }
.swr-disp { color: var(--acc); font-weight: 500; font-size: 0.85rem; min-width: 34px; }

input[type=range] {
  -webkit-appearance: none; 
  appearance: none; /* Standard property */
  height: 2px;
  background: var(--b2); 
  border-radius: 1px; 
  cursor: pointer;
}

input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; 
  appearance: none; /* Standard property */
  width: 12px; 
  height: 12px;
  border-radius: 50%; 
  background: var(--acc); 
  cursor: pointer;
  box-shadow: 0 0 6px rgba(0,229,255,0.4);
}

#swr { width: 80px; }

/* Lifestyle slider */
.ls-wrap { display: flex; flex-direction: column; gap: 4px; }
.ls-slider-row { display: flex; align-items: center; gap: 10px; }
#lifestyle { width: 160px; }
#lifestyle::-webkit-slider-thumb { background: var(--acc2); box-shadow: 0 0 6px rgba(255,107,53,0.5); }
input[type=range]#lifestyle { background: linear-gradient(to right, #1a3a2a, #2a4a1a, #4a3010); }

.ls-labels {
  display: flex; justify-content: space-between;
  font-size: 0.46rem; color: var(--tx3); letter-spacing: 0.1em;
  text-transform: uppercase;
}
.ls-current { font-size: 0.72rem; font-weight: 500; color: var(--acc2); min-width: 80px; }

/* Budget — Fix 1: left border + column layout matching other ctrl-blocks,
   min-width prevents it from being squeezed off screen before mobile breakpoint */
.budget-block {
  display: flex;
  flex-direction: column;
  justify-content: center;
  height: 100%;
  padding: 0 24px;
  border-left: 1px solid var(--b1);
  flex-shrink: 0;
  min-width: 140px;
}
.budget-num {
  font-family: 'Unbounded', sans-serif;
  font-size: 1rem; font-weight: 700;
  color: var(--acc);
  letter-spacing: -0.02em;
  line-height: 1.1;
}
.budget-sub { font-size: 0.5rem; color: var(--tx3); letter-spacing: 0.15em; text-transform: uppercase; margin-top: 3px; }

/* ─── MOBILE HEADER ───────────────────────────────────────────────────── */
.mobile-header-right {
  margin-left: auto;
  align-items: center;
  gap: 10px;
  padding-right: 14px;
}
.mobile-budget-pill {
  font-family: 'Unbounded', sans-serif;
  font-size: 0.72rem; font-weight: 700;
  color: var(--acc);
  background: var(--s2);
  border: 1px solid var(--b2);
  padding: 5px 10px;
  border-radius: 100px;
  white-space: nowrap;
}
.mobile-settings-btn {
  background: var(--s2); border: 1px solid var(--b2);
  color: var(--tx3); width: 40px; height: 40px;
  border-radius: 6px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.mobile-settings-btn:hover,
.mobile-settings-btn:focus { color: var(--acc); border-color: var(--acc); outline: none; }

/* ─── MOBILE CONTROLS DRAWER ──────────────────────────────────────────── */
#mobile-controls-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.55);
  z-index: 80;
  opacity: 0; transition: opacity 0.25s;
}
#mobile-controls-backdrop.show { display: block; opacity: 1; }

#mobile-controls-drawer {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--s1);
  border-top: 1px solid var(--b2);
  border-radius: 14px 14px 0 0;
  padding: 12px 20px 32px;
  z-index: 90;
  transform: translateY(100%);
  transition: transform 0.32s cubic-bezier(0.4,0,0.2,1);
  display: flex; flex-direction: column; gap: 18px;
}
#mobile-controls-drawer.open { transform: translateY(0); }

.mcd-handle {
  width: 36px; height: 4px;
  background: var(--b3); border-radius: 2px;
  margin: 0 auto 4px;
  flex-shrink: 0;
}
.mcd-title {
  font-family: 'Unbounded', sans-serif;
  font-size: 0.85rem; font-weight: 700;
  text-align: center; flex-shrink: 0;
}
.mcd-field { display: flex; flex-direction: column; gap: 8px; }
.mcd-budget-row {
  background: var(--s2); border: 1px solid var(--b2);
  border-radius: 6px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 4px;
  align-items: center; text-align: center;
}
.mcd-budget-row .budget-num { font-size: 1.3rem; }
.mcd-close-btn {
  background: var(--acc); color: #000;
  border: none; border-radius: 6px;
  padding: 13px; font-family: 'DM Mono', monospace;
  font-size: 0.8rem; font-weight: 500;
  letter-spacing: 0.08em; cursor: pointer;
  text-transform: uppercase;
  transition: opacity 0.15s;
  flex-shrink: 0;
}
.mcd-close-btn:active { opacity: 0.8; }

/* ─── LAYOUT ──────────────────────────────────────────────────────────── */
.main { flex: 1; display: flex; position: relative; overflow: hidden; }

#map-wrap {
  flex: 1; position: relative; overflow: hidden;
  transition: margin-right 0.38s cubic-bezier(0.4,0,0.2,1);
}
#map-wrap.panel-open { margin-right: var(--panel); }

svg#map { width: 100%; height: 100%; cursor: grab; display: block; }
svg#map:active { cursor: grabbing; }

/* Countries */
.country {
  stroke: #050709; stroke-width: 0.3px; cursor: pointer;
  transition: filter 0.1s, stroke-width 0.1s;
}
.country:hover { filter: brightness(1.45); stroke: rgba(255,255,255,0.35); stroke-width: 0.7px; }
.country.selected { stroke: rgba(255,255,255,0.7); stroke-width: 1px; filter: brightness(1.2); }
.country.no-data { fill: #0e1118; cursor: default; }
.country.no-data:hover { filter: none; stroke: #050709; stroke-width: 0.3px; }

/* City dots */
/* FIX: Removed `r` CSS property — SVG r is not reliably animatable via CSS
   across browsers (fails in Firefox). Hover sizing is now handled in script.js
   via JS mouseenter/mouseleave, which sets .attr("r", ...) directly. */
.city-dot {
  cursor: pointer;
}
.city-dot circle {
  stroke: rgba(0,0,0,0.7);
  stroke-width: 1px;
}
/* Selected state still styled via CSS stroke, but r is JS-managed */
.city-dot.selected circle {
  stroke: #fff;
  stroke-width: 2px;
}
.city-dot text {
  fill: rgba(255,255,255,0.85);
  font-family: 'DM Mono', monospace;
  pointer-events: none;
  text-shadow: 0 1px 3px rgba(0,0,0,0.9), 0 0 6px rgba(0,0,0,0.9);
  dominant-baseline: middle;
}

/* ─── SIDE PANEL ──────────────────────────────────────────────────────── */
#panel {
  position: absolute; top: 0; right: 0;
  width: var(--panel); height: 100%;
  background: var(--s1);
  border-left: 1px solid var(--b1);
  transform: translateX(100%);
  transition: transform 0.38s cubic-bezier(0.4,0,0.2,1);
  z-index: 20;
  display: flex; flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
}
#panel::-webkit-scrollbar { width: 3px; }
#panel::-webkit-scrollbar-thumb { background: var(--b2); }
#panel.open { transform: translateX(0); }

/* Panel drag handle (mobile) */
.panel-handle {
  width: 36px; height: 4px;
  background: var(--b3); border-radius: 2px;
  margin: 10px auto 0;
  flex-shrink: 0;
}

.ph {
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--b1);
  position: relative;
  flex-shrink: 0;
}
.ph-close {
  position: absolute; top: 14px; right: 14px;
  background: var(--s2); border: 1px solid var(--b2);
  color: var(--tx3); width: 26px; height: 26px;
  border-radius: 3px; cursor: pointer; font-size: 0.9rem;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.15s, border-color 0.15s;
}
.ph-close:hover { color: var(--tx); border-color: var(--b3); }

.ph-type {
  font-size: 0.5rem; letter-spacing: 0.2em; text-transform: uppercase;
  color: var(--tx3); margin-bottom: 4px;
}
.ph-name {
  font-family: 'Unbounded', sans-serif; font-weight: 700;
  font-size: 1.15rem; line-height: 1.15; margin-bottom: 2px;
}
.ph-region { font-size: 0.6rem; color: var(--tx2); }

/* Summary grid */
.p-summary {
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  padding: 14px 20px; gap: 10px;
  border-bottom: 1px solid var(--b1);
  flex-shrink: 0;
}
.ps-lbl { font-size: 0.48rem; letter-spacing: 0.18em; text-transform: uppercase; color: var(--tx3); margin-bottom: 3px; }
.ps-val { font-family: 'Unbounded', sans-serif; font-size: 0.85rem; font-weight: 700; }

/* Verdict */
.p-verdict {
  margin: 12px 20px 4px;
  padding: 9px 14px;
  border-radius: 3px; border: 1px solid;
  font-size: 0.65rem; font-weight: 500;
  letter-spacing: 0.06em; text-align: center;
  flex-shrink: 0;
}
.v-yes  { background: rgba(0,229,136,0.08); border-color: #00e588; color: #00e588; }
.v-edge { background: rgba(255,208,64,0.08); border-color: #ffd040; color: #ffd040; }
.v-no   { background: rgba(255,80,60,0.08);  border-color: #ff503c; color: #ff503c; }

/* Lifestyle breakdown bars */
.p-breakdown-lbl {
  padding: 12px 20px 6px;
  font-size: 0.5rem; letter-spacing: 0.18em;
  text-transform: uppercase; color: var(--tx3);
  flex-shrink: 0;
}
.ls-bars { padding: 0 14px 6px; flex-shrink: 0; }
.ls-bar-row {
  background: var(--s2); border: 1px solid var(--b1);
  border-radius: 3px; padding: 10px 12px; margin-bottom: 6px;
  position: relative; overflow: hidden;
}
.ls-bar-row.active { border-color: var(--b3); }
.ls-bar-bg {
  position: absolute; left: 0; top: 0; bottom: 0;
  opacity: 0.12; border-radius: 3px;
  transition: width 0.4s cubic-bezier(0.4,0,0.2,1);
}
.ls-bar-top {
  display: flex; justify-content: space-between; align-items: baseline;
  position: relative; z-index: 1; margin-bottom: 2px;
}
.ls-bar-name { font-size: 0.65rem; font-weight: 500; }
.ls-bar-val  { font-size: 0.75rem; font-weight: 500; }
.ls-bar-sub  {
  position: relative; z-index: 1;
  font-size: 0.58rem; color: var(--tx2);
  display: flex; justify-content: space-between;
}
.ls-bar-pct { font-weight: 500; }

/* Required capital */
.p-capital {
  margin: 4px 20px 10px;
  padding: 10px 14px;
  background: var(--s2); border: 1px solid var(--b1); border-radius: 3px;
  display: flex; justify-content: space-between; align-items: center;
  flex-shrink: 0;
}
.p-capital-lbl { font-size: 0.5rem; letter-spacing: 0.15em; text-transform: uppercase; color: var(--tx3); }
.p-capital-val { font-family: 'Unbounded', sans-serif; font-size: 0.9rem; font-weight: 700; }

/* Note */
.p-note {
  padding: 10px 20px 16px;
  font-size: 0.6rem; color: var(--tx2);
  line-height: 1.6; border-top: 1px solid var(--b1);
  font-style: italic; flex-shrink: 0;
}


/* 1. Hide legend/stats when desktop tip is active */
#map-wrap:has(#tip.show) #legend,
#map-wrap:has(#tip.show) #stats {
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

/* 2. Hide legend/stats when mobile tip is active */
#map-wrap:has(#mobile-tip[style*="block"]) #legend,
#map-wrap:has(#mobile-tip[style*="block"]) #stats {
  opacity: 0 !important;
  visibility: hidden !important;
  pointer-events: none !important;
}

/* Country cities section inside panel */
.p-cities-lbl {
  padding: 10px 20px 6px;
  font-size: 0.5rem; letter-spacing: 0.18em;
  text-transform: uppercase; color: var(--tx3);
  border-top: 1px solid var(--b1);
  flex-shrink: 0;
}

/* FIX: Cities list scroll — logic now driven by JS checkScrollable()
   which sets .scrollable class when panel.scrollHeight > panel.clientHeight.
   The panel itself is overflow-y:auto so it handles scrolling naturally. */
.p-cities-list {
  padding: 0 14px 10px;
}
.p-cities-list::-webkit-scrollbar { width: 3px; }
.p-cities-list::-webkit-scrollbar-thumb { background: var(--b2); }

.p-city-chip {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 10px; margin-bottom: 4px;
  background: var(--s2); border: 1px solid var(--b1); border-radius: 3px;
  cursor: pointer; transition: border-color 0.15s;
  font-size: 0.65rem;
}
.p-city-chip:hover { border-color: var(--b3); }
.p-city-chip-left { display: flex; align-items: center; gap: 8px; }
.p-city-chip-dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.p-city-chip-type { font-size: 0.52rem; color: var(--tx3); margin-left: 4px; }
.p-city-chip-cost { font-weight: 500; }

/* ─── OVERLAYS ────────────────────────────────────────────────────────── */
#legend {
  position: absolute; bottom: 22px; left: 22px;
  background: var(--s1); border: 1px solid var(--b1);
  border-radius: 4px; padding: 11px 15px; z-index: 10;
}
.leg-lbl { font-size: 0.48rem; letter-spacing: 0.18em; text-transform: uppercase; color: var(--tx3); margin-bottom: 8px; }
.grad { width: 150px; height: 5px; border-radius: 3px; background: linear-gradient(to right,#ff2233,#ff9922,#ffd040,#aaee44,#00e588); margin-bottom: 5px; }
.grad-row { display: flex; justify-content: space-between; font-size: 0.52rem; color: var(--tx3); }

#stats {
  position: absolute; bottom: 22px; right: 22px;
  background: var(--s1); border: 1px solid var(--b1);
  border-radius: 4px; padding: 11px 15px; z-index: 10;
  min-width: 150px;
  transition: opacity 0.15s;
}
/* Fix 3: hide stats box while a tooltip is active so it doesn't clash */
#map-wrap.tip-active #stats { opacity: 0; pointer-events: none; }
#map-wrap.panel-open #stats { display: none; }
.stats-lbl { font-size: 0.48rem; letter-spacing: 0.18em; text-transform: uppercase; color: var(--tx3); margin-bottom: 7px; }
.st-row { display: flex; justify-content: space-between; align-items: center; font-size: 0.65rem; margin: 3px 0; gap: 10px; }
.st-dot { display: inline-block; width: 6px; height: 6px; border-radius: 50%; margin-right: 5px; vertical-align: middle; }

.zoom-wrap {
  position: absolute; top: 14px; right: 14px;
  display: flex; flex-direction: column; gap: 3px; z-index: 10;
}
#map-wrap.panel-open .zoom-wrap { right: calc(var(--panel) + 14px); }
.zb {
  background: var(--s1); border: 1px solid var(--b2);
  color: var(--tx3); width: 28px; height: 28px; border-radius: 3px;
  cursor: pointer; font-size: 1rem;
  display: flex; align-items: center; justify-content: center;
  transition: color 0.15s, border-color 0.15s;
}
.zb:hover { color: var(--acc); border-color: var(--acc); }

/* ─── TOOLTIP ─────────────────────────────────────────────────────────── */
#tip {
  position: fixed; 
  pointer-events: none;
  background: var(--s1); /* Solid background to hide what's behind */
  border: 1px solid var(--b2);
  border-radius: 4px; 
  padding: 10px 14px;
  opacity: 0; 
  transition: opacity 0.1s;
  z-index: 100; /* Higher than #legend and #stats (which are 10) */
  min-width: 190px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.8); /* Stronger shadow to hide the edge */
}
#tip.show { opacity: 1; }
.tip-name { font-family: 'Unbounded', sans-serif; font-size: 0.85rem; font-weight: 700; margin-bottom: 7px; }
.tip-row { display: flex; justify-content: space-between; gap: 14px; font-size: 0.63rem; margin: 3px 0; }
.tip-lbl { color: var(--tx2); }
.tip-val { font-weight: 500; }
.tip-hint { margin-top: 7px; font-size: 0.55rem; color: var(--tx3); font-style: italic; }

/* ─── MOBILE BOTTOM TOOLTIP ───────────────────────────────────────────── */
#mobile-tip {
  display: none;
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: var(--s1);
  border-top: 1px solid var(--b2);
  padding: 10px 16px 20px;
  z-index: 15;
  pointer-events: none;
}
.mtip-name {
  font-family: 'Unbounded', sans-serif;
  font-size: 0.9rem; font-weight: 700;
  margin-bottom: 2px;
}
.mtip-sub { font-size: 0.62rem; color: var(--tx2); }
.mtip-hint {
  font-size: 0.52rem; color: var(--acc);
  margin-top: 4px; letter-spacing: 0.1em;
  text-transform: uppercase;
}

/* ─── MOBILE RESPONSIVE ───────────────────────────────────────────────── */
@media (max-width: 900px) {
  .mobile-only  { display: flex !important; }
  .desktop-only { display: none !important; }

  /* Header: fixed height, logo + mobile controls only */
  header {
    height: 52px;
    overflow: visible;
  }

  /* Panel: slides up from bottom as a drawer */
  #panel {
    top: auto;
    bottom: 0;
    right: 0;
    left: 0;
    width: 100%;
    height: 72vh;
    transform: translateY(100%);
    border-left: none;
    border-top: 1px solid var(--b2);
    border-radius: 14px 14px 0 0;
  }
  #panel.open {
    transform: translateY(0);
  }
/* Mobile capital formatting inside the drawer */
  #capital-mobile {
    /* 1. Reset browser defaults */
    background: var(--s2); /* Matches desktop .cap-wrap background */
    border: 1px solid var(--b2); /* Matches desktop .cap-wrap border */
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    border-radius: 3px;

    /* 2. Typography */
    color: var(--tx);
    font-family: 'DM Mono', monospace;
    font-size: 0.9rem;
    font-weight: 500;

    /* 3. Layout & Touch Target */
    width: 100%;
    padding: 12px; /* Bigger touch target for mobile */
    margin: 10px 0;
    display: block;
    box-sizing: border-box;
  }

  /* Focus state to match desktop behavior */
  #capital-mobile:focus {
    border-color: var(--acc);
  }

  /* Map: shrink vertically when panel is open to show map peek above */
  #map-wrap.panel-open {
    margin-right: 0;
    margin-bottom: 72vh;
  }

  /* Zoom buttons: 44px minimum touch target (Apple HIG) */
  .zb { width: 44px; height: 44px; font-size: 1.2rem; }

  /* Keep zoom controls in top-right regardless of panel state */
  .zoom-wrap { top: 10px; right: 10px; }
  #map-wrap.panel-open .zoom-wrap { right: 10px; }

  /* Stats and legend: tuck into corners */
  #stats { bottom: 10px; left: 10px; right: auto; padding: 8px 10px; }
  #legend { bottom: 10px; right: 10px; left: auto; }

  /* Hide stats when panel is open (they'd be under the panel drawer) */
  #map-wrap.panel-open #stats  { display: none; }
  #map-wrap.panel-open #legend { display: none; }

  /* Desktop cursor tooltip: hidden on touch devices */
  #tip { display: none !important; }

  /* Mobile bottom tooltip: shown on tap */
  #mobile-tip { display: none; } /* JS controls display:block */

  /* Slightly larger panel header text on mobile */
  .ph-name { font-size: 1rem; }

  /* Taller capital chip touch target */
  .p-city-chip { padding: 10px 12px; }
}

@media (max-width: 420px) {
  .logo-text { font-size: 0.9rem; }
  .mobile-budget-pill { font-size: 0.62rem; padding: 4px 8px; }
}