:root {
  --brand: #cb0101;
  --brand-deep: #a30000;
  --on-brand: #fff;
  --chat-bg: #f3f3f4;
  --stage: #e4e4e6;
  --ink: #16181b;
  --muted: #6b6f76;
  --line: #ececee;
  --panel-font: "Onest", system-ui, sans-serif;
  --chat-font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

/* Демо — как приложение: страница не прокручивается и не тянется, прокручивается только лента чата */
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }

body {
  background: var(--stage);
  color: var(--ink);
  font-family: var(--panel-font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

button { font: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 3px solid color-mix(in srgb, var(--brand) 55%, white); outline-offset: 2px; }

/* Сцена: телефон и карточка рядом */

.stage {
  height: 100dvh;
  overflow: hidden;
  display: grid;
  grid-template-columns: 390px 360px;
  justify-content: center;
  align-items: center;
  gap: 56px;
  padding: 32px 24px;
}

.device-column { display: flex; flex-direction: column; align-items: center; gap: 14px; }

.hint { margin: 0; font-size: 15px; color: #3a3d42; }

.device {
  position: relative;
  width: 390px;
  height: min(844px, calc(100dvh - 110px));
  border: 10px solid #1d1d1f;
  border-radius: 52px;
  overflow: hidden;
  background: #000;
  box-shadow: 0 30px 60px -20px rgba(20, 22, 26, 0.35);
}

.screen { position: absolute; inset: 0; }
.screen[hidden] { display: none; }

/* Экран рекламы */

.ad-screen { display: flex; align-items: center; background: #000; overflow: hidden; touch-action: none; user-select: none; cursor: grab; }
.ad-screen img { pointer-events: none; -webkit-user-drag: none; }
.ad-frame { position: relative; width: 100%; }
.ad-frame img { display: block; width: 100%; height: auto; }

.ad-cta {
  position: absolute;
  border: 0;
  border-radius: 14px;
  background: transparent;
  animation: cta-pulse 2.2s ease-out infinite;
}

@keyframes cta-pulse {
  0% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.75); }
  70% { box-shadow: 0 0 0 14px rgba(255, 255, 255, 0); }
  100% { box-shadow: 0 0 0 0 rgba(255, 255, 255, 0); }
}

/* Чат */

/* Чат — шторка: выезжает по нажатию или за пальцем при свайпе вверх */
.chat-screen {
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  font-family: var(--chat-font);
  transform: translateY(100%);
  transition: transform 340ms cubic-bezier(0.2, 0.8, 0.2, 1);
  box-shadow: 0 -12px 30px -18px rgba(0, 0, 0, 0.45);
}
.chat-screen.open { transform: translateY(0); }
.chat-screen.dragging { transition: none; }

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 14px 12px 6px;
  background: #fff;
  border-bottom: 1px solid var(--line);
}

.icon-btn {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  border: 0; border-radius: 50%;
  background: transparent; color: var(--ink);
}
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }

.avatar { width: 40px; height: 40px; border-radius: 50%; flex: none; }

.who { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.who strong { font-size: 15.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.online { font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.online::before { content: ""; display: inline-block; width: 7px; height: 7px; margin-right: 6px; border-radius: 50%; background: #1fb85b; vertical-align: 1px; }

.card-toggle { display: none; }

.feed {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.bubble {
  max-width: 80%;
  padding: 9px 13px 7px;
  font-size: 15px;
  line-height: 1.4;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.bubble time { display: block; margin-top: 2px; font-size: 11px; opacity: 0.55; text-align: right; }

.bubble.bot { align-self: flex-start; background: #fff; color: var(--ink); border-radius: 18px 18px 18px 6px; }
.bubble.me { align-self: flex-end; background: var(--brand); color: var(--on-brand); border-radius: 18px 18px 6px 18px; }
.bubble.me + .bubble.bot, .bubble.bot + .bubble.me { margin-top: 8px; }

.note { align-self: center; font-size: 12.5px; color: var(--muted); padding: 6px 10px; text-align: center; }

.typing { align-self: flex-start; background: #fff; border-radius: 18px 18px 18px 6px; padding: 12px 14px; display: flex; gap: 4px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: #a9acb2; animation: typing 1.1s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-4px); opacity: 1; } }

.replies { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 12px 10px; }
.replies:empty { display: none; }
.replies button { animation: reply-in 220ms cubic-bezier(0.2, 0.8, 0.2, 1) backwards; }
.replies button:nth-child(2) { animation-delay: 40ms; }
.replies button:nth-child(3) { animation-delay: 80ms; }
.replies button:nth-child(4) { animation-delay: 120ms; }
@keyframes reply-in { from { opacity: 0; transform: translateY(8px); } }
.replies button {
  border: 1.5px solid var(--brand);
  background: #fff;
  color: var(--brand-deep);
  border-radius: 999px;
  padding: 7px 14px;
  font-family: var(--chat-font);
  font-size: 14px;
  font-weight: 500;
}

.composer { display: flex; gap: 8px; padding: 10px 12px 14px; background: #fff; border-top: 1px solid var(--line); }
.composer input {
  flex: 1;
  min-width: 0;
  border: 0;
  border-radius: 22px;
  background: #f1f1f3;
  padding: 10px 16px;
  /* 16px: при меньшем шрифте iPhone увеличивает страницу при фокусе, и всё прыгает */
  font: 16px var(--chat-font);
  color: var(--ink);
}
.composer input:focus-visible { outline: 2px solid color-mix(in srgb, var(--brand) 30%, white); outline-offset: 0; }
.send { width: 42px; height: 42px; flex: none; border: 0; border-radius: 50%; background: var(--brand); display: grid; place-items: center; }
.send svg { width: 20px; height: 20px; fill: var(--on-brand); }
.send:disabled { opacity: 0.45; cursor: default; }

/* Карточка клиента */

.lead-sheet {
  max-height: calc(100dvh - 64px);
  overflow-y: auto;
  background: #fff;
  border-radius: 24px;
  padding: 28px 28px 24px;
  box-shadow: 0 20px 40px -24px rgba(20, 22, 26, 0.3);
}

.lead-head h2 { margin: 0; font-size: 21px; line-height: 1.25; font-weight: 600; letter-spacing: -0.01em; }
.close-sheet { display: none; }

.lead-status { display: flex; flex-wrap: wrap; gap: 8px; margin: 16px 0 6px; }
.chip { font-size: 13px; font-weight: 500; padding: 5px 11px; border-radius: 999px; background: #f1f2f4; color: #3a3d42; }
.chip[hidden] { display: none; }
.warmth.cold { background: #e6eefa; color: #2f5e9e; }
.warmth.warm { background: #fff1d6; color: #8a5a00; }
.warmth.hot { background: color-mix(in srgb, var(--brand) 14%, white); color: var(--brand-deep); }

.lead-fields { margin: 0; display: grid; grid-template-columns: 108px 1fr; }
.lead-fields dt, .lead-fields dd { margin: 0; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 14.5px; line-height: 1.35; }
.lead-fields dt { color: var(--muted); padding-right: 12px; }
.lead-fields dd { color: var(--ink); }
.lead-fields dd.empty { color: #b4b7bc; }
.lead-fields dd.fresh { animation: fresh 1.8s ease-out; }
@keyframes fresh {
  0% { background: color-mix(in srgb, var(--brand) 18%, white); }
  100% { background: transparent; }
}

.handoff { margin-top: 16px; padding: 12px 14px; border-radius: 14px; background: #fff7e6; }
.handoff[hidden] { display: none; }
.handoff strong { font-size: 13.5px; font-weight: 600; color: #8a5a00; }
.handoff p { margin: 4px 0 0; font-size: 14.5px; line-height: 1.35; }

.lead-note { margin: 18px 0 0; font-size: 13.5px; line-height: 1.45; color: var(--muted); }
.credit { margin: 10px 0 0; font-size: 12px; line-height: 1.45; color: #9a9da3; }

/* Телефон: всё во весь экран, карточка шторкой */

@media (max-width: 860px) {
  /* Телефон закреплён на месте; высота идёт из visualViewport, чтобы поле ввода плавно вставало над клавиатурой.
     Сдвигать его нельзя: сдвиг удлиняет страницу, и Safari начинает её прокручивать */
  body { position: fixed; inset: 0; }
  .stage { display: block; padding: 0; height: 100%; }
  .hint { display: none; }
  .device {
    width: 100vw;
    height: var(--app-height, 100dvh);
    border: 0;
    border-radius: 0;
    box-shadow: none;
    transition: height 260ms cubic-bezier(0.2, 0.8, 0.2, 1);
  }
  .keyboard-open .composer { padding-bottom: 10px; }
  /* Новый Safari рисует над клавиатурой пилюлю адреса и панель со стрелками, сквозь них видно фон страницы:
     под рекламой он чёрный, в чате белый, в цвет поля ввода, чтобы зона над клавиатурой сливалась с полем */
  html, body { background: #000; }
  html.chat-open, html.chat-open body { background: #fff; }
  .card-toggle {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border: 0;
    border-radius: 999px;
    padding: 7px 12px;
    background: color-mix(in srgb, var(--brand) 10%, white);
    color: var(--brand-deep);
    font-size: 13px;
    font-weight: 600;
    font-family: var(--chat-font);
  }
  .card-toggle.updated::after { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--brand); }
  .lead-panel {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: rgba(16, 18, 22, 0.4);
    display: flex;
    align-items: flex-end;
    visibility: hidden;
    opacity: 0;
    transition: opacity 200ms, visibility 200ms;
  }
  .lead-panel.open { visibility: visible; opacity: 1; }
  .lead-sheet {
    width: 100%;
    max-height: 88dvh;
    overflow-y: auto;
    border-radius: 22px 22px 0 0;
    padding: 22px 20px calc(20px + env(safe-area-inset-bottom));
    transform: translateY(24px);
    transition: transform 220ms cubic-bezier(0.2, 0.8, 0.2, 1);
  }
  .lead-panel.open .lead-sheet { transform: none; }
  .lead-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
  .lead-head h2 { font-size: 19px; }
  .close-sheet { display: block; border: 0; background: #f1f2f4; border-radius: 999px; padding: 6px 12px; font-size: 13.5px; }
  .chat-head { padding-top: calc(12px + env(safe-area-inset-top)); }
  .composer { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}

@media (prefers-reduced-motion: reduce) {
  .ad-cta, .typing span, .lead-fields dd.fresh, .replies button { animation: none; }
  .chat-screen, .device, .lead-panel, .lead-sheet { transition: none; }
}
