/* Support as a Telegram chat: the visitor's page (/support/) and the admin's conversation (admin.css adds its
   frame). Bubbles: the other side on the left, yours on the right in Telegram blue; the date and service lines
   as small pills; three dots while someone types; the composer pinned at the bottom above the safe area. */

:root {
  --color-who-assistant: #1d6fb0;
  --color-who-admin: #8944ab;
  --chat: 42rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-who-assistant: #6ab3f3;
    --color-who-admin: #bf8ae6;
  }
}

.chat-page {
  display: flex;
  flex-direction: column;
}

/* The header: back, who answers, our mark */
.chat-top {
  position: sticky;
  top: 0;
  z-index: 6;
  background: var(--color-bar);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 0.5px solid var(--color-line);
  padding-top: env(safe-area-inset-top);
}

.chat-top__in {
  display: grid;
  grid-template-columns: minmax(var(--tap), 1fr) minmax(0, auto) minmax(var(--tap), 1fr);
  align-items: center;
  gap: var(--space-xs);
  max-width: var(--chat);
  min-height: 3.25rem;
  margin-inline: auto;
  padding: 0 max(0.5rem, env(safe-area-inset-right)) 0 max(0.5rem, env(safe-area-inset-left));
}

.chat-top__back {
  justify-self: start;
  display: inline-flex;
  align-items: center;
  gap: 0.125rem;
  min-height: var(--tap);
  padding-right: var(--space-xs);
  font-size: var(--text-base);
  white-space: nowrap;
}

.chat-top__back:hover {
  text-decoration: none;
}

.chat-top__back svg {
  width: 1.375rem;
  height: 1.375rem;
}

.chat-top__title {
  display: grid;
  justify-items: center;
  min-width: 0;
  text-align: center;
  line-height: 1.2;
}

.chat-top__title b {
  font-size: var(--text-base);
  font-weight: 600;
  white-space: nowrap;
}

.chat-top__title small {
  max-width: 100%;
  font-size: var(--text-2xs);
  color: var(--color-hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* A narrow screen: the back button is its chevron only (its name stays for screen readers) */
@media (max-width: 22.5rem) {
  .chat-top__back .back-link__text {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  .chat-top__back {
    min-width: var(--tap);
  }
}

.chat-top__title small.is-typing {
  color: var(--color-link);
}

.chat-top__mark {
  justify-self: end;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: var(--radius-pill);
  display: grid;
  place-items: center;
  background: linear-gradient(180deg, #4fa6f0, #2273bf);
}

.chat-top__mark svg {
  width: 1.125rem;
  height: 1.125rem;
  fill: #ffffff;
}

/* A pinned line: the order this chat is about (Telegram's pinned message) */
.pinned {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  max-width: var(--chat);
  min-height: var(--tap);
  margin-inline: auto;
  padding: 0.375rem max(1rem, env(safe-area-inset-right)) 0.375rem max(1rem, env(safe-area-inset-left));
  color: var(--color-text);
  border-top: 0.5px solid var(--color-line);
}

a.pinned:hover {
  text-decoration: none;
}

.pinned::before {
  content: "";
  flex: none;
  align-self: stretch;
  width: 2px;
  border-radius: 1px;
  background: var(--color-accent);
}

.pinned__text {
  display: grid;
  min-width: 0;
  line-height: 1.25;
}

.pinned__text b {
  font-size: var(--text-2xs);
  font-weight: 600;
  color: var(--color-link);
}

.pinned__text span {
  font-size: var(--text-xs);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* The messages */
.chat__list {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: var(--space-sm) max(0.5rem, env(safe-area-inset-right)) var(--space-md) max(0.5rem, env(safe-area-inset-left));
}

.chat__in {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  width: 100%;
  max-width: var(--chat);
  margin-inline: auto;
}

.chat__intro {
  align-self: center;
  max-width: 22rem;
  margin: 0 auto var(--space-sm);
  padding: 0.375rem 0.75rem;
  border-radius: var(--radius-cell);
  background: var(--color-cell);
  color: var(--color-hint);
  font-size: var(--text-2xs);
  line-height: 1.45;
  text-align: center;
}

.chat__intro a {
  white-space: nowrap;
}

.service {
  align-self: center;
  max-width: 85%;
  margin: var(--space-sm) 0 var(--space-2xs);
  padding: 0.25rem 0.625rem;
  border-radius: var(--radius-pill);
  background: var(--color-cell);
  color: var(--color-hint);
  font-size: var(--text-2xs);
  font-weight: 500;
  line-height: 1.35;
  text-align: center;
  overflow-wrap: anywhere;
}

.chat__messages {
  display: flex;
  flex-direction: column;
}

.msg {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  margin-top: var(--space-xs);
}

.msg--cont .msg__who {
  display: none;
}

.msg--cont {
  margin-top: 0.125rem;
}

.msg--out {
  align-items: flex-end;
}

.msg__bubble {
  position: relative;
  max-width: min(85%, 28rem);
  padding: 0.375rem 0.75rem 0.4375rem;
  border-radius: 1.125rem;
  background: var(--color-bubble-in);
  color: var(--color-text);
  font-size: 1rem;
  line-height: 1.35;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg--out .msg__bubble {
  background: var(--color-bubble-out);
  color: #ffffff;
}

.msg--tail.msg--in .msg__bubble {
  border-bottom-left-radius: 0.375rem;
}

.msg--tail.msg--out .msg__bubble {
  border-bottom-right-radius: 0.375rem;
}

.msg--new .msg__bubble {
  animation: bubble-in var(--dur-short) var(--ease-out) both;
}

.msg--in.msg--new .msg__bubble {
  transform-origin: 0 100%;
}

.msg--out.msg--new .msg__bubble {
  transform-origin: 100% 100%;
}

@keyframes bubble-in {
  from {
    opacity: 0;
    transform: scale(0.92) translateY(6px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

.msg__bubble a {
  color: var(--color-link);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}

.msg--out .msg__bubble a {
  color: #ffffff;
}

.msg__who {
  display: block;
  margin-bottom: 0.0625rem;
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--color-who-assistant);
}

.msg__who--admin {
  color: var(--color-who-admin);
}

.msg--out .msg__who {
  color: #ffffff;
}

/* The time (and the ticks of yours) sits at the end of the last line, as in Telegram */
.msg__meta {
  float: right;
  position: relative;
  top: 0.4rem;
  display: inline-flex;
  align-items: center;
  gap: 0.1875rem;
  margin-left: 0.625rem;
  font-size: 0.6875rem;
  line-height: 1;
  color: var(--color-hint);
  white-space: nowrap;
  user-select: none;
}

.msg--out .msg__meta {
  color: #ffffff;
}

.msg__meta svg {
  width: 1rem;
  height: 0.875rem;
}

.msg__retry {
  margin-top: 0.125rem;
  padding: 0 0.25rem;
  min-height: 2rem;
  border: 0;
  background: transparent;
  color: var(--color-error);
  font-size: var(--text-2xs);
  cursor: pointer;
}

.msg--failed .msg__bubble {
  opacity: 0.75;
}

.msg--pending .msg__meta svg {
  animation: pulse 1.4s ease-in-out infinite;
}

/* Three dots while someone types */
.typing {
  display: inline-flex;
  align-self: flex-start;
  align-items: center;
  gap: 0.25rem;
  margin-top: var(--space-xs);
  padding: 0.8125rem 0.875rem;
  border-radius: 1.125rem 1.125rem 1.125rem 0.375rem;
  background: var(--color-bubble-in);
  animation: bubble-in var(--dur-short) var(--ease-out) both;
  transform-origin: 0 100%;
}

.typing i {
  width: 0.4375rem;
  height: 0.4375rem;
  border-radius: var(--radius-pill);
  background: var(--color-hint);
  animation: typing 1.2s ease-in-out infinite;
}

.typing i:nth-child(2) {
  animation-delay: 0.15s;
}

.typing i:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing {
  0%,
  60%,
  100% {
    opacity: 0.35;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* Nothing yet: a hello and three questions to start with */
.chat__empty {
  margin: auto 0;
  padding: var(--space-lg) var(--space-md);
  text-align: center;
}

.chat__empty .hero {
  margin-bottom: var(--space-md);
}

.chat__empty h1 {
  margin: 0;
  font-size: var(--text-lg);
  font-weight: 700;
}

.chat__empty p {
  max-width: 20rem;
  margin: var(--space-2xs) auto var(--space-md);
  color: var(--color-hint);
  font-size: var(--text-sm);
  line-height: 1.45;
}

.chip--ask {
  color: var(--color-link) !important;
  font-weight: 500;
}

/* The composer, pinned at the bottom */
.composer {
  position: sticky;
  bottom: 0;
  z-index: 6;
  background: var(--color-bar);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  backdrop-filter: saturate(1.4) blur(12px);
  border-top: 0.5px solid var(--color-line);
  padding: 0.5rem max(0.5rem, env(safe-area-inset-right)) calc(0.5rem + env(safe-area-inset-bottom))
    max(0.5rem, env(safe-area-inset-left));
}

.composer__in {
  max-width: var(--chat);
  margin-inline: auto;
}

.composer__tools {
  display: flex;
  justify-content: center;
  margin-bottom: 0.375rem;
}

.composer__tools:empty,
.composer__tools[hidden] {
  display: none;
}

.composer__status {
  margin: 0 0 0.375rem;
  font-size: var(--text-xs);
  text-align: center;
  color: var(--color-hint);
}

.composer__status:empty {
  display: none;
}

.composer__status[data-tone="error"] {
  color: var(--color-error);
}

.composer__row {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
}

.composer__field {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  border-radius: 1.375rem;
  background: var(--color-cell);
  border: 0.5px solid var(--color-line);
}

.composer__field:focus-within {
  border-color: var(--color-accent);
  box-shadow: 0 0 0 1px var(--color-accent);
}

.composer textarea {
  flex: 1;
  min-width: 0;
  min-height: 2.75rem;
  max-height: 9.5rem;
  padding: 0.6875rem 0.875rem;
  border: 0;
  border-radius: 1.375rem;
  background: transparent;
  color: var(--color-text);
  font: 400 var(--text-base) / 1.3 var(--font-ui);
  resize: none;
  overflow-y: auto;
}

.composer textarea:focus {
  outline: none;
}

.composer textarea::placeholder {
  color: var(--color-hint);
  opacity: 1;
}

.composer__side {
  flex: none;
  display: grid;
  justify-items: center;
  gap: 0.125rem;
}

.composer__count {
  font-size: 0.75rem;
  line-height: 1;
  color: var(--color-hint);
  font-variant-numeric: tabular-nums;
}

.composer__count[data-tone="error"] {
  color: var(--color-error);
}

.composer__send {
  flex: none;
  width: var(--tap);
  height: var(--tap);
  border: 0;
  border-radius: var(--radius-pill);
  display: grid;
  place-items: center;
  background: var(--color-accent);
  color: #ffffff;
  transition: background-color var(--dur-micro) var(--ease-out), transform var(--dur-micro) var(--ease-out);
}

.composer__send svg {
  width: 1.375rem;
  height: 1.375rem;
}

.composer__send:active {
  transform: scale(0.92);
}

.composer__send:disabled {
  background: var(--color-line);
  color: var(--color-hint);
  cursor: default;
  transform: none;
}

.composer__send:focus-visible {
  outline-offset: 2px;
}

.composer__send[aria-busy="true"] svg {
  animation: pulse 1s ease-in-out infinite;
}

@media (prefers-reduced-motion: reduce) {
  .typing i {
    animation: pulse 1.4s ease-in-out infinite !important;
  }

  .msg--pending .msg__meta svg,
  .composer__send[aria-busy="true"] svg {
    animation: pulse 1.4s ease-in-out infinite !important;
  }
}
