:root {
  --bg: #0f172a;
  --panel: #ffffff;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --accent: #2563eb;
  --accent-soft: #eff6ff;
  --sidebar-w: 320px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: "Segoe UI", "Noto Sans Thai", system-ui, sans-serif; color: var(--ink); }

#app { display: flex; height: 100vh; }

#sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  z-index: 1000;
}
#sidebar header { padding: 16px 18px 12px; border-bottom: 1px solid var(--line); }
#sidebar h1 { font-size: 17px; margin: 0 0 4px; }
.muted { color: var(--muted); font-size: 12.5px; margin: 0; }

.list { flex: 1; overflow-y: auto; padding: 8px; }
.group { margin-bottom: 6px; }
.group-head { display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding: 6px 8px 4px; }
.group-head .gname { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .02em; }
.group-head .cmp { font-size: 11.5px; color: var(--accent); display: inline-flex; align-items: center; gap: 4px; cursor: pointer; white-space: nowrap; }
.item.older { opacity: .9; }
.item.older .name { font-weight: 500; }
.badge.old { background: #94a3b8; }
.item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.item:hover { border-color: var(--accent); background: var(--accent-soft); }
.item.latest { border-left: 4px solid var(--accent); }
.item .row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.item .name { font-weight: 600; font-size: 14px; }
.item .km { color: var(--accent); font-weight: 600; font-size: 13px; white-space: nowrap; }
.item .sub { color: var(--muted); font-size: 12px; margin-top: 3px; }
.item .badge { display: inline-block; font-size: 10.5px; background: var(--accent); color: #fff; border-radius: 999px; padding: 1px 7px; margin-left: 6px; vertical-align: middle; }
.item label.toggle { display: inline-flex; align-items: center; gap: 5px; font-size: 12px; color: var(--muted); margin-top: 6px; cursor: pointer; }

#sidebar footer { padding: 10px 14px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
#sidebar footer .toolbar { display: flex; gap: 6px; }
#sidebar footer button { flex: 1; padding: 7px 6px; border: 1px solid var(--line); background: #fff; border-radius: 8px;
  font-size: 12px; color: var(--ink); cursor: pointer; }
#sidebar footer button:hover { border-color: var(--accent); color: var(--accent); }

.item .name .sw, .summary .sw { display: inline-block; width: 11px; height: 11px; border-radius: 3px;
  margin-right: 6px; vertical-align: -1px; }
.item .actions { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; }
.item .dl { font-size: 11.5px; color: var(--muted); text-decoration: none; }
.item .dl:hover { color: var(--accent); }

#map { flex: 1; height: 100%; }

#toggleSidebar {
  position: absolute; top: 12px; left: 12px; z-index: 1100;
  display: none; width: 40px; height: 40px; border: none; border-radius: 8px;
  background: var(--panel); box-shadow: 0 1px 6px rgba(0,0,0,.3); font-size: 18px; cursor: pointer;
}

.popup-title { font-weight: 700; margin-bottom: 4px; }
.popup table { border-collapse: collapse; font-size: 12.5px; }
.popup td { padding: 1px 6px 1px 0; vertical-align: top; }
.popup td.k { color: var(--muted); white-space: nowrap; }

.empty { padding: 24px 16px; color: var(--muted); font-size: 13px; line-height: 1.6; }

.legend { background: rgba(255,255,255,.94); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 10px; font-size: 12px; color: var(--ink); box-shadow: 0 1px 4px rgba(0,0,0,.15);
  display: flex; flex-direction: column; gap: 3px; max-width: 240px; max-height: 220px; overflow-y: auto; }
.legend span { display: flex; align-items: center; gap: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.legend i { flex: 0 0 auto; display: inline-block; width: 20px; height: 0; border-top-width: 3px; border-top-style: solid; }
.legend i.solid { border-top-style: solid; }
.legend i.dash { border-top-style: dashed; }

@media (max-width: 720px) {
  #sidebar {
    position: absolute; top: 0; left: 0; height: 100%;
    transform: translateX(-100%); transition: transform .2s;
    box-shadow: 2px 0 12px rgba(0,0,0,.2);
  }
  #app.open #sidebar { transform: translateX(0); }
  #toggleSidebar { display: block; }
}

/* ---- summary table modal ---- */
.modal { position: fixed; inset: 0; z-index: 2000; background: rgba(15,23,42,.45);
  display: flex; align-items: center; justify-content: center; padding: 20px; }
.modal[hidden] { display: none; }
.modal-card { background: #fff; border-radius: 14px; width: min(820px, 100%); max-height: 86vh;
  display: flex; flex-direction: column; overflow: hidden; box-shadow: 0 20px 50px rgba(2,6,23,.3); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.modal-head h2 { font-size: 16px; margin: 0; }
.modal-actions { display: flex; gap: 8px; }
.modal-actions button { padding: 6px 12px; border: 1px solid var(--line); background: #fff; border-radius: 8px; cursor: pointer; font-size: 13px; }
.modal-actions button:hover { border-color: var(--accent); color: var(--accent); }
.modal-actions .close { border: none; font-size: 15px; }
.modal-body { overflow: auto; padding: 4px 0; }
table.summary { width: 100%; border-collapse: collapse; font-size: 13px; }
table.summary th, table.summary td { text-align: left; padding: 8px 14px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.summary th { position: sticky; top: 0; background: #f8fafc; color: var(--muted); font-weight: 600; }
table.summary td.num, table.summary th.num { text-align: right; }
table.summary a { color: var(--accent); text-decoration: none; }

/* ---- print / PDF ---- */
#printTitle { display: none; }
@media print {
  #sidebar, #toggleSidebar, .modal, .leaflet-control-zoom, .leaflet-control-layers { display: none !important; }
  #app { display: block; height: auto; }
  #map { width: 100% !important; height: 88vh !important; }
  #printTitle { display: block; font-size: 16px; font-weight: 700; padding: 8px 4px; }
  .legend { box-shadow: none; }
  @page { margin: 12mm; }
}
