/* 숫자카드 덧셈 — 단순한 아이 방 테마. 한 화면에 한 과제, 큰 글자·큰 버튼 */
:root {
  --ink: #2d3142;
  --ink-soft: #6b6f80;
  --surface: rgba(255, 255, 255, 0.9);
  --press: rgba(45, 49, 66, 0.12);
  --ok-bg: #d4f0d6;
  --ng-bg: #f7e6d2;
  --tap: 64px;              /* 최소 터치 영역 (REQ-ART-003) */
  --radius: 24px;
  --gap: clamp(10px, 2vmin, 24px);

  /* 홈(기본) 색 */
  --bg: #f4efe6;
  --accent: #3d6fb6;
  --room-1: #efe6d6; --room-2: #e2d5bf; --room-3: #c9b79a;
}

/* 단계별 색조 (REQ-ART-002): 하늘색 / 연두 / 연노랑 / 연보라 */
body[data-level="1"] { --bg: #dcedf9; --accent: #2f74b5; --room-1: #d3e7f6; --room-2: #b9d7ee; --room-3: #8fb7d8; }
body[data-level="2"] { --bg: #e4f3da; --accent: #3f8a3a; --room-1: #dbefcd; --room-2: #c2e2ad; --room-3: #98c57f; }
body[data-level="3"] { --bg: #fbf1cc; --accent: #a8730a; --room-1: #f8ecbd; --room-2: #f0dc95; --room-3: #d8bd63; }
body[data-level="4"] { --bg: #ece3f8; --accent: #7655b5; --room-1: #e5daf5; --room-2: #d1c1ec; --room-3: #ab95d6; }

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;      /* 당겨서 새로고침 방지 (REQ-PLAT-003) */
}

body {
  position: fixed;
  inset: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-rounded, "SF Pro Rounded", -apple-system, system-ui, "Apple SD Gothic Neo", "Noto Sans KR", sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;     /* 더블탭 확대 방지 */
  transition: background-color 0.4s;
}

button {
  font: inherit;
  color: inherit;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
  min-width: var(--tap);
  min-height: var(--tap);
}
button:focus-visible { outline: 4px solid var(--accent); outline-offset: 3px; }

/* ---------- 배경 (REQ-ART-001): 흐리게 깔아 글자가 잘 보이게 ---------- */
#room {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.5;
  filter: blur(2px);
  pointer-events: none;
}
#room .r1 { fill: var(--room-1); }
#room .r2 { fill: var(--room-2); }
#room .r3 { fill: var(--room-3); }
#room .r3s { stroke: var(--room-3); }
#room rect, #room path { transition: fill 0.4s, stroke 0.4s; }

main { position: relative; height: 100%; }

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right))
           max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}

/* ---------- 시작 화면 ---------- */
#home { align-items: center; justify-content: center; gap: clamp(20px, 5vmin, 56px); }

#title {
  margin: 0;
  font-size: clamp(34px, 7.5vmin, 76px);
  font-weight: 800;
  letter-spacing: -0.02em;
}

.levels {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: clamp(12px, 2.5vmin, 28px);
  width: min(100%, 1100px);
}

.level-card {
  --lv: #ddd;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1vmin;
  padding: clamp(16px, 3.5vmin, 36px) 8px;
  border-radius: 32px;
  background: var(--lv);
  box-shadow: 0 7px 0 rgba(45, 49, 66, 0.14);
  transition: transform 0.08s, box-shadow 0.08s;
}
.level-card:active { transform: translateY(5px); box-shadow: 0 2px 0 rgba(45, 49, 66, 0.14); }
.level-card[data-level="1"] { --lv: #bfe0f5; }
.level-card[data-level="2"] { --lv: #cdeabb; }
.level-card[data-level="3"] { --lv: #f6e3a1; }
.level-card[data-level="4"] { --lv: #ddd0f2; }

.lv-num { font-size: clamp(56px, 14vmin, 140px); font-weight: 800; line-height: 1; }
.lv-desc { font-size: clamp(16px, 2.8vmin, 26px); color: var(--ink-soft); }
.lv-best {
  margin-top: 0.6vmin;
  padding: 6px 16px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.7);
  font-size: clamp(16px, 2.8vmin, 26px);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* ---------- 게임 화면 ---------- */
.bar { display: flex; align-items: center; gap: var(--gap); flex: none; }

.icon-btn {
  width: var(--tap);
  height: var(--tap);
  border-radius: 50%;
  background: var(--surface);
  color: var(--ink-soft);
  font-size: 26px;
}
.icon-btn:active { background: var(--press); }

/* 조용한 시간 막대 (REQ-TIMER-002): 점멸·소리 없음 */
.timebar { flex: 1; height: 16px; border-radius: 99px; background: rgba(45, 49, 66, 0.1); overflow: hidden; }
#timefill { height: 100%; background: var(--accent); opacity: 0.7; transform-origin: left; transition: transform 0.2s linear; }
#timeleft { min-width: 3.2em; text-align: right; font-size: clamp(18px, 3vmin, 28px); color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.score {
  min-width: 2.6em;
  padding: 8px 18px;
  border-radius: 99px;
  background: var(--surface);
  text-align: center;
  font-size: clamp(22px, 4vmin, 38px);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.play { flex: 1; min-height: 0; display: flex; gap: var(--gap); }
body[data-level="1"] .play { flex-direction: column; }

.problem {
  flex: 1 1 55%;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 32px;
  background: var(--surface);
  transition: background-color 0.15s;
}
.problem.ok { background: var(--ok-bg); }
.problem.ng { background: var(--ng-bg); }
body[data-level="1"] .problem { flex: 1 1 42%; }

#answers { flex: 1 1 45%; min-width: 0; min-height: 0; display: flex; }
body[data-level="1"] #answers { flex: 1 1 58%; }

.horizontal {
  font-size: clamp(44px, 14vmin, 160px);
  font-weight: 800;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
body[data-level="2"] .horizontal { font-size: clamp(30px, 8vmin, 96px); }

.answer-box {
  display: inline-block;
  min-width: 1.5em;
  height: 1.15em;
  padding: 0 0.1em;
  border-bottom: 0.08em solid var(--accent);
  color: var(--accent);
  text-align: center;
  vertical-align: bottom;
}

/* 1단계 숫자카드 5×2 (REQ-CARD-001) */
.cards { flex: 1; display: grid; grid-template-columns: repeat(5, 1fr); grid-template-rows: 1fr 1fr; gap: clamp(8px, 2vmin, 20px); }

.card, .keypad button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: 0 5px 0 rgba(45, 49, 66, 0.12);
  transition: transform 0.06s, box-shadow 0.06s;
}
.card:active, .keypad button:active { transform: translateY(3px); box-shadow: 0 2px 0 rgba(45, 49, 66, 0.12); }

.card { gap: 0.6vmin; padding: 4px; }
.card .num { font-size: clamp(32px, 8vmin, 88px); font-weight: 800; line-height: 1; }
.card .pic { width: clamp(28px, 8vmin, 84px); height: clamp(28px, 8vmin, 84px); }
.card .pic img { display: block; width: 100%; height: 100%; object-fit: contain; pointer-events: none; }

/* 2~4단계 키패드 (REQ-PAD-001) */
.keypad { flex: 1; display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(4, 1fr); gap: clamp(8px, 1.6vmin, 18px); }
.keypad button { font-size: clamp(28px, 6vmin, 64px); font-weight: 800; }
.keypad .ok-key { background: var(--accent); color: #fff; font-size: clamp(22px, 4.5vmin, 46px); }
.keypad .ok-key:disabled { opacity: 0.4; cursor: default; }

/* 세로식 (REQ-PAD-002): 자리 맞춤 */
.vertical { font-size: clamp(40px, 12vmin, 130px); font-weight: 800; line-height: 1.12; font-variant-numeric: tabular-nums; }
body[data-level="4"] .vertical { font-size: clamp(36px, 10vmin, 116px); }
.vrow { display: grid; grid-template-columns: 0.8em repeat(var(--w), 0.78em); justify-items: center; align-items: center; }
.vrow.op { border-bottom: 0.07em solid var(--ink); padding-bottom: 0.04em; margin-bottom: 0.1em; }
.vrow.ans .cell { width: 0.72em; height: 1.08em; border-radius: 0.12em; background: rgba(45, 49, 66, 0.07); color: var(--accent); line-height: 1.08em; text-align: center; }
.vrow.carry { height: 0.55em; }
.vrow.carry .cell { font-size: 0.48em; color: var(--accent); align-self: end; }

/* ---------- 결과·해설 ---------- */
#result, #explain { align-items: center; justify-content: center; }

.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: clamp(10px, 2.2vmin, 24px);
  max-width: 100%;
  max-height: 100%;
  padding: clamp(20px, 4vmin, 44px) clamp(20px, 6vmin, 64px);
  border-radius: 36px;
  background: var(--surface);
  text-align: center;
}
.panel p { margin: 0; }

.newbest {
  padding: 6px 20px;
  border-radius: 99px;
  background: #ffe58a;
  color: #6b4e00;
  font-size: clamp(18px, 3.6vmin, 34px);
  font-weight: 800;
}
.big-score { font-size: clamp(64px, 20vmin, 200px); font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; }
.big-score small { font-size: 0.35em; margin-left: 0.1em; }
.sub { font-size: clamp(16px, 3vmin, 28px); color: var(--ink-soft); }
.sub b { color: var(--ink); }

.actions { display: flex; flex-wrap: wrap; justify-content: center; gap: clamp(10px, 2vmin, 20px); }

.btn {
  padding: 0 28px;
  border-radius: 99px;
  background: rgba(45, 49, 66, 0.08);
  font-size: clamp(18px, 3.2vmin, 30px);
  font-weight: 700;
}
.btn:active { background: var(--press); }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:active { filter: brightness(0.9); }

#explain .panel { flex: 1; width: min(100%, 960px); justify-content: space-between; }
#ex-body { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: clamp(8px, 2vmin, 20px); }
#ex-body .horizontal { font-size: clamp(36px, 10vmin, 120px); }
#ex-body > p.step { font-size: clamp(22px, 5vmin, 56px); font-weight: 700; font-variant-numeric: tabular-nums; }
.captions { display: flex; flex-direction: column; gap: 0.3em; }
.captions p { font-size: clamp(18px, 3.4vmin, 34px); font-weight: 700; font-variant-numeric: tabular-nums; }
.hl { padding: 0 0.12em; border-radius: 0.15em; background: rgba(255, 229, 138, 0.8); color: var(--ink); }

/* 해설 페이드인: 천천히 (REQ-EXPL-003) */
.step { opacity: 0; transition: opacity 0.9s ease; }
.step.show { opacity: 1; }

/* ---------- 보호자 메뉴 ---------- */
dialog {
  border: none;
  border-radius: 28px;
  padding: 28px 32px;
  background: #fff;
  color: var(--ink);
  text-align: center;
}
dialog::backdrop { background: rgba(45, 49, 66, 0.35); }
dialog h2 { margin: 0 0 20px; font-size: 24px; }

/* ---------- 세로 화면 (폰·태블릿 세로) (REQ-PLAT-002, 004) ---------- */
@media (orientation: portrait) {
  .levels { grid-template-columns: repeat(2, 1fr); }
  .play { flex-direction: column; }
  .problem { flex: 1 1 45%; }
  #answers { flex: 1 1 55%; }
  body[data-level="2"] .horizontal { font-size: clamp(28px, 10vmin, 110px); }
}

/* 좁은 폰(360px): 카드 5장이 64px 이상 들어가도록 여백을 줄임 */
@media (max-width: 380px) {
  .screen { padding-left: max(8px, env(safe-area-inset-left)); padding-right: max(8px, env(safe-area-inset-right)); }
  .cards { gap: 6px; }
  .card { min-width: 0; }
}

/* 낮은 가로 화면(폰 가로): 해설 설명문을 세로식 옆에 둠 */
@media (max-height: 500px) and (orientation: landscape) {
  #ex-body { flex-direction: row; gap: 4vw; }
}

@media (prefers-reduced-motion: reduce) {
  .step { transition-duration: 0.01s; }
}
