@import url('https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap');

:root {
    --primary: #00B8D4;
    --primary-dark: #0086CA;
    --primary-light: #E1F5FE;
    --text-primary: #050505;
    --text-secondary: #65676B;
    --bg-primary: #FFFFFF;
    --bg-secondary: #F0F2F5;
    --bg-tertiary: #E4E6EB;
    --border: #CED0D4;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
    --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.12);
    --radius-sm: 8px;
    --radius-md: 18px;
    --radius-lg: 24px;
    --gradient: linear-gradient(135deg, #00B8D4 0%, #0086CA 100%);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: 'Manrope', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg-secondary);
    color: var(--text-primary);
    line-height: 1.5;
    overflow: hidden;
    /* Fix iOS Safari viewport */
    position: fixed;
    width: 100%;
    height: 100%;
}

/* ===== LOGIN SCREEN ===== */
#loginScreen {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 50%, #f093fb 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: gradientShift 15s ease infinite;
    background-size: 200% 200%;
}

@keyframes gradientShift {
    0%, 100% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
}

.login-container {
    width: 100%;
    max-width: 420px;
    padding: 24px;
}

.login-box {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px);
    border-radius: var(--radius-lg);
    padding: 48px 40px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: slideUp 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

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

.login-logo {
    width: 80px;
    height: 80px;
    margin: 0 auto 24px;
    animation: logoFloat 3s ease-in-out infinite;
}

@keyframes logoFloat {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-10px); }
}

.login-logo svg {
    width: 100%;
    height: 100%;
    filter: drop-shadow(0 4px 12px rgba(0, 184, 212, 0.3));
}

.login-box h1 {
    font-size: 28px;
    font-weight: 800;
    text-align: center;
    margin-bottom: 8px;
    background: var(--gradient);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.login-subtitle {
    text-align: center;
    color: var(--text-secondary);
    font-size: 15px;
    margin-bottom: 32px;
}

.input-group {
    margin-bottom: 16px;
}

.input-group input {
    width: 100%;
    padding: 14px 16px;
    border: 2px solid var(--border);
    border-radius: var(--radius-md);
    font-size: 15px;
    font-family: inherit;
    transition: all 0.2s;
    background: var(--bg-primary);
}

.input-group input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 4px rgba(0, 184, 212, 0.1);
}

/* Remember Me Checkbox */
.remember-me-label {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 16px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-secondary);
    user-select: none;
}

.remember-me-label input[type="checkbox"] {
    width: 18px;
    height: 18px;
    accent-color: var(--primary);
    cursor: pointer;
    flex-shrink: 0;
}

.remember-me-label span {
    line-height: 1.3;
}

.btn-primary {
    width: 100%;
    padding: 14px;
    background: var(--gradient);
    color: white;
    border: none;
    border-radius: var(--radius-md);
    font-size: 16px;
    font-weight: 700;
    font-family: inherit;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    position: relative;
    overflow: hidden;
}

.btn-primary::before {
    content: '';
    position: absolute;
    inset: 0;
    background: linear-gradient(135deg, rgba(255,255,255,0.3) 0%, rgba(255,255,255,0) 100%);
    opacity: 0;
    transition: opacity 0.3s;
}

.btn-primary:hover::before {
    opacity: 1;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 24px rgba(0, 184, 212, 0.4);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-loader {
    display: inline-block;
    width: 18px;
    height: 18px;
    border: 2px solid rgba(255,255,255,0.3);
    border-top-color: white;
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.error-message {
    margin-top: 12px;
    padding: 12px;
    background: rgba(239, 83, 80, 0.1);
    color: #D32F2F;
    border-radius: var(--radius-sm);
    font-size: 14px;
    text-align: center;
    display: none;
}

.error-message.show {
    display: block;
    animation: shake 0.4s;
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-8px); }
    75% { transform: translateX(8px); }
}

/* ===== CHAT SCREEN ===== */
#chatScreen {
    position: fixed;
    inset: 0;
    display: flex;
    flex-direction: column;
}

.chat-container {
    width: 100%;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height - fix cho mobile keyboard */
    display: flex;
    flex-direction: column;
    background: var(--bg-primary);
    max-height: -webkit-fill-available; /* iOS Safari fix */
}

/* Header */
.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 20px;
    padding-top: max(12px, env(safe-area-inset-top)); /* iOS notch */
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-sm);
    position: relative;
    z-index: 10;
    flex-shrink: 0; /* Prevent header from shrinking */
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-logo {
    width: 32px;
    height: 32px;
}

.header-logo svg {
    width: 100%;
    height: 100%;
}

.chat-header h2 {
    font-size: 18px;
    font-weight: 700;
}

.header-right {
    display: flex;
    gap: 8px;
}

.icon-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--bg-secondary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    color: var(--text-secondary);
}

.icon-btn:hover {
    background: var(--bg-tertiary);
    transform: scale(1.05);
}

.icon-btn.active {
    background: var(--primary);
    color: white;
}

.icon-btn svg {
    width: 20px;
    height: 20px;
}

/* Delete Toolbar */
.delete-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 20px;
    background: #FFF3CD;
    border-bottom: 1px solid #FFE69C;
    animation: slideDown 0.3s;
}

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

.toolbar-info {
    font-size: 14px;
    font-weight: 600;
    color: #856404;
}

.toolbar-info span {
    color: #00B8D4;
    font-size: 16px;
}

.toolbar-actions {
    display: flex;
    gap: 8px;
}

.btn-toolbar {
    padding: 8px 16px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-cancel {
    background: #6c757d;
    color: white;
}

.btn-cancel:hover {
    background: #5a6268;
}

.btn-delete {
    background: #dc3545;
    color: white;
}

.btn-delete:hover:not(:disabled) {
    background: #c82333;
    transform: translateY(-2px);
}

.btn-delete:disabled {
    background: #e7e7e7;
    color: #999;
    cursor: not-allowed;
}

/* Messages Container */
.messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--bg-secondary);
    position: relative;
    -webkit-overflow-scrolling: touch; /* Smooth scrolling on iOS */
    min-height: 0; /* Fix flex shrink issue */
}

.messages-wrapper {
    padding: 20px 40px 20px 40px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    min-height: 100%;
}

.loading-messages {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.spinner {
    width: 40px;
    height: 40px;
    margin: 0 auto 16px;
    border: 3px solid var(--bg-tertiary);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

/* Load More Indicator - ADDED FOR LAZY LOAD */
.load-more-indicator {
    text-align: center;
    padding: 16px 20px;
    color: var(--text-secondary);
    font-size: 14px;
}

.load-more-indicator .spinner {
    width: 24px;
    height: 24px;
    margin: 0 auto;
}

/* Message Bubbles */
.message {
    display: flex;
    gap: 8px;
    max-width: 70%;
    width: fit-content; /* CRITICAL: Make bubble only as wide as content */
    animation: messageSlideIn 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    position: relative;
}

.message:hover .message-actions {
    opacity: 1;
}

@keyframes messageSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.own {
    margin-left: auto;
    flex-direction: row-reverse;
}

/* Message Avatar */
.message-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--gradient);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 14px;
    flex-shrink: 0;
    margin-top: 20px; /* Align with username */
}

/* Message Actions */
.message-actions {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    right: -60px;
    opacity: 0;
    transition: opacity 0.2s;
}

.message.own .message-actions {
    left: -40px;
    right: auto;
}

.message-actions-btn {
    width: 32px;
    height: 32px;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
    position: relative;  /* Make button the positioning context for menu */
}

.message-actions-btn:hover {
    background: var(--bg-tertiary);
    transform: scale(1.1);
}

.message-actions-btn svg {
    width: 18px;
    height: 18px;
}

/* Message Context Menu */
.message-menu {
    position: absolute;
    bottom: calc(100% + 5px);  /* 4px trên button */
    right: -60px;
    background: var(--bg-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    min-width: 150px;
    opacity: 0;
    visibility: hidden;
    transform: translateY(4px);
    transition: all 0.2s cubic-bezier(0.16, 1, 0.3, 1);
    z-index: 1000;
}

.message.own .message-menu {
    right: auto;
    left: -4px;;
}

.message-menu.show {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}

.menu-item {
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    transition: background 0.2s;
    border: none;
    background: none;
    width: 100%;
    text-align: left;
    font-size: 15px;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
}

.menu-item:first-child {
    border-radius: var(--radius-md) var(--radius-md) 0 0;
}

.menu-item:last-child {
    border-radius: 0 0 var(--radius-md) var(--radius-md);
}

.menu-item:only-child {
    border-radius: var(--radius-md);
}

.menu-item:hover {
    background: var(--bg-secondary);
}

.menu-item.danger {
    color: #dc3545;
}

.menu-item.danger:hover {
    background: rgba(220, 53, 69, 0.1);
}

.menu-item svg {
    width: 18px;
    height: 18px;
    flex-shrink: 0;
}

/* Delete Mode Styles */
.message.delete-mode {
    cursor: pointer;
}

.message.delete-mode:hover {
    background: rgba(0, 184, 212, 0.05);
    border-radius: var(--radius-md);
}

.message.delete-mode.selected {
    background: rgba(0, 184, 212, 0.15);
    border-radius: var(--radius-md);
}

.message-checkbox {
    position: absolute;
    left: -30px;
    top: 50%;
    transform: translateY(-50%);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s;
}

.message.own .message-checkbox {
    right: -30px;
    left: auto;
}

.message.delete-mode .message-checkbox {
    opacity: 1;
    pointer-events: all;
}

.message-checkbox input[type="checkbox"] {
    width: 20px;
    height: 20px;
    cursor: pointer;
    accent-color: var(--primary);
}

/* Message Content Container */
.message-content {
    flex: 0 1 auto; /* CRITICAL: Don't grow, allow shrink, auto size */
    min-width: 0; /* CRITICAL FIX: Allow content to shrink below initial size */
    max-width: 100%; /* Don't exceed message container */
}

.message-username {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
    padding: 0 12px;
}

.message.own .message-username {
    text-align: right;
}

.message-bubble {
    padding: 10px 16px;
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    box-shadow: var(--shadow-sm);
    position: relative;
    /* CRITICAL FIX FOR TEXT OVERFLOW */
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: break-word;
    max-width: 100%; /* Ensure bubble doesn't exceed parent width */
}

.message.own .message-bubble {
    background: var(--gradient);
    color: white;
}

.message-text {
    font-size: 15px;
    line-height: 1.4;
    white-space: pre-wrap;
    /* CRITICAL FIX: Allow long words/URLs to break */
    word-wrap: break-word;
    word-break: break-word;
    overflow-wrap: break-word;
    /* Break long unbroken strings like URLs */
    hyphens: auto;
}

/* Single image */
.message-image {
    max-width: 250px;
    max-height: 250px;
    object-fit: cover;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: all 0.2s;
    display: block;
}

.message-image:hover {
    transform: scale(1.02);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* Multiple images grid */
.message-images-grid {
    display: grid;
    gap: 4px;
    max-width: 300px;
    border-radius: var(--radius-sm);
    overflow: hidden;
}

.message-images-grid.count-2 {
    grid-template-columns: repeat(2, 1fr);
}

.message-images-grid.count-3 {
    grid-template-columns: repeat(2, 1fr);
}

.message-images-grid.count-3 .message-image:first-child {
    grid-column: span 2;
}

.message-images-grid.count-4 {
    grid-template-columns: repeat(2, 1fr);
}

.message-images-grid.count-5,
.message-images-grid.count-more {
    grid-template-columns: repeat(3, 1fr);
}

.message-images-grid .message-image {
    width: 100%;
    height: 150px;
    max-width: none;
    max-height: none;
    object-fit: cover;
    border-radius: 0;
}

.message-images-grid.count-1 .message-image {
    height: 250px;
    border-radius: var(--radius-sm);
}

/* More indicator */
.image-more-overlay {
    position: relative;
}

.image-more-overlay::after {
    content: '+' attr(data-more);
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 32px;
    font-weight: 700;
}

.message-video {
    max-width: 300px;
    max-height: 300px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: transform 0.2s;
}

.message-video:hover {
    transform: scale(1.02);
}

.message-document {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: rgba(0, 0, 0, 0.05);
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.2s;
    max-width: 100%;
}

.message.own .message-document {
    background: rgba(255, 255, 255, 0.2);
}

.message-document:hover {
    background: rgba(0, 0, 0, 0.08);
}

.message.own .message-document:hover {
    background: rgba(255, 255, 255, 0.3);
}

.document-icon {
    width: 40px;
    height: 40px;
    background: var(--primary-light);
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.document-icon svg {
    width: 24px;
    height: 24px;
    color: var(--primary);
}

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

.document-name {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.document-size {
    font-size: 12px;
    opacity: 0.7;
}

.message-time {
    font-size: 11px;
    color: var(--text-secondary);
    padding: 0 12px;
}

.message.own .message-time {
    text-align: right;
}

/* Input Area */
.chat-input-container {
    padding: 12px 20px 20px;
    padding-bottom: max(20px, env(safe-area-inset-bottom)); /* iOS home indicator */
    background: var(--bg-primary);
    border-top: 1px solid var(--border);
    flex-shrink: 0; /* Prevent input from shrinking */
}

.input-wrapper {
    background: var(--bg-secondary);
    border-radius: var(--radius-lg);
    padding: 8px;
}

.file-preview {
    padding: 12px;
    border-bottom: 1px solid var(--border);
}

.preview-content {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.preview-item {
    display: flex;
    align-items: center;
    gap: 12px;
    background: var(--bg-primary);
    padding: 12px;
    border-radius: var(--radius-sm);
    position: relative;
}

.preview-thumbnail {
    width: 48px;
    height: 48px;
    border-radius: var(--radius-sm);
    background: var(--bg-tertiary);
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    flex-shrink: 0;
}

.preview-thumbnail img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

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

.preview-name {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.preview-size {
    font-size: 12px;
    color: var(--text-secondary);
}

.preview-remove {
    width: 24px;
    height: 24px;
    border: none;
    background: var(--bg-tertiary);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: var(--text-secondary);
    transition: all 0.2s;
    padding: 0;
    position: absolute;
    top: 4px;
    right: 4px;
}

.preview-remove:hover {
    background: #ef5350;
    color: white;
}

.preview-remove svg {
    width: 14px;
    height: 14px;
}

.input-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
}

.username-input {
    width: 120px;
    padding: 10px 12px;
    border: none;
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    font-size: 14px;
    font-family: inherit;
    transition: all 0.2s;
}

.username-input:focus {
    outline: none;
    box-shadow: 0 0 0 2px var(--primary-light);
}

#messageInput {
    flex: 1;
    padding: 10px 12px;
    border: none;
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    font-size: 15px;
    font-family: inherit;
    resize: none;
    max-height: 120px;
    transition: all 0.2s;
}

#messageInput:focus {
    outline: none;
    box-shadow: 0 0 0 2px var(--primary-light);
}

.send-btn {
    background: var(--gradient);
    color: white;
}

.send-btn:hover {
    background: var(--primary-dark);
    transform: scale(1.1);
}

.send-btn.disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.send-btn.disabled:hover {
    transform: none;
}

/* Scrollbar */
.messages-container::-webkit-scrollbar {
    width: 8px;
}

.messages-container::-webkit-scrollbar-track {
    background: transparent;
}

.messages-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary);
}

/* Mobile Responsive */
@media (max-width: 768px) {
    .message {
        max-width: 85%;
    }
    
    .chat-header {
        padding: 10px 16px;
        padding-top: max(10px, env(safe-area-inset-top));
    }
    
    .messages-wrapper {
        padding: 16px 16px 16px 16px; /* Reduced horizontal padding on mobile */
    }
    
    .chat-input-container {
        padding: 10px 16px 16px;
        padding-bottom: max(16px, env(safe-area-inset-bottom));
    }
    
    .username-input {
        width: 100px;
        font-size: 13px;
    }
    
    #messageInput {
        font-size: 16px; /* Prevent zoom on iOS */
        max-height: 80px;
    }
    
    /* Message actions on mobile - always show on tap */
    .message-actions {
        right: -30px;
    }
    
    .message.own .message-actions {
        left: -30px;
    }
    
    .message-actions-btn {
        width: 28px;
        height: 28px;
    }
    
    .message-actions-btn svg {
        width: 16px;
        height: 16px;
    }
    
    .message-checkbox {
        left: -26px;
    }
    
    .message.own .message-checkbox {
        right: -26px;
    }
}

/* Extra small devices */
@media (max-width: 480px) {
    .messages-wrapper {
        padding: 12px 12px 12px 12px;
    }
    
    .message {
        max-width: 90%;
    }
}

/* ===== TOAST & LIGHTBOX ANIMATIONS ===== */
@keyframes slideInRight {
    from { transform: translateX(100px); opacity: 0; }
    to { transform: translateX(0); opacity: 1; }
}

@keyframes slideOutRight {
    from { transform: translateX(0); opacity: 1; }
    to { transform: translateX(100px); opacity: 0; }
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

/* Lightbox overlay */
.lightbox-overlay {
    animation: fadeIn 0.2s ease-in;
}

.lightbox-overlay img {
    animation: fadeIn 0.3s ease-in;
}