:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --border: #dde2ec;
  --text: #1f2633;
  --muted: #667085;
  --accent: #2f6fde;
  --accent-ink: #ffffff;
  --run: #1f9d55;
  --run-hover: #198246;
  --err-bg: #fff1f0;
  --err: #b42318;
  --hint-bg: #fff8e6;
  --hint: #8a5a00;
  --code-font: 15px;
  --side-w: 42%;
  font-family: 'Sarabun', system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); display: flex; flex-direction: column; overflow: hidden; }

/* ---------- แถบด้านบน ---------- */
.topbar {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 8px 16px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; }
.logo { height: 48px; width: auto; display: block; }
.brand h1 { font-size: 18px; margin: 0; line-height: 1.2; }
.brand p { margin: 0; font-size: 13px; color: var(--muted); }
.controls { display: flex; gap: 10px; flex-wrap: wrap; }
.field { display: flex; flex-direction: column; font-size: 12px; color: var(--muted); gap: 2px; }
.field select {
  font: inherit; font-size: 14px; color: var(--text); padding: 5px 8px; min-width: 210px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
}
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
.btn {
  font: inherit; font-size: 14px; padding: 7px 12px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: #eef2fa; }
.btn.primary { background: var(--run); border-color: var(--run); color: #fff; font-weight: 700; padding: 7px 18px; }
.btn.primary:hover { background: var(--run-hover); }
.btn.primary:disabled { opacity: .6; cursor: progress; }
.btn:focus-visible, .mini:focus-visible, select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- พื้นที่หลัก ---------- */
main { flex: 1; display: flex; min-height: 0; }
#blocklyArea { flex: 1; position: relative; min-width: 0; }
#blocklyDiv { position: absolute; inset: 0; }
#splitter { width: 6px; cursor: col-resize; background: var(--border); flex: none; }
#splitter:hover, #splitter.dragging { background: var(--accent); }
#side { width: var(--side-w); min-width: 280px; display: flex; flex-direction: column; min-height: 0; background: var(--surface); }

.panel { display: flex; flex-direction: column; min-height: 0; }
.code-panel { flex: 1 1 50%; border-bottom: 1px solid var(--border); }
.out-panel { flex: 1 1 50%; }
.panel-head {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px;
  border-bottom: 1px solid var(--border); background: #fafbfd;
}
.panel-head h2 { font-size: 15px; margin: 0; }
.tools { margin-left: auto; display: flex; gap: 4px; }
.mini {
  font: inherit; font-size: 13px; padding: 3px 9px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.mini:hover { background: #eef2fa; }

#codePre {
  flex: 1; margin: 0; overflow: auto; border-radius: 0; background: #fbfcfe;
  font-family: 'JetBrains Mono', Consolas, monospace; font-size: var(--code-font); line-height: 1.5;
}
#codePre code { font-family: inherit; font-size: inherit; }
#codePre, #codePre code, #output { font-variant-ligatures: none; font-feature-settings: "liga" 0, "calt" 0; }
pre[class*="language-"].line-numbers { padding-left: 3.4em; }
.line-highlight { background: linear-gradient(to right, rgba(220, 38, 38, .18) 70%, rgba(220, 38, 38, 0)); }

.status { font-size: 13px; padding: 2px 8px; border-radius: 999px; background: #eef2fa; color: var(--muted); }
.status.ready { background: #e7f6ec; color: #1f7a45; }
.status.running, .status.loading { background: #eaf1ff; color: var(--accent); }
.status.error { background: var(--err-bg); color: var(--err); }

#filesBox { padding: 8px 12px; border-bottom: 1px solid var(--border); font-size: 14px; background: #fafbfd; }
#filesBox ul { margin: 4px 0 0; padding-left: 18px; }
#filesBox code { font-family: 'JetBrains Mono', monospace; font-size: 13px; }

#output {
  flex: 1; overflow: auto; padding: 10px 12px;
  font-family: 'JetBrains Mono', Consolas, monospace; font-size: var(--code-font);
}
#output .placeholder { color: var(--muted); font-family: 'Sarabun', sans-serif; }
#output pre { margin: 0; white-space: pre-wrap; word-break: break-word; font: inherit; }
#output pre.stderr { color: #9a6700; }
#output .err { background: var(--err-bg); color: var(--err); border-radius: 8px; padding: 8px 10px; margin: 6px 0; }
#output .hint { background: var(--hint-bg); color: var(--hint); border-radius: 8px; padding: 8px 10px; margin: 6px 0; font-family: 'Sarabun', sans-serif; }
#output img { max-width: 100%; display: block; margin: 8px 0; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
#output .table-wrap { overflow-x: auto; margin: 8px 0; }
#output table.df { border-collapse: collapse; font-size: 0.85em; font-family: 'Sarabun', sans-serif; }
#output table.df th, #output table.df td { border: 1px solid var(--border); padding: 3px 8px; text-align: right; }
#output table.df thead th { background: #eef2fa; }
#output table.df tbody tr:nth-child(even) { background: #f8f9fc; }
#output .sep { border: none; border-top: 1px dashed var(--border); margin: 10px 0; }

#toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px);
  background: #1f2633; color: #fff; padding: 8px 16px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: .2s;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Blockly ---------- */
.blocklyTreeLabel { font-family: 'Sarabun', sans-serif !important; font-size: 15px !important; }
.blocklyToolboxCategory { padding: 2px 0; }

/* ---------- จอเล็ก ---------- */
@media (max-width: 900px) {
  body { overflow: auto; }
  main { flex-direction: column; }
  #blocklyArea { flex: none; height: 62vh; }
  #splitter { display: none; }
  #side { width: auto; min-width: 0; }
  .code-panel, .out-panel { min-height: 300px; }
  .field select { min-width: 0; width: 100%; }
  .controls, .field { width: 100%; }
}

/* ---------- กล่องแก้ไข CSV ---------- */
#csvDialog { border: 1px solid var(--border); border-radius: 12px; padding: 16px; width: min(560px, calc(100vw - 32px)); }
#csvDialog::backdrop { background: rgba(15, 23, 42, .35); }
#csvDialog h3 { margin: 0 0 4px; }
#csvDialog p { margin: 0 0 8px; color: var(--muted); font-size: 14px; }
#csvDialog textarea { width: 100%; font-family: 'JetBrains Mono', monospace; font-size: 14px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; }
.dlg-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 10px; }
