:root { color-scheme: dark; }
* { box-sizing: border-box; }
body {
  margin: 0; background: #12131c; color: #e8e8f0;
  font-family: system-ui, -apple-system, 'Apple SD Gothic Neo', 'Malgun Gothic', sans-serif;
}
[hidden] { display: none !important; }
button {
  background: #2d3048; color: #fff; border: 1px solid #454a6e; border-radius: 8px;
  padding: 7px 12px; cursor: pointer; font: inherit;
}
#battle button:hover:not(:disabled) { background: #3b3f5e; } /* 로비 버튼은 각자 hover 가 있다 */
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.big { padding: 12px 20px; font-size: 16px; font-weight: bold; }
button.gold { background: linear-gradient(#e0a400, #a86f00); border-color: #ffcf4d; }
button.hard { background: #6b1f2a; border-color: #b3374a; }
button.danger { background: #3a1820; border-color: #7a2a3a; margin-top: 24px; }
h2, h3, h4 { margin: 12px 0 8px; }
.hint { color: #9a9cb8; font-size: 13px; }


/* 전투 (3D, 전체 화면) */
#battle { position: fixed; inset: 0; overflow: hidden; background: #000; }
#view { position: absolute; inset: 0; touch-action: none; }
#view canvas { display: block; width: 100%; height: 100%; cursor: grab; }
#view canvas:active { cursor: grabbing; }
.dmg-layer { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.dmg {
  position: absolute; left: 0; top: 0; font-weight: 900; font-size: 15px; white-space: nowrap;
  text-shadow: 0 0 3px #000, 0 2px 0 #000; will-change: transform;
}
.dmg.big { font-size: 24px; text-shadow: 0 0 6px #ff6a00, 0 2px 0 #000; }
/* 전투 HUD — 어떤 화면 크기에서도 한 줄 유지 (줄바꿈 없이 크기만 줄어든다) */
#hud {
  position: absolute; top: 0; left: 0; right: 0; display: flex; justify-content: space-between; align-items: flex-start; gap: 6px;
  padding: calc(6px + env(safe-area-inset-top, 0px)) calc(8px + env(safe-area-inset-right, 0px)) 6px calc(8px + env(safe-area-inset-left, 0px));
  pointer-events: none; background: linear-gradient(rgba(0,0,0,0.5), transparent);
}
#hud > * { pointer-events: auto; }
.hud-info { display: flex; flex-direction: column; gap: 4px; align-items: flex-start; min-width: 0; font-weight: 800; font-size: 15px; }
.pill { background: rgba(10,10,24,0.72); border: 1px solid rgba(255,255,255,0.18); border-radius: 10px; padding: 5px 12px; white-space: nowrap; text-shadow: 0 1px 2px #000; min-width: 96px; }
.pill.money { color: #9dffb0; }
.pill.stage { max-width: 40cqw; overflow: hidden; text-overflow: ellipsis; font-size: 12px; padding: 3px 10px; color: #cfc8ff; }
.hud-btns { position: relative; display: flex; gap: 4px; flex-wrap: nowrap; flex: none; }
.hud-btns > button { background: rgba(10,10,24,0.72); border-color: rgba(255,255,255,0.22); min-width: 38px; height: 34px; padding: 0 8px; font-size: 14px; font-weight: 700; white-space: nowrap; }
#hudMenu { position: absolute; top: 40px; right: 0; display: flex; flex-direction: column; gap: 4px; padding: 6px; z-index: 5;
  background: rgba(16,14,40,0.96); border: 1px solid #5b54a8; border-radius: 12px; box-shadow: 0 8px 20px rgba(0,0,0,0.5); }
#hudMenu[hidden] { display: none; }
#hudMenu button { text-align: left; white-space: nowrap; background: rgba(255,255,255,0.06); border-color: transparent; padding: 8px 12px; }
#hudMenu button.quit { color: #ff9aa8; }
#hudMenu kbd { font-size: 10px; color: #999; margin-left: 6px; font-family: inherit; }
#waveCtl { position: absolute; right: calc(10px + env(safe-area-inset-right, 0px)); bottom: calc(var(--hotbar-h, 96px) + 14px + env(safe-area-inset-bottom, 0px));
  display: flex; flex-direction: column; align-items: flex-end; gap: 6px; z-index: 3; }
#autoBtn { font-weight: 800; font-size: 13px; padding: 6px 12px; border-radius: 999px; background: rgba(10,10,24,0.8); border: 2px solid rgba(255,255,255,0.25); color: #aaa; }
#autoBtn.on { color: #3a2200; background: linear-gradient(#9dffb0, #2fc060); border-color: #d8ffe0; box-shadow: 0 0 12px rgba(80,255,140,0.5); }
.wave-btn small { font-size: 11px; }
.wave-btn {
  font-weight: 900; font-size: clamp(13px, 3.2cqw, 17px); padding: 10px 16px; border-radius: 12px; white-space: nowrap; z-index: 3;
  background: linear-gradient(#ffe27a, #f0a820 60%, #d88a10); color: #3a2200; border: 2px solid #fff0a0;
  box-shadow: 0 4px 0 #8a5400, 0 0 16px rgba(255,200,60,0.45);
}
.wave-btn:hover:not(:disabled) { background: linear-gradient(#ffe27a, #f0a820 60%, #d88a10); filter: brightness(1.08); }
.wave-btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #8a5400; }
#toast {
  position: absolute; top: 22%; left: 50%; transform: translateX(-50%); font-size: clamp(18px, 5cqw, 28px); font-weight: 900;
  text-shadow: 0 0 12px #000, 0 2px 0 #000; opacity: 0; pointer-events: none; white-space: nowrap;
}
#toast.danger { color: #ff5a5a; }
#toast.show { animation: toast 1.8s ease-out; }
@keyframes toast { 0% { opacity: 0; transform: translateX(-50%) scale(1.6); } 15% { opacity: 1; transform: translateX(-50%) scale(1); } 75% { opacity: 1; } 100% { opacity: 0; } }
#camHelp {
  position: absolute; top: 56px; right: 10px; max-width: min(340px, calc(100cqw - 20px)); background: rgba(20,21,30,0.92);
  border: 1px solid #454a6e; border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.6; z-index: 4;
}
#bottom {
  position: absolute; left: 0; right: 0; bottom: 0; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 6px calc(8px + env(safe-area-inset-right, 0px)) calc(8px + env(safe-area-inset-bottom, 0px)) calc(8px + env(safe-area-inset-left, 0px));
  pointer-events: none; background: linear-gradient(transparent, rgba(0,0,0,0.5));
}
#bottom > * { pointer-events: auto; }
/* 유닛 바: 6칸이 항상 한 줄 — 칸 너비는 화면 폭에 맞춰 줄어든다 */
#hotbar { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 96px); gap: clamp(3px, 1cqw, 6px); width: 100%; justify-content: center; }
.hot-card {
  position: relative; display: flex; flex-direction: column; align-items: center; min-width: 0; width: 100%;
  border: 2px solid; border-radius: 10px; padding: 4px 2px; background: rgba(20,18,40,0.92); overflow: hidden;
}
.hot-card .hk { position: absolute; top: 1px; right: 5px; font-size: 10px; color: #888; }
.hot-card .abadge { top: 2px; left: 2px; width: 17px; height: 17px; font-size: 9px; }
.hot-card .ic { width: clamp(28px, 6cqw, 38px); aspect-ratio: 1; border-radius: 50%; object-fit: cover; object-position: 50% 18%; background: rgba(255, 255, 255, 0.12); border: 2px solid rgba(0, 0, 0, 0.5); }
.hot-card .nm, .hot-card .shape { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.hot-card .nm { font-size: clamp(9px, 2.4cqw, 12px); font-weight: 700; }
.hot-card .shape { font-size: clamp(8px, 2cqw, 10px); color: #ffb060; }
.hot-card .cost { color: #7dff9a; font-size: clamp(10px, 2.4cqw, 12px); font-weight: 700; }
.hot-card .cnt { font-size: 10px; color: #aaa; }
.hot-card.active { background: #3b3f5e; box-shadow: 0 0 0 2px #fff; }
.hot-card.disabled { opacity: 0.5; }
#selPanel { background: rgba(20,18,40,0.94); border: 1px solid #5b54a8; border-radius: 12px; padding: 7px 10px; max-width: min(560px, 100%); }
.sel-title { font-weight: bold; font-size: clamp(13px, 3.4cqw, 16px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sel-title small { color: #aaa; font-weight: normal; }
.sel-stats { font-size: clamp(11px, 2.8cqw, 13px); margin: 3px 0; }
.sel-btns { display: flex; gap: 5px; flex-wrap: nowrap; }
.sel-btns button { flex: 1 1 0; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 7px 6px; font-size: clamp(11px, 2.8cqw, 14px); }
#selPanel .hint { font-size: 11px; }

/* 배치 확정 버튼 (미리보기 옆에 뜬다) */
#placeConfirm { position: absolute; left: 0; top: 0; z-index: 4; display: flex; gap: 6px; pointer-events: auto; }
#placeConfirm[hidden] { display: none; }
#placeConfirm button { height: 44px; min-width: 44px; border-radius: 12px; font-size: 18px; font-weight: 900; padding: 0 12px; box-shadow: 0 4px 0 rgba(0,0,0,0.45); }
#placeConfirm .pc-ok { background: linear-gradient(#7affa0, #1fb050); border-color: #d0ffe0; color: #053015; }
#placeConfirm .pc-ok small { font-size: 12px; }
#placeConfirm .pc-ok.no { background: linear-gradient(#8a8a9a, #4a4a5a); border-color: #aaa; color: #ddd; }
#placeConfirm .pc-no { background: linear-gradient(#ff8a9a, #c02a44); border-color: #ffd0d8; color: #fff; }
#placeConfirm button:active { transform: translateY(3px); box-shadow: 0 1px 0 rgba(0,0,0,0.45); }
.hot-card { touch-action: none; }


/* 낮은 화면(가로 폰): 유닛 바를 납작하게 — 아이콘·비용만 */
@container app (max-height: 480px) {
  .hud-info { gap: 3px; }
  .pill { padding: 3px 10px; font-size: 13px; }
  .pill.stage { display: none; }
  .hot-card { flex-direction: row; justify-content: center; gap: 4px; padding: 3px 4px; }
  .hot-card .nm, .hot-card .shape, .hot-card .cnt, .hot-card .hk { display: none; }
  .hot-card .ic { width: 28px; }
  #bottom { gap: 4px; }
  .sel-stats { display: none; }
  #selPanel .hint { display: none; }
}

/* 그래픽 설정 */
.quality-list { display: flex; flex-direction: column; gap: 6px; margin-top: 10px; }
.quality { display: flex; gap: 12px; align-items: center; text-align: left; padding: 10px 12px; }
.quality b { font-size: 18px; width: 24px; text-align: center; }
.quality span { font-size: 13px; color: #cfd0e6; }
.quality.on { background: #5a4bd6; border-color: #8a7cff; }

/* 모달 */
#modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.65); display: flex;
  align-items: center; justify-content: center; z-index: 10; padding: 16px;
}
.modal-box {
  background: #1f2133; border: 1px solid #5a4bd6; border-radius: 14px; padding: 20px;
  max-width: 460px; width: 100%; text-align: center;
}
.modal-box .mats { justify-content: center; }
.modal-box .close { margin-top: 14px; }
.confirm-row { justify-content: center; margin-top: 14px; }
.confirm-row .danger { margin-top: 0; }

/* 공격 형태 표시 */
.shape-tag { color: #ffb060; }
