/* ── Base ─────────────────────────────────────────────────── */
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 13px;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-text-size-adjust: none;
}

#map {
  position: fixed;
  inset: 0;
  touch-action: none;
}

/* ── Shared overlay panel ─────────────────────────────────── */
.overlay-panel {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 12;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  border-radius: 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.13), 0 1px 4px rgba(0,0,0,0.06);
  border: 1px solid rgba(0,0,0,0.08);
  width: 280px;
  max-height: calc(100dvh - max(12px, env(safe-area-inset-top)) - max(12px, env(safe-area-inset-bottom)) - 24px);
  transform: translateX(-110%);
  visibility: visible;
  opacity: 0;
  transition: transform 0.25s ease, opacity 0.25s ease;
}

.overlay-panel.open {
  transform: translateX(0);
  opacity: 1;
}

/* ── Mobile styles ──────────────────────────────────────── */
@media (max-width: 600px) {
  .right-stack {
    position: static;
  }

  .overlay-panel {
    position: fixed;
    inset: 0;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-width: 100%;
    border-radius: 16px 16px 0 0;
    max-height: calc(100dvh - max(12px, env(safe-area-inset-top)));
    transform: translateY(100%);
    visibility: hidden;
    opacity: 1;
    transition: transform 0.25s ease, visibility 0s linear 0.25s;
  }

  .overlay-panel.open {
    transform: translateY(0);
    visibility: visible;
    transition: transform 0.25s ease, visibility 0s linear 0s;
  }

  .overlay-body {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }

  #location-overlay .btn-chevron {
    display: flex;
  }

  .location-overlay.collapsed .overlay-body {
    display: none;
  }

  .location-overlay.collapsed .btn-chevron svg {
    transform: rotate(180deg);
  }

  #closest-panel {
    position: fixed;
    inset: 0;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-width: 100%;
    border-radius: 16px 16px 0 0;
    max-height: calc(100dvh - max(12px, env(safe-area-inset-top)));
    transform: translateY(100%);
    visibility: hidden;
    opacity: 1;
    transition: transform 0.25s ease, visibility 0s linear 0.25s;
    z-index: 13;
  }

  #closest-panel.open {
    transform: translateY(0);
    visibility: visible;
    transition: transform 0.25s ease, visibility 0s linear 0s;
  }
}

/* ── Right stack (closest + location) ─────────────────── */
.right-stack {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  top: calc(48px + max(12px, env(safe-area-inset-top)));
  bottom: max(12px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-rows: 1fr auto;
  gap: 12px;
  pointer-events: none;
  z-index: 10;
  overflow: hidden;
}

.right-stack > .overlay-panel {
  pointer-events: auto;
}

#layers-panel { z-index: 14; }
#closest-panel { z-index: 13; }
#location-overlay { z-index: 12; }

/* ── Desktop layout ─────────────────────────────────────── */
@media (min-width: 601px) {
  #layers-panel {
    max-height: calc(100dvh - max(12px, env(safe-area-inset-top)) - max(12px, env(safe-area-inset-bottom)) - 24px);
  }

  .right-stack > .overlay-panel {
    position: relative;
    top: auto;
    left: auto;
    min-height: 0;
    display: flex;
    pointer-events: auto;
  }

  #closest-panel {
    grid-row: 1;
    position: relative;
    top: auto;
    left: auto;
    display: flex;
    flex-direction: column;
    min-height: 0;
    max-height: none;
    overflow: hidden;
    transform: translateX(110%);
    opacity: 0;
    transition: transform 0.25s ease, opacity 0.25s ease;
  }

  #closest-panel .overlay-body {
    min-height: 0;
    flex: 1;
    overflow-y: auto;
  }

  #closest-panel.open {
    transform: translateX(0);
    opacity: 1;
  }

  #location-overlay {
    grid-row: 2;
    position: relative;
    top: auto;
    left: auto;
    overflow: hidden;
    max-height: 280px;
    transform: translateX(110%);
    opacity: 0;
    transition: transform 0.25s ease, opacity 0.25s ease;
  }

  #location-overlay.open {
    transform: translateX(0);
    opacity: 1;
  }
}

/* ── Toggle button & backdrop ─────────────────────────────── */
.controls-toggle {
  display: block;
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 5;
  background: #ffffff;
  border: 1px solid rgba(0,0,0,0.1);
  padding: 8px 11px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.12);
  font-size: 17px;
  cursor: pointer;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

/* ── Overlay header ─────────────────────────────────────── */
.overlay-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid #f0f1f3;
  flex-shrink: 0;
  background: #ffffff;
  z-index: 1;
}

#location-overlay .overlay-header {
  justify-content: flex-start;
  gap: 8px;
}

#location-overlay .overlay-header .overlay-title-group {
  flex: 1;
}

.overlay-title-group {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.overlay-title {
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: #1e293b;
  margin: 0;
}

.overlay-subtitle {
  font-size: 12px;
  font-weight: 500;
  color: #64748b;
  margin: 0;
  display: none;
}

.overlay-subtitle:empty {
  display: none;
}

.btn-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: #64748b;
  padding: 0;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

.btn-chevron {
  display: none;
  background: none;
  border: none;
  cursor: pointer;
  color: #64748b;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  -webkit-tap-highlight-color: transparent;
}

.btn-chevron svg {
  width: 20px;
  height: 20px;
  transition: transform 0.2s ease;
}

.overlay-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
}

.overlay-body::-webkit-scrollbar { width: 4px; }
.overlay-body::-webkit-scrollbar-track { background: transparent; }
.overlay-body::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 4px; }

/* ── Shared buttons ───────────────────────────────────────── */
.btn {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

.btn:active {
  transform: none;
  opacity: 1;
}

.btn-primary {
  background: #3b82f6;
  color: #fff;
  border: none;
}
.btn-primary:hover { background: #2563eb; }
.btn-primary:disabled {
  background: #94a3b8;
  cursor: not-allowed;
}

.btn-secondary {
  background: #f0f2f5;
  color: #1d1d1f;
  border: none;
}
.btn-secondary:hover { background: #eef0f4; }
.btn-secondary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.btn-secondary.active {
  background: #3b82f6;
  color: white;
}
.btn-secondary.active:hover { background: #2563eb; }
.btn-secondary.active:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ── Layer tree ───────────────────────────────────────────── */
.overlay-body > .group { border-top: 1px solid #f0f1f3; }
.overlay-body > .group:first-child { border-top: none; }

.group-header {
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  padding: 8px 14px 8px 10px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.group-header:hover { background: #f5f7fa; }

.overlay-body > .group > .group-header {
  padding: 9px 14px 9px 12px;
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: #64748b;
}

.overlay-body > .group .group .group-header {
  padding: 5px 14px 5px 12px;
  font-size: 12px;
  font-weight: 600;
  color: #1e293b;
}

.group-children,
.flat-children {
  border-left: 2px solid #e8eaed;
  margin-left: 20px;
}
.group-children.collapsed,
.flat-children.collapsed { display: none; }
.flat-children { border-left: none; margin-left: 0; }

.overlay-body label,
.tree-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 14px 8px 10px;
  margin: 0;
  cursor: pointer;
  color: #374151;
  font-size: 12.5px;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
.overlay-body label:hover,
.tree-item:hover { background: #f5f7fa; }

.group-children > label { padding-left: 12px; }
.flat-children > label { padding-left: 28px; }

.overlay-body input[type=checkbox],
.checkbox {
  flex-shrink: 0;
  width: 18px;
  height: 18px;
  accent-color: #3b82f6;
  cursor: pointer;
  margin: 0;
}

.expand-btn {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
  width: 16px;
  height: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #94a3b8;
  font-size: 9px;
  flex-shrink: 0;
}
.expand-btn:hover { color: #475569; }
.expand-btn.collapsed { transform: rotate(-90deg); }

.radius-control,
.location-overlay-radius-control {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 4px;
  color: #64748b;
  font-size: 12px;
}
.radius-control { padding: 8px 14px 8px 12px; }
.location-overlay-radius-control { padding: 8px 0; }

.radius-control input[type=number],
.location-overlay-radius-control input[type=number] {
  width: 72px;
  padding: 3px 6px;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  font-size: 12px;
  font-family: inherit;
  color: #1e293b;
  outline: none;
  /* transition: border-color 0.15s, box-shadow 0.15s; */
}
.radius-control input[type=number]:focus,
.location-overlay-radius-control input[type=number]:focus {
  border-color: #3b82f6;
  box-shadow: 0 0 0 2px rgba(59,130,246,0.15);
}

/* ── Closest ──────────────────────────────────────────────── */
.closest-item {
  padding: 4px 0;
  font-size: 12px;
}
.closest-item-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
}
.closest-item .closest-label {
  color: #64748b;
}
.closest-item .closest-dist {
  color: #64748b;
  font-weight: 400;
  font-size: 11px;
}
.closest-item .closest-name {
  font-weight: 600;
  color: #1e293b;
  margin-top: 1px;
}
.closest-waiting {
  font-size: 12px;
  color: #94a3b8;
  font-style: italic;
}

.closest-poi-group { margin-top: 2px; }
.closest-poi-header {
  position: sticky;
  top: 0;
  background: #ffffff;
  z-index: 1;
  display: flex;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  padding: 4px 0;
  user-select: none;
  font-size: 12px;
  font-weight: 600;
  color: #1e293b;
  /* transition: background 0.12s; */
}
.closest-poi-header:hover { background: #f5f7fa; }
.closest-poi-children {
  border-left: 2px solid #e8eaed;
  margin-left: 12px;
  padding-left: 2px;
}
.closest-poi-children.collapsed { display: none; }

@media (max-width: 600px) {
  .overlay-body .closest-item {
    padding: 10px 0;
    border-bottom: 1px solid #f0f1f3;
    font-size: 14px;
  }
  .overlay-body .closest-item:last-child { border-bottom: none; }
  .overlay-body .closest-item .closest-label { font-size: 13px; }
  .overlay-body .closest-item .closest-dist { font-size: 13px; }
  .overlay-body .closest-item .closest-name { font-size: 15px; margin-top: 2px; }
  .overlay-body .closest-waiting { font-size: 14px; }
}

/* ── Location overlay content ───────────────────────────────────── */
.location-overlay-coords {
  font-size: 13px;
  color: #64748b;
  margin-bottom: 12px;
  font-family: monospace;
}
.location-overlay-actions {
  display: flex;
  gap: 8px;
}
.location-overlay-actions button {
  flex: 1;
}
#location-overlay-radius-section,
#location-overlay-measure-section {
  margin-top: 12px;
  display: none;
}
.location-overlay-measure-coords {
  font-size: 11px;
  color: #64748b;
  font-family: monospace;
  margin-bottom: 8px;
}
.location-overlay-measure-distance {
  font-size: 20px;
  font-weight: 700;
  color: #1e293b;
  text-align: center;
  padding: 8px 0;
  font-family: monospace;
}
.measure-center-marker {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  z-index: 10;
  pointer-events: none;
  width: 24px;
  height: 24px;
  display: none;
}
.measure-center-marker.active {
  display: block;
}
.measure-center-marker svg {
  width: 100%;
  height: 100%;
}
.settings-toggle {
  top: calc(48px + max(12px, env(safe-area-inset-top)));
  left: max(12px, env(safe-area-inset-left));
  right: auto;
}

#settings-panel .overlay-body {
  padding: 8px 0;
}

.settings-map-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  text-decoration: none;
  color: #1e293b;
  font-size: 14px;
  font-weight: 500;
  border-bottom: 1px solid #f0f1f3;
  transition: background 0.12s;
}

.settings-map-item:last-child {
  border-bottom: none;
}

.settings-map-item:hover {
  background: #f5f7fa;
}

.settings-map-item.active {
  background: #eff6ff;
  font-weight: 600;
}

.settings-map-name {
  flex: 1;
}

.settings-map-badge {
  font-size: 11px;
  font-weight: 600;
  color: #3b82f6;
  background: #dbeafe;
  padding: 2px 8px;
  border-radius: 10px;
}

.settings-waiting {
  font-size: 13px;
  color: #94a3b8;
  font-style: italic;
  padding: 16px 20px;
  text-align: center;
}

.location-overlay-radius-view {
  margin-top: 12px;
}
.location-overlay-radius-control input[type=range] {
  flex: 1;
  accent-color: #ff6600;
  cursor: pointer;
}
.location-overlay-radius-value {
  font-size: 13px;
  color: #64748b;
  margin-top: 4px;
}
