* { box-sizing: border-box; }
:root {
  --paper: #fffdf6;
  --navy: #1d2a44;
  --ink: #2e3850;
  --orange: #f08a24;
  --orange-deep: #d96b12;
  --red: #ec4a4a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}
html, body { width: 100%; height: 100%; overflow: hidden; background: #e4e9e6; }
body {
  margin: 0;
  color: var(--navy);
  font-family: 'Pretendard Variable', Pretendard, -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
#stage {
  position: relative; width: 100%; height: 100dvh; overflow: hidden; isolation: isolate; transform: translateZ(0);
  background: linear-gradient(#eef2ef, #d9e2dd);
  touch-action: none;
}
#game { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; }

/* 상단 고정 버튼 */
.back-link, .icon-btn {
  position: absolute; top: calc(12px + var(--safe-top)); z-index: 30;
  padding: 7px 12px; border: 1px solid rgba(29,42,68,.12); border-radius: 999px;
  background: rgba(255,253,246,.92); color: var(--navy); box-shadow: 0 3px 10px rgba(30,40,30,.12);
  font: inherit; font-size: 12px; font-weight: 600; text-decoration: none; line-height: 1.2;
}
.back-link { left: calc(12px + var(--safe-left)); }
.icon-btn { right: calc(12px + var(--safe-right)); width: 34px; height: 30px; padding: 0; display: grid; place-items: center; cursor: pointer; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: var(--navy); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn .spk { fill: var(--navy); }
.icon-btn .mute { display: none; }
.icon-btn.off .wave { display: none; }
.icon-btn.off .mute { display: inline; stroke: #8a8f9c; }
.icon-btn.off .spk { fill: #8a8f9c; stroke: #8a8f9c; }

/* HUD — 아이콘 + 숫자만 */
#hud {
  position: absolute; top: calc(50px + var(--safe-top)); left: 50%; z-index: 10;
  transform: translateX(-50%); pointer-events: none;
}
.hud-bar {
  display: flex; align-items: center; gap: 2px;
  padding: 5px 6px; border-radius: 999px; background: rgba(255,253,246,.9);
  border: 1px solid rgba(29,42,68,.1); box-shadow: 0 6px 18px rgba(30,40,30,.14);
}
.hud-cell { display: flex; align-items: center; gap: 5px; padding: 0 10px; }
.hud-cell + .hud-cell { border-left: 1px solid rgba(29,42,68,.12); }
#found-cell { gap: 10px; }
.device-count { display: inline-flex; align-items: center; gap: 5px; }
.hud-cell svg { width: 18px; height: 18px; fill: none; stroke: #6b7286; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.hud-cell strong {
  color: var(--navy); font-size: 19px; line-height: 1.1; font-weight: 800;
  font-variant-numeric: tabular-nums; letter-spacing: -.02em; min-width: 1.4em;
}
.hud-cell small { color: #8a90a0; font-size: 12px; font-weight: 700; }
#time-cell strong { min-width: 1.6em; }
#hud.warn #time-cell strong { color: var(--red); }
#hud.warn #time-cell svg { stroke: var(--red); }
.bump { animation: bump .28s cubic-bezier(.2,1.6,.4,1); }
@keyframes bump { from { transform: scale(1.3); } }

/* 조준점: 손 닿는 기기에 걸리면 동그라미로 */
#reticle {
  position: absolute; left: 50%; top: 50%; z-index: 9; width: 6px; height: 6px; margin: -3px 0 0 -3px;
  border-radius: 50%; background: rgba(255,255,255,.95); box-shadow: 0 0 0 1.5px rgba(20,30,40,.45);
  pointer-events: none; opacity: 0; transition: width .12s, height .12s, margin .12s, background .12s, opacity .2s;
}
#stage.playing #reticle { opacity: 1; }
#reticle.near {
  width: 22px; height: 22px; margin: -11px 0 0 -11px; background: transparent;
  box-shadow: 0 0 0 2px rgba(255,255,255,.95), 0 0 0 3.5px rgba(20,30,40,.35);
}

/* 휴대폰 조이스틱 — 왼쪽 아래 누른 자리에 생긴다 */
.stick {
  --sx: calc(78px + var(--safe-left)); --sy: calc(100% - 110px - var(--safe-bottom));
  position: absolute; inset: 0; z-index: 8; pointer-events: none; display: none;
}
#stage.playing .stick { display: block; }
.stick-base, .stick-knob {
  position: absolute; left: var(--sx); top: var(--sy); border-radius: 50%;
}
.stick-base {
  width: 104px; height: 104px; margin: -52px 0 0 -52px;
  border: 2px solid rgba(255,255,255,.75); background: rgba(20,30,40,.12);
  box-shadow: 0 2px 10px rgba(0,0,0,.12); opacity: .55; transition: opacity .15s;
}
.stick-knob {
  width: 46px; height: 46px; margin: -23px 0 0 -23px;
  background: rgba(255,255,255,.85); box-shadow: 0 2px 8px rgba(0,0,0,.2); opacity: .6;
}
.stick.on .stick-base, .stick.on .stick-knob { opacity: 1; }
@media (pointer: fine) { .stick { display: none !important; } }

/* 감도 버튼 */
.chop-btn {
  position: absolute; right: calc(18px + var(--safe-right)); bottom: calc(26px + var(--safe-bottom)); z-index: 12;
  width: 76px; height: 76px; padding: 0; border-radius: 50%; display: none; cursor: pointer;
  border: 2px solid rgba(255,255,255,.85); background: rgba(255,248,236,.55);
  box-shadow: 0 4px 14px rgba(0,0,0,.18); touch-action: none; -webkit-touch-callout: none;
}
#stage.playing .chop-btn { display: grid; place-items: center; }
.chop-btn img { width: 64px; height: 64px; object-fit: contain; border-radius: 50%; pointer-events: none; }
.chop-btn:active { transform: scale(.94); background: rgba(255,236,210,.8); }
@media (pointer: fine) {
  .chop-btn { right: auto; left: calc(18px + var(--safe-left)); width: 58px; height: 58px; opacity: .8; }
  .chop-btn img { width: 48px; height: 48px; }
}

#flash { position: absolute; inset: 0; z-index: 11; pointer-events: none; opacity: 0; }
#flash.wrong { box-shadow: inset 0 0 0 6px var(--red), inset 0 0 90px rgba(236,74,74,.45); animation: flash .45s ease-out; }
#flash.good { background: radial-gradient(circle at 50% 50%, rgba(255,255,255,.35), transparent 40%); animation: flash .22s ease-out; }
@keyframes flash { 0% { opacity: 1; } 100% { opacity: 0; } }

/* 화면들 */
.screen { position: absolute; inset: 0; z-index: 20; }
.screen[hidden] { display: none !important; }
.screen.center {
  display: grid; place-items: center; padding: 24px;
  background: rgba(25,35,30,.3); backdrop-filter: blur(3px);
}
#loading { background: linear-gradient(#eef2ef, #d9e2dd); backdrop-filter: none; }
.loading-text { margin: 0; color: #5f6f68; font-size: 14px; font-weight: 600; animation: breathe 1.2s ease-in-out infinite alternate; }
@keyframes breathe { to { opacity: .45; } }

.panel {
  width: min(100%, 340px); padding: 24px 22px 20px; border-radius: 22px; text-align: center;
  background: var(--paper); border: 1px solid rgba(29,42,68,.08); box-shadow: 0 18px 50px rgba(20,30,25,.28);
  animation: panel-in .26s cubic-bezier(.2,1.2,.35,1);
}
@keyframes panel-in { from { opacity: 0; transform: translateY(12px) scale(.98); } }
.panel-title { color: var(--navy); font-size: 23px; font-weight: 850; letter-spacing: -.045em; }
.panel p { margin: 8px 0 18px; color: #6b7286; font-size: 14px; line-height: 1.5; }
.main-btn {
  width: 100%; padding: 14px 18px; border: 0; border-radius: 999px; cursor: pointer;
  background: linear-gradient(135deg, var(--orange), var(--orange-deep)); color: #fff;
  box-shadow: 0 6px 16px rgba(217,107,18,.32); font: inherit; font-size: 17px; font-weight: 800; letter-spacing: -.02em;
}
.main-btn:active { transform: translateY(1px); }

/* 타이틀: 위에 제목, 아래에 시작 카드. 가운데는 교실이 보이게 비워둔다. */
#title { pointer-events: none; }
#title .panel { pointer-events: auto; }
.title-top { position: absolute; top: calc(54px + var(--safe-top)); left: 0; right: 0; text-align: center; }
.title-tag {
  display: inline-block; padding: 4px 11px; border-radius: 999px; background: var(--navy); color: #fff;
  font-size: 12px; font-weight: 700; letter-spacing: .02em;
}
.title-name {
  margin-top: 6px; color: var(--navy); font-size: clamp(34px, 9vw, 48px); font-weight: 900; letter-spacing: -.06em; line-height: 1.05;
  text-shadow: 0 3px 0 #fff, 0 6px 18px rgba(255,255,255,.8);
}
.title-panel {
  position: absolute; left: 50%; bottom: calc(40px + var(--safe-bottom)); transform: translateX(-50%);
  width: min(calc(100% - 32px), 340px); padding: 18px 18px 16px; animation: none;
}
.title-panel .title-copy { margin: 0 0 12px; color: var(--ink); font-size: 14px; font-weight: 600; word-break: keep-all; overflow-wrap: break-word; }
.keys { display: flex; justify-content: center; gap: 14px; margin: 0 0 14px; }
.key-group { display: inline-flex; gap: 3px; align-items: center; }
.keys kbd {
  display: inline-grid; place-items: center; min-width: 24px; height: 24px; padding: 0 5px; border-radius: 6px;
  background: #f1ece0; border: 1px solid #ddd4c2; box-shadow: 0 2px 0 #d6ccb8;
  color: var(--ink); font: inherit; font-size: 11px; font-weight: 800;
}
.keys kbd.wide { min-width: 60px; }
.keys svg { width: 26px; height: 26px; fill: none; stroke: var(--ink); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.keys svg .fill { fill: var(--ink); }
.keys svg .arrow { stroke: var(--orange-deep); }
.keys img { width: 30px; height: 30px; object-fit: contain; border-radius: 50%; }
.keys.coarse { display: none; }
@media (pointer: coarse) { .keys.fine { display: none; } .keys.coarse { display: flex; } }
.best { margin: 10px 0 0 !important; color: #8a7a5c !important; font-size: 12px !important; }
.best[hidden] { display: none; }

/* 결과 */
.result-score { margin: 0 0 14px; }
.result-score span { display: block; color: #7b8295; font-size: 12px; font-weight: 600; }
.result-score strong { color: var(--orange-deep); font-size: 44px; font-weight: 900; letter-spacing: -.04em; line-height: 1.1; font-variant-numeric: tabular-nums; }
.result-score em {
  display: inline-block; margin-left: 6px; padding: 3px 8px; border-radius: 999px; vertical-align: 12px;
  background: var(--orange); color: #fff; font-size: 11px; font-style: normal; font-weight: 800;
}
.result-score em[hidden] { display: none; }
.result-stats { display: flex; margin: 0 0 18px; padding: 10px 0; border-radius: 14px; background: #f3efe4; }
.result-stats div { flex: 1; min-width: 0; }
.result-stats div + div { border-left: 1px solid rgba(29,42,68,.1); }
.result-stats dt { color: #7b8295; font-size: 11px; font-weight: 600; }
.result-stats dd { margin: 2px 0 0; padding: 0 4px; color: var(--navy); font-size: 15px; font-weight: 800; font-variant-numeric: tabular-nums; word-break: keep-all; }

@media (max-height: 640px) {
  .title-top { top: calc(44px + var(--safe-top)); }
  .title-name { font-size: 32px; }
  .title-panel { bottom: calc(14px + var(--safe-bottom)); padding: 12px 16px 12px; }
  .title-panel .title-copy { margin-bottom: 8px; }
  .keys { margin-bottom: 10px; }
  .chop-btn { bottom: calc(18px + var(--safe-bottom)); }
}
@media (max-height: 640px) and (min-width: 560px) {
  .title-top { top: calc(14px + var(--safe-top)); }
  .title-panel { left: auto; right: calc(16px + var(--safe-right)); transform: none; width: 300px; }
}
@media (max-height: 400px) {
  .screen.center { padding: 12px; }
  .result-panel {
    display: grid; grid-template-columns: 1fr 1.6fr; gap: 8px 12px; align-items: center;
    width: min(100%, 480px); padding: 14px 18px;
  }
  .result-panel > .panel-title, .result-panel > p, .result-panel > .main-btn { grid-column: 1 / -1; }
  .result-panel > .panel-title { font-size: 21px; line-height: 1.2; }
  .result-panel > p { margin: 0; font-size: 13px; line-height: 1.35; }
  .result-panel .result-score, .result-panel .result-stats { margin: 0; }
  .result-panel .result-score strong { font-size: 34px; }
  .result-panel .result-stats { padding: 8px 0; }
  .result-panel .result-stats dd { font-size: 14px; }
  .result-panel .main-btn { padding: 11px 18px; }
}
@media (max-width: 360px) {
  .hud-cell { padding: 0 7px; }
  .hud-cell strong { font-size: 17px; }
}
