/* 우리 아이 첫 배움터 — 디자인 가이드(docs/03) 기반 */
:root {
  --bg: #FFF9F0;
  --ink: #4E342E;
  --ink-soft: #8D7468;
  --line: #F0E4D6;
  --star: #FFC93C;
  --green: #4CAF50;  --green-bg: #F1F8E9;
  --orange: #FF8A3D; --orange-bg: #FFF3E0;
  --blue: #42A5F5;   --blue-bg: #E3F2FD;
  --purple: #AB7DF6; --purple-bg: #F3E5F5;
  --pink: #F06292;   --pink-bg: #FCE4EC;
  --shadow: 0 6px 20px rgba(78, 52, 46, 0.10);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}
button { font-family: inherit; color: inherit; border: none; cursor: pointer; }

/* ---------- 화면 공통 ---------- */
.screen {
  position: absolute; inset: 0; display: none;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.screen.active { display: flex; }

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; min-height: 64px;
}
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: #FFFFFF; border: 2px solid var(--line);
  border-radius: 999px; padding: 8px 16px;
  font-size: 20px; font-weight: 800; box-shadow: var(--shadow);
}
.btn-round {
  width: 64px; height: 64px; border-radius: 50%;
  background: #FFFFFF; border: 2px solid var(--line);
  font-size: 28px; box-shadow: var(--shadow);
  display: grid; place-items: center;
  transition: transform 0.15s ease;
}
.btn-round:active { transform: scale(1.08); }
.btn-round.picked { border-color: var(--ink); border-width: 3px; background: var(--orange-bg); }
.btn-round.flash { background: var(--star); }
.hidden { display: none !important; }

/* ---------- 시작 화면 ---------- */
#screen-start { align-items: center; justify-content: center; gap: 20px; text-align: center;
  background: var(--bg); }
#screen-home { background: var(--bg); }
#screen-start h1 { font-size: clamp(30px, 5vw, 48px); font-weight: 800; margin: 0; }
.start-credit {
  /* 뷰포트 기준 고정 — 화면 크기와 무관하게 항상 화면 맨 바닥에 붙는다 */
  position: fixed; left: 0; right: 0;
  bottom: max(2px, env(safe-area-inset-bottom));
  margin: 0; padding: 0 clamp(12px, 3vw, 40px); text-align: center;
  font-size: clamp(9px, 1.1vw, 12px); line-height: 1.5;
  color: var(--ink-soft); opacity: 0.8;
}
#screen-start .island { font-size: clamp(56px, 12vw, 110px); line-height: 1.1; }
#btn-start {
  font-size: 32px; font-weight: 800; color: #fff;
  background: var(--orange); border-radius: 999px;
  padding: 20px 64px; box-shadow: var(--shadow);
  transition: transform 0.15s ease;
}
#btn-start:active { transform: scale(1.07); }

/* ---------- 홈 (섬 지도) ---------- */
#home-main {
  flex: 1; display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  gap: 14px; padding: 4px 18px;
  min-height: 0;
}
.cat-card {
  border-radius: 28px; border-bottom: 6px solid rgba(0, 0, 0, 0.10);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; box-shadow: var(--shadow);
  min-height: 0; padding: 10px 12px;
}
.cat-head {
  display: flex; align-items: center; justify-content: center; gap: 12px;
  background: none; font-size: clamp(17px, 2.4vw, 23px); font-weight: 800;
  transition: transform 0.15s ease;
}
.cat-head:active { transform: scale(1.05); }
.cat-head svg { height: clamp(52px, 10vh, 84px); width: auto; }
.cat-char { height: clamp(52px, 10vh, 84px); width: auto; }

/* 생성 이미지 에셋 — 부모의 font-size(1em)를 그대로 상속해 이모지처럼 배치된다 */
img.ai {
  width: 1em; height: 1em; object-fit: contain;
  vertical-align: -0.12em; pointer-events: none;
}
.btn-round img.ai { width: 68%; height: 68%; vertical-align: middle; }
img.ai.flip { transform: scaleX(-1); }   /* next 화살표를 뒤집어 '뒤로'로 재사용 */
.stage-glyph img.ai, .subcat-glyph img.ai, .intro-emoji img.ai,
.ch-emoji img.ai, .char-corner img.ai, #celebrate-star img.ai { vertical-align: middle; }
/* 생성 이미지는 사방 여백이 커서 이모지 대비 작게 보인다 — 상자 크기로 보정 */
.stage-glyph img.ai { width: 1.3em; height: 1.3em; }
.subcat-glyph img.ai { width: 1.5em; height: 1.5em; }
.char-corner img.ai { width: 1.3em; height: 1.3em; }
.sticker img.ai { display: block; pointer-events: none; width: 1.3em; height: 1.3em; }
#screen-start .island img.ai { width: 1.8em; height: 1.8em; }
.sleep-chars img.ai { width: 1.2em; height: 1.2em; }
.cat-chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; }
.chip-sub {
  background: #FFFFFF; border-radius: 999px;
  min-height: 48px; padding: 8px 18px;
  font-size: clamp(14px, 1.9vw, 18px); font-weight: 700;
  border-bottom: 3px solid rgba(0, 0, 0, 0.08);
  box-shadow: var(--shadow);
  transition: transform 0.15s ease;
}
.chip-sub:active { transform: scale(1.08); }
#door-lines { background: var(--green-bg); }
#door-hangul { background: var(--orange-bg); }
#door-alpha { background: var(--blue-bg); }
#door-num { background: var(--purple-bg); }
#door-sketch {
  margin: 12px 18px 14px; min-height: 84px;
  background: var(--pink-bg); border-radius: 24px;
  flex-direction: row; gap: 16px; padding: 10px 16px;
}
#door-sketch .cat-head { font-size: clamp(16px, 2.2vw, 21px); }

.topbar-btns { display: flex; gap: 10px; }

/* ---------- 스테이지 목록 ---------- */
#stages-grid {
  flex: 1; display: grid;
  grid-template-columns: repeat(auto-fill, minmax(108px, 1fr));
  gap: 12px; padding: 6px 18px 24px;
  align-content: start;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.stage-section {
  grid-column: 1 / -1;
  font-size: 20px; font-weight: 800;
  padding: 10px 6px 0;
}
.stage-card {
  position: relative; aspect-ratio: 1;
  background: #FFFFFF; border-radius: 26px;
  border-bottom: 6px solid rgba(0, 0, 0, 0.08);
  box-shadow: var(--shadow);
  display: grid; place-items: center;
  transition: transform 0.15s ease;
}
.stage-card:active { transform: scale(1.08); }
.stage-glyph { font-size: clamp(36px, 6vw, 64px); font-weight: 800; }
.stage-star { position: absolute; top: 8px; right: 10px; font-size: 22px; }

/* ---------- 활동 화면 ---------- */
#screen-activity, #screen-coloring, #screen-sketch { background: var(--bg); }
.activity-main { flex: 1; position: relative; padding: 0 18px; min-height: 0; }
.activity-stage {
  position: relative; width: 100%; height: 100%;
  background: #FFFFFF; border: 2px solid var(--line);
  border-radius: 28px; box-shadow: var(--shadow); overflow: hidden;
}
.activity-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.char-corner {
  position: absolute; right: 14px; bottom: 10px;
  font-size: 44px; pointer-events: none;
  animation: breathe 2s ease-in-out infinite;
}
@keyframes breathe { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@media (prefers-reduced-motion: reduce) { .char-corner { animation: none; } }

.bottombar {
  display: flex; align-items: center; justify-content: center;
  gap: 16px; padding: 12px 18px; min-height: 88px;
}

/* 숫자 세기 */
#count-box {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px;
}
#count-label { font-size: 96px; font-weight: 800; color: var(--purple); min-height: 110px; }
#count-fruits { display: flex; flex-wrap: wrap; justify-content: center; gap: 18px; max-width: 70%; }
.fruit {
  font-size: 72px; background: none;
  transition: transform 0.15s ease;
}
.fruit:active { transform: scale(1.15); }
.fruit.counted { transform: scale(0.92); filter: grayscale(0.15); opacity: 0.75; }
.fruit.counted::after { content: '✓'; position: absolute; margin-left: -46px; margin-top: -10px; font-size: 30px; color: var(--green); }

/* ---------- 크레용 팔레트 ---------- */
.palette {
  display: flex; align-items: flex-end; justify-content: center;
  gap: 8px; padding: 6px 12px 10px; min-height: 84px;
  flex-wrap: nowrap; overflow-x: auto;
}
.crayon {
  width: 44px; height: 62px; flex: none;
  clip-path: polygon(50% 0%, 82% 16%, 82% 100%, 18% 100%, 18% 16%);
  border-radius: 6px;
  transition: transform 0.2s ease;
}
.crayon.white { box-shadow: inset 0 0 0 2px var(--line); }
.crayon.rainbow {
  background: linear-gradient(180deg, #F44336, #FF9800, #FFEB3B, #4CAF50, #2196F3, #3F51B5, #9C27B0);
}
.crayon.picked { transform: translateY(-12px) rotate(-4deg); }

/* ---------- 스케치북 ---------- */
#sketch-canvas { background: #FFFFFF; }
#sketch-tools { display: flex; gap: 10px; align-items: center; }
.sketch-bar { display: flex; align-items: center; gap: 10px; padding: 6px 14px 10px; }
.sketch-bar .palette { flex: 1; min-height: 72px; padding: 0; }
#screen-sketch .activity-stage { border-radius: 20px; }

/* ---------- 갤러리 ---------- */
#gallery-grid {
  flex: 1; overflow-y: auto;
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 14px; padding: 6px 18px 18px;
  align-content: start;
}
#gallery-grid img {
  width: 100%; border-radius: 18px;
  border: 3px solid #FFFFFF; box-shadow: var(--shadow);
  background: #fff;
}
.gallery-empty {
  grid-column: 1 / -1; text-align: center;
  color: var(--ink-soft); font-size: 20px; line-height: 1.7; margin-top: 60px;
}

/* ---------- 카드 게임 ---------- */
#game-main {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 26px;
  padding: 20px;
}
.game-intro { display: flex; flex-direction: column; align-items: center; gap: 8px; animation: starpop 0.5s ease; }
.intro-emoji { font-size: 110px; line-height: 1.2; }
.intro-word { font-size: 72px; font-weight: 800; }
.game-prompt { display: flex; align-items: center; justify-content: center; }
.prompt-word { font-size: 60px; font-weight: 800; }
.prompt-num { font-size: 110px; font-weight: 800; line-height: 1; }
.prompt-add { display: flex; align-items: center; gap: 14px; font-size: 40px; flex-wrap: wrap; justify-content: center; }
.prompt-add span { max-width: 300px; }
.prompt-add b { font-size: 48px; font-weight: 800; }
.choice-row { display: flex; gap: 22px; flex-wrap: wrap; justify-content: center; }
.choice-card {
  min-width: 150px; min-height: 130px; max-width: 240px;
  background: #FFFFFF; border: 3px solid var(--line); border-radius: 26px;
  border-bottom-width: 6px;
  box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  padding: 14px; transition: transform 0.15s ease;
}
.choice-card:active { transform: scale(1.06); }
.choice-card.right { border-color: var(--green); background: var(--green-bg); transform: scale(1.08); }
.choice-card.dim { opacity: 0.35; }
.ch-emoji { font-size: 76px; }
.ch-letter { font-size: 68px; font-weight: 800; }
.ch-num { font-size: 68px; font-weight: 800; }
.ch-fruits { font-size: 30px; line-height: 1.4; word-break: break-all; text-align: center; max-width: 190px; display: inline-block; }
.ch-word { font-size: 44px; font-weight: 800; }
.gfx { vertical-align: middle; margin: 2px; }
.prompt-add span {
  display: inline-flex; flex-wrap: wrap; gap: 4px;
  justify-content: center; align-items: center; max-width: 320px;
}
#voice-select {
  font-size: 16px; padding: 8px 12px; border-radius: 12px;
  border: 2px solid var(--line); background: #fff; color: var(--ink);
  max-width: 300px;
}
.game-sub { font-size: 19px; font-weight: 700; color: var(--ink-soft); }
.wordtrace-wrap {
  flex: 1; width: 100%; align-self: stretch;
  position: relative; min-height: 0;
}
.wordtrace-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
/* 오른손으로 쓸 때 손바닥이 닿는 우측/하단을 피해 좌측 상단에 배치 */
.wt-done { position: absolute; left: 10px; top: 10px; z-index: 5; }
.wt-eraser { position: absolute; left: 10px; top: 86px; z-index: 5; }
#btn-color-done { position: absolute; left: 10px; top: 10px; z-index: 5; }

/* 글자 만들기 (조합 드래그) */
.combine-wrap { display: flex; align-items: center; gap: 40px; }
.combine-block {
  width: 130px; height: 130px;
  background: #FFFFFF; border: 3px solid var(--line); border-radius: 26px;
  border-bottom-width: 7px; box-shadow: var(--shadow);
  display: grid; place-items: center;
  font-size: 76px; font-weight: 800;
  cursor: grab; user-select: none; touch-action: none;
}
.combine-block.dragging { transform: scale(1.1); cursor: grabbing; box-shadow: 0 12px 28px rgba(78,52,46,0.25); }
.combine-target {
  width: 210px; height: 170px;
  border: 4px dashed var(--ink-soft); border-radius: 28px;
  display: flex; align-items: center; justify-content: center; gap: 6px;
  background: rgba(255,255,255,0.6);
}
.combine-target.made { border-style: solid; border-color: var(--green); background: var(--green-bg); }
.cb-slot { font-size: 64px; font-weight: 800; min-width: 40px; text-align: center; }
.cb-made { font-size: 100px; font-weight: 800; animation: starpop 0.6s ease; }

/* 색 미션 크레용 힌트 */
.crayon.hint { animation: crayonhint 0.45s ease 3; }
@keyframes crayonhint {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-14px) scale(1.15); }
}

/* ---------- 스티커북 (보물창고) ---------- */
#sticker-book {
  flex: 1; margin: 0 18px; position: relative;
  background: #FFFDF7;
  border: 2px solid var(--line); border-radius: 24px;
  box-shadow: var(--shadow);
  background-image: radial-gradient(var(--line) 1.5px, transparent 1.5px);
  background-size: 34px 34px;
  overflow: hidden;
}
#sticker-tray {
  min-height: 96px; margin: 12px 18px 16px;
  background: var(--orange-bg); border-radius: 20px;
  display: flex; align-items: center; justify-content: center;
  gap: 14px; padding: 8px; flex-wrap: wrap;
}
.sticker {
  font-size: 56px; line-height: 1;
  cursor: grab; user-select: none; touch-action: none;
  filter: drop-shadow(0 3px 4px rgba(78,52,46,0.25));
  transition: transform 0.15s ease;
}
#sticker-book .sticker { position: absolute; }
.sticker.dragging { transform: scale(1.25) rotate(-6deg); cursor: grabbing; }
.tray-empty { color: var(--ink-soft); font-size: 17px; text-align: center; line-height: 1.6; margin: 0; }

/* ---------- 부모 영역 ---------- */
#parent-body {
  flex: 1; overflow-y: auto; padding: 0 26px 30px;
  max-width: 640px; width: 100%; margin: 0 auto;
  font-size: 17px;
}
#parent-body h3 { margin: 26px 0 10px; font-size: 18px; }
#parent-body h3 small { font-weight: 400; color: var(--ink-soft); font-size: 13px; }
.p-row {
  display: flex; align-items: center; gap: 12px;
  margin: 8px 0; font-weight: 600;
}
.p-row span { width: 96px; flex: none; }
.p-row b { width: 60px; text-align: right; font-variant-numeric: tabular-nums; }
.p-bar {
  flex: 1; height: 14px; background: var(--line);
  border-radius: 999px; overflow: hidden;
}
.p-bar i { display: block; height: 100%; border-radius: 999px; }
#limit-select {
  font-size: 17px; padding: 8px 14px; border-radius: 12px;
  border: 2px solid var(--line); background: #fff; color: var(--ink);
}
.p-credits { color: var(--ink-soft); font-size: 14px; line-height: 1.7; }
.p-credits p { margin: 0 0 10px; }
.p-credits b { color: var(--ink); }
#parent-gallery { display: flex; flex-wrap: wrap; gap: 10px; }
#parent-gallery img {
  width: 120px; border-radius: 12px; border: 2px solid var(--line); background: #fff;
}
.p-dim { color: var(--ink-soft); }
#btn-reset {
  background: #fff; border: 2px solid var(--line); border-radius: 14px;
  padding: 12px 20px; font-size: 16px; font-weight: 700; color: var(--ink-soft);
}

/* ---------- 모달 (게이트·배웅) ---------- */
.modal {
  position: absolute; inset: 0; z-index: 60;
  display: none; align-items: center; justify-content: center;
  background: rgba(78, 52, 46, 0.35);
}
.modal.active { display: flex; }
.modal-card, .sleep-card {
  background: var(--bg); border-radius: 28px; box-shadow: var(--shadow);
  padding: 30px 40px; text-align: center; position: relative;
  display: flex; flex-direction: column; align-items: center; gap: 14px;
}
.modal-card p { margin: 0; color: var(--ink-soft); font-weight: 700; }
#gate-q { font-size: 40px; font-weight: 800; }
#gate-answers { display: flex; gap: 16px; }
.gate-btn {
  width: 76px; height: 76px; border-radius: 50%;
  background: #fff; border: 2px solid var(--line);
  font-size: 28px; font-weight: 800; box-shadow: var(--shadow);
}
#gate-close { position: absolute; top: -18px; right: -18px; width: 48px; height: 48px; font-size: 18px; }

/* 하위 카테고리 선택 모달 */
.subcat-card { max-width: min(88vw, 640px); padding: 26px 28px; }
#subcat-title { font-size: clamp(20px, 3vw, 26px); font-weight: 800; color: var(--ink); }
#subcat-grid {
  display: flex; flex-wrap: wrap; justify-content: center; gap: 12px;
  max-height: 62vh; overflow-y: auto; padding: 4px;
}
.subcat-btn {
  width: clamp(96px, 15vw, 126px); aspect-ratio: 1;
  background: #FFFFFF; border-radius: 22px;
  border-bottom: 5px solid rgba(0, 0, 0, 0.08); box-shadow: var(--shadow);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px;
  font-weight: 800; font-size: clamp(13px, 1.8vw, 17px);
  transition: transform 0.15s ease;
}
.subcat-btn:active { transform: scale(1.08); }
.subcat-glyph { font-size: clamp(30px, 4.6vw, 44px); font-weight: 800; line-height: 1; }
#subcat-close { position: absolute; top: -18px; right: -18px; width: 48px; height: 48px; font-size: 18px; }
.sleep-card { font-size: 22px; font-weight: 800; line-height: 1.6; }
.sleep-chars { font-size: 40px; }
#sleep-overlay { z-index: 90; background: rgba(36, 27, 21, 0.85); }

/* ---------- 축하 오버레이 ---------- */
#celebrate {
  position: absolute; inset: 0; z-index: 40;
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  background: rgba(255, 249, 240, 0.92);
}
#celebrate.active { display: flex; }
#celebrate-star { font-size: 120px; animation: starpop 0.6s ease; }
@keyframes starpop { 0% { transform: scale(0); } 70% { transform: scale(1.25); } 100% { transform: scale(1); } }
#celebrate-extra { font-size: 34px; font-weight: 800; min-height: 44px; }
.celebrate-btns { display: flex; gap: 22px; margin-top: 12px; }
.celebrate-btns .btn-round { width: 84px; height: 84px; font-size: 40px; }
/* 캔버스 속성 크기(width=화면폭×dpr)가 요소 크기가 되지 않도록 CSS 크기 고정 —
   빠지면 iPad(dpr 2)에서 요소가 화면의 2배가 되어 전체 화면이 축소돼 보임 */
#confetti-canvas { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 50; }

/* ---------- 세로 모드 안내 ---------- */
#rotate-overlay {
  position: absolute; inset: 0; z-index: 100;
  display: none; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
  background: var(--bg); text-align: center;
  font-size: 24px; font-weight: 800;
}
#rotate-overlay .rotate-icon { font-size: 72px; animation: rotatehint 1.6s ease-in-out infinite; }
@keyframes rotatehint { 0%, 100% { transform: rotate(0); } 50% { transform: rotate(90deg); } }
@media (orientation: portrait) { #rotate-overlay { display: flex; } }
