:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-2: #f4f5f7;
  --line: #e6e8ec;
  --line-2: #d5d9e0;
  --text: #1c2230;
  --muted: #6b7385;
  --accent: #f5b301;
  --accent-2: #ffc933;
  --accent-ink: #1c1600;
  --ok: #16a34a;
  --danger: #dc2626;
  --warn: #b45309;
  --info: #2563eb;
  --canvas: #fdf5e8;
  --grid: #ead9bd;
  --term-bg: #fbfbfc;
  --mono: "JetBrains Mono", "Fira Code", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --side-w: 450px;
  --bottom-h: 34%;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 13px/1.45 var(--sans); overflow: hidden; display: flex; flex-direction: column; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
kbd { font: 11px var(--mono); background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 4px; padding: 0 4px; }
code { font: 12px var(--mono); background: var(--panel-2); padding: 1px 4px; border-radius: 4px; color: #9a3412; }

/* ---------- top bar ---------- */
.topbar { display: flex; align-items: center; gap: 14px; padding: 0 0 0 16px; height: 56px; background: var(--panel); border-bottom: 1px solid var(--line); flex: none; }
.brand { display: flex; align-items: center; gap: 8px; padding-right: 14px; border-right: 1px solid var(--line); height: 100%; }
.brand svg rect { fill: var(--accent); }
.brand svg path { fill: #1c2230; }
.brand svg circle { fill: var(--accent); }
.brand-name { font-size: 19px; font-weight: 800; letter-spacing: -.3px; }
.brand-name b { color: var(--accent); }
.chip { font: 700 10px var(--mono); padding: 2px 7px; border-radius: 99px; background: #fff4d1; color: #8a6400; border: 1px solid #f3dc94; }
.proj-name { background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 5px 8px; width: 240px; color: var(--text); font-weight: 600; }
.proj-name:hover, .proj-name:focus { border-color: var(--line-2); outline: none; background: var(--panel-2); }
.tb-group { display: flex; align-items: center; gap: 6px; }
.tb-files { height: 100%; gap: 6px; }
.tb-spacer { flex: 1; }
.btn { background: var(--panel); border: 1px solid var(--line-2); border-radius: 8px; padding: 6px 11px; cursor: pointer; white-space: nowrap; font-weight: 500; }
.btn:hover { background: var(--panel-2); }
.btn:focus-visible, .tab:focus-visible, .run-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.btn-icon { padding: 6px 9px; min-width: 32px; }
.btn-sm { padding: 3px 9px; font-size: 12px; }
.btn-dark { background: #111; color: #fff; border-color: #111; border-radius: 0; height: 100%; padding: 0 26px; font-weight: 600; }
.btn-dark:hover { background: #2a2a2a; }
.btn-accent { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.btn-accent:hover { background: var(--accent-2); }
select.btn { padding-right: 6px; }

/* ---------- workspace ---------- */
.workspace { flex: 1; min-height: 0; display: flex; }

/* left panel */
.side { width: var(--side-w); min-width: 320px; max-width: 70%; flex: none; display: flex; flex-direction: column; background: var(--panel); min-height: 0; }
.tabs { display: flex; align-items: center; gap: 2px; padding: 6px 8px; border-bottom: 1px solid var(--line); background: var(--panel); flex: none; }
.side > .tabs { margin: 8px 8px 0; padding: 4px; border: 0; background: var(--panel-2); border-radius: 10px; }
.side > .tabs .tab { flex: 1; justify-content: center; }
.tab { background: none; border: 0; border-radius: 7px; padding: 6px 12px; cursor: pointer; color: var(--muted); display: flex; align-items: center; gap: 6px; font-weight: 500; }
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); background: #fff; box-shadow: 0 1px 2px rgba(16, 24, 40, .12); font-weight: 600; }
.bottom .tabs .tab.active { background: var(--panel-2); box-shadow: none; }
.tabs-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.check { display: flex; align-items: center; gap: 5px; color: var(--muted); font-size: 12px; cursor: pointer; }
.check input { accent-color: var(--accent); }
.badge { background: var(--danger); color: #fff; border-radius: 99px; font: 600 10px var(--mono); padding: 0 6px; }
.mini-dot { width: 7px; height: 7px; border-radius: 50%; background: #c8ccd4; }
.mini-dot.on { background: var(--ok); }
.tabpane { display: none; min-height: 0; }
.tabpane.active { display: flex; flex-direction: column; flex: 1; }
.editor-wrap { flex: 1; min-height: 0; display: flex; flex-direction: column; margin-top: 8px; border-top: 1px solid var(--line); }
#editorPane { position: relative; }
#editorPane textarea { flex: 1; width: 100%; resize: none; border: 0; background: #fff; color: var(--text); font: 13px/1.6 var(--mono); padding: 10px; tab-size: 2; outline: none; }
.CodeMirror { height: 100%; font: 13.5px/1.6 var(--mono); color: #1c2230; }
.CodeMirror-gutters { background: #fff; border-right: 0; }
.CodeMirror-linenumber { color: #9aa1ae; padding: 0 10px 0 8px; }
.CodeMirror-activeline-background { background: #fffaeb; }
.cm-s-default .cm-keyword { color: #0b3fd1; }
.cm-s-default .cm-type, .cm-s-default .cm-variable-3 { color: #0b3fd1; }
.cm-s-default .cm-def { color: #111; }
.cm-s-default .cm-number { color: #0f8a5f; }
.cm-s-default .cm-string { color: #b3261e; }
.cm-s-default .cm-meta { color: #a35a00; }
.cm-s-default .cm-comment { color: #8a93a3; font-style: italic; }
.cm-error-line { background: rgba(220, 38, 38, .12) !important; }
.ref { overflow: auto; padding: 4px 18px 18px; color: #3b4352; }
.ref h3 { margin: 14px 0 6px; font-size: 14px; color: var(--text); }
.ref h4 { margin: 14px 0 4px; font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: #8a6400; }
.ref p { margin: 0 0 6px; }

/* parts pane */
.palette { overflow: hidden; }
.search { margin: 10px; padding: 7px 10px; border-radius: 8px; border: 1px solid var(--line-2); background: var(--panel); }
.search:focus { outline: none; border-color: var(--accent); }
.palette-list { flex: 1; overflow: auto; padding: 0 10px 10px; display: block; }
.palette-group { display: grid; grid-template-columns: repeat(auto-fill, minmax(118px, 1fr)); gap: 8px; }
.palette-cat { grid-column: 1 / -1; font: 700 10.5px var(--sans); text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 10px 2px 0; }
.part { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 6px 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); cursor: grab; text-align: center; }
.part:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(245, 179, 1, .18); }
.part svg { width: 64px; height: 48px; }
.part span { font-size: 12px; font-weight: 500; line-height: 1.25; }
.palette-hint { font-size: 11.5px; color: var(--muted); margin: 0; padding: 10px 12px; border-top: 1px solid var(--line); }

/* bottom dock */
.bottom { height: var(--bottom-h); min-height: 120px; display: flex; flex-direction: column; flex: none; }
.serial-out { flex: 1; margin: 0; overflow: auto; padding: 8px 12px; font: 12.5px/1.55 var(--mono); background: var(--term-bg); color: #1c2230; white-space: pre-wrap; word-break: break-word; }
.serial-form, .mqtt-form { display: flex; gap: 6px; padding: 6px 8px; border-top: 1px solid var(--line); background: var(--panel); }
.serial-form input, .mqtt-form input { flex: 1; min-width: 0; padding: 5px 8px; border-radius: 7px; border: 1px solid var(--line-2); background: var(--panel); font-family: var(--mono); font-size: 12px; }
.serial-form input:focus, .mqtt-form input:focus { outline: none; border-color: var(--accent); }
.serial-form select { border-radius: 7px; border: 1px solid var(--line-2); background: var(--panel); padding: 0 4px; font-size: 12px; }

.iot { overflow: hidden; }
.iot-status { display: flex; gap: 6px; padding: 8px; flex-wrap: wrap; border-bottom: 1px solid var(--line); }
.iot-stat { display: flex; flex-direction: column; background: var(--panel-2); border-radius: 8px; padding: 4px 10px; min-width: 90px; }
.iot-stat .k { font-size: 10px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.iot-stat .v { font: 12px var(--mono); }
.iot-stat .v.ok { color: var(--ok); }
.iot-stat .v.wait { color: var(--warn); }
.iot-topics { display: flex; gap: 6px; padding: 8px; overflow-x: auto; flex: none; }
.iot-topics:empty { display: none; }
.topic-card { background: var(--panel); border: 1px solid var(--line-2); border-radius: 10px; padding: 6px 10px; min-width: 130px; cursor: pointer; text-align: left; }
.topic-card:hover { border-color: var(--accent); }
.topic-card .t { font: 10.5px var(--mono); color: var(--muted); display: block; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topic-card .p { font: 700 16px var(--mono); display: block; max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.iot-log { flex: 1; overflow: auto; font: 12px/1.6 var(--mono); padding: 4px 10px; background: var(--term-bg); }
.iot-log .m { display: flex; gap: 8px; white-space: nowrap; }
.iot-log .tm { color: #9aa1ae; }
.iot-log .dir { width: 18px; text-align: center; font-weight: 700; }
.iot-log .dev .dir { color: var(--info); }
.iot-log .ui .dir { color: var(--ok); }
.iot-log .tp { color: #8a6400; }
.iot-log .pl { color: var(--text); overflow: hidden; text-overflow: ellipsis; }
.iot-empty { color: var(--muted); padding: 10px 2px; font-family: var(--sans); white-space: normal; }

.console { overflow: auto; background: var(--term-bg); }
#consoleOut { font: 12px/1.55 var(--mono); padding: 6px 10px; }
.log { padding: 3px 0; border-bottom: 1px solid #eef0f3; display: flex; gap: 8px; }
.log .tm { color: #9aa1ae; flex: none; }
.log.error { color: var(--danger); }
.log.warn { color: var(--warn); }
.log.info { color: var(--info); }
.log a { color: inherit; font-weight: 600; }

/* splitters */
.splitter-v { width: 1px; flex: none; cursor: col-resize; background: var(--line); position: relative; }
.splitter-v::after { content: ""; position: absolute; inset: 0 -4px; }
.splitter-h { height: 1px; flex: none; cursor: row-resize; background: var(--line); position: relative; }
.splitter-h::after { content: ""; position: absolute; inset: -4px 0; }
.splitter-v:hover, .splitter-h:hover, .dragging { background: var(--accent); }

/* ---------- canvas ---------- */
.stage { flex: 1; min-width: 0; position: relative; background: var(--canvas); }
.canvas { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; cursor: default; }
.canvas.cs-panning { cursor: grabbing; }
.cs-grid-dot { fill: var(--grid); }
.float-bar { position: absolute; top: 14px; left: 50%; transform: translateX(-50%); display: flex; align-items: center; gap: 6px; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 6px; box-shadow: 0 4px 16px rgba(60, 40, 0, .08); z-index: 2; }
.run-btn { display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 14px; border: 0; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-weight: 700; cursor: pointer; }
.run-btn:hover { background: var(--accent-2); }
.run-btn.running { background: var(--danger); color: #fff; }
.float-bar .btn { border-color: transparent; background: var(--panel-2); height: 34px; }
.float-bar .btn:hover { background: #eceef2; }
.bar-sep { width: 1px; height: 22px; background: var(--line); margin: 0 2px; }
.sim-clock { display: flex; align-items: center; gap: 6px; font: 12px var(--mono); color: var(--muted); padding: 0 10px; height: 34px; border-radius: 8px; background: var(--panel-2); min-width: 110px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #c8ccd4; }
.dot.on { background: var(--ok); box-shadow: 0 0 8px var(--ok); animation: blink 1.2s infinite; }
@keyframes blink { 50% { opacity: .45; } }
.stage-tools { position: absolute; right: 14px; bottom: 14px; display: flex; align-items: center; gap: 4px; background: #fff; border: 1px solid var(--line); border-radius: 10px; padding: 4px; box-shadow: 0 4px 16px rgba(60, 40, 0, .08); }
.stage-tools .btn { border-color: transparent; }
.stage-tools span { font: 11px var(--mono); color: var(--muted); width: 40px; text-align: center; }
.stage-hint { position: absolute; top: 70px; left: 50%; transform: translateX(-50%); background: #1c2230; color: #fff; border-radius: 8px; padding: 6px 12px; font-size: 12px; pointer-events: none; opacity: 0; transition: opacity .15s; white-space: nowrap; max-width: 90%; overflow: hidden; text-overflow: ellipsis; }
.stage-hint kbd { background: #2c3445; border-color: #3b4558; color: #fff; }
.stage-hint.show { opacity: 1; }

/* parts on canvas */
.cs-comp { cursor: move; }
.cs-comp.cs-interactive { cursor: pointer; }
.cs-sel { fill: none; stroke: transparent; stroke-width: 1.5; stroke-dasharray: 5 4; }
.cs-comp.cs-selected .cs-sel { stroke: #d99a00; }
.cs-comp:hover .cs-sel { stroke: #d8c7a6; }
.cs-comp.cs-selected:hover .cs-sel { stroke: #d99a00; }
.cs-id { font: 10px var(--mono); fill: #8a7a5c; text-anchor: middle; opacity: 0; pointer-events: none; }
.cs-comp:hover .cs-id, .cs-comp.cs-selected .cs-id { opacity: 1; }
.cs-pin { cursor: crosshair; }
.cs-pin-hit { fill: transparent; }
.cs-pin-dot { fill: #e0b43a; stroke: #7a5c10; stroke-width: .8; transition: transform .1s; transform-box: fill-box; transform-origin: center; }
.cs-pin:hover .cs-pin-dot, .cs-pin.cs-active .cs-pin-dot { fill: #22c55e; stroke: #0b4d24; transform: scale(1.7); }
.canvas.cs-wiring .cs-pin-dot { fill: #86efac; }
.cs-wire { cursor: pointer; }
.cs-wire-hit { fill: none; stroke: transparent; stroke-width: 12; }
.cs-wire-under { fill: none; stroke: rgba(0, 0, 0, .25); stroke-width: 4.4; stroke-linecap: round; }
.cs-wire-line { fill: none; stroke-width: 2.6; stroke-linecap: round; }
.cs-wire:hover .cs-wire-line { stroke-width: 3.4; }
.cs-wire.cs-selected .cs-wire-under { stroke: var(--accent); stroke-width: 8; opacity: .7; }
.cs-temp { fill: none; stroke: #16a34a; stroke-width: 2.4; stroke-dasharray: 6 4; pointer-events: none; }
.cs-esp-lbl { font: 600 7.6px var(--mono); fill: #c9d2e0; }
.cs-chip { font: 700 12px var(--sans); fill: #3b414b; text-anchor: middle; letter-spacing: .5px; }
.cs-chip-sm { font-size: 7.5px; font-weight: 600; }
.cs-tiny { font: 600 6.5px var(--mono); fill: #9aa6b8; text-anchor: middle; }
.cs-light { fill: #e7f0ff; }
.cs-dark { fill: #374151; }
.cs-part-lbl { font: 9px var(--mono); fill: #6b5a3a; text-anchor: middle; }
.cs-lcd { font: 15px var(--mono); }
.cs-pulse { animation: pulse .35s infinite alternate; }
@keyframes pulse { from { opacity: .3; } to { opacity: 1; } }
.part .cs-part-lbl { display: none; }

/* properties panel */
.props { position: absolute; left: 14px; bottom: 14px; width: 250px; background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: 0 10px 30px rgba(60, 40, 0, .12); }
.props h4 { margin: 0 0 2px; font-size: 13.5px; }
.props .sub { color: var(--muted); font: 11px var(--mono); margin-bottom: 10px; }
.props .row { margin: 8px 0; }
.props label { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.props label b { color: var(--text); font: 11.5px var(--mono); }
.props input[type=range] { width: 100%; accent-color: #d99a00; }
.props select { width: 100%; padding: 5px 6px; border-radius: 7px; background: #fff; border: 1px solid var(--line-2); }
.props .toggle { display: flex; align-items: center; gap: 8px; color: var(--text); cursor: pointer; justify-content: flex-start; }
.props .toggle input { accent-color: #d99a00; width: 16px; height: 16px; }
.props .hint { font-size: 11.5px; color: var(--muted); margin: 6px 0 0; }
.props .actions { display: flex; gap: 6px; margin-top: 10px; }
.props .danger { color: var(--danger); border-color: #f3c4c4; }
.props .danger:hover { background: #fdecec; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line-2); cursor: pointer; padding: 0; }
.swatch.on { box-shadow: 0 0 0 2px #1c2230; }

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

/* ---------- narrow screens ---------- */
@media (max-width: 900px) {
  body { overflow: auto; }
  .topbar { height: auto; flex-wrap: wrap; padding: 8px 12px; gap: 8px; }
  .brand { border: 0; padding: 0; }
  .proj-name { width: 150px; }
  .tb-spacer { display: none; }
  .tb-files { width: 100%; overflow-x: auto; height: auto; }
  .btn-dark { height: auto; padding: 6px 12px; border-radius: 8px; }
  .workspace { flex-direction: column-reverse; flex: none; }
  .stage { height: 60vh; flex: none; }
  .splitter-v, .splitter-h { display: none; }
  .side { width: 100%; max-width: none; min-width: 0; }
  .editor-wrap { height: 55vh; flex: none; }
  .bottom { height: 40vh; }
  .float-bar { max-width: calc(100% - 32px); }
  .props { width: auto; right: 14px; bottom: 64px; }
}
