:root {
  --blue: #2f6bf0;
  --blue-50: #eaf1ff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e6ebf3;
  --ok: #15803d;
  --ok-bg: #e7f6ec;
  --warn: #b45309;
  --warn-bg: #fdf2dd;
  --card: rgba(255, 255, 255, 0.94);
  --shadow: 0 8px 28px rgba(30, 50, 90, 0.1), 0 1px 2px rgba(30, 50, 90, 0.06);
  --r: 14px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: #eef2f8;
  color: var(--ink);
  font: 14px/1.4 "IBM Plex Sans Thai", system-ui, sans-serif;
}
button { font: inherit; color: inherit; cursor: pointer; }
#scene { position: fixed; inset: 0; }
#scene canvas { display: block; }
.labels { position: absolute; inset: 0; pointer-events: none; }
.loading { position: fixed; inset: 0; display: grid; place-items: center; color: var(--muted); font-weight: 500; }
body.ready .loading { display: none; }

.card {
  background: var(--card);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.8);
  border-radius: var(--r);
  box-shadow: var(--shadow);
}
.muted { color: var(--muted); }

/* top bar */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; height: 64px; z-index: 10;
  display: flex; align-items: center; gap: 16px; padding: 0 20px;
  background: rgba(255, 255, 255, 0.92); backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 20px; letter-spacing: 0.01em; white-space: nowrap; }
.logo { width: 36px; height: 36px; border-radius: 10px; background: linear-gradient(145deg, #4b85ff, #1f4fc4); display: grid; place-items: center; box-shadow: 0 4px 10px rgba(47, 107, 240, 0.35); }
.search {
  flex: 1; max-width: 460px; display: flex; align-items: center; gap: 10px;
  height: 42px; padding: 0 12px; border: 1px solid var(--line); border-radius: 12px; background: #fff; color: var(--muted);
}
.search input { flex: 1; border: 0; outline: 0; font: inherit; color: var(--ink); background: none; min-width: 0; }
.search kbd { border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; font-size: 12px; }
.site-chip { margin-left: auto; display: flex; align-items: center; gap: 10px; padding: 6px 14px 6px 6px; border: 1px solid var(--line); border-radius: 12px; background: #fff; }
.site-chip .code { background: var(--blue); color: #fff; font-weight: 700; font-size: 12px; padding: 6px 8px; border-radius: 8px; }
.site-chip b { display: block; font-size: 14px; }
.site-chip small { color: var(--muted); font-size: 12px; }
.live { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 20px; background: var(--ok-bg); color: var(--ok); font-weight: 500; }
.live i { width: 8px; height: 8px; border-radius: 50%; background: #22c55e; box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.2); }
.live b { font-variant-numeric: tabular-nums; }
.btn-import { display: flex; align-items: center; gap: 6px; height: 40px; padding: 0 14px; border: 0; border-radius: 10px; background: var(--blue); color: #fff; font-weight: 600; }
.btn-import:hover { background: #1f57d6; }

/* KPI row */
.kpis { position: fixed; top: 80px; left: 20px; display: flex; gap: 12px; z-index: 5; max-width: calc(100vw - 460px); overflow-x: auto; padding-bottom: 4px; }
.kpi { display: flex; align-items: center; gap: 12px; padding: 12px 18px 12px 12px; min-width: 190px; }
.kpi small { display: block; color: var(--muted); font-size: 12.5px; }
.kpi b { font-size: 24px; font-weight: 700; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; margin-right: 6px; }
.kpi em { font-style: normal; font-size: 12px; color: var(--muted); }
.kpi em.up { color: var(--ok); }
.ico { width: 44px; height: 44px; border-radius: 12px; background: var(--blue-50); color: var(--blue); display: grid; place-items: center; flex: none; }
.ico.sm { width: 32px; height: 32px; border-radius: 9px; }

/* view controls */
.viewctrl { position: fixed; top: 80px; right: 384px; display: flex; flex-direction: column; gap: 10px; z-index: 5; }
.stack { display: flex; flex-direction: column; padding: 4px; }
.stack button { width: 38px; height: 36px; border: 0; background: none; border-radius: 9px; font-size: 18px; font-weight: 500; display: grid; place-items: center; color: #334155; }
.stack button[data-speed] { font-size: 12.5px; font-weight: 600; }
.stack button:hover { background: #f1f5fb; }
.stack button.on { background: var(--blue-50); color: var(--blue); }

/* right panel */
.panel { position: fixed; top: 80px; right: 20px; width: 350px; max-height: calc(100vh - 80px - 250px); overflow-y: auto; padding: 16px; z-index: 5; }
.ph { display: flex; gap: 12px; align-items: center; }
.thumb { width: 52px; height: 52px; border-radius: 12px; background: linear-gradient(145deg, #4b85ff, #1f4fc4); display: grid; place-items: center; flex: none; }
.ph small { color: var(--blue); font-weight: 600; font-size: 11.5px; letter-spacing: 0.04em; }
.ph h2 { margin: 0; font-size: 18px; }
.ph p { margin: 0; color: var(--muted); font-size: 12.5px; }
.status-row { display: flex; align-items: center; gap: 10px; margin: 14px 0 12px; font-size: 12.5px; color: var(--muted); }
.pill { display: inline-block; padding: 2px 10px; border-radius: 8px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.pill.ok { background: var(--ok-bg); color: var(--ok); }
.pill.warn { background: var(--warn-bg); color: var(--warn); }
.pill.info { background: var(--blue-50); color: var(--blue); }
.pill.muted { background: #f1f5f9; color: var(--muted); }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: #f5f8fc; border-radius: 10px; padding: 10px 12px; }
.stat small { color: var(--muted); font-size: 12px; }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat b i { font-style: normal; font-weight: 400; font-size: 12px; color: var(--muted); }
.bar { height: 5px; background: #e2e8f0; border-radius: 3px; overflow: hidden; margin-top: 6px; }
.bar span { display: block; height: 100%; background: var(--blue); border-radius: 3px; transition: width 0.3s; }
.bar.green span { background: #22c55e; }
.panel h4 { display: flex; justify-content: space-between; margin: 16px 0 6px; font-size: 13.5px; }
.panel h4 span { color: var(--muted); font-weight: 400; font-size: 12px; }
.stations, .fleet { list-style: none; margin: 0; padding: 0; }
.stations li { display: grid; grid-template-columns: 10px 1fr 70px 36px 72px; align-items: center; gap: 8px; padding: 7px 4px; border-bottom: 1px solid var(--line); cursor: pointer; font-size: 13px; }
.stations li:hover { background: #f7f9fd; }
.stations .sw { width: 10px; height: 10px; border-radius: 3px; }
.stations .bar { margin: 0; }
.stations .pct { font-variant-numeric: tabular-nums; color: var(--muted); font-size: 12px; text-align: right; }
.stations .pill { text-align: center; }
.fleet li { display: grid; grid-template-columns: 60px 1fr auto; gap: 8px; padding: 6px 4px; font-size: 12.5px; border-bottom: 1px solid var(--line); }
.fleet em { font-style: normal; color: var(--muted); }

/* flow tracker */
.flow { position: fixed; left: 20px; bottom: 20px; width: min(760px, calc(100vw - 500px)); padding: 14px 18px; z-index: 5; }
.flow-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; }
.flow-head .muted { font-size: 12.5px; }
.flow-head .lot { margin-left: auto; font-size: 13px; color: var(--muted); }
.flow-body { display: flex; gap: 16px; align-items: center; }
.steps { list-style: none; display: flex; flex: 1; margin: 0; padding: 0; }
.steps li { flex: 1; display: flex; flex-direction: column; align-items: center; text-align: center; position: relative; font-size: 12px; }
.steps li::before { content: ""; position: absolute; top: 15px; left: -50%; width: 100%; height: 3px; background: #e2e8f0; z-index: 0; }
.steps li:first-child::before { display: none; }
.steps li.done::before, .steps li.cur::before { background: var(--blue); }
.steps .dot { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: #eef2f7; color: var(--muted); font-weight: 700; position: relative; z-index: 1; border: 3px solid #fff; }
.steps li.done .dot { background: var(--blue); color: #fff; }
.steps li.cur .dot { background: #fff; color: var(--blue); border-color: var(--blue); box-shadow: 0 0 0 5px rgba(47, 107, 240, 0.15); }
.steps b { margin-top: 6px; font-weight: 600; font-size: 12.5px; }
.steps small { color: var(--muted); font-variant-numeric: tabular-nums; }
.lotcard { display: flex; gap: 10px; align-items: center; background: #f5f8fc; border-radius: 12px; padding: 10px 12px; width: 210px; }
.lot-box { width: 46px; height: 46px; border-radius: 10px; background: #fff; display: grid; place-items: center; flex: none; }
.lotcard > div:last-child { flex: 1; }
.lotcard small { color: var(--muted); font-size: 12px; }
.lotcard em { font-style: normal; font-size: 12px; color: var(--ok); font-weight: 600; }

/* docks table */
.docks { position: fixed; right: 20px; bottom: 20px; width: 460px; padding: 12px 14px; z-index: 5; }
.tabs { display: flex; align-items: center; gap: 4px; margin-bottom: 6px; }
.tabs svg { margin-right: 6px; }
.tabs button { border: 0; background: none; padding: 5px 10px; border-radius: 8px; font-weight: 600; font-size: 13px; color: var(--muted); }
.tabs button.on { background: var(--blue-50); color: var(--blue); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
td { padding: 7px 4px; border-top: 1px solid var(--line); vertical-align: middle; }
tr[data-dock], tr[data-truck] { cursor: pointer; }
tr[data-dock]:hover, tr[data-truck]:hover { background: #f7f9fd; }
td small { display: block; color: var(--muted); font-size: 11.5px; }
td .bul { display: inline-block; width: 7px; height: 7px; border-radius: 50%; margin-right: 6px; background: #f59e0b; }
td .bul.out { background: var(--blue); }
td.prog { white-space: nowrap; text-align: right; color: var(--muted); font-size: 12px; }
.mini { display: inline-block; width: 42px; height: 5px; background: #e2e8f0; border-radius: 3px; overflow: hidden; vertical-align: middle; margin-right: 6px; }
.mini span { display: block; height: 100%; background: #22c55e; }
td em { font-style: normal; }

/* 3D labels */
.pin { pointer-events: auto; cursor: pointer; display: flex; flex-direction: column; align-items: center; transform: translateY(-20px); }
.pin-dot {
  width: 34px; height: 34px; border-radius: 50% 50% 50% 0; transform: rotate(-45deg);
  background: var(--blue); color: #fff; display: grid; place-items: center;
  box-shadow: 0 6px 14px rgba(31, 79, 196, 0.4); border: 3px solid #fff;
}
.pin-dot svg { transform: rotate(45deg); }
.pin-name { margin-top: 6px; background: #fff; padding: 2px 10px; border-radius: 8px; font-size: 12px; font-weight: 600; box-shadow: var(--shadow); opacity: 0; transition: opacity 0.15s; white-space: nowrap; }
.pin:hover .pin-name { opacity: 1; }
.tag { pointer-events: auto; cursor: pointer; background: rgba(255, 255, 255, 0.95); padding: 1px 8px; border-radius: 6px; font-size: 11px; font-weight: 600; box-shadow: 0 2px 6px rgba(30, 50, 90, 0.18); white-space: nowrap; }
.tag.truck { color: var(--blue); }
.tag.station { color: #334155; font-weight: 500; }

.toast { position: fixed; left: 50%; top: 84px; transform: translate(-50%, -10px); background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px; opacity: 0; pointer-events: none; transition: 0.2s; z-index: 20; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.drop-hint { position: fixed; inset: 12px; border: 3px dashed var(--blue); border-radius: 20px; background: rgba(234, 241, 255, 0.7); display: none; place-items: center; font-size: 20px; font-weight: 600; color: var(--blue); z-index: 30; pointer-events: none; }
body.dragging .drop-hint { display: grid; }

@media (max-width: 1100px) {
  .panel, .docks, .viewctrl .stack:first-child { display: none; }
  .viewctrl { right: 16px; }
  .kpis { max-width: calc(100vw - 80px); left: 16px; }
  .flow { left: 16px; right: 16px; width: auto; }
  .lotcard, .site-chip, .search kbd { display: none; }
}
@media (max-width: 640px) {
  .topbar { gap: 8px; padding: 0 16px; }
  .brand span, .search, .btn-import { display: none; }
  .live { margin-left: auto; }
  .kpi { min-width: 160px; }
  .steps b { font-size: 10.5px; }
}
.tag.truck.out { color: #0f766e; }
.tag.rs { color: #b45309; }
.loading b { margin-left: 6px; color: var(--blue); }
.tag.measure { color: #e11d48; font-size: 13px; padding: 4px 10px; text-align: center; pointer-events: none; }
.tag.measure small { display: block; color: #475569; font-size: 10px; font-weight: 500; }
.tag.svc { color: #475569; font-size: 10px; }
.flow-tabs { display: flex; gap: 4px; }
.flow-tabs button { border: 0; background: none; padding: 4px 10px; border-radius: 8px; font-weight: 600; font-size: 13px; color: var(--muted); cursor: pointer; }
.flow-tabs button.on { background: var(--blue-50); color: var(--blue); }
.tag.line { color: #0f172a; text-align: center; font-size: 11px; }
.tag.line small, .tag.store small { display: block; color: #64748b; font-weight: 500; font-size: 10px; }
.tag.store { color: #0f766e; text-align: center; font-size: 12px; padding: 3px 10px; }
.tag.refr { color: #334155; font-size: 10px; padding: 0 6px; pointer-events: none; }
.tag.refr.cond { color: #b45309; }
.tag.fl { color: #b45309; font-size: 10px; }
.tag.truck.prod { color: #0f766e; }
.tag.qc { color: #92400e; background: #fef3c7; font-size: 11px; pointer-events: none; }
.tag.door { font-size: 10px; padding: 0 6px; pointer-events: none; }
.tag.door.in { color: #15803d; }
.tag.door.out { color: #b91c1c; }

/* route colour legend (matches the colours painted on the plan) */
.route-legend { position: fixed; left: 20px; bottom: 200px; z-index: 5; list-style: none; margin: 0; padding: 8px 12px; display: grid; gap: 4px; font-size: 12px; max-width: 300px; }
.route-legend[hidden] { display: none; }
.route-legend li { display: flex; align-items: center; gap: 8px; }
.route-legend i { width: 18px; height: 4px; border-radius: 2px; flex: none; }
.tag.isobay { font-size: 10px; padding: 1px 6px; background: rgba(255,255,255,.92); color: #334155; border: 1px solid #cbd5e1; border-radius: 6px; white-space: nowrap; }
.tag.isobay.low { background: #fee2e2; border-color: #fca5a5; color: #b91c1c; }
.tag.truck.iso { color: #b91c1c; }
