:root {
  --ink: #0f172a; --ink2: #334155; --muted: #64748b; --line: #e2e8f0; --bg: #f1f4f8; --card: #fff;
  --accent: #0ea5a4; --accent-ink: #0b7f7e; --blue: #2563eb; --green: #16a34a; --red: #dc2626;
  --top: #0f172a; --radius: 10px;
  color-scheme: light;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column; background: var(--bg); color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
kbd { font: 11px ui-monospace, Menlo, monospace; background: #e8edf3; border: 1px solid #cbd5e1; border-bottom-width: 2px; border-radius: 4px; padding: 0 4px; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 1px; }

/* ---------- barra superior ---------- */
#top { flex: none; display: flex; align-items: center; gap: 10px; height: 54px; padding: 0 12px; background: var(--top); color: #e2e8f0; }
.brand { display: flex; align-items: center; gap: 8px; color: #fff; text-decoration: none; font-size: 19px; font-weight: 700; letter-spacing: .2px; }
.brand b { color: #5eead4; }
#projName { width: clamp(110px, 18vw, 220px); background: transparent; border: 1px solid transparent; border-radius: 6px; padding: 5px 8px; color: #e2e8f0; }
#projName:hover, #projName:focus { border-color: #334155; background: #1e293b; outline: none; }
.tools { display: flex; align-items: center; gap: 2px; }
.tools button, #btnMute {
  background: transparent; border: 0; border-radius: 6px; padding: 6px 10px; color: #cbd5e1;
}
.tools button:hover, #btnMute:hover { background: rgba(255, 255, 255, .1); color: #fff; }
.sep { width: 1px; height: 20px; background: #334155; margin: 0 6px; }
.spacer { flex: 1; }
#simTime { font: 13px ui-monospace, Menlo, monospace; color: #5eead4; min-width: 52px; text-align: right; }
.run, .stop { border: 0; border-radius: 8px; padding: 8px 16px; font-weight: 600; color: #fff; white-space: nowrap; }
.run { background: var(--green); } .run:hover { background: #15803d; }
.stop { background: var(--red); } .stop:hover { background: #b91c1c; }

#credits { flex: none; padding: 5px 14px; font-size: 12px; color: #94a3b8; background: var(--top); text-align: center; border-top: 1px solid #1e293b; }
#credits strong { color: #e2e8f0; font-weight: 600; }

/* ---------- layout ---------- */
main { flex: 1; display: flex; min-height: 0; position: relative; }
#stage { flex: 1; position: relative; min-width: 0; overflow: hidden; }
#side { flex: none; width: 310px; display: flex; flex-direction: column; background: var(--card); border-left: 1px solid var(--line); transition: width .15s ease; }
body.code-open #side { width: min(46vw, 680px); }

/* ---------- canvas ---------- */
#canvas { width: 100%; height: 100%; display: block; background: #f6f8fb; touch-action: none; user-select: none; -webkit-user-select: none; }
#canvas.panning { cursor: grabbing; } #canvas.pan-ready { cursor: grab; }
.part { cursor: move; }
.part [data-ctl] { cursor: grab; }
body.running .t-button, body.running .t-switch { cursor: pointer; }
.part .selbox { display: none; fill: rgba(37, 99, 235, .05); stroke: #2563eb; stroke-width: 1.2; stroke-dasharray: 4 3; pointer-events: none; }
.part.sel .selbox { display: block; }
.plabel { font-family: system-ui, sans-serif; pointer-events: none; }
#gWires, #gOver { pointer-events: none; }
.wo { fill: none; stroke: rgba(0, 0, 0, .32); stroke-width: 5.6; stroke-linecap: round; }
.wl { fill: none; stroke-width: 3.6; stroke-linecap: round; }
.wire.sel .wo { stroke: rgba(37, 99, 235, .55); stroke-width: 9; }
.wh { fill: #fff; stroke: #2563eb; stroke-width: 2; }
.hov { fill: rgba(37, 99, 235, .2); stroke: #2563eb; stroke-width: 1.5; }

#tip { position: absolute; z-index: 4; pointer-events: none; background: #0f172a; color: #fff; font-size: 12px; padding: 3px 8px; border-radius: 6px; white-space: nowrap; box-shadow: 0 2px 8px rgba(0, 0, 0, .25); }
#hint { position: absolute; left: 12px; bottom: 10px; font-size: 12px; color: var(--muted); pointer-events: none; max-width: 70%; }
#zoom { position: absolute; right: 12px; bottom: 12px; display: flex; background: var(--card); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 1px 4px rgba(15, 23, 42, .12); overflow: hidden; }
#zoom button { border: 0; background: transparent; width: 34px; height: 32px; font-size: 18px; color: var(--ink2); }
#zoom button:hover { background: #eef2f7; }
#toast { position: absolute; left: 50%; top: 14px; transform: translate(-50%, -20px); opacity: 0; pointer-events: none; transition: .2s; background: #0f172a; color: #fff; padding: 9px 16px; border-radius: 10px; box-shadow: 0 6px 20px rgba(15, 23, 42, .3); max-width: min(90%, 560px); z-index: 6; }
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.err { background: #991b1b; }

/* ---------- inspetor ---------- */
#inspector { position: absolute; left: 12px; top: 12px; width: 232px; z-index: 3; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 6px 20px rgba(15, 23, 42, .14); padding: 10px 12px 12px; }
.ih { font-weight: 700; margin-bottom: 8px; }
#inspector .f { display: grid; grid-template-columns: 1fr auto; gap: 4px 8px; align-items: center; margin-bottom: 8px; font-size: 13px; color: var(--ink2); }
#inspector .f select, #inspector .f input[type=text] { grid-column: 1 / -1; padding: 5px 8px; border: 1px solid #cbd5e1; border-radius: 6px; background: #fff; }
#inspector .f input[type=range] { grid-column: 1; width: 100%; accent-color: var(--accent); }
#inspector output { font-size: 12px; color: var(--muted); min-width: 34px; text-align: right; }
.irow { display: flex; gap: 6px; margin-top: 4px; flex-wrap: wrap; }
.irow button { flex: 1; padding: 6px 8px; border: 1px solid #cbd5e1; background: #f8fafc; border-radius: 6px; font-size: 13px; }
.irow button:hover { background: #eef2f7; }
.irow .danger { color: var(--red); border-color: #fecaca; background: #fff5f5; }
.sw { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.swatch { width: 24px; height: 24px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px #94a3b8; padding: 0; }
.swatch.on { box-shadow: 0 0 0 2px var(--blue); }

/* ---------- painel lateral ---------- */
.tabs { flex: none; display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; padding: 11px 8px; border: 0; background: transparent; color: var(--muted); font-weight: 600; border-bottom: 3px solid transparent; }
.tabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
.tabs button:hover { background: #f8fafc; }
#paneParts { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#psearch { margin: 10px 12px 4px; padding: 7px 10px; border: 1px solid #cbd5e1; border-radius: 8px; }
#palette { flex: 1; overflow: auto; padding: 4px 12px 16px; }
.pcat { margin: 14px 0 6px; font-size: 11px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.pgrid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.pitem { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px 6px; border: 1px solid var(--line); border-radius: 10px; background: #fff; cursor: grab; }
.pitem:hover { border-color: var(--accent); box-shadow: 0 2px 8px rgba(14, 165, 164, .18); }
.pitem svg { width: 100%; height: 62px; overflow: visible; pointer-events: none; }
.pitem span { font-size: 12px; text-align: center; color: var(--ink2); line-height: 1.2; }

#paneCode { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#editor { flex: 1 1 56%; min-height: 160px; }
#codeStatus { flex: none; padding: 5px 12px; font-size: 12px; border-top: 1px solid #1e293b; background: #0b1220; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: default; }
#codeStatus.ok { color: #6ee7b7; } #codeStatus.bad { color: #fca5a5; cursor: pointer; }
.btabs { flex: none; display: flex; gap: 2px; border-bottom: 1px solid var(--line); padding: 0 8px; background: #f8fafc; }
.btabs button { padding: 8px 12px; border: 0; background: transparent; color: var(--muted); font-weight: 600; font-size: 13px; border-bottom: 2px solid transparent; }
.btabs button.on { color: var(--ink); border-bottom-color: var(--accent); }
#msgBadge { background: var(--red); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; margin-left: 4px; }
#pane-serial, #pane-msgs { flex: 0 1 40%; min-height: 130px; display: flex; flex-direction: column; background: #fff; }
#serialOut { flex: 1; margin: 0; padding: 8px 12px; overflow: auto; font: 12.5px/1.45 ui-monospace, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-all; background: #fff; color: #0f172a; }
#serialForm { flex: none; display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--line); background: #f8fafc; }
#serialForm input { flex: 1; min-width: 0; padding: 6px 8px; border: 1px solid #cbd5e1; border-radius: 6px; }
#serialForm select, #serialForm button, .msgsbar button { padding: 6px 10px; border: 1px solid #cbd5e1; border-radius: 6px; background: #fff; }
#serialForm button:hover, .msgsbar button:hover { background: #eef2f7; }
#msgs { flex: 1; overflow: auto; padding: 6px 12px; font-size: 12.5px; }
.msg { padding: 4px 0; border-bottom: 1px solid #f1f5f9; }
.msg .mt { color: var(--muted); font-size: 11px; margin-right: 6px; }
.msg.error { color: #b91c1c; } .msg.warn { color: #b45309; } .msg.info { color: var(--ink2); }
.msg a { color: var(--blue); }
.msgsbar { flex: none; padding: 6px 8px; border-top: 1px solid var(--line); background: #f8fafc; }

/* ---------- editor de código ---------- */
.ed { position: relative; display: flex; background: #0f172a; color: #e2e8f0; font: 13px/20px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; overflow: hidden; }
.ed-gutter { flex: none; width: 46px; overflow: hidden; background: #0b1220; color: #64748b; text-align: right; padding-top: 8px; user-select: none; }
.ed-nums > div { height: 20px; padding-right: 10px; }
.ed-nums .err { color: #fff; background: #dc2626; }
.ed-main { position: relative; flex: 1; overflow: hidden; }
.ed-hl, .ed-ta { position: absolute; top: 0; left: 0; margin: 0; padding: 8px 12px; font: inherit; line-height: 20px; tab-size: 2; -moz-tab-size: 2; white-space: pre; border: 0; letter-spacing: 0; }
.ed-hl { min-width: 100%; width: max-content; pointer-events: none; }
.ed-ta { width: 100%; height: 100%; background: transparent; color: transparent; caret-color: #fff; resize: none; outline: none; overflow: auto; }
.ed-ta::selection { background: rgba(59, 130, 246, .45); }
.ed-hl .ln { height: 20px; }
.ed-hl .ln.err { background: rgba(220, 38, 38, .22); box-shadow: inset 3px 0 #ef4444; }
.hc { color: #64748b; font-style: italic; } .hs { color: #86efac; } .hn { color: #fbbf24; } .hp { color: #c4b5fd; }
.hk { color: #f472b6; } .ht { color: #7dd3fc; } .hq { color: #fdba74; } .hb { color: #93c5fd; } .hf { color: #fde68a; }

/* ---------- modal ---------- */
#modal { position: fixed; inset: 0; z-index: 20; background: rgba(15, 23, 42, .55); display: flex; align-items: center; justify-content: center; padding: 16px; }
.mbox { background: var(--card); border-radius: 14px; max-width: 640px; width: 100%; max-height: 88vh; overflow: auto; padding: 22px 24px; box-shadow: 0 20px 60px rgba(0, 0, 0, .35); }
.mbox h2 { margin: 0 0 6px; font-size: 20px; } .mbox h3 { margin: 18px 0 6px; font-size: 15px; }
.muted { color: var(--muted); margin: 0 0 12px; }
.exlist { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.ex { text-align: left; padding: 10px 12px; border: 1px solid var(--line); border-radius: 10px; background: #fff; display: flex; flex-direction: column; gap: 2px; }
.ex:hover { border-color: var(--accent); background: #f0fdfa; }
.ex span { font-size: 12.5px; color: var(--muted); }
.mfoot { margin-top: 18px; display: flex; justify-content: flex-end; }
.mfoot button { padding: 8px 16px; border: 1px solid #cbd5e1; border-radius: 8px; background: #fff; }
.mfoot .primary { background: var(--accent); color: #fff; border-color: var(--accent); font-weight: 600; }
.help li { margin-bottom: 6px; } .keys { line-height: 2; }
code { font: 12.5px ui-monospace, Menlo, monospace; background: #eef2f7; padding: 1px 5px; border-radius: 4px; }

/* ---------- telas pequenas ---------- */
@media (max-width: 900px) {
  .tools .opt, .brand span, #projName { display: none; }
  #side { position: absolute; right: 0; top: 0; bottom: 0; width: min(92vw, 340px); z-index: 5; box-shadow: -8px 0 24px rgba(15, 23, 42, .18); }
  body.code-open #side { width: 100vw; }
  .exlist { grid-template-columns: 1fr; }
  #hint { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
