/* ============================================================================
   Chat 1:1 — UI nutriologo web
   Layout WhatsApp-Web: sidebar conversaciones + panel de mensajes.
   Usa las variables CSS globales del header (--bg, --card, --accent, etc.).
   ========================================================================== */

.ybt-chat {
    display: grid;
    grid-template-columns: 340px 1fr;
    height: calc(100vh - 0px);
    background: var(--bg);
    color: var(--text);
    overflow: hidden;
}

/* El atributo HTML `hidden` debe ganarle a las reglas display: flex de .chat-empty
   y .chat-active (misma especificidad → el orden los pierde). Lo forzamos. */
.ybt-chat [hidden] {
    display: none !important;
}

@media (max-width: 980px) {
    .ybt-chat {
        grid-template-columns: 1fr;
    }
    .chat-side { display: flex; }
    .chat-panel { display: none; }
    .ybt-chat.is-conv-open .chat-side { display: none; }
    .ybt-chat.is-conv-open .chat-panel { display: flex; }
}

/* ── Sidebar ────────────────────────────────────────────────────────────── */

.chat-side {
    display: flex;
    flex-direction: column;
    background: var(--card);
    border-right: 1px solid var(--border);
    min-height: 0;
}

.chat-side-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-side-title {
    margin: 0;
    font-size: 16px;
    font-weight: 600;
}

.chat-side-sub {
    margin: 2px 0 0 0;
    font-size: 11px;
    color: var(--muted);
}

.chat-icon-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    width: 32px;
    height: 32px;
    border-radius: 8px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 16px;
    transition: background .15s ease, border-color .15s ease;
}

.chat-icon-btn:hover {
    background: var(--card-2);
    border-color: var(--border-hi, var(--border));
}

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

.chat-side-search {
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-side-search input {
    width: 100%;
    height: 36px;
    background: var(--card-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0 12px;
    color: var(--text);
    font: inherit;
}

.chat-side-search input:focus {
    outline: none;
    border-color: var(--accent);
}

/* ── Lista de conversaciones ───────────────────────────────────────────── */

.conv-list {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 4px 0;
}

.conv-item {
    display: grid;
    grid-template-columns: 44px 1fr auto;
    align-items: center;
    gap: 10px;
    padding: 10px 14px;
    cursor: pointer;
    transition: background .12s ease;
    border-bottom: 1px solid var(--border);
}

.conv-item:hover {
    background: var(--card-2);
}

.conv-item.is-active {
    background: var(--card-2);
    border-left: 3px solid var(--accent);
    padding-left: 11px;
}

.conv-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: rgba(79, 140, 255, .15);
    color: var(--accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 16px;
    overflow: hidden;
}

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

.conv-avatar.has-img {
    background: transparent;
}

.conv-mid {
    min-width: 0;
}

.conv-name {
    font-size: 14px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-preview {
    font-size: 12px;
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

.conv-preview.is-deleted {
    font-style: italic;
}

.conv-side {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
}

.conv-time {
    font-size: 10px;
    color: var(--muted);
}

.conv-unread {
    min-width: 18px;
    height: 18px;
    border-radius: 999px;
    background: var(--accent);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 5px;
}

.conv-unread.is-zero {
    visibility: hidden;
}

.conv-skel {
    height: 64px;
    margin: 4px 12px;
    border-radius: 12px;
    background: linear-gradient(90deg, var(--card-2), rgba(255, 255, 255, .04), var(--card-2));
    background-size: 200% 100%;
    animation: convSkel 1.2s linear infinite;
}

@keyframes convSkel {
    0%   { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

.conv-empty {
    padding: 30px 14px;
    text-align: center;
    color: var(--muted);
    font-size: 13px;
}

/* ── Panel del chat ────────────────────────────────────────────────────── */

.chat-panel {
    display: flex;
    flex-direction: column;
    background: var(--bg-soft);
    min-height: 0;
}

.chat-empty {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 40px;
    color: var(--muted);
    text-align: center;
}

.chat-empty-icon {
    font-size: 56px;
    opacity: .5;
    margin-bottom: 12px;
}

.chat-empty h3 {
    margin: 0 0 6px 0;
    color: var(--text);
    font-weight: 600;
}

.chat-empty p {
    margin: 0;
    font-size: 13px;
}

.chat-active {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.chat-head {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: var(--card);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.chat-back {
    display: none;
}

@media (max-width: 980px) {
    .chat-back { display: inline-flex; }
}

.chat-head-avatar {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: rgba(79, 140, 255, .15);
    color: var(--accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 15px;
    flex-shrink: 0;
    overflow: hidden;
}

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

.chat-head-avatar.has-img {
    background: transparent;
}

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

.chat-head-name {
    font-weight: 600;
    font-size: 15px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-head-sub {
    font-size: 11px;
    color: var(--muted);
    margin-top: 2px;
}

/* ── Scroller de mensajes ──────────────────────────────────────────────── */

.msg-scroller {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background:
        radial-gradient(800px 600px at 50% -10%, rgba(79, 140, 255, .04) 0%, transparent 50%),
        var(--bg-soft);
}

.msg-load-older {
    text-align: center;
    color: var(--muted);
    font-size: 12px;
    padding: 8px;
    cursor: pointer;
    border-radius: 8px;
}

.msg-load-older:hover {
    background: var(--card-2);
}

.msg-list {
    display: flex;
    flex-direction: column;
    gap: 2px;
    max-width: 760px;
    margin: 0 auto;
}

/* ── Burbujas ──────────────────────────────────────────────────────────── */

.msg {
    display: flex;
    flex-direction: column;
    max-width: 70%;
}

.msg.is-mine {
    align-self: flex-end;
    align-items: flex-end;
}

.msg.is-other {
    align-self: flex-start;
    align-items: flex-start;
}

.msg-bubble {
        padding: 5px 10px;
    border-radius: 14px;
    font-size: 13.5px;
    line-height: .60;
    word-wrap: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    position: relative;
    width: fit-content;
    max-width: 100%;
}

/* Hora pequeñita en la esquina inferior derecha, DENTRO de la burbuja. */
.bubble-time {
    position: absolute;
    right: 8px;
    bottom: 3px;
    font-size: 10px;
    line-height: 1;
    opacity: .65;
    white-space: nowrap;
    pointer-events: none;
}

.msg.is-mine .bubble-time {
    color: rgba(255, 255, 255, .85);
}

.msg.is-mine .bubble-time .check.is-read {
    color: #b7e6ff;
}

.msg.is-other .bubble-time {
    color: var(--muted);
}

.msg.is-mine .msg-bubble {
    background: var(--accent);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.msg.is-other .msg-bubble {
    background: var(--card);
    color: var(--text);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--border);
}

.msg.is-deleted .msg-bubble {
    font-style: italic;
    opacity: .7;
}

/* Acciones (editar / eliminar) que aparecen al hover sobre mensajes propios. */
.msg {
    position: relative;
}

.msg-actions {
    display: flex;
    gap: 4px;
    margin-bottom: 2px;
    opacity: 0;
    transition: opacity .12s ease;
}

.msg:hover .msg-actions {
    opacity: 1;
}

.msg-act {
    background: var(--card);
    border: 1px solid var(--border);
    color: var(--muted);
    width: 24px;
    height: 24px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 11px;
    line-height: 1;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: color .12s ease, border-color .12s ease;
}

.msg-act:hover {
    color: var(--text);
    border-color: var(--border-hi, var(--border));
}

/* Modo editar inline: textarea ocupa el lugar del texto. */
.msg-bubble.is-editing {
    padding-right: 10px;     /* sin hueco para timeChip */
}

.msg-edit-input {
    width: 240px;
    min-height: 28px;
    background: rgba(255, 255, 255, .15);
    border: 1px solid rgba(255, 255, 255, .3);
    border-radius: 8px;
    padding: 4px 6px;
    color: inherit;
    font: inherit;
    resize: none;
    outline: none;
}

.msg.is-other .msg-edit-input {
    background: var(--card-2);
    border-color: var(--border);
    color: var(--text);
}

.msg-edit-hint {
    display: block;
    font-size: 10px;
    opacity: .7;
    margin-top: 3px;
}

.msg-meta .check {
    font-size: 12px;
}

.msg-meta .check.is-read {
    color: var(--accent);
}

.msg-edited {
    font-style: italic;
    opacity: .8;
}

/* ── Adjuntos en burbuja ───────────────────────────────────────────────── */

.msg-adj-grid {
    display: grid;
    gap: 4px;
    margin-bottom: 6px;
    max-width: 320px;
}

.msg-adj-grid.n1 { grid-template-columns: 1fr; }
.msg-adj-grid.n2 { grid-template-columns: 1fr 1fr; }
.msg-adj-grid.n3 { grid-template-columns: 1fr 1fr; }
.msg-adj-grid.n3 .msg-adj:first-child { grid-column: 1 / 3; }
.msg-adj-grid.n4 { grid-template-columns: 1fr 1fr; }

.msg-adj {
    width: 100%;
    aspect-ratio: 1;
    object-fit: cover;
    border-radius: 8px;
    cursor: pointer;
    background: var(--card-2);
    transition: opacity .15s ease;
}

.msg-adj:hover {
    opacity: .9;
}

/* Cuando solo hay 1 adjunto sin texto, deja que respete su aspecto natural */
.msg-bubble.is-img-only {
    padding: 4px;
    background: transparent !important;
    border: none !important;
}

.msg-bubble.is-img-only .msg-adj {
    aspect-ratio: auto;
    max-height: 360px;
    width: auto;
    max-width: 100%;
}

/* ── Composer ──────────────────────────────────────────────────────────── */

.chat-composer {
    background: var(--card);
    border-top: 1px solid var(--border);
    flex-shrink: 0;
}

.composer-row {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
}

.composer-btn {
    background: transparent;
    border: 1px solid var(--border);
    color: var(--muted);
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    flex-shrink: 0;
    transition: all .15s ease;
}

.composer-btn:hover:not(:disabled) {
    color: var(--text);
    border-color: var(--border-hi, var(--border));
    background: var(--card-2);
}

.composer-btn.primary {
    background: var(--accent);
    color: #fff;
    border-color: transparent;
}

.composer-btn.primary:hover:not(:disabled) {
    filter: brightness(1.1);
    color: #fff;
}

.composer-btn:disabled {
    opacity: .4;
    cursor: not-allowed;
}

.composer-btn svg {
    width: 18px;
    height: 18px;
}

#msgInput {
    flex: 1;
    min-height: 38px;
    max-height: 140px;
    padding: 8px 12px;
    background: var(--card-2);
    border: 1px solid var(--border);
    border-radius: 18px;
    color: var(--text);
    font: inherit;
    font-size: 14px;
    line-height: 1.4;
    resize: none;
    overflow-y: auto;
}

#msgInput:focus {
    outline: none;
    border-color: var(--accent);
}

/* Previews de adjuntos antes de enviar */
.attach-previews {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
    padding: 8px 12px 0;
}

.attach-preview {
    position: relative;
    width: 56px;
    height: 56px;
    border-radius: 8px;
    overflow: hidden;
    background: var(--card-2);
    border: 1px solid var(--border);
}

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

.attach-preview .attach-x {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: rgba(0, 0, 0, .65);
    color: #fff;
    border: none;
    font-size: 11px;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}

.attach-preview .attach-progress {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, .55);
    color: #fff;
    font-size: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ── Modal nuevo chat ──────────────────────────────────────────────────── */

.chat-dialog {
    border: 1px solid var(--border);
    border-radius: 14px;
    background: var(--card);
    color: var(--text);
    padding: 0;
    width: min(440px, 92vw);
    max-height: 80vh;
    box-shadow: 0 12px 40px rgba(0, 0, 0, .4);
}

.chat-dialog::backdrop {
    background: rgba(0, 0, 0, .55);
    backdrop-filter: blur(2px);
}

.chat-dialog-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
}

.chat-dialog-head h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 600;
}

.chat-dialog-body {
    padding: 12px 16px 16px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow: hidden;
    max-height: 70vh;
}

#newChatSearch {
    width: 100%;
    height: 38px;
    padding: 0 12px;
    background: var(--card-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    color: var(--text);
    font: inherit;
}

#newChatSearch:focus {
    outline: none;
    border-color: var(--accent);
}

.new-chat-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.new-chat-item {
    display: grid;
    grid-template-columns: 36px 1fr;
    align-items: center;
    gap: 10px;
    padding: 8px 6px;
    border-radius: 8px;
    cursor: pointer;
    transition: background .12s ease;
}

.new-chat-item:hover {
    background: var(--card-2);
}

.new-chat-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(79, 140, 255, .15);
    color: var(--accent);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    font-size: 13px;
}

.new-chat-name {
    font-size: 14px;
    font-weight: 500;
}
