/* ════════════════════════════════════════════════════════════
   Speech Combat — чат с ИИ (диалог).
   Дизайн перенесён 1:1 из проекта speechcombat (battle screen):
   mk-hud (шапка с аватарами) + chat.css (пузыри) + compose.css (ввод),
   но без игровых элементов (HP, таймер, раунды, сайдбар «Приёмы»).
   //
   Speech Combat — AI chat (dialog).
   Design lifted 1:1 from the speechcombat project (battle screen):
   mk-hud (header with avatars) + chat.css (bubbles) + compose.css (input),
   but without the game elements (HP, timer, rounds, "Приёмы" sidebar).
   ════════════════════════════════════════════════════════════ */

/* Экран чата занимает всю высоту окна; прокручивается ТОЛЬКО лента диалога —
   страница целиком не скроллится. Шапка сверху, композер снизу.
   //
   The chat screen fills the viewport; ONLY the dialog feed scrolls — the page
   itself does not scroll. Header on top, composer at the bottom. */
body.chat-page {
  /* Высота = реально видимая область (без системной панели телефона и адресной
     строки). Значение --app-height ставит JS из window.visualViewport; svh/vh —
     запасные варианты. Это надёжнее, чем dvh, который Chrome на Android
     завышает на «залоченной» странице, из-за чего поле ввода уезжало под
     нижнюю панель навигации.
     //
     Height = the actually-visible area (excludes the phone's nav bar and the
     address bar). --app-height is set by JS from window.visualViewport; svh/vh
     are fallbacks. This is more reliable than dvh, which Chrome on Android
     overestimates on a locked page, pushing the input under the nav bar. */
  height: 100vh;
  height: 100svh;
  height: var(--app-height, 100svh);
  overflow: hidden;
  display: flex; flex-direction: column;
  box-sizing: border-box;
  /* Резервируем место под системную нижнюю панель (edge-to-edge Android /
     home-indicator iOS). На таких устройствах visualViewport включает область
     под панелью, поэтому нижний inset нужен явно.
     Reserve space for the bottom system bar (edge-to-edge Android / iOS home
     indicator). On such devices visualViewport includes the area under the bar,
     so the bottom inset is needed explicitly. */
  padding-bottom: max(env(safe-area-inset-bottom, 0px), var(--bottom-inset, 0px));
}
body.chat-page .container { flex: 1; min-height: 0; width: 100%; display: flex; flex-direction: column; }

.chat-wrap {
  max-width: 860px; width: 100%; margin: 0 auto;
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
}

/* ───── HUD (шапка: ТЫ vs ИИ) ───── */
.mk-hud {
  display: flex; align-items: center; gap: 12px;
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg2) 100%);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  padding: 14px 18px;
  margin-bottom: 12px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.3);
  flex-shrink: 0;
}
.mk-hud-side { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.mk-hud-right { justify-content: flex-end; }
.mk-hud-info { min-width: 0; }
.mk-hud-name { font-size: 11px; font-weight: 800; letter-spacing: 2px; text-transform: uppercase; margin-bottom: 3px; }
.mk-hud-name.blue { color: var(--blue); }
.mk-hud-name.red  { color: var(--red); text-align: right; }
.mk-hud-sub { font-size: 11px; color: var(--text2); }
.mk-hud-right .mk-hud-sub { text-align: right; }
.mk-hud-center { text-align: center; min-width: 96px; flex-shrink: 0; }
.mk-scenario-name { font-size: 10px; color: var(--text3); letter-spacing: 1px; text-transform: uppercase; }
.mk-scenario-tactic { font-size: 13px; font-weight: 800; color: var(--text); margin-top: 2px; }

.mk-avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 15px; font-weight: 800; letter-spacing: 1px;
}
.mk-avatar-blue { background: rgba(79,143,224,0.25); color: var(--blue); border: 2px solid var(--blue); }
.mk-avatar-red  { background: rgba(255,23,68,0.25); color: var(--red); border: 2px solid var(--red); }

/* ───── Область чата ───── */
.chat-area {
  flex: 1;
  display: flex; flex-direction: column; gap: 12px;
  padding: 16px;
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow-y: auto;
  min-height: 0;
}
.chat-area::-webkit-scrollbar { width: 8px; }
.chat-area::-webkit-scrollbar-track { background: var(--surface); border-radius: 4px; }
.chat-area::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 4px; }
.chat-area::-webkit-scrollbar-thumb:hover { background: var(--text3); }

/* ───── Сообщение ───── */
.msg { display: flex; gap: 10px; align-items: flex-start; animation: msg-fade-in 0.3s ease; }
.msg-player   { flex-direction: row-reverse; }
.msg-opponent { flex-direction: row; }
.msg-system   { justify-content: center; text-align: center; }
@keyframes msg-fade-in { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

.msg-avatar {
  width: 32px; height: 32px; border-radius: 50%; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  border: 2px solid var(--border); font-size: 12px; font-weight: 800;
}
.msg-player .msg-avatar   { border-color: var(--blue); background: rgba(79,143,224,0.18); color: var(--blue-bright); }
.msg-opponent .msg-avatar { border-color: var(--red);  background: rgba(255,23,68,0.18); color: var(--red); }

/* ───── Пузырь сообщения ───── */
.msg-body {
  max-width: 70%; padding: 10px 14px; border-radius: 12px;
  font-size: 14px; line-height: 1.5; word-wrap: break-word; white-space: pre-wrap;
}
.msg-player .msg-body   { background: var(--blue); color: #fff; border-bottom-right-radius: 4px; }
.msg-opponent .msg-body { background: var(--surface); color: var(--text); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg-system .msg-body {
  background: rgba(212,160,23,0.15); color: #d4a017; border: 1px solid rgba(212,160,23,0.3);
  font-weight: 700; text-align: center; max-width: 100%;
}

/* ───── Композер (ввод) ───── */
.chat-compose {
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 12px 14px; margin-top: 12px;
  flex-shrink: 0;
}
.chat-compose:focus-within { border-color: var(--blue); }
.chat-compose textarea {
  flex: 1; min-height: 46px; max-height: 200px; padding: 11px 12px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 10px;
  font-size: 14px; color: var(--text); font-family: inherit; resize: none;
  transition: border-color 0.2s;
}
.chat-compose textarea:focus { outline: none; border-color: var(--blue); }
.chat-compose textarea::placeholder { color: var(--text3); }

.compose-btn.primary {
  background: var(--blue); color: #fff; border: 1px solid var(--blue); border-radius: 10px;
  width: 48px; height: 46px; flex-shrink: 0; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s, box-shadow 0.15s, opacity 0.15s;
}
.compose-btn.primary:hover { background: var(--blue-bright); box-shadow: 0 4px 12px rgba(79,143,224,0.3); }
.compose-btn.primary:active { background: var(--blue-deep); }
.compose-btn.primary:disabled { opacity: 0.55; cursor: not-allowed; }
.compose-btn.primary svg { width: 18px; height: 18px; }

/* ───── Индикатор «печатает» ───── */
.msg-typing-bubble { min-width: 56px; display: flex; align-items: center; justify-content: center; }
.dot-typing { display: inline-flex; gap: 5px; align-items: center; height: 18px; }
.dot-typing span { width: 8px; height: 8px; border-radius: 50%; background: var(--text3); animation: dotBounce 1.4s infinite ease-in-out; }
.dot-typing span:nth-child(2) { animation-delay: 0.16s; }
.dot-typing span:nth-child(3) { animation-delay: 0.32s; }
@keyframes dotBounce { 0%,60%,100% { transform: translateY(0); opacity: 0.35; } 30% { transform: translateY(-6px); opacity: 1; } }

.chat-limit { margin-top: 12px; text-align: center; color: #d4a017; font-size: 13px; font-weight: 600; }

/* ───── Адаптив ───── */
@media (max-width: 600px) {
  .chat-area { min-height: 0; padding: 10px; gap: 8px; }
  .msg-body { max-width: 82%; font-size: 13px; padding: 8px 12px; }

  /* HUD: на мобильном текст роли налезал на аватар ИИ. Ужимаем отступы,
     даём центру сжиматься (min-width:0) и обрезаем длинный текст роли
     до двух строк, чтобы он не наезжал на аватар.
     //
     HUD: on mobile the role text overlapped the AI avatar. Shrink paddings,
     let the center column shrink (min-width:0) and clamp the long role text
     to two lines so it never collides with the avatar. */
  .mk-hud { padding: 10px 12px; gap: 8px; }
  .mk-avatar { width: 34px; height: 34px; font-size: 13px; }
  .mk-hud-side { gap: 8px; }
  .mk-hud-center { min-width: 0; }
  .mk-scenario-tactic { font-size: 12px; }
  .mk-hud-sub {
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }
}
