:root {
  --bg: #0e1117;
  --bg-panel: #151a23;
  --bg-elev: #1b2230;
  --line: #262d3a;
  --text: #e6eaf2;
  --text-dim: #93a0b5;
  --accent: #4da3ff;
  --up: #f0855a;
  --down: #6aa9e0;
  --flat: #9aa5b1;
  --ocean: #0b0f16;
  --sidebar-w: 340px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  /* minmax(0, …): колонка не должна растягиваться под min-content шапки,
     иначе на узком экране страница становится шире окна */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.4 "Segoe UI", Roboto, system-ui, sans-serif;
  overflow: hidden;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
}

/* ---------- Шапка ---------- */

.topbar {
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 10px 18px;
  background: var(--bg-panel);
  border-bottom: 1px solid var(--line);
}

.brand { display: flex; flex-direction: column; min-width: 220px; }
.brand-title { font-size: 16px; font-weight: 600; letter-spacing: .2px; }
.brand-sub { font-size: 11px; color: var(--text-dim); }

.totals { display: flex; gap: 28px; margin-left: auto; }
.total { display: flex; flex-direction: column; align-items: flex-end; }
.total-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .4px; }
.total-value { font-size: 18px; font-weight: 600; font-variant-numeric: tabular-nums; }

.modes { display: flex; gap: 4px; padding: 3px; background: var(--bg-elev); border-radius: 8px; }
.mode {
  border: 0; background: transparent; padding: 7px 14px; border-radius: 6px;
  color: var(--text-dim); font-size: 13px;
}
.mode:hover { color: var(--text); }
.mode.is-active { background: var(--accent); color: #06101c; font-weight: 600; }

/* ---------- Основная область ---------- */

main {
  display: grid;
  grid-template-columns: var(--sidebar-w) minmax(0, 1fr);
  min-height: 0;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  border-right: 1px solid var(--line);
  background: var(--bg-panel);
  min-height: 0;
}

.search-row { display: flex; gap: 6px; padding: 10px 12px 6px; }
#search {
  flex: 1; min-width: 0;
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 8px;
  color: var(--text); font: inherit; padding: 8px 10px; outline: none;
}
#search:focus { border-color: var(--accent); }

.icon-btn {
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 8px;
  color: var(--text-dim); padding: 6px 10px; line-height: 1;
}
.icon-btn:hover { color: var(--text); border-color: var(--accent); }

.sort-row { display: flex; gap: 4px; padding: 4px 12px 8px; }
.sort {
  flex: 1; border: 1px solid var(--line); background: var(--bg-elev); border-radius: 7px;
  padding: 6px 4px; font-size: 12px; color: var(--text-dim);
}
.sort.is-active { color: var(--text); border-color: var(--accent); background: #1d2a3d; }

.list-head {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 4px 14px 6px;
  font-size: 11px; color: var(--text-dim); border-bottom: 1px solid var(--line);
}

/* Кнопка «список на весь экран» нужна только там, где карта и список делят
   экран по высоте: на широком экране они и так видны одновременно. */
.list-full-btn {
  display: none;
  border: 1px solid var(--line); background: var(--bg-elev); border-radius: 6px;
  color: var(--text-dim); font-size: 11px; line-height: 1; padding: 5px 9px;
  white-space: nowrap; flex: 0 0 auto;
}
.list-full-btn:hover { color: var(--text); border-color: var(--accent); }

.country-list {
  margin: 0; padding: 4px 6px; list-style: none;
  overflow-y: auto; min-height: 0;
}

.row {
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr) 74px 56px;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.row.is-hidden { display: none; }
.row:hover, .row.is-hover { background: var(--bg-elev); }
.row.is-selected { background: #1d2f47; box-shadow: inset 2px 0 0 var(--accent); }

.row-rank { color: var(--text-dim); font-size: 11px; text-align: right; font-variant-numeric: tabular-nums; }
.row-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-pop { text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }
.row-rate { text-align: right; font-variant-numeric: tabular-nums; font-size: 12px; }

.up { color: var(--up); }
.down { color: var(--down); }
.flat { color: var(--flat); }

/* ---------- Карточка страны ---------- */

.detail {
  border-top: 1px solid var(--line);
  padding: 10px 12px 12px;
  background: var(--bg-elev);
  max-height: 44vh;
  overflow-y: auto;
}
.detail-actions { display: flex; gap: 6px; }
.detail-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.detail-name { font-size: 15px; font-weight: 600; }
.detail-region { font-size: 11px; color: var(--text-dim); }
.detail-grid {
  display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; margin-top: 10px;
}
.cell { display: flex; flex-direction: column; }
.cell span { font-size: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .3px; }
.cell b { font-size: 14px; font-variant-numeric: tabular-nums; font-weight: 600; }
.detail-notes {
  margin: 10px 0 0; padding-left: 16px; font-size: 11px; color: var(--text-dim);
}

/* ---------- Карта ---------- */

.map-pane { position: relative; min-width: 0; background: var(--ocean); }

#map { width: 100%; height: 100%; display: block; touch-action: none; cursor: grab; }
#map.is-dragging { cursor: grabbing; }

.country {
  /* заливку задаёт JS через атрибут fill: CSS-свойство перебило бы его */
  stroke: #0b0f16;
  stroke-width: .6;
  transition: opacity .12s ease;
}
.country.is-nodata { fill: #262c38; }
.country-dot { stroke: #0b0f16; stroke-width: .8; }
.country.is-hover { stroke: #fff; stroke-width: 1.4; }
.country.is-selected { stroke: var(--accent); stroke-width: 2; }
.has-search .country:not(.is-hit) { opacity: .28; }
.country.is-hit { stroke: #ffd166; stroke-width: 1.6; }

.map-controls {
  position: absolute; top: 12px; right: 12px; display: flex; flex-direction: column; gap: 6px;
}
.map-controls .icon-btn { width: 32px; height: 32px; padding: 0; }

.legend {
  position: absolute; left: 12px; bottom: 12px;
  background: rgba(21, 26, 35, .92);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; max-width: 260px;
  backdrop-filter: blur(4px);
}
.legend-title { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .4px; margin-bottom: 6px; }
.legend-items { display: flex; flex-direction: column; gap: 3px; font-size: 12px; }
.legend-item { display: flex; align-items: center; gap: 8px; }
.legend-item i { width: 16px; height: 12px; border-radius: 3px; display: inline-block; flex: 0 0 auto; }
.legend-short { display: none; }

.tooltip {
  position: absolute; top: 0; left: 0; pointer-events: none;
  background: rgba(21, 26, 35, .96);
  border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; min-width: 190px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
}
.tt-name { font-weight: 600; margin-bottom: 4px; }
.tt-row { display: flex; justify-content: space-between; gap: 14px; font-size: 12px; color: var(--text-dim); }
.tt-row b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- Таймлайн ---------- */

.timeline {
  background: var(--bg-panel);
  border-top: 1px solid var(--line);
  padding: 8px 20px 12px;
}
.timeline-inner { --frac: 0; }
.timeline-head { display: flex; align-items: baseline; gap: 12px; }
.timeline-label { font-size: 11px; color: var(--text-dim); text-transform: uppercase; letter-spacing: .4px; }
.timeline-year { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
#yearReset { font-size: 11px; padding: 4px 8px; }

.slider-wrap { position: relative; padding-top: 4px; }

#yearSlider {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 22px; background: transparent; cursor: pointer; margin: 0;
}
#yearSlider::-webkit-slider-runnable-track {
  height: 8px; border-radius: 4px;
  background: linear-gradient(90deg, var(--accent) 0 calc(var(--frac, 0) * 100%), #29303d 0);
}
#yearSlider::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 20px; height: 20px; margin-top: -6px; border-radius: 50%;
  background: #fff; border: 3px solid var(--accent); box-shadow: 0 2px 6px rgba(0, 0, 0, .5);
}
#yearSlider::-moz-range-track { height: 8px; border-radius: 4px; background: #29303d; }
#yearSlider::-moz-range-progress { height: 8px; border-radius: 4px; background: var(--accent); }
#yearSlider::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%; background: #fff; border: 3px solid var(--accent);
}
#yearSlider:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: 6px; }

.ticks { position: relative; height: 14px; margin-top: 2px; }
.tick {
  position: absolute; transform: translateX(-50%);
  font-size: 10px; color: var(--text-dim); font-variant-numeric: tabular-nums;
}
.tick::before {
  content: ""; position: absolute; left: 50%; top: -4px; width: 1px; height: 3px; background: var(--line);
}

/* ---------- Узкие экраны ---------- */

/* Планшеты и телефоны: карта сверху, список снизу.
   Треки — minmax(0, …), иначе в альбомной ориентации минимальные высоты
   выталкивают таймлайн за пределы экрана (у body overflow: hidden).
   Шапка одной строкой требует ~900px, поэтому здесь же убираются уточнения
   в значениях и фиксированная ширина названия — иначе подписи переносятся. */
@media (max-width: 900px) {
  main { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) minmax(0, 47%); }
  .sidebar { border-right: 0; border-top: 1px solid var(--line); order: 2; }
  .map-pane { order: 1; }
  /* Шапка в одну строку: показатели ужимаются первыми, название и режимы —
     нет; лишнее прячется в многоточие, а не переносится и не растит страницу. */
  .topbar { overflow: hidden; }
  .brand { min-width: 0; flex: 0 1 auto; }
  .brand-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .brand-sub { display: none; }          /* длинная подпись съедает место у показателей */
  .totals { gap: 14px; min-width: 0; flex: 1 1 auto; overflow: hidden; }
  .total { min-width: 0; }
  .total-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .total-value { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
  .total-extra { display: none; }
  .modes { flex: 0 0 auto; }
  .mode { white-space: nowrap; }
}

/* Телефоны: шапка в три строки (иначе flex-строка требует ~900px и уезжает
   за экран вместе с переключателем режимов), крупные цели для пальца. */
@media (max-width: 760px) {
  /* dvh: в мобильных браузерах панель адресной строки съедает часть 100% */
  html, body { height: 100dvh; }

  .topbar {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: stretch;
    gap: 6px;
    padding: 7px 12px;
  }
  .brand-title { font-size: 15px; }

  .totals { margin-left: 0; gap: 8px; justify-content: space-between; }
  .total { align-items: flex-start; flex: 1 1 0; min-width: 0; }
  .total-label { font-size: 9px; letter-spacing: .2px; }
  .total-value {
    font-size: 15px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%;
  }

  .modes { display: grid; grid-template-columns: 1fr 1fr; }
  .mode { padding: 9px 6px; text-align: center; }

  .search-row { padding: 6px 10px 4px; }
  /* 16px — при меньшем размере iOS Safari зумит страницу на фокусе поля */
  #search { padding: 10px; font-size: 16px; }
  .search-row .icon-btn { min-width: 42px; }
  .detail-actions .icon-btn, #sortDir { min-height: 36px; padding: 6px 12px; }
  .sort-row { padding: 4px 10px 6px; }
  .sort { padding: 8px 4px; }
  .list-head { padding: 2px 12px 4px; font-size: 10px; }
  #listHint { display: none; }

  .row { padding: 9px 8px; grid-template-columns: 26px minmax(0, 1fr) 70px 54px; }
  .detail { padding: 10px; }
  .detail-grid { gap: 7px 8px; }

  .map-controls { top: 8px; right: 8px; gap: 8px; }
  .map-controls .icon-btn { width: 38px; height: 38px; }

  .timeline { padding: 5px 12px 8px; }
  .timeline-year { font-size: 19px; }
  #yearSlider { height: 30px; }
  #yearSlider::-webkit-slider-thumb { width: 26px; height: 26px; margin-top: -9px; }
  #yearSlider::-moz-range-thumb { width: 22px; height: 22px; }
}

/* Легенда: колонка в полный рост не годится ни для узкого, ни для низкого
   экрана — вместо неё полоса понизу карты с короткими подписями и переносом
   по строкам (прокрутки нет, за краем ничего не прячется). */
@media (max-width: 760px), (max-height: 520px) {
  .legend { left: 8px; right: 8px; bottom: 8px; max-width: none; padding: 6px 8px; }
  .legend-title { margin-bottom: 4px; font-size: 10px; }
  .legend-items { flex-direction: row; flex-wrap: wrap; gap: 4px 10px; }
  .legend-item { flex: 0 0 auto; gap: 5px; font-size: 11px; white-space: nowrap; }
  .legend-full { display: none; }
  .legend-short { display: inline; }

  /* Панели мало по высоте: карточка страны и список не помещаются вместе,
     поэтому карточка встаёт на место списка (закрытие — ✕ или Esc). */
  .detail { max-height: none; }
  body.has-detail .list-head,
  body.has-detail .country-list { display: none; }
  body.has-detail .sidebar { grid-template-rows: auto auto minmax(0, 1fr); }

  /* Список на весь экран: карта убирается, панель занимает всю область.
     Высоты снова хватает обоим, поэтому карточка страны не вытесняет список. */
  .list-full-btn { display: inline-flex; align-items: center; min-height: 28px; }
  body.is-list-full .map-pane { display: none; }
  body.is-list-full main { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
  body.is-list-full.has-detail .list-head { display: flex; }
  body.is-list-full.has-detail .country-list { display: block; }
  /* Долю карточке задаёт трек грида: max-height в процентах считался бы от
     самого трека (то есть от содержимого карточки) и оставлял пустоту. */
  body.is-list-full.has-detail .sidebar { grid-template-rows: auto auto auto minmax(0, 1fr) minmax(0, 45%); }
}

/* Очень низкий экран (SE и подобные): название приложения уступает место списку */
@media (max-width: 760px) and (max-height: 660px) and (orientation: portrait) {
  .brand { display: none; }
  main { grid-template-rows: minmax(0, 1fr) minmax(0, 52%); }
}

/* Телефон в альбомной ориентации: высоты мало — список возвращается вбок */
@media (max-width: 900px) and (max-height: 520px) and (orientation: landscape) {
  main { grid-template-columns: 280px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
  .sidebar { order: 1; border-top: 0; border-right: 1px solid var(--line); }
  .map-pane { order: 2; }
  .row { grid-template-columns: 24px minmax(0, 1fr) 62px 46px; gap: 4px; }
  /* Высоты мало: шапка ужимается в одну строку без названия приложения */
  .topbar { grid-template-columns: minmax(0, 1fr) auto; gap: 4px 12px; padding: 5px 12px; }
  .brand { display: none; }
  .totals { grid-column: 1; }
  .modes { grid-column: 2; grid-row: 1; }
  .sidebar > * { min-width: 0; }          /* узкая колонка: содержимое не расталкивает её */
  .sort { min-width: 0; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #listHint { display: none; }
  .timeline { padding: 4px 14px 6px; }
}

/* Устройства без мыши: hover-эффекты только мешают (залипают после тапа) */
@media (hover: none) {
  .row:hover { background: transparent; }
  .row.is-hover { background: var(--bg-elev); }
  .row.is-selected, .row.is-selected:hover { background: #1d2f47; }
}
