:root {
  --bg: #f6f6f2; --card: #fff; --ink: #17211a; --muted: #5d6a60; --line: #dcded6; --accent: #1f6f43;
  --accent-ink: #fff; --ok: #1e7f45; --warn: #a86400; --bad: #b3261e; --series: #2a78d6; --grid: #e6e7e1;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #121613; --card: #1b211d; --ink: #e7ede8; --muted: #9aa89f; --line: #2d3630; --accent: #3fa36b;
    --accent-ink: #07130c; --ok: #4cc47c; --warn: #e5a23c; --bad: #ff7a70; --series: #3987e5; --grid: #2a322d; }
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select { font: inherit; color: inherit; }
button { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px;
  min-height: 40px; cursor: pointer; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.on { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
button:disabled { opacity: .45; cursor: default; }
input, select { background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px;
  min-height: 40px; max-width: 100%; }
a { color: var(--accent); }
h1 { font-size: 22px; margin: 8px 0 16px; }
h2 { font-size: 16px; margin: 20px 0 8px; }
.muted { color: var(--muted); }
.ok { color: var(--ok); } .warn { color: var(--warn); } .bad { color: var(--bad); }

#login { max-width: 320px; margin: 12vh auto; padding: 0 16px; display: flex; flex-direction: column; gap: 10px;
  align-items: stretch; text-align: center; }
#login img { align-self: center; }
#login label { display: flex; flex-direction: column; gap: 4px; text-align: left; }

#app { height: 100dvh; display: flex; flex-direction: column; }
/* dragging a fence corner must not select the page */
nav, header, button, #mapwrap, #app.split { -webkit-user-select: none; user-select: none; }
header { display: flex; gap: 10px; align-items: center; padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
  border-bottom: 1px solid var(--line); background: var(--card); font-size: 13px; }
#demo { display: flex; gap: 10px; align-items: center; padding: 6px 16px; font-size: 13px;
  background: var(--card); border-bottom: 1px solid var(--line); color: var(--muted); }
#demo span { flex: 1; }
#demo button { padding: 4px 10px; font-size: 13px; white-space: nowrap; }
#help { max-width: 560px; width: calc(100% - 32px); max-height: calc(100% - 48px); border: 1px solid var(--line);
  border-radius: 14px; padding: 16px 18px; background: var(--card); color: inherit; font-size: 15px; line-height: 1.45; }
#help::backdrop { background: rgb(0 0 0 / .45); }
#help h2 { margin: 0 0 8px; font-size: 19px; }
#help p { margin: 0 0 10px; }
#help form { text-align: right; }
header #summary { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pill { border: 1px solid currentColor; border-radius: 99px; padding: 1px 9px; font-weight: 600; white-space: nowrap; }
main { flex: 1; min-height: 0; position: relative; display: flex; flex-direction: column; }
#mapwrap { flex: 1; min-height: 0; position: relative; }
#map { position: absolute; inset: 0; background: var(--grid); }
#mapbar { position: absolute; top: 8px; left: 8px; right: 52px; display: flex; flex-wrap: wrap; gap: 6px;
  pointer-events: none; }
#mapbar > * { pointer-events: auto; box-shadow: 0 1px 4px rgb(0 0 0 / .25); }
#app.split #mapbar .legend, #mapwrap:has(#sheet:not([hidden])) .legend { display: none; }
#mapbar .legend { background: var(--card); border-radius: 10px; padding: 6px 10px; display: flex; gap: 10px;
  flex-wrap: wrap; font-size: 12px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 4px; }
#sheet { position: absolute; left: 0; right: 0; bottom: 0; max-height: 55%; overflow: auto; background: var(--card);
  z-index: 3; border-radius: 16px 16px 0 0; border-top: 1px solid var(--line); padding: 12px 16px 16px; }
#panel { flex: 1; min-height: 0; overflow: auto; padding: 4px 16px 24px; }
/* the fence tab shows the map above its controls */
#app.split #panel { flex: 0 1 auto; max-height: 52%; border-top: 1px solid var(--line); background: var(--bg); }
nav { display: flex; border-top: 1px solid var(--line); background: var(--card);
  padding-bottom: env(safe-area-inset-bottom); }
nav button { flex: 1; border: 0; border-radius: 0; background: none; color: var(--muted); min-height: 52px;
  position: relative; }
nav button.on { color: var(--accent); font-weight: 700; box-shadow: inset 0 3px 0 var(--accent); }
nav b { position: absolute; top: 6px; margin-left: 2px; background: var(--bad); color: var(--card);
  border-radius: 99px; font-size: 11px; min-width: 18px; padding: 0 5px; }
@media (min-width: 900px) {
  main { flex-direction: row; }
  #panel { flex: 1; }
  #app.split #panel { flex: 0 0 380px; max-height: none; border-top: 0; border-left: 1px solid var(--line); }
  #sheet { right: auto; width: 380px; max-height: 80%; border-radius: 0 16px 0 0; }
  #panel > * { max-width: 760px; }
}

.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.row > .grow { flex: 1; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin: 8px 0; }
.card.tap { cursor: pointer; }
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(96px, 1fr)); gap: 8px; }
.tile { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; }
.tile b { display: block; font-size: 22px; }
.tile span { font-size: 12px; color: var(--muted); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: right; padding: 6px 4px; border-bottom: 1px solid var(--line); }
th:first-child, td:first-child { text-align: left; }
th { color: var(--muted); font-weight: 500; }
ul.plain { list-style: none; padding: 0; margin: 6px 0; }
ul.plain li { padding: 3px 0; }
.field { display: flex; justify-content: space-between; gap: 12px; align-items: center; margin: 8px 0; }
.field input[type=number] { width: 90px; }
.bar { display: flex; height: 10px; border-radius: 6px; overflow: hidden; gap: 2px; background: var(--grid); }
.bar i { display: block; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--muted); font-size: 11px; }
.tip { position: absolute; pointer-events: none; background: var(--card); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 8px; font-size: 12px; box-shadow: 0 2px 8px rgb(0 0 0 / .25); white-space: nowrap; }
.vertex { width: 22px; height: 22px; border-radius: 50%; background: #fff; border: 3px solid #e0662f;
  box-shadow: 0 0 3px rgb(0 0 0 / .5); cursor: grab; }
.vertex.sel { background: #e0662f; }
.vertex.mid { width: 16px; height: 16px; opacity: .65; border-width: 2px; }
#toast { position: fixed; left: 50%; bottom: 72px; transform: translateX(-50%); background: var(--ink);
  color: var(--bg); border-radius: 10px; padding: 8px 14px; opacity: 0; transition: opacity .2s; pointer-events: none;
  max-width: 90vw; text-align: center; }
#toast.show { opacity: 1; }
