/* Messaging: full-height conversation on phones, docked panel on desktop. */
.chat {
  position: fixed; z-index: 60; left: 0; right: 0;
  top: var(--vv-top, 0px); height: 100vh; height: var(--vv-h, 100dvh);
  display: flex; flex-direction: column; background: var(--chat-bg); color: var(--ink);
  overscroll-behavior: contain;
}
.chat-scrim { position: fixed; inset: 0; z-index: 55; background: var(--scrim); display: none; }
html.chat-open, html.chat-open body { overflow: hidden; }
@media (min-width: 960px) {
  .chat { left: auto; right: 24px; top: auto; bottom: 24px; width: 440px; height: min(760px, calc(100dvh - 48px)); border-radius: var(--r-sheet); overflow: hidden; box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .45), 0 0 0 1px var(--line); }
  .chat-scrim:not([hidden]) { display: block; }
}
@media (prefers-reduced-motion: no-preference) {
  .chat:not([hidden]) { animation: chat-in .26s cubic-bezier(.2, .8, .2, 1); }
  @keyframes chat-in { from { transform: translateY(24px); opacity: 0; } to { transform: none; opacity: 1; } }
}

.chat-head {
  display: flex; align-items: center; gap: .25rem; padding: calc(6px + env(safe-area-inset-top, 0px)) 6px 6px;
  background: var(--surface); border-bottom: 1px solid var(--line); flex: none;
}
@media (min-width: 960px) { .chat-head { padding-top: 6px; } }
.chat-id { display: flex; align-items: center; gap: .6rem; flex: 1; min-width: 0; }
.chat-id h2 { font-family: var(--body); font-size: 1.05rem; font-weight: 700; letter-spacing: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-status { font-size: var(--fs-xs); color: var(--muted); font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-id > div { min-width: 0; }
.chat-status.human { color: var(--warn); }
.chat-status.agent { color: var(--ok); }

.avatar { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 50%; flex: none; overflow: hidden; }
.avatar svg { width: 100%; height: 100%; }
.avatar.agent { background: var(--agent-accent); color: var(--on-accent); font: 700 .9rem/1 var(--display); letter-spacing: .02em; }
.avatar.bot { border-radius: 30%; background: var(--accent-wash); color: var(--accent-ink); }
.avatar.bot svg { width: 64%; height: 64%; }

.conn-banner { flex: none; padding: .55rem 1rem; font-size: var(--fs-sm); font-weight: 700; text-align: center; background: var(--warn-wash); color: var(--warn); }
.conn-banner.offline { background: var(--danger-wash); color: var(--danger); }

.chat-log {
  flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 2px; padding: 12px 12px 8px;
}
.chat-log > :first-child { margin-top: auto; }
.chat-log:focus-visible { outline-offset: -3px; }

.day-sep { align-self: center; margin: 10px 0 6px; padding: .2rem .65rem; border-radius: 999px; background: var(--surface-2); color: var(--muted); font-size: var(--fs-xs); font-weight: 700; }

.sender { display: flex; align-items: center; gap: .4rem; margin: 10px 0 4px 2px; font-size: var(--fs-xs); font-weight: 700; color: var(--ink-2); }
.sender .avatar { width: 22px; height: 22px; }
.sender .avatar.agent { font-size: .58rem; }
.tag { padding: .08rem .4rem; border-radius: 6px; font-size: .6875rem; font-weight: 700; letter-spacing: .03em; text-transform: uppercase; }
.tag.bot { background: var(--surface-3); color: var(--ink-2); }
.tag.person { background: var(--ok-wash); color: var(--ok); }

.bubble {
  max-width: min(84%, 520px); padding: .6rem .85rem; border-radius: 20px; font-size: 1.0625rem; line-height: 1.45;
  white-space: pre-wrap; overflow-wrap: anywhere; word-break: break-word; position: relative;
}
.bubble.me { align-self: flex-end; background: var(--bubble-me); color: var(--on-bubble-me); border-bottom-right-radius: 8px; }
.bubble.me + .bubble.me { border-top-right-radius: 8px; }
.bubble.bot { align-self: flex-start; background: var(--bubble-bot); color: var(--on-bubble-bot); box-shadow: inset 0 0 0 1px var(--line); border-bottom-left-radius: 8px; }
.bubble.agent { align-self: flex-start; background: var(--bubble-agent); color: var(--on-bubble-agent); border-bottom-left-radius: 8px; }
.bubble.bot + .bubble.bot, .bubble.agent + .bubble.agent { border-top-left-radius: 8px; }
.bubble + .bubble { margin-top: 2px; }
.bubble.pending { opacity: .7; }
.bubble.failed { box-shadow: inset 0 0 0 2px var(--danger); }
.bubble.photo { padding: 4px; background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); }
.bubble.photo img { display: block; max-width: min(240px, 100%); max-height: 320px; border-radius: 16px; object-fit: cover; }
.bubble.photo .photo-ph { display: grid; place-items: center; width: 200px; height: 140px; border-radius: 16px; background: var(--surface-2); color: var(--muted); font-size: var(--fs-sm); }

.meta-line { font-size: var(--fs-xs); color: var(--muted); margin: 3px 4px 6px; display: flex; gap: .35rem; align-items: center; }
.meta-line.me { align-self: flex-end; }
.meta-line .seen { color: var(--ok); font-weight: 700; }
.retry { border: 0; background: none; padding: .35rem 0; min-height: 44px; color: var(--danger); font-weight: 700; font-size: var(--fs-sm); cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }

.sys {
  align-self: center; display: flex; gap: .6rem; align-items: start; max-width: min(94%, 520px); margin: 10px 0;
  padding: .75rem .9rem; border-radius: var(--r-control); background: var(--surface); box-shadow: inset 0 0 0 1px var(--line);
  font-size: var(--fs-sm); color: var(--ink-2); line-height: 1.45;
}
.sys svg { width: 20px; height: 20px; flex: none; margin-top: 1px; color: var(--warn); }
.sys.done svg { color: var(--ok); }

.typing { align-self: flex-start; display: inline-flex; gap: 5px; padding: .85rem 1rem; border-radius: 20px; border-bottom-left-radius: 8px; background: var(--bubble-bot); box-shadow: inset 0 0 0 1px var(--line); margin-top: 2px; }
.typing.agent { background: var(--bubble-agent); box-shadow: none; }
.typing i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

/* Empty conversation: welcome sits near the composer, in thumb reach */
.chat-welcome { display: grid; justify-items: center; gap: .6rem; text-align: center; padding: var(--sp-3) 4px var(--sp-2); }
.chat-welcome .avatar { width: 64px; height: 64px; }
.chat-welcome h3 { font-size: 1.4rem; }
.chat-welcome p { color: var(--ink-2); max-width: 34ch; }
.chat-welcome .topic-grid { width: 100%; max-width: 440px; }
.chat-welcome .topic-grid button { background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); }
.chat-welcome .topic-grid button[data-opt="opt:human"] { background: var(--ink); color: var(--paper); box-shadow: none; }
.chat-welcome .topic-grid button[data-opt="opt:human"] svg { color: inherit; }
.chat-welcome .topic-grid .emo { font-size: 1.3rem; line-height: 1; width: 24px; text-align: center; flex: none; }
.loading-log { display: grid; gap: .6rem; padding: 1rem 0; }
.loading-log span { height: 44px; width: 70%; border-radius: 18px; background: var(--surface-2); animation: pulse 1.2s infinite ease-in-out; }
.loading-log span:nth-child(2) { width: 50%; align-self: end; justify-self: end; }
@keyframes pulse { 50% { opacity: .5; } }

.new-pill {
  position: absolute; left: 50%; translate: -50% 0; bottom: calc(var(--composer-h, 70px) + var(--quick-h, 0px) + 12px); z-index: 2;
  display: inline-flex; align-items: center; gap: .35rem; min-height: 40px; padding: 0 1rem; border: 0; border-radius: 999px;
  background: var(--ink); color: var(--paper); font-weight: 700; box-shadow: var(--shadow-lift); cursor: pointer;
}
.new-pill svg { width: 18px; height: 18px; rotate: 90deg; }

/* Quick replies wrap; they never scroll sideways */
.quick { flex: none; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 6px; padding: 6px 12px 8px; }
.quick button {
  min-height: 42px; padding: .45rem .9rem; border: 0; border-radius: 999px; cursor: pointer; touch-action: manipulation;
  background: var(--surface); color: var(--ink); box-shadow: inset 0 0 0 1.5px var(--line-strong); font: 700 .95rem/1.2 var(--body); text-align: left;
}
.quick button:active { background: var(--surface-2); }
@media (hover: hover) { .quick button:hover { box-shadow: inset 0 0 0 1.5px var(--ink); } }
.quick button.person { background: var(--ink); color: var(--paper); box-shadow: none; }

.composer {
  flex: none; display: flex; align-items: flex-end; gap: 6px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom, 0px)); background: var(--surface); border-top: 1px solid var(--line);
}
.chat.kb .composer { padding-bottom: 8px; }
.composer textarea {
  flex: 1; min-width: 0; min-height: 46px; max-height: 140px; resize: none; padding: .7rem 1rem; border-radius: 23px;
  border: 1.5px solid var(--line-strong); background: var(--paper); color: var(--ink); font: 400 1.0625rem/1.35 var(--body);
}
.composer textarea:focus { outline: none; border-color: var(--focus); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent); }
.composer textarea::placeholder { color: var(--muted); }
.composer .attach { width: 46px; height: 46px; border-radius: 50%; background: var(--surface-2); }
.send { display: grid; place-items: center; width: 46px; height: 46px; border-radius: 50%; border: 0; background: var(--accent); color: var(--on-accent); cursor: pointer; flex: none; transition: background-color .15s, transform .12s; touch-action: manipulation; }
.send svg { width: 22px; height: 22px; }
.send:active { transform: scale(.94); }
.send[disabled] { background: var(--surface-3); color: var(--muted); cursor: default; }

/* Bottom sheet (chat actions) */
.sheet-scrim { position: fixed; inset: 0; z-index: 75; background: var(--scrim); }
.sheet {
  position: fixed; z-index: 80; left: 0; right: 0; bottom: 0; display: grid; gap: .5rem;
  padding: 10px 12px calc(12px + env(safe-area-inset-bottom, 0px)); background: var(--surface); border-radius: var(--r-sheet) var(--r-sheet) 0 0;
  box-shadow: 0 -10px 40px rgba(0, 0, 0, .25);
}
.sheet .grabber { display: block; margin: 0 auto 6px; }
@media (min-width: 960px) { .sheet { left: auto; right: 36px; bottom: 36px; width: 416px; border-radius: var(--r-sheet); } }
@media (prefers-reduced-motion: no-preference) { .sheet:not([hidden]) { animation: sheet-up .24s cubic-bezier(.2, .8, .2, 1); } }

/* Links inside the conversation (Telegram, jump to a tab) */
.quick a, .quick button.link { display: inline-flex; align-items: center; gap: .4rem; min-height: 42px; padding: .45rem .9rem; border-radius: 999px; font: 700 .95rem/1.2 var(--body); text-decoration: none; }
.quick a.tg { background: var(--telegram); color: var(--on-telegram); }
.quick a svg { width: 18px; height: 18px; }
.quick button.person { background: var(--ink); color: var(--paper); box-shadow: none; }
.sys .sys-actions { display: flex; flex-wrap: wrap; gap: .4rem; margin-top: .5rem; }
.sys .sys-actions a { display: inline-flex; align-items: center; gap: .35rem; min-height: 40px; padding: .3rem .8rem; border-radius: 999px; background: var(--telegram); color: var(--on-telegram); font-weight: 700; text-decoration: none; }
.sys .sys-actions svg { width: 16px; height: 16px; color: inherit; margin: 0; }
.avatar.bot.pic { border-radius: 50%; box-shadow: 0 0 0 1.5px var(--accent); }

/* Shortcut chips above the input: tapping sends the message. They wrap, never scroll sideways. */
.shortcuts { flex: none; display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 10px 8px; background: var(--chat-bg); }
.shortcuts button {
  min-height: 44px; padding: .4rem .85rem; border: 0; border-radius: 999px; cursor: pointer; touch-action: manipulation;
  background: var(--surface); color: var(--ink); font: 700 .9rem/1.2 var(--body);
  box-shadow: inset 0 0 0 1.5px color-mix(in srgb, var(--accent) 60%, var(--line-strong)); transition: transform .12s, background-color .15s;
}
.shortcuts button:active { transform: scale(.95); background: var(--accent-wash); }
.shortcuts button.sent { background: var(--accent); color: var(--on-accent); }
@media (hover: hover) { .shortcuts button:hover { box-shadow: inset 0 0 0 2px var(--accent); } }
.chat.kb .shortcuts { display: none; }
@media (prefers-reduced-motion: reduce) { .shortcuts button:active { transform: none; } }
