:root {
  --bg: #f1f5f9;
  --panel: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #4338ca;
  --accent-ink: #ffffff;
  --surface-muted: #f8fafc;
  --hover: #f8fafc;
  --selected-bg: #fef2f2;
  --map-bg: #e2e8f0;
  --overlay: rgba(255, 255, 255, 0.94);
  --shadow: rgba(15, 23, 42, 0.08);
  --radius: 10px;
  color-scheme: light;
}

:root[data-theme="dark"] {
  --bg: #0b1120;
  --panel: #111827;
  --ink: #e5e7eb;
  --muted: #94a3b8;
  --line: #334155;
  --accent: #a5b4fc;
  --accent-ink: #111827;
  --surface-muted: #172033;
  --hover: #1e293b;
  --selected-bg: #3f1d2b;
  --map-bg: #0f172a;
  --overlay: rgba(17, 24, 39, 0.94);
  --shadow: rgba(0, 0, 0, 0.32);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  font-family: "Segoe UI", "Noto Sans", "Arial", sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
}

.layout {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100%;
}

/* ------------------------------------------------------------------ sidebar */
.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding-bottom: 24px;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--line);
  background: linear-gradient(135deg, #312e81, #4338ca);
  color: #fff;
}

.theme-button {
  flex: none;
  padding: 7px 10px;
  border: 1px solid rgba(255, 255, 255, 0.42);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 11px;
  cursor: pointer;
}

.theme-button:hover {
  background: rgba(255, 255, 255, 0.2);
}

.brand h1 {
  margin: 0;
  font-size: 18px;
  letter-spacing: -0.01em;
}

.brand p {
  margin: 4px 0 0;
  font-size: 12px;
  opacity: 0.8;
}

.panel {
  padding: 16px 20px;
  border-bottom: 1px solid var(--line);
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.step-hint {
  margin: -2px 0 10px;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.4;
}

/* --------------------------------------------------------------------- tabs */
.tabs {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.tab {
  --tab-color: var(--accent);
  display: grid;
  grid-template-columns: 26px 1fr;
  align-items: center;
  gap: 8px;
  text-align: left;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  font: inherit;
  color: var(--ink);
  cursor: pointer;
  border-left: 4px solid var(--tab-color);
  transition: background 0.15s, box-shadow 0.15s;
}

.step-number {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  color: #fff;
  background: var(--tab-color);
  font-size: 12px;
  font-weight: 700;
}

.tab:hover {
  background: var(--hover);
}

.tab.is-active {
  background: color-mix(in srgb, var(--tab-color) 14%, var(--panel));
  box-shadow: inset 0 0 0 1px var(--tab-color);
  font-weight: 600;
}

.tab.is-empty {
  color: #94a3b8;
  border-left-color: #cbd5e1;
  cursor: help;
}

.tab.is-locked {
  color: #94a3b8;
  border-left-color: #cbd5e1;
  background: var(--surface-muted);
  cursor: not-allowed;
}

.tab.is-locked .step-number,
.tab.is-empty .step-number {
  background: #cbd5e1;
}

.tab:disabled {
  opacity: 0.7;
}

.tab.is-locked::after {
  content: " · эхлээд дээрх түвшнээс сонгоно уу";
  font-size: 11px;
  grid-column: 2;
}

.tab.is-empty::after {
  grid-column: 2;
}

.tab.is-empty::after {
  content: " · өгөгдөл ороогүй";
  font-size: 11px;
}

/* --------------------------------------------------------------- breadcrumb */
.breadcrumb {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 4px;
  margin-top: 12px;
  font-size: 12px;
}

.crumb {
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

.crumb.is-current {
  color: var(--ink);
  font-weight: 600;
  text-decoration: none;
  cursor: default;
}

.crumb-sep {
  color: #94a3b8;
}

/* --------------------------------------------------------------- input/list */
input[type="search"],
input[type="text"] {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  font: inherit;
  background: var(--surface-muted);
  color: var(--ink);
}

input:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 35%, #fff);
  outline-offset: 1px;
}

.list-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0 6px;
  font-size: 12px;
  color: var(--muted);
}

.link {
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
}

.unit-list {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.unit + .unit {
  border-top: 1px solid var(--line);
}

.unit button {
  display: flex;
  flex-direction: column;
  gap: 2px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border: 0;
  background: none;
  font: inherit;
  cursor: pointer;
}

.unit button:hover {
  background: var(--hover);
}

.unit.is-selected button {
  background: var(--selected-bg);
  box-shadow: inset 3px 0 0 #dc2626;
}

.unit-name {
  font-weight: 600;
}

.unit-meta {
  font-size: 11px;
  color: var(--muted);
}

.empty {
  padding: 12px;
  color: var(--muted);
  font-size: 12px;
}

/* ------------------------------------------------------------------ details */
.detail-title {
  margin: 0 0 8px;
  font-size: 16px;
}

.detail-badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}

.badge {
  --badge-color: var(--accent);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  color: var(--badge-color);
  background: color-mix(in srgb, var(--badge-color) 18%, var(--panel));
  border: 1px solid color-mix(in srgb, var(--badge-color) 38%, var(--panel));
}

.badge-plain {
  --badge-color: #64748b;
}

.detail-grid {
  display: grid;
  grid-template-columns: 88px 1fr;
  gap: 4px 10px;
  margin: 0 0 12px;
  font-size: 12px;
}

.detail-grid dt {
  color: var(--muted);
}

.detail-grid dd {
  margin: 0;
}

.detail-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

button {
  font: inherit;
}

.detail-actions button,
.coord-row button,
.ghost {
  padding: 7px 12px;
  border-radius: var(--radius);
  border: 1px solid var(--accent);
  background: var(--accent);
  color: var(--accent-ink);
  cursor: pointer;
}

.detail-actions button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.ghost {
  background: var(--panel);
  color: var(--ink);
  border-color: var(--line);
}

.wide {
  width: 100%;
  margin-top: 8px;
}

.hint {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.5;
}

.risk-summary {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: baseline;
  gap: 8px;
}

.risk-summary strong {
  font-size: 22px;
}

.risk-summary span {
  color: var(--muted);
  font-size: 12px;
}

.risk-accuracy {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface-muted);
  font-size: 11px;
}

.risk-accuracy span {
  color: var(--muted);
}

.risk-popup {
  min-width: 220px;
  max-width: 320px;
}

.risk-popup h3 {
  margin: 0 0 6px;
  font-size: 14px;
}

.risk-popup p {
  margin: 5px 0;
  line-height: 1.45;
}

.risk-popup .risk-description {
  max-height: 150px;
  overflow-y: auto;
}

.risk-popup .risk-place,
.risk-popup .risk-estimated {
  color: var(--muted);
  font-size: 11px;
}

.coord-row {
  display: flex;
  gap: 6px;
}

.check {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 13px;
  cursor: pointer;
}

/* ------------------------------------------------------------------- status */
.status-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 12px;
}

.status-row.is-missing {
  color: var(--muted);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: none;
}

.status-label {
  flex: 1;
}

.status-value {
  color: var(--muted);
}

.note {
  margin: 10px 0 0;
  font-size: 11px;
  line-height: 1.5;
  color: var(--muted);
}

.note.muted {
  margin-top: 4px;
  opacity: 0.75;
}

/* ---------------------------------------------------------------------- map */
.map-wrap {
  position: relative;
}

#map {
  position: absolute;
  inset: 0;
  background: var(--map-bg);
}

/* SVG polygon focus-ийн browser outline нь геометрийн bbox-ийг цэнхэр
   дөрвөлжин болгон зурдаг. Keyboard focus-ийг хил дагасан glow-оор орлуулна. */
.leaflet-overlay-pane path.leaflet-interactive:focus {
  outline: none;
}

.leaflet-overlay-pane path.leaflet-interactive:focus-visible {
  filter: drop-shadow(0 0 2px #2563eb);
}

.summary {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 500;
  display: flex;
  gap: 12px;
  padding: 8px 14px;
  background: var(--overlay);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 2px 10px var(--shadow);
  font-size: 12px;
}

.summary:empty {
  display: none;
}

.summary span + span {
  padding-left: 12px;
  border-left: 1px solid var(--line);
  color: var(--muted);
}

.risk-legend {
  position: absolute;
  top: 54px;
  left: 12px;
  z-index: 500;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--overlay);
  box-shadow: 0 2px 10px var(--shadow);
  color: var(--muted);
  font-size: 11px;
}

.risk-legend:empty {
  display: none;
}

.unit-label {
  white-space: nowrap;
  pointer-events: none;
}

.unit-label span {
  display: inline-block;
  transform: translate(-50%, -50%);
  font-size: 11px;
  font-weight: 600;
  color: #1e293b;
  text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 3px #fff;
}

:root[data-theme="dark"] .leaflet-tile-pane {
  filter: brightness(0.62) invert(1) contrast(0.9) hue-rotate(180deg) saturate(0.65);
}

:root[data-theme="dark"] .leaflet-overlay-pane {
  filter: brightness(1.35) saturate(1.25);
}

:root[data-theme="dark"] .leaflet-control-zoom a,
:root[data-theme="dark"] .leaflet-control-layers,
:root[data-theme="dark"] .leaflet-popup-content-wrapper,
:root[data-theme="dark"] .leaflet-popup-tip,
:root[data-theme="dark"] .leaflet-tooltip {
  background: var(--panel);
  color: var(--ink);
  border-color: var(--line);
}

:root[data-theme="dark"] .leaflet-control-zoom a {
  border-bottom-color: var(--line);
}

:root[data-theme="dark"] .leaflet-control-attribution {
  background: var(--overlay);
  color: var(--muted);
}

:root[data-theme="dark"] .leaflet-control-attribution a {
  color: var(--accent);
}

:root[data-theme="dark"] .unit-label span {
  color: #f8fafc;
  text-shadow: 0 0 3px #0f172a, 0 0 3px #0f172a, 0 0 3px #0f172a;
}

.loading,
.toast {
  position: absolute;
  z-index: 600;
  padding: 8px 14px;
  border-radius: var(--radius);
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

.loading {
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: #1e293b;
  color: #fff;
}

.toast {
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  max-width: 520px;
  background: #7f1d1d;
  color: #fff;
  line-height: 1.5;
}

.is-visible {
  opacity: 1;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 60vh;
  }

  .sidebar {
    max-height: 40vh;
  }
}
