:root {
  --bg: #F1F5F9;
  --surface: #FFFFFF;
  --border: #E2E8F0;
  --text: #0F172A;
  --muted: #64748B;
  --primary: #1E40AF;
  --primary-hover: #1D4ED8;
  --emerald: #10B981;
  --shadow: 0 4px 16px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  height: 100dvh;
  font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--text);
  background: var(--bg);
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.hidden { display: none !important; }
.mobile-only { display: none !important; }

.topbar {
  height: 54px;
  min-height: 54px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  border-top: 3px solid var(--primary);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 0 12px;
  z-index: 30;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  flex: 1;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.topbar-center {
  flex: 1;
  max-width: 380px;
  min-width: 140px;
}

.brand-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, #1D4ED8, #10B981);
  flex-shrink: 0;
}

.brand-titles {
  min-width: 0;
}

.brand-titles h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.brand-sub {
  display: block;
  font-size: 11px;
  color: var(--muted);
}

.icon-btn, .tool-btn, .primary-btn, .basemap-select {
  height: 32px;
  border-radius: 6px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  border: 1px solid #CBD5E1;
  background: var(--surface);
  color: var(--text);
  padding: 0 10px;
}

.icon-btn {
  width: 32px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.tool-btn:hover, .icon-btn:hover { background: #F8FAFC; }
.tool-btn.active {
  background: #EFF6FF;
  border-color: var(--primary);
  color: var(--primary);
}

.primary-btn {
  background: var(--primary);
  border-color: #1E3A8A;
  color: #FFFFFF;
}
.primary-btn:hover { background: var(--primary-hover); }

.tool-group {
  display: flex;
  align-items: center;
  gap: 4px;
}

.search-box {
  position: relative;
  width: 100%;
}

.search-box input {
  width: 100%;
  height: 32px;
  border: 1px solid #CBD5E1;
  border-radius: 6px;
  padding: 0 10px;
  font-size: 12px;
  outline: none;
}
.search-box input:focus { border-color: var(--primary); }

.search-results {
  position: absolute;
  top: 36px;
  left: 0;
  right: 0;
  max-height: 240px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  box-shadow: var(--shadow);
  z-index: 40;
}

.search-item {
  padding: 7px 10px;
  font-size: 12px;
  cursor: pointer;
  border-bottom: 1px solid #F1F5F9;
}
.search-item:hover { background: #EFF6FF; }
.search-item small { display: block; color: var(--muted); font-size: 11px; }

.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.sidebar {
  width: 300px;
  min-width: 260px;
  max-width: 85vw;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 25;
  transition: transform 0.22s ease, margin-left 0.22s ease;
}

.sidebar.collapsed { margin-left: -300px; }

.sidebar-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.36);
  z-index: 22;
}

.sidebar-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12.5px;
  font-weight: 700;
}

.sidebar-header-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.mobile-search-bar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 10px;
  display: flex;
  align-items: center;
  gap: 6px;
  z-index: 29;
}

.mobile-search-bar .search-box { flex: 1; }

.mobile-controls-panel {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  background: #F8FAFC;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.mobile-ctrl-label {
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.basemap-select-mobile {
  width: 100%;
  height: 34px;
  border-radius: 6px;
  border: 1px solid #CBD5E1;
  background: var(--surface);
  color: var(--text);
  padding: 0 10px;
  font-size: 12.5px;
  font-weight: 600;
}

.mobile-tool-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.mobile-tool-row .tool-btn {
  flex: 1 1 auto;
  min-height: 32px;
}

.measure-close-btn {
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.14);
  color: #FFFFFF;
  border-radius: 99px;
  width: 22px;
  height: 22px;
  padding: 0;
  cursor: pointer;
  font-size: 11px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.badge-mode {
  font-size: 10.5px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 99px;
  background: #EFF6FF;
  color: var(--primary);
  border: 1px solid #BFDBFE;
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.group-header {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  margin-top: 6px;
}

.layer-card {
  background: #F8FAFC;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.layer-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.layer-check {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  flex: 1;
  min-width: 0;
}

.layer-check span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.layer-zoom-btn {
  border: 1px solid #CBD5E1;
  background: #FFFFFF;
  border-radius: 4px;
  font-size: 11px;
  padding: 2px 6px;
  cursor: pointer;
  color: var(--primary);
}
.layer-zoom-btn:hover { background: #EFF6FF; }

.opacity-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
}
.opacity-row input[type="range"] { flex: 1; accent-color: var(--primary); }

.legend-box {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-top: 4px;
  border-top: 1px dashed var(--border);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 11px;
  color: #334155;
}

.legend-swatch {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
  border: 1px solid rgba(15, 23, 42, 0.25);
}
.legend-swatch.point { border-radius: 50%; width: 12px; height: 12px; }
.legend-swatch.line { height: 4px; border-radius: 2px; border: none; }

.legend-img {
  max-width: 100%;
  height: auto;
  align-self: flex-start;
  border-radius: 3px;
}

.map-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
}

#map {
  width: 100%;
  height: 100%;
  background: #E2E8F0;
}

.measure-banner {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.9);
  color: #FFFFFF;
  padding: 7px 14px;
  border-radius: 99px;
  font-size: 12px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 12;
}
.measure-hint { font-weight: 400; color: #CBD5E1; font-size: 11px; }

.sync-toast {
  position: absolute;
  top: 12px;
  right: 12px;
  background: #ECFDF5;
  color: #065F46;
  border: 1px solid #6EE7B7;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 600;
  box-shadow: var(--shadow);
  z-index: 12;
}

.coords-bar {
  position: absolute;
  bottom: 10px;
  left: 10px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 3px 8px;
  font-size: 11px;
  font-family: Consolas, monospace;
  color: #334155;
  z-index: 10;
}

.popup-wrap { max-width: min(270px, 80vw); }
.popup-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}
.popup-title { font-size: 12.5px; font-weight: 700; color: var(--primary); }
.popup-pager { display: flex; align-items: center; gap: 4px; font-size: 11px; }
.popup-pager button {
  border: 1px solid #CBD5E1;
  background: #FFF;
  border-radius: 4px;
  cursor: pointer;
  padding: 1px 6px;
}
.popup-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 11.5px;
  max-height: 200px;
  display: block;
  overflow-y: auto;
}
.popup-table th, .popup-table td {
  text-align: left;
  padding: 3px 6px;
  border-bottom: 1px solid #F1F5F9;
  vertical-align: top;
  word-break: break-word;
}
.popup-table th { color: var(--muted); font-weight: 600; width: 42%; }

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.modal-card {
  width: min(440px, 92vw);
  background: var(--surface);
  border-radius: 8px;
  padding: 16px;
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}
.modal-header h2 { margin: 0; font-size: 15px; }
.modal-hint { font-size: 12px; color: var(--muted); margin: 0 0 10px 0; }
.share-url-row { display: flex; gap: 8px; }
.share-url-row input {
  flex: 1;
  height: 32px;
  border: 1px solid #CBD5E1;
  border-radius: 6px;
  padding: 0 8px;
  font-size: 12px;
}
.copy-feedback { margin: 8px 0 0 0; font-size: 11.5px; color: #059669; font-weight: 600; }

@media (max-width: 768px) {
  .mobile-only { display: flex !important; }
  .icon-btn.mobile-only { display: inline-flex !important; }
  .topbar {
    height: 50px;
    min-height: 50px;
    padding: 0 8px;
    gap: 6px;
  }
  .topbar-right { gap: 5px; }
  .brand-sub { display: none; }
  .brand-titles h1 { font-size: 13.5px; }
  .topbar-center, .tool-group, #basemap-select, #btn-zoom-all { display: none !important; }
  #btn-share {
    padding: 0 10px;
    font-size: 12px;
  }
  .sidebar {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    width: min(300px, 84vw);
    min-width: 0;
    margin-left: 0 !important;
    box-shadow: var(--shadow);
    transform: translateX(0);
  }
  .sidebar.collapsed {
    transform: translateX(-105%);
    margin-left: 0 !important;
  }
  .measure-banner {
    max-width: calc(100vw - 24px);
    padding: 6px 10px;
    font-size: 11.5px;
    border-radius: 10px;
  }
  .measure-hint { display: none; }
  .popup-table { max-height: 150px; }
}

@media (max-width: 520px) {
  .coords-bar { display: none; }
  .brand-dot { display: none; }
}
