.chat-bubble {
  position: fixed;
  right: max(0.9rem, env(safe-area-inset-right));
  bottom: calc(max(0.9rem, env(safe-area-inset-bottom)) + 0.1rem);
  z-index: 1300;
  min-height: 3.2rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: linear-gradient(130deg, var(--brand), var(--brand-strong));
  box-shadow: 0 16px 30px rgba(0, 0, 0, 0.32);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.58rem 0.86rem 0.58rem 0.95rem;
  cursor: pointer;
  transition: transform 0.25s ease, box-shadow 0.25s ease, padding 0.28s ease, width 0.28s ease;
}

.chat-bubble:hover,
.chat-bubble:focus-visible {
  transform: translateY(-2px) scale(1.02);
}

.chat-bubble.is-compact {
  width: 3.2rem;
  min-width: 3.2rem;
  padding: 0;
  gap: 0;
}

.chat-bubble-label {
  color: #fff;
  font-size: 0.83rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
  overflow: hidden;
  max-width: 8.4rem;
  opacity: 1;
  transform: translateX(0);
  transition: opacity 0.25s ease, max-width 0.25s ease, transform 0.25s ease;
}

.chat-bubble.is-compact .chat-bubble-label {
  opacity: 0;
  max-width: 0;
  transform: translateX(0.4rem);
}

.chat-bubble img {
  width: 1.32rem;
  height: 1.32rem;
  display: block;
  flex: 0 0 auto;
  filter: invert(1);
}

.chat-widget {
  position: fixed;
  right: max(0.75rem, env(safe-area-inset-right));
  bottom: calc(4.9rem + env(safe-area-inset-bottom));
  z-index: 1300;
  width: min(25rem, calc(100vw - 1.5rem - env(safe-area-inset-left) - env(safe-area-inset-right)));
  max-height: min(35rem, calc(100dvh - 6.5rem - env(safe-area-inset-top)));
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  overflow: hidden;
  background: color-mix(in srgb, var(--surface-strong) 95%, transparent);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  opacity: 1;
  transform: translateY(0) scale(1);
  transform-origin: right bottom;
  transition: opacity 0.24s ease, transform 0.24s ease;
}

.chat-widget.is-entering {
  opacity: 0;
  transform: translateY(0.75rem) scale(0.985);
}

.chat-widget[hidden] {
  display: none;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  align-items: start;
  padding: 0.9rem;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
}

.chat-header strong {
  font-family: var(--font-head);
  display: block;
  line-height: 1.2;
  font-size: 0.93rem;
}

.chat-header p {
  margin: 0.35rem 0 0;
  font-size: 0.78rem;
  color: var(--text-soft);
}

.chat-close {
  border: 1px solid var(--line);
  background: var(--surface-strong);
  color: var(--text);
  border-radius: 999px;
  width: 1.95rem;
  height: 1.95rem;
  cursor: pointer;
}

.chat-messages {
  height: min(22rem, calc(100dvh - 16rem));
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0.8rem;
  display: grid;
  gap: 0.6rem;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
}

.chat-msg {
  max-width: 92%;
  padding: 0.56rem 0.68rem;
  border-radius: 0.75rem;
  border: 1px solid var(--line);
  font-size: clamp(0.86rem, 0.8rem + 0.2vw, 0.95rem);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  opacity: 1;
  transform: translateY(0);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.chat-msg.is-entering {
  opacity: 0;
  transform: translateY(0.4rem);
}

.chat-msg.assistant {
  justify-self: start;
  background: color-mix(in srgb, var(--surface-strong) 92%, transparent);
  color: var(--text);
}

.chat-msg.user {
  justify-self: end;
  background: linear-gradient(130deg, var(--brand), var(--brand-strong));
  color: #fff;
  border-color: transparent;
}

.chat-msg.error {
  justify-self: start;
  background: rgba(219, 47, 60, 0.15);
  border-color: rgba(219, 47, 60, 0.45);
  color: var(--text);
}

.chat-msg.typing {
  justify-self: start;
  color: var(--text-soft);
  font-style: italic;
}

.chat-form {
  border-top: 1px solid var(--line);
  padding: 0.72rem;
  display: grid;
  gap: 0.5rem;
  background: color-mix(in srgb, var(--surface-strong) 95%, transparent);
}

.chat-label {
  font-size: 0.76rem;
  color: var(--text-soft);
}

.chat-form textarea {
  width: 100%;
  resize: none;
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: var(--surface-strong);
  color: var(--text);
  padding: 0.56rem;
  font-family: var(--font-body);
  font-size: max(16px, 0.93rem);
  line-height: 1.45;
}

.chat-form button {
  width: 100%;
  min-height: 2.65rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid transparent;
  border-radius: 999px;
  background: linear-gradient(130deg, var(--brand), var(--brand-strong));
  color: #fff;
  box-shadow: 0 10px 18px rgba(0, 0, 0, 0.22);
  padding: 0.63rem 0.9rem;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  cursor: pointer;
}

.chat-form button:hover,
.chat-form button:focus-visible {
  border-color: color-mix(in srgb, #fff 45%, transparent);
}

.chat-form button:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

@media (max-width: 640px) {
  .chat-widget {
    right: max(0.5rem, env(safe-area-inset-right));
    left: max(0.5rem, env(safe-area-inset-left));
    width: auto;
    bottom: calc(4.55rem + env(safe-area-inset-bottom));
    max-height: min(37rem, calc(100dvh - 5.8rem - env(safe-area-inset-top)));
  }

  .chat-messages {
    height: min(24rem, calc(100dvh - 15.2rem));
    padding: 0.72rem;
  }

  .chat-bubble {
    right: max(0.62rem, env(safe-area-inset-right));
    bottom: calc(max(0.62rem, env(safe-area-inset-bottom)) + 0.05rem);
  }

  .chat-bubble-label {
    font-size: 0.86rem;
  }
}

@media (max-width: 390px) {
  .chat-widget {
    bottom: calc(4.35rem + env(safe-area-inset-bottom));
    max-height: min(39rem, calc(100dvh - 5.3rem - env(safe-area-inset-top)));
  }

  .chat-header {
    padding: 0.82rem;
  }

  .chat-header strong {
    font-size: 0.9rem;
  }

  .chat-header p {
    font-size: 0.8rem;
  }

  .chat-msg {
    max-width: 95%;
    font-size: 0.92rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .chat-widget,
  .chat-msg,
  .chat-bubble,
  .chat-bubble-label {
    transition: none !important;
  }
}
