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

:root {
    --bg:            #0b141a;
    --header-bg:     #1f2c34;
    --bubble-in:     #202c33;
    --bubble-out:    #005c4b;
    --text:          #e9edef;
    --text-muted:    #8696a0;
    --accent:        #25D366;
    --accent-dim:    #1a9e4a;
    --btn-bg:        #1f2c34;
    --btn-border:    #25D366;
    --btn-text:      #25D366;
    --divider:       #2a3942;
    --footer-bg:     #1f2c34;
    --radius-msg:    12px;
    --radius-btn:    22px;
    --shadow-bubble: 0 1px 2px rgba(0,0,0,.35);
    --font:          'SF Pro Display', -apple-system, 'Segoe UI', system-ui, sans-serif;
    --safe-bottom:   env(safe-area-inset-bottom, 0px);
}

html, body {
    height: 100%;
    background: var(--bg);
    font-family: var(--font);
    color: var(--text);
    overscroll-behavior: none;
    -webkit-font-smoothing: antialiased;
}

/* ─── Layout ────────────────────────────────────────────── */
.app-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100dvh;
    background:
        radial-gradient(ellipse 70% 50% at 50% 0%, rgba(37,211,102,.06) 0%, transparent 60%),
        var(--bg);
}

.chat-container {
    width: 100%;
    max-width: 500px;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
    background-image:
        url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%2325D366' fill-opacity='0.025'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E"),
        var(--bg);
}

/* ─── Header ────────────────────────────────────────────── */
.chat-header {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px 10px 6px;
    padding-top: calc(10px + env(safe-area-inset-top, 0px));
    background: var(--header-bg);
    border-bottom: 1px solid rgba(255,255,255,.05);
    backdrop-filter: blur(20px);
    z-index: 100;
    flex-shrink: 0;
    animation: slideDown .4s cubic-bezier(.2,.8,.2,1) both;
}

@keyframes slideDown {
    from { transform: translateY(-100%); opacity: 0; }
    to   { transform: translateY(0);     opacity: 1; }
}

.back-btn {
    background: none;
    border: none;
    color: var(--accent);
    cursor: pointer;
    padding: 6px 8px;
    display: flex;
    align-items: center;
    border-radius: 8px;
    transition: background .15s;
    flex-shrink: 0;
}
.back-btn:hover { background: rgba(37,211,102,.1); }

.header-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 17px;
    font-weight: 700;
    color: #0b141a;
    overflow: hidden;
    position: relative;
    box-shadow: 0 0 0 2px rgba(37,211,102,.3);
}

.header-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
}

.header-info {
    flex: 1;
    min-width: 0;
}

.header-name {
    font-size: 16px;
    font-weight: 600;
    color: var(--text);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.2;
}

.header-status {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 2px;
}

.status-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--accent);
    animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50%       { opacity: .6; transform: scale(.8); }
}

#statusText {
    font-size: 12px;
    color: var(--accent);
    font-weight: 400;
}

.header-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    transition: color .2s, background .2s;
}
.icon-btn:hover {
    color: var(--accent);
    background: rgba(37,211,102,.08);
}

/* ─── Chat Body ─────────────────────────────────────────── */
.chat-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px 12px 8px;
    scroll-behavior: smooth;
    display: flex;
    flex-direction: column;
}

/* Custom scrollbar */
.chat-body::-webkit-scrollbar { width: 3px; }
.chat-body::-webkit-scrollbar-track { background: transparent; }
.chat-body::-webkit-scrollbar-thumb { background: rgba(255,255,255,.12); border-radius: 3px; }

.messages-list {
    display: flex;
    flex-direction: column;
    gap: 3px;
    width: 100%;
}

/* ─── Date Divider ──────────────────────────────────────── */
.date-divider {
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 8px 0 12px;
}

.date-divider span {
    background: rgba(17, 27, 33, 0.85);
    border: 1px solid rgba(255,255,255,.07);
    color: var(--text-muted);
    font-size: 11.5px;
    padding: 4px 12px;
    border-radius: 8px;
    backdrop-filter: blur(8px);
    letter-spacing: .3px;
}

/* ─── Message Rows ──────────────────────────────────────── */
.message-row {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    max-width: 100%;
    opacity: 0;
    transform: translateY(12px);
    animation: msgIn .35s cubic-bezier(.2,.8,.2,1) forwards;
}

@keyframes msgIn {
    to { opacity: 1; transform: translateY(0); }
}

.message-row.incoming {
    align-self: flex-start;
    margin-left: 0;
}

.message-row.outgoing {
    align-self: flex-end;
    flex-direction: row-reverse;
}

/* Avatar inside chat */
.msg-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--accent);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    color: #0b141a;
    overflow: hidden;
    margin-bottom: 4px;
    opacity: 0;
    transition: opacity .2s;
}

.msg-avatar.visible { opacity: 1; }

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

/* ─── Bubbles ───────────────────────────────────────────── */
.bubble {
    max-width: min(74vw, 320px);
    padding: 8px 12px 6px;
    border-radius: var(--radius-msg);
    box-shadow: var(--shadow-bubble);
    position: relative;
    word-break: break-word;
    line-height: 1.45;
    font-size: 14.5px;
}

/* Incoming tail */
.message-row.incoming .bubble {
    background: var(--bubble-in);
    color: var(--text);
    border-bottom-left-radius: 4px;
}

/* Outgoing tail */
.message-row.outgoing .bubble {
    background: var(--bubble-out);
    color: var(--text);
    border-bottom-right-radius: 4px;
}

.bubble-time {
    display: block;
    text-align: right;
    font-size: 10.5px;
    color: rgba(233,237,239,.5);
    margin-top: 3px;
    margin-bottom: -2px;
    letter-spacing: .2px;
}

.message-row.outgoing .bubble-time {
    color: rgba(233,237,239,.6);
}

/* Double tick for outgoing */
.bubble-time.sent::after {
    content: ' ✓✓';
    color: var(--accent);
    font-size: 9px;
}

/* ─── Image in bubble ───────────────────────────────────── */
.bubble-img {
    display: block;
    width: 100%;
    max-width: 260px;
    border-radius: 8px;
    object-fit: cover;
    cursor: zoom-in;
    transition: opacity .2s, transform .2s;
    margin-bottom: 4px;
}

.bubble-img:hover {
    opacity: .92;
    transform: scale(1.01);
}

/* Image bubble has less padding */
.bubble.bubble--image {
    padding: 5px 5px 6px;
    background: transparent !important;
    box-shadow: none;
}

.bubble.bubble--image .bubble-time {
    padding-right: 4px;
}

/* ─── Typing Indicator ──────────────────────────────────── */
.typing-row {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    align-self: flex-start;
    animation: msgIn .25s cubic-bezier(.2,.8,.2,1) both;
}

.typing-bubble {
    background: var(--bubble-in);
    border-radius: var(--radius-msg);
    border-bottom-left-radius: 4px;
    padding: 12px 14px;
    box-shadow: var(--shadow-bubble);
    display: flex;
    align-items: center;
    gap: 5px;
}

.typing-dot {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: var(--text-muted);
    animation: typingBounce 1.2s ease-in-out infinite;
}

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

@keyframes typingBounce {
    0%, 80%, 100% { transform: translateY(0); opacity: .4; }
    40%            { transform: translateY(-6px); opacity: 1; }
}

.typing-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    background: var(--accent);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    font-weight: 700;
    color: #0b141a;
    flex-shrink: 0;
    margin-bottom: 4px;
}

/* ─── Quick Reply Buttons ───────────────────────────────── */
.options-wrap {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 6px 0 4px 36px;
    opacity: 0;
    transform: translateY(8px);
    transition: opacity .4s cubic-bezier(.2,.8,.2,1), transform .4s cubic-bezier(.2,.8,.2,1);
    align-self: flex-start;
    width: 100%;
}

.options-wrap.visible {
    opacity: 1;
    transform: translateY(0);
}

.option-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 18px;
    border: 1.5px solid var(--btn-border);
    background: var(--btn-bg);
    color: var(--btn-text);
    border-radius: var(--radius-btn);
    font-size: 14px;
    font-weight: 500;
    font-family: var(--font);
    cursor: pointer;
    /* ── Micro-interaction: spring scale ── */
    transition:
        background .2s,
        color .2s,
        border-color .2s,
        box-shadow .2s,
        transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    text-align: left;
    width: fit-content;
    max-width: 280px;
    box-shadow: 0 2px 8px rgba(0,0,0,.25), 0 0 0 0 rgba(37,211,102,0);
    position: relative;
    overflow: hidden;
    animation: btnPop .3s cubic-bezier(.34,1.56,.64,1) both;
    will-change: transform;
}

.option-btn:hover {
    background: var(--accent);
    color: #0b141a;
    border-color: var(--accent);
    transform: scale(1.03) translateY(-1px);
    box-shadow: 0 6px 20px rgba(37,211,102,.35);
}

.option-btn:active {
    transform: scale(0.97) translateY(1px);
    box-shadow: 0 1px 4px rgba(0,0,0,.3);
    transition-duration: .08s;
}

@keyframes btnPop {
    from { opacity: 0; transform: scale(.9) translateY(6px); }
    to   { opacity: 1; transform: scale(1)  translateY(0);   }
}

/* Stagger each button */
.option-btn:nth-child(1) { animation-delay: .05s; }
.option-btn:nth-child(2) { animation-delay: .12s; }
.option-btn:nth-child(3) { animation-delay: .19s; }
.option-btn:nth-child(4) { animation-delay: .26s; }
.option-btn:nth-child(5) { animation-delay: .33s; }

/* ─── Footer ────────────────────────────────────────────── */
.chat-footer {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 10px;
    padding-bottom: calc(8px + var(--safe-bottom));
    background: var(--footer-bg);
    border-top: 1px solid rgba(255,255,255,.05);
    flex-shrink: 0;
    animation: slideUp .4s .1s cubic-bezier(.2,.8,.2,1) both;
}

@keyframes slideUp {
    from { transform: translateY(100%); opacity: 0; }
    to   { transform: translateY(0);    opacity: 1; }
}

.attach-btn, .mic-btn {
    background: none;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: color .2s, background .2s, transform 0.18s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.attach-btn:hover {
    color: var(--accent);
    background: rgba(37,211,102,.08);
    transform: scale(1.12);
}

.attach-btn:active { transform: scale(0.94); }

/* Mic button changes color when input has text */
.mic-btn {
    transition: color .2s, background .2s, transform 0.18s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.mic-btn.has-text {
    color: var(--accent);
}

.mic-btn:hover {
    color: var(--accent);
    background: rgba(37,211,102,.08);
    transform: scale(1.12);
}

.mic-btn:active { transform: scale(0.94); }

/* ─── Input Field (contenteditable) ────────────────────── */
.input-field {
    flex: 1;
    background: #2a3942;
    border-radius: 24px;
    padding: 10px 16px;
    min-height: 44px;
    max-height: 120px;
    overflow-y: auto;
    display: flex;
    align-items: center;
    cursor: text;
    transition: background .2s, box-shadow .2s;
    border: 1px solid rgba(255,255,255,.04);
    color: var(--text);
    font-size: 14.5px;
    font-family: var(--font);
    line-height: 1.4;
    outline: none;
    word-break: break-word;
}

.input-field:hover { background: #2f3f49; }

.input-field:focus {
    background: #2f3f49;
    box-shadow: 0 0 0 1.5px rgba(37,211,102,.25);
}

/* Placeholder via CSS — shown when div is empty */
.input-field:empty::before {
    content: attr(data-placeholder);
    color: var(--text-muted);
    pointer-events: none;
    user-select: none;
}

/* ─── Responsive ────────────────────────────────────────── */
@media (min-width: 500px) {
    .chat-container {
        height: min(100dvh, 820px);
        border-radius: 20px;
        overflow: hidden;
        box-shadow:
            0 30px 80px rgba(0,0,0,.6),
            0 0 0 1px rgba(255,255,255,.06),
            0 0 60px rgba(37,211,102,.05);
        margin: auto;
    }
}

@media (max-width: 499px) {
    .bubble { max-width: 78vw; }
}

/* ─── Scroll anchor ─────────────────────────────────────── */
#scrollAnchor { height: 1px; flex-shrink: 0; }
