/* ห้องแล็บหุ่นยนต์ — palette | script | stage บนจอกว้าง, ซ้อนกันบนจอเล็ก
   ใช้ token ชุดเดียวกับ hub ให้ทุก app เป็นตระกูลเดียวกัน */

:root{
  --sans:'IBM Plex Sans Thai','Noto Sans Thai',system-ui,-apple-system,'Segoe UI',sans-serif;
  --mono:ui-monospace,'SF Mono',Menlo,monospace;
  --ink:#191a24;
  --muted:#6b6c7e;
  --line:#e6e3dc;
  --surface:#fff;
  --bg:#f5f4ef;
  --brand:#2e7d88;
  --brand-ink:#1f5f68;
  --brand-soft:#dcecee;
  --ok:#129a72;
  --warn:#d68a17;
  --danger:#e2564a;

  --c-motion:#4c7ef3;
  --c-control:#e8a33d;
  --c-sensing:#16bcc4;
  --c-output:#9b5de5;
}

*{box-sizing:border-box}
html,body{margin:0;height:100%;overflow:hidden}
body{
  font-family:var(--sans);color:var(--ink);background:var(--bg);
  display:flex;flex-direction:column;-webkit-font-smoothing:antialiased;
  user-select:none;-webkit-user-select:none;
}

/* ============ แผงบล็อก ============ */
#palette{
  flex:none;background:var(--surface);border-bottom:1px solid var(--line);
  padding:8px 10px;overflow-x:auto;overflow-y:hidden;
}
.pal-head{font-size:.8rem;font-weight:700;color:var(--muted);margin-bottom:6px}
#pal-list{display:flex;gap:14px;align-items:flex-start}
.pal-cat{flex:none}
.pal-cat-name{
  font-size:.7rem;font-weight:700;margin-bottom:4px;letter-spacing:.02em;
}
.pal-blocks{display:flex;gap:5px}

/* ============ บล็อก ============ */
.blk{
  position:relative;border-radius:8px;color:#fff;
  font-size:.8rem;font-weight:600;line-height:1.3;
  padding:8px 11px;cursor:grab;
  box-shadow:0 1px 2px rgba(25,26,36,.18);
  display:flex;align-items:center;gap:6px;flex-wrap:wrap;
  min-height:36px;
}
.blk:active{cursor:grabbing}
.blk.cat-motion{background:var(--c-motion)}
.blk.cat-control{background:var(--c-control)}
.blk.cat-sensing{background:var(--c-sensing)}
.blk.cat-output{background:var(--c-output)}
.blk.is-running{outline:3px solid #f7c04a;outline-offset:1px}

/* ช่องกรอกในบล็อก */
.blk input,.blk select{
  font-family:inherit;font-size:.76rem;font-weight:700;color:var(--ink);
  background:#fff;border:none;border-radius:999px;padding:3px 8px;
  min-width:46px;text-align:center;
}
.blk select{text-align:left;max-width:100%;flex:1 1 130px;min-width:120px}
.blk input:focus,.blk select:focus{outline:2px solid rgba(255,255,255,.7)}
.blk .del{
  margin-left:auto;background:rgba(0,0,0,.18);border:none;color:#fff;
  width:20px;height:20px;border-radius:50%;cursor:pointer;font-size:.7rem;
  display:grid;place-items:center;flex:none;
}
.blk .del:hover{background:rgba(0,0,0,.35)}

/* ช่องค่าเกณฑ์ของเงื่อนไข (เช่น ระยะ < 25 ซม.) */
.cv-wrap{
  display:inline-flex;align-items:center;gap:5px;
  background:rgba(0,0,0,.13);border-radius:999px;padding:2px 8px 2px 4px;
}
.cv-wrap .cv-unit{font-size:.7rem;font-weight:600;opacity:.95;white-space:nowrap}
.cv-wrap input{min-width:44px}
/* ตัวเชื่อมเงื่อนไข และ/หรือ + เงื่อนไขที่สอง */
.combo-sel{flex:0 0 auto;min-width:64px;max-width:88px;font-weight:700;
  background:rgba(0,0,0,.14);color:#fff;border-radius:999px;padding:3px 6px;text-align:center}
.combo-sel option{color:var(--ink)}
.cond-2{display:contents}

/* บล็อกปากคาบ (C-block) */
.cwrap{border-radius:8px}
.cwrap.cat-control{background:var(--c-control)}
.cmouth{
  margin-left:14px;padding:2px 6px 6px;
  border-left:4px solid rgba(0,0,0,.14);
  min-height:26px;
}
.cfoot{height:10px}
.clabel{
  font-size:.72rem;font-weight:700;color:#fff;
  padding:3px 11px 5px;opacity:.92;
}

/* ============ พื้นที่สคริปต์ ============ */
#editor{
  flex:none;display:flex;flex-direction:column;gap:6px;
  background:var(--surface);border-bottom:1px solid var(--line);
  padding:10px 12px;max-height:38vh;
}
.ed-head{display:flex;align-items:center;gap:10px}
.ed-title{font-weight:700;font-size:.88rem}
.ed-count{font-size:.75rem;color:var(--muted);margin-left:auto}
.ed-hint{margin:0;font-size:.7rem;color:var(--muted)}
.script{
  flex:1;overflow-y:auto;min-height:90px;
  border:1.5px dashed var(--line);border-radius:12px;padding:8px;
  background:var(--bg);
}
.script.drag-over{border-color:var(--brand);background:var(--brand-soft)}
.script-empty{
  color:#a0a1b0;font-size:.8rem;text-align:center;margin:26px 0;
}

/* จุดวางบล็อก */
.slot{
  height:8px;border-radius:4px;margin:2px 0;transition:all .12s;
}
.slot.hot{height:26px;background:var(--brand-soft);border:2px dashed var(--brand)}

/* บล็อกที่กำลังลาก */
#drag-ghost{
  position:fixed;z-index:900;pointer-events:none;opacity:.9;
  transform:translate(-14px,-14px);
}

/* ============ สนาม 3D ============ */
#stage{position:relative;flex:1;min-height:0;background:#fff}
#webgl-canvas{width:100%;height:100%;display:block;touch-action:none}

.status{
  position:absolute;left:12px;top:12px;right:12px;
  background:rgba(255,255,255,.95);border:1px solid var(--line);
  border-radius:12px;padding:8px 13px;font-size:.82rem;
  box-shadow:0 4px 14px rgba(25,26,36,.08);pointer-events:none;
  max-width:min(520px,calc(100% - 24px));line-height:1.5;
}
.status.ok{border-color:#bfe9da;color:#0a6b50;background:#f0fbf7}
.status.bad{border-color:#f6cdc9;color:#8f2018;background:#fdf3f2}
.status.warn{border-color:#f7e2bd;color:#7d5209;background:#fffaf0}

.sensors{
  position:absolute;right:12px;top:12px;display:flex;gap:6px;
  background:rgba(255,255,255,.95);border:1px solid var(--line);
  border-radius:999px;padding:5px 10px;font-size:.7rem;color:var(--muted);
  box-shadow:0 4px 14px rgba(25,26,36,.08);
}
.sensor{display:flex;align-items:center;gap:4px}
.sensor .dot{
  width:10px;height:10px;border-radius:50%;background:#d6d3ca;
  border:1px solid rgba(0,0,0,.12);transition:background .1s;
}
.sensor.on .dot{background:#e2564a;box-shadow:0 0 6px rgba(226,86,74,.6)}

.stage-foot{
  position:absolute;left:12px;right:12px;bottom:12px;
  display:flex;gap:8px;flex-wrap:wrap;
}
.btn{
  padding:10px 16px;border-radius:999px;border:1px solid transparent;
  font-family:inherit;font-size:.85rem;font-weight:600;cursor:pointer;
  box-shadow:0 2px 6px rgba(25,26,36,.12);transition:filter .15s;
}
.btn:active{transform:translateY(1px)}
.btn[disabled]{opacity:.45;pointer-events:none}
.btn-ghost{background:var(--surface);border-color:var(--line);color:var(--ink)}
.btn-python{background:#2f3142;color:#fff}
.btn-run{background:var(--brand);color:#fff;flex:1;min-width:100px}
.btn-submit{background:var(--ok);color:#fff}
.mini-btn{
  padding:5px 11px;border-radius:999px;border:1px solid var(--line);
  background:var(--surface);font-family:inherit;font-size:.75rem;
  cursor:pointer;color:var(--ink);text-decoration:none;display:inline-block;
}
.mini-btn:hover{background:var(--bg)}

/* ============ หน้าต่างโค้ด ============ */
.code-modal{
  position:fixed;inset:0;z-index:800;display:grid;place-items:center;
  background:rgba(25,26,36,.55);backdrop-filter:blur(3px);padding:20px;
}
.code-modal[hidden]{display:none}
.code-box{
  background:var(--surface);border-radius:16px;width:min(760px,100%);
  max-height:82vh;display:flex;flex-direction:column;overflow:hidden;
  box-shadow:0 30px 70px -25px rgba(25,26,36,.5);
}
.code-head{
  display:flex;align-items:center;gap:10px;padding:14px 18px;
  border-bottom:1px solid var(--line);
}
.code-head strong{flex:1;font-size:.95rem}
.code-body{
  margin:0;padding:16px 18px;overflow:auto;flex:1;
  font-family:var(--mono);font-size:.78rem;line-height:1.65;
  background:#1d1f2b;color:#e7e9f4;white-space:pre;user-select:text;
}
.code-foot{
  display:flex;align-items:center;gap:8px;padding:12px 18px;
  border-top:1px solid var(--line);flex-wrap:wrap;
}
.code-note{flex:1;font-size:.73rem;color:var(--muted);min-width:200px}
.code-note code{background:var(--bg);padding:1px 5px;border-radius:5px}

/* ============ จอกว้าง: 3 คอลัมน์แบบ Scratch ============ */
@media (min-width:1000px){
  body{flex-direction:row}
  #palette{
    /* กว้างขึ้นเพราะบล็อกเงื่อนไขมีทั้ง dropdown และช่องค่าเกณฑ์ในบรรทัดเดียว */
    width:260px;height:100%;border-bottom:none;border-right:1px solid var(--line);
    overflow-y:auto;overflow-x:hidden;padding:14px 12px;
  }
  #pal-list{flex-direction:column;gap:16px}
  .pal-blocks{flex-direction:column}
  #editor{
    width:340px;height:100%;max-height:none;border-bottom:none;
    border-right:1px solid var(--line);padding:14px;
  }
  .script{min-height:0}
}

/* ============ ช่องโจทย์ไม่รู้จบ ============
   แยกเป็นคอลัมน์/ชั้นของตัวเอง ไม่ยัดเข้า #editor เพราะแผงสคริปต์มี min-height
   ถ้าไปเบียดกันจะดันเนื้อหาล้นออกนอกกล่องแล้วทับกัน */
#quest-dock{
  flex:none;background:var(--surface);border-bottom:1px solid var(--line);
  padding:10px 12px;max-height:40vh;overflow-y:auto;
}
#quest-dock:empty{display:none}
@media (min-width:1000px){
  #quest-dock{
    width:320px;height:100%;max-height:none;border-bottom:none;
    border-right:1px solid var(--line);padding:14px;
  }
}
@media (max-width:999px){ #quest-dock{order:4;max-height:none} }

/* ---------- แถบเลือกภารกิจ/สนาม ---------- */
.world-bar{
  position:absolute;left:12px;top:12px;z-index:6;
  display:flex;gap:6px;align-items:center;flex-wrap:wrap;
  background:rgba(255,255,255,.95);border:1px solid var(--line);
  border-radius:999px;padding:5px 8px;box-shadow:0 4px 14px rgba(25,26,36,.08);
  max-width:calc(100% - 24px);
}
.wb-select{
  font-family:inherit;font-size:.78rem;font-weight:600;color:var(--ink);
  border:1px solid var(--line);border-radius:999px;padding:5px 10px;background:var(--bg);
}
.wb-btn{
  font-family:inherit;font-size:.76rem;border:1px solid var(--line);
  border-radius:999px;padding:5px 11px;background:var(--surface);cursor:pointer;color:var(--ink);
}
.wb-btn:hover{background:var(--bg)}
.wb-goal{font-size:.73rem;color:var(--muted);padding:0 4px;max-width:280px}

/* ---------- แผงเซ็นเซอร์ ---------- */
.sensors{
  /* เลื่อนลงมากันชนกับปุ่มสลับภาษาที่มุมขวาบน */
  position:absolute;right:12px;top:54px;display:flex;gap:12px;flex-wrap:wrap;
  background:rgba(255,255,255,.95);border:1px solid var(--line);
  border-radius:14px;padding:8px 12px;font-size:.7rem;color:var(--muted);
  box-shadow:0 4px 14px rgba(25,26,36,.08);max-width:min(420px,calc(100% - 24px));
}
.sensor-group{display:flex;flex-direction:column;gap:3px}
.sensor-group b{font-size:.66rem;color:var(--ink);opacity:.65}
.sensor{display:flex;align-items:center;gap:4px}
.sensor .dot{
  width:9px;height:9px;border-radius:50%;background:#d6d3ca;
  border:1px solid rgba(0,0,0,.12);transition:background .1s;
}
.sensor.on .dot{background:#e2564a;box-shadow:0 0 6px rgba(226,86,74,.6)}
.sensor-val{display:flex;gap:4px;align-items:center;white-space:nowrap}
.sensor-val i{font-style:normal;font-weight:700;color:var(--ink);font-variant-numeric:tabular-nums}

/* ---------- ตัวออกแบบสนาม ---------- */
.edit-box{width:min(860px,100%)}
.edit-tools{
  display:flex;gap:6px;flex-wrap:wrap;padding:12px 18px;border-bottom:1px solid var(--line);
}
.tool-btn{
  display:flex;align-items:center;gap:6px;padding:6px 12px;border-radius:999px;
  border:1px solid var(--line);background:var(--surface);cursor:pointer;
  font-family:inherit;font-size:.78rem;color:var(--ink);
}
.tool-btn.is-active{border-color:var(--ink);background:var(--ink);color:#fff}
.tool-dot{width:12px;height:12px;border-radius:3px;flex:none}
.edit-grid-wrap{padding:16px 18px;overflow:auto;flex:1}
.edit-grid{display:grid;gap:2px;touch-action:none;width:max-content;margin:0 auto}
.edit-cell{
  width:34px;height:34px;border-radius:5px;background:#f0efe9;
  border:1px solid #e2e0d8;cursor:pointer;display:grid;place-items:center;
  font-size:.8rem;line-height:1;
}
.edit-cell:hover{outline:2px solid var(--brand)}

/* แถบสถานะต้องหลบแถบเลือกภารกิจที่มุมบนซ้าย */
.status{ top:58px; max-width:min(460px,calc(100% - 24px)); }
@media (max-width:760px){ .status{ top:92px } .sensors{ position:static;margin:8px 12px;max-width:none } }


/* ============ Responsive ============ */
/* แท็บเล็ตแนวตั้ง / มือถือ: เรียงเป็นชั้น สนามอยู่บนสุดให้เห็นผลทันที */
@media (max-width:999px){
  /* สามชั้นรวมกันสูงเกินจอ (46+32+42vh) — ต้องปล่อยให้ทั้งหน้าเลื่อนแนวตั้งได้
     ไม่งั้นแผงสคริปต์กับปุ่มส่งงานหลุดจอถาวร · ลากบล็อกบน canvas ไม่ชนกัน เพราะ canvas ตั้ง touch-action:none ไว้แล้ว */
  html,body{height:auto;min-height:100%;overflow-y:auto}
  body{flex-direction:column}
  #stage{order:1;min-height:46vh;flex:none;height:46vh}
  #palette{order:2;max-height:32vh}
  #editor{order:3;max-height:42vh}
  .world-bar{position:static;margin:8px 12px 0;border-radius:14px;justify-content:flex-start}
  .status{position:static;margin:8px 12px;max-width:none}
  .sensors{position:static;margin:0 12px 8px;max-width:none;border-radius:14px}
  .stage-foot{position:static;padding:0 12px 10px}
  .btn-run{flex:1 1 100%}
}
@media (max-width:560px){
  .blk{font-size:.76rem;padding:7px 9px}
  .pal-blocks{flex-direction:column}
  #pal-list{flex-direction:column;gap:12px}
  .stage-foot .btn{flex:1 1 calc(50% - 4px);min-width:0}
  .edit-cell{width:26px;height:26px;font-size:.7rem}
}

/* ---------- หน้าต่างตั้งค่าบอร์ด ---------- */
.btn-board{background:#134e4a;color:#fff}
.board-box{width:min(880px,100%)}
.board-pick{padding:14px 18px;border-bottom:1px solid var(--line);display:flex;flex-direction:column;gap:6px}
.mini-label{font-size:.74rem;font-weight:700;color:var(--muted)}
.form-sel{
  font-family:inherit;font-size:.86rem;padding:8px 12px;border:1px solid var(--line);
  border-radius:10px;background:var(--bg);color:var(--ink);max-width:340px;
}
.board-note{margin:0;font-size:.76rem;color:var(--muted);line-height:1.55}
.board-grid{padding:14px 18px;overflow-y:auto;flex:1;display:flex;flex-direction:column;gap:14px}
.board-group-title{font-size:.78rem;font-weight:700;color:var(--brand-ink);margin-bottom:6px}
.board-rows{display:grid;grid-template-columns:repeat(auto-fill,minmax(230px,1fr));gap:8px}
.board-row{
  display:flex;align-items:center;gap:8px;background:var(--bg);
  border:1px solid var(--line);border-radius:10px;padding:7px 10px;
}
.board-row.has-error{border-color:var(--danger);background:#fdf3f2}
.board-row label{flex:1;font-size:.78rem;min-width:0}
.board-row select{
  font-family:inherit;font-size:.78rem;padding:5px 8px;border:1px solid var(--line);
  border-radius:8px;background:#fff;color:var(--ink);min-width:88px;
}
.board-row .opt-tag{font-size:.66rem;color:var(--muted)}
#board-errors{color:var(--danger)}
#board-errors:empty::before{content:'ตรวจแล้วไม่พบปัญหา';color:var(--ok)}

/* ปุ่มปิดเสียง */
.btn.is-off{opacity:.55}
