:root {
  --bg: #EEF2FA;
  --panel: #FFFFFF;
  --ink: #2B3A55;
  --muted: #7A879E;
  --line: #DCE3EF;
  --accent: #7C5CFF;
  --ws: #F8FAFE;
  --dot: #D6DDEA;
  --block-w: 170px;
  --block-h: 60px;
  /* puzzle shape: a notch cut into the top, a matching tab under the bottom */
  --notch-top: 0 0, 16px 0, 22px 9px, 44px 9px, 50px 0, 100% 0;
  --flat-top: 0 0, 100% 0;
  --tab-bottom: 100% 100%, 50px 100%, 44px calc(100% + 9px), 22px calc(100% + 9px), 16px 100%, 0 100%;
  --flat-bottom: 100% 100%, 0 100%;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: 'Baloo 2', ui-rounded, 'Arial Rounded MT Bold', system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overflow: hidden;
  display: grid;
  grid-template-rows: 68px 1fr;
}
button { font: inherit; color: inherit; }

/* SVG icon classes */
svg .s { fill: none; stroke: #fff; stroke-width: 5; stroke-linecap: round; stroke-linejoin: round; }
svg .f { fill: #fff; }

/* ---------- Top bar ---------- */
.topbar {
  background: var(--accent);
  display: flex; align-items: center; gap: 10px;
  padding: 0 14px; color: #fff;
}
.logo { font-weight: 800; font-size: 26px; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.spacer { flex: 1; }
.proj-name-input {
  height: 42px; width: 200px; min-width: 0; border: 0; border-radius: 14px;
  background: rgba(255,255,255,.18); color: #fff;
  font: inherit; font-size: 18px; font-weight: 700; padding: 0 14px;
}
.proj-name-input::placeholder { color: rgba(255,255,255,.6); }
.proj-name-input:focus { outline: 3px solid #FFD23F; background: rgba(255,255,255,.28); }
.tbtn.save { background: #FFD23F; box-shadow: inset 0 -4px 0 #E0AE1F; }
.tbtn.save svg .s { stroke: #6B4E00; }
.logo-mark { width: 46px; height: 46px; filter: drop-shadow(0 2px 2px rgba(0,0,0,.2)); }
.logo-name { font-weight: 700; letter-spacing: .3px; }
.logo-name b { font-weight: 800; color: #FFD23F; }
.sep { width: 12px; }
.tbtn {
  width: 50px; height: 50px; border-radius: 16px; border: 0;
  background: rgba(255,255,255,.18);
  display: grid; place-items: center; cursor: pointer;
  transition: transform .1s;
}
.tbtn:active { transform: scale(.92); }
.tbtn svg { width: 28px; height: 28px; }
.tbtn.flag { background: #3CC46A; width: 72px; box-shadow: inset 0 -4px 0 #2A9B50; }
.tbtn.stop { background: #FF5C5C; width: 72px; box-shadow: inset 0 -4px 0 #D63B3B; }
.tbtn.flag svg, .tbtn.stop svg { width: 34px; height: 34px; }
.tbtn.off { opacity: .45; }
.lang-btn {
  height: 50px; border: 0; border-radius: 16px; padding: 5px; cursor: pointer;
  background: rgba(255,255,255,.18); display: flex; gap: 4px;
}
.lang-btn span {
  display: grid; place-items: center; min-width: 40px; border-radius: 12px;
  font-size: 17px; font-weight: 800; color: rgba(255,255,255,.75);
}
.lang-btn span.active { background: #fff; color: var(--accent); }
.tbtn.flag.running { animation: pulse .8s infinite alternate; }
@keyframes pulse { to { filter: brightness(1.25); transform: scale(1.06); } }

/* ---------- Layout ---------- */
main {
  display: grid;
  grid-template-columns: 356px 1fr minmax(360px, 34vw);
  min-height: 0;
}
body.big-stage main { grid-template-columns: 356px 1fr 52vw; }

.left {
  position: relative;
  display: grid; grid-template-columns: 92px 1fr;
  min-height: 0;
  background: var(--panel);
  border-right: 1px solid var(--line);
}
.cats {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 10px 0; overflow-y: auto;
  border-right: 1px solid var(--line);
}
.cat {
  border: 0; background: none; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  width: 80px; padding: 6px 0; border-radius: 16px;
  font-size: 14px; font-weight: 700; color: var(--muted);
}
.cat .dot {
  width: 52px; height: 52px; border-radius: 50%;
  background: var(--c); box-shadow: inset 0 -4px 0 var(--d);
  display: grid; place-items: center;
}
.cat .dot svg { width: 30px; height: 30px; }
.cat.active { background: color-mix(in srgb, var(--c) 18%, white); color: var(--ink); }

.palette { overflow-y: auto; padding: 14px 10px 30px; }
.palette h2 { margin: 0 0 12px 4px; font-size: 22px; line-height: 1; color: var(--c); }
.pal-grid { display: grid; grid-template-columns: 1fr; gap: 20px; justify-items: start; padding-left: 4px; }
.pal-item {
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  font-size: 12px; line-height: 1.1; font-weight: 600; color: var(--muted); text-align: center;
}

.trash {
  position: absolute; inset: 0; z-index: 20;
  display: none; place-items: center;
  background: rgba(255, 92, 92, .10);
  border: 4px dashed #FF5C5C;
  pointer-events: none;
}
.trash svg { width: 90px; height: 90px; }
.trash svg .s { stroke: #FF5C5C; }
body.dragging-ws .trash { display: grid; }
body.over-trash .trash { background: rgba(255, 92, 92, .25); }

/* ---------- Workspace ---------- */
.workspace {
  position: relative; overflow: auto;
  background-color: var(--ws);
  background-image: radial-gradient(var(--dot) 1.6px, transparent 1.7px);
  background-size: 26px 26px;
  background-attachment: local;
}
.canvas { position: relative; width: 2400px; height: 1600px; }
.script { position: absolute; }
.chain { display: flex; flex-direction: column; align-items: flex-start; }

/* ---------- Blocks (stack top to bottom, like Scratch) ---------- */
.block {
  position: relative; flex: none;
  min-width: var(--block-w); height: var(--block-h);
  border-radius: 12px;
  background: var(--c);
  box-shadow: inset 0 -5px 0 var(--d);
  display: flex; flex-direction: column; align-items: stretch;
  cursor: grab; touch-action: none;
  clip-path: polygon(var(--notch-top), var(--tab-bottom));
}
/* the little puzzle tab that fits into the notch of the block below (cut to shape by clip-path) */
.block::after {
  content: ''; position: absolute;
  left: 16px; bottom: -9px;
  width: 34px; height: 12px;
  background: var(--d);
}
.block-head {
  height: var(--block-h); flex: none;
  display: flex; flex-direction: row; align-items: center; gap: 8px;
  padding: 0 12px 4px 10px;
}
.block-head svg { width: 34px; height: 34px; flex: none; }
.block-text {
  color: #fff; font-size: 17px; font-weight: 800; line-height: 1; white-space: nowrap;
  text-shadow: 0 1px 0 rgba(0,0,0,.18);
}
.badge {
  background: #fff; color: var(--ink);
  border-radius: 999px; min-width: 36px; max-width: 110px; height: 28px; padding: 0 9px; margin-left: auto;
  font-weight: 800; font-size: 17px; line-height: 29px; text-align: center;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.canvas .badge { cursor: pointer; }
.badge .note-dot { display: inline-block; width: 18px; height: 18px; border-radius: 50%; vertical-align: -3px; }

/* start blocks get a rounded "hat" top, end blocks a rounded bottom */
.shape-hat { clip-path: polygon(var(--flat-top), var(--tab-bottom)); height: calc(var(--block-h) + 10px); border-radius: 34px 34px 12px 12px / 22px 22px 12px 12px; padding-top: 10px; }
.shape-hat > .block-head { height: var(--block-h); }
.shape-cap { clip-path: polygon(var(--notch-top), var(--flat-bottom)); border-radius: 12px 12px 24px 24px / 12px 12px 18px 18px; }
.shape-cap::after { display: none; }

/* Repeat: a "C" with an open mouth that wraps the blocks inside it */
.shape-c { height: auto; background: transparent; box-shadow: none; }
.shape-c::before {               /* the left arm of the C */
  content: ''; position: absolute; left: 0; width: 24px;
  top: calc(var(--block-h) - 12px); bottom: 12px;
  background: var(--c);
}
.shape-c > .block-head { position: relative; background: var(--c); border-radius: 12px 12px 12px 0; }
.shape-c > .block-head::after {  /* tab inside the mouth, for the first block in it */
  content: ''; position: absolute; left: 40px; bottom: -9px;
  width: 34px; height: 12px; background: var(--c);
  clip-path: polygon(0 0, 100% 0, calc(100% - 6px) 100%, 6px 100%);
}
.shape-c > .inner {
  position: relative; z-index: 1;
  margin-left: 24px; min-height: 56px; padding: 0 0 14px;
}
/* empty mouth: a dashed slot showing where a block goes */
.shape-c > .inner:empty::before {
  content: ''; display: block;
  width: 136px; height: 42px; margin: 14px 0 0 8px;
  border: 3px dashed var(--c); border-radius: 10px;
  background: color-mix(in srgb, var(--c) 10%, transparent);
}
.c-foot {
  height: 24px; background: var(--c);
  border-radius: 0 12px 12px 12px;
  box-shadow: inset 0 -5px 0 var(--d);
}
.shape-c.running { box-shadow: none; }
.shape-c.running > .block-head, .shape-c.running > .c-foot { box-shadow: inset 0 0 0 4px #FFE066; }

.block.running { box-shadow: inset 0 0 0 4px #FFE066, inset 0 -5px 0 var(--d); filter: brightness(1.12); }

.ghost {
  position: fixed; left: 0; top: 0; z-index: 1000;
  pointer-events: none; display: none;
  filter: drop-shadow(0 10px 14px rgba(0,0,0,.28));
}
body.dragging .ghost { display: block; }
body.dragging, body.dragging * { cursor: grabbing !important; }
.drop-ind {
  position: fixed; z-index: 999; display: none; pointer-events: none;
  width: var(--block-w); height: 10px; border-radius: 5px;
  background: #FFD23F; box-shadow: 0 0 0 4px rgba(255,210,63,.35);
}

/* ---------- Right column ---------- */
.right {
  display: flex; flex-direction: column; gap: 12px;
  padding: 12px; min-height: 0; overflow-y: auto;
}
.stage-wrap {
  position: relative; flex: none; width: 100%; aspect-ratio: 4 / 3;
  border-radius: 20px; overflow: hidden;
  background: #fff; box-shadow: 0 0 0 3px var(--line);
}
.stage { position: absolute; left: 0; top: 0; width: 480px; height: 360px; transform-origin: 0 0; overflow: hidden; }
.stage-grid {
  position: absolute; inset: 0; pointer-events: none; display: none;
  background-image: linear-gradient(rgba(0,0,0,.09) 1px, transparent 1px), linear-gradient(90deg, rgba(0,0,0,.09) 1px, transparent 1px);
  background-size: 24px 24px;
}
.stage.grid .stage-grid { display: block; }
.sprite {
  position: absolute; width: 64px; height: 64px;
  cursor: grab; touch-action: none;
  transition: opacity .2s;
  filter: drop-shadow(0 3px 2px rgba(0,0,0,.22));
}
.sprite svg { display: block; width: 100%; height: 100%; }
.sprite.hidden { opacity: 0; pointer-events: none; }
.bubble {
  position: absolute; z-index: 5; display: none; pointer-events: none;
  transform: translate(-50%, -100%);
  background: #fff; border: 3px solid var(--ink); border-radius: 16px;
  padding: 2px 12px; font-size: 20px; font-weight: 700; white-space: nowrap;
}
.bubble::after {
  content: ''; position: absolute; left: 50%; bottom: -10px; margin-left: -8px;
  border: 8px solid transparent; border-top-color: var(--ink); border-bottom: 0;
}
.bubble.show { display: block; }

.bottom-row { display: grid; grid-template-columns: 1fr 112px; gap: 12px; }
.panel { background: var(--panel); border-radius: 20px; padding: 10px 12px 12px; box-shadow: 0 0 0 1px var(--line); }
.panel-title { font-weight: 800; font-size: 15px; color: var(--muted); margin-bottom: 6px; }
.sprites { display: flex; flex-wrap: wrap; gap: 10px; }
.tile {
  position: relative; width: 70px; height: 70px; border-radius: 18px;
  background: var(--bg); border: 3px solid transparent;
  display: grid; place-items: center; font-size: 40px; cursor: pointer;
}
.tile svg { width: 54px; height: 54px; }
.tile.active { border-color: var(--accent); background: #EFEAFF; }
.tile .x {
  position: absolute; top: -8px; right: -8px; width: 26px; height: 26px; border-radius: 50%;
  border: 0; background: #FF5C5C; color: #fff; font-weight: 800; font-size: 16px; line-height: 26px;
  display: none; cursor: pointer; padding: 0;
}
.tile.active .x { display: block; }
.tile.add { border: 3px dashed var(--line); background: none; color: var(--muted); font-size: 38px; font-weight: 800; }
.backdrop-btn { width: 86px; height: 70px; border-radius: 16px; border: 3px solid var(--line); cursor: pointer; }

/* ---------- Picker & modal ---------- */
.picker {
  position: fixed; z-index: 1100; display: none;
  background: #fff; border-radius: 20px; padding: 10px;
  box-shadow: 0 12px 40px rgba(30,40,80,.25);
  max-width: 320px;
}
.picker.open { display: flex; flex-wrap: wrap; gap: 8px; }
.picker .opt {
  width: 56px; height: 56px; border-radius: 16px; border: 0;
  background: var(--bg); font-size: 26px; font-weight: 800; cursor: pointer;
}
.picker .opt.sel { box-shadow: 0 0 0 3px var(--accent); }
.picker .opt svg { width: 38px; height: 38px; vertical-align: middle; }
.badge .mini svg { width: 24px; height: 24px; vertical-align: -6px; }

/* turn-angle dial */
.picker.kind-angle { padding: 6px; border-radius: 50%; }
.dial svg { display: block; }
.dial-face { fill: var(--bg); stroke: var(--line); stroke-width: 3; }
.dial-wedge > * { fill: color-mix(in srgb, var(--c) 30%, transparent); stroke: var(--c); stroke-width: 3; stroke-linejoin: round; }
.dial-ticks path { stroke: #B5C0D3; stroke-width: 3; stroke-linecap: round; }
.dial-char { transform-box: view-box; transform-origin: 120px 120px; transition: transform .35s ease; }
.dial-opt { cursor: pointer; }
.dial-opt circle { fill: #fff; stroke: var(--c); stroke-width: 3; }
.dial-opt text { font: 800 15px 'Baloo 2', system-ui, sans-serif; fill: var(--ink); text-anchor: middle; pointer-events: none; }
.dial-opt:hover circle { fill: color-mix(in srgb, var(--c) 18%, white); }
.dial-opt.sel circle { fill: var(--c); }
.dial-opt.sel text { fill: #fff; }
.picker input {
  width: 100%; height: 50px; border-radius: 14px; border: 3px solid var(--line);
  font: inherit; font-size: 22px; font-weight: 700; padding: 0 12px; color: var(--ink);
}
.picker .ok { width: 100%; height: 50px; border: 0; border-radius: 14px; background: #3CC46A; color: #fff; font-size: 22px; font-weight: 800; cursor: pointer; }

.modal { position: fixed; inset: 0; z-index: 1200; display: none; place-items: center; background: rgba(30,40,80,.35); }
.modal.open { display: grid; }
.modal-card { background: #fff; border-radius: 24px; padding: 20px; max-width: min(640px, 92vw); box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-card h3 { margin: 0 0 12px; font-size: 24px; }
.choices { display: flex; flex-wrap: wrap; gap: 12px; }
.choice { width: 92px; height: 92px; border-radius: 20px; border: 3px solid var(--line); background: var(--bg); font-size: 52px; cursor: pointer; display: grid; place-items: center; }
.choice.place { width: 128px; height: 96px; font-size: 15px; font-weight: 800; align-items: end; padding-bottom: 4px; color: var(--ink); }
.choice.place span { background: rgba(255,255,255,.85); border-radius: 8px; padding: 0 8px; }

/* character picker with categories */
.modal-card.wide { width: min(780px, 94vw); max-width: none; }
.char-tabs { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.char-tab {
  border: 0; border-radius: 14px; padding: 6px 14px 6px 10px; cursor: pointer;
  background: var(--bg); font-size: 16px; font-weight: 800; color: var(--ink);
  display: flex; align-items: center; gap: 6px;
}
.char-tab span svg { display: block; width: 28px; height: 28px; }
.char-tab.active { background: var(--accent); color: #fff; }
.choices.chars { max-height: 52vh; overflow-y: auto; padding: 4px; gap: 10px; }
.choices.chars .choice { width: 82px; height: 82px; font-size: 48px; }
.choice:hover { border-color: var(--accent); }
.choice svg { width: 90%; height: 90%; }

/* ---------- Home screen ---------- */
.home {
  position: fixed; inset: 0; z-index: 1150; display: none; overflow-y: auto;
  background: radial-gradient(circle at 85% 0%, #9B7BFF, transparent 45%), linear-gradient(160deg, #7C5CFF, #4F7BFF);
  color: #fff; padding: 28px clamp(16px, 4vw, 48px) 48px;
}
body.at-home .home { display: block; }
.home-head { display: flex; align-items: center; gap: 18px; margin-bottom: 30px; }
.home-logo { width: 96px; height: 96px; filter: drop-shadow(0 6px 10px rgba(0,0,0,.25)); }
.home-title { font-size: clamp(36px, 6vw, 56px); font-weight: 700; line-height: 1; }
.home-title b { font-weight: 800; color: #FFD23F; }
.home-sub { font-size: 19px; opacity: .9; margin-top: 6px; }
.home-head .lang-btn { margin-left: auto; align-self: flex-start; }
.home h2 { font-size: 26px; margin: 0 0 16px; }
.proj-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 22px; }
.proj {
  position: relative; border: 0; border-radius: 22px; padding: 10px 10px 12px; cursor: pointer;
  background: #fff; color: var(--ink); text-align: left;
  box-shadow: 0 10px 26px rgba(20,20,70,.28); transition: transform .15s;
}
.proj:hover { transform: translateY(-4px); }
.proj-thumb { position: relative; aspect-ratio: 4 / 3; border-radius: 14px; overflow: hidden; }
.proj-thumb span { position: absolute; width: 32px; height: 32px; transform: translate(-50%, -50%); }
.proj-thumb svg { display: block; width: 100%; height: 100%; }
.proj-name { font-weight: 800; font-size: 19px; margin: 8px 4px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proj-date { font-size: 14px; color: var(--muted); margin: 0 4px; }
.proj .x {
  position: absolute; top: -10px; right: -10px; width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid #fff; background: #FF5C5C; color: #fff; font-weight: 800; font-size: 20px; line-height: 1; cursor: pointer;
  display: grid; place-items: center;
}
.proj.new {
  display: grid; place-items: center; align-content: center; gap: 4px; min-height: 230px;
  background: rgba(255,255,255,.16); color: #fff; border: 4px dashed rgba(255,255,255,.75);
  box-shadow: none; font-size: 22px; font-weight: 800; text-align: center;
}
.proj.new .plus { font-size: 70px; line-height: .8; }

.toast {
  position: fixed; left: 50%; bottom: 28px; z-index: 1400; transform: translate(-50%, 20px);
  background: #3CC46A; color: #fff; font-size: 20px; font-weight: 800;
  padding: 10px 22px; border-radius: 16px; box-shadow: 0 10px 30px rgba(0,0,0,.2);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- Smaller screens: stage on top ---------- */
@media (max-width: 960px) {
  body { overflow: auto; grid-template-rows: 60px auto; }
  .topbar { gap: 6px; padding: 0 10px; }
  .logo { font-size: 20px; white-space: nowrap; }
  .tbtn { width: 42px; height: 42px; }
  .tbtn.flag, .tbtn.stop { width: 56px; }
  .lang-btn { height: 42px; }
  .logo, #bigBtn { display: none; }
  .proj-name-input { width: 110px; height: 40px; font-size: 16px; padding: 0 10px; }
  .home-logo { width: 64px; height: 64px; }
  .lang-btn span { min-width: 32px; font-size: 15px; }
  .sep { display: none; }
  main, body.big-stage main { grid-template-columns: 336px 1fr; grid-template-rows: auto 560px; }
  .right { grid-column: 1 / -1; grid-row: 1; min-height: auto; flex-direction: row; flex-wrap: wrap; align-items: flex-start; overflow: visible; }
  .stage-wrap { width: min(100%, 480px); }
  .bottom-row { flex: 1; min-width: 220px; grid-template-columns: 1fr; }
  .left { grid-template-columns: 78px 1fr; border-top: 1px solid var(--line); }
  .cat { width: 70px; }
  .workspace { border-top: 1px solid var(--line); }
}
