:root {
  --ink: #1a1a1a;
  --paper: #ffffff;
  --bg: #f3f0ea;
  --line: #d9d4cb;
  --accent: #ffcc33;
  --accent-ink: #4a3600;
  --btn: 60px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  height: 100dvh; overflow: hidden;
  background: var(--bg); color: var(--ink);
  font-family: "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", system-ui, sans-serif;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; border: 0; background: none; }

/* ---------- 상단 ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 8px 10px; flex: 0 0 auto;
}
.topbar-center { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center; }
.nav-btn {
  width: var(--btn); height: var(--btn); border-radius: 50%;
  background: var(--paper); border: 3px solid var(--ink);
  font-size: 40px; line-height: 1; font-weight: 700; flex: 0 0 auto;
  display: grid; place-items: center; padding-bottom: 6px;
}
.nav-btn:active { background: var(--accent); }
.chip {
  display: flex; align-items: center; gap: 6px; height: 44px; padding: 0 14px;
  border-radius: 22px; background: var(--paper); border: 2px solid var(--ink);
  font-weight: 700; font-size: 16px;
}
.chip:active { background: var(--accent); }
.chip .ico { font-size: 20px; }
.counter { font-weight: 800; font-size: 18px; min-width: 70px; text-align: center; letter-spacing: 1px; }

/* ---------- 캔버스 ---------- */
.main { flex: 1 1 auto; min-height: 0; display: grid; place-items: center; padding: 0 8px; }
.stage {
  position: relative; background: var(--paper);
  box-shadow: 0 2px 0 var(--line), 0 10px 30px rgba(0,0,0,.12);
  width: 300px; height: 300px; /* JS가 크기 조정 */
  overflow: hidden;
}
.stage canvas {
  position: absolute; inset: 0; width: 100%; height: 100%;
  touch-action: none;
}
#live { pointer-events: none; }
#base { pointer-events: none; }
.stage.crayon #live { opacity: .55; }
.title-box {
  position: absolute; left: 0; top: 0; pointer-events: none;
  background: var(--ink); color: #fff;
  font-weight: 900; font-size: clamp(16px, 4.2cqw, 34px);
  padding: .35em .6em .4em; line-height: 1.15;
  max-width: 85%;
}
.stage { container-type: inline-size; }
.stage[data-tool="fill"] canvas { cursor: crosshair; }
.stage[data-tool="eraser"] canvas { cursor: cell; }

/* ---------- 툴바 ---------- */
.toolbar { flex: 0 0 auto; padding: 6px 8px 8px; display: flex; flex-direction: column; gap: 6px; }
.row { display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap; }
.tool, .action {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-width: var(--btn); height: var(--btn); padding: 0 8px; border-radius: 16px;
  background: var(--paper); border: 3px solid transparent; box-shadow: 0 2px 0 var(--line);
}
.tool .ico, .action .ico { font-size: 26px; line-height: 1; }
.tool .lbl, .action .lbl { font-size: 11px; font-weight: 700; }
.action .ico { font-size: 24px; }
.tool.on { border-color: var(--ink); background: var(--accent); }
.tool:active, .action:active { transform: translateY(2px); box-shadow: none; }
.action:disabled { opacity: .35; }
.sep { width: 2px; height: 40px; background: var(--line); border-radius: 2px; }
.sizes { display: flex; gap: 6px; }
.size {
  width: 50px; height: var(--btn); border-radius: 14px; display: grid; place-items: center;
  background: var(--paper); border: 3px solid transparent; box-shadow: 0 2px 0 var(--line);
}
.size i { display: block; width: var(--d); height: var(--d); border-radius: 50%; background: var(--ink); }
.size.on { border-color: var(--ink); background: var(--accent); }

.colors { gap: 8px; }
.color {
  width: 46px; height: 46px; border-radius: 50%;
  border: 3px solid #fff; box-shadow: 0 0 0 2px var(--line), 0 2px 0 var(--line);
  transition: transform .1s;
}
.color.on { transform: scale(1.22); box-shadow: 0 0 0 3px var(--ink); }
.color[data-color="#ffffff"] { box-shadow: 0 0 0 2px #aaa; }
.color[data-color="#ffffff"].on { box-shadow: 0 0 0 3px var(--ink); }

/* ---------- 하단 링크 / 광고 자리 ---------- */
.site-links {
  flex: 0 0 auto; display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 14px;
  padding: 2px 10px 6px; font-size: 12px; color: #777;
}
.site-links a { color: #555; text-decoration: none; }
.site-links a:hover { text-decoration: underline; }
.app-ad { flex: 0 0 auto; text-align: center; padding: 0 8px; }
.app-ad:empty { display: none; }

/* ---------- 모달 ---------- */
.modal[hidden], .toast[hidden] { display: none; }
.modal {
  position: fixed; inset: 0; z-index: 50; background: rgba(0,0,0,.45);
  display: grid; place-items: center; padding: 12px;
}
.modal-card { background: var(--paper); border-radius: 20px; padding: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.gallery-card { width: min(96vw, 1100px); max-height: 92dvh; display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 10px; }
.modal-head h2 { margin: 0; font-size: 22px; }
.close { width: 48px; height: 48px; border-radius: 50%; border: 2px solid var(--ink); font-size: 22px; font-weight: 700; }
.gallery {
  overflow: auto; display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  padding: 4px;
}
.thumb {
  display: flex; flex-direction: column; gap: 6px; padding: 8px; border-radius: 14px;
  background: var(--bg); border: 3px solid transparent; text-align: left;
}
.thumb.on { border-color: var(--ink); background: var(--accent); }
.thumb canvas { width: 100%; aspect-ratio: 1; background: #fff; border-radius: 8px; box-shadow: 0 1px 0 var(--line); }
.thumb .t { font-size: 13px; font-weight: 700; line-height: 1.25; }
.thumb .n { font-size: 11px; opacity: .6; }
.lang-card { width: min(90vw, 420px); }
.lang-list { display: flex; flex-direction: column; gap: 10px; }
.lang-list button { height: 64px; border-radius: 16px; font-size: 22px; font-weight: 800; border: 3px solid var(--line); background: var(--paper); }
.lang-list button.on { border-color: var(--ink); background: var(--accent); }
.confirm-card { text-align: center; width: min(90vw, 420px); }
.confirm-card p { font-size: 26px; font-weight: 800; margin: 12px 0 22px; }
.confirm-btns { display: flex; gap: 12px; }
.big { flex: 1; height: 70px; border-radius: 18px; font-size: 22px; font-weight: 800; border: 3px solid var(--ink); }
.big.no { background: var(--paper); }
.big.yes { background: #ff6b6b; color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: 140px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 12px 20px; border-radius: 30px;
  font-weight: 700; font-size: 18px; z-index: 60; pointer-events: none;
}

/* ---------- 좁은 화면 ---------- */
@media (max-width: 560px) {
  :root { --btn: 48px; }
  .toolbar { padding: 4px 6px 6px; gap: 4px; }
  .row { gap: 5px; }
  .tool, .action { min-width: var(--btn); padding: 0 4px; border-radius: 12px; }
  .tool .lbl, .action .lbl, .chip .lbl { display: none; }
  .chip { padding: 0 10px; }
  .color { width: 34px; height: 34px; border-width: 2px; }
  .size { width: 40px; border-radius: 12px; }
  .sep { display: none; }
}
/* 가로로 긴 화면: 툴바를 오른쪽에 */
@media (min-aspect-ratio: 4/3) and (min-width: 900px) {
  body { display: grid; grid-template-rows: auto 1fr auto auto; grid-template-columns: 1fr 190px; }
  .topbar { grid-column: 1 / -1; }
  .main { grid-row: 2; grid-column: 1; }
  .toolbar { grid-row: 2; grid-column: 2; justify-content: center; padding-right: 12px; }
  .app-ad, .site-links { grid-column: 1 / -1; }
  .row { justify-content: flex-start; }
  .sep { width: 100%; height: 2px; }
}
