:root {
  --ink: #18313b;
  --ink-soft: #36525c;
  --paper: #f7f5ef;
  --surface: #fffefa;
  --surface-2: #eef3f1;
  --river: #14798a;
  --river-soft: #dceff0;
  --jiangnan: #c65f49;
  --jiangnan-soft: #f7e7e1;
  --venice: #6d6397;
  --gold: #e4b94d;
  --muted: #63777d;
  --line: #ccd8d5;
  --line-dark: #8da6a6;
  --shadow: 0 18px 48px rgba(25, 53, 62, 0.10);
  --serif: "Source Serif 4", Georgia, serif;
  --sans: "Source Sans 3", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font: 1rem/1.55 var(--sans);
}
a { color: var(--river); }
button, input { font: inherit; }
button:focus-visible, a:focus-visible, input:focus-visible, [tabindex="0"]:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 3px;
}
.skip-link {
  position: fixed;
  z-index: 1000;
  left: 1rem;
  top: -5rem;
  padding: .65rem .9rem;
  background: var(--ink);
  color: #fff;
}
.skip-link:focus { top: 1rem; }

.masthead {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: .75rem clamp(1rem, 4vw, 3.5rem);
  border-bottom: 1px solid var(--line);
  background: rgba(247, 245, 239, .94);
}
.wordmark {
  display: flex;
  align-items: center;
  gap: .8rem;
  color: var(--ink);
  text-decoration: none;
}
.wordmark .mark {
  width: 46px;
  height: 46px;
  display: grid;
  align-content: center;
  gap: 4px;
  padding: 10px;
  border-radius: 50%;
  background: var(--ink);
}
.wordmark .mark i {
  display: block;
  height: 2px;
  border-radius: 999px;
  background: #b9e3e4;
}
.wordmark .mark i:nth-child(2) { width: 72%; margin-left: 28%; background: var(--gold); }
.wordmark strong, .wordmark small { display: block; }
.wordmark strong { font: 700 1rem/1.1 var(--serif); }
.wordmark small { margin-top: .28rem; color: var(--muted); font: .64rem/1 var(--mono); }
.source-chip {
  display: grid;
  gap: .12rem;
  padding: .5rem .75rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  text-decoration: none;
}
.source-chip span { color: var(--muted); font: .58rem var(--mono); text-transform: uppercase; }
.source-chip b { color: var(--ink); font-size: .78rem; }

main { width: min(1320px, 100%); margin: auto; padding: 0 clamp(1rem, 4vw, 3.5rem) 4rem; }
.opening {
  min-height: 285px;
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(320px, .55fr);
  gap: 3rem;
  align-items: end;
  padding: clamp(2.5rem, 5vw, 4.8rem) 0 2.2rem;
}
.eyebrow {
  margin: 0 0 .65rem;
  color: var(--jiangnan);
  font: 500 .68rem var(--mono);
  letter-spacing: .09em;
}
h1, h2, h3 { font-family: var(--serif); }
h1 {
  max-width: 760px;
  margin: 0;
  font-size: clamp(3rem, 6vw, 5.6rem);
  line-height: .88;
  letter-spacing: -.045em;
}
h1 em { color: var(--river); font-weight: 600; }
.opening-copy > p:last-child {
  max-width: 810px;
  margin: 1.3rem 0 0;
  color: var(--ink-soft);
  font-size: 1.03rem;
}
.source-facts {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 0;
  border-top: 1px solid var(--line-dark);
  border-bottom: 1px solid var(--line-dark);
}
.source-facts div { min-width: 0; padding: 1rem .8rem; }
.source-facts dt { color: var(--muted); font: .58rem var(--mono); text-transform: uppercase; }
.source-facts dd { margin: .5rem 0 0; font: 700 1.08rem/1 var(--serif); }

.view-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
  background: #e8ece9;
}
.view-tabs button {
  min-height: 82px;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: .08rem .7rem;
  align-content: center;
  border: 0;
  border-radius: 10px;
  padding: .9rem 1rem;
  background: transparent;
  color: var(--muted);
  text-align: left;
  cursor: pointer;
}
.view-tabs button > span {
  grid-row: 1 / 3;
  align-self: center;
  color: var(--line-dark);
  font: 500 .65rem var(--mono);
}
.view-tabs b { color: var(--ink); font: 700 1rem var(--serif); }
.view-tabs small { color: var(--muted); font-size: .72rem; }
.view-tabs button.active { background: var(--surface); box-shadow: 0 4px 12px rgba(25, 53, 62, .08); }
.view-tabs button.active > span { color: var(--jiangnan); }

.view-panel {
  min-height: 640px;
  padding: clamp(1.2rem, 3vw, 2.4rem);
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 16px 16px;
  background: var(--surface);
  box-shadow: var(--shadow);
}
.view-head {
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: 2rem;
}
.view-head h2 {
  margin: 0;
  font-size: clamp(1.8rem, 3vw, 2.8rem);
  line-height: 1;
  letter-spacing: -.025em;
}
.control-label {
  display: block;
  margin-bottom: .35rem;
  color: var(--muted);
  font: .6rem var(--mono);
  text-transform: uppercase;
}
.feature-filter {
  display: grid;
  grid-template-columns: repeat(3, minmax(92px, 1fr));
  gap: 4px;
  padding: 4px;
  border-radius: 10px;
  background: #e5ebe9;
}
.feature-filter button {
  min-height: 38px;
  border: 0;
  border-radius: 7px;
  padding: .4rem .75rem;
  background: transparent;
  color: var(--ink-soft);
  font-weight: 600;
  cursor: pointer;
}
.feature-filter button.active { background: var(--ink); color: #fff; box-shadow: 0 3px 8px rgba(25, 49, 59, .16); }

.encoding-note {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin: 1.35rem 0 .9rem;
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
}
.encoding-note span { padding: .65rem .8rem; color: var(--muted); font-size: .78rem; }
.encoding-note span + span { border-left: 1px solid var(--line); }
.encoding-note b { margin-right: .3rem; color: var(--ink); }

.map-pair { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; min-width: 0; }
.map-card {
  min-width: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #e9eeea;
}
.map-card header {
  min-height: 78px;
  display: flex;
  justify-content: space-between;
  align-items: end;
  gap: .8rem;
  padding: .85rem 1rem;
  background: var(--surface);
}
.map-card header p {
  margin: 0 0 .18rem;
  color: var(--jiangnan);
  font: 500 .58rem var(--mono);
  text-transform: uppercase;
}
.map-card:nth-child(2) header p { color: var(--venice); }
.map-card header h3 { margin: 0; font-size: 1.15rem; line-height: 1.05; }
.map-card header > span { color: var(--muted); font: .6rem var(--mono); white-space: nowrap; }
.leaflet-map { height: 365px; width: 100%; background: #e8efed; }
.leaflet-container { font-family: var(--sans); }
.leaflet-control-zoom { overflow: hidden; border: 0 !important; border-radius: 9px !important; box-shadow: 0 4px 14px rgba(24, 49, 59, .18) !important; }
.leaflet-control-zoom a { color: var(--ink) !important; }
.leaflet-control-attribution { max-width: 72%; background: rgba(255, 255, 255, .86) !important; font-size: 9px !important; }
.leaflet-tooltip {
  border: 0;
  border-radius: 8px;
  box-shadow: 0 5px 18px rgba(24, 49, 59, .18);
  color: var(--ink);
  font: .78rem var(--sans);
}
.leaflet-tooltip b, .leaflet-tooltip small { display: block; }
.leaflet-tooltip small { color: var(--muted); }
.map-failure {
  height: 365px;
  display: grid;
  place-items: center;
  padding: 2rem;
  color: var(--muted);
  text-align: center;
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: .7rem 1.35rem;
  padding: .75rem .2rem 1.1rem;
  color: var(--muted);
  font-size: .74rem;
}
.map-legend > span { display: inline-flex; align-items: center; gap: .35rem; }
.map-legend small { margin-left: auto; font-size: .66rem; }
.water-symbol, .selected-symbol, .place-symbol { display: inline-block; flex: 0 0 auto; border-radius: 50%; }
.water-symbol { width: 14px; height: 14px; border: 3px solid var(--river); background: rgba(20, 121, 138, .18); }
.place-symbol { border: 2px solid #fff; background: var(--jiangnan); box-shadow: 0 0 0 1px var(--jiangnan); }
.place-symbol.small { width: 9px; height: 9px; }
.place-symbol.medium { width: 14px; height: 14px; margin-left: -3px; }
.place-symbol.large { width: 20px; height: 20px; margin-left: -3px; }
.selected-symbol { width: 15px; height: 15px; border: 4px solid var(--gold); background: var(--ink); }

.analysis-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(300px, .55fr);
  gap: 1rem;
  margin-top: .15rem;
}
.population-panel, .record-drawer {
  min-height: 385px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
}
.population-panel { padding: 1.2rem; }
.population-panel > header {
  display: flex;
  justify-content: space-between;
  align-items: start;
  gap: 1rem;
}
.population-panel h3 { margin: 0; font-size: 1.45rem; line-height: 1.1; }
.scale-badge {
  padding: .28rem .5rem;
  border-radius: 999px;
  background: var(--river-soft);
  color: var(--river);
  font: 500 .58rem var(--mono);
  white-space: nowrap;
}
.population-chart { min-height: 255px; margin-top: .6rem; }
.population-chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.population-chart [data-record-id] { cursor: pointer; }
.population-chart [data-record-id]:hover circle,
.population-chart [data-record-id]:focus circle,
.population-chart [data-record-id].current circle { stroke: var(--gold); stroke-width: 5; }
.chart-grid { stroke: #dbe3e0; stroke-width: 1; }
.chart-baseline { stroke: #8ba09f; stroke-width: 1.2; }
.chart-tick { fill: var(--muted); font: 10px var(--mono); }
.chart-name { fill: var(--ink); font: 600 14px var(--sans); }
.chart-region { fill: var(--muted); font: 10px var(--mono); }
.chart-value { fill: var(--ink); font: 600 11px var(--mono); }
.chart-stem { stroke: #a8bab7; stroke-width: 2; }
.chart-note { margin: .1rem 0 0; padding-top: .85rem; border-top: 1px solid var(--line); color: var(--muted); font-size: .76rem; }
.chart-note b { color: var(--ink); }

.record-drawer { padding: 1.15rem; background: #edf3f1; }
.drawer-empty { color: var(--muted); }
.record-drawer .drawer-tag {
  display: inline-block;
  padding: .25rem .5rem;
  border-radius: 999px;
  background: var(--jiangnan-soft);
  color: var(--jiangnan);
  font: 500 .6rem var(--mono);
  text-transform: uppercase;
}
.record-drawer[data-region="Venice"] .drawer-tag { background: #ece8f5; color: var(--venice); }
.record-drawer h3 { margin: .95rem 0 .15rem; font-size: 1.8rem; line-height: 1; }
.record-drawer > p { margin: .4rem 0; color: var(--muted); font-size: .82rem; }
.record-drawer dl { margin: .9rem 0; border-top: 1px solid var(--line); }
.record-drawer dl div {
  display: grid;
  grid-template-columns: 1fr 1.2fr;
  gap: .6rem;
  padding: .46rem 0;
  border-bottom: 1px solid var(--line);
}
.record-drawer dt { color: var(--muted); font-size: .71rem; }
.record-drawer dd { margin: 0; font: .7rem var(--mono); overflow-wrap: anywhere; }
.record-drawer a {
  display: inline-block;
  padding: .56rem .72rem;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-weight: 600;
  text-decoration: none;
}

.record-search { width: min(300px, 100%); color: var(--muted); font-size: .75rem; }
.record-search input {
  width: 100%;
  min-height: 42px;
  margin-top: .35rem;
  border: 1px solid var(--line-dark);
  border-radius: 8px;
  padding: .6rem .7rem;
  background: #fff;
  color: var(--ink);
}
.table-wrap { overflow-x: auto; margin-top: 1.5rem; border: 1px solid var(--line); border-radius: 12px; }
table { width: 100%; border-collapse: collapse; background: #fff; }
th, td { padding: .75rem .68rem; border-bottom: 1px solid var(--line); text-align: left; vertical-align: top; }
th { position: sticky; top: 0; background: var(--ink); color: #fff; font: 500 .62rem var(--mono); text-transform: uppercase; }
td { font-size: .8rem; }
td code { font-size: .68rem; }
tbody tr:hover { background: #f0f6f4; }
.class-chip { display: inline-flex; padding: .18rem .38rem; border-radius: 999px; background: var(--river-soft); font: .64rem var(--mono); }
.method-note { padding: .75rem .85rem; border-left: 4px solid var(--gold); background: #fff7dc; color: var(--muted); font-size: .8rem; }

.boundary-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1rem; margin-top: 1.6rem; }
.boundary-grid article {
  min-height: 315px;
  display: flex;
  flex-direction: column;
  padding: 1.2rem;
  border: 1px solid var(--line);
  border-top: 5px solid var(--river);
  border-radius: 12px;
  background: #edf7f5;
}
.boundary-grid article.partial { border-top-color: var(--gold); background: #fff8df; }
.boundary-grid article.unsupported { border-top-color: var(--jiangnan); background: #fcf0ec; }
.boundary-grid span { font: 500 .62rem var(--mono); letter-spacing: .07em; }
.boundary-grid h3 { margin: 1.3rem 0 .55rem; font-size: 1.35rem; line-height: 1.05; }
.boundary-grid p { margin: 0; color: var(--muted); font-size: .86rem; }
.boundary-grid b { margin-top: auto; padding-top: 1.3rem; font-size: .78rem; text-transform: uppercase; }
.provenance-strip {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  margin-top: 1rem;
  overflow: hidden;
  border-radius: 12px;
  background: var(--ink);
  color: #fff;
}
.provenance-strip div { min-width: 0; padding: .8rem; }
.provenance-strip div + div { border-left: 1px solid #42606a; }
.provenance-strip span { display: block; color: #a5bdc1; font: .58rem var(--mono); text-transform: uppercase; }
.provenance-strip a, .provenance-strip code { display: block; margin-top: .3rem; color: #94dfda; font-size: .74rem; overflow-wrap: anywhere; }

footer {
  display: flex;
  justify-content: space-between;
  gap: 2rem;
  padding: 1.1rem clamp(1rem, 4vw, 3.5rem);
  background: var(--ink);
  color: #bfd0d2;
  font-size: .74rem;
}
footer p { max-width: 820px; margin: 0; }
#loadStatus { font-family: var(--mono); text-align: right; }

@media (max-width: 980px) {
  .opening { grid-template-columns: 1fr; gap: 1.4rem; }
  .source-facts { max-width: 620px; }
  .analysis-grid { grid-template-columns: 1fr; }
  .record-drawer { min-height: auto; }
}
@media (max-width: 760px) {
  .masthead { align-items: flex-start; }
  .source-chip span { display: none; }
  .opening { min-height: auto; padding-top: 2.6rem; }
  h1 { font-size: clamp(2.7rem, 14vw, 4.4rem); }
  .view-tabs { grid-template-columns: 1fr; }
  .view-tabs button { min-height: 68px; }
  .view-tabs small { display: none; }
  .view-head { align-items: stretch; flex-direction: column; gap: 1rem; }
  .feature-filter { grid-template-columns: repeat(3, 1fr); }
  .encoding-note, .map-pair, .boundary-grid, .provenance-strip { grid-template-columns: 1fr; }
  .encoding-note span + span, .provenance-strip div + div { border-left: 0; border-top: 1px solid var(--line); }
  .leaflet-map { height: 330px; }
  .map-legend small { width: 100%; margin-left: 0; }
  .source-facts { grid-template-columns: 1fr; }
  .source-facts div + div { border-top: 1px solid var(--line); }
  footer { flex-direction: column; }
  #loadStatus { text-align: left; }
}
@media (max-width: 480px) {
  .wordmark small { display: none; }
  .source-chip { font-size: .7rem; }
  .view-panel { padding: 1rem; }
  .population-panel { padding: .9rem; overflow-x: auto; }
  .population-chart svg { min-width: 620px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .001ms !important; }
}
