/* ===== CSS Variables ===== */
:root {
    --primary: #2563eb;
    --primary-hover: #1d4ed8;
    --primary-light: #dbeafe;
    --background: #f8fafc;
    --sidebar-bg: #ffffff;
    --chat-bg: #f1f5f9;
    --white: #ffffff;
    --text-primary: #1e293b;
    --text-secondary: #64748b;
    --text-muted: #94a3b8;
    --border: #e2e8f0;
    --border-light: #f1f5f9;
    --success: #22c55e;
    --warning: #f59e0b;
    --error: #ef4444;
    --whatsapp: #25D366;
    --instagram: #E4405F;

    --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
    --shadow: 0 1px 3px 0 rgb(0 0 0 / 0.1), 0 1px 2px -1px rgb(0 0 0 / 0.1);
    --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
    --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);

    --radius-sm: 6px;
    --radius: 8px;
    --radius-lg: 12px;
    --radius-xl: 16px;
    --radius-full: 9999px;

    --sidebar-width: 360px;
    --header-height: 64px;
    --input-height: 72px;

    --transition: all 0.2s ease;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: 16px;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--background);
    color: var(--text-primary);
    line-height: 1.5;
    overflow: hidden;
}

button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
}

input, textarea {
    font-family: inherit;
    border: none;
    outline: none;
}

/* ===== App Container ===== */
.app-container {
    display: flex;
    height: 100vh;
    width: 100%;
    overflow: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--sidebar-bg);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    height: 100vh;
}

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

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--primary);
}

.logo-icon {
    width: 28px;
    height: 28px;
}

.connection-status {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-top: 8px;
    font-size: 0.75rem;
    color: var(--text-muted);
}

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

.connection-status.connected .status-dot {
    background: var(--success);
    animation: none;
}

.connection-status.disconnected .status-dot {
    background: var(--error);
    animation: none;
}

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

/* ===== Search Box ===== */
.search-box {
    padding: 12px 16px;
    position: relative;
}

.search-icon {
    position: absolute;
    left: 28px;
    top: 50%;
    transform: translateY(-50%);
    width: 18px;
    height: 18px;
    color: var(--text-muted);
    pointer-events: none;
}

.search-input {
    width: 100%;
    padding: 10px 12px 10px 40px;
    background: var(--background);
    border-radius: var(--radius);
    font-size: 0.875rem;
    color: var(--text-primary);
    transition: var(--transition);
}

.search-input::placeholder {
    color: var(--text-muted);
}

.search-input:focus {
    background: var(--white);
    box-shadow: 0 0 0 2px var(--primary-light);
}

/* ===== Conversations List ===== */
.conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conversation-item {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: var(--transition);
}

.conversation-item:hover {
    background: var(--background);
}

.conversation-item.active {
    background: var(--primary-light);
}

.conversation-avatar {
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary) 0%, #60a5fa 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--white);
    font-weight: 600;
    font-size: 1.125rem;
    flex-shrink: 0;
    position: relative;
}

.conversation-avatar.whatsapp {
    background: linear-gradient(135deg, var(--whatsapp) 0%, #128C7E 100%);
}

.conversation-avatar.instagram {
    background: linear-gradient(135deg, var(--instagram) 0%, #833AB4 100%);
}

.channel-indicator {
    position: absolute;
    bottom: -2px;
    right: -2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--white);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-sm);
}

.channel-indicator svg {
    width: 12px;
    height: 12px;
}

.channel-indicator.whatsapp svg {
    color: var(--whatsapp);
}

.channel-indicator.instagram svg {
    color: var(--instagram);
}

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

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

.conversation-name {
    font-weight: 600;
    font-size: 0.9375rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-time {
    font-size: 0.75rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

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

.unread-badge {
    background: var(--primary);
    color: var(--white);
    font-size: 0.6875rem;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: var(--radius-full);
    margin-left: 8px;
}

/* ===== Chat Area ===== */
.chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    min-width: 0;
}

/* ===== Empty State ===== */
.empty-state {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-muted);
    padding: 40px;
}

.empty-icon {
    width: 80px;
    height: 80px;
    margin-bottom: 16px;
    opacity: 0.5;
}

.empty-state h2 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: 8px;
}

.empty-state p {
    font-size: 0.875rem;
}

/* ===== Chat Container ===== */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
}

/* ===== Chat Header ===== */
.chat-header {
    height: var(--header-height);
    min-height: var(--header-height);
    padding: 0 20px;
    background: var(--white);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 16px;
}

.back-button {
    display: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    transition: var(--transition);
}

.back-button:hover {
    background: var(--background);
    color: var(--text-primary);
}

.back-button svg {
    width: 24px;
    height: 24px;
}

.chat-contact {
    display: flex;
    align-items: center;
    gap: 12px;
    flex: 1;
    min-width: 0;
}

.contact-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary) 0%, #60a5fa 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--white);
    font-weight: 600;
    font-size: 1rem;
    flex-shrink: 0;
}

.contact-avatar.whatsapp {
    background: linear-gradient(135deg, var(--whatsapp) 0%, #128C7E 100%);
}

.contact-avatar.instagram {
    background: linear-gradient(135deg, var(--instagram) 0%, #833AB4 100%);
}

.contact-info {
    min-width: 0;
}

.contact-name {
    font-size: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-phone {
    font-size: 0.8125rem;
    color: var(--text-secondary);
}

.channel-badge {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    border-radius: var(--radius);
    background: var(--background);
}

.channel-badge svg {
    width: 20px;
    height: 20px;
}

.channel-badge .whatsapp-icon {
    color: var(--whatsapp);
}

.channel-badge .instagram-icon {
    color: var(--instagram);
}

/* ===== Messages Area ===== */
.messages-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
}

.messages-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: auto;
}

/* ===== Message Bubbles ===== */
.message {
    max-width: 70%;
    animation: fadeIn 0.2s ease;
}

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

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

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

.message-bubble {
    padding: 10px 14px;
    border-radius: var(--radius-lg);
    position: relative;
    word-wrap: break-word;
}

.message.incoming .message-bubble {
    background: var(--white);
    color: var(--text-primary);
    border-bottom-left-radius: 4px;
    box-shadow: var(--shadow-sm);
}

.message.outgoing .message-bubble {
    background: var(--primary);
    color: var(--white);
    border-bottom-right-radius: 4px;
}

.message-content {
    font-size: 0.9375rem;
    line-height: 1.5;
    white-space: pre-wrap;
}

.message-media {
    max-width: 280px;
    border-radius: var(--radius);
    overflow: hidden;
    margin-bottom: 4px;
}

.message-media img {
    width: 100%;
    height: auto;
    display: block;
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: 4px;
}

.message-time {
    font-size: 0.6875rem;
    opacity: 0.7;
}

.message-status {
    display: flex;
    align-items: center;
}

.message-status svg {
    width: 14px;
    height: 14px;
}

.message.outgoing .message-status.delivered svg,
.message.outgoing .message-status.read svg {
    color: #a5d6ff;
}

/* ===== Message Input ===== */
.message-input-container {
    padding: 12px 20px 20px;
    background: var(--chat-bg);
}

.message-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 12px;
    background: var(--white);
    border-radius: var(--radius-xl);
    padding: 8px 8px 8px 16px;
    box-shadow: var(--shadow);
}

.message-input {
    flex: 1;
    resize: none;
    font-size: 0.9375rem;
    line-height: 1.5;
    max-height: 120px;
    padding: 8px 0;
    color: var(--text-primary);
    background: transparent;
}

.message-input::placeholder {
    color: var(--text-muted);
}

.send-button {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: var(--primary);
    color: var(--white);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

.send-button:hover:not(:disabled) {
    background: var(--primary-hover);
    transform: scale(1.05);
}

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

.send-button svg {
    width: 20px;
    height: 20px;
}

/* ===== Loading Spinner ===== */
.loading-spinner {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    color: var(--text-muted);
    gap: 12px;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--border);
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

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

/* ===== Toast Notification ===== */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--text-primary);
    color: var(--white);
    padding: 12px 24px;
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    opacity: 0;
    transition: all 0.3s ease;
    z-index: 1000;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

/* ===== Date Separator ===== */
.date-separator {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 16px 0;
}

.date-separator span {
    background: var(--white);
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 4px 12px;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-sm);
}

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

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

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

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

/* ===== Responsive ===== */
@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        min-width: 100%;
        z-index: 100;
        transition: transform 0.3s ease;
    }

    .sidebar.hidden {
        transform: translateX(-100%);
    }

    .chat-area {
        width: 100%;
    }

    .back-button {
        display: flex;
    }

    .message {
        max-width: 85%;
    }

    .empty-state {
        display: none;
    }

    .chat-container {
        display: flex !important;
    }
}

@media (max-width: 480px) {
    :root {
        --header-height: 56px;
    }

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

    .logo {
        font-size: 1.125rem;
    }

    .search-box {
        padding: 8px 12px;
    }

    .conversation-item {
        padding: 10px;
    }

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

    .chat-header {
        padding: 0 12px;
    }

    .messages-area {
        padding: 12px;
    }

    .message-input-container {
        padding: 8px 12px 12px;
    }
}
