:root {
  --brand: #2563eb;
  --brand-dark: #1e4fd1;
  --bg: #eef2f9;
  --surface: #ffffff;
  --ink: #1a2233;
  --muted: #6b7280;
  --bot: #ffffff;
  --user: #2563eb;
  --radius: 18px;
  --line: #e4eaf5;                              /* тонкая рамка вместо тяжёлой тени */
  --shadow: 0 6px 24px rgba(20, 40, 90, .08);   /* остаётся у шапки/панелей, не у сообщений */
  --safe-b: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* Фиксированный фрейм: прокручивается только лента, поле ввода и кнопки всегда на экране */
.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  background: var(--bg);
}

/* Шапка */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px calc(14px);
  background: linear-gradient(135deg, var(--brand), #4f7cf0);
  color: #fff;
  box-shadow: 0 2px 12px rgba(20, 40, 90, .18);
}
/* Аватар — картинка на белом круге: фон у файла белый, поэтому круг тоже белый */
.top__avatar {
  width: 44px; height: 44px; flex: 0 0 44px;
  object-fit: cover; background: #fff;
  border-radius: 50%;
  box-shadow: 0 1px 4px rgba(10, 25, 60, .18);
}
.top__text { min-width: 0; }
.top__text h1 { margin: 0; font-size: 16px; line-height: 1.25; font-weight: 650; }
.top__text p  { margin: 2px 0 0; font-size: 12.5px; opacity: .9; }

/* Телефон отдела — всегда под рукой */
.top__phone {
  margin-left: auto; flex: 0 0 auto;
  display: inline-flex; align-items: center; gap: 6px;
  padding: 8px 12px; border-radius: 999px;
  background: rgba(255,255,255,.16); color: #fff;
  font-size: 12.5px; font-weight: 600; white-space: nowrap;
  text-decoration: none;
}
.top__phone:hover { background: rgba(255,255,255,.26); }
.top__phone-ico { font-size: 14px; }
@media (max-width: 520px) {
  .top__phone-num { display: none; }
  .top__phone { padding: 9px 11px; }
  .top__phone-ico { font-size: 16px; }
}
@media (max-width: 360px) {
  .top { gap: 9px; padding: 12px 12px; }
  .top__avatar { width: 38px; height: 38px; flex-basis: 38px; font-size: 20px; }
  .top__text h1 { font-size: 14.5px; }
}

/* Лента сообщений */
.log {
  flex: 1 1 auto;
  min-height: 0;                 /* без этого flex-элемент не сжимается и лента выталкивает кнопки за экран */
  padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 10px;
  overflow-y: auto;
  overscroll-behavior: contain;
}
/* Иначе при переполнении ленты flex сжимает блоки (карточка вопросов схлопывалась в полоску) */
.log > * { flex: 0 0 auto; }
.msg {
  max-width: 88%;
  padding: 12px 15px;
  border-radius: var(--radius);
  font-size: 15px; line-height: 1.55;
  overflow-wrap: break-word;
  animation: pop .18s ease-out;
}
@media (min-width: 600px) { .msg { max-width: 78%; } }
.msg a { color: inherit; text-decoration: underline; text-underline-offset: 2px; }
.msg--bot  {
  align-self: flex-start; background: var(--bot); color: var(--ink);
  border: 1px solid var(--line); box-shadow: 0 1px 2px rgba(20, 40, 90, .05);
  border-bottom-left-radius: 6px;
}
.msg--user {
  align-self: flex-end; background: var(--user); color: #fff;
  white-space: pre-wrap; border-bottom-right-radius: 6px;
  box-shadow: 0 2px 6px rgba(37, 99, 235, .22);
}
.msg--bot a { color: var(--brand-dark); }

/* Текст ответа бота: абзацы и списки вместо сплошного pre-wrap */
.msg--bot p  { margin: 0 0 9px; }
.msg--bot p:last-child { margin-bottom: 0; }
.msg--bot ul, .msg--bot ol { margin: 0 0 9px; padding-left: 20px; }
.msg--bot ul:last-child, .msg--bot ol:last-child { margin-bottom: 0; }
.msg--bot li { margin: 0 0 4px; padding-left: 2px; }
.msg--bot li:last-child { margin-bottom: 0; }
.msg--bot ul { list-style: none; padding-left: 4px; }
.msg--bot ul li { position: relative; padding-left: 16px; }
.msg--bot ul li::before {
  content: ""; position: absolute; left: 3px; top: .62em;
  width: 5px; height: 5px; border-radius: 50%; background: var(--brand);
}
.msg--bot ol { padding-left: 22px; }
.msg--bot ol li::marker { color: var(--brand-dark); font-weight: 600; }

/* Ошибка сети/лимита — не путать с ответом бота */
.msg--error {
  align-self: stretch; max-width: 100%;
  display: flex; gap: 9px; align-items: flex-start;
  background: #fff5f5; border: 1px solid #f7c8ca; color: #9b1c20;
  font-size: 13.5px; border-radius: 12px; box-shadow: none;
}
.msg--error::before { content: "⚠"; flex: 0 0 auto; font-size: 15px; line-height: 1.4; }

.msg__links { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.msg__links a {
  display: inline-block; font-size: 13.5px; font-weight: 600;
  color: var(--brand-dark); text-decoration: none;
  padding: 7px 11px; background: #eef3ff; border-radius: 10px; width: fit-content;
}

@keyframes pop { from { transform: translateY(6px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Индикатор набора */
.typing { display: inline-flex; gap: 4px; padding: 14px 16px; }
.typing span {
  width: 7px; height: 7px; background: #b8c1d4; border-radius: 50%;
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .3; } 40% { opacity: 1; } }

/* Меню частых вопросов — список, а не «таблетки»: вопросы длинные и в пилюлях рвались */
.chips {
  align-self: stretch;
  display: flex; flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 1px 2px rgba(20, 40, 90, .05);
  overflow: hidden;
}
.chips__title {
  padding: 11px 16px 9px;
  font-size: 11.5px; font-weight: 700; letter-spacing: .6px; text-transform: uppercase;
  color: var(--muted); background: #f8fafe;
  border-bottom: 1px solid var(--line);
}
.chip {
  position: relative;
  font: inherit; font-size: 14px; line-height: 1.35; font-weight: 500;
  text-align: left;
  padding: 13px 34px 13px 16px;
  background: none; color: var(--ink);
  border: none; border-bottom: 1px solid #f0f3fa;
  cursor: pointer; transition: background .15s;
}
.chip:last-child { border-bottom: none; }
.chip::after {
  content: "›"; position: absolute; right: 14px; top: 50%; transform: translateY(-52%);
  font-size: 20px; line-height: 1; color: #a9b6d0;
}
.chip:hover { background: #f5f8ff; }
.chip:hover::after { color: var(--brand); }
.chip:active { background: #eef3ff; }

/* Поле ввода */
.composer {
  position: sticky; bottom: 0;
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px 9px;
  background: var(--surface);
  border-top: 1px solid var(--line);
}
.composer__input {
  flex: 1 1 auto; resize: none;
  font: inherit; font-size: 15px; line-height: 1.4;
  max-height: 120px;
  padding: 11px 14px;
  border: 1px solid #d6dced; border-radius: 22px;
  background: #f7f9fd; color: var(--ink);
  outline: none;
}
.composer__input:focus { border-color: var(--brand); background: #fff; }
.composer__send {
  flex: 0 0 44px; width: 44px; height: 44px;
  border: none; border-radius: 50%;
  background: var(--brand); color: #fff; font-size: 18px;
  cursor: pointer; transition: background .15s, transform .05s;
}
.composer__send:hover { background: var(--brand-dark); }
.composer__send:active { transform: scale(.94); }
.composer__send:disabled { opacity: .5; cursor: default; }

/* Нижняя строка действий: документы + контакты в один ряд, а не две широкие полосы */
.actions {
  display: flex; gap: 8px;
  padding: 0 12px calc(10px + var(--safe-b));
  background: var(--surface);
}
.actions__btn {
  flex: 1 1 0; min-width: 0;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 11px 8px;
  font: inherit; font-size: 13px; font-weight: 600; line-height: 1.25;
  text-wrap: balance;
  border-radius: 12px; cursor: pointer;
  transition: background .15s, filter .15s, transform .05s;
}
.actions__btn:active { transform: scale(.985); }
.actions__btn--docs {
  color: #fff; border: 1px solid transparent;
  background: linear-gradient(135deg, #0ea472, #0b8a60);
  box-shadow: 0 2px 6px rgba(11, 138, 96, .22);
}
.actions__btn--docs:hover { filter: brightness(1.06); }
.actions__btn--lead {
  color: var(--brand-dark); background: #fff;
  border: 1px solid #d6e0f5;
}
.actions__btn--lead:hover { background: #f2f6ff; }

/* Список документов внутри сообщения бота */
.docs { margin-top: 10px; display: flex; flex-direction: column; gap: 8px; }
.msg--bot a.docs__item {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 12px;
  background: #f1f8f4; border: 1px solid #cdeadd; border-radius: 12px;
  text-decoration: none; color: var(--ink);
  transition: background .15s;
}
.msg--bot a.docs__item:hover { background: #e4f4eb; }
.docs__icon { font-size: 20px; flex: 0 0 auto; }
.docs__meta { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1 1 auto; }
.docs__title { font-size: 13.5px; font-weight: 600; line-height: 1.35; }
.docs__sub { font-size: 11.5px; color: var(--muted); }
.docs__dl { flex: 0 0 auto; font-size: 18px; color: #0b8a60; }

/* Модалка */
.modal {
  position: fixed; inset: 0; z-index: 20;
  display: grid; place-items: end center;
  background: rgba(15, 25, 50, .45);
  animation: fade .15s ease-out;
}
.modal[hidden] { display: none; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal__card {
  width: 100%; max-width: 640px;
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  padding: 22px 20px calc(24px + var(--safe-b));
  box-shadow: 0 -8px 40px rgba(15, 25, 50, .25);
  animation: slideup .22s ease-out;
  max-height: 92dvh; overflow-y: auto;
}
@keyframes slideup { from { transform: translateY(30px); } to { transform: none; } }
.modal__close {
  position: absolute; top: 14px; right: 16px;
  border: none; background: none; font-size: 18px; color: var(--muted);
  cursor: pointer;
}
.modal__card h2 { margin: 0 0 4px; font-size: 18px; }
.modal__hint { margin: 0 0 16px; font-size: 13.5px; color: var(--muted); }

.modal__card label { display: block; margin-bottom: 14px; font-size: 13.5px; font-weight: 600; color: #38414f; }
.modal__card input[type=text],
.modal__card input[type=tel],
.modal__card textarea {
  width: 100%; margin-top: 6px;
  font: inherit; font-size: 15px; font-weight: 400;
  padding: 11px 13px;
  border: 1px solid #d6dced; border-radius: 12px; background: #f7f9fd;
  outline: none; resize: vertical;
}
.modal__card input:focus, .modal__card textarea:focus { border-color: var(--brand); background: #fff; }
.req { color: #e5484d; }

.consent { display: flex !important; gap: 10px; align-items: flex-start; font-weight: 400 !important; font-size: 12.5px !important; color: var(--muted) !important; }
.consent input { margin-top: 2px; flex: 0 0 auto; width: 18px; height: 18px; accent-color: var(--brand); }

.btn-primary {
  width: 100%; margin-top: 4px;
  font: inherit; font-size: 15px; font-weight: 650;
  padding: 13px; border: none; border-radius: 12px;
  background: var(--brand); color: #fff; cursor: pointer;
  transition: background .15s;
}
.btn-primary:hover { background: var(--brand-dark); }
.btn-primary:disabled { opacity: .6; cursor: default; }

.lead-status { margin: 12px 0 0; font-size: 13.5px; text-align: center; min-height: 18px; }
.lead-status.ok  { color: #16794c; }
.lead-status.err { color: #e5484d; }

/* Видимый фокус с клавиатуры (мышью рамка не появляется) */
:where(.chip, .actions__btn, .composer__send, .top__phone, .modal__close, .btn-primary,
       .msg a, .docs__item):focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 8px;
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; animation-iteration-count: 1 !important;
      transition-duration: .001ms !important; scroll-behavior: auto !important; }
}
