:root {
  --ink: #17211d;
  --muted: #5f6c64;
  --line: #d7ded7;
  --panel: #f7faf6;
  --paper: #ffffff;
  --water: #327a8a;
  --field: #518d55;
  --basalt: #2f3634;
  --rust: #bd5431;
  --amber: #c28b2c;
  --shadow: 0 16px 38px rgba(20, 30, 25, 0.16);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  color: var(--ink);
  font-family:
    Inter, Arial, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
  background: #dce5df;
}

button,
input {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 430px);
  min-height: 100%;
}

.sidebar {
  z-index: 700;
  grid-column: 2;
  display: flex;
  min-height: 100dvh;
  max-height: 100dvh;
  flex-direction: column;
  gap: 14px;
  overflow: hidden;
  border-left: 1px solid var(--line);
  background: rgba(247, 250, 246, 0.96);
  box-shadow: var(--shadow);
}

.map-shell {
  grid-column: 1;
  grid-row: 1;
  position: relative;
  min-width: 0;
  min-height: 100dvh;
}

.app-header {
  padding: 22px 22px 0;
}

.eyebrow {
  margin: 0 0 8px;
  color: var(--water);
  font-size: 0.73rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1 {
  margin: 0;
  font-size: 1.65rem;
  line-height: 1.08;
  letter-spacing: 0;
}

.bounds-label {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.controls {
  display: grid;
  gap: 10px;
  padding: 0 22px;
}

.search-label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

#town-search {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--ink);
  background: var(--paper);
  outline: none;
}

#town-search:focus {
  border-color: var(--water);
  box-shadow: 0 0 0 3px rgba(50, 122, 138, 0.16);
}

.filter-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.filter-button {
  min-height: 36px;
  cursor: pointer;
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--basalt);
  background: var(--paper);
  font-size: 0.84rem;
  font-weight: 750;
}

.filter-button.active {
  border-color: var(--water);
  color: #fff;
  background: var(--water);
}

.list-summary {
  padding: 0 22px;
  color: var(--muted);
  font-size: 0.82rem;
}

.town-list {
  display: grid;
  gap: 7px;
  min-height: 138px;
  max-height: 34vh;
  overflow: auto;
  padding: 0 16px 2px 22px;
  scrollbar-width: thin;
}

.town-button {
  display: grid;
  grid-template-columns: 11px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  width: 100%;
  min-height: 44px;
  cursor: pointer;
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--ink);
  text-align: right;
  background: transparent;
}

.town-button:hover,
.town-button[aria-current="true"] {
  border-color: var(--line);
  background: var(--paper);
}

.town-button[aria-current="true"] {
  box-shadow: 0 8px 18px rgba(23, 33, 29, 0.08);
}

.town-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--field);
  box-shadow: 0 0 0 3px rgba(81, 141, 85, 0.15);
}

.town-dot.town {
  background: var(--rust);
  box-shadow: 0 0 0 3px rgba(189, 84, 49, 0.16);
}

.town-dot.hamlet {
  background: var(--amber);
  box-shadow: 0 0 0 3px rgba(194, 139, 44, 0.16);
}

.town-name {
  overflow: hidden;
  font-size: 0.93rem;
  font-weight: 780;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.town-meta {
  color: var(--muted);
  font-size: 0.78rem;
}

.town-detail {
  min-height: 0;
  overflow: auto;
  border-top: 1px solid var(--line);
  padding: 18px 22px 20px;
  background: var(--paper);
}

.detail-kicker {
  margin: 0 0 6px;
  color: var(--water);
  font-size: 0.72rem;
  font-weight: 800;
  letter-spacing: 0;
  text-transform: uppercase;
}

.detail-title {
  margin: 0;
  font-size: 1.45rem;
  line-height: 1.12;
  letter-spacing: 0;
}

.detail-subtitle {
  margin: 8px 0 14px;
  color: var(--muted);
  font-size: 0.9rem;
}

.chip-row {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 16px;
}

.chip {
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 8px;
  color: var(--basalt);
  background: #f6f3ea;
  font-size: 0.76rem;
  font-weight: 760;
}

.detail-section {
  margin-top: 16px;
}

.detail-section h2 {
  margin: 0 0 7px;
  font-size: 0.86rem;
  letter-spacing: 0;
}

.detail-section p {
  margin: 0;
  color: #30403a;
  font-size: 0.92rem;
  line-height: 1.45;
}

.detail-list {
  display: grid;
  gap: 7px;
  margin: 0;
  padding-left: 18px;
  color: #30403a;
  font-size: 0.9rem;
  line-height: 1.38;
}

.event-list li {
  display: grid;
  gap: 4px;
}

.event-list strong {
  color: var(--basalt);
}

.event-list small,
.subtle-note {
  color: var(--muted);
  font-size: 0.8rem;
}

.source-links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 18px;
}

.source-links a {
  border-bottom: 1px solid rgba(50, 122, 138, 0.35);
  color: var(--water);
  font-size: 0.8rem;
  font-weight: 720;
  text-decoration: none;
}

.source-note {
  border-top: 1px solid var(--line);
  padding: 12px 22px 16px;
  color: var(--muted);
  background: #f1f5ef;
  font-size: 0.8rem;
  line-height: 1.42;
}

.source-note summary {
  cursor: pointer;
  color: var(--basalt);
  font-weight: 800;
}

.source-note p {
  margin: 9px 0 0;
}

#map {
  width: 100%;
  height: 100dvh;
  background: #d8e2dd;
}

.legend {
  position: absolute;
  right: 16px;
  bottom: 18px;
  z-index: 650;
  display: grid;
  gap: 8px;
  min-width: 138px;
  border: 1px solid rgba(23, 33, 29, 0.12);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--basalt);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 10px 26px rgba(23, 33, 29, 0.15);
  font-size: 0.79rem;
  font-weight: 720;
}

.legend div {
  display: flex;
  align-items: center;
  gap: 8px;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--field);
}

.legend-dot.town {
  background: var(--rust);
}

.legend-dot.hamlet {
  background: var(--amber);
}

.legend-line {
  display: inline-block;
  width: 22px;
  height: 0;
  border-top: 3px solid var(--rust);
}

.settlement-marker {
  position: relative;
  width: 18px;
  height: 18px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: var(--field);
  box-shadow:
    0 2px 8px rgba(20, 30, 25, 0.28),
    0 0 0 5px rgba(81, 141, 85, 0.18);
}

.settlement-marker.town {
  background: var(--rust);
  box-shadow:
    0 2px 8px rgba(20, 30, 25, 0.28),
    0 0 0 5px rgba(189, 84, 49, 0.18);
}

.settlement-marker.major {
  width: 24px;
  height: 24px;
  border-width: 3px;
}

.settlement-marker.hamlet {
  width: 14px;
  height: 14px;
  background: var(--amber);
  box-shadow:
    0 2px 8px rgba(20, 30, 25, 0.24),
    0 0 0 4px rgba(194, 139, 44, 0.18);
}

.settlement-marker.selected {
  background: var(--amber);
  box-shadow:
    0 4px 12px rgba(20, 30, 25, 0.3),
    0 0 0 8px rgba(194, 139, 44, 0.28);
}

.grid-label {
  color: #44524b;
  font-size: 0.68rem;
  font-weight: 760;
  text-shadow:
    0 1px 0 #fff,
    1px 0 0 #fff,
    0 -1px 0 #fff,
    -1px 0 0 #fff;
}

.leaflet-popup-content-wrapper {
  border-radius: 8px;
}

.leaflet-popup-content {
  margin: 12px 14px;
  color: var(--ink);
  font-family:
    Inter, Arial, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
  text-align: right;
}

.popup-title {
  margin: 0 0 4px;
  font-weight: 850;
}

.popup-meta {
  margin: 0;
  color: var(--muted);
  font-size: 0.82rem;
}

@media (max-width: 880px) {
  html,
  body {
    height: auto;
    min-height: 100%;
    overflow-x: hidden;
  }

  .app {
    display: flex;
    min-height: 100svh;
    flex-direction: column;
  }

  .map-shell {
    order: 1;
    min-height: 68svh;
    height: 68svh;
  }

  #map {
    height: 68svh;
  }

  .sidebar {
    order: 2;
    position: relative;
    right: auto;
    bottom: auto;
    left: auto;
    min-height: auto;
    max-height: none;
    border-top: 1px solid var(--line);
    border-left: 0;
    overflow: visible;
  }

  .app-header {
    padding: 14px 16px 0;
  }

  h1 {
    font-size: 1.25rem;
  }

  .bounds-label {
    display: none;
  }

  .controls {
    padding: 0 16px;
  }

  .town-list {
    display: flex;
    min-height: 48px;
    max-height: none;
    gap: 8px;
    overflow-x: auto;
    overflow-y: visible;
    padding: 0 16px 8px;
  }

  .town-button {
    grid-template-columns: 9px max-content;
    min-width: max-content;
    min-height: 40px;
  }

  .town-meta {
    display: none;
  }

  .list-summary {
    padding: 0 16px;
  }

  .town-detail {
    overflow: visible;
    padding: 14px 16px 18px;
  }

  .source-note {
    display: none;
  }

  .legend {
    top: auto;
    right: 14px;
    bottom: 14px;
  }
}

@media (max-width: 520px) {
  .filter-row {
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .filter-button {
    min-width: 0;
    padding: 0 4px;
    font-size: 0.78rem;
  }

  .detail-title {
    font-size: 1.22rem;
  }

  .legend {
    font-size: 0.74rem;
  }
}
