/* 나무젓가락 다리 공방 — 공책 낙서 느낌 */
@font-face {
  font-family: 'Gaegu';
  src: url('fonts/Gaegu-Regular.woff2') format('woff2');
  font-weight: 400; font-display: swap;
}
@font-face {
  font-family: 'Gaegu';
  src: url('fonts/Gaegu-Bold.woff2') format('woff2');
  font-weight: 700; font-display: swap;
}

:root {
  --paper: #fbf6e8; --paper2: #f3ead2; --rule: rgba(90, 140, 200, 0.16); --margin: rgba(220, 90, 90, 0.35);
  --ink: #2c2a35; --ink2: #5d5866; --dim: #8b8392;
  --red: #e2553f; --orange: #f29b38; --yellow: #f7cf4a; --green: #4fae5b; --blue: #3f7fd0;
  --wobble: 255px 14px 225px 14px / 14px 225px 14px 255px;
  --wobble2: 14px 225px 14px 255px / 255px 14px 225px 14px;
  --font: 'Gaegu', 'Apple SD Gothic Neo', 'Malgun Gothic', system-ui, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  height: 100dvh; min-height: 0;
  background-color: var(--paper);
  background-image:
    linear-gradient(90deg, transparent 38px, var(--margin) 38px, var(--margin) 40px, transparent 40px),
    repeating-linear-gradient(0deg, transparent 0 27px, var(--rule) 27px 28px);
  color: var(--ink);
  font-family: var(--font); font-size: 19px;
  display: flex; flex-direction: column; overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { cursor: not-allowed; }
[hidden] { display: none !important; }
img { -webkit-user-drag: none; user-select: none; }

/* ───── 공통 버튼 ───── */
.btn {
  border: 2.5px solid var(--ink); background: #fffdf6; border-radius: var(--wobble);
  padding: 8px 18px; font-weight: 700; font-size: 1.05rem; line-height: 1.1;
  box-shadow: 2px 3px 0 var(--ink); transition: transform 0.08s;
}
.btn:hover { transform: translate(-1px, -1px) rotate(-0.6deg); }
.btn:active { transform: translate(2px, 3px); box-shadow: 0 0 0 var(--ink); }
.btn.small { padding: 4px 10px; font-size: 0.95rem; box-shadow: 1.5px 2px 0 var(--ink); }
.btn.primary { background: var(--yellow); }
.btn.big { font-size: 1.35rem; padding: 10px 26px; }
.row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 14px; }

/* ───── 상단 ───── */
.topbar {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px 4px;
  padding-top: max(8px, env(safe-area-inset-top));
  padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right));
}
.back-btn {
  display: inline-flex; align-items: center; gap: 6px; padding: 5px 12px;
  border: 2px solid var(--ink); border-radius: var(--wobble2); background: #fffdf6;
  color: var(--ink); text-decoration: none; font-size: 1rem; font-weight: 700;
  box-shadow: 1.5px 2px 0 var(--ink);
}
.back-btn:hover { background: #fff3c4; }
.total { margin-left: auto; font-weight: 700; font-size: 1.15rem; display: flex; align-items: center; gap: 4px; }
.total small { color: var(--dim); font-weight: 400; }
.star-ico { display: inline-block; width: 22px; height: 22px; background: center / contain no-repeat; }
.tools { display: flex; gap: 6px; }
.mini {
  width: 36px; height: 36px; border: 2px solid var(--ink); border-radius: var(--wobble);
  background: #fffdf6; font-weight: 700; font-size: 1.1rem; padding: 0; box-shadow: 1.5px 2px 0 var(--ink);
}

.screen { flex: 1; min-height: 0; overflow-y: auto; padding: 4px 16px 28px; }
.screen.center { display: flex; align-items: center; justify-content: center; }

/* ───── 불러오기 ───── */
.load-box { text-align: center; width: min(320px, 80vw); }
.pencil { font-size: 2.6rem; display: inline-block; animation: scribble 0.7s ease-in-out infinite alternate; }
@keyframes scribble { from { transform: translateX(-14px) rotate(-12deg); } to { transform: translateX(14px) rotate(8deg); } }
.load-bar { height: 14px; border: 2px solid var(--ink); border-radius: var(--wobble); overflow: hidden; background: #fff; }
.load-bar i {
  display: block; height: 100%; width: 0; transition: width 0.15s;
  background: repeating-linear-gradient(-45deg, var(--orange) 0 6px, var(--yellow) 6px 12px);
}

/* ───── 타이틀 ───── */
.title-wrap { max-width: 760px; margin: 0 auto; text-align: center; }
.title-art {
  margin: 4px auto 6px; max-width: 640px; padding: 8px; background: #fff;
  border: 2.5px solid var(--ink); border-radius: var(--wobble2); box-shadow: 3px 4px 0 var(--ink);
  transform: rotate(-1deg);
}
.title-art img { display: block; width: 100%; height: auto; border-radius: 6px; }
.logo {
  margin: 14px 0 2px; font-size: clamp(2rem, 7vw, 3.2rem); font-weight: 700; letter-spacing: -1px;
  text-shadow: 3px 3px 0 var(--yellow);
}
.tagline { margin: 0; color: var(--ink2); font-size: 1.1rem; line-height: 1.35; }

/* ───── 선택 ───── */
.sel-wrap { max-width: 780px; margin: 0 auto; }
.sel-head { display: flex; align-items: center; gap: 10px; margin: 4px 0 10px; }
.sel-head h2 { margin: 0; font-size: 1.5rem; }
.ch-tabs { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.ch-tab {
  border: 2.5px solid var(--ink); border-radius: var(--wobble); background: #fffdf6; padding: 6px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 0; box-shadow: 2px 3px 0 var(--ink);
  font-weight: 700; font-size: 1rem; line-height: 1.1; position: relative; overflow: hidden;
}
.ch-tab .em { font-size: 1.5rem; }
.ch-tab small { font-weight: 400; color: var(--ink2); font-size: 0.85rem; }
.ch-tab.on { background: var(--yellow); transform: rotate(-1.5deg); }
.ch-tab:disabled { opacity: 0.45; }
.ch-desc { margin: 12px 2px 8px; color: var(--ink2); font-size: 1.05rem; }
.lv-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 12px; }
@media (min-width: 560px) { .lv-grid { grid-template-columns: repeat(3, 1fr); } }
.lv-btn {
  border: 2.5px solid var(--ink); border-radius: var(--wobble2); background: #fffdf6 center / cover no-repeat;
  padding: 0; display: flex; flex-direction: column; overflow: hidden; box-shadow: 2px 3px 0 var(--ink);
  text-align: left; transition: transform 0.1s;
}
.lv-btn:nth-child(odd) { transform: rotate(-0.7deg); }
.lv-btn:nth-child(even) { transform: rotate(0.6deg); }
.lv-btn:hover:not(:disabled) { transform: rotate(0) translateY(-2px); }
.lv-btn .thumb { display: block; width: 100%; aspect-ratio: 16 / 9; background: var(--paper2); }
.lv-btn .cap { padding: 4px 8px 6px; display: flex; align-items: center; gap: 6px; background: rgba(255, 253, 246, 0.95); border-top: 2px solid var(--ink); }
.lv-btn .cap b { font-size: 1.1rem; }
.lv-btn .cap span { font-size: 0.95rem; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lv-btn .st { display: flex; gap: 1px; }
.lv-btn .st img, .stars img { display: block; }
.lv-btn:disabled { filter: grayscale(0.9); opacity: 0.55; }
.lv-btn .lock { font-size: 1.6rem; position: absolute; }
.lv-btn .thumb-wrap { position: relative; display: flex; align-items: center; justify-content: center; }

/* ───── 플레이 ───── */
#play {
  flex: 1; display: flex; flex-direction: column; min-height: 0; width: 100%; max-width: 1180px; margin: 0 auto;
  padding: 0 10px; padding-bottom: max(8px, env(safe-area-inset-bottom));
  padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right));
}
.p-head { display: flex; align-items: center; gap: 8px; min-width: 0; }
.p-title { font-size: 1.2rem; font-weight: 700; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.p-title b { background: var(--yellow); padding: 0 6px; border-radius: var(--wobble); }
.p-cost { margin-left: auto; font-size: 1.2rem; font-weight: 700; white-space: nowrap; padding: 1px 10px; border: 2px dashed transparent; border-radius: var(--wobble); }
.p-cost small { color: var(--ink2); font-weight: 400; }
.p-cost.over { color: var(--red); border-color: var(--red); background: #ffe8e2; }
.hint { margin: 4px 0; font-size: 1.02rem; line-height: 1.25; color: var(--ink2); padding: 2px 8px; border-left: 4px solid var(--orange); background: rgba(255, 240, 200, 0.55); }
.board { flex: 1; min-height: 120px; position: relative; display: flex; align-items: center; justify-content: center; }
#board {
  touch-action: none; display: block; cursor: crosshair;
  border: 2.5px solid var(--ink); border-radius: 6px 10px 7px 12px; box-shadow: 3px 4px 0 rgba(44, 42, 53, 0.85);
  background: #fff;
}
#board.erase { cursor: cell; }
#board.sim { cursor: default; }
.msg {
  position: absolute; left: 50%; top: 10px; transform: translateX(-50%) rotate(-0.8deg);
  background: #fffdf6; border: 2.5px solid var(--red); color: var(--red); font-weight: 700;
  padding: 4px 14px; border-radius: var(--wobble); font-size: 1.08rem; text-align: center; max-width: 92%;
  box-shadow: 2px 3px 0 rgba(0, 0, 0, 0.25); pointer-events: none;
}
.msg.ok { border-color: var(--green); color: #2f7b3a; }

.mats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; padding: 8px 2px 2px; }
.mats.locked { opacity: 0.45; pointer-events: none; }
.mat {
  min-width: 0; display: flex; flex-direction: column; align-items: stretch; gap: 0;
  border: 2.5px solid var(--ink); border-radius: var(--wobble2); background: #fffdf6; padding: 4px 8px 4px;
  box-shadow: 2px 3px 0 var(--ink); text-align: left; line-height: 1.05;
}
.mat .pic { height: 16px; background: left center / contain no-repeat; margin: 2px 0 3px; }
.mat b { font-size: 1.05rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.mat small { color: var(--ink2); font-size: 0.85rem; }
.mat.on { background: #fff1b8; transform: translateY(-2px) rotate(-1deg); box-shadow: 3px 5px 0 var(--ink); }
.mat .kind { font-size: 0.78rem; color: var(--dim); }

.toolbar { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; padding-top: 8px; }
#simBar { grid-template-columns: 1fr 1fr 1fr 2fr; }
.tool {
  border: 2.5px solid var(--ink); border-radius: var(--wobble); background: #fffdf6; padding: 3px 2px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 0;
  min-height: 52px; box-shadow: 2px 3px 0 var(--ink); line-height: 1.05;
}
.tool .ti { font-size: 1.3rem; }
.tool span:last-child { font-size: 0.95rem; font-weight: 700; }
.tool.on { background: #fff1b8; }
.tool:active { transform: translate(2px, 3px); box-shadow: none; }
.tool:disabled { opacity: 0.4; }
.tool.go { background: #bfe8a8; }
.tool.go.blocked { background: #f1d6cf; }
.tool.stop { background: #ffc9b8; }

/* ───── 모달 ───── */
.modal {
  position: fixed; inset: 0; background: rgba(60, 52, 40, 0.42); display: flex; align-items: center; justify-content: center;
  padding: 16px; z-index: 10;
}
.panel {
  background: #fffdf6; border: 3px solid var(--ink); border-radius: var(--wobble2); box-shadow: 4px 6px 0 var(--ink);
  padding: 16px 20px; max-width: 460px; width: 100%; max-height: 90vh; overflow-y: auto;
  background-image: repeating-linear-gradient(0deg, transparent 0 27px, var(--rule) 27px 28px);
  animation: pop 0.22s ease-out;
}
@keyframes pop { from { transform: scale(0.85) rotate(-3deg); opacity: 0; } to { transform: none; opacity: 1; } }
.panel h2 { margin: 0 0 8px; font-size: 1.6rem; }
.panel p, .panel li { font-size: 1.08rem; line-height: 1.4; margin: 4px 0; }
.panel ul { padding-left: 20px; margin: 6px 0; }
.result { text-align: center; }
.stars { display: flex; justify-content: center; gap: 6px; margin: 4px 0 6px; }
.stars img { width: 54px; height: 54px; object-fit: contain; animation: stamp 0.35s backwards; }
.stars img:nth-child(2) { animation-delay: 0.15s; }
.stars img:nth-child(3) { animation-delay: 0.3s; }
@keyframes stamp { from { transform: scale(2.2) rotate(-30deg); opacity: 0; } to { transform: none; opacity: 1; } }
.fx-img { width: 120px; height: auto; display: block; margin: 0 auto; }
.ok-line { color: #2f7b3a; font-weight: 700; }
.bad-line { color: var(--red); font-weight: 700; }
.err h2 { color: var(--red); }
.noscript { padding: 20px; text-align: center; }
.kbd { font-size: 0.95rem; color: var(--ink2); }

/* ───── 좁은 화면(세로 휴대폰) ───── */
@media (max-width: 520px) {
  body { font-size: 17px; }
  .back-txt { display: none; }
  .hint { font-size: 0.95rem; }
  .p-title { font-size: 1.05rem; }
  .p-cost { font-size: 1.05rem; padding: 1px 6px; }
  .tool { min-height: 50px; }
  .tool span:last-child { font-size: 0.82rem; }
  .mats { gap: 6px; }
  .mat { padding: 3px 5px; }
  .mat b { font-size: 0.92rem; white-space: normal; line-height: 1; }
  .mat small { font-size: 0.74rem; }
  .mat .kind { font-size: 0.7rem; }
  .ch-tab { font-size: 0.9rem; }
  .ch-tab small { display: none; }
}
/* 낮은 화면(가로 휴대폰): 힌트·여백 줄이기 */
@media (max-height: 520px) {
  .hint { display: none; }
  .topbar { padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 2px; }
  .mats { padding-top: 4px; }
  .mat .pic { display: none; }
  .tool { min-height: 40px; }
  .tool .ti { font-size: 1.05rem; }
}
