* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
    --bg-body: #f0f2f5;
    --bg-card: #ffffff;
    --bg-header: #ffffff;
    --bg-chat: #e5ddd5; 
    --bg-notice: #f7f9fa;
    --bg-input: #ffffff;
    
    --bg-msg-me: #fce4ec;       
    --bg-msg-them: #ffffff;     
    --text-msg-me: #1c1e21;
    --text-msg-them: #1c1e21;
    
    --quote-border-me: #d81b60; 
    --quote-sender-me: #d81b60; 
    --quote-bg-me: rgba(216, 27, 96, 0.08); 
    --quote-border-them: #ab47bc; 
    --quote-sender-them: #ab47bc; 
    --quote-bg-them: rgba(171, 71, 188, 0.08);
    
    --msg-meta-me: #9e9e9e;
    --msg-meta-them: #9e9e9e;
    --tick-unseen: #9e9e9e;
    --tick-seen: #34B7F1;

    --text-main: #1c1e21;
    --text-muted: #65676b;
    --text-card-h3: #050505;
    --border-color: #e4e6eb;
    --accent-color: #d81b60;
    --accent-hover: rgba(216, 27, 96, 0.15);
    
    --send-btn-bg: #d81b60;
    --send-btn-active: #ad144e;
    --btn-neutral-active: #e4e6eb;
}

[data-theme="dark"] {
    color-scheme: dark;
    --bg-body: #121212;
    --bg-card: #1e1e1e;
    --bg-header: #1e1e1e;
    --bg-chat: #0a0a0a; 
    --bg-notice: #151515;
    --bg-input: #2c2c2e;
    
    --bg-msg-me: #6a1b9a;       
    --bg-msg-them: #2b2b2b;     
    --text-msg-me: #ffffff;
    --text-msg-them: #ffffff;
    
    --quote-border-me: #ffffff; 
    --quote-sender-me: #ffffff; 
    --quote-bg-me: rgba(255, 255, 255, 0.15); 
    --quote-border-them: #ce93d8; 
    --quote-sender-them: #ce93d8; 
    --quote-bg-them: rgba(206, 147, 216, 0.15); 

    --msg-meta-me: rgba(255, 255, 255, 0.7);
    --msg-meta-them: rgba(255, 255, 255, 0.6);
    --tick-unseen: rgba(255, 255, 255, 0.8);
    --tick-seen: #4fc3f7;

    --text-main: #f5f5f5;
    --text-muted: #8e8e93;
    --text-card-h3: #ffffff;
    --border-color: #2a2a2a;
    --accent-color: #ce93d8;
    --accent-hover: rgba(206, 147, 216, 0.25);
    
    --send-btn-bg: #6a1b9a; 
    --send-btn-active: #4a148c;
    --btn-neutral-active: #3a3a3c;
}

html, body { height: 100%; width: 100vw; overflow: hidden; }

body { 
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; 
    background: var(--bg-body); color: var(--text-main); 
    display: flex; justify-content: center; align-items: center; 
    transition: background 0.2s, color 0.2s;
    -webkit-touch-callout: none; -webkit-user-select: none; user-select: none;
}

.auth-overlay {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0; width: 100%; height: 100%;
    background: rgba(0, 0, 0, 0.5); backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
    display: flex; flex-direction: column; justify-content: center; align-items: center;
    z-index: 10000; opacity: 0; pointer-events: none; transition: opacity 0.3s ease; overflow: hidden; padding: 12px;
}
.auth-overlay.show { opacity: 1; pointer-events: all; }

.auth-card {
    background: var(--bg-card); width: 100%; max-width: 350px; padding: 24px 20px; 
    border-radius: 24px; text-align: center; box-shadow: 0 16px 40px rgba(0,0,0,0.2); 
    border: 1px solid var(--border-color); position: relative; transform: translateY(0); 
    transition: transform 0.2s ease-out; margin: auto; flex-shrink: 0;
}
.auth-card.webview-shift { transform: translateY(-80px) !important; }

@media (max-height: 600px) {
    .auth-card { padding: 18px 16px; }
    .auth-header-wrapper h2 { font-size: 1.15rem; margin-bottom: 4px; }
    .auth-header-wrapper p { font-size: 0.78rem; margin-bottom: 12px; }
    .auth-input-group .auth-input-box { padding: 10px 14px; margin-bottom: 12px; font-size: 14px; }
    .auth-submit-btn { padding: 11px; font-size: 14px; }
    .auth-card.webview-shift { transform: translateY(-110px) !important; }
}

.auth-close-btn { 
    position: absolute; top: 10px; right: 14px; background: none; border: none; 
    font-size: 1.5rem; color: var(--text-muted); cursor: pointer; transition: color 0.15s ease; 
}
.auth-close-btn:active { color: #ff3b30; }

.auth-header-wrapper h2 { color: var(--text-main); font-size: 1.35rem; margin-bottom: 6px; }
.auth-header-wrapper p { color: var(--text-muted); font-size: 0.85rem; margin-bottom: 20px; line-height: 1.4; }

.auth-input-group .auth-input-box { 
    width: 100%; padding: 13px 16px; border-radius: 12px; border: 1px solid var(--border-color); 
    background: var(--bg-input); color: var(--text-main); font-size: 15px; outline: none; 
    text-align: center; transition: border-color 0.2s; margin-bottom: 12px; display: block; font-family: inherit;
}
.auth-input-group .auth-input-box:focus { border-color: var(--accent-color); }
.trusted-device-label { display: flex; align-items: center; justify-content: center; gap: 8px; font-size: 0.8rem; color: var(--text-muted); cursor: pointer; margin-bottom: 16px; user-select: none; }
.trusted-device-label input { cursor: pointer; width: 16px; height: 16px; accent-color: var(--accent-color); }

.auth-submit-btn { 
    width: 100%; padding: 13px; border-radius: 12px; border: none; background: var(--send-btn-bg); color: #fff; font-weight: 600; cursor: pointer; font-size: 15px; transition: background-color 0.15s ease, filter 0.15s ease; box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1); outline: none;
}
.auth-submit-btn:active { background-color: var(--send-btn-active); filter: brightness(0.92); }
.auth-error { color: #ff3b30; font-size: 0.85rem; margin-top: 10px; opacity: 0; transition: opacity 0.2s; font-weight: 500; }
.auth-error.show { opacity: 1; }

.page-view { width: 100%; height: 100dvh; display: none; flex-direction: column; overflow: hidden; background: var(--bg-card); }
.page-view.active { display: flex; }

#studyPage { overflow-y: auto; background: var(--bg-body); }
.study-header { background: var(--bg-header); padding: 20px; border-bottom: 1px solid var(--border-color); position: sticky; top: 0; z-index: 10; width: 100%; }
.header-layout-box { max-width: 800px; margin: 0 auto; width: 100%; display: flex; flex-direction: row; justify-content: space-between; align-items: center; gap: 16px; }
.header-text-block { flex: 1; }
.study-header h1 { font-size: 1.4rem; color: var(--accent-color); }
.study-header p { font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.header-actions { display: flex; align-items: center; gap: 10px; }

.theme-toggle-btn { background: var(--bg-body); color: var(--text-main); border: 1px solid var(--border-color); padding: 6px 12px; border-radius: 16px; font-size: 0.75rem; font-weight: 600; cursor: pointer; transition: background-color 0.15s ease, border-color 0.15s ease; outline: none; }
.theme-toggle-btn:active { background-color: var(--btn-neutral-active); }

.share-icon-btn { background: var(--bg-body); color: var(--text-main); border: 1px solid var(--border-color); width: 32px; height: 32px; border-radius: 50%; display: flex; justify-content: center; align-items: center; cursor: pointer; transition: background-color 0.15s ease; outline: none; }
.share-icon-btn:active { background-color: var(--btn-neutral-active); }
.share-icon-btn svg { width: 16px; height: 16px; fill: currentColor; }

.doubt-corner { background: transparent; color: var(--text-main); border: 1px solid var(--border-color); padding: 6px 12px; border-radius: 16px; font-size: 0.75rem; font-weight: 600; cursor: pointer; transition: background-color 0.15s ease, border-color 0.15s ease; outline: none; }
.doubt-corner:hover { background: var(--bg-notice); }
.doubt-corner:active { background-color: var(--btn-neutral-active); border-color: var(--accent-color); }

.toast-notification { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(100px); background: #323232; color: #ffffff; padding: 12px 24px; border-radius: 24px; font-size: 0.85rem; font-weight: 500; opacity: 0; transition: 0.3s; pointer-events: none; z-index: 9999; }
.toast-notification.show { transform: translateX(-50%) translateY(0); opacity: 1; }

.study-content { padding: 16px; display: flex; flex-direction: column; gap: 16px; width: 100%; max-width: 800px; margin: 0 auto; }
.section-title { font-size: 0.9rem; text-transform: uppercase; color: var(--text-muted); letter-spacing: 1px; margin-top: 14px; margin-bottom: -4px; padding-left: 4px; border-left: 3px solid var(--accent-color); line-height: 1; }
.card { background: var(--bg-card); padding: 16px; border-radius: 12px; border: 1px solid var(--border-color); }
.card h3 { margin-bottom: 10px; color: var(--text-card-h3); font-size: 1.05rem; border-bottom: 1px solid var(--border-color); padding-bottom: 6px; }
.card h4 { color: var(--accent-color); font-size: 0.85rem; margin-top: 10px; margin-bottom: 4px; }
.card p, .card li { font-size: 0.85rem; color: var(--text-main); opacity: 0.85; line-height: 1.5; margin-bottom: 8px; }
.card ul { margin-left: 18px; margin-bottom: 10px; }

.chat-container { width: 100%; height: 100%; background: var(--bg-chat); display: flex; flex-direction: column; overflow: hidden; position: fixed; top: 0; left: 0; right: 0; bottom: 0; }
.chat-layout-wrapper { display: flex; flex-direction: column; width: 100%; height: 100%; margin: 0 auto; overflow: hidden; position: relative; }

.chat-top-panel { position: sticky; top: 0; left: 0; width: 100%; z-index: 100; display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; background: var(--bg-header); border-bottom: 1px solid var(--border-color); min-height: 54px; flex-shrink: 0; }
.user-presence-info { display: flex; align-items: center; gap: 10px; }
.presence-dot { width: 10px; height: 10px; border-radius: 50%; background: #8e8e93; transition: background 0.3s; }
.presence-dot.online { background: #34c759; box-shadow: 0 0 8px rgba(52, 199, 89, 0.6); }
.presence-text-group { display: flex; flex-direction: column; }
.peer-title { font-size: 0.95rem; font-weight: 600; color: var(--text-main); line-height: 1.1; }
.peer-status { font-size: 0.72rem; color: var(--text-muted); margin-top: 2px; transition: color 0.2s; }
.peer-status.typing { color: var(--accent-color); font-weight: 600; }

.chat-panel-actions { display: flex; align-items: center; gap: 8px; }
.chat-theme-btn { padding: 5px 10px; font-size: 0.72rem; }
.delete-chat-btn { background: transparent; border: none; cursor: pointer; color: #ff3b30; padding: 6px; border-radius: 8px; transition: background-color 0.15s ease; display: flex; align-items: center; justify-content: center; outline: none; }
.delete-chat-btn:active { background: rgba(255, 59, 48, 0.18); }

.messages-relative-container { position: relative; flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; overflow: hidden; }

.chat-loading-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: var(--bg-chat); display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 12px; z-index: 20; opacity: 1; transition: opacity 0.3s ease; }
.chat-loading-overlay.hidden { opacity: 0; pointer-events: none; }
.chat-loading-overlay span { font-size: 0.8rem; color: var(--text-muted); font-weight: 500; }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--border-color); border-top-color: var(--accent-color); animation: spin 0.8s linear infinite; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

.empty-chat-state { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); display: none; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 24px; pointer-events: none; z-index: 10; width: 90%; max-width: 300px; opacity: 0; transition: opacity 0.25s ease; }
.empty-chat-state.show { display: flex; opacity: 1; }
.empty-icon-wrap { width: 72px; height: 72px; border-radius: 50%; background: var(--bg-card); display: flex; align-items: center; justify-content: center; margin-bottom: 16px; border: 1px solid var(--border-color); }
.empty-icon-wrap svg { width: 36px; height: 36px; color: var(--accent-color); }
.empty-chat-state h3 { font-size: 1.05rem; font-weight: 600; color: var(--text-main); margin-bottom: 6px; }
.empty-chat-state p { font-size: 0.8rem; color: var(--text-muted); line-height: 1.4; }

#messages { flex: 1 1 auto; padding: 16px; overflow-y: auto; overscroll-behavior-y: contain; display: flex; flex-direction: column-reverse; gap: 8px; background: var(--bg-chat); -webkit-overflow-scrolling: touch; }

.msg { position: relative; padding: 6px 10px 8px 10px; border-radius: 12px; max-width: 80%; min-width: 90px; word-break: break-word; cursor: pointer; user-select: none; transition: filter 0.15s ease; display: flex; flex-direction: column; }
.msg:active { filter: brightness(0.92); }
.msg.me { background: var(--bg-msg-me); color: var(--text-msg-me); align-self: flex-end; border-bottom-right-radius: 4px; box-shadow: 0 1px 1px rgba(0,0,0,0.1); }
.msg.them { background: var(--bg-msg-them); color: var(--text-msg-them); align-self: flex-start; border-bottom-left-radius: 4px; box-shadow: 0 1px 1px rgba(0,0,0,0.1); }

.msg-text { font-size: 0.95rem; line-height: 1.35; position: relative; }
.msg-meta-spacer { display: inline-block; width: 65px; height: 10px; }
.msg-meta { position: absolute; bottom: 4px; right: 8px; font-size: 0.65rem; display: flex; align-items: center; gap: 3px; }
.msg.me .msg-meta { color: var(--msg-meta-me); }
.msg.them .msg-meta { color: var(--msg-meta-them); }
.msg-tick { color: var(--tick-unseen); margin-bottom: 1px; transition: color 0.3s ease; }
.msg-tick.read { color: var(--tick-seen); }

@keyframes highlightFlash { 0% { filter: brightness(1.3); transform: scale(1.02); } 100% { filter: brightness(1); transform: scale(1); } }
.highlight-flash { animation: highlightFlash 1.2s ease-out; }

.reply-preview { display: none; align-items: center; justify-content: space-between; padding: 10px 16px; background: var(--bg-header); border-top: 1px solid var(--border-color); z-index: 10; }
.reply-preview.show { display: flex; }
.reply-preview-content { display: flex; flex-direction: column; overflow: hidden; border-left: 4px solid var(--accent-color); padding-left: 10px; border-radius: 2px; flex: 1; margin-right: 12px; }
.reply-sender { font-size: 0.8rem; font-weight: 700; color: var(--accent-color); margin-bottom: 4px; }
.reply-text { font-size: 0.85rem; color: var(--text-muted); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; white-space: normal; line-height: 1.3; margin-top: 2px; }
.cancel-reply-btn { background: none; border: none; font-size: 1.6rem; color: var(--text-muted); cursor: pointer; padding: 0 8px; outline: none; }
.cancel-reply-btn:active { color: #ff3b30; }

.quoted-msg { position: relative; padding: 6px 10px 6px 12px; border-radius: 6px; margin-bottom: 6px; text-align: left; display: flex; flex-direction: row; align-items: center; gap: 8px; overflow: hidden; cursor: pointer; }
.quoted-body { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.quoted-thumb { width: 44px; height: 44px; object-fit: cover; border-radius: 4px; flex-shrink: 0; background: rgba(128, 128, 128, 0.2); }
.quoted-msg::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; }
.quoted-sender { font-weight: 700; font-size: 0.75rem; margin-bottom: 3px; line-height: 1.2; }
.quoted-text { font-size: 0.85rem; line-height: 1.3; display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; text-overflow: ellipsis; }

.msg.me .quoted-msg { background: var(--quote-bg-me); }
.msg.me .quoted-msg::before { background-color: var(--quote-border-me); }
.msg.me .quoted-sender { color: var(--quote-sender-me); }
.msg.me .quoted-text { color: var(--text-msg-me); opacity: 0.9; }

.msg.them .quoted-msg { background: var(--quote-bg-them); }
.msg.them .quoted-msg::before { background-color: var(--quote-border-them); }
.msg.them .quoted-sender { color: var(--quote-sender-them); }
.msg.them .quoted-text { color: var(--text-msg-them); opacity: 0.8; }

.input-area { display: flex; padding: 14px; background: var(--bg-header); gap: 10px; border-top: 1px solid var(--border-color); align-items: center; position: relative; flex-shrink: 0; padding-bottom: calc(14px + env(safe-area-inset-bottom)); }

.emoji-toggle-btn { background: transparent; border: none; font-size: 1.4rem; cursor: pointer; padding: 0 4px; outline: none; transition: transform 0.1s ease; color: var(--text-muted); display: flex; align-items: center; justify-content: center;}
.emoji-toggle-btn:hover { color: var(--text-main); }
.emoji-toggle-btn:active { transform: scale(0.92); }
.emoji-toggle-btn .icon-keyboard { display: none; }
.emoji-toggle-btn.is-keyboard .icon-emoji { display: none; }
.emoji-toggle-btn.is-keyboard .icon-keyboard { display: block; animation: toggleIconPop 0.18s ease-out; }
.emoji-toggle-btn.is-keyboard { color: var(--accent-color); }
@keyframes toggleIconPop { 0% { transform: scale(0.7); opacity: 0.4; } 100% { transform: scale(1); opacity: 1; } }

#msgInput { flex: 1; padding: 12px 14px; border: 1px solid var(--border-color); border-radius: 20px; background: var(--bg-input); color: var(--text-main); outline: none; font-size: 16px; max-height: 120px; overflow-y: auto; white-space: pre-wrap; word-break: break-word; }
#msgInput:empty::before { content: "Type a message..."; color: var(--text-muted); pointer-events: none; }

.char-counter { font-size: 0.75rem; color: var(--text-muted); transition: color 0.2s; display: none; font-weight: 500; }
.char-counter.show { display: block; }
.char-counter.warning { color: #ff9800; }
.char-counter.danger { color: #ff3b30; font-weight: 700; }

button#sendBtn { width: 44px; height: 44px; padding: 0; border: none; background: var(--send-btn-bg); color: #fff; border-radius: 50%; cursor: pointer; flex-shrink: 0; display: flex; justify-content: center; align-items: center; transition: background-color 0.15s ease, transform 0.15s ease; outline: none; box-shadow: 0 2px 6px rgba(0,0,0,0.15); }
button#sendBtn svg { margin-left: 2px; }
button#sendBtn:active { background-color: var(--send-btn-active); transform: scale(0.92); }
button#sendBtn:disabled { background: var(--border-color); color: var(--text-muted); cursor: not-allowed; opacity: 0.7; box-shadow: none; transform: scale(1); }

.scroll-to-bottom-btn { position: absolute; bottom: 16px; right: 16px; width: 42px; height: 42px; border-radius: 50%; background: var(--bg-card); color: var(--text-muted); border: 1px solid var(--border-color); box-shadow: 0 4px 12px rgba(0,0,0,0.15); display: flex; justify-content: center; align-items: center; cursor: pointer; z-index: 30; transition: opacity 0.25s, transform 0.25s, background-color 0.15s ease; outline: none; }
.scroll-to-bottom-btn:hover { background: var(--bg-notice); color: var(--text-main); }
.scroll-to-bottom-btn:active { background-color: var(--btn-neutral-active); }
.scroll-to-bottom-btn.hidden { opacity: 0; pointer-events: none; transform: translateY(20px) scale(0.8); }

.unread-badge { position: absolute; top: -4px; right: -4px; background: #25D366; color: #ffffff; font-size: 0.7rem; font-weight: 700; min-width: 20px; height: 20px; border-radius: 10px; display: flex; align-items: center; justify-content: center; border: 2px solid var(--bg-chat); transition: opacity 0.2s, transform 0.2s; box-shadow: 0 2px 4px rgba(0,0,0,0.1); }
.unread-badge.hidden { opacity: 0; transform: scale(0); pointer-events: none; }

@media (max-width: 580px) {
    .header-layout-box { flex-direction: column; align-items: flex-start; gap: 12px; }
    .header-actions { width: 100%; justify-content: space-between; }
    .doubt-corner { align-self: flex-start; }
}
@media (min-width: 481px) { .chat-layout-wrapper { max-width: 700px; } }

#messages, #studyPage { scrollbar-width: thin; scrollbar-color: var(--border-color) transparent; }
#messages::-webkit-scrollbar, #studyPage::-webkit-scrollbar { width: 6px; }
#messages::-webkit-scrollbar-track, #studyPage::-webkit-scrollbar-track { background: transparent; }
#messages::-webkit-scrollbar-thumb, #studyPage::-webkit-scrollbar-thumb { background-color: var(--border-color); border-radius: 10px; }
#messages::-webkit-scrollbar-thumb:hover, #studyPage::-webkit-scrollbar-thumb:hover { background-color: var(--text-muted); }

.msg-text.deleted-text { color: var(--text-muted); opacity: 0.85; }

.dialog-action-row { display: flex; gap: 10px; width: 100%; margin-top: 8px; }
.dialog-btn { flex: 1; padding: 10px 0; border-radius: 10px; border: none; font-size: 0.85rem; font-weight: 600; cursor: pointer; transition: transform 0.15s ease, background-color 0.15s ease; outline: none; }
.dialog-btn:active { transform: scale(0.95); }
.dialog-cancel-btn { background: var(--bg-body); color: var(--text-main); border: 1px solid var(--border-color); }
.dialog-cancel-btn:active { background: var(--btn-neutral-active); }
.dialog-delete-btn { background: rgba(255, 59, 48, 0.1); color: #ff3b30; }
.dialog-delete-btn:active { background: rgba(255, 59, 48, 0.2); }
[data-theme="dark"] .dialog-delete-btn { background: rgba(255, 69, 58, 0.15); color: #ff453a; }

/* --- MODERN MEDIA PICKER (EMOJI / GIF) --- */
@keyframes slideUpFade {
    0% { opacity: 0; transform: translateY(15px); }
    100% { opacity: 1; transform: translateY(0); }
}

/* Panel lives in the normal flow BELOW the input bar. Its height is set by JS to the real keyboard height. */
.media-picker {
    display: none; flex-direction: column; flex-shrink: 0; position: relative; z-index: 5;
    width: 100%; height: 0; overflow: hidden;
    background: var(--bg-header); border-top: 1px solid var(--border-color);
}
.media-picker.show { display: flex; }
.media-picker.animating { transition: height 0.22s cubic-bezier(0.2, 0.9, 0.3, 1); }

/* While the panel is open it replaces the keyboard, so the bottom safe-area moves from the input bar to the panel */
.chat-container.media-panel-open .input-area { padding-bottom: 14px; }
.chat-container.media-panel-open .media-picker-content { padding-bottom: env(safe-area-inset-bottom); }

/* GIF/emoji search focused (soft keyboard is up): panel fills the space above the keyboard, chat collapses */
.chat-container.media-search-mode .messages-relative-container { flex: 0 0 0; }
.chat-container.media-search-mode .media-picker { flex: 1 1 auto; min-height: 0; }
.chat-container.media-search-mode .media-picker-content { padding-bottom: 0; }

.media-picker-header { padding: 8px 12px; border-bottom: 1px solid var(--border-color); background: transparent; flex-shrink: 0; }
.media-tabs { display: flex; gap: 8px; }
.media-search { margin-top: 8px; }
.media-tab { flex: 1; background: transparent; border: none; padding: 6px 0; font-size: 0.9rem; font-weight: 600; color: var(--text-muted); cursor: pointer; border-radius: 8px; transition: all 0.2s; }
.media-tab.active { background: var(--bg-notice); color: var(--text-main); }
[data-theme="dark"] .media-tab.active { background: rgba(255, 255, 255, 0.1); }
.media-search { width: 100%; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--border-color); background: var(--bg-input); color: var(--text-main); outline: none; font-size: 0.85rem; }

.media-picker-content { flex: 1; overflow: hidden; position: relative; }

/* Dynamic container that houses either emoji-mart or gif-grid */
.media-grid { display: none; width: 100%; height: 100%; overflow-y: auto; overflow-x: hidden; padding: 10px; }
.media-grid.active { display: block; }

/* EmojiMart Specific Wrapper Overrides */
.emoji-grid { display: none; width: 100%; height: 100%; padding: 0; }
.emoji-grid.active { display: block; }
em-emoji-picker {
    width: 100%; height: 100%; min-height: 0; max-height: none; display: block;
    /* Blend emoji-mart's internal theme into the app's own palette so the panel reads as
       one seamless surface (WhatsApp/Telegram style) instead of a separate boxed widget. */
    --em-rgb-background: 255, 255, 255;
    --em-rgb-input: 240, 242, 245;
    --em-rgb-color: 28, 30, 33;
    --em-rgb-accent: 216, 27, 96;
    --em-color-border: #e4e6eb;
    --em-color-border-over: #d8dadf;
    --em-font-family: inherit;
}
[data-theme="dark"] em-emoji-picker {
    --em-rgb-background: 30, 30, 30;
    --em-rgb-input: 44, 44, 46;
    --em-rgb-color: 245, 245, 245;
    --em-rgb-accent: 206, 147, 216;
    --em-color-border: #2a2a2a;
    --em-color-border-over: #3a3a3a;
}

.gif-grid.active {
    display: grid;
    grid-template-columns: 1fr 1fr;
    grid-auto-rows: min-content;
    gap: 8px;
    align-content: start;
}
.gif-item { width: 100%; height: 140px; object-fit: cover; border-radius: 8px; cursor: pointer; display: block; background: var(--border-color); transition: filter 0.1s; }
.gif-item:active { filter: brightness(0.8); }

#mediaLoading { grid-column: 1 / -1; text-align: center; font-size: 0.8rem; color: var(--text-muted); margin-top: 20px; }
#mediaLoading.hidden { display: none; }

/* --- GIF MESSAGES --- */
.msg-gif-wrap { max-width: 280px; line-height: 0; border-radius: 12px; overflow: hidden; }
.msg-gif-wrap.loading { min-width: 160px; min-height: 140px; background: rgba(128, 128, 128, 0.15); }
.msg-gif { display: block; max-width: 100%; max-height: 320px; width: auto; height: auto; border-radius: 12px; -webkit-user-drag: none; }
.msg-gif-wrap.failed { line-height: 1.35; padding: 10px 12px; font-size: 0.8rem; color: var(--text-muted); background: rgba(128, 128, 128, 0.15); }
.msg-gif-caption { margin-top: 6px; }

/* GIF with no caption / no quoted reply: no bubble, no border, no shadow */
.msg.msg-gif-only { background: transparent; box-shadow: none; padding: 0; min-width: 0; max-width: min(280px, 80%); border-radius: 12px; }
.msg.msg-gif-only .msg-text { font-size: 0; line-height: 0; }

/* time + ticks float over the GIF as a small pill (like WhatsApp) */
.msg.msg-gif-only .msg-meta { bottom: 6px; right: 6px; padding: 2px 7px; border-radius: 10px; background: rgba(0, 0, 0, 0.5); color: #fff; }
.msg.msg-gif-only .msg-tick { color: rgba(255, 255, 255, 0.85); }
.msg.msg-gif-only .msg-tick.read { color: #4fc3f7; }

/* --- PENDING GIF PREVIEW (shown above the input until Send is pressed) --- */
.gif-preview { display: none; padding: 10px 16px; background: var(--bg-header); border-top: 1px solid var(--border-color); flex-shrink: 0; z-index: 10; }
.gif-preview.show { display: block; }
.gif-preview-inner { position: relative; display: inline-block; line-height: 0; }
.gif-preview-inner img { display: block; max-height: 130px; max-width: min(220px, 70vw); width: auto; height: auto; border-radius: 12px; background: rgba(128, 128, 128, 0.15); }
.cancel-gif-preview-btn { position: absolute; top: -8px; right: -8px; width: 24px; height: 24px; border-radius: 50%; border: none; background: rgba(0, 0, 0, 0.7); color: #fff; font-size: 1rem; line-height: 24px; text-align: center; cursor: pointer; padding: 0; outline: none; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.3); }
.cancel-gif-preview-btn:active { background: #ff3b30; }

/* GIF thumbnail in the "Replying to..." bar */
.reply-thumb { display: none; width: 44px; height: 44px; object-fit: cover; border-radius: 6px; flex-shrink: 0; margin-right: 8px; background: rgba(128, 128, 128, 0.2); }
.reply-thumb.show { display: block; }

/* --- RECENT GIFS TAB --- */
.recent-grid.active { display: grid; grid-template-columns: 1fr 1fr; grid-auto-rows: min-content; gap: 8px; align-content: start; }
.recent-header { grid-column: 1 / -1; display: flex; align-items: center; justify-content: space-between; padding: 0 2px; font-size: 0.72rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; }
.recent-clear-btn { background: none; border: none; padding: 2px 0; color: var(--accent-color); font-size: 0.75rem; font-weight: 600; text-transform: none; letter-spacing: 0; cursor: pointer; outline: none; }
.recent-clear-btn:active { opacity: 0.6; }
.media-empty { grid-column: 1 / -1; text-align: center; color: var(--text-muted); font-size: 0.82rem; line-height: 1.5; margin-top: 44px; padding: 0 16px; }

/* --- NEW: LINKED DEVICES MENU & MODAL STYLES --- */
.menu-container { position: relative; display: flex; align-items: center; }
.menu-btn { background: transparent; border: none; cursor: pointer; color: var(--text-main); padding: 4px; border-radius: 50%; display: flex; align-items: center; justify-content: center; outline: none; transition: background-color 0.15s; }
.menu-btn:active { background: var(--btn-neutral-active); }
.chat-dropdown { position: absolute; top: 100%; right: 0; background: var(--bg-card); border: 1px solid var(--border-color); border-radius: 12px; box-shadow: 0 4px 16px rgba(0,0,0,0.15); min-width: 160px; z-index: 200; overflow: hidden; opacity: 1; transition: opacity 0.2s, transform 0.2s; transform-origin: top right; margin-top: 4px; }
.chat-dropdown.hidden { opacity: 0; transform: scale(0.95); pointer-events: none; }
.dropdown-item { padding: 12px 16px; font-size: 0.9rem; font-weight: 500; color: var(--text-main); cursor: pointer; transition: background 0.15s; text-align: left; }
.dropdown-item:hover { background: var(--bg-notice); }
.dropdown-item:active { background: var(--btn-neutral-active); }

.devices-list { display: flex; flex-direction: column; gap: 8px; max-height: 300px; overflow-y: auto; text-align: left; padding-right: 4px; }
.device-item { padding: 12px; border: 1px solid var(--border-color); border-radius: 12px; display: flex; justify-content: space-between; align-items: center; background: var(--bg-notice); }
.device-info { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.device-name { font-size: 0.9rem; font-weight: 600; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 2px; }
.device-meta { font-size: 0.75rem; color: var(--text-muted); }
.device-meta.current { color: #34c759; font-weight: 600; }
.device-revoke-btn { flex-shrink: 0; margin-left: 8px; background: rgba(255, 59, 48, 0.1); border: none; color: #ff3b30; padding: 6px 12px; border-radius: 8px; font-size: 0.75rem; font-weight: 600; cursor: pointer; transition: background 0.15s, opacity 0.15s; }
.device-revoke-btn:active { background: rgba(255, 59, 48, 0.2); }
[data-theme="dark"] .device-revoke-btn { background: rgba(255, 69, 58, 0.15); color: #ff453a; }

/* --- NEW: FORGOT PIN LINK STYLES --- */
.forgot-pin-link { display: block; text-align: right; font-size: 0.8rem; color: var(--accent-color); text-decoration: none; font-weight: 600; margin-top: -6px; margin-bottom: 14px; outline: none; transition: opacity 0.15s; }
.forgot-pin-link:active { opacity: 0.6; }
.forgot-pin-link.hidden { display: none; }

/* --- NEW: MODERN OTP-STYLE PIN BOXES --- */
.pin-box-container { display: flex; justify-content: center; gap: 12px; margin-bottom: 16px; width: 100%; }
.pin-box { width: 50px; height: 50px; text-align: center; font-size: 1.5rem; font-weight: 700; border-radius: 12px; border: 1px solid var(--border-color); background: var(--bg-input); color: var(--text-main); outline: none; transition: border-color 0.2s, box-shadow 0.2s; -webkit-text-security: disc; }
.pin-box:focus { border-color: var(--accent-color); box-shadow: 0 0 0 2px rgba(216, 27, 96, 0.15); }
[data-theme="dark"] .pin-box:focus { box-shadow: 0 0 0 2px rgba(206, 147, 216, 0.2); }

/* --- PRIVACY BLUR OVERLAY FOR RECENT APPS --- */
.privacy-overlay {
    position: fixed; top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0, 0, 0, 0.65); backdrop-filter: blur(16px); -webkit-backdrop-filter: blur(16px);
    z-index: 9999999; opacity: 0; pointer-events: none;
    transition: opacity 0.25s ease;
}
.privacy-overlay.active { 
    opacity: 1; pointer-events: all; 
    transition: none !important; 
}
[data-theme="dark"] .privacy-overlay { background: rgba(0, 0, 0, 0.75); }
