:root {
  --navy: #0f172a;
  --teal: #0f766e;
  --blue: #2563eb;
  --green: #16a34a;
  --amber: #f59e0b;
  --red: #b91c1c;
  --line: #dbe5f1;
  --muted: #64748b;
  --bg: #eef2f7;
  --side-w: 320px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Sarabun", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: #1e293b;
  line-height: 1.5;
}

/* ---------- แถบบน ---------- */
.topbar { background: var(--navy); color: #fff; padding: 12px 0; }
.topbar .inner {
  max-width: 1600px; margin: 0 auto; padding: 0 20px;
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
}
.brand { font-weight: 800; font-size: 1.1rem; color: #fff; text-decoration: none; letter-spacing: .4px; }
.brand span { color: #5eead4; font-weight: 700; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.clock { font-size: .85rem; color: rgba(255,255,255,.72); font-weight: 700; }
.who { display: flex; flex-direction: column; line-height: 1.2; text-align: right; }
.who b { font-size: .86rem; color: #fff; }
.who small { font-size: .72rem; color: #5eead4; font-weight: 700; }
.menu-toggle {
  display: none; background: rgba(255,255,255,.14); color: #fff; border: 0;
  font-size: 1.1rem; padding: 7px 13px; border-radius: 9px; cursor: pointer;
}

/* ---------- หน้าเข้าสู่ระบบ ---------- */
.login-body {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, var(--navy), var(--teal));
  margin: 0; padding: 20px;
}
.login-wrap { width: 100%; max-width: 400px; }
.login-card {
  background: #fff; border-radius: 20px; padding: 32px 30px;
  box-shadow: 0 24px 60px rgba(0,0,0,.28);
}
.login-brand { font-size: 1.4rem; font-weight: 800; color: var(--navy); letter-spacing: .3px; }
.login-brand span { color: var(--teal); }
.login-sub { color: var(--muted); font-size: .88rem; margin: 4px 0 22px; }
.login-foot { color: var(--muted); font-size: .78rem; text-align: center; margin: 20px 0 0; line-height: 1.6; }

/* ---------- โครงหน้า ---------- */
.shell {
  max-width: 1600px; margin: 0 auto; padding: 20px;
  display: grid; grid-template-columns: var(--side-w) minmax(0,1fr); gap: 18px; align-items: start;
}
.shell-main { min-width: 0; }
.shell-side { position: sticky; top: 18px; display: grid; gap: 12px; }

.side-card {
  background: #fff; border: 1px solid var(--line); border-radius: 16px;
  padding: 14px; box-shadow: 0 8px 22px rgba(15,23,42,.05);
}
.side-title {
  font-size: .72rem; font-weight: 800; letter-spacing: 1.5px;
  color: var(--muted); text-transform: uppercase; margin-bottom: 10px;
}
.side-menu { display: grid; gap: 4px; }
.side-link {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px 11px;
  border-radius: 12px; text-decoration: none; color: #1e293b; border: 1px solid transparent;
}
.side-link:hover { background: #f8fafc; border-color: var(--line); }
.side-link.active { background: var(--navy); border-color: var(--navy); }
.side-link.active b { color: #fff; }
.side-link.active small { color: rgba(255,255,255,.68); }
.side-ico { font-size: 1.1rem; line-height: 1.2; }
.side-text b { display: block; font-size: .9rem; font-weight: 700; }
.side-text small { display: block; color: var(--muted); font-size: .76rem; }

.side-stat { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid #f1f5f9; font-size: .86rem; }
.side-stat:last-child { border-bottom: 0; }
.side-stat b { font-weight: 800; }

/* ---------- รายการทะเบียนรถแยกตาม HUB ---------- */
.hub-total {
  background: var(--navy); color: #fff; font-size: .68rem; font-weight: 800;
  padding: 2px 8px; border-radius: 999px; letter-spacing: 0;
}
.hub-search {
  width: 100%; padding: 7px 10px; font-size: .84rem; border: 1px solid #cbd5e1;
  border-radius: 9px; margin-bottom: 7px;
}
.hub-actions { display: flex; gap: 6px; margin-bottom: 8px; }
.hub-mini {
  flex: 1; background: #f1f5f9; border: 1px solid var(--line); color: #475569;
  font: 700 .74rem "Sarabun", sans-serif; padding: 5px 6px; border-radius: 8px; cursor: pointer;
}
.hub-mini:hover { background: #e2e8f0; }

.hub-list { max-height: 520px; overflow-y: auto; margin: 0 -4px; padding: 0 4px; }
.hub-hint { color: var(--muted); font-size: .75rem; margin: 9px 0 0; text-align: center; }

/* ---------- แผนที่คู่กับรายการทะเบียน ---------- */
.map-layout {
  display: grid; grid-template-columns: minmax(0,1fr) 290px; gap: 14px;
  align-items: stretch; margin-bottom: 16px;
}
/* กล่องรายการข้างแผนที่ — สูงเท่าแผนที่ แล้วเลื่อนดูภายในกล่อง */
.hub-beside { margin: 0; display: flex; flex-direction: column; max-height: calc(100vh - 210px); }
.hub-beside .hub-list { max-height: none; flex: 1; min-height: 220px; }
.hub-loading, .hub-empty { color: var(--muted); font-size: .84rem; text-align: center; padding: 14px 0; }

.hub-group { margin-bottom: 5px; }
.hub-head {
  width: 100%; display: flex; align-items: center; gap: 7px; cursor: pointer;
  background: #f8fafc; border: 1px solid var(--line); border-radius: 9px;
  padding: 7px 9px; font-family: inherit; text-align: left;
}
.hub-head:hover { background: #eef2f7; }
.hub-head .caret { color: var(--muted); font-size: .7rem; transition: transform .15s; }
.hub-group.open .hub-head .caret { transform: rotate(90deg); }
.hub-head .name { flex: 1; font-weight: 800; font-size: .82rem; color: #0f172a; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hub-head .cnt { font-size: .72rem; font-weight: 800; color: var(--muted); white-space: nowrap; }
.hub-head .run {
  font-size: .68rem; font-weight: 800; color: #fff; background: var(--green);
  padding: 1px 6px; border-radius: 999px; white-space: nowrap;
}

.hub-items { display: none; padding: 4px 0 6px 6px; }
.hub-group.open .hub-items { display: block; }

.hub-item {
  width: 100%; display: flex; align-items: center; gap: 7px; cursor: pointer;
  background: none; border: 0; border-radius: 7px; padding: 5px 7px;
  font-family: inherit; text-align: left;
}
.hub-item:hover { background: #eff6ff; }
.hub-item .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.hub-item .plate {
  flex: 1; font-size: .8rem; font-weight: 700; color: #0f172a; background: none;
  padding: 0; border-radius: 0; letter-spacing: 0; min-width: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.hub-item .spd { font-size: .72rem; font-weight: 800; color: var(--muted); white-space: nowrap; }
.hub-item .spd.on { color: var(--green); }
.hub-item .spd.over { color: var(--red); }

/* ---------- การ์ด ---------- */
.card {
  background: #fff; border: 1px solid var(--line); border-radius: 16px;
  padding: 18px; box-shadow: 0 8px 22px rgba(15,23,42,.05); margin-bottom: 16px;
}
.card h2 { margin: 0 0 3px; font-size: 1.1rem; color: var(--navy); }
.card .hint { color: var(--muted); font-size: .86rem; margin: 0 0 14px; }
.card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.card-head h2 { margin: 0; }

/* ---------- ฮีโร่ ---------- */
.hero {
  background: linear-gradient(135deg, var(--navy), var(--teal));
  color: #fff; border-radius: 18px; padding: 20px 24px; margin-bottom: 16px;
}
.hero .eyebrow { font-size: .72rem; font-weight: 800; letter-spacing: 2px; color: rgba(255,255,255,.65); }
.hero h1 { margin: 5px 0 0; font-size: 1.55rem; }
.hero p { margin: 5px 0 0; color: rgba(255,255,255,.8); font-size: .93rem; }

/* ---------- KPI ---------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px,1fr)); gap: 11px; margin-bottom: 16px; }
.kpi {
  background: #fff; border: 1px solid var(--line); border-radius: 14px; padding: 14px;
  border-left: 5px solid var(--muted); text-decoration: none; color: inherit; display: block;
}
.kpi b { display: block; font-size: 1.5rem; color: var(--navy); line-height: 1.2; }
.kpi span { display: block; color: var(--muted); font-size: .78rem; font-weight: 800; margin-top: 2px; }
.kpi.moving  { border-left-color: var(--green); }
.kpi.idling  { border-left-color: var(--amber); }
.kpi.parked  { border-left-color: var(--blue); }
.kpi.offline { border-left-color: #94a3b8; }
.kpi.alert   { border-left-color: var(--red); }
.kpi.total   { border-left-color: var(--navy); }
a.kpi:hover { box-shadow: 0 10px 24px rgba(15,23,42,.12); transform: translateY(-1px); transition: .15s; }

/* ---------- ฟอร์ม ---------- */
label { display: block; font-weight: 800; font-size: .78rem; color: var(--muted); margin-bottom: 5px; }
input[type=text], input[type=date], input[type=time], input[type=search], select {
  width: 100%; padding: 10px 13px; border: 1px solid #cbd5e1; border-radius: 10px;
  background: #fff; font-size: .95rem; font-family: inherit; color: #0f172a;
}
input:focus, select:focus { outline: 2px solid rgba(37,99,235,.3); border-color: var(--blue); }
.field { margin-bottom: 12px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px,1fr)); gap: 11px; align-items: end; }

.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 0; cursor: pointer;
  background: var(--navy); color: #fff; font-weight: 800; font-size: .9rem;
  padding: 10px 17px; border-radius: 10px; text-decoration: none; font-family: inherit; white-space: nowrap;
}
.btn:hover { background: #1e293b; }
.btn:disabled { background: #94a3b8; cursor: not-allowed; }
.btn.primary { background: var(--blue); }
.btn.primary:hover { background: #1d4ed8; }
.btn.teal { background: var(--teal); }
.btn.light { background: #fff; color: var(--navy); border: 1px solid var(--line); }
.btn.light:hover { background: #f8fafc; }
.btn.sm { padding: 6px 12px; font-size: .82rem; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

/* ---------- combo ค้นหารถ ---------- */
.combo { position: relative; }
.combo-box { display: flex; }
.combo-box input { border-radius: 10px 0 0 10px; }
.combo-toggle {
  border: 1px solid #cbd5e1; border-left: 0; background: #f8fafc; cursor: pointer;
  border-radius: 0 10px 10px 0; padding: 0 13px; color: var(--muted);
}
.combo-menu {
  position: absolute; z-index: 1200; top: calc(100% + 5px); left: 0; right: 0;
  background: #fff; border: 1px solid var(--line); border-radius: 12px;
  box-shadow: 0 16px 36px rgba(15,23,42,.18); max-height: 320px; overflow: auto; padding: 5px;
}
.combo-menu[hidden] { display: none; }
.combo-option {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 9px 11px; border-radius: 9px; cursor: pointer; font-family: inherit;
}
.combo-option:hover, .combo-option.is-active { background: #eff6ff; }
.combo-option b { display: block; font-size: .9rem; color: #0f172a; }
.combo-option small { display: block; color: var(--muted); font-size: .76rem; }
.combo-empty { padding: 12px; text-align: center; color: var(--muted); font-size: .86rem; }

/* ---------- แจ้งเตือน ---------- */
.notice { border-radius: 12px; padding: 13px 16px; margin-bottom: 14px; font-size: .9rem; }
.notice-title { display: block; font-weight: 800; margin-bottom: 2px; }
.notice.danger { background: #fef2f2; color: #991b1b; border-left: 5px solid #ef4444; }
.notice.success { background: #f0fdf4; color: #166534; border-left: 5px solid #22c55e; }
.notice.info { background: #eff6ff; color: #1e40af; border-left: 5px solid var(--blue); }
.notice.warn { background: #fffbeb; color: #b45309; border-left: 5px solid var(--amber); }

/* ---------- ตาราง ---------- */
.table-wrap { overflow: auto; border: 1px solid var(--line); border-radius: 12px; background: #fff; }
.table-wrap.tall { max-height: 640px; }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th {
  background: #f8fafc; color: #475569; text-align: left; font-size: .76rem; font-weight: 800;
  padding: 10px; border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; white-space: nowrap;
}
td { padding: 9px 10px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: #f8fafc; }
.center { text-align: center; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* ---------- ชิป/ป้าย ---------- */
.chip {
  display: inline-block; font-size: .74rem; font-weight: 800; padding: 3px 10px;
  border-radius: 999px; white-space: nowrap;
}
.chip-moving  { background: #dcfce7; color: #14532d; }
.chip-idling  { background: #fef3c7; color: #92400e; }
.chip-parked  { background: #dbeafe; color: #1e3a8a; }
.chip-offline { background: #e2e8f0; color: #475569; }
.chip-nodata  { background: #f1f5f9; color: #94a3b8; }
.chip-bad     { background: #fee2e2; color: #991b1b; }
.chip-warn    { background: #fef3c7; color: #92400e; }
.chip-ok      { background: #dcfce7; color: #14532d; }
.chip-neutral { background: #e2e8f0; color: #475569; }

.plate {
  display: inline-block; background: var(--navy); color: #fff; font-weight: 800;
  padding: 2px 9px; border-radius: 6px; font-size: .84rem; letter-spacing: .3px;
}
.speed { font-weight: 800; }
.speed.over { color: var(--red); }
.muted { color: var(--muted); font-size: .84rem; }
.mono { font-family: "Courier New", monospace; }

/* ---------- แผนที่ ---------- */
.map-box { border: 1px solid var(--line); border-radius: 14px; overflow: hidden; background: #fff; }
#map { width: 100%; height: 640px; }
#map.tall { height: calc(100vh - 210px); min-height: 520px; }
.map-toolbar {
  display: flex; gap: 9px; align-items: center; flex-wrap: wrap;
  padding: 11px 13px; border-bottom: 1px solid var(--line); background: #fbfdff;
}
.map-toolbar .grow { flex: 1; min-width: 180px; }
.legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: .8rem; color: var(--muted); }
.legend i { display: inline-block; width: 11px; height: 11px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }

.marker-pin {
  border-radius: 50%; border: 2px solid #fff; box-shadow: 0 2px 6px rgba(0,0,0,.35);
  display: flex; align-items: center; justify-content: center; color: #fff;
  font-size: 11px; font-weight: 800;
}

/* ---------- หมุดรถพร้อมป้ายทะเบียน ---------- */
.veh-marker { background: none !important; border: 0 !important; }
.veh-wrap { position: relative; display: flex; flex-direction: column; align-items: center; }
.veh-plate {
  position: absolute; top: 100%; left: 50%; transform: translateX(-50%);
  margin-top: 2px; white-space: nowrap;
  background: rgba(15,23,42,.9); color: #fff;
  font: 800 10.5px/1.35 "Sarabun", sans-serif; letter-spacing: .2px;
  padding: 1px 6px; border-radius: 5px; border: 1px solid rgba(255,255,255,.35);
  box-shadow: 0 2px 5px rgba(0,0,0,.3); pointer-events: none;
}
.veh-plate.dim { background: rgba(100,116,139,.82); }
/* ซ่อนป้ายเมื่อผู้ใช้กดปิด */
.plates-off .veh-plate { display: none; }
.leaflet-popup-content { font-family: "Sarabun", sans-serif; font-size: .86rem; margin: 12px 14px; }
.leaflet-popup-content b { color: var(--navy); }
.popup-row { display: flex; justify-content: space-between; gap: 14px; padding: 2px 0; }
.popup-row span { color: var(--muted); }

/* ---------- ตัวเล่นย้อนหลัง ---------- */
.player { display: flex; align-items: center; gap: 11px; padding: 11px 13px; border-top: 1px solid var(--line); background: #fbfdff; flex-wrap: wrap; }
.player input[type=range] { flex: 1; min-width: 180px; }
.player .pos { font-weight: 800; font-size: .86rem; min-width: 118px; }

/* ---------- แถบสถานะรถบนหน้ารายละเอียด ---------- */
.detail-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(190px,1fr)); gap: 11px; }
.detail-item { background: #f8fafc; border: 1px solid var(--line); border-radius: 12px; padding: 11px 13px; }
.detail-item small { display: block; color: var(--muted); font-weight: 800; font-size: .74rem; }
.detail-item b { font-size: .98rem; }

footer { text-align: center; color: var(--muted); font-size: .8rem; padding: 6px 0 24px; }

.loading {
  position: fixed; right: 18px; bottom: 18px; z-index: 2000;
  background: var(--navy); color: #fff; border-radius: 12px; padding: 12px 16px;
  display: none; align-items: center; gap: 11px; box-shadow: 0 16px 34px rgba(15,23,42,.3);
}
.loading.show { display: flex; }
.spinner {
  width: 18px; height: 18px; border: 3px solid rgba(255,255,255,.25);
  border-top-color: #5eead4; border-radius: 50%; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 1150px) {
  .shell { grid-template-columns: 1fr; }
  .shell-side { position: static; display: none; }
  .shell-side.open { display: grid; }
  .menu-toggle { display: inline-block; }
  .who { display: none; }
}
@media (max-width: 1350px) {
  /* จอแคบลง ให้รายการทะเบียนลงมาอยู่ใต้แผนที่แทนการเบียดด้านข้าง */
  .map-layout { grid-template-columns: 1fr; }
  .hub-beside { max-height: 420px; }
}

@media (max-width: 720px) {
  .form-grid { grid-template-columns: 1fr; }
  table { min-width: 640px; }
  #map { height: 460px; }
}
