:root {
    --app-bg: #e5ddd5; /* Basic fallback before image */
    /* Often WhatsApp uses a doodle background, we'll simulate a color close to it */
    --app-bg-color: #efeae2;
    --header-bg: #008069;
    --header-text: #ffffff;
    --incoming-bubble: #ffffff;
    --outgoing-bubble: #d9fdd3;
    --bubble-shadow: 0 1px 0.5px rgba(11,20,26,.13);
    --text-primary: #111b21;
    --text-secondary: #667781;
    --text-meta: #8696a0;
    --link-color: #039be5;
    --input-bg: #f0f2f5;
    --compose-bg: #ffffff;
}

body {
    background-color: #d1d7db; /* Desktop background surrounding the phone view */
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.app-container {
    width: 100%;
    height: 100%;
    background-color: var(--app-bg-color);
    background-image: url('https://user-images.githubusercontent.com/15075759/28719144-86dc0f70-73b1-11e7-911d-60d70fcded21.png'); /* WhatsApp Doodle Pattern */
    background-blend-mode: overlay;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

/* On larger screens, limit width to look like a phone or web app window */
@media (min-width: 900px) {
    .app-container {
        width: 100%;
        max-width: 1600px; /* Full width essentially, but let's keep it responsive */
        height: 100vh;
        box-shadow: 0 0 20px rgba(0,0,0,0.1);
    }
    body {
        padding: 0;
    }
}

/* Header */
.chat-header {
    background-color: var(--header-bg);
    padding: 10px 16px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    color: var(--header-text);
    z-index: 10;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.user-profile {
    display: flex;
    align-items: center;
    gap: 10px;
    cursor: pointer;
}

.back-button {
    margin-right: 5px;
    cursor: pointer;
}

.avatar img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    object-fit: cover;
}

.user-info {
    display: flex;
    flex-direction: column;
}

.username {
    font-size: 16px;
    font-weight: 500;
    margin: 0;
    line-height: normal;
}

.status {
    font-size: 13px;
    opacity: 0.8;
}

.header-actions {
    display: flex;
    gap: 15px;
}

.icon-btn {
    background: none;
    border: none;
    color: inherit;
    cursor: pointer;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Chat Area */
.chat-area {
    flex: 1;
    overflow-y: auto;
    padding: 20px 5% 10px 5%; /* Side padding dynamic */
    display: flex;
    flex-direction: column;
    gap: 2px; /* Close grouping, margin handling in bubbles */
}

/* Scrollbar Styling */
.chat-area::-webkit-scrollbar {
    width: 6px;
}
.chat-area::-webkit-scrollbar-thumb {
    background-color: rgba(0,0,0,0.2);
    border-radius: 3px;
}

/* Date Divider */
.date-divider {
    align-self: center;
    background-color: #ffffff;
    box-shadow: 0 1px 0.5px rgba(11,20,26,.13);
    border-radius: 8px;
    padding: 5px 12px;
    font-size: 12.5px;
    color: var(--text-secondary);
    margin: 12px 0;
    text-transform: uppercase;
    position: sticky;
    top: 10px;
    z-index: 5;
    opacity: 0.9;
}

/* Message Bubbles */
.message-row {
    display: flex;
    margin-bottom: 8px; /* Standard gap between messages */
    width: 100%;
}

.message-row.sent {
    justify-content: flex-end;
}

.message-row.received {
    justify-content: flex-start;
}

/* Grouping Logic: reduce margin if consecutive */
.message-row.group-top {
    margin-bottom: 2px;
}
.message-row.group-middle {
    margin-bottom: 2px;
}
.message-row.group-bottom {
    margin-bottom: 8px;
}

.message-bubble {
    max-width: 65%; /* Mobile standard */
    padding: 6px 7px 8px 9px;
    border-radius: 7.5px;
    position: relative;
    font-size: 14.2px;
    line-height: 19px;
    color: var(--text-primary);
    box-shadow: var(--bubble-shadow);
    display: flex;
    flex-direction: column;
}

@media (min-width: 600px) {
    .message-bubble {
        max-width: 50%; /* Desktop standard */
    }
}

.formatted-text{
    padding-right: 20px;
}

.message-row.received .message-bubble {
    background-color: var(--incoming-bubble);
    border-top-left-radius: 0;
}

.message-row.sent .message-bubble {
    background-color: var(--outgoing-bubble);
    border-top-right-radius: 0;
}

/* Tail simulation (optional, can be CSS borders) */
.message-row.received.tail .message-bubble::before {
    content: "";
    position: absolute;
    top: 0;
    left: -8px;
    width: 8px;
    height: 13px;
    background: linear-gradient(to top left, transparent 50%, var(--incoming-bubble) 50%);
    border-top-right-radius: 10px; /* Slight curve */
}

.message-row.sent.tail .message-bubble::after {
    content: "";
    position: absolute;
    top: 0;
    right: -8px;
    width: 8px;
    height: 13px;
    background: linear-gradient(to top right, transparent 50%, var(--outgoing-bubble) 50%);
    border-top-left-radius: 10px;
}

/* Message Content */
.message-content {
    word-wrap: break-word;
    white-space: pre-wrap;
}

.message-media {
    margin-bottom: 4px;
    border-radius: 6px;
    overflow: hidden;
    cursor: pointer;
    background: #cfd8dc;
    display: flex;
    justify-content: center;
    align-items: center;
    min-width: 200px;
    max-height: 400px;
}

.message-media img, .message-media video {
    width: 100%;
    height: auto;
    object-fit: cover;
    max-height: 400px;
}

/* Metadata (Time + Ticks) */
.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 4px;
    margin-top: -4px; /* Pull up closer to text bottom line */
    margin-left: 20px; /* Space from text */
    float: right; /* Inline flow */
    position: relative;
    top: 4px;
}

.message-time {
    font-size: 11px;
    color: var(--text-meta);
}

.read-ticks {
    color: #53bdeb; /* Blue ticks */
}
.read-ticks svg {
    width: 16px;
    height: 16px;
}

/* Input Area */
.chat-input-area {
    background-color: var(--input-bg);
    padding: 10px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
    z-index: 10;
}

.input-wrapper {
    flex: 1;
    background-color: var(--compose-bg);
    border-radius: 8px;
    padding: 9px 12px;
    display: flex;
    align-items: center;
}

.input-wrapper input {
    border: none;
    outline: none;
    width: 100%;
    font-size: 15px;
}

/* Animations */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Search UI */
.hidden {
    display: none !important;
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    transition: opacity 0.2s ease;
}

.header-content.search-view {
    gap: 10px;
}

.search-input-container {
    flex: 1;
    background-color: white;
    border-radius: 8px;
    height: 40px;
    display: flex;
    align-items: center;
    padding: 0 10px;
    position: relative;
    max-width: 600px; /* Limit width */
    overflow: hidden; /* Clip children to border radius */
}

.search-input-container input {
    border: none;
    outline: none;
    flex: 1; /* Take remaining space properly */
    width: auto; /* Reset width */
    min-width: 0; /* Allow shrinking below content size if needed */
    height: 100%;
    font-size: 15px;
    margin-left: 10px;
    background-color: transparent; /* Prevent white background form overlaying rounded corners */
}

.search-input-container #calendar-btn {
    color: #54656f;
    flex-shrink: 0; /* Prevent button from squashing */
}

/* Search Overlay */
.search-overlay {
    position: absolute;
    top: 60px; /* Below header */
    left: 0;
    width: 100%;
    height: calc(100% - 60px);
    background-color: #ffffff; /* Light background */
    z-index: 20;
    padding: 10px 0;
    overflow-y: auto;
    color: var(--text-primary); /* Dark text */
}

.search-results-list {
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 800px;
    margin: 0 auto;
}

.search-result-item {
    padding: 12px 20px;
    border-bottom: 1px solid #f0f2f5; /* Light separator */
    cursor: pointer;
    transition: background 0.1s;
}

.search-result-item:hover {
    background-color: #f5f6f6;
}

.result-date {
    font-size: 13px;
    color: var(--text-secondary);
    margin-bottom: 4px;
}

.result-sender {
    font-size: 15px;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 2px;
}

/* Green for one, white for other in search results to distinguishing? Or just keep simple */
.result-sender.me {
    color: #008069; /* WhatsApp Teal for me */
}
.result-sender.them {
    color: var(--text-primary);
}

.result-snippet {
    font-size: 14px;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.result-snippet .highlight {
    color: #008069; /* Green highlight text */
    font-weight: bold;
    background-color: rgba(37, 211, 102, 0.2); /* Light green bg */
}

/* Date Picker */
.date-picker {
    position: absolute;
    top: 55px;
    left: 20px; /* Adjust as needed */
    background-color: #ffffff; /* Light bg */
    box-shadow: 0 4px 12px rgba(0,0,0,0.15); /* Softer shadow */
    border-radius: 8px;
    padding: 15px;
    z-index: 50; /* Higher than overlay (20) */
    width: 280px;
    display: flex;
    flex-direction: column;
    color: var(--text-primary);
    border: 1px solid #e9edef;
}

.calendar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
}

.calendar-header button {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 16px;
    padding: 5px;
}

.calendar-title {
    font-weight: 500;
}

.calendar-grid {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    text-align: center;
    font-size: 13px;
    gap: 5px;
}

.calendar-day-header {
    color: var(--text-meta);
    padding-bottom: 5px;
}

.calendar-day {
    padding: 8px 0;
    cursor: pointer;
    border-radius: 50%;
    color: var(--text-primary);
}

.calendar-day:hover {
    background-color: #f0f2f5;
}

.calendar-day.selected {
    background-color: #008069;
    color: white;
}

.calendar-day.empty {
    cursor: default;
}

.calendar-day.has-msg {
    font-weight: bold;
    color: var(--text-primary);
    border: 1px solid #008069; /* Visual clue for dates with messages */
}
.calendar-day.no-msg {
    color: #d1d7db; /* Dimmed */
    pointer-events: none;
    border: none;
}

/* Scroll Jump Highlight Animation */
@keyframes highlight-pulse {
    0% { background-color: rgba(37, 211, 102, 0.3); }
    100% { background-color: transparent; }
}

.message-row.highlight-jump .message-bubble {
    animation: highlight-pulse 2s ease-out;
}

/* Lightbox */
.lightbox-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.9);
    z-index: 100; /* Highest priority */
    display: flex;
    justify-content: center;
    align-items: center;
    backdrop-filter: blur(5px);
}

.lightbox-overlay img {
    max-width: 90%;
    max-height: 90%;
    object-fit: contain;
    border-radius: 4px;
    box-shadow: 0 0 20px rgba(0,0,0,0.5);
}

.lightbox-close-btn {
    position: absolute;
    top: 20px;
    left: 20px; /* Requested: Left corner */
    background: none;
    border: none;
    color: white;
    font-size: 40px;
    cursor: pointer;
    line-height: 1;
    z-index: 101;
    opacity: 0.8;
    transition: opacity 0.2s;
}

.lightbox-close-btn:hover {
    opacity: 1;
}

/* Profile Switcher Dropdown */
.profile-dropdown {
    position: absolute;
    top: 60px;
    left: 60px;
    background-color: white;
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 60;
    min-width: 220px;
    overflow: hidden;
    animation: fadeIn 0.1s ease-out;
}

.dropdown-item {
    display: flex;
    align-items: center;
    padding: 12px 15px;
    cursor: pointer;
    transition: background 0.1s;
    color: var(--text-primary);
    font-size: 14.5px;
}

.dropdown-item:hover {
    background-color: #f0f2f5;
}

.dropdown-item img {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    margin-right: 12px;
    object-fit: cover;
}
