:root {
  --bg: #0f172a;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --radius: 12px;
  --sticky-bg: #f8fafc;
  --shadow: 0 1px 3px rgba(15, 23, 42, .12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: #f1f5f9;
  -webkit-text-size-adjust: 100%;
  line-height: 1.4;
  height: 100%;
}

main {
  padding: 12px;
  max-width: 1400px;
  margin: 0 auto;
  height: 100%;
  display: flex;
  flex-direction: column;
}
.metrics-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.metrics-row .metric-block {
  flex: 1;
}

.unit-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  user-select: none;
  flex: none;
}
.unit-toggle:hover {
  background: var(--accent-soft);
  color: var(--accent);
  border-color: var(--accent);
  transform: scale(1.05);
}
.unit-toggle:active {
  transform: scale(0.95);
}

/* ---------- Controls ---------- */
.controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 12px;
}
.control-block {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 12px;
  box-shadow: var(--shadow);
}
.control-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; flex-wrap: wrap; }
.control-title { font-weight: 600; font-size: .9rem; }
.hint { color: var(--muted); font-size: .72rem; }

.chips-summary { display: none; }
.chip-list { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line);
  background: #f8fafc;
  color: var(--ink);
  border-radius: 999px;
  padding: 6px 12px;
  font-size: .8rem;
  cursor: pointer;
  user-select: none;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition: background .12s, border-color .12s;
}
.dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.chip[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: #1e3a8a;
  font-weight: 600;
}

/* Sort */
.sort-row { display: flex; gap: 8px; flex-wrap: wrap; }
select, input[type="search"] {
  font: inherit;
  font-size: .85rem;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  max-width: 100%;
}
select { min-width: 160px; }

/* Country selector */
.country-block summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: .9rem;
}
.country-block summary::-webkit-details-marker { display: none; }
.country-block summary::after {
  content: "▾";
  margin-left: auto;
  color: var(--muted);
  transition: transform .15s;
}
.country-block[open] summary::after { transform: rotate(180deg); }
.badge {
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  padding: 1px 9px;
  font-size: .72rem;
  font-weight: 600;
}
.country-tools { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
#countrySearch { width: 100%; }
.region-buttons, .select-actions { display: flex; flex-wrap: wrap; gap: 6px; }
.region-buttons button, .select-actions button {
  font: inherit;
  font-size: .75rem;
  padding: 5px 10px;
  border: 1px solid var(--line);
  background: #f8fafc;
  border-radius: 999px;
  cursor: pointer;
  color: var(--ink);
}
.region-buttons button:active, .select-actions button:active { background: var(--accent-soft); }

.country-list {
  margin-top: 10px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 4px 10px;
  max-height: 240px;
  overflow-y: auto;
}
.country-list label {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: .82rem;
  padding: 3px 0;
  cursor: pointer;
}
.country-list input { width: 16px; height: 16px; accent-color: var(--accent); }
.country-list label.hidden { display: none; }

/* ---------- Table ---------- */
.table-wrap {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  box-shadow: var(--shadow);
  flex: 1;
  min-height: 0;
}
.loading { padding: 28px 16px; text-align: center; color: var(--muted); }
.loading.error { color: #b91c1c; }

table { border-collapse: separate; border-spacing: 0; font-size: .8rem; width: max-content; }

thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background: var(--sticky-bg);
  font-weight: 600;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: .02em;
  font-size: .68rem;
  padding: 8px 6px;
  border-bottom: 2px solid var(--line);
  white-space: nowrap;
}

th, td { border-bottom: 1px solid var(--line); }

/* sticky left columns */
.col-country, .col-flight, .col-metric { position: sticky; background: #fff; z-index: 2; }
.col-country { left: 0; min-width: 104px; max-width: 104px; }
.col-flight  { left: 104px; min-width: 50px; text-align: center; }
.col-metric  { left: 154px; min-width: 88px; }

thead .col-country { left: 0; z-index: 4; }
thead .col-flight  { left: 104px; z-index: 4; }
thead .col-metric  { left: 154px; z-index: 4; }

/* sortable headers */
thead th.sortable { cursor: pointer; user-select: none; transition: background 0.15s; }
thead th.sortable:hover { background: #e2e8f0; }
.sort-indicator { font-size: 0.8em; margin-left: 2px; opacity: 0.3; display: inline-block; width: 10px; }
.sortable[aria-sort="ascending"] .sort-indicator,
.sortable[aria-sort="descending"] .sort-indicator { opacity: 1; color: var(--accent); }

/* hide metric column when a single metric is selected */
body.single-metric .col-metric { display: none; }

/* flight column is hidden by default; when it's off, the metric column
   shifts left to sit right after the country column */
body.no-flight .col-metric,
body.no-flight thead .col-metric { left: 104px; }

td.col-country {
  font-weight: 600;
  vertical-align: top;
  padding: 8px 8px;
  border-right: 2px solid var(--line);
  line-height: 1.25;
}
td.col-country .flag-name { display: block; }
td.col-flight {
  vertical-align: top;
  padding: 8px 4px;
  color: var(--accent);
  font-weight: 600;
  font-size: .78rem;
  border-right: 1px solid var(--line);
}
td.col-metric {
  color: var(--muted);
  padding: 6px 8px;
  font-size: .75rem;
  border-right: 2px solid var(--line);
}
td.metric-unit { color: #94a3b8; font-size: .68rem; }

td.val {
  text-align: center;
  padding: 6px 4px;
  min-width: 46px;
  font-variant-numeric: tabular-nums;
  color: #1a1a1a;
}
td.val.empty { color: #cbd5e1; background: #f8fafc !important; }

/* zebra grouping by country */
tbody tr.country-start td { border-top: 2px solid #cbd5e1; }

/* sortable metrics */
td.col-metric.sortable-metric {
  cursor: pointer;
  user-select: none;
  transition: background 0.12s, color 0.12s;
}
td.col-metric.sortable-metric:hover {
  background: #f1f5f9;
  color: var(--accent);
}
td.col-metric.sortable-metric.active-sort {
  background: var(--accent-soft);
  color: #1e3a8a;
  font-weight: 600;
}
td.col-metric.sortable-metric.active-sort .metric-unit {
  color: #3b82f6;
}
td.col-metric .metric-cell-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
}
td.col-metric .sort-indicator {
  margin-left: auto;
  flex-shrink: 0;
}

/* ---------- Responsive ---------- */
@media (min-width: 760px) {
  .controls { display: grid; grid-template-columns: 1fr; align-items: start; }
}

/* On mobile, collapse the metric chips to a single summary row; tap to expand. */
@media (max-width: 759px) {
  .chips-summary {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    border: 1px solid var(--line);
    background: #f8fafc;
    border-radius: 999px;
    padding: 6px 12px;
    font: inherit;
    font-size: .85rem;
    font-weight: 600;
    color: var(--ink);
    cursor: pointer;
  }
  .chips-summary .chips-chevron {
    margin-left: auto;
    color: var(--muted);
    transition: transform .15s;
  }
  .chips-summary[aria-expanded="true"] .chips-chevron { transform: rotate(180deg); }
  .chips-summary-extra { color: var(--muted); font-weight: 500; font-size: .78rem; }

  #metricChips .chip-list { display: none; margin-top: 8px; }
  #metricChips.expanded .chip-list { display: flex; }
}
