:root {
  --bg: #0f1419;
  --surface: #161d24;
  --surface-2: #1d262f;
  --surface-3: #25303b;
  --border: #2c3946;
  --text: #e6edf3;
  --text-dim: #9aa8b5;
  --text-faint: #6b7a88;
  --accent: #2dd4bf;
  --accent-strong: #14b8a6;
  --accent-ink: #04201c;
  --danger: #f87171;
  --warn: #fbbf24;
  --ok: #4ade80;
  --node-body: #262c33;
  --node-head: #353d47;
  --node-text: #e8edf2;
  --graph-bg: #1b1f24;
  --graph-grid: #262b31;
  --radius: 10px;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f3f5f7;
  --surface-3: #e7ebef;
  --border: #d6dde3;
  --text: #17202a;
  --text-dim: #4d5b68;
  --text-faint: #7d8a96;
  --accent: #0d9488;
  --accent-strong: #0f766e;
  --accent-ink: #ffffff;
  --danger: #dc2626;
  --warn: #b45309;
  --ok: #15803d;
  --graph-bg: #20252b;
  --graph-grid: #2a3037;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--sans);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); }
code, pre, textarea.code { font-family: var(--mono); }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--text-dim); margin: 0 0 12px; font-weight: 600; }
h3 { font-size: 13px; margin: 0 0 8px; font-weight: 600; }
[hidden] { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; }
.brand h1 { margin: 0; font-size: 15px; font-weight: 700; }
.brand small { display: block; color: var(--text-dim); font-size: 12px; }
.actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.btn {
  font: 500 13px var(--sans);
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-3); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn.primary:hover { background: var(--accent-strong); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.icon { padding: 6px 10px; font-size: 15px; line-height: 1; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--text-dim); }
.btn.ghost:hover { background: var(--surface-3); color: var(--text); }
.btn.danger:hover { color: var(--danger); }
.btn:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .tab:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

select, input, textarea {
  font: 13px var(--sans);
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 6px 8px;
  width: 100%;
  min-width: 0;
}
input[type="checkbox"] { width: 16px; height: 16px; padding: 0; margin: 2px 0 0; flex: none; accent-color: var(--accent); cursor: pointer; }
select { cursor: pointer; }
.select-wrap select { width: auto; padding: 7px 10px; }
input.invalid, textarea.invalid { border-color: var(--danger); }
input:disabled { opacity: .5; }

.menu { position: relative; }
.menu-list {
  position: absolute; top: calc(100% + 4px); left: 0; z-index: 30;
  min-width: 210px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 4px;
  box-shadow: 0 10px 30px rgba(0,0,0,.35);
}
.menu-list.right { left: auto; right: 0; }
.menu-list button {
  display: block; width: 100%; text-align: left;
  font: 13px var(--sans); color: var(--text);
  background: none; border: 0; border-radius: 6px; padding: 7px 10px; cursor: pointer;
}
.menu-list button:hover { background: var(--surface-3); }
.menu-list hr { border: 0; border-top: 1px solid var(--border); margin: 4px; }

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(260px, 300px) minmax(420px, 1fr) minmax(380px, 1fr);
  gap: 14px;
  padding: 14px 16px;
  align-items: start;
}
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
}
.sidebar { display: flex; flex-direction: column; gap: 14px; }
.card-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.card-head h2 { margin: 0; }

.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.field > span { font-size: 12px; color: var(--text-dim); font-weight: 500; }
.field em { font-style: normal; color: var(--text-faint); font-weight: 400; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.more summary { cursor: pointer; color: var(--text-dim); font-size: 12px; margin-bottom: 8px; }

.node-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.node-list li {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 9px; border-radius: 8px; cursor: pointer;
  border: 1px solid transparent;
}
.node-list li:hover { background: var(--surface-2); }
.node-list li.active { background: var(--surface-2); border-color: var(--accent); }
.node-list .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); flex: none; }
.node-list .dot.action { background: #f6ad55; border-radius: 2px; transform: rotate(45deg); }
.node-list .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.node-list .name small { display: block; color: var(--text-faint); font-size: 11px; font-family: var(--mono); }
.node-list .err { color: var(--danger); font-weight: 700; }

.checks ul { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; font-size: 12.5px; }
.checks li { display: flex; gap: 7px; align-items: flex-start; }
.checks li::before { flex: none; font-weight: 700; width: 14px; text-align: center; }
.checks li.error::before { content: "\2715"; color: var(--danger); }
.checks li.warn::before { content: "!"; color: var(--warn); }
.checks li.ok::before { content: "\2713"; color: var(--ok); }

/* ---------- Editor ---------- */
.editor { display: flex; flex-direction: column; gap: 16px; }
.editor-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.editor-head h2 { margin: 0; }
.editor-head .btns { display: flex; gap: 6px; }
.node-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }
.node-grid .field { margin: 0; }
.node-grid .wide { grid-column: 1 / -1; }
.toggles { display: flex; flex-wrap: wrap; gap: 8px; }
.toggle {
  display: flex; align-items: flex-start; gap: 8px;
  flex: 1 1 220px;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; cursor: pointer;
}
.toggle input { margin-top: 2px; }
.toggle b { display: block; font-size: 13px; }
.toggle small { color: var(--text-dim); font-size: 12px; }

.attr-section h3 { display: flex; align-items: center; justify-content: space-between; }
.attr-section h3 .count { color: var(--text-faint); font-weight: 400; margin-left: 6px; }
.attr-list { display: flex; flex-direction: column; gap: 6px; }
.attr {
  display: grid;
  grid-template-columns: 12px minmax(90px, 1.1fr) minmax(110px, 1fr) minmax(80px, 1fr) auto;
  grid-template-areas:
    "pin name type def btns"
    ".   label desc desc desc";
  gap: 5px 6px;
  align-items: center;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 8px;
}
.attr.fixed { opacity: .7; }
.attr .pin { grid-area: pin; width: 10px; height: 10px; border-radius: 50%; }
.attr .pin.exec { border-radius: 1px; clip-path: polygon(0 0, 100% 50%, 0 100%); }
.attr .a-name { grid-area: name; font-family: var(--mono); font-size: 12.5px; }
.attr .a-type { grid-area: type; font-size: 12.5px; }
.attr .a-def { grid-area: def; font-family: var(--mono); font-size: 12.5px; }
.attr .a-label { grid-area: label; font-size: 12px; }
.attr .a-desc { grid-area: desc; font-size: 12px; }
.attr .btns { grid-area: btns; display: flex; gap: 2px; }
.attr .btns .btn { padding: 3px 6px; font-size: 12px; }
.attr input, .attr select { padding: 4px 6px; }
.empty { color: var(--text-faint); font-size: 12.5px; padding: 6px 2px; }

.code-block { border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.code-block .ctx {
  font: 12.5px/1.5 var(--mono); color: var(--text-faint);
  padding: 6px 10px; white-space: pre; overflow-x: auto; background: var(--surface-3);
}
.code-block textarea.code {
  display: block; width: 100%; border: 0; border-radius: 0;
  background: transparent; resize: vertical;
  font: 12.5px/1.5 var(--mono);
  padding: 8px 10px;
  tab-size: 4;
  white-space: pre; overflow-x: auto;
}
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin: 6px 0 4px; }
.chip {
  font: 11.5px var(--mono); color: var(--text-dim);
  background: var(--surface-3); border: 1px solid var(--border); border-radius: 999px; padding: 2px 8px; cursor: pointer;
}
.chip:hover { color: var(--text); border-color: var(--accent); }
.sub { color: var(--text-dim); font-size: 12px; margin: 2px 0 6px; }

/* ---------- Preview ---------- */
.preview { position: sticky; top: 70px; max-height: calc(100vh - 84px); overflow: auto; }
.tabs { display: flex; align-items: center; gap: 2px; border-bottom: 1px solid var(--border); margin: -4px -4px 12px; flex-wrap: wrap; }
.tab-tool { margin-left: auto; display: inline-flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.tab {
  font: 500 13px var(--sans); color: var(--text-dim);
  background: none; border: 0; border-bottom: 2px solid transparent;
  padding: 8px 12px; cursor: pointer;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.hint { color: var(--text-dim); font-size: 12.5px; margin: 0 0 10px; }
.panel-title { margin-top: 16px; }

.graph-canvas {
  background-color: var(--graph-bg);
  background-image:
    linear-gradient(var(--graph-grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--graph-grid) 1px, transparent 1px);
  background-size: 20px 20px;
  border-radius: 8px;
  padding: 22px 16px;
  display: flex; justify-content: center; overflow-x: auto;
}
.graph-canvas svg { max-width: 100%; height: auto; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; margin-top: 8px; font-size: 11.5px; color: var(--text-dim); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.property-panel {
  background: #23272c; color: #d3d8dd; border-radius: 8px; padding: 6px 0; font-size: 12.5px;
  border: 1px solid #343a41;
}
.pp-group { padding: 4px 10px; font-weight: 600; color: #aeb6be; background: #2b3036; margin-bottom: 4px; }
.pp-row { display: grid; grid-template-columns: 40% 1fr; align-items: center; gap: 8px; padding: 3px 10px; }
.pp-row label { color: #b9c1c8; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pp-fields { display: flex; gap: 4px; }
.pp-field { flex: 1; background: #1a1d21; border: 1px solid #3a4048; border-radius: 3px; padding: 2px 6px; font: 12px var(--mono); color: #e1e5e9; min-width: 0; overflow: hidden; }
.pp-check { width: 14px; height: 14px; border: 1px solid #6b747d; border-radius: 2px; background: #1a1d21; display: grid; place-items: center; font-size: 11px; color: #2dd4bf; }
.pp-empty { padding: 6px 10px; color: #7d8790; }

.files { display: grid; grid-template-columns: minmax(150px, 38%) 1fr; gap: 10px; min-height: 360px; }
.file-tree { list-style: none; margin: 0; padding: 0; font: 12px var(--mono); overflow: auto; }
.file-tree li { padding: 3px 6px; border-radius: 5px; cursor: pointer; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: var(--text-dim); }
.file-tree li.dir { cursor: default; color: var(--text-faint); }
.file-tree li.file:hover { background: var(--surface-2); color: var(--text); }
.file-tree li.active { background: var(--surface-3); color: var(--text); }
.file-view { min-width: 0; display: flex; flex-direction: column; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.file-view-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px; background: var(--surface-2); border-bottom: 1px solid var(--border); }
.file-tools { display: flex; gap: 6px; flex: none; }
.file-view-head code { font-size: 12px; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-view pre { margin: 0; flex: 1; overflow: auto; max-height: 60vh; background: #0d1117; }
.file-view pre code { display: block; padding: 10px 12px; font-size: 12px; line-height: 1.5; color: #e6edf3; background: transparent; }

.use ol { padding-left: 20px; margin: 0; display: flex; flex-direction: column; gap: 12px; }
.use li p { margin: 2px 0 6px; color: var(--text-dim); }
.use pre { background: var(--surface-2); border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px; overflow-x: auto; font-size: 12px; margin: 4px 0; }
.use .versions { width: 100%; border-collapse: collapse; margin-top: 16px; font-size: 12.5px; }
.use .versions th, .use .versions td { text-align: left; padding: 5px 6px; border-bottom: 1px solid var(--border); }
.use .versions th { color: var(--text-dim); font-weight: 500; }

/* ---------- Footer / toasts ---------- */
.footer { padding: 14px 16px 22px; color: var(--text-dim); font-size: 12.5px; text-align: center; }
.footer p { margin: 4px 0; }
.footer .fine { color: var(--text-faint); font-size: 11.5px; }
.toast-area { position: fixed; bottom: 16px; right: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; max-width: min(420px, calc(100vw - 32px)); }
.toast {
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: 8px; padding: 10px 12px; box-shadow: 0 8px 24px rgba(0,0,0,.35); font-size: 13px;
  white-space: pre-line;
}
.toast.error { border-left-color: var(--danger); }
.toast.warn { border-left-color: var(--warn); }

.drop-overlay {
  position: fixed; inset: 0; z-index: 60; display: grid; place-items: center;
  background: rgba(15, 20, 25, .8); color: #fff; font-size: 18px; border: 3px dashed var(--accent);
}

/* ---------- About / FAQ ---------- */
.about { padding: 8px 16px 0; }
.about-inner { max-width: 1100px; margin: 0 auto; display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
.about h2 { margin-bottom: 8px; }
.about p, .about li { color: var(--text-dim); font-size: 13px; margin: 0 0 6px; }
.about ul { padding-left: 18px; margin: 0; }
.about details { border-top: 1px solid var(--border); padding: 7px 0; }
.about summary { cursor: pointer; font-weight: 500; font-size: 13px; }
.about details p { margin-top: 6px; }

/* ---------- ROS / Python cards ---------- */
.ros-box, .python-box { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.ros-box h3, .python-box h3 { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.python-box h3 code { font-weight: 400; color: var(--text-dim); font-size: 12px; }
.python-box .btns { display: flex; gap: 6px; }
.python-box.kept { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); }
.python-box .sub code, .ros-box .sub code { font-size: 11.5px; }
.steps { margin: 4px 0 0; padding-left: 20px; color: var(--text-dim); font-size: 12.5px; display: flex; flex-direction: column; gap: 3px; }
.steps b { color: var(--text); font-weight: 600; }

/* ---------- Code wrap ---------- */
.file-view.wrap pre code { white-space: pre-wrap; word-break: break-word; }
.btn[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }

/* ---------- Full screen preview ---------- */
body.has-fullscreen { overflow: hidden; }
.preview.fullscreen {
  position: fixed; inset: 0; z-index: 70;
  max-height: none; height: 100dvh;
  border-radius: 0; border: 0;
  padding: 12px 16px;
  display: flex; flex-direction: column;
}
.preview.fullscreen .tab-panel:not([hidden]) { flex: 1; min-height: 0; overflow: auto; }
.preview.fullscreen .files { height: 100%; min-height: 0; }
.preview.fullscreen .file-view pre { max-height: none; }
.preview.fullscreen .file-tree { max-height: none; }
.preview.fullscreen .graph-canvas { min-height: 45vh; align-items: center; }
.preview.fullscreen .graph-canvas svg { width: min(100%, 760px); }
.preview.fullscreen .use { max-width: 900px; margin: 0 auto; }

/* ---------- Responsive ---------- */
/* Panels adapt to their own width (container queries), so they work in any column layout. */
.preview, .editor { container-type: inline-size; }

@container (max-width: 640px) {
  .files { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .file-tree { max-height: 190px; border: 1px solid var(--border); border-radius: 8px; padding: 4px; }
  .preview.fullscreen .file-tree { max-height: 28vh; }
}
@container (max-width: 560px) {
  .node-grid { grid-template-columns: 1fr; }
  .attr {
    grid-template-columns: 12px 1fr 1fr auto;
    grid-template-areas:
      "pin name name btns"
      ".   type def def"
      ".   label desc desc";
  }
}

/* Laptop / small desktop: preview moves under the editor */
@media (max-width: 1280px) {
  .layout { grid-template-columns: minmax(250px, 290px) 1fr; }
  .preview:not(.fullscreen) { grid-column: 1 / -1; position: static; max-height: none; }
}

/* Tablet portrait and phones: one column */
@media (max-width: 960px) {
  .layout { grid-template-columns: 1fr; padding: 12px 16px; }
  .sidebar { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); align-items: start; }
}

/* Phones */
@media (max-width: 640px) {
  .topbar { position: relative; padding: 10px 16px; gap: 10px; }
  .brand { padding-right: 52px; }
  .brand small { display: none; }
  .actions { width: 100%; display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 8px; }
  .actions .select-wrap, .actions .select-wrap select { width: 100%; }
  .actions .select-wrap, .actions .btn.primary { grid-column: 1 / -1; }
  .actions .btn.primary { order: 5; }
  .actions:has(#writeFolderBtn[hidden]) #importMenu { grid-column: 1 / -1; }
  .actions .menu, .actions .menu > .btn { width: 100%; }
  #themeBtn { position: absolute; top: 10px; right: 16px; }
  .tab { padding: 8px 9px; }
  .tab-tool span { display: none; }
  .preview.fullscreen { padding: 10px 12px; }
}

/* Touch screens: larger targets, and 16px inputs so iOS does not zoom on focus */
@media (pointer: coarse) {
  .btn { min-height: 40px; }
  .btn.small, .attr .btns .btn { min-height: 36px; min-width: 36px; }
  input, select, textarea { font-size: 16px; min-height: 40px; }
  .chip { padding: 6px 10px; }
  .file-tree li { padding: 7px 8px; }
}

/* ---------- Red field error pop-up ---------- */
.field-error {
  position: absolute; z-index: 90;
  background: #dc2626; color: #fff;
  font: 500 12.5px/1.4 var(--sans);
  padding: 7px 10px; border-radius: 7px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .35);
  pointer-events: none;
}
.field-error::before {
  content: ""; position: absolute; top: -6px; left: var(--arrow-left, 16px);
  border: 6px solid transparent; border-top: 0; border-bottom-color: #dc2626;
}
input.flash { border-color: #dc2626 !important; box-shadow: 0 0 0 3px rgba(220, 38, 38, .25); }

textarea.msg-def { font: 12.5px/1.5 var(--mono); resize: vertical; white-space: pre; tab-size: 4; }
