* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: "Hiragino Maru Gothic ProN", "Yu Gothic", "Meiryo", sans-serif;
  background: linear-gradient(180deg, #fff8e7 0%, #ffe9c7 100%);
  color: #333;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 18px;
  background: linear-gradient(90deg, #fff 0%, #fff7d6 100%);
  border-bottom: 4px solid #ffd24d;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
  height: 64px;
}

.logo {
  display: flex;
  gap: 4px;
  font-size: 32px;
  font-weight: bold;
  letter-spacing: 2px;
}

.logo-p {
  display: inline-block;
  color: #fff;
  background: var(--c);
  width: 40px;
  height: 40px;
  text-align: center;
  line-height: 40px;
  border-radius: 8px;
  box-shadow: 0 3px 0 rgba(0,0,0,0.15);
  text-shadow: 1px 1px 0 rgba(0,0,0,0.15);
  transform: rotate(-3deg);
}
.logo-p:nth-child(even) { transform: rotate(3deg); }

.controls { display: flex; gap: 10px; }

.controls button {
  padding: 8px 18px;
  font-size: 18px;
  font-weight: bold;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 4px 0 rgba(0,0,0,0.2);
  transition: transform 0.05s;
}
.controls button:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,0.2); }
.btn-run { background: #4ec84e; color: white; }
.btn-stop { background: #e84545; color: white; }
.btn-reset { background: #ffb13a; color: white; }

.sample-select {
  padding: 8px 14px;
  font-size: 16px;
  font-weight: bold;
  font-family: inherit;
  border: none;
  border-radius: 24px;
  cursor: pointer;
  background: #fff;
  color: #d04a9a;
  box-shadow: 0 4px 0 rgba(0,0,0,0.2);
  border: 2px solid #d04a9a;
}

.workspace {
  display: grid;
  grid-template-columns: minmax(500px, 1fr) 240px minmax(320px, 1fr);
  gap: 12px;
  padding: 12px;
  height: calc(100vh - 64px);
}

/* Stage */
.stage-area {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.stage-frame {
  background: #fff;
  border: 6px solid #ffd24d;
  border-radius: 16px;
  padding: 6px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.1);
}
.stage {
  position: relative;
  width: 480px;
  height: 360px;
  background: linear-gradient(180deg, #c9e9ff 0%, #e9f7d4 100%);
  border-radius: 10px;
  overflow: hidden;
}
#penCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
.character {
  position: absolute;
  width: 60px;
  height: 60px;
  margin-left: -30px;
  margin-top: -30px;
  transition: left 0.1s linear, top 0.1s linear, transform 0.1s linear;
  pointer-events: none;
}
.char-body {
  font-size: 48px;
  line-height: 60px;
  text-align: center;
  filter: drop-shadow(2px 3px 2px rgba(0,0,0,0.2));
}
.char-bubble {
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: white;
  border: 2px solid #333;
  border-radius: 12px;
  padding: 4px 10px;
  font-size: 14px;
  white-space: nowrap;
  display: none;
  margin-bottom: 4px;
}
.char-bubble.show { display: block; }
.char-bubble::after {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -6px;
  border: 6px solid transparent;
  border-top-color: white;
}

.char-picker {
  margin-top: 10px;
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
}
.char-btn {
  font-size: 24px;
  width: 44px;
  height: 44px;
  border: 3px solid #ffd24d;
  border-radius: 50%;
  background: white;
  cursor: pointer;
}
.char-btn:hover { background: #fff7d6; }
.char-btn.active { background: #ffd24d; }

/* Palette */
.palette {
  background: #fff;
  border-radius: 16px;
  padding: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.cat-tabs {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 2px;
  margin-bottom: 8px;
}
.cat-tab {
  border: none;
  padding: 6px 0;
  font-size: 11px;
  font-weight: bold;
  font-family: inherit;
  background: #eee;
  border-radius: 8px 8px 0 0;
  cursor: pointer;
}
.cat-tab[data-cat="motion"] { background: #c8e0ff; }
.cat-tab[data-cat="looks"]  { background: #e0c8ff; }
.cat-tab[data-cat="sound"]  { background: #ffd0d0; }
.cat-tab[data-cat="control"]{ background: #ffe6a8; }
.cat-tab[data-cat="pen"]    { background: #c8f0c8; }
.cat-tab[data-cat="vars"]   { background: #ffd6ee; }
.cat-tab.active {
  outline: 3px solid #ffb13a;
  transform: translateY(-2px);
}
.block-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* Blocks */
.block {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px;
  padding: 8px 12px;
  border-radius: 6px;
  color: white;
  font-weight: bold;
  font-size: 14px;
  cursor: grab;
  user-select: none;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,0.15), 0 2px 3px rgba(0,0,0,0.15);
  position: relative;
  min-height: 32px;
}
.block:active { cursor: grabbing; }
.block.cat-motion  { background: #4a8fe0; }
.block.cat-looks   { background: #9b6bd9; }
.block.cat-sound   { background: #e06b6b; }
.block.cat-control { background: #e0a040; }
.block.cat-pen     { background: #4ab14a; }
.block.cat-vars    { background: #d04a9a; }

.block input {
  padding: 2px 6px;
  border: none;
  border-radius: 12px;
  font-family: inherit;
  font-size: 13px;
  text-align: center;
  width: 56px;
}
.block input.param-text { width: 100px; }
.block input.param-color { width: 36px; height: 22px; padding: 0; }

.block.dragging { opacity: 0.5; }
.block.running {
  outline: 4px solid #ffeb3b;
  outline-offset: -2px;
}

/* C-shape (control with body) */
.block.c-block {
  flex-direction: column;
  align-items: stretch;
  padding: 0;
  background: transparent;
  box-shadow: none;
}
.c-head, .c-foot {
  background: #e0a040;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 4px;
  font-weight: bold;
  color: white;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,0.15);
}
.c-head { border-radius: 6px 6px 0 0; }
.c-foot { border-radius: 0 0 6px 6px; min-height: 14px; padding: 4px 12px; }
.c-body {
  background: #e0a040;
  padding: 4px 4px 4px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 32px;
}
.c-body .drop-zone {
  min-height: 24px;
  background: rgba(255,255,255,0.2);
  border-radius: 4px;
  border: 2px dashed rgba(255,255,255,0.5);
}
.c-body.has-children .drop-zone { display: none; }

/* Script area */
.script-area {
  background: #fff;
  border-radius: 16px;
  padding: 12px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.08);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.script-header {
  margin-bottom: 4px;
}
.hat-block {
  display: inline-block;
  background: #6b6b6b;
  color: white;
  padding: 8px 16px;
  border-radius: 16px 16px 4px 4px;
  font-weight: bold;
}
.script {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  background: #f5f5f5;
  border-radius: 0 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-height: 100px;
}
.script.drag-over { background: #fff3cd; }
.script:empty::before {
  content: "ここにブロックをドラッグしてね";
  color: #999;
  text-align: center;
  padding: 40px 0;
  display: block;
  font-size: 14px;
}

.trash {
  margin-top: 8px;
  padding: 12px;
  text-align: center;
  background: #ffe0e0;
  border: 3px dashed #e84545;
  border-radius: 12px;
  font-weight: bold;
  color: #e84545;
}
.trash.drag-over { background: #ffb3b3; }

.drop-zone.drag-over { background: rgba(255,255,255,0.5)!important; }

/* Block delete X on script blocks */
.script .block .x-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  background: #fff;
  color: #e84545;
  border: 2px solid #e84545;
  border-radius: 50%;
  font-size: 11px;
  font-weight: bold;
  line-height: 14px;
  text-align: center;
  cursor: pointer;
  display: none;
}
.script .block:hover > .x-btn { display: block; }
