/* Modern Mobile-First Messenger Design */

:root {
    /* Light Theme Colors */
    --primary: #075E54;
    --primary-dark: #054B43;
    --primary-light: #128C7E;
    --accent: #25D366;
    --accent-hover: #20BD5A;

    /* Background Colors */
    --bg-primary: #FFFFFF;
    --bg-secondary: #F0F2F5;
    --bg-tertiary: #E5DDD5;
    --bg-chat: #E5DDD5;

    /* Text Colors */
    --text-primary: #111B21;
    --text-secondary: #667781;
    --text-tertiary: #8696A0;
    --text-white: #FFFFFF;

    /* Message Colors */
    --message-sent: #D9FDD3;
    --message-received: #FFFFFF;
    --message-hover: #F5F6F6;

    /* UI Elements */
    --border-color: #E9EDEF;
    --shadow-sm: 0 1px 3px rgba(11, 20, 26, 0.08);
    --shadow-md: 0 2px 8px rgba(11, 20, 26, 0.12);
    --shadow-lg: 0 8px 24px rgba(11, 20, 26, 0.15);

    /* Status Colors */
    --online: #25D366;
    --offline: #8696A0;
    --typing: #667781;

    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;

    /* Border Radius */
    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 16px;
    --radius-xl: 24px;
    --radius-full: 9999px;

    /* Safe Areas */
    --safe-area-top: env(safe-area-inset-top);
    --safe-area-bottom: env(safe-area-inset-bottom);
}

/* Dark Theme */
body.dark-theme {
    --primary: #00A884;
    --primary-dark: #008F72;
    --primary-light: #00C89D;

    --bg-primary: #111B21;
    --bg-secondary: #202C33;
    --bg-tertiary: #0B141A;
    --bg-chat: #0B141A;

    --text-primary: #E9EDEF;
    --text-secondary: #8696A0;
    --text-tertiary: #667781;

    --message-sent: #005C4B;
    --message-received: #202C33;
    --message-hover: #2A3942;

    --border-color: #2A3942;
}

/* Reset & Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.5;
    overflow: hidden;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* App Container */
.app-container {
    position: relative;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
    background: var(--bg-primary);
}

/* Views */
.view {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    transform: translateX(-100%);
    opacity: 0;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1), opacity 0.3s ease;
    pointer-events: none;
}

.view.active {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
}

/* App Header */
.app-header {
    background: var(--primary);
    color: var(--text-white);
    padding: var(--spacing-md);
    padding-top: calc(var(--spacing-md) + var(--safe-area-top));
    box-shadow: var(--shadow-md);
    z-index: 100;
}

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

.app-title {
    font-size: 1.375rem;
    font-weight: 600;
    letter-spacing: -0.02em;
}

.header-actions {
    display: flex;
    gap: var(--spacing-xs);
    align-items: center;
}

/* Icon Buttons */
.icon-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: inherit;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s ease;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}

.icon-btn:active {
    background: rgba(255, 255, 255, 0.15);
}

.icon-btn svg {
    width: 20px;
    height: 20px;
}

/* Search Container */
.search-container {
    padding: var(--spacing-md);
    padding-top: var(--spacing-sm);
    background: var(--primary);
    display: none;
}

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

.search-input-wrapper {
    position: relative;
    display: flex;
    align-items: center;
    background: var(--bg-primary);
    border-radius: var(--radius-md);
    padding: 0 var(--spacing-md);
    gap: var(--spacing-sm);
}

.search-input-wrapper svg {
    width: 18px;
    height: 18px;
    color: var(--text-secondary);
    flex-shrink: 0;
}

.search-input-wrapper input {
    flex: 1;
    border: none;
    background: transparent;
    padding: 12px 0;
    font-size: 0.9375rem;
    color: var(--text-primary);
    outline: none;
}

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

.clear-search {
    width: 24px;
    height: 24px;
    border: none;
    background: transparent;
    color: var(--text-secondary);
    display: none;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border-radius: var(--radius-full);
    padding: 0;
}

.clear-search svg {
    width: 16px;
    height: 16px;
}

.search-input-wrapper input:not(:placeholder-shown) ~ .clear-search {
    display: flex;
}

/* Chats List */
.chats-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    background: var(--bg-primary);
    -webkit-overflow-scrolling: touch;
}

.chat-item {
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    padding: var(--spacing-md);
    background: var(--bg-primary);
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    transition: background 0.15s ease;
    position: relative;
}

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

.chat-item .avatar-wrapper {
    position: relative;
    flex-shrink: 0;
}

.avatar {
    width: 52px;
    height: 52px;
    border-radius: var(--radius-full);
    background: var(--primary-light);
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--text-white);
    font-weight: 600;
    font-size: 1.25rem;
    overflow: hidden;
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.online-indicator {
    position: absolute;
    bottom: 2px;
    right: 2px;
    width: 14px;
    height: 14px;
    background: var(--online);
    border: 2px solid var(--bg-primary);
    border-radius: var(--radius-full);
    display: none;
}

.online-indicator.active {
    display: block;
}

.chat-item-content {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs);
}

.chat-item-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: var(--spacing-sm);
}

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

.chat-item-time {
    font-size: 0.75rem;
    color: var(--text-tertiary);
    flex-shrink: 0;
}

.chat-item-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--spacing-sm);
}

.chat-item-preview {
    flex: 1;
    font-size: 0.875rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: flex;
    align-items: center;
    gap: var(--spacing-xs);
}

.chat-item-preview svg {
    width: 14px;
    height: 14px;
    flex-shrink: 0;
}

.unread-badge {
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    background: var(--accent);
    color: var(--text-white);
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.6875rem;
    font-weight: 600;
    flex-shrink: 0;
}

/* Floating Action Button */
.fab {
    position: fixed;
    bottom: calc(80px + var(--safe-area-bottom));
    left: var(--spacing-md);
    width: 56px;
    height: 56px;
    background: var(--accent);
    color: var(--text-white);
    border: none;
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-lg);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    z-index: 50;
}

.fab:active {
    transform: scale(0.95);
    box-shadow: var(--shadow-md);
}

.fab svg {
    width: 24px;
    height: 24px;
}

/* Bottom Navigation */
.bottom-nav {
    display: flex;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    padding-bottom: var(--safe-area-bottom);
    box-shadow: 0 -2px 8px rgba(11, 20, 26, 0.08);
}

.nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--spacing-xs);
    padding: 10px;
    border: none;
    background: transparent;
    color: var(--text-tertiary);
    cursor: pointer;
    transition: color 0.2s ease;
    min-height: 60px;
}

.nav-item svg {
    width: 24px;
    height: 24px;
}

.nav-item span {
    font-size: 0.6875rem;
    font-weight: 500;
}

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

/* Chat View */
.chat-view {
    background: var(--bg-chat);
}

.chat-header {
    background: var(--bg-primary);
    padding: var(--spacing-sm) var(--spacing-md);
    padding-top: calc(var(--spacing-sm) + var(--safe-area-top));
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    box-shadow: var(--shadow-sm);
    z-index: 100;
}

.back-btn {
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: var(--text-primary);
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    margin-left: calc(-1 * var(--spacing-sm));
}

.back-btn:active {
    background: var(--bg-secondary);
}

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

.chat-header-info {
    flex: 1;
    display: flex;
    align-items: center;
    gap: var(--spacing-md);
    min-width: 0;
}

.chat-header .avatar-wrapper {
    position: relative;
}

.chat-header .avatar {
    width: 40px;
    height: 40px;
    font-size: 1.125rem;
}

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

.chat-name {
    font-size: 1rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.3;
}

.chat-status {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    line-height: 1.3;
}

.chat-header-actions {
    display: flex;
    gap: var(--spacing-xs);
}

.chat-header .icon-btn:active {
    background: var(--bg-secondary);
}

/* Messages Container */
.messages-container {
    flex: 1;
    position: relative;
    overflow: hidden;
}

.messages-area {
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    padding: var(--spacing-md);
    -webkit-overflow-scrolling: touch;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    background-image:
        repeating-linear-gradient(
            45deg,
            transparent,
            transparent 35px,
            rgba(255, 255, 255, 0.03) 35px,
            rgba(255, 255, 255, 0.03) 70px
        );
}

/* Messages */
.message {
    display: flex;
    margin-bottom: var(--spacing-xs);
    animation: messageSlideIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

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

.message.sent {
    justify-content: flex-end;
}

.message.received {
    justify-content: flex-start;
}

.message-bubble {
    max-width: 80%;
    padding: 7px 10px;
    border-radius: var(--radius-sm);
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 1px 2px rgba(11, 20, 26, 0.13);
}

.message.sent .message-bubble {
    background: var(--message-sent);
    border-top-right-radius: 0;
    margin-right: 2px;
}

.message.received .message-bubble {
    background: var(--message-received);
    border-top-left-radius: 0;
    margin-left: 2px;
}

.message-text {
    font-size: 0.9375rem;
    line-height: 1.5;
    color: var(--text-primary);
    margin-bottom: 4px;
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    font-size: 0.6875rem;
    color: var(--text-tertiary);
    margin-top: 2px;
}

.message-meta svg {
    width: 14px;
    height: 14px;
    color: #53BDEB;
}

/* Typing Indicator */
.typing-indicator {
    display: none;
    align-items: center;
    gap: 4px;
    padding: 12px 16px;
    background: var(--message-received);
    border-radius: var(--radius-sm);
    width: fit-content;
    margin: var(--spacing-md);
    box-shadow: 0 1px 2px rgba(11, 20, 26, 0.13);
}

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

.typing-dot {
    width: 8px;
    height: 8px;
    background: var(--text-tertiary);
    border-radius: var(--radius-full);
    animation: typingBounce 1.4s infinite;
}

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

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

@keyframes typingBounce {
    0%, 60%, 100% {
        transform: translateY(0);
    }
    30% {
        transform: translateY(-8px);
    }
}

/* Message Input */
.message-input-container {
    background: var(--bg-primary);
    padding: var(--spacing-sm) var(--spacing-md);
    padding-bottom: calc(var(--spacing-sm) + var(--safe-area-bottom));
    border-top: 1px solid var(--border-color);
}

.message-input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: var(--spacing-sm);
}

.emoji-btn,
.voice-btn {
    color: var(--text-secondary);
}

.input-wrapper {
    flex: 1;
    display: flex;
    align-items: flex-end;
    gap: var(--spacing-sm);
    background: var(--bg-secondary);
    border-radius: var(--radius-xl);
    padding: var(--spacing-sm) var(--spacing-md);
    min-height: 44px;
}

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

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

.attach-btn {
    color: var(--text-secondary);
}

.send-btn {
    width: 44px;
    height: 44px;
    border: none;
    background: var(--accent);
    color: var(--text-white);
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.2s ease, background 0.2s ease;
    flex-shrink: 0;
}

.send-btn:active {
    transform: scale(0.95);
    background: var(--accent-hover);
}

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

/* Loading Spinner */
.loading-spinner {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.3);
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 9999;
}

.loading-spinner.active {
    display: flex;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255, 255, 255, 0.3);
    border-top-color: var(--accent);
    border-radius: var(--radius-full);
    animation: spin 0.8s linear infinite;
}

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

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

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

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

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

/* Tablet & Desktop */
@media (min-width: 768px) {
    .app-container {
        max-width: 1200px;
        margin: 0 auto;
        display: grid;
        grid-template-columns: 380px 1fr;
    }

    .chats-view {
        position: relative;
        transform: none;
        opacity: 1;
        pointer-events: auto;
        border-left: 1px solid var(--border-color);
    }

    .chat-view {
        position: relative;
        transform: none;
        opacity: 1;
        pointer-events: auto;
    }

    .bottom-nav {
        display: none;
    }

    .fab {
        bottom: var(--spacing-lg);
    }

    .back-btn {
        display: none;
    }
}

/* Accessibility */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* High Contrast */
@media (prefers-contrast: high) {
    .icon-btn:active,
    .chat-item:active {
        background: var(--primary-light);
        color: var(--text-white);
    }
}
