/* =============================================================
   ULS Chat — iOS iMessage-inspired UI
   ============================================================= */

/* ── CUSTOM PROPERTIES ───────────────────────────────────────── */
:root {
  /* iOS palette */
  --blue:          #007AFF;
  --blue-dark:     #0062CC;
  --blue-light:    #4DA3FF;
  --green:         #34C759;
  --red:           #FF3B30;
  --gray-1:        #F2F2F7;
  --gray-2:        #E5E5EA;
  --gray-3:        #C7C7CC;
  --gray-4:        #8E8E93;
  --gray-5:        #636366;
  --label:         #1C1C1E;
  --label-2:       #3A3A3C;
  --separator:     rgba(60,60,67,0.12);
  --bg:            #F2F2F7;
  --surface:       #FFFFFF;

  /* Bubbles */
  --bubble-user:   #007AFF;
  --bubble-agent:  #FFFFFF;
  --text-user:     #FFFFFF;
  --text-agent:    #1C1C1E;

  /* Layout */
  --header-h:      68px;
  --input-h:       72px;
  --max-w:         480px;
  --radius-bubble: 20px;
  --radius-chip:   20px;

  /* Shadows */
  --shadow-xs: 0 1px 3px rgba(0,0,0,0.07);
  --shadow-sm: 0 2px 8px rgba(0,0,0,0.08);
  --shadow-md: 0 8px 32px rgba(0,0,0,0.10);
  --shadow-lg: 0 20px 60px rgba(0,0,0,0.14);

  /* Typography */
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Inter', 'Helvetica Neue', Arial, sans-serif;
}

/* ── RESET ───────────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

/* ── BASE ────────────────────────────────────────────────────── */
html {
  height: 100%;
  background: var(--bg);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-size-adjust: 100%;
}

body {
  min-height: 100%;
  min-height: 100dvh;
  background: var(--bg);
  display: flex;
  justify-content: center;
  align-items: center;
}

/* ── BACKGROUND GLOW (desktop only) ─────────────────────────── */
.bg-glow {
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(ellipse 60% 50% at 30% 20%, hsla(210,100%,70%,0.18) 0%, transparent 70%),
    radial-gradient(ellipse 50% 60% at 70% 80%, hsla(250,90%,70%,0.14) 0%, transparent 70%);
  pointer-events: none;
}

/* ── CHAT WRAPPER ────────────────────────────────────────────── */
.chat-wrapper {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: var(--max-w);
  height: 100dvh;
  background: var(--bg);
  overflow: hidden;
}

/* ── HEADER ──────────────────────────────────────────────────── */
.chat-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  height: var(--header-h);
  background: rgba(255,255,255,0.82);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-bottom: 1px solid var(--separator);
  position: relative;
  z-index: 50;
}

/* Avatar */
.header-avatar-wrap {
  position: relative;
  flex-shrink: 0;
}

.header-avatar,
.header-avatar-fallback {
  width: 44px;
  height: 44px;
  border-radius: 50%;
}

.header-avatar {
  object-fit: cover;
  display: block;
}

.header-avatar-fallback {
  display: none;
  align-items: center;
  justify-content: center;
  background: linear-gradient(140deg, #007AFF 0%, #5AC8FA 100%);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: -0.5px;
  user-select: none;
}

.online-dot {
  position: absolute;
  bottom: 1px;
  right: 1px;
  width: 13px;
  height: 13px;
  background: var(--green);
  border-radius: 50%;
  border: 2.5px solid white;
  animation: pulse-online 2.4s ease-in-out infinite;
}

@keyframes pulse-online {
  0%, 100% { box-shadow: 0 0 0 0 rgba(52,199,89,0.5); }
  50%       { box-shadow: 0 0 0 5px rgba(52,199,89,0); }
}

/* Header text */
.header-info {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.header-name {
  font-size: 17px;
  font-weight: 600;
  color: var(--label);
  letter-spacing: -0.3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  font-size: 12px;
  font-weight: 500;
  color: var(--green);
}

/* Icon button */
.icon-btn {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border-radius: 10px;
  border: none;
  background: none;
  cursor: pointer;
  color: var(--gray-4);
  transition: background 0.14s, color 0.14s;
}

.icon-btn:hover {
  background: var(--gray-2);
  color: var(--red);
}

.icon-btn:active {
  opacity: 0.6;
}

/* ── MESSAGES AREA ───────────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  overscroll-behavior: contain;
  padding: 12px 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-messages::-webkit-scrollbar         { width: 3px; }
.chat-messages::-webkit-scrollbar-track  { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb  { background: var(--gray-3); border-radius: 2px; }

/* ── DATE DIVIDER ────────────────────────────────────────────── */
.date-divider {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 2px 12px;
  color: var(--gray-4);
  font-size: 11px;
  font-weight: 500;
  letter-spacing: 0.4px;
  text-transform: uppercase;
}

.date-divider::before,
.date-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--separator);
}

/* ── MESSAGE ROW ─────────────────────────────────────────────── */
.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 100%;
}

.msg-row + .msg-row {
  margin-top: 2px;
}

/* Group spacing — add more space between different senders */
.msg-row.user  + .msg-row.agent,
.msg-row.agent + .msg-row.user {
  margin-top: 10px;
}

.msg-row.user  { align-items: flex-end;  }
.msg-row.agent { align-items: flex-start; }

/* Entrance animation */
@keyframes msg-in {
  from { opacity: 0; transform: translateY(8px) scale(0.98); }
  to   { opacity: 1; transform: translateY(0)   scale(1);    }
}

.msg-row.animated {
  animation: msg-in 0.2s cubic-bezier(0.34,1.26,0.64,1) both;
}

/* ── BUBBLE ──────────────────────────────────────────────────── */
.bubble {
  display: inline-block;
  max-width: 76%;
  padding: 10px 14px;
  border-radius: var(--radius-bubble);
  font-size: 15.5px;
  line-height: 1.45;
  word-break: break-word;
  white-space: pre-wrap;
}

/* User bubble — blue, right side, pointy bottom-right */
.msg-row.user .bubble {
  background: var(--bubble-user);
  color: var(--text-user);
  border-bottom-right-radius: 5px;
  box-shadow: 0 1px 3px rgba(0,122,255,0.25);
}

/* Agent bubble — white, left side, pointy bottom-left */
.msg-row.agent .bubble {
  background: var(--bubble-agent);
  color: var(--text-agent);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-xs);
}

/* Timestamp */
.bubble-time {
  font-size: 11px;
  color: var(--gray-4);
  margin-top: 3px;
  padding: 0 3px;
}

.msg-row.user  .bubble-time { text-align: right;  }
.msg-row.agent .bubble-time { text-align: left;   }

/* ── STREAMING CURSOR ────────────────────────────────────────── */
.stream-cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--gray-4);
  border-radius: 1px;
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 0.8s step-start infinite;
}

@keyframes blink {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0; }
}

/* ── TYPING INDICATOR ────────────────────────────────────────── */
.typing-row {
  align-items: flex-start;
}

.typing-bubble {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 12px 16px;
  background: var(--bubble-agent);
  border-radius: var(--radius-bubble);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-xs);
  width: fit-content;
}

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0);    background: var(--gray-3); }
  30%           { transform: translateY(-5px); background: var(--gray-5); }
}

.typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--gray-3);
  animation: typing-bounce 1.3s ease-in-out infinite;
}

.typing-dot:nth-child(1) { animation-delay: 0s;    }
.typing-dot:nth-child(2) { animation-delay: 0.18s; }
.typing-dot:nth-child(3) { animation-delay: 0.36s; }

/* ── CHIPS / QUICK REPLIES ───────────────────────────────────── */
.chips-wrap {
  margin-top: 10px;
  margin-left: 0;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 7px;
}

@keyframes chip-in {
  from { opacity: 0; transform: translateX(-6px); }
  to   { opacity: 1; transform: translateX(0);    }
}

.chip {
  padding: 9px 18px;
  border-radius: var(--radius-chip);
  border: 1.5px solid var(--blue);
  background: var(--surface);
  color: var(--blue);
  font-size: 15px;
  font-weight: 500;
  font-family: var(--font);
  line-height: 1.3;
  cursor: pointer;
  text-align: left;
  max-width: 300px;
  transition: background 0.13s ease, color 0.13s ease,
              transform 0.13s ease, box-shadow 0.13s ease;
  animation: chip-in 0.22s ease both;
}

.chip:hover {
  background: var(--blue);
  color: #fff;
  box-shadow: 0 3px 12px rgba(0,122,255,0.28);
  transform: translateY(-1px);
}

.chip:active {
  transform: scale(0.96) translateY(0);
  box-shadow: none;
}

/* Back chip */
.chip-back {
  border-color: var(--gray-3);
  color: var(--gray-5);
  font-size: 14px;
  padding: 7px 14px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.chip-back::before {
  content: '‹';
  font-size: 19px;
  font-weight: 300;
  line-height: 1;
  margin-top: -1px;
}

.chip-back:hover {
  background: var(--gray-2);
  color: var(--label);
  box-shadow: none;
}

/* Stagger delay helpers */
.chip:nth-child(1)  { animation-delay: 0ms;   }
.chip:nth-child(2)  { animation-delay: 45ms;  }
.chip:nth-child(3)  { animation-delay: 90ms;  }
.chip:nth-child(4)  { animation-delay: 135ms; }
.chip:nth-child(5)  { animation-delay: 180ms; }
.chip:nth-child(6)  { animation-delay: 225ms; }

/* ── INPUT BAR ───────────────────────────────────────────────── */
.chat-input-bar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  padding-bottom: max(10px, env(safe-area-inset-bottom, 10px));
  background: rgba(255,255,255,0.88);
  backdrop-filter: blur(24px) saturate(180%);
  -webkit-backdrop-filter: blur(24px) saturate(180%);
  border-top: 1px solid var(--separator);
  height: var(--input-h);

  /* Hidden by default during menu phase */
  opacity: 0;
  transform: translateY(8px);
  pointer-events: none;
  transition: opacity 0.22s ease, transform 0.22s ease;
}

.chat-input-bar.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: auto;
}

.chat-input {
  flex: 1;
  min-width: 0;
  height: 38px;
  padding: 0 16px;
  border-radius: 19px;
  border: none;
  background: var(--gray-2);
  font-size: 15.5px;
  font-family: var(--font);
  color: var(--label);
  outline: none;
  transition: background 0.13s;
}

.chat-input::placeholder {
  color: var(--gray-4);
}

.chat-input:focus {
  background: #DDDDE2;
}

.send-btn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--blue);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding-left: 1px; /* optical center */
  transition: background 0.13s, transform 0.13s, opacity 0.13s;
}

.send-btn:hover:not(:disabled) {
  background: var(--blue-dark);
  transform: scale(1.07);
}

.send-btn:active:not(:disabled) {
  transform: scale(0.94);
}

.send-btn:disabled {
  background: var(--gray-3);
  cursor: not-allowed;
  transform: none;
}

/* ── AI THINKING LABEL ───────────────────────────────────────── */
.ai-thinking-label {
  font-size: 11px;
  color: var(--gray-4);
  text-align: center;
  padding: 4px 0 8px;
  letter-spacing: 0.2px;
  animation: fade-in 0.3s ease;
}

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

/* ── ERROR NOTICE ────────────────────────────────────────────── */
.error-notice {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 12px;
  color: var(--red);
  padding: 6px 12px;
  background: rgba(255,59,48,0.06);
  border-radius: 10px;
  margin: 4px auto;
  max-width: 280px;
  text-align: center;
}

/* ── SPACER ──────────────────────────────────────────────────── */
.spacer { flex: 1; min-height: 8px; }

/* ── DESKTOP FRAME ───────────────────────────────────────────── */
@media (min-width: 520px) {
  body {
    background: linear-gradient(135deg, #dce8ff 0%, #e8d8ff 60%, #d8efff 100%);
    min-height: 100vh;
  }

  .bg-glow { opacity: 1; }

  .chat-wrapper {
    height: min(750px, 94dvh);
    border-radius: 44px;
    box-shadow: var(--shadow-lg), 0 0 0 1px rgba(255,255,255,0.6) inset;
    overflow: hidden;
  }
}

@media (max-width: 519px) {
  .bg-glow { display: none; }

  .chat-wrapper {
    height: 100dvh;
    border-radius: 0;
    max-width: 100%;
  }
}

/* ── REDUCED MOTION ──────────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
