/* Плагін «Чат»: скляна картка з кімнатами, стрічкою повідомлень і полем вводу */

/* Висота картки: сторінка не прокручується, прокручується лише стрічка */
body.chat-view .main { padding-bottom: 24px; }
body.chat-view .main, body.chat-view .main-inner, .chat-card, .chat-top, .chat-pills { min-width: 0; }
.chat-card { max-width: 100%; }
.chat-head-row { display: flex; align-items: baseline; justify-content: space-between; }
.chat-card {
  --chat-h: calc(100vh - 150px);
  position: relative; display: flex; flex-direction: column;
  height: var(--chat-h); min-height: 420px;
  border-radius: 26px; overflow: hidden;
  background: var(--glass-bg);
  -webkit-backdrop-filter: var(--blur); backdrop-filter: var(--blur);
  box-shadow: var(--glass-edge), var(--glass-shadow);
}
@supports (height: 100dvh) { .chat-card { --chat-h: calc(100dvh - 150px); } }

/* ---------- Верх: кімнати й статус ---------- */
.chat-top { padding: 12px 12px 8px; border-bottom: .5px solid var(--sep); flex: none; }
.chat-pills { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding: 4px 6px 6px 4px; margin: -4px -6px -6px -4px; -webkit-overflow-scrolling: touch; }
.chat-pills::-webkit-scrollbar { display: none; }
.chat-pill {
  flex: none; padding: 8px 14px; min-height: 36px; border-radius: 99px; font-size: 14px; font-weight: 600;
  color: var(--label); background: var(--fill); transition: background .25s var(--ease), color .25s, box-shadow .25s;
}
.chat-pill:hover { background: var(--fill2); }
.chat-pill.on { background: var(--accent); color: #fff; box-shadow: 0 4px 14px color-mix(in srgb, var(--accent) 35%, transparent); }
.chat-status { margin: 8px 4px 0; font-size: 13px; color: var(--label2); display: flex; align-items: center; gap: 6px; min-height: 18px; }
.chat-status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--label3); flex: none; }
.chat-status[data-state="online"]::before { background: var(--green); box-shadow: 0 0 0 3px var(--green-tint); }
.chat-status[data-state="retry"]::before, .chat-status[data-state="connecting"]::before, .chat-status[data-state="checking"]::before {
  background: var(--orange); animation: chatPulse 1.2s ease-in-out infinite; }
.chat-status[data-state="noserver"]::before, .chat-status[data-state="badname"]::before { background: var(--red); }
@keyframes chatPulse { 50% { opacity: .3; } }

/* ---------- Стрічка ---------- */
.chat-list {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 10px 12px 14px; display: flex; flex-direction: column; gap: 3px;
}
.cm-day { display: flex; justify-content: center; margin: 12px 0 6px; position: sticky; top: 0; z-index: 1; pointer-events: none; }
.cm-day span { font-size: 12px; font-weight: 600; color: var(--label2); padding: 4px 10px; border-radius: 99px;
  background: var(--glass-strong, var(--bg2)); box-shadow: var(--glass-edge); }
.cm-sys { align-self: center; text-align: center; max-width: 90%; font-size: 12.5px; color: var(--label2); padding: 4px 10px; margin: 4px 0; }
.cm-empty { margin: auto; text-align: center; color: var(--label2); padding: 20px; }
.cm-empty svg { width: 76px; height: auto; }
.cm-empty p { margin: 8px 0 0; font-size: 15px; }

.cm { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; position: relative; }
.cm:not(.cont) { margin-top: 8px; }
.cm.me { justify-content: flex-end; }
.cm-bubble {
  position: relative; padding: 8px 12px 7px; border-radius: 20px; font-size: 15.5px; line-height: 1.35;
  max-width: min(78%, 440px); white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word;
  background: var(--bg2); color: var(--label); box-shadow: 0 1px 1.5px rgba(0, 0, 0, .06);
}
.cm.other .cm-body { display: flex; flex-direction: column; align-items: flex-start; min-width: 0; max-width: min(78%, 440px); }
.cm.other .cm-bubble { max-width: 100%; border-bottom-left-radius: 8px; }
.cm.other.cont .cm-bubble { border-top-left-radius: 8px; }
.cm.me .cm-bubble { background: var(--accent); color: #fff; border-bottom-right-radius: 8px; }
.cm.me.cont .cm-bubble { border-top-right-radius: 8px; }
.cm-t { font-size: 11px; opacity: .6; margin-left: 8px; float: right; margin-top: 5px; line-height: 1; font-variant-numeric: tabular-nums; }
.cm.me .cm-t { opacity: .75; }
.cm-head { display: flex; align-items: baseline; gap: 8px; margin: 0 0 3px 4px; font-size: 13px; }
.cm-head b { font-weight: 600; color: var(--label); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cm-head .cm-t { float: none; margin: 0; color: var(--label2); opacity: 1; }
.cm-av {
  flex: none; width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: 15px; color: #fff; text-transform: uppercase;
  background: linear-gradient(160deg, hsl(var(--h) 75% 62%), hsl(calc(var(--h) + 30) 70% 48%));
}
.cm.cont .cm-av { visibility: hidden; height: 0; }
.cm-mute {
  flex: none; align-self: center; width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center;
  color: var(--label2); opacity: 0; transition: opacity .2s, background .2s; font-size: 16px;
}
.cm-mute svg { width: 17px; height: 17px; }
.cm.other:hover .cm-mute, .cm-mute:focus-visible { opacity: .8; }
.cm-mute:hover { background: var(--fill); opacity: 1; }
@media (hover: none) { .cm-mute { opacity: .35; } .cm.cont .cm-mute { display: none; } }
.cm.me .cm-text, .cm.other .cm-text { user-select: text; -webkit-user-select: text; }

/* «Нові повідомлення ↓» */
.chat-new {
  position: absolute; left: 50%; bottom: 112px; transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  display: flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 99px; font-size: 13px; font-weight: 600;
  background: var(--accent); color: #fff; box-shadow: 0 6px 18px rgba(0, 0, 0, .18); transition: transform .3s var(--ease), opacity .3s; z-index: 2;
}
.chat-new svg { width: 14px; height: 14px; }
.chat-new.show { transform: translate(-50%, 0); opacity: 1; pointer-events: auto; }

/* ---------- Поле вводу ---------- */
.chat-foot { flex: none; padding: 6px 10px 10px; border-top: .5px solid var(--sep); }
.chat-safety { margin: 0 4px 6px; font-size: 11.5px; color: var(--label2); text-align: center; }
.chat-compose { display: flex; align-items: center; gap: 8px; position: relative; }
.chat-input {
  flex: 1; min-width: 0; height: 44px; border: 0; outline: none; border-radius: 22px; padding: 0 64px 0 16px;
  font: inherit; font-size: 16px; /* 16px — iOS не збільшує сторінку при фокусі */
  background: var(--fill); color: var(--label); transition: box-shadow .2s;
}
.chat-input:focus { box-shadow: inset 0 0 0 2px var(--accent); }
.chat-input:disabled { opacity: .6; }
.chat-input::placeholder { color: var(--label3); }
.chat-count { position: absolute; right: 62px; font-size: 11px; color: var(--label3); font-variant-numeric: tabular-nums; pointer-events: none; }
.chat-count.warn { color: var(--orange); }
.chat-count.over { color: var(--red); }
.chat-send {
  flex: none; width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center;
  background: var(--accent); color: #fff; transition: transform .15s, opacity .2s, background .2s;
}
.chat-send svg { width: 20px; height: 20px; }
.chat-send:active:not(:disabled) { transform: scale(.92); }
.chat-send:disabled { background: var(--fill2); color: var(--label3); cursor: default; }

/* ---------- Порожній стан / налаштування ---------- */
.chat-empty { flex: 1; overflow-y: auto; padding: 22px 22px 28px; text-align: center; display: flex; flex-direction: column; align-items: center; }
.chat-empty-fox { width: 92px; margin: 4px auto 6px; }
.chat-empty-fox svg { width: 100%; height: auto; }
.chat-empty h2 { margin: 4px 0 6px; font-size: 21px; letter-spacing: -.02em; }
.chat-empty p { margin: 6px 0; color: var(--label2); font-size: 15px; line-height: 1.4; max-width: 420px; }
.chat-empty p b { color: var(--label); }
.chat-empty .btn { margin-top: 14px; }
.chat-code {
  display: inline-block; margin: 8px 0; padding: 8px 14px; border-radius: 12px; background: var(--fill);
  font-family: "SF Mono", ui-monospace, Menlo, monospace; font-size: 15px; color: var(--label); user-select: all; -webkit-user-select: all;
}
.chat-server-row { display: flex; gap: 8px; width: 100%; max-width: 440px; margin-top: 10px; }
.chat-server-row .field { flex: 1; min-width: 0; font-size: 16px; }
.chat-server-row .btn { flex: none; margin: 0; width: auto; padding-left: 18px; padding-right: 18px; }
.list .chat-server-row { max-width: none; margin-top: 0; }
.chat-muted-h { margin: 14px 16px 8px; }

/* ---------- Значок непрочитаних на пункті меню ---------- */
[data-go="chat"] { position: relative; }
.chat-badge {
  position: absolute; top: 4px; left: calc(50% + 6px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 99px;
  background: var(--red); color: #fff; font-size: 11px; font-weight: 700; line-height: 18px; text-align: center;
  box-shadow: 0 0 0 2px var(--bg2); pointer-events: none; animation: chatBadge .4s var(--ease);
}
.sidebar .nav-item .chat-badge { left: 30px; top: 4px; }
@keyframes chatBadge { from { transform: scale(0); } }

/* ---------- Телефон: картка між верхньою панеллю і нижнім таб-баром ---------- */
@media (max-width: 1100px) {
  .chat-card { --chat-h: calc(100vh - 170px - env(safe-area-inset-top)); }
  @supports (height: 100dvh) { .chat-card { --chat-h: calc(100dvh - 170px - env(safe-area-inset-top)); } }
}
@media (max-width: 700px) {
  body.chat-view .main { padding-bottom: calc(86px + env(safe-area-inset-bottom)); }
  .chat-head-row { display: none; }
  /* 10 (відступ) + ~66 (верхня панель) + ~86 (таб-бар з відступом) */
  .chat-card { min-height: 320px; border-radius: 24px;
    --chat-h: calc(100vh - 10px - 66px - 86px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  @supports (height: 100dvh) {
    .chat-card { --chat-h: calc(100dvh - 10px - 66px - 86px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  }
  .chat-top { padding: 10px 10px 6px; }
  .chat-list { padding: 8px 8px 12px; }
  .cm-bubble { max-width: 82%; }
  .cm.other .cm-body { max-width: calc(100% - 80px); }
  .cm-av { width: 28px; height: 28px; font-size: 13px; }
  .chat-new { bottom: 100px; }
  .mobile-nav .chat-badge { top: 0; left: calc(50% + 4px); }
}

/* ---------- Без анімацій ---------- */
.no-motion .chat-status::before, .no-motion .chat-badge { animation: none; }
@media (prefers-reduced-motion: reduce) { .chat-status::before, .chat-badge { animation: none; } }

/* ---------- Тема «Боброве містечко» (town): площа ---------- */
html[data-theme="town"] .chat-card { -webkit-backdrop-filter: none; backdrop-filter: none; border: 1.5px solid rgba(90, 60, 30, .18); box-shadow: 0 2px 0 rgba(90, 60, 30, .08); border-radius: 22px; }
html[data-theme="town"] .cm.other .cm-bubble { background: rgba(90, 60, 30, .06); box-shadow: inset 0 0 0 1px rgba(90, 60, 30, .12); }
html[data-theme="town"] .chat-input { background: rgba(255, 255, 255, .6); box-shadow: inset 0 0 0 1.5px rgba(90, 60, 30, .18); }
html[data-theme="town"] .chat-input:focus { box-shadow: inset 0 0 0 1.5px var(--accent); }
html[data-theme="town"] .chat-pill:not(.on) { box-shadow: inset 0 0 0 1.5px rgba(90, 60, 30, .18); }
html[data-theme="town"] .chat-pill.on { box-shadow: none; }
