* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }

/* --- Accessibility utilities --- */

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  z-index: 9999;
  padding: 0.5rem 1rem;
  background: #e0e0e0;
  color: #0d0d0d;
  font-weight: 700;
  border-radius: 0 0 4px 0;
  text-decoration: none;
  font-size: 0.9rem;
}
.skip-link:focus { top: 0; }

/* Visible focus ring for all interactive elements */
:focus-visible {
  outline: 2px solid #8ab4d6;
  outline-offset: 2px;
}

body {
  font-family: 'Space Grotesk', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #0d0d0d;
  color: #e0e0e0;
}

a { color: #8ab4d6; }

#app { display: flex; flex-direction: column; height: 100vh; }

/* --- Header --- */

#header {
  display: flex;
  align-items: center;
  padding: 0.45rem 1.2rem;
  background: #111;
  border-bottom: 1px solid #222;
  gap: 1.5rem;
  flex-shrink: 0;
}

#brand h1 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  white-space: nowrap;
  line-height: 1.2;
}

.subtitle {
  font-size: 0.62rem;
  color: #555;
  display: block;
  margin-top: 1px;
}

#tabs { display: flex; gap: 4px; flex-wrap: wrap; }

.tab {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  color: #777;
  padding: 0.3rem 0.8rem;
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 500;
  transition: all 0.15s;
  white-space: nowrap;
}

.tab:hover { background: #222; color: #bbb; border-color: #444; }
.tab.active { background: #152030; color: #e0e0e0; border-color: #3a5577; }

/* --- Maps --- */

#maps { display: flex; flex: 1; min-height: 0; }

.map-wrap {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.map-wrap:first-child { border-right: 1px solid #333; }

.map-label {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  background: rgba(0,0,0,0.7);
  padding: 2px 12px;
  border-radius: 3px;
  font-size: 0.6rem;
  font-weight: 600;
  color: #888;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  pointer-events: none;
  white-space: nowrap;
}

.map { flex: 1; min-height: 0; }

/* HOLC legend on 1935 map */
#holc-legend {
  position: absolute;
  bottom: 10px;
  left: 10px;
  z-index: 1000;
}
#holc-legend img {
  width: 140px;
  border: 1px solid #333;
  border-radius: 3px;
  display: block;
}

/* --- Legend --- */

#legend {
  position: absolute;
  bottom: 10px;
  left: 10px;
  z-index: 1000;
  background: rgba(13,13,13,0.92);
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 0.68rem;
  line-height: 1.5;
  border: 1px solid #2a2a2a;
  color: #ccc;
  max-width: 170px;
}

.legend-title { font-weight: 600; margin-bottom: 2px; color: #e0e0e0; font-size: 0.7rem; }
.legend-row { display: flex; align-items: center; gap: 5px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 2px; display: inline-block; flex-shrink: 0; }
.legend-hint { margin-top: 5px; font-size: 0.55rem; color: #555; line-height: 1.3; font-style: italic; }

/* --- Bottom panel --- */

#bottom {
  display: flex;
  height: 28%;
  min-height: 110px;
  background: #111;
  border-top: 1px solid #222;
}

#narrative {
  flex: 3;
  padding: 0.7rem 1.2rem;
  overflow-y: auto;
  border-right: 1px solid #1a1a1a;
  min-width: 0;
}

/* Sub-layer selector */
#sublayer-nav {
  display: flex;
  gap: 3px;
  flex-wrap: wrap;
  margin-bottom: 0.4rem;
}
#sublayer-nav:empty { display: none; }

.sub-btn {
  background: #1a1a1a;
  border: 1px solid #282828;
  color: #666;
  padding: 2px 8px;
  font-size: 0.62rem;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
  transition: all 0.12s;
}
.sub-btn:hover { color: #999; border-color: #444; }
.sub-btn.active { background: #152030; color: #ccc; border-color: #3a5577; }

#narrative-text {
  font-size: 0.78rem;
  color: #999;
  line-height: 1.65;
}

#narrative-text em {
  color: #c27070;
  font-style: normal;
  font-weight: 600;
}

.source {
  font-size: 0.58rem;
  color: #3a3a3a;
  margin-top: 0.5rem;
  line-height: 1.4;
}

/* Tract popup detail */

.detail-header {
  font-size: 0.82rem;
  font-weight: 600;
  margin-bottom: 2px;
}

.detail-holc {
  font-size: 0.72rem;
  color: #777;
  margin: 3px 0 5px;
}

.detail-grade {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 3px;
  color: #fff;
  font-weight: 600;
  font-size: 0.65rem;
}

.detail-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 16px;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  padding: 1px 0;
  gap: 8px;
  min-width: 140px;
}

.detail-label { color: #777; }
.detail-value { color: #e0e0e0; font-weight: 500; }

/* --- Chart --- */

#chart-area {
  flex: 2;
  min-width: 200px;
  padding: 0.6rem 0.8rem;
  display: flex;
  flex-direction: column;
  overflow: visible;
}

#chart-title {
  font-size: 0.72rem;
  font-weight: 600;
  color: #bbb;
  margin-bottom: 0.3rem;
}

#chart {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 0 2px 2px;
}

#chart-note {
  font-size: 0.52rem;
  color: #444;
  margin-top: 0.2rem;
  line-height: 1.3;
}

.bar-group {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 0;
  height: 100%;
  justify-content: flex-end;
  cursor: default;
}

.bar {
  width: 65%;
  border-radius: 3px 3px 0 0;
  transition: height 0.4s ease, opacity 0.15s ease, width 0.15s ease;
  min-height: 1px;
  opacity: 0.75;
}

.bar-group:hover .bar { opacity: 1; width: 80%; }

.bar-val {
  font-size: 0.58rem;
  color: #666;
  margin-bottom: 2px;
  white-space: nowrap;
  transition: color 0.15s ease;
  flex-shrink: 0;
}

.bar-group:hover .bar-val { color: #ccc; }

.bar-label {
  font-size: 0.55rem;
  color: #4a4a4a;
  margin-top: 3px;
  text-align: center;
  line-height: 1.2;
  max-width: 100%;
  white-space: pre-line;
  transition: color 0.15s ease;
  flex-shrink: 0;
}

.bar-group:hover .bar-label { color: #999; }

/* --- Popups --- */

.leaflet-popup-content-wrapper {
  background: rgba(20,20,20,0.95);
  color: #ddd;
  border-radius: 5px;
  font-size: 0.8rem;
  border: 1px solid #333;
  font-family: 'Space Grotesk', sans-serif;
}
.leaflet-popup-tip { background: rgba(20,20,20,0.95); }

/* --- Mobile --- */

@media (max-width: 900px) {
  #header {
    flex-direction: column;
    align-items: flex-start;
    padding: 0.4rem 0.8rem;
    gap: 0.3rem;
  }
  #tabs { gap: 3px; }
  .tab { padding: 0.25rem 0.6rem; font-size: 0.68rem; }
  #maps { flex-direction: column; }
  .map-wrap:first-child { border-right: none; border-bottom: 1px solid #333; }
  #bottom { flex-direction: column; height: auto; max-height: 38vh; }
  #narrative { border-right: none; border-bottom: 1px solid #222; }
  #chart-area { width: 100%; height: 140px; flex: none; }
}

@media (max-width: 600px) {
  #bottom { max-height: 32vh; }
  #narrative-text { font-size: 0.7rem; }
  .sub-btn { font-size: 0.58rem; padding: 2px 6px; }
  .tab { padding: 0.2rem 0.45rem; font-size: 0.62rem; }
  #chart-area { height: 120px; }
}

/* --- Accessible text-view toggle button --- */

#a11y-toggle {
  margin-left: auto;
  background: #1a1a1a;
  border: 1px solid #3a5577;
  color: #8ab4d6;
  padding: 0.3rem 0.8rem;
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.72rem;
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}
#a11y-toggle:hover { background: #222; border-color: #5a7599; }
#a11y-toggle[aria-pressed="true"] { background: #152030; color: #e0e0e0; }

/* --- Tract summary panel --- */

#tract-summary {
  margin-top: 0.6rem;
  padding: 0.6rem 0.8rem;
  background: #161c24;
  border: 1px solid #2e4060;
  border-radius: 5px;
}

.tract-summary-heading {
  font-size: 0.85rem;
  font-weight: 700;
  color: #e0e0e0;
  margin-bottom: 0.3rem;
}

.tract-summary-body {
  font-size: 0.75rem;
  color: #aaa;
  line-height: 1.55;
  margin-bottom: 0.4rem;
}

.tract-summary-metrics {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 16px;
}

.tract-summary-pair {
  display: flex;
  gap: 6px;
  font-size: 0.75rem;
  padding: 1px 0;
}
.tract-summary-pair dt { color: #777; }
.tract-summary-pair dd { color: #e0e0e0; font-weight: 500; }

#tract-summary-close {
  margin-top: 0.5rem;
  background: none;
  border: 1px solid #333;
  color: #666;
  padding: 2px 8px;
  border-radius: 3px;
  cursor: pointer;
  font-family: inherit;
  font-size: 0.65rem;
}
#tract-summary-close:hover { color: #bbb; border-color: #555; }

/* --- Tract select (keyboard nav) --- */

#tract-select-wrap {
  padding: 0.3rem 1.2rem;
  background: #111;
  border-top: 1px solid #1a1a1a;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

#tract-select {
  background: #1a1a1a;
  border: 1px solid #2a2a2a;
  color: #ccc;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  font-family: inherit;
  font-size: 0.72rem;
  max-width: 320px;
  width: 100%;
}

/* --- Text view (accessible table mode) --- */

#text-view {
  flex: 1;
  overflow-y: auto;
  padding: 1.2rem 1.5rem;
  background: #0d0d0d;
}

#text-view:focus { outline: none; }

.text-view-heading {
  font-size: 1rem;
  font-weight: 700;
  color: #e0e0e0;
  margin-bottom: 1rem;
}

.text-view-story {
  margin-bottom: 1.5rem;
}

.text-view-story-title {
  font-size: 0.85rem;
  font-weight: 600;
  color: #8ab4d6;
  cursor: pointer;
  padding: 0.3rem 0;
  list-style: none;
}
.text-view-story-title::-webkit-details-marker { display: none; }
.text-view-story-title::before { content: '\25B6\00A0'; font-size: 0.6rem; }
details[open] .text-view-story-title::before { content: '\25BC\00A0'; }

.text-view-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 0.6rem;
  margin-bottom: 1rem;
  font-size: 0.75rem;
}

.text-view-table caption {
  text-align: left;
  font-size: 0.7rem;
  font-weight: 600;
  color: #777;
  padding-bottom: 4px;
  caption-side: top;
}

.text-view-table th,
.text-view-table td {
  border: 1px solid #222;
  padding: 0.25rem 0.5rem;
  text-align: left;
  vertical-align: top;
}

.text-view-table thead th {
  background: #1a1a1a;
  color: #ccc;
  font-weight: 600;
}

.text-view-table tbody tr:nth-child(even) { background: #111; }
.text-view-table tbody th { color: #bbb; font-weight: 500; }
.text-view-table tbody td { color: #999; }
