/* ==========================================================================================================
   Block program editor (js/ui/blocks.js). Layout is driven by the container width (container queries):
   wide: palette on the left, narrow: palette on top. The workspace scrolls inside its own box.
   ========================================================================================================== */

.be-host { container-type: inline-size; container-name: be; width: 100%; height: 100%; min-height: 0; }
.be {
  --gap: 12px;
  display: grid; gap: var(--gap);
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  height: 100%; min-height: 0; max-width: 100%;
  user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.be[data-readonly="1"] { grid-template-rows: minmax(0, 1fr); }
/* 3.0.1: while the program runs the editor is read-only and has no palette: the program gets the whole width (before, on a
   wide editor it fell into the 200 px palette column and the blocks were squeezed, texts wrapped into three lines) */
.be[data-readonly="1"], .be.is-tight[data-readonly="1"] { grid-template-columns: minmax(0, 1fr); }
@container be (min-width: 620px) {
  .be { grid-template-columns: 200px minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); }
  .be .be-palette { flex-direction: column; align-self: stretch; overflow-y: auto; }
  .be .pal-grid { flex-direction: column; flex-wrap: nowrap; }
  .be .pal-item { flex: 0 0 auto; width: 100%; }
  .be .pal-hint { display: block; }
}
/* wide editor but a short one: the side palette would need scrolling -> two compact columns (set by blocks.js) */
@container be (min-width: 620px) {
  .be.is-tight .be-palette { padding: 8px; }
  .be.is-tight .pal-hint { display: none; }
  .be.is-tight .pal-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
  .be.is-tight .pal-item { flex-direction: column; justify-content: center; text-align: center; gap: 2px; padding: 6px 3px; min-width: 0; min-height: 64px; font-size: 0.78rem; border-radius: 12px; box-shadow: 0 3px 0 var(--cat-dark); }
  .be.is-tight .pal-item:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--cat-dark); }
  .be.is-tight .pal-item .pal-icon { width: 26px; height: 26px; }
  .be.is-tight .pal-item .pal-label { overflow-wrap: normal; word-break: keep-all; hyphens: manual; }
}
@container be (max-width: 619px) {
  .be .pal-hint { display: none; }
  .be .pal-head { display: none; }
  /* compact palette: small square tiles (icon above the label), as many per row as fit */
  .be .be-palette { padding: 8px; }
  .be .pal-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 6px; }
  .be .pal-item { flex-direction: column; justify-content: center; text-align: center; gap: 2px; padding: 6px 3px; min-width: 0; min-height: 64px; font-size: 0.78rem; border-radius: 12px; box-shadow: 0 3px 0 var(--cat-dark); }
  .be .pal-item:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--cat-dark); }
  .be .pal-item .pal-icon { width: 26px; height: 26px; }
  .be .pal-item .pal-label { overflow-wrap: normal; word-break: keep-all; hyphens: manual; }
}
@container be (max-width: 619px) {
  .be .be-undo .bar-text, .be .be-redo .bar-text { display: none; }
}
@container be (max-width: 380px) {
  .be .bar-text { display: none; }
}

/* ---- palette */
.be-palette {
  position: relative; display: flex; flex-direction: column; gap: 8px;
  background: var(--surface-2); border: 2px solid var(--border); border-radius: var(--r-lg);
  padding: 10px; min-width: 0;
  transition: border-color 0.15s, background 0.15s;
}
.be-palette[hidden] { display: none; }
.pal-head { padding: 2px 4px 0; }
.pal-title { margin: 0; font-size: var(--fs-md); }
.pal-hint { margin: 2px 0 0; font-size: var(--fs-xs); color: var(--text-muted); line-height: 1.3; }
.pal-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.pal-item {
  --cat: var(--blk-move); --cat-dark: var(--blk-move-dark); --on-cat: var(--on-blk);
  flex: 1 1 128px; min-width: 112px; min-height: 56px;
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
  background: var(--cat); color: var(--on-cat); border: 0; border-radius: 14px;
  box-shadow: 0 4px 0 var(--cat-dark);
  font-size: var(--fs-md); font-weight: 800; text-align: left; line-height: 1.1;
  cursor: grab; touch-action: none; -webkit-user-select: none; user-select: none;
  transition: transform 0.08s, box-shadow 0.08s, filter 0.15s;
}
.pal-item:hover { filter: brightness(1.06); }
.pal-item:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--cat-dark); }
.pal-item .pal-icon { flex: none; display: inline-flex; width: 30px; height: 30px; }
.pal-item .pal-icon svg { width: 100%; height: 100%; }
.pal-item .pal-label { min-width: 0; overflow-wrap: anywhere; }
.pal-item.is-disabled { opacity: 0.45; filter: grayscale(0.4); cursor: not-allowed; }
.pal-trash { display: none; align-items: center; justify-content: center; gap: 8px; min-height: 56px; font-weight: 800; color: var(--danger-dark); }
.be-palette.is-trash { border-color: var(--danger); background: var(--danger-soft); }
.be-palette.is-trash .pal-grid, .be-palette.is-trash .pal-head { display: none; }
.be-palette.is-trash .pal-trash { display: flex; }

/* ---- category colours */
.cat-move { --cat: var(--blk-move); --cat-dark: var(--blk-move-dark); --cat-soft: var(--blk-move-soft); --on-cat: var(--on-blk); }
.cat-loop { --cat: var(--blk-loop); --cat-dark: var(--blk-loop-dark); --cat-soft: var(--blk-loop-soft); --on-cat: var(--on-blk-loop); }
.cat-cond { --cat: var(--blk-cond); --cat-dark: var(--blk-cond-dark); --cat-soft: var(--blk-cond-soft); --on-cat: var(--on-blk); }
.cat-proc { --cat: var(--blk-proc); --cat-dark: var(--blk-proc-dark); --cat-soft: var(--blk-proc-soft); --on-cat: var(--on-blk); }
.cat-count { --cat: var(--blk-count); --cat-dark: var(--blk-count-dark); --cat-soft: var(--blk-count-soft); --on-cat: var(--on-blk); }

/* ---- workspace */
.be-work { display: flex; flex-direction: column; gap: 8px; min-width: 0; min-height: 0; }
.be-bar { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.be-bar[hidden] { display: none; }
.be-count {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 14px;
  background: var(--surface); border: 2px solid var(--border-strong); border-radius: var(--r-pill);
  font-weight: 800; font-size: var(--fs-md); font-variant-numeric: tabular-nums;
}
.be-count.is-near { border-color: var(--warning); background: var(--warning-soft); }
.be-count.is-full { border-color: var(--danger); background: var(--danger-soft); color: var(--danger-dark); }
.be-count.shake { animation: be-shake 0.45s; }
@keyframes be-shake { 0%, 100% { transform: none; } 20% { transform: translateX(-6px); } 40% { transform: translateX(6px); } 60% { transform: translateX(-4px); } 80% { transform: translateX(4px); } }
.be-bar .btn { min-height: 44px; }
.be-msg { display: none; padding: 8px 12px; border-radius: var(--r-md); font-weight: 700; font-size: var(--fs-sm); }
.be-msg.is-shown { display: block; }
.be-msg.tone-warn { background: var(--warning-soft); color: #5c4200; border: 2px solid var(--warning); }
.be-msg.tone-info { background: var(--info-soft); color: var(--text); border: 2px solid var(--primary-soft); }

.be-scroll {
  flex: 1 1 auto; min-height: 140px; overflow: auto; overscroll-behavior: contain;
  background: var(--surface-2); border: 2px solid var(--border); border-radius: var(--r-lg);
  padding: 10px; -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}
.be-panels { display: grid; gap: 12px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 290px), 1fr)); align-items: start; }
.panel { background: var(--surface); border: 2px solid var(--border); border-radius: var(--r-lg); padding: 8px; min-width: 0; }
.panel-head { display: flex; align-items: center; gap: 8px; padding: 2px 6px 8px; }
.panel-icon { display: inline-flex; width: 28px; height: 28px; align-items: center; justify-content: center; border-radius: 50%; background: var(--primary); color: var(--on-primary); padding: 5px; }
.panel-icon svg { width: 100%; height: 100%; }
.panel-proc .panel-icon { background: var(--blk-proc); }
.panel-proc { border-color: var(--blk-proc-soft); }
.panel-title { margin: 0; font-size: var(--fs-md); }

/* ---- lanes, slots */
.lane { display: flex; flex-direction: column; min-width: 0; }
.lane-nested { padding: 6px 6px 6px 10px; background: var(--cat-soft); border-left: 14px solid var(--cat); }
.lane-empty { padding: 10px; text-align: center; font-size: var(--fs-sm); color: var(--text-muted); font-style: italic; }
.slot {
  position: relative; display: flex; align-items: center; justify-content: center;
  height: 10px; margin: 0; border-radius: 8px; cursor: pointer; outline: none;
  transition: height 0.12s var(--ease), background 0.12s;
}
.slot::before { content: ""; position: absolute; left: 0; right: 0; top: -13px; bottom: -13px; }
.slot .slot-label { display: none; font-size: var(--fs-sm); font-weight: 800; color: var(--primary-dark); pointer-events: none; }
.slot:hover { background: var(--primary-soft); }
.slot.is-selected {
  height: 40px; background: var(--primary-soft); border: 3px dashed var(--primary);
}
.slot.is-selected .slot-label { display: inline; }
.slot.is-empty { height: 54px; border: 3px dashed var(--border-strong); background: transparent; }
.slot.is-empty .slot-label { display: inline; color: var(--text-muted); font-weight: 700; text-align: center; padding: 0 8px; }
.slot.is-empty.is-selected { border-color: var(--primary); background: var(--primary-soft); }
.slot.is-empty.is-selected .slot-label { color: var(--primary-dark); }
.slot:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; }

.be.is-dragging .slot { height: 24px; background: rgba(36, 89, 214, 0.08); }
.be.is-dragging .slot.is-empty { height: 54px; }
.be.is-dragging .slot::before { top: -6px; bottom: -6px; }
.be.is-dragging .slot .slot-label { display: none; }
.be.is-dragging .slot.is-empty .slot-label { display: inline; }
.be.is-dragging .slot.is-selected { border-style: dashed; }
.be.is-dragging .slot.is-target {
  height: 46px; background: var(--accent-soft); border: 3px solid var(--accent);
}
.be.is-dragging .slot.is-target .slot-label { display: inline; color: #6b3a00; }
.be.is-dragging .blk.is-dragging { opacity: 0.35; }
body.be-no-select { user-select: none; -webkit-user-select: none; cursor: grabbing; }
body.be-no-select * { cursor: grabbing !important; }

/* ---- blocks */
.blk { position: relative; border-radius: 14px; outline: none; }
.blk-head {
  display: flex; align-items: center; flex-wrap: wrap; gap: 6px; min-height: 54px; padding: 4px 8px 4px 0;
  background: var(--cat); color: var(--on-cat); border-radius: 14px; box-shadow: 0 4px 0 var(--cat-dark);
  font-weight: 800; font-size: var(--fs-md); cursor: pointer;
}
.is-container > .blk-head { border-bottom-left-radius: 0; border-bottom-right-radius: 0; box-shadow: none; }
.blk-handle {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: 44px; min-height: 48px; padding: 0; border: 0; background: transparent; color: inherit; opacity: 0.8;
  cursor: grab; touch-action: none;
}
.blk-handle svg { width: 22px; height: 22px; }
.be[data-readonly="1"] .blk-head { padding-left: 12px; cursor: default; }
.blk-icon { flex: none; display: inline-flex; width: 28px; height: 28px; }
.blk-icon svg { width: 100%; height: 100%; }
.blk-label { margin-right: 4px; line-height: 1.1; }
.rep-x, .rep-count { font-size: 1.2em; }
.blk-foot { height: 16px; background: var(--cat); border-radius: 0 0 14px 14px; box-shadow: 0 4px 0 var(--cat-dark); }
.lane-sep {
  padding: 3px 14px; background: var(--cat); color: var(--on-cat); font-weight: 800; font-size: var(--fs-sm);
  cursor: pointer;
}
.blk .lane-sep:first-of-type { display: block; }
.lane-sep-then { padding-top: 5px; }

.blk.is-selected > .blk-head { box-shadow: 0 0 0 3px #ffffff, 0 0 0 6px var(--primary-dark); position: relative; z-index: 1; }
.blk.is-selected.is-container > .blk-head { box-shadow: 0 0 0 3px #ffffff, 0 0 0 6px var(--primary-dark); }
.blk:focus-visible > .blk-head { outline: 4px solid var(--focus); outline-offset: 2px; }
.blk.is-invalid > .blk-head { background-image: repeating-linear-gradient(45deg, rgba(0, 0, 0, 0.12) 0 8px, transparent 8px 16px); outline: 3px dashed var(--danger); outline-offset: -3px; }
.blk.is-running > .blk-head, .blk.is-running > .blk-foot {
  box-shadow: 0 0 0 4px #ffd23f, 0 0 18px 4px rgba(255, 210, 63, 0.9);
  position: relative; z-index: 2;
}
.blk.is-running.hl-true > .blk-head { box-shadow: 0 0 0 4px #3ddc84, 0 0 18px 4px rgba(61, 220, 132, 0.8); }
.blk.is-running.hl-false > .blk-head { box-shadow: 0 0 0 4px #ff6b7d, 0 0 18px 4px rgba(255, 107, 125, 0.8); }
.blk.just-added > .blk-head { animation: be-pop 0.5s var(--ease-bounce); }
@keyframes be-pop { from { transform: scale(0.85); opacity: 0.4; } to { transform: none; opacity: 1; } }

/* stepper, condition chip, toolbar */
.stepper { display: inline-flex; align-items: center; background: rgba(255, 255, 255, 0.3); border-radius: 12px; }
.step-btn {
  width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 12px; background: transparent; color: inherit; cursor: pointer; padding: 0;
}
.step-btn svg { width: 22px; height: 22px; }
.step-btn:hover:not(:disabled) { background: rgba(255, 255, 255, 0.35); }
.step-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.step-val { min-width: 34px; text-align: center; font-size: 1.35rem; font-weight: 900; font-variant-numeric: tabular-nums; border-radius: 8px; line-height: 44px; }
.cond-chip {
  display: inline-flex; align-items: center; gap: 6px; min-height: 44px; padding: 0 10px 0 14px;
  background: #ffffff; color: var(--text); border: 3px solid var(--cat-dark); border-radius: 12px;
  font: inherit; font-weight: 800; cursor: pointer; text-align: left;
}
span.cond-chip { cursor: default; padding-right: 14px; }
.cond-chip svg { width: 18px; height: 18px; flex: none; }
.blk-tools { display: inline-flex; gap: 4px; margin-left: auto; }
.blk-tool {
  width: 44px; height: 44px; display: inline-flex; align-items: center; justify-content: center; padding: 0;
  border: 0; border-radius: 12px; background: rgba(255, 255, 255, 0.28); color: inherit; cursor: pointer;
}
.blk-tool svg { width: 22px; height: 22px; }
.blk-tool:hover:not(:disabled) { background: rgba(255, 255, 255, 0.5); }
.blk-tool:disabled { opacity: 0.3; cursor: not-allowed; }
.blk-tool.tool-del { background: #ffffff; color: var(--danger); }
.blk-tool.tool-del:hover { background: var(--danger-soft); }

/* ---- drag ghost */
.be-ghost {
  position: fixed; left: 0; top: 0; z-index: 1200; pointer-events: none; opacity: 0.94;
  filter: drop-shadow(0 12px 16px rgba(0, 0, 0, 0.3)); max-height: 150px; overflow: hidden; border-radius: 14px;
  --cat: var(--blk-move);
}
.be-ghost .blk, .be-ghost .pal-item { width: 100%; }
.be-ghost .pal-item { cursor: grabbing; }
.be-ghost.is-trash { opacity: 0.6; transform-origin: center; }

/* ---- condition popover */
.cond-popover {
  position: fixed; z-index: 1100; width: min(340px, calc(100vw - 16px));
  background: var(--surface); color: var(--text); border: 3px solid var(--cat-dark, var(--blk-cond-dark)); border-radius: 18px;
  box-shadow: var(--sh-3); padding: 12px; display: flex; flex-direction: column; gap: 8px; animation: pop-in 0.18s var(--ease);
}
.cp-title { font-weight: 900; padding: 0 2px; }
.cp-options { display: flex; flex-direction: column; gap: 6px; }
.cp-option {
  display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 6px 12px; border: 2px solid var(--border-strong);
  background: var(--surface); border-radius: 12px; font: inherit; font-weight: 800; text-align: left; cursor: pointer;
}
.cp-option .cp-dot { width: 22px; height: 22px; border-radius: 50%; border: 3px solid var(--border-strong); flex: none; background: var(--surface); }
.cp-option.is-on { border-color: var(--blk-cond-dark); background: var(--blk-cond-soft); }
.cp-option.is-on .cp-dot { border-color: var(--blk-cond-dark); background: var(--blk-cond-dark); box-shadow: inset 0 0 0 3px var(--blk-cond-soft); }
.cp-row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.cp-not {
  display: inline-flex; align-items: center; gap: 10px; min-height: 48px; padding: 4px 12px 4px 6px; border: 2px solid var(--border-strong);
  background: var(--surface); border-radius: 999px; font: inherit; font-weight: 900; cursor: pointer;
}
.cp-not-track { width: 52px; height: 30px; border-radius: 999px; background: var(--border-strong); position: relative; flex: none; transition: background 0.15s; }
.cp-not-knob { position: absolute; left: 3px; top: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; box-shadow: var(--sh-1); transition: transform 0.15s var(--ease); }
.cp-not.is-on { border-color: var(--danger); background: var(--danger-soft); }
.cp-not.is-on .cp-not-track { background: var(--danger); }
.cp-not.is-on .cp-not-knob { transform: translateX(22px); }
.cp-preview { flex: 1 1 140px; font-weight: 800; color: var(--blk-cond-dark); }
.cp-done { align-self: flex-end; }

@media (prefers-reduced-motion: reduce) {
  .blk.just-added > .blk-head, .be-count.shake, .cond-popover { animation: none; }
}

/* 3.0: distance sensor in the condition popover */
.cp-dist, .cp-colors { display: flex; flex-direction: column; gap: 6px; padding: 8px; border-radius: 12px; background: var(--blk-cond-soft); }
.cp-dist[hidden], .cp-colors[hidden] { display: none; }
.cp-colors-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cp-dist-help { margin: 0; font-size: 0.9rem; color: var(--text-muted, var(--text)); }
.cp-dist-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.cp-dist-word { font-weight: 800; }
.cp-ops { display: inline-flex; gap: 4px; }
.cp-op {
  min-width: 44px; min-height: 44px; border-radius: 10px; border: 2px solid var(--border-strong); background: var(--surface);
  font: inherit; font-weight: 900; font-size: 1.25rem; cursor: pointer;
}
.cp-op.is-on { border-color: var(--blk-cond-dark); background: var(--blk-cond-dark); color: #fff; }
.cp-num { min-width: 28px; text-align: center; font-weight: 900; font-size: 1.25rem; }

/* 3.4: colour sensor in the condition popover and on the condition chip */
.cp-color {
  display: inline-flex; flex-direction: column; align-items: center; gap: 2px; min-width: 58px; min-height: 58px; padding: 4px 6px;
  border-radius: 12px; border: 2px solid var(--border-strong); background: var(--surface); font: inherit; font-weight: 800; font-size: 0.85rem; cursor: pointer;
}
.cp-color.is-on { border-color: var(--blk-cond-dark); box-shadow: 0 0 0 2px var(--blk-cond-dark); }
.cp-color .color-swatch { display: block; }
.cond-chip svg.color-swatch { width: 20px; height: 20px; border-radius: 6px; box-shadow: 0 0 0 1.5px rgba(255, 255, 255, 0.85); }
