:root {
  --bg: #0b1020;
  --bg-2: #111832;
  --panel: rgba(20, 28, 54, 0.82);
  --panel-solid: #141c36;
  --line: rgba(148, 163, 214, 0.16);
  --line-2: rgba(148, 163, 214, 0.28);
  --text: #e6e9f5;
  --muted: #8d97b8;
  --accent: #6ea8ff;
  --accent-2: #8b5cf6;
  --good: #34d399;
  --bad: #f87171;
  --nodata: #2a3352;
  --ocean: #0d1530;
  --radius: 12px;
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: radial-gradient(1200px 700px at 60% -10%, #1a2550 0%, var(--bg) 60%) fixed;
  display: grid;
  grid-template-rows: auto 1fr auto;
  font-size: 13px;
  overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
kbd {
  font-family: inherit; font-size: 11px; padding: 1px 5px; border-radius: 4px;
  border: 1px solid var(--line-2); background: var(--bg-2);
}
.muted { color: var(--muted); }

/* ── Top bar ─────────────────────────────────────────────── */
.topbar {
  display: grid;
  grid-template-columns: 290px 1fr auto;
  align-items: center;
  gap: 20px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  background: rgba(11, 16, 32, 0.6);
  backdrop-filter: blur(10px);
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { width: 30px; height: 30px; color: var(--accent); }
.brand h1 { font-size: 17px; margin: 0; letter-spacing: 0.2px; }
.subtitle { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.kpi-title { min-width: 0; }
.kpi-title h2 {
  margin: 2px 0 0; font-size: 18px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.kpi-cat {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px; color: var(--accent);
  font-weight: 600;
}

.top-controls { display: flex; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 3px; }
.field span { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.8px; color: var(--muted); }
.field input, .field select, .search-wrap input {
  background: var(--bg-2);
  border: 1px solid var(--line-2);
  border-radius: 8px;
  padding: 7px 10px;
  outline: none;
  min-width: 150px;
}
.field input:focus, .field select:focus, .search-wrap input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(110, 168, 255, 0.18); }

.lang-toggle {
  display: inline-flex;
  background: var(--bg-2);
  border: 1px solid var(--line-2);
  border-radius: 8px;
  padding: 3px;
  gap: 3px;
  height: 35px;
  align-items: center;
}
.lang-toggle button {
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  cursor: pointer;
  line-height: 1.2;
  transition: background 0.15s ease, color 0.15s ease;
}
.lang-toggle button:hover {
  color: var(--text);
}
.lang-toggle button[aria-pressed="true"],
.lang-toggle button.active {
  background: var(--accent);
  color: #0b1020;
}

/* ── Layout ──────────────────────────────────────────────── */
.layout {
  display: grid;
  grid-template-columns: 290px 1fr 340px;
  gap: 12px;
  padding: 12px;
  min-height: 0;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

/* ── KPI list ────────────────────────────────────────────── */
.search-wrap { padding: 12px; border-bottom: 1px solid var(--line); }
.search-wrap input { width: 100%; }
.kpi-list { overflow-y: auto; flex: 1; padding: 6px 6px 12px; scrollbar-width: thin; }
.cat-group { margin-bottom: 4px; }
.cat-head {
  display: flex; align-items: center; justify-content: space-between;
  width: 100%; border: 0; background: none; cursor: pointer;
  padding: 9px 8px 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.9px;
  color: var(--muted); font-weight: 600;
}
.cat-head:hover { color: var(--text); }
.cat-head .count { font-weight: 500; opacity: 0.7; }
.cat-group.collapsed .kpi-item { display: none; }
.cat-head .chev { transition: transform 0.2s; display: inline-block; margin-right: 6px; }
.cat-group.collapsed .chev { transform: rotate(-90deg); }
.kpi-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  text-align: left; border: 0; background: none; cursor: pointer;
  padding: 7px 10px; border-radius: 8px; line-height: 1.3; color: #c9cfe6;
}
.kpi-item:hover { background: rgba(110, 168, 255, 0.08); color: var(--text); }
.kpi-item.active { background: linear-gradient(90deg, rgba(110, 168, 255, 0.22), rgba(139, 92, 246, 0.14)); color: #fff; }
.kpi-item .num { font-size: 10.5px; color: var(--muted); width: 20px; flex: none; text-align: right; }
.kpi-item .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.dot.up { background: var(--good); }
.dot.down { background: var(--bad); }
.dot.neutral { background: #a5b4fc; }
.hint { margin: 0; padding: 8px 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 11.5px; }

/* ── Map ─────────────────────────────────────────────────── */
.map-wrap {
  position: relative;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: radial-gradient(circle at 50% 40%, #13204a 0%, var(--ocean) 70%);
  overflow: hidden;
  min-height: 0;
}
#map { width: 100%; height: 100%; display: block; cursor: grab; }
#map:active { cursor: grabbing; }
.sphere { fill: rgba(30, 50, 110, 0.18); stroke: rgba(148, 163, 214, 0.2); }
.graticule { fill: none; stroke: rgba(148, 163, 214, 0.07); stroke-width: 0.5; }
.country {
  stroke: rgba(8, 12, 28, 0.85);
  stroke-width: 0.4;
  vector-effect: non-scaling-stroke;
  transition: fill 0.45s ease, opacity 0.25s ease;
  cursor: pointer;
}
.country.nodata { fill: var(--nodata); }
.country.dim { opacity: 0.18; }
.country:hover { stroke: #fff; stroke-width: 1.2; }
.country.selected { stroke: #fff; stroke-width: 2; }

.map-buttons {
  position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; gap: 6px;
}
.map-buttons button {
  width: 32px; height: 32px; border-radius: 8px; cursor: pointer; font-size: 16px;
  background: var(--panel); border: 1px solid var(--line-2);
}
.map-buttons button:hover { border-color: var(--accent); }

.legend {
  position: absolute; left: 14px; bottom: 14px;
  width: min(440px, calc(100% - 28px));
  background: var(--panel); border: 1px solid var(--line-2);
  border-radius: var(--radius); padding: 10px 12px 8px;
  backdrop-filter: blur(8px);
}
.legend-head { display: flex; justify-content: space-between; gap: 10px; font-weight: 600; font-size: 12px; }
.polarity { font-weight: 500; font-size: 11px; color: var(--muted); white-space: nowrap; }
#legendSvg { width: 100%; display: block; margin-top: 4px; overflow: visible; }
#legendSvg text { fill: var(--muted); font-size: 10px; }
#legendSvg .marker { stroke: #fff; stroke-width: 2; fill: none; }
.legend-foot { display: flex; justify-content: space-between; color: var(--muted); font-size: 11px; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: -1px; }
.swatch.nodata { background: var(--nodata); }

.tooltip {
  position: absolute; pointer-events: none; z-index: 10;
  background: rgba(10, 14, 30, 0.95); border: 1px solid var(--line-2);
  border-radius: 10px; padding: 10px 12px; min-width: 200px; max-width: 280px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}
.tooltip .t-name { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 8px; }
.tooltip .t-sub { color: var(--muted); font-size: 11px; margin: 2px 0 8px; }
.tooltip .t-value { font-size: 20px; font-weight: 700; }
.tooltip .t-unit { color: var(--muted); font-size: 12px; font-weight: 500; margin-left: 4px; }
.tooltip .t-row { display: flex; justify-content: space-between; color: var(--muted); margin-top: 4px; }
.tooltip .t-row b { color: var(--text); font-weight: 600; }
.flag { font-size: 18px; line-height: 1; }

/* ── Right panel ─────────────────────────────────────────── */
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; padding: 12px; border: 0; background: none; cursor: pointer; color: var(--muted);
  font-weight: 600; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-body { overflow-y: auto; flex: 1; min-height: 0; scrollbar-width: thin; }
.kpi-meta { padding: 12px 14px; border-bottom: 1px solid var(--line); color: var(--muted); line-height: 1.5; }
.kpi-meta .stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 10px; }
.kpi-meta .stat { background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 8px; }
.kpi-meta .stat b { display: block; color: var(--text); font-size: 13px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-meta .stat span { font-size: 10.5px; text-transform: uppercase; letter-spacing: 0.6px; }
.kpi-meta .stat small { display: block; font-size: 10.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kpi-meta code { font-size: 11px; color: #a5b4fc; }
.rank-toolbar { display: flex; align-items: center; justify-content: space-between; padding: 10px 14px 4px; }
.chip {
  border: 1px solid var(--line-2); background: var(--bg-2); border-radius: 999px;
  padding: 4px 10px; cursor: pointer; font-size: 12px;
}
.chip:hover { border-color: var(--accent); }
.rank-list { list-style: none; margin: 0; padding: 4px 8px 12px; }
.rank-list li {
  display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px;
  padding: 6px 6px; border-radius: 8px; cursor: pointer; position: relative;
}
.rank-list li:hover, .rank-list li.hl { background: rgba(110, 168, 255, 0.1); }
.rank-list li.sel { background: rgba(110, 168, 255, 0.2); }
.rank-list .r { color: var(--muted); font-size: 11px; text-align: right; }
.rank-list .n { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: flex; align-items: center; gap: 6px; }
.rank-list .n i { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.rank-list .v { font-variant-numeric: tabular-nums; font-weight: 600; }
.rank-list .bar { grid-column: 2 / 4; height: 3px; border-radius: 2px; background: var(--bg-2); overflow: hidden; }
.rank-list .bar span { display: block; height: 100%; border-radius: 2px; }

/* Profile */
.profile { padding: 12px 14px; }
.profile .empty { padding: 30px 6px; text-align: center; line-height: 1.6; }
.p-head { display: flex; align-items: center; gap: 12px; margin-bottom: 4px; }
.p-head .flag { font-size: 34px; }
.p-head h3 { margin: 0; font-size: 18px; }
.p-head p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }
.p-score { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0 6px; }
.p-score div { background: var(--bg-2); border: 1px solid var(--line); border-radius: 8px; padding: 8px; }
.p-score span { display: block; font-size: 10.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.6px; }
.p-score b { font-size: 16px; }
.p-cat { margin-top: 14px; }
.p-cat h4 {
  margin: 0 0 6px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.9px; color: var(--muted);
  display: flex; justify-content: space-between;
}
.p-row {
  display: grid; grid-template-columns: 1fr 86px; gap: 2px 10px; padding: 6px 6px; border-radius: 8px; cursor: pointer;
}
.p-row:hover { background: rgba(110, 168, 255, 0.08); }
.p-row.active { background: rgba(110, 168, 255, 0.16); }
.p-row .lbl { font-size: 12px; color: #c9cfe6; line-height: 1.3; }
.p-row .val { text-align: right; font-weight: 600; font-variant-numeric: tabular-nums; font-size: 12px; }
.p-row .pbar {
  grid-column: 1 / 3; height: 4px; border-radius: 2px; position: relative; margin: 3px 0 1px;
  background: linear-gradient(90deg, rgba(248,113,113,.22), rgba(250,204,21,.18), rgba(52,211,153,.22));
}
.p-row .pbar::after { content: ''; position: absolute; left: 50%; top: -2px; width: 1px; height: 8px; background: rgba(148,163,214,.35); }
.p-row.neutral .pbar { background: rgba(165, 180, 252, 0.18); }
.field select { max-width: 230px; }
.p-row .pbar span { position: absolute; top: -3px; width: 10px; height: 10px; border-radius: 50%; margin-left: -5px; border: 2px solid #0b1020; z-index: 1; }
.p-row .meta { grid-column: 1 / 3; font-size: 10.5px; color: var(--muted); }
.p-row.na .val { color: var(--muted); font-weight: 400; }

.footer {
  padding: 6px 18px 10px; color: var(--muted); font-size: 11.5px; text-align: center;
}

/* ── RTL & Arabic ────────────────────────────────────────── */
html[dir="rtl"] {
  font-family: 'IBM Plex Sans Arabic', 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}
html[dir="rtl"] button,
html[dir="rtl"] input,
html[dir="rtl"] select {
  font-family: inherit;
}
html[dir="rtl"] .map-buttons {
  right: auto;
  left: 12px;
}
html[dir="rtl"] .rank-list .r {
  text-align: left;
}
html[dir="rtl"] .p-row .val {
  text-align: left;
}
html[dir="rtl"] .p-row .pbar {
  background: linear-gradient(270deg, rgba(248,113,113,.22), rgba(250,204,21,.18), rgba(52,211,153,.22));
}
html[dir="rtl"] .p-row .pbar span {
  margin-left: 0;
  margin-right: -5px;
}

/* ── Responsive ──────────────────────────────────────────── */
@media (max-width: 1250px) {
  .layout { grid-template-columns: 250px 1fr 300px; }
  .topbar { grid-template-columns: 250px 1fr; }
  .top-controls { grid-column: 1 / -1; }
}
@media (max-width: 920px) {
  body { overflow: auto; }
  .layout { grid-template-columns: 1fr; grid-template-rows: 300px 60vh 500px; }
  .map-wrap { order: -1; grid-row: 2; }
  .left { grid-row: 1; }
  .topbar { grid-template-columns: 1fr; }
  .top-controls { flex-wrap: wrap; }
}

.no-anim .country { transition: none !important; }
