/* ============================================================
   hemstitch-core — оформление компонентов плагина.
   Разметка — строго по контракту (форма, калькулятор, чат).
   Токены берём из темы через var(--hs-*, fallback): без темы
   компонент выглядит так же, с темой — наследует палитру.
   Mobile-first. Логику JS не меняем.
   ============================================================ */

/* --- 00. Локальные алиасы токенов с фолбэками (одна точка) --- */
.hs-form,
.hs-calc,
#hs-chat,
#hs-chat-toggle,
.hs-open-chat {
	--_bg: var(--hs-bg, #FAF6F0);
	--_surface: var(--hs-surface, #FFFDF9);
	--_ink: var(--hs-ink, #2E2A25);
	--_muted: var(--hs-muted, #6F675C);
	--_accent: var(--hs-accent, #A98B5F);
	--_accent-ink: var(--hs-accent-ink, #7A6238);
	--_soft: var(--hs-accent-soft, #EFE6D8);
	--_line: var(--hs-line, #E7DFD3);
	--_ok: var(--hs-ok, #3E6B34);
	--_ok-bg: var(--hs-ok-bg, #EAF0E4);
	--_err: var(--hs-err, #A03D22);
	--_err-bg: var(--hs-err-bg, #F7E8E2);
	--_radius: var(--hs-radius, 14px);
	--_radius-sm: var(--hs-radius-sm, 10px);
	--_shadow: var(--hs-shadow, 0 10px 30px rgba(46, 42, 37, .08));
	--_shadow-lg: var(--hs-shadow-lg, 0 18px 50px rgba(46, 42, 37, .16));
	--_font-body: var(--hs-font-body, -apple-system, "Segoe UI", Roboto, sans-serif);
	--_font-display: var(--hs-font-display, Georgia, "Times New Roman", serif);
	font-family: var(--_font-body);
	color: var(--_ink);
}

/* --- 01. Форма заявки: .hs-form / .hs-row / .hs-two --- */
.hs-form { max-width: 640px; }
.hs-row { margin: 0 0 18px; }
.hs-row > label { display: block; font-weight: 600; font-size: .95rem; }

.hs-form input[type="text"],
.hs-form input[type="number"],
.hs-form select,
.hs-form textarea {
	display: block; width: 100%; margin-top: 6px; padding: 12px 14px;
	font: inherit; font-weight: 400; color: var(--_ink);
	background: var(--_surface); border: 1px solid var(--_line);
	border-radius: var(--_radius-sm);
	transition: border-color .15s ease, box-shadow .15s ease;
}
.hs-form input:focus,
.hs-form select:focus,
.hs-form textarea:focus {
	outline: none; border-color: var(--_accent-ink);
	box-shadow: 0 0 0 3px var(--_soft);
}
/* Прогрессивное улучшение: подсветка невалидного после взаимодействия, без JS */
.hs-form input:user-invalid,
.hs-form select:user-invalid,
.hs-form textarea:user-invalid { border-color: var(--_err); }

.hs-form select {
	appearance: none; padding-right: 40px;
	/* Шеврон — inline-SVG в data URI, внешних запросов нет */
	background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%236F675C' stroke-width='1.6' stroke-linecap='round'/%3E%3C/svg%3E");
	background-repeat: no-repeat; background-position: right 14px center;
}
.hs-form textarea { resize: vertical; min-height: 110px; }

.hs-form input[type="file"] {
	display: block; width: 100%; margin-top: 6px; padding: 10px;
	font: inherit; font-size: .9rem; font-weight: 400; color: var(--_muted);
	background: var(--_surface); border: 1px dashed var(--_line);
	border-radius: var(--_radius-sm);
}
.hs-form input[type="file"]::file-selector-button {
	margin-right: 12px; padding: 8px 14px; cursor: pointer;
	font: inherit; font-size: .85rem; font-weight: 600;
	color: var(--_accent-ink); background: var(--_soft);
	border: 1px dashed var(--_accent); border-radius: 8px;
}

/* Две колонки — только на планшете и шире; на мобильном всё в столбик */
.hs-two > label + label { margin-top: 18px; }
@media (min-width: 600px) {
	.hs-row.hs-two { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
	.hs-two > label + label { margin-top: 0; }
}

.hs-hint { display: block; margin-top: 6px; font-size: .85rem; font-weight: 400; color: var(--_muted); }

/* Предварительная стоимость — «бирка» */
.hs-total {
	margin: 4px 0 18px; padding: 14px 16px;
	background: var(--_soft); border: 1px dashed var(--_accent);
	border-radius: var(--_radius-sm);
}
#hs-total { font-family: var(--_font-display); font-weight: 400; font-size: 1.5rem; }
#hs-total-note { display: inline; margin-left: 8px; }

/* Honeypot: визуально скрыт, но остаётся в DOM и доступен ботам */
.hs-hp { position: absolute; left: -100vw; top: auto; width: 1px; height: 1px; overflow: hidden; }

.hs-consent {
	display: flex; gap: 10px; align-items: flex-start;
	margin: 0 0 18px; font-size: .9rem; font-weight: 400;
	color: var(--_muted); cursor: pointer;
}
.hs-consent input { flex: none; width: 18px; height: 18px; margin-top: 2px; accent-color: var(--_accent-ink); }

/* --- 02. Кнопки: .hs-btn (submit) и .hs-btn.hs-open-chat --- */
.hs-btn {
	position: relative; display: inline-flex; align-items: center; justify-content: center; gap: 8px;
	padding: 14px 28px; border: 0; border-radius: var(--_radius-sm);
	background: var(--_ink); color: #FFFDF9;
	font: inherit; font-weight: 600; font-size: 1rem; letter-spacing: .02em;
	text-decoration: none; cursor: pointer; box-shadow: var(--_shadow);
	transition: transform .18s ease, box-shadow .18s ease, background-color .18s ease;
}
/* Декоративная «строчка» по периметру — приём CSS, не намёк на технологию */
.hs-btn::after {
	content: ""; position: absolute; inset: 5px; pointer-events: none;
	border: 1px dashed rgba(255, 253, 249, .45); border-radius: 6px;
}
.hs-btn:hover { background: #3A352E; transform: translateY(-2px); box-shadow: var(--_shadow-lg); }
.hs-btn:active { transform: translateY(0); }
.hs-btn[disabled] { opacity: .6; cursor: wait; transform: none; }

.hs-btn.hs-open-chat { background: transparent; color: var(--_ink); border: 1px solid var(--_ink); box-shadow: none; }
.hs-btn.hs-open-chat::after { border-color: var(--_line); }
.hs-btn.hs-open-chat:hover { background: var(--_soft); }

/* --- 03. Результат отправки: #hs-result.hs-ok / .hs-err --- */
.hs-result { margin-top: 14px; font-size: .95rem; }
.hs-result:empty { display: none; } /* пустой — без рамки и отступов */
.hs-result.hs-ok,
.hs-result.hs-err { padding: 12px 16px; border: 1px solid; border-radius: var(--_radius-sm); }
.hs-result.hs-ok { background: var(--_ok-bg); color: var(--_ok); border-color: #B9CBAF; }
.hs-result.hs-err { background: var(--_err-bg); color: var(--_err); border-color: #E0B4A4; }

/* --- 04. Калькулятор: .hs-calc --- */
.hs-calc {
	max-width: 440px; padding: 20px;
	background: var(--_surface); border: 1px solid var(--_line);
	border-radius: var(--_radius); box-shadow: var(--_shadow);
}
.hs-calc label { font-weight: 600; font-size: .95rem; }
.hs-calc input {
	display: inline-block; width: 7.5em; margin-left: 10px; padding: 10px 12px;
	font: inherit; font-weight: 400; color: var(--_ink);
	background: var(--_bg); border: 1px solid var(--_line); border-radius: var(--_radius-sm);
}
.hs-calc input:focus { outline: none; border-color: var(--_accent-ink); box-shadow: 0 0 0 3px var(--_soft); }
.hs-calc p { margin: 14px 0 0; }
#hs-calc-total { font-family: var(--_font-display); font-weight: 400; font-size: 1.8rem; }
#hs-calc-note { display: block; margin-top: 4px; }

/* --- 05. Чат: кнопка-«пуговка» #hs-chat-toggle --- */
#hs-chat-toggle {
	position: fixed; right: 12px; bottom: calc(12px + env(safe-area-inset-bottom, 0px)); z-index: 9990;
	display: inline-flex; align-items: center; gap: 10px;
	padding: 12px 18px; border: 0; border-radius: 999px;
	background: var(--_ink); color: #FFFDF9;
	font: inherit; font-weight: 600; font-size: .95rem; cursor: pointer;
	box-shadow: 0 12px 30px rgba(46, 42, 37, .28);
	transition: transform .18s ease, box-shadow .18s ease;
}
#hs-chat-toggle:hover { transform: translateY(-2px); box-shadow: 0 18px 40px rgba(46, 42, 37, .34); }
#hs-chat-toggle svg { width: 20px; height: 20px; flex: none; } /* если применён SVG-патч */

/* --- 06. Чат: окно #hs-chat --- */
#hs-chat {
	position: fixed; right: 12px; bottom: calc(76px + env(safe-area-inset-bottom, 0px)); z-index: 9991;
	width: calc(100vw - 24px); height: min(560px, 70vh);
	display: flex; flex-direction: column; overflow: hidden;
	background: var(--_surface); border: 1px solid var(--_line);
	border-radius: 18px; box-shadow: 0 24px 60px rgba(46, 42, 37, .24);
}
/* КРИТИЧНО: атрибут hidden должен побеждать display:flex, иначе чат всегда открыт */
#hs-chat[hidden] { display: none; }
#hs-chat:not([hidden]) { animation: hs-pop .22s ease; }
@keyframes hs-pop {
	from { opacity: 0; transform: translateY(10px) scale(.98); }
	to   { opacity: 1; transform: none; }
}

#hs-chat header {
	display: flex; align-items: center; justify-content: space-between; gap: 12px;
	padding: 16px 18px 14px; background: var(--_surface);
	border-bottom: 1px dashed var(--_accent);
	font-family: var(--_font-display); font-size: 1.15rem; color: var(--_ink);
}
#hs-chat-close {
	flex: none; width: 34px; height: 34px;
	border: 1px solid var(--_line); border-radius: 50%;
	background: transparent; color: var(--_muted);
	font-size: 1.2rem; line-height: 1; cursor: pointer;
	transition: color .15s ease, border-color .15s ease;
}
#hs-chat-close:hover { color: var(--_ink); border-color: var(--_accent-ink); }

#hs-chat-log {
	flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 10px;
	padding: 16px; background: var(--_bg);
}
.hs-msg {
	max-width: 84%; padding: 10px 14px; border-radius: 14px;
	font-size: .95rem; line-height: 1.5;
	white-space: pre-wrap; overflow-wrap: anywhere; /* длинные строки и XSS-текст не рвут ленту */
}
.hs-msg.hs-user { align-self: flex-end; background: var(--_ink); color: #FFFDF9; border-bottom-right-radius: 4px; }
.hs-msg.hs-bot { align-self: flex-start; background: var(--_surface); color: var(--_ink); border: 1px solid var(--_line); border-bottom-left-radius: 4px; }
/* «Печатает» — тот же .hs-bot с текстом «…», отдельного класса нет: оформлен как обычный ответ */

#hs-chat-form { display: flex; gap: 8px; padding: 12px; background: var(--_surface); border-top: 1px dashed var(--_line); }
#hs-chat-text {
	flex: 1; min-width: 0; padding: 12px 16px;
	font: inherit; font-size: .95rem; color: var(--_ink);
	background: var(--_bg); border: 1px solid var(--_line); border-radius: 999px;
}
#hs-chat-text:focus { outline: none; border-color: var(--_accent-ink); box-shadow: 0 0 0 3px var(--_soft); }
#hs-chat-form button[type="submit"] {
	flex: none; width: 44px; height: 44px; border: 0; border-radius: 50%;
	background: var(--_accent-ink); color: #FFFDF9; font-size: 1rem; cursor: pointer;
	transition: filter .15s ease, transform .15s ease;
}
#hs-chat-form button[type="submit"]:hover { filter: brightness(1.1); transform: scale(1.05); }

#hs-chat-human.hs-link {
	display: block; width: 100%; padding: 4px 12px 12px;
	border: 0; background: var(--_surface); color: var(--_muted);
	font: inherit; font-size: .82rem; text-align: center; cursor: pointer;
	text-decoration: underline; text-decoration-style: dotted; text-underline-offset: 3px;
}
#hs-chat-human.hs-link:hover { color: var(--_accent-ink); }

/* --- 07. Фокус с клавиатуры --- */
.hs-form :focus-visible,
.hs-calc :focus-visible,
.hs-open-chat:focus-visible,
#hs-chat-toggle:focus-visible,
#hs-chat :focus-visible {
	outline: 2px solid var(--_accent-ink);
	outline-offset: 2px;
}

/* --- 08. Планшет и десктоп --- */
@media (min-width: 481px) {
	#hs-chat-toggle { right: 22px; bottom: calc(22px + env(safe-area-inset-bottom, 0px)); padding: 14px 22px; }
	#hs-chat { right: 22px; bottom: calc(88px + env(safe-area-inset-bottom, 0px)); width: min(370px, calc(100vw - 32px)); height: min(560px, 72vh); }
}

/* --- 09. Уважение к настройкам пользователя --- */
@media (prefers-reduced-motion: reduce) {
	.hs-form *, .hs-calc *, .hs-open-chat,
	#hs-chat, #hs-chat *, #hs-chat-toggle {
		animation-duration: .01ms !important;
		transition-duration: .01ms !important;
	}
}
