:root{
  --bg1:#f6efe9; --bg2:#ebe6f3; --ink:#1e293b; --muted:#64748b; --line:#e2e8f0; --card:rgba(255,255,255,.92);
  --brand:#0f766e; --brand2:#14b8a6;
  --working:#10b981; --idle:#f59e0b; --inactive:#ef4444; --leave:#8b5cf6; --off:#94a3b8; --walk:#6366f1;
  --radius:14px; --shadow:0 6px 24px rgba(30,41,59,.10),0 1px 3px rgba(30,41,59,.08);
}
*{box-sizing:border-box}
html,body{margin:0;height:100%;font-family:Prompt,system-ui,-apple-system,"Segoe UI",Tahoma,sans-serif;color:var(--ink);background:linear-gradient(135deg,var(--bg1),var(--bg2));}
button,input,select{font:inherit;color:inherit}
a{color:var(--brand)}

/* ---------- app layout ---------- */
body.app{overflow:hidden;user-select:none;-webkit-user-select:none}
#scene{position:fixed;inset:0;width:100vw;height:100vh;display:block;cursor:grab;touch-action:none}
#scene.dragging{cursor:grabbing}
.hud{position:fixed;z-index:5;display:flex;flex-direction:column;gap:10px;pointer-events:none}
.hud>*{pointer-events:auto}
.hud.tl{top:14px;left:14px;width:280px}
.hud.tr{top:14px;right:14px}
.hud.br{right:14px;bottom:78px;width:350px}
.hud.bottom{left:50%;transform:translateX(-50%);bottom:12px;flex-direction:row;align-items:center;gap:10px;max-width:calc(100vw - 28px)}
.card{background:var(--card);backdrop-filter:blur(8px);-webkit-backdrop-filter:blur(8px);border-radius:var(--radius);box-shadow:var(--shadow);border:1px solid rgba(255,255,255,.7)}

.clock{padding:12px 16px}
.brand{display:flex;align-items:center;gap:8px;font-size:12.5px;font-weight:600}
.brand img{width:20px;height:20px;object-fit:contain}
.mode{display:flex;align-items:center;gap:6px;font-size:12px;color:var(--brand);margin-top:2px;font-weight:500}
.mode .dot{width:8px;height:8px;border-radius:50%;background:var(--working);box-shadow:0 0 0 0 rgba(16,185,129,.6);animation:pulse 1.8s infinite}
.mode .dot.rp{background:var(--idle);animation:none}
.mode .dot.err{background:var(--inactive);animation:none}
@keyframes pulse{0%{box-shadow:0 0 0 0 rgba(16,185,129,.55)}70%{box-shadow:0 0 0 8px rgba(16,185,129,0)}100%{box-shadow:0 0 0 0 rgba(16,185,129,0)}}
.time{font-size:34px;font-weight:700;letter-spacing:.5px;line-height:1.15;font-variant-numeric:tabular-nums}
.sub{font-size:12px;color:#b45309}

.dash{padding:10px 12px;max-height:calc(100vh - 250px);overflow:auto}
.dash-head{display:flex;align-items:center;justify-content:space-between;font-size:14px;margin-bottom:6px}
.x{border:0;background:#f1f5f9;border-radius:8px;width:26px;height:26px;cursor:pointer;color:var(--muted);line-height:1}
.x:hover{background:#e2e8f0}
.collapsed .dash-body,.collapsed .feed-list{display:none}
.tiles{display:grid;grid-template-columns:1fr 1fr;gap:6px}
.tile{background:#f8fafc;border-radius:10px;padding:6px 10px;border-left:4px solid var(--off)}
.tile span{display:block;font-size:11px;color:var(--muted)}
.tile b{font-size:22px;font-variant-numeric:tabular-nums}
.tile.s-working{border-color:var(--working)}.tile.s-idle{border-color:var(--idle)}.tile.s-inactive{border-color:var(--inactive)}.tile.s-leave{border-color:var(--leave)}
.busiest{font-size:12px;color:var(--muted);margin:8px 2px 4px}
.busiest b{color:var(--ink)}
.spark-wrap{background:#f8fafc;border-radius:10px;padding:6px 8px 4px}
#spark{width:100%;height:56px;display:block}
.spark-cap{font-size:10.5px;color:var(--muted)}
.sect{font-size:12.5px;font-weight:600;margin:10px 2px 4px}
.idle-list{display:flex;flex-direction:column;gap:3px;max-height:190px;overflow:auto}
.idle-row{display:flex;gap:8px;align-items:flex-start;font-size:12px;padding:5px 8px;background:#fff7ed;border-radius:8px;cursor:pointer;border:1px solid transparent}
.idle-row:hover{border-color:#fdba74}
.idle-row.red{background:#fef2f2}.idle-row.red:hover{border-color:#fca5a5}
.idle-row b{white-space:nowrap;max-width:1200px}
.idle-row small{color:var(--muted);display:block;line-height:1.3}
.empty{font-size:12px;color:var(--muted);padding:4px 8px}
.bot-sum{display:flex;flex-direction:column;gap:3px}
.bot-row{display:flex;gap:8px;align-items:center;font-size:12px;padding:4px 8px;border-radius:8px;background:#f8fafc;cursor:pointer}
.bot-row:hover{background:#eef2ff}
.bdot{width:9px;height:9px;border-radius:50%;flex:none;background:var(--off)}
.bdot.b-ok{background:var(--working)}.bdot.b-quiet{background:#60a5fa}.bdot.b-late{background:var(--idle)}.bdot.b-stalled,.bdot.b-error{background:var(--inactive)}.bdot.b-off,.bdot.b-unknown{background:#cbd5e1}
.bot-row .bn{flex:1;min-width:0;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.bot-row .ba{color:var(--muted);font-size:11px;white-space:nowrap;max-width:1200px}

.toolbar{display:flex;align-items:center;gap:4px;padding:6px 8px;flex-wrap:wrap;justify-content:flex-end;max-width:calc(100vw - 320px)}
.toolbar button,.toolbar a{border:0;background:transparent;border-radius:10px;min-width:34px;height:34px;padding:0 10px;cursor:pointer;font-size:14px;text-decoration:none;color:var(--ink);display:inline-flex;align-items:center;justify-content:center;white-space:nowrap;max-width:1200px}
.toolbar button:hover,.toolbar a:hover{background:#f1f5f9}
.toolbar button.on{background:var(--brand);color:#fff}
.toolbar .sep{width:1px;height:22px;background:var(--line);margin:0 4px}

.feed{padding:10px 12px}
.feed-list{max-height:200px;overflow:auto;display:flex;flex-direction:column;gap:2px}
.fi{display:grid;grid-template-columns:40px 1fr;gap:6px;font-size:12px;padding:4px 4px;border-radius:6px;cursor:pointer}
.fi:hover{background:#f1f5f9}
.fi .ft{color:var(--muted);font-variant-numeric:tabular-nums}
.fi .fw{font-weight:600}
.fi .sd{display:inline-block;width:8px;height:8px;border-radius:2px;margin-right:4px;vertical-align:middle}
.fi.new{animation:flash 2.2s ease-out}
@keyframes flash{0%{background:#fef9c3}100%{background:transparent}}

.replay{display:flex;align-items:center;gap:8px;padding:8px 12px}
.replay button{border:0;border-radius:10px;height:32px;padding:0 12px;background:#f1f5f9;cursor:pointer;font-weight:600;white-space:nowrap;max-width:1200px}
.replay .live-btn.on{background:#dcfce7;color:#047857}
.replay .live-btn{color:var(--muted)}
.replay input[type=date]{border:1px solid var(--line);border-radius:8px;padding:4px 6px;font-size:13px;background:#fff}
.replay input[type=range]{width:260px;accent-color:var(--brand)}
#timeLbl{font-weight:700;min-width:64px;font-variant-numeric:tabular-nums}
.legend{display:flex;align-items:center;flex-wrap:wrap;gap:3px 12px;padding:7px 14px;font-size:11.5px;max-width:560px}
.legend span{white-space:nowrap;max-width:1200px}
.lg{display:inline-block;width:10px;height:10px;border-radius:50%;margin-right:5px;vertical-align:-1px}
.s-working.lg{background:var(--working)}.s-idle.lg{background:var(--idle)}.s-inactive.lg{background:var(--inactive)}.s-leave.lg{background:var(--leave)}.s-off.lg{background:var(--off)}.s-walk.lg{background:var(--walk)}

.drawer{position:fixed;z-index:8;top:64px;right:14px;width:380px;max-height:calc(100vh - 160px);overflow:auto;background:#fff;border-radius:var(--radius);box-shadow:0 18px 50px rgba(30,41,59,.22);padding:18px 18px 14px;transform:translateX(calc(100% + 30px));transition:transform .25s ease}
.drawer.open{transform:none}
.drawer .close{position:absolute;top:12px;right:12px}
.dh{display:flex;gap:12px;align-items:center;margin-bottom:10px;padding-right:30px}
.avatar{width:48px;height:48px;border-radius:50%;display:flex;align-items:center;justify-content:center;font-weight:700;color:#fff;font-size:18px;flex:none}
.dh h3{margin:0;font-size:18px}
.dh .muted{font-size:12px}
.pill{display:inline-block;font-size:12px;font-weight:600;border-radius:999px;padding:2px 10px;color:#fff}
.box{background:#f8fafc;border-radius:10px;padding:10px 12px;margin:8px 0;font-size:13px}
.box h4{margin:0 0 6px;font-size:12.5px;color:var(--muted);font-weight:600}
.kv{display:grid;grid-template-columns:auto 1fr;gap:4px 10px;font-size:12.5px}
.kv span:nth-child(odd){color:var(--muted)}
.tl-list{display:flex;flex-direction:column;gap:4px;font-size:12.5px}
.tl-list div{display:grid;grid-template-columns:42px 1fr;gap:6px}
.tl-list .t{color:var(--muted);font-variant-numeric:tabular-nums}
.bars{display:flex;flex-direction:column;gap:4px}
.bar{display:grid;grid-template-columns:120px 1fr 28px;gap:6px;align-items:center;font-size:12px}
.bar i{display:block;height:8px;border-radius:4px}
.chips{display:flex;flex-wrap:wrap;gap:4px}
.chip{display:inline-block;font-size:11.5px;background:#eef2ff;color:#3730a3;border-radius:999px;padding:2px 9px;margin:2px 4px 2px 0}
.chip.solar{background:#fef3c7;color:#92400e}
.plist{display:flex;flex-direction:column;gap:4px}
.prow{display:flex;gap:8px;align-items:center;font-size:12.5px;padding:5px 8px;border-radius:8px;background:#f8fafc;cursor:pointer}
.prow:hover{background:#eef2ff}
.prow .sdot{width:9px;height:9px;border-radius:50%;flex:none}
.prow small{color:var(--muted);display:block}
.muted{color:var(--muted)}
.small{font-size:12px}
.toast{position:fixed;left:50%;top:16px;transform:translateX(-50%);background:#1e293b;color:#fff;padding:8px 14px;border-radius:10px;font-size:13px;z-index:20;opacity:0;transition:opacity .3s;pointer-events:none}
.toast.show{opacity:.95}


/* ---------- login & secondary pages ---------- */
.login-page{display:flex;align-items:center;justify-content:center;min-height:100vh;padding:16px}
.login-card{width:100%;max-width:380px;background:#fff;border-radius:18px;box-shadow:var(--shadow);padding:30px 28px;display:flex;flex-direction:column;gap:12px;text-align:center}
.login-logo{width:64px;height:64px;object-fit:contain;margin:0 auto}
.login-card h1{margin:0;font-size:21px}
.login-card p{margin:0}
.login-card label{text-align:left;font-size:13px;color:var(--muted);display:flex;flex-direction:column;gap:4px}
.login-card input{border:1px solid var(--line);border-radius:10px;padding:10px 12px;font-size:15px;color:var(--ink)}
.login-card input:focus{outline:2px solid var(--brand2);border-color:transparent}
.login-card button,.pairform button{border:0;background:var(--brand);color:#fff;border-radius:10px;padding:11px;font-weight:600;cursor:pointer;font-size:15px}
.login-card button:hover{background:#115e59}
.alert{background:#fef2f2;color:#b91c1c;border-radius:10px;padding:8px 10px;font-size:13px}
.ok{background:#ecfdf5;color:#047857;border-radius:10px;padding:8px 10px;font-size:13px;margin-top:8px}
.hint{font-size:12px;color:var(--muted)}
body.page{padding:24px 16px 60px;max-width:1150px;margin:0 auto}
.page-head h1{margin:6px 0;font-size:22px}
.back{text-decoration:none;font-size:14px}
.card h2{margin:0 0 10px;font-size:17px}
section.card{padding:16px 18px;margin-top:16px;background:#fff}
.tbl{width:100%;border-collapse:collapse;font-size:13.5px}
.tbl th{text-align:left;color:var(--muted);font-weight:500;border-bottom:1px solid var(--line);padding:6px}
.tbl td{border-bottom:1px solid #f1f5f9;padding:8px 6px;vertical-align:top}
.tbl tr.dim td{opacity:.5}
.linkrow{font-size:12.5px;color:#334155;margin:4px 0}
form.inline{display:inline}
.btn-sm{border:0;border-radius:8px;padding:3px 10px;font-size:12px;background:var(--brand);color:#fff;cursor:pointer}
.btn-sm.ghost{background:#f1f5f9;color:var(--ink)}
.btn-sm.danger{background:#fee2e2;color:#b91c1c}
.pairform{display:flex;gap:8px;flex-wrap:wrap;align-items:center}
.pairform select{border:1px solid var(--line);border-radius:8px;padding:8px;max-width:420px;flex:1;min-width:220px;background:#fff}
@media (max-width:700px){.tbl thead{display:none}.tbl tr{display:block;border-bottom:1px solid var(--line);padding:6px 0}.tbl td{display:block;border:0;padding:3px 4px}}

.open-sys{display:block;text-align:center;color:#fff;text-decoration:none;font-weight:600;border-radius:10px;padding:9px 12px;margin:6px 0 10px;box-shadow:0 2px 8px rgba(30,41,59,.15)}
.open-sys:hover{filter:brightness(1.08)}

/* ================= Responsive ================= */
.mtabs,.mstats,.feed .more{display:none}
@media (max-width:1100px) and (min-width:761px){
  .hud.tl{width:250px}.time{font-size:28px}
  .legend{display:none}
  .toolbar{max-width:calc(100vw - 290px)}
  .toolbar button,.toolbar a{min-width:30px;padding:0 7px;height:30px;font-size:13px}
  .replay input[type=range]{width:170px}
  .hud.br{width:300px}
  .drawer{width:340px}
}
@media (max-width:760px){
  :root{--tabh:calc(58px + env(safe-area-inset-bottom))}
  /* top status card */
  .hud.tl{top:calc(8px + env(safe-area-inset-top));left:8px;right:8px;width:auto}
  .clock{padding:8px 12px;display:grid;grid-template-columns:auto 1fr;grid-template-areas:"brand brand" "time mode" "time sub" "stats stats";column-gap:12px;align-items:center}
  .clock .brand{grid-area:brand;font-size:11.5px}
  .clock .brand img{width:16px;height:16px}
  .clock .time{grid-area:time;font-size:26px}
  .clock .mode{grid-area:mode;margin:0;font-size:11px}
  .clock .sub{grid-area:sub;font-size:11px;line-height:1.3}
  .mstats{grid-area:stats;display:grid;grid-template-columns:repeat(4,1fr);gap:6px;margin-top:6px}
  .mstats div{background:#f8fafc;border-radius:9px;padding:4px 6px;border-left:3px solid var(--off);font-size:10.5px;color:var(--muted);line-height:1.2;cursor:pointer}
  .mstats div b{display:block;font-size:17px;color:var(--ink)}
  .mstats .s-working{border-color:var(--working)}.mstats .s-idle{border-color:var(--idle)}.mstats .s-inactive{border-color:var(--inactive)}.mstats .s-leave{border-color:var(--leave)}
  /* sheets (dashboard / feed) */
  .dash,.hud.br{display:none}
  body[data-mtab=dash] .dash,body[data-mtab=feed] .hud.br{display:block}
  .dash,.hud.br .feed{position:fixed;left:0;right:0;bottom:var(--tabh);top:auto;max-height:68vh;overflow:auto;border-radius:18px 18px 0 0;box-shadow:0 -10px 40px rgba(30,41,59,.18);padding:14px 16px;z-index:7;background:#fff}
  .hud.br{left:0;right:0;bottom:0;width:auto}
  .hud.br .feed-list{max-height:none}
  .dash .x,.feed .x{width:32px;height:32px;font-size:16px}
  .idle-list{max-height:none}
  .tile b{font-size:20px}
  /* map tools: vertical column on the right */
  .hud.tr{top:auto;bottom:calc(var(--tabh) + 62px);right:8px}
  .toolbar{flex-direction:column;max-width:none;padding:4px;gap:2px}
  .toolbar .sep,.toolbar [data-act=feed],.toolbar [data-act=people],.toolbar [data-act=zin],.toolbar [data-act=zout],.toolbar [data-act=full]{display:none}
  .toolbar button,.toolbar a{min-width:38px;height:38px;padding:0;font-size:15px}
  .toolbar [data-act=labels],.toolbar [data-act=bots]{font-size:0}
  .toolbar [data-act=labels]::before{content:"🏷️";font-size:15px}
  .toolbar [data-act=bots]::before{content:"🤖";font-size:15px}
  .toolbar a[href="logout.php"]{font-size:11px}
  /* replay row */
  .hud.bottom{left:8px;right:64px;transform:none;bottom:calc(var(--tabh) + 8px);max-width:none}
  .legend{display:none}
  .replay{width:100%;padding:6px 8px;gap:6px}
  .replay button{height:30px;padding:0 9px}
  .replay input[type=range]{flex:1;width:auto;min-width:60px}
  #dateIn{display:none}
  #timeLbl{min-width:40px;font-size:13px}
  body[data-mtab=dash] .hud.bottom,body[data-mtab=feed] .hud.bottom,body[data-mtab=dash] .hud.tr,body[data-mtab=feed] .hud.tr{display:none}
  /* tab bar */
  .mtabs{display:grid;grid-template-columns:repeat(4,1fr);position:fixed;left:0;right:0;bottom:0;height:var(--tabh);padding-bottom:env(safe-area-inset-bottom);background:rgba(255,255,255,.97);backdrop-filter:blur(8px);border-top:1px solid var(--line);z-index:9}
  .mtabs button{border:0;background:none;display:flex;flex-direction:column;align-items:center;justify-content:center;gap:1px;font-size:11px;color:var(--muted);position:relative;cursor:pointer}
  .mtabs button i{font-style:normal;font-size:19px;line-height:1.1;filter:grayscale(.6);opacity:.75}
  .mtabs button.on{color:var(--brand);font-weight:600}
  .mtabs button.on i{filter:none;opacity:1}
  .mtabs .badge{position:absolute;top:5px;left:calc(50% + 6px);background:var(--inactive);color:#fff;font-size:10px;border-radius:999px;padding:0 5px;line-height:16px;min-width:16px}
  .mtabs .badge:empty{display:none}
  /* detail drawer → bottom sheet */
  .drawer{left:0;right:0;top:auto;bottom:0;width:auto;max-height:82vh;border-radius:18px 18px 0 0;padding:16px 16px calc(20px + env(safe-area-inset-bottom));transform:translateY(105%);z-index:10}
  .drawer.open{transform:none}
  .drawer::before{content:"";display:block;width:40px;height:4px;border-radius:2px;background:#cbd5e1;margin:-6px auto 10px}
  .toast{top:auto;bottom:calc(var(--tabh) + 60px)}
}
@media (max-width:760px){
  /* หน้าแผนที่: การ์ดกิจกรรมล่าสุดแบบย่อ (เหมือนบนคอม) */
  body[data-mtab=map] .hud.br{display:block;left:8px;right:8px;bottom:calc(var(--tabh) + 58px);top:auto;width:auto}
  body[data-mtab=map] .hud.br .feed{position:static;max-height:none;border-radius:14px;box-shadow:var(--shadow);padding:8px 12px 6px;background:var(--card)}
  body[data-mtab=map] .hud.br .dash-head{margin-bottom:2px;font-size:13px}
  body[data-mtab=map] .hud.br .x{display:none}
  body[data-mtab=map] .hud.br .more{display:inline-block;font-size:12px;color:var(--brand);font-weight:600;cursor:pointer;padding:2px 4px}
  body[data-mtab=map] .hud.br .feed-list{max-height:84px;overflow:hidden;gap:0}
  body[data-mtab=map] .hud.br .fi{padding:2px 2px;font-size:11.5px;grid-template-columns:36px 1fr}
  body[data-mtab=map] .hud.br .fi>span:last-child{white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
  body[data-mtab=map] .hud.tr{bottom:calc(var(--tabh) + 186px)}
  body[data-mtab=map] .hud.bottom{right:8px}
  .toolbar button,.toolbar a{min-width:36px;height:34px}
}
