:root {
  --bg: #f4f3ef; --panel: #ffffff; --ink: #1f2328; --muted: #5d636b; --line: #d9d7d0;
  --accent: #c0392b; --accent-ink: #fff; --sel: #1e88e5;
  --bar-h: 48px; --pal-w: 280px;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #17191c; --panel: #202327; --ink: #e8e6e1; --muted: #9aa0a6; --line: #34383d; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 14px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, select, input { font: inherit; color: inherit; }

#bar { position: fixed; inset: 0 0 auto 0; height: var(--bar-h); display: flex; align-items: center; gap: 6px;
  padding: 0 10px; background: var(--panel); border-bottom: 1px solid var(--line); z-index: 1000; overflow-x: auto; }
.brand { white-space: nowrap; }
.sep { flex: 1 0 8px; }
.field { display: flex; align-items: center; gap: 6px; color: var(--muted); white-space: nowrap; }
.field select { padding: 4px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.btn, #props button { padding: 5px 10px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg);
  cursor: pointer; white-space: nowrap; }
.btn:hover, #props button:hover { border-color: var(--muted); }
.btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.icon { border: 0; background: transparent; font-size: 20px; cursor: pointer; padding: 4px 8px; }

#palette { position: fixed; top: var(--bar-h); bottom: 0; left: 0; width: var(--pal-w); background: var(--panel);
  border-right: 1px solid var(--line); overflow-y: auto; z-index: 900; padding: 8px; }
#search { width: 100%; padding: 6px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
details { margin-top: 8px; }
summary { cursor: pointer; font-weight: 600; padding: 4px 0; color: var(--muted); }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.tile { min-width: 0; border: 1px solid var(--line); border-radius: 6px; background: #e9e7e1; cursor: grab; padding: 3px; text-align: left; }
.tile canvas { display: block; width: 100%; aspect-ratio: 16 / 9; }
.tile span { display: block; font-size: 11px; color: #33383d; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tile:hover { border-color: var(--sel); }

#map { position: fixed; top: var(--bar-h); bottom: 0; left: var(--pal-w); right: 0; background: #cfd3cf; }
body.no-pal #palette { display: none; }
body.no-pal #map { left: 0; }
#map.tool { cursor: crosshair; }

#props { position: fixed; top: calc(var(--bar-h) + 10px); right: 10px; width: 270px; background: var(--panel);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px; z-index: 950; box-shadow: 0 4px 18px rgba(0,0,0,.18); }
#props[hidden] { display: none; }
.props-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
#p-desc { color: var(--muted); font-size: 12px; margin: 4px 0 8px; max-height: 6em; overflow: auto; }
.row { display: flex; align-items: center; gap: 6px; margin: 6px 0; }
.row > label:first-child { width: 70px; color: var(--muted); }
.row input[type=number] { width: 90px; padding: 3px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.row input[type=range] { flex: 1; }
.row.btns { flex-wrap: wrap; }
.row.btns button { flex: 1 0 auto; }
#props button.danger { color: #b3261e; }
#props.photo .piece-only, #props.piece .photo-only, #props.piece .img-only,
#props.ortho .piece-only, #props.ortho .photo-only, #props.ortho .xf-only,
#props:not(.road) .road-only, #props.road .xf-only, #props.road .piece-only, #props.road .photo-only, #props.road .img-only { display: none; }
#props select { flex: 1; min-width: 0; padding: 3px 6px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.rb-vtx { position: absolute; left: 0; top: 0; width: 14px; height: 14px; margin: -7px 0 0 -7px; border-radius: 50%; background: #fff;
  border: 2px solid var(--sel); box-shadow: 0 1px 3px rgba(0,0,0,.45); pointer-events: auto; cursor: move; touch-action: none; }
.rb-port { position: absolute; left: 0; top: 0; width: 16px; height: 16px; margin: -8px 0 0 -8px; border-radius: 50%;
  border: 2px solid #ff9800; background: rgba(255, 152, 0, .25); pointer-events: none; }
.rb-port::after { content: ''; position: absolute; left: 50%; top: 50%; width: 16px; height: 2px; margin-top: -1px;
  background: #ff9800; transform-origin: 0 50%; transform: rotate(var(--dir)); }
.rb-port.free::after { display: none; }
.rb-vtx.mid { width: 9px; height: 9px; margin: -4.5px 0 0 -4.5px; opacity: .8; cursor: copy; }
.tile.road img { display: block; width: 100%; aspect-ratio: 16 / 7; object-fit: contain; background: #a7aca6; }
#imagery { margin: 0 0 8px; }
.layer { display: flex; align-items: center; gap: 6px; padding: 3px 0; }
.layer span { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.layer span:hover { color: var(--sel); }
.layer button { padding: 2px 8px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); cursor: pointer; }
.hint { color: var(--muted); font-size: 11px; margin: 8px 0 0; }

/* pieces and photos live in their own Leaflet panes */
.rb-el { position: absolute; left: 0; top: 0; transform-origin: 0 0; pointer-events: none; user-select: none; max-width: none; }
.rb-sel { position: absolute; left: 0; top: 0; transform-origin: 0 0; border: 1.5px dashed var(--sel); pointer-events: none; }
.rb-handle { position: absolute; width: 18px; height: 18px; margin: -9px 0 0 -9px; border-radius: 50%;
  background: var(--sel); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4); pointer-events: auto; cursor: grab; touch-action: none; }
.rb-label { background: rgba(0,0,0,.75); color: #fff; border: 0; border-radius: 4px; padding: 2px 6px; font-weight: 600; }
.rb-label::before { display: none; }
.rb-pin { width: 22px; height: 22px; border-radius: 50%; color: #fff; font-weight: 700; display: grid; place-items: center;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.5); }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: rgba(20,20,20,.9); color: #fff;
  padding: 8px 14px; border-radius: 8px; z-index: 2000; opacity: 0; transition: opacity .2s; pointer-events: none; max-width: 90vw; }
#toast.show { opacity: 1; }

@media (max-width: 760px) {
  :root { --pal-w: 0px; }
  #palette { width: 100%; top: auto; height: 42vh; border-right: 0; border-top: 1px solid var(--line); }
  body:not(.no-pal) #map { bottom: 42vh; }
  #props { left: 10px; right: 10px; width: auto; padding: 8px; }
  #p-desc, .hint { display: none; }
  .grid { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
