/* 휴대폰 가로 화면·노치(safe-area)·앱(PWA) 전용 배치.
   index.html <style> 뒤에 불러와서 같은 자리를 덮어쓴다.
   (index.html 큰 배치는 화면 세션 몫이라 여기로 따로 뺌. 앞에 html body를 붙여 우선순위를 높임) */

:root {
  /* 노치·둥근 모서리·홈 바를 피하는 여백. 노치 없는 화면에선 원래 값(14px)과 같다 */
  --sl: max(14px, env(safe-area-inset-left));
  --sr: max(14px, env(safe-area-inset-right));
  --st: max(14px, env(safe-area-inset-top));
  --sb: max(16px, env(safe-area-inset-bottom));
}
html, body { overscroll-behavior: none; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none; }
html { background: #0d1410; }

/* ---------- 모든 화면: 노치를 피해 가장자리 위치 ---------- */
html body #hud { left: var(--sl); top: var(--st); }
html body #autobox { left: var(--sl); }
html body #mini { right: var(--sr); top: var(--st); }
html body #quest { right: var(--sr); }
html body #help { left: var(--sl); bottom: var(--sb); }
html body #hotbar { bottom: var(--sb); }
html body #buildbar { right: var(--sr); }
html body #mountbox { left: var(--sl); }
html body.touch #joy { left: calc(var(--sl) + 10px); }
html body.touch #tbtns { right: calc(var(--sr) + 4px); }
html body #overlay { padding: var(--st) var(--sr) var(--sb) var(--sl); }

/* 창(소지품·지도·멈춤)이 열려 있으면 그 위로 뜨는 단추·막대를 숨김 (작은 화면에서 창을 가렸음) */
#overlay:not(.hidden) ~ #touch, #overlay:not(.hidden) ~ #autobox, #overlay:not(.hidden) ~ #fsBtn,
#overlay:not(.hidden) ~ #sndBtn, #overlay:not(.hidden) ~ #mountbox, #overlay:not(.hidden) ~ #buildbar { visibility: hidden; }

/* ---------- 전체 화면 단추 ---------- */
#fsBtn { display: none; position: fixed; top: var(--st); right: calc(var(--sr) + 230px); width: 36px; height: 36px; border-radius: 50%; border: 1px solid rgba(222,186,112,.3); background: linear-gradient(180deg, rgba(34,29,21,.85), rgba(20,17,13,.8)); color: #f7e2ae; cursor: pointer; box-shadow: 0 6px 16px rgba(0,0,0,.35); z-index: 5; padding: 0; place-items: center; }
#fsBtn svg { display: block; margin: auto; }
body.touch #fsBtn { display: grid; }
@media (max-width: 700px) { #fsBtn { right: calc(var(--sr) + 166px); } }

/* ---------- 새 버전 띠 ---------- */
#updBar { position: fixed; left: 50%; bottom: calc(var(--sb) + 84px); transform: translateX(-50%); z-index: 30; display: flex; align-items: center; gap: 10px; padding: 8px 10px 8px 16px; border-radius: 999px; background: linear-gradient(180deg, rgba(40,33,22,.95), rgba(22,18,12,.95)); border: 1px solid rgba(242,199,92,.55); box-shadow: 0 10px 28px rgba(0,0,0,.45), 0 0 20px rgba(242,199,92,.2); font-size: 13px; font-weight: 700; white-space: nowrap; animation: pop .2s ease-out; }
#updBar .mbtn { padding: 7px 12px; font-size: 12.5px; border-radius: 999px; }
#updBar .upd-x { background: none; border: 0; color: rgba(246,238,220,.6); font: inherit; font-size: 12px; cursor: pointer; padding: 6px; }

/* ---------- 세로로 들었을 때 안내 ---------- */
#rotateHint { display: none; position: fixed; inset: 0; z-index: 100; align-items: center; justify-content: center; text-align: center; padding: var(--st) var(--sr) var(--sb) var(--sl); background: radial-gradient(circle at 50% 40%, #3a2440, #140f1c 75%); color: #f6eedc; }
#rotateHint .rh-in { display: flex; flex-direction: column; align-items: center; gap: 10px; max-width: 300px; }
#rotateHint b { font-family: 'Black Han Sans', 'Apple SD Gothic Neo', sans-serif; font-weight: 400; font-size: 30px; letter-spacing: 1px; color: #f7e2ae; margin-top: 6px; }
#rotateHint span { font-size: 14px; color: rgba(246,238,220,.72); line-height: 1.5; }
#rotateHint .mbtn { margin-top: 12px; padding: 12px 18px; font-size: 14px; }
#rotateHint .rg-phone { transform-origin: 100px 78px; transform-box: view-box; animation: rgTurn 2.6s ease-in-out infinite; }
@keyframes rgTurn { 0%, 22% { transform: rotate(0); } 55%, 85% { transform: rotate(90deg); } 100% { transform: rotate(0); } }
@media (orientation: portrait) and (max-width: 900px) {
  body.touch #rotateHint { display: flex; }
}
@media (prefers-reduced-motion: reduce) { #rotateHint .rg-phone { animation: none; transform: rotate(90deg); } }

/* =========================================================
   가로 휴대폰 (높이 500px 이하: 844×390, 932×430 등)
   위 왼쪽: 상태 막대 → 자동 사냥 | 위 가운데: 시대 표시(캔버스)·알림
   위 오른쪽: 미니맵 → 할 일 | 아래 왼쪽: 이동 막대 | 아래 가운데: 단축칸
   아래 오른쪽: 행동 단추(엄지 자리)
   ========================================================= */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --sl: max(10px, env(safe-area-inset-left)); --sr: max(10px, env(safe-area-inset-right)); --st: max(8px, env(safe-area-inset-top)); --sb: max(8px, env(safe-area-inset-bottom)); }

  /* 상태 막대 */
  html body #hud { width: 176px; padding: 6px 9px 5px; border-radius: 11px; }
  html body #hud .bar { grid-template-columns: 50px 1fr 24px; gap: 5px; font-size: 10.5px; margin: 2px 0; }
  html body #hud .bar > span:first-child::before { width: 14px; height: 12px; margin-right: 2px; }
  html body #hud .bar > div { height: 8px; }
  html body #hud #clock { margin-top: 4px; padding-top: 4px; font-size: 10.5px; }
  html body #hud img.ico.clk { width: 13px; height: 13px; }

  html body #autobox { width: 176px; font-size: 11px; } /* 세로 위치는 bagui.js가 상태 막대 바로 아래로 맞춤 */
  html body #autobox.off button { padding: 5px 9px; border-radius: 9px; }
  html body #autobox.on, html body #autobox.menu { padding: 7px 9px; }
  html body #autobox .seg { margin: 3px 0; } html body #autobox .seg span { width: 54px; }

  /* 미니맵·할 일 */
  html body #mini { width: 92px; height: 92px; border-width: 2px; }
  html body #quest { top: calc(var(--st) + 102px); width: 172px; padding: 6px 9px; font-size: 10.5px; border-radius: 10px; }
  html body #quest .qt { font-size: 12px; margin: 2px 0; }
  html body #quest .qs { line-height: 1.35; }
  html body #sndBtn { right: calc(var(--sr) + 100px); top: var(--st); width: 32px; height: 32px; }
  html body #fsBtn { right: calc(var(--sr) + 138px); width: 32px; height: 32px; }

  /* 알림 띠: 캔버스의 시대 표시(위 가운데) 아래 */
  html body #toast { top: calc(var(--st) + 40px); font-size: 12.5px; padding: 7px 16px; max-width: 56vw; }

  /* 단축칸: 이동 막대와 행동 단추 사이. 넘치면 옆으로 밀어 보기 */
  html body #hotbar { bottom: var(--sb); gap: 4px; padding: 4px; border-radius: 13px; max-width: calc(100vw - var(--sl) - var(--sr) - 2 * 196px); overflow-x: auto; scrollbar-width: none; }
  html body #hotbar::-webkit-scrollbar { display: none; }
  html body #hotbar .slot { width: 40px; height: 40px; flex: none; border-radius: 9px; }
  html body #hotbar .slot.sel { transform: translateY(-1px); }
  html body #hotbar .slot .ic img.ico { width: 22px; height: 22px; }
  html body #hotbar .slot .nm { display: none; }
  html body #hotbar .slot .key { left: 2px; top: 2px; min-width: 12px; height: 12px; font-size: 8px; line-height: 12px; padding: 0 2px; }
  html body #hotbar .slot .cnt { right: 3px; top: 1px; font-size: 10px; }
  html body:not(.touch) #hotbar { max-width: calc(100vw - var(--sl) - var(--sr) - 40px); }

  /* 탈것 정보: 아래 가운데, 단축칸 위 */
  html body #mountbox, html body.touch #mountbox { left: 50%; right: auto; bottom: calc(var(--sb) + 52px); transform: translateX(-50%); width: 230px; padding: 6px 9px; font-size: 11px; }
  html body #mountbox .mb { margin: 2px 0; }

  /* 동료(애완동물) 상자: 왼쪽, 이동 막대 바로 위. 작게 줄이고 넘치면 상자 안에서 밀어 보기
     (petui.js가 bottom을 직접 넣으므로 !important) */
  html body #petbox { left: var(--sl); bottom: calc(var(--sb) + 124px) !important; width: 176px; max-height: calc(100dvh - var(--st) - var(--sb) - 274px); overflow-y: auto; padding: 5px 7px; font-size: 10.5px; scrollbar-width: none; }
  html body #petbox .ph { margin-bottom: 2px; } html body #petbox .ph span { font-size: 9.5px; }
  html body #petbox .pr { grid-template-columns: 34px 1fr; gap: 5px; padding: 2px 3px; }
  html body #petbox canvas { width: 34px; height: 22px; }
  html body #petbox .ph2 { display: none; }
  html body #petbox .pb { height: 4px; margin: 2px 0; }
  html body #petbox .po { gap: 2px; margin-top: 3px; }
  html body #petbox .po button { min-height: 22px; padding: 0; font-size: 9px; letter-spacing: -.3px; white-space: nowrap; overflow: hidden; }
  html body #petpop { max-height: calc(100dvh - 16px); overflow-y: auto; }

  /* 인벤토리 창(#p-inv): 화면을 꽉 채우고, 상태 탭의 인형 칸을 낮게 */
  html body #p-inv { width: calc(100vw - var(--sl) - var(--sr) - 8px); height: calc(100dvh - var(--st) - var(--sb) - 6px); padding: 8px 12px 6px; }
  html body #p-inv .inv-head { gap: 10px; padding-bottom: 6px; }
  html body #p-inv .inv-head h2 { font-size: 18px; }
  html body #p-inv .tab-state { gap: 12px; }
  html body #p-inv .doll { height: 260px; }
  html body #p-inv .dollcv { width: 188px; height: 220px; }
  html body #p-inv .eq { width: 58px; height: 58px; }
  html body #p-inv .eq-head, html body #p-inv .eq-acc, html body #p-inv .eq-trinket { top: 10px; }
  html body #p-inv .eq-feet, html body #p-inv .eq-back { bottom: 10px; }
  html body #p-inv .eq-body, html body #p-inv .eq-hand { top: calc(50% - 29px); }

  /* 인벤토리 '부족' 탭: 얼굴 그림·여백을 낮게 */
  html body #p-inv canvas.npcp { height: 90px; }
  html body #p-inv .tb-hunt { margin-top: 0; }
  html body #p-inv .tb-mid { margin-top: 8px; }
  html body #p-inv .tb-name { padding: 8px 10px; gap: 8px; }
  html body #p-inv .tb-name b { font-size: 18px; }
  html body #p-inv .tb-fire { width: 44px; height: 44px; } html body #p-inv .tb-fire img.ico { width: 36px; height: 36px; }
  html body #p-inv .tb-stats div { padding: 5px 8px; }

  /* 짓기 막대: 오른쪽, 행동 단추 왼쪽 위 */
  html body #buildbar, html body.touch #buildbar { right: calc(var(--sr) + 186px); top: calc(var(--st) + 42px); bottom: auto; width: 122px; max-height: calc(100dvh - var(--st) - var(--sb) - 100px); overflow-y: auto; padding: 5px; gap: 3px; }
  html body #buildbar .bb { padding: 3px 6px; font-size: 10.5px; }
  html body #buildbar .bb img.ico { width: 16px; height: 16px; }

  /* 이동 막대 */
  html body.touch #joy { bottom: calc(var(--sb) + 4px); width: 116px; height: 116px; }
  html body.touch #knob { left: 35px; top: 35px; width: 46px; height: 46px; }

  /* 행동 단추: 오른손 엄지가 닿는 부채꼴. 행동(큰 단추)이 맨 오른쪽 아래 */
  html body.touch #tbtns { bottom: var(--sb); grid-template-columns: 48px 48px 66px; grid-template-rows: 48px 48px 48px; gap: 6px;
    grid-template-areas: "craft map ." "run eat act" ". use act"; }
  html body.touch #tbtns button { height: auto; font-size: 11.5px; }
  html body.touch #tbtns button[data-a=craft] { grid-area: craft; }
  html body.touch #tbtns button[data-a=map] { grid-area: map; }
  html body.touch #tbtns button[data-a=run] { grid-area: run; }
  html body.touch #tbtns button[data-a=eat] { grid-area: eat; }
  html body.touch #tbtns button[data-a=use] { grid-area: use; }
  html body.touch #tbtns button[data-a=act] { grid-area: act; font-size: 14px; border-radius: 34px; }

  /* 창(소지품·지도·멈춤): 화면 높이에 맞게 */
  html body .panel { max-height: calc(100dvh - var(--st) - var(--sb) - 6px); padding: 10px 14px; border-radius: 14px; }
  html body .phead { margin: -2px 0 8px; padding-bottom: 7px; }
  html body .phead h2, html body #p-menu h2 { font-size: 19px; }
  html body #p-craft { width: min(900px, calc(100vw - var(--sl) - var(--sr) - 8px)); }
  html body #p-menu { width: min(420px, calc(100vw - var(--sl) - var(--sr) - 8px)); gap: 6px; }
  html body #p-menu .mbtn { padding: 9px; font-size: 14px; }
  html body #bagbox { max-height: calc(100dvh - var(--st) - var(--sb) - 6px); }
  html body .rc { min-height: 52px; padding: 8px 12px 8px 54px; }
  html body .rh .ic { width: 34px; height: 34px; left: 10px; top: 9px; }

  /* 시작 화면 */
  html body .tbox { width: min(400px, 70vw); padding: 14px 22px 12px; }
  html body .tbox h1 { font-size: 50px; letter-spacing: 6px; padding-left: 6px; }
  html body .tbox .tag { margin-top: 4px; font-size: 10px; }
  html body .tbox .sub { margin: 6px 0 10px; font-size: 12px; line-height: 1.5; }
  html body .tbox input { padding: 9px; margin-bottom: 7px; font-size: 14px; }
  html body .tbox .mbtn { padding: 10px; margin-bottom: 6px; font-size: 15px; }
  html body .keys { display: none; }
  html body #updBar { bottom: calc(var(--sb) + 52px); }
}

/* 아주 낮은 가로 화면(높이 360 이하) */
@media (orientation: landscape) and (max-height: 380px) {
  /* 동료 전체 명령 줄은 숨김(동료를 누르면 뜨는 창에 같은 명령이 있음) */
  html body #petbox .po { display: none; }
}
@media (orientation: landscape) and (max-height: 360px) {
  html body #quest .qs { display: none; }
  html body #hud #clock { display: none; }
}
