:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --text: #1a2233;
  --muted: #5b6577;
  --border: #d8dee8;
  --accent: #0b2a6f;
  --accent-text: #ffffff;
  --hq: #c62828;
  --pt: #0b57d0;
  --warn-bg: #fff4e0;
  --warn-text: #7a4a00;
  --err-bg: #fde8e8;
  --err-text: #8a1c1c;
  --ok-bg: #e6f4ea;
  --ok-text: #1e5b2f;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #10141c; --surface: #19202b; --text: #e6e9ef; --muted: #9aa4b5;
    --border: #2c3544; --accent: #6f9cff; --accent-text: #0b1220;
    --warn-bg: #3a2c10; --warn-text: #ffd48a; --err-bg: #3d1717; --err-text: #ffb3b3;
    --ok-bg: #133020; --ok-text: #a6e3b8;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font-family: "Sarabun", system-ui, -apple-system, "Segoe UI", sans-serif;
  font-size: 15px; line-height: 1.5;
  display: flex; flex-direction: column;
}
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; background: var(--accent); color: var(--accent-text);
}
.topbar h1 { margin: 0; font-size: 1.25rem; }
.topbar .sub { margin: 2px 0 0; opacity: .85; font-size: .9rem; }
.badge {
  font-size: .8rem; padding: 3px 10px; border-radius: 999px;
  border: 1px solid currentColor; white-space: nowrap;
}

.layout { flex: 1; display: grid; grid-template-columns: minmax(340px, 460px) minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); min-height: 0; }
.panel { min-height: 0; padding: 16px; overflow-y: auto; border-right: 1px solid var(--border); background: var(--surface); }
.panel h2 { font-size: 1.05rem; margin: 4px 0 8px; }
.panel h2:not(:first-child) { margin-top: 20px; }
.mapcol { position: relative; min-height: 420px; }
#map { position: absolute; inset: 0; }

.hint, .note { color: var(--muted); font-size: .85rem; margin: 0 0 8px; }
code { background: var(--bg); padding: 1px 4px; border-radius: 4px; }
textarea, input {
  width: 100%; font: inherit; color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 6px; padding: 8px;
}
textarea { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .85rem; resize: vertical; }

.row { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
button, .button, .filebtn {
  font: inherit; font-size: .9rem; cursor: pointer; text-decoration: none;
  padding: 6px 12px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); display: inline-flex; align-items: center;
}
button:hover, .button:hover, .filebtn:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
.button[aria-disabled="true"] { opacity: .45; pointer-events: none; }
.filebtn input { display: none; }

.routebar { margin: 4px 0 10px; }
.routelabel { display: block; font-size: .8rem; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; justify-content: center; padding: 8px 6px; border-right: 1px solid var(--border); background: var(--surface); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { color: #fff; font-weight: 600; }
.seg .m-star[aria-pressed="true"] { background: #0b57d0; }
.seg .m-loop[aria-pressed="true"] { background: #7b1fa2; }
.seg .m-line[aria-pressed="true"] { background: #5b6577; }
.hint.privacy { font-size: .75rem; margin: 6px 0 0; }
.loopopts { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 8px; }
.loopopts[hidden], .loopopts [hidden] { display: none; }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: .88rem; cursor: pointer; }
.check input { width: 18px; height: 18px; margin: 0; accent-color: #7b1fa2; }
td .sub.visit { color: #7b1fa2; font-weight: 600; }
.cluster {
  width: 36px; height: 36px; border-radius: 50%; display: flex; align-items: center; justify-content: center;
  background: var(--pt); color: #fff; font: 700 13px/1 system-ui, sans-serif;
  border: 3px solid rgba(255,255,255,.9); box-shadow: 0 0 0 4px rgba(11,87,208,.3), 0 1px 4px rgba(0,0,0,.35);
}
.routestatus { font-size: .85rem; margin-top: 6px; min-height: 1.2em; color: var(--muted); }
.routestatus.busy::before { content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 6px; border: 2px solid currentColor; border-right-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; vertical-align: -1px; }
.routestatus.ok { color: var(--ok-text); }
.routestatus.warn { color: var(--warn-text); }
.routestatus.err { color: var(--err-text); }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .routestatus.busy::before { animation: none; } }
.messages { display: flex; flex-direction: column; gap: 4px; }
.msg { font-size: .85rem; padding: 6px 8px; border-radius: 6px; }
.msg.warn { background: var(--warn-bg); color: var(--warn-text); }
.msg.err { background: var(--err-bg); color: var(--err-text); }
.msg.ok { background: var(--ok-bg); color: var(--ok-text); }

.tablewrap { border: 1px solid var(--border); border-radius: 6px; }
table { width: 100%; border-collapse: collapse; table-layout: fixed; font-size: .88rem; }
col.w-no { width: 34px; }
col.w-dist { width: 84px; }
col.w-act { width: 72px; }
th, td { padding: 8px 6px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
th { background: var(--bg); font-weight: 600; font-size: .8rem; color: var(--muted); }
tbody tr:last-child td { border-bottom: 0; }
td.c-name { overflow-wrap: anywhere; }
td.c-name b, td.c-dist b { display: block; }
td .sub { display: block; font-size: .75rem; color: var(--muted); line-height: 1.35; }
td.c-dist { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
td.c-act { text-align: right; }
.no { display: inline-flex; width: 22px; height: 22px; border-radius: 50%; align-items: center; justify-content: center; background: var(--pt); color: #fff; font-size: .75rem; font-weight: 700; }
tr.hq td { color: var(--hq); }
tr.hq .c-no { font-weight: 700; text-align: center; }
tbody tr[data-i] { cursor: pointer; }
tbody tr[data-i]:hover { background: var(--bg); }
tr.sum td { background: var(--bg); }
.navbtn { display: inline-flex; align-items: center; justify-content: center; min-height: 34px; padding: 4px 10px; border-radius: 6px; background: var(--accent); color: var(--accent-text); text-decoration: none; font-weight: 600; font-size: .82rem; }
.navbtn:hover { filter: brightness(1.1); }
.coord { font-family: ui-monospace, Menlo, Consolas, monospace; font-size: .72rem; }
.empty { color: var(--muted); text-align: center; margin: 12px 0; }

.settings { margin-top: 16px; border-top: 1px solid var(--border); padding-top: 10px; }
.settings summary { cursor: pointer; font-weight: 600; }
.grid3 { display: grid; grid-template-columns: 1.3fr 1fr 1fr; gap: 8px; margin-top: 8px; }
.grid3 label { font-size: .8rem; color: var(--muted); }

.foot { padding: 6px 16px; font-size: .75rem; color: var(--muted); border-top: 1px solid var(--border); background: var(--surface); }

/* หมุดตัวเลขบน Leaflet */
.pin {
  width: 28px; height: 28px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: var(--pt); border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4);
  display: flex; align-items: center; justify-content: center;
}
.pin span { transform: rotate(45deg); color: #fff; font: 700 12px/1 system-ui, sans-serif; }
.pin.hq { background: var(--hq); width: 34px; height: 34px; }
.pp { font-family: "Sarabun", system-ui, sans-serif; font-size: 14px; color: #1a2233; }
.pp-title { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.pp-road { margin-bottom: 2px; }
.pp-road b { font-size: 16px; color: #0b2a6f; }
.pp-road .pp-sub { display: inline; margin-left: 4px; }
.pp-sub { color: #5b6577; font-size: 12px; }
.pp-actions { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.pp-btn { display: block; text-align: center; padding: 8px 10px; border-radius: 6px; border: 1px solid #c9d1de; color: #0b2a6f !important; text-decoration: none; font-weight: 600; }
.pp-btn.primary { background: #0b2a6f; border-color: #0b2a6f; color: #fff !important; }
.pp-btn.ghost { border-color: transparent; font-weight: 400; text-decoration: underline; padding: 4px; }
.leaflet-popup-content { margin: 12px 14px; }
.leaflet-tooltip.dist { font-size: 11px; padding: 1px 5px; }

@media (max-width: 820px) {
  html, body { height: auto; }
  .topbar { padding: 10px 16px; }
  .topbar h1 { font-size: 1.05rem; }
  .topbar .sub { display: none; }
  .layout { flex: none; display: flex; flex-direction: column; }
  /* มือถือ: แผนที่อยู่บนสุด ตามด้วยตารางผลลัพธ์ แล้วจึงช่องกรอก */
  .mapcol { order: -1; height: 60vh; min-height: 320px; }
  .panel { display: flex; flex-direction: column; }
  .sec-results { order: -1; }
  .panel { overflow: visible; border-right: 0; padding: 12px 16px; }
  .grid3 { grid-template-columns: 1fr; }
  button, .button, .filebtn { min-height: 40px; }
  .seg button { min-height: 42px; }
}
