:root {
  color-scheme: dark;
  --chat-bg: #111111;
  --chat-panel: #191919;
  --chat-panel-strong: #222222;
  --chat-border: rgba(255, 255, 255, 0.12);
  --chat-text: #ffffff;
  --chat-muted: #b7b7b7;
  --chat-accent: #c62828;
  --chat-accent-strong: #a61b1b;
  --chat-shadow: 0 20px 58px rgba(0, 0, 0, 0.38);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  min-width: 320px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.chat-widget {
  position: fixed;
  right: 18px;
  bottom: max(18px, env(safe-area-inset-bottom));
  left: auto;
  z-index: 1400;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 12px;
  pointer-events: none;
  opacity: 0;
  transform: translateY(120px) translateZ(0);
  transition: opacity 320ms cubic-bezier(.22, 1, .36, 1), transform 320ms cubic-bezier(.22, 1, .36, 1);
  max-width: calc(100vw - 28px);
}

.chat-widget.is-visible {
  opacity: 1;
  pointer-events: auto;
  transform: translateY(0) translateZ(0);
}

.chat-fab {
  position: relative;
  width: 68px;
  height: 68px;
  border: 0;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  background: linear-gradient(135deg, var(--chat-accent) 0%, var(--chat-accent-strong) 100%);
  box-shadow: 0 16px 36px rgba(198, 40, 40, 0.28);
  cursor: pointer;
  transition: transform 220ms ease, box-shadow 220ms ease, filter 220ms ease;
  will-change: transform, box-shadow;
}

.chat-fab:hover,
.chat-fab:focus-visible {
  transform: translateY(-2px) translateZ(0);
  box-shadow: 0 24px 46px rgba(198, 40, 40, 0.34);
}

.chat-fab.has-unread {
  animation: fab-bounce 420ms cubic-bezier(.2, .8, .2, 1);
}

.chat-fab__pulse {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  border: 2px solid rgba(255, 255, 255, 0.24);
  animation: pulse 1600ms ease-out infinite;
}

.chat-fab__icon {
  width: 28px;
  height: 28px;
}

@keyframes pulse {
  0% {
    transform: scale(0.92);
    opacity: 0.85;
  }
  100% {
    transform: scale(1.22);
    opacity: 0;
  }
}

@keyframes fab-bounce {
  0% { transform: translateY(12px) translateZ(0); }
  50% { transform: translateY(-5px) translateZ(0); }
  100% { transform: translateY(0) translateZ(0); }
}

.chat-badge {
  position: absolute;
  top: -2px;
  left: -4px;
  min-width: 24px;
  height: 24px;
  padding: 0 7px;
  border-radius: 999px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.8rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(135deg, #fb923c, #f97316);
  box-shadow: 0 10px 24px rgba(249, 115, 22, 0.24);
}

.chat-window {
  width: min(360px, calc(100vw - 28px));
  height: min(470px, calc(100dvh - 72px));
  max-width: calc(100vw - 28px);
  max-height: calc(100dvh - 72px);
  display: flex;
  flex-direction: column;
  border-radius: 18px;
  overflow: hidden;
  background: linear-gradient(180deg, #1b1b1b 0%, #111111 100%);
  border: 1px solid var(--chat-border);
  box-shadow: var(--chat-shadow);
  backdrop-filter: blur(18px);
  /* CSS variable that holds the header height (px). Updated at runtime for dynamic header sizes. Default is a sensible fallback. */
  --chat-header-height: 64px;
  transform-origin: bottom right;
}

.chat-window[hidden] {
  display: none !important;
}

.chat-window.is-open {
  animation: chat-open 240ms cubic-bezier(.2, .8, .2, 1) both;
}

.chat-window.is-closing {
  animation: chat-close 220ms ease forwards;
}

.chat-window.is-minimized {
  height: 74px;
  width: 320px;
  max-width: min(320px, calc(100vw - 32px));
  grid-template-rows: auto;
}

.chat-window.is-minimized .chat-phone,
.chat-window.is-minimized .chat-messages,
.chat-window.is-minimized .chat-input {
  display: none !important;
}

.chat-window.is-minimized .chat-header {
  border-bottom: 0;
  padding: 14px 16px;
}

@keyframes chat-open {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes chat-close {
  from {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
  to {
    opacity: 0;
    transform: translateY(14px) scale(0.98);
  }
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.chat-header__info {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.chat-header__avatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, rgba(198, 40, 40, 0.22), rgba(166, 27, 27, 0.28));
  font-size: 1.2rem;
}

.chat-header__title {
  margin: 0 0 2px;
  font-size: 1rem;
  color: var(--chat-text);
}

.chat-header__subtitle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin: 0;
  font-size: 0.9rem;
  color: var(--chat-muted);
}

.chat-status-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 999px;
  background: #22c55e;
  box-shadow: 0 0 0 4px rgba(34, 197, 94, 0.14);
}

.chat-header__actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-header__btn {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--chat-text);
  background: rgba(255, 255, 255, 0.06);
  cursor: pointer;
  transition: background 180ms ease, transform 180ms ease;
}

.chat-header__btn:hover,
.chat-header__btn:focus-visible {
  background: rgba(255, 255, 255, 0.12);
  transform: translateY(-1px);
}

.chat-phone,
.chat-messages,
.chat-input {
  padding: 12px 14px 14px;
}

.chat-phone {
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 10px;
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
}

.chat-phone__icon {
  width: 64px;
  height: 64px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  background: rgba(198, 40, 40, 0.12);
  color: #ffb5b5;
  margin: 0 auto;
}

.chat-phone__title {
  margin: 0;
  font-size: 1.4rem;
  color: var(--chat-text);
  text-align: center;
}

.chat-phone__message {
  margin: 0;
  text-align: center;
  color: var(--chat-muted);
  line-height: 1.7;
}

.chat-phone__form {
  display: grid;
  gap: 10px;
}

.chat-phone__input-group {
  position: relative;
}

.chat-phone__input {
  width: 100%;
  min-height: 52px;
  padding: 0 16px 0 54px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  color: var(--chat-text);
  background: rgba(255, 255, 255, 0.06);
  font-size: 1rem;
}

.chat-phone__input::placeholder {
  color: rgba(255, 255, 255, 0.52);
}

.chat-phone__input:focus {
  outline: none;
  border-color: rgba(198, 40, 40, 0.68);
  box-shadow: 0 0 0 4px rgba(198, 40, 40, 0.16);
}

.chat-phone__submit {
  position: absolute;
  right: 8px;
  left: auto;
  top: 50%;
  transform: translateY(-50%);
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent-strong));
  cursor: pointer;
}

.chat-phone__error {
  margin: 0;
  color: #ff9aa5;
  font-size: 0.92rem;
  line-height: 1.6;
}

.chat-messages {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  overflow: hidden;
  /* Ensure the messages area starts below the header. The header height is provided by --chat-header-height (px).
     A small extra gap (8px) is added for comfortable spacing. The runtime script updates the variable on desktop.
  */
  padding-top: calc(var(--chat-header-height, 64px) + 8px);
}

.chat-messages__list {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 2px 2px 2px 0;
}

.chat-messages__list::-webkit-scrollbar {
  width: 8px;
}

.chat-messages__list::-webkit-scrollbar-track {
  background: transparent;
}

.chat-messages__list::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 999px;
}

.chat-message {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: fit-content;
  max-width: 86%;
  min-width: 0;
}

.chat-message + .chat-message {
  margin-top: 12px;
}

.chat-message--user {
  align-self: flex-end;
  align-items: flex-end;
}

.chat-message--admin {
  align-self: flex-start;
  align-items: flex-start;
}

.chat-message--user .chat-message__bubble,
.chat-message--user .chat-message__meta {
  align-self: flex-end;
}

.chat-message--admin .chat-message__bubble,
.chat-message--admin .chat-message__meta {
  align-self: flex-start;
}

.chat-message--admin .chat-message__meta {
  order: -1;
}

.chat-message--admin .chat-message__bubble {
  order: 0;
}

.chat-message__bubble {
  margin: 0;
}

.chat-message__meta {
  margin: 0;
}

.chat-message__bubble {
  display: block;
  width: fit-content;
  max-width: 100%;
  padding: 12px 14px;
  border-radius: 16px;
  line-height: 1.7;
  color: var(--chat-text);
  font-size: 0.95rem;
  background: #1e1e1e;
  border: 1px solid rgba(255, 255, 255, 0.06);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.chat-message--user .chat-message__bubble {
  background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent-strong));
  border-color: transparent;
  border-top-right-radius: 6px;
}

.chat-message--admin .chat-message__bubble {
  background: linear-gradient(135deg, rgba(198, 40, 40, 0.12), rgba(255, 255, 255, 0.04));
  border: 1px solid rgba(198, 40, 40, 0.28);
  border-top-left-radius: 6px;
}

.chat-message__meta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  width: fit-content;
  font-size: 0.8rem;
  color: var(--chat-muted);
  line-height: 1.5;
}

.chat-message--user .chat-message__meta {
  justify-content: flex-end;
}

.chat-message--admin .chat-message__meta {
  justify-content: flex-start;
}

.chat-typing {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 4px;
  color: var(--chat-muted);
  font-size: 0.9rem;
}

.chat-typing__dots {
  display: inline-flex;
  gap: 6px;
}

.chat-typing__dot {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.75);
  animation: blink 1.2s ease-in-out infinite;
}

.chat-typing__dot:nth-child(2) {
  animation-delay: 0.12s;
}

.chat-typing__dot:nth-child(3) {
  animation-delay: 0.24s;
}

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

.chat-input {
  flex: 0 0 auto;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.chat-input__form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
}

.chat-input__field {
  min-width: 0;
  width: 100%;
  min-height: 46px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  padding: 0 14px;
  color: var(--chat-text);
  background: rgba(255, 255, 255, 0.06);
}

.chat-input__field:focus {
  outline: none;
  border-color: rgba(198, 40, 40, 0.68);
  box-shadow: 0 0 0 4px rgba(198, 40, 40, 0.16);
}

.chat-input__send {
  width: 46px;
  height: 46px;
  border: 0;
  border-radius: 14px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(135deg, var(--chat-accent), var(--chat-accent-strong));
  cursor: pointer;
  transition: transform 180ms ease, filter 180ms ease;
}

.chat-input__send:hover:not(:disabled),
.chat-input__send:focus-visible:not(:disabled) {
  transform: translateY(-1px);
  filter: brightness(1.05);
}

.chat-input__send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

@media (min-width: 768px) and (max-width: 1023px) {
  .chat-widget {
    right: 18px;
    bottom: max(18px, env(safe-area-inset-bottom));
  }

  .chat-window {
    width: min(360px, calc(100vw - 28px));
    height: min(520px, calc(100dvh - 28px));
  }
}

@media (max-width: 767px) {
  .chat-widget {
    right: 8px;
    bottom: max(8px, env(safe-area-inset-bottom));
    left: 8px;
    align-items: stretch;
    padding: 0;
    max-width: calc(100vw - 16px);
  }

  .chat-fab {
    width: 60px;
    height: 60px;
    margin: 0 0 0 auto;
  }


  .chat-window {
    width: min(100%, calc(100vw - 16px));
    height: min(82dvh, 560px);
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 16px);
    border-radius: 18px;
    margin: 0 auto;
  }

  .chat-window.is-minimized {
    width: min(320px, calc(100vw - 16px));
    height: 74px;
    max-width: calc(100vw - 16px);
    border-radius: 18px;
    margin: 0 0 0 auto;
  }

  .chat-phone,
  .chat-messages,
  .chat-input {
    padding: 12px 12px 14px;
  }

  .chat-message {
    max-width: 88%;
  }

  .chat-message + .chat-message {
    margin-top: 14px;
  }

  .chat-input__field {
    min-height: 46px;
  }

  .chat-input__send {
    width: 46px;
  }
}

@media (max-width: 767px) and (orientation: landscape) {
  .chat-widget {
    right: 6px;
    bottom: max(6px, env(safe-area-inset-bottom));
    left: 6px;
  }

  .chat-window {
    height: min(58dvh, 420px);
    max-height: calc(100dvh - 12px);
  }

  .chat-header {
    padding: 10px 12px 10px;
  }

  .chat-header__avatar {
    width: 34px;
    height: 34px;
  }

  .chat-header__title {
    font-size: 0.95rem;
  }

  .chat-header__subtitle {
    font-size: 0.76rem;
  }

  .chat-phone,
  .chat-messages,
  .chat-input {
    padding: 10px 12px 12px;
  }

  .chat-phone__icon {
    width: 52px;
    height: 52px;
  }

  .chat-phone__title {
    font-size: 1.1rem;
  }

  .chat-input__field {
    min-height: 42px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}
