:root {
  --surface: #ffffff;
  --surface-muted: #f5f7fb;
  --border: #d9dee8;
  --text: #172033;
  --text-muted: #647084;
  --accent: #2364d2;
  --shadow: 0 12px 36px rgba(25, 37, 64, 0.18);
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  margin: 0;
  color: var(--text);
  background: var(--surface-muted);
  font-family: "Segoe UI", "Noto Sans TC", system-ui, sans-serif;
}

main {
  height: 100vh;
  overflow: hidden;
}

button,
select {
  font: inherit;
}

.map-shell {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
}

.filter-bar {
  position: relative;
  z-index: 10;
  display: grid;
  grid-template-columns: minmax(180px, 240px) minmax(140px, 180px) minmax(150px, 190px) minmax(260px, 1fr) auto;
  gap: 12px;
  align-items: end;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: 0 1px 8px rgba(20, 31, 52, 0.08);
  max-height: 122px;
  overflow: hidden;
}

.brand-title {
  font-size: 18px;
  font-weight: 700;
  line-height: 1.25;
}

.brand-subtitle {
  margin-top: 2px;
  color: var(--text-muted);
  font-size: 12px;
}

.filter-field {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.filter-field > span {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
}

.category-picker {
  min-height: 31px;
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 4px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #fff;
  min-width: 0;
}

.selected-category-text {
  flex: 1;
  min-width: 0;
  color: var(--text);
  font-size: 13px;
  line-height: 1.35;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.category-option {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #f9fbff;
  color: var(--text);
  font-size: 13px;
  white-space: nowrap;
}

.category-option input {
  margin: 0;
}

.filter-actions {
  display: flex;
  gap: 8px;
  white-space: nowrap;
}

.category-dialog {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 18px;
}

.category-dialog[hidden] {
  display: none;
}

.category-dialog-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.42);
}

.category-dialog-panel {
  position: relative;
  z-index: 1;
  width: min(760px, 94vw);
  max-height: min(680px, 88vh);
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 14px;
  padding: 18px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.category-dialog-header {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
}

.category-dialog-title {
  font-size: 18px;
  font-weight: 700;
}

.category-dialog-subtitle {
  margin-top: 3px;
  color: var(--text-muted);
  font-size: 13px;
}

.category-dialog .category-list {
  grid-template-columns: 1fr;
}

.category-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 2px;
}

.category-dialog-body {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  min-height: 0;
  overflow: hidden;
}

.category-group {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fbfcff;
}

.category-group-title {
  font-size: 15px;
  font-weight: 700;
}

.category-group .category-list {
  align-content: start;
  overflow-y: auto;
  padding-right: 2px;
}

.category-group-note {
  margin: 0;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.55;
}

.map-stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
}

.map-canvas {
  width: 100%;
  height: 100%;
  min-height: 0;
}

.drawer-toggle {
  position: absolute;
  right: 18px;
  top: 18px;
  z-index: 7;
  min-width: 48px;
  height: 38px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  box-shadow: var(--shadow);
}

.map-type-toggle {
  position: absolute;
  right: 78px;
  top: 18px;
  z-index: 7;
  min-width: 58px;
  height: 38px;
  border: 1px solid rgba(15, 23, 42, 0.18);
  border-radius: 6px;
  background: #ffffff;
  color: var(--text);
  font-weight: 700;
  box-shadow: var(--shadow);
}

.map-type-toggle[aria-pressed="true"] {
  background: #111827;
  color: #ffffff;
  border-color: #111827;
}

.search-area-button {
  position: absolute;
  left: 50%;
  top: 18px;
  z-index: 7;
  transform: translateX(-50%);
  height: 38px;
  padding: 0 16px;
  border: 1px solid rgba(15, 23, 42, 0.18);
  border-radius: 999px;
  background: #ffffff;
  color: var(--accent);
  font-weight: 700;
  box-shadow: var(--shadow);
}

.search-area-button[hidden] {
  display: none;
}

.loading-state {
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: 8;
  transform: translate(-50%, -50%);
  display: inline-flex;
  gap: 9px;
  align-items: center;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: var(--shadow);
  color: var(--text-muted);
}

.loading-state.hidden {
  display: none;
}

.loading-spinner {
  width: 18px;
  height: 18px;
  border: 3px solid #dbe3f0;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.summary-drawer {
  position: fixed;
  right: 0;
  top: 0;
  z-index: 20;
  width: min(440px, 34vw);
  min-width: 360px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 18px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  box-shadow: var(--shadow);
  transform: translateX(102%);
  transition: transform 180ms ease;
  overflow-y: auto;
}

.summary-drawer.open {
  transform: translateX(0);
}

.drawer-header {
  position: sticky;
  top: -18px;
  z-index: 3;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin: -18px -18px 0;
  padding: 18px 18px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  box-shadow: 0 8px 16px rgba(25, 37, 64, 0.08);
}

.drawer-title {
  font-size: 20px;
  font-weight: 700;
}

.drawer-subtitle {
  margin-top: 4px;
  color: var(--text-muted);
  font-size: 13px;
}

.summary-section {
  display: grid;
  gap: 10px;
}

.summary-section h2 {
  margin: 0;
  font-size: 15px;
  font-weight: 700;
}

.metric-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.metric {
  display: grid;
  gap: 4px;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface-muted);
}

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

.metric strong {
  font-size: 24px;
}

.list-summary {
  display: grid;
  gap: 8px;
}

.summary-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
}

.summary-row span {
  color: var(--text-muted);
}

.population-summary {
  display: grid;
  gap: 10px;
}

.district-chart {
  display: grid;
  gap: 9px;
  max-height: 320px;
  padding-right: 4px;
  overflow-y: auto;
}

.district-bar-row {
  display: grid;
  gap: 5px;
}

.district-bar-label {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: baseline;
  font-size: 13px;
}

.district-bar-label span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.district-bar-label strong {
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 700;
}

.district-bar-track {
  height: 12px;
  overflow: hidden;
  border-radius: 999px;
  background: #e7ebf2;
}

.district-bar-track > span {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: #174ea6;
  box-shadow: inset 0 -1px 0 rgba(0, 0, 0, 0.16);
}

.data-note {
  margin-top: 2px;
  padding: 10px 12px;
  border: 1px solid #cfd8e3;
  border-radius: 6px;
  background: #f8fafc;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1.55;
}

.age-row {
  display: grid;
  grid-template-columns: 76px 1fr 58px;
  gap: 8px;
  align-items: center;
}

.age-bar {
  height: 10px;
  overflow: hidden;
  border-radius: 999px;
  background: #e7ebf2;
}

.age-bar > span {
  display: block;
  height: 100%;
  background: var(--accent);
}

.empty {
  color: var(--text-muted);
  font-size: 14px;
}

@media (max-width: 960px) {
  .filter-bar {
    grid-template-columns: 1fr 1fr;
  }

  .brand-block,
  .category-field,
  .filter-actions {
    grid-column: 1 / -1;
  }

  .category-list,
  .category-dialog-body {
    grid-template-columns: 1fr;
  }

  .summary-drawer {
    width: min(420px, 92vw);
    min-width: 0;
  }

  .map-canvas {
    min-height: 0;
  }
}
