:root {
  color-scheme: light;
  --bg: #f4f6f7;
  --panel: #ffffff;
  --panel-strong: #f9faf9;
  --text: #17201f;
  --muted: #66716f;
  --line: #dce3e1;
  --teal: #0e9384;
  --coral: #ff5b4f;
  --amber: #f6b23e;
  --shadow: 0 20px 70px rgba(22, 32, 31, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

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

button,
input,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 312px minmax(0, 1fr);
  min-height: 100vh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
  min-height: 100vh;
  padding: 22px;
  border-right: 1px solid var(--line);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 13px;
  min-height: 52px;
}

.brand-logo {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  box-shadow: 0 10px 26px rgba(20, 32, 31, 0.22);
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.1;
}

.brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.panel {
  padding: 16px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
}

.upload-panel form {
  display: grid;
  gap: 12px;
  border-radius: 8px;
  transition: box-shadow 160ms ease;
}

.upload-panel form.drag-over {
  box-shadow: 0 0 0 4px rgba(255, 91, 79, 0.14);
}

.drop-zone {
  display: grid;
  gap: 8px;
  place-items: center;
  min-height: 154px;
  padding: 20px;
  border: 1px dashed #aebbb8;
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  text-align: center;
  transition: border-color 160ms ease, background 160ms ease, box-shadow 160ms ease, transform 160ms ease;
}

.drop-zone.drag-over {
  border-color: var(--coral);
  border-style: solid;
  background: #ffece8;
  box-shadow: inset 0 0 0 2px rgba(255, 91, 79, 0.28), 0 12px 34px rgba(255, 91, 79, 0.16);
  transform: translateY(-1px);
}

.drop-zone.drag-over .upload-icon {
  background: var(--coral);
  color: #ffffff;
}

.drop-zone strong {
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
}

.drop-zone small {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

.upload-icon {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 8px;
  background: #fff1ee;
  color: var(--coral);
  font-size: 24px;
  font-weight: 800;
}

#fitFile {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.primary-button {
  min-height: 42px;
  border: 0;
  border-radius: 8px;
  background: var(--coral);
  color: #ffffff;
  font-size: 14px;
  font-weight: 800;
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.secondary-button {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
}

.export-button {
  width: 100%;
  margin-top: 12px;
}

.app-version {
  display: grid;
  gap: 4px;
  margin-top: auto;
  padding-top: 4px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}

.app-version strong {
  color: var(--text);
  font-weight: 800;
}

.app-version a {
  color: var(--teal);
  text-decoration: none;
}

.app-version a:hover {
  text-decoration: underline;
}

.helper-text {
  min-height: 38px;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.helper-text.error {
  color: #b83e35;
}

.route-progress {
  display: grid;
  gap: 6px;
  margin-top: 10px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.route-progress-track {
  height: 5px;
  overflow: hidden;
  border-radius: 999px;
  background: #e4ebe8;
}

.route-progress-track span {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--teal), var(--coral));
  transition: width 160ms ease;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 0;
}

.stats-grid div {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-width: 0;
  min-height: 78px;
  padding: 12px;
  border-radius: 8px;
  background: #ffffff;
}

.stats-grid dt {
  min-height: 28px;
  margin-bottom: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  line-height: 1.25;
  text-transform: uppercase;
}

.stats-grid dd {
  margin: 0;
  overflow: hidden;
  color: var(--text);
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 16px;
  line-height: 1.15;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.panel-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  font-size: 13px;
  font-weight: 800;
  text-transform: uppercase;
  color: #3c4644;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  color: #24302e;
  font-size: 14px;
  font-weight: 650;
}

.check-row input {
  width: 16px;
  height: 16px;
  accent-color: var(--teal);
}

.kind-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
}

.kind-dot.run {
  background: #ff3b30;
}

.kind-dot.cycle {
  background: var(--teal);
}

.kind-dot.walk {
  background: var(--amber);
}

.kind-dot.original {
  background: #2563eb;
}

.kind-dot.points {
  background: linear-gradient(90deg, #2563eb 0 50%, #ff3b30 50% 100%);
}

.segmented {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 4px;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #eef2f1;
}

.segmented button {
  min-height: 34px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 750;
  cursor: pointer;
}

.segmented .selected {
  background: #ffffff;
  color: var(--text);
  box-shadow: 0 6px 18px rgba(20, 32, 31, 0.08);
}

.range-label {
  display: flex;
  justify-content: space-between;
  margin-top: 18px;
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--coral);
}

.map-stage {
  display: flex;
  min-width: 0;
  height: 100vh;
  min-height: 100vh;
  padding: 18px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.map-wrap {
  flex: 1;
  position: relative;
  min-height: 0;
  overflow: hidden;
  border: 1px solid #ced8d5;
  border-radius: 8px;
  background: #dde7e2;
  box-shadow: var(--shadow);
}

.empty-state {
  position: absolute;
  left: 50%;
  top: 50%;
  display: grid;
  gap: 6px;
  width: min(360px, calc(100% - 40px));
  padding: 18px;
  border: 1px solid rgba(255, 255, 255, 0.72);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: 0 16px 44px rgba(23, 32, 31, 0.12);
  text-align: center;
  transform: translate(-50%, -50%);
}

.empty-state strong {
  font-size: 18px;
}

.empty-state span {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.empty-state.hidden {
  display: none;
}

#map,
#demoMap {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#demoMap {
  display: none;
}

.map-selection-surface {
  position: absolute;
  inset: 0;
  z-index: 350;
  cursor: crosshair;
  user-select: none;
}

.map-selection-surface.hidden {
  display: none;
}

.demo-mode #demoMap {
  display: block;
}

.map-overlay {
  position: absolute;
  right: 18px;
  bottom: 18px;
  display: none;
  grid-template-columns: repeat(3, minmax(112px, 1fr));
  gap: 10px;
  width: min(520px, calc(100% - 36px));
  padding: 10px;
  border: 1px solid rgba(255, 255, 255, 0.74);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(14px);
}

.map-attribution {
  position: absolute;
  left: 12px;
  bottom: 12px;
  z-index: 500;
  padding: 4px 7px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.82);
  color: #53605e;
  font-size: 11px;
  line-height: 1;
}

.map-attribution a {
  color: inherit;
}

.leaflet-container {
  font-family: inherit;
}

.leaflet-tile {
  user-select: none;
  -webkit-user-drag: none;
}

.leaflet-container.middle-panning {
  cursor: grabbing;
}

.leaflet-container.area-selecting {
  cursor: crosshair;
  user-select: none;
}

.map-selection-box {
  position: absolute;
  z-index: 690;
  border: 1px solid rgba(37, 99, 235, 0.9);
  background: rgba(37, 99, 235, 0.14);
  pointer-events: none;
}

.map-delete-selection {
  position: absolute;
  left: 56px;
  top: 18px;
  z-index: 700;
  min-height: 34px;
  padding: 0 12px;
  border: 1px solid rgba(255, 59, 48, 0.28);
  border-radius: 8px;
  background: #ff3b30;
  color: #ffffff;
  font-size: 13px;
  font-weight: 850;
  box-shadow: 0 10px 28px rgba(23, 32, 31, 0.18);
}

.map-delete-selection:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.debug-panel {
  position: absolute;
  right: 18px;
  top: 18px;
  z-index: 720;
  display: grid;
  gap: 8px;
  width: min(440px, calc(100% - 36px));
  max-height: min(360px, calc(100% - 36px));
  padding: 10px;
  border: 1px solid rgba(23, 32, 31, 0.16);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 16px 40px rgba(23, 32, 31, 0.18);
  color: #17201f;
}

.debug-panel.collapsed {
  width: auto;
}

.debug-panel.collapsed pre {
  display: none;
}

.debug-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.debug-panel-header strong {
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.debug-panel-header div {
  display: flex;
  gap: 6px;
}

.debug-button {
  min-height: 26px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  color: var(--text);
  font-size: 11px;
  font-weight: 800;
}

.debug-panel pre {
  min-height: 80px;
  max-height: 290px;
  margin: 0;
  overflow: auto;
  white-space: pre-wrap;
  color: #263230;
  font: 11px/1.4 ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

.hidden {
  display: none !important;
}

.route-point-handle {
  cursor: grab;
  touch-action: none;
  filter: drop-shadow(0 4px 12px rgba(23, 32, 31, 0.28));
}

.route-point-handle:active {
  cursor: grabbing;
}

.route-point-handle.is-dragging {
  cursor: grabbing;
}

.route-point-handle.selected {
  stroke-width: 5;
  filter: drop-shadow(0 0 0 rgba(255, 255, 255, 1)) drop-shadow(0 8px 18px rgba(23, 32, 31, 0.34));
}

.route-endpoint-marker {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: grab;
  filter: drop-shadow(0 8px 18px rgba(23, 32, 31, 0.28));
}

.route-endpoint-marker:active,
.route-endpoint-marker.is-dragging {
  cursor: grabbing;
}

.route-endpoint-marker .endpoint-pin {
  position: relative;
  width: 22px;
  height: 22px;
  border: 3px solid #ffffff;
  border-radius: 999px;
  background: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.24);
}

.route-endpoint-marker .endpoint-pin::after {
  position: absolute;
  left: 50%;
  bottom: -11px;
  width: 10px;
  height: 10px;
  background: inherit;
  border-right: 3px solid #ffffff;
  border-bottom: 3px solid #ffffff;
  content: "";
  transform: translateX(-50%) rotate(45deg);
}

.route-endpoint-marker .endpoint-label {
  padding: 5px 8px;
  border: 2px solid #ffffff;
  border-radius: 7px;
  background: #15201f;
  color: #ffffff;
  font-size: 10px;
  font-weight: 900;
  line-height: 1;
}

.route-endpoint-marker.finish .endpoint-pin {
  background: #7c3aed;
  box-shadow: 0 0 0 3px rgba(124, 58, 237, 0.24);
}

.route-endpoint-marker.changed .endpoint-pin {
  background: #ff3b30;
  box-shadow: 0 0 0 3px rgba(255, 59, 48, 0.25);
}

.route-endpoint-marker.selected .endpoint-pin {
  box-shadow: 0 0 0 4px rgba(255, 255, 255, 0.94), 0 0 0 8px rgba(21, 32, 31, 0.22);
}

.point-menu-popup .leaflet-popup-content-wrapper {
  border-radius: 8px;
  box-shadow: 0 14px 34px rgba(23, 32, 31, 0.2);
}

.point-menu-popup .leaflet-popup-content {
  width: 148px;
  margin: 8px;
}

.point-menu {
  display: grid;
  gap: 6px;
}

.point-menu-title {
  padding: 4px 6px 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.point-menu-action {
  width: 100%;
  min-height: 34px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: #ffffff;
  color: var(--text);
  font-size: 13px;
  font-weight: 800;
  text-align: left;
}

.point-menu-action:hover:not(:disabled) {
  background: #f4f6f7;
}

.point-menu-action.danger {
  color: #b83e35;
}

.point-menu-action.danger:hover:not(:disabled) {
  border-color: rgba(255, 91, 79, 0.22);
  background: #fff1ee;
}

.point-menu-action:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.map-overlay div {
  padding: 10px 12px;
  border-radius: 7px;
  background: #ffffff;
}

.metric-label {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.map-overlay strong {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 18px;
  line-height: 1.1;
}

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
  }

  .sidebar {
    min-height: auto;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .map-stage {
    display: block;
    min-height: 680px;
  }

  .map-wrap {
    min-height: 560px;
  }

  .map-overlay {
    grid-template-columns: 1fr;
  }
}
