:root {
    --bg-color: #f5f5f7;
    --app-bg: #fff;
    --sidebar-bg: #ffffff;
    --chat-bg: #f0f0f0; 
    --primary: #0084ff;
    --primary-dark: #0066cc;
    --primary-light: #e7f3ff;
    --text-primary: #0a0a0b;
    --text-secondary: #65676b;
    --border-color: #e5e5ea;
    --msg-sent-bg: #0084ff;
    --msg-sent-color: #ffffff;
    --msg-received-bg: #e5e5ea;
    --msg-received-color: #0a0a0b;
    --header-height: 60px;
    --input-height: 60px;
    --font-family: 'Vazirmatn', sans-serif;
}

* { box-sizing: border-box; outline: none; -webkit-tap-highlight-color: transparent; }

html, body {
    margin: 0; padding: 0;
    font-family: var(--font-family);
    background: var(--bg-color);
    height: 100%;
    width: 100%;
    overflow: hidden;
    color: var(--text-primary);
}

/* --- Scrollbar Hiding --- */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: rgba(0,0,0,0.2); border-radius: 3px; }

/* --- Login Screen --- */
#login-screen {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: linear-gradient(135deg, var(--primary-light), #fff); z-index: 2000;
    display: none; justify-content: center; align-items: center;
}
.auth-box { width: 90%; max-width: 380px; text-align: center; padding: 30px 20px; background: white; border-radius: 20px; box-shadow: 0 10px 40px rgba(0,132,255,0.15); }
.auth-logo { font-size: 64px; color: var(--primary); margin-bottom: 20px; animation: float 3s ease-in-out infinite; }
@keyframes float { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-10px); } }
.auth-box h2 { color: var(--primary); margin-bottom: 30px; font-weight: 700; font-size: 24px;}

input[type="text"], input[type="password"], input[type="email"] {
    width: 100%; padding: 14px 16px; margin-bottom: 15px;
    border: 1px solid var(--border-color); border-radius: 12px;
    font-family: var(--font-family); font-size: 15px; background: var(--chat-bg); transition: all 0.3s ease;
}
input:focus { border-color: var(--primary); background: #fff; box-shadow: 0 0 0 3px var(--primary-light); }

.btn {
    width: 100%; padding: 14px; background: linear-gradient(135deg, var(--primary), var(--primary-dark)); color: white;
    border: none; border-radius: 24px; font-family: var(--font-family);
    font-size: 16px; font-weight: 600; cursor: pointer; box-shadow: 0 4px 15px rgba(0,132,255,0.4);
    transition: all 0.3s ease;
}
.btn:hover { transform: translateY(-2px); box-shadow: 0 6px 20px rgba(0,132,255,0.5); }
.btn:active { transform: scale(0.98); }
.btn-sm { width: auto; padding: 5px 15px; font-size: 12px; }
.btn-text { width: auto; background: none; color: #54656f; padding: 8px 0; font-size: 14px; font-weight: 500; cursor: pointer; border-radius: 6px; transition: 0.2s;}
.btn-text:hover:not(.active) { opacity: 0.8; }
.btn-text.active { background: #ffffff; color: var(--primary); box-shadow: 0 1px 2px rgba(0,0,0,0.1); }

.toggle-link { margin-top: 20px; font-size: 14px; color: var(--primary); cursor: pointer; display: inline-block; border-bottom: 1px dashed var(--primary); }

/* --- Main App Layout --- */
#app { display: none; width: 100%; height: 100%; position: fixed; top: 0; left: 0; bottom: 0; right: 0; box-shadow: 0 0 50px rgba(0,0,0,0.1); }

aside, main {
    position: absolute; top: 0; bottom: 0;
    width: 100%; height: 100%;
    display: flex; flex-direction: column;
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    right: 0; left: 0; background: #fff;
}

/* --- Sidebar --- */
aside { background: var(--sidebar-bg); z-index: 10; transform: translateX(0); overflow: hidden; }

.sidebar-header {
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex; flex-direction: column;
    justify-content: center;
    padding: 12px 15px;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.04);
}
.sidebar-top-row { display: flex; justify-content: space-between; align-items: center; width: 100%; margin-bottom: 12px; }
.current-user-name { font-size: 18px; font-weight: 700; color: var(--text-primary); }
.sidebar-actions { display: flex; gap: 8px; }
.icon-btn { background: none; border: none; font-size: 22px; color: var(--primary); cursor: pointer; padding: 8px; border-radius: 50%; transition: all 0.2s ease; display: flex; align-items: center; justify-content: center; }
.icon-btn:hover { background: var(--primary-light); transform: scale(1.05); }
.icon-btn:active { transform: scale(0.95); }

/* Sidebar Tabs (Telegram Style) */
.sidebar-tabs {
    display: flex; background: var(--chat-bg); padding: 3px;
    border-radius: 10px; margin-bottom: 8px; gap: 3px;
}
.tab-btn {
    flex: 1; text-align: center; padding: 8px 0; border-radius: 8px;
    font-size: 14px; font-weight: 600; color: var(--text-secondary); cursor: pointer; transition: all 0.3s ease;
}
.tab-btn.active { background: #ffffff; color: var(--primary); box-shadow: 0 2px 6px rgba(0,132,255,0.15); }
.tab-btn:hover:not(.active) { color: var(--text-primary); }

.contact-search { padding: 10px 12px; background: var(--sidebar-bg); border-bottom: 1px solid var(--border-color); }
.search-box { position: relative; }
.search-box input { margin-bottom: 0; border-radius: 24px; background: var(--chat-bg); border: 1px solid var(--border-color); padding-right: 40px; transition: all 0.2s ease; }
.search-box input:focus { background: #fff; border-color: var(--primary); }
.search-box i { position: absolute; right: 14px; top: 50%; transform: translateY(-50%); color: var(--text-secondary); font-size: 18px; }

.contact-list { flex: 1; overflow-y: auto; }

.contact-item {
    padding: 10px 12px; display: flex; align-items: center; cursor: pointer;
    border-bottom: 1px solid var(--border-color); transition: all 0.2s ease; position: relative;
}
.contact-item:hover { background: var(--primary-light); transform: translateX(-4px); }
.contact-item.active { background: var(--primary-light); border-right: 4px solid var(--primary); padding-right: 8px; }

.avatar {
    width: 52px; height: 52px; border-radius: 50%; 
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-size: 22px; font-weight: 600; margin-left: 12px; flex-shrink: 0;
    box-shadow: 0 2px 8px rgba(0,132,255,0.3);
    transition: transform 0.2s ease;
}
.contact-info { flex: 1; overflow: hidden; text-align: right; margin-left: 10px; }
.contact-name { font-weight: 700; font-size: 15px; color: var(--text-primary); margin-bottom: 4px; display: flex; justify-content: space-between; align-items: center;}
.contact-preview { font-size: 13px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 220px; direction: rtl; font-weight: 400;}

.delete-contact-btn { color: #dc3545; font-size: 18px; padding: 5px; opacity: 0; transition: 0.2s; }
.contact-item:hover .delete-contact-btn { opacity: 1; }

.badge { background: var(--primary); color: white; font-size: 11px; font-weight: 700; padding: 3px 8px; border-radius: 12px; min-width: 20px; text-align: center; display: inline-block; box-shadow: 0 2px 4px rgba(0,132,255,0.3); }
.badge-count-zero { display: none; }
.star-icon { color: #ccc; margin-right: 5px; display: none;}
.is-saved .star-icon { display: inline-block; color: #f1c40f; }

/* --- Main Chat Area --- */
main {
    background: #ffffff;
    z-index: 5; transform: translateX(100%); height: 100%;
}

.chat-header {
    height: var(--header-height);
    background: #ffffff;
    border-bottom: 1px solid var(--border-color);
    display: flex; align-items: center; padding: 0 12px; flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.06);
}
.chat-info { flex: 1; display: flex; align-items: center; }
.back-btn { margin-left: 8px; color: var(--primary); font-size: 24px; }
.chat-avatar { width: 44px; height: 44px; font-size: 18px; margin: 0 12px; background: linear-gradient(135deg, var(--primary), var(--primary-dark)); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: 600; box-shadow: 0 2px 8px rgba(0,132,255,0.3); }
.chat-details { display: flex; flex-direction: column; align-items: flex-start; }
.chat-title { font-weight: 700; font-size: 16px; line-height: 1.2; color: var(--text-primary); }
.chat-status-text { font-size: 12px; color: var(--text-secondary); margin-top: 2px; }
.chat-actions { display: flex; }

.chat-area { flex: 1; padding: 20px 5%; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; background: linear-gradient(180deg, #ffffff 0%, var(--chat-bg) 100%); }

/* Message Bubbles (Swapped: Sent Left, Received Right) */
.msg {
    max-width: 75%; padding: 10px 14px; border-radius: 18px; font-size: 15px;
    line-height: 1.6; position: relative; word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(0,0,0,0.1); margin-bottom: 2px; animation: msgSlideIn 0.3s ease;
}
@keyframes msgSlideIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }
.msg.sent { align-self: flex-start; background: var(--msg-sent-bg); color: var(--msg-sent-color); border-top-left-radius: 18px; border-top-right-radius: 4px; }
.msg.received { align-self: flex-end; background: var(--msg-received-bg); color: var(--msg-received-color); border-top-left-radius: 4px; border-top-right-radius: 18px; }.bubble { word-wrap: break-word; display: flex; flex-direction: column; }
.msg-time { font-size: 12px; opacity: 0.75; margin-top: 4px; display: inline-flex; align-items: center; gap: 4px; vertical-align: bottom; clear: both; line-height: 1; }
.msg-time i { font-size: 12px; }
.meta { font-size: 12px; opacity: 0.75; margin-top: 4px; display: inline-flex; align-items: center; gap: 3px; vertical-align: bottom; line-height: 1; }
.meta i { font-size: 13px; }
.msg.sent .msg-time { float: left; color: rgba(255,255,255,0.8); }
.msg.received .msg-time { float: right; color: var(--text-secondary); }
.msg.sent .meta { float: left; color: rgba(255,255,255,0.8); }
.msg.received .meta { float: right; color: var(--text-secondary); }

/* Image Styles in Messages */
.image-container {
    display: flex;
    justify-content: center;
    align-items: center;
    max-width: 280px;
    border-radius: 10px;
    overflow: hidden;
    position: relative;
}
.msg .thumb {
    max-width: 100%;
    max-height: 300px;
    width: auto;
    height: auto;
    border-radius: 10px;
    cursor: pointer;
    display: block;
    transition: all 0.2s ease;
    object-fit: cover;
}
.msg .thumb:hover { 
    transform: scale(1.02); 
    box-shadow: 0 4px 12px rgba(0,0,0,0.2); 
}
.image-actions {
    position: absolute;
    top: 8px;
    right: 8px;
    display: flex;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s ease;
}
.image-container:hover .image-actions {
    opacity: 1;
}
.image-actions button {
    background: linear-gradient(135deg, rgba(0,132,255,0.9), rgba(0,102,204,0.9));
    color: white;
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    transition: all 0.2s ease;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
}
.image-actions button:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}
.audio-container {
    max-width: 280px;
    padding: 4px 0;
    display: flex;
    align-items: center;
    gap: 8px;
}
.download-audio {
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    color: white;
    border: none;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 14px;
    transition: all 0.2s ease;
    flex-shrink: 0;
}
.download-audio:hover {
    transform: scale(1.1);
    box-shadow: 0 2px 8px rgba(0,132,255,0.3);
}
.msg audio { 
    width: 100%; 
    height: 32px;
    border-radius: 8px;
    flex-shrink: 1;
}
.file-container {
    padding: 0;
}
.msg a { 
    color: var(--primary); 
    text-decoration: none; 
    font-weight: 600; 
    padding: 10px 14px; 
    background: var(--primary-light); 
    border-radius: 8px; 
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin: 4px 0;
    transition: all 0.2s ease;
    cursor: pointer;
}
.msg a:hover { 
    background: var(--primary);
    color: white;
}

.input-area {
    height: auto; min-height: var(--input-height);
    background: #ffffff; display: none; flex-direction: row; align-items: center; padding: 0 12px;
    flex-shrink: 0; border-top: 1px solid var(--border-color); z-index: 20;
    padding-bottom: calc(10px + env(safe-area-inset-bottom)); padding-top: 10px;
}
.input-area input { flex: 1; margin: 0 10px; height: 40px; padding: 0 16px; border: 1px solid var(--border-color); border-radius: 24px; background: var(--chat-bg); font-size: 15px; transition: all 0.2s ease; }
.input-area input:focus { background: #fff; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.input-icon-btn { background: none; border: none; cursor: pointer; color: var(--primary); padding: 8px; width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; transition: all 0.2s ease; flex-shrink: 0; font-size: 20px; }
.input-icon-btn:hover { transform: scale(1.1); color: var(--primary-dark); }
.input-icon-btn:active { transform: scale(0.95); }
.send-btn { color: var(--primary); font-size: 22px; }

/* --- Modals --- */
.modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.5); display: none; justify-content: center; align-items: center; z-index: 3000; backdrop-filter: blur(3px); animation: fadeIn 0.2s ease; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.modal-content { background: white; padding: 24px; border-radius: 16px; width: 90%; max-width: 400px; box-shadow: 0 20px 60px rgba(0,0,0,0.15); animation: slideUp 0.3s cubic-bezier(0.34, 1.56, 0.64, 1); display: flex; flex-direction: column; height: 60vh; }
@keyframes slideUp { from {transform: translateY(40px); opacity: 0;} to {transform: translateY(0); opacity: 1;} }
.user-result-item { padding: 14px 12px; border-bottom: 1px solid var(--border-color); display: flex; justify-content: space-between; align-items: center; transition: all 0.2s ease; cursor: pointer; }
.user-result-item:hover { background: var(--primary-light); transform: translateX(-4px); }

/* Admin & Profile */
@media (max-width: 768px) { .modal-content { width: 100%; height: 100%; border-radius: 0; max-width: none; } }

#admin-panel {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: #fff; z-index: 4000;
    display: none; overflow-y: auto; padding: 24px;
}
table { width: 100%; border-collapse: collapse; margin-top: 20px; font-size: 14px; }
th, td { padding: 14px 12px; text-align: right; border-bottom: 1px solid var(--border-color); }
th { background: var(--primary-light); font-weight: 700; color: var(--primary); }
tr:hover { background: #f9f9f9; }

/* --- Desktop Layout --- */
@media (min-width: 769px) {
    #app { display: none; } 
    #app.visible { display: flex; }
    aside { width: 380px; border-left: 1px solid var(--border-color); position: static; transform: none !important; }
    main { flex: 1; position: static; transform: none !important; }
    .back-btn { display: none; }
    .modal-content { width: 400px; height: auto; border-radius: 16px; }
    .chat-area { padding: 20px 5%; }
}
/* --- Mobile State Classes --- */
#app.chat-active aside { transform: translateX(-100%); }
#app.chat-active main { transform: translateX(0); }

/* --- Mobile-specific adjustments (improve touch targets/layout) --- */
@media (max-width: 768px) {
    :root { --header-height: 56px; --input-height: 64px; }

    /* Make sure app is visible on mobile by default when logged in */
    #app { display: flex; }
    aside { width: 100%; position: fixed; left: 0; top: 0; bottom: 0; z-index: 20; transform: translateX(0); }
    main { width: 100%; position: fixed; left: 0; top: 0; bottom: 0; z-index: 10; transform: translateX(100%); }
    #app.chat-active aside { transform: translateX(-100%); }
    #app.chat-active main { transform: translateX(0); }

    /* Make chat header more compact */
    .chat-header { height: var(--header-height); padding: 8px 10px; }
    .chat-avatar { width: 40px; height: 40px; font-size: 16px; margin: 0 8px; }
    .current-user-name { font-size: 16px; }

    /* Tweak contact row sizes and spacing for thumb navigation */
    .contact-item { padding: 12px; }
    .avatar { width: 44px; height: 44px; font-size: 18px; margin-left: 10px; }
    .contact-preview { max-width: none; }

    /* Messages take more width on small screens */
    .msg { max-width: 90%; font-size: 16px; padding: 12px 16px; border-radius: 16px; }
    .image-container, .audio-container { max-width: 90%; }

    /* Input area fixed to bottom and visible */
    .input-area { position: fixed; left: 0; right: 0; bottom: 0; display: flex; padding: 8px 10px; background: #fff; box-shadow: 0 -6px 20px rgba(0,0,0,0.06); }
    .input-area input { height: 44px; margin: 0 8px; }
    .input-icon-btn { width: 48px; height: 48px; }

    /* Avoid chat content hidden behind input */
    .chat-area { padding-bottom: calc(var(--input-height) + 24px + env(safe-area-inset-bottom)); }

    /* Make modal full-screen and easier to use */
    .modal-content { width: 100%; height: 100%; max-width: none; border-radius: 0; padding: 16px; }

    /* Larger tappable icons */
    .icon-btn { padding: 10px; font-size: 24px; }

    /* Ensure delete button spacing */
    .msg .msg-delete { font-size: 18px; }
}
