:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1d2733;
  --muted: #607083;
  --line: #dde3ea;
  --brand: #1f3a5f;
  --brand-2: #2d5a8c;
  --accent: #d4a017;
  --danger: #c0392b;
  --ok: #1e8449;
  --radius: 10px;
  --shadow: 0 6px 24px rgba(20, 35, 55, .12);
  font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Tahoma", system-ui, sans-serif;
  color: var(--ink);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); }
button, input, select, textarea { font: inherit; }
.hidden { display: none !important; }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 14px; line-height: 1.2;
}
.btn:hover { background: #f0f3f7; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn-primary:hover { background: var(--brand-2); }
.btn-danger { color: var(--danger); border-color: #f1c4bf; }
.btn-danger:hover { background: #fdecea; }
.btn-block { width: 100%; }
.btn-sm { padding: 4px 10px; font-size: 13px; }
label { display: block; font-size: 13px; color: var(--muted); margin: 10px 0 4px; }
input[type=text], input[type=password], input[type=datetime-local], select, textarea {
  width: 100%; padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #9cc0ea; border-color: #9cc0ea; }
textarea { min-height: 70px; resize: vertical; }
.error-text { color: var(--danger); font-size: 13px; margin-top: 8px; min-height: 1em; }

/* ---------- login ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: linear-gradient(160deg, #1f3a5f 0%, #2d5a8c 55%, #eef2f7 55%); }
.login-card { width: 100%; max-width: 400px; background: var(--panel); border-radius: 14px; box-shadow: var(--shadow); padding: 28px; }
.brand { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.brand-badge { width: 44px; height: 44px; border-radius: 10px; background: var(--brand); display: grid; place-items: center; }
.brand h1 { font-size: 20px; margin: 0; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 13px; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; background: #eef2f7; border-radius: 9px; padding: 4px; gap: 4px; }
.segmented button { border: 0; background: transparent; padding: 8px; border-radius: 7px; cursor: pointer; color: var(--muted); font-size: 14px; }
.segmented button.active { background: #fff; color: var(--brand); font-weight: 600; box-shadow: 0 1px 3px rgba(0,0,0,.1); }
.sso-box { text-align: center; padding: 18px 0 4px; color: var(--muted); font-size: 14px; }
.login-foot { margin-top: 18px; font-size: 12px; color: var(--muted); text-align: center; }

/* ---------- app layout ---------- */
.app { display: grid; grid-template-columns: 340px 1fr; height: 100%; }
.sidebar { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sidebar header { padding: 14px 16px; background: var(--brand); color: #fff; display: flex; align-items: center; gap: 10px; }
.sidebar header h1 { font-size: 17px; margin: 0; flex: 1; }
.user-box { font-size: 12px; opacity: .9; text-align: right; }
.user-box button { margin-top: 4px; background: transparent; color: #fff; border: 1px solid rgba(255,255,255,.5); border-radius: 6px; padding: 2px 8px; cursor: pointer; font-size: 12px; }
.side-scroll { overflow-y: auto; flex: 1; padding: 6px 16px 20px; }
.section { border-bottom: 1px solid var(--line); padding: 12px 0; }
.section:last-child { border-bottom: 0; }
.section h2 { font-size: 14px; margin: 0 0 8px; color: var(--brand); display: flex; justify-content: space-between; align-items: center; }
.tool-grid { display: grid; grid-template-columns: 1fr; gap: 6px; }
.tool-grid .btn { justify-content: flex-start; }
.tool-grid .btn.active { background: #fff6dc; border-color: var(--accent); }
.filter-item { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 14px; cursor: pointer; }
.filter-item input { width: 16px; height: 16px; }
.swatch { width: 12px; height: 12px; border-radius: 50%; display: inline-block; border: 1px solid rgba(0,0,0,.2); }
.swatch.sq { border-radius: 3px; }
.swatch.line { height: 4px; border-radius: 2px; width: 16px; }
.count { margin-left: auto; font-size: 12px; color: var(--muted); background: #eef2f7; border-radius: 10px; padding: 1px 8px; }
.row { display: flex; gap: 6px; }
.row > * { flex: 1; }
.result-summary { font-size: 13px; background: #f4f7fb; border-radius: 8px; padding: 8px 10px; margin-top: 10px; }
.result-list { list-style: none; padding: 0; margin: 8px 0 0; font-size: 13px; }
.result-list li { display: flex; gap: 8px; padding: 6px 4px; border-bottom: 1px dashed var(--line); cursor: pointer; }
.result-list li:hover { background: #f4f7fb; }
.seq { width: 22px; height: 22px; border-radius: 50%; background: var(--danger); color: #fff; display: grid; place-items: center; font-size: 12px; flex: none; }

.map-wrap { position: relative; min-width: 0; }
#map { position: absolute; inset: 0; }
#map.picking { cursor: crosshair !important; }
/* while adding data, existing features must not swallow the click — it belongs to the map */
#map.picking .leaflet-overlay-pane .leaflet-interactive,
#map.picking .leaflet-marker-pane .leaflet-interactive { pointer-events: none !important; }

/* banner shown while adding data */
.mode-banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 1000;
  background: #1d2733; color: #fff; border-radius: 10px; padding: 10px 12px 10px 16px; box-shadow: var(--shadow);
  display: flex; align-items: center; gap: 10px; font-size: 14px; max-width: calc(100% - 24px);
}
.mode-banner kbd { background: #3b4756; border-radius: 4px; padding: 1px 6px; font-size: 12px; }
.mode-banner .btn { padding: 5px 10px; font-size: 13px; white-space: nowrap; flex: none; }

.coord-bar { position: absolute; bottom: 8px; left: 8px; z-index: 900; background: rgba(255,255,255,.9); border-radius: 6px; padding: 3px 8px; font-size: 12px; color: var(--muted); }

/* modal */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15, 25, 40, .35); z-index: 2000; display: grid; place-items: center; padding: 16px; }
.modal { width: 100%; max-width: 420px; background: #fff; border-radius: 12px; box-shadow: var(--shadow); padding: 20px; max-height: 90vh; overflow: auto; }
.modal h3 { margin: 0 0 4px; font-size: 17px; }
.modal .sub { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

/* toast */
.toast-wrap { position: fixed; right: 16px; bottom: 16px; z-index: 3000; display: flex; flex-direction: column; gap: 8px; }
.toast { background: #1d2733; color: #fff; padding: 10px 14px; border-radius: 8px; box-shadow: var(--shadow); font-size: 14px; max-width: 340px; }
.toast.ok { background: var(--ok); }
.toast.err { background: var(--danger); }

/* map markers */
.cctv-icon { background: transparent; border: 0; }
.route-num { background: var(--danger); color: #fff; border: 2px solid #fff; border-radius: 50%; width: 24px !important; height: 24px !important;
  display: grid; place-items: center; font-size: 12px; font-weight: 700; box-shadow: 0 1px 4px rgba(0,0,0,.4); }
.popup h4 { margin: 0 0 4px; font-size: 15px; }
.popup .meta { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.popup .desc { font-size: 13px; white-space: pre-wrap; margin-bottom: 6px; }
.tag { display: inline-block; font-size: 11px; padding: 1px 7px; border-radius: 10px; background: #eef2f7; color: var(--muted); margin-right: 4px; }
.tag.sensitive { background: #fdecea; color: var(--danger); }

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 45vh 1fr; }
  .sidebar { order: 2; border-right: 0; border-top: 1px solid var(--line); }
  .map-wrap { order: 1; }
}
