:root { --bg: #0b0b0e; --surface: #16161c; --primary: #0088cc; --text: #fff; --border: #2a2a35; --danger: #e74c3c; }
* { box-sizing: border-box; margin: 0; padding: 0; font-family: system-ui, sans-serif; -webkit-tap-highlight-color: transparent; }
html, body { width: 100%; height: 100%; height: 100dvh; overflow: hidden; background: var(--bg); color: var(--text); }
#app, .screen { height: 100%; height: 100dvh; display: flex; flex-direction: column; width: 100%; }
.hidden { display: none !important; }

.full-screen { position: fixed; inset: 0; width: 100vw; height: 100dvh; display: flex; justify-content: center; align-items: center; z-index: 9999999 !important; background: var(--bg); }

#auth-screen { justify-content: center; align-items: center; }
.auth-box { background: var(--surface); padding: 30px; border-radius: 16px; width: 90%; max-width: 400px; text-align: center; }
.auth-box h1 { color: var(--primary); margin-bottom: 20px; }
.auth-box input { width: 100%; padding: 14px; margin-bottom: 15px; border-radius: 10px; border: 1px solid var(--border); background: var(--bg); color: white; font-size:16px;}
.primary-btn { background: var(--primary); color: white; border: none; padding: 10px 20px; border-radius: 8px; cursor: pointer; font-weight: bold; font-size:14px; transition: 0.2s;}
.primary-btn:active { transform: scale(0.95); }
.secondary-btn { background: transparent; color: var(--primary); border: 1px solid var(--primary) !important; padding: 10px 20px; border-radius: 8px; cursor: pointer; font-size:14px; transition: 0.2s;}
.secondary-btn:active { transform: scale(0.95); }
.auth-box button { width: 100%; margin-bottom: 10px; }

header { background: var(--surface); padding: 6px 12px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border); z-index: 20; width: 100%; flex-shrink:0; flex-wrap: nowrap !important; min-height: 48px;}
.header-left { flex: 1 !important; min-width: 0; display: flex; align-items: center; gap: 6px; }
#header-title { font-size: 16px !important; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; margin: 0; font-weight: bold;}
.header-right { display: flex !important; gap: 2px; flex-shrink: 0; align-items: center; }

.icon-btn { background: none; border: none; color: white; font-size: 16px; cursor: pointer; position:relative; width: 32px !important; height: 32px !important; padding: 0 !important; display: flex; justify-content: center; align-items: center; border-radius: 50% !important; z-index: 1000 !important; touch-action: manipulation;}
.icon-btn:active { transform: scale(0.92); background: rgba(255,255,255,0.15) !important; }

.content-area { flex: 1; overflow: hidden; display: flex; flex-direction: column; width: 100%; position: relative; }
.tab-content { display: none; flex: 1; overflow-y: auto; overflow-x:hidden; flex-direction: column; width: 100%; }
.tab-content.active { display: flex; }

#tab-chat.active { height: 100%; display: flex; flex-direction: column; overflow: hidden; }
#messages-container { flex: 1; overflow-y: auto; padding: 10px 12px; width: 100%; position:relative; -webkit-overflow-scrolling: touch;}

#messages-list { display: flex; flex-direction: column; gap: 4px; width: 100%; min-height: 100%; }

.input-area { background: var(--surface); padding: 6px 10px; display: flex; gap: 6px; border-top: 1px solid var(--border); align-items: center; width: 100%; flex-shrink:0; z-index: 10; position: relative; min-height: 50px;}
.input-area input[type="text"] { flex: 1; padding: 10px 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--bg); color: white; font-size:15px; outline:none; min-width: 0; -webkit-appearance: none; -webkit-user-select: text; user-select: text; }
.send-btn { color: var(--primary); font-size: 22px; display:flex; justify-content:center; align-items:center; padding:0 4px; flex-shrink:0; background: none; border: none; cursor: pointer;}

.date-separator { align-self: center; background: var(--surface); color: #888; font-size: 10px; padding: 3px 10px; border-radius: 10px; margin: 6px 0; border: 1px solid var(--border); z-index: 1;}
.message-wrapper { display: flex; flex-direction: column; max-width: 85%; position: relative; animation: popIn 0.2s ease-out; transition: transform 0.2s; margin-bottom: 2px;}
.message-wrapper.own { align-self: flex-start; }
.message-wrapper.other { align-self: flex-end; }
.msg-header { display: flex; align-items: center; gap: 6px; font-size: 11px; color: #888; margin-bottom: 2px; cursor:pointer;}
.msg-header img.avatar { width: 20px; height: 20px; border-radius: 50%; object-fit: cover; }
.message-bubble { padding: 6px 10px; border-radius: 12px; background: var(--surface); border: 1px solid var(--border); word-wrap: break-word; max-width: 100%; font-size:14px; color: #fff; min-width: 20px; box-shadow: 0 1px 2px rgba(0,0,0,0.2); line-height: 1.4;}
.message-wrapper.own .message-bubble { background: var(--primary); border: none; }
.message-bubble img { max-width: 100%; border-radius: 6px; margin-top: 3px; display: block; }
.message-bubble video { max-width: 100%; border-radius: 6px; margin-top: 3px; display: block; background: #000; }
.typing-indicator { position: absolute; bottom: 50px; left: 15px; background: var(--surface); color: var(--primary); font-size: 10px; padding: 3px 10px; border-radius: 10px; border: 1px solid var(--border); z-index: 10; font-weight: bold; animation: fadeIn 0.3s ease;}
.msg-delete-btn { position: absolute; top: -6px; left: -6px; background: var(--danger); color: white; border: none; border-radius: 50%; width: 18px; height: 18px; display: none; cursor:pointer; align-items:center; justify-content:center; font-size: 10px;}
.message-wrapper:hover .msg-delete-btn { display: flex; }

@keyframes popIn { from { opacity: 0; transform: translateY(5px) scale(0.95); } to { opacity: 1; transform: translateY(0) scale(1); } }

.fancy-name { font-weight: bold; background: linear-gradient(90deg, #00d2ff 0%, #3a7bd5 100%); -webkit-background-clip: text; -webkit-text-fill-color: transparent; animation: shine 3s infinite linear; background-size: 200% auto; }
@keyframes shine { to { background-position: 200% center; } }

.role-badge { font-size: 9px; padding: 1px 5px; border-radius: 10px; font-weight: bold; color: white; display: inline-flex; align-items: center; justify-content: center; margin-right: 4px; text-shadow: 0 1px 1px rgba(0,0,0,0.5); animation: pulseGlow 2s infinite alternate; }
.role-owner { background: linear-gradient(45deg, #FFD700, #FF8C00); box-shadow: 0 0 5px #FFD700; border: 1px solid #FFF8DC; }
.role-admin { background: linear-gradient(45deg, #9b59b6, #8e44ad); box-shadow: 0 0 5px #9b59b6; border: 1px solid #d2b4de; }
.role-mod { background: linear-gradient(45deg, #2ecc71, #27ae60); box-shadow: 0 0 5px #2ecc71; border: 1px solid #abebc6; }
.role-trusted { background: linear-gradient(45deg, #3498db, #2980b9); box-shadow: 0 0 5px #3498db; border: 1px solid #aed6f1; }
@keyframes pulseGlow { from { transform: scale(1); filter: brightness(1); } to { transform: scale(1.05); filter: brightness(1.3); } }

.story-header { padding: 12px; display: flex; justify-content: space-between; align-items: center; background: var(--surface); border-bottom: 1px solid var(--border); }
.story-header h3 { font-size: 15px; }
.stories-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)); gap: 12px; padding: 12px; }
.story-item { display: flex; flex-direction: column; align-items: center; cursor: pointer; position:relative;}
.story-circle { width: 60px; height: 60px; border-radius: 50%; border: 2px solid var(--border); padding: 2px; margin-bottom: 4px; position: relative; transition: 0.3s; }
.story-circle.unseen { border-color: var(--primary); }
.story-circle img { width: 100%; height: 100%; border-radius: 50%; object-fit: cover; }
.story-views-badge { position: absolute; bottom: 0; right: 0; background: #e74c3c; color: white; font-size: 9px; padding: 1px 4px; border-radius: 8px; font-weight:bold;}
.story-name { font-size: 11px; text-align: center; color: #ccc; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; width:100%;}
.nav-badge { position: absolute; top: -5px; right: -5px; background: var(--danger); color: white; font-size: 9px; padding: 1px 4px; border-radius: 8px; }

.create-post { padding: 12px; background: var(--surface); border-bottom: 1px solid var(--border); }
.create-post textarea { width: 100%; height: 60px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; color: white; padding: 8px; resize: none; margin-bottom: 8px; font-size:14px;}
.post-actions { display: flex; justify-content: space-between; align-items: center; }
.post-card { background: var(--surface); padding: 12px; border-bottom: 1px solid var(--border); margin-bottom: 8px; animation: fadeIn 0.3s ease;}
.post-header { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.post-header img { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; }
.post-header .name-area { flex: 1; cursor: pointer; }
.post-media { width: 100%; max-height: 350px; object-fit: contain; border-radius: 6px; margin-top: 8px; background: #000; display: block; }
.post-actions-bar { display: flex; gap: 12px; padding-top: 8px; margin-top: 8px; border-top: 1px solid var(--border); }
.post-action-btn { background: none; border: none; color: #aaa; font-size: 13px; display: flex; align-items: center; gap: 4px; cursor: pointer; padding: 4px 8px; border-radius: 6px; transition: 0.2s; }
.post-action-btn:active { background: rgba(255,255,255,0.05); }
.post-action-btn.liked { color: #e74c3c; }
.post-action-btn.liked span { color: #e74c3c; font-weight: bold; }
.post-action-btn span { font-weight: bold; font-size: 12px; }

#comments-modal .modal-content { display: flex; flex-direction: column; max-height: 88dvh; }
.comment-item { display: flex; gap: 8px; padding: 6px 0; border-bottom: 1px solid rgba(255,255,255,0.05); align-items: flex-start; }
.comment-item img { width: 30px; height: 30px; border-radius: 50%; object-fit: cover; flex-shrink: 0; }
.comment-content { flex: 1; min-width: 0; }
.comment-content b { font-size: 12px; display: block; margin-bottom: 2px; }
.comment-content p { font-size: 13px; word-wrap: break-word; line-height: 1.3; }
.comment-content small { font-size: 9px; color: #666; }
.comment-delete { background: none; border: none; color: var(--danger); cursor: pointer; font-size: 12px; padding: 2px 4px; flex-shrink: 0; }

.profile-header { padding: 15px; text-align: center; background: var(--surface); }
.profile-header img { width: 80px; height: 80px; border-radius: 50%; object-fit: cover; border: 2px solid var(--primary); margin-bottom: 10px; }
.profile-header textarea { width: 100%; max-width: 400px; height: 60px; background: var(--bg); border: 1px solid var(--border); border-radius: 6px; color: white; padding: 8px; margin: 10px auto; display: block; resize: none; }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.9); z-index: 99999; display: flex; justify-content: center; align-items: center; height: 100dvh; width:100vw;}
.modal-content { background: var(--surface); padding: 15px; border-radius: 10px; width: 90%; max-width: 400px; border: 1px solid var(--border); max-height:90dvh; overflow-y:auto; position: relative;}
.confirm-box { background: var(--surface); padding: 15px; border-radius: 10px; text-align: center; width: 90%; max-width: 350px; border:1px solid var(--border); z-index: 100000;}
.confirm-actions { display: flex; gap: 8px; margin-top: 12px; }
.confirm-actions button { flex: 1; }
.setting-row { display: flex; justify-content: space-between; align-items: center; margin-bottom: 10px; padding: 8px; background: var(--bg); border-radius: 6px; }
.setting-row select { padding: 4px; background: var(--surface); color: white; border: 1px solid var(--border); border-radius: 4px; font-size:13px;}
.modal-content .icon-btn { position: relative; z-index: 10000 !important; pointer-events: auto !important; }
.modal-content > div:first-child { position: sticky; top: 0; background: var(--surface); z-index: 5; padding-bottom: 8px; }

/* ============ إصلاح القائمة الجانبية (المستخدمين) ============ */
#sidebar-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); z-index: 99998; }
.sidebar { 
    width: 280px; 
    background: var(--surface); 
    position: fixed; /* تم التغيير لـ fixed لضمان الثبات */
    right: 0; 
    top: 0; 
    bottom: 0; 
    z-index: 99999; 
    transform: translateX(100%); 
    transition: 0.3s; 
    display: flex; 
    flex-direction: column; 
    height: 100%;
    height: 100dvh;
}
.sidebar.open { transform: translateX(0); }
.sidebar-header { padding: 12px; display: flex; justify-content: space-between; align-items: center; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.user-lists-container { 
    flex: 1; 
    overflow-y: auto !important; /* إجبار التمرير */
    overflow-x: hidden;
    min-height: 0; /* مهم جداً لعمل التمرير في الفليكس بوكس */
    padding-bottom: 60px; /* مسافة فارغة في الأسفل */
    -webkit-overflow-scrolling: touch; /* تمرير سلس على الهاتف */
    overscroll-behavior: contain;
}
.user-lists-container h4 { padding: 8px 12px; font-size: 12px; color: #888; background: rgba(255,255,255,0.02); margin-top:4px; position: sticky; top: 0; z-index: 2;}
.user-item { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid rgba(255,255,255,0.03); cursor: pointer; }
.user-item img { width: 36px; height: 36px; border-radius: 50%; object-fit: cover; }
.user-item .status-dot { width: 7px; height: 7px; border-radius: 50%; margin-left: auto; flex-shrink:0;}
.user-item .online { background: #2ecc71; box-shadow: 0 0 5px #2ecc71; animation: pulseGlow 1.5s infinite alternate; }
.user-item .offline { background: #555; }
.user-item .last-seen-text { font-size: 9px; color: #888; display: block; margin-top:1px; }
/* ======================================================== */

.bottom-nav { background: var(--surface); display: flex; justify-content: space-around; padding: 4px 0; border-top: 1px solid var(--border); z-index: 20; flex-shrink:0;}
.nav-item { background: none; border: none; color: #888; display: flex; flex-direction: column; align-items: center; font-size: 18px; cursor: pointer; gap: 1px; position:relative; flex: 1 1 0 !important; min-width: 0 !important; padding: 2px !important;}
.nav-item > span:last-child { font-size: 9px !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; display: block; text-align: center; }
.nav-item.active { color: var(--primary); }

#media-viewer { position: fixed; inset: 0; background: rgba(0,0,0,0.95); z-index: 9999999; display: flex; justify-content: center; align-items: center; height:100dvh;}
#media-viewer img, #media-viewer video { max-width: 100%; max-height: 90vh; object-fit: contain; }
.close-viewer { position: absolute; top: 15px; right: 15px; background: rgba(255,255,255,0.2); color: white; border: none; width: 36px; height: 36px; border-radius: 50%; font-size: 18px; cursor: pointer; z-index: 100000 !important; }

.story-nav { position: absolute; top: 50%; transform: translateY(-50%); font-size: 30px; color: white; cursor: pointer; padding: 8px; z-index: 100; user-select: none; background: rgba(0,0,0,0.3); border-radius: 50%; }
.story-nav.prev { left: 8px; }
.story-nav.next { right: 8px; }

#toast-container { position: fixed; top: 15px; left: 50%; transform: translateX(-50%); z-index: 99999999; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { background: var(--primary); color: white; padding: 10px 16px; border-radius: 6px; font-weight: bold; box-shadow: 0 3px 10px rgba(0,0,0,0.5); animation: slideDown 0.3s ease, fadeOut 0.3s ease 2.7s forwards; font-size: 13px;}
@keyframes slideDown { from { top: -50px; opacity: 0; } to { top: 15px; opacity: 1; } }
@keyframes fadeOut { to { opacity: 0; } }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.reply-quote { background: rgba(255,255,255,0.1); border-right: 3px solid rgba(255,255,255,0.5); padding: 4px 6px; border-radius: 4px; margin-bottom: 4px; font-size: 11px; }
.message-wrapper.own .reply-quote { background: rgba(0,0,0,0.15); border-right-color: rgba(255,255,255,0.8); }
.reply-quote b { display: block; color: #aed6f1; margin-bottom: 1px; font-size: 10px;}
.reply-quote span { color: #ddd; display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

#reply-preview-box { background: var(--surface); border-top: 1px solid var(--border); padding: 8px 12px; display: flex; justify-content: space-between; align-items: center; border-right: 3px solid var(--primary); margin: 0; border-radius: 0; }
.reply-preview-content { flex: 1; overflow: hidden; }
.reply-preview-title { color: var(--primary); font-size: 11px; font-weight: bold; margin-bottom: 1px; }
.reply-preview-text { color: #ccc; font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

@media (max-width: 380px) {
    .icon-btn { width: 28px; height: 28px; font-size: 14px; }
    #header-title { font-size: 14px !important; }
    .nav-item { font-size: 16px !important; }
    .nav-item > span:last-child { font-size: 8px !important; }
    .message-wrapper { max-width: 92% !important; }
    .modal-content { width: 95% !important; padding: 12px !important; }
    .message-bubble { font-size: 13px; padding: 5px 8px; }
}
#back-btn { color: var(--primary); font-size: 20px; font-weight: bold; }

#ban-screen { z-index: 999999999 !important; background: #0b0b0e !important; }
