/**
 * Customer Messages Page Styles (Phase E)
 * Veranda Market - Jobs & Services v2 Unified Inbox
 *
 * Token source: ../css/jobs-and-services/tokens.css (loaded first in HTML).
 * No local --primary-color / --bg-primary / etc. — everything resolves via
 * the shared design tokens (--primary-color, --gray-XXX, --white, --shadow-X,
 * --radius-X, --transition).
 */

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

.messages-main {
    flex: 1;
    padding-top: 80px;
    background-color: var(--gray-50);
    color: var(--gray-900);
    font-family: var(--font-primary);
}

.messages-main .container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
    height: calc(100vh - 80px);
    display: flex;
    flex-direction: column;
}

/* Page Header */
.page-header {
    margin-bottom: 1.25rem;
}

.page-header h1 {
    font-size: 1.5rem;
    font-weight: 700;
    font-family: var(--font-secondary);
    margin: 0 0 0.25rem;
    color: var(--gray-900);
}

.page-subtitle {
    font-size: 0.9rem;
    color: var(--gray-500);
    margin: 0;
}

/* ===================================
   Messages Wrapper
   =================================== */
.messages-wrapper {
    flex: 1;
    display: grid;
    grid-template-columns: 360px 1fr;
    background: var(--white);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    overflow: hidden;
    min-height: 0;
}

/* ===================================
   Conversations Sidebar
   =================================== */
.conversations-sidebar {
    border-right: 1px solid var(--gray-200);
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.conversations-header {
    padding: 1.25rem;
    border-bottom: 1px solid var(--gray-100);
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.conversations-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 0;
}

.unread-count {
    font-size: 0.8rem;
    color: var(--gray-400);
}

.unread-count.has-unread {
    color: var(--primary-color);
    font-weight: 600;
}

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

.conversation-item {
    display: grid;
    grid-template-columns: 48px 1fr auto;
    align-items: start;
    gap: 0.85rem;
    padding: 0.9rem 1.1rem;
    cursor: pointer;
    transition: background var(--transition);
    border-bottom: 1px solid var(--gray-100);
    position: relative;
}

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

.conversation-item.active {
    background: rgba(26, 137, 23, 0.08);
}

.conversation-item.unread {
    background: rgba(102, 126, 234, 0.05);
}

.conversation-item.unread .conversation-name {
    font-weight: 700;
}

.conversation-item.unread .last-message {
    color: var(--gray-900);
    font-weight: 500;
}

.conversation-avatar {
    position: relative;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    background: var(--gray-100);
    color: var(--gray-600);
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

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

.conversation-info {
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.conversation-top-row {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    gap: 0.5rem;
}

.conversation-name {
    font-size: 0.95rem;
    color: var(--gray-900);
    font-weight: 500;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conversation-time {
    font-size: 0.72rem;
    color: var(--gray-400);
    white-space: nowrap;
    flex-shrink: 0;
}

.conversation-meta-row {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-wrap: wrap;
}

.last-message {
    font-size: 0.85rem;
    color: var(--gray-500);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin: 0;
}

.unread-badge {
    min-width: 20px;
    height: 20px;
    background: var(--primary-color);
    color: var(--white);
    font-size: 0.7rem;
    font-weight: 700;
    border-radius: 10px;
    padding: 0 6px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    align-self: center;
}

/* ===================================
   Context Badge (Booking / Application / Listing / Direct)
   =================================== */
.context-badge {
    display: inline-flex;
    align-items: center;
    gap: 0.25rem;
    font-size: 0.65rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    padding: 0.12rem 0.45rem;
    border-radius: 4px;
    line-height: 1.4;
    flex-shrink: 0;
}

.context-badge i {
    font-size: 0.6rem;
}

.context-badge--booking {
    background: rgba(245, 158, 11, 0.15);
    color: var(--warning-orange);
}

.context-badge--application {
    background: rgba(102, 126, 234, 0.15);
    color: var(--secondary-color);
}

.context-badge--listing {
    background: rgba(16, 185, 129, 0.15);
    color: var(--success-green);
}

.context-badge--direct {
    background: var(--gray-100);
    color: var(--gray-600);
}

.context-subtitle {
    font-size: 0.78rem;
    color: var(--gray-500);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1 1 auto;
    min-width: 0;
}

/* ===================================
   Chat Section
   =================================== */
.chat-section {
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.chat-header {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--gray-100);
    background: var(--white);
}

.back-to-list {
    display: none;
    background: none;
    border: none;
    font-size: 1.1rem;
    color: var(--gray-500);
    cursor: pointer;
    padding: 0.5rem;
}

.provider-info-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-decoration: none;
    flex: 1;
    color: inherit;
    min-width: 0;
}

.chat-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    overflow: hidden;
    background: var(--gray-100);
    color: var(--gray-600);
    font-weight: 700;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    flex-shrink: 0;
}

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

.chat-provider-info {
    display: flex;
    flex-direction: column;
    min-width: 0;
}

.chat-provider-name {
    font-weight: 600;
    font-size: 0.95rem;
    color: var(--gray-900);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-provider-profession {
    font-size: 0.8rem;
    color: var(--primary-color);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-actions {
    display: flex;
    gap: 0.5rem;
}

.chat-action {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: var(--gray-50);
    color: var(--gray-600);
    display: flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
    transition: all var(--transition);
}

.chat-action:hover {
    background: var(--primary-color);
    color: var(--white);
}

.chat-action.whatsapp {
    background: rgba(37, 211, 102, 0.1);
    color: #25d366;
}

.chat-action.whatsapp:hover {
    background: #25d366;
    color: var(--white);
}

/* Chat Messages */
.chat-messages {
    flex: 1;
    padding: 1.5rem;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    background: var(--gray-50);
}

.date-divider {
    text-align: center;
    padding: 0.5rem 0;
}

.date-divider span {
    background: var(--white);
    padding: 0.35rem 1rem;
    border-radius: 20px;
    font-size: 0.75rem;
    color: var(--gray-400);
    font-weight: 500;
    box-shadow: var(--shadow-sm);
}

.message {
    max-width: 75%;
    display: flex;
    flex-direction: column;
}

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

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

.message-content {
    padding: 0.75rem 1rem;
    border-radius: var(--radius-md);
    word-wrap: break-word;
}

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

.message.received .message-content {
    background: var(--white);
    color: var(--gray-900);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--gray-100);
}

.message-content p {
    font-size: 0.9rem;
    margin: 0 0 0.3rem;
    line-height: 1.45;
}

.message-time {
    font-size: 0.68rem;
    opacity: 0.7;
    display: block;
    text-align: right;
}

/* Optimistic & failed states */
.message.message-pending .message-content {
    opacity: 0.6;
}

.message.send-failed .message-content {
    border: 1px solid var(--danger-red);
}

.message-status {
    font-size: 0.7rem;
    margin-top: 0.2rem;
    display: inline-flex;
    align-items: center;
    gap: 0.3rem;
    align-self: flex-end;
    color: var(--gray-500);
}

.message.send-failed .message-status {
    color: var(--danger-red);
}

.message-retry-btn {
    background: none;
    border: none;
    color: var(--danger-red);
    cursor: pointer;
    font-size: 0.7rem;
    text-decoration: underline;
    padding: 0;
}

.message-retry-btn:hover {
    color: var(--primary-color);
}

/* Chat Input */
.chat-input-area {
    padding: 1rem 1.5rem;
    border-top: 1px solid var(--gray-100);
    background: var(--white);
}

.chat-form {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: var(--gray-50);
    border-radius: 30px;
    padding: 0.5rem;
}

.chat-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 0.9rem;
    padding: 0.6rem 1rem;
    outline: none;
    color: var(--gray-900);
}

.chat-input:disabled {
    cursor: not-allowed;
}

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

.send-btn:hover:not(:disabled) {
    background: var(--primary-dark);
}

.send-btn:disabled {
    background: var(--gray-300);
    cursor: not-allowed;
}

/* ===================================
   Empty / Loading States
   =================================== */
.chat-empty,
.no-conversations {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2rem;
    background: var(--gray-50);
}

.empty-content {
    text-align: center;
    color: var(--gray-400);
}

.empty-content i {
    font-size: 4rem;
    margin-bottom: 1rem;
    opacity: 0.35;
}

.empty-content h3 {
    font-size: 1.25rem;
    margin: 0 0 0.5rem;
    color: var(--gray-600);
}

.empty-content p {
    font-size: 0.9rem;
    margin: 0 0 1.5rem;
}

.loading-state,
.error-state,
.empty-messages {
    padding: 2rem 1rem;
    text-align: center;
    color: var(--gray-400);
    font-size: 0.9rem;
}

.loading-state i {
    margin-right: 0.5rem;
}

.error-state {
    color: var(--danger-red);
}

/* ===================================
   Access Modal (legacy phone-only customers)
   =================================== */
.access-modal {
    position: fixed;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.access-modal-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
}

.access-modal-content {
    position: relative;
    background: var(--white);
    border-radius: var(--radius-lg);
    padding: 2rem;
    width: 100%;
    max-width: 420px;
    text-align: center;
}

.access-icon {
    width: 80px;
    height: 80px;
    background: rgba(26, 137, 23, 0.12);
    color: var(--primary-color);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    margin: 0 auto 1.5rem;
}

.access-modal-content h2 {
    font-size: 1.5rem;
    font-weight: 600;
    margin: 0 0 0.5rem;
}

.access-modal-content > p {
    font-size: 0.9rem;
    color: var(--gray-500);
    margin: 0 0 1.25rem;
}

.access-form {
    margin-bottom: 0.5rem;
}

.form-group {
    margin-bottom: 1rem;
}

.phone-input-wrapper {
    display: flex;
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-md);
    overflow: hidden;
}

.country-code {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0 1rem;
    background: var(--gray-50);
    border-right: 1px solid var(--gray-200);
    font-size: 0.9rem;
    color: var(--gray-600);
}

.flag-icon {
    width: 20px;
    height: 14px;
    object-fit: cover;
    border-radius: 2px;
}

.phone-input-wrapper input {
    flex: 1;
    border: none;
    padding: 0.85rem 1rem;
    font-size: 1rem;
    outline: none;
    background: var(--white);
}

.field-error {
    display: block;
    font-size: 0.8rem;
    color: var(--danger-red);
    text-align: left;
    margin-top: 0.5rem;
    min-height: 1em;
}

.access-or {
    font-size: 0.85rem;
    color: var(--gray-500);
    margin: 0.75rem 0 0;
}

.access-or a {
    color: var(--primary-color);
    font-weight: 600;
    text-decoration: none;
}

.access-or a:hover {
    text-decoration: underline;
}

.access-note {
    font-size: 0.78rem;
    color: var(--gray-400);
    margin: 0.5rem 0 0;
}

/* ===================================
   Buttons
   =================================== */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.85rem 1.5rem;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all var(--transition);
    border: none;
    text-decoration: none;
}

.btn-primary {
    background: var(--primary-color);
    color: var(--white);
}

.btn-primary:hover {
    background: var(--primary-dark);
}

.btn-full {
    width: 100%;
}

/* ===================================
   Toast
   =================================== */
.toast {
    position: fixed;
    bottom: 2rem;
    left: 50%;
    transform: translateX(-50%) translateY(100px);
    background: var(--gray-900);
    color: var(--white);
    padding: 0.9rem 1.5rem;
    border-radius: var(--radius-md);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    box-shadow: var(--shadow-lg);
    opacity: 0;
    visibility: hidden;
    transition: all var(--transition);
    z-index: 3000;
    max-width: 90vw;
}

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

.toast i {
    color: var(--success-green);
    font-size: 1.15rem;
}

.toast.error i {
    color: var(--danger-red);
}

.toast-message {
    font-size: 0.9rem;
}

/* ===================================
   Header support (overlays the shared header from style.css)
   =================================== */
.messages-main .back-link {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    color: var(--gray-600);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    transition: color var(--transition);
}

.messages-main .back-link:hover {
    color: var(--primary-color);
}

/* ===================================
   Responsive Styles
   =================================== */
@media (max-width: 992px) {
    .messages-wrapper {
        grid-template-columns: 1fr;
    }

    .conversations-sidebar {
        border-right: none;
    }

    .chat-section {
        display: none;
        position: fixed;
        inset: 0;
        background: var(--white);
        /* Above the mobile header (mobile-nav.css makes .header position:fixed
           z-index:1000) so an open conversation fully covers it. */
        z-index: 1100;
    }

    .chat-section.active {
        display: flex;
    }

    .back-to-list {
        display: block;
    }

    .chat-empty {
        display: none;
    }
}

@media (max-width: 768px) {
    .messages-main .container {
        padding: 1rem;
    }

    .page-header {
        margin-bottom: 1rem;
    }

    .page-header h1 {
        font-size: 1.25rem;
    }

    .messages-wrapper {
        border-radius: var(--radius-md);
    }

    .conversations-header {
        padding: 1rem;
    }

    .conversation-item {
        padding: 0.8rem 1rem;
    }

    .conversation-avatar {
        width: 44px;
        height: 44px;
    }

    .chat-header {
        padding: 0.85rem 1rem;
    }

    .chat-messages {
        padding: 1rem;
    }

    .chat-input-area {
        padding: 0.75rem 1rem;
    }

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

@media (max-width: 480px) {
    .chat-actions {
        gap: 0.25rem;
    }

    .chat-action {
        width: 36px;
        height: 36px;
    }

    .access-modal-content {
        padding: 1.5rem;
    }
}

/* Mobile header is fixed + 56px tall (mobile-nav.css). The base padding-top /
   container height assume the ~80px desktop header — realign them here. */
@media (max-width: 767px) {
    .messages-main {
        padding-top: 56px;
    }

    .messages-main .container {
        height: calc(100vh - 56px);
    }
}
