/* ───────── 휴대폰 화면 ─────────
   PC 화면(style.css)은 그대로 두고, 세로 휴대폰(너비 640px 이하)과 가로로 눕힌 휴대폰(높이 500px 이하)만 따로 맞춘다.
   세로: 상단 바 2줄 → 사무실 → 진행 카드 → 순위판 → 하단 탭. 탭 창은 전체 화면 시트, 팝업은 아래에서 올라오는 시트. */

/* 어디서든: 당겨서 새로고침·바운스로 게임이 다시 로드되지 않게 */
html, body { overscroll-behavior: none; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }   /* 아이폰을 눕혀도 글자가 멋대로 커지지 않게 */
.win-body, .bd-list, .tbl-wrap, .panel-tabs { overscroll-behavior: contain; }
.d-short, .vt-short, .tb-br { display: none; }
.street-hint { position: absolute; left: 50%; top: 44%; transform: translateX(-50%); background: rgba(43,29,20,.85); color: #fff; font-size: 12px; padding: 4px 10px; border-radius: 999px; pointer-events: none; white-space: nowrap; animation: fadeIn .3s, fadeOut .5s 2.1s forwards; z-index: 2; }
.m-tip { position: fixed; z-index: 400; max-width: min(320px, calc(100vw - 16px)); background: #2b1d14; color: #fff; font-size: 13px; line-height: 1.5; padding: 7px 10px; border-radius: 8px; box-shadow: 0 4px 0 rgba(0,0,0,.35); white-space: pre-line; pointer-events: none; animation: fadeIn .12s; }
.invite-link { font-family: var(--font-mono); word-break: break-all; background: var(--paper); border: 2px dashed var(--ink); border-radius: 6px; padding: 6px 8px; user-select: all; -webkit-user-select: all; }
.tbl-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
body.touch #screen-game { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
body.touch #screen-game input, body.touch #screen-game textarea, body.touch .invite-link { -webkit-user-select: text; user-select: text; }

@media (max-width: 640px) {
  body { font-size: 13px; }

  /* ── 상단 바 (2줄) ── */
  #topbar {
    flex-wrap: wrap; overflow: visible; gap: 4px;
    padding: calc(4px + env(safe-area-inset-top, 0px)) 6px 5px;
    border-bottom-width: 3px;
  }
  #topbar .tb-item { font-size: 12px; min-height: 30px; padding: 2px 6px; gap: 4px; }
  #tb-status { order: 0; }
  #topbar .tb-company { order: 1; padding: 2px; flex: none; }
  #topbar .tb-company .cname { display: none; }
  #tb-money-item { order: 2; }
  #tb-fans-item { order: 3; }
  #tb-rank { order: 4; }
  #topbar .tb-spacer { order: 5; }
  #tb-inbox { order: 6; }
  #tb-board { display: none; }
  .tb-br { display: block; order: 8; flex-basis: 100%; height: 0; }
  #tb-date { order: 9; flex: 1 1 0; min-width: 0; }
  #tb-date .d-long { display: none; }
  #tb-date .d-short { display: inline; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #tb-date .week-bar { flex: 1 1 auto; min-width: 42px; width: auto; height: 8px; }
  #topbar .speed-btns { order: 10; gap: 3px; }
  #tb-menu { order: 11; }   /* 메뉴는 둘째 줄 끝(돈 숫자가 길어도 첫 줄이 넘치지 않게) */
  #topbar .speed-btns .btn { min-height: 32px; padding: 2px 8px; font-size: 13px; }
  #topbar .speed-btns .btn:first-child { min-width: 70px; }
  #tb-vote { order: 12; flex-basis: 100%; justify-content: center; }
  #tb-vote.lock { display: none !important; }   /* 휴대폰: '시간은 방장' 표시는 줄을 차지하니 숨기고, 버튼을 누르면 안내 */
  #topbar > .btn { min-height: 32px; min-width: 36px; padding: 2px 8px; font-size: 14px; }
  .tb-logo { width: 28px; height: 28px; font-size: 16px; }

  /* ── 가운데: 사무실 → 진행 카드 → 순위판 ── */
  #stage { display: flex; flex-direction: column; }
  #office-wrap { flex: none; height: clamp(220px, calc(100vw * var(--scene-aspect, .9)), 48%); border-bottom: 3px solid var(--ink); }   /* 장면은 카메라가 꽉 채워 확대한다(끌어서 둘러보기) */
  #view-toggle { top: 6px; bottom: auto; right: 6px; gap: 3px; }   /* 위쪽(벽·하늘)에 둬야 바닥의 직원·회사 이름표를 가리지 않는다 */
  #view-toggle .btn { min-height: 30px; padding: 2px 8px; font-size: 12px; }
  #view-toggle .tb-item { font-size: 11px; }
  .vt-long { display: none; }
  .vt-short { display: inline; }

  #hud-projects { display: none; }   /* 휴대폰: 진행 카드는 아래 '진행·판매' 카드로 합쳤다 */
  .hud-card { padding: 6px 8px; }
  .hud-card .t { font-size: 14px; }
  .hud-body .game-preview { width: 84px; height: 56px; }
  .hud-card .stats { grid-template-columns: repeat(5, auto); justify-content: space-between; gap: 2px 6px; font-size: 13px; }

  #feed {
    position: relative; inset: auto; transform: none; width: auto; z-index: auto; box-shadow: none; transition: none;
    flex: 1 1 0; min-height: 0; margin: 6px 6px 6px; border: 3px solid var(--ink); border-radius: 8px; overflow: hidden;
  }
  .feed-tabs button { min-height: 36px; font-size: 13px; }
  .bd-head { padding: 5px 10px 2px; }
  .bd-row { padding: 6px 7px; font-size: 13px; grid-template-columns: 20px 10px minmax(0, 1fr) auto 34px; }
  .bd-name small { font-size: 11px; }
  .bd-val { font-size: 12px; }
  .bd-more { min-height: 34px; }

  /* ── 하단 탭 ── */
  #nav { gap: 4px; padding: 5px 5px calc(5px + var(--safe-b)); overflow: hidden; }
  .nav-btn { flex: 1 1 0; min-width: 0; padding: 4px 1px 3px; font-size: 11px; border-width: 2px; }
  .nav-btn .ico { font-size: 20px; line-height: 24px; }
  .nav-btn.on { transform: translateY(-1px); }
  .nav-btn .dot { top: -6px; right: -2px; }

  /* ── 탭 창: 상단 바와 하단 탭 사이를 꽉 채우는 시트 ── */
  #panel, #panel.wide {
    left: 0; right: 0; top: var(--tb-h, 78px); bottom: var(--nav-h, 64px);
    border-radius: 0; border-left: 0; border-right: 0; border-bottom: 0; box-shadow: none;
    animation: sheetUpM .2s ease-out;
  }
  #panel .win-head { border-radius: 0; padding: 6px 8px 6px 12px; font-size: 16px; }
  #panel .win-head .btn { min-height: 34px; min-width: 40px; font-size: 15px; }
  #panel .win-body { padding: 8px 8px 16px; }
  @keyframes sheetUpM { from { transform: translateY(24px); opacity: .4; } to { transform: none; opacity: 1; } }

  /* 창 안의 탭: 한 줄로 두고 손가락으로 민다 (스크롤해도 맨 위에 붙어 있다) */
  .panel-tabs {
    flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none;
    position: sticky; top: -8px; z-index: 4; background: var(--cream);
    margin: -8px -8px 8px; padding: 8px 8px 6px; border-bottom: 2px solid var(--cream-3);
    scroll-snap-type: x proximity;
  }
  .panel-tabs::-webkit-scrollbar { display: none; }
  .panel-tabs .btn { flex: none; min-height: 34px; padding: 3px 11px; font-size: 13px; scroll-snap-align: start; }

  /* ── 팝업: 아래에서 올라오는 시트, 버튼은 엄지가 닿는 아래쪽에 크게 ── */
  .modal-back { place-items: end stretch; padding: 0; grid-template-columns: minmax(0, 1fr); }   /* 내용이 넓어도 팝업은 화면 폭을 넘지 않는다 */
  .modal, .modal.wide {
    width: 100%; max-height: calc(100% - 8px);
    border-radius: 14px 14px 0 0; border-bottom: 0;
    animation: sheetIn .24s cubic-bezier(.2, .9, .3, 1);
  }
  .modal .win-head { border-radius: 11px 11px 0 0; padding: 9px 8px 9px 12px; font-size: 16px; }
  .modal .win-head .btn { min-height: 34px; min-width: 40px; font-size: 15px; }
  .modal .win-body { padding: 10px 10px 14px; }
  .modal-foot { padding: 8px 10px calc(10px + var(--safe-b)); gap: 8px; flex-wrap: wrap; }
  .modal-foot > .btn { flex: 1 1 auto; min-height: 46px; }
  .modal-foot > .grow, .modal-foot > span:empty { display: none; }
  @keyframes sheetIn { from { transform: translateY(40%); opacity: .3; } to { transform: none; opacity: 1; } }

  /* ── 손가락 크기 ── */
  .btn { min-height: 40px; }
  .btn.small { min-height: 34px; padding: 4px 10px; }
  .btn.tiny { min-height: 30px; padding: 2px 8px; font-size: 12px; }
  .btn.big { min-height: 50px; font-size: 16px; padding: 10px 16px; }
  .input, select.input { font-size: 16px; min-height: 40px !important; }   /* 16px 미만이면 아이폰이 입력할 때 화면을 확대한다 */
  .emp-actions { gap: 4px; }
  .emp-actions.one-line { flex-wrap: nowrap; }
  .emp-actions.one-line .btn { flex: 1 1 auto; min-width: 0; padding: 4px 3px; font-size: 12px; }
  input[type="checkbox"] { width: 20px; height: 20px; flex: none; }
  label.row { min-height: 32px; }
  /* 가격·배분 슬라이더: 손가락으로 잡기 쉬운 큰 손잡이 */
  input[type="range"] { -webkit-appearance: none; appearance: none; height: 36px; background: transparent; touch-action: pan-y; }
  input[type="range"]::-webkit-slider-runnable-track { height: 10px; background: var(--cream-3); border: 2px solid var(--ink); border-radius: 6px; }
  input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 28px; height: 28px; margin-top: -11px; background: var(--yellow); border: 3px solid var(--ink); border-radius: 50%; box-shadow: 0 2px 0 rgba(0,0,0,.3); }
  input[type="range"]::-moz-range-track { height: 8px; background: var(--cream-3); border: 2px solid var(--ink); border-radius: 6px; }
  input[type="range"]::-moz-range-thumb { width: 24px; height: 24px; background: var(--yellow); border: 3px solid var(--ink); border-radius: 50%; }
  .chip { font-size: 12px; line-height: 18px; white-space: normal; max-width: 100%; }   /* 좁으면 칩 안에서 줄바꿈 (카드 밖으로 삐져나오지 않게) */
  .small { font-size: 12px; }
  .card { padding: 9px; }

  /* ── 목록·격자: 휴대폰 너비에 맞춰 2~3칸 ── */
  .cards { grid-template-columns: minmax(0, 1fr); }
  .pick-grid { grid-template-columns: repeat(auto-fill, minmax(96px, 1fr)); }
  .pick-grid[style] { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .grid[style*="minmax"] { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }
  .remedy-grid, .on-metrics, .ach-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .pkg-grid { grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
  .titles { grid-template-columns: 1fr; }
  .dev-main { gap: 8px; }
  .dev-main > .col { min-width: 0 !important; flex-basis: 100%; }
  .preview-wrap { width: 100%; align-items: center; }
  .br-row { grid-template-columns: 22px minmax(0, 1.3fr) minmax(40px, 1fr) auto; gap: 6px; font-size: 12px; }
  .st-triad { gap: 8px; }
  .st-rows { min-width: 0; flex-basis: 100%; }
  .st-row { grid-template-columns: 82px 1fr 26px 34px 34px; gap: 4px; }
  .office-steps div { font-size: 10px; padding: 5px 1px; }
  .wizard-steps span { font-size: 11px; padding: 4px 2px; }
  .alloc-row { grid-template-columns: auto minmax(0, 1fr) auto; grid-template-areas: "name eff val" "minus bar plus"; gap: 4px 6px; padding: 4px 0; border-bottom: 2px dashed var(--cream-3); }
  .alloc-row .al-name { grid-area: name; } .alloc-row .al-eff { grid-area: eff; text-align: center; }
  .alloc-row .al-val { grid-area: val; } .alloc-row .al-minus { grid-area: minus; } .alloc-row .al-bar { grid-area: bar; } .alloc-row .al-plus { grid-area: plus; }
  .alloc-row .btn.tiny { min-height: 34px; min-width: 52px; }
  .legal-form .lf-sum { flex-wrap: wrap; }
  .legal-form .lf-sum .btn { flex: 1 1 100%; }

  /* 표: 글자를 줄이고, 그래도 넓으면 표만 가로로 민다 */
  table.tbl { font-size: 12px; word-break: keep-all; overflow-wrap: anywhere; }   /* 한국어 낱말이 한 글자씩 쪼개지지 않게 (띄어쓰기에서만 줄바꿈) */
  .price-tbl td { white-space: nowrap; }
  table.tbl th { white-space: normal; }   /* 머리글(숫자 칸 포함)은 두 줄로 접혀도 된다 → 표가 화면 폭 안에 들어온다 */
  table.tbl th, table.tbl td { padding: 5px 4px; }
  .tbl-wrap { margin: 0 -2px; }
  .tbl-wrap > table.tbl { min-width: 100%; }

  /* ── 알림·연출 위치 ── */
  #toast-root { top: calc(var(--tb-h, 78px) + 6px); width: calc(100vw - 16px); }
  .toast { font-size: 13px; width: 100%; text-align: center; padding: 6px 10px; }
  #toast-root .toast:nth-last-child(n+3) { display: none; }   /* 휴대폰은 화면이 좁아 최신 알림 2개만 */
  .news-banner { top: calc(var(--tb-h, 78px) + 8px); max-width: calc(100vw - 16px); width: calc(100vw - 16px); padding: 7px 10px; }
  #ach-stack { left: 8px; right: 8px; bottom: calc(var(--nav-h, 64px) + 8px); align-items: stretch; }
  .ach-pop { max-width: none; }
  .mfx-big-pop .t { font-size: 26px; }
  .celebrate .box { padding: 16px 12px; }
  .celebrate .ico { font-size: 56px; }
  .celebrate p { font-size: 14px; }
  .celebrate > .win { width: calc(100vw - 12px) !important; max-height: calc(100% - 12px) !important; }
  .phase-stamp { padding: 8px 14px; max-width: calc(100vw - 24px); }
  .speech { white-space: normal; max-width: 62vw; text-align: center; font-size: 12px; }   /* 긴 말풍선이 화면 밖으로 잘리지 않게 */

  /* 리뷰쇼 */
  .review-stage { grid-template-columns: repeat(2, 1fr); gap: 6px; }
  .judge canvas { width: 56px; height: 56px; }
  .judge .score { font-size: 30px; }
  .review-total .big-num { font-size: 34px; }

  /* 시상식 */
  .cer-box { width: calc(100vw - 12px); max-height: calc(100% - 12px); overflow-y: auto; }
  .cer-head { font-size: 17px; }
  .cer-stage { gap: 8px; padding: 8px; min-height: 0; }
  .cer-nom { width: calc(50% - 8px); padding: 6px; }
  .cer-foot { flex-wrap: wrap; }

  /* 판결 */
  .court-box { min-width: 0; width: calc(100vw - 24px); padding: 14px 12px 16px; }
  .court-title { font-size: 40px; }

  /* 엔딩 */
  .ending-stage { width: calc(100vw - 16px); max-height: calc(100% - 16px); gap: 8px; padding-top: 34px; }
  .ending-stage .tbl-wrap { width: 100%; }
  .ending-stage h2 { font-size: 20px; }
  .ending-title h1 { font-size: 24px; }
  .podium { gap: 6px; width: 100%; }
  .pod { width: 32%; }
  .pod-logo { width: 44px; height: 44px; font-size: 24px; line-height: 40px; }
  .pod-block { font-size: 26px; }
  .pod-1 .pod-block { height: 110px; } .pod-2 .pod-block { height: 80px; } .pod-3 .pod-block { height: 58px; }
  .ending-skip { right: 8px; top: 8px; }
  .move-canvas { width: calc(100vw - 32px); }

  /* 타이틀·대기실 */
  .title-wrap { padding: calc(14px + env(safe-area-inset-top, 0px)) 10px 28px; gap: 12px; justify-content: flex-start; }
  .title-sub { font-size: 13px; }
  .color-pick { gap: 5px; }
  .color-pick button { width: 30px; height: 30px; flex: 1 0 26px; max-width: 34px; }
  .logo-pick { gap: 5px; }
  .logo-pick button { width: auto; flex: 1 0 34px; max-width: 42px; height: 40px; }
  .title-tabs .btn { padding: 6px 4px; font-size: 13px; }
  .code-big { font-size: 24px; }
  .title-box .row.wrap > label.field { flex: 1 1 140px; }   /* 회사 수·속도·게임 길이: 휴대폰에선 2칸씩 (글자가 잘리지 않게) */
  /* 멀티 대기실: 서버 현황은 2×2 (숫자 왼쪽 · 설명 오른쪽), 방 목록은 페이지와 함께 스크롤 */
  .room-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .rs-tile { display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: 8px; align-items: center; text-align: left; padding: 6px 8px; }
  .rs-num { grid-row: 1 / 3; font-size: 20px; }
  .room-list { max-height: none; overflow: visible; }
  .room-card { padding: 8px; }
  .room-card .lobby-logo { width: 36px; height: 36px; font-size: 20px; flex: 0 0 auto; }
  .room-card .rc-title { font-size: 14px; }
  .room-card > .btn { min-width: 64px; min-height: 40px; padding: 6px 8px; }
  /* 방 수 배지는 탭 모서리에 띄워서 탭 글자 폭을 잡아먹지 않게 */
  .title-tabs .btn { position: relative; min-width: 0; }
  /* 튜토리얼: 카드는 화면 위나 아래에 가득 (화살표가 대상 바로 옆을 가리킨다) */
  .tut-card { left: 8px !important; right: 8px !important; width: auto; max-height: 40vh; padding: 8px 10px; }
  .tut-card .tut-body { font-size: 13px; line-height: 1.55; }
  .tut-card.compact { max-height: 27vh; }
  .tut-card .tut-title { font-size: 14.5px; }
  .tut-card.tip { left: 8px; right: 8px; max-width: none; }
  .tut-card .tut-foot .btn { min-height: 36px; }
  /* 새로 창업: 혼자 · 친구와는 2칸, 튜토리얼은 아래 한 줄 */
  .play-picks { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .play-pick:nth-child(3) { grid-column: 1 / -1; flex-direction: row; text-align: left; gap: 10px; }
  .play-pick:nth-child(3) b { white-space: nowrap; flex: 0 0 auto; }
  /* 가이드: 챕터 목록은 위쪽 가로 스크롤 */
  .gd-wrap { grid-template-columns: minmax(0, 1fr); gap: 8px; }
  .gd-nav { flex-direction: row; overflow-x: auto; overflow-y: hidden; max-height: none; padding-bottom: 4px; }
  .gd-nav .btn { flex: 0 0 auto; white-space: nowrap; }
  .gd-body { max-height: none; overflow: visible; }
  /* 구제금: 회사 카드는 한 줄에 하나, 금액 창 버튼은 손가락 크기 */
  .bail-cards { grid-template-columns: minmax(0, 1fr); }
  .bail-range { height: 32px; }
  .bail-owe-row .btn { min-height: 38px; }
  .tab-count { position: absolute; top: -7px; right: -4px; min-width: 18px; height: 18px; line-height: 14px; font-size: 10px; margin: 0; padding: 0 3px; z-index: 1; }
}

/* 아주 좁은 휴대폰 (360px 이하): 숫자와 버튼을 조금 더 줄인다 */
@media (max-width: 360px) {
  #topbar .tb-item { padding: 2px 4px; font-size: 11px; gap: 3px; }
  #topbar .speed-btns .btn { padding: 2px 6px; font-size: 12px; }
  #topbar .speed-btns .btn:first-child { min-width: 62px; }
  .nav-btn { font-size: 10px; }
  .st-row { grid-template-columns: 70px 1fr 24px 30px 30px; }
  .emp-actions.one-line { flex-wrap: wrap; }
  .emp-actions.one-line .btn { flex: 1 1 28%; }
}

/* 가로로 눕힌 휴대폰: 높이가 낮으니 탭 글자를 숨기고 창을 꽉 채운다 */
@media (max-height: 500px) and (min-width: 641px) {
  #topbar { padding: 3px 6px; }
  #topbar .tb-item { min-height: 26px; padding: 1px 6px; font-size: 12px; }
  #topbar .btn.small { min-height: 26px; }
  .nav-btn { flex-direction: row; justify-content: center; gap: 4px; padding: 3px 6px; min-width: 0; flex: 1 1 0; }
  .nav-btn .ico { font-size: 16px; line-height: 18px; }
  #nav { padding: 4px 6px calc(4px + var(--safe-b)); }
  #panel, #panel.wide { top: calc(var(--tb-h, 36px) + 4px); bottom: calc(var(--nav-h, 40px) + 4px); left: 4px; right: 4px; }
  .modal { max-height: calc(100% - 6px); }
  .modal-back { padding: 4px; }
  #hud-projects { max-width: min(300px, 46%); }
  .hud-body .game-preview { display: none; }
}

/* 태블릿·가로 휴대폰 (한 줄 상단 바가 빠듯한 폭): 회사 이름 글자는 숨기고(로고는 그대로) 날짜를 짧게 */
@media (min-width: 641px) and (max-width: 960px) {
  #topbar .tb-company .cname { display: none; }
  #tb-date .d-long { display: none; }
  #tb-date .d-short { display: inline; }
  #topbar { gap: 4px; }
  #topbar .tb-item { padding: 2px 6px; }
}
@media (min-width: 641px) and (max-width: 800px) {
  #topbar .speed-btns .btn { padding: 2px 6px; }
  #tb-date .week-bar { width: 30px; }
}
