:root {
  color-scheme: light;
  --bg: #f7f6f2;
  --panel: #ffffff;
  --ink: #202124;
  --muted: #667085;
  --line: #d9dee7;
  --green: #1f9d72;
  --yellow: #e0a21b;
  --gray: #9aa3af;
  --red: #cf3f34;
  --blue: #2f65d9;
  --teal: #176b87;
  --shadow: 0 14px 36px rgba(23, 34, 48, 0.14);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Noto Sans TC", "Segoe UI", sans-serif;
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  height: 100dvh;
  min-height: 0;
  overflow: hidden;
}

.map-region {
  position: relative;
  min-width: 0;
}

.map {
  width: 100%;
  height: 100%;
  background: #e8ecef;
}

.status {
  position: absolute;
  left: 16px;
  top: 16px;
  z-index: 800;
  max-width: min(520px, calc(100% - 32px));
  border: 1px solid rgba(32, 33, 36, 0.12);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: var(--shadow);
  color: var(--muted);
  padding: 10px 12px;
  font-size: 14px;
}

.status:empty {
  display: none;
}

.panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
  border-left: 1px solid var(--line);
  background: var(--panel);
}

.sheet-tabs {
  display: none;
}

.sheet-handle {
  display: none;
}

.panel-header {
  padding: 22px 22px 14px;
  border-bottom: 1px solid var(--line);
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--teal);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 24px;
  line-height: 1.25;
}

.summary {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.6;
}

.controls {
  display: grid;
  gap: 8px;
  padding: 16px 22px;
  border-bottom: 1px solid var(--line);
}

.search-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: end;
  gap: 8px;
}

.controls label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

#filterToggle,
#closeDetail,
.sheet-tabs button {
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 8px 12px;
  font-weight: 750;
}

#filterToggle[aria-expanded="true"] {
  border-color: var(--teal);
  background: #e8f3f5;
  color: var(--teal);
}

.filter-summary {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.filter-fields {
  display: none;
  gap: 12px;
  padding-top: 8px;
}

.filters-expanded .filter-fields {
  display: grid;
}

input[type="search"],
select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  padding: 8px 10px;
}

input[type="search"]:focus,
select:focus {
  outline: 2px solid rgba(47, 101, 217, 0.22);
  border-color: var(--blue);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.segmented label,
.checkbox {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--ink);
  font-size: 13px;
  font-weight: 600;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  padding: 12px 22px;
  border-bottom: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}

.legend span {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.dot,
.ring {
  display: inline-block;
  width: 12px;
  height: 12px;
}

.dot {
  border-radius: 50%;
}

.ring {
  border-radius: 50%;
  background: #fff;
}

.ring.punishment {
  border: 3px solid var(--red);
}

.ring.judgment {
  border: 3px solid var(--blue);
}

.results {
  flex: 1;
  min-height: 0;
  overflow: auto;
  margin: 0;
  padding: 0;
  list-style: none;
}

.result {
  display: grid;
  gap: 6px;
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 14px 22px;
  text-align: left;
  cursor: pointer;
}

.result:hover,
.result:focus,
.result.selected {
  background: #f3f7fb;
  outline: none;
}

.result.selected {
  border-left: 4px solid var(--teal);
  padding-left: 18px;
}

.result strong {
  font-size: 15px;
  line-height: 1.35;
}

.result small {
  color: var(--muted);
  line-height: 1.45;
}

.result-updated {
  font-size: 12px;
}

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

.badge {
  border-radius: 999px;
  padding: 3px 8px;
  background: #eef2f6;
  color: #344054;
  font-size: 12px;
  font-weight: 700;
}

.dot.large-public,
.marker.large-public {
  background: #1f9d72;
}

.dot.small-home,
.marker.small-home {
  background: #2f65d9;
}

.dot.quasi-public,
.marker.quasi-public {
  background: #e0a21b;
}

.dot.private,
.marker.private {
  background: #cf3f34;
}

.dot.type-unknown,
.marker.type-unknown {
  background: #8b95a5;
}

.badge.large-public {
  background: #dff6ed;
  color: #0b6b4a;
}

.badge.small-home {
  background: #e5ecff;
  color: #244fb0;
}

.badge.quasi-public {
  background: #fff2c7;
  color: #805b00;
}

.badge.private {
  background: #fde7e3;
  color: #a93227;
}

.badge.type-unknown,
.badge.flag {
  background: #eef0f3;
  color: #555f6d;
}

.badge.source {
  background: #e8f3f5;
  color: var(--teal);
}

.badge.punishment {
  background: #fde7e3;
  color: #a93227;
}

.badge.judgment {
  background: #e5ecff;
  color: #244fb0;
}

.marker {
  width: 34px;
  height: 34px;
  border: 3px solid #fff;
  border-radius: 50% 50% 50% 8px;
  box-shadow: 0 7px 14px rgba(15, 23, 42, 0.22);
  transform: rotate(-45deg);
}

.marker.punishment {
  border-color: var(--red);
}

.marker.judgment {
  border-color: var(--blue);
}

.cluster {
  display: grid;
  place-items: center;
  width: 58px;
  height: 58px;
  border: 3px solid rgba(255, 255, 255, 0.94);
  border-radius: 50%;
  background: var(--teal);
  box-shadow: 0 12px 28px rgba(15, 23, 42, 0.26);
  color: #fff;
  text-align: center;
}

.cluster.medium {
  background: #27548a;
}

.cluster.large {
  background: #5f3dc4;
}

.cluster.has-flags {
  border-color: var(--red);
}

.cluster strong {
  display: block;
  font-size: 18px;
  line-height: 1;
}

.cluster span {
  display: block;
  max-width: 48px;
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 10px;
  font-weight: 750;
  line-height: 1.1;
}

.popup.compact {
  min-width: 240px;
  max-width: 320px;
}

.popup.compact .badges {
  margin-top: -2px;
}

.popup {
  display: grid;
  gap: 10px;
  min-width: 260px;
  max-width: 360px;
}

.popup h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1.35;
}

.popup dl {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 6px 10px;
  margin: 0;
  font-size: 13px;
}

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

.popup dd {
  margin: 0;
}

.popup-section {
  border-top: 1px solid var(--line);
  padding-top: 10px;
}

.popup-section h3 {
  margin: 0 0 8px;
  font-size: 14px;
}

.popup-section ul {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 18px;
}

.popup-section li {
  line-height: 1.45;
}

.popup-section p {
  margin: 0;
  color: #344054;
  font-size: 13px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.popup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.popup-actions a {
  border-radius: 8px;
  background: #eef2f6;
  color: #1d2939;
  padding: 7px 9px;
  text-decoration: none;
  font-weight: 700;
}

.popup-actions a.primary {
  background: var(--teal);
  color: #fff;
}

.detail-panel {
  display: none;
  flex-direction: column;
  min-height: 220px;
  max-height: 46%;
  border-top: 1px solid var(--line);
  background: #fff;
}

.detail-panel[hidden] {
  display: none;
}

.detail-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
}

.detail-header h2 {
  margin: 0;
  font-size: 17px;
  line-height: 1.35;
}

.detail-header .eyebrow {
  margin-bottom: 4px;
}

.detail-content {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 14px 22px 18px;
}

.detail-content .popup {
  max-width: none;
}

.detail-content .popup h2 {
  display: none;
}

.leaflet-popup-content {
  margin: 14px 16px;
}

@media (max-width: 900px) {
  .leaflet-popup-pane {
    display: none;
  }

  .app-shell {
    position: relative;
    display: block;
    height: 100dvh;
  }

  .map-region {
    height: 100%;
  }

  .panel {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 900;
    height: var(--sheet-height, min(72dvh, 640px));
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -18px 40px rgba(15, 23, 42, 0.22);
    transition: height 180ms ease;
    will-change: height;
  }

  .panel > .results,
  .panel > .detail-panel {
    min-height: 0;
  }

  .panel.sheet-dragging {
    transition: none;
  }

  .sheet-handle {
    display: grid;
    place-items: center;
    min-height: 28px;
    border: 0;
    border-radius: 16px 16px 0 0;
    background: #fff;
    padding: 8px 0 4px;
    touch-action: none;
  }

  .sheet-handle span {
    display: block;
    width: 42px;
    height: 5px;
    border-radius: 999px;
    background: #c7ced8;
  }

  .sheet-tabs {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
    padding: 8px 14px;
    border-bottom: 1px solid var(--line);
  }

  .sheet-tabs button.active {
    border-color: var(--teal);
    background: var(--teal);
    color: #fff;
  }

  .panel-header {
    display: none;
  }

  .legend {
    position: sticky;
    top: 0;
    z-index: 2;
    display: flex;
    flex-wrap: nowrap;
    gap: 6px;
    overflow-x: auto;
    border-bottom: 1px solid var(--line);
    background: rgba(255, 255, 255, 0.96);
    padding: 6px 14px;
    scrollbar-width: none;
    white-space: nowrap;
  }

  .legend::-webkit-scrollbar {
    display: none;
  }

  .legend span {
    flex: 0 0 auto;
    gap: 5px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: #fff;
    padding: 4px 8px;
    color: #475467;
    font-size: 11px;
    font-weight: 700;
  }

  .legend .dot,
  .legend .ring {
    width: 8px;
    height: 8px;
  }

  .legend .ring {
    border-width: 2px;
  }

  .controls {
    display: none;
    padding: 10px 14px;
  }

  .filter-fields,
  .filters-expanded .filter-fields {
    display: none;
  }

  .panel[data-mode="filter"] .filter-fields {
    display: grid;
  }

  .panel[data-mode="filter"] .controls {
    display: grid;
  }

  .panel[data-mode="filter"] .results,
  .panel[data-mode="filter"] .legend,
  .panel[data-mode="filter"] .detail-panel,
  .panel[data-mode="detail"] .legend,
  .panel[data-mode="detail"] .results {
    display: none;
  }

  .panel[data-mode="list"] .detail-panel {
    display: none;
  }

  .panel[data-mode="detail"] .detail-panel {
    display: flex;
    flex: 1;
    min-height: 0;
    max-height: none;
    border-top: 0;
  }

  .panel[data-mode="detail"] .detail-panel[hidden] {
    display: none;
  }

  .panel.sheet-collapsed .filter-summary {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .panel.sheet-collapsed .results,
  .panel.sheet-collapsed .legend,
  .panel.sheet-collapsed .detail-panel,
  .panel.sheet-collapsed .filter-fields {
    display: none;
  }

  .panel.sheet-collapsed[data-mode="detail"] .detail-panel {
    display: none;
  }

  .result {
    padding: 12px 14px;
  }

  .result.selected {
    padding-left: 10px;
  }

  .detail-header {
    padding: 12px 14px;
  }

  .detail-content {
    padding: 12px 14px 18px;
  }
}

@media (max-width: 520px) {
  .controls,
  .detail-header,
  .detail-content,
  .result {
    padding-left: 12px;
    padding-right: 12px;
  }
}
