/* styles.css — a dark, high-contrast shell around a full-bleed map.
   Dark because the map is the content and chrome should recede; high
   contrast and large hit targets because this gets used at arm's length,
   in sunlight, possibly with one hand. */

:root {
  --bg: #0d1117;
  --panel: rgba(16, 20, 28, 0.92);
  --panel-solid: #10141c;
  --line: rgba(255, 255, 255, 0.14);
  --text: #f2f5f9;
  --muted: #9aa5b4;
  --accent: #ffd400;
  --danger: #ff453a;
  --radius: 14px;
  --tap: 48px;                     /* never go below this on a touch target */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

/* Author `display` declarations beat the UA stylesheet's `[hidden]{display:none}`,
   and the sheets/bars below all set one. Without this every panel would be
   painted on top of the map at load. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans Thai',
        'Helvetica Neue', Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
  touch-action: manipulation;              /* kill the 300ms double-tap delay */
}

#map {
  position: absolute;
  inset: 0;
  background: #0a0d12;
  /* dvh keeps the map full-height when iOS Safari's URL bar collapses */
  height: 100dvh;
  z-index: 0;
}

.fatal {
  position: absolute; z-index: 999; inset: auto 0 0 0;
  background: var(--danger); color: #fff; padding: 14px; text-align: center;
}

/* ── top bar ─────────────────────────────────────────────── */
#topbar {
  position: absolute;
  top: calc(var(--safe-t) + 8px);
  left: calc(var(--safe-l) + 8px);
  right: calc(var(--safe-r) + 8px);
  z-index: 500;
  display: flex;
  gap: 8px;
  align-items: flex-start;
  pointer-events: none;
}
#topbar > * { pointer-events: auto; }

.coord-readout {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 8px 12px;
  background: var(--panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  font: 500 13px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: left;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.coord-row { display: flex; gap: 8px; align-items: baseline; white-space: nowrap; }
.coord-row b {
  flex: none; width: 32px; color: var(--muted);
  font: 600 10px/1 -apple-system, sans-serif; letter-spacing: .06em;
}
.coord-row span { overflow: hidden; text-overflow: ellipsis; }

.top-actions { display: flex; gap: 8px; flex: none; }

/* Losing signal mid-survey is not an error state — the cached map keeps
   working — so this reports rather than alarms. */
#net-badge {
  align-self: center;
  display: grid; place-items: center;
  height: 30px; padding: 0 10px;
  background: var(--panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(255, 212, 0, .5);
  border-radius: 999px;
  color: var(--accent);
  font: 600 11px/1 inherit; letter-spacing: .06em; white-space: nowrap;
  box-shadow: var(--shadow);
}
body.sun #net-badge { height: 34px; font-size: 13px; }

.icon-btn {
  width: var(--tap); height: var(--tap);
  display: grid; place-items: center;
  background: var(--panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--text);
  box-shadow: var(--shadow);
  cursor: pointer;
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active { transform: scale(.96); }
.icon-btn.close { width: 36px; height: 36px; border: 0; background: transparent; font-size: 18px; box-shadow: none; }

/* ── floating tools ──────────────────────────────────────── */
#tools {
  position: absolute;
  right: calc(var(--safe-r) + 12px);
  bottom: calc(var(--safe-b) + 28px);
  z-index: 500;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
body.sheet-open #tools,
body.drawing #tools,
body.picking #tools,
body.editing #tools { opacity: 0; pointer-events: none; }

.fab {
  width: 54px; height: 54px;
  display: grid; place-items: center;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--panel);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  color: var(--text);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform .1s, background .15s, color .15s;
}
.fab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.fab:active { transform: scale(.94); }
.fab.active { background: var(--accent); color: #17150a; border-color: var(--accent); }
.fab.busy { animation: pulse 1.1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .45; } }

/* ── crosshair ───────────────────────────────────────────── */
#crosshair {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  z-index: 450;
  pointer-events: none;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,.6));
}

/* ── drawing / editing action bars ───────────────────────── */
#drawbar, #editbar, #p2pbar {
  position: absolute;
  left: calc(var(--safe-l) + 8px);
  right: calc(var(--safe-r) + 8px);
  bottom: calc(var(--safe-b) + 10px);
  z-index: 600;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom, 0px) * 0);
  background: var(--panel);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
}
.drawbar-info { display: flex; flex-direction: column; gap: 2px; }
.drawbar-info > span:first-child { font-weight: 700; }
.drawbar-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.drawbar-actions .btn { flex: 1 1 auto; min-width: 84px; }

/* ── buttons ─────────────────────────────────────────────── */
.btn {
  min-height: 44px;
  padding: 10px 14px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.07);
  color: var(--text);
  font: 600 14px/1.2 inherit;
  cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primary { background: var(--accent); color: #17150a; border-color: var(--accent); }
.btn.danger { color: var(--danger); border-color: rgba(255,69,58,.4); }
.btn.ghost { background: rgba(255,255,255,.05); }
.btn.tiny { min-height: 32px; padding: 4px 10px; font-size: 13px; }

/* ── bottom sheets ───────────────────────────────────────── */
#scrim {
  position: absolute; inset: 0; z-index: 700;
  background: rgba(0,0,0,.45);
  backdrop-filter: blur(2px);
}
.sheet {
  position: absolute;
  left: 0; right: 0; bottom: 0;
  z-index: 800;
  max-height: 82dvh;
  display: flex; flex-direction: column;
  background: var(--panel-solid);
  border-top: 1px solid var(--line);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -8px 40px rgba(0,0,0,.6);
  padding-bottom: var(--safe-b);
  animation: rise .18s ease-out;
}
@keyframes rise { from { transform: translateY(14px); opacity: .4; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }

/* On a wide screen a full-width sheet is silly — dock it left. */
@media (min-width: 820px) {
  /* Likewise the readout: stretched across a monitor it reads as a banner. */
  .coord-readout { flex: 0 1 340px; }

  .sheet {
    left: calc(var(--safe-l) + 12px);
    bottom: 12px;
    width: 400px;
    right: auto;
    border-radius: 18px;
    max-height: calc(100dvh - 120px);
  }
}

.sheet-grip {
  width: 40px; height: 4px; border-radius: 2px;
  background: rgba(255,255,255,.25);
  margin: 8px auto 0;
  flex: none;
}
.sheet-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 14px 4px;
  flex: none;
}
.sheet-head h2 { margin: 0; font-size: 17px; }
.sheet-body {
  padding: 8px 14px 18px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 14px;
}

.section { display: flex; flex-direction: column; gap: 8px; }
.section-title {
  display: flex; align-items: center; justify-content: space-between;
  font: 600 11px/1 inherit; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted);
}
.muted { color: var(--muted); }
.small { font-size: 12px; }

.row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; min-height: var(--tap);
}
.toggle input { width: 46px; height: 28px; flex: none; accent-color: var(--accent); }

.field { display: flex; flex-direction: column; gap: 5px; }
.field > span { font-size: 12px; color: var(--muted); }
.field input[type=text], .field select {
  min-height: 46px;
  padding: 10px 12px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: rgba(255,255,255,.06);
  color: var(--text);
  font: 15px/1.2 inherit;
  width: 100%;
}
.field input[type=text] { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.field input:focus, .field select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field input[type=range] { width: 100%; height: 34px; accent-color: var(--accent); }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 0.6fr 1fr 1fr; gap: 10px; }

.sheet-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.sheet-actions .btn { flex: 1 1 auto; min-width: 96px; }

.stats { display: flex; flex-direction: column; gap: 2px; }
.stat {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 7px 0; border-bottom: 1px solid rgba(255,255,255,.07);
  font-size: 13px;
}
.stat span { color: var(--muted); }
.stat b { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; text-align: right; }

/* ── colour swatches ─────────────────────────────────────── */
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch {
  width: 40px; height: 40px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.25);
  cursor: pointer; padding: 0;
}
.swatch.active {
  box-shadow: 0 0 0 3px var(--panel-solid), 0 0 0 5px #fff;
}

/* ── basemap picker ──────────────────────────────────────── */
.basemap-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.basemap-item {
  display: flex; flex-direction: column; gap: 2px;
  min-height: 56px; padding: 10px 12px;
  border-radius: 11px; border: 1px solid var(--line);
  background: rgba(255,255,255,.05);
  color: var(--text); text-align: left; cursor: pointer;
}
.basemap-item.active { border-color: var(--accent); background: rgba(255,212,0,.12); }
.bm-name { font-weight: 600; font-size: 14px; }
.bm-sub { font-size: 11px; color: var(--muted); }

/* ── layer list ──────────────────────────────────────────── */
#layer-list { display: flex; flex-direction: column; gap: 10px; }
.layer {
  border: 1px solid var(--line);
  border-radius: 12px;
  overflow: hidden;
  background: rgba(255,255,255,.03);
}
.layer.active { border-color: rgba(255,212,0,.55); }
.layer-head { display: flex; align-items: center; gap: 6px; padding: 6px 8px; }
.layer-head .layer-name {
  flex: 1 1 auto; min-width: 0;
  background: transparent; border: 0; color: var(--text);
  font: 600 15px/1.2 inherit; padding: 10px 4px;
}
.layer-head .layer-name:focus { outline: none; background: rgba(255,255,255,.07); border-radius: 8px; }
.layer-head .count {
  flex: none; min-width: 24px; text-align: center;
  font-size: 12px; color: var(--muted);
}
.eye, .kill {
  flex: none; width: 40px; height: 40px;
  display: grid; place-items: center;
  background: transparent; border: 0; color: var(--muted);
  cursor: pointer; border-radius: 8px;
}
.eye svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.eye svg.off { opacity: .45; }
.eye.small { width: 36px; height: 36px; }
.kill { font-size: 16px; }
.kill:hover { color: var(--danger); }

.feat-list { border-top: 1px solid var(--line); }
.feat-row {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 8px 6px 12px;
  min-height: 44px;
  cursor: pointer;
}
.feat-row + .feat-row { border-top: 1px solid rgba(255,255,255,.06); }
.feat-row:active { background: rgba(255,255,255,.06); }
.feat-row .dot { width: 12px; height: 12px; border-radius: 3px; flex: none; }
.feat-row .fname { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.feat-row .fmeta { flex: none; font-size: 12px; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.feat-empty { padding: 10px 12px; }

/* ── map overlays: labels, chips, vertices, gps ──────────── */
.map-label { pointer-events: none; }
.map-label span {
  display: inline-block;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  padding: 3px 7px;
  border-radius: 7px;
  background: rgba(8, 11, 16, .72);
  border: 1px solid rgba(255,255,255,.16);
  border-left: 3px solid var(--accent);
  color: #fff;
  font: 600 12px/1.35 -apple-system, 'Noto Sans Thai', sans-serif;
  text-shadow: 0 1px 2px rgba(0,0,0,.9);
}
.map-label span i { font-style: normal; font-weight: 500; color: #d7dee8; font-size: 11px; }
.map-label.is-selected span { border-color: #fff; background: rgba(8,11,16,.88); }
.map-label.sun span { font-size: 15px; padding: 5px 9px; background: rgba(0,0,0,.85); }
.map-label.sun span i { font-size: 13px; color: #fff; }

.seg-chip { pointer-events: none; }
.seg-chip span {
  display: inline-block;
  transform: translate(-50%, -50%);
  white-space: nowrap;
  padding: 1px 5px;
  border-radius: 5px;
  background: rgba(8, 11, 16, .66);
  color: #fff;
  font: 600 10.5px/1.3 ui-monospace, SFMono-Regular, Menlo, monospace;
  text-shadow: 0 1px 2px rgba(0,0,0,.9);
}
.seg-chip.is-selected span { background: rgba(8,11,16,.85); box-shadow: 0 0 0 1px var(--accent); }
.seg-chip.sun span { font-size: 13px; padding: 2px 6px; background: rgba(0,0,0,.85); }

.vtx { background: none; border: 0; }
.vtx i {
  display: block; width: 16px; height: 16px; margin: 6px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid #111;
  box-shadow: 0 1px 4px rgba(0,0,0,.6);
}
.vtx.ghost i {
  width: 12px; height: 12px; margin: 6px;
  background: rgba(255,255,255,.55);
  border: 2px dashed #111;
}

/* Pin-picking halos: a dashed ring marks what is tappable, and a filled
   badge carries the order it will be walked in. */
.pick { pointer-events: none; background: none; border: 0; }
.pick i {
  display: block;
  width: 26px; height: 26px; margin: 4px;
  box-sizing: border-box;
  border-radius: 50%;
  border: 2px dashed rgba(255,255,255,.85);
  background: rgba(8, 11, 16, .3);
  box-shadow: 0 1px 4px rgba(0,0,0,.6);
  font-style: normal;
}
.pick.on i {
  border: 2px solid #17150a;
  background: var(--accent);
  color: #17150a;
  font: 700 13px/22px ui-monospace, SFMono-Regular, Menlo, monospace;
  text-align: center;
}
body.sun .pick i { width: 32px; height: 32px; margin: 1px; }
body.sun .pick.on i { font-size: 15px; line-height: 28px; }

.gps-dot i {
  display: block; width: 16px; height: 16px; margin: 3px;
  border-radius: 50%;
  background: #3b82f6;
  border: 3px solid #fff;
  box-shadow: 0 0 0 1px rgba(0,0,0,.35), 0 2px 6px rgba(0,0,0,.5);
}

/* ── toast ───────────────────────────────────────────────── */
#toast {
  position: absolute;
  left: 50%;
  bottom: calc(var(--safe-b) + 96px);
  transform: translateX(-50%);
  z-index: 900;
  display: flex; align-items: center; gap: 12px;
  max-width: calc(100vw - 32px);
  padding: 11px 14px;
  border-radius: 12px;
  background: rgba(28, 33, 43, .96);
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
  font-size: 14px;
}
#toast button {
  flex: none;
  background: none; border: 0;
  color: var(--accent);
  font: 700 14px/1 inherit;
  padding: 6px 4px;
  cursor: pointer;
}

/* ── sun mode: fatten everything the eye needs to find ───── */
body.sun {
  --panel: rgba(6, 8, 12, .96);
  --panel-solid: #06080c;
  --line: rgba(255,255,255,.3);
  --text: #ffffff;
  --muted: #c8d2df;
}
body.sun .coord-readout { font-size: 14px; }
body.sun .btn { font-size: 15px; }

/* ── Leaflet overrides ───────────────────────────────────── */
.leaflet-container {
  background: #0a0d12;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
}
.leaflet-control-attribution {
  background: rgba(8, 11, 16, .7) !important;
  color: var(--muted) !important;
  font-size: 10px !important;
  padding: 2px 6px !important;
  max-width: 62vw;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.leaflet-control-attribution a { color: var(--muted) !important; }
.leaflet-control-scale-line {
  background: rgba(8, 11, 16, .7) !important;
  color: #fff !important;
  border-color: rgba(255,255,255,.6) !important;
  font: 600 11px/1.4 ui-monospace, Menlo, monospace !important;
}
.leaflet-bottom.leaflet-left { margin-bottom: calc(var(--safe-b) + 4px); }
body.drawing .leaflet-bottom,
body.picking .leaflet-bottom,
body.editing .leaflet-bottom { display: none; }

/* Base tiles get a touch more contrast — satellite imagery at midday is
   washed out on a phone screen outdoors. */
.base-tiles { filter: contrast(1.06) saturate(1.05); }
body.sun .base-tiles { filter: contrast(1.18) saturate(1.12) brightness(.94); }
