:root {
  --bg: #0c1418;
  --surface: #131e23;
  --surface-2: #1a262c;
  --line: #243239;
  --text: #e7ecee;
  --muted: #8aa0a8;
  --accent: #5cd3c4;
  --fz2: #69a8ff;   /* medium-risk blue */
  --fz3: #f25c7a;   /* high-risk red-pink */
  --town: #f0c36a;  /* built-up amber */
  --bld:  #ffd66b;
  --defence: #7be39a;     /* defence line - bright green */
  --behind:  #3b8a5a;     /* protected zone fill - muted green */
  --gap:     #ff9560;     /* unprotected hotspot - amber */
  --avoided: #7be39a;     /* damage avoided KPI - matches defence */
  --rail:    #c9c2b8;     /* base railway colour */
  --rail-risk: #f25c7a;   /* railway segment in FZ3 - same red as FZ3 */
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: 'Satoshi', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-feature-settings: "ss01","ss02";
}

body {
  display: grid;
  grid-template-columns: 380px 1fr;
}

#sidebar {
  background: var(--surface);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 24px 22px 16px;
  display: flex;
  flex-direction: column;
  gap: 22px;
  position: relative;
  z-index: 500;
}

#sidebar header .logo {
  display: flex;
  gap: 12px;
  align-items: center;
  color: var(--accent);
}
#sidebar header h1 {
  font-weight: 900;
  font-size: 20px;
  letter-spacing: -0.01em;
  color: var(--text);
  line-height: 1;
}
.kicker {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
  margin-top: 4px;
}

.kpis {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.kpi {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  position: relative;
}
.kpi:first-child { grid-column: span 2; }
.kpi-label {
  font-size: 11px;
  letter-spacing: 0.02em;
  color: var(--muted);
  text-transform: uppercase;
}
.kpi-label small { text-transform: none; letter-spacing: 0; opacity: .7; font-size: 10px;}
.kpi-value {
  font-family: 'JetBrains Mono', monospace;
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}
.kpi-sub {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--muted);
}
.kpi-fz3 .kpi-value { color: var(--fz3); }
.kpi-fz2 .kpi-value { color: var(--fz2); }
.kpi-ead .kpi-value, .kpi-npv .kpi-value { color: var(--accent); font-size: 18px; }
.kpi-avoided .kpi-value { color: var(--avoided); font-size: 18px; }
.kpi-gap .kpi-value { color: var(--gap); font-size: 18px; }
.kpi-avoided { border-left: 2px solid var(--avoided); }
.kpi-gap     { border-left: 2px solid var(--gap); }

.econ h2 {
  display: flex; align-items: center; justify-content: space-between;
}
.econ h2 button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.econ h2 button:hover { color: var(--accent); border-color: var(--accent); }
.econ-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}
.cc-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  cursor: pointer;
  padding: 8px 10px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  user-select: none;
}
.cc-toggle small { color: var(--muted); font-size: 10px; display: block; margin-top: 2px; }
.cc-toggle span { line-height: 1.3; }

#method-modal {
  position: fixed;
  inset: 0;
  background: rgba(8,14,17,0.78);
  backdrop-filter: blur(6px);
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
#method-modal[hidden] { display: none; }
.modal-inner {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 28px 32px;
  max-width: 640px;
  width: 100%;
  max-height: 88vh;
  overflow-y: auto;
  position: relative;
  font-size: 13px;
  line-height: 1.55;
}
.modal-inner h2 { font-size: 18px; font-weight: 900; margin-bottom: 6px; color: var(--text); text-transform: none; letter-spacing: 0; }
.modal-inner h3 { font-size: 11px; text-transform: uppercase; letter-spacing: 0.14em; color: var(--accent); margin-top: 18px; margin-bottom: 6px; font-weight: 700; }
.modal-inner p { color: var(--text); margin-bottom: 6px; }
.modal-inner p.lede { color: var(--muted); font-style: italic; margin-bottom: 12px; }
.modal-inner ul { list-style: none; padding-left: 0; }
.modal-inner li { padding: 3px 0; color: var(--text); font-size: 12px; }
.modal-inner code {
  background: var(--surface-2);
  padding: 1px 5px;
  border-radius: 3px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  color: var(--accent);
}
.modal-inner a { color: var(--accent); }
.modal-inner .close {
  position: absolute;
  top: 12px;
  right: 14px;
  background: transparent;
  border: none;
  color: var(--muted);
  font-size: 28px;
  cursor: pointer;
  line-height: 1;
}
.modal-inner .close:hover { color: var(--text); }

section h2 {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--muted);
  margin-bottom: 10px;
  font-weight: 700;
}

.layers {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.layers label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  cursor: pointer;
  user-select: none;
  padding: 4px 0;
}
.layers small { color: var(--muted); font-size: 11px; }

input[type=checkbox] {
  appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 4px;
  border: 1.5px solid var(--line);
  background: var(--bg);
  position: relative;
  cursor: pointer;
}
input[type=checkbox]:checked {
  background: var(--accent);
  border-color: var(--accent);
}
input[type=checkbox]:checked::after {
  content: '';
  position: absolute;
  inset: 2px;
  background: var(--surface);
  -webkit-mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'><path fill='black' d='M10 3L4.5 8.5 2 6l1-1 1.5 1.5L9 2z'/></svg>") center/contain no-repeat;
          mask: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12'><path fill='black' d='M10 3L4.5 8.5 2 6l1-1 1.5 1.5L9 2z'/></svg>") center/contain no-repeat;
}

.dot {
  width: 12px; height: 12px;
  border-radius: 3px;
  display: inline-block;
  flex-shrink: 0;
}
.dot-fz2 { background: var(--fz2); opacity: .55; border: 1px solid var(--fz2); }
.dot-fz3 { background: var(--fz3); border: 1px solid var(--fz3); }
.dot-town { background: transparent; border: 1.5px solid var(--town); }
.dot-bld { background: var(--bld); border-radius: 50%; }
.dot-defence { background: var(--defence); border: 1px solid var(--defence); height: 4px; align-self: center; }
.dot-behind  { background: rgba(123, 227, 154, 0.28); border: 1px solid var(--behind); }
.dot-rail    { background: var(--rail); height: 3px; align-self: center; border-radius: 0; }

.towns-head {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.towns h2 { margin-bottom: 0; }
.sort-controls {
  display: flex; gap: 4px;
}
.sort-controls button {
  background: transparent;
  border: 1px solid var(--line);
  color: var(--muted);
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  padding: 4px 6px;
  border-radius: 4px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.sort-controls button:hover { color: var(--text); border-color: var(--text); }
.sort-controls button.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--surface);
}

#filter {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 12px;
  font-family: inherit;
  margin-bottom: 10px;
}
#filter:focus { outline: none; border-color: var(--accent); }

#town-list {
  display: flex;
  flex-direction: column;
  gap: 1px;
  border-radius: 6px;
  overflow: hidden;
}
.town-row {
  background: var(--surface-2);
  padding: 10px 12px;
  cursor: pointer;
  border: 1px solid transparent;
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 12px;
  align-items: center;
  transition: background .15s ease, border-color .15s ease;
}
.town-row:hover { background: #20303a; }
.town-row.active {
  border: 1px solid var(--accent);
  background: #1d2c34;
}
.town-name {
  font-weight: 700;
  font-size: 13px;
  color: var(--text);
}
.town-meta {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--muted);
  grid-column: 1 / -1;
}
.town-bar {
  grid-column: 1 / -1;
  display: flex;
  height: 5px;
  margin-top: 4px;
  border-radius: 3px;
  overflow: hidden;
  background: #0f1a1f;
}
.town-bar .seg-fz3 { background: var(--fz3); }
.town-bar .seg-fz2 { background: var(--fz2); opacity: .55; }
.town-bar .seg-protected { background: var(--avoided); opacity: .85; }
.town-bar .seg-unprot    { background: var(--gap);     opacity: .9; }
.town-bar .seg-safe { background: var(--line); flex: 1; }
.town-fz3-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  font-weight: 700;
  color: var(--accent);
  text-align: right;
  line-height: 1.1;
}
.town-fz3-num small {
  display: block;
  font-size: 9px;
  color: var(--muted);
  font-weight: 500;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.town-fz3-num.zero { color: var(--muted); font-weight: 500; opacity: .5; }
.town-fz3-num.gap { color: var(--gap); }
.town-fz3-num.avoided { color: var(--avoided); }
.town-meta .badge {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  padding: 1px 5px;
  border-radius: 3px;
  margin-left: 4px;
  letter-spacing: 0.03em;
}
.town-meta .badge.prot { background: rgba(123, 227, 154, 0.16); color: var(--avoided); }
.town-meta .badge.gap  { background: rgba(255, 149, 96, 0.16); color: var(--gap); }

/* Railway list */
.rail { margin-top: 18px; }
.rail-totals {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.04em;
}
.rail-row {
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 2px 8px;
}
.rail-row:last-child { border-bottom: 0; }
.rail-name {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
}
.rail-num {
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  color: var(--fz3);
  text-align: right;
  white-space: nowrap;
}
.rail-num.zero { color: var(--muted); opacity: .5; }
.rail-num small {
  display: block;
  font-size: 9px;
  color: var(--muted);
  font-weight: 500;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  margin-top: 1px;
}
.rail-meta {
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  color: var(--muted);
  grid-column: 1 / -1;
}
.rail-bar {
  grid-column: 1 / -1;
  height: 3px;
  display: flex;
  border-radius: 2px;
  overflow: hidden;
  margin-top: 4px;
  background: #0f1a1f;
}
.rail-bar .seg-fz3 { background: var(--fz3); }
.rail-bar .seg-fz2 { background: var(--fz2); opacity: .6; }
.rail-bar .seg-behind { background: var(--avoided); opacity: .35; }
.rail-bar .seg-safe { background: var(--line); flex: 1; }

#sidebar footer {
  margin-top: auto;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: 10px;
  color: var(--muted);
  line-height: 1.5;
}
#sidebar footer p + p { margin-top: 6px; }
#sidebar footer .warn { color: #d99860; font-weight: 600; }
#sidebar footer a { color: var(--accent); text-decoration: none; }
#sidebar footer a:hover { text-decoration: underline; }

#map {
  height: 100vh;
  background: #0a1115;
}

#legend {
  position: fixed;
  bottom: 16px;
  right: 16px;
  background: rgba(19, 30, 35, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 11px;
  color: var(--text);
  z-index: 600;
  display: flex;
  flex-direction: column;
  gap: 5px;
}
#legend div { display: flex; align-items: center; gap: 8px; }

.leaflet-container { background: #0a1115; }
.leaflet-control-attribution {
  background: rgba(19, 30, 35, 0.85) !important;
  color: var(--muted) !important;
  font-size: 10px !important;
}
.leaflet-control-attribution a { color: var(--accent) !important; }

.leaflet-popup-content-wrapper {
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  border: 1px solid var(--line);
  box-shadow: 0 6px 24px rgba(0,0,0,.4);
}
.leaflet-popup-tip { background: var(--surface) !important; }
.leaflet-popup-content {
  margin: 12px 14px;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.45;
}
.popup-title {
  font-weight: 900;
  font-size: 15px;
  margin-bottom: 6px;
}
.popup-stats {
  display: grid;
  grid-template-columns: auto auto;
  gap: 4px 12px;
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
}
.popup-stats .label { color: var(--muted); }
.popup-stats .value { color: var(--text); text-align: right; font-weight: 700; }
.popup-stats .value.fz3 { color: var(--fz3); }
.popup-stats .value.fz2 { color: var(--fz2); }
.popup-stats .value.protected { color: var(--avoided); }
.popup-stats .value.gap { color: var(--gap); }

.popup-econ {
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.popup-econ > div {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
}
.popup-econ .label {
  font-size: 11px;
  color: var(--muted);
  font-family: 'JetBrains Mono', monospace;
}
.popup-econ .value.econ {
  color: var(--accent);
  font-family: 'JetBrains Mono', monospace;
  font-size: 13px;
  font-weight: 700;
}
.popup-econ .value.avoided { color: var(--avoided); font-family: 'JetBrains Mono', monospace; font-size: 13px; font-weight: 700; }
.popup-econ .value.gap     { color: var(--gap);     font-family: 'JetBrains Mono', monospace; font-size: 13px; font-weight: 700; }
.popup-econ small { font-weight: 500; opacity: 0.65; font-size: 10px;}

#sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 700;
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
}

@media (max-width: 880px) {
  body { grid-template-columns: 1fr; grid-template-rows: 1fr; }
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 84vw;
    max-width: 380px;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 6px 0 22px rgba(0,0,0,.4);
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-toggle { display: flex; }
  #map { height: 100vh; width: 100vw; }
  #legend { bottom: 12px; right: 12px; }
}

.back-link {
  display: inline-block;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10.5px;
  color: var(--muted);
  text-decoration: none;
  letter-spacing: 0.04em;
  padding: 6px 10px;
  margin: 4px 0 16px;
  border: 1px solid var(--line);
  border-radius: 4px;
  transition: color 140ms ease, border-color 140ms ease;
}
.back-link:hover { color: #0ABAB5; border-color: #0ABAB5; }
