/* ========================================
   iOS 17 iMessage Bubbles - Pixel Perfect
   ======================================== */

/* Base Message Bubble */
.message {
    max-width: 70%;
    padding: 8px 12px;
    border-radius: 17px;
    font-size: 17px;
    line-height: 1.35;
    position: relative;
    word-wrap: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
}

/* Sent Messages (Blue - iMessage style) */
.message.sent {
    align-self: flex-end;
    background: var(--imessage-bubble-sent);
    color: var(--imessage-text-sent);
    margin-left: auto;
    margin-right: 0;
}

/* Received Messages (Gray) */
.message.received {
    align-self: flex-start;
    background: var(--imessage-bubble-received);
    color: var(--imessage-text-received);
    margin-right: auto;
    margin-left: 0;
}

/* Message Tail - Sent (Right side, Blue) */
.message.sent.has-tail {
    border-bottom-right-radius: 4px;
}

.message.sent.has-tail::before {
    content: '';
    position: absolute;
    bottom: 0;
    right: -6px;
    width: 12px;
    height: 18px;
    background: var(--imessage-bubble-sent);
    border-bottom-left-radius: 12px;
}

.message.sent.has-tail::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: -12px;
    width: 12px;
    height: 18px;
    background: var(--ios-bg-primary);
    border-bottom-left-radius: 8px;
}

/* Message Tail - Received (Left side, Gray) */
.message.received.has-tail {
    border-bottom-left-radius: 4px;
}

.message.received.has-tail::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: -6px;
    width: 12px;
    height: 18px;
    background: var(--imessage-bubble-received);
    border-bottom-right-radius: 12px;
}

.message.received.has-tail::after {
    content: '';
    position: absolute;
    bottom: 0;
    left: -12px;
    width: 12px;
    height: 18px;
    background: var(--ios-bg-primary);
    border-bottom-right-radius: 8px;
}

/* Message Spacing */
.message + .message {
    margin-top: 2px;
}

.message.sent + .message.received,
.message.received + .message.sent {
    margin-top: 8px;
}

/* Image Messages */
.message.image {
    padding: 0;
    max-width: 65%;
    background: transparent !important;
    overflow: hidden;
    border-radius: 18px;
}

.message.image img {
    display: block;
    max-width: 100%;
    max-height: 280px;
    width: auto;
    height: auto;
    border-radius: 18px;
    object-fit: cover;
}

.message.image::before,
.message.image::after {
    display: none;
}

/* Emoji-only Messages */
.message.emoji-only {
    background: transparent !important;
    font-size: 48px;
    padding: 4px;
    line-height: 1;
}

.message.emoji-only::before,
.message.emoji-only::after {
    display: none;
}

/* Message Timestamp (shown on tap) */
.message-time {
    font-size: 11px;
    color: var(--ios-label-tertiary);
    text-align: center;
    padding: 4px 0;
    opacity: 0;
    height: 0;
    overflow: hidden;
    transition: all 0.2s ease;
}

.message-time.visible {
    opacity: 1;
    height: auto;
    padding: 8px 0;
}

/* Read Receipts */
.read-receipt {
    font-size: 11px;
    color: var(--ios-label-tertiary);
    text-align: left;
    padding-left: 4px;
    margin-top: 4px;
}

/* Delivery Status */
.message.sent .delivery-status {
    position: absolute;
    bottom: -16px;
    right: 4px;
    font-size: 10px;
    color: var(--ios-label-tertiary);
}

/* ========================================
   Message Animations
   ======================================== */

/* Appear Animation */
@keyframes messageSlideIn {
    0% {
        opacity: 0;
        transform: translateY(10px) scale(0.95);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.message {
    animation: messageSlideIn 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

/* Sent Message Animation */
@keyframes messageSent {
    0% {
        opacity: 0;
        transform: translateX(20px) scale(0.9);
    }
    50% {
        transform: translateX(-3px) scale(1.02);
    }
    100% {
        opacity: 1;
        transform: translateX(0) scale(1);
    }
}

.message.sent.new {
    animation: messageSent 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* Received Message Animation */
@keyframes messageReceived {
    0% {
        opacity: 0;
        transform: translateX(-20px) scale(0.9);
    }
    50% {
        transform: translateX(3px) scale(1.02);
    }
    100% {
        opacity: 1;
        transform: translateX(0) scale(1);
    }
}

.message.received.new {
    animation: messageReceived 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* Bubble Pop Effect */
@keyframes bubblePop {
    0% {
        transform: scale(0);
    }
    50% {
        transform: scale(1.1);
    }
    75% {
        transform: scale(0.95);
    }
    100% {
        transform: scale(1);
    }
}

.message.pop {
    animation: bubblePop 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* Long Press Effect */
.message.pressed {
    transform: scale(0.98);
    transition: transform 0.1s ease;
}

/* ========================================
   Link Preview (optional future feature)
   ======================================== */
.link-preview {
    background: var(--ios-bg-secondary);
    border-radius: 12px;
    overflow: hidden;
    margin-top: 4px;
}

.link-preview-image {
    width: 100%;
    height: 120px;
    object-fit: cover;
}

.link-preview-content {
    padding: 10px 12px;
}

.link-preview-title {
    font-size: 15px;
    font-weight: 600;
    color: var(--ios-label);
    margin-bottom: 2px;
}

.link-preview-domain {
    font-size: 12px;
    color: var(--ios-label-tertiary);
    text-transform: uppercase;
}

/* ========================================
   Reactions (optional future feature)
   ======================================== */
.message-reactions {
    position: absolute;
    top: -12px;
    right: 8px;
    display: flex;
    gap: 2px;
}

.reaction {
    background: var(--ios-bg-primary);
    border: 0.5px solid var(--ios-separator);
    border-radius: 12px;
    padding: 2px 6px;
    font-size: 14px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

/* ========================================
   Responsive Adjustments
   ======================================== */
@media (max-width: 480px) {
    .message {
        max-width: 80%;
    }

    .message.image {
        max-width: 75%;
    }
}

/* ========================================
   RTL Support
   ======================================== */
html[dir="rtl"] .message.sent {
    margin-left: 0;
    margin-right: auto;
}

html[dir="rtl"] .message.received {
    margin-right: 0;
    margin-left: auto;
}

/* Tail positions for RTL */
html[dir="rtl"] .message.sent.has-tail {
    border-bottom-right-radius: 17px;
    border-bottom-left-radius: 4px;
}

html[dir="rtl"] .message.sent.has-tail::before {
    right: auto;
    left: -6px;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 12px;
}

html[dir="rtl"] .message.sent.has-tail::after {
    right: auto;
    left: -12px;
    border-bottom-left-radius: 0;
    border-bottom-right-radius: 8px;
}

html[dir="rtl"] .message.received.has-tail {
    border-bottom-left-radius: 17px;
    border-bottom-right-radius: 4px;
}

html[dir="rtl"] .message.received.has-tail::before {
    left: auto;
    right: -6px;
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 12px;
}

html[dir="rtl"] .message.received.has-tail::after {
    left: auto;
    right: -12px;
    border-bottom-right-radius: 0;
    border-bottom-left-radius: 8px;
}

/* ========================================
   Voice Messages - iOS Style
   ======================================== */
.message.voice-message {
    padding: 8px 12px;
    min-width: 180px;
    max-width: 240px;
}

.voice-player {
    display: flex;
    align-items: center;
    gap: 10px;
}

.voice-play-btn {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.25);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: transform 0.1s ease, background 0.2s ease;
}

.voice-play-btn:active {
    transform: scale(0.95);
}

.voice-play-btn svg {
    width: 16px;
    height: 16px;
}

.voice-waveform {
    flex: 1;
    height: 28px;
    position: relative;
    display: flex;
    align-items: center;
    overflow: hidden;
    border-radius: 4px;
}

.voice-progress {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 0%;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 4px;
    transition: width 0.1s linear;
}

.voice-bars {
    display: flex;
    align-items: center;
    gap: 2px;
    height: 100%;
    width: 100%;
    position: relative;
    z-index: 1;
}

.voice-bars .bar {
    flex: 1;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 2px;
    min-width: 3px;
    transition: opacity 0.1s ease;
}

.voice-duration {
    font-size: 12px;
    color: rgba(255, 255, 255, 0.9);
    min-width: 32px;
    text-align: right;
    font-variant-numeric: tabular-nums;
}

/* Received voice message styling */
.message.received.voice-message .voice-play-btn {
    background: rgba(0, 0, 0, 0.1);
    color: var(--imessage-text-received);
}

.message.received.voice-message .voice-progress {
    background: rgba(0, 0, 0, 0.15);
}

.message.received.voice-message .voice-bars .bar {
    background: rgba(0, 0, 0, 0.4);
}

.message.received.voice-message .voice-duration {
    color: var(--imessage-text-received);
    opacity: 0.7;
}

/* Hide audio element */
.voice-player audio {
    display: none;
}

/* RTL adjustments for voice messages */
html[dir="rtl"] .voice-player {
    flex-direction: row-reverse;
}

html[dir="rtl"] .voice-duration {
    text-align: left;
}

html[dir="rtl"] .voice-progress {
    left: auto;
    right: 0;
}
