:root {
    /* --- Modern Dark Palette --- */
    --bg-app: #09090b;
    /* Pure Black/Zinc base */
    --bg-sidebar: #121212;
    /* Slightly lighter sidebar */
    --bg-header: #181c20;
    /* Distinct header/input bg */
    --bg-card: #27272a;

    --accent: #22c55e;
    /* Snappy Green */
    --accent-dim: #15803d;

    --text-primary: #f4f4f5;
    --text-secondary: #a1a1aa;

    --border: #27272a;

    --msg-out: #166534;
    /* Green message bubble */
    --msg-in: #27272a;
    /* Grey message bubble */

    --header-height: 64px;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: 'Inter', sans-serif;
    font-weight: 450;
    background-color: var(--bg-app);
    color: var(--text-primary);
    width: 100vw;
    height: 100vh;
    overflow: hidden;
}

/* --- App Container --- */
#app {
    width: 100%;
    height: 100%;
    position: relative;
    display: flex;
}

/* --- Utils --- */
.hidden {
    display: none !important;
}

button {
    font-family: inherit;
    cursor: pointer;
    border: none;
    background: none;
}

h1,
h2,
h3,
p {
    margin: 0;
}

/* =========================================
   1. The Login Page (The Design You Liked)
   ========================================= */
#auth-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle at center, #1a1a1a 0%, #000 100%);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.auth-wrapper {
    width: 100%;
    max-width: 420px;
    padding: 20px;
    text-align: center;
    animation: fadeIn 0.4s ease-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.auth-header h1 {
    font-size: 3rem;
    font-weight: 800;
    letter-spacing: -2px;
    margin-bottom: 5px;
    background: linear-gradient(to right, #fff, #999);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.tagline {
    font-size: 1.1rem;
    color: var(--text-secondary);
    margin-bottom: 30px;
    line-height: 1.4;
}

.highlight {
    color: var(--text-primary);
    font-weight: 600;
    text-decoration: underline decoration-var(--accent) 3px;
}

.auth-card {
    background: rgba(24, 24, 27, 0.6);
    backdrop-filter: blur(12px);
    border: 1px solid var(--border);
    padding: 40px 30px;
    border-radius: 24px;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.5);
    text-align: left;
}

.auth-card h2 {
    margin-bottom: 20px;
    font-size: 1.5rem;
    text-align: center;
    color: var(--text-primary);
}

.input-group {
    margin-bottom: 20px;
}

.input-group label {
    display: block;
    font-size: 0.85rem;
    color: var(--text-secondary);
    margin-bottom: 8px;
    font-weight: 500;
}

.input-group input {
    width: 100%;
    background: #000;
    border: 1px solid var(--border);
    padding: 14px;
    border-radius: 12px;
    color: white;
    font-size: 1rem;
    transition: 0.2s;
}

.input-group input:focus {
    border-color: var(--accent);
    outline: none;
    box-shadow: 0 0 0 2px rgba(34, 197, 94, 0.2);
}

.glow-btn {
    width: 100%;
    background: var(--accent);
    color: black;
    font-weight: 700;
    padding: 16px;
    border-radius: 12px;
    font-size: 1rem;
    transition: 0.2s;
}

.glow-btn:hover {
    box-shadow: 0 0 20px rgba(34, 197, 94, 0.4);
    transform: translateY(-1px);
}

.divider {
    text-align: center;
    color: var(--text-secondary);
    margin: 20px 0;
    font-size: 0.9rem;
}

.toggle-link {
    text-align: center;
    color: var(--accent);
    cursor: pointer;
    font-weight: 600;
}

.auth-footer {
    margin-top: 40px;
    color: var(--text-secondary);
    font-size: 0.9rem;
    opacity: 0.7;
}

.auth-footer b {
    color: var(--text-primary);
}

#auth-error {
    background: rgba(255, 0, 0, 0.1);
    color: #ff6b6b;
    padding: 10px;
    border-radius: 8px;
    margin-bottom: 15px;
    text-align: center;
}

/* =========================================
   2. Main Layout (Sidebar + Chat) 
   ========================================= */
#main-layout {
    display: flex;
    width: 100%;
    height: 100%;
    background: var(--bg-app);
}

/* --- Sidebar --- */
#sidebar {
    width: 320px;
    min-width: 320px;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
}

.pane-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-bottom: 1px solid var(--border);
    background: var(--bg-header);
    flex-shrink: 0;
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 12px;
    font-weight: 600;
}

.avatar-circle {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: linear-gradient(135deg, #333, #555);
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-weight: bold;
    font-size: 1rem;
}

.actions {
    display: flex;
    align-items: center;
    gap: 8px;
}

.action-btn {
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 0.85rem;
    font-weight: 500;
    color: var(--text-secondary);
    background: var(--bg-card);
    border: none;
    transition: all 0.2s;
    white-space: nowrap;
}

.action-btn:hover {
    background: #333;
    color: var(--text-primary);
}

.action-btn.primary-text {
    color: var(--accent);
    background: var(--bg-card);
}

.action-btn.primary-text:hover {
    background: #333;
}

.search-bar {
    padding: 12px;
}

.search-bar input {
    width: 100%;
    background: var(--bg-header);
    border: none;
    padding: 10px 14px;
    border-radius: 8px;
    color: var(--text-primary);
}

#conversations-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.chat-item {
    padding: 12px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 12px;
    cursor: pointer;
    border-bottom: 1px solid transparent;
}

.chat-item:hover {
    background: var(--bg-card);
}

.chat-item.active {
    background: var(--bg-card);
}

/* Online indicator dot */
.online-dot {
    width: 10px;
    height: 10px;
    background: #22c55e;
    border-radius: 50%;
    border: 2px solid #18181b;
    flex-shrink: 0;
}

/* Delete button - shows on hover */
.delete-btn {
    display: none;
    width: 28px;
    height: 28px;
    background: rgba(220, 38, 38, 0.15);
    color: #f87171;
    border: none;
    border-radius: 6px;
    font-size: 1.2rem;
    cursor: pointer;
    flex-shrink: 0;
    margin-left: auto;
}

.delete-btn:hover {
    background: rgba(220, 38, 38, 0.3);
}

.chat-item:hover .delete-btn {
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-item-info {
    flex: 1;
    min-width: 0;
}

.chat-item-top {
    display: flex;
    justify-content: space-between;
    margin-bottom: 4px;
}

.chat-item-name {
    font-weight: 600;
    font-size: 0.95rem;
}

.chat-item-time {
    font-size: 0.75rem;
    color: var(--text-secondary);
}

.chat-item-bottom {
    display: flex;
    justify-content: space-between;
}

.chat-item-preview {
    color: var(--text-secondary);
    font-size: 0.85rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.unread-badge {
    background: var(--accent);
    color: black;
    font-size: 0.7rem;
    padding: 2px 6px;
    border-radius: 10px;
    font-weight: bold;
}

/* --- Chat Area (MAJOR OVERHAUL) --- */
#chat-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    position: relative;
    /* WhatsApp-style dark wallpaper background */
    background-color: #0b141a;
    background-image:
        linear-gradient(rgba(11, 20, 26, 0.92), rgba(11, 20, 26, 0.92)),
        url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Cpath d='M0 0h40v40H0V0zm40 40h40v40H40V40zm0-40h2l-2 2V0zm0 4l4-4h2l-6 6V4zm0 4l8-8h2L40 10V8zm0 4L52 0h2L40 14v-2zm0 4L56 0h2L40 18v-2zm0 4L60 0h2L40 22v-2zm0 4L64 0h2L40 26v-2zm0 4L68 0h2L40 30v-2zm0 4L72 0h2L40 34v-2zm0 4L76 0h2L40 38v-2zm0 4L80 0v2L42 40h-2zm4 0L80 4v2L46 40h-2zm4 0L80 8v2L50 40h-2zm4 0l28-28v2L54 40h-2zm4 0l24-24v2L58 40h-2zm4 0l20-20v2L62 40h-2zm4 0l16-16v2L66 40h-2zm4 0l12-12v2L70 40h-2zm4 0l8-8v2l-6 6h-2zm4 0l4-4v2l-2 2h-2z'/%3E%3C/g%3E%3C/svg%3E");
    overflow: hidden;
}

#no-chat-selected {
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: var(--text-secondary);
    text-align: center;
    padding: 40px;
}

#no-chat-selected h2 {
    color: var(--text-primary);
    margin-bottom: 10px;
    font-size: 1.8rem;
}

#no-chat-selected p {
    opacity: 0.7;
}

#active-chat-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    width: 100%;
}

.chat-header {
    background: var(--bg-header);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

.chat-info {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-text h3 {
    font-size: 1rem;
    margin-bottom: 2px;
    color: var(--text-primary);
}

.status-text {
    font-size: 0.8rem;
    color: var(--text-secondary);
}

.status-text.online {
    color: var(--accent);
}

/* Typing Indicator */
.typing-text {
    font-size: 0.8rem;
    color: var(--accent);
    font-style: italic;
    animation: typingPulse 1.5s ease-in-out infinite;
}

@keyframes typingPulse {

    0%,
    100% {
        opacity: 0.6;
    }

    50% {
        opacity: 1;
    }
}

/* Messages Container */
#messages-container {
    flex: 1;
    overflow-y: auto;
    padding: 16px 5%;
    display: flex;
    flex-direction: column;
    min-height: 0;
    /* Smooth scroll */
    scroll-behavior: smooth;
}

#messages-list {
    margin-top: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
    padding-bottom: 8px;
}

/* Date Separator */
.date-separator {
    align-self: center;
    background: #182229;
    color: #8696a0;
    font-size: 12.5px;
    padding: 5px 12px;
    border-radius: 7.5px;
    margin: 10px 0;
    box-shadow: 0 1px 0.5px rgba(11, 20, 26, .13);
}

/* Message Bubbles - WhatsApp Style (Exact Match) */
.message {
    max-width: 65%;
    padding: 6px 7px 8px 9px;
    border-radius: 7.5px;
    font-size: 14.2px;
    line-height: 19px;
    position: relative;
    word-wrap: break-word;
    box-shadow: 0 1px 0.5px rgba(11, 20, 26, .13);
}

.message.sent {
    align-self: flex-end;
    background: #005c4b;
    color: #e9edef;
    border-top-right-radius: 0;
}

.message.received {
    align-self: flex-start;
    background: #1f2c34;
    color: #e9edef;
    border-top-left-radius: 0;
}

.message.pending {
    opacity: 1;
}

/* Message text wrapper */
.msg-text {
    display: inline;
    word-break: break-word;
}

/* Timestamp - inline floating right, WhatsApp exact style */
.msg-meta {
    display: inline-flex;
    align-items: center;
    float: right;
    margin: -4px 0 -5px 12px;
    position: relative;
    top: 7px;
    font-size: 11px;
    color: hsla(0, 0%, 100%, .6);
    gap: 3px;
    white-space: nowrap;
}

.message.received .msg-meta {
    color: #8696a0;
}

.msg-meta .ticks {
    font-size: 0.9rem;
    margin-left: 3px;
    color: rgba(255, 255, 255, 0.5);
}

.msg-meta .ticks.read {
    color: #53bdeb;
}

/* Input Area - Clean Modern Style */
.chat-input-area {
    padding: 12px 16px;
    background: var(--bg-header);
    border-top: 1px solid rgba(255, 255, 255, 0.08);
    flex-shrink: 0;
    padding-bottom: max(12px, env(safe-area-inset-bottom));
}

#message-form {
    display: flex;
    gap: 10px;
    width: 100%;
    align-items: center;
}

#message-input {
    flex: 1;
    background: #2a3942;
    border: none;
    border-radius: 8px;
    color: var(--text-primary);
    font-size: 1rem;
    padding: 12px 16px;
    transition: background 0.2s;
}

#message-input:focus {
    outline: none;
    background: #323f48;
}

#message-input::placeholder {
    color: #8696a0;
}

#send-btn {
    width: 44px;
    height: 44px;
    background: var(--accent);
    color: #111b21;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.15s, background 0.2s;
    flex-shrink: 0;
}

#send-btn:hover {
    transform: scale(1.08);
    background: #25d366;
}

#send-btn:active {
    transform: scale(0.95);
}

/* Emoji Button */
.icon-btn {
    width: 40px;
    height: 40px;
    background: transparent;
    border: none;
    font-size: 1.4rem;
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* Emoji Picker Panel */
.emoji-picker {
    position: absolute;
    bottom: 100%;
    left: 10px;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px;
    margin-bottom: 8px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
    z-index: 100;
}

.emoji-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
}

.emoji-item {
    font-size: 1.5rem;
    padding: 6px;
    cursor: pointer;
    border-radius: 6px;
    text-align: center;
    transition: background 0.15s;
}

.emoji-item:hover {
    background: rgba(255, 255, 255, 0.1);
}

.chat-input-area {
    position: relative;
}

#send-btn svg {
    width: 20px;
    height: 20px;
}

/* --- Modals --- */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 3000;
}

.modal-content {
    background: var(--bg-card);
    padding: 24px;
    width: 90%;
    max-width: 360px;
    border-radius: 16px;
    border: 1px solid var(--border);
}

.modal-content input {
    width: 100%;
    padding: 12px;
    background: #000;
    border: 1px solid var(--border);
    color: white;
    border-radius: 8px;
    margin: 16px 0;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 12px;
}

.modal-actions button {
    padding: 10px 20px;
    border-radius: 8px;
    font-weight: 600;
}

.primary {
    background: var(--accent);
    color: black;
}

.secondary {
    color: var(--text-secondary);
}

.danger {
    background: #dc2626;
    color: white;
}

.danger:hover {
    background: #ef4444;
}

.modal-desc {
    color: var(--text-secondary);
    font-size: 0.9rem;
    margin: 10px 0 20px;
    line-height: 1.4;
}

.error-text {
    color: #f87171;
    font-size: 0.85rem;
    margin-bottom: 10px;
}

/* Chat Item Delete Button */
.chat-item-actions {
    display: none;
    margin-left: auto;
}

.chat-item:hover .chat-item-actions,
.chat-item.show-actions .chat-item-actions {
    display: flex;
}

.delete-chat-btn {
    padding: 6px 10px;
    background: rgba(220, 38, 38, 0.2);
    color: #f87171;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 500;
}

.delete-chat-btn:hover {
    background: rgba(220, 38, 38, 0.4);
}

/* --- Mobile Responsive --- */
.mobile-only {
    display: none;
}

@media (max-width: 768px) {
    #sidebar {
        width: 100%;
        border-right: none;
    }

    #chat-area {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 100;
        transform: translateX(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        background: var(--bg-app);
    }

    /* When chat is active */
    body.show-chat #chat-area {
        transform: translateX(0);
    }

    body.show-chat #sidebar {
        display: none;
    }

    .mobile-only {
        display: block;
        font-size: 1.5rem;
        margin-right: 12px;
        color: var(--text-primary);
    }
}