:root{
  --bg:#fff8dd;
  --panel:#fdf7ee;
  --border:#e0dbd4;
  --text:#3f3226;
  --accent:#f5e2c4;
  --chip-bg:#fff;
  --chip-border:#e2ded7;
  --boardw:720px;
  --boardh:420px;
  --radius:24px;
  --shadow:0 18px 40px rgba(0,0,0,.08);
  font-family:-apple-system,BlinkMacSystemFont,"Apple SD Gothic Neo","맑은 고딕",system-ui,sans-serif;
}

*{box-sizing:border-box;}

html,body{
  margin:0;
  padding:0;
  height:100%;
  background:var(--bg);
  color:var(--text);
}

body{
  display:flex;
  justify-content:center;
  align-items:flex-start;
}

/* 앱 기본 레이아웃 */
#app.app{
  position:relative;
  width:100%;
  max-width:1200px;
  min-height:100vh;
  padding:18px 16px 40px;
}

/* 공통 유틸 */
.hidden{
  display:none !important;
}

/* 대시보드로 돌아가기 버튼 */
.back-button-wrapper {
  width: 100%;
  max-width: 1400px;
  padding-bottom: 12px;
  text-align: left;
}

.back-button {
  display: inline-block;
  padding: 10px 15px;
  background-color: white;
  color: var(--text);
  text-decoration: none;
  border-radius: 20px;
  border: 1px solid #ddd;
  font-weight: bold;
}

/* 헤더 & 컨트롤 */
.app-header{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:12px;
  margin-bottom:24px; /* 제목과 게임 사이 간격 */
}

.title{
  font-size:1.3rem;
  font-weight:800;
  letter-spacing:.03em;
}

.controls{
  display:flex;
  flex-wrap:wrap;
  gap:8px;
  justify-content:flex-end;
}

/* 칩/버튼 공통 */
.chip{
  display:inline-flex;
  align-items:center;
  gap:6px;
  padding:8px 14px;
  border-radius:999px;
  background:var(--chip-bg);
  border:1px solid var(--chip-border);
  font-size:.9rem;
  white-space:nowrap;
}
.chip b{font-weight:800;}

.letters-chip{
  display:flex;
  align-items:center;
  gap:6px;
}

.letters{
  display:flex;
  gap:4px;
}

.letter-slot{
  width:22px;
  height:22px;
  border-radius:50%;
  border:1px solid var(--chip-border);
  background:#f9f5ec;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size:.8rem;
}

.letter-slot.filled{
  background:#7f5bff;
  color:#fff;
  border-color:#5b46c8;
  font-weight:700;
}

button{
  font-family:inherit;
  cursor:pointer;
  border-radius:999px;
  border:none;
  font-size:.9rem;
}

button.ghost{
  padding:8px 14px;
  background:#fff;
  border:1px solid var(--chip-border);
  box-shadow:0 2px 4px rgba(0,0,0,.04);
}
button.ghost:hover{
  background:#f5f1ea;
}

button.primary,
button.primary-outline{
  padding:10px 18px;
  font-weight:700;
}

button.primary{
  background:#7f5bff;
  color:#fff;
  box-shadow:0 8px 18px rgba(127,91,255,.4);
}
button.primary:hover{
  filter:brightness(1.05);
}

button.primary-outline{
  background:#fff;
  border:1px solid #7f5bff;
  color:#5b46c8;
}

select{
  border:none;
  background:transparent;
  font:inherit;
  padding:2px 4px;
  outline:none;
}

/* 장면 레이아웃 */
.scene{
  /* position:absolute; inset:... 제거해서 지도 레이어가 위로 안 덮게 */
  margin-top:16px;        /* 제목과의 간격 */
  padding:12px;
  display:grid;
  gap:12px;
}

.scene--world{
  grid-template-columns:1fr;
}

.scene--game{
  grid-template-columns:1.05fr 1fr;
}

#sceneGame{
  display:none;
}

.app.mode-play #sceneWorld{
  display:none;
}
.app.mode-play #sceneGame{
  display:grid;
}

/* 패널 */
.panel{
  background:var(--panel);
  border-radius:var(--radius);
  border:1px solid var(--border);
  box-shadow:var(--shadow);
  display:flex;
  flex-direction:column;
  overflow:hidden;
}

.panel header{
  position:relative;
  z-index:1;
  padding:10px 16px;
  border-bottom:1px solid #ebe4da;
  font-weight:700;
  display:flex;
  align-items:center;
  gap:8px;
}

.panel header .sub{
  font-weight:400;
  font-size:.85rem;
  color:#8a7f74;
}

/* 스테이지 바 */
.stagebar{
  padding:10px 12px 8px;
  display:flex;
  flex-wrap:wrap;
  gap:6px;
  border-bottom:1px solid #f1e9dd;
}

.stagechip{
  padding:6px 10px;
  border-radius:999px;
  border:1px solid #e3ddd2;
  background:#fff;
  font-size:.8rem;
  cursor:pointer;
}

.stagechip.done{
  background:#22c55e;
  color:#fff;
  border-color:#16a34a;
}

/* 지도 */
#map{
  width:100%;
  height:100%;
  min-height:420px;
  border-radius:0 0 var(--radius) var(--radius);
  overflow:hidden;
}

/* 미리보기 */
.preview-wrap{
  padding:10px;
}

#previewImg{
  width:100%;
  border-radius:18px;
  object-fit:cover;
  display:block;
}

/* 퍼즐 HUD */
.hud-row{
  padding:10px 12px 6px;
  display:flex;
  flex-wrap:wrap;
  gap:8px;
}

/* 퍼즐 보드 */
.board{
  margin:6px 12px 14px;
  border-radius:18px;
  border:1px solid #e0dbd4;
  background:#e9e4dd;
  box-shadow:0 8px 24px rgba(0,0,0,.06);
  position:relative;
  padding:8px;
  display:flex;
  align-items:center;
  justify-content:center;
  overflow:hidden; /* 퍼즐 넘침 방지 */
}

.frame{
  position:relative;
  background:#fff;
  border-radius:12px;
  display:grid;
  gap:2px;
  overflow:hidden;
}

.tile{
  position:relative;
  border-radius:8px;
  background-size:cover;
  background-position:center;
  background-repeat:no-repeat;
  box-shadow:0 1px 3px rgba(0,0,0,.12);
}

.tile.selected{
  outline:3px solid #f97316;
  outline-offset:-2px;
}

.board.show-index .tile::after{
  content:attr(data-idx);
  position:absolute;
  bottom:4px;
  right:6px;
  font-size:.7rem;
  font-weight:700;
  color:#fff;
  text-shadow:0 0 4px rgba(0,0,0,.8);
}

/* 모달 (클리어 / 난이도 선택) */
.modal{
  position:fixed;
  inset:0;
  display:flex;
  align-items:center;
  justify-content:center;
  pointer-events:none;
  z-index:1200; /* Leaflet 지도(z 약 1000)보다 위에 두기 */
}

.modal .modal-backdrop{
  position:absolute;
  inset:0;
  background:rgba(0,0,0,.35);
  opacity:0;
  transition:opacity .2s;
}

.modal .modal-content{
  position:relative;
  max-width:520px;
  width:calc(100% - 40px);
  background:#fff;
  border-radius:20px;
  padding:20px 22px 18px;
  box-shadow:0 24px 60px rgba(0,0,0,.35);
  transform:translateY(12px);
  opacity:0;
  transition:opacity .2s,transform .2s;
  max-height:calc(100vh - 80px); /* 화면보다 살짝 작게 */
  overflow-y:auto;               /* 내부 스크롤 생성 */
}

.modal.small .modal-content{
  max-width:420px;
}

.modal.show{
  pointer-events:auto;
}

.modal.show .modal-backdrop{
  opacity:1;
}

.modal.show .modal-content{
  opacity:1;
  transform:translateY(0);
}

.modal h2{
  margin:0 0 6px;
  font-size:1.15rem;
  font-weight:800;
}

.modal p{
  margin:4px 0;
}

.earned-letter{
  margin:8px 0 4px;
  font-size:2.4rem;
  font-weight:800;
  text-align:center;
  opacity:0;
  transform:scale(0.6);
}

.earned-letter.show{
  animation:popLetter 1s ease-out forwards;
}

@keyframes popLetter{
  0%{
    transform:scale(0.2);
    opacity:0;
  }
  30%{
    transform:scale(1.3);
    opacity:1;
  }
  60%{
    transform:scale(1);
    opacity:1;
  }
  100%{
    transform:scale(1);
    opacity:1;
  }
}

.modal p.muted{
  font-size:.88rem;
  color:#7c7267;
}

.modal img{
  width:100%;
  border-radius:16px;
  margin-top:10px;
}

.modal-actions{
  margin-top:14px;
  display:flex;
  flex-wrap:wrap;
  justify-content:flex-end;
  gap:8px;
}

.diff-buttons{
  margin-top:10px;
  display:flex;
  flex-wrap:wrap;
  gap:8px;
  justify-content:space-between;
}

/* 반응형 */
@media (max-width: 1024px){
  .scene--game{
    grid-template-columns:1fr;
  }
  #previewImg{
    max-height:340px;
    object-fit:contain;
  }
  :root{
    --boardw:640px;
    --boardh:520px;
  }
  .scene--game{
    display:flex;
    flex-direction:column;
  }
  .scene--game > .panel:first-child{
    order:2; /* 미리보기 패널 */
  }
  .scene--game > .panel:last-child{
    order:1; /* 퍼즐 패널 */
  }
}

@media (max-width: 720px){
  #app.app{
    padding:10px 8px 24px;
  }
  .scene{
    margin-top:20px;
  }
  .app-header{
    flex-direction:column;
    align-items:flex-start;
  }
  .controls{
    justify-content:flex-start;
  }
  :root{
    --boardw:520px;
    --boardh:480px;
  }
}
