:root {
  --s: 1.5;
  --wood: #2b1a0e;
  --wood2: #4a2c14;
  --paper: #f4e4bc;
  --paper2: #e6cf98;
  --ink: #3a2410;
  --gold: #ffd34d;
  --accent: #f5a524;
  --red: #d9483b;
  --green: #4fae4a;
}
* { box-sizing: border-box; -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; background: #120b06; color: var(--ink); touch-action: none;
  font-family: -apple-system, "Apple SD Gothic Neo", "Noto Sans KR", "Malgun Gothic", sans-serif; }
/* 아이폰: 주소창·툴바를 뺀 실제 보이는 영역(visualViewport)에 맞춤 → main.js fitApp() */
#app { position: fixed; left: 0; top: var(--app-top, 0px); width: var(--app-w, 100%); height: var(--app-h, 100%); overflow: hidden; }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#safeProbe { position: absolute; visibility: hidden; pointer-events: none; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.hidden { display: none !important; }
.spr { background-repeat: no-repeat; flex: none; }

/* ---------- 공통 버튼 ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 4px; padding: 8px 16px; border-radius: 10px;
  border: 2px solid #6b3b0a; background: linear-gradient(#ffd96a, #e8961c); color: #3a1f00; font-weight: 800; font-size: 15px;
  box-shadow: 0 3px 0 #6b3b0a; cursor: pointer; white-space: nowrap; font-family: inherit; }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #6b3b0a; }
.btn.gray { background: linear-gradient(#ddd, #aaa); border-color: #555; box-shadow: 0 3px 0 #555; color: #333; }
.btn.green { background: linear-gradient(#8de07a, #3e9e33); border-color: #1f5a17; box-shadow: 0 3px 0 #1f5a17; color: #0f2a0b; }
.btn.red { background: linear-gradient(#ff8f80, #c93a2c); border-color: #6d1a12; box-shadow: 0 3px 0 #6d1a12; color: #2a0703; }
.btn.small { padding: 4px 10px; font-size: 12px; border-radius: 8px; }
.btn[disabled] { filter: grayscale(1) brightness(.85); pointer-events: none; }
.gold { color: #b07800; font-weight: 800; }
.gold-tag { display: inline-flex; align-items: center; gap: 4px; background: rgba(0,0,0,.45); color: var(--gold); padding: 4px 10px; border-radius: 999px; font-weight: 800; }

/* ---------- 화면 ---------- */
.screen { position: absolute; inset: 0; display: flex; flex-direction: column; background: radial-gradient(circle at 50% 40%, #5a3a1c, #1d1007 75%); color: var(--paper); }
.topbar { display: flex; align-items: center; gap: 10px; padding: 8px max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left)); background: rgba(0,0,0,.35); border-bottom: 2px solid #000; flex: none; }
.topbar h2 { margin: 0; font-size: 18px; color: var(--gold); text-shadow: 0 2px 0 #000; }
.topbar .grow { flex: 1; }

/* 로딩 */
#scr-loading { align-items: center; justify-content: center; gap: 14px; }
#scr-loading .bar { width: min(360px, 70vw); height: 12px; background: #000; border-radius: 6px; overflow: hidden; border: 1px solid #a77; }
#scr-loading .bar div { height: 100%; width: 0; background: var(--gold); }

/* 타이틀 */
#scr-title { align-items: center; justify-content: center; overflow: hidden; }
#scr-title .art { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%) scale(var(--title-scale, 2)); transform-origin: center; filter: brightness(.75); }
#scr-title .logo { position: relative; font-size: clamp(36px, 9vh, 72px); font-weight: 900; color: var(--gold); letter-spacing: 2px;
  text-shadow: 0 4px 0 #6a3b00, 0 0 20px rgba(0,0,0,.8); margin-bottom: 4px; }
#scr-title .sub { position: relative; font-size: 14px; color: #fff; text-shadow: 0 2px 4px #000; margin-bottom: 22px; }
#scr-title .menu { position: relative; display: flex; flex-direction: column; gap: 12px; min-width: 220px; }
#scr-title .install-hint { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); width: max-content; max-width: 92vw; background: rgba(0,0,0,.7); color: #fff; font-size: 13px; padding: 8px 14px; border-radius: 10px; border: 1px solid var(--gold); text-align: center; }
#scr-title .player-chip { position: relative; display: flex; align-items: center; gap: 8px; margin-bottom: 12px; background: rgba(0,0,0,.55); color: #fff; padding: 5px 6px 5px 12px; border-radius: 999px; font-size: 14px; }
.profile input { display: block; width: 100%; margin: 6px 0; padding: 9px 10px; border-radius: 8px; border: 2px solid #8a5a2b; background: #fffaf0; color: var(--ink);
  font-size: 16px; font-family: inherit; -webkit-user-select: text; user-select: text; touch-action: manipulation; }
.profile .plist { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; margin: 4px 0 10px; max-height: 120px; overflow: auto; touch-action: pan-y; }
.profile .pitem { display: flex; flex-direction: column; align-items: center; padding: 6px 12px; border-radius: 10px; border: 2px solid #8a5a2b; background: #ecd8a8; color: var(--ink); font-family: inherit; font-size: 14px; cursor: pointer; }
.profile .pitem small { font-size: 11px; color: #6b4423; }
.profile .pitem.on { background: #ffd96a; border-color: #6b3b0a; }
.profile .ptitle { font-weight: 800; text-align: left; margin-top: 4px; }
.profile .pmsg { min-height: 18px; color: var(--red); font-size: 13px; text-align: left; }
.profile .pnote { font-size: 12px; color: #6b4423; margin: 4px 0 0; }
#scr-title .note { position: absolute; bottom: 8px; left: 0; right: 0; text-align: center; font-size: 11px; color: rgba(255,255,255,.55); }

/* 지도 */
#scr-map .chapter-nav { display: flex; align-items: center; gap: 8px; }
#scr-map .chapter-nav b { min-width: 90px; text-align: center; font-size: 17px; color: var(--gold); }
#scr-map .board { flex: 1; overflow: auto; padding: 12px max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); display: flex; align-items: center; justify-content: center; touch-action: pan-y; }
#scr-map .grid { display: grid; grid-template-columns: repeat(8, minmax(64px, 1fr)); gap: 8px; width: min(900px, 100%);
  background: var(--paper); border: 4px solid #6b4423; border-radius: 14px; padding: 12px; box-shadow: 0 8px 30px rgba(0,0,0,.6); }
.tile { position: relative; aspect-ratio: 1 / 0.9; border-radius: 10px; border: 2px solid #8a5a2b; background: linear-gradient(#fff6dc, #ecd29a);
  display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--ink); cursor: pointer; font-weight: 800; }
.tile .no { font-size: 17px; }
.tile .type { font-size: 10px; padding: 1px 5px; border-radius: 6px; color: #fff; margin-top: 2px; }
.tile .type.B { background: #8b2bd9; } .tile .type.W { background: #2b7bd9; } .tile .type.D { background: #6b4423; } .tile .type.T { background: #2a9d6a; }
.tile .stars { font-size: 11px; color: #e09a00; letter-spacing: -1px; margin-top: 2px; }
.tile.locked { filter: grayscale(1) brightness(.7); cursor: default; }
.tile.next { border-color: var(--red); box-shadow: 0 0 0 3px rgba(217,72,59,.35); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(217,72,59,.15); } }

/* 상점 */
#scr-shop .tabs { display: flex; gap: 6px; }
.tab { padding: 6px 14px; border-radius: 8px 8px 0 0; background: #6b4423; color: #f4e4bc; font-weight: 800; cursor: pointer; border: 2px solid #000; border-bottom: none; }
.tab.on { background: var(--paper); color: var(--ink); }
#scr-shop .list { flex: 1; overflow: auto; padding: 12px max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 10px; align-content: start; touch-action: pan-y; }
.card { display: flex; gap: 10px; background: var(--paper); color: var(--ink); border: 3px solid #6b4423; border-radius: 12px; padding: 10px; align-items: center; }
.card.locked { opacity: .6; }
.card.equipped { border-color: var(--green); box-shadow: 0 0 0 2px rgba(79,174,74,.4); }
.card .icon { width: 64px; height: 64px; display: flex; align-items: center; justify-content: center; background: #e6cf98; border-radius: 10px; flex: none; overflow: hidden; }
.card .body { flex: 1; min-width: 0; }
.card .name { font-weight: 900; font-size: 15px; }
.card .lv { font-size: 12px; color: #8a5a2b; margin-left: 4px; }
.card .desc { font-size: 12px; line-height: 1.45; margin: 3px 0 6px; color: #5a3a1c; }
.card .acts { display: flex; gap: 6px; flex-wrap: wrap; }
.hero-panel { grid-column: 1 / -1; display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 8px; background: var(--paper); color: var(--ink); border: 3px solid #6b4423; border-radius: 12px; padding: 12px; }
.hero-panel .stat { background: #ecd8a8; border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.hero-panel .stat b { display: block; font-size: 17px; }
.expbar { height: 8px; border-radius: 4px; background: #00000030; overflow: hidden; margin-top: 4px; }
.expbar div { height: 100%; background: var(--accent); }

/* 모달 */
#modal { position: absolute; inset: 0; background: rgba(0,0,0,.6); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 12px; }
#modal .box { background: var(--paper); color: var(--ink); border: 4px solid #6b4423; border-radius: 16px; padding: 16px 20px; min-width: min(360px, 92%); max-width: 520px; max-height: 94%; overflow: auto; touch-action: pan-y;
  box-shadow: 0 10px 40px rgba(0,0,0,.6); text-align: center; }
#modal h3 { margin: 0 0 8px; font-size: 22px; }
#modal .row { display: flex; justify-content: space-between; gap: 12px; padding: 4px 2px; border-bottom: 1px dashed #c9a86a; font-size: 14px; text-align: left; }
#modal .acts { display: flex; gap: 8px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
#modal .stars { display: flex; justify-content: center; gap: 6px; margin: 8px 0; min-height: 50px; }
#modal .stars .spr.off { filter: grayscale(1) brightness(.5); opacity: .5; }
.toggle { display: flex; justify-content: space-between; align-items: center; padding: 6px 2px; font-size: 15px; }

/* ---------- 전투 HUD ---------- */
#hud { position: absolute; inset: 0; pointer-events: none; }
#hudStage { position: absolute; left: 0; top: 0; transform-origin: 0 0; pointer-events: none; }
#unitBtns, #maceBtns { position: absolute; inset: 0; pointer-events: none; }
#hudStage .sbtn, #hudStage .ubtn, #hudStage .mbtn { position: absolute; pointer-events: auto; cursor: pointer; touch-action: none; }
#hudStage .ubtn.locked { pointer-events: none; }
#adminTap { position: absolute; pointer-events: auto; }
.admin h4 { margin: 10px 0 4px; text-align: left; font-size: 14px; color: #6b3b0a; border-bottom: 2px solid #c9a86a; }
.admin .grid { display: flex; flex-wrap: wrap; gap: 6px; }
.admin .grid input { width: 90px; padding: 5px 6px; font-size: 16px; border-radius: 6px; border: 2px solid #8a5a2b; -webkit-user-select: text; user-select: text; }
.admin .who { font-size: 12px; color: #8a5a2b; }
.admin .grid.rows { gap: 4px 8px; }
.admin .erow { display: flex; align-items: center; justify-content: space-between; gap: 6px; flex: 1 1 calc(50% - 8px); min-width: 180px; font-size: 13px; background: rgba(107,59,10,.08); padding: 3px 4px 3px 8px; border-radius: 6px; }
.admin .erow input { width: 78px; padding: 3px 5px; font-size: 16px; border-radius: 6px; border: 2px solid #8a5a2b; -webkit-user-select: text; user-select: text; }
.lvtap { padding: 4px 2px; cursor: default; }
.ubtn .cd, .mbtn .cd { position: absolute; left: 3px; right: 3px; bottom: 3px; height: 0; background: rgba(0,0,0,.6); border-radius: 4px; pointer-events: none; }
.ubtn .key { position: absolute; top: 2px; left: 4px; font-size: 7px; color: #fff; opacity: .8; text-shadow: 0 1px 2px #000; pointer-events: none; }
.mbtn .lbl { position: absolute; font-size: 9px; font-weight: 900; color: #fff; text-shadow: 0 1px 2px #000, 0 0 2px #000; pointer-events: none; }
.mbtn .lbl.cost { left: 2px; bottom: 0; color: #9fdcff; }
.mbtn .lbl.grade { right: 0; top: 0; color: #ffe14d; }
.mbtn .ring { position: absolute; left: 3px; top: 3px; width: 46px; height: 46px; border-radius: 50%; background: radial-gradient(#ffeec2, #c98a2f); border: 2px solid #5a2f08; display: flex; align-items: center; justify-content: center; box-sizing: border-box; }
.mbtn .ring.plain { background: none; border: none; }
.mbtn.off .ring, .mbtn.off .spr { filter: grayscale(1) brightness(.7); }
.mbtn .cd { left: 3px; top: 3px; right: auto; bottom: auto; width: 46px; height: 46px; border-radius: 50%; background: transparent; }
#rotate { position: absolute; inset: 0; z-index: 99; background: #120b06; color: var(--gold); display: none; align-items: center; justify-content: center; text-align: center; font-size: 20px; padding: 20px; }
@media (orientation: portrait) and (max-width: 700px) { #rotate { display: flex; } }

/* ---------- 낮은 가로 화면 (아이폰 가로 등) ---------- */
@media (max-height: 460px) {
  .topbar { padding-top: 5px; padding-bottom: 5px; gap: 6px; flex-wrap: nowrap; overflow-x: auto; }
  .topbar h2 { font-size: 15px; }
  .btn { padding: 6px 12px; font-size: 14px; }
  .btn.small { padding: 3px 8px; font-size: 12px; }
  .gold-tag { padding: 3px 8px; font-size: 12px; }
  .tab { padding: 4px 10px; font-size: 13px; }
  #scr-map .board { align-items: flex-start; padding-top: 6px; }
  #scr-map .grid { gap: 5px; padding: 8px; border-width: 3px; }
  .tile { aspect-ratio: auto; height: calc((var(--app-h, 100vh) - 74px) / 3 - 6px); min-height: 44px; border-radius: 8px; }
  .tile .no { font-size: 14px; }
  .tile .type { font-size: 9px; margin-top: 1px; }
  .tile .stars { font-size: 10px; margin-top: 0; }
  #scr-shop .list { grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 8px; padding-top: 8px; }
  .card { padding: 7px; gap: 8px; }
  .card .icon { width: 52px; height: 52px; }
  .card .desc { font-size: 11px; margin: 2px 0 4px; }
  #modal .box { padding: 10px 14px; }
  #modal h3 { font-size: 18px; margin-bottom: 4px; }
  #modal .row { font-size: 13px; padding: 2px; }
  #modal .acts { margin-top: 8px; gap: 6px; }
  #modal .stars { min-height: 0; margin: 4px 0; }
  #scr-title .logo { font-size: 40px; }
  #scr-title .sub { margin-bottom: 12px; }
  #scr-title .menu { gap: 8px; }
  #scr-title .player-chip { margin-bottom: 8px; font-size: 13px; }
  .profile input { padding: 6px 8px; margin: 4px 0; }
  .profile .plist { max-height: 70px; margin-bottom: 4px; }
}

/* 휴대폰 가로 화면(터치): 맨 위 메뉴·상점 버튼이 작고 화면 끝에 붙어 잘 안 눌리던 문제 → 버튼을 키우고 위쪽 여백을 둠 */
@media (pointer: coarse) and (max-height: 500px) {
  .topbar { padding-top: 22px; padding-bottom: 5px; gap: 8px; } /* 맨 위 약 20pt는 iOS 알림 센터 영역이라 터치가 먹히지 않음 */
  .topbar .btn.small { min-height: 34px; min-width: 40px; padding: 5px 12px; font-size: 13px; }
  /* 상단 바가 약 26px 커진 만큼 지도 칸 높이 계산도 맞춤 (원래 74px 기준이라 아래 줄이 잘렸음) */
  .tile { height: calc((var(--app-h, 100vh) - 100px) / 3 - 6px); }
  .topbar .tabs { align-self: stretch; display: flex; align-items: flex-end; gap: 6px; }
  .topbar .tab { min-height: 32px; display: inline-flex; align-items: center; padding: 6px 14px; font-size: 14px; }
  #hudStage .sbtn::after { content: ''; position: absolute; inset: -6px -8px -10px; }
}
