:root {
  --ink: #1c241c;
  --muted: #5d675e;
  --paper: #f3efe6;
  --card: #fffdf8;
  --line: #ddd4c6;
  --green: #399e76;
  --green-2: #14422d;
  --accent: #c4452d;
  --shadow: 0 16px 40px rgba(40, 32, 18, 0.12);
  color: var(--ink);
  --font-main: "Montserrat", "Segoe UI", "Helvetica Neue", sans-serif;
  --font-fixed: "Consolas", "Courier New", monospace;
  background: var(--paper);
}

* { box-sizing: border-box; }

html, body, #app, div, span, p, button, textarea, input, select, option, optgroup, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, details, figcaption, figure, footer, header, hgroup, main, nav, section, summary {
  font-family: var(--font-main);
}

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

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

button, textarea {
  font-family: var(--font-main);
}

button {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.45rem 0.9rem;
  cursor: pointer;
}

button:disabled {
  cursor: default;
  opacity: 0.55;
}

button.primary {
  background: var(--green);
  color: #f7f3ea;
  border-color: var(--green);
}

button.active {
  background: var(--green-2);
  color: #f7f3ea;
  border-color: var(--green-2);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1rem;
  background: var(--green);
  color: #f6f1e6;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 14rem;
}

.brand h1, .card-body h2, .connect-card h2 {
  font-family: var(--font-main);
  font-weight: 600;
  letter-spacing: -0.02em;
}

.brand h1 {
  margin: 0;
  font-size: 1.25rem;
  text-transform: uppercase;
}

.brand p, .status-quiet {
  margin: 0.1rem 0 0;
  color: #d7e2d6;
  font-size: 0.82rem;
}

.brand-mark {
  width: 0.85rem;
  height: 2rem;
  border-radius: 999px;
  background: linear-gradient(var(--accent), #e7b15a);
  display: none;
}

.actions, .filters {
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.views button, .filters button {
  background: transparent;
  color: inherit;
  border-color: rgba(255, 255, 255, 0.25);
}

.topbar button.active, .filters button.active {
  background: #fff;
  color: var(--green);
  border-color: var(--green);
  font-weight: 500;
}

.actions { margin-left: auto; }

.action-group {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  margin-left: 0.15rem;
  padding-left: 0.55rem;
  border-left: 1px solid rgba(255, 255, 255, 0.28);
}

.settings-open,
.settings-popover {
  display: none;
}

.topbar .actions button {
  background: transparent;
  color: inherit;
  border-color: rgba(255, 255, 255, 0.35);
}

.signed-in {
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.82rem;
}

.topbar .actions button.primary {
  background: #f4efe4;
  color: var(--green);
  border-color: #f4efe4;
}

.sources {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 1.25rem;
  align-items: center;
  padding: 0.4rem 1rem;
  background: #163026;
  color: #f6f1e6;
  font-size: 0.82rem;
}

.sources button {
  margin-left: 0.45rem;
  padding: 0.2rem 0.7rem;
  background: transparent;
  color: inherit;
  border-color: rgba(255, 255, 255, 0.35);
}

.source-on {
  margin-left: 0.45rem;
  color: #d7e2d6;
}

.provider-block + .provider-block {
  margin-top: 1rem;
}

.filters {
  flex-direction: column;
  align-items: stretch;
  gap: 0.45rem;
  padding: 0.65rem 0.8rem;
  background: #e7e0d4;
  border-bottom: 1px solid var(--line);
}

.toggle-row, .range-row {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  width: 100%;
}

.toggle-row button {
  flex: 1 1 0;
}

.range-row label {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
  color: var(--muted);
  font-size: 0.78rem;
  white-space: nowrap;
}

.range-row input {
  /* flex: 1 1 auto; */
  min-width: 0;
  width: 125px;
  font-family: inherit;
  font-size: 0.8rem;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0.28rem 0.5rem;
}

.filters button {
  color: var(--ink);
  background: var(--card);
  white-space: nowrap;
  border: 1px solid #ccc;
}

.filters em {
  font-style: normal;
  color: var(--muted);
  margin-left: 0.25rem;
}

.filters button.active em { color: var(--green); }

.syncbar {
  position: relative;
  min-height: 1.8rem;
  background: #efe4cc;
  color: var(--ink);
  overflow: hidden;
  font-size: 0.85rem;
}

.syncbar span {
  position: relative;
  z-index: 1;
  display: block;
  padding: 0.35rem 1rem;
}

.syncbar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  background: #e7c27a;
}

.syncbar-fill.indeterminate {
  width: 35% !important;
  animation: slide 1.1s ease-in-out infinite;
}

@keyframes slide {
  from { left: -35%; }
  to { left: 100%; }
}

.banner {
  margin: 0;
  padding: 0.55rem 1rem;
  background: #f8d7cf;
  color: #6d2418;
}

.notice {
  margin: 0;
  padding: 0.45rem 1rem;
  background: #e5efe6;
  color: var(--green);
}

.on-map-bar {
  display: none;
}

.workspace {
  flex: 1;
  min-height: 0;
  display: flex;
}

.map-stage {
  position: relative;
  flex: 1;
  min-width: 0;
  min-height: 0;
  background: #a9ddef;
}

#overview-map,
#overview-map.leaflet-container,
.mini-map,
.mini-map.leaflet-container {
  background: #a9ddef;
}

#overview-map {
  position: absolute;
  inset: 0;
}

.connect-card {
  position: absolute;
  z-index: 500;
  top: 1.25rem;
  left: 1.25rem;
  width: min(28rem, calc(100% - 2.5rem));
  max-height: calc(100% - 2.5rem);
  overflow: auto;
  padding: 1.1rem 1.15rem 1.2rem;
  background: rgba(255, 253, 248, 0.96);
  border: 1px solid var(--line);
  border-radius: 1rem;
  box-shadow: var(--shadow);
}

.connect-card a {
  color: var(--ink);
}

.connect-card h2 { margin: 0 0 0.4rem; }

.connect-card p {
  margin: 0 0 0.9rem;
  color: var(--muted);
  line-height: 1.45;
}

.return-url {
  color: var(--ink);
  font-family: var(--font-fixed);
  font-size: 0.82rem;
}

.connect-card label {
  display: block;
  margin: 0.9rem 0 0.7rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.file-input {
  display: none;
}

.connect-card input[type="text"],
.connect-card input[type="password"],
.connect-card textarea {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  resize: vertical;
  background: #fff;
  color: var(--ink);
}

.legend {
  position: absolute;
  z-index: 500;
  left: 0.8rem;
  bottom: 1.6rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem 0.8rem;
  max-width: 18rem;
  padding: 0.55rem 0.7rem;
  background: rgba(255, 253, 248, 0.94);
  border-radius: 0.7rem;
  font-size: 0.78rem;
}

.legend i {
  display: inline-block;
  width: 0.7rem;
  height: 0.7rem;
  margin-right: 0.3rem;
  border-radius: 99px;
}

.route-popover {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: grid;
  place-items: center;
  padding: 1.25rem;
  background: rgba(24, 32, 28, 0.46);
}

.route-dialog.gate-dialog {
  width: min(28rem, 100%);
  height: auto;
  padding: 0 1rem 0.4rem;
}

.gate-dialog label {
  display: block;
  margin: 0.4rem 0 0.2rem;
  font-size: 0.85rem;
  color: var(--muted);
}

.gate-dialog input {
  display: block;
  width: 100%;
  margin-top: 0.35rem;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--line);
  border-radius: 0.7rem;
  background: #fff;
  color: var(--ink);
}

.gate-dialog .route-tools {
  justify-content: flex-end;
}

.gate-dialog button.primary {
  background: var(--green);
  color: #f7f3ea;
  border-color: var(--green);
}

.gate-error,
.archive-note {
  margin: 0.35rem 0 0.6rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.gate-error {
  color: #8c3b2a;
}

.route-dialog {
  width: min(100rem, 100%);
  height: min(100rem, calc(100vh - 4.5rem));
  display: flex;
  flex-direction: column;
  background: var(--paper, #f7f3ea);
  border-radius: 1rem;
  overflow: hidden;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.28);
}

.route-dialog header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem 0.35rem;
}

.route-dialog h2 {
  margin: 0;
  font-size: 1.15rem;
}

.route-dialog header p {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.7rem;
  margin: 0.25rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.route-dialog header p.route-name {
  display: block;
  color: inherit;
  font-size: 0.95rem;
  font-weight: 650;
}

.route-tools {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.45rem 1rem 0.7rem;
}

.route-tools .toggle-row {
  flex: 1;
}

#route-map {
  flex: 1;
  min-height: 10rem;
  background: #d7e7c8;
}

.card-title,
.route-name {
  margin: 0.15rem 0 0;
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-title {
  font-size: 0.84rem;
}

.elev-plot {
  flex: none;
  align-self: stretch;
  width: 100%;
  min-width: 0;
  margin: 0;
  padding: 0.35rem 1rem 0.75rem;
  box-sizing: border-box;
}

.elev-plot svg {
  display: block;
  width: 100%;
  max-width: 100%;
  height: 4.5rem;
  background: #f4efe6;
  border-radius: 0.35rem;
}

.elev-plot polygon {
  fill: #c5d7b4;
}

.elev-plot polyline {
  fill: none;
  stroke: #1d4e3a;
  stroke-width: 2;
  vector-effect: non-scaling-stroke;
}

.elev-plot figcaption {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 0.25rem;
  color: var(--muted, #6d655c);
  font-size: 0.75rem;
}

.elev-wait {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  flex: none;
  margin: 0;
  padding: 0.7rem 1rem 0.9rem;
  color: var(--muted, #6d655c);
}

.spinner {
  width: 0.9rem;
  height: 0.9rem;
  border: 2px solid #d9d0c2;
  border-top-color: #1d4e3a;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.list-stage {
  width: 420px;
  flex: none;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--line);
  background: var(--paper);
}

.list-scroll {
  flex: 1;
  overflow: auto;
  padding: 0.6rem;
  /* padding-top: 0px; */
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.card {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.4rem;
  padding: 0.5rem 0.55rem 0.45rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.4rem;
  cursor: pointer;
}

.card.selected {
  border-color: #c026d3;
  box-shadow: inset 0 0 0 1px #c026d3;
}

.mini-frame {
  position: relative;
}

.mini-map, .mini-empty {
  position: relative;
  height: 132px;
  border-radius: 0.65rem;
  overflow: hidden;
  background: #a9ddef;
}

.source-badge,
.source-marker {
  display: grid;
  place-items: center;
  width: 1.35rem;
  height: 1.35rem;
  border-radius: 99px;
  color: #fff;
  font-size: 0.68rem;
  font-weight: 700;
  line-height: 1;
  border: 1px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.28);
}

.source-badge {
  position: absolute;
  z-index: 500;
  top: 0.35rem;
  left: 0.35rem;
  pointer-events: none;
}

.map-popout {
  position: absolute;
  z-index: 600;
  top: 0.35rem;
  right: 0.35rem;
  width: 1.7rem;
  height: 1.7rem;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 0.4rem;
  background: rgba(255, 253, 248, 0.94);
  color: #163026;
  border: 1px solid #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.28);
  cursor: pointer;
}

.map-popout svg,
.track-popout svg {
  width: 0.95rem;
  height: 0.95rem;
  display: block;
}

.track-popout {
  background: transparent;
  border: 0;
  pointer-events: auto;
}

.track-popout button {
  width: 28px;
  height: 28px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 0.4rem;
  background: rgba(255, 253, 248, 0.96);
  color: #163026;
  border: 1px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  cursor: pointer;
}

.connections-dialog {
  width: min(28rem, 100%);
  height: auto;
  max-height: calc(100vh - 4.5rem);
  padding: 0.85rem 0.9rem 1rem;
  overflow: auto;
}

.connections-dialog > header {
  align-items: center;
  padding: 0 0.1rem 0.15rem;
}

.connections-dialog .settings-block {
  margin-top: 0.7rem;
  padding: 0.75rem;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 0.85rem;
}

.connections-dialog .settings-block h3 {
  margin: 0 0 0.55rem;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 650;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.connections-dialog .sources {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.7rem;
  margin: 0;
  padding: 0;
  background: transparent;
  color: var(--ink);
}

.connections-dialog .source {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.35rem;
}

.connections-dialog .sources button {
  width: 100%;
  margin-left: 0;
  background: #fff;
  color: var(--ink);
  border-color: var(--line);
}

.connections-dialog .source-on,
.settings-dialog .source-on {
  margin-left: 0.15rem;
  color: #8eae9b;
}

.connections-dialog .action-group {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.45rem;
  width: 100%;
  margin: 0;
  padding: 0;
  border: 0;
}

.connections-dialog .action-group button {
  width: 100%;
  background: #fff;
  color: var(--ink);
  border-color: var(--line);
}

.connections-dialog .action-group button.active {
  background: var(--green);
  color: #f7f3ea;
  border-color: var(--green);
}

.connections-dialog .connect-card {
  position: static;
  z-index: auto;
  width: auto;
  max-height: none;
  margin: 0.7rem 0 0;
  box-shadow: none;
}

button.danger {
  background: var(--accent);
  color: #fffaf6;
  border-color: var(--accent);
}

.source-badge.strava,
.source-marker.strava,
.source-name.strava { color: #fc4c02; }

.source-badge.strava,
.source-marker.strava {
  background: #fc4c02;
  color: #fff;
}

.source-badge.fitnesssyncer,
.source-marker.fitnesssyncer {
  background: #1d4e89;
  color: #fff;
}

.source-name {
  font-weight: 650;
}

.source-name.fitnesssyncer { color: #1d4e89; }

.mini-map,
.mini-map .leaflet-container,
.mini-map .leaflet-interactive {
  cursor: pointer;
}

.mini-empty {
  display: grid;
  place-items: center;
  color: var(--muted);
  border: 1px dashed #c9c1b3;
}

.os-note {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.68rem;
  line-height: 1.3;
}

.stats {
  display: flex;
  justify-content: space-between;
  /* flex-wrap: wrap; */
  /* gap: 0.35rem 0.55rem; */
  margin: 0;
  font-size: 0.7rem;
  font-weight: 500
}

.stat-date {
  /* flex: 1; */
}

.stats small {
  color: var(--muted);
  margin-left: 0.25rem;
}

.text-button {
  margin-top: 0.8rem;
  padding-left: 0;
  padding-right: 0;
  border: 0;
  background: transparent;
  color: var(--green-2);
  border-radius: 0;
}

.empty {
  margin: 2rem auto;
  color: var(--muted);
}

.leaflet-popup-content-wrapper {
  border-radius: 0.7rem;
}

.popup strong { display: block; }
.popup div { margin-top: 0.15rem; }

@media (max-width: 800px) {
  html, body, #app {
    height: 100%;
    overflow: hidden;
  }

  .list-scroll {
   padding-top: 0px; */
}

  .topbar {
    flex: none;
    flex-wrap: nowrap;
    gap: 0.6rem;
  }

  .brand {
    min-width: 0;
    flex: 1;
  }

  .brand h1 {
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .settings-open {
    display: grid;
    place-items: center;
    flex: none;
    width: 2.4rem;
    height: 2.4rem;
    margin-left: auto;
    padding: 0;
    border-color: rgba(255, 255, 255, 0.35);
    background: transparent;
    color: #f6f1e6;
  }

  .settings-open svg {
    width: 1.25rem;
    height: 1.25rem;
  }

  .settings-popover.open {
    position: fixed;
    inset: 0;
    z-index: 1800;
    display: grid;
    align-items: start;
    justify-items: center;
    overflow: auto;
    padding: 0.8rem;
    background: rgba(24, 32, 28, 0.46);
  }

  .settings-dialog {
    width: min(28rem, 100%);
    max-height: calc(100vh - 1.6rem);
    overflow: auto;
    padding: 0.85rem 0.9rem 1rem;
    background: var(--paper);
    border-radius: 1rem;
    box-shadow: var(--shadow);
  }

  .settings-dialog > header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
  }

  .settings-dialog .syncbar {
    margin-top: 0.7rem;
    border-radius: 0.7rem;
  }

  .settings-dialog h2 {
    margin: 0;
    font-size: 1.05rem;
  }

  .settings-block {
    margin-top: 0.7rem;
    padding: 0.75rem;
    background: var(--card);
    border: 1px solid var(--line);
    border-radius: 0.85rem;
  }

  .settings-block:not(:has(.actions, .sources, .filters)) {
    display: none;
  }

  .settings-block h3 {
    margin: 0 0 0.55rem;
    color: var(--muted);
    font-size: 0.72rem;
    font-weight: 650;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  .settings-dialog .actions,
  .settings-dialog .sources,
  .settings-dialog .filters {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
    margin: 0;
    padding: 0;
    background: transparent;
    color: var(--ink);
    border: 0;
  }

  .settings-dialog .actions {
    flex-direction: row;
    flex-wrap: wrap;
  }

  .settings-dialog .action-group {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
    width: 100%;
    margin: 0;
    padding: 0;
    border: 0;
  }

  .settings-dialog .actions button,
  .settings-dialog .sources button {
    background: var(--card);
    color: var(--ink);
    border-color: var(--line);
    width: 100%;
  }

  .settings-dialog .actions button.primary,
  .settings-dialog .actions button.active {
    background: var(--green);
    color: #f7f3ea;
    border-color: var(--green);
  }

  .settings-dialog .source {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.35rem;
  }

  .settings-dialog .connect-card {
    position: static;
    width: auto;
    max-height: none;
    margin-top: 0.7rem;
    box-shadow: none;
  }

  #settings-connect:empty {
    display: none;
  }

  .workspace {
    flex-direction: column;
    overflow: hidden;
  }

  .map-stage {
    flex: none;
    width: 100%;
    height: auto;
    aspect-ratio: 1 / 0.6;
    min-height: 0;
  }

  .on-map-inline {
    display: none;
  }

  .on-map-bar {
    display: flex;
    flex: none;
    justify-content: stretch;
    padding: 0.25rem 0.7rem;
    background: var(--paper);
    /* border-top: 1px solid var(--line); */
  }

  .on-map-bar button {
    flex: 1;
    color: var(--ink);
    background: var(--card);
    border: 1px solid var(--line);
  }

  .on-map-bar button.active {
    background: var(--green);
    color: #f7f3ea;
    border-color: var(--green);
  }

  .list-stage {
    width: auto;
    flex: 1;
    min-height: 0;
    border-left: 0;
    /* border-top: 1px solid var(--line); */
  }

  .connect-card { top: 0.7rem; left: 0.7rem; }
}
