/* CODE — the block editor and the text editor for When scripts.
   Uses the tokens app.css defines; every one has a fallback so the editor
   also stands up on its own (tests/code_demo.html). Phone first: the palette
   is a bottom drawer under 760px of the editor's own width, a left column
   above it. */

.ce {
  --ce-bg: var(--bg, #0a0810);
  --ce-card: var(--card, #16131f);
  --ce-card2: var(--card-2, #1f1a2b);
  --ce-ink: var(--ink, #f6f2ea);
  --ce-soft: var(--soft, #c9c2d8);
  --ce-dim: var(--dim, #8d86a0);
  --ce-line: var(--line, #2c2638);
  --ce-blockink: #15111d;
  --ce-bad: #ff5b5b;
  --ce-mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  --ce-drawer: 66px;
  container-type: inline-size;
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  background: var(--ce-bg);
  color: var(--ce-ink);
  font-family: var(--font-body, system-ui, -apple-system, sans-serif);
  font-size: 16px;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.ce *, .ce *::before, .ce *::after { box-sizing: border-box; }
:where(.ce) button { font: inherit; color: inherit; cursor: pointer; }
.ce button:focus-visible, .ce input:focus-visible, .ce textarea:focus-visible {
  outline: 3px solid var(--cat-events, #ffb53d); outline-offset: 2px;
}

/* ---------------------------------------------------------------- toolbar */
.ce-bar {
  -webkit-user-select: none; user-select: none;
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; border-bottom: 1px solid var(--ce-line);
  background: var(--ce-card); flex: none; min-height: 58px;
}
.ce-seg { display: inline-flex; background: var(--ce-bg); border: 1px solid var(--ce-line); border-radius: 14px; padding: 3px; }
.ce-seg button {
  border: 0; background: transparent; min-height: 40px; padding: 0 16px; border-radius: 11px;
  font-weight: 700; font-size: 15px; letter-spacing: .02em; color: var(--ce-soft);
}
.ce-seg button[aria-pressed="true"] { background: var(--ce-ink); color: var(--ce-blockink); }
.ce-title { flex: 1; min-width: 0; font-family: var(--font-head, system-ui, sans-serif); font-weight: 700;
  font-size: 16px; color: var(--ce-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ce-title b { color: var(--ce-ink); }
.ce-iconbtn {
  min-width: 44px; min-height: 44px; border-radius: 12px; border: 1px solid var(--ce-line);
  background: var(--ce-card2); display: inline-grid; place-items: center; font-size: 19px; padding: 0 10px;
}
.ce-iconbtn:disabled { opacity: .35; cursor: default; }
.ce-probs {
  min-height: 40px; border-radius: 12px; padding: 0 12px; border: 1px solid #5c2630;
  background: #2a1218; color: #ffb3b3; font-weight: 700; font-size: 14px; white-space: nowrap;
}
.ce-probs[hidden] { display: none; }
.ce-probs .n { display: inline-grid; place-items: center; min-width: 22px; height: 22px; border-radius: 11px; background: var(--ce-bad);
  color: #1a0b0e; font-weight: 900; margin-right: 6px; padding: 0 6px; }
.ce-iconbtn svg { width: 22px; height: 22px; display: block; }
@container (max-width: 560px) {
  .ce-title { display: none; }
  .ce-bar { justify-content: space-between; }
  .ce-probs .w { display: none; }
  .ce-probs .n { margin-right: 0; }
  .ce-probs { padding: 0 8px; }
  .ce-tbar .ce-hint { visibility: hidden; }
  .ce-seg button { padding: 0 13px; }
}

/* ----------------------------------------------------------------- layout */
.ce-main { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ce-blocksview { position: relative; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ce-blocksview[hidden], .ce-textview[hidden] { display: none; }

.ce-script {
  position: relative; flex: 1; min-height: 0; overflow: auto; overscroll-behavior: contain;
  padding: 16px 12px calc(var(--ce-drawer) + 40px);
  background:
    radial-gradient(circle at 1px 1px, rgba(255,255,255,.055) 1px, transparent 1.5px) 0 0 / 22px 22px,
    var(--ce-bg);
}
.ce-stack { position: relative; display: flex; flex-direction: column; gap: 18px; max-width: 760px; }

@container (min-width: 760px) {
  .ce-blocksview { flex-direction: row; }
  .ce-script { padding: 22px 26px 80px; }
}

/* ----------------------------------------------------------------- blocks */
.cb { --c: var(--cat-events, #ffb53d); position: relative; touch-action: manipulation;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.cb-row {
  display: flex; align-items: flex-start; gap: 8px;
  min-height: 48px; padding: 6px 6px 6px 14px;
  background: var(--c); color: var(--ce-blockink);
  border-radius: 13px; font-weight: 650; font-size: 16px; line-height: 1.2;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.18), 0 2px 0 rgba(0,0,0,.35);
  cursor: grab;
}
.cb-sent { flex: 1; min-width: 0; display: flex; flex-wrap: wrap; align-items: center; gap: 6px 7px; min-height: 36px; }
.cb-w { white-space: nowrap; }
.cb-punc { margin-left: -5px; }
.cb-row .cb-more {
  margin-left: auto; border: 0; background: rgba(0,0,0,.12); color: var(--ce-blockink);
  min-width: 36px; min-height: 36px; border-radius: 10px; font-size: 18px; font-weight: 800; line-height: 1;
  display: inline-grid; place-items: center;
}
.cb-row .cb-more:hover { background: rgba(0,0,0,.22); }

/* a command: a tidy brick with a little tab on top, like it clicks on */
.cb-cmd > .cb-row { position: relative; }
.cb-cmd > .cb-row::before {
  content: ""; position: absolute; left: 18px; top: -5px; width: 26px; height: 7px;
  background: var(--c); border-radius: 6px 6px 0 0;
}

/* a hat: a big rounded cap, then a C that holds the body, then a foot */
.cb-hat, .cb-hatlike { display: flex; flex-direction: column; }
.cb-hatlike > .cb-row::before { display: none; }
.cb-hat > .cb-row, .cb-hatlike > .cb-row {
  border-radius: 26px 26px 6px 6px; min-height: 58px; padding: 10px 10px 10px 18px;
  font-size: 18px; font-weight: 750;
}
.cb-hat > .cb-row .cb-sent > .cb-w:first-child { font-family: var(--font-head, system-ui, sans-serif); font-weight: 800;
  font-size: 20px; letter-spacing: .01em; }
.cb-body {
  position: relative; display: flex; flex-direction: column; gap: 9px;
  margin-left: 0; padding: 12px 0 12px 22px; min-height: 30px;
  border-left: 16px solid var(--c);
}
.cb-foot { height: 20px; width: 128px; max-width: 60%; background: var(--c); border-radius: 0 10px 14px 14px;
  box-shadow: 0 2px 0 rgba(0,0,0,.35); }
.cb-empty {
  min-height: 48px; display: flex; align-items: center; padding: 0 14px; border-radius: 12px;
  border: 2px dashed color-mix(in srgb, var(--c) 55%, transparent); color: var(--ce-dim); font-size: 15px;
}
.cb-empty.ce-target { border-style: solid; color: var(--ce-ink); background: color-mix(in srgb, var(--c) 18%, transparent); }
.cb-empty::before { content: "+"; display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 10px; border-radius: 7px;
  background: color-mix(in srgb, var(--c) 30%, transparent); color: var(--ce-ink); font-weight: 900; flex: none; }
.cb-empty { cursor: pointer; }

/* a note (a -- comment line) */
.cb-note > .cb-row { background: transparent; color: var(--ce-dim); box-shadow: none; border: 1px dashed var(--ce-line);
  font-style: italic; font-weight: 500; }
.cb-note > .cb-row::before { display: none; }
.cb-note .cb-more { color: var(--ce-soft); background: rgba(255,255,255,.06); }

/* a line nobody can read yet: kept, never thrown away */
.cb-bad > .cb-row { background: #3a2a30; color: #ffd9d9; box-shadow: inset 0 0 0 2px var(--ce-bad); }
.cb-bad > .cb-row::before { background: #3a2a30; }
.cb-bad .cb-more { color: #ffd9d9; background: rgba(255,255,255,.08); }
.cb-rawtext.is-note { font-family: inherit; font-style: italic; background: transparent; padding-left: 0; }
.cb-rawtext { flex: 1; min-width: 0; font-family: var(--ce-mono); font-size: 15px; font-weight: 600; white-space: pre-wrap; word-break: break-word;
  border: 0; background: rgba(0,0,0,.25); color: inherit; border-radius: 9px; padding: 6px 10px; min-height: 36px; text-align: left; }
.cb.has-err > .cb-row { box-shadow: inset 0 0 0 3px var(--ce-bad), 0 2px 0 rgba(0,0,0,.35); }
.cb-err {
  display: flex; gap: 8px; align-items: flex-start; margin: 6px 0 2px; padding: 8px 12px;
  background: #2a1218; border: 1px solid #5c2630; color: #ffc2c2; border-radius: 12px;
  font-size: 15px; font-weight: 500; line-height: 1.35; max-width: 600px;
}
.cb-err::before { content: "!"; flex: none; width: 22px; height: 22px; border-radius: 50%; background: var(--ce-bad);
  color: #1a0b0e; font-weight: 900; display: grid; place-items: center; font-size: 14px; }

/* the pills inside the sentence: tap to change */
.cb-pill {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; max-width: 100%;
  padding: 3px 12px; border-radius: 999px; border: 2px solid rgba(0,0,0,.14);
  background: #fbf8f2; color: var(--ce-blockink); font-weight: 700; font-size: 16px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; cursor: pointer;
  box-shadow: 0 1px 0 rgba(0,0,0,.25);
}
.cb-pill:hover { background: #fff; border-color: rgba(0,0,0,.35); }
.cb-pill.t-num { font-variant-numeric: tabular-nums; min-width: 44px; justify-content: center; }
.cb-pill.t-expr, .cb-pill.t-place { font-family: var(--ce-mono); font-size: 15px; font-weight: 650; }
.cb-pill.t-text::before, .cb-pill.t-text::after { content: "\201C"; color: #8a7f99; font-weight: 800; }
.cb-pill.t-text::after { content: "\201D"; }
.cb-pill.t-key { border-radius: 9px; border-width: 2px 2px 4px; font-family: var(--ce-mono); text-transform: uppercase;
  font-size: 14px; letter-spacing: .04em; }
.cb-pill .sw { width: 18px; height: 18px; border-radius: 50%; border: 2px solid rgba(0,0,0,.25); flex: none; }
.cb-pill img { width: 24px; height: 24px; object-fit: contain; image-rendering: pixelated; border-radius: 5px;
  background: repeating-conic-gradient(#ddd 0 25%, #fff 0 50%) 0 0 / 8px 8px; flex: none; }
.cb-pill .gl { font-size: 15px; line-height: 1; }
.cb-pill .kd { width: 10px; height: 10px; border-radius: 3px; background: currentColor; opacity: .55; flex: none; }
.cb-pill.t-that { background: #1d1826; color: #fff; border-color: rgba(255,255,255,.25); }

/* states */
.cb.is-sel > .cb-row { outline: 3px solid var(--ce-ink); outline-offset: 2px; }
.cb.is-flash > .cb-row { animation: ce-flash .9s ease-out; }
@keyframes ce-flash { 0% { filter: brightness(1.6); transform: scale(1.03); } 100% { filter: none; transform: none; } }
.cb.is-held { opacity: .28; }
.cb-row.ce-pressing, .ce-pressing > .cb-row { transform: scale(.97); transition: transform .3s; }
.ce-dragging, .ce-dragging * { cursor: grabbing !important; }

/* the ghost that follows your finger */
.ce-ghost { position: absolute; left: 0; top: 0; z-index: 9999; pointer-events: none; opacity: .94;
  filter: drop-shadow(0 14px 22px rgba(0,0,0,.55)); }
.ce-ghost .cb-body, .ce-ghost .cb-foot { display: none; }
.ce-ghost .cb-err, .ce-ghost .pb-help, .ce-ghost .pb-helptext { display: none; }

/* where it will land */
.ce-drop {
  position: absolute; height: 6px; border-radius: 6px; background: var(--ce-ink); pointer-events: none; z-index: 5;
  box-shadow: 0 0 0 3px rgba(10,8,16,.8), 0 0 18px rgba(255,255,255,.55); transform: translateY(-3px);
}
.ce-drop::before { content: ""; position: absolute; left: -8px; top: -5px; width: 16px; height: 16px; border-radius: 50%;
  background: var(--ce-ink); box-shadow: 0 0 0 3px rgba(10,8,16,.8); }
.ce-drop[hidden] { display: none; }
.cb.ce-into > .cb-body { background: color-mix(in srgb, var(--c) 9%, transparent); border-radius: 0 12px 12px 0; }

.ce-emptyscript { max-width: 520px; margin: 18px auto; padding: 22px 20px; border: 2px dashed var(--ce-line);
  border-radius: 18px; color: var(--ce-soft); text-align: center; line-height: 1.45; font-size: 16px; }
.ce-emptyscript b { color: var(--ce-ink); }
.ce-emptyscript h3 { margin: 0 0 6px; font-family: var(--font-head, system-ui, sans-serif); color: var(--ce-ink); font-size: 20px; }

/* ---------------------------------------------------------------- palette */
.ce-pal {
  -webkit-user-select: none; user-select: none;
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; flex-direction: column; max-height: var(--ce-drawer);
  background: var(--ce-card); border-top: 1px solid var(--ce-line); border-radius: 20px 20px 0 0;
  box-shadow: 0 -12px 30px rgba(0,0,0,.45); transition: max-height .25s ease;
}
.ce-pal.open { max-height: 62%; }
.ce-palhead { display: flex; align-items: center; gap: 8px; padding: 10px 10px 8px; flex: none; }
.ce-grab { flex: none; min-height: 44px; padding: 0 14px; border-radius: 14px; border: 0;
  background: var(--ce-ink); color: var(--ce-blockink); font-weight: 800; font-size: 15px; white-space: nowrap; }
.ce-chips { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; flex: 1; min-width: 0; padding: 2px; }
.ce-chips::-webkit-scrollbar { display: none; }
.ce-chip {
  --c: #888; flex: none; display: inline-flex; align-items: center; gap: 7px; min-height: 44px; padding: 0 13px;
  border-radius: 999px; border: 2px solid color-mix(in srgb, var(--c) 45%, transparent);
  background: color-mix(in srgb, var(--c) 12%, var(--ce-card)); color: var(--ce-ink); font-weight: 650; font-size: 15px; white-space: nowrap;
}
.ce-chip::before { content: ""; width: 12px; height: 12px; border-radius: 50%; background: var(--c); }
.ce-chip[aria-pressed="true"] { background: var(--c); color: var(--ce-blockink); border-color: var(--c); }
.ce-chip[aria-pressed="true"]::before { background: var(--ce-blockink); }
.ce-palbody { display: none; flex-direction: column; min-height: 0; flex: 1; }
.ce-pal.open .ce-palbody { display: flex; }
.ce-search { margin: 0 10px 8px; min-height: 44px; border-radius: 12px; border: 1px solid var(--ce-line);
  background: var(--ce-bg); color: var(--ce-ink); font: inherit; font-size: 16px; padding: 0 14px; }
.ce-search::placeholder { color: var(--ce-dim); }
.ce-pallist { overflow-y: auto; overscroll-behavior: contain; padding: 4px 10px 20px; flex: 1; min-height: 0; }
.ce-palcat { margin: 14px 2px 8px; display: flex; align-items: center; gap: 8px; font-family: var(--font-head, system-ui, sans-serif);
  font-size: 17px; font-weight: 800; color: var(--ce-ink); }
.ce-palcat::before { content: ""; width: 14px; height: 14px; border-radius: 4px; background: var(--c); }
.ce-palcat:first-child { margin-top: 2px; }
.ce-palnote { margin: -4px 2px 10px; color: var(--ce-dim); font-size: 14px; line-height: 1.35; }
.pb-wrap { display: flex; flex-direction: column; margin-bottom: 9px; }
.pb-line { display: flex; align-items: flex-start; gap: 6px; }
.pb { flex: 1; min-width: 0; }
.pb .cb-row { cursor: grab; }
.pb.is-hat .cb-row { border-radius: 22px 22px 10px 10px; font-size: 17px; }
.pb .cb-pill { cursor: inherit; pointer-events: none; min-height: 32px; }
.pb-help { flex: none; width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--ce-line); background: var(--ce-card2);
  font-weight: 800; color: var(--ce-soft); margin-top: 2px; }
.pb-help[aria-expanded="true"] { background: var(--ce-ink); color: var(--ce-blockink); }
.pb-helptext { margin: 6px 50px 2px 4px; padding: 10px 12px; border-radius: 12px; background: var(--ce-card2); color: var(--ce-soft);
  font-size: 15px; line-height: 1.4; }
.pb-helptext[hidden] { display: none; }
.ce-palempty { color: var(--ce-dim); padding: 18px 4px; font-size: 15px; }

/* the bin that appears while you carry a block on a phone */
.ce-trash {
  position: absolute; left: 8px; right: 8px; bottom: 6px; z-index: 30; height: 56px;
  border-radius: 16px; display: flex; align-items: center; justify-content: center; gap: 10px;
  background: #2a1218; border: 2px dashed #7a2b37; color: #ffc2c2; font-weight: 800; font-size: 17px; white-space: nowrap;
  opacity: 0; transform: translateY(14px); transition: opacity .15s, transform .15s; pointer-events: none;
}
.ce-trash.show { opacity: 1; transform: none; }
.ce-trash.hot { background: var(--ce-bad); color: #1a0b0e; border-style: solid; border-color: var(--ce-bad); transform: scale(1.03); }
.ce-pal.is-trash { box-shadow: inset 0 0 0 3px var(--ce-bad); }

@container (min-width: 760px) {
  .ce-pal { position: relative; order: -1; width: 350px; flex: none; max-height: none; height: auto; border-radius: 0;
    border-top: 0; border-right: 1px solid var(--ce-line); box-shadow: none; }
  .ce-pal .ce-palbody { display: flex; }
  .ce-palhead { flex-direction: column; align-items: stretch; padding: 12px 10px 6px; }
  .ce-grab { display: none; }
  .ce-chips { flex-wrap: wrap; overflow: visible; }
  .ce-chip { min-height: 38px; padding: 0 11px; font-size: 14px; }
  .ce-trash { display: none; }
  .ce-pal.is-trash::after {
    content: "Drop here to delete"; position: absolute; inset: 0; display: grid; place-items: center;
    background: rgba(42,18,24,.92); color: #ffc2c2; font-weight: 800; font-size: 18px; z-index: 3;
  }
  .ce-pal.is-trash.hot::after { background: rgba(255,91,91,.92); color: #1a0b0e; }
}

/* ------------------------------------------------------------ text view */
.ce-textview { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.ce-tbar { display: flex; gap: 6px; align-items: center; padding: 8px 10px; border-bottom: 1px solid var(--ce-line); flex: none; }
.ce-tbar .ce-iconbtn { font-size: 15px; font-weight: 700; }
.ce-tbar .grow { flex: 1; }
.ce-tbar .ce-hint { color: var(--ce-dim); font-size: 14px; }
.ce-tsplit { flex: 1; min-height: 0; display: flex; position: relative; }
.ce-editor { position: relative; flex: 1; min-width: 0; display: flex; background: #0d0b14; }
.ce-gutter {
  flex: none; width: 46px; overflow: hidden; padding: 12px 0 0; text-align: right; user-select: none; -webkit-user-select: none;
  font-family: var(--ce-mono); font-size: 15px; line-height: 24px; color: #5f5873; border-right: 1px solid var(--ce-line);
}
.ce-gutterin { will-change: transform; }
.ce-gutter div div { padding-right: 9px; height: 24px; }
.ce-gutter div.err { color: #ff8b8b; font-weight: 800; background: rgba(255,91,91,.12); }
.ce-codewrap { position: relative; flex: 1; min-width: 0; overflow: hidden; }
.ce-hl, .ce-ta {
  position: absolute; inset: 0; margin: 0; padding: 12px 14px; border: 0;
  font-family: var(--ce-mono); font-size: 15px; line-height: 24px; tab-size: 2;
  white-space: pre; overflow: auto; letter-spacing: 0; word-spacing: 0; font-variant-ligatures: none;
}
.ce-hl { color: #d9d3e6; pointer-events: none; overflow: hidden; }
.ce-ta { background: transparent; color: transparent; caret-color: #fff; resize: none; outline: none; -webkit-text-fill-color: transparent; }
.ce-ta::selection { background: rgba(120,160,255,.35); }
.ce-hl .k-hat { color: var(--cat-events, #ffb53d); font-weight: inherit; }
.ce-hl .k-str { color: #ffc7e4; }
.ce-hl .k-num { color: #9cf0ff; }
.ce-hl .k-com { color: #7a7390; font-style: normal; }
.ce-hl .k-thing { color: #ffffff; text-decoration: underline; text-decoration-color: rgba(255,255,255,.28); text-underline-offset: 4px; }
.ce-hl .k-val { color: #fff3c4; }
.ce-hl .k-op { color: #b6aecb; }
.ce-hl .k-kw { color: #c9c2d8; }
.ce-hl .k-name { color: #f6f2ea; }
/* indent guides: one faint line per step in */
.ce-hl .ig { box-shadow: inset 1px 0 0 rgba(255,255,255,.10); }
.ce-measure { position: absolute; left: -9999px; top: 0; visibility: hidden; white-space: pre;
  font-family: var(--ce-mono); font-size: 15px; letter-spacing: 0; font-variant-ligatures: none; }
.ce-hl .k-err { text-decoration: underline wavy var(--ce-bad); text-decoration-thickness: 2px; text-underline-offset: 4px;
  background: rgba(255,91,91,.08); }
.ce-terrs { flex: none; max-height: min(30%, 136px); overflow: auto; border-top: 1px solid var(--ce-line); background: var(--ce-card); }
.ce-terrs:empty { display: none; }
.ce-terr { display: flex; gap: 10px; width: 100%; text-align: left; border: 0; background: transparent; padding: 10px 14px;
  color: #ffc2c2; font-size: 15px; line-height: 1.35; border-bottom: 1px solid var(--ce-line); min-height: 44px; }
.ce-terr b { color: #ff8b8b; flex: none; }

.ce-help {
  position: absolute; inset: 0; z-index: 15; background: var(--ce-card); display: flex; flex-direction: column;
}
.ce-help[hidden] { display: none; }
.ce-helphead { display: flex; align-items: center; gap: 8px; padding: 10px; border-bottom: 1px solid var(--ce-line); }
.ce-helphead h3 { margin: 0; flex: 1; font-family: var(--font-head, system-ui, sans-serif); font-size: 18px; }
.ce-helplist { overflow: auto; padding: 4px 12px 24px; flex: 1; }
.ce-helpitem { display: block; width: 100%; text-align: left; border: 0; border-left: 5px solid var(--c); background: var(--ce-card2);
  border-radius: 10px; padding: 10px 12px; margin: 0 0 8px; min-height: 44px; }
.ce-helpitem code { display: block; font-family: var(--ce-mono); font-size: 15px; color: var(--ce-ink); white-space: pre-wrap; word-break: break-word; }
.ce-helpitem span { display: block; color: var(--ce-soft); font-size: 14.5px; line-height: 1.35; margin-top: 4px; }
.ce-helpitem small { color: var(--ce-dim); font-size: 13px; }
@container (min-width: 760px) {
  .ce-help { position: relative; inset: auto; width: 320px; flex: none; border-left: 1px solid var(--ce-line); }
  .ce-help .ce-helpclose { display: none; }
}

/* ---------------------------------------------------------- the popover */
.ce-scrim { position: absolute; inset: 0; z-index: 40; background: rgba(5,4,9,.55); }
.ce-scrim[hidden] { display: none; }
.ce-pop {
  position: absolute; z-index: 41; left: 0; right: 0; bottom: 0; max-height: 78%;
  display: flex; flex-direction: column; background: var(--ce-card); color: var(--ce-ink);
  border: 1px solid var(--ce-line); border-radius: 22px 22px 0 0; box-shadow: 0 -16px 40px rgba(0,0,0,.55);
  animation: ce-up .18s ease-out;
}
@keyframes ce-up { from { transform: translateY(30px); opacity: 0; } }
.ce-pop[hidden] { display: none; }
.ce-pophead { display: flex; align-items: center; gap: 10px; padding: 14px 14px 8px; }
.ce-pophead h3 { margin: 0; flex: 1; font-family: var(--font-head, system-ui, sans-serif); font-size: 19px; }
.ce-popbody { overflow: auto; padding: 4px 14px 18px; }
.ce-popnote { color: var(--ce-soft); font-size: 15px; line-height: 1.4; margin: 0 0 12px; }
.ce-opts { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 8px; }
.ce-opt {
  display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 8px 12px; border-radius: 13px; text-align: left;
  border: 2px solid var(--ce-line); background: var(--ce-card2); font-weight: 650; font-size: 16px; overflow: hidden;
}
.ce-opt[aria-pressed="true"] { border-color: var(--ce-ink); background: #2a2438; }
.ce-opt .gl { font-size: 22px; width: 26px; text-align: center; }
.ce-opt .sw { width: 26px; height: 26px; border-radius: 50%; flex: none; border: 2px solid rgba(255,255,255,.25); }
.ce-opt img { width: 36px; height: 36px; object-fit: contain; image-rendering: pixelated; border-radius: 7px; flex: none;
  background: repeating-conic-gradient(#3a3447 0 25%, #2a2535 0 50%) 0 0 / 10px 10px; }
.ce-opt small { display: block; color: var(--ce-dim); font-weight: 500; font-size: 13px; }
.ce-keys { display: grid; grid-template-columns: repeat(auto-fill, minmax(48px, 1fr)); gap: 6px; }
.ce-keys .ce-opt { justify-content: center; padding: 0; font-family: var(--ce-mono); text-transform: uppercase; font-size: 15px; }
.ce-keys .wide { grid-column: span 2; }
.ce-dirs { display: grid; grid-template-columns: repeat(3, 72px); grid-template-rows: repeat(2, 64px); gap: 8px; justify-content: center; }
.ce-dirs .ce-opt { justify-content: center; font-size: 15px; flex-direction: column; gap: 0; padding: 4px; }
.ce-dirs .ce-opt b { font-size: 24px; line-height: 1; }
.ce-field {
  width: 100%; min-height: 52px; border-radius: 13px; border: 2px solid var(--ce-line); background: var(--ce-bg); color: var(--ce-ink);
  font: inherit; font-size: 18px; padding: 0 14px; margin: 0 0 10px;
}
.ce-field.mono { font-family: var(--ce-mono); font-size: 17px; }
.ce-field:focus { border-color: var(--ce-ink); outline: none; }
.ce-pad { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.ce-pad button { min-height: 54px; border-radius: 13px; border: 1px solid var(--ce-line); background: var(--ce-card2); font-size: 22px; font-weight: 700; }
.ce-pad button.ok { background: var(--cat-making, #33d17a); color: var(--ce-blockink); grid-row: span 2; font-size: 18px; }
.ce-pad button.fn { color: var(--ce-soft); font-size: 18px; }
.ce-row2 { display: flex; gap: 8px; margin-top: 4px; }
.ce-btn { min-height: 50px; padding: 0 18px; border-radius: 13px; border: 1px solid var(--ce-line); background: var(--ce-card2);
  font-weight: 750; font-size: 16px; }
.ce-btn.primary { background: var(--ce-ink); color: var(--ce-blockink); border-color: var(--ce-ink); flex: 1; }
.ce-btn.danger { background: #2a1218; color: #ffb3b3; border-color: #5c2630; }
.ce-menu { display: flex; flex-direction: column; gap: 8px; }
.ce-menu .ce-btn { text-align: left; display: flex; align-items: center; gap: 12px; }
.ce-menu .ce-btn i { font-style: normal; width: 24px; text-align: center; font-size: 18px; }
.ce-sub { font-size: 13px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: var(--ce-dim); margin: 14px 2px 8px; }
.ce-preview { margin: 0 0 12px; }
.ce-preview .cb-row { cursor: default; }

@container (min-width: 760px) {
  .ce-pop { left: auto; right: auto; bottom: auto; width: 380px; max-height: min(560px, 86%); border-radius: 18px;
    box-shadow: 0 20px 50px rgba(0,0,0,.6); }
  .ce-scrim { background: rgba(5,4,9,.25); }
}

.ce-toast { position: absolute; left: 50%; top: 70px; transform: translateX(-50%); z-index: 50; background: var(--ce-ink);
  color: var(--ce-blockink); padding: 10px 16px; border-radius: 12px; font-weight: 700; font-size: 15px; pointer-events: none;
  box-shadow: 0 10px 24px rgba(0,0,0,.4); max-width: 90%; text-align: center; }
.ce-toast[hidden] { display: none; }

@media (prefers-reduced-motion: reduce) {
  .ce *, .ce *::before, .ce *::after { animation: none !important; transition: none !important; }
}

/* ------------------------------------------------- round 2: C blocks, arms */
/* IF / REPEAT: a top bar, a spine down the left, a foot. ELSE IF / ELSE are
   bars on the same spine, so the whole thing reads as one piece. */
.cb-c { display: flex; flex-direction: column; }
.cb-c > .cb-row { border-radius: 13px 13px 13px 4px; }
.cb-c > .cb-body, .cb-arm > .cb-body {
  border-left: 14px solid var(--c); padding: 9px 0 9px 14px; gap: 9px; min-height: 30px;
}
.cb-arm { position: relative; display: flex; flex-direction: column; }
.cb-arm > .cb-row { border-radius: 4px 13px 13px 4px; min-height: 46px; }
.cb-arm > .cb-row .cb-sent > .cb-w:first-child { font-weight: 800; }
.cb-arm.is-sel > .cb-row { outline: 3px solid var(--ce-ink); outline-offset: 2px; }
.cb-arm.has-err > .cb-row { box-shadow: inset 0 0 0 3px var(--ce-bad), 0 2px 0 rgba(0,0,0,.35); }
.cb-cfoot {
  display: flex; gap: 6px; align-items: center; min-height: 18px; width: max-content; min-width: 112px; max-width: 100%;
  padding: 0 6px 0 18px; background: var(--c); border-radius: 0 12px 14px 14px; box-shadow: 0 2px 0 rgba(0,0,0,.35);
}
.cb-c:has(.cb-addarm) > .cb-cfoot { min-height: 40px; padding: 4px 6px 4px 18px; }
.cb-addarm {
  border: 0; min-height: 32px; padding: 0 12px; border-radius: 9px; background: rgba(0,0,0,.16); color: var(--ce-blockink);
  font-weight: 800; font-size: 14px; white-space: nowrap;
}
.cb-addarm:hover { background: rgba(0,0,0,.26); }
.cb.is-into > .cb-body, .cb-arm.is-into > .cb-body { background: color-mix(in srgb, var(--c) 10%, transparent); border-radius: 0 12px 12px 0; }
.cb.is-into > .cb-body > .cb-empty, .cb-arm.is-into > .cb-body > .cb-empty { border-style: solid; color: var(--ce-ink); }
.cb-arm.ce-into > .cb-body { background: color-mix(in srgb, var(--c) 9%, transparent); border-radius: 0 12px 12px 0; }
.ce-ghost .cb-arm, .ce-ghost .cb-cfoot { display: none; }

/* a function you made: its own colour, a notch so it looks like a label */
.cb-fn > .cb-row { border-radius: 26px 26px 6px 6px; }
.cb-fn > .cb-row .cb-sent > .cb-w:first-child { font-family: var(--font-head, system-ui, sans-serif); letter-spacing: .02em; }

/* palette: a C block shows its mouth */
.pb.is-c .cb-row { border-radius: 13px 13px 13px 4px; }
.pb-mouth { height: 14px; width: 14px; background: var(--c); }
.pb-cfoot { height: 12px; width: 88px; background: var(--c); border-radius: 0 10px 12px 12px; }
.ce-ghost .pb-mouth, .ce-ghost .pb-cfoot { display: block; }
.ce-chip.is-start { --c: var(--ce-ink); }
.ce-palcat.is-start::before { background: linear-gradient(135deg, var(--cat-events, #ffb53d), var(--cat-motion, #3bb8f5), var(--cat-making, #33d17a)); }
.ce-makefn { width: 100%; margin: 2px 0 8px; }
.ce-pallist .ce-sub { margin-top: 10px; }

/* ------------------------------------------------ text view: tips + ideas */
.ce-errtip {
  position: absolute; left: 10px; right: 10px; z-index: 6; max-width: 560px; padding: 8px 12px; border-radius: 12px;
  background: #2a1218; border: 1px solid #7a2b37; color: #ffd0d0; font-size: 15px; line-height: 1.35;
  box-shadow: 0 8px 22px rgba(0,0,0,.5); pointer-events: none;
}
.ce-errtip b { color: #ff9b9b; }
.ce-errtip[hidden] { display: none; }
.ce-sug {
  position: absolute; z-index: 7; display: flex; flex-direction: column; gap: 6px; padding: 6px;
  background: var(--ce-card); border: 1px solid #3a3348; border-radius: 14px; box-shadow: 0 14px 30px rgba(0,0,0,.55);
  max-height: 210px; overflow: auto; overscroll-behavior: contain;
}
.ce-sug[hidden] { display: none; }
.ce-sugwords { display: flex; flex-wrap: wrap; gap: 6px; }
.ce-suglines { display: flex; flex-direction: column; gap: 4px; }
.ce-sugitem {
  --c: #9aa3b5; display: inline-flex; align-items: center; gap: 8px; min-height: 40px; padding: 0 12px; border-radius: 10px;
  border: 1px solid color-mix(in srgb, var(--c) 45%, transparent); background: color-mix(in srgb, var(--c) 14%, var(--ce-card2));
  font-family: var(--ce-mono); font-size: 15px; font-weight: 650; color: var(--ce-ink); text-align: left; white-space: nowrap;
}
.ce-sugitem.t-value { border-style: dashed; }
.ce-sugitem.t-line { min-height: 38px; white-space: normal; font-weight: 550; color: var(--ce-soft); }
.ce-sugitem.t-line i { font-style: normal; color: var(--c); font-weight: 900; }
.ce-sugitem[aria-selected="true"] { background: var(--c); color: var(--ce-blockink); border-color: var(--c); }
.ce-sugitem[aria-selected="true"] i { color: var(--ce-blockink); }
@media (pointer: coarse) { .ce-sugitem { min-height: 44px; } }
.ce-help .ce-search { margin: 8px 12px 6px; }
.ce-palcat.is-start { --c: var(--ce-ink); }

/* ------------------------------------------------------- the test builder */
.ce-condkinds { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.ce-condkind { display: flex; flex-direction: column; align-items: flex-start; gap: 4px; min-height: 64px; padding: 10px 12px;
  border-radius: 13px; border: 2px solid var(--ce-line); background: var(--ce-card2); text-align: left; }
.ce-condkind b { font-size: 16px; }
.ce-condkind code { font-family: var(--ce-mono); font-size: 13.5px; color: var(--ce-soft); }
.ce-condkind:hover { border-color: var(--ce-soft); }
.ce-condnow, .ce-condprev { margin: 0 0 12px; padding: 10px 12px; border-radius: 12px; background: var(--ce-bg); border: 1px dashed var(--ce-line);
  color: var(--ce-soft); font-size: 15px; }
.ce-condprev { border-style: solid; border-color: var(--ce-soft); display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  position: sticky; top: -4px; z-index: 2; background: var(--ce-card); }
.ce-condprev code { flex: 1; min-width: 150px; }
.ce-condprev .ce-btn.primary { flex: none; min-height: 46px; }
.ce-condnow code, .ce-condprev code { font-family: var(--ce-mono); color: var(--ce-ink); font-size: 16px; font-weight: 650; word-break: break-word; }
.ce-back { min-height: 40px; margin-bottom: 10px; }
.ce-mix .ce-btn { flex: 1; }

/* touch screens (iPad too, whose wide layout used the mouse sizes): 44px targets */
@media (pointer: coarse) {
  .ce-seg button, .ce-probs { min-height: 44px; }
  .ce-chip { min-height: 44px; }
  /* a block's … is the way to duplicate or delete it: a full finger-sized button */
  .cb-row .cb-more { min-width: 44px; min-height: 44px; }
}

/* BUG HUNT: notes (# lines) read like one quiet note, not a pile of dashed boxes:
   one per line made the example games' first screens a wall of empty-looking blocks */
.cb-note > .cb-row { min-height: 36px; padding: 3px 4px 3px 12px; border: 0; border-left: 3px solid var(--ce-line);
  border-radius: 4px 10px 10px 4px; background: rgba(255,255,255,.035); font-size: 15px; align-items: center; }
.cb-note + .cb-note { margin-top: -9px; }
.ce-stack > .cb-note + .cb-note { margin-top: -18px; }
.cb-note + .cb-note > .cb-row { border-top-left-radius: 0; border-top-right-radius: 0; }
.cb-note:has(+ .cb-note) > .cb-row { border-bottom-left-radius: 0; border-bottom-right-radius: 0; }
.cb-note .cb-more { background: transparent; min-height: 30px; }
/* a long sum, list or text in a pill wraps onto more lines instead of being cut off at the edge */
.cb-pill.t-expr, .cb-pill.t-text, .cb-pill.t-place { white-space: normal; overflow-wrap: anywhere; text-align: left; border-radius: 18px; }

/* a WHEN inside a TO: a hat that waits for its block to be DOne */
.cb-infn > .cb-row { border-radius: 20px 20px 6px 6px; }
.cb-tag { align-self: center; flex: none; padding: 3px 9px; border-radius: 999px; background: rgba(0,0,0,.22);
  color: var(--ce-blockink); font-size: 14px; font-weight: 800; letter-spacing: .02em; white-space: nowrap; }
/* the Functions drawer's "WHENs inside your own blocks" card */
.ce-fnev { margin: 6px 0 12px; border: 1px solid var(--ce-line); border-radius: 14px; padding: 4px 12px 12px;
  background: rgba(255,255,255,.03); }
.ce-fnev > summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; font-weight: 800; font-size: 15px;
  color: var(--ce-ink); list-style: none; }
.ce-fnev > summary::-webkit-details-marker { display: none; }
.ce-fnev > summary::before { content: "+"; display: inline-grid; place-items: center; width: 22px; height: 22px; margin-right: 10px;
  border-radius: 7px; background: color-mix(in srgb, var(--cat-functions, #ff6680) 35%, transparent); font-weight: 900; flex: none; }
.ce-fnev[open] > summary::before { content: "\2212"; }
.ce-fnev .ce-palnote { margin: 2px 0 8px; }
.ce-fnev-code { margin: 0 0 8px; padding: 10px 12px; border-radius: 10px; background: #0f0c16; border: 1px solid var(--ce-line);
  color: var(--ce-ink); font-family: var(--ce-mono); font-size: 13px; line-height: 1.45; white-space: pre; overflow-x: auto; }
.ce-fnev-add { width: 100%; margin-bottom: 4px; }
