:root {
  --cat-motion:    #4C97FF;
  --cat-looks:     #9966FF;
  --cat-sound:     #CF63CF;
  --cat-events:    #FFBF00;
  --cat-control:   #FFAB19;
  --cat-sensing:   #5CB1D6;
  --cat-operators: #59C059;
  --cat-variables: #FF8C1A;
  --cat-myblocks:  #FF6680;
  --cat-pen:       #0FBD8C;

  --bg:        #f4f4f9;
  --panel:     #ffffff;
  --panel-2:   #fafbfd;
  --border:    #d0d4dc;
  --text:      #1f2330;
  --muted:     #6b7280;
  --drop:      #1a73e8;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", Arial, sans-serif;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  user-select: none;
  overflow: hidden;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 14px;
  background: #4d97ff;
  color: white;
  height: 38px;
}
#topbar h1 {
  font-size: 15px;
  font-weight: 600;
  margin: 0;
}
#topbar button {
  background: rgba(255,255,255,0.15);
  border: 1px solid rgba(255,255,255,0.3);
  color: white;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  margin-left: 6px;
}
#topbar button:hover { background: rgba(255,255,255,0.28); }

#app {
  display: grid;
  grid-template-columns: 64px 200px 1fr 500px;
  grid-template-areas: "tabs paletteblocks workspace stage";
  height: calc(100vh - 38px);
  gap: 0;
}

/* ---------- Palette ---------- */

#palette {
  display: contents;
}

#category-tabs {
  grid-area: tabs;
  background: var(--panel);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 6px 0;
}
.cat-tab {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 8px 0;
  cursor: pointer;
  font-size: 10px;
  color: var(--muted);
  border-left: 3px solid transparent;
  gap: 3px;
}
.cat-tab:hover { background: var(--panel-2); }
.cat-tab.active {
  background: var(--panel-2);
  border-left-color: currentColor;
  color: var(--text);
}
.cat-tab .swatch {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--swatch);
}

#palette-blocks {
  grid-area: paletteblocks;
  background: var(--panel-2);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 10px 8px 60px;
}
#palette-blocks .palette-block-wrap {
  margin-bottom: 6px;
  cursor: grab;
}
#palette-blocks .palette-block-wrap:active { cursor: grabbing; }

/* ---------- Workspace ---------- */

#workspace {
  grid-area: workspace;
  background:
    radial-gradient(circle, #d8dce5 1px, transparent 1px) 0 0 / 20px 20px,
    var(--panel-2);
  position: relative;
  overflow: auto;
}
#workspace-inner {
  position: relative;
  min-width: 100%;
  min-height: 100%;
  width: 2000px;
  height: 2000px;
}
.script {
  position: absolute;
  display: inline-flex;
  flex-direction: column;
  gap: 0;
}

#trash {
  position: absolute;
  right: 16px;
  bottom: 16px;
  width: 50px;
  height: 50px;
  background: white;
  border: 2px dashed var(--border);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  z-index: 5;
  pointer-events: auto;
}
#trash.over {
  background: #ffe5e5;
  border-color: #d33;
}

/* ---------- Stage ---------- */

#stage-panel {
  grid-area: stage;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-left: 1px solid var(--border);
  overflow: hidden;
}

#stage-toolbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-2);
}
#stage-toolbar button {
  border: 1px solid var(--border);
  background: white;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
}
#btn-flag { color: #4caf50; }
#btn-stop { color: #d33; }
#status {
  margin-left: auto;
  color: var(--muted);
  font-size: 11px;
}

#stage-wrap {
  position: relative;
  width: 480px;
  height: 360px;
  margin: 10px auto;
  background: white;
  border: 1px solid var(--border);
  border-radius: 4px;
  overflow: hidden;
}
#pen-canvas, #sprite-canvas {
  position: absolute;
  inset: 0;
  width: 480px;
  height: 360px;
}

#speech-bubble {
  position: absolute;
  background: white;
  border: 1px solid #888;
  border-radius: 14px;
  padding: 6px 12px;
  font-size: 12px;
  max-width: 200px;
  pointer-events: none;
  z-index: 3;
  box-shadow: 0 2px 4px rgba(0,0,0,0.08);
}
#speech-bubble.think { border-style: dashed; }

#ask-overlay {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  display: flex;
  align-items: center;
  gap: 8px;
  z-index: 4;
}
[hidden] { display: none !important; }
#ask-question { flex: 1; font-size: 12px; }
#ask-input {
  flex: 2;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 4px 8px;
}
#ask-submit {
  background: #4caf50;
  border: none;
  color: white;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
}

#monitors {
  padding: 4px 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 100px;
  overflow-y: auto;
}
.monitor {
  background: var(--cat-variables);
  color: white;
  padding: 3px 8px;
  border-radius: 12px;
  font-size: 12px;
}
.monitor.list {
  background: white;
  color: var(--text);
  border: 1px solid var(--cat-variables);
  padding: 4px 8px;
}
.monitor .label { font-weight: 600; margin-right: 6px; }
.monitor .value { background: rgba(255,255,255,0.3); padding: 1px 6px; border-radius: 8px; }

#variable-controls {
  padding: 8px 10px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 6px;
}
#variable-controls button {
  background: var(--cat-variables);
  color: white;
  border: none;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
}

/* ---------- Blocks ---------- */

.block {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px 10px;
  background: var(--cat-color, #888);
  color: white;
  font-size: 12px;
  font-weight: 500;
  border-radius: 4px;
  position: relative;
  min-height: 24px;
  box-shadow: 0 1px 0 rgba(0,0,0,0.15) inset, 0 0 0 1px rgba(0,0,0,0.1);
  cursor: grab;
  white-space: nowrap;
}
.block:active { cursor: grabbing; }

.block.cat-Motion    { --cat-color: var(--cat-motion); }
.block.cat-Looks     { --cat-color: var(--cat-looks); }
.block.cat-Sound     { --cat-color: var(--cat-sound); }
.block.cat-Events    { --cat-color: var(--cat-events); color: #4a3500; }
.block.cat-Control   { --cat-color: var(--cat-control); }
.block.cat-Sensing   { --cat-color: var(--cat-sensing); }
.block.cat-Operators { --cat-color: var(--cat-operators); }
.block.cat-Variables { --cat-color: var(--cat-variables); }
.block.cat-MyBlocks  { --cat-color: var(--cat-myblocks); }
.block.cat-Pen       { --cat-color: var(--cat-pen); }

/* Shape variants */
.block.shape-hat {
  border-top-left-radius: 18px;
  border-top-right-radius: 18px;
  padding-top: 10px;
}
.block.shape-cap {
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
  padding-bottom: 8px;
}
.block.shape-reporter {
  border-radius: 999px;
  padding: 3px 10px;
  min-height: 20px;
  font-size: 11px;
}
.block.shape-boolean {
  padding: 3px 14px;
  min-height: 20px;
  font-size: 11px;
  clip-path: polygon(10px 0, calc(100% - 10px) 0, 100% 50%, calc(100% - 10px) 100%, 10px 100%, 0 50%);
}

/* Stack notch + bump visuals */
.block.shape-stack::before,
.block.shape-cblock-head::before {
  content: "";
  position: absolute;
  top: -3px;
  left: 12px;
  width: 14px;
  height: 4px;
  background: var(--cat-color);
  border-radius: 0 0 4px 4px;
}
.block.shape-stack::after,
.block.shape-cblock-tail::after {
  content: "";
  position: absolute;
  bottom: -3px;
  left: 12px;
  width: 14px;
  height: 4px;
  background: var(--cat-color);
  border-radius: 0 0 4px 4px;
}
.block.shape-cap::before {
  content: "";
  position: absolute;
  top: -3px;
  left: 12px;
  width: 14px;
  height: 4px;
  background: var(--cat-color);
  border-radius: 0 0 4px 4px;
}
.block.shape-hat::after {
  content: "";
  position: absolute;
  bottom: -3px;
  left: 12px;
  width: 14px;
  height: 4px;
  background: var(--cat-color);
  border-radius: 0 0 4px 4px;
}

/* C-block */
.cblock {
  display: inline-flex;
  flex-direction: column;
  align-items: flex-start;
  position: relative;
  background: var(--cat-color, #888);
  border-radius: 4px;
  padding: 0;
  box-shadow: 0 0 0 1px rgba(0,0,0,0.1);
  color: white;
  font-size: 12px;
  cursor: grab;
}
.cblock.cat-Control   { --cat-color: var(--cat-control); }
.cblock.cat-Events    { --cat-color: var(--cat-events); }
.cblock-head {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px 10px;
  min-height: 24px;
  position: relative;
}
.cblock-substack {
  background: transparent;
  margin-left: 12px;
  padding: 4px 0 4px 12px;
  min-width: 100px;
  min-height: 28px;
  border-left: 4px solid var(--cat-color);
  display: flex;
  flex-direction: column;
  gap: 0;
}
.cblock-tail {
  position: relative;
  padding: 6px 10px;
  min-height: 14px;
  min-width: 28px;
}

/* Block inputs */
.block-input,
.cblock-head .block-input {
  background: white;
  color: var(--text);
  padding: 1px 6px;
  border-radius: 10px;
  min-width: 22px;
  display: inline-block;
  text-align: center;
  font-size: 12px;
  outline: none;
  border: none;
}
.block-input.bool-slot {
  background: rgba(0,0,0,0.2);
  color: white;
  min-width: 40px;
  min-height: 16px;
  clip-path: polygon(6px 0, calc(100% - 6px) 0, 100% 50%, calc(100% - 6px) 100%, 6px 100%, 0 50%);
  padding: 2px 10px;
}
.block-input select {
  background: rgba(255,255,255,0.85);
  color: var(--text);
  border: none;
  border-radius: 8px;
  font-size: 11px;
  padding: 1px 4px;
  outline: none;
  max-width: 150px;
}
.block-input-field {
  background: transparent;
  color: var(--text);
  border: none;
  outline: none;
  font-size: 12px;
  text-align: center;
  width: 32px;
  padding: 0;
  font-family: inherit;
}
.block-input-field.string {
  width: 80px;
  text-align: left;
}
.block-input-field::-webkit-outer-spin-button,
.block-input-field::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

/* Slots that hold a dropped reporter/boolean */
.slot-has-child {
  background: transparent !important;
  padding: 0 !important;
}
.slot-has-child > .block-input { display: none; }

/* Drag visuals */
.dragging {
  position: fixed !important;
  pointer-events: none;
  z-index: 1000;
  opacity: 0.92;
  transform: rotate(0.5deg);
  box-shadow: 0 6px 18px rgba(0,0,0,0.25);
}
.drop-indicator {
  background: var(--drop);
  height: 4px;
  margin: 1px 0;
  border-radius: 2px;
}
.slot-hover {
  outline: 2px solid var(--drop);
  outline-offset: 1px;
}

/* misc */
.placeholder {
  color: var(--muted);
  font-style: italic;
  padding: 20px;
  text-align: center;
}

::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: #c4c8d0; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #a8acb4; }
