:root {
  color-scheme: dark;
  --bg: #090d0b;
  --ink: #edf4ef;
  --muted: #9bad9f;
  --panel: #111815;
  --panel-soft: #17211c;
  --line: #28362e;
  --topbar: rgba(13, 18, 16, 0.96);
  --control-bg: #0b110f;
  --control-hover: #132219;
  --map-bg: #101714;
  --legend-bg: rgba(14, 20, 17, 0.92);
  --side-bg: #0d1411;
  --row-border: #202b25;
  --row-hover: #17221d;
  --bib-bg: #1c2b23;
  --checkpoint-border: #111815;
  --tooltip-bg: #111815;
  --attribution-bg: rgba(13, 20, 17, 0.88);
  --brand-mark-ink: #08100c;
  --focus: rgba(241, 196, 91, 0.28);
  --course: #48d188;
  --course-shadow: #1a6c47;
  --men: #43b7e8;
  --women: #f05a9d;
  --inactive: #7f897e;
  --accent: #f1c45b;
  --danger: #ef7669;
  --shadow: 0 18px 45px rgba(0, 0, 0, 0.34);
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #f6f8f3;
  --ink: #111a15;
  --muted: #5d6d62;
  --panel: #ffffff;
  --panel-soft: #eef5ed;
  --line: #d7e1d8;
  --topbar: rgba(250, 252, 247, 0.96);
  --control-bg: #ffffff;
  --control-hover: #eef7ef;
  --map-bg: #eef2ea;
  --legend-bg: rgba(255, 255, 255, 0.92);
  --side-bg: #f5f8f2;
  --row-border: #e3ebe2;
  --row-hover: #eef7ef;
  --bib-bg: #e4f3e9;
  --checkpoint-border: #ffffff;
  --tooltip-bg: #ffffff;
  --attribution-bg: rgba(255, 255, 255, 0.9);
  --brand-mark-ink: #08100c;
  --focus: rgba(39, 129, 82, 0.24);
  --course: #0f9d61;
  --course-shadow: #9fd8b4;
  --men: #1679ad;
  --women: #cf3177;
  --inactive: #869087;
  --accent: #b47a06;
  --danger: #c8493f;
  --shadow: 0 16px 36px rgba(25, 42, 31, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  overflow: hidden;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
select {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-rows: auto 1fr;
  height: 100vh;
  min-height: 0;
  overflow: hidden;
}

.topbar {
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  min-height: 78px;
  padding: 12px 16px;
  background: var(--topbar);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 3px 18px rgba(0, 0, 0, 0.28);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 236px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--course);
  color: var(--brand-mark-ink);
  font-weight: 800;
  letter-spacing: 0;
  text-decoration: none;
}

.brand h1,
.panel-heading h2 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1 {
  font-size: 1.18rem;
  line-height: 1.1;
}

.brand a:not(.brand-mark) {
  color: inherit;
  text-decoration-color: rgba(72, 209, 136, 0.46);
  text-underline-offset: 3px;
}

.brand a:not(.brand-mark):hover {
  color: var(--course);
  text-decoration-color: currentColor;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
  white-space: nowrap;
}

.toolbar {
  display: flex;
  align-items: end;
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.control {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

.control input,
.control select {
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--control-bg);
  color: var(--ink);
  padding: 0 10px;
  min-width: 118px;
}

.search-control input {
  width: min(260px, 31vw);
}

.control input:focus,
.control select:focus,
.icon-button:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 1px;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--control-bg);
  color: var(--course);
  cursor: pointer;
}

.icon-button:hover {
  border-color: var(--course);
  background: var(--control-hover);
}

.icon-button svg {
  width: 18px;
  height: 18px;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  min-height: 0;
  overflow: hidden;
}

.map-wrap {
  position: relative;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

#map {
  width: 100%;
  height: 100%;
  min-height: 0;
  background: var(--map-bg);
}

.map-legend {
  position: absolute;
  left: 14px;
  bottom: 22px;
  z-index: 450;
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--legend-bg);
  color: var(--muted);
  font-size: 0.8rem;
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.28);
}

.legend-dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 5px;
  border-radius: 50%;
  vertical-align: -1px;
}

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

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

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

.side-panel {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 14px;
  min-width: 0;
  min-height: 0;
  padding: 14px;
  border-left: 1px solid var(--line);
  background: var(--side-bg);
  overflow: hidden;
}

.estimate-note {
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: var(--muted);
  font-size: 0.82rem;
  box-shadow: var(--shadow);
}

.stats-band {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stats-band div,
.runner-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.stats-band div {
  min-width: 0;
  padding: 10px;
}

.stats-band strong {
  display: block;
  font-size: 1.08rem;
  line-height: 1.15;
}

.stats-band span {
  color: var(--muted);
  font-size: 0.74rem;
}

.runner-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
}

.panel-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 12px 10px;
  border-bottom: 1px solid var(--line);
}

.panel-heading h2 {
  font-size: 0.95rem;
}

.panel-heading span {
  color: var(--muted);
  font-size: 0.75rem;
  text-align: right;
}

.runner-list {
  min-height: 0;
  overflow: auto;
}

.runner-row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  border-bottom: 1px solid var(--row-border);
  background: transparent;
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}

.runner-row:hover,
.runner-row:focus-visible {
  background: var(--row-hover);
  outline: none;
}

.bib-pill {
  display: inline-flex;
  justify-content: center;
  min-width: 36px;
  padding: 3px 5px;
  border-radius: 6px;
  background: var(--bib-bg);
  color: var(--course);
  font-size: 0.78rem;
  font-weight: 800;
}

.runner-main {
  min-width: 0;
}

.runner-name {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 750;
}

.runner-meta {
  display: block;
  overflow: hidden;
  color: var(--muted);
  font-size: 0.76rem;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.runner-record {
  display: block;
  overflow: hidden;
  color: var(--accent);
  font-size: 0.74rem;
  font-weight: 750;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.runner-record.behind {
  color: var(--danger);
}

.runner-mile {
  color: var(--course);
  font-size: 0.78rem;
  font-weight: 800;
  white-space: nowrap;
}

.empty-state,
.error-state {
  padding: 18px 12px;
  color: var(--muted);
  font-size: 0.9rem;
}

.error-state {
  color: var(--danger);
}

.checkpoint-icon {
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border: 2px solid var(--checkpoint-border);
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.28);
}

.leaflet-tooltip.runner-tooltip {
  border: 0;
  border-radius: 8px;
  padding: 0;
  background: transparent;
  box-shadow: none;
}

.tooltip-card {
  min-width: 220px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--tooltip-bg);
  color: var(--ink);
  box-shadow: 0 14px 32px rgba(0, 0, 0, 0.44);
}

.tooltip-card strong {
  display: block;
  margin-bottom: 2px;
}

.tooltip-card span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.35;
}

.leaflet-control-attribution {
  border-radius: 6px 0 0 0;
  background: var(--attribution-bg) !important;
  color: var(--muted);
  font-size: 10px;
}

.leaflet-control-attribution a {
  color: var(--course);
}

.leaflet-bar {
  border-color: var(--line) !important;
  box-shadow: var(--shadow);
}

.leaflet-bar a,
.leaflet-bar a:hover {
  border-bottom-color: var(--line);
  background: var(--panel);
  color: var(--ink);
}

.leaflet-bar a:hover {
  background: var(--panel-soft);
}

.creator-credit {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: var(--muted);
  font-size: 0.78rem;
}

.creator-credit a {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  color: var(--course);
  font-weight: 800;
  text-decoration: none;
}

.creator-credit a:hover {
  text-decoration: underline;
  text-underline-offset: 3px;
}

.creator-credit svg {
  width: 14px;
  height: 14px;
  fill: currentColor;
}

@media (max-width: 980px) {
  body {
    overflow: auto;
  }

  .app-shell {
    height: auto;
    min-height: 100vh;
    overflow: visible;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .toolbar {
    justify-content: flex-start;
  }

  .workspace {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(58vh, 1fr) auto;
    overflow: visible;
  }

  .map-wrap,
  #map {
    height: 58vh;
    min-height: 58vh;
  }

  .side-panel {
    border-top: 1px solid var(--line);
    border-left: 0;
    overflow: visible;
  }

  .runner-panel {
    max-height: 42vh;
  }
}

@media (max-width: 640px) {
  .topbar {
    gap: 8px;
    min-height: 0;
    padding: 8px 10px;
  }

  .brand {
    gap: 10px;
    min-width: 0;
  }

  .brand-mark {
    width: 36px;
    height: 36px;
    font-size: 0.88rem;
  }

  .brand h1 {
    font-size: 1.02rem;
  }

  .brand p {
    margin-top: 2px;
    font-size: 0.76rem;
    white-space: normal;
  }

  .toolbar {
    display: flex;
    align-items: stretch;
    flex-wrap: wrap;
    gap: 6px;
    width: 100%;
    min-width: 0;
  }

  .control {
    gap: 0;
    min-width: 0;
  }

  .control > span {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .search-control input,
  .control input,
  .control select {
    height: 34px;
    width: 100%;
    min-width: 0;
    padding: 0 8px;
    font-size: 0.82rem;
  }

  .search-control {
    order: 1;
    flex: 1 0 100%;
    min-width: 0;
  }

  .gender-control {
    order: 4;
  }

  .status-control {
    order: 5;
  }

  .scope-control {
    order: 6;
  }

  .gender-control,
  .status-control {
    flex: 1 1 calc((100% - 6px) / 2);
    min-width: 0;
  }

  .scope-control {
    flex: 1 0 100%;
    min-width: 0;
  }

  #refreshButton {
    order: 7;
  }

  #fitButton {
    display: none;
  }

  #themeButton {
    order: 8;
  }

  .icon-button {
    flex: 1 1 calc((100% - 6px) / 2);
    height: 32px;
    min-width: 0;
    width: auto;
  }

  .stats-band {
    display: none;
  }

  .side-panel {
    gap: 10px;
    padding: 0 10px 10px;
  }

  .estimate-note {
    display: none;
  }

  .panel-heading {
    padding: 10px;
  }

  .runner-row {
    padding: 8px 10px;
  }

  .workspace {
    grid-template-rows: auto auto;
  }

  .map-wrap,
  #map {
    height: 40vh;
    min-height: 280px;
  }

  .map-legend {
    left: 10px;
    right: 10px;
    bottom: 12px;
    justify-content: center;
  }
}
