:root {
  color-scheme: light;
  --bg: #f5f1e8;
  --panel: #fbfaf6;
  --surface: #ffffff;
  --text: #2f302d;
  --muted: #7c7a70;
  --line: #ddd7ca;
  --line-strong: #c9c1b2;
  --accent: #5f7568;
  --accent-soft: #edf2ee;
  --shadow: 0 1px 2px rgba(47, 48, 45, 0.08);
}

* {
  box-sizing: border-box;
}

/* Leaflet layout fallback.
   Keep these rules local so the map still renders correctly if CDN CSS is blocked. */
.leaflet-container {
  position: relative;
  overflow: hidden;
  background: #e8e2d6;
  outline-offset: 1px;
  font: 12px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
}

.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer {
  position: absolute;
  left: 0;
  top: 0;
}

.leaflet-tile-pane {
  z-index: 200;
}

.leaflet-overlay-pane {
  z-index: 400;
}

.leaflet-shadow-pane {
  z-index: 500;
}

.leaflet-marker-pane {
  z-index: 600;
}

.leaflet-tooltip-pane {
  z-index: 650;
}

.leaflet-popup-pane {
  z-index: 700;
}

.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow {
  user-select: none;
  -webkit-user-drag: none;
}

.leaflet-tile {
  border: 0;
  filter: inherit;
  pointer-events: none;
  visibility: hidden;
}

.leaflet-tile-loaded {
  visibility: inherit;
}

.leaflet-control-container .leaflet-top,
.leaflet-control-container .leaflet-bottom {
  position: absolute;
  z-index: 1000;
  pointer-events: none;
}

.leaflet-top {
  top: 0;
}

.leaflet-right {
  right: 0;
}

.map-panel.preview-active .leaflet-right {
  right: min(466px, calc(100% - 58px));
}

.leaflet-bottom {
  bottom: 0;
}

.leaflet-left {
  left: 0;
}

.leaflet-control {
  position: relative;
  z-index: 800;
  float: left;
  clear: both;
  pointer-events: auto;
}

.leaflet-right .leaflet-control {
  float: right;
}

.leaflet-top .leaflet-control {
  margin-top: 10px;
}

.leaflet-bottom .leaflet-control {
  margin-bottom: 10px;
}

.leaflet-left .leaflet-control {
  margin-left: 10px;
}

.leaflet-right .leaflet-control {
  margin-right: 10px;
}

.leaflet-control-zoom a,
.leaflet-control-layers-toggle {
  display: block;
  min-width: 28px;
  min-height: 28px;
  border-bottom: 1px solid var(--line);
  background: rgba(251, 250, 246, 0.94);
  color: var(--text);
  line-height: 28px;
  text-align: center;
  text-decoration: none;
}

.leaflet-control-layers {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(251, 250, 246, 0.94);
  box-shadow: var(--shadow);
  color: var(--text);
  padding: 8px 10px;
}

.leaflet-control-layers:not(.leaflet-control-layers-expanded) {
  padding: 0;
}

.leaflet-control-layers:not(.leaflet-control-layers-expanded) .leaflet-control-layers-list {
  display: none;
}

.leaflet-control-layers-toggle::before {
  display: block;
  width: 100%;
  height: 100%;
  color: var(--text);
  content: "≡";
  font-size: 19px;
  font-weight: 700;
  line-height: 28px;
  text-align: center;
}

.leaflet-control-layers-expanded .leaflet-control-layers-toggle {
  display: none;
}

.map-panel.preview-active .leaflet-control-layers-expanded {
  position: relative;
  z-index: 1400;
}

.leaflet-control-attribution {
  background: rgba(251, 250, 246, 0.86);
  color: var(--muted);
  padding: 2px 6px;
}

.leaflet-popup {
  position: absolute;
  text-align: center;
}

.leaflet-popup-content-wrapper {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  box-shadow: 0 6px 18px rgba(47, 48, 45, 0.12);
  padding: 1px;
  text-align: left;
}

.leaflet-popup-content {
  margin: 16px 18px 15px;
  max-height: min(360px, 52vh);
  overflow-y: auto;
  line-height: 1.5;
}

.leaflet-popup-close-button {
  position: absolute;
  top: 0;
  right: 0;
  width: 26px;
  height: 26px;
  color: var(--muted);
  font: 18px/26px Arial, sans-serif;
  text-align: center;
  text-decoration: none;
}

html,
body,
.app-shell {
  height: 100%;
  margin: 0;
}

html,
body {
  overflow: hidden;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  letter-spacing: 0;
}

.app-shell {
  display: grid;
  height: 100vh;
  overflow: hidden;
  grid-template-columns: minmax(360px, 440px) minmax(0, 1fr);
  transition: grid-template-columns 180ms ease;
}

.app-shell.sidebar-collapsed {
  grid-template-columns: 62px minmax(0, 1fr);
}

.sidebar {
  display: flex;
  min-width: 0;
  min-height: 0;
  max-height: 100vh;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: var(--panel);
  box-shadow: var(--shadow);
  z-index: 500;
}

.brand {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
  padding: 22px 22px 16px;
  border-bottom: 1px solid var(--line);
  background: #f8f5ee;
}

.brand h1 {
  margin: 0 0 6px;
  color: #292a27;
  font-size: 19px;
  line-height: 1.25;
  font-weight: 650;
}

.brand p {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
}

.sidebar-toggle,
.preview-close {
  display: inline-grid;
  width: 30px;
  height: 30px;
  flex: 0 0 auto;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font: inherit;
  font-size: 20px;
  line-height: 1;
}

.sidebar-toggle:hover,
.preview-close:hover {
  border-color: var(--line-strong);
  background: #fffdf8;
}

.sidebar-body {
  display: flex;
  min-height: 0;
  flex: 1;
  flex-direction: column;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.app-shell.sidebar-collapsed .brand {
  justify-content: center;
  padding: 14px 10px;
}

.app-shell.sidebar-collapsed .brand > div,
.app-shell.sidebar-collapsed .sidebar-body {
  display: none;
}

.toolbar {
  display: grid;
  flex: 0 0 auto;
  gap: 13px;
  padding: 16px 22px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

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

.toolbar select,
.toolbar input {
  width: 100%;
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  padding: 8px 10px;
}

.toolbar select:focus,
.toolbar input:focus {
  border-color: var(--accent);
  outline: 2px solid rgba(95, 117, 104, 0.16);
}

.summary {
  display: grid;
  flex: 0 0 auto;
  grid-template-columns: 1fr 1fr;
  border-bottom: 1px solid var(--line);
  background: #f8f5ee;
}

.summary div {
  display: grid;
  gap: 2px;
  padding: 14px 22px;
}

.summary div + div {
  border-left: 1px solid var(--line);
}

.summary .metric {
  color: #292a27;
  font-size: 24px;
  font-weight: 650;
  line-height: 1;
}

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

.overview-panel {
  display: grid;
  flex: 0 0 auto;
  min-height: 0;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}

.overview-toggle {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  border: 0;
  background: #f8f5ee;
  color: var(--text);
  cursor: pointer;
  font: inherit;
  padding: 13px 22px;
  text-align: left;
}

.overview-toggle span {
  font-size: 14px;
  font-weight: 700;
}

.overview-toggle small {
  color: var(--muted);
  font-size: 12px;
}

.overview-body {
  display: grid;
  gap: 12px;
  padding: 0 18px 16px;
}

.overview-panel:not(.is-open) .overview-body {
  display: none;
}

.overview-panel.is-open .overview-toggle {
  border-bottom: 1px solid var(--line);
  margin-bottom: 14px;
}

.overview-card {
  display: grid;
  gap: 10px;
}

.overview-card + .overview-card {
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.overview-card header {
  display: grid;
  gap: 3px;
}

.overview-card h2 {
  margin: 0;
  color: #3a3b37;
  font-size: 14px;
  font-weight: 750;
  line-height: 1.25;
}

.overview-card p {
  margin: 0;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.35;
}

.district-chart {
  display: grid;
  gap: 7px;
}

.district-summary-row {
  display: grid;
  grid-template-columns: minmax(72px, 0.8fr) minmax(0, 1fr);
  align-items: center;
  gap: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fffdf8;
  box-shadow: var(--shadow);
  color: #12384f;
  cursor: pointer;
  font: inherit;
  padding: 0;
  text-align: left;
}

.district-summary-row:hover {
  border-color: var(--line-strong);
  background: #ffffff;
}

.district-summary-row strong,
.district-summary-row span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.district-summary-row strong {
  padding: 9px 10px;
  color: #26352e;
  font-size: 14px;
  font-weight: 750;
}

.district-summary-row span {
  height: 100%;
  padding: 9px 10px;
  background: #f5df8b;
  color: #17374b;
  font-size: 13px;
  font-weight: 750;
  text-align: center;
}

.method-overview {
  display: grid;
  grid-template-columns: 102px minmax(0, 1fr);
  align-items: center;
  gap: 12px;
}

.method-donut {
  display: grid;
  width: 98px;
  height: 98px;
  place-items: center;
  border-radius: 50%;
  background: #ddd7ca;
}

.method-donut::before {
  content: "";
  position: absolute;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: var(--panel);
}

.method-donut {
  position: relative;
}

.method-donut span {
  position: relative;
  z-index: 1;
  display: grid;
  color: #30312e;
  font-size: 20px;
  font-weight: 750;
  line-height: 1;
  text-align: center;
}

.method-donut small {
  margin-top: 3px;
  color: var(--muted);
  font-size: 10px;
  font-weight: 650;
}

.method-legend {
  display: grid;
  gap: 7px;
}

.method-legend-row {
  display: grid;
  grid-template-columns: 10px minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 7px;
  color: var(--text);
  font-size: 12px;
}

.method-swatch {
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.method-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.method-count {
  min-width: 20px;
  color: #30312e;
  font-size: 13px;
  text-align: right;
}

.method-legend-row small {
  min-width: 38px;
  color: var(--muted);
  text-align: right;
}

.case-list {
  display: grid;
  gap: 9px;
  min-height: 0;
  margin: 0;
  padding: 14px;
  overflow: auto;
  list-style: none;
  background: var(--bg);
}

.case-list-panel {
  display: flex;
  min-height: 0;
  flex: 0 0 auto;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

.case-list-toggle {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex: 0 0 auto;
  border: 0;
  border-bottom: 1px solid var(--line);
  background: #f8f5ee;
  color: var(--text);
  cursor: pointer;
  font: inherit;
  list-style: none;
  padding: 13px 22px;
  text-align: left;
}

.case-list-toggle span {
  font-size: 14px;
  font-weight: 700;
}

.case-list-toggle small {
  color: var(--muted);
  font-size: 12px;
}

.case-list-panel:not(.is-open) .case-list {
  display: none;
}

.case-list-panel.is-open {
  flex: 1;
  min-height: 0;
}

.case-list-panel.is-open .case-list {
  display: grid;
  flex: 1;
  height: auto;
  min-height: 0;
  max-height: 100%;
  overflow-y: auto;
}

.case-list button {
  display: grid;
  width: 100%;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  font: inherit;
  padding: 11px 12px;
  text-align: left;
  box-shadow: var(--shadow);
}

.case-list button:hover {
  border-color: var(--line-strong);
  background: #fffdf8;
}

.case-list span {
  overflow-wrap: anywhere;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.35;
}

.case-list small {
  overflow-wrap: anywhere;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.35;
}

.map-panel {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: #e8e2d6;
}

#map {
  height: 100%;
  width: 100%;
}

.popup {
  min-width: 315px;
  max-width: 380px;
  font-size: 14px;
}

.popup-header {
  display: grid;
  gap: 3px;
  padding: 0 6px 12px 0;
  border-bottom: 1px solid var(--line);
}

.popup-header strong {
  display: block;
  color: #292a27;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.35;
}

.popup-header span {
  color: var(--muted);
  font-size: 13px;
}

.popup-section {
  padding: 12px 0 11px;
  border-bottom: 1px solid var(--line);
}

.popup-section:last-child {
  padding-bottom: 0;
  border-bottom: 0;
}

.popup-section-title {
  margin: 0 0 8px;
  color: var(--accent);
  font-size: 13px;
  font-weight: 700;
  line-height: 1.3;
}

.popup dl {
  display: grid;
  grid-template-columns: minmax(92px, 0.42fr) minmax(0, 1fr);
  gap: 7px 12px;
  margin: 0;
}

.popup dt {
  color: var(--muted);
  font-weight: 650;
  line-height: 1.45;
}

.popup dd {
  margin: 0;
  overflow-wrap: anywhere;
  color: var(--text);
  font-weight: 500;
  line-height: 1.45;
}

.doc-links {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.doc-links a,
.doc-links span {
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--surface);
  color: var(--accent);
  padding: 5px 8px;
  text-decoration: none;
}

.preview-panel {
  position: absolute;
  top: 18px;
  right: 18px;
  z-index: 1300;
  width: min(430px, calc(100% - 36px));
  max-height: calc(100% - 36px);
  pointer-events: none;
  transform: translateX(18px);
  opacity: 0;
  transition: opacity 160ms ease, transform 160ms ease;
}

.preview-panel.is-open {
  pointer-events: auto;
  transform: translateX(0);
  opacity: 1;
}

.preview-card {
  position: relative;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(251, 250, 246, 0.98);
  box-shadow: 0 10px 26px rgba(47, 48, 45, 0.16);
}

.preview-close {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 1;
}

.preview-content {
  max-height: calc(100vh - 80px);
  overflow: auto;
  padding: 20px 20px 18px;
}

.preview-tabs {
  position: sticky;
  top: 0;
  z-index: 2;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  margin: 0 42px 14px 0;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(240, 235, 225, 0.98);
  box-shadow: 0 6px 10px rgba(47, 48, 45, 0.06);
}

.preview-tabs button {
  min-height: 34px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
}

.preview-tabs button.is-active {
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
}

.preview-content .popup {
  max-width: none;
  min-width: 0;
  padding-right: 34px;
}

.architecture-preview {
  display: grid;
  gap: 14px;
}

.architecture-preview-header {
  display: grid;
  gap: 4px;
  padding: 0 42px 12px 0;
  border-bottom: 1px solid var(--line);
}

.architecture-preview-header strong {
  color: #292a27;
  font-size: 16px;
  font-weight: 750;
  line-height: 1.35;
}

.architecture-preview-header span {
  color: var(--muted);
  font-size: 13px;
}

.architecture-preview-header a,
.popup a {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
}

.architecture-pages {
  display: grid;
  gap: 12px;
}

.architecture-page {
  margin: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.architecture-page img {
  display: block;
  width: 100%;
  height: auto;
  background: #fff;
}

.architecture-page figcaption {
  padding: 7px 9px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}

.architecture-empty {
  display: grid;
  gap: 6px;
  padding: 28px 12px;
  color: var(--muted);
  text-align: center;
}

.architecture-empty strong {
  color: var(--text);
  font-size: 15px;
}

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

@media (max-width: 760px) {
  html,
  body,
  .app-shell {
    height: 100dvh;
  }

  .app-shell {
    position: relative;
    display: block;
    overflow: hidden;
  }

  .map-panel {
    position: absolute;
    inset: 0;
    min-height: 100dvh;
  }

  .sidebar {
    position: absolute;
    right: 8px;
    bottom: 8px;
    left: 8px;
    z-index: 1100;
    max-height: min(72dvh, 620px);
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: rgba(251, 250, 246, 0.98);
    box-shadow: 0 10px 24px rgba(47, 48, 45, 0.18);
    transform: translateY(0);
    transition: opacity 160ms ease, transform 180ms ease;
  }

  .app-shell.sidebar-collapsed .sidebar {
    transform: translateY(calc(100% - 62px));
  }

  .app-shell.preview-open .sidebar {
    pointer-events: none;
    opacity: 0;
    transform: translateY(100%);
  }

  .brand {
    min-height: 62px;
    align-items: center;
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
  }

  .brand h1 {
    margin-bottom: 3px;
    font-size: 16px;
  }

  .brand p {
    font-size: 12px;
  }

  .sidebar-toggle,
  .preview-close {
    width: 36px;
    height: 36px;
  }

  .app-shell.sidebar-collapsed .brand {
    justify-content: space-between;
    padding: 12px 14px;
  }

  .app-shell.sidebar-collapsed .brand > div {
    display: block;
  }

  .app-shell.sidebar-collapsed .sidebar-body {
    display: none;
  }

  .sidebar-body {
    max-height: calc(72dvh - 62px);
    overflow: hidden;
  }

  .toolbar {
    gap: 10px;
    padding: 12px 14px;
  }

  .toolbar select,
  .toolbar input {
    min-height: 40px;
    font-size: 16px;
  }

  .summary {
    display: none;
  }

  .overview-toggle,
  .case-list-toggle {
    padding: 12px 14px;
  }

  .overview-body {
    max-height: 28vh;
    overflow: auto;
    padding: 0 14px 14px;
  }

  .case-list {
    gap: 8px;
    padding: 10px;
  }

  .case-list-panel.is-open .case-list {
    max-height: 34vh;
  }

  .case-list button {
    padding: 10px 11px;
  }

  .leaflet-top {
    top: 8px;
  }

  .leaflet-left {
    left: 8px;
  }

  .leaflet-right {
    right: 8px;
  }

  .map-panel.preview-active .leaflet-right {
    right: 8px;
  }

  .leaflet-control {
    margin-top: 0;
  }

  .leaflet-control-layers {
    max-width: calc(100vw - 22px);
    font-size: 12px;
  }

  .leaflet-bottom {
    bottom: 72px;
  }

  .app-shell.preview-open .leaflet-bottom {
    bottom: min(72dvh, 620px);
  }

  .preview-panel {
    top: auto;
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    max-height: min(74dvh, 650px);
    transform: translateY(100%);
  }

  .preview-panel.is-open {
    transform: translateY(0);
  }

  .preview-card {
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 10px 10px 0 0;
    box-shadow: 0 -10px 26px rgba(47, 48, 45, 0.16);
  }

  .preview-content {
    max-height: min(74dvh, 650px);
    padding: 16px 14px 20px;
  }

  .preview-tabs {
    top: 0;
    margin: 0 46px 12px 0;
  }

  .preview-content .popup {
    padding-right: 0;
  }

  .popup {
    min-width: 0;
    max-width: none;
    font-size: 14px;
  }

  .popup-header {
    padding-right: 42px;
  }

  .popup dl {
    grid-template-columns: minmax(82px, 0.38fr) minmax(0, 1fr);
    gap: 7px 10px;
  }

  .architecture-preview-header {
    padding-right: 42px;
  }

  .architecture-pages {
    gap: 10px;
  }

  .architecture-page {
    border-radius: 4px;
  }
}
