/* FlexKey — чат поддержки. Переписан с нуля.
   Десктоп: карточка справа внизу. Мобильный (≤720px): на весь экран; подъём над клавиатурой делает браузер
   (iPhone) или VirtualKeyboard API (Android Chrome), без JS-позиционирования. */

#fkSupportRoot {
  --fk-accent: #f46c36;
  --fk-accent-hi: #ff8a4c;
  --fk-bg: #0f0f12;
  --fk-surface: #17171c;
  --fk-surface-2: #1f1f26;
  --fk-line: #26262e;
  --fk-text: #f4f4f6;
  --fk-muted: #8e8e9a;
  --fk-ok: #3ddc84;
  --fk-err: #ff8d78;
  --fk-ease: cubic-bezier(.22, .8, .24, 1);
  position: fixed;
  right: 22px;
  bottom: 22px;
  z-index: 10000;
  font-family: var(--font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif);
  color: var(--fk-text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}
#fkSupportRoot *, #fkSupportRoot *::before, #fkSupportRoot *::after { box-sizing: border-box; }
#fkSupportRoot button, #fkSupportRoot input, #fkSupportRoot textarea { font-family: inherit; }
#fkSupportRoot [hidden] { display: none !important; }

/* ───────── Кнопка-запускалка ───────── */
.fks-launch {
  position: relative;
  width: 58px; height: 58px;
  display: grid; place-items: center;
  padding: 0; border: 0; border-radius: 50%;
  background: linear-gradient(135deg, var(--fk-accent-hi), var(--fk-accent));
  color: #fff; cursor: pointer;
  box-shadow: 0 10px 28px rgba(244, 108, 54, .38), 0 2px 6px rgba(0, 0, 0, .35);
  transition: transform .2s var(--fk-ease), box-shadow .2s var(--fk-ease);
}
.fks-launch:hover { transform: translateY(-2px); box-shadow: 0 14px 32px rgba(244, 108, 54, .46), 0 2px 6px rgba(0, 0, 0, .35); }
.fks-launch:active { transform: scale(.96); }
.fks-launch:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }
.fks-launch svg { width: 27px; height: 27px; }
.fks-badge {
  position: absolute; top: -4px; right: -4px;
  min-width: 20px; height: 20px; padding: 0 6px;
  display: none; place-items: center;
  border-radius: 10px; border: 2px solid var(--fk-bg);
  background: #fff; color: #d94712;
  font-size: 11px; font-weight: 700; line-height: 1;
}
.fks-badge.on { display: grid; }

/* ───────── Панель ───────── */
.fks-panel {
  position: absolute; right: 0; bottom: 72px;
  width: 384px; height: min(640px, calc(100vh - 112px)); min-height: 380px;
  display: flex; flex-direction: column; overflow: hidden;
  border: 1px solid var(--fk-line); border-radius: 22px;
  background: var(--fk-bg);
  box-shadow: 0 28px 70px rgba(0, 0, 0, .55), 0 2px 10px rgba(0, 0, 0, .4);
  transform-origin: 100% 100%;
  animation: fks-in .2s var(--fk-ease);
}
@keyframes fks-in { from { opacity: 0; transform: translateY(10px) scale(.97); } to { opacity: 1; transform: none; } }

/* Шапка */
.fks-head {
  flex: none; display: flex; align-items: center; gap: 12px;
  padding: 14px 14px 13px 16px;
  background: linear-gradient(180deg, #1b1b21, #141418);
  border-bottom: 1px solid var(--fk-line);
}
.fks-avatar {
  position: relative; flex: none; width: 40px; height: 40px;
  display: grid; place-items: center; border-radius: 50%;
  background: linear-gradient(135deg, var(--fk-accent-hi), var(--fk-accent)); color: #fff;
}
.fks-avatar svg { width: 21px; height: 21px; }
.fks-avatar::after {
  content: ""; position: absolute; right: -1px; bottom: -1px;
  width: 11px; height: 11px; border-radius: 50%;
  background: var(--fk-ok); border: 2px solid #17171c;
}
.fks-title { min-width: 0; flex: 1; }
.fks-title b { display: block; font-size: 16px; font-weight: 650; letter-spacing: -.01em; line-height: 1.2; }
.fks-title small { display: block; margin-top: 3px; color: var(--fk-muted); font-size: 12.5px; line-height: 1.2; }
.fks-iconbtn {
  flex: none; width: 36px; height: 36px; padding: 0;
  display: grid; place-items: center; border: 0; border-radius: 50%;
  background: transparent; color: #c9c9d1; cursor: pointer;
  transition: background .15s;
}
.fks-iconbtn:hover { background: var(--fk-surface-2); }
.fks-iconbtn:active { background: #2a2a33; }
.fks-iconbtn svg { width: 20px; height: 20px; }

.fks-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* Состояния: загрузка / ошибка */
.fks-status {
  margin: auto; padding: 28px; max-width: 280px;
  text-align: center; color: var(--fk-muted); font-size: 14px; line-height: 1.5;
}
.fks-spin {
  width: 26px; height: 26px; margin: 0 auto 12px;
  border: 3px solid var(--fk-surface-2); border-top-color: var(--fk-accent); border-radius: 50%;
  animation: fks-rot .8s linear infinite;
}
@keyframes fks-rot { to { transform: rotate(360deg); } }

/* ───────── Знакомство ───────── */
.fks-intro { flex: 1; min-height: 0; overflow-y: auto; padding: 26px 22px 22px; display: flex; flex-direction: column; gap: 14px; overscroll-behavior: contain; }
.fks-intro h3 { margin: 0; font-size: 21px; font-weight: 700; letter-spacing: -.02em; }
.fks-intro p { margin: 0 0 4px; color: #a9a9b4; font-size: 14.5px; line-height: 1.5; }
.fks-label { display: grid; gap: 6px; color: var(--fk-muted); font-size: 12.5px; }
.fks-input {
  width: 100%; height: 48px; padding: 0 14px;
  border: 1px solid var(--fk-line); border-radius: 14px;
  background: var(--fk-surface); color: var(--fk-text);
  font-size: 16px; outline: none; transition: border-color .15s, box-shadow .15s;
}
.fks-input::placeholder, .fks-field textarea::placeholder { color: #6c6c78; }
.fks-input:focus, .fks-field textarea:focus { border-color: var(--fk-accent); box-shadow: 0 0 0 3px rgba(244, 108, 54, .16); }
.fks-error { min-height: 18px; color: var(--fk-err); font-size: 13px; line-height: 1.35; }
.fks-primary {
  height: 48px; border: 0; border-radius: 14px; cursor: pointer;
  background: linear-gradient(135deg, var(--fk-accent-hi), var(--fk-accent)); color: #fff;
  font-size: 15px; font-weight: 650;
  box-shadow: 0 8px 20px rgba(244, 108, 54, .28);
  transition: transform .15s, opacity .15s;
}
.fks-primary:active { transform: scale(.985); }
.fks-primary:disabled { opacity: .55; cursor: default; }
.fks-note { margin: 0; color: #6c6c78; font-size: 12px; line-height: 1.45; }

/* ───────── Переписка ───────── */
.fks-scroll { overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.fks-messages { flex: 1; min-height: 0; padding: 14px 14px 8px; display: flex; flex-direction: column; }
.fks-messages::before { content: ""; flex: 1 0 0; } /* прижимает короткую переписку к низу */

.fks-day { align-self: center; margin: 10px 0 8px; padding: 4px 11px; border-radius: 999px; background: var(--fk-surface); color: var(--fk-muted); font-size: 11.5px; font-weight: 500; }
.fks-msg {
  max-width: 82%; padding: 9px 13px 7px; margin-top: 2px;
  border-radius: 18px; background: var(--fk-surface-2); color: #ececf0;
  font-size: 15px; line-height: 1.42; white-space: pre-wrap; overflow-wrap: anywhere;
}
.fks-msg.fresh { animation: fks-pop .18s var(--fk-ease); }
@keyframes fks-pop { from { opacity: 0; transform: translateY(5px); } to { opacity: 1; transform: none; } }
.fks-msg.theirs { align-self: flex-start; }
.fks-msg.own { align-self: flex-end; background: linear-gradient(135deg, var(--fk-accent-hi), var(--fk-accent)); color: #fff; }
.fks-msg.gap { margin-top: 10px; }
.fks-msg.theirs.end { border-bottom-left-radius: 6px; }
.fks-msg.own.end { border-bottom-right-radius: 6px; }
.fks-msg.theirs.cont { border-top-left-radius: 6px; }
.fks-msg.own.cont { border-top-right-radius: 6px; }
.fks-msg.pending { opacity: .6; }
.fks-msg time { display: block; margin-top: 3px; font-size: 10.5px; line-height: 1; opacity: .62; text-align: right; font-variant-numeric: tabular-nums; }

.fks-empty { margin: auto; padding: 18px 10px; text-align: center; color: var(--fk-muted); font-size: 14.5px; line-height: 1.5; }
.fks-empty b { display: block; margin-bottom: 4px; color: var(--fk-text); font-size: 17px; font-weight: 650; }
.fks-chips { margin-top: 14px; display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.fks-chip {
  padding: 8px 13px; border: 1px solid var(--fk-line); border-radius: 999px;
  background: var(--fk-surface); color: #d6d6dd; font-size: 13.5px; cursor: pointer;
  transition: border-color .15s, background .15s;
}
.fks-chip:hover, .fks-chip:active { border-color: var(--fk-accent); background: #1d1a19; }

/* Поле ввода */
.fks-composer { flex: none; padding: 10px 12px calc(12px + env(safe-area-inset-bottom, 0px)); background: var(--fk-bg); border-top: 1px solid var(--fk-line); }
.fks-cerror { padding: 0 4px 8px; color: var(--fk-err); font-size: 12.5px; line-height: 1.35; }
.fks-field { display: flex; align-items: flex-end; gap: 8px; }
.fks-field textarea {
  flex: 1; min-width: 0; height: 44px; min-height: 44px; max-height: 120px;
  padding: 11px 15px; resize: none; overflow-y: auto;
  border: 1px solid var(--fk-line); border-radius: 22px;
  background: var(--fk-surface); color: var(--fk-text);
  font-size: 16px; line-height: 20px; outline: none;
  transition: border-color .15s, box-shadow .15s;
}
.fks-send {
  flex: none; width: 44px; height: 44px; padding: 0;
  display: grid; place-items: center; border: 0; border-radius: 50%;
  background: linear-gradient(135deg, var(--fk-accent-hi), var(--fk-accent)); color: #fff; cursor: pointer;
  transition: transform .15s, background .15s, color .15s;
}
.fks-send svg { width: 20px; height: 20px; margin-left: -1px; }
.fks-send:active { transform: scale(.92); }
.fks-send:disabled { background: var(--fk-surface-2); color: #5d5d69; cursor: default; transform: none; }

/* Админ: список обращений и шапка диалога */
.fks-inbox { flex: 1; min-height: 0; padding: 8px; }
.fks-conv { width: 100%; display: block; padding: 12px 14px; text-align: left; border: 0; border-radius: 14px; background: transparent; color: inherit; cursor: pointer; }
.fks-conv:hover, .fks-conv:active { background: var(--fk-surface); }
.fks-conv-top { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.fks-conv-top b { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; font-weight: 600; }
.fks-unread { flex: none; min-width: 20px; height: 20px; padding: 0 6px; display: grid; place-items: center; border-radius: 10px; background: var(--fk-accent); color: #fff; font-size: 11.5px; font-weight: 700; }
.fks-conv-last, .fks-conv-meta { display: block; margin-top: 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.fks-conv-last { color: #b4b4be; }
.fks-conv-meta { color: var(--fk-muted); font-size: 12px; }
.fks-threadtop { flex: none; display: flex; align-items: center; gap: 6px; padding: 6px 10px; border-bottom: 1px solid var(--fk-line); color: #b4b4be; font-size: 13px; }
.fks-threadtop span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ───────── Мобильный: на весь экран ─────────
   Подъём над клавиатурой без JS: в Chrome/Edge Android (VirtualKeyboard API, overlaysContent) окно укорачивается
   на высоту клавиатуры через env(keyboard-inset-height); в Safari на iPhone эта переменная = 0, и браузер сам плавно
   сдвигает видимую область так, чтобы поле ввода оказалось над клавиатурой. */
@media (max-width: 720px) {
  #fkSupportRoot { right: 14px; bottom: calc(70px + env(safe-area-inset-bottom, 0px)); }
  .fks-launch { width: 56px; height: 56px; }
  #fkSupportRoot.fks-open .fks-launch { visibility: hidden; }
  .fks-panel {
    position: fixed; left: 0; right: 0; top: 0; width: 100%; height: auto; min-height: 0; max-height: none;
    bottom: env(keyboard-inset-height, 0px);
    border: 0; border-radius: 0; box-shadow: none;
    animation: fks-fade .16s ease-out;
  }
  @keyframes fks-fade { from { opacity: 0; } to { opacity: 1; } }
  .fks-head { padding: calc(10px + env(safe-area-inset-top, 0px)) 10px 10px 16px; }
  .fks-msg { font-size: 15.5px; max-width: 86%; }
}

@media (prefers-reduced-motion: reduce) {
  .fks-panel, .fks-msg { animation: none !important; }
  #fkSupportRoot .fks-panel, .fks-launch, .fks-send, .fks-primary { transition: none !important; }
}
