:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --border: #dde3ec;
  --text: #1d2433;
  --muted: #6b7489;
  --accent: #0b7285;
  --pi: #c51a4a;
  --accent-ink: #ffffff;
  --warn-bg: #fff4e5;
  --warn-ink: #8a4b00;
  --code-bg: #1f232b;
  --radius: 10px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'IBM Plex Sans Thai', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 8px 14px; background: var(--surface); border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.logo { height: 38px; width: auto; display: block; flex: none; }
.title { font-weight: 600; font-size: 17px; line-height: 1.1; }
.subtitle { font-size: 12px; color: var(--muted); }
.actions { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; }
button, select, input {
  font: inherit; font-size: 14px; color: var(--text);
  border: 1px solid var(--border); background: var(--surface);
  border-radius: 8px; padding: 6px 11px; cursor: pointer;
}
input { cursor: text; }
button:hover, select:hover { border-color: #b7c2d3; background: #f8fafc; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { background: #095c6b; }
button:disabled { opacity: .5; cursor: default; }

/* ---------- layout ---------- */
.layout { flex: 1; display: flex; min-height: 0; }
.workspace { flex: 1 1 60%; position: relative; min-width: 300px; }
#blocklyDiv { position: absolute; inset: 0; }
.splitter { width: 6px; cursor: col-resize; background: var(--border); flex: none; }
.splitter:hover, .splitter.drag { background: var(--accent); }
.panel {
  flex: 0 0 38%; min-width: 280px; display: flex; flex-direction: column;
  background: var(--code-bg); color: #e6e6e6;
}
.tabs { display: flex; background: #171a20; }
.tab {
  flex: 1; border: 0; border-radius: 0; background: transparent; color: #a9b1c2; padding: 10px;
  border-bottom: 2px solid transparent;
}
.tab:hover { background: #22262e; color: #fff; }
.tab.active { color: #fff; border-bottom-color: #3bc9db; }
.tabpane { display: none; flex: 1; min-height: 0; flex-direction: column; }
.tabpane.active { display: flex; }

.code { margin: 0; flex: 1; overflow: auto; padding: 0; }
.code code {
  display: block; padding: 14px 16px; min-height: 100%;
  font-family: 'JetBrains Mono', Consolas, monospace; font-size: 13px; line-height: 1.55;
  background: transparent !important; white-space: pre;
}
.warnings, .libs { font-size: 13px; padding: 8px 14px; line-height: 1.5; }
.warnings { background: var(--warn-bg); color: var(--warn-ink); }
.warnings div::before { content: '⚠ '; }
.libs { background: #243040; color: #cfe3ff; }
.libs b { color: #fff; }
.libs code { background: #33445a; padding: 1px 6px; border-radius: 5px; font-size: 12px; }


.proj-name { width: 120px; }
dialog.small { max-width: 400px; }
dialog.wide { max-width: 760px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; }
dialog code, .hint code { background: #eef3f8; padding: 1px 6px; border-radius: 5px; font-size: 13px; }

/* ---------- run / stop ---------- */
button.run { background: #2b8a3e; color: #fff; border-color: #2b8a3e; font-weight: 600; }
button.run:hover { background: #237032; }
button.run.busy { background: #5c940d; border-color: #5c940d; }
button.stop { background: #c92a2a; color: #fff; border-color: #c92a2a; font-weight: 600; }
button.stop:hover { background: #a61e1e; }
button.stop:disabled { background: var(--surface); color: var(--muted); border-color: var(--border); }
.tab-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: transparent; margin-left: 4px; vertical-align: middle; }
.tab-dot.on { background: #40c057; box-shadow: 0 0 0 3px rgba(64,192,87,.25); }

/* ---------- Pi connection ---------- */
.pi-status { display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: #171a20; font-size: 13px; color: #cfd6e3; }
.pi-status .dot { width: 10px; height: 10px; border-radius: 50%; background: #868e96; flex: none; }
.pi-status.ok .dot { background: #40c057; }
.pi-status.bad .dot { background: #fa5252; }
.pi-status span:nth-child(2) { flex: 1; min-width: 0; }
.small-btn { font-size: 12px; padding: 3px 8px; background: #2a2f39; color: #e6e6e6; border-color: #3a404c; border-radius: 7px; }
.small-btn:hover { background: #343a46; border-color: #4a5160; }
.pi-connect { display: flex; flex-wrap: wrap; gap: 8px; align-items: flex-end; padding: 10px 12px; background: #1b1f26; border-top: 1px solid #262b33; }
.pi-connect label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: #a9b1c2; flex: 1 1 160px; }
.pi-connect input { background: #2a2f39; color: #e6e6e6; border-color: #3a404c; width: 100%; }
.pi-connect .hint { flex-basis: 100%; margin: 0; font-size: 12px; color: #a9b1c2; line-height: 1.5; }
.pi-connect .hint code { background: #2a2f39; color: #e6e6e6; }
.pi-connect .hint.warn { color: #ffd8a8; }
.pi-info { display: flex; flex-wrap: wrap; gap: 4px 6px; padding: 8px 12px; background: #1b1f26; border-top: 1px solid #262b33; font-size: 12px; }
.pi-info span { background: #2a2f39; color: #cfd6e3; border-radius: 999px; padding: 2px 9px; }
.pi-info span.no { color: #868e96; text-decoration: line-through; }
.pi-info span.yes { color: #b2f2bb; }

.console {
  flex: 1; margin: 0; overflow: auto; padding: 12px 14px; white-space: pre-wrap; word-break: break-word;
  font-family: 'JetBrains Mono', Consolas, monospace; font-size: 13px; line-height: 1.5; color: #e6e6e6; min-height: 120px;
}
.console .sys { color: #74c0fc; }
.console .err { color: #ffa8a8; }
.console .inp { color: #ffe066; }
.run-input { display: flex; gap: 6px; padding: 8px; background: #171a20; }
.run-input input { flex: 1; min-width: 100px; background: #2a2f39; color: #e6e6e6; border-color: #3a404c; }
.run-input button { background: #2a2f39; color: #e6e6e6; border-color: #3a404c; }

.files { background: #1b1f26; border-top: 1px solid #262b33; font-size: 13px; color: #cfd6e3; max-height: 40%; overflow: auto; }
.files summary { padding: 8px 12px; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.files-bar { padding: 0 12px 8px; }
.upload-btn { display: inline-block; cursor: pointer; }
.file-list { list-style: none; margin: 0; padding: 0 12px 10px; }
.file-list li { display: flex; gap: 8px; align-items: center; padding: 4px 0; border-bottom: 1px solid #262b33; }
.file-list a { color: #a5d8ff; text-decoration: none; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-list a:hover { text-decoration: underline; }
.file-list small { color: #868e96; white-space: nowrap; }

/* ---------- camera ---------- */
.cam-bar { display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: #171a20; font-size: 13px; color: #cfd6e3; }
.cam-bar span { flex: 1; }
.cam-view { flex: 1; display: flex; align-items: center; justify-content: center; background: #111318; min-height: 0; padding: 10px; }
.cam-view img { max-width: 100%; max-height: 100%; border-radius: 8px; }
.cam-empty { color: #5c6370; font-size: 15px; }

/* ---------- GPIO header dialog ---------- */
.pin-header { display: grid; grid-template-columns: 1fr auto auto 1fr; gap: 3px 6px; align-items: center; font-size: 12.5px; max-height: 60vh; overflow: auto; padding: 4px; }
.pin-header .lbl { padding: 3px 8px; border-radius: 6px; background: #f1f3f5; white-space: nowrap; }
.pin-header .lbl.l { text-align: right; }
.pin-header .num { width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center; font-weight: 600; font-size: 11px; color: #fff; }
.pin-header .gpio { background: #e3f4f6; color: #0b5563; font-weight: 600; }
.pin-header .pwr5 { background: #ffe3e3; color: #a61e1e; }
.pin-header .pwr3 { background: #fff3bf; color: #8a5a00; }
.pin-header .gnd { background: #343a40; color: #fff; }
.pin-header .other { background: #f1f3f5; color: #868e96; }
.n-gpio { background: #0b7285; } .n-pwr5 { background: #e03131; } .n-pwr3 { background: #f08c00; } .n-gnd { background: #212529; } .n-other { background: #adb5bd; }

/* ---------- dialog / toast ---------- */
dialog {
  border: 0; border-radius: 14px; padding: 22px 26px; max-width: 560px; width: calc(100% - 32px);
  box-shadow: 0 20px 60px rgba(0,0,0,.3); color: var(--text); background: var(--surface);
}
dialog::backdrop { background: rgba(15, 20, 30, .45); }
dialog h2 { margin-top: 0; font-size: 19px; }
dialog li { margin-bottom: 6px; line-height: 1.55; }
.muted { color: var(--muted); font-size: 13px; }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #1d2433; color: #fff; padding: 9px 18px; border-radius: 999px; font-size: 14px;
  box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 100;
}


/* Blockly tweaks */
.blocklyToolboxDiv { background: #eef1f6 !important; font-family: 'IBM Plex Sans Thai', sans-serif; }
.blocklyTreeLabel { font-family: 'IBM Plex Sans Thai', sans-serif !important; font-size: 14px !important; }
.blocklyText, .blocklyHtmlInput { font-family: 'IBM Plex Sans Thai', sans-serif !important; }

@media (max-width: 860px) {
  body { overflow: auto; }
  .layout { flex-direction: column; }
  .workspace { flex: none; height: 65vh; min-width: 0; }
  .splitter { display: none; }
  .panel { flex: none; height: 60vh; min-width: 0; }
  .subtitle { display: none; }
}
.libs .muted-dark { color: #9fb3cc; font-size: 12px; margin-top: 3px; }
