* { box-sizing: border-box; margin: 0; padding: 0; }
:root {
    --primary: #6a5af9;
    --primary-dark: #5646e0;
    --primary-light: #eef0ff;
    --gradient: linear-gradient(135deg, #6a5af9, #9c6ffb);
    --bg: #f4f5f9;
    --border: #e7e8f0;
    --text: #1c1e29;
    --text-muted: #8a8d9c;
    --danger: #e74c3c;
    --success: #2ecc71;
    --radius: 14px;
    --shadow: 0 4px 16px rgba(30, 20, 90, 0.06);
    --shadow-lg: 0 12px 40px rgba(30, 20, 90, 0.18);
}
html, body {
    height: 100%;
    overflow: hidden;
}
/* Fix lỗi 100vh trên mobile (thanh địa chỉ ẩn/hiện, bàn phím ảo làm sai lệch chiều cao).
   --vh được JS set = 1% chiều cao thực (innerHeight). 100dvh là fallback hiện đại,
   trình duyệt nào hỗ trợ sẽ tự ưu tiên khai báo phía sau. */
:root { --vh: 1vh; }
body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg);
    color: var(--text);
}
button { font-family: inherit; }
input { font-family: inherit; }

/* ---------- AUTH PAGES ---------- */
.auth-page {
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    height: 100dvh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gradient);
    padding: 20px;
}
.auth-box {
    background: #fff;
    padding: 40px;
    border-radius: 20px;
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 380px;
}
.auth-box h1 { text-align: center; margin-bottom: 24px; font-size: 26px; font-weight: 700; }
.auth-box form { display: flex; flex-direction: column; gap: 14px; }
.auth-box input {
    padding: 13px 16px;
    border: 1.5px solid var(--border);
    border-radius: 10px;
    font-size: 15px;
    outline: none;
    transition: border-color .15s;
}
.auth-box input:focus { border-color: var(--primary); }
.auth-box button {
    padding: 13px;
    background: var(--gradient);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity .15s, transform .1s;
}
.auth-box button:hover { opacity: .92; }
.auth-box button:active { transform: scale(0.98); }
.auth-box p { text-align: center; margin-top: 18px; font-size: 14px; color: var(--text-muted); }
.auth-box a { color: var(--primary); text-decoration: none; font-weight: 600; }
.auth-box a:hover { text-decoration: underline; }
.alert { padding: 11px 14px; border-radius: 10px; margin-bottom: 16px; font-size: 13.5px; }
.alert-error { background: #fdecea; color: #c0392b; }
.alert-success { background: #eafaf1; color: #219150; }

/* ---------- APP LAYOUT ---------- */
.app { display: flex; height: 100vh; height: calc(var(--vh, 1vh) * 100); height: 100dvh; width: 100vw; overflow: hidden; }

.sidebar {
    width: 320px;
    min-width: 320px;
    background: #fff;
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    height: 100dvh;
}
.sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    padding: 16px;
    border-bottom: 1px solid var(--border);
}
.me-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 6px;
    border-radius: 10px;
    flex: 1;
    min-width: 0;
    text-align: left;
    transition: background .15s;
}
.me-profile:hover { background: var(--bg); }
.me-info { display: flex; flex-direction: column; min-width: 0; overflow: hidden; }
.me-info strong {
    font-size: 14.5px; font-weight: 600;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.me-info small { font-size: 12px; color: var(--text-muted); }

.logout-btn {
    flex-shrink: 0;
    display: flex; align-items: center; justify-content: center;
    width: 36px; height: 36px;
    border-radius: 10px;
    color: var(--text-muted);
    text-decoration: none;
    transition: background .15s, color .15s;
}
.logout-btn:hover { background: #fdecea; color: var(--danger); }

/* ---------- AVATAR (dùng chung khắp app) ---------- */
.avatar-wrap {
    position: relative;
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    overflow: visible;
    background: var(--gradient);
}
.avatar-wrap img {
    width: 100%; height: 100%;
    border-radius: 50%;
    object-fit: cover;
    display: block;
}
.avatar-fallback {
    width: 100%; height: 100%;
    border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    color: #fff; font-weight: 700;
}
.avatar-sm { width: 42px; height: 42px; font-size: 16px; }
.avatar-md { width: 48px; height: 48px; font-size: 18px; }
.avatar-lg { width: 88px; height: 88px; font-size: 32px; }

.dot {
    width: 10px; height: 10px; border-radius: 50%;
    background: var(--text-muted);
    display: inline-block;
    flex-shrink: 0;
}
.dot.online { background: var(--success); }
.avatar-wrap > .dot {
    position: absolute;
    bottom: -1px; right: -1px;
    border: 2.5px solid #fff;
    width: 12px; height: 12px;
}

/* ---------- ADD FRIEND ---------- */
.add-friend { display: flex; padding: 12px 14px 4px; gap: 8px; }
.add-friend input {
    flex: 1;
    min-width: 0;
    padding: 10px 14px;
    border: 1.5px solid var(--border);
    border-radius: 22px;
    font-size: 13.5px;
    outline: none;
    transition: border-color .15s;
}
.add-friend input:focus { border-color: var(--primary); }
.add-friend button {
    flex-shrink: 0;
    border: none;
    background: var(--gradient);
    color: #fff;
    border-radius: 50%;
    width: 38px; height: 38px;
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: opacity .15s;
}
.add-friend button:hover { opacity: .9; }
.small-msg {
    padding: 4px 16px 0;
    font-size: 12px;
    color: var(--primary);
    min-height: 18px;
}

/* ---------- FRIEND REQUESTS ---------- */
.friend-requests { padding: 4px 10px; }
.friend-request-item {
    display: flex; align-items: center; gap: 8px;
    background: #fff8e8; padding: 10px 12px; border-radius: 12px;
    margin: 6px 0; font-size: 13px;
}
.friend-request-item span { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.friend-request-item .actions { flex-shrink: 0; display: flex; gap: 6px; }
.friend-request-item button {
    border: none; border-radius: 8px; width: 26px; height: 26px; cursor: pointer; font-size: 12px;
    display: flex; align-items: center; justify-content: center;
}
.btn-accept { background: var(--success); color: #fff; }
.btn-reject { background: var(--danger); color: #fff; }

/* ---------- FRIEND LIST ---------- */
.friend-list { flex: 1; overflow-y: auto; padding: 6px 8px; }
.empty-hint { text-align: center; color: var(--text-muted); font-size: 13px; margin-top: 24px; padding: 0 16px; }

.friend-item {
    display: flex; align-items: center; gap: 12px;
    padding: 10px; border-radius: 12px; cursor: pointer;
    transition: background .12s;
}
.friend-item:hover { background: var(--bg); }
.friend-item.active { background: var(--primary-light); }
.friend-info { flex: 1; min-width: 0; }
.friend-name {
    font-weight: 600; font-size: 14.5px;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.friend-status { font-size: 12px; color: var(--text-muted); margin-top: 1px; }
.unread-badge {
    flex-shrink: 0;
    background: var(--primary);
    color: #fff; font-size: 11px;
    border-radius: 12px; min-width: 20px; height: 20px;
    padding: 0 6px;
    display: flex; align-items: center; justify-content: center;
    font-weight: 700;
}

/* ---------- CHAT WINDOW ---------- */
.chat-window { flex: 1; display: flex; flex-direction: column; background: var(--bg); height: 100vh; height: calc(var(--vh, 1vh) * 100); height: 100dvh; overflow: hidden; min-width: 0; }
.no-chat {
    flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
    color: var(--text-muted); gap: 10px;
}
.no-chat-icon { font-size: 48px; }
.no-chat p { font-size: 15px; }

.chat-area { flex: 1; display: flex; flex-direction: column; position: relative; min-height: 0; }
.chat-header {
    padding: 14px 20px; background: #fff; border-bottom: 1px solid var(--border);
    display: flex; align-items: center; gap: 12px; font-size: 15px; flex-shrink: 0;
}
.chat-header-info { display: flex; flex-direction: column; min-width: 0; }
.chat-header-info strong { font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.back-btn {
    display: none; /* chỉ hiện trên mobile, xem @media bên dưới */
    border: none; background: transparent; color: var(--text);
    width: 36px; height: 36px; border-radius: 50%;
    align-items: center; justify-content: center; flex-shrink: 0;
    cursor: pointer; margin-right: 2px;
}
.back-btn:hover { background: var(--bg); }
.chat-header-info small { font-size: 12px; color: var(--text-muted); }

.messages {
    flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px;
    min-height: 0;
}
.msg-row { display: flex; align-items: flex-end; gap: 8px; max-width: 100%; }
.msg-row.mine { justify-content: flex-end; }
.msg-row .avatar-wrap { width: 30px; height: 30px; font-size: 13px; }
.msg-content { display: flex; flex-direction: column; max-width: 62%; }
.msg-row.mine .msg-content { align-items: flex-end; }

.bubble {
    padding: 10px 14px; border-radius: 16px; font-size: 14.5px; line-height: 1.45;
    background: #fff; box-shadow: var(--shadow);
    word-break: break-word;
    max-width: 100%;
}
.msg-row.mine .bubble { background: var(--gradient); color: #fff; border-bottom-right-radius: 4px; }
.msg-row:not(.mine) .bubble { border-bottom-left-radius: 4px; }
.bubble.emoji-msg { font-size: 40px; background: transparent; box-shadow: none; padding: 0; line-height: 1; }
.bubble img, .bubble video { max-width: 260px; max-height: 320px; border-radius: 12px; display: block; }
.msg-time { font-size: 10.5px; color: var(--text-muted); margin-top: 3px; padding: 0 4px; }

.composer {
    display: flex; align-items: center; gap: 6px; padding: 12px 16px;
    background: #fff; border-top: 1px solid var(--border); flex-shrink: 0;
}
.composer button {
    border: none; background: transparent; font-size: 21px; cursor: pointer;
    width: 38px; height: 38px; border-radius: 50%;
    display: flex; align-items: center; justify-content: center;
    flex-shrink: 0;
    transition: background .12s;
    color: var(--text-muted);
}
.composer button:hover { background: var(--bg); }
.composer input[type="text"] {
    flex: 1;
    min-width: 0;
    padding: 11px 16px; border-radius: 22px; border: 1.5px solid var(--border); font-size: 14px;
    outline: none;
    transition: border-color .15s;
}
.composer input[type="text"]:focus { border-color: var(--primary); }
.send-btn { color: #fff !important; background: var(--gradient) !important; }
.send-btn:hover { opacity: .9; background: var(--gradient) !important; }

.picker-panel {
    display: none; position: absolute; bottom: 64px; left: 16px;
    background: #fff; border: 1px solid var(--border); border-radius: 16px;
    box-shadow: var(--shadow-lg); padding: 12px;
    width: 300px; max-height: 260px; overflow-y: auto;
    flex-wrap: wrap; gap: 4px;
    z-index: 20;
}
.picker-panel.show { display: flex; }
.picker-panel span {
    font-size: 24px; cursor: pointer; padding: 6px; border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    width: 40px; height: 40px;
    transition: background .1s;
}
.picker-panel span:hover { background: var(--bg); }

/* ---------- PROFILE MODAL ---------- */
.modal-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(20, 15, 50, 0.45);
    align-items: center; justify-content: center;
    z-index: 100;
    padding: 20px;
}
.modal-overlay.show { display: flex; }
.modal-box {
    background: #fff;
    border-radius: 20px;
    width: 100%;
    max-width: 420px;
    box-shadow: var(--shadow-lg);
    max-height: 90vh;
    overflow-y: auto;
}
.modal-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: 20px 22px 12px;
}
.modal-header h2 { font-size: 18px; font-weight: 700; }
.modal-close {
    border: none; background: var(--bg); color: var(--text-muted);
    width: 32px; height: 32px; border-radius: 50%; cursor: pointer;
    font-size: 15px;
    display: flex; align-items: center; justify-content: center;
}
.modal-close:hover { background: #eee; }
.modal-body { padding: 8px 22px 4px; display: flex; flex-direction: column; gap: 6px; }

.profile-avatar-edit {
    display: flex; flex-direction: column; align-items: center; gap: 10px;
    padding: 12px 0 20px;
}
.avatar-upload-btn {
    font-size: 13px; font-weight: 600; color: var(--primary);
    cursor: pointer; padding: 7px 16px;
    border: 1.5px solid var(--primary-light); border-radius: 20px;
    background: var(--primary-light);
    transition: opacity .15s;
}
.avatar-upload-btn:hover { opacity: .85; }

.form-label { font-size: 12.5px; font-weight: 600; color: var(--text-muted); margin-top: 10px; }
.form-input {
    padding: 11px 14px; border: 1.5px solid var(--border); border-radius: 10px;
    font-size: 14.5px; outline: none; transition: border-color .15s;
}
.form-input:focus { border-color: var(--primary); }
.form-input:disabled { background: var(--bg); color: var(--text-muted); cursor: not-allowed; }

.modal-footer {
    display: flex; justify-content: flex-end; gap: 10px;
    padding: 16px 22px 22px;
}
.btn-secondary, .btn-primary {
    padding: 10px 20px; border-radius: 10px; font-size: 14px; font-weight: 600;
    cursor: pointer; border: none; transition: opacity .15s;
}
.btn-secondary { background: var(--bg); color: var(--text); }
.btn-secondary:hover { background: #eaeaf0; }
.btn-primary { background: var(--gradient); color: #fff; }
.btn-primary:hover { opacity: .9; }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; }

/* ---------- SCROLLBARS ---------- */
.friend-list::-webkit-scrollbar, .messages::-webkit-scrollbar, .picker-panel::-webkit-scrollbar, .modal-box::-webkit-scrollbar {
    width: 6px;
}
.friend-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb, .picker-panel::-webkit-scrollbar-thumb, .modal-box::-webkit-scrollbar-thumb {
    background: #d8d9e6; border-radius: 10px;
}

/* ---------- RESPONSIVE ---------- */
@media (max-width: 720px) {
    .sidebar { width: 100%; min-width: 0; position: absolute; inset: 0; z-index: 5; }
    .app.chat-open .sidebar { display: none; }
    .chat-window { width: 100%; }

    /* Hiện nút quay lại trong chat header */
    .back-btn { display: flex; }

    /* Chống iOS Safari tự động zoom khi focus input (yêu cầu font-size >= 16px) */
    .auth-box input,
    #addFriendInput,
    .composer input[type="text"],
    .form-input,
    .modal-box input[type="text"] {
        font-size: 16px;
    }

    /* Tránh picker emoji/sticker tràn ra ngoài màn hình nhỏ */
    .picker-panel {
        left: 8px;
        right: 8px;
        width: auto;
        max-width: calc(100vw - 16px);
    }

    /* Modal hồ sơ không bị bàn phím ảo che mất phần dưới */
    .modal-box {
        max-height: calc(var(--vh, 1vh) * 90);
        max-height: 90dvh;
    }

    /* Composer không bị thu nhỏ quá mức trên màn hình hẹp */
    .composer { padding: 10px 12px; }
    .composer input[type="text"] { padding: 10px 14px; }
}
