:root {
  --bg: #0b0e14; --panel: #131824; --panel2: #1a2130; --line: #273044;
  --text: #e2e8f0; --muted: #94a3b8; --accent: #3b82f6; --ok: #22c55e; --warn: #f59e0b; --err: #ef4444;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 15px/1.5 'Sarabun', system-ui, sans-serif;
  display: grid; grid-template-rows: auto 1fr 170px; height: 100dvh;
}
button, select, input { font: inherit; color: inherit; }
button, select {
  background: var(--panel2); border: 1px solid var(--line); border-radius: 8px;
  padding: 6px 12px; cursor: pointer;
}
button:hover { border-color: var(--accent); }
button:disabled { opacity: .4; cursor: not-allowed; }

header {
  display: flex; gap: 8px; align-items: center; flex-wrap: wrap;
  padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; font-size: 18px; margin-right: 8px; }
.brand small { color: var(--warn); font-weight: 600; }
.run { background: #15803d; border-color: #16a34a; font-weight: 700; }
.run.stop { background: #b91c1c; border-color: #ef4444; }

main { display: grid; grid-template-columns: 150px 1fr 340px; min-height: 0; }
#palette { background: var(--panel); border-right: 1px solid var(--line); padding: 10px; overflow: auto; }
#palette h3, #side h3 { margin: 0 0 8px; font-size: 14px; color: var(--muted); font-weight: 600; }
#partBtns { display: flex; flex-direction: column; gap: 6px; }
#partBtns button { text-align: left; font-size: 13px; line-height: 1.3; }

#stage { position: relative; min-height: 0; overflow: hidden;
  background-color: #0e121a;
  background-image: radial-gradient(#1f2737 1px, transparent 1px); background-size: 20px 20px; }
#board { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; }
.float { position: absolute; top: 10px; left: 10px; display: flex; flex-direction: column; gap: 8px; }
.sensor { background: rgba(19,24,36,.94); border: 1px solid var(--line); border-radius: 10px; padding: 6px 10px; width: 260px; max-height: calc(100% - 70px); overflow: auto; }
.sensor b { font-size: 13px; }
.sensor .row { display: grid; grid-template-columns: 70px 1fr auto; gap: 6px; align-items: center; font-size: 12px; line-height: 1.3; }
.float { max-height: 100%; }
.sensor input[type=range] { width: 100%; }
.zoom { position: absolute; right: 10px; bottom: 10px; display: flex; gap: 4px; }
.zoom button { padding: 4px 10px; }
#hint { position: absolute; left: 10px; bottom: 10px; right: 170px; font-size: 12px; color: var(--muted); pointer-events: none; }

#side { background: var(--panel); border-left: 1px solid var(--line); overflow: auto; padding: 10px; }
.tabs { display: flex; gap: 4px; margin-bottom: 10px; }
.tabs button { flex: 1; font-size: 13px; padding: 6px 4px; }
.tabs button.on { background: var(--accent); border-color: var(--accent); }
#badge { background: var(--err); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; }
#badge:empty { display: none; }
.tab { display: none; }
.tab.on { display: block; }
.chk { padding: 8px 10px; border-radius: 8px; margin-bottom: 6px; font-size: 14px; border-left: 4px solid; background: var(--panel2); }
.chk.err { border-color: var(--err); }
.chk.warn { border-color: var(--warn); }
.chk.ok { border-color: var(--ok); }
.chk.info { border-color: var(--accent); color: var(--muted); }
.desc { color: var(--muted); font-size: 14px; margin: 0 0 10px; }
.field { display: grid; gap: 2px; margin-bottom: 8px; font-size: 14px; }
.field input, .field select { background: var(--panel2); border: 1px solid var(--line); border-radius: 8px; padding: 5px 8px; }
#code { background: #0b0e14; border: 1px solid var(--line); border-radius: 8px; padding: 10px; font-size: 12px; overflow: auto; max-height: 60vh; }
#props { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 10px; }
#props:empty { display: none; }
.swatches { display: flex; gap: 6px; margin: 6px 0 10px; }
.swatches button { width: 28px; height: 28px; padding: 0; border-radius: 50%; }
.danger { border-color: var(--err); color: #fca5a5; }

footer { background: #07090d; border-top: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.sbar { display: flex; gap: 12px; align-items: center; padding: 4px 12px; font-size: 13px; color: var(--muted); background: var(--panel); }
.sbar span { color: var(--ok); margin-right: auto; }
.sbar button { padding: 2px 10px; font-size: 13px; }
#serial { margin: 0; padding: 6px 12px; overflow: auto; flex: 1; font: 13px/1.45 Consolas, 'Courier New', monospace; color: #86efac; }

/* SVG */
.pcb { fill: #111318; stroke: #2563eb; stroke-width: 3; }
.ant { fill: none; stroke: #94a3b8; stroke-width: 3; }
.chip { fill: #6b7280; stroke: #9ca3af; }
.chipt { fill: #e5e7eb; font: 700 20px sans-serif; text-anchor: middle; }
.usb { fill: #9ca3af; }
.plabel { fill: #cbd5e1; font: 9px sans-serif; }
.tiny { fill: #94a3b8; font: 10px 'Sarabun', sans-serif; }
.dht { fill: #f1f5f9; stroke: #cbd5e1; }
.dhtt { fill: #111827; font: 700 12px sans-serif; text-anchor: middle; }
.leg { stroke: #cbd5e1; stroke-width: 3; }
.btnbody { fill: #1f2937; stroke: #475569; }
.cap { fill: #dc2626; stroke: #7f1d1d; stroke-width: 2; cursor: pointer; }
.cap.down { fill: #7f1d1d; }
.relaypcb { fill: #0e7490; stroke: #22d3ee; }
.waves path { fill: none; stroke: #facc15; stroke-width: 2.5; }
.spin { animation: spin .6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.part { cursor: grab; }
.selbox { fill: none; stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; }
.wire { fill: none; stroke-width: 4; stroke-linejoin: round; stroke-linecap: round; pointer-events: none; }
.wire.sel { stroke-width: 6; filter: drop-shadow(0 0 4px #fff); }
.wire.ghost { stroke: #fff; stroke-dasharray: 6 6; stroke-width: 2; }
.hit { fill: none; stroke: transparent; stroke-width: 14; cursor: pointer; }
.pin { fill: #e2e8f0; stroke: #0b0e14; stroke-width: 2; cursor: crosshair; }
.pin:hover { fill: #facc15; }
.pin.on { fill: #4ade80; }
.pin.pend { fill: #facc15; stroke: #fff; }

@media (max-width: 860px) {
  body { display: block; height: auto; }
  main { display: flex; flex-direction: column; }
  #palette { border: 0; border-bottom: 1px solid var(--line); }
  #partBtns { flex-direction: row; overflow-x: auto; }
  #partBtns button { flex: 0 0 auto; }
  #stage { height: 62vh; display: flex; flex-direction: column; }
  #board { flex: 1; min-height: 0; }
  .float { position: static; padding: 8px 8px 0; order: -1; }
  .sensor { width: auto; }
  .sbar { flex-wrap: wrap; }
  #serialIn { width: 100%; min-width: 0; }
  #serialSendBox { flex: 1 1 100%; }
  #hint { display: none; }
  #side { border: 0; }
  footer { height: 200px; }
}

/* ระดับ B: บอร์ด Uno + ช่องเขียนโค้ด */
.unopcb { fill: #00878f; stroke: #5eead4; stroke-width: 2; }
.unot { fill: #e0f2fe; font: 700 34px sans-serif; }
#codeEdit { width: 100%; min-height: 58vh; margin-top: 8px; resize: vertical; background: #0b0e14; color: #e2e8f0;
  border: 1px solid var(--line); border-radius: 8px; padding: 10px; font: 13px/1.5 Consolas, 'Courier New', monospace; tab-size: 2; }
.cerr { white-space: pre-wrap; font: 12px/1.4 Consolas, monospace; color: #fca5a5; background: #0b0e14; padding: 8px; border-radius: 6px; margin: 6px 0 0; max-height: 40vh; overflow: auto; }
#speed { color: var(--warn); font-style: normal; margin-left: 8px; }
#serialSendBox { display: flex; gap: 4px; }
#serialSendBox[hidden] { display: none; }
#serialIn { background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 2px 8px; width: 220px; }
.meter { font-size: 13px; background: var(--panel2); border-radius: 8px; padding: 6px 10px; margin: 8px 0; line-height: 1.7; }
.meter:empty { display: none; }
#props select { width: 100%; }

/* จอแอปมือถือจำลอง */
.phone { background: #f8fafc; color: #0f172a; border: 8px solid #0f172a; border-radius: 26px; padding: 10px 12px 14px; max-width: 320px; margin: 0 auto 10px; }
.pbar { display: flex; justify-content: space-between; font-size: 12px; font-weight: 600; }
.purl { font-size: 11px; color: #64748b; background: #e2e8f0; border-radius: 6px; padding: 2px 8px; margin: 6px 0 4px; }
.phone h4 { margin: 10px 0 4px; font-size: 13px; color: #334155; }
.pgrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.pcard { background: #fff; border: 1px solid #e2e8f0; border-radius: 10px; padding: 6px 4px; text-align: center; }
.pcard small { display: block; font-size: 11px; color: #64748b; }
.pcard b { font-size: 18px; }
.pcard i { font-style: normal; font-size: 11px; color: #64748b; margin-left: 2px; }
.pcard.bad b { color: #dc2626; }
.tabs button[hidden] { display: none; }
