:root {
    --bg-color: #ffffff;
    --sidebar-bg: #ffffff;
    --chat-bg: #ffffff;
    --primary-color: #0084ff;
    --primary-hover: #0073e6;
    --text-main: #050505;
    --text-muted: #65676b;
    --border-color: #e4e6eb;
    --msg-sent-bg: #0084ff;
    --msg-received-bg: #e4e6eb;
    --radius: 18px;
}

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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.app-container {
    display: flex;
    width: 100%;
    max-width: 1200px;
    height: 100vh;
    background: var(--sidebar-bg);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    overflow: hidden;
}

@media (min-width: 768px) {
    .app-container {
        height: 95vh;
        border-radius: var(--radius);
    }
}

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

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

.sidebar-header h2 {
    font-size: 1.25rem;
    font-weight: 600;
}

.btn-new-chat {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, color 0.2s;
}

.btn-new-chat:hover {
    background: var(--bg-color);
    color: var(--text-main);
}

.contacts-list {
    flex: 1;
    overflow-y: auto;
}

.contact-item {
    width: calc(100% - 16px);
    display: flex;
    align-items: center;
    padding: 10px 12px;
    margin: 4px 8px;
    border: none;
    background: none;
    cursor: pointer;
    text-align: left;
    border-radius: 8px;
    transition: background 0.2s;
}

.contact-item:hover, .contact-item.active {
    background: rgba(0, 0, 0, 0.05);
}

.contact-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary-color);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 1.1rem;
    margin-right: 16px;
    text-transform: uppercase;
    flex-shrink: 0;
}

.contact-details {
    overflow: hidden;
    flex: 1;
}

.contact-name {
    display: block;
    font-weight: 600;
    font-size: 1.05rem;
    margin-bottom: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-preview {
    display: block;
    font-size: 0.9rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.sidebar-empty {
    padding: 32px 24px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.95rem;
    line-height: 1.5;
}

/* Chat Area */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    position: relative;
}

.chat-header {
    padding: 16px 20px;
    background: var(--sidebar-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    height: 64px;
}

.chat-header-info h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.chat-header-info p {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-top: 2px;
}

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

.empty-state {
    margin: auto;
    padding: 16px 24px;
    background: rgba(255, 255, 255, 0.8);
    border-radius: var(--radius);
    color: var(--text-muted);
    text-align: center;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}

.message {
    display: flex;
    margin-bottom: 12px;
    max-width: 80%;
}

.message.sent {
    align-self: flex-end;
}

.message.received {
    align-self: flex-start;
}

.message-bubble {
    padding: 10px 16px;
    border-radius: var(--radius);
    position: relative;
    box-shadow: none;
    overflow-wrap: break-word;
}

.message.sent .message-bubble {
    background: var(--msg-sent-bg);
    color: #ffffff;
}

.message.received .message-bubble {
    background: var(--msg-received-bg);
    color: var(--text-main);
}

/* Grouping Consecutive Messages */
.message.sent:has(+ .message.sent),
.message.received:has(+ .message.received) {
    margin-bottom: 2px;
}

.message.sent:has(+ .message.sent) .message-bubble {
    border-bottom-right-radius: 4px;
}

.message.sent + .message.sent .message-bubble {
    border-top-right-radius: 4px;
}

.message.received:has(+ .message.received) .message-bubble {
    border-bottom-left-radius: 4px;
}

.message.received + .message.received .message-bubble {
    border-top-left-radius: 4px;
}

.message-bubble p {
    font-size: 0.9375rem;
    line-height: 1.35;
    margin-bottom: 6px;
}

.message-time {
    font-size: 0.7rem;
    display: block;
    text-align: right;
    margin-top: 4px;
    opacity: 0.7;
}

/* Hide time and fix spacing on consecutive grouped messages */
.message.sent:has(+ .message.sent) .message-time,
.message.received:has(+ .message.received) .message-time {
    display: none;
}

.message.sent:has(+ .message.sent) .message-bubble p,
.message.received:has(+ .message.received) .message-bubble p {
    margin-bottom: 0;
}

.message.sent .message-time {
    color: rgba(255, 255, 255, 0.8);
}

.message.received .message-time {
    color: var(--text-muted);
}

/* Message Input */
.message-input-area {
    padding: 16px 20px;
    background: var(--sidebar-bg);
    display: flex;
    align-items: flex-end;
    gap: 12px;
}

.message-input-area textarea {
    flex: 1;
    padding: 10px 16px;
    border: none;
    background: #f0f2f5;
    border-radius: 20px;
    resize: none;
    font-family: inherit;
    font-size: 0.95rem;
    max-height: 140px;
    outline: none;
    transition: background 0.2s;
}

.message-input-area textarea:focus {
    background: #e4e6eb;
}

.btn-send {
    background: none;
    color: var(--primary-color);
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
    flex-shrink: 0;
}

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

.btn-send:disabled {
    background: none;
    color: var(--text-muted);
    cursor: not-allowed;
}

/* Modal */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,0.5);
    z-index: 100;
}

.modal {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 24px;
    border-radius: var(--radius);
    z-index: 101;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.2);
    flex-direction: column;
    gap: 20px;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.modal-header h3 {
    font-size: 1.25rem;
}

.close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-muted);
}

.modal-body {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.modal-body label {
    font-size: 0.95rem;
    color: var(--text-main);
    font-weight: 500;
}

.modal-body input {
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 1rem;
    outline: none;
}

.modal-body input:focus {
    border-color: var(--primary-color);
}

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

.btn-primary {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 500;
    transition: background 0.2s;
}

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

@media (max-width: 767px) {
    .sidebar {
        display: none;
        width: 100%;
        max-width: none;
    }
    /* Further JS logic could be added to toggle sidebar view on mobile */
}