/* ==========================================================================
   Canberrun Admin — trail map editor (v14)
   Everything inside the #editor container. The container's own fixed
   positioning (.editor) belongs to admin.css; tokens come from its :root.
   ========================================================================== */

/* The fixed .editor layer lives in admin.css with no overflow of its own; clip it
   here (this file loads after it) so nothing inside can paint outside the viewport. */
.editor { overflow: hidden; }

.ed {
  /* Safe-area shorthands — admin.css defines none, so keep our own (R-18). */
  --ed-safe-t: env(safe-area-inset-top, 0px);
  --ed-safe-r: env(safe-area-inset-right, 0px);
  --ed-safe-b: env(safe-area-inset-bottom, 0px);
  --ed-safe-l: env(safe-area-inset-left, 0px);
  display: grid;
  /* An explicit 1fr track: without it the top bar's min-content width sizes the
     whole layer and everything below it paints off-screen on a phone (A-01). */
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: 48px minmax(0, 1fr);
  height: 100%;
  min-height: 0;
  min-width: 0;
  max-width: 100%;
  background: var(--surface);
  color: var(--ink);
  font-size: 13.5px;
}

/* ---------------------------------------------------------------- top bar */

.ed-top {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
  padding: 0 calc(14px + var(--ed-safe-r)) 0 calc(14px + var(--ed-safe-l));
  border-bottom: 1px solid var(--hairline);
  background: var(--surface);
}

.ed-top__title { flex: 0 1 auto; min-width: 0; font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ed-top__stats { color: var(--ink-muted); font-size: 12.5px; white-space: nowrap; }
.ed-top__spacer { flex: 1; }

.ed-changes { display: flex; align-items: center; gap: 10px; min-width: 0; font-size: 12.5px; }
.ed-changes__sum { color: var(--ink-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.ed-pill {
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-sunk);
  color: var(--ink-faint);
  font-weight: 600;
  white-space: nowrap;
}
.ed-pill.is-dirty { background: var(--warn-soft); color: var(--warn); }
.ed-pill:empty { display: none; }

/* ---------------------------------------------------------------- buttons */

.ed-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 8px 13px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: var(--surface-sunk);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.2;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color .13s var(--ease), border-color .13s var(--ease), color .13s var(--ease);
}
.ed-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ed-btn[disabled] { opacity: .5; cursor: default; }
.ed-btn--primary { background: var(--accent); color: #fff; }
.ed-btn--primary:hover:not([disabled]) { background: var(--accent-dark); }
.ed-btn--ghost { background: var(--surface); border-color: var(--hairline); color: var(--ink-muted); }
.ed-btn--ghost:hover:not([disabled]) { background: var(--surface-sunk); color: var(--ink); }
.ed-btn--danger { background: var(--danger); color: #fff; }
.ed-btn--danger:hover:not([disabled]) { background: #b93a32; }
.ed-btn--danger-ghost { background: var(--surface); border-color: var(--hairline); color: var(--danger); }
.ed-btn--danger-ghost:hover:not([disabled]) { background: var(--danger-soft); border-color: var(--danger-soft); }
.ed-btn--sm { padding: 4px 9px; font-size: 12px; border-radius: 7px; }
.ed-btn.is-armed { background: var(--danger-soft); border-color: var(--danger); color: var(--danger); }
.ed-btn, .ed-tool, .ed-toggle { -webkit-user-select: none; user-select: none; touch-action: manipulation; }
.ed-back { gap: 5px; padding-left: 9px; }
.ed-back__label { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.ed-btn .spinner { width: 12px; height: 12px; }

/* ------------------------------------------------------------------- body */

.ed-body {
  display: grid;
  grid-template-columns: 56px 270px minmax(0, 1fr);
  min-height: 0;
  min-width: 0;
}

.ed-rail {
  display: grid;
  gap: 6px;
  align-content: start;
  padding: 10px 6px calc(10px + var(--ed-safe-b)) calc(6px + var(--ed-safe-l));
  background: var(--surface-sunk);
  border-right: 1px solid var(--hairline);
}

.ed-tool {
  display: grid;
  place-items: center;
  gap: 3px;
  padding: 8px 2px 7px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--ink-muted);
  font: inherit;
  font-size: 10.5px;
  font-weight: 600;
  cursor: pointer;
}
.ed-tool:hover { background: var(--surface); color: var(--ink); }
.ed-tool:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.ed-tool.is-active { background: var(--accent); color: #fff; }
.ed-icon { display: block; width: 22px; height: 22px; }
.ed-icon svg { width: 22px; height: 22px; display: block; }

/* ------------------------------------------------------------------ panel */

.ed-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  border-right: 1px solid var(--hairline);
  background: var(--surface);
  overflow-y: auto;
  overscroll-behavior: contain;
  min-height: 0;
}

.ed-tool-panel { display: flex; flex-direction: column; gap: 11px; }
.ed-tool-panel[hidden] { display: none; }

.ed-panel__title { margin: 0; font-size: 14px; font-weight: 700; letter-spacing: -.01em; }
.ed-panel__sub {
  margin: 4px 0 -6px;
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.ed-hint { margin: 0; font-size: 12px; line-height: 1.45; color: var(--ink-muted); }
.ed-hint--keys { color: var(--ink-faint); margin-top: -6px; }

.ed-row { display: flex; gap: 6px; flex-wrap: wrap; }
/* The row that finishes the job. Plain layout here; on a phone it pins to the
   foot of the panel (A-12). --stack keeps a lone full-width button full width. */
.ed-actions--stack { display: grid; gap: 6px; }

.ed-details {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--hairline);
}

/* v32: what a report brought with it — their track and their pin. Context for every
   tool rather than a tool of its own, so it sits above the tool panel behind a rule,
   the way the new-trail details block does. */
.ed-ref {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--hairline);
}

.ed-ref__who { color: var(--ink); font-weight: 600; overflow-wrap: anywhere; }

/* v32.2: "Add part of track" borrows the brush, and the block swaps its buttons for the
   pick controls while it has it — an accent edge says the brush is not the tool's. */
.ed-ref__pick { display: flex; flex-direction: column; gap: 8px; }
.ed-ref.is-picking { border-left: 3px solid var(--accent); padding-left: 9px; }
.ed-ref__marked {
  color: var(--ink);
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}

.ed-field { display: flex; flex-direction: column; gap: 5px; }
.ed-field > label {
  font-size: 11px;
  font-weight: 650;
  letter-spacing: .04em;
  text-transform: uppercase;
  color: var(--ink-faint);
}
.ed-opt { text-transform: none; letter-spacing: 0; font-weight: 500; }
.ed-field input[type="text"] {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--hairline);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 13.5px;
}
.ed-field input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.ed-stat {
  padding: 10px 12px;
  border-radius: 9px;
  background: var(--surface-sunk);
}
.ed-stat b { display: block; font-size: 14.5px; font-variant-numeric: tabular-nums; }
.ed-stat span { display: block; margin-top: 2px; font-size: 11.5px; color: var(--ink-muted); overflow-wrap: anywhere; }

/* brush toggle */
.ed-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  align-self: flex-start;
  padding: 5px 11px 5px 6px;
  border: 1px solid var(--hairline);
  border-radius: 999px;
  background: var(--surface);
  color: var(--ink);
  font: inherit;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.ed-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.ed-toggle i {
  position: relative;
  width: 28px;
  height: 16px;
  border-radius: 999px;
  background: #d9d4cb;
  transition: background-color .13s var(--ease);
}
.ed-toggle i::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .25);
  transition: left .13s var(--ease);
}
.ed-toggle.is-on { border-color: var(--danger); }
.ed-toggle.is-on i { background: var(--danger); }
.ed-toggle.is-on i::after { left: 14px; }

/* lists: erased paths, added paths, the import legend */
.ed-list {
  border: 1px solid var(--hairline);
  border-radius: 9px;
  overflow: hidden;
  font-size: 12.5px;
}
.ed-list[hidden] { display: none; }
.ed-list__row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px 6px 10px;
  border-top: 1px solid var(--hairline);
  background: var(--surface);
}
.ed-list__row:first-child { border-top: 0; }
.ed-list__row.is-new { background: #eef4fc; }
.ed-list__row.is-gone { background: var(--danger-soft); }
.ed-list__row.is-gone .ed-list__name { text-decoration: line-through; color: #a03a32; }
.ed-list__name {
  flex: 1;
  min-width: 0;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ed-list__name--link {
  padding: 0;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  font-weight: 600;
  text-align: left;
  cursor: pointer;
}
.ed-list__name--link:hover { color: var(--accent-dark); }
.ed-list__meta { color: var(--ink-muted); font-size: 11.5px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.ed-x {
  width: 26px;
  height: 22px;
  padding: 0 4px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--ink-faint);
  font: inherit;
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}
.ed-x:hover { background: var(--danger-soft); color: var(--danger); }
.ed-list__row.is-gone .ed-x { width: auto; font-size: 11.5px; font-weight: 600; color: var(--ink-muted); }
.ed-list__row.is-gone .ed-x:hover { background: var(--ok-soft); color: var(--ok); }

.ed-legend-list label { cursor: pointer; }
.ed-legend-list input { width: 15px; height: 15px; accent-color: var(--accent); flex: 0 0 auto; margin: 0; }
.ed-legend__item.is-off { color: var(--ink-faint); }
.ed-legend__item.is-off .ed-list__name { font-weight: 500; }
.ed-swatch { display: inline-block; width: 18px; height: 4px; border-radius: 2px; flex: 0 0 auto; }

/* import steps */
.ed-step { display: flex; flex-direction: column; gap: 11px; }
.ed-step[hidden] { display: none; }
.ed-step__head { display: flex; gap: 9px; align-items: baseline; font-size: 12.5px; line-height: 1.45; }
.ed-step__head b {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--accent-soft);
  color: var(--accent-dark);
  font-size: 11px;
}

/* -------------------------------------------------------------------- map */

.ed-map {
  position: relative;
  min-height: 0;
  min-width: 0;
  background: var(--surface-sunk);
}
.ed-map__canvas { position: absolute; inset: 0; }

.ed-legend {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 2;
  display: grid;
  gap: 4px;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .92);
  box-shadow: var(--shadow-card);
  font-size: 11.5px;
  color: var(--ink);
  pointer-events: none;
}
.ed-legend[hidden] { display: none; }
.ed-legend div { display: flex; align-items: center; gap: 7px; }
.ed-legend i { display: inline-block; width: 18px; height: 3px; border-radius: 2px; }

/* Draggable vertex handle. No `position` here — it must not override
   .maplibregl-marker's absolute positioning. */
.ed-vertex {
  /* The browser must not claim the touch as a pan: it would cancel the pointer
     mid press-and-hold, and dragging a point would fight the map. */
  touch-action: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid #1d6fd0;
  box-shadow: 0 1px 4px rgba(29, 28, 26, .35);
  cursor: grab;
}
.ed-vertex:active { cursor: grabbing; }

/* The brush cursor while the brush is on (beats the inline cursor the hover handler sets) */
.ed-map .maplibregl-canvas.is-wiping {
  cursor: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24'><circle cx='12' cy='12' r='10' fill='rgba(209,68,59,0.18)' stroke='%23d1443b' stroke-width='2'/></svg>") 12 12, crosshair !important;
}

@media (max-width: 900px) {
  .ed-body { grid-template-columns: 56px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .ed-panel {
    grid-column: 1 / -1;
    grid-row: 2;
    max-height: 50vh;
    max-height: 50dvh;   /* vh is the *large* viewport on a phone (R-10) */
    min-height: 190px;
    padding-bottom: 0;   /* the pinned action row carries it, so it sits flush */
    border-right: 0;
    border-top: 1px solid var(--hairline);
  }
  /* The tool panel and the import steps stop being boxes so the pinned action
     row below belongs to the scrolling panel itself — otherwise it cannot rise
     above its own step, which on a short landscape panel is already off the
     bottom (A-12). [hidden] still wins over this by specificity. */
  .ed-tool-panel, .ed-step { display: contents; }
  /* Those children are now the panel's own flex items: none of them may shrink,
     or a list (overflow: hidden, so its automatic minimum size is 0) collapses
     under its own rows. The panel scrolls instead. */
  .ed-tool-panel > *, .ed-step > * { flex-shrink: 0; }
  .ed-map { grid-column: 2; grid-row: 1; }
  .ed-rail { grid-row: 1; }
  .ed-changes__sum { display: none; }

  /* Top bar: the back arrow, then the three that must survive — Undo, Discard,
     Save. The panel already carries the title and the stats (A-01/R-01). */
  .ed-top { gap: 8px; padding: 0 calc(8px + var(--ed-safe-r)) 0 calc(8px + var(--ed-safe-l)); }
  .ed-top__title, .ed-top__stats, .ed-back__label { display: none; }
  .ed-back { padding: 8px 12px; }
  .ed-btn { padding: 8px 11px; }

  /* Each tool's primary action goes last and stays at the foot of the panel,
     instead of sitting below the fold (A-12). */
  .ed-actions {
    order: 9;
    position: sticky;
    bottom: 0;
    z-index: 3;
    margin: 0 -14px;
    padding: 10px 14px calc(12px + var(--ed-safe-b));
    background: var(--surface);
    border-top: 1px solid var(--hairline);
  }

  /* The attribution fills the foot of a narrow map, and it is a licence
     obligation: put the legend at the top instead (A-19). */
  .ed-legend { top: 10px; bottom: auto; }
}

/* A phone in landscape has room for the changes pill; a 360 px portrait one has not. */
@media (max-width: 619px) {
  .ed-changes { display: none; }
}

/* Landscape has room for more panel, but the map must not become a sliver:
   60% of the height, and never so much that under 150 px of map is left. */
@media (max-width: 900px) and (orientation: landscape) {
  .ed-panel { max-height: 60vh; max-height: min(60dvh, calc(100dvh - 198px)); min-height: 170px; }
}

/* Import: "start from a place" search + results (v14.1) */
.ed-row--search { flex-wrap: nowrap; }
.ed-row--search input[type="search"] {
  flex: 1 1 auto; min-width: 0; padding: 7px 10px; font: inherit; font-size: 13px;
  border: 1px solid var(--hairline); border-radius: var(--radius-sm); background: var(--surface); color: var(--ink);
}
.ed-row--search input[type="search"]:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.ed-place-results { max-height: 220px; overflow-y: auto; overscroll-behavior: contain; }
.ed-list__row--pick { width: 100%; text-align: left; font: inherit; background: var(--surface); border-left: 0; border-right: 0; border-bottom: 0; cursor: pointer; }
.ed-list__row--pick:hover, .ed-list__row--pick:focus-visible { background: var(--accent-soft); outline: none; }
.ed-list__meta.is-warn { color: var(--danger); }

/* Wrapping is the safety valve on a phone: the field keeps a small basis so the
   Search button stays beside it at 360 px and only drops under it if it truly
   cannot fit (A-03). After the rules above, which would win on source order. */
@media (max-width: 900px) {
  .ed-row--search { flex-wrap: wrap; }
  .ed-row--search input[type="search"] { flex: 1 1 140px; }
  .ed-row--search .ed-btn { flex: 0 1 auto; }
}

/* Brush mode: Whole path / Section (v14.2) */
.ed-brushmode { display: flex; flex-direction: column; gap: 6px; }
.ed-seg { display: inline-flex; align-self: flex-start; border: 1px solid var(--hairline); border-radius: var(--radius-sm); overflow: hidden; }
.ed-seg .ed-btn { border: 0; border-radius: 0; padding: 4px 10px; font-size: 12px; background: var(--surface); }
.ed-seg .ed-btn + .ed-btn { border-left: 1px solid var(--hairline); }
.ed-seg .ed-btn.is-active { background: var(--accent); color: #fff; }
.ed-brushmode .ed-hint--keys { margin-top: 0; }

/* Unmark and Delete last point are touch stand-ins for Shift and the right
   button: hidden unless the pointer is coarse. */
.ed-touch-only { display: none; }

/* ------------------------------------------------------------------- touch
   Finger-sized targets, and the controls that stand in for the keys and the
   right mouse button a phone does not have (R-03, R-07, A-16). A fine pointer
   keeps the desktop sizes. */

@media (pointer: coarse) {
  .ed-btn { min-height: 40px; }
  .ed-btn--sm { min-height: 36px; padding: 6px 12px; font-size: 12.5px; }
  .ed-seg .ed-btn { min-height: 38px; padding: 8px 14px; }
  .ed-toggle { min-height: 40px; padding: 6px 13px 6px 8px; font-size: 12.5px; }
  .ed-tool { padding: 11px 2px 10px; }
  .ed-x { min-width: 40px; height: 36px; font-size: 17px; }
  .ed-list__row { padding: 9px 8px 9px 10px; }
  .ed-list__row--pick { min-height: 46px; }
  .ed-legend-list input { width: 22px; height: 22px; }
  /* 16 px, or iOS zooms the page in on focus and never zooms back (R-03). */
  .ed-field input[type="text"], .ed-row--search input[type="search"] { font-size: 16px; }
  /* One scroller, not two nested ones (R-16). */
  .ed-place-results { max-height: none; }
  /* Shift, Ctrl+Z and B are not on a phone; the on-screen controls cover them. */
  .ed-hint--keys { display: none; }
  /* Fat enough to drag, and to press and hold to delete (R-07). */
  .ed-vertex { width: 22px; height: 22px; border-width: 4px; }
  /* Unmark and Delete last point stand in for Shift and the right button. */
  .ed-touch-only { display: inline-flex; }
}
