/* ── Screens ── */
.screen { display: none !important; }
.screen.active { display: block !important; }
#screen-waiting.active { display: flex !important; }
#screen-chat.active { display: flex !important; }

/* ── Prevent flash on refresh ── */
body.loading .screen { display: none !important; }

/* ── Register / Rejected screens ── */
.page-center {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: var(--light-most-gray);
}
.auth-card {
  width: 100%;
  max-width: 420px;
}
.auth-logo {
  display: flex;
  align-items: flex-start;
  justify-content: flex-start;
  gap: 12px;
  margin-bottom: 32px;
  width: 100%;
  text-align: left;
}
.auth-logo__icon-wrap {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  background: transparent;
  border-radius: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
.auth-logo__icon {
  width: 36px;
  height: 36px;
  object-fit: contain;
}
.auth-logo__copy {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  min-width: 0;
}
.auth-logo__title {
  font-size: 16px;
  font-weight: 600;
  color: var(--dark-gray);
  line-height: 1.25;
}
.auth-logo__subtitle {
  font-size: 13px;
  font-weight: 400;
  color: var(--lighter-gray);
  line-height: 1.35;
}
.auth-intro {
  text-align: left;
  line-height: 1.45;
  margin-top: 0;
}
.auth-error {
  display: none;
  padding: 10px 14px;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 10px;
  color: var(--red);
  font-size: 14px;
  text-align: center;
  margin-bottom: 16px;
}
.auth-preloader {
  display: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  font-size: 14px;
  color: var(--lighter-gray);
  margin-bottom: 12px;
}
.rejected-icon {
  width: 80px;
  height: 80px;
  margin: 0 auto 20px;
  background: #fef2f2;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}
.rejected-icon svg {
  width: 32px;
  height: 32px;
  color: var(--red);
}

/* ── Waiting screen ── */
#screen-waiting {
  min-height: 100vh;
  background: var(--white);
  align-items: center;
  justify-content: center;
  flex-direction: column;
}
.waiting-card {
  text-align: center;
  padding: 40px;
}
.waiting-card img {
  max-width: 128px;
  margin-bottom: 32px;
}
.waiting-card h3 {
  margin-bottom: 8px;
}
.waiting-card p {
  color: var(--lighter-gray);
  font-size: 15px;
}
/* Только GIF при восстановлении сессии (перезагрузка) */
#screen-waiting.screen-waiting--silent .waiting-card h3,
#screen-waiting.screen-waiting--silent .waiting-card p {
  display: none;
}
#screen-waiting.screen-waiting--silent .waiting-card img {
  margin-bottom: 0;
}

/* ══════════════════════════════════════
   Chat
   ══════════════════════════════════════ */

#screen-chat {
  flex-direction: column;
  height: 100vh;
}
.chat-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-width: 600px;
  margin: 0 auto;
  background: var(--white);
  width: 100%;
}
@media (min-width: 601px) {
  body.chat-mode { background: var(--light-most-gray); }
  .chat-wrapper { box-shadow: var(--shadow); }
}
body.chat-mode { overflow: hidden; }

/* ── Header ── */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: #3390ec;
  color: var(--white);
  flex-shrink: 0;
}
.chat-header__logo {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
  object-fit: contain;
  border-radius: 0;
  background: transparent;
  padding: 0;
}
.chat-header__info { flex: 1; min-width: 0; }
.chat-header__title {
  font-weight: 500;
  font-size: 15px;
  color: var(--white);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-header__subtitle {
  font-size: 12px;
  opacity: 0.7;
}

/* ── Messages area ── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 12px;
  background: #eef2f5;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ── Message wrap (avatar + bubble) ── */
.msg-wrap {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  align-self: flex-start;
  max-width: 84%;
  animation: msgIn .2s ease-out;
}

/* ── Avatars ── */
.msg-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 1px;
}
.msg-avatar--system {
  background: transparent;
  overflow: hidden;
  border-radius: 0;
}
.msg-avatar--system img {
  width: 36px;
  height: 36px;
  object-fit: contain;
  border-radius: 0;
}
.msg-avatar--operator {
  background: #3390ec;
}
.msg-avatar--operator svg {
  width: 15px;
  height: 15px;
  fill: #fff;
}

/* ── Message bubbles ── */
.msg {
  padding: 8px 12px 6px;
  font-size: 14px;
  line-height: 1.5;
  word-wrap: break-word;
  position: relative;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── User messages (right, green) ── */
.msg-user {
  align-self: flex-end;
  max-width: 78%;
  background: #d9fdd3;
  color: #111;
  border-radius: 12px 12px 4px 12px;
  padding: 8px 12px 6px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
  animation: msgIn .2s ease-out;
  transition: background-color .55s ease;
}

/* ── System messages ── */
.msg-system {
  background: #fff;
  color: #111;
  border-radius: 4px 12px 12px 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
  flex: 1;
  min-width: 0;
  transition: background-color .55s ease;
}

/* ── Operator messages ── */
.msg-operator {
  background: #e3e8ff;
  color: #111;
  border-radius: 4px 12px 12px 12px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
  flex: 1;
  min-width: 0;
  transition: background-color .55s ease;
}
.msg-operator.msg-operator--media-only {
  display: inline-block;
  width: auto;
  max-width: min(78vw, 360px);
  flex: 0 0 auto;
  min-width: 0;
}
.msg-operator-name {
  font-size: 13px;
  font-weight: 500;
  color: var(--blue);
  margin-bottom: 2px;
}

/* ── Quote block (Telegram style) ── */
.msg-quote {
  display: flex;
  flex-direction: column;
  gap: 1px;
  padding: 6px 10px;
  margin: 0 -2px 6px;
  border-left: 3px solid var(--blue);
  border-radius: 4px;
  font-size: 13px;
  cursor: default;
  transition: background .15s;
  background: rgba(66, 36, 253, 0.07);
}
.msg-quote--clickable { cursor: pointer; }
.msg-quote--clickable:hover { background: rgba(0,0,0,0.05); }
.msg-quote--clickable:active { background: rgba(0,0,0,0.08); }
.msg-quote__author {
  font-size: 12px;
  font-weight: 500;
  color: var(--blue);
  line-height: 1.3;
}
.msg-quote__text {
  color: #555;
  line-height: 1.35;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* ── Highlight on scroll-to (без рывка/моргания) ── */
.msg-user.msg--highlight { background-color: #b2e6a8 !important; }
.msg-system.msg--highlight { background-color: #cde0f5 !important; }
.msg-operator.msg--highlight { background-color: #b8c5f7 !important; }

/* ── Common message styles ── */
.msg b, .msg strong { font-weight: 500; }
.msg a { color: var(--blue); text-decoration: underline; }
.msg-user a { color: #111; }
.ui-icon {
  width: 0.95em;
  height: 0.95em;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  vertical-align: -0.12em;
  margin-right: 0.12em;
  color: currentColor;
}
.ui-icon svg {
  width: 100%;
  height: 100%;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.msg-time {
  font-size: 11px;
  color: rgba(0,0,0,0.35);
  margin-top: 3px;
  text-align: right;
  line-height: 1;
}

/* ── Blue double checkmarks ── */
.msg-checks {
  color: #4fc3f7;
  font-size: 12px;
  font-weight: 700;
  letter-spacing: -2px;
  margin-left: 2px;
}

/* ── Typing indicator (bouncing dots) ── */
.typing-bubble {
  display: flex !important;
  align-items: center;
  gap: 5px;
  padding: 12px 18px !important;
  min-height: 40px;
}
.typing-bubble .dot {
  width: 8px;
  height: 8px;
  background: #aab;
  border-radius: 50%;
  animation: typingDot 1.4s infinite ease-in-out both;
}
.typing-bubble .dot:nth-child(1) { animation-delay: 0s; }
.typing-bubble .dot:nth-child(2) { animation-delay: 0.16s; }
.typing-bubble .dot:nth-child(3) { animation-delay: 0.32s; }
@keyframes typingDot {
  0%, 80%, 100% { transform: scale(0.5); opacity: 0.35; }
  40% { transform: scale(1); opacity: 1; }
}

/* ── Gossvyaz links ── */
.gossvyaz-links {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-top: 8px;
}
.gossvyaz-links a {
  display: block;
  padding: 10px 14px;
  background: #3390ec;
  color: var(--white);
  border-radius: 8px;
  text-align: center;
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  transition: background .2s;
}
.gossvyaz-links a:hover { background: #2b84db; }

/* ── ESIA button ── */
.esia-auth-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  padding: 12px 20px;
  border: none;
  border-radius: 8px;
  background: #0d4cd3;
  color: #fff;
  font-family: inherit;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background .2s;
  margin-top: 8px;
}
.esia-auth-btn:hover { background: #1d5deb; }
.esia-auth-btn:active { background: #0b40b3; }

/* ── File attachments (incoming) ── */
.msg-file img {
  max-width: 100%;
  border-radius: 8px;
  margin-top: 6px;
}
.msg-operator .msg-file {
  margin-top: 10px;
  margin-bottom: 8px;
}
.msg-operator .msg-file .file-attach-row {
  margin-bottom: 10px;
}
.msg-operator .msg-file .msg-link-btn {
  margin-top: 0;
}
.chat-media-thumb {
  max-width: min(240px, 100%);
  max-height: 220px;
  border-radius: 8px;
  margin-top: 6px;
  display: block;
  background: #000;
}
.chat-media-thumb--anim {
  cursor: default;
  pointer-events: none;
}
.msg-file a {
  display: inline-block;
  margin-top: 6px;
  color: var(--blue);
}
.doc-preview {
  display: block;
  max-width: min(260px, 100%);
  margin-top: 6px;
  border-radius: 10px;
  padding: 12px;
  text-decoration: none !important;
  color: #111 !important;
  box-shadow: 0 1px 2px rgba(0,0,0,0.08);
}
.doc-preview--pdf {
  background: linear-gradient(145deg, #ffe2e2 0%, #ffd0d0 100%);
}
.doc-preview--doc {
  background: linear-gradient(145deg, #e7efff 0%, #d5e4ff 100%);
}
.doc-preview--xls {
  background: linear-gradient(145deg, #e5f8ea 0%, #c9efd4 100%);
}
.doc-preview__badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: .04em;
  border-radius: 6px;
  padding: 3px 7px;
  background: rgba(255,255,255,0.72);
  margin-bottom: 8px;
}
.doc-preview__title {
  font-size: 13px;
  font-weight: 600;
  line-height: 1.35;
  word-break: break-word;
}
.doc-preview__meta {
  margin-top: 4px;
  font-size: 12px;
  opacity: .72;
}
.msg-link-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-top: 8px;
  padding: 8px 12px;
  border-radius: 8px;
  background: #3390ec;
  color: #fff !important;
  text-decoration: none !important;
  font-size: 13px;
  font-weight: 500;
}
.msg-link-btn:hover { background: #2b84db; }

/* ── User file message (outgoing) — Telegram-style row ── */
.file-attach-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.file-attach-icon {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: #3390ec;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 1px 3px rgba(51, 144, 236, 0.35);
}
.file-attach-icon svg {
  width: 15px;
  height: 15px;
  display: block;
  flex-shrink: 0;
  color: #fff;
}
.file-preview { margin-bottom: 8px; }
.file-preview-img {
  max-width: min(220px, 100%);
  max-height: 180px;
  border-radius: 10px;
  object-fit: cover;
  cursor: pointer;
  display: block;
  transition: opacity .15s;
}
.file-preview-img:hover { opacity: 0.92; }
.chat-photo-thumb { cursor: pointer; }
.msg-file img.chat-photo-thumb {
  cursor: pointer;
  transition: opacity .15s;
}
.msg-file img.chat-photo-thumb:hover { opacity: 0.92; }
.file-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; flex: 1; }
.file-name {
  font-size: 13px;
  font-weight: 500;
  word-break: break-word;
  line-height: 1.3;
}
.file-size {
  font-size: 12px;
  opacity: 0.55;
}
/* Индикатор отправки файла (в строке времени, как у обычных сообщений) */
.msg-file-uploading {
  opacity: 0.55;
  font-size: 11px;
  animation: fileUploadHint 1.2s ease-in-out infinite;
}
@keyframes fileUploadHint {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 0.85; }
}
.msg-send-error {
  color: #c62828;
  font-size: 11px;
  font-weight: 500;
}

/* ── Photo lightbox ── */
.photo-lightbox {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 9999;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.photo-lightbox.open {
  display: flex;
}
.photo-lightbox__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
  cursor: pointer;
}
.photo-lightbox__inner {
  position: relative;
  z-index: 2;
  max-width: min(96vw, 1200px);
  max-height: min(88vh, 900px);
  margin: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.photo-lightbox__img {
  max-width: 100%;
  max-height: min(88vh, 900px);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.5);
}
.photo-lightbox__close {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  z-index: 3;
  width: 44px;
  height: 44px;
  border: none;
  border-radius: 50%;
  background: rgba(255,255,255,0.15);
  color: #fff;
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .2s;
}
.photo-lightbox__close:hover { background: rgba(255,255,255,0.28); }

/* ── Input area ── */
.chat-input-area {
  padding: 6px 8px 8px;
  background: #fff;
  border-top: 1px solid #e5e5e5;
  flex-shrink: 0;
  position: relative;
}
.chat-input-wrap {
  display: flex;
  align-items: center;
  gap: 4px;
}
.chat-input-wrap .form-input {
  flex: 1;
  height: 42px;
  border-radius: 21px;
  padding: 8px 14px;
  font-size: 14px;
  border: 1px solid #ddd;
  background: var(--light-most-gray);
  min-width: 0;
}
.chat-input-wrap .form-input:focus {
  border-color: var(--blue);
  background: #fff;
}
.chat-icon-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #e8f3ff;
  color: #3390ec;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
  overflow: hidden;
  -webkit-appearance: none;
  appearance: none;
  -webkit-tap-highlight-color: transparent;
  transition: color .15s, background .15s, transform .1s;
}
.chat-icon-btn:hover {
  color: #fff;
  background: #3390ec;
}
.chat-icon-btn:active {
  color: #fff;
  background: #2b84db;
  transform: scale(0.96);
}
.chat-send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: #3390ec;
  color: var(--white);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}
.chat-send-btn:hover { background: #2b84db; }
.chat-send-btn:active { transform: scale(0.93); }

/* На тач-устройствах не оставляем "залипший" hover после тапа */
@media (hover: none) {
  .chat-icon-btn:hover {
    color: #3390ec;
    background: #e8f3ff;
  }
}

/* ── Emoji picker ── */
.emoji-picker {
  display: none;
  position: absolute;
  bottom: 54px;
  left: 8px;
  right: 8px;
  width: auto;
  max-width: min(360px, calc(100vw - 16px));
  margin-left: auto;
  margin-right: 0;
  max-height: min(42vh, 320px);
  background: #fff;
  border-radius: 0 !important;
  box-shadow: 0 4px 24px rgba(0,0,0,0.15);
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior-x: none;
  touch-action: pan-y;
  padding: 10px 8px 10px 10px;
  z-index: 20;
  box-sizing: border-box;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,0.15) transparent;
}
.emoji-picker::-webkit-scrollbar {
  width: 6px;
}
.emoji-picker::-webkit-scrollbar-track {
  background: transparent;
  border-radius: 3px;
  margin: 4px 0;
}
.emoji-picker::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15);
  border-radius: 3px;
}
.emoji-picker::-webkit-scrollbar-thumb:hover {
  background: rgba(0,0,0,0.28);
}
.emoji-picker::-webkit-scrollbar-button {
  display: none;
  height: 0;
  width: 0;
}
.emoji-picker.open { display: block; }
.emoji-picker__section-title {
  font-size: 10px;
  font-weight: 600;
  color: #888;
  padding: 6px 4px 4px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.emoji-picker__grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 2px;
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
}
.emoji-picker__grid span {
  display: flex;
  align-items: center;
  justify-content: center;
  aspect-ratio: 1;
  max-width: 100%;
  font-size: clamp(18px, 5.5vw, 24px);
  border-radius: 0 !important;
  cursor: pointer;
  transition: background .1s;
  user-select: none;
  box-sizing: border-box;
}
.emoji-picker__grid span:hover {
  background: rgba(66, 36, 253, 0.08);
}
@media (min-width: 400px) {
  .emoji-picker__grid {
    grid-template-columns: repeat(8, minmax(0, 1fr));
  }
}
@media (min-width: 520px) {
  .emoji-picker {
    max-height: min(38vh, 340px);
  }
  .emoji-picker__grid span {
    font-size: 22px;
  }
}

/* ── Upload toast ── */
.upload-toast {
  position: absolute;
  bottom: 60px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.75);
  color: #fff;
  font-size: 13px;
  padding: 8px 16px;
  border-radius: 20px;
  z-index: 20;
  white-space: nowrap;
  animation: toastIn .2s ease-out;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

@media (max-width: 380px) {
  .emoji-picker {
    left: 6px;
    right: 6px;
    max-width: none;
  }
  .emoji-picker__grid {
    grid-template-columns: repeat(6, minmax(0, 1fr));
  }
}

/* ── Mobile adjustments ── */
@media (max-width: 480px) {
  .chat-messages { padding: 14px 10px 10px; gap: 14px; }
  .msg-user { max-width: 85%; }
  .msg-wrap { max-width: 88%; }
  .chat-header { padding: 8px 12px; }
  .chat-input-area { padding: 6px 8px 8px; }
  .chat-input-wrap .form-input { height: 40px; }
  .chat-send-btn { width: 40px; height: 40px; }
}
@media (max-width: 360px) {
  .msg-user { max-width: 90%; }
  .msg-wrap { max-width: 92%; }
  .chat-messages { gap: 12px; }
}

/* ── Custom scrollbar ── */
.chat-messages::-webkit-scrollbar {
  width: 6px;
}
.chat-messages::-webkit-scrollbar-track {
  background: transparent;
}
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15);
  border-radius: 3px;
}
.chat-messages::-webkit-scrollbar-thumb:hover {
  background: rgba(0,0,0,0.28);
}
.chat-messages::-webkit-scrollbar-button {
  display: none;
  height: 0;
  width: 0;
}
.chat-messages {
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,0.15) transparent;
}

/* ── Scroll-to-bottom button ── */
.scroll-down-btn {
  display: none;
  position: absolute;
  bottom: 70px;
  right: 16px;
  z-index: 10;
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.18);
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: transform .15s, box-shadow .15s;
}
.scroll-down-btn.visible {
  display: flex;
}
.scroll-down-btn:hover {
  box-shadow: 0 3px 12px rgba(0,0,0,0.25);
  transform: scale(1.06);
}
.scroll-down-btn:active {
  transform: scale(0.95);
}
.scroll-down-btn svg {
  width: 22px;
  height: 22px;
  fill: #555;
}
.scroll-down-badge {
  position: absolute;
  top: -6px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  background: var(--blue);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  border-radius: 10px;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
  line-height: 20px;
  text-align: center;
  box-sizing: border-box;
}
.scroll-down-badge.visible {
  display: flex;
}

/* chat-wrapper needs position:relative for the button */
.chat-wrapper { position: relative; }
