/*
  Touch controls and phone layout, built on the UI kit tokens in ui.css.
  Everything is scoped to html.touch / body.touch (set by src/touch.js), except the bottom-sheet settings for narrow screens.
*/

html.touch, body.touch { touch-action: none; overscroll-behavior: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
body.touch { position: fixed; inset: 0; --sl: max(var(--ui-s3), env(safe-area-inset-left)); --sr: max(var(--ui-s3), env(safe-area-inset-right)); --st: max(10px, env(safe-area-inset-top)); --sb: max(var(--ui-s3), env(safe-area-inset-bottom)); }
body.touch input, body.touch textarea { -webkit-user-select: text; user-select: text; }
body.touch button, body.touch select, body.touch input { touch-action: manipulation; }
body.touch .ui-panel-body, body.touch #chat.open #chat-log, body.touch .ui-card { touch-action: pan-y; }

#touch { position: fixed; inset: 0; }
#touch .t-zone { position: absolute; inset: 0; touch-action: none; }

.t-stick { position: absolute; left: calc(var(--sl) + 78px); bottom: calc(var(--sb) + 78px); width: 0; height: 0; pointer-events: none; opacity: 0.5; transition: opacity 0.2s; }
.t-stick::before { content: ''; position: absolute; left: -60px; top: -60px; width: 120px; height: 120px; border-radius: 50%; border: 2px solid var(--ui-paper-a); background: rgba(0, 33, 69, 0.18); box-shadow: 0 0 0 1.5px var(--ui-line-soft), var(--ui-shadow-sm); }
.t-stick i { position: absolute; left: -26px; top: -26px; width: 52px; height: 52px; border-radius: 50%; border: var(--ui-border); background: var(--ui-paper-a); box-shadow: var(--ui-shadow-sm); }
.t-stick.on { opacity: 1; transition: none; }
.t-stick.sprint::before { border-color: var(--ui-gold); background: var(--ui-gold-soft); }
.t-stick.sprint i { background: var(--ui-gold); }

.t-btn { position: absolute; display: grid; place-items: center; width: 50px; height: 50px; padding: 0; border: var(--ui-border); border-radius: 50%; background: var(--ui-paper-a); color: var(--ui-navy); box-shadow: var(--ui-shadow-sm); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); touch-action: none; transition: none; }
.t-btn:hover { background: var(--ui-paper-a); }
.t-btn:active { transform: none; box-shadow: var(--ui-shadow-sm); }
.t-btn.down { background: var(--ui-paper-2); box-shadow: none; }
.t-btn.on { background: var(--ui-gold); }
.t-btn svg { pointer-events: none; }

.t-pad { position: absolute; right: var(--sr); bottom: var(--sb); width: 0; height: 0; }
.t-big { width: 76px; height: 76px; }
.t-big svg { width: 30px; height: 30px; }
.t-jump { right: 4px; bottom: 6px; }
.t-jump .t-fish { display: none; }
#touch.fish .t-jump { background: var(--ui-gold); }
#touch.fish .t-jump .t-up { display: none; }
#touch.fish .t-jump .t-fish { display: block; }
.t-pb { display: none; }
#touch.pb .t-pb { display: grid; }
#touch.pb .t-jump { right: 12px; bottom: 104px; width: 58px; height: 58px; }
.t-fire { right: 0; bottom: 0; width: 88px; height: 88px; background: var(--ui-red); color: var(--ui-paper); }
.t-fire:hover { background: var(--ui-red); }
.t-fire.down { background: #9c0c24; }
.t-aim { right: 102px; bottom: 4px; width: 54px; height: 54px; }
.t-reload { right: 106px; bottom: 72px; width: 46px; height: 46px; }
.t-swap { right: 84px; bottom: 132px; width: auto; min-width: 44px; height: 36px; padding: 0 12px; border-radius: var(--ui-r-pill); gap: 5px; grid-auto-flow: column; align-items: center; font-size: var(--ui-t-sm); font-weight: 750; }
.t-swap svg { width: 16px; height: 16px; }

.t-top { position: absolute; top: calc(var(--st) + 52px); right: var(--sr); display: flex; flex-direction: column; gap: 10px; }
.t-top .t-btn { position: static; width: 44px; height: 44px; }
.t-top .t-btn svg { width: 21px; height: 21px; }

.t-ctx { position: absolute; left: 50%; bottom: calc(var(--sb) + 16px); transform: translateX(-50%); display: flex; flex-direction: column-reverse; align-items: center; gap: var(--ui-s2); max-width: 46vw; }
.t-pill { padding: 10px 18px; font-size: 15px; white-space: nowrap; max-width: 46vw; overflow: hidden; text-overflow: ellipsis; touch-action: none; cursor: pointer; }
.t-pill:active { background: var(--ui-gold); }

.t-guide .ui-group { margin-bottom: var(--ui-s2); }
.t-guide .ui-row > span:last-child { color: var(--ui-ink-2); font-weight: 500; text-align: right; }
body.touch [data-pane='controls'] > :not(.t-guide) { display: none; }
.t-rotate { display: none; }
@media (orientation: portrait) { html.touch .t-rotate { display: block; color: var(--ui-gold-ink); font-weight: 650; } }

/* HUD placement on touch screens: the bottom corners belong to the thumbs */
body.touch #brand, body.touch #prompt, body.touch .npc-tag.prompt, body.touch .pb-keys, body.touch .pb-weapons, body.touch #attribution, body.touch .fr-keys { display: none; }
body.touch #gear { top: var(--st); right: var(--sr); width: 44px; height: 44px; }
body.touch #clock { top: calc(var(--st) + 5px); right: calc(var(--sr) + 54px); }
body.touch #location { top: var(--st); left: var(--sl); bottom: auto; max-width: 52vw; }
body.touch #location b { font-size: 15px; }
body.touch #location span { font-size: var(--ui-t-xs); }
body.touch #online { top: calc(var(--st) + 44px); left: var(--sl); right: auto; }
body.touch #chat { top: calc(var(--st) + 80px); left: var(--sl); bottom: auto; width: min(340px, 54vw); flex-direction: column-reverse; }
body.touch #chat-log { max-height: 22vh; }
body.touch #chat-input { font-size: 16px; }
body.touch #minimap { pointer-events: auto; top: calc(var(--st) + 52px); right: calc(var(--sr) + 56px); bottom: auto; width: 104px; height: 104px; border-width: 3px; }
body.touch #speedo { top: calc(var(--st) + 164px); right: calc(var(--sr) + 58px); bottom: auto; }
body.touch #pb-panel { top: calc(var(--st) + 164px); right: calc(var(--sr) + 56px); bottom: auto; width: 128px; padding: 7px 10px; }
body.touch .pb-ammo b { font-size: 22px; }
body.touch #toast { top: 30%; max-width: min(80vw, 460px); font-size: var(--ui-t-sm); }
body.touch #npc-card { bottom: auto; top: calc(30% + 64px); }
body.touch #run { top: calc(var(--st) + 80px); left: var(--sl); width: 210px; }
body.touch #players { top: calc(var(--st) + 56px); }
body.touch #fps { top: auto; bottom: 4px; left: 50%; transform: translateX(-50%); }
body.touch #labels .label.building { font-size: 11px; }
body.touch.menu-open #minimap, body.touch.menu-open #speedo, body.touch.menu-open #online, body.touch.menu-open #pb-panel { transform: none; visibility: visible; }
body.touch.menu-open #toast, body.touch.menu-open #players, body.touch.menu-open #npc-card { left: 50%; }
body.touch.menu-open #touch .t-top { visibility: hidden; }

@media (orientation: portrait) {
  body.touch #clock { display: none; }
  #touch.pb .t-ctx { bottom: calc(var(--sb) + 184px); }
}
@media (orientation: landscape) and (max-height: 500px) {
  .t-top { flex-direction: row; top: var(--st); right: calc(var(--sr) + 54px); }
  body.touch #clock { display: none; }
  body.touch #location { max-width: 34vw; }
  body.touch #chat-log { max-height: 26vh; }
  body.touch #minimap { top: calc(var(--st) + 54px); right: var(--sr); width: 88px; height: 88px; }
  body.touch #speedo { top: calc(var(--st) + 148px); right: var(--sr); }
  body.touch #pb-panel { top: calc(var(--st) + 54px); right: calc(var(--sr) + 100px); }
  body.touch #toast { top: calc(var(--st) + 56px); max-width: min(46vw, 420px); }
  body.touch #npc-card { top: calc(var(--st) + 120px); }
}

/* Map and dialogs */
body.touch .map-panel { width: calc(100vw - var(--sl) - var(--sr)); height: calc(100% - var(--st) - var(--sb)); }
body.touch #map-canvas { cursor: default; }
body.touch .map-bar { top: var(--ui-s2); width: calc(100% - 16px); max-width: 560px; flex-wrap: wrap; padding: 6px; }
body.touch .map-bar input { flex: 1 1 140px; width: auto; min-width: 0; font-size: 16px; }
body.touch .map-hint { font-size: var(--ui-t-xs); }
body.touch .intro h1 { font-size: 34px; }
body.touch .ui-card { max-height: calc(100% - 24px); }
body.touch #character { top: var(--st); bottom: var(--sb); right: var(--sr); max-width: calc(100vw - 24px); }

/* Loading bar and lost graphics notice (all devices) */
.load-bar { width: min(260px, 70vw); height: 6px; margin: 2px auto var(--ui-s2); }
.load-bar > i { transition: width 0.3s ease; }
#gl-lost { position: fixed; inset: 0; z-index: var(--ui-z-top); display: grid; place-items: center; background: rgba(0, 20, 45, 0.5); backdrop-filter: blur(3px); }
#gl-lost .ui-card { max-width: min(360px, 90vw); text-align: center; }
#gl-lost p { margin: 0 0 var(--ui-s3); }

/* Settings: bottom sheet on narrow screens, full-height side panel on short ones */
@media (max-width: 600px) {
  #menu { top: auto; left: 0; right: 0; bottom: 0; width: auto; height: min(68vh, 560px); }
  #menu .ui-panel { border-radius: var(--ui-r-lg) var(--ui-r-lg) 0 0; border-bottom: 0; padding-bottom: env(safe-area-inset-bottom); animation: sheet-in 0.2s ease-out; }
  #menu .ui-panel::before { content: ''; align-self: center; flex: none; width: 38px; height: 4px; margin-top: var(--ui-s2); border-radius: 2px; background: var(--ui-line-soft); }
  #menu .ui-panel-head { padding-top: var(--ui-s1); }
  body.menu-open #minimap, body.menu-open #speedo, body.menu-open #online, body.menu-open #attribution, body.menu-open #pb-panel { transform: none; }
  body.menu-open #toast, body.menu-open #players, body.menu-open #npc-card { left: 50%; }
}
@keyframes sheet-in { from { transform: translateY(40px); opacity: 0; } }
@media (max-height: 500px) and (min-width: 601px) {
  body.touch #menu { top: var(--st); bottom: var(--sb); right: var(--sr); }
}
