/* ===== CSS Variables - WhatsApp Dark Theme ===== */
:root {
    --bg-app: #111B21;
    --bg-sidebar: #111B21;
    --bg-chat: #0B141A;
    --bg-header: #202C33;
    --bg-input: #2A3942;
    --bg-bubble-user: #005C4B;
    --bg-bubble-other: #202C33;
    --bg-hover: #202C33;
    --bg-active: #2A3942;
    --bg-search: #202C33;
    --bg-modal: #233138;
    --text-primary: #E9EDEF;
    --text-secondary: #8696A0;
    --text-bubble: #E9EDEF;
    --text-timestamp: #8696A0;
    --accent: #00A884;
    --accent-hover: #06CF9C;
    --border: #2A3942;
    --border-light: #374045;
    --danger: #EA4335;
    --online: #00A884;
    --unread-bg: #00A884;
    --unread-text: #111B21;
    --typing-dot: #8696A0;
    --shadow: rgba(0,0,0,0.3);
    --font: 'Segoe UI', system-ui, -apple-system, 'Helvetica Neue', sans-serif;
    --chat-pattern: url("data:image/svg+xml,%3Csvg width='300' height='300' xmlns='http://www.w3.org/2000/svg'%3E%3Cdefs%3E%3Cpattern id='p' width='60' height='60' patternUnits='userSpaceOnUse'%3E%3Cpath d='M30 5 L35 15 L30 12 L25 15Z' fill='%23ffffff' opacity='0.02'/%3E%3Ccircle cx='50' cy='40' r='3' fill='%23ffffff' opacity='0.015'/%3E%3Cpath d='M10 45 L15 50 L10 55 L5 50Z' fill='%23ffffff' opacity='0.02'/%3E%3C/pattern%3E%3C/defs%3E%3Crect width='300' height='300' fill='url(%23p)'/%3E%3C/svg%3E");
}

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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: var(--font);
    background: var(--bg-app);
    color: var(--text-primary);
    font-size: 14.2px;
    line-height: 1.4;
}

a {
    color: var(--accent);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* ===== App Layout ===== */
#app {
    display: flex;
    height: 100vh;
    max-width: 1600px;
    margin: 0 auto;
    position: relative;
    overflow: hidden;
}

/* ===== Sidebar ===== */
#sidebar {
    width: 30%;
    min-width: 300px;
    max-width: 420px;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border);
    background: var(--bg-sidebar);
}

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--bg-header);
    height: 60px;
}

.sidebar-header-left {
    display: flex;
    align-items: center;
}

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

/* ===== Avatars ===== */
.avatar-placeholder {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    font-size: 14px;
    color: white;
    background: var(--accent);
    flex-shrink: 0;
}

.avatar-placeholder.user {
    background: #6B7B8D;
}

.avatar-img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.contact-avatar {
    position: relative;
}

.contact-avatar .online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 10px;
    height: 10px;
    background: var(--online);
    border-radius: 50%;
    border: 2px solid var(--bg-sidebar);
}

/* ===== Search ===== */
.search-container {
    padding: 8px 12px;
    background: var(--bg-sidebar);
}

.search-box {
    display: flex;
    align-items: center;
    background: var(--bg-search);
    border-radius: 8px;
    padding: 6px 12px;
    gap: 12px;
}

.search-box input {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 14px;
    font-family: var(--font);
    outline: none;
}

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

/* ===== Contact List ===== */
.contact-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

.contact-list::-webkit-scrollbar {
    width: 6px;
}

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

.contact-item {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    cursor: pointer;
    transition: background 0.15s;
    gap: 14px;
    border-bottom: 1px solid var(--border);
}

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

.contact-item.active {
    background: var(--bg-active);
}

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

.contact-name-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 8px;
}

.contact-name {
    font-size: 16px;
    font-weight: 400;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.contact-anime {
    font-size: 11px;
    color: var(--text-secondary);
    margin-top: 1px;
}

.contact-time {
    font-size: 12px;
    color: var(--text-secondary);
    white-space: nowrap;
}

.contact-time.unread {
    color: var(--accent);
}

.contact-preview-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
    margin-top: 2px;
}

.contact-preview {
    font-size: 13px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.unread-badge {
    background: var(--unread-bg);
    color: var(--unread-text);
    font-size: 11px;
    font-weight: 600;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
    flex-shrink: 0;
}

/* ===== Icon Buttons ===== */
.icon-btn {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s, color 0.15s;
}

.icon-btn:hover {
    background: rgba(255,255,255,0.1);
    color: var(--text-primary);
}

/* ===== Chat Area ===== */
#chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    background: var(--bg-chat);
    min-height: 0;
    overflow: hidden;
}

/* ===== Empty State ===== */
.empty-state {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--bg-chat);
}

.empty-state-content {
    text-align: center;
    padding: 40px;
}

.empty-state-icon {
    font-size: 80px;
    margin-bottom: 20px;
}

.empty-state-content h2 {
    font-size: 28px;
    font-weight: 300;
    color: var(--text-primary);
    margin-bottom: 12px;
}

.empty-state-content p {
    color: var(--text-secondary);
    font-size: 14px;
}

/* ===== Chat View ===== */
.chat-view {
    flex: 1;
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

.chat-view.hidden {
    display: none;
}

/* ===== Chat Header ===== */
.chat-header {
    display: flex;
    align-items: center;
    padding: 10px 16px;
    background: var(--bg-header);
    height: 60px;
    gap: 12px;
    z-index: 10;
}

.back-btn {
    display: none;
}

.chat-contact-avatar .avatar-img,
.chat-contact-avatar .avatar-placeholder {
    width: 40px;
    height: 40px;
}

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

.chat-contact-info h3 {
    font-size: 16px;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-status {
    font-size: 12.5px;
    color: var(--text-secondary);
}

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

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

/* ===== Messages ===== */
.messages {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 20px 60px;
    background: var(--bg-chat);
    background-image: var(--chat-pattern);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.messages::-webkit-scrollbar {
    width: 6px;
}

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

.messages-container {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

/* ===== Message Bubbles ===== */
.message {
    display: flex;
    margin-bottom: 2px;
    max-width: 65%;
    animation: fadeIn 0.2s ease;
}

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

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

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

.message-bubble {
    padding: 6px 7px 8px 9px;
    border-radius: 7.5px;
    position: relative;
    min-width: 80px;
    word-wrap: break-word;
    overflow-wrap: break-word;
}

.message.sent .message-bubble {
    background: var(--bg-bubble-user);
    border-top-right-radius: 0;
}

.message.received .message-bubble {
    background: var(--bg-bubble-other);
    border-top-left-radius: 0;
}

/* Bubble tails */
.message.sent .message-bubble::before {
    content: '';
    position: absolute;
    top: 0;
    right: -8px;
    width: 0;
    height: 0;
    border-left: 8px solid var(--bg-bubble-user);
    border-bottom: 8px solid transparent;
}

.message.received .message-bubble::before {
    content: '';
    position: absolute;
    top: 0;
    left: -8px;
    width: 0;
    height: 0;
    border-right: 8px solid var(--bg-bubble-other);
    border-bottom: 8px solid transparent;
}

/* First message in group gets the tail */
.message.sent + .message.sent .message-bubble::before,
.message.received + .message.received .message-bubble::before {
    display: none;
}

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

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

.message-text {
    color: var(--text-bubble);
    font-size: 14.2px;
    line-height: 19px;
    white-space: pre-wrap;
}

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

.message-time {
    font-size: 11px;
    color: rgba(255,255,255,0.6);
}

/* Read receipts */
.message-check {
    display: flex;
    align-items: center;
}

.message-check svg {
    width: 16px;
    height: 11px;
}

.message-check.read svg path {
    fill: #53BDEB;
}

.message-check.delivered svg path {
    fill: rgba(255,255,255,0.5);
}

/* Speaker icon on received messages */
.message-speaker {
    position: absolute;
    top: 4px;
    right: 4px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 2px;
    opacity: 0;
    transition: opacity 0.15s;
}

.message:hover .message-speaker {
    opacity: 0.7;
}

.message-speaker:hover {
    opacity: 1 !important;
}

/* Date separator */
.date-separator {
    text-align: center;
    margin: 12px 0;
}

.date-separator span {
    background: #1D2A31;
    color: var(--text-secondary);
    font-size: 12.5px;
    padding: 5px 12px;
    border-radius: 7.5px;
    box-shadow: 0 1px 0.5px var(--shadow);
}

/* ===== Typing Indicator ===== */
.typing-indicator {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 4px 0;
    align-self: flex-start;
}

.typing-indicator.hidden {
    display: none;
}

.typing-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    display: none;
}

.typing-bubble {
    background: var(--bg-bubble-other);
    border-radius: 7.5px;
    border-top-left-radius: 0;
    padding: 10px 16px;
    position: relative;
}

.typing-bubble::before {
    content: '';
    position: absolute;
    top: 0;
    left: -8px;
    width: 0;
    height: 0;
    border-right: 8px solid var(--bg-bubble-other);
    border-bottom: 8px solid transparent;
}

.typing-dots {
    display: flex;
    gap: 4px;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--typing-dot);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-dots span:nth-child(1) { animation-delay: 0s; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
    30% { transform: translateY(-6px); opacity: 1; }
}

/* ===== Input Area ===== */
.input-area {
    display: flex;
    align-items: flex-end;
    padding: 8px 10px;
    background: var(--bg-header);
    gap: 6px;
}

.input-wrapper {
    flex: 1;
    background: var(--bg-input);
    border-radius: 8px;
    padding: 9px 12px;
    display: flex;
    align-items: center;
}

.input-wrapper textarea {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 15px;
    font-family: var(--font);
    line-height: 20px;
    resize: none;
    outline: none;
    max-height: 100px;
    overflow-y: auto;
}

.input-wrapper textarea::placeholder {
    color: var(--text-secondary);
}

.input-wrapper textarea::-webkit-scrollbar {
    width: 4px;
}

.input-wrapper textarea::-webkit-scrollbar-thumb {
    background: var(--border-light);
    border-radius: 2px;
}

.send-btn {
    color: var(--text-secondary) !important;
}

.send-btn:hover {
    color: var(--accent) !important;
}

.send-btn.hidden, .mic-btn.hidden {
    display: none;
}

/* ===== Utility ===== */
.hidden {
    display: none !important;
}

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

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

    .back-btn {
        display: flex;
    }

    .messages {
        padding: 12px 16px;
    }

    .message {
        max-width: 85%;
    }

    .emoji-picker {
        left: 8px;
        right: 8px;
        width: auto;
        bottom: 65px;
    }
}

@media (max-width: 480px) {
    .messages {
        padding: 8px 12px;
    }

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

/* ===== Loading Screen ===== */
.app-loading {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--bg-app);
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
}

.app-loading.hidden {
    display: none;
}

.loading-content {
    text-align: center;
}

.loading-icon {
    font-size: 64px;
    margin-bottom: 20px;
}

.loading-content p {
    color: var(--text-secondary);
    font-size: 15px;
    margin-top: 16px;
}

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

.loading-spinner.small {
    width: 20px;
    height: 20px;
    border-width: 2px;
    display: inline-block;
    vertical-align: middle;
    margin-right: 8px;
}

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