:root {
  --imessage-blue: linear-gradient(135deg, #007AFF 0%, #0051D5 100%);
  --imessage-blue-solid: #007AFF;
  --imessage-gray: #E9E9EB;
  --imessage-bg: #FFFFFF;
  --imessage-header-bg: rgba(249, 249, 249, 0.94);
  --imessage-input-bg: #F6F6F6;
  --text-primary: #000000;
  --text-secondary: #8E8E93;
  --text-tertiary: #C7C7CC;
  --separator: rgba(60, 60, 67, 0.36);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  min-height: 100vh;
  background: #F2F2F7;
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.imessage-container {
  width: 100%;
  max-width: 600px;
  height: 85vh;
  max-height: 800px;
  border-radius: 20px;
  background: var(--imessage-bg);
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.12), 0 2px 8px rgba(0, 0, 0, 0.08);
  position: relative;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

/* Hide phone elements */
.dynamic-island,
.status-bar {
  display: none;
}

/* iMessage Header */
.imessage-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px 10px;
  background: var(--imessage-header-bg);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-bottom: 0.5px solid var(--separator);
}

.header-left {
  display: flex;
  align-items: center;
  gap: 2px;
  color: var(--imessage-blue-solid);
  font-size: 17px;
  cursor: pointer;
  min-width: 70px;
}

.header-left svg {
  width: 22px;
  height: 22px;
}

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

.contact-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #5856D6 0%, #AF52DE 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  color: white;
  font-weight: 600;
  overflow: hidden;
}

.contact-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.contact-name {
  font-size: 11px;
  font-weight: 400;
  color: var(--text-primary);
}

.header-right {
  display: flex;
  align-items: center;
  gap: 18px;
  min-width: 70px;
  justify-content: flex-end;
}

.header-right svg {
  width: 22px;
  height: 22px;
  color: var(--imessage-blue-solid);
  cursor: pointer;
}

/* Chat Area */
.chat-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 6px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
  background: var(--imessage-bg);
  scroll-behavior: smooth;
}

.chat-area::-webkit-scrollbar {
  display: none;
}

.time-separator {
  text-align: center;
  font-size: 11px;
  color: var(--text-secondary);
  padding: 8px 0 6px;
  font-weight: 400;
}

.message-wrapper {
  display: flex;
  flex-direction: column;
  margin-bottom: 1px;
  animation: messageAppear 0.25s ease-out forwards;
}

.message-wrapper.sent {
  align-items: flex-end;
}

.message-wrapper.received {
  align-items: flex-start;
}

/* Add spacing between message groups */
.message-wrapper.sent + .message-wrapper.received,
.message-wrapper.received + .message-wrapper.sent {
  margin-top: 6px;
}

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

.bubble {
  max-width: 75%;
  padding: 8px 12px;
  font-size: 17px;
  line-height: 1.38;
  word-wrap: break-word;
  word-break: break-word;
  position: relative;
  letter-spacing: -0.01em;
}

.bubble.sent {
  background: #007AFF;
  color: #ffffff;
  border-radius: 18px 18px 4px 18px;
  margin-right: 6px;
}

.bubble.received {
  background: #E9E9EB;
  color: #000000;
  border-radius: 18px 18px 18px 4px;
  margin-left: 6px;
}

/* Image messages */
.bubble.image {
  padding: 4px;
  background: transparent;
  box-shadow: none;
}

.bubble.image img {
  max-width: 200px;
  max-height: 250px;
  border-radius: 16px;
  display: block;
}

.bubble.sent.image {
  background: transparent;
}

/* Message reactions */
.bubble-container {
  position: relative;
}

.reaction {
  position: absolute;
  top: -12px;
  background: linear-gradient(180deg, #F8F8F8 0%, #E8E8E8 100%);
  border: 0.5px solid rgba(0,0,0,0.1);
  border-radius: 100px;
  padding: 3px 7px 4px;
  font-size: 16px;
  line-height: 1;
  box-shadow: 0 2px 8px rgba(0,0,0,0.12), 0 0 0 0.5px rgba(0,0,0,0.04);
  cursor: pointer;
  z-index: 10;
  animation: reaction-pop 0.2s ease-out;
}

@keyframes reaction-pop {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); opacity: 1; }
}

.reaction.sent {
  right: -6px;
}

.reaction.received {
  left: -6px;
}

.reaction-picker {
  position: absolute;
  bottom: 100%;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20px);
  border-radius: 20px;
  padding: 8px 12px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  display: flex;
  gap: 8px;
  z-index: 100;
  margin-bottom: 8px;
}

.reaction-picker.sent {
  right: 0;
}

.reaction-picker.received {
  left: 0;
}

.reaction-picker span {
  font-size: 24px;
  cursor: pointer;
  transition: transform 0.1s;
}

.reaction-picker span:hover {
  transform: scale(1.2);
}

/* Image input */
.image-input {
  display: none;
}

.input-accessories {
  display: flex;
  align-items: center;
  padding-bottom: 4px;
  gap: 4px;
}

.camera-btn {
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
}

.camera-btn svg {
  width: 26px;
  height: 26px;
  color: var(--text-secondary);
}

/* Image preview */
.image-preview {
  position: absolute;
  bottom: 60px;
  left: 12px;
  max-width: 120px;
  background: white;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}

.image-preview img {
  width: 100%;
  display: block;
}

.image-preview-close {
  position: absolute;
  top: 4px;
  right: 4px;
  background: rgba(0, 0, 0, 0.6);
  color: white;
  border: none;
  border-radius: 50%;
  width: 20px;
  height: 20px;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Consecutive message radius adjustments */
.bubble.sent.first {
  border-radius: 18px 18px 4px 18px;
}
.bubble.sent.middle {
  border-radius: 18px 4px 4px 18px;
}
.bubble.sent.last {
  border-radius: 18px 4px 18px 18px;
}

.bubble.received.first {
  border-radius: 18px 18px 18px 4px;
}
.bubble.received.middle {
  border-radius: 4px 18px 18px 4px;
}
.bubble.received.last {
  border-radius: 4px 18px 18px 18px;
}

.read-receipt {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 3px;
  padding-right: 4px;
  text-align: right;
}

/* Typing Indicator */
.typing-wrapper {
  display: flex;
  align-items: flex-start;
  margin-bottom: 1px;
}

.typing-indicator {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 12px 16px;
  background: var(--imessage-gray);
  border-radius: 18px 18px 18px 4px;
  margin-left: 2px;
}

.typing-indicator .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #8E8E93;
  animation: typingPulse 1.4s ease-in-out infinite;
}

.typing-indicator .dot:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator .dot:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typingPulse {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* Input Area */
.input-area {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 12px 12px 16px;
  background: var(--imessage-bg);
  border-top: 0.5px solid var(--separator);
}

.input-accessories svg {
  width: 30px;
  height: 30px;
  color: var(--text-secondary);
  cursor: pointer;
}

.input-field-container {
  flex: 1;
  display: flex;
  align-items: flex-end;
  background: var(--imessage-input-bg);
  border-radius: 20px;
  border: 0.5px solid var(--separator);
  padding: 4px 4px 4px 14px;
  gap: 6px;
  min-height: 36px;
}

.input-field {
  flex: 1;
  border: none;
  background: transparent;
  font-size: 17px;
  line-height: 1.3;
  color: var(--text-primary);
  resize: none;
  outline: none;
  min-height: 22px;
  max-height: 100px;
  padding: 2px 0;
}

.input-field::placeholder {
  color: var(--text-tertiary);
}

.send-button {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: none;
  background: var(--imessage-blue);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.12s ease;
  flex-shrink: 0;
}

.send-button:hover {
  transform: scale(1.05);
}

.send-button:active {
  transform: scale(0.92);
}

.send-button:disabled {
  opacity: 0.35;
  cursor: default;
  transform: none;
}

.send-button svg {
  width: 16px;
  height: 16px;
}

/* Home Indicator */
.home-indicator {
  display: none;
}

/* Responsive */
@media (max-width: 650px) {
  body {
    padding: 0;
    background: var(--imessage-bg);
  }
  .imessage-container {
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
    box-shadow: none;
  }
}

.hidden {
  display: none !important;
}

/* Message Effects */
@keyframes slam {
  0% { transform: scale(3); opacity: 0; }
  50% { transform: scale(0.9); }
  75% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes loud {
  0%, 100% { transform: scale(1); }
  25% { transform: scale(1.15) rotate(-2deg); }
  50% { transform: scale(1.2); }
  75% { transform: scale(1.15) rotate(2deg); }
}

@keyframes gentle {
  0% { transform: scale(0.3); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.effect-slam { animation: slam 0.5s ease-out; }
.effect-loud .bubble { animation: loud 0.6s ease-in-out 3; font-size: 22px !important; }
.effect-gentle .bubble { animation: gentle 1.5s ease-out; opacity: 0.8; }

.effect-invisible .bubble {
  background: var(--imessage-gray) !important;
  color: transparent !important;
  text-shadow: 0 0 8px rgba(0,0,0,0.5);
  cursor: pointer;
  user-select: none;
}
.effect-invisible .bubble.revealed {
  color: var(--text-primary) !important;
  text-shadow: none;
  transition: all 0.3s;
}
.effect-invisible .bubble.sent.revealed {
  color: white !important;
  background: var(--imessage-blue) !important;
}

/* Link Previews */
.link-preview {
  background: #F2F2F7;
  border-radius: 16px;
  overflow: hidden;
  margin-top: 4px;
  max-width: 280px;
  border: 1px solid rgba(0,0,0,0.06);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.link-preview:hover {
  transform: scale(1.02);
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.link-preview-image {
  width: 100%;
  max-height: 150px;
  object-fit: cover;
  display: block;
}

.link-preview-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
}

.link-preview-favicon {
  width: 32px;
  height: 32px;
  border-radius: 6px;
  flex-shrink: 0;
}

.link-preview-text {
  flex: 1;
  min-width: 0;
}

.link-preview-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.link-preview-domain {
  font-size: 12px;
  color: var(--text-secondary);
  text-transform: lowercase;
}

/* Message timestamp on hover */
.bubble::after {
  content: attr(data-time);
  position: absolute;
  bottom: -18px;
  font-size: 10px;
  color: var(--text-secondary);
  opacity: 0;
  transition: opacity 0.2s;
  white-space: nowrap;
}

.bubble.sent::after {
  right: 4px;
}

.bubble.received::after {
  left: 4px;
}

.message-wrapper:hover .bubble::after {
  opacity: 1;
}

/* Dark mode support */
@media (prefers-color-scheme: dark) {
  :root {
    --imessage-gray: #3A3A3C;
    --imessage-bg: #000000;
    --imessage-header-bg: rgba(28, 28, 30, 0.94);
    --imessage-input-bg: #1C1C1E;
    --text-primary: #FFFFFF;
    --text-secondary: #8E8E93;
    --text-tertiary: #48484A;
    --separator: rgba(84, 84, 88, 0.65);
  }

  body {
    background: #1C1C1E;
  }

  .link-preview {
    background: #2C2C2E;
    border-color: rgba(255,255,255,0.1);
  }

  .typing-indicator .dot {
    background: #636366;
  }

  .reaction {
    background: linear-gradient(180deg, #3A3A3C 0%, #2C2C2E 100%);
    border-color: rgba(255,255,255,0.1);
  }
}

/* Message Timestamps */
.timestamp-divider {
  text-align: center;
  padding: 16px 0 8px;
  font-size: 12px;
  color: var(--text-secondary);
  font-weight: 500;
}

/* Reply indicator */
.reply-context {
  font-size: 12px;
  color: var(--text-secondary);
  padding: 4px 12px 0;
  margin-bottom: -2px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.reply-context::before {
  content: '';
  width: 2px;
  height: 12px;
  background: var(--text-tertiary);
  border-radius: 1px;
}

/* Search Bar */
.search-bar {
  display: flex;
  background: var(--imessage-input-bg);
  border-bottom: 0.5px solid var(--separator);
  gap: 8px;
  align-items: center;
  transition: all 0.25s ease;
  max-height: 0;
  overflow: hidden;
  padding: 0 12px;
  opacity: 0;
}

.search-bar.active {
  max-height: 50px;
  padding: 8px 12px;
  opacity: 1;
}

.search-input {
  flex: 1;
  background: rgba(0,0,0,0.05);
  border: none;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 14px;
  outline: none;
  color: var(--text-primary);
}

.search-close {
  background: none;
  border: none;
  color: var(--imessage-blue-solid);
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
}

.message-wrapper.highlighted .bubble {
  background: #FFD60A !important;
  color: #000 !important;
}

/* Search icon button */
.search-icon-btn {
  background: none;
  border: none;
  color: var(--imessage-blue-solid);
  cursor: pointer;
  padding: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.search-icon-btn svg {
  width: 22px;
  height: 22px;
}

/* Enhanced message animations */
@keyframes bubblePop {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.05); }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes slideInLeft {
  from { transform: translateX(-20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

@keyframes slideInRight {
  from { transform: translateX(20px); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* Typing indicator bounce animation */
@keyframes typingBounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.typing-indicator .dot {
  animation: typingBounce 1.4s ease-in-out infinite;
}

.typing-indicator .dot:nth-child(1) { animation-delay: 0s; }
.typing-indicator .dot:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator .dot:nth-child(3) { animation-delay: 0.3s; }

/* Send button pulse on enable */
@keyframes sendPulse {
  0% { transform: scale(1); }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.send-button:not(:disabled) {
  animation: sendPulse 0.3s ease-out;
}

/* Read receipt animation */
.read-receipt {
  transition: all 0.3s ease;
}

/* Reaction pop animation - enhanced */
@keyframes reactionPop {
  0% { transform: scale(0) rotate(-20deg); opacity: 0; }
  60% { transform: scale(1.3) rotate(5deg); }
  100% { transform: scale(1) rotate(0deg); opacity: 1; }
}

.reaction {
  animation: reactionPop 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* Smooth bubble tap feedback */
.bubble:active {
  transform: scale(0.98);
  transition: transform 0.1s ease;
}

/* Copy notification */
.copy-notification {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0,0,0,0.8);
  color: white;
  padding: 12px 24px;
  border-radius: 12px;
  font-size: 14px;
  z-index: 1000;
  animation: fadeInOut 2s forwards;
}

@keyframes fadeInOut {
  0%, 100% { opacity: 0; }
  10%, 90% { opacity: 1; }
}

/* Reply Preview Bar */
.reply-preview {
  display: none;
  background: var(--imessage-input-bg);
  border-left: 3px solid var(--imessage-blue-solid);
  padding: 8px 12px;
  margin: 0 12px 8px;
  border-radius: 8px;
  position: relative;
}

.reply-preview.active {
  display: block;
}

.reply-preview-label {
  font-size: 11px;
  color: var(--imessage-blue-solid);
  font-weight: 600;
  margin-bottom: 2px;
}

.reply-preview-text {
  font-size: 13px;
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.reply-preview-close {
  position: absolute;
  top: 50%;
  right: 8px;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 18px;
  line-height: 1;
}

/* Reply quote in message */
.reply-quote {
  background: rgba(0,0,0,0.1);
  border-left: 2px solid rgba(255,255,255,0.4);
  padding: 4px 8px;
  margin-bottom: 6px;
  border-radius: 4px;
  font-size: 12px;
  opacity: 0.8;
}

.bubble.received .reply-quote {
  background: rgba(0,0,0,0.05);
  border-left-color: var(--text-secondary);
}

/* Reasoning bubble */
.reasoning-bubble {
  font-size: 12px;
  color: var(--text-secondary);
  font-style: italic;
  padding: 6px 12px;
  background: rgba(0,0,0,0.03);
  border-radius: 12px;
  margin-bottom: 4px;
  max-width: 250px;
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

/* Search highlight */
.bubble.search-highlight {
  background: #FFD60A !important;
  color: #000 !important;
}

.message-wrapper.search-match {
  position: relative;
}

.message-wrapper.search-match::before {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: -2px;
  bottom: -2px;
  background: rgba(255, 214, 10, 0.15);
  border-radius: 12px;
  pointer-events: none;
}
