/* Chat module - clean fixed layout */
.chat-floating-btn {
  position: fixed;
  top: 16px;
  left: 16px;
  width: 50px;
  height: 50px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.18);
  background: rgba(18,18,28,.88);
  color: #fff;
  font-size: 22px;
  z-index: 2147483000;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 12px 30px rgba(0,0,0,.38);
  backdrop-filter: blur(12px);
}

.chat-red-dot {
  position: absolute;
  top: 7px;
  right: 7px;
  width: 11px;
  height: 11px;
  background: #ff2d55;
  border: 2px solid rgba(18,18,28,.95);
  border-radius: 50%;
  display: none;
}

.chat-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.35);
  z-index: 2147482998;
  display: none;
}

.chat-backdrop.open {
  display: block;
}

.chat-panel {
  position: fixed;
  top: 0;
  right: -390px;
  width: 370px;
  max-width: 94vw;
  height: 100dvh;
  max-height: 100dvh;
  background: rgba(14,14,24,.98);
  color: #fff;
  z-index: 2147483001;
  transition: right .22s ease;
  display: flex;
  flex-direction: column;
  box-shadow: -12px 0 38px rgba(0,0,0,.45);
  overflow: hidden;
}

.chat-panel.open {
  right: 0;
}

/* TOP fixed area */
.chat-header {
  flex: 0 0 auto;
  padding: 13px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(255,255,255,.075);
  border-bottom: 1px solid rgba(255,255,255,.12);
}

.chat-header strong {
  font-size: 16px;
}

.chat-header button {
  background: transparent;
  border: none;
  color: #fff;
  font-size: 25px;
  cursor: pointer;
}

.chat-profile-box {
  flex: 0 0 auto;
  padding: 8px 10px;
  border-bottom: 1px solid rgba(255,255,255,.10);
  background: rgba(255,255,255,.035);
  max-height: 140px;
  overflow-y: auto;
  box-sizing: border-box;
}

.chat-avatar-list {
  display: flex;
  gap: 7px;
  overflow-x: auto;
  padding: 6px 1px 4px;
  margin-bottom: 6px;
}

.chat-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
  cursor: pointer;
  border: 2px solid transparent;
  flex: 0 0 32px;
}

.chat-avatar.selected {
  border-color: #fff;
  box-shadow: 0 0 0 2px rgba(255,255,255,.18);
}

.chat-profile-row {
  display: flex;
  gap: 7px;
  align-items: center;
}

.chat-profile-box input {
  flex: 1;
  min-width: 0;
  height: 36px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.12);
  background: rgba(255,255,255,.10);
  color: #fff;
  outline: none;
  box-sizing: border-box;
}

.chat-profile-box input::placeholder {
  color: rgba(255,255,255,.58);
}

.chat-profile-box button {
  flex: 0 0 auto;
  height: 36px;
  border: none;
  border-radius: 999px;
  padding: 0 12px;
  cursor: pointer;
  background: rgba(255,255,255,.90);
  color: #111;
  font-weight: 700;
}

.chat-error {
  flex: 0 0 auto;
  color: #ff9a9a;
  font-size: 12px;
  padding: 6px 10px;
  display: none;
  background: rgba(14,14,24,.98);
}

.chat-error.show {
  display: block;
}

/* MIDDLE scroll area only */
.chat-messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 10px 10px 8px;
  -webkit-overflow-scrolling: touch;
  box-sizing: border-box;
}

.chat-empty {
  opacity: .55;
  text-align: center;
  font-size: 13px;
  margin-top: 20px;
}

/* Messages */
.chat-message {
  width: 100%;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  margin-bottom: 6px;
  box-sizing: border-box;
}

.chat-message img {
  width: 26px;
  height: 26px;
  flex: 0 0 26px;
  object-fit: cover;
  border-radius: 50%;
}

.chat-message-other {
  justify-content: flex-start;
  padding-right: 28px;
}

.chat-message-own {
  justify-content: flex-end;
  padding-left: 42px;
}

.chat-message-own img {
  display: none !important;
}

.chat-bubble {
  width: fit-content;
  max-width: min(74%, 480px);
  padding: 7px 10px;
  border-radius: 18px;
  line-height: 1.32;
  box-shadow: none;
  box-sizing: border-box;
}

.chat-message-own .chat-bubble {
  margin-left: auto;
  margin-right: 0;
  background: rgba(72,211,136,.22);
  border: 1px solid rgba(72,211,136,.18);
  border-bottom-right-radius: 5px;
}

.chat-message-other .chat-bubble {
  margin-right: auto;
  margin-left: 0;
  background: rgba(255,255,255,.115);
  border: 1px solid rgba(255,255,255,.08);
  border-bottom-left-radius: 5px;
}

.chat-name {
  font-size: 10.5px;
  opacity: .65;
  margin-bottom: 1px;
  line-height: 1.1;
}

.chat-message-own .chat-name {
  display: none;
}

.chat-text {
  font-size: 13.5px;
  line-height: 1.32;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}

.chat-time {
  font-size: 9.5px;
  opacity: .48;
  margin-top: 2px;
  line-height: 1;
}

.chat-message-own .chat-time {
  text-align: right;
}

.chat-delete {
  display: block;
  background: transparent !important;
  border: none !important;
  color: #ff9a9a !important;
  cursor: pointer;
  font-size: 10px !important;
  opacity: .6;
  margin-top: 2px !important;
  padding: 0 !important;
}

/* BOTTOM fixed composer */
.chat-send-row {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid rgba(255,255,255,.14);
  background: rgba(14,14,24,.99);
  box-sizing: border-box;
}

.chat-send-row input {
  flex: 1 1 auto;
  min-width: 0;
  height: 44px;
  padding: 0 14px;
  border-radius: 999px;
  border: none;
  background: #fff;
  color: #111;
  font-size: 16px;
  outline: none;
  box-sizing: border-box;
}

.chat-send-row button {
  flex: 0 0 58px;
  width: 58px;
  height: 44px;
  border: none;
  border-radius: 999px;
  padding: 0;
  cursor: pointer;
  background: rgba(255,255,255,.90);
  color: #111;
  font-weight: 700;
}

@media (max-width: 700px) {
  .chat-floating-btn {
    top: 14px;
    left: 14px;
    width: 48px;
    height: 48px;
  }

  .chat-panel {
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100svh;
    max-height: 100svh;
    right: -100vw;
    border-radius: 0;
  }

  .chat-panel.open {
    right: 0 !important;
  }

  .chat-header {
    padding-top: calc(12px + env(safe-area-inset-top));
  }

  .chat-profile-box {
    max-height: 128px;
    padding: 7px 9px;
  }

  .chat-avatar-list {
    gap: 6px;
    padding-top: 4px;
    margin-bottom: 5px;
  }

  .chat-avatar {
    width: 30px;
    height: 30px;
    flex-basis: 30px;
  }

  .chat-profile-box input {
    height: 34px;
    font-size: 14px;
  }

  .chat-profile-box button {
    height: 34px;
    font-size: 13px;
  }

  .chat-messages {
    padding-left: 7px;
    padding-right: 7px;
  }

  .chat-message {
    gap: 5px;
    margin-bottom: 5px;
  }

  .chat-message img {
    width: 24px;
    height: 24px;
    flex-basis: 24px;
  }

  .chat-message-own {
    padding-left: 42px;
  }

  .chat-message-other {
    padding-right: 22px;
  }

  .chat-bubble {
    max-width: calc(100vw - 72px);
    padding: 6px 9px;
    border-radius: 17px;
  }

  .chat-text {
    font-size: 13.2px;
    line-height: 1.3;
  }

  .chat-send-row {
    padding: 9px 9px calc(9px + env(safe-area-inset-bottom));
  }

  body.chat-panel-open {
    overflow: hidden;
  }
}

@supports not (height: 100svh) {
  @media (max-width: 700px) {
    .chat-panel {
      height: 100vh;
      max-height: 100vh;
    }
  }
}


/* TOP PROFILE UI FIX: full-width username + save button */
.chat-profile-box {
  padding: 8px 10px 9px !important;
  max-height: 165px !important;
  overflow-y: auto !important;
}

.chat-profile-row {
  display: flex !important;
  flex-direction: column !important;
  gap: 7px !important;
  width: 100% !important;
}

.chat-profile-box input,
#chatNameInput {
  display: block !important;
  width: 100% !important;
  flex: none !important;
  min-width: 0 !important;
  height: 38px !important;
  min-height: 38px !important;
  padding: 0 13px !important;
  border-radius: 999px !important;
  box-sizing: border-box !important;
  font-size: 14px !important;
  background: rgba(255,255,255,0.12) !important;
  color: #fff !important;
}

.chat-profile-box button,
#saveChatProfileBtn {
  display: block !important;
  width: 100% !important;
  flex: none !important;
  height: 36px !important;
  min-height: 36px !important;
  border-radius: 999px !important;
  box-sizing: border-box !important;
  font-size: 13px !important;
  font-weight: 700 !important;
}

.chat-avatar-list {
  width: 100% !important;
  display: flex !important;
  align-items: center !important;
  gap: 8px !important;
  overflow-x: auto !important;
  padding: 4px 1px 6px !important;
  margin-bottom: 6px !important;
  box-sizing: border-box !important;
}

.chat-avatar {
  width: 34px !important;
  height: 34px !important;
  flex: 0 0 34px !important;
}

@media (max-width: 700px) {
  .chat-profile-box {
    max-height: 158px !important;
    padding: 7px 9px 8px !important;
  }

  .chat-profile-box input,
  #chatNameInput {
    height: 38px !important;
    min-height: 38px !important;
  }

  .chat-profile-box button,
  #saveChatProfileBtn {
    height: 35px !important;
    min-height: 35px !important;
  }
}


/* ANDROID CHAT SCROLL FIX + AI BOT REPLACEMENT TARGET */
.chat-panel {
  contain: layout paint;
}

.chat-messages,
#chatMessages {
  flex: 1 1 auto !important;
  min-height: 0 !important;
  overflow-y: auto !important;
  overflow-x: hidden !important;
  -webkit-overflow-scrolling: touch !important;
  overscroll-behavior: contain !important;
  touch-action: pan-y !important;
  padding-bottom: 14px !important;
}

.chat-send-row,
#chatSendForm {
  flex: 0 0 auto !important;
  position: relative !important;
  z-index: 3 !important;
}

.chat-header,
.chat-profile-box,
.chat-error {
  flex: 0 0 auto !important;
}

@media (max-width: 700px) {
  .chat-panel {
    height: 100dvh !important;
    max-height: 100dvh !important;
    min-height: 100dvh !important;
  }

  @supports (height: 100svh) {
    .chat-panel {
      height: 100svh !important;
      max-height: 100svh !important;
      min-height: 100svh !important;
    }
  }

  .chat-messages,
  #chatMessages {
    min-height: 0 !important;
    max-height: none !important;
  }

  .chat-send-row input,
  #chatInput {
    font-size: 16px !important;
  }
}
