:root {
  --panel: #1c2027; --panel-2: #232830; --line: #333944;
  --text: #d9d7cf; --text-heading: #f2f0e8; --text-muted: #868c96;
  --input-bg: #101317;
  --accent: #f26522; --accent-hover: #ef4137;
  --go: #34b27a; --go-hover: #2a9666;
  --danger: #e14b3d;
  --route: #ff9d2e; --route-casing: #4a2500;
  --shadow: rgba(0,0,0,.55);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; overscroll-behavior: none; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #1c3822; color: var(--text);
  touch-action: manipulation;
}

#map { position: fixed; inset: 0; background: #1c3822; z-index: 0; }
#map:focus, #map:focus-visible { outline: none; }
.leaflet-container { font: inherit; }

/* --- Top search bar --- */
#topbar {
  position: fixed; top: calc(.6rem + var(--safe-top)); left: .6rem; right: 4.2rem;
  z-index: 1000; max-width: 30rem;
}
#searchwrap { position: relative; display: flex; }
#search {
  width: 100%; padding: .8rem 2.4rem .8rem 1rem;
  background: var(--panel); color: var(--text-heading);
  border: 1px solid var(--line); border-radius: 10px;
  font-size: 1rem; box-shadow: 0 6px 20px var(--shadow);
  -webkit-appearance: none; appearance: none;
}
#search:focus { outline: none; border-color: var(--accent); }
#search::placeholder { color: var(--text-muted); }
#search-clear {
  position: absolute; right: .3rem; top: 50%; transform: translateY(-50%);
  font-size: 1.3rem; padding: .2rem .55rem;
}
.ghost-btn {
  background: none; border: none; color: var(--text-muted);
  cursor: pointer; border-radius: 8px;
}
.ghost-btn:active { color: var(--text-heading); }

#suggestions {
  margin-top: .35rem; background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; overflow: hidden; box-shadow: 0 10px 30px var(--shadow);
  max-height: 55vh; overflow-y: auto;
}
.sug {
  display: flex; flex-direction: column; gap: .1rem;
  padding: .65rem .9rem; cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.sug:last-child { border-bottom: none; }
.sug:active, .sug.kb { background: var(--panel-2); }
.sug .s-label { color: var(--text-heading); font-weight: 600; }
.sug .s-sub { color: var(--text-muted); font-size: .82rem; }
.sug .s-unmapped { color: var(--accent); }

/* --- Floating action buttons --- */
#fabs {
  position: fixed; right: .6rem; top: calc(.6rem + var(--safe-top));
  display: flex; flex-direction: column; gap: .55rem; z-index: 1000;
}
.fab {
  width: 3rem; height: 3rem; border-radius: 50%;
  background: var(--panel); color: var(--text-heading);
  border: 1px solid var(--line); font-size: 1.25rem;
  box-shadow: 0 6px 20px var(--shadow); cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.fab:active { background: var(--panel-2); }
.fab.on { background: var(--go); border-color: var(--go); color: #06130c; }
.fab.busy { color: var(--accent); }

/* --- Bottom sheet --- */
#sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1100;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0; box-shadow: 0 -10px 40px var(--shadow);
  padding: .4rem 1rem calc(1rem + var(--safe-bottom));
  max-height: 62vh; overflow-y: auto;
}
.sheet-grab {
  width: 2.6rem; height: .3rem; border-radius: 99px;
  background: var(--line); margin: .3rem auto .6rem;
}
.sheet-title { color: var(--text-heading); font-size: 1.25rem; font-weight: 700; }
.sheet-sub { color: var(--text-muted); margin-top: .15rem; font-size: .9rem; }
.sheet-note { color: var(--accent); margin-top: .5rem; font-size: .9rem; }
.sheet-actions { display: flex; gap: .6rem; margin-top: .9rem; flex-wrap: wrap; }
.btn {
  border: none; border-radius: 10px; padding: .7rem 1.1rem;
  font-size: .95rem; font-weight: 700; cursor: pointer;
}
.btn-go { background: var(--go); color: #06130c; }
.btn-go:active { background: var(--go-hover); }
.btn-quiet { background: var(--panel-2); color: var(--text); border: 1px solid var(--line); }
.btn-danger { background: var(--danger); color: #fff; }
.btn:disabled { opacity: .45; cursor: default; }

.start-list { display: flex; flex-direction: column; gap: .45rem; margin-top: .75rem; }
.start-opt {
  display: flex; align-items: center; gap: .6rem; text-align: left;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px;
  color: var(--text-heading); padding: .7rem .9rem; font-size: .95rem; cursor: pointer;
}
.start-opt:active { border-color: var(--accent); }
.start-opt .ico { font-size: 1.1rem; }
.start-opt small { color: var(--text-muted); display: block; }

.route-head { display: flex; align-items: center; gap: .6rem; }
.route-head .sheet-title { flex: 1; font-size: 1.05rem; }

/* --- Toast --- */
#toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(1.4rem + var(--safe-bottom)); z-index: 2000;
  background: var(--panel-2); color: var(--text-heading);
  border: 1px solid var(--line); border-radius: 10px;
  padding: .6rem 1rem; font-size: .9rem; max-width: 90vw;
  box-shadow: 0 10px 30px var(--shadow); text-align: center;
}

/* --- Map markers --- */
.pin { position: relative; }
.pin .pin-dot {
  position: absolute; left: 50%; bottom: 0; transform: translateX(-50%);
  width: 30px; height: 30px; border-radius: 50% 50% 50% 0;
  background: var(--accent); border: 2px solid #fff;
  transform: translateX(-50%) rotate(-45deg);
  transform-origin: center; box-shadow: 0 3px 8px var(--shadow);
}
.pin .pin-label {
  position: absolute; left: 50%; bottom: 34px; transform: translateX(-50%);
  background: rgba(16,19,23,.92); color: #f2f0e8; white-space: nowrap;
  padding: .2rem .55rem; border-radius: 6px; font-size: .8rem; font-weight: 700;
  border: 1px solid var(--line); pointer-events: none;
}
.gps-dot {
  width: 18px; height: 18px; border-radius: 50%;
  background: #2f86ff; border: 3px solid #fff;
  box-shadow: 0 0 0 2px rgba(47,134,255,.35), 0 2px 6px var(--shadow);
}
.node-dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: #ffd23e; border: 2px solid #6b4b00;
}
.node-dot.sel { background: #ff5d3d; border-color: #fff; }
.anchor-dot {
  width: 16px; height: 16px; border-radius: 4px;
  background: #b26bff; border: 2px solid #fff; transform: rotate(45deg);
}

/* --- Editor panel --- */
#editor {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1200;
  background: var(--panel); border-top: 1px solid var(--line);
  border-radius: 16px 16px 0 0; box-shadow: 0 -10px 40px var(--shadow);
  display: flex; flex-direction: column;
  height: 44vh; padding-bottom: var(--safe-bottom);
}
#editor-head { display: flex; align-items: center; border-bottom: 1px solid var(--line); }
#editor-tabs { display: flex; flex: 1; }
.etab {
  flex: 1; background: none; border: none; color: var(--text-muted);
  padding: .8rem 0; font-size: .9rem; font-weight: 700; cursor: pointer;
  border-bottom: 2px solid transparent; text-transform: uppercase; letter-spacing: .05em;
}
.etab.active { color: var(--accent); border-bottom-color: var(--accent); }
#editor-close { font-size: 1.5rem; padding: 0 1rem; }
#editor-body { flex: 1; overflow-y: auto; padding: .75rem 1rem; }
#editor-foot {
  display: flex; align-items: center; gap: .8rem;
  padding: .6rem 1rem; border-top: 1px solid var(--line);
}
#editor-status { flex: 1; font-size: .82rem; color: var(--text-muted); }
.save-btn {
  background: var(--go); color: #06130c; border: none; border-radius: 10px;
  padding: .6rem 1.4rem; font-weight: 700; font-size: .95rem; cursor: pointer;
}
.save-btn:disabled { opacity: .45; }

.ed-hint { font-size: .85rem; color: var(--text-muted); margin-bottom: .6rem; line-height: 1.35; }
.ed-toolbar { display: flex; gap: .5rem; flex-wrap: wrap; margin-bottom: .6rem; }
.ed-btn {
  background: var(--panel-2); border: 1px solid var(--line); color: var(--text-heading);
  border-radius: 8px; padding: .45rem .7rem; font-size: .85rem; font-weight: 600; cursor: pointer;
}
.ed-btn.on { background: var(--accent); border-color: var(--accent); color: #2a0d05; }
.ed-btn.danger { color: #ff8f83; }

.site-row {
  display: flex; align-items: center; gap: .6rem;
  padding: .5rem .4rem; border-bottom: 1px solid var(--line);
  cursor: pointer; font-size: .92rem;
}
.site-row.sel { background: var(--panel-2); border-radius: 8px; }
.site-row .code { font-weight: 700; color: var(--text-heading); min-width: 3.6rem; }
.site-row .who { flex: 1; color: var(--text-muted); font-size: .82rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.site-row .done { color: var(--go); }
.site-row .missing { color: var(--danger); }

.camp-row {
  display: flex; align-items: center; gap: .6rem;
  padding: .5rem .4rem; border-bottom: 1px solid var(--line);
  cursor: pointer; font-size: .92rem;
}
.camp-row .nm { flex: 1; font-weight: 600; color: var(--text-heading);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.camp-row .sites { max-width: 45%; color: var(--text-muted); font-size: .82rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.camp-row .del { color: #ff8f83; background: none; border: none; font-size: 1.1rem; cursor: pointer; }

.anchor-row {
  display: flex; align-items: center; gap: .6rem;
  padding: .5rem .4rem; border-bottom: 1px solid var(--line); font-size: .88rem;
}
.anchor-row .lbl { flex: 1; color: var(--text-heading); }
.anchor-row .del { color: #ff8f83; background: none; border: none; font-size: 1.1rem; cursor: pointer; }

.ed-search {
  width: 100%; padding: .55rem .8rem; margin-bottom: .5rem;
  background: var(--input-bg); color: var(--text-heading);
  border: 1px solid var(--line); border-radius: 8px; font-size: .95rem;
}
.ed-search:focus { outline: none; border-color: var(--accent); }

@media (min-width: 700px) {
  #sheet, #editor { left: auto; right: .6rem; width: 24rem; border-radius: 14px;
    bottom: .6rem; border: 1px solid var(--line); }
  #editor { height: 60vh; }
}
