:root {
  --blue: #007AFF;
  --bubble-in: #E9E9EB;
  --bg: #F2F2F7;
  --font-sans: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Helvetica, Arial, sans-serif;
  --font-serif: 'Cormorant Garamond', Georgia, 'Times New Roman', serif;
}

*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: stretch;
  overflow: hidden;
}

/* ── Screen ── */

.screen {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 430px;
  height: 100dvh;
  background: var(--bg);
  overflow: hidden;
}

/* ── Header ── */

.chat-header {
  display: flex;
  align-items: center;
  padding: 8px 12px 10px;
  gap: 0;
  background: rgba(242, 242, 247, 0.95);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 0.5px solid rgba(0, 0, 0, 0.12);
  flex-shrink: 0;
}

.header-back {
  background: none;
  border: none;
  color: var(--blue);
  cursor: pointer;
  padding: 4px 8px 4px 0;
  display: flex;
  align-items: center;
  flex-shrink: 0;
  min-width: 36px;
  -webkit-tap-highlight-color: transparent;
}

.header-center {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}

.avatar {
  width: 50px;
  height: 50px;
  border-radius: 50%;
  background: #EBF3FA;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
  overflow: hidden;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center top;
}

.chat-name {
  font-size: 1rem;
  font-weight: 700;
  color: #000;
  line-height: 1.2;
  letter-spacing: -0.3px;
}

.chat-sub {
  font-size: 0.6875rem;
  color: #8E8E93;
}

.header-actions {
  display: flex;
  align-items: center;
  min-width: 36px;
  justify-content: flex-end;
}

.header-action {
  background: rgba(0, 0, 0, 0.07);
  border: none;
  color: var(--blue);
  cursor: pointer;
  padding: 7px 10px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

/* ── Chat body ── */

.chat-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  padding: 0 16px 8px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
}

.chat-spacer {
  flex: 1;
}

/* ── Day label ── */

.day-label {
  text-align: center;
  font-size: 0.75rem;
  font-weight: 500;
  color: #8E8E93;
  padding: 14px 0 10px;
  letter-spacing: 0.01em;
}

/* ── Bubbles ── */

.bubble {
  padding: 10px 14px;
  font-size: 1.0625rem;
  line-height: 1.35;
  word-break: break-word;
  position: relative;
  opacity: 0;
  margin-top: 4px;
}

.bubble.show {
  animation: popIn 0.36s cubic-bezier(0.34, 1.5, 0.64, 1) forwards;
}

/* Incoming */
.bubble-in {
  background: var(--bubble-in);
  color: #000;
  border-radius: 18px 18px 18px 18px;
  align-self: flex-start;
  max-width: 75%;
  margin-right: 60px;
}

/* Tighten top corners for mid-group incoming */
.bubble-in.grouped-top {
  border-top-left-radius: 6px;
}
.bubble-in.grouped-bottom {
  border-bottom-left-radius: 6px;
}

/* Outgoing */
.bubble-out {
  background: var(--blue);
  color: #fff;
  border-radius: 18px 18px 18px 18px;
  align-self: flex-end;
  max-width: 75%;
  margin-left: 60px;
}

/* Confirm (blue incoming) */
.bubble-confirm {
  background: var(--blue);
  color: #fff;
  border-radius: 18px 18px 18px 18px;
  align-self: flex-start;
  max-width: 75%;
  margin-right: 60px;
}

/* Spacing between sender groups */
.bubble-in  + .bubble-out,
.bubble-out + .bubble-in,
.bubble-out + .bubble-confirm,
.bubble-confirm + .bubble-out {
  margin-top: 24px;
}

/* ── Bubble tails ── */

/* Incoming tail — bottom left */
.bubble-in.tailed {
  border-bottom-left-radius: 4px;
}
.bubble-in.tailed::before {
  content: '';
  position: absolute;
  bottom: 0;
  left: -8px;
  width: 22px;
  height: 22px;
  background: var(--bubble-in);
  clip-path: path('M 22 0 C 22 0 22 22 0 22 C 8 22 22 14 22 0 Z');
}

/* Outgoing tail — bottom right */
.bubble-out.tailed {
  border-bottom-right-radius: 4px;
}
.bubble-out.tailed::before {
  content: '';
  position: absolute;
  bottom: 0;
  right: -8px;
  width: 22px;
  height: 22px;
  background: var(--blue);
  clip-path: path('M 0 0 C 0 0 0 22 22 22 C 14 22 0 14 0 0 Z');
}

/* Confirm tail — bottom left, blue */
.bubble-confirm.tailed {
  border-bottom-left-radius: 4px;
}
.bubble-confirm.tailed::before {
  content: '';
  position: absolute;
  bottom: 0;
  left: -8px;
  width: 22px;
  height: 22px;
  background: var(--blue);
  clip-path: path('M 22 0 C 22 0 22 22 0 22 C 8 22 22 14 22 0 Z');
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.82) translateY(10px); }
  to   { opacity: 1; transform: scale(1)    translateY(0); }
}

/* ── Image bubble ── */

.bubble-img {
  align-self: flex-start;
  max-width: 72%;
  margin-right: 60px;
  margin-top: 4px;
  border-radius: 18px;
  overflow: hidden;
  position: relative;
  opacity: 0;
  flex-shrink: 0;
}

.bubble-img.show {
  animation: popIn 0.36s cubic-bezier(0.34, 1.5, 0.64, 1) forwards;
}

.bubble-img img {
  width: 100%;
  display: block;
  max-height: 260px;
  object-fit: cover;
}

/* ── Delivered ── */

.delivered {
  align-self: flex-end;
  font-size: 0.6875rem;
  color: #8E8E93;
  padding: 3px 2px 0;
  opacity: 0;
  animation: fadeUp 0.3s ease 0.4s forwards;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ── Typing indicator ── */

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 12px 14px;
  background: var(--bubble-in);
  border-radius: 18px 18px 18px 4px;
  align-self: flex-start;
  margin-top: 2px;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.typing.show {
  opacity: 1;
}

.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #8E8E93;
  animation: bounce 1.4s ease infinite;
}
.typing span:nth-child(2) { animation-delay: 0.18s; }
.typing span:nth-child(3) { animation-delay: 0.36s; }

@keyframes bounce {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30%           { opacity: 1;   transform: translateY(-5px); }
}

/* ── Input bar ── */

.chat-input {
  flex-shrink: 0;
  padding: 10px 16px;
  padding-bottom: max(20px, env(safe-area-inset-bottom));
  background: var(--bg);
  transform: translateY(110%);
  transition: transform 0.5s cubic-bezier(0.34, 1.2, 0.64, 1);
}

.chat-input.show {
  transform: translateY(0);
}

.input-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.plus-btn {
  width: 44px;
  height: 44px;
  background: #E5E5EA;
  border: none;
  border-radius: 50%;
  color: #1C1C1E;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

.input-pill {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 4px;
  background: #fff;
  border-radius: 22px;
  padding: 12px 10px 12px 16px;
  border: 0.5px solid rgba(0, 0, 0, 0.18);
}

.phone-field {
  flex: 1;
  border: none;
  background: none;
  font-family: var(--font-sans);
  font-size: 1.0625rem;
  color: #000;
  outline: none;
  min-width: 0;
}

.phone-field::placeholder {
  color: #8E8E93;
}

.send-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: var(--blue);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0.3;
  transition: opacity 0.2s ease, transform 0.12s ease;
  -webkit-tap-highlight-color: transparent;
}

.send-btn:not(:disabled) {
  opacity: 1;
}

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

.send-btn:disabled {
  cursor: default;
}

.mic-btn {
  width: 30px;
  height: 30px;
  border: none;
  background: none;
  color: #3C3C43;
  cursor: default;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  opacity: 0.6;
  -webkit-tap-highlight-color: transparent;
}
