/* Карта участков НСПД — Telegram Mini App. Цвета берутся из темы Telegram (--tg-theme-*). */

:root {
  --fb-bg: #ffffff;
  --fb-text: #1c1c1e;
  --fb-hint: #8e8e93;
  --fb-link: #2481cc;
  --fb-btn: #2481cc;
  --fb-btn-text: #ffffff;
  --fb-bg2: #f1f1f4;
  --fb-sep: rgba(0, 0, 0, 0.09);
  --fb-danger: #e53935;

  --bg: var(--tg-theme-bg-color, var(--fb-bg));
  --text: var(--tg-theme-text-color, var(--fb-text));
  --hint: var(--tg-theme-hint-color, var(--fb-hint));
  --link: var(--tg-theme-link-color, var(--fb-link));
  --btn: var(--tg-theme-button-color, var(--fb-btn));
  --btn-text: var(--tg-theme-button-text-color, var(--fb-btn-text));
  --bg2: var(--tg-theme-secondary-bg-color, var(--fb-bg2));
  --section: var(--tg-theme-section-bg-color, var(--bg));
  --sep: var(--tg-theme-section-separator-color, var(--fb-sep));
  --accent: var(--tg-theme-accent-text-color, var(--link));
  --danger: var(--tg-theme-destructive-text-color, var(--fb-danger));

  --green: #1f9d48;
  --green-fill: #34c759;
  --lime: #8aa61c;
  --lime-fill: #c6e04a;
  --grey: #8e8e93;
  --zone: #e5383b;
  --warn: #e69100;

  --radius: 14px;
  --sheet-h: 46vh;
  --vh: var(--tg-viewport-stable-height, 100vh);
  --safe-top: var(--tg-content-safe-area-inset-top, env(safe-area-inset-top, 0px));
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not(.light) {
    --fb-bg: #1c1c1e;
    --fb-text: #f2f2f7;
    --fb-hint: #8e8e93;
    --fb-link: #5aa9ff;
    --fb-btn: #3a8ee6;
    --fb-bg2: #000000;
    --fb-sep: rgba(255, 255, 255, 0.12);
    --fb-danger: #ff6961;
    color-scheme: dark;
  }
}

:root.dark { color-scheme: dark; }
:root.dark .leaflet-tile-pane { filter: brightness(0.82) contrast(1.05); }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  overflow: hidden;
  overscroll-behavior: none;
}

#app { position: fixed; inset: 0; height: var(--vh); }
#map { position: absolute; inset: 0; background: var(--bg2); }

button { font: inherit; color: inherit; }
input, select, textarea { font: inherit; color: var(--text); }

.ico {
  width: 20px; height: 20px; flex: none;
  fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.ico .fill { fill: currentColor; }

/* ---------------------------------------------------------------- верх */
.topbar {
  position: absolute; z-index: 1000;
  top: calc(var(--safe-top) + 10px); left: 10px; right: 10px;
  display: flex; gap: 8px;
}
.searchbox {
  flex: 1; display: flex; align-items: center; gap: 6px;
  height: 44px; padding: 0 6px 0 12px;
  background: var(--bg); border-radius: 22px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  color: var(--hint);
}
.searchbox input {
  flex: 1; min-width: 0; height: 100%;
  border: 0; outline: 0; background: transparent; font-size: 15px;
}
.searchbox input::-webkit-search-cancel-button { display: none; }

.fab {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: var(--bg); color: var(--text);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.18);
  cursor: pointer;
}
.fab:active { transform: scale(0.94); }
.fab.on { color: var(--btn); }

.fab-col {
  position: absolute; z-index: 900; right: 10px;
  bottom: calc(var(--sheet-h) + 14px);
  display: flex; flex-direction: column; gap: 10px;
  transition: bottom 0.2s ease;
}

.icon-btn { border: 0; background: none; padding: 6px; border-radius: 50%; color: var(--hint); cursor: pointer; display: grid; }
.icon-btn.small .ico { width: 16px; height: 16px; }

.dropdown {
  position: absolute; z-index: 1001;
  top: calc(var(--safe-top) + 60px); left: 10px; right: 10px;
  max-height: 50vh; overflow: auto;
  background: var(--bg); border-radius: var(--radius);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.22);
  padding: 6px 0;
}
.dropdown.layers { left: auto; width: 270px; }
.dropdown .row { display: flex; align-items: center; gap: 10px; padding: 10px 14px; cursor: pointer; }
.dropdown .row:active { background: var(--bg2); }
.dropdown .row .sub { color: var(--hint); font-size: 13px; }
.dropdown .row > div { min-width: 0; }
.dropdown .row .name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.pill {
  position: absolute; z-index: 1000;
  top: calc(var(--safe-top) + 62px); left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 8px;
  padding: 7px 14px; border-radius: 20px;
  background: var(--btn); color: var(--btn-text); font-size: 14px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2); cursor: pointer; white-space: nowrap;
}

/* ---------------------------------------------------------------- нижняя панель */
.sheet {
  position: absolute; z-index: 950; left: 0; right: 0; bottom: 0;
  height: var(--sheet-h);
  display: flex; flex-direction: column;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.16);
  transition: height 0.2s ease;
}
.sheet.dragging, .sheet.dragging ~ .fab-col { transition: none; }
#app.sheet-full .fab-col, #app.sheet-full .leaflet-bottom, #app.sheet-full .leaflet-control-zoom { visibility: hidden; }
.sheet-handle { flex: none; height: 22px; display: grid; place-items: center; cursor: grab; touch-action: none; }
.sheet-handle span { width: 38px; height: 5px; border-radius: 3px; background: var(--sep); filter: contrast(1.6); }

.tabs { flex: none; display: flex; gap: 4px; padding: 0 10px 8px; border-bottom: 1px solid var(--sep); }
.tab {
  flex: 1; position: relative;
  border: 0; background: none; padding: 8px 2px; border-radius: 10px;
  color: var(--hint); font-size: 14px; font-weight: 500; cursor: pointer; white-space: nowrap;
}
.tab.active { color: var(--btn); background: color-mix(in srgb, var(--btn) 12%, transparent); }

.sheet-body { flex: 1; overflow-x: hidden; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 10px 14px calc(18px + var(--safe-bottom)); }
.pane { display: none; }
.pane.active { display: block; }

/* ---------------------------------------------------------------- режимы зоны */
.modes { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; margin: 0 -14px; padding-left: 14px; padding-right: 14px; scrollbar-width: none; }
.modes::-webkit-scrollbar { display: none; }
.mode {
  flex: none; display: flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: 12px;
  border: 1px solid var(--sep); background: var(--section);
  font-size: 14px; cursor: pointer; white-space: nowrap;
}
.mode .ico { width: 18px; height: 18px; color: var(--hint); }
.mode.active { border-color: var(--btn); color: var(--btn); background: color-mix(in srgb, var(--btn) 10%, transparent); }
.mode.active .ico { color: var(--btn); }

.hint { margin: 10px 0 8px; color: var(--hint); font-size: 14px; min-height: 1.4em; }
.hint.warn { color: var(--warn); }

.mode-panel { display: flex; flex-direction: column; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 13px; color: var(--hint); }

.stepper { display: flex; align-items: center; gap: 6px; }
.stepper input {
  width: 96px; height: 40px; padding: 0 10px; text-align: center;
  border: 1px solid var(--sep); border-radius: 10px; background: var(--section); font-size: 17px; font-weight: 600;
  -moz-appearance: textfield;
}
.stepper.wide input { width: 120px; text-align: left; }
.stepper input::-webkit-inner-spin-button, .stepper input::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
.stepper .unit { color: var(--hint); }
.step {
  width: 40px; height: 40px; border-radius: 10px; border: 0;
  background: var(--bg2); font-size: 22px; line-height: 1; cursor: pointer;
}
.step:active { filter: brightness(0.92); }

.slider { width: 100%; accent-color: var(--btn); margin: 2px 0; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--sep); background: var(--section);
  padding: 5px 11px; border-radius: 16px; font-size: 13px; cursor: pointer; white-space: nowrap;
}
.chip.active { background: var(--btn); color: var(--btn-text); border-color: var(--btn); }
.chip .n { opacity: 0.7; margin-left: 4px; }

.coords { font-size: 13px; color: var(--hint); }
.coords b { color: var(--text); font-weight: 500; font-variant-numeric: tabular-nums; }

.btn-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 4px; }
.btn-row.end { justify-content: flex-end; }
.btn {
  border: 0; border-radius: 11px; padding: 9px 14px;
  background: var(--btn); color: var(--btn-text); font-size: 14px; font-weight: 500; cursor: pointer;
}
.btn.ghost { background: var(--bg2); color: var(--text); }
.btn.primary { background: var(--btn); color: var(--btn-text); }
.btn.danger { background: color-mix(in srgb, var(--danger) 14%, transparent); color: var(--danger); }
.btn.wide { width: 100%; margin-top: 8px; padding: 12px; }
.btn:disabled { opacity: 0.5; }
.btn:active { filter: brightness(0.93); }

.inline-form { display: flex; gap: 8px; }
.inline-form input {
  flex: 1; min-width: 0; height: 42px; padding: 0 12px;
  border: 1px solid var(--sep); border-radius: 11px; background: var(--section);
}

.summary {
  margin: 12px 0 4px; padding: 10px 12px; border-radius: 12px; background: var(--bg2);
  display: flex; flex-wrap: wrap; gap: 4px 14px; align-items: baseline;
}
.summary:empty { display: none; }
.summary .big { font-size: 18px; font-weight: 600; }
.summary .warn { color: var(--warn); width: 100%; font-size: 13px; }
.summary .err { color: var(--danger); width: 100%; font-size: 13px; }

.card { margin-top: 12px; padding: 12px; border-radius: 14px; background: var(--section); border: 1px solid var(--sep); }
.card > summary { cursor: pointer; font-weight: 600; list-style: none; display: flex; align-items: center; gap: 8px; }
.card > summary::-webkit-details-marker { display: none; }
.card > summary::after { content: "›"; margin-left: auto; color: var(--hint); font-size: 20px; transform: rotate(90deg); transition: transform 0.2s; }
.card[open] > summary::after { transform: rotate(-90deg); }
.card-title { font-weight: 600; margin-bottom: 4px; }
.rules-body { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.group-title { margin-top: 6px; font-size: 13px; color: var(--hint); text-transform: uppercase; letter-spacing: 0.03em; }
.check { display: flex; align-items: center; gap: 10px; padding: 4px 0; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--btn); }

.badge {
  display: inline-block; min-width: 18px; padding: 0 6px; border-radius: 9px;
  background: var(--btn); color: var(--btn-text); font-size: 12px; font-weight: 600; line-height: 18px; text-align: center;
}
.tab .badge { position: absolute; top: -6px; right: 0; font-size: 10px; line-height: 15px; min-width: 15px; padding: 0 4px; }

.muted { color: var(--hint); }
.small { font-size: 13px; }
.empty { padding: 18px 4px; text-align: center; }
.warn { color: var(--warn); font-size: 13px; margin: 8px 0; }

/* ---------------------------------------------------------------- результаты */
.res-head { display: flex; align-items: flex-start; gap: 10px; }
.res-title { font-size: 17px; font-weight: 600; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; margin: 8px 0; font-size: 12px; color: var(--hint); }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; border: 2px solid; }
.sw.parcel { border-color: #0a63ff; }
.sw.active { border-color: #ff8a00; }
.sw.excluded { border-color: #0a63ff; border-style: dashed; opacity: 0.7; }
.filters { margin: 8px 0; }
.res-tools { display: flex; gap: 8px; margin-bottom: 8px; }
.res-tools select {
  flex: 1; min-width: 0; height: 38px; padding: 0 10px;
  border: 1px solid var(--sep); border-radius: 10px; background: var(--section);
}

.list { display: flex; flex-direction: column; }
.list-head, .group-head { display: flex; align-items: baseline; gap: 6px; padding: 12px 4px 4px; border-bottom: 1px solid var(--sep); }
.list-head { font-size: 15px; font-weight: 600; padding-top: 16px; }
.group-head { font-size: 13px; font-weight: 600; color: var(--hint); }
.list-head .n, .group-head .n { margin-left: auto; font-weight: 400; color: var(--hint); font-variant-numeric: tabular-nums; }
.item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 4px; border-bottom: 1px solid var(--sep); cursor: pointer;
}
.item:active, .item.sel { background: color-mix(in srgb, var(--btn) 9%, transparent); }
.item .num { flex: none; min-width: 36px; color: var(--link); font-variant-numeric: tabular-nums; font-size: 14px; }
.item .main { flex: 1; min-width: 0; }
.item .cad { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; }
.item .sub { font-size: 12px; color: var(--hint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub .own { font-weight: 600; color: var(--text); }
.item .area { flex: none; font-weight: 600; font-size: 14px; font-variant-numeric: tabular-nums; }
.item .dot { flex: none; width: 10px; height: 10px; border-radius: 3px; }
.dot.public { background: var(--green-fill); }
.dot.unspecified { background: var(--lime-fill); }
.dot.excluded { background: var(--grey); }
.more-btn { margin: 10px auto; }

.parcel-card { position: relative; }
.parcel-card .pc-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.parcel-card .pc-cad { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 16px; font-weight: 600; }
.parcel-card .pc-close { position: absolute; top: 6px; right: 6px; }
/* Колонка названий не шире 45%: длинные названия правил переносятся, а не растягивают панель вбок */
.kv { display: grid; grid-template-columns: fit-content(45%) minmax(0, 1fr); gap: 4px 12px; margin-top: 8px; font-size: 13px; }
.kv dt, .kv .k { color: var(--hint); overflow-wrap: anywhere; }
.kv dd, .kv .v { margin: 0; overflow-wrap: anywhere; }
.status { display: inline-block; padding: 1px 8px; border-radius: 8px; font-size: 12px; font-weight: 600; }
.status.public { background: color-mix(in srgb, var(--green-fill) 25%, transparent); color: var(--green); }
.status.unspecified { background: color-mix(in srgb, var(--lime-fill) 35%, transparent); color: var(--lime); }
.status.excluded { background: color-mix(in srgb, var(--grey) 20%, transparent); color: var(--grey); }

.hist { padding: 10px 4px; border-bottom: 1px solid var(--sep); cursor: pointer; }
.hist:active { background: var(--bg2); }
.hist .t { display: flex; justify-content: space-between; gap: 8px; font-weight: 600; }
.hist .d { font-size: 13px; color: var(--hint); margin-top: 2px; }

.imp { padding: 10px 0; border-bottom: 1px solid var(--sep); }
.imp:last-child { border-bottom: 0; }
.imp .meta { font-size: 12px; color: var(--hint); }
.imp .btn { margin-top: 6px; padding: 6px 10px; font-size: 13px; }
.help { padding-left: 18px; margin: 8px 0 0; font-size: 14px; }
.help li { margin-bottom: 6px; }

/* ---------------------------------------------------------------- диалоги */
.overlay {
  position: absolute; inset: 0; z-index: 2000;
  display: grid; place-items: center; padding: 20px;
  background: rgba(0, 0, 0, 0.35);
}
.overlay[hidden] { display: none; }
.dialog { width: min(420px, 100%); padding: 18px; border-radius: 18px; background: var(--bg); box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3); }
.dialog-title { display: flex; align-items: center; gap: 10px; font-size: 17px; font-weight: 600; margin-bottom: 8px; }
.dialog textarea {
  width: 100%; padding: 10px; border: 1px solid var(--sep); border-radius: 12px; background: var(--section);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; resize: vertical;
}
.job-progress { min-height: 2.8em; margin: 6px 0; }

.spinner {
  width: 16px; height: 16px; flex: none; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: spin 0.8s linear infinite;
}
.spinner.big { width: 22px; height: 22px; color: var(--btn); }
.spinner.done { animation: none; border-color: var(--green); }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: absolute; z-index: 3000; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--sheet-h) + 16px);
  max-width: calc(100% - 32px); padding: 10px 16px; border-radius: 12px;
  background: rgba(30, 30, 32, 0.92); color: #fff; font-size: 14px; text-align: center;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

/* ---------------------------------------------------------------- Leaflet */
.leaflet-container { font: inherit; background: var(--bg2); }
.leaflet-bottom { bottom: var(--sheet-h); transition: bottom 0.2s ease; }
.leaflet-top { top: calc(var(--safe-top) + 64px); }
.leaflet-control-attribution { font-size: 10px; background: rgba(255, 255, 255, 0.75) !important; }
.leaflet-control-zoom { display: none; }
@media (hover: hover) and (pointer: fine) { .leaflet-control-zoom { display: block; } }
.leaflet-popup-content-wrapper { border-radius: 14px; background: var(--bg); color: var(--text); }
.leaflet-popup-tip { background: var(--bg); }
.leaflet-popup-content { margin: 12px 14px; font-size: 14px; line-height: 1.4; }
.leaflet-popup-content .btn { margin-top: 6px; padding: 7px 10px; font-size: 13px; }
.popup-cad { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 600; }

.vtx {
  display: grid; place-items: center;
  width: 26px; height: 26px; border-radius: 50%;
  background: #fff; border: 2.5px solid var(--zone); color: #222;
  font-size: 12px; font-weight: 700; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}
.vtx.center { background: var(--zone); color: #fff; }
.vtx.handle { width: 22px; height: 22px; background: #fff; border-color: var(--zone); }
.vtx.handle::after { content: "↔"; font-size: 12px; color: var(--zone); }
.mid {
  width: 16px; height: 16px; border-radius: 50%;
  background: rgba(255, 255, 255, 0.85); border: 2px dashed var(--zone);
}
.me-dot { width: 16px; height: 16px; border-radius: 50%; background: #2f80ed; border: 3px solid #fff; box-shadow: 0 0 0 6px rgba(47, 128, 237, 0.25); }
