/* ==========================================================================
   Чат с консультантом (template-parts/chat.php, assets/js/chat.js).
   Подключается только при включённом чате. Отступ от края экрана — --corner,
   как у плашки cookie; над плашкой кнопку поднимает --chat-lift (ставит chat.js).
   ========================================================================== */

.chat {
	--chat-btn: 52px;
	--chat-gap: 12px;
	position: fixed; right: var(--corner); bottom: calc(var(--corner) + var(--chat-lift, 0px));
	z-index: 55; font-size: 15px; line-height: 1.45;
}
.chat.is-lifted { bottom: calc(var(--corner) + var(--chat-lift) + var(--chat-gap)); }
.chat[hidden], .chat [hidden] { display: none !important; }
.chat__sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* --- Кнопка: капсула, текст слева, круглый значок справа --------------------
   Рабочее время (.is-on): значок переливается зелёным (палитра --c-green), по первой строке
   изредка проходит блик. Вечер и выходные (.is-soft): тот же зелёный, вдвое медленнее, без блика.
   Ночь (.is-off): значок серо-зелёный, без движения. */
.chat {
	--chat-green-dark: color-mix(in srgb, var(--c-green), black 35%);
	--chat-green-light: color-mix(in srgb, var(--c-green), white 35%);
}
.chat__launcher {
	position: relative; display: inline-flex; align-items: center; gap: 12px;
	height: var(--chat-btn); padding: 0 6px 0 20px; border: 0; border-radius: 999px;
	background: var(--paper); color: var(--fg); font: inherit; text-align: left; cursor: pointer;
	box-shadow: 0 8px 32px rgb(0 0 0 / .14);
	transition: opacity .2s, transform .2s, padding .3s, gap .3s;
}
.chat__launcher:hover { transform: translateY(-1px); }
.chat__label {
	display: flex; flex-direction: column; max-width: 260px; overflow: hidden; white-space: nowrap; line-height: 1.15;
	transition: max-width .3s, opacity .2s;
}
.chat__line { font-size: 15px; font-weight: 600; }
.chat__sub { margin-top: 2px; font-size: 12px; color: var(--fg-muted); }
.chat__sub:empty { display: none; }
.chat.is-on .chat__line {
	background: linear-gradient(100deg, var(--fg) 40%, var(--chat-green-light) 50%, var(--fg) 60%) 100% 0 / 300% 100%;
	-webkit-background-clip: text; background-clip: text; color: transparent;
	animation: chat-shine 9s ease-in-out infinite;
}
.chat__knob {
	position: relative; flex: none; display: grid; place-items: center;
	width: calc(var(--chat-btn) - 12px); height: calc(var(--chat-btn) - 12px); border-radius: 50%; color: #fff;
	background: linear-gradient(135deg, color-mix(in srgb, var(--fg-muted) 75%, var(--c-green)), color-mix(in srgb, var(--fg-muted) 45%, var(--paper)));
}
.chat.is-on .chat__knob, .chat.is-soft .chat__knob {
	background: linear-gradient(135deg, var(--chat-green-dark), var(--c-green), var(--chat-green-light), var(--c-green), var(--chat-green-dark)) 0 50% / 300% 300%;
	animation: chat-flow 8s ease-in-out infinite;
}
.chat.is-soft .chat__knob { animation-duration: 16s; }
.chat__knob svg { width: 22px; height: 22px; }
/* Точка «в сети» — светло-зелёная; красная — пришёл ответ, пока окно закрыто (поверх). */
.chat.is-on .chat__knob::after, .chat.is-soft .chat__knob::after {
	content: ''; position: absolute; top: 0; right: 0; width: 10px; height: 10px; border-radius: 50%;
	background: color-mix(in srgb, var(--c-green), white 55%); box-shadow: 0 0 0 2px var(--paper);
}
.chat__dot {
	position: absolute; top: -1px; right: -1px; z-index: 1; width: 12px; height: 12px; border-radius: 50%;
	background: var(--accent); box-shadow: 0 0 0 2px var(--paper);
}
@keyframes chat-flow { 0%, 100% { background-position: 0 50%; } 50% { background-position: 100% 50%; } }
@keyframes chat-shine { 0%, 75% { background-position: 100% 0; } 100% { background-position: 0 0; } }
@media (prefers-reduced-motion: reduce) {
	.chat .chat__knob, .chat .chat__line { animation: none; }
}
/* Форма заявки на экране — кнопка уходит, пока окно закрыто. */
.chat.is-tucked:not(.is-open) .chat__launcher { opacity: 0; transform: translateY(8px); pointer-events: none; }

/* --- Окно ------------------------------------------------------------------ */
.chat__panel {
	position: absolute; right: 0; bottom: calc(var(--chat-btn) + var(--chat-gap));
	display: flex; flex-direction: column;
	/* Высота — до 520px, но не выше шапки сайта (88px) с запасом. */
	width: 360px; height: min(520px, calc(100dvh - 200px - var(--chat-lift, 0px)));
	border-radius: var(--radius-sm); background: var(--paper); color: var(--fg);
	box-shadow: 0 8px 32px rgb(0 0 0 / .14); overflow: hidden;
}
.chat__panel.is-drop { outline: 2px dashed var(--accent); outline-offset: -6px; }
.chat__head { display: flex; align-items: center; gap: 8px; padding: 10px 10px 10px 18px; border-bottom: 1px solid var(--rule); }
.chat__title { flex: 1; margin: 0; font-family: var(--font-head); font-weight: 600; font-size: 17px; }
.chat__close {
	display: grid; place-items: center; width: 36px; height: 36px; padding: 0; border: 0; border-radius: 50%;
	background: none; color: var(--fg-muted); cursor: pointer;
}
.chat__close:hover { background: var(--inset); color: var(--fg); }
.chat__close svg { width: 20px; height: 20px; }

/* --- Переписка ------------------------------------------------------------- */
.chat__log {
	flex: 1; overflow-y: auto; overscroll-behavior: contain;
	display: flex; flex-direction: column; gap: 8px; padding: 16px; background: var(--inset);
}
.chat__msg { max-width: 85%; padding: 8px 12px; border-radius: 14px; background: var(--paper); overflow-wrap: anywhere; }
.chat__msg p { margin: 0; white-space: pre-wrap; }
.chat__msg a { color: var(--accent); }
.chat__msg--op { align-self: flex-start; border-bottom-left-radius: 4px; }
/* Свои сообщения — пастельный зелёный из палитры (--t-4): алый читался как «не доставлено». */
.chat__msg--me { align-self: flex-end; border-bottom-right-radius: 4px; background: var(--t-4); }
.chat__msg--sys { align-self: center; max-width: 92%; padding: 2px 8px; background: none; color: var(--fg-muted); font-size: 13px; text-align: center; }
.chat__when { display: block; margin-top: 2px; font-size: 11px; color: var(--fg-muted); }
.chat__msg--me .chat__when { text-align: right; }
.chat__img { display: block; }
.chat__img img { display: block; max-width: 100%; max-height: 220px; min-width: 60px; min-height: 40px; border-radius: 10px; background: var(--inset); }
.chat__file {
	display: flex; flex-direction: column; align-items: flex-start; gap: 2px; padding: 0; border: 0;
	background: none; color: var(--fg); font: inherit; text-align: left; cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}
.chat__file span { font-size: 12px; color: var(--fg-muted); text-decoration: none; }

/* --- Поле ввода ------------------------------------------------------------ */
.chat__form { display: flex; flex-direction: column; gap: 8px; padding: 10px 12px 12px; border-top: 1px solid var(--rule); }
.chat__consent { margin: 0; font-size: 13px; }
.chat__row { display: flex; align-items: flex-end; gap: 6px; }
.chat__row textarea {
	flex: 1; min-width: 0; min-height: 44px; max-height: 140px; resize: none;
	padding: 10px 12px; border: 1px solid var(--rule); border-radius: 12px; background: var(--inset);
	font: inherit; font-size: 16px; line-height: 1.4; color: var(--fg);
}
.chat__row textarea::placeholder { color: var(--placeholder); }
.chat__row textarea:focus { outline: 2px solid var(--focus); outline-offset: 1px; }
.chat__attach, .chat__send {
	flex: none; display: grid; place-items: center; width: 44px; height: 44px; border: 0; border-radius: 12px; cursor: pointer;
}
.chat__attach { position: relative; color: var(--fg-muted); }
.chat__attach:hover { background: var(--inset); color: var(--fg); }
.chat__attach input { position: absolute; inset: 0; opacity: 0; width: 100%; cursor: pointer; }
.chat__attach:focus-within { outline: 2px solid var(--focus); outline-offset: 1px; }
.chat__attach svg, .chat__send svg { width: 22px; height: 22px; }
.chat__send { background: var(--accent); color: #fff; }
.chat__send:hover { background: var(--accent-dark); }
.chat__send[disabled] { opacity: .5; cursor: default; }
.chat.is-busy .chat__send { opacity: .5; }
.chat__files { display: flex; flex-wrap: wrap; gap: 6px; }
.chat__chip { display: inline-flex; align-items: center; gap: 6px; max-width: 100%; padding: 3px 4px 3px 3px; border-radius: 999px; background: var(--inset); font-size: 13px; }
.chat__chip img { width: 26px; height: 26px; border-radius: 50%; object-fit: cover; }
.chat__chip-name { max-width: 150px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; padding-left: 6px; }
.chat__chip img + .chat__chip-name { padding-left: 0; }
.chat__chip-x { width: 22px; height: 22px; padding: 0; border: 0; border-radius: 50%; background: none; color: var(--fg-muted); font-size: 16px; line-height: 1; cursor: pointer; }
.chat__chip-x:hover { background: var(--paper); color: var(--fg); }
.chat__error { margin: 0; font-size: 13px; color: var(--accent-dark); }

/* --- Телефон: окно выезжает снизу во всю ширину, над клавиатурой ------------ */
@media (max-width: 720px) {
	.chat__panel {
		position: fixed; left: 0; right: 0; bottom: var(--vv-b, 0px);
		width: auto; height: min(75svh, calc(var(--vv-h, 100svh) - var(--corner)));
		border-radius: var(--radius-sm) var(--radius-sm) 0 0;
		box-shadow: 0 -8px 32px rgb(0 0 0 / .18);
	}
	.chat.is-open .chat__launcher { display: none; }
	/* Телефон: обе строки, как на компьютере; начали листать — капсула сворачивается в кружок, вверху — снова капсула.
	   Пока видна плашка cookie, тоже кружок: капсула над плашкой закрывала бы кнопку телефона. */
	.chat.is-mini .chat__launcher, .chat.is-lifted .chat__launcher { gap: 0; padding: 0 6px; }
	.chat.is-mini .chat__label, .chat.is-lifted .chat__label { max-width: 0; opacity: 0; }
	.chat__log { padding: 12px; }
}
