:root {
  --ink: #20303f;
  --muted: #6b7a89;
  --line: #dde3ea;
  --bg: #f4f6f8;
  --panel: #ffffff;
  --accent: #2f6fd0;
  --accent-ink: #12386b;
  --go: #1f7a44;
  --skip: #a33131;
  --warn: #b8730f;
}

* { box-sizing: border-box; }

/*
  Сторінку відкриває iframe фіксованої висоти, тож прокручуватись має НЕ документ,
  а внутрішні списки. Для цього висота задається жорстко, а не min-height:
  з min-height внутрішній overflow не вмикається взагалі - список кандидатів
  розтягував увесь документ, а карта лишалась на своїй висоті, і під нею
  зʼявлялась порожнеча на весь залишок прокрутки
*/
html, body { height: 100%; }

body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font: 14px/1.45 "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

.shell { display: flex; height: 100%; min-height: 0; }

/* Ліве меню */
.menu {
  width: 210px;
  flex: 0 0 210px;
  min-height: 0;
  overflow-y: auto;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 18px 0;
  display: flex;
  flex-direction: column;
}
.brand {
  padding: 0 18px 18px;
  font-weight: 600;
  font-size: 15px;
  line-height: 1.25;
}
.brand span { color: var(--muted); font-weight: 400; font-size: 12px; }
.menu-item {
  appearance: none;
  border: 0;
  background: transparent;
  text-align: left;
  padding: 10px 18px;
  font: inherit;
  color: var(--ink);
  cursor: pointer;
  border-left: 3px solid transparent;
}
.menu-item:hover { background: #f0f4f9; }
.menu-item.is-active {
  border-left-color: var(--accent);
  background: #eef4fd;
  font-weight: 600;
}
.menu-foot {
  margin-top: auto;
  padding: 12px 18px 0;
  font-size: 11px;
  color: var(--muted);
  border-top: 1px solid var(--line);
}

.content { flex: 1; min-width: 0; min-height: 0; display: flex; flex-direction: column; }
.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.is-active { display: flex; }
.screen h1 { font-size: 18px; margin: 18px 20px 10px; }

/* Форма */
.setup {
  display: flex;
  gap: 12px;
  align-items: flex-end;
  padding: 14px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.field { display: flex; flex-direction: column; gap: 4px; flex: 1 1 220px; position: relative; }
.field-narrow { flex: 0 0 200px; }
.field-check { flex: 0 0 auto; padding-bottom: 8px; }
.field label { font-size: 12px; color: var(--muted); }
.field input, .field select {
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
  background: #fff;
}
.field input:focus, .field select:focus { outline: 2px solid #c7dcfb; border-color: var(--accent); }

button.primary {
  appearance: none;
  border: 0;
  background: var(--accent);
  color: #fff;
  font: inherit;
  font-weight: 600;
  padding: 9px 18px;
  border-radius: 4px;
  cursor: pointer;
}
button.primary:hover { background: var(--accent-ink); }
button.primary:disabled { background: #a9bcd6; cursor: default; }

.suggest {
  position: absolute;
  top: 100%;
  left: 0; right: 0;
  z-index: 1200;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px;
  max-height: 260px;
  overflow-y: auto;
  box-shadow: 0 6px 18px rgba(32, 48, 63, .12);
}
.suggest button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  font: inherit;
  padding: 8px 10px;
  cursor: pointer;
  border-bottom: 1px solid #f0f3f6;
}
.suggest button:hover { background: #eef4fd; }
.suggest .sub { display: block; font-size: 11px; color: var(--muted); }

/* Робоча область */
.workspace { flex: 1; display: flex; min-height: 0; }
.map-wrap { flex: 1; min-width: 0; position: relative; }
#map { position: absolute; inset: 0; }
.map-note {
  position: absolute;
  left: 12px; bottom: 12px;
  z-index: 500;
  background: rgba(255,255,255,.92);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 10px;
  font-size: 12px;
}

.side {
  width: 400px;
  flex: 0 0 400px;
  border-left: 1px solid var(--line);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.totals {
  flex: 0 0 auto;
  padding: 10px 14px;
  background: #eef4fd;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
}
.totals b { font-size: 15px; }
.totals .warn { color: var(--warn); display: block; margin-top: 4px; }
.list-head {
  flex: 0 0 auto;
  display: flex;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--line);
  font-weight: 600;
}
.muted { color: var(--muted); font-weight: 400; }
.list { flex: 1; overflow-y: auto; }
.list.wide { padding: 0 20px 20px; }
.empty { color: var(--muted); padding: 18px 14px; }

.card {
  padding: 11px 14px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.card:hover { background: #f7fafd; }
.card.is-active { background: #eef4fd; }
.card.is-taken { background: #f0f8f3; }
.card-top { display: flex; justify-content: space-between; gap: 8px; align-items: baseline; }
.card-name { font-weight: 600; }
.card-amount { color: var(--muted); white-space: nowrap; font-size: 12px; }
.card-line { font-size: 12px; color: var(--muted); margin-top: 2px; }
.card-why { font-size: 12px; margin-top: 5px; color: var(--accent-ink); }
.tag {
  display: inline-block;
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 9px;
  background: #eef1f5;
  color: var(--muted);
  margin-right: 4px;
}
.tag.active { background: #e6f4ec; color: var(--go); }
.tag.rough { background: #fdf2e3; color: var(--warn); }
.card-buttons { display: flex; gap: 6px; margin-top: 8px; }
.card-buttons button {
  appearance: none;
  font: inherit;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 4px;
  border: 1px solid var(--line);
  background: #fff;
  cursor: pointer;
}
.card-buttons .go.is-on { background: var(--go); border-color: var(--go); color: #fff; }
.card-buttons .skip.is-on { background: var(--skip); border-color: var(--skip); color: #fff; }
.card-buttons a { font-size: 12px; margin-left: auto; align-self: center; color: var(--accent); }

.actions { flex: 0 0 auto; padding: 12px 14px; border-top: 1px solid var(--line); }
.actions button { width: 100%; }

.trip {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 12px 14px;
  margin-bottom: 10px;
}
.trip h3 { margin: 0 0 4px; font-size: 14px; }
.trip .meta { font-size: 12px; color: var(--muted); }
.trip a { color: var(--accent); font-size: 13px; }

.toast {
  position: fixed;
  left: 50%; bottom: 22px;
  transform: translateX(-50%);
  background: var(--ink);
  color: #fff;
  padding: 10px 18px;
  border-radius: 5px;
  z-index: 2400;
  max-width: 70vw;
}
.toast.bad { background: var(--skip); }

[hidden] { display: none !important; }

/* Форма у два ряди: маршрут зверху, зріз візитів під ним */
.setup { flex-direction: column; align-items: stretch; gap: 10px; }
.setup-row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }

/* Поле точки маршруту: пошук візиту чи адреси плюс вхід у реєстр */
.place { display: flex; gap: 6px; }
.place input { flex: 1; min-width: 0; }
.pick {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 4px;
  font: inherit;
  font-size: 12px;
  padding: 0 10px;
  color: var(--accent);
  cursor: pointer;
  white-space: nowrap;
}
.pick:hover { background: #eef4fd; border-color: var(--accent); }

.picked {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--muted);
  padding-top: 2px;
}
.picked-kind {
  background: #e6f4ec;
  color: var(--go);
  border-radius: 9px;
  padding: 1px 7px;
  font-weight: 600;
}
.picked .sub { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.picked .clear {
  appearance: none;
  border: 0;
  background: none;
  color: var(--muted);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  margin-left: auto;
  padding: 0 2px;
}
.picked .clear:hover { color: var(--skip); }

.suggest-group {
  padding: 6px 10px 3px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--muted);
  background: #f7fafd;
}
.suggest-item { display: block; width: 100%; text-align: left; }
.suggest-item.is-active { background: #eef4fd; }
.suggest-more {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  border-top: 1px solid var(--line);
  background: #fff;
  font: inherit;
  font-size: 12px;
  color: var(--accent);
  padding: 8px 10px;
  cursor: pointer;
}
.suggest-more:hover { background: #eef4fd; }

/* Зріз візитів, у якому шукаємо кандидатів */
.filters {
  align-items: center;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--line);
  font-size: 12px;
}
.filters-label { color: var(--muted); }
.filter-inline { color: var(--muted); display: flex; align-items: center; gap: 5px; }
.filters select {
  padding: 5px 8px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
  font-size: 12px;
  background: #fff;
  max-width: 260px;
}

.dropdown { position: relative; }
.dropdown-toggle {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 4px;
  font: inherit;
  font-size: 12px;
  padding: 5px 10px;
  cursor: pointer;
}
.dropdown-toggle:hover { border-color: var(--accent); }
.dropdown-menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 1200;
  min-width: 260px;
  max-height: 300px;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: 0 6px 18px rgba(32, 48, 63, .12);
}
.dropdown-actions { display: flex; gap: 6px; padding: 8px; border-bottom: 1px solid var(--line); }
.dropdown-actions button {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 4px;
  font: inherit;
  font-size: 11px;
  padding: 3px 8px;
  cursor: pointer;
}
.check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  cursor: pointer;
}
.check:hover { background: #f7fafd; }
.check span:nth-of-type(1) { flex: 1; }

/* Реєстр візитів окремим вікном */
.modal { position: fixed; inset: 0; z-index: 1800; display: flex; align-items: center; justify-content: center; }
.modal-backdrop { position: absolute; inset: 0; background: rgba(32, 48, 63, .45); }
.modal-box {
  position: relative;
  width: min(820px, 92vw);
  max-height: 84vh;
  background: var(--panel);
  border-radius: 8px;
  box-shadow: 0 18px 50px rgba(32, 48, 63, .3);
  display: flex;
  flex-direction: column;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
}
.modal-head h2 { margin: 0; font-size: 16px; }
.modal-head .icon {
  appearance: none;
  border: 0;
  background: none;
  font-size: 22px;
  line-height: 1;
  color: var(--muted);
  cursor: pointer;
}
.modal-tools {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.modal-tools input[type="text"], #registrySearch {
  flex: 1 1 280px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 4px;
  font: inherit;
}
.modal-list { flex: 1; overflow-y: auto; min-height: 120px; }
.modal-foot { padding: 10px 18px; border-top: 1px solid var(--line); font-size: 12px; }

.reg-row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 9px 18px;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.reg-row:hover { background: #eef4fd; }
.reg-main { flex: 1; min-width: 0; }
.reg-name { font-weight: 600; display: block; }
.reg-row .sub { font-size: 11px; color: var(--muted); display: block; }
.reg-meta { flex: 0 0 200px; text-align: right; }
.reg-row a { font-size: 12px; color: var(--accent); flex: 0 0 auto; }

/*
  Форма лежить над картою: керування масштабом Leaflet сидить на z-index 1000,
  і без власного контексту накладання випадайки ховались під кнопками зуму
*/
/* flex: 0 0 auto - форма ніколи не стискається під тиском карти і списку */
.setup { position: relative; z-index: 1100; flex: 0 0 auto; }

/* Вибір точки кліком по карті */
.map-tools {
  position: absolute;
  top: 10px; right: 10px;
  z-index: 700;
  display: flex;
  gap: 6px;
}
.map-pick {
  appearance: none;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, .94);
  border-radius: 4px;
  font: inherit;
  font-size: 12px;
  padding: 6px 10px;
  cursor: pointer;
  box-shadow: 0 1px 4px rgba(32, 48, 63, .18);
}
.map-pick:hover { border-color: var(--accent); color: var(--accent); }
.map-pick.is-on { background: var(--accent); border-color: var(--accent); color: #fff; }

body.is-picking .leaflet-container { cursor: crosshair; }

.pick-popup .leaflet-popup-content { margin: 8px 10px; min-width: 240px; }
.pick-head { font-size: 12px; color: var(--muted); padding: 4px 0; }
.pick-list button {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  border-top: 1px solid var(--line);
  background: none;
  font: inherit;
  font-size: 13px;
  padding: 7px 4px;
  cursor: pointer;
}
.pick-list button:first-child { border-top: 0; }
.pick-list button:hover { background: #eef4fd; }
.pick-list .sub { display: block; font-size: 11px; color: var(--muted); }

/* Пояснення "чому саме він" */
.why-toggle {
  appearance: none;
  border: 0;
  background: none;
  font: inherit;
  font-size: 12px;
  color: var(--accent);
  padding: 4px 0 0;
  cursor: pointer;
}
.why-toggle:hover { text-decoration: underline; }
.card-reasons {
  margin-top: 6px;
  padding: 8px 10px;
  background: #f7fafd;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-size: 12px;
}
.card-reasons ul { margin: 0 0 6px; padding-left: 16px; }
.card-reasons li { margin-bottom: 2px; }
.card-reasons dl {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
}
.card-reasons dt { color: var(--muted); }
.card-reasons dd { margin: 0; }

/* Другорядна дія поруч з головною кнопкою */
button.ghost {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  font: inherit;
  padding: 9px 14px;
  border-radius: 4px;
  cursor: pointer;
}
button.ghost:hover { border-color: var(--skip); color: var(--skip); }

/* Дві дії підряд під списком кандидатів */
.actions { display: flex; flex-direction: column; gap: 8px; }
.actions button { width: 100%; }
.actions button.primary:disabled { background: #a9bcd6; cursor: default; }
