:root {
  --bg: #F2F4F7;
  --card: #FFFFFF;
  --ink: #142233;
  --ink-2: #4A5A6E;
  --ink-3: #7C8A9C;
  --line: #DFE4EA;
  --brand: #0B3D6E;
  --primary: #0B63C4;
  --primary-ink: #FFFFFF;
  --primary-soft: #E6F0FB;
  --ok: #18794E;
  --ok-soft: #E3F4EA;
  --warn: #9A5B00;
  --warn-soft: #FFF3DD;
  --danger: #C62828;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 32, 52, .06), 0 2px 8px rgba(16, 32, 52, .05);
  --mono: "IBM Plex Mono", ui-monospace, Menlo, monospace;
  --tabs-h: 64px;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font: 16px/1.5 "IBM Plex Sans Thai", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  padding-bottom: calc(var(--tabs-h) + env(safe-area-inset-bottom));
}
a { color: var(--primary); }
button, input, select, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Top bar & tabs ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: var(--brand); color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; min-width: 0; }
.brand-mark {
  font: 600 13px/1 var(--mono); letter-spacing: .06em;
  background: #fff; color: var(--brand); padding: 6px 7px; border-radius: 6px;
}
.brand-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who { font-size: 13px; opacity: .8; white-space: nowrap; }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1000;
  display: grid; grid-template-columns: repeat(4, 1fr);
  height: calc(var(--tabs-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card); border-top: 1px solid var(--line);
}
.tabs a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  font-size: 12px; color: var(--ink-3); text-decoration: none;
}
.tabs svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tabs a.active { color: var(--primary); font-weight: 600; }

/* ---------- Layout ---------- */
.view { max-width: 720px; margin: 0 auto; padding: 16px; }
.stack > * + * { margin-top: 12px; }
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; }
.card h2 { font-size: 13px; font-weight: 600; letter-spacing: .04em; text-transform: uppercase; color: var(--ink-3); margin: 0 0 10px; }
.muted { color: var(--ink-3); font-size: 14px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1 1 auto; min-width: 0; }
.center { text-align: center; }

/* ---------- Forms ---------- */
label.f { display: block; font-size: 13px; color: var(--ink-2); margin: 12px 0 4px; }
.input, select.input, textarea.input {
  width: 100%; padding: 11px 12px; font-size: 16px;
  background: #fff; border: 1px solid var(--line); border-radius: 10px; outline: none;
}
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea.input { min-height: 96px; resize: vertical; }
.search {
  position: sticky; top: 56px; z-index: 900;
  margin: -16px -16px 0; padding: 12px 16px 10px; background: var(--bg);
}
.search .input { padding-left: 42px; font-size: 17px; height: 50px; border-radius: 12px; box-shadow: var(--shadow); border-color: transparent; }
.search-wrap { position: relative; }
.search-wrap svg { position: absolute; left: 13px; top: 13px; width: 24px; height: 24px; fill: none; stroke: var(--ink-3); stroke-width: 2; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 0 16px; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; color: var(--ink); font-size: 15px; font-weight: 500; text-decoration: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: translateY(1px); }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.soft { background: var(--primary-soft); border-color: transparent; color: var(--primary); }
.btn.danger { color: var(--danger); border-color: #F2C9C9; }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn[disabled] { opacity: .55; pointer-events: none; }
.btns { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; margin-top: 12px; }

/* ---------- Chips & badges ---------- */
.chips { display: flex; gap: 6px; overflow-x: auto; padding: 8px 0 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; padding: 6px 12px; border-radius: 999px; font-size: 13px; white-space: nowrap;
  background: #fff; border: 1px solid var(--line); color: var(--ink-2); cursor: pointer;
}
.chip.on { background: var(--brand); border-color: var(--brand); color: #fff; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; flex: none; }
.badge { display: inline-block; font-size: 12px; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.wait { background: var(--warn-soft); color: var(--warn); }

/* ---------- Site list ---------- */
.list { display: grid; gap: 8px; margin-top: 4px; }
.item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  background: var(--card); border-radius: 12px; box-shadow: var(--shadow); color: inherit; text-decoration: none;
}
.item .code { font: 600 16px/1.2 var(--mono); }
.item .name { font-size: 14px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .dots { display: flex; gap: 4px; margin-top: 4px; align-items: center; }
.item .chev { width: 18px; height: 18px; stroke: var(--ink-3); fill: none; stroke-width: 2; flex: none; }
mark { background: #FFE58F; color: inherit; border-radius: 2px; }

/* ---------- Site detail ---------- */
.hero { padding: 18px 16px; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); }
.hero .gg { font: 600 28px/1.1 var(--mono); letter-spacing: .02em; }
.hero .local { font-size: 17px; color: var(--ink-2); margin-top: 4px; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 6px 12px; font-size: 15px; }
.kv dt { color: var(--ink-3); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.coord { font: 500 18px/1.3 var(--mono); padding: 10px 12px; background: var(--bg); border-radius: 10px; user-select: all; }
.route-row { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-top: 1px solid var(--line); font-size: 14px; }
.route-row:first-of-type { border-top: 0; }
.route-row .nb { margin-left: auto; display: flex; gap: 6px; }
.route-row .nb a { font: 500 13px var(--mono); padding: 3px 8px; border-radius: 6px; background: var(--primary-soft); text-decoration: none; }
.contact { padding: 12px 0; border-top: 1px solid var(--line); }
.contact:first-of-type { border-top: 0; padding-top: 0; }
.contact .op { font-size: 13px; color: var(--ink-3); }
.contact .nm { font-weight: 600; font-size: 16px; }
.line-act { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.line-act .val { flex: 1; min-width: 0; font-family: var(--mono); font-size: 15px; overflow-wrap: anywhere; }
.minimap { height: 200px; border-radius: 10px; margin-top: 12px; z-index: 0; }
.history { font-size: 13px; color: var(--ink-2); }
.history li { margin: 4px 0; }
details summary { cursor: pointer; color: var(--ink-2); font-size: 14px; }

/* ---------- Route calc ---------- */
.swap { display: flex; justify-content: center; margin: 6px 0 -4px; }
.result { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: var(--bg); border-radius: 10px; padding: 12px; }
.stat .n { font: 600 26px/1.1 var(--mono); }
.stat .u { font-size: 13px; color: var(--ink-3); }
.bigmap { height: 320px; border-radius: 12px; margin-top: 12px; z-index: 0; }
.fullmap { height: calc(100vh - 56px - var(--tabs-h) - env(safe-area-inset-bottom) - 32px); min-height: 360px; border-radius: 12px; z-index: 0; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 13px; margin-bottom: 8px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.site-pin {
  font: 600 11px/1 var(--mono); color: #fff; padding: 3px 5px; border-radius: 4px;
  border: 1.5px solid #fff; box-shadow: 0 1px 3px rgba(0,0,0,.35); white-space: nowrap;
  transform: translate(-50%, -50%); display: inline-block;
}

/* ---------- Tables (admin) ---------- */
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; }
.tbl th, .tbl td { text-align: left; padding: 8px 6px; border-top: 1px solid var(--line); vertical-align: top; }
.tbl th { color: var(--ink-3); font-weight: 500; border-top: 0; }
.checks { display: grid; gap: 6px; margin-top: 4px; }
.checks label { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.checks input { width: 18px; height: 18px; }

/* ---------- Login ---------- */
.login { max-width: 380px; margin: 8vh auto 0; }
.login h1 { font-size: 22px; margin: 0 0 4px; }
.err { color: var(--danger); font-size: 14px; margin-top: 10px; min-height: 20px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabs-h) + 16px + env(safe-area-inset-bottom)); z-index: 2000;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #142233; color: #fff; padding: 10px 16px; border-radius: 999px; font-size: 14px;
  transition: .2s ease; max-width: calc(100% - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--danger); }

.empty { text-align: center; color: var(--ink-3); padding: 40px 16px; }
.spinner { width: 28px; height: 28px; margin: 40px auto; border: 3px solid var(--line); border-top-color: var(--primary); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

@media (min-width: 720px) {
  .view { padding: 24px 16px; }
  .search { top: 56px; }
}
