
  :root {
    /* Palette drawn from a DVS event frame: dark sensor field,
       teal = ON polarity, ember = OFF polarity. */
    --field:   #0b0e14;
    --panel:   #12161f;
    --panel-2: #171c27;
    --line:    #242b3a;
    --ink:     #e8ecf4;
    --muted:   #8b94a7;
    --on:      #4fd6be;   /* ON events / go / ready */
    --on-dim:  #1f5c50;
    --off:     #ff6b5e;   /* OFF events / errors */
    --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  }
  * { box-sizing: border-box; }
  html, body { height: 100%; }
  body { height: 100dvh; overflow: hidden; }
  body {
    margin: 0; background: var(--field); color: var(--ink);
    font-family: var(--sans); font-size: 15px; line-height: 1.5;
    display: flex; flex-direction: column;
  }

  header {
    display: flex; align-items: baseline; gap: 14px; flex-wrap: wrap;
    padding: 10px 16px 6px; flex-shrink: 0;
  }
  header h1 {
    margin: 0; font-family: var(--mono); font-weight: 700;
    font-size: 18px; letter-spacing: .02em;
  }
  header h1 .on  { color: var(--on); }
  header h1 .off { color: var(--off); }
  header .sub { color: var(--muted); font-size: 13px; }

  #status {
    margin-left: auto; font-family: var(--mono); font-size: 12.5px;
    padding: 4px 12px; border: 1px solid var(--line); border-radius: 999px;
    color: var(--muted); white-space: nowrap;
  }
  #status.busy { color: var(--ink); border-color: var(--on-dim); }
  #status.ok   { color: var(--on);  border-color: var(--on-dim); }
  #status.err  { color: var(--off); border-color: var(--off); }
  #status.busy::before {
    content: ""; display: inline-block; width: 7px; height: 7px; border-radius: 50%;
    background: var(--on); margin-right: 8px; animation: pulse 1s infinite alternate;
  }
  @keyframes pulse { from { opacity: .25 } to { opacity: 1 } }
  @media (prefers-reduced-motion: reduce) { #status.busy::before { animation: none; } }

  #chips { display: flex; gap: 8px; flex-wrap: wrap; padding: 8px 16px 0; flex-shrink: 0; }
  .chip {
    font-family: var(--mono); font-size: 12.5px; color: var(--muted);
    background: var(--panel); border: 1px solid var(--line); border-radius: 999px;
    padding: 4px 12px; cursor: pointer;
  }
  .chip:hover { color: var(--ink); border-color: var(--on-dim); }

  main {
    flex: 1; display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
    gap: 14px; padding: 10px 16px 8px; min-height: 0;
  }

  .pane { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
  .pane-label {
    font-family: var(--mono); font-size: 11px; letter-spacing: .12em;
    text-transform: uppercase; color: var(--muted); margin-bottom: 6px;
  }

  #editor-wrap { border: 1px solid var(--line); border-radius: 10px; overflow: hidden; flex: 1; min-height: 0; }
  textarea#code {
    width: 100%; height: 100%; min-height: 0; resize: none;
    background: var(--panel); color: var(--ink); border: 0; padding: 12px;
    font-family: var(--mono); font-size: 13.5px;
  }
  .CodeMirror {
    height: 100%; min-height: 0;
    background: var(--panel); color: var(--ink);
    font-family: var(--mono); font-size: 13.5px; line-height: 1.55;
  }
  .CodeMirror-gutters { background: var(--panel-2); border-right: 1px solid var(--line); }
  .CodeMirror-linenumber { color: #4a5265; }
  .CodeMirror-cursor { border-left: 2px solid var(--on); }
  .CodeMirror-selected { background: #223046 !important; }
  .cm-s-default .cm-keyword  { color: var(--off); }
  .cm-s-default .cm-def      { color: var(--on); }
  .cm-s-default .cm-variable { color: var(--ink); }
  .cm-s-default .cm-string   { color: #a3d9a5; }
  .cm-s-default .cm-comment  { color: #5c6577; font-style: italic; }
  .cm-s-default .cm-number   { color: #e5c07b; }
  .cm-s-default .cm-builtin, .cm-s-default .cm-property { color: #7fb4ff; }

  .toolbar { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
  button#run {
    font-family: var(--mono); font-weight: 700; font-size: 14px;
    color: #07130f; background: var(--on); border: 0; border-radius: 8px;
    padding: 8px 22px; cursor: pointer;
  }
  button#run:disabled { background: var(--on-dim); color: #9fb6b0; cursor: wait; }
  .hint { color: var(--muted); font-size: 12.5px; font-family: var(--mono); }
  button#clear {
    margin-left: auto; font-family: var(--mono); font-size: 12.5px;
    color: var(--muted); background: none; border: 1px solid var(--line);
    border-radius: 8px; padding: 6px 12px; cursor: pointer;
  }
  button#clear:hover { color: var(--ink); }

  #console {
    height: clamp(64px, 13vh, 110px); min-height: 0; overflow: auto;
    background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
    padding: 12px; font-family: var(--mono); font-size: 12.5px;
    white-space: pre-wrap; word-break: break-word;
  }
  #console .err  { color: var(--off); }
  #console .info { color: var(--muted); }

  #visuals {
    display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px; flex: 1; min-height: 0;
  }
  #visuals:has(#camera-wrap[hidden]) { grid-template-columns: minmax(0, 1fr); }
  .preview-panel { display: grid; grid-template-rows: auto minmax(0, 1fr); min-height: 0; min-width: 0; }
  #canvas-wrap { display: none; }
  .preview-media { position: relative; min-height: 0; overflow: hidden; border: 1px solid var(--line); border-radius: 10px; background: #000; }
  #camera-preview, #out-canvas {
    position: absolute; inset: 0; display: block; width: 100%; height: 100%; object-fit: contain;
  }
  #out-canvas { image-rendering: pixelated; }
  #console-panel { margin: 8px 0 0; flex: none; }
  #console-panel summary { cursor: pointer; }

  footer {
    padding: 4px 16px 8px; flex-shrink: 0; color: var(--muted); font-size: 12px;
    display: flex; gap: 6px; flex-wrap: wrap;
  }
  footer a { color: var(--on); text-decoration: none; }
  footer a:hover { text-decoration: underline; }

  button:focus-visible, .chip:focus-visible, a:focus-visible { outline: 2px solid var(--on); outline-offset: 2px; }

  .toolbar { flex-wrap: wrap; flex-shrink: 0; gap: 8px; }
  button, select, input { font: inherit; }
  .control { color: var(--ink); background: var(--panel-2); border: 1px solid var(--line); border-radius: 7px; padding: 6px 10px; cursor: pointer; }
  button:disabled { opacity: .5; cursor: default; }
  .intro { margin: 0 16px; color: var(--muted); font-size: 13px; flex-shrink: 0; }
  .source-bar { margin: 8px 16px 0; padding: 8px 10px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; flex-shrink: 0; font-size: 13px; }
  #webcam-controls { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
  [hidden] { display: none !important; }
  #stats { color: var(--on); font-family: var(--mono); font-size: 11px; margin: 6px 0 0; flex: none; }
  #editor-help { margin: 0 0 8px; color: var(--muted); font-size: 12px; }
  #status { white-space: normal; }
  #help { margin: 0 16px 4px; color: var(--muted); font-size: 12px; flex-shrink: 0; }
  #help summary { cursor: pointer; }
  #help[open] { position: fixed; z-index: 10; bottom: 36px; left: 0; right: 0; max-height: 50dvh; overflow: auto; padding: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 40px #0009; }
  #help code { color: var(--ink); }
  @media (max-width: 1100px) { .toolbar .hint { display: none; } header .sub { display: none; } }
  @media (max-height: 650px) and (min-width: 701px) {
    .intro { display: none; }
    #console { height: 64px; }
    #editor-help { font-size: 11px; }
    footer { font-size: 11px; }
  }
  @media (max-width: 700px) {
    body { height: auto; min-height: 100dvh; overflow: auto; }
    main { display: flex; flex-direction: column; }
    .output-pane { order: -1; }
    #visuals { flex: none; height: clamp(160px, 30dvh, 280px); }
    #editor-wrap { flex: none; height: 280px; }
    header { gap: 6px; }
    header h1 { font-size: 16px; }
    #status { font-size: 11px; }
    #chips { gap: 6px; }
    #help[open] { bottom: 12px; }
  }
