:root {
  --bg: #ffffff;
  --bg2: #f3f5f2;
  --fg: #1b1f1c;
  --muted: #5f6b63;
  --line: #d5dbd6;
  --accent: #1f7a4a;
  --accent-fg: #ffffff;
  --danger: #b42318;
  --bar: #1d3b2a;
  --bar-fg: #f2f7f3;
  --shadow: 0 4px 18px rgba(0, 0, 0, .22);
  --hiking: #c2410c;
  --bicycle: #1d4ed8;
  --other: #6b7280;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1c211e; --bg2: #252b27; --fg: #e8ede9; --muted: #9aa69e; --line: #3a423d;
    --accent: #3fae73; --accent-fg: #0c1a12; --danger: #f87171; --bar: #12261b; --bar-fg: #e8ede9;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #1c211e; --bg2: #252b27; --fg: #e8ede9; --muted: #9aa69e; --line: #3a423d;
  --accent: #3fae73; --accent-fg: #0c1a12; --danger: #f87171; --bar: #12261b; --bar-fg: #e8ede9;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--fg);
  -webkit-tap-highlight-color: transparent;
}
#map { position: fixed; inset: 0; background: var(--bg2); }

/* --- top bar ------------------------------------------------------------ */
#topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 1000;
  display: flex; align-items: center; gap: 8px;
  padding: calc(6px + env(safe-area-inset-top)) 12px 6px;
  background: var(--bar); color: var(--bar-fg); box-shadow: var(--shadow);
}
.app-title { font-weight: 650; white-space: nowrap; }
#btn-edits { margin-left: auto; }
.chip {
  font-size: 12px; padding: 2px 8px; border-radius: 999px; background: rgba(255, 255, 255, .14);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 45vw;
}
.chip.busy::before { content: "⟳ "; }
.chip.err { background: rgba(248, 113, 113, .35); }
.badge { display: inline-block; min-width: 1.4em; padding: 0 5px; border-radius: 999px; background: var(--accent); color: var(--accent-fg); font-size: 12px; text-align: center; }
.leaflet-top { top: calc(44px + env(safe-area-inset-top)); }

/* --- buttons ---------------------------------------------------------------- */
.btn {
  font: inherit; font-size: 14px; color: var(--fg); background: var(--bg2);
  border: 1px solid var(--line); border-radius: 8px; padding: 7px 12px; min-height: 36px;
  cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
#topbar .btn { background: transparent; color: var(--bar-fg); border-color: rgba(255, 255, 255, .3); }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.icon { padding: 4px 10px; min-width: 36px; }
.btn.small { padding: 2px 8px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }

.fab {
  position: fixed; right: 16px; bottom: calc(24px + env(safe-area-inset-bottom)); z-index: 900;
  width: 56px; height: 56px; border-radius: 50%; border: none;
  background: var(--accent); color: var(--accent-fg); font-size: 32px; line-height: 1;
  box-shadow: var(--shadow); cursor: pointer;
}
body.editing .fab, body.placing .fab { display: none; }

/* --- placing ---------------------------------------------------------------- */
#place-ui[hidden] { display: none; }
.crosshair {
  position: fixed; left: 50%; top: 50%; width: 44px; height: 44px; margin: -22px 0 0 -22px;
  z-index: 950; pointer-events: none;
  background:
    linear-gradient(var(--danger), var(--danger)) center / 2px 100% no-repeat,
    linear-gradient(var(--danger), var(--danger)) center / 100% 2px no-repeat;
  filter: drop-shadow(0 0 2px #fff);
}
.place-bar {
  position: fixed; left: 16px; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 950;
  background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px;
  display: flex; flex-direction: column; gap: 10px; max-width: 480px; margin: 0 auto;
}
.place-bar .row { justify-content: flex-end; }

/* --- panel: bottom sheet on phones, side panel on wide screens -------------- */
#panel {
  position: fixed; z-index: 1000; background: var(--bg); box-shadow: var(--shadow);
  display: flex; flex-direction: column;
  left: 0; right: 0; bottom: 0; height: 56vh; border-radius: var(--radius) var(--radius) 0 0;
  padding-bottom: env(safe-area-inset-bottom);
}
#panel[hidden] { display: none; }
#panel.tall { height: 88vh; }
.panel-grip { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 6px auto 0; cursor: pointer; }
@media (min-width: 820px) {
  #panel, #panel.tall { left: auto; top: 44px; width: 400px; height: auto; border-radius: 0; }
  .panel-grip { display: none; }
}
.panel-head { display: flex; align-items: flex-start; gap: 8px; padding: 8px 16px 4px; }
.panel-title { flex: 1; min-width: 0; }
.panel-head h2 { margin: 0; font-size: 17px; }
.panel-head small { color: var(--muted); }
.menu-wrap { position: relative; }
.menu {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 10; min-width: 190px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px 0;
}
.menu[hidden] { display: none; }
.menu-info { font-size: 13px; padding: 2px 14px 6px; line-height: 1.5; }
.menu-info .err { color: var(--danger); }
.menu-sep { border: none; border-top: 1px solid var(--line); margin: 6px 0; }
.menu-label { font-size: 12px; color: var(--muted); padding: 4px 14px; }
.menu-item {
  display: flex; align-items: center; gap: 8px; width: 100%; text-align: left; font: inherit; font-size: 15px;
  padding: 10px 14px; background: none; border: none; color: var(--fg); cursor: pointer;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--bg2); }
.menu-item::after { content: ""; margin-left: auto; }
.menu-item.on::after { content: "✓"; color: var(--accent); font-weight: 700; }
.menu-item:disabled { opacity: .45; cursor: default; }
.photo-row { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 16px 8px; }
.photo-row .btn { text-decoration: none; }
.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 16px; }
.tab { font: inherit; background: none; border: none; color: var(--muted); padding: 8px 14px; border-bottom: 2px solid transparent; cursor: pointer; }
.tab.active { color: var(--fg); border-bottom-color: var(--accent); font-weight: 600; }
.panel-body { flex: 1; overflow-y: auto; padding: 10px 16px; -webkit-overflow-scrolling: touch; }

.hint { color: var(--muted); font-size: 13px; margin: 4px 0 10px; }
.warn { color: var(--danger); font-size: 13px; }

/* --- fingers -------------------------------------------------------------- */
.finger {
  border: 1px solid var(--line); border-left: 6px solid var(--c, var(--other));
  border-radius: 10px; padding: 8px 10px; margin-bottom: 10px; background: var(--bg);
}
.finger.selected { box-shadow: 0 0 0 2px var(--c, var(--accent)); }
.fhead { display: flex; align-items: center; gap: 8px; }
.fhead select { font: inherit; font-weight: 600; padding: 4px 6px; border-radius: 6px; border: 1px solid var(--line); background: var(--bg2); color: var(--fg); }
.fhead .meta { flex: 1; color: var(--muted); font-size: 12px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dest { display: flex; gap: 6px; margin-top: 6px; align-items: center; }
.dest input { font: inherit; font-size: 16px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg2); color: var(--fg); min-width: 0; }
.dest .dname { flex: 1; }
.dest .ddist { width: 64px; text-align: right; }
.dest .unit { color: var(--muted); font-size: 13px; }
.dest .btn { padding: 2px 6px; min-height: 32px; min-width: 30px; }
.add-dest { margin-top: 6px; }
.routes { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; align-items: center; }
.routes .label { font-size: 12px; color: var(--muted); width: 100%; }
.rchip {
  font: inherit; font-size: 13px; border-radius: 999px; padding: 3px 10px; cursor: pointer;
  border: 1px solid var(--line); background: var(--bg2); color: var(--fg);
}
.rchip.on { background: var(--c, var(--accent)); border-color: var(--c, var(--accent)); color: #fff; }
.rchip.sugg { border-style: dashed; }
.rchip small { opacity: .75; }
.routes input { font: inherit; font-size: 14px; padding: 3px 8px; border: 1px solid var(--line); border-radius: 999px; background: var(--bg2); color: var(--fg); width: 110px; }

#tags-text {
  width: 100%; min-height: 260px; height: calc(100% - 40px); resize: vertical;
  font: 14px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  padding: 8px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg2); color: var(--fg);
}

/* --- dialog ----------------------------------------------------------------- */
dialog { border: none; border-radius: var(--radius); padding: 0; width: min(520px, calc(100vw - 32px)); background: var(--bg); color: var(--fg); box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dlg { padding: 0 0 12px; }
.dlg > .row, .dlg > .hint { padding: 0 16px; }
#edits-list { list-style: none; margin: 0; padding: 0 16px; max-height: 50vh; overflow-y: auto; }
#edits-list li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); cursor: pointer; }
#edits-list li .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
#edits-list li .txt { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#edits-list li.empty { color: var(--muted); cursor: default; }

/* --- toast + map bits ----------------------------------------------------- */
#toast {
  position: fixed; left: 50%; top: calc(56px + env(safe-area-inset-top)); transform: translateX(-50%);
  z-index: 2000; background: var(--fg); color: var(--bg); padding: 8px 14px; border-radius: 8px;
  font-size: 14px; opacity: 0; pointer-events: none; transition: opacity .2s; max-width: calc(100vw - 32px);
}
#toast.show { opacity: .94; }
.gp-pin { width: 22px; height: 22px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 2px #000, 0 2px 6px rgba(0, 0, 0, .4); }
.branch-label {
  background: var(--c, #333); color: #fff; font-size: 12px; font-weight: 700; padding: 1px 5px;
  border-radius: 6px; white-space: nowrap; box-shadow: 0 1px 3px rgba(0, 0, 0, .4); transform: translate(-50%, -50%);
  display: inline-block;
}
.branch-label.tier3:not(.assigned) { font-size: 13px; padding: 2px 7px; box-shadow: 0 0 0 2px #fff, 0 1px 4px rgba(0, 0, 0, .5); }
.branch-label.tier0 { opacity: .7; font-weight: 500; }
.branch-label.tier0:not(.assigned) { font-size: 11px; }
.leaflet-container { font: inherit; }
