/* Chat stays available in battles, cutscenes and story dialogue (phones, tablets and desktop).
   Before, a battle hid #chat (battle.css) and the phone's chat button with the rest of #m-ui, and cinema mode
   (js/main.js cinemaMode) faded the chat out. Now:
   - Battle: the chat is a "Chat" button with the unread badge until it is opened (every screen: the ticker would sit
     in the way). Desktop layout (.lay-desktop): bottom left, above the battle's message line, and it opens there; the
     command dock (bottom right) is never covered. Portrait / short-landscape layouts: opened, it is a sheet over the
     top half under the foe's HP card (portrait) or a panel on the left, clear of the moves and the message line.
     The open chat has one clear × (back to the button); Esc and a tap outside close it too (js/hudextras.js), and
     opening it puts the cursor in the message box.
   - Cutscene (body.cinema): the same round button above the lower film bar; the open chat stays clear of the bar.
   - Story dialogue on a touch screen (html.dlg-open, js/dialogue.js): the open chat moves to the top, so the dialogue
     box and its YES / NO or choice list stay free.
   Keys: Enter stays confirm / continue in a battle and a dialogue; "/" opens the chat there (js/battle/ui.js,
   js/dialogue.js; Enter or "/" in the open world, js/main.js). While the chat box has the focus no key reaches the
   battle or the dialogue; Esc (or the shrink button) closes it and the battle has the keys again. */

/* ---------- the round button instead of the ticker ---------- */
html body.battle-active #chat:not([data-state="expanded"]) .chat-card,
body.cinema #chat:not([data-state="expanded"]) .chat-card {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: 0; border: 0; overflow: hidden; clip-path: inset(50%); opacity: 0; pointer-events: none;
}
html.touch body.battle-active #chat:not([data-state="expanded"]) .chat-bubble, html body.battle-active #chat:not([data-state="expanded"]) .chat-bubble,
html body.cinema #chat:not([data-state="expanded"]) .chat-bubble {
  display: inline-flex !important; align-items: center; gap: 8px; width: auto; min-width: 48px; height: 48px; padding: 0 16px 0 13px; border-radius: 24px;
  font: 900 14px/1 Nunito, system-ui, sans-serif; background: rgba(13, 21, 44, .92); border-color: var(--line-2); }
html body.battle-active #chat:not([data-state="expanded"]) .cb-label, html body.cinema #chat:not([data-state="expanded"]) .cb-label { display: inline; }
html.touch body.battle-active #chat:not([data-state="expanded"]), html body.battle-active #hud #chat:not([data-state="expanded"]),
html body.cinema #hud #chat:not([data-state="expanded"]) { width: auto; max-width: none; right: auto; opacity: 1; }

/* ---------- the open chat in a battle or cutscene: one clear × (back to the Chat button); Esc and a tap outside do the same (js/hudextras.js) ---------- */
html body.battle-active #chat[data-state="expanded"] #chat-size, html body.cinema #chat[data-state="expanded"] #chat-size { display: none; }
html body.battle-active #chat[data-state="expanded"] #chat-hide, html body.cinema #chat[data-state="expanded"] #chat-hide {
  display: grid; width: 36px; height: 36px; border-radius: 10px; color: #fff; background: rgba(255, 255, 255, .14); border: 1px solid var(--line-2); }
html body.battle-active #chat[data-state="expanded"] #chat-hide:hover, html body.cinema #chat[data-state="expanded"] #chat-hide:hover { background: rgba(255, 95, 107, .35); border-color: rgba(255, 95, 107, .7); }
html.touch body.battle-active #chat[data-state="expanded"] #chat-hide, html.touch body.cinema #chat[data-state="expanded"] #chat-hide { width: 42px; height: 42px; }
html body.battle-active #chat[data-state="expanded"] #chat-hide .ic, html body.cinema #chat[data-state="expanded"] #chat-hide .ic { width: 20px; height: 20px; }

/* ---------- battle ---------- */
body.battle-active #chat { z-index: 7; }   /* above the battle HUD (#battle-ui, z-index 6) */
/* desktop layout: bottom-left, above the message line (16px margin + the line + a gap) */
html body.battle-active #chat { left: max(16px, var(--hud-x, 16px)); bottom: 88px; }
html.touch body.battle-active:has(#battle-ui.lay-desktop) #chat { left: calc(var(--sal, 0px) + 16px); bottom: calc(var(--sab, 0px) + 88px); transform: none; }
html.touch body.battle-active:has(#battle-ui.lay-desktop) #chat[data-state="expanded"] { width: min(440px, calc(100vw - 470px - 48px - var(--sal, 0px) - var(--sar, 0px))); }
/* portrait layout: the button on the left, level with your HP card, just above the message line and the dock */
html body.battle-active:has(#battle-ui.lay-portrait) #chat:not([data-state="expanded"]) { left: calc(var(--sal, 0px) + 8px); bottom: calc(var(--sab, 0px) + 8px + 188px + 6px + 44px + 14px); }
/* short landscape: the button above the message line */
html body.battle-active:has(#battle-ui.lay-landscape) #chat:not([data-state="expanded"]) { left: calc(var(--sal, 0px) + 8px); bottom: calc(var(--sab, 0px) + 6px + 52px + 8px); }
/* opened in the portrait layout: a sheet over the top half, right under the foe's HP card (the battle HUD is drawn
   above the game HUD, so the sheet starts below that card instead of under it) */
html body.battle-active:has(#battle-ui.lay-portrait) #chat[data-state="expanded"] {
  top: calc(var(--sat, 0px) + 64px); bottom: auto; left: calc(var(--sal, 0px) + 8px); right: calc(var(--sar, 0px) + 8px); width: auto; transform: none; }
html body.battle-active:has(#battle-ui.lay-portrait) #chat[data-state="expanded"] .chat-card { border-radius: 16px; padding-bottom: 0; box-shadow: 0 12px 30px rgba(0, 0, 0, .45); }
html body.battle-active:has(#battle-ui.lay-portrait) #chat[data-state="expanded"] #chat-log { height: min(calc(24 * var(--udvh, 1dvh)), 220px); }
/* opened in the short landscape layout: a panel on the left under the foe's HP card, beside the dock */
html body.battle-active:has(#battle-ui.lay-landscape) #chat[data-state="expanded"] {
  top: calc(var(--sat, 0px) + 62px); bottom: auto; left: calc(var(--sal, 0px) + 8px); transform: none;
  width: min(420px, calc(100vw - min(400px, 52vw) - 32px - var(--sal, 0px) - var(--sar, 0px))); }
html body.battle-active:has(#battle-ui.lay-landscape) #chat[data-state="expanded"] #chat-log { height: min(calc(26 * var(--udvh, 1dvh)), 170px); }

/* ---------- cutscene (film bars, 8vh each) ---------- */
html body.cinema #hud #chat { opacity: 1; z-index: 8; left: calc(var(--sal, 0px) + 12px); bottom: calc(8 * var(--uvh, 1vh) + 12px); transform: none; }
html body.cinema #chat:not([data-state="expanded"]) .chat-bubble { opacity: .8; }

/* ---------- touch: the open chat goes to the top while a dialogue box or a cutscene has the bottom ---------- */
html.touch.portrait.dlg-open #chat[data-state="expanded"], html.touch.portrait body.cinema #chat[data-state="expanded"] { top: 0; bottom: auto; left: 0; right: 0; width: 100%; transform: none; }
html.touch.portrait.dlg-open #chat[data-state="expanded"] .chat-card, html.touch.portrait body.cinema #chat[data-state="expanded"] .chat-card {
  border-radius: 0 0 18px 18px; padding-top: var(--sat, 0px); padding-bottom: 0; box-shadow: 0 12px 30px rgba(0, 0, 0, .45); }
html.touch.portrait body.cinema #chat[data-state="expanded"] .chat-card { padding-top: calc(8vh + 4px); }
html.touch.portrait.dlg-open #chat[data-state="expanded"] #chat-log, html.touch.portrait body.cinema #chat[data-state="expanded"] #chat-log { height: min(28dvh, 230px); }
html.touch.landscape.dlg-open #chat[data-state="expanded"], html.touch.landscape body.cinema #chat[data-state="expanded"] {
  top: calc(var(--sat, 0px) + 6px); bottom: auto; left: calc(var(--sal, 0px) + 8px); transform: none; width: min(400px, 46vw); }
html.touch.landscape body.cinema #chat[data-state="expanded"] { top: calc(8vh + 6px); }
html.touch.landscape.dlg-open #chat[data-state="expanded"] #chat-log, html.touch.landscape body.cinema #chat[data-state="expanded"] #chat-log { height: min(30dvh, 200px); }
