/* ── 스테이지 선택 ── */
.title-block { margin: 12px 0 20px; }
.title-block h1 { font-size: 26px; margin-bottom: 6px; }
.stage-list { list-style: none; padding: 0; margin: 0 0 20px; display: grid; gap: 10px; }
.stage-card {
  width: 100%; display: flex; align-items: center; gap: 14px; text-align: left;
  background: var(--panel); border: 1px solid var(--grid-line); border-radius: 14px; padding: 14px 16px; box-shadow: var(--shadow);
}
.stage-card:hover { border-color: var(--accent); }
.stage-no { font-size: 20px; font-weight: 800; color: var(--accent); min-width: 28px; text-align: center; }
.stage-info { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.stage-info small, .stage-rec small { color: var(--muted); }
.stage-rec { display: flex; flex-direction: column; align-items: flex-end; }
.stars { color: var(--accent); letter-spacing: 1px; }
.stars.big { font-size: 28px; margin: 4px 0; }
.rules { background: var(--panel); border-radius: 12px; padding: 12px 16px; margin-bottom: 12px; }
.rules summary { font-weight: 700; cursor: pointer; }
.rules ol { padding-left: 20px; }
.rules li { margin: 6px 0; }

/* ── 플레이 화면 레이아웃 ── */
.topbar { display: flex; align-items: center; gap: 8px; margin: -4px -4px 4px; }
.topbar-title { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar-title strong { font-size: 17px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; line-height: 1.25; }
/* 좋아요·신고·순위·대사·소리까지 버튼이 많아지면 좁은 화면에서 간격을 줄인다 */
.topbar { gap: 4px; }
@media (max-width: 520px) { .topbar .icon-btn { min-width: 36px; } .topbar .like-btn { padding: 0 2px; } }
.topbar-title small { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.costbar { display: flex; flex-wrap: wrap; gap: 4px 12px; align-items: baseline; padding: 6px 2px 10px; font-size: 14px; }
.cost-main b { font-size: 20px; color: var(--accent); }
.cost-best { margin-left: auto; }
.star-rule { width: 100%; font-size: 12px; }

.play-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas: "board" "side" "dock";
  gap: 12px;
}
.board-area { grid-area: board; min-width: 0; }
.side { grid-area: side; display: grid; gap: 12px; align-content: start; }
.dock {
  grid-area: dock;
  position: sticky; bottom: 0; z-index: 5;
  background: var(--bg);
  padding: 8px 0 calc(8px + env(safe-area-inset-bottom));
  display: grid; gap: 8px;
}
.hint { text-align: center; margin: 8px 0 0; font-size: 13px; min-height: 1.4em; }
body:not([data-mode="PLACING"]) .hint { visibility: hidden; }
.keys { display: none; font-size: 12px; margin: 0; }

@media (min-width: 1024px) {
  .play-layout {
    grid-template-columns: 220px minmax(0, 1fr) 300px;
    grid-template-areas: "dock board side";
    align-items: start;
  }
  .dock { position: static; background: transparent; padding: 0; }
  .tools { grid-template-columns: 1fr !important; }
  .controls { flex-direction: column; }
  .controls .btn { width: 100%; }
  .keys { display: block; }
  .log-panel .log { max-height: 46vh; }
}

/* ── 도구함 ── */
.tools { display: grid; grid-template-columns: repeat(auto-fit, minmax(72px, 1fr)); gap: 6px; }
.tool {
  position: relative; display: flex; align-items: center; gap: 6px;
  background: var(--panel); border: 2px solid var(--grid-line); border-radius: var(--radius-button);
  padding: 6px 8px; min-height: 48px;
}
.tool.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.tool.needs .tool-count { color: var(--bad); font-weight: 700; }
.tool kbd { position: absolute; top: 2px; left: 4px; opacity: .5; }
.tool-icon { display: inline-flex; width: 28px; height: 28px; align-items: center; justify-content: center; font-size: 22px; --cell: 36px; }
.tool-icon .obj { position: static; }
.tool-label { flex: 1; font-size: 13px; text-align: left; }
.tool-count { font-size: 13px; font-variant-numeric: tabular-nums; }
@media (max-width: 1023px) {
  .tool { flex-direction: column; gap: 0; padding: 4px; }
  .tool-label { text-align: center; font-size: 12px; }
}

.controls { display: flex; gap: 8px; }
.controls .btn { flex: 1; }
.controls .run { flex: 1.6; font-size: 17px; }
.controls-running { display: none; }
body[data-mode="RUNNING"] .controls-placing, body[data-mode="RESULT"] .controls-placing { display: none; }
body[data-mode="RUNNING"] .controls-running, body[data-mode="RESULT"] .controls-running { display: flex; }
body[data-mode="RUNNING"] .tools, body[data-mode="RESULT"] .tools { opacity: .45; pointer-events: none; }
.speed select { height: 44px; border-radius: var(--radius-button); border: 1px solid var(--grid-line); background: var(--panel); color: var(--fg); padding: 0 8px; font: inherit; }
@media (max-width: 1023px) {
  .controls-running .btn { padding: 8px 6px; font-size: 13px; }
}

/* ── 모바일: 도크를 얇게, 버튼은 줄어들거나 줄바꿈 ── */
@media (max-width: 1023px) {
  .screen { padding: 12px; }
  .tools { display: flex !important; overflow-x: auto; gap: 8px; padding: 4px 4px 6px; scrollbar-width: thin; }
  .tool { flex: 0 0 64px; min-height: 58px; }
  .controls { flex-wrap: wrap; gap: 6px; }
  .controls .btn { min-width: 0; padding: 8px 6px; font-size: 14px; }
  .controls-running .btn { flex: 1 1 calc(33% - 12px); }
  .controls-running .speed { flex: 0 0 auto; }
  .costbar { padding: 4px 2px 6px; font-size: 13px; }
  .cost-best { font-size: 12px; }
  .dock { padding-top: 6px; box-shadow: 0 -8px 12px -8px rgba(0, 0, 0, .35); }
  .topbar-title strong { font-size: 15px; }
}
@media (max-width: 380px) {
  .controls .btn { font-size: 13px; padding: 8px 4px; }
  .tool { flex-basis: 58px; }
}

/* ── 로그·상태 ── */
.log-panel summary { cursor: pointer; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.log { list-style: none; padding: 0; margin: 8px 0 0; max-height: 180px; overflow: auto; font-size: 13px; }
.log li button { width: 100%; text-align: left; background: none; border: 0; padding: 6px 8px; border-radius: 6px; }
.log li button:hover { background: var(--cell-alt); }
.log li.current button { background: var(--accent); color: var(--accent-fg); }
.log-stuck { padding: 6px 8px; color: var(--bad); font-weight: 700; }
.side h3 { font-size: 14px; margin-bottom: 6px; }
.status { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 14px; }
.status dt { color: var(--muted); }
.status dd { margin: 0; display: flex; gap: 4px; align-items: center; --cell: 40px; }

/* ── 격자판 ── */
/* justify-content:center는 보드가 넘치면 왼쪽이 잘려 스크롤로도 못 본다. margin:auto는 넘칠 때 왼쪽 정렬로 바뀐다. */
.board-wrap { overflow: auto; display: flex; padding: 4px; touch-action: manipulation; max-width: 100%; }
.board-area { position: relative; }
.zoom-btn { position: absolute; top: 0; right: 4px; z-index: 4; min-height: 30px !important; padding: 2px 10px !important; font-size: 13px; }
.board-area:has(> .zoom-btn:not([hidden])) { padding-top: 38px; }
.board-wrap.zoomed { max-height: 58vh; overscroll-behavior: contain; }
.board {
  position: relative; flex: none; margin: auto;
  width: calc(var(--w) * var(--cell)); height: calc(var(--h) * var(--cell));
  user-select: none; -webkit-user-select: none;
  border-radius: 8px; box-shadow: var(--shadow);
  --move-ms: 0ms;
}
.cells { display: grid; grid-template-columns: repeat(var(--w), var(--cell)); grid-auto-rows: var(--cell); border-radius: 8px; overflow: hidden; }
.cell { position: relative; background: var(--cell); box-shadow: inset 0 0 0 .5px var(--grid-line); }
.cell.alt { background: var(--cell-alt); }
.cell.void { background: var(--cell-void); background-image: repeating-linear-gradient(45deg, transparent 0 6px, rgba(0,0,0,.18) 6px 12px); }
.cell.exit { box-shadow: inset 0 0 0 3px var(--ok); }
.exit-mark { position: absolute; right: 3px; bottom: 1px; font-size: calc(var(--cell) * .3); opacity: .9; }
.cell.placeable::after { content: ''; position: absolute; inset: 42%; border-radius: 50%; background: var(--accent); opacity: .22; pointer-events: none; }
.cell.target { box-shadow: inset 0 0 0 4px var(--accent); }
.cell.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }
.slot { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; pointer-events: none; }

.obj { display: flex; align-items: center; justify-content: center; font-size: calc(var(--cell) * .56); line-height: 1; position: relative; }
.obj .pin { position: absolute; top: -.45em; right: -.55em; font-size: .4em; }
.token {
  display: inline-flex; align-items: center; justify-content: center;
  width: calc(var(--cell) * .56); height: calc(var(--cell) * .56);
  background: var(--c); color: #fff; font-weight: 800; font-size: calc(var(--cell) * .26);
  text-shadow: 0 1px 1px rgba(0,0,0,.4);
}
.token.small { width: calc(var(--cell) * .34); height: calc(var(--cell) * .34); font-size: calc(var(--cell) * .17); }
.shape-circle { border-radius: 50%; }
.shape-square { border-radius: 3px; }
.shape-triangle { clip-path: polygon(50% 4%, 98% 94%, 2% 94%); padding-top: .35em; }
.shape-diamond { clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%); }
.pedestal {
  width: calc(var(--cell) * .72); height: calc(var(--cell) * .72);
  display: flex; align-items: center; justify-content: center;
  border: calc(var(--cell) * .07) solid var(--c); border-radius: 6px;
  background: color-mix(in srgb, var(--c) 18%, transparent);
}
.pedestal-letter { color: var(--c); font-weight: 800; font-size: calc(var(--cell) * .28); }

/* 변 요소 */
.edges { position: absolute; inset: 0; pointer-events: none; }
.edge { position: absolute; display: flex; align-items: center; justify-content: center; border-radius: 3px; }
.edge.side-E { left: calc((var(--x) + 1) * var(--cell) - var(--edge) / 2); top: calc(var(--y) * var(--cell) + 2px); width: var(--edge); height: calc(var(--cell) - 4px); }
.edge.side-S { top: calc((var(--y) + 1) * var(--cell) - var(--edge) / 2); left: calc(var(--x) * var(--cell) + 2px); height: var(--edge); width: calc(var(--cell) - 4px); }
/* 철망: 엇갈린 그물 무늬 */
.edge-fence { background: repeating-linear-gradient(45deg, var(--fence) 0 2px, transparent 2px 5px), repeating-linear-gradient(-45deg, var(--fence) 0 2px, transparent 2px 5px); box-shadow: 0 0 0 1px var(--fence); border-radius: 1px; }
.edge-door { background: var(--c); box-shadow: 0 0 0 2px #3a4152; border-radius: 1px; } /* 보안문: 금속 테 + 색 띠 */
.edge-door.open { background: transparent; border: 2px dashed var(--c); }
/* 1.4 교대문: 보라색 문. 열리면 점선 */
.edge-toggle { background: #7c5cff; box-shadow: 0 0 0 2px #3a4152; border-radius: 1px; }
.edge-toggle.open { background: transparent; box-shadow: none; border: 2px dashed #7c5cff; }
.edge-toggle .edge-label { background: #7c5cff; color: #fff; }
.edge-oneway { background: #8a93a3; box-shadow: 0 0 0 1px #3a4152; } /* 회전 게이트: 금속 봉 */
.edge-label {
  font-size: calc(var(--cell) * .2); font-weight: 800; color: #fff; background: var(--c, var(--fg));
  border-radius: 4px; padding: 1px 3px; white-space: nowrap; line-height: 1.1;
}
.edge-oneway .edge-label { background: var(--fg); color: var(--bg); }

/* 이동 경로 */
.trail { position: absolute; inset: 0; pointer-events: none; overflow: visible; }
.trail-seg { fill: none; stroke: var(--path); stroke-width: .06; stroke-dasharray: .14 .1; stroke-linecap: round; opacity: .85; }
.trail-seg.sled { stroke: #3e7bdd; stroke-dasharray: none; stroke-width: .08; }
.trail-num { font-size: .26px; font-weight: 800; fill: var(--fg); text-anchor: middle; dominant-baseline: middle; paint-order: stroke; stroke: var(--bg); stroke-width: .06px; }

/* 허스키 */
.husky {
  position: absolute; left: 0; top: 0; width: var(--cell); height: var(--cell);
  transform: translate(calc(var(--x) * var(--cell)), calc(var(--y) * var(--cell)));
  transition: transform var(--move-ms) ease-out;
  pointer-events: none; z-index: 2;
}
.husky-inner { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; }
.husky-inner.jumping { animation: hop var(--move-ms) ease-out; }
@keyframes hop { 50% { transform: translateY(calc(var(--cell) * -.55)) scale(1.18); } }
.sprite-husky { font-size: calc(var(--cell) * .62); filter: drop-shadow(0 2px 2px rgba(0,0,0,.3)); }
.sprite-pixel { display: block; }
.sprite-pixel svg { display: block; width: 100%; height: 100%; }
.sprite-husky.sprite-pixel { width: calc(var(--cell) * .82); height: calc(var(--cell) * .82); filter: drop-shadow(0 2px 2px rgba(0,0,0,.3)); }
.obj .sprite-pixel { width: calc(var(--cell) * .74); height: calc(var(--cell) * .74); }
.tool-icon .sprite-pixel { width: calc(var(--cell) * .74); height: calc(var(--cell) * .74); }
.held { position: absolute; right: 2px; bottom: 2px; display: flex; }
.husky.success .sprite-husky { animation: celebrate .6s ease-in-out 2; }
@keyframes celebrate { 50% { transform: translateY(-20%) rotate(-10deg); } }
.husky.stuck .sprite-husky { transform: rotate(-14deg); opacity: .85; }

/* 설명 마크 */
.marks { position: absolute; inset: 0; pointer-events: none; z-index: 3; }
.marks > * {
  position: absolute; left: calc((var(--x) + .5) * var(--cell)); top: calc((var(--y) + .5) * var(--cell));
  transform: translate(-50%, -50%);
}
.scan-num { left: calc((var(--x) + .18) * var(--cell)) !important; top: calc((var(--y) + .2) * var(--cell)) !important;
  width: 18px; height: 18px; border-radius: 50%; background: var(--fg); color: var(--bg); font-size: 11px; font-weight: 800;
  display: flex; align-items: center; justify-content: center; opacity: .8; }
.stuck-mark { top: calc((var(--y) + .12) * var(--cell)) !important; width: 24px; height: 24px; border-radius: 50%; background: var(--bad); color: #fff; font-weight: 900; display: flex; align-items: center; justify-content: center; }
.probe-badge { width: 16px; height: 16px; border-radius: 50%; background: var(--panel); color: var(--muted); font-size: 11px; display: flex; align-items: center; justify-content: center; }
.probe-badge.blocked { background: var(--bad); color: #fff; }
.reject-badge { top: calc((var(--y) + .85) * var(--cell)) !important; background: var(--bad); color: #fff; font-size: 10px; padding: 1px 4px; border-radius: 4px; white-space: nowrap; }

/* 결과 시트 */
.sheet {
  position: fixed; left: 50%; bottom: 0; transform: translateX(-50%);
  width: min(520px, 100vw); z-index: 20;
  background: var(--panel); border-radius: 18px 18px 0 0; box-shadow: 0 -6px 30px rgba(0,0,0,.25);
  padding: 20px 20px calc(20px + env(safe-area-inset-bottom));
  animation: sheet-up .25s ease-out;
}
.sheet h2 { font-size: 22px; margin-bottom: 6px; }
.sheet h2.ok { color: var(--ok); }
.sheet h2.bad { color: var(--bad); }
.sheet p { margin: 4px 0; }
.sheet .badge { display: inline-block; background: var(--accent); color: var(--accent-fg); font-weight: 700; padding: 2px 10px; border-radius: 999px; margin-right: 6px; }
.sheet-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.sheet-actions .btn { flex: 1; }
.sheet-close { position: absolute; top: 8px; right: 8px; border: 0; background: none; width: 40px; height: 40px; font-size: 16px; color: var(--muted); }
.probe-list { margin: 6px 0; padding-left: 20px; font-size: 14px; }
@keyframes sheet-up { from { transform: translate(-50%, 100%); } }
@media (prefers-reduced-motion: reduce) {
  .husky { transition: none; }
  .husky-inner.jumping, .husky.success .sprite-husky, .sheet, .cell.shake { animation: none; }
}

/* 대사 */
.dialogue {
  position: fixed; inset: 0; z-index: 40;
  display: flex; align-items: flex-end; justify-content: center;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, .22);
}
.dialogue[hidden] { display: none; }
.dlg-box {
  width: min(640px, 100%);
  display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 10px 14px;
  background: var(--panel); border: 2px solid var(--grid-line); border-radius: 16px;
  box-shadow: var(--shadow); padding: 16px 18px 12px; cursor: pointer;
  animation: dlg-in .18s ease-out;
}
.dialogue[data-who="husky"] .dlg-box { border-color: var(--accent); }
.dlg-face {
  width: 56px; height: 56px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--cell-alt); font-size: 32px; overflow: hidden;
}
.dlg-face-pixel svg { width: 78%; height: 78%; }
.dlg-name { color: var(--accent); font-size: 14px; }
.dlg-text { margin: 4px 0 0; font-size: 16px; line-height: 1.6; word-break: keep-all; }
.dlg-actions { grid-column: 1 / -1; display: flex; align-items: center; gap: 8px; }
.dlg-count { font-size: 12px; margin-right: auto; }
.dlg-actions .btn { min-height: 40px; padding: 6px 14px; }
@keyframes dlg-in { from { transform: translateY(12px); opacity: 0; } }

.cell.spot { box-shadow: inset 0 0 0 4px var(--accent), 0 0 0 3px var(--accent); z-index: 1; animation: spot-pulse 1.1s ease-in-out infinite; }
@keyframes spot-pulse { 50% { box-shadow: inset 0 0 0 4px var(--accent), 0 0 0 7px transparent; } }
@media (prefers-reduced-motion: reduce) {
  .dlg-box, .cell.spot { animation: none; }
}

/* 대화상자 (게임·제작소·관리자 공용) */
.ed-dialog { border: 0; border-radius: 16px; background: var(--panel); color: var(--fg); padding: 20px; width: min(560px, calc(100vw - 32px)); box-shadow: var(--shadow); }
.ed-dialog::backdrop { background: rgba(0,0,0,.45); }
.ed-dialog h3 { margin-bottom: 8px; }
.ed-dialog input[type="file"] { width: 100%; margin: 8px 0; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 14px; }
.small-btn { min-height: 30px !important; padding: 2px 8px !important; font-size: 12px; }

/* ── 휴대폰(좁은 화면): 스토리 모드와 같은 화면 구조. 색은 그대로 ──
   위에서부터: 구역 이름 + 스테이지 이름 → 조건 칩 → 판 → 안내 한 줄 → 도구 → 버튼 → 접힌 행동 로그 → 상태 */
.stage-zone { display: none; }
@media (max-width: 1023px) {
  .stage-zone { display: block; color: var(--accent); font-size: 12px; letter-spacing: .12em; font-weight: 700; }
  .topbar { gap: 8px; align-items: center; }
  .topbar-title strong { font-size: 18px; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; -webkit-line-clamp: unset; }
  .topbar-title #stage-desc { display: none; }
  .costbar { gap: 6px; padding: 8px 0 6px; align-items: center; }
  .costbar > span { width: auto; margin: 0; font-size: 12px; padding: 3px 8px; border: 1px solid var(--grid-line); background: var(--panel); display: inline-flex; align-items: center; gap: 4px; }
  .costbar .cost-main b { font-size: 14px; }
  .costbar .cost-best, .costbar .star-rule { margin-left: 0; width: auto; font-size: 12px; }
  .play-layout { grid-template-areas: "board" "dock" "side"; gap: 8px; }
  .dock { position: static; background: transparent; box-shadow: none; padding: 0; }
  .hint { margin: 6px 0; }
  .tools { justify-content: safe center; }
  .controls { justify-content: center; flex-wrap: wrap; gap: 6px; }
  .controls .btn, .controls .run { flex: 0 1 auto; min-height: 40px; padding: 8px 14px; font-size: 14px; }
  .controls-running .btn { flex: 0 1 auto; }
  .controls-running #btn-stop { order: -1; }
  .log-panel { margin-top: 4px; }
  .log-panel summary { font-weight: 400; }
}
