/* ============================================================
 * 코딩 로봇 — style.css  (자기완결형, 외부 셸 의존 없음)
 * 디자인 토큰은 사이트 규격 반영: Primary #2563eb / Green #16a34a / XP #f59e0b
 * ============================================================ */
:root {
  --color-primary: #2563eb;
  --color-secondary: #16a34a;
  --color-xp: #f59e0b;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #eef2f8;
  --card: #ffffff;

  /* 블록 카테고리 색 (월드 색과 공유) */
  --rc-move: #2563eb;  /* 이동 — 파랑 */
  --rc-loop: #f59e0b;  /* 반복 — 골드 */
  --rc-cond: #8b5cf6;  /* 조건 — 보라 */
  --rc-func: #16a34a;  /* 함수 — 초록 */
  --rc-gem:  #0891b2;  /* 줍기 — 청록 */

  --stage-wc: var(--rc-move);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: 'Pretendard Variable', Pretendard, -apple-system, BlinkMacSystemFont,
               system-ui, 'Malgun Gothic', 'Apple SD Gothic Neo', sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
button { font-family: inherit; cursor: pointer; }
h1 { margin: 0; }

/* hidden 속성은 저자 CSS(#screen-play{display:flex}, .func-panel{display:flex})에
   우선순위/출처로 눌려 무시될 수 있다(빈 플레이 화면이 메뉴를 덮거나, 비함수
   스테이지에 '내 블록 정의' 패널이 뜨는 버그) → 전역 !important 로 강제. */
[hidden] { display: none !important; }

/* ══════════════ 메뉴 화면 ══════════════ */
#screen-menu {
  min-height: 100dvh;
  background: linear-gradient(180deg, #f8fafc 0%, #eef2f8 100%);
}
.menu-inner {
  max-width: 940px;
  margin: 0 auto;
  padding: 4.2rem 1.1rem 4rem;
  position: relative;
}
.exit-pill {
  position: absolute; top: 1rem; left: 1.1rem;
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .42rem .85rem; border-radius: 999px;
  background: #fff; border: 1px solid var(--line);
  color: var(--muted); font-weight: 700; font-size: .84rem;
  text-decoration: none; box-shadow: 0 1px 4px rgba(0,0,0,.06);
  transition: background .15s, color .15s;
}
.exit-pill:hover { color: var(--color-primary); background: #f1f5ff; }

.menu-head { text-align: center; margin-bottom: 2rem; }
.menu-head h1 { font-size: 2rem; font-weight: 800; letter-spacing: -.02em; }
.menu-head .rc-bot { font-size: 1.7rem; }
.menu-sub { color: var(--muted); margin: .7rem auto 0; max-width: 560px; line-height: 1.6; font-size: .98rem; }
.menu-sub b { color: var(--color-xp); }

.menu-list { display: flex; flex-direction: column; gap: 2rem; }

.world-sec { }
.world-head {
  display: flex; align-items: baseline; flex-wrap: wrap; gap: .5rem .7rem;
  padding: 0 0 .7rem; margin-bottom: 1rem;
  border-bottom: 2px solid var(--wc, var(--color-primary));
}
.world-name { font-size: 1.25rem; font-weight: 800; color: var(--wc); }
.world-tag {
  font-size: .74rem; font-weight: 700; color: #fff; background: var(--wc);
  padding: .12rem .5rem; border-radius: 999px;
}
.world-desc { color: var(--muted); font-size: .9rem; }

.stage-grid {
  display: grid; gap: .8rem;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
}
.stage-card {
  position: relative; text-align: left;
  display: flex; flex-direction: column; gap: .35rem;
  padding: .85rem .9rem; min-height: 92px;
  border: 1px solid var(--line); border-radius: 16px;
  background: var(--card);
  box-shadow: 0 2px 6px rgba(15,23,42,.05);
  transition: transform .12s, box-shadow .15s, border-color .15s;
}
.stage-card:not(.locked):hover { transform: translateY(-3px); box-shadow: 0 8px 18px rgba(15,23,42,.12); border-color: var(--wc); }
.stage-card.locked { opacity: .55; cursor: not-allowed; background: #f8fafc; }
.stage-card.done { border-color: var(--wc); }
.sc-num {
  font-size: .72rem; font-weight: 800; color: #fff; background: var(--wc);
  width: 1.5rem; height: 1.5rem; border-radius: 8px;
  display: inline-flex; align-items: center; justify-content: center;
}
.sc-title { font-weight: 700; font-size: 1rem; }
.sc-stars { color: var(--color-xp); font-size: 1.05rem; letter-spacing: 1px; margin-top: auto; }
.sc-lock { position: absolute; top: .8rem; right: .8rem; font-size: 1.05rem; }

/* ══════════════ 플레이 화면 (풀스크린 셸) ══════════════ */
#screen-play {
  position: fixed; inset: 0; height: 100dvh;
  display: flex; flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}
.play-top {
  flex: 0 0 auto;
  display: flex; align-items: center; gap: .8rem;
  padding: .6rem .9rem; background: #fff; border-bottom: 1px solid var(--line);
}
.play-titles { flex: 1; min-width: 0; text-align: center; line-height: 1.15; }
.play-world { display: block; font-size: .74rem; font-weight: 700; color: var(--stage-wc); }
.play-stage-title { font-size: 1.1rem; font-weight: 800; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.play-count { font-size: .8rem; color: var(--muted); font-weight: 700; white-space: nowrap; }
#block-count.over { color: var(--color-xp); }

.mini-btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 999px; padding: .42rem .8rem; font-weight: 700; font-size: .82rem;
  transition: background .15s;
}
.mini-btn:hover { background: #f1f5f9; }
.mini-btn.primary { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }

/* embed 모드 진행 표시(월드 N/전체 · 지금까지 딴 별) — 목록 버튼 자리를 대신한다 */
.embed-progress {
  font-size: .8rem; font-weight: 700; color: var(--stage-wc, var(--color-primary));
  white-space: nowrap;
}
.succ-complete { color: var(--color-secondary); font-weight: 800; }

.play-hint {
  flex: 0 0 auto; margin: 0; padding: .5rem .95rem;
  font-size: .86rem; color: #475569; background: #fffbeb;
  border-bottom: 1px solid #fde9c8;
}

/* 데스크탑 3컬럼: [미로+컨트롤] | [팔레트] | [내 프로그램] (code.org 관례)
   — 조립 공간(3열)이 헤더~바닥 전체 높이를 쓴다.
   세 열은 한 덩어리로 밀착시키고 덩어리째 중앙 정렬: 미로 열이 남는 폭을
   흡수하면(flex:1) 와이드일수록 미로↔팔레트 사이 죽은 공간이 커져
   시선 왕복 거리가 늘어난다 → 남는 폭은 덩어리 바깥 여백으로. */
.play-main {
  flex: 1 1 auto; min-height: 0;
  display: flex; gap: .8rem; padding: .8rem;
  justify-content: center;
}
.play-left {
  flex: 0 1 640px; min-width: 280px;
  display: flex; flex-direction: column; gap: .6rem;
}
.maze-wrap { flex: 1; display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; }
.play-side-bottom {
  flex: 0 0 auto;
  display: flex; flex-direction: column; align-items: center; gap: .45rem;
}

/* 2열: 팔레트 세로 스택 */
.play-palette-col {
  flex: 0 0 172px;
  display: flex; flex-direction: column; gap: .55rem;
  min-height: 0; overflow-y: auto;
}

/* 3열: 조립 공간 — 와이드에서 넉넉히 */
.play-right {
  flex: 0 0 clamp(380px, 34vw, 520px);
  display: flex; flex-direction: column; min-height: 0; gap: .45rem;
}
.ws-label { font-size: .76rem; font-weight: 800; color: var(--muted); letter-spacing: .02em; }
.ws-label.func { color: var(--rc-func); }

/* 팔레트 */
.palette { display: flex; flex-wrap: wrap; gap: .4rem; }
.palette.sub { gap: .3rem; }
.play-palette-col .palette { flex-direction: column; flex-wrap: nowrap; gap: .5rem; }
.play-palette-col .pal-btn { width: 100%; padding: .68rem .5rem; font-size: .92rem; }
.pal-btn {
  border: none; border-radius: 999px; color: #fff; font-weight: 700;
  padding: .5rem .85rem; font-size: .88rem; line-height: 1;
  box-shadow: 0 2px 5px rgba(15,23,42,.14);
  transition: transform .1s, filter .12s;
}
.pal-btn:hover { filter: brightness(1.06); }
.pal-btn:active { transform: scale(.94); }
.pal-btn:disabled { opacity: .5; cursor: default; box-shadow: none; }
.cat-move { background: var(--rc-move); }
.cat-loop { background: var(--rc-loop); }
.cat-cond { background: var(--rc-cond); }
.cat-func { background: var(--rc-func); }
.cat-gem  { background: var(--rc-gem); }

/* 워크스페이스(프로그램) — 세로 스크롤 */
.workspace { flex: 1 1 auto; overflow-y: auto; min-height: 0; padding-right: 2px; }
.prog-root { }
.prog-root.locked { pointer-events: none; opacity: .92; }

.func-panel {
  margin-top: .7rem; padding-top: .6rem; border-top: 2px dashed #cbd5e1;
  display: flex; flex-direction: column; gap: .4rem;
}

/* 통(블록 목록) */
.block-body {
  min-height: 42px; border: 2px dashed #cbd5e1; border-radius: 12px;
  padding: .6rem .5rem; display: flex; flex-direction: column; gap: .35rem;
  background: #fbfdff;
}
/* 루트(전체 프로그램) 통은 상하 여백을 더 크게 — 같은 계층에 블록을 추가하려고
   빈 여백을 클릭하기 쉽도록 클릭 표적을 넓힌다. */
#prog-root > .block-body, #func-root > .block-body { padding-top: 1.1rem; padding-bottom: 1.1rem; }
.block-body.active { border-color: var(--color-primary); background: #eff5ff; box-shadow: 0 0 0 2px rgba(37,99,235,.12) inset; }
.bucket-empty { color: #94a3b8; font-size: .82rem; padding: .3rem .2rem; }

/* 블록 칩 */
.chip {
  border-radius: 10px; background: #fff; border: 1px solid var(--line);
  border-left: 5px solid var(--cc, var(--rc-move));
  box-shadow: 0 1px 3px rgba(15,23,42,.07);
}
.chip.cat-move { --cc: var(--rc-move); }
.chip.cat-loop { --cc: var(--rc-loop); }
.chip.cat-cond { --cc: var(--rc-cond); }
.chip.cat-func { --cc: var(--rc-func); }
.chip.cat-gem  { --cc: var(--rc-gem); }
.chip-head { display: flex; align-items: center; gap: .4rem; padding: .38rem .5rem; }
.chip-label { font-weight: 700; font-size: .9rem; }
.chip-del {
  margin-left: auto; border: none; background: #f1f5f9; color: #64748b;
  width: 1.35rem; height: 1.35rem; border-radius: 7px; font-size: 1rem; line-height: 1;
  display: inline-flex; align-items: center; justify-content: center;
}
.chip-del:hover { background: #fee2e2; color: #dc2626; }
.chip.running { box-shadow: 0 0 0 3px var(--color-xp), 0 2px 8px rgba(245,158,11,.4); }

.chip-bodies { padding: 0 .5rem .5rem 1rem; display: flex; flex-direction: column; gap: .35rem; }
.branch { }
.branch-label { display: block; font-size: .74rem; font-weight: 700; color: var(--muted); margin-bottom: .2rem; }

/* 반복 스테퍼 */
.stepper { display: inline-flex; align-items: center; gap: .25rem; font-size: .85rem; font-weight: 700; color: var(--rc-loop); }
.stepper button {
  border: 1px solid #fcd88f; background: #fff7e6; color: #b45309;
  width: 1.5rem; height: 1.5rem; border-radius: 6px; font-size: 1rem; line-height: 1; font-weight: 800;
}
.stepper button:hover { background: #ffedcc; }
.st-num { min-width: 1.1rem; text-align: center; }

/* 조건 선택 */
.cond-select {
  border: 1px solid #ddd6fe; background: #f5f3ff; color: #6d28d9;
  border-radius: 8px; padding: .2rem .4rem; font-size: .8rem; font-weight: 700;
}

/* 컨트롤 — 미로 아래 중앙 */
.play-controls { flex: 0 0 auto; display: flex; gap: .5rem; width: min(440px, 100%); }
.ctrl-btn {
  flex: 1; border: 1px solid var(--line); background: #fff; color: var(--ink);
  border-radius: 12px; padding: .7rem; font-weight: 800; font-size: .95rem;
  transition: background .15s, transform .1s;
}
.ctrl-btn:hover { background: #f1f5f9; }
.ctrl-btn:active { transform: scale(.97); }
.ctrl-btn.run { flex: 2; background: var(--color-primary); color: #fff; border-color: var(--color-primary); }
.ctrl-btn.run:hover { background: #1d4ed8; }
.ctrl-btn:disabled { opacity: .55; cursor: default; }

/* 피드백 — 미로 아래 중앙 */
.play-msg {
  flex: 0 0 auto; min-height: 1.4rem;
  font-size: .88rem; color: var(--muted); padding: .1rem;
  text-align: center; max-width: 440px;
}
.play-msg.ok { color: var(--color-secondary); font-weight: 700; }
.play-msg.no { color: #dc2626; font-weight: 700; }
.play-msg.hint { color: var(--color-xp); font-weight: 700; }
.succ { display: flex; flex-direction: column; align-items: center; gap: .35rem; }
.succ-stars { font-size: 1.6rem; color: var(--color-xp); letter-spacing: 3px; }
.succ-line { color: var(--ink); font-weight: 700; font-size: .9rem; }
.succ-btns { display: flex; gap: .5rem; margin-top: .2rem; }
/* func4 클리어 → 꽃길 로봇 다리 안내 */
.succ-bridge { font-size: .84rem; color: var(--muted); font-weight: 600; margin-top: .35rem; }
.succ-bridge a { color: #ec4899; font-weight: 800; text-decoration: underline; text-underline-offset: 2px; }

/* ══════════════ 미로 ══════════════ */
.maze-board {
  position: relative;
  width: calc(var(--cols) * var(--cell));
  height: calc(var(--rows) * var(--cell));
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--cell));
  grid-auto-rows: var(--cell);
  background: #dbe4f0;
  border-radius: 14px;
  box-shadow: 0 6px 20px rgba(15,23,42,.14);
  padding: 0; overflow: hidden;
}
.mz-cell {
  width: var(--cell); height: var(--cell);
  display: flex; align-items: center; justify-content: center;
  border: 1px solid rgba(148,163,184,.28);
  font-size: calc(var(--cell) * .58); line-height: 1;
  background: #f7fafc;
}
.mz-cell.wall { background: #e6f0e0; }
.mz-cell.goal { background: #fff5da; }
.mz-star { animation: rc-star 2.2s ease-in-out infinite; }
.maze-board.win .mz-star { animation: rc-star-pop .5s ease; }
@keyframes rc-star { 0%,100% { transform: scale(1); } 50% { transform: scale(1.14); } }
@keyframes rc-star-pop { 0% { transform: scale(1); } 40% { transform: scale(1.7); } 100% { transform: scale(1.2); } }

.mz-gem {
  position: absolute; top: 0; left: 0;
  width: var(--cell); height: var(--cell);
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cell) * .5); line-height: 1;
  transform: translate(calc(var(--gc) * var(--cell)), calc(var(--gr) * var(--cell)));
  transition: opacity .25s, transform .25s;
  pointer-events: none;
}
.mz-gem.taken { opacity: 0; transform: translate(calc(var(--gc) * var(--cell)), calc(var(--gr) * var(--cell))) scale(1.8); }

.mz-robot {
  position: absolute; top: 0; left: 0;
  width: var(--cell); height: var(--cell);
  transition: transform .28s ease;
  pointer-events: none; z-index: 5;
}
.mz-face {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cell) * .62); line-height: 1; z-index: 2;
  filter: drop-shadow(0 1px 2px rgba(0,0,0,.2));
}
.mz-arrow {
  position: absolute; inset: 0;
  display: flex; align-items: flex-start; justify-content: center;
  padding-top: 1px;
  font-size: calc(var(--cell) * .32); line-height: 1; z-index: 1;
  color: var(--stage-wc);
  transition: transform .28s ease;
}
.mz-robot.bump { animation: rc-bump .4s ease; }
@keyframes rc-bump {
  0%,100% { }
  25% { transform: translate(var(--tx,0), var(--ty,0)); }
}
/* bump 은 JS 가 translate 를 유지하므로 흔들림만 별도로 */
.mz-robot.bump .mz-face { animation: rc-shake .4s ease; }
@keyframes rc-shake { 0%,100%{transform:translateX(0)} 20%{transform:translateX(-3px)} 40%{transform:translateX(3px)} 60%{transform:translateX(-2px)} 80%{transform:translateX(2px)} }

/* ══════════════ embed 모드 (전자책 iframe, ~320-900px × 600-750px) ══════════════
   100dvh 셸이라 iframe 안에서는 dvh = iframe 높이. 낮은 높이에 맞춰 헤더·여백을 압축.
   비-embed 화면(body에 .embed 없음)은 이 블록의 영향을 전혀 받지 않는다. */
body.embed .play-top { padding: .4rem .7rem; gap: .5rem; }
body.embed .play-stage-title { font-size: 1rem; }
body.embed .play-world { font-size: .7rem; }
body.embed .play-hint { padding: .35rem .8rem; font-size: .8rem; }
body.embed .play-main { padding: .55rem; gap: .55rem; }

/* ══════════════ 반응형 (모바일: 세로 스택) ══════════════ */
@media (max-width: 820px) {
  .play-main { flex-direction: column; gap: .5rem; padding: .5rem; }
  .play-left { flex: 0 0 auto; }
  .maze-wrap { flex: 0 0 auto; height: 38vh; }
  .play-palette-col { flex: 0 0 auto; overflow-y: visible; }
  .play-palette-col .palette { flex-direction: row; flex-wrap: wrap; gap: .4rem; }
  .play-palette-col .pal-btn { width: auto; padding: .5rem .85rem; font-size: .88rem; }
  .play-right { flex: 1 1 auto; }
  .workspace { min-height: 80px; }
}
/* embed 전용 · 560px 이하(진짜 폰)만: 위 세로 스택을 좁은 iframe 높이(600~750px)에
   맞춰 더 압축. 561px 이상은 아래 2컬럼 규칙이 대체한다(2026-07-09). */
@media (max-width: 560px) {
  body.embed .maze-wrap { height: 32vh; }
  body.embed .workspace { min-height: 56px; }
  body.embed .play-side-bottom { gap: .3rem; }
}
/* embed 전용 2컬럼(561~900px, 2026-07-09): 세로 스택(미로→실행→팔레트→내 프로그램
   순으로 아래로 쌓임)은 실행 결과(미로)를 보려면 위로, 블록을 고치려면(내 프로그램)
   아래로 스크롤을 오가야 해 코딩 퍼즐의 핵심 루프가 끊겼다.
   → 왼쪽 [미로+실행+피드백] · 오른쪽 [팔레트(위, 가로 랩) + 내 프로그램(아래,
   남은 높이 채움 + 내부 스크롤 전담)] 2컬럼 그리드로 둘을 한 화면에 둔다.
   구 821~900px 세로 스택 규칙을 흡수해 대체(비-embed는 3열 그대로, 무변경). */
@media (min-width: 561px) and (max-width: 900px) {
  body.embed .play-main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(180px, 260px);
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas: "maze palette" "maze workspace";
  }
  body.embed .play-left { grid-area: maze; min-width: 0; min-height: 0; }
  /* ≤820px 스택 규칙의 height:38vh를 되돌림 — 2컬럼에서는 미로가 왼쪽 열 높이를 채운다 */
  body.embed .maze-wrap { flex: 1 1 auto; height: auto; }
  body.embed .play-palette-col {
    grid-area: palette; flex: 0 0 auto; overflow-y: visible; min-height: 0;
  }
  body.embed .play-palette-col .palette { flex-direction: row; flex-wrap: wrap; gap: .35rem; }
  body.embed .play-palette-col .pal-btn { width: auto; padding: .45rem .7rem; font-size: .82rem; }
  body.embed .play-right { grid-area: workspace; min-height: 0; }
}
@media (max-width: 480px) {
  .menu-inner { padding-top: 3.6rem; }
  .menu-head h1 { font-size: 1.6rem; }
  .maze-wrap { height: 34vh; }
  .pal-btn { padding: .45rem .7rem; font-size: .82rem; }
}

/* ── 블록 끌어 옮기기 (재정렬 · 반복/조건 안팎 이동) ── */
.chip-head { cursor: grab; }
.chip-head:active { cursor: grabbing; }
.chip.dragging { opacity: .4; }
.pal-btn { cursor: grab; }
.pal-btn.dragging { opacity: .5; }
.block-body.drag-over { background: rgba(37, 99, 235, .06); }
.drop-line {
  height: 4px; border-radius: 3px; margin: 3px 6px;
  background: var(--stage-wc, var(--color-primary));
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .7);
}
.chip-del, .stepper, .cond-select { cursor: pointer; }

/* 컨테이너 헤더를 눌러 그 안을 '담는 위치'로 선택했을 때 강조 */
.chip.holds-active > .cont-head { background: #eff5ff; border-radius: 8px; }

/* '시작' 블록 — 코드 시작 위치 표시 + 최상위 선택 손잡이(이동·삭제 불가) */
.start-chip {
  display: flex; align-items: center; gap: .4rem;
  padding: .4rem .7rem; border-radius: 10px;
  background: #ecfdf5; border: 1px dashed #86efac; color: #166534;
  font-weight: 800; font-size: .9rem; cursor: pointer; user-select: none;
}
.start-chip .start-flag { font-size: 1.05rem; }
.start-chip.holds-active { background: #d1fae5; border: 1px solid var(--color-secondary); box-shadow: 0 0 0 2px rgba(22,163,74,.18) inset; }
