/* Base styles and variables */
:root {
    --primary-color: #4f6df5;
    --secondary-color: #e8eaf6;
    --text-primary: #333;
    --text-secondary: #666;
    --sidebar-bg: #f5f7fb;
    --chat-bg: #ffffff;
    --sent-message-bg: var(--primary-color);
    --received-message-bg: #e9eaeb;
    --sent-message-text: white;
    --received-message-text: #333;
    --input-bg: #f0f2f5;
    --border-color: #e0e0e0;
    --online-status: #4caf50;
    --offline-status: #bdbdbd;
    --shadow-color: rgba(0, 0, 0, 0.1);
    --typing-dot-color: #555;
    --modal-bg: white;
}

.dark-theme {
    --primary-color: #7289da;
    --secondary-color: #2c2f33;
    --text-primary: #ffffff;
    --text-secondary: #b9bbbe;
    --sidebar-bg: #2f3136;
    --chat-bg: #36393f;
    --sent-message-bg: var(--primary-color);
    --received-message-bg: #40444b;
    --sent-message-text: white;
    --received-message-text: #dcddde;
    --input-bg: #40444b;
    --border-color: #202225;
    --online-status: #43b581;
    --offline-status: #747f8d;
    --shadow-color: rgba(0, 0, 0, 0.3);
    --typing-dot-color: #b9bbbe;
    --modal-bg: #2f3136;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    transition: background-color 0.3s, color 0.3s;
}

body {
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--secondary-color);
    color: var(--text-primary);
}

/* App Container */
.app-container {
    width: 100%;
    max-width: 1200px;
    height: 90vh;
    display: flex;
    box-shadow: 0 10px 20px var(--shadow-color);
    border-radius: 12px;
    overflow: hidden;
}

/* Sidebar */
.sidebar {
    width: 30%;
    background-color: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-color);
}

.sidebar-header {
    padding: 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
    color: var(--text-primary);
}

.search-container {
    padding: 15px;
    position: relative;
}

.search-input {
    width: 100%;
    padding: 10px 40px 10px 15px;
    border-radius: 20px;
    border: none;
    background-color: var(--input-bg);
    color: var(--text-primary);
    outline: none;
}

.search-icon {
    position: absolute;
    right: 25px;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-secondary);
}

.user-list {
    flex: 1;
    overflow-y: auto;
    padding: 10px 0;
}

.user-item {
    display: flex;
    align-items: center;
    padding: 12px 20px;
    cursor: pointer;
    transition: background-color 0.2s;
    position: relative;
}

.user-item:hover {
    background-color: rgba(0, 0, 0, 0.05);
}

.user-item.active {
    background-color: rgba(79, 109, 245, 0.1);
}

.user-image {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    margin-right: 15px;
    position: relative;
}

.user-image img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.user-info {
    flex: 1;
}

.user-info h3 {
    font-size: 16px;
    margin-bottom: 5px;
    color: var(--text-primary);
}

.user-info p {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 150px;
}

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

.status-indicator {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    position: absolute;
    bottom: 0;
    right: 0;
    border: 2px solid var(--sidebar-bg);
}

.online {
    background-color: var(--online-status);
}

.offline {
    background-color: var(--offline-status);
}

.profile {
    display: flex;
    align-items: center;
    padding: 15px 20px;
    background-color: var(--sidebar-bg);
    border-top: 1px solid var(--border-color);
}

.profile-image {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 15px;
    position: relative;
}

.profile-image img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.profile-info {
    flex: 1;
}

.profile-info h3 {
    font-size: 15px;
    margin-bottom: 2px;
    color: var(--text-primary);
}

.profile-info p {
    font-size: 12px;
    color: var(--online-status);
}

/* Chat Container */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: var(--chat-bg);
}

.chat-header {
    padding: 15px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    background-color: var(--sidebar-bg);
}

.chat-header-user {
    display: flex;
    align-items: center;
}

.back-button {
    display: none;
    margin-right: 15px;
    cursor: pointer;
    color: var(--text-primary);
}

.chat-header-image {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    margin-right: 15px;
    position: relative;
}

.chat-header-image img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

.chat-header-info h3 {
    font-size: 16px;
    margin-bottom: 3px;
    color: var(--text-primary);
}

.chat-header-info p {
    font-size: 13px;
    color: var(--online-status);
}

.chat-header-actions {
    display: flex;
}

.icon-button {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 16px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 10px;
    color: var(--text-primary);
    transition: background-color 0.2s;
}

.icon-button:hover {
    background-color: rgba(0, 0, 0, 0.05);
}

.message-search-container {
    padding: 10px 20px;
    background-color: var(--sidebar-bg);
    display: none;
    position: relative;
    border-bottom: 1px solid var(--border-color);
}

.message-search-container.active {
    display: block;
}

.message-search-input {
    width: 100%;
    padding: 8px 35px 8px 15px;
    border-radius: 20px;
    border: 1px solid var(--border-color);
    background-color: var(--input-bg);
    color: var(--text-primary);
    outline: none;
}

.close-search {
    position: absolute;
    right: 30px;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    color: var(--text-secondary);
}

.search-results {
    max-height: 200px;
    overflow-y: auto;
    margin-top: 10px;
}

.search-result {
    padding: 8px 15px;
    cursor: pointer;
    border-radius: 4px;
    margin-bottom: 5px;
    background-color: var(--input-bg);
    color: var(--text-primary);
}

.search-result:hover {
    background-color: rgba(79, 109, 245, 0.1);
}

.chat-messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.chat-date {
    text-align: center;
    color: var(--text-secondary);
    font-size: 12px;
    margin: 15px 0;
    position: relative;
}

.chat-date::before, .chat-date::after {
    content: "";
    position: absolute;
    top: 50%;
    width: 70px;
    height: 1px;
    background-color: var(--border-color);
}

.chat-date::before {
    right: calc(50% + 40px);
}

.chat-date::after {
    left: calc(50% + 40px);
}

.message {
    max-width: 70%;
    margin-bottom: 15px;
    position: relative;
    clear: both;
    animation: messageAppear 0.3s ease;
}

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

.message-sent {
    float: right;
    background-color: var(--sent-message-bg);
    color: var(--sent-message-text);
    border-radius: 18px 18px 4px 18px;
    align-self: flex-end;
}

.message-received {
    float: left;
    background-color: var(--received-message-bg);
    color: var(--received-message-text);
    border-radius: 18px 18px 18px 4px;
    align-self: flex-start;
}

.message-content {
    padding: 12px 16px;
    font-size: 15px;
    word-break: break-word;
}

.message-image-container {
    max-width: 300px;
    border-radius: 12px;
    overflow: hidden;
    margin-bottom: 5px;
}

.message-image {
    width: 100%;
    height: auto;
    cursor: pointer;
    transition: transform 0.3s ease;
}

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

.message-info {
    font-size: 11px;
    margin-top: 5px;
    display: flex;
    align-items: center;
}

.message-sent .message-info {
    justify-content: flex-end;
    padding-right: 10px;
    color: rgba(255, 255, 255, 0.7);
}

.message-received .message-info {
    padding-left: 10px;
    color: var(--text-secondary);
}

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

.typing-indicator {
    display: none;
    padding: 15px;
    align-self: flex-start;
}

.typing-indicator.active {
    display: flex;
}

.typing-indicator span {
    height: 8px;
    width: 8px;
    margin: 0 2px;
    background-color: var(--typing-dot-color);
    border-radius: 50%;
    display: inline-block;
    animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-indicator span:nth-child(1) {
    animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingBounce {
    0%, 80%, 100% {
        transform: scale(0.6);
    }
    40% {
        transform: scale(1.0);
    }
}

.chat-input {
    padding: 15px 20px;
    display: flex;
    align-items: center;
    border-top: 1px solid var(--border-color);
    background-color: var(--sidebar-bg);
}

.input-container {
    flex: 1;
    margin: 0 10px;
    background-color: var(--input-bg);
    border-radius: 20px;
    padding: 0 15px;
}

.message-input {
    width: 100%;
    padding: 12px 0;
    border: none;
    background: transparent;
    color: var(--text-primary);
    outline: none;
}

.send-button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: none;
    background-color: var(--primary-color);
    color: white;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    transition: transform 0.2s;
}

.send-button:hover {
    transform: scale(1.05);
}

.send-button:active {
    transform: scale(0.95);
}

/* Emoji Picker */
.emoji-picker {
    position: absolute;
    bottom: 80px;
    left: 20px;
    width: 300px;
    height: 250px;
    background-color: var(--chat-bg);
    border-radius: 10px;
    box-shadow: 0 5px 15px var(--shadow-color);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 1000;
}

.emoji-picker.active {
    display: flex;
}

.emoji-categories {
    display: flex;
    padding: 10px;
    border-bottom: 1px solid var(--border-color);
    background-color: var(--sidebar-bg);
}

.emoji-category {
    flex: 1;
    background: none;
    border: none;
    font-size: 18px;
    padding: 5px;
    cursor: pointer;
    transition: transform 0.2s;
}

.emoji-category:hover {
    transform: scale(1.1);
}

.emoji-category.active {
    background-color: rgba(79, 109, 245, 0.1);
    border-radius: 4px;
}

.emoji-list {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 5px;
    padding: 10px;
    overflow-y: auto;
}

.emoji {
    font-size: 20px;
    padding: 5px;
    cursor: pointer;
    text-align: center;
    border-radius: 4px;
    transition: background-color 0.2s, transform 0.1s;
}

.emoji:hover {
    background-color: rgba(0, 0, 0, 0.05);
    transform: scale(1.1);
}

/* Theme Toggle */
.toggle-container {
    position: relative;
}

.theme-toggle {
    opacity: 0;
    position: absolute;
}

.toggle-label {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 50px;
    height: 26px;
    background-color: var(--primary-color);
    border-radius: 50px;
    position: relative;
    cursor: pointer;
    padding: 5px;
}

.toggle-ball {
    position: absolute;
    left: 2px;
    top: 2px;
    width: 22px;
    height: 22px;
    background-color: white;
    border-radius: 50%;
    transition: transform 0.3s ease;
}

.theme-toggle:checked + .toggle-label .toggle-ball {
    transform: translateX(24px);
}

.fa-moon {
    color: #f1c40f;
}

.fa-sun {
    color: #f39c12;
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.5);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
}

.modal.active {
    opacity: 1;
    visibility: visible;
}

.modal-content {
    width: 100%;
    max-width: 400px;
    background-color: var(--modal-bg);
    padding: 25px;
    border-radius: 10px;
    box-shadow: 0 5px 20px rgba(0, 0, 0, 0.2);
}

.modal-content h2 {
    margin-bottom: 20px;
    color: var(--text-primary);
}

.modal-content input {
    width: 100%;
    padding: 12px 15px;
    margin-bottom: 20px;
    border-radius: 5px;
    border: 1px solid var(--border-color);
    background-color: var(--input-bg);
    color: var(--text-primary);
}

.modal-buttons {
    display: flex;
    justify-content: flex-end;
}

.modal-buttons button {
    padding: 10px 20px;
    border-radius: 5px;
    cursor: pointer;
    transition: background-color 0.2s;
}

#cancel-username {
    background-color: transparent;
    border: 1px solid var(--border-color);
    color: var(--text-primary);
    margin-right: 10px;
}

#save-username {
    background-color: var(--primary-color);
    border: none;
    color: white;
}

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

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

::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0.2);
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background-color: rgba(0, 0, 0, 0.3);
}

/* Responsive Design */
@media (max-width: 900px) {
    .app-container {
        height: 100vh;
        border-radius: 0;
        max-width: 100%;
    }
}

@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        left: 0;
        top: 0;
        height: 100%;
        width: 80%;
        z-index: 100;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
    }

    .sidebar.active {
        transform: translateX(0);
    }

    .chat-container {
        width: 100%;
    }

    .back-button {
        display: block;
    }

    .app-container {
        position: relative;
    }
}

@media (max-width: 480px) {
    .chat-messages {
        padding: 15px 10px;
    }

    .message {
        max-width: 85%;
    }

    .message-content {
        padding: 10px 12px;
        font-size: 14px;
    }

    .emoji-picker {
        width: 90%;
        left: 5%;
    }
}