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

:root {
    --primary: #0088cc;
    --primary-dark: #006699;
    --bg-dark: #17212b;
    --bg-darker: #0e1621;
    --bg-light: #242f3d;
    --text-primary: #ffffff;
    --text-secondary: #8b9198;
    --border-color: #2b3642;
    --message-in: #182533;
    --message-out: #2b5278;
    --danger: #e53935;
    --success: #4caf50;
    --app-height: 100dvh;
    --app-offset-top: 0px;
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
}

html {
    height: 100%;
    height: 100dvh;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    background-color: var(--bg-dark);
    color: var(--text-primary);
    height: var(--app-height, 100dvh);
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
    color-scheme: dark;
}

/* ==================== LOGIN PAGE ==================== */
.login-page {
    height: var(--app-height, 100dvh);
    min-height: 0;
    width: 100%;
    position: fixed;
    left: 0;
    right: 0;
    top: var(--app-offset-top, 0px);
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(135deg, var(--bg-darker) 0%, var(--bg-dark) 100%);
    padding: 16px;
    padding-top: calc(16px + var(--safe-top));
    padding-bottom: calc(16px + var(--safe-bottom));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.login-container {
    background-color: var(--bg-light);
    padding: 40px;
    border-radius: 16px;
    width: 100%;
    max-width: 400px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.4);
}

.login-header {
    text-align: center;
    margin-bottom: 30px;
}

.login-icon {
    font-size: 3rem;
    display: block;
    margin-bottom: 15px;
}

.login-header h1 {
    font-size: 1.5rem;
    margin-bottom: 5px;
}

.login-header p {
    color: var(--text-secondary);
    font-size: 0.9rem;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    margin-bottom: 8px;
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    background-color: var(--bg-darker);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 16px;
    transition: border-color 0.2s;
}

.form-group input:focus {
    outline: none;
    border-color: var(--primary);
}

.form-group input::placeholder {
    color: var(--text-secondary);
}

.login-btn {
    width: 100%;
    padding: 14px;
    background-color: var(--primary);
    border: none;
    border-radius: 8px;
    color: white;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
}

.login-btn:hover {
    background-color: var(--primary-dark);
}

.login-btn:active {
    transform: scale(0.98);
}

.login-btn:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.register-btn {
    background: linear-gradient(135deg, #4caf50, #2e7d32);
}

.register-btn:hover {
    background: linear-gradient(135deg, #2e7d32, #1b5e20);
}

.login-error {
    color: var(--danger);
    text-align: center;
    margin-top: 15px;
    font-size: 0.85rem;
    min-height: 20px;
}

/* ==================== APP ==================== */
.app {
    display: flex;
    height: var(--app-height, 100dvh);
    width: 100%;
    position: fixed;
    left: 0;
    right: 0;
    top: var(--app-offset-top, 0px);
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 320px;
    min-width: 280px;
    background-color: var(--bg-darker);
    border-right: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border-color);
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.header-top h1 {
    font-size: 1.25rem;
    font-weight: 600;
}

.logout-btn {
    opacity: 0.7;
    transition: opacity 0.2s;
}

.logout-btn:hover {
    opacity: 1;
    background-color: rgba(229, 57, 53, 0.2) !important;
}

.stats {
    display: flex;
    gap: 20px;
    margin-bottom: 15px;
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary);
}

.stat-label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    text-transform: uppercase;
}

.online-status {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 0.8rem;
    color: var(--success);
}

.status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: var(--success);
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

.notify-toggle {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 12px;
    cursor: pointer;
    user-select: none;
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.notify-toggle input {
    position: absolute;
    opacity: 0;
    width: 0;
    height: 0;
}

.toggle-switch {
    position: relative;
    width: 40px;
    height: 22px;
    background-color: var(--bg-light);
    border-radius: 11px;
    flex-shrink: 0;
    transition: background-color 0.2s;
}

.toggle-switch::after {
    content: '';
    position: absolute;
    top: 3px;
    left: 3px;
    width: 16px;
    height: 16px;
    background-color: white;
    border-radius: 50%;
    transition: transform 0.2s;
}

.notify-toggle input:checked + .toggle-switch {
    background-color: var(--primary);
}

.notify-toggle input:checked + .toggle-switch::after {
    transform: translateX(18px);
}

.notify-toggle input:focus-visible + .toggle-switch {
    box-shadow: 0 0 0 2px var(--primary);
}

.toggle-text {
    line-height: 1.2;
}

.user-list {
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    min-height: 0;
}

.user-item {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    cursor: pointer;
    transition: background-color 0.2s;
    border-bottom: 1px solid var(--border-color);
}

.user-item:hover,
.user-item:active {
    background-color: var(--bg-light);
}

.user-item.active {
    background-color: var(--primary);
}

.user-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--primary-dark));
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1.1rem;
    margin-right: 12px;
    flex-shrink: 0;
}

.user-content {
    flex: 1;
    min-width: 0;
}

.user-name {
    font-weight: 500;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-preview {
    font-size: 0.85rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.user-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    margin-left: 8px;
}

.user-time {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.unread-badge {
    background-color: var(--primary);
    color: white;
    font-size: 0.7rem;
    font-weight: 600;
    padding: 2px 8px;
    border-radius: 10px;
    min-width: 20px;
    text-align: center;
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--bg-dark);
    height: 100%;
    min-width: 0;
    overflow: hidden;
}

.no-chat-selected {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.empty-state {
    text-align: center;
    color: var(--text-secondary);
}

.empty-icon {
    font-size: 4rem;
    display: block;
    margin-bottom: 20px;
}

.empty-state h2 {
    font-size: 1.5rem;
    margin-bottom: 10px;
    color: var(--text-primary);
}

.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
}

/* Chat Header */
.chat-header {
    padding: 15px 20px;
    background-color: var(--bg-darker);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-shrink: 0;
}

.back-btn {
    display: none;
    flex-shrink: 0;
    font-size: 2rem;
    line-height: 1;
    width: 44px;
    height: 44px;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.user-info {
    display: flex;
    align-items: center;
    min-width: 0;
    flex: 1;
}

.user-details {
    min-width: 0;
}

.user-details h2 {
    font-size: 1rem;
    font-weight: 600;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.user-id {
    font-size: 0.75rem;
    color: var(--primary);
    font-family: monospace;
    background-color: var(--bg-light);
    padding: 2px 8px;
    border-radius: 4px;
    margin-top: 4px;
    display: inline-block;
}

.chat-actions {
    display: flex;
    gap: 4px;
    flex-shrink: 0;
}

.btn-icon {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    padding: 8px;
    border-radius: 8px;
    transition: background-color 0.2s;
    touch-action: manipulation;
}

.btn-icon:hover {
    background-color: var(--bg-light);
}

.btn-icon.danger:hover {
    background-color: rgba(229, 57, 53, 0.2);
}

/* Messages */
.messages-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 0; /* Important pour le scroll dans flexbox */
    -webkit-overflow-scrolling: touch;
}

.message {
    max-width: 70%;
    padding: 10px 14px;
    border-radius: 12px;
    position: relative;
    animation: fadeIn 0.3s ease;
}

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

.message.incoming {
    background-color: var(--message-in);
    align-self: flex-start;
    border-bottom-left-radius: 4px;
}

.message.outgoing {
    background-color: var(--message-out);
    align-self: flex-end;
    border-bottom-right-radius: 4px;
}

.message-content {
    word-wrap: break-word;
    overflow-wrap: anywhere;
    line-height: 1.4;
    white-space: pre-wrap;
}

.message-time {
    font-size: 0.7rem;
    color: var(--text-secondary);
    margin-top: 5px;
    text-align: right;
}

.message.outgoing .message-time {
    color: rgba(255, 255, 255, 0.6);
}

.message-status {
    display: inline-block;
    margin-left: 5px;
}

.message-status.sent {
    color: var(--text-secondary);
}

.message-status.delivered {
    color: var(--success);
}

/* Reply Area */
.reply-area {
    padding: 15px 20px;
    padding-bottom: calc(15px + var(--safe-bottom));
    background-color: var(--bg-darker);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0; /* Empêche la zone de réponse de rétrécir */
}

#reply-form {
    display: flex;
    align-items: flex-end;
    gap: 12px;
}

#reply-form .btn-icon {
    flex-shrink: 0;
    font-size: 1.3rem;
    padding: 10px;
}

#reply-input {
    flex: 1;
    min-width: 0;
    background-color: var(--bg-light);
    border: none;
    border-radius: 12px;
    padding: 12px 16px;
    color: var(--text-primary);
    font-family: inherit;
    font-size: 16px;
    resize: none;
    max-height: 150px;
    line-height: 1.4;
}

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

#reply-input::placeholder {
    color: var(--text-secondary);
}

.send-btn {
    background-color: var(--primary);
    border: none;
    border-radius: 12px;
    padding: 12px 20px;
    color: white;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 8px;
    transition: background-color 0.2s, transform 0.1s;
}

.send-btn:hover {
    background-color: var(--primary-dark);
}

.send-btn:active {
    transform: scale(0.98);
}

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

.send-icon {
    font-size: 1rem;
}

.send-label {
    display: inline;
}

/* Loading */
.loading {
    text-align: center;
    padding: 40px;
    color: var(--text-secondary);
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background-color: var(--border-color);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: var(--text-secondary);
}

/* Toast notifications */
.toast {
    position: fixed;
    bottom: max(20px, var(--safe-bottom));
    right: 20px;
    background-color: var(--bg-light);
    color: var(--text-primary);
    padding: 12px 20px;
    border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    animation: slideIn 0.3s ease;
    z-index: 1000;
    max-width: calc(100vw - 32px);
}

.toast.success {
    border-left: 4px solid var(--success);
}

.toast.error {
    border-left: 4px solid var(--danger);
}

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

/* Message Images */
.message-image {
    max-width: 100%;
    max-height: 300px;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.2s;
}

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

/* Attach Button */
.attach-btn {
    background: none;
    border: none;
    font-size: 1.3rem;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 8px;
    transition: background-color 0.2s;
    flex-shrink: 0;
}

.attach-btn:hover {
    background-color: var(--bg-light);
}

/* Image Modal */
.image-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.image-modal-backdrop {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    cursor: pointer;
}

.image-modal-content {
    position: relative;
    max-width: 90%;
    max-height: 90%;
    z-index: 2001;
}

.image-modal-content img {
    max-width: 100%;
    max-height: 90vh;
    border-radius: 8px;
    box-shadow: 0 10px 50px rgba(0, 0, 0, 0.5);
}

.image-modal-close {
    position: absolute;
    top: -40px;
    right: 0;
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    padding: 8px 12px;
    border-radius: 8px;
    transition: background-color 0.2s;
}

.image-modal-close:hover {
    background-color: rgba(255, 255, 255, 0.1);
}

/* Responsive */
@media (max-width: 768px) {
    .login-container {
        padding: 28px 20px;
        border-radius: 12px;
        box-shadow: none;
    }

    .login-header h1 {
        font-size: 1.35rem;
    }

    .app {
        display: block;
    }

    .sidebar {
        position: absolute;
        inset: 0;
        width: 100%;
        min-width: 0;
        height: 100%;
        z-index: 20;
        border-right: none;
        transform: translateX(0);
        transition: transform 0.25s ease;
        padding-top: var(--safe-top);
        padding-left: var(--safe-left);
        padding-right: var(--safe-right);
    }

    .app.chat-open .sidebar {
        transform: translateX(-100%);
        pointer-events: none;
    }

    .chat-area {
        width: 100%;
        height: 100%;
        position: absolute;
        inset: 0;
        z-index: 10;
    }

    .no-chat-selected {
        display: none !important;
    }

    .chat-container {
        padding-top: var(--safe-top);
        padding-left: var(--safe-left);
        padding-right: var(--safe-right);
    }

    .sidebar-header {
        padding: 14px 16px 12px;
    }

    .notify-toggle {
        min-height: 44px;
    }

    .header-top h1 {
        font-size: 1.1rem;
    }

    .stats {
        gap: 16px;
        margin-bottom: 10px;
    }

    .stat-value {
        font-size: 1.25rem;
    }

    .user-item {
        padding: 12px 16px;
        min-height: 68px;
    }

    .user-avatar {
        width: 44px;
        height: 44px;
        font-size: 1rem;
    }

    .back-btn {
        display: flex;
    }

    .chat-header {
        padding: 8px 8px 8px 4px;
        gap: 4px;
    }

    .chat-header .user-avatar {
        width: 40px;
        height: 40px;
        margin-right: 8px;
        font-size: 0.9rem;
    }

    .user-details h2 {
        font-size: 0.95rem;
    }

    .user-id {
        font-size: 0.65rem;
        padding: 1px 6px;
        max-width: 100%;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .btn-icon {
        width: 40px;
        height: 40px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        padding: 0;
        font-size: 1.15rem;
    }

    .messages-container {
        padding: 12px;
        gap: 8px;
    }

    .message {
        max-width: 88%;
        padding: 8px 12px;
    }

    .message-image {
        max-height: 220px;
    }

    .reply-area {
        padding: 10px 10px calc(10px + var(--safe-bottom));
    }

    #reply-form {
        gap: 8px;
    }

    #reply-form .btn-icon,
    .attach-btn {
        width: 44px;
        height: 44px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    #reply-input {
        padding: 11px 12px;
        max-height: 120px;
        border-radius: 22px;
    }

    .send-btn {
        width: 44px;
        height: 44px;
        padding: 0;
        border-radius: 50%;
        justify-content: center;
        gap: 0;
        flex-shrink: 0;
    }

    .send-label {
        display: none;
    }

    .send-icon {
        font-size: 1.1rem;
        line-height: 1;
    }

    .empty-icon {
        font-size: 3rem;
    }

    .empty-state h2 {
        font-size: 1.2rem;
        padding: 0 16px;
    }

    .empty-state p {
        padding: 0 16px;
    }

    .toast {
        left: 16px;
        right: 16px;
        bottom: calc(16px + var(--safe-bottom));
        text-align: center;
        animation: slideUp 0.3s ease;
    }

    .image-modal-content {
        max-width: 96%;
        max-height: 85%;
    }

    .image-modal-close {
        top: auto;
        bottom: -48px;
        right: 50%;
        transform: translateX(50%);
    }
}

@media (max-width: 380px) {
    .user-id {
        display: none;
    }

    .header-top h1 {
        font-size: 1rem;
    }
}

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

@media (min-width: 769px) and (max-width: 1024px) {
    .sidebar {
        width: 280px;
        min-width: 240px;
    }

    .message {
        max-width: 80%;
    }
}
