.chat-launcher,
.chat-panel,
.chat-panel * {
  box-sizing: border-box;
}

.chat-launcher {
  position: fixed;
  z-index: 950;
  right: max(18px, env(safe-area-inset-right));
  bottom: max(18px, env(safe-area-inset-bottom));
  width: 52px;
  height: 52px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: #fff;
  background: #0071e3;
  box-shadow: 0 10px 30px rgba(0, 66, 132, .28);
  display: grid;
  place-items: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: transform .28s cubic-bezier(.34, 1.56, .64, 1), box-shadow .2s ease;
}

.chat-launcher:hover { transform: translateY(-2px); box-shadow: 0 14px 34px rgba(0, 66, 132, .32); }
.chat-launcher:active { transform: scale(.96); }
.chat-launcher:focus-visible { outline: 3px solid rgba(0, 113, 227, .28); outline-offset: 3px; }

.chat-launcher-icon {
  width: 24px;
  height: 19px;
  border: 2px solid currentColor;
  border-radius: 8px;
  position: relative;
}

.chat-launcher-icon::after {
  content: "";
  position: absolute;
  left: 4px;
  bottom: -5px;
  width: 7px;
  height: 7px;
  border-left: 2px solid currentColor;
  transform: skewY(-35deg);
  background: #0071e3;
}

.chat-badge {
  position: absolute;
  top: -4px;
  right: -4px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  border: 2px solid #fff;
  border-radius: 10px;
  background: #ff3b30;
  color: #fff;
  font: 700 11px/16px -apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif;
  text-align: center;
}

.chat-panel {
  --chat-border: rgba(0, 0, 0, .09);
  --chat-muted: #6e6e73;
  --chat-surface: #fff;
  position: fixed;
  z-index: 960;
  right: max(18px, env(safe-area-inset-right));
  bottom: calc(max(18px, env(safe-area-inset-bottom)) + 64px);
  width: min(380px, calc(100vw - 36px));
  height: min(560px, calc(100dvh - 110px));
  overflow: hidden;
  border: 1px solid var(--chat-border);
  border-radius: 24px;
  background: rgba(255, 255, 255, .96);
  box-shadow: 0 24px 70px rgba(0, 0, 0, .2);
  backdrop-filter: saturate(180%) blur(22px);
  -webkit-backdrop-filter: saturate(180%) blur(22px);
  color: #1d1d1f;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  transform-origin: bottom right;
  animation: chat-enter .22s cubic-bezier(.2, .7, .2, 1);
}

@keyframes chat-enter { from { opacity: 0; transform: translateY(12px) scale(.98); } }

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

.chat-header {
  min-height: 74px;
  padding: 17px 16px 14px 18px;
  border-bottom: 1px solid var(--chat-border);
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-agent-mark {
  width: 38px;
  height: 38px;
  flex: 0 0 auto;
  min-height: 44px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(145deg, #2997ff, #0066cc);
  font-size: 15px;
  font-weight: 700;
}

.chat-heading { min-width: 0; flex: 1; }
.chat-heading strong { display: block; font-size: 16px; line-height: 1.35; letter-spacing: -.01em; }
.chat-heading span { display: block; margin-top: 2px; color: var(--chat-muted); font-size: 12px; line-height: 1.35; }

.chat-close {
  width: 44px;
  height: 44px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: #f2f2f7;
  color: #3a3a3c;
  cursor: pointer;
  font-size: 22px;
  line-height: 1;
}
.chat-close:focus-visible { outline: 2px solid #0071e3; outline-offset: 2px; }

.chat-context {
  min-height: 43px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--chat-border);
  display: flex;
  align-items: center;
  gap: 7px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chat-context::-webkit-scrollbar { display: none; }

.chat-context-pill {
  max-width: 230px;
  flex: 0 0 auto;
  padding: 6px 10px;
  border: 0;
  border-radius: 999px;
  color: #3a3a3c;
  background: #f2f2f7;
  font: 600 12px/1.2 inherit;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}
.chat-context-pill[aria-pressed="true"] { color: #fff; background: #1d1d1f; }

.chat-messages {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px 14px 12px;
  scroll-behavior: smooth;
}

.chat-empty,
.chat-loading,
.chat-error {
  min-height: 100%;
  padding: 34px 18px;
  display: grid;
  place-content: center;
  text-align: center;
  color: var(--chat-muted);
  font-size: 13px;
  line-height: 1.6;
}
.chat-empty strong { color: #1d1d1f; display: block; margin-bottom: 5px; font-size: 17px; }

.chat-retry {
  justify-self: center;
  margin-top: 12px;
  padding: 8px 15px;
  border: 0;
  border-radius: 999px;
  background: #0071e3;
  color: #fff;
  font: 600 13px/1.2 inherit;
  cursor: pointer;
}

.chat-day {
  margin: 5px 0 13px;
  color: #86868b;
  font-size: 11px;
  text-align: center;
}

.chat-message { margin: 0 0 10px; display: flex; flex-direction: column; align-items: flex-start; }
.chat-message.is-user { align-items: flex-end; }
.chat-bubble {
  max-width: 82%;
  padding: 10px 12px;
  border-radius: 17px 17px 17px 5px;
  background: #f2f2f7;
  color: #1d1d1f;
  font-size: 14px;
  line-height: 1.48;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.chat-message.is-user .chat-bubble { color: #fff; background: #0071e3; border-radius: 17px 17px 5px 17px; }
.chat-message-time { padding: 4px 5px 0; color: #86868b; font-size: 10px; }

.chat-connection {
  padding: 5px 14px;
  color: #8a6200;
  background: #fff9e8;
  font-size: 11px;
  text-align: center;
}
.chat-connection[hidden] { display: none; }

.chat-quick {
  padding: 8px 12px 6px;
  display: flex;
  gap: 7px;
  overflow-x: auto;
  scrollbar-width: none;
}
.chat-quick::-webkit-scrollbar { display: none; }
.chat-quick button {
  flex: 0 0 auto;
  min-height: 44px;
  padding: 7px 11px;
  border: 1px solid rgba(0, 113, 227, .22);
  border-radius: 999px;
  color: #0066cc;
  background: rgba(0, 113, 227, .06);
  font: 500 12px/1.2 inherit;
  cursor: pointer;
}

.chat-compose {
  padding: 8px 10px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--chat-border);
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.chat-compose textarea {
  width: 100%;
  min-height: 44px;
  max-height: 96px;
  resize: none;
  padding: 10px 12px;
  border: 1px solid rgba(0, 0, 0, .13);
  border-radius: 18px;
  outline: 0;
  background: #f7f7f9;
  color: #1d1d1f;
  font: 14px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", sans-serif;
}
.chat-compose textarea:focus { border-color: #0071e3; box-shadow: 0 0 0 3px rgba(0, 113, 227, .12); }
.chat-send {
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  padding: 0;
  border: 0;
  border-radius: 50%;
  color: #fff;
  background: #0071e3;
  font-size: 18px;
  cursor: pointer;
}
.chat-send:disabled { color: #aaa; background: #e5e5ea; cursor: default; }

@media (max-width: 600px) {
  .chat-launcher {
    right: max(14px, env(safe-area-inset-right));
    bottom: max(84px, calc(env(safe-area-inset-bottom) + 16px));
  }
  .chat-panel {
    right: 0;
    bottom: var(--chat-keyboard-offset, 0px);
    width: 100%;
    height: min(76dvh, 680px, calc(var(--chat-viewport-height, 100dvh) - 12px));
    max-height: calc(var(--chat-viewport-height, 100dvh) - env(safe-area-inset-top) - 12px);
    border-width: 1px 0 0;
    border-radius: 24px 24px 0 0;
    transform-origin: bottom center;
  }
}

@media (prefers-color-scheme: dark) {
  .chat-panel {
    --chat-border: rgba(255, 255, 255, .11);
    --chat-muted: #98989d;
    --chat-surface: #1c1c1e;
    color: #f5f5f7;
    background: rgba(28, 28, 30, .97);
    box-shadow: 0 24px 70px rgba(0, 0, 0, .5);
  }
  .chat-badge { border-color: #1c1c1e; }
  .chat-close, .chat-context-pill, .chat-bubble { color: #f5f5f7; background: #2c2c2e; }
  .chat-context-pill[aria-pressed="true"] { color: #1d1d1f; background: #f5f5f7; }
  .chat-empty strong { color: #f5f5f7; }
  .chat-compose textarea { color: #f5f5f7; background: #2c2c2e; border-color: rgba(255, 255, 255, .14); }
  .chat-connection { color: #ffd60a; background: #322b08; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-panel { animation: none; }
  .chat-launcher, .chat-messages { transition: none; scroll-behavior: auto; }
}
