:root {
  --bg: #f4f6fa; --panel: #fff; --text: #1c2333; --muted: #5d667a; --border: #d5dae6;
  --accent: #2563eb; --accent-text: #fff; --code-bg: #0f1729; --code-text: #dbe4f5;
  --line-hl: #3b4a1b; --chg-fill: #fff3c4; --chg-stroke: #e69500;
  --box: #eef2fb; --cell: #fff; --head: #d9e3fa; --freed: #9aa3b5;
  --err-bg: #fde8e8; --err-text: #9b1c1c;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1320; --panel: #171e30; --text: #e6eaf5; --muted: #98a2bb; --border: #2b3550;
    --accent: #5b8cff; --accent-text: #0b1020; --code-bg: #0a0f1d; --code-text: #dbe4f5;
    --line-hl: #4a5a1e; --chg-fill: #4a3d10; --chg-stroke: #ffb020;
    --box: #1f2840; --cell: #131a2c; --head: #2a3556; --freed: #6b7490;
    --err-bg: #3a1717; --err-text: #ffb4b4;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.45 system-ui, "Segoe UI", sans-serif; }
header { padding: 14px 24px 4px; }
header h1 { margin: 0; font-size: 22px; }
header p { margin: 2px 0 0; color: var(--muted); }
main { display: grid; grid-template-columns: minmax(340px, 480px) 1fr; gap: 16px; padding: 12px 24px 24px; }
@media (max-width: 960px) { main { grid-template-columns: 1fr; } }
section { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px; min-width: 0; }
.toolbar, .controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 4px 0 8px; }
label { font-size: 13px; color: var(--muted); display: flex; gap: 6px; align-items: center; }
select, button { font: inherit; padding: 6px 10px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); cursor: pointer; }
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button.active { outline: 2px solid var(--accent); }
.hidden { display: none !important; }
.codewrap { border-radius: 8px; overflow: hidden; background: var(--code-bg); }
#editor, #codeview { width: 100%; height: 520px; margin: 0; overflow: auto; color: var(--code-text);
  font: 13px/1.55 ui-monospace, Consolas, "Courier New", monospace; }
#editor { background: transparent; border: 0; padding: 10px; resize: vertical; white-space: pre; tab-size: 4; }
#codeview { padding: 6px 0; }
.ln { display: flex; white-space: pre; padding-right: 10px; }
.ln .n { flex: none; width: 38px; text-align: right; padding-right: 10px; color: #6b7794; user-select: none; }
.ln.cur { background: var(--line-hl); box-shadow: inset 3px 0 0 #b7e04a; }
.ln.err { background: #5a1f1f; box-shadow: inset 3px 0 0 #ff6b6b; }
.erro { background: var(--err-bg); color: var(--err-text); padding: 8px 10px; border-radius: 6px; margin-top: 8px; }
.hint { font-size: 12.5px; color: var(--muted); margin: 6px 0 0; }
code { font-family: ui-monospace, Consolas, monospace; background: color-mix(in srgb, var(--accent) 14%, transparent);
  padding: 0 4px; border-radius: 4px; }
.controls .speed { margin-left: auto; }
#counter { font-variant-numeric: tabular-nums; color: var(--muted); min-width: 90px; text-align: right; }
.msg { border-left: 4px solid var(--accent); background: var(--box); padding: 10px 12px; border-radius: 6px;
  min-height: 62px; margin-bottom: 10px; }
.msg.erro-msg { border-color: #e03131; background: var(--err-bg); }
.badge { display: inline-block; font-weight: 700; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  background: var(--accent); color: var(--accent-text); margin-right: 8px; }
/* o diagrama usa sempre cores claras para que as fontes pretas fiquem legíveis (inclusive no modo escuro) */
.canvaswrap { border: 1px solid #d5dae6; border-radius: 8px; background: #f4f6fa; overflow: auto;
  --box: #eef2fb; --cell: #fff; --head: #d9e3fa; --border: #b8c0d4; --freed: #6b7490;
  --chg-fill: #fff3c4; --chg-stroke: #e69500; }
#canvas { display: block; width: 100%; height: auto; min-height: 280px; }
.bottom { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
@media (max-width: 700px) { .bottom { grid-template-columns: 1fr; } }
.bottom h3 { margin: 0 0 6px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.console pre { margin: 0; min-height: 84px; background: var(--code-bg); color: #9cf5b0; padding: 8px 10px; border-radius: 6px;
  font: 13px/1.5 ui-monospace, Consolas, monospace; white-space: pre-wrap; }
.legend ul { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.legend li { padding: 2px 6px; border-radius: 4px; color: var(--muted); }
.legend li b { display: inline-block; width: 20px; }
.legend li.done { color: var(--text); }
.legend li.done b { color: #2f9e44; }
.legend li.now { background: var(--chg-fill); color: var(--text); font-weight: 600; }


/* SVG: todas as fontes do diagrama em preto; tamanhos grandes no interior dos nós */
svg text { fill: #000; font-family: ui-monospace, Consolas, "Courier New", monospace; }
svg .title { font-family: system-ui, "Segoe UI", sans-serif; font-weight: 700; letter-spacing: .04em; }
svg .box { fill: var(--box); stroke: var(--border); stroke-width: 1.5; }
svg .head { fill: var(--head); }
svg .hd { font-weight: 700; }
svg .cell { fill: var(--cell); stroke: var(--border); stroke-width: 1.2; }
svg .chg { fill: var(--chg-fill); stroke: var(--chg-stroke); stroke-width: 2.2; }
svg .freed { stroke: var(--freed); stroke-dasharray: 6 4; fill: transparent; }
svg .small { font-size: 14px; }
svg .val { font-size: 22px; font-weight: 700; }
svg .slash { stroke: #000; stroke-width: 1.6; }
svg .vname { font-weight: 700; }
svg .vaddr { font-size: 13px; }
svg .vdest { font-weight: 700; }
svg .idx { font-size: 13px; }
svg .fname { font-family: system-ui, "Segoe UI", sans-serif; font-weight: 700; }
svg .frame { fill: none; stroke: #000; stroke-width: 1.2; stroke-dasharray: 3 5; opacity: .55; }
svg .ghost { fill: none; stroke: #000; stroke-width: 1.2; stroke-dasharray: 4 3; }
