/* ============================================================
   Chat Page — CandyAI Telegram Mini App
   ============================================================ */

/* ---------- Local design tokens (fallback to tg-theme vars) -- */
:root {
  --chat-bg: var(--bg, var(--tg-theme-bg-color, #000000));
  --chat-card: var(--bg-card, var(--tg-theme-card-bg, #121212));
  --chat-text: var(--text, var(--tg-theme-text-color, #fff0fa));
  --chat-text-secondary: var(--text-secondary, var(--tg-theme-hint-color, #a97bb7));
  --chat-accent: var(--accent, var(--tg-theme-button-color, #ff4fa3));
  --chat-accent-gradient: var(--accent-gradient, linear-gradient(135deg, #ff4fa3, #c44fff));
  --chat-border: var(--border, rgba(255, 79, 163, 0.15));
  --chat-glass: rgba(12, 12, 12, 0.78);
  --chat-topbar-h: 56px;
  --chat-input-row-h: auto;
  --chat-safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ---------- Page wrapper ------------------------------------ */
#page-chat {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  position: relative;
  background: var(--chat-bg);
}

/* ---------- Top bar ----------------------------------------- */
.chat-topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 10px;
  height: var(--chat-topbar-h);
  padding: 0 12px;
  background: var(--chat-glass);
  -webkit-backdrop-filter: blur(24px);
  backdrop-filter: blur(24px);
  border-bottom: 1px solid var(--chat-border);
  flex-shrink: 0;
}

.chat-back-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--chat-accent);
  font-size: 22px;
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}
.chat-back-btn:active {
  opacity: 0.6;
}

.chat-topbar-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  border: 2px solid var(--chat-accent);
}

.chat-topbar-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}

.chat-topbar-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--chat-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-topbar-status {
  font-size: 12px;
  color: var(--chat-text-secondary);
}

.chat-loyalty-badge {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  background: rgba(255, 79, 163, 0.12);
  font-size: 12px;
  font-weight: 600;
  color: var(--chat-accent);
  flex-shrink: 0;
  white-space: nowrap;
}

/* ---------- Message list ------------------------------------ */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 12px 10px 16px;
  scroll-behavior: smooth;
}

/* ---------- Message row ------------------------------------- */
.chat-msg {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 84%;
  opacity: 1;
  transform: translateY(0);
}
.chat-msg.animate-in {
  animation: chatMsgIn 0.22s ease-out forwards;
}
.chat-msg--user {
  align-self: flex-end;
  flex-direction: row-reverse;
}
.chat-msg--girl {
  align-self: flex-start;
}

.chat-msg-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  margin-bottom: 2px;
}

/* ---------- Bubbles ----------------------------------------- */
.chat-bubble {
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
}

.chat-bubble--user {
  background: linear-gradient(135deg, #ff4fa3, #c44fff);
  color: #fff;
  border-radius: 16px 16px 4px 16px;
}

.chat-bubble--girl {
  background: var(--chat-card);
  border: 1px solid var(--chat-border);
  color: var(--chat-text);
  border-radius: 16px 16px 16px 4px;
}

/* ---------- Photo / Video bubble ---------------------------- */
.chat-bubble--media {
  padding: 4px;
  overflow: hidden;
  max-width: 260px;
}

.chat-bubble--media img,
.chat-bubble--media video {
  display: block;
  width: 100%;
  border-radius: 12px;
  cursor: pointer;
  object-fit: cover;
  max-height: 340px;
}

.chat-media-shimmer {
  width: 220px;
  height: 220px;
  border-radius: 12px;
  background: linear-gradient(110deg, var(--chat-card) 30%, rgba(255,255,255,0.04) 50%, var(--chat-card) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.4s ease-in-out infinite;
}

/* ---------- Typing indicator -------------------------------- */
.chat-typing {
  display: none;
  align-self: flex-start;
  align-items: flex-end;
  gap: 6px;
  padding: 0 10px;
}
.chat-typing.visible {
  display: flex;
}

.chat-typing-bubble {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 12px 18px;
  background: var(--chat-card);
  border: 1px solid var(--chat-border);
  border-radius: 16px 16px 16px 4px;
}

.chat-typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--chat-accent);
  opacity: 0.35;
  animation: typingBounce 0.6s ease-in-out infinite;
}
.chat-typing-dot:nth-child(2) { animation-delay: 0.15s; }
.chat-typing-dot:nth-child(3) { animation-delay: 0.30s; }

/* ---------- Action buttons (retry / continue) --------------- */
.chat-actions {
  display: flex;
  gap: 8px;
  padding: 4px 0 2px 36px;
  align-self: flex-start;
}
.chat-actions:empty {
  display: none;
}

.chat-action-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 500;
  color: var(--chat-text-secondary);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--chat-border);
  border-radius: 20px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s, color 0.15s;
}
.chat-action-btn:active {
  background: rgba(255, 79, 163, 0.15);
  color: var(--chat-accent);
}

/* ---------- Quick action bar -------------------------------- */
.chat-quick-actions {
  display: flex;
  gap: 8px;
  padding: 6px 12px 2px;
  flex-shrink: 0;
}

.chat-quick-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--chat-border);
  background: rgba(255, 255, 255, 0.04);
  font-size: 18px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s;
  flex-shrink: 0;
  padding: 0;
  color: var(--chat-text);
}
.chat-quick-btn:active {
  background: rgba(255, 79, 163, 0.15);
}
.chat-quick-btn.loading {
  opacity: 0.5;
  pointer-events: none;
}

/* ---------- Input area -------------------------------------- */
.chat-input-area {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 10px calc(8px + var(--chat-safe-bottom));
  background: var(--chat-glass);
  -webkit-backdrop-filter: blur(24px);
  backdrop-filter: blur(24px);
  border-top: 1px solid var(--chat-border);
  flex-shrink: 0;
}

.chat-input {
  flex: 1;
  resize: none;
  border: none;
  outline: none;
  background: rgba(255, 255, 255, 0.06);
  color: var(--chat-text);
  font-size: 15px;
  line-height: 1.4;
  padding: 10px 14px;
  border-radius: 22px;
  max-height: calc(1.4em * 4 + 20px); /* ~4 lines */
  overflow-y: auto;
  font-family: inherit;
  -webkit-appearance: none;
}
.chat-input::placeholder {
  color: var(--chat-text-secondary);
}

.chat-send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, #ff4fa3, #c44fff);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  flex-shrink: 0;
  padding: 0;
  transition: opacity 0.15s, transform 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.chat-send-btn:disabled {
  opacity: 0.35;
  cursor: default;
}
.chat-send-btn:active:not(:disabled) {
  transform: scale(0.92);
}

/* ---------- Messages left badge ----------------------------- */
.chat-messages-left {
  text-align: center;
  font-size: 12px;
  color: var(--chat-text-secondary);
  padding: 4px 0 0;
  flex-shrink: 0;
}

/* ---------- Donation modal tiers ---------------------------- */
.chat-donation-tier {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--chat-border);
  cursor: pointer;
  transition: background 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.chat-donation-tier:active {
  background: rgba(255, 79, 163, 0.12);
}
.chat-donation-tier-emoji {
  font-size: 28px;
  flex-shrink: 0;
}
.chat-donation-tier-info {
  flex: 1;
  min-width: 0;
}
.chat-donation-tier-name {
  font-size: 15px;
  font-weight: 600;
  color: var(--chat-text);
}
.chat-donation-tier-desc {
  font-size: 12px;
  color: var(--chat-text-secondary);
  margin-top: 2px;
}
.chat-donation-tier-cost {
  font-size: 14px;
  font-weight: 600;
  color: var(--chat-accent);
  flex-shrink: 0;
}

/* ---------- Scenario modal ---------------------------------- */
.chat-scenario-textarea {
  width: 100%;
  min-height: 120px;
  resize: vertical;
  border: 1px solid var(--chat-border);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.04);
  color: var(--chat-text);
  font-size: 14px;
  line-height: 1.5;
  padding: 12px;
  font-family: inherit;
  outline: none;
  box-sizing: border-box;
}
.chat-scenario-textarea:focus {
  border-color: var(--chat-accent);
}
.chat-scenario-info {
  font-size: 12px;
  color: var(--chat-text-secondary);
  margin-top: 8px;
}
.chat-scenario-save {
  display: block;
  width: 100%;
  margin-top: 14px;
  padding: 14px;
  border-radius: 14px;
  border: none;
  background: linear-gradient(135deg, #ff4fa3, #c44fff);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.chat-scenario-save:active {
  opacity: 0.8;
}

/* ---------- Photo fullscreen modal -------------------------- */
.chat-photo-fullscreen {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  box-sizing: border-box;
}
.chat-photo-fullscreen img {
  max-width: 100%;
  max-height: 80vh;
  border-radius: 12px;
  object-fit: contain;
}

/* ---------- Keyframes --------------------------------------- */
@keyframes chatMsgIn {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes typingBounce {
  0%, 100% { transform: translateY(0); opacity: 0.35; }
  50%      { transform: translateY(-5px); opacity: 1; }
}

@keyframes shimmer {
  0%   { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}
