/* ════════════════════════════════════════════════════════════
   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),
   plus the game elements: round timer, rounds and the "Приёмы" sidebar (no HP).
   ════════════════════════════════════════════════════════════ */

/* Экран чата занимает всю высоту окна; прокручивается ТОЛЬКО лента диалога —
   страница целиком не скроллится. Шапка сверху, композер снизу.
   //
   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: 1180px; width: 100%; margin: 0 auto;
  flex: 1; min-height: 0;
  display: flex; flex-direction: column;
  /* Якорь для объявления раунда (.round-announce, position:absolute).
     Anchor for the round announcement (.round-announce, position:absolute). */
  position: relative;
}

/* Две колонки: диалог + панель «Приёмы» справа (как в оригинальном
   speechcombat: .battle-container = .chat-column + сайдбар 320px).
   //
   Two columns: the dialog plus the "Приёмы" panel on the right (same as the
   original speechcombat: .battle-container = .chat-column + a 320px sidebar). */
.chat-layout {
  display: flex; gap: 16px; align-items: stretch;
  flex: 1; min-height: 0;
}
.chat-column {
  flex: 1; min-width: 0; 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; }

/* ───── Таймер раунда и номер раунда (центр HUD) ─────
   Round timer and round number (HUD center). */
.mk-timer {
  font-size: 30px; font-weight: 900; line-height: 1.05;
  color: #d4a017; letter-spacing: 1px;
  font-variant-numeric: tabular-nums;   /* цифры не «прыгают» / digits do not jitter */
  text-shadow: 0 0 12px rgba(212,160,23,0.35);
}
/* Последние 10 секунд раунда — красный и пульсация.
   The round's final 10 seconds — red and pulsing. */
.mk-timer.urgent { color: var(--red); text-shadow: 0 0 12px rgba(255,23,68,0.45); animation: mk-timer-pulse 1s ease-in-out infinite; }
.mk-timer.over   { color: var(--text3); text-shadow: none; }
@keyframes mk-timer-pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.12); } }
.mk-round { font-size: 10px; font-weight: 700; color: var(--text3); letter-spacing: 1px; margin-top: 2px; }

/* ───── Объявление раунда поверх ленты («ROUND 1») ─────
   Round announcement over the feed ("ROUND 1"). */
.round-announce {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;   /* не перехватывает клики по чату / does not swallow chat clicks */
  z-index: 5;
}
.round-announce[hidden] { display: none; }
.round-announce span {
  font-size: clamp(40px, 8vw, 72px); font-weight: 900; letter-spacing: 4px;
  color: var(--text); text-transform: uppercase;
  text-shadow: 0 0 30px rgba(212,160,23,0.55), 0 4px 20px rgba(0,0,0,0.35);
  animation: round-announce-in 1.8s ease-out forwards;
}
@keyframes round-announce-in {
  0%   { opacity: 0; transform: scale(1.6); }
  18%  { opacity: 1; transform: scale(1); }
  75%  { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.94); }
}

.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); }

/* ───── Область чата ───── */
/* Лента без «карточки»: прозрачный фон, без рамки — пузыри лежат прямо на фоне
   страницы (как в оригинальном speechcombat).
   //
   The feed has no "card": transparent background, no border — the bubbles sit
   directly on the page background (as in the original speechcombat). */
.chat-area {
  flex: 1;
  display: flex; flex-direction: column; gap: 12px;
  padding: 16px 4px;
  background: transparent;
  border: none;
  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); }

/* ───── Пузырь сообщения ───── */
/* Цвета пузырей — из оригинального speechcombat: тёмно-синие градиенты со
   светлым текстом, а не белая «карточка».
   //
   Bubble colors come from the original speechcombat: dark-blue gradients with
   light text, not a white "card". */
.msg-body {
  max-width: 70%; padding: 10px 14px; border-radius: 16px;
  font-size: 14px; line-height: 1.5; word-wrap: break-word; white-space: pre-wrap;
  box-shadow: 0 1px 3px rgba(0,0,0,0.12);
}
.msg-player .msg-body   { background: linear-gradient(135deg, #2B5278, #1e3a5f); color: #fff; border-bottom-right-radius: 4px; }
.msg-opponent .msg-body { background: linear-gradient(135deg, #1e2d3d, #182533); color: #fff; 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%;
}

/* ───── Голосовое сообщение ─────
   Voice message. */
/* Идёт запись — кнопка микрофона красная и пульсирует.
   Recording in progress — the mic button turns red and pulses. */
#micBtn.recording {
  background: var(--red); color: #fff; border-color: var(--red);
  animation: mic-pulse 1.2s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255,23,68,0.45); }
  50%      { box-shadow: 0 0 0 7px rgba(255,23,68,0); }
}
#micBtn:disabled { opacity: .45; cursor: not-allowed; }

/* Пузырь с озвученным ответом: внутри обычный аудио-плеер браузера.
   A bubble with the voiced reply: a plain browser audio player inside. */
.msg-audio { padding: 8px 10px; }
.msg-audio audio { display: block; height: 36px; max-width: 260px; }

/* ───── Разбор диалога (карточка в конце ленты) ─────
   Dialog analysis (a card at the end of the feed). */
.dialog-result {
  align-self: center;
  width: 100%;
  max-width: 640px;
  background: var(--surface);
  border: 1px solid rgba(212,160,23,0.45);
  border-radius: var(--radius-lg, 12px);
  padding: 14px 18px;
  box-shadow: 0 2px 14px rgba(0,0,0,0.18);
  animation: msg-fade-in 0.3s ease-out;
}
.dialog-result-head {
  font-size: 11px; font-weight: 800; letter-spacing: 2px; text-transform: uppercase;
  color: #d4a017; margin-bottom: 8px;
}
.dialog-result-body {
  font-size: 14px; line-height: 1.55; color: var(--text);
  white-space: pre-wrap;   /* сохраняем переносы из ответа ИИ / keep the AI answer's line breaks */
  word-break: break-word;
}

/* ───── Композер (ввод) ───── */
.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;
  }
}
