/* theme tokens: theme.js ตั้ง data-theme ที่ <html> เสมอ (light | dark) */
:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e3e6eb;
  --text: #1f2430;
  --muted: #6b7280;
  --accent: #0284c7;       /* โทนฟ้าตามหน้า client (เดิมแดงแบรนด์) */
  --accent-soft: #e0f2fe;
  --danger: #dc2626;       /* แดงเฉพาะสิ่งที่มีความหมาย: ลบ, error, ออกจากระบบ */
  --danger-soft: #fee2e2;
  --online: #16a34a;
  --offline: #9ca3af;
  --focus: #0b6bcb;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 16px rgba(16, 24, 40, .06);
  --nav-h: 56px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115;
  --surface: #171a20;
  --surface-2: #21252d;
  --border: #2a2f38;
  --text: #e6e8ec;
  --muted: #9aa1ad;
  --accent: #38bdf8;
  --accent-soft: #0c2a3d;
  --danger: #f87171;
  --danger-soft: #3a1c1f;
  --online: #34d399;
  --offline: #6b7280;
  --focus: #6cb2ff;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .35);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: "Inter", "Segoe UI", "Noto Sans Thai", "Leelawadee UI", system-ui, sans-serif;
  font-size: 14px;
}
button, input { font: inherit; color: inherit; }
a { color: inherit; }

.app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.layout { display: flex; flex: 1; min-height: 0; position: relative; }

/* ---------- shared ---------- */
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; }
.brand-mark {
  width: 32px; height: 32px;
  display: grid; place-items: center;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
  font-size: 16px;
  flex-shrink: 0;
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text b { font-size: 15px; letter-spacing: .6px; }
.brand-text small { color: var(--muted); font-size: 11px; }

.icon-btn {
  width: 36px; height: 36px;
  display: inline-grid; place-items: center;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
:root[data-theme="dark"] .i-moon, :root:not([data-theme="dark"]) .i-sun { display: none; }

.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--offline); flex-shrink: 0; }
.dot.on { background: var(--online); box-shadow: 0 0 0 3px color-mix(in srgb, var(--online) 25%, transparent); }

/* ---------- topnav ---------- */
.topnav {
  height: var(--nav-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 700;
}
.nav-left, .nav-right { display: flex; align-items: center; gap: 12px; min-width: 0; }
.menu-btn { display: none; }
.nav-links { display: flex; gap: 4px; margin-left: 16px; min-width: 0; overflow-x: auto; scrollbar-width: none; }
.nav-links::-webkit-scrollbar { display: none; }
.nav-links a {
  white-space: nowrap;
  padding: 7px 12px;
  border-radius: 8px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 500;
}
.nav-links a:hover { background: var(--surface-2); color: var(--text); }
.nav-links a.active { background: var(--accent-soft); color: var(--accent); }

.nav-summary {
  display: flex; align-items: center; gap: 6px;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.nav-summary b { color: var(--online); }
.nav-summary .sum-total { color: var(--muted); }
.nav-clock { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }

.user { position: relative; display: flex; align-items: center; padding-left: 12px; border-left: 1px solid var(--border); }
.user-btn {
  display: flex; align-items: center; gap: 8px;
  min-height: 40px; padding: 4px 8px 4px 4px;
  border: 1px solid transparent; border-radius: 999px;
  background: none; cursor: pointer;
}
.user-btn:hover, .user-btn[aria-expanded="true"] { background: var(--surface-2); }
.user-btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.user-btn .chev { color: var(--muted); }
.user-menu {
  position: absolute; top: calc(100% + 8px); right: 0;
  min-width: 220px;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  z-index: 900;
}
.user-menu-head { display: flex; flex-direction: column; gap: 2px; padding: 8px 10px 10px; margin-bottom: 4px; border-bottom: 1px solid var(--border); }
.user-menu-head small { color: var(--muted); font-size: 12px; }
.user-menu button, .user-menu a {
  display: block; width: 100%;
  padding: 9px 10px;
  border: 0; border-radius: 8px;
  background: none;
  text-align: left; text-decoration: none;
  cursor: pointer;
}
.user-menu button:hover, .user-menu a:hover { background: var(--surface-2); }
.user-menu #logout { color: var(--danger); }
.modal .field { margin-bottom: 12px; }
.avatar {
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: 50%;
  background: var(--surface-2);
  font-weight: 700;
  font-size: 13px;
  text-transform: uppercase;
}
.user-name { font-weight: 600; }

/* ---------- sidebar ---------- */
.sidebar {
  width: 320px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  padding-top: 14px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  z-index: 500;
}

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 0 14px 12px; }
.stat {
  display: flex; flex-direction: column; align-items: flex-start;
  padding: 8px 10px;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
}
.stat b { font-size: 18px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.stat[data-filter="online"] b { color: var(--online); }
.stat.active { border-color: var(--accent); background: var(--accent-soft); }
.stat:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.search { padding: 0 14px 10px; }
.search input {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface-2);
  outline: none;
}
.search input:focus { border-color: var(--focus); }

.list { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 0 8px 8px; }
.group-title {
  position: sticky; top: 0; z-index: 1;
  margin: 0;
  padding: 4px 0 2px;
  background: var(--surface);
}
.group-toggle {
  width: 100%;
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px;
  border: 0; border-radius: 8px;
  background: none;
  color: var(--muted);
  font-size: 11px; font-weight: 600; letter-spacing: .4px; text-transform: uppercase;
  cursor: pointer;
  text-align: left;
}
.group-toggle:hover { background: var(--surface-2); color: var(--text); }
.group-toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.group-toggle .chev { flex-shrink: 0; transition: transform .15s ease; }
.group-toggle[aria-expanded="true"] .chev { transform: rotate(90deg); }
.group-name { flex: 1; }
.group-count { font-variant-numeric: tabular-nums; }
.list-tools { display: flex; justify-content: flex-end; padding: 0 14px 4px; }
.link-btn { padding: 2px 4px; border: 0; background: none; color: var(--muted); font-size: 12px; cursor: pointer; border-radius: 6px; }
.link-btn:hover { color: var(--text); }
.link-btn:focus-visible { outline: 2px solid var(--focus); }
@media (prefers-reduced-motion: reduce) { .group-toggle .chev { transition: none; } }
.item {
  width: 100%;
  display: grid;
  grid-template-columns: 10px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 9px 8px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  cursor: pointer;
  text-align: left;
}
.item > span { min-width: 0; }
.item:hover { background: var(--surface-2); }
.item.selected { background: var(--accent-soft); }
.item:focus-visible { outline: 2px solid var(--focus); outline-offset: -2px; }
.item .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item .prov { color: var(--muted); font-size: 12px; }
.item .lat { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.empty { padding: 24px 8px; color: var(--muted); text-align: center; }

.side-foot { padding: 10px 14px; border-top: 1px solid var(--border); color: var(--muted); font-size: 12px; }

/* ---------- map ---------- */
.map-wrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; background: var(--bg); }
:root[data-theme="dark"] .base-tiles { filter: invert(1) hue-rotate(180deg) brightness(.85) contrast(.9) saturate(.5); }

.pin-wrap { display: grid; place-items: center; background: none; border: 0; }
.pin {
  display: block;
  width: 14px; height: 14px;
  border-radius: 50%;
  background: var(--offline);
  border: 2px solid var(--surface);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .35);
  transition: width .15s, height .15s;
}
.pin.on { background: var(--online); }
/* alert จากสาขา (ส้ม, ยังไม่รับทราบ = กระพริบ) */
:root { --alert: #f97316; }
:root[data-theme="dark"] { --alert: #fb923c; }
@keyframes alert-ring { 0%, 100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--alert) 70%, transparent); } 50% { box-shadow: 0 0 0 7px color-mix(in srgb, var(--alert) 0%, transparent); } }
@keyframes alert-blink { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
.pin.alert { outline: 3px solid var(--alert); outline-offset: 1px; }
.pin.alert-new { animation: alert-ring 1.2s ease-out infinite; }
.alert-count {
  display: inline-block; min-width: 18px; margin-right: 6px; padding: 0 5px;
  border-radius: 999px; background: var(--alert); color: #fff;
  font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
}
.alert-count.alert-new { animation: alert-blink 1s step-end infinite; }
.pop .state-alert { color: var(--alert); font-weight: 700; }
@media (prefers-reduced-motion: reduce) { .pin.alert-new, .alert-count.alert-new { animation: none; } }
.pin.selected { width: 20px; height: 20px; border: 3px solid var(--accent); }
.pin-wrap:hover .pin:not(.selected) { width: 17px; height: 17px; }

.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}
.leaflet-popup-content-wrapper { border-radius: 12px; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }
.leaflet-container { font: inherit; }
.leaflet-tooltip { background: var(--surface); color: var(--text); border-color: var(--border); box-shadow: var(--shadow); }
.leaflet-tooltip-top:before { border-top-color: var(--surface); }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 80%, transparent) !important; color: var(--muted); }
.leaflet-control-attribution a { color: var(--muted); }
.leaflet-bar { border-color: var(--border) !important; }
.leaflet-bar a { background: var(--surface); color: var(--text); border-color: var(--border); }
.leaflet-bar a:hover { background: var(--surface-2); }

.pop h3 { margin: 0 0 2px; font-size: 15px; }
.pop .prov { color: var(--muted); font-size: 12px; margin-bottom: 8px; }
.pop dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0 0 12px; font-size: 12px; }
.pop dt { color: var(--muted); }
.pop dd { margin: 0; font-variant-numeric: tabular-nums; }
.pop .state-on { color: var(--online); font-weight: 600; }
.pop .state-off { color: var(--muted); font-weight: 600; }
.pop .open {
  display: block;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff !important;
  text-align: center;
  text-decoration: none;
  font-weight: 600;
}
.pop .open.disabled { background: var(--surface-2); color: var(--muted) !important; pointer-events: none; }

/* ---------- modal ---------- */
.modal {
  width: min(380px, calc(100vw - 32px));
  padding: 24px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 50px rgba(0, 0, 0, .25);
}
.modal::backdrop { background: rgba(0, 0, 0, .45); }
.modal[open] { animation: modal-in .16s ease-out; }
@keyframes modal-in { from { opacity: 0; transform: translateY(8px) scale(.98); } }
.modal-icon {
  width: 44px; height: 44px;
  display: grid; place-items: center;
  margin-bottom: 14px;
  border-radius: 12px;
  background: var(--danger-soft);
  color: var(--danger);
}
.modal h2 { margin: 0 0 6px; font-size: 17px; }
.modal p { margin: 0 0 20px; color: var(--muted); line-height: 1.5; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn {
  min-height: 40px;
  padding: 8px 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 600;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.btn-danger { border-color: transparent; background: var(--danger); color: #fff; }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 88%, #000); }
@media (max-width: 420px) {
  .modal-actions .btn { flex: 1; }
}
@media (prefers-reduced-motion: reduce) {
  .modal[open] { animation: none; }
}

/* ---------- branches page ---------- */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: .92em; }

.page { flex: 1; overflow-y: auto; padding: 24px max(16px, env(safe-area-inset-right)) 32px max(16px, env(safe-area-inset-left)); }
.page > * { max-width: 1200px; margin-left: auto; margin-right: auto; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.page-head h1 { margin: 0 0 4px; font-size: 22px; }
/* หัวข้อย่อยในหน้า: ต้องคง margin auto ของ .page > * ไว้ ไม่งั้นหลุดไปชิดซ้ายบนจอกว้าง */
.section-title { margin-top: 24px; margin-bottom: 8px; font-size: 16px; }
.page-head p { margin: 0; color: var(--muted); }
.page-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.page-actions input {
  width: 220px; min-height: 40px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); outline: none;
}
.page-actions input:focus { border-color: var(--focus); }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; overflow: hidden; }
.table { width: 100%; border-collapse: collapse; }
.table th {
  padding: 10px 14px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 11px; font-weight: 600; letter-spacing: .4px; text-transform: uppercase;
  text-align: left;
}
.table td { padding: 12px 14px; border-top: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:hover td { background: color-mix(in srgb, var(--surface-2) 50%, transparent); }
.table .empty { padding: 40px 14px; }
.cell-main { font-weight: 600; }
.cell-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.col-actions { width: 1%; white-space: nowrap; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.status { display: inline-flex; align-items: center; gap: 8px; color: var(--muted); font-weight: 500; }
.status.on { color: var(--online); }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; text-decoration: none; color: var(--text); }
.btn:disabled { opacity: .6; cursor: default; }
.btn-sm { min-height: 32px; padding: 5px 12px; font-size: 13px; border-radius: 8px; }
.btn-primary { border-color: transparent; background: var(--accent); color: #fff; }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn-ghost-danger { color: var(--danger); }
.btn-ghost-danger:hover { background: var(--danger-soft); }
.btn.disabled { opacity: .45; pointer-events: none; }

/* form dialog */
.modal-wide { width: min(720px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow-y: auto; }
.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.modal-head h2 { margin: 0; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.span-2 { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field > span { font-size: 12px; font-weight: 600; color: var(--muted); }
.hint-inline { font-weight: 400; }
.field input, .field select {
  width: 100%; min-height: 40px; padding: 8px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); outline: none;
}
.field input:focus, .field select:focus { border-color: var(--focus); }
.field input[readonly] { opacity: .7; }
.field [aria-invalid="true"] { border-color: var(--danger); }
.hint { color: var(--muted); font-size: 12px; }
.err { color: var(--danger); font-size: 12px; }
.err:empty { display: none; }
.pick-map { height: 240px; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.coords { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.form-error { margin-top: 14px; padding: 10px 12px; border-radius: 10px; background: var(--danger-soft); color: var(--danger); }
.modal-wide .modal-actions { margin-top: 20px; }
.modal-wide > p.muted { margin: 0 0 16px; line-height: 1.5; }

.cmd { margin-bottom: 12px; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.cmd-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 6px 6px 12px; background: var(--surface-2); font-size: 12px; color: var(--muted); }
.cmd pre { margin: 0; padding: 12px; white-space: pre-wrap; word-break: break-all; font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 12px; }
.steps { margin: 16px 0 0; padding-left: 20px; line-height: 1.7; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 16px; margin: 0 0 14px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }

/* หัวตารางที่กดเรียงได้ (หน้า Branches) */
.th-sort {
  display: inline-flex; align-items: center; gap: 4px; padding: 0; border: 0; background: none;
  font: inherit; letter-spacing: inherit; text-transform: inherit; color: inherit; cursor: pointer;
}
.th-sort:hover, th[aria-sort="ascending"] .th-sort, th[aria-sort="descending"] .th-sort { color: var(--text); }
.th-sort:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.sort-ind { width: 0; height: 0; border-left: 4px solid transparent; border-right: 4px solid transparent; opacity: 0; }
th[aria-sort="none"] .th-sort:hover .sort-ind { opacity: .35; border-top: 5px solid currentColor; }
th[aria-sort="ascending"] .sort-ind { opacity: 1; border-top: 0; border-bottom: 5px solid currentColor; }
th[aria-sort="descending"] .sort-ind { opacity: 1; border-bottom: 0; border-top: 5px solid currentColor; }

/* คำขอเข้า VPN ที่รออนุมัติ (หน้า Branches) */
.join-pending { margin-bottom: 20px; }
.join-pending .section-title { margin-top: 0; }
.join-pending > p { margin: 0 0 10px; font-size: 13px; }
.join-req {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap; padding: 12px 14px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--accent); border-radius: 12px; box-shadow: 0 0 0 3px var(--accent-soft);
}
.join-code { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; font-size: 20px; font-weight: 700; letter-spacing: .08em; color: var(--accent); }
.join-info { flex: 1 1 180px; min-width: 0; }
.join-info .cell-main { overflow-wrap: anywhere; }
.join-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.join-actions .page-select { min-height: 32px; padding: 5px 10px; font-size: 13px; max-width: 260px; }
details.alt { margin-top: 12px; }
details.alt summary { cursor: pointer; color: var(--muted); font-size: 13px; }
details.alt[open] summary { margin-bottom: 8px; }
details.alt p { margin: 0 0 10px; font-size: 13px; }
@media (max-width: 560px) {
  .join-actions { width: 100%; }
  .join-actions .page-select { flex: 1 1 100%; max-width: none; }
}

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  padding: 10px 16px;
  border-radius: 10px;
  background: var(--text); color: var(--surface);
  box-shadow: var(--shadow);
  opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 2000;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast[data-kind="error"] { background: var(--danger); color: #fff; }

/* ---------- users page ---------- */
.tag {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 11px; font-weight: 600;
  vertical-align: middle;
}
.tag-accent, .tag-admin { background: var(--accent-soft); color: var(--accent); }
.tag-superuser { background: color-mix(in srgb, var(--focus) 22%, transparent); color: var(--focus); }
.tag-user { background: color-mix(in srgb, var(--online) 16%, transparent); color: var(--online); }
.tag-muted { color: var(--muted); }
.table tr.is-disabled td { opacity: .6; }
fieldset.field { border: 0; padding: 0; margin: 0; }
fieldset.field legend { padding: 0; margin-bottom: 6px; font-size: 12px; font-weight: 600; color: var(--muted); }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg label {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 38px; padding: 6px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  cursor: pointer;
}
.seg input { width: auto; min-height: 0; accent-color: var(--accent); }
.seg label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.branch-pick { margin-top: 10px; }
.branch-pick-tools { display: flex; gap: 6px; margin-bottom: 8px; }
.branch-pick-tools input {
  flex: 1; min-width: 0; min-height: 32px; padding: 5px 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface-2); outline: none;
}
.branch-checks {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 2px 12px;
  max-height: 220px; overflow-y: auto;
  padding: 8px; border: 1px solid var(--border); border-radius: 10px;
}
.check { display: flex; align-items: center; gap: 8px; min-height: 32px; cursor: pointer; }
.check input { width: 16px; height: 16px; min-height: 0; accent-color: var(--accent); flex-shrink: 0; }

/* ---------- alerts ---------- */
.nav-alert-count { display: inline-block; min-width: 18px; margin-left: 4px; padding: 0 5px; border-radius: 999px; background: var(--alert); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center; }
.nav-alert-count.alert-new { animation: alert-blink 1s step-end infinite; }
.tag-alert { background: color-mix(in srgb, var(--alert) 16%, transparent); color: var(--alert); }
.table tr.alert-row-new td:first-child { box-shadow: inset 3px 0 0 var(--alert); }
.page-select { min-height: 40px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }

/* ---------- login ---------- */
.login-page { position: relative; display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.login-theme { position: absolute; top: 16px; right: 16px; }
.login-card {
  width: 100%; max-width: 360px;
  padding: 28px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
}
.login-card .brand { margin-bottom: 20px; }
.login-card label { display: block; margin: 12px 0 4px; font-size: 12px; color: var(--muted); }
.login-card input {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface-2); outline: none;
}
.login-card input:focus { border-color: var(--focus); }
.login-card button[type="submit"] {
  width: 100%; margin-top: 20px; padding: 10px;
  border: 0; border-radius: 10px;
  background: var(--accent); color: #fff; font-weight: 600; cursor: pointer;
}
.login-err { margin-top: 12px; color: var(--danger); font-size: 13px; }

/* ---------- responsive ---------- */
.side-head, .backdrop { display: none; }

/* แท็บเล็ต / แล็ปท็อปจอเล็ก */
@media (max-width: 1100px) {
  .nav-clock { display: none; }
  .sidebar { width: 280px; }
}
@media (max-width: 900px) {
  .user-name { display: none; }
  .nav-links { margin-left: 4px; }
}

/* มือถือ: sidebar เป็น drawer ทับแผนที่ */
@media (max-width: 760px) {
  :root { --nav-h: 52px; }
  .topnav { padding: 0 max(12px, env(safe-area-inset-right)) 0 max(12px, env(safe-area-inset-left)); gap: 8px; }
  .nav-left, .nav-right { gap: 8px; }
  .menu-btn { display: inline-grid; }
  .brand-text, .sum-total, .user-btn .chev { display: none; }
  .user-btn { padding: 4px; }
  .nav-links { margin-left: 0; gap: 2px; }
  .nav-links a { padding: 7px 9px; }
  .user { padding-left: 8px; }
  .icon-btn { width: 40px; height: 40px; }

  .sidebar {
    position: absolute; inset: 0 auto 0 0;
    width: min(340px, 86vw);
    padding-top: 0;
    padding-bottom: env(safe-area-inset-bottom);
    transform: translateX(-100%);
    visibility: hidden;
    transition: transform .22s ease, visibility 0s linear .22s;
    box-shadow: var(--shadow);
    z-index: 800;
  }
  .sidebar.open { transform: none; visibility: visible; transition: transform .22s ease; }
  .side-head {
    display: flex; align-items: center; justify-content: space-between;
    padding: 10px 10px 10px 14px;
    font-size: 15px;
  }
  .backdrop:not([hidden]) {
    display: block;
    position: absolute; inset: 0;
    background: rgba(0, 0, 0, .4);
    z-index: 750;
  }
  .item { padding: 11px 8px; }       /* touch target */
  .search input { padding: 11px 12px; font-size: 16px; } /* 16px กัน iOS zoom */

  .leaflet-control-zoom { display: none; } /* ใช้ pinch zoom */
}

/* ตาราง -> การ์ด บนจอเล็ก */
@media (max-width: 760px) {
  .page { padding-top: 16px; }
  .page-actions, .page-actions input { width: 100%; }
  .page-actions input { flex: 1; width: auto; }
  .card { background: transparent; border: 0; overflow: visible; }
  .table thead { display: none; }
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table tr { margin-bottom: 10px; padding: 12px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; }
  .table td { padding: 4px 0; border: 0; }
  .table tbody tr:hover td { background: transparent; }
  .table td[data-label]::before { content: attr(data-label); display: block; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .4px; }
  .table td[data-label="Status"]::before { display: none; }
  .col-actions { width: auto; }
  .row-actions { justify-content: flex-start; flex-wrap: wrap; margin-top: 8px; }
  .row-actions .btn { flex: 1; min-height: 38px; }
  .form-grid { grid-template-columns: 1fr; }
  .modal-wide { padding: 18px; }
  .pick-map { height: 200px; }
}

@media (max-width: 420px) {
  .nav-summary, .topnav .brand { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .sidebar, .sidebar.open { transition: none; }
}

/* ลิงก์ดาวน์โหลดในหน้า Backups */
.file-link { padding: 0; border: 0; background: none; font: inherit; color: var(--accent); cursor: pointer; text-decoration: none; }
.file-link:hover { text-decoration: underline; }
.file-link:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.file-link code { color: inherit; }
