:root {
  --forest: #1f3a2e;
  --forest-2: #2c5241;
  --moss: #6b8f5e;
  --route: #e4572e;
  --route-snapless: #b6462a;
  --paper: #f7f4ec;
  --ink: #1d231f;
  --muted: #5f6a63;
  --line: rgba(29, 35, 31, 0.12);
  --card: rgba(255, 255, 255, 0.94);
  --shadow: 0 6px 24px rgba(16, 28, 22, 0.18);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --sheet-h: calc(176px + var(--safe-bottom));
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #151a17;
    --ink: #eef1ec;
    --muted: #a3ada6;
    --line: rgba(238, 241, 236, 0.12);
    --card: rgba(24, 30, 27, 0.94);
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}

button, select, input { font: inherit; color: inherit; }
button { cursor: pointer; }

#map {
  position: fixed;
  inset: 0 0 var(--sheet-h) 0;
  touch-action: none;
  background: #dfe5d8;
}
body.drawing #map { cursor: crosshair; }

/* Top stats bar */
.topbar {
  position: fixed;
  top: calc(var(--safe-top) + 10px);
  left: 12px;
  right: 12px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 6px;
  z-index: 1000;
  pointer-events: none;
}
.stats {
  display: flex;
  gap: 2px;
  background: var(--card);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 8px 6px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: auto;
}
.stat {
  display: grid;
  grid-template-columns: auto auto;
  align-items: baseline;
  column-gap: 3px;
  padding: 0 10px;
  min-width: 78px;
}
.stat + .stat { border-left: 1px solid var(--line); }
.stat-value { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -0.02em; }
.stat-unit { font-size: 13px; color: var(--muted); font-weight: 600; }
.stat-label { grid-column: 1 / -1; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }

.busy {
  background: var(--forest);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}

/* Side buttons */
.side-buttons {
  position: fixed;
  right: 12px;
  top: calc(var(--safe-top) + 92px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 1000;
}
.round {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  background: var(--card);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
  padding: 0;
}
.round svg { width: 24px; height: 24px; stroke: var(--forest-2); stroke-width: 2; fill: var(--forest-2); stroke-linecap: round; stroke-linejoin: round; }
@media (prefers-color-scheme: dark) { .round svg { stroke: #cfe3c8; fill: #cfe3c8; } }
.round.active svg { stroke: #2a7de1; fill: #2a7de1; }

/* Hint + toast */
.hint, .toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1001;
  max-width: calc(100% - 32px);
  text-align: center;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--shadow);
  pointer-events: none;
}
.hint { bottom: calc(var(--sheet-h) + 12px); background: var(--forest); color: #fff; }
.toast { top: calc(var(--safe-top) + 84px); background: var(--ink); color: var(--paper); border-radius: 14px; }

/* Bottom sheet */
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--sheet-h);
  background: var(--paper);
  border-top: 1px solid var(--line);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
}
.profile {
  position: relative;
  flex: 1;
  min-height: 0;
  margin: 8px 12px 0;
  touch-action: none;
}
#profile-canvas { width: 100%; height: 100%; display: block; }
.profile-empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  color: var(--muted);
  font-size: 13px;
}
.profile-readout {
  position: absolute;
  top: 0;
  right: 0;
  font-size: 12px;
  font-weight: 600;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 2px 8px;
  font-variant-numeric: tabular-nums;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  padding: 6px 8px 8px;
}
.tool {
  border: none;
  background: transparent;
  border-radius: 12px;
  padding: 6px 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 11px;
  font-weight: 600;
  color: var(--ink);
}
.tool svg { width: 24px; height: 24px; stroke: currentColor; stroke-width: 2; fill: currentColor; stroke-linecap: round; stroke-linejoin: round; }
.tool:disabled { opacity: 0.35; }
.tool.primary { background: var(--line); }
.tool.primary[aria-pressed="true"] { background: var(--route); color: #fff; }

/* Panels */
dialog.panel {
  border: none;
  padding: 0;
  margin: auto auto 0;
  width: 100%;
  max-width: 520px;
  max-height: 80vh;
  border-radius: 20px 20px 0 0;
  background: var(--paper);
  color: var(--ink);
  box-shadow: var(--shadow);
}
dialog.panel::backdrop { background: rgba(10, 16, 12, 0.35); }
.panel-inner { padding: 14px 16px calc(18px + var(--safe-bottom)); }
.panel-head { display: flex; justify-content: space-between; align-items: center; }
.panel h2 { margin: 0; font-size: 18px; }
.panel h3 { margin: 18px 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.close { border: none; background: var(--line); width: 32px; height: 32px; border-radius: 50%; font-size: 20px; line-height: 1; }
.panel-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 12px; }
.panel-grid button {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  padding: 12px;
  font-weight: 600;
  text-align: left;
}
.panel-grid .danger { color: #c0392b; }
.row { display: flex; justify-content: space-between; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); gap: 12px; }
.row input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--forest-2); }
.row select { border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 6px 8px; }
.fineprint { color: var(--muted); font-size: 12px; margin: 14px 0 0; }

.route-list { list-style: none; padding: 0; margin: 12px 0 0; max-height: 55vh; overflow: auto; }
.route-list li { display: flex; align-items: center; gap: 8px; border-bottom: 1px solid var(--line); padding: 8px 0; }
.route-list .open { flex: 1; border: none; background: none; text-align: left; padding: 4px 0; }
.route-list .name { font-weight: 600; display: block; }
.route-list .meta { color: var(--muted); font-size: 12px; }
.route-list .del { border: none; background: var(--line); border-radius: 8px; padding: 6px 10px; font-size: 12px; color: #c0392b; }

/* Map markers */
.wp {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--route);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
}
.wp.start { border-color: #2e9e5b; background: #2e9e5b; }
.wp.end { border-color: var(--route); background: var(--route); }
.wp.mid { width: 14px; height: 14px; margin: 2px; }
.me-dot {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #2a7de1;
  border: 3px solid #fff;
  box-shadow: 0 0 0 6px rgba(42, 125, 225, 0.2), 0 1px 4px rgba(0, 0, 0, 0.3);
}
.leaflet-control-attribution { font-size: 10px; }
.leaflet-control-zoom { display: none; }
