/* ==========================================
   CHAT DEL GRUPO DE CADA COMUNIDAD (js/community-chat.js)
   Prefijo .cchat- · Marca: navy #0B2E44, dorado #D4B06A, beige #F4E9D8
   Modo oscuro: .theme-dark en <html>
   ========================================== */

.cchat {
    --cchat-bg: #F4E9D8;
    --cchat-header-bg: #0B2E44;
    --cchat-header-fg: #FFFFFF;
    --cchat-header-sub: rgba(255, 255, 255, 0.78);
    --cchat-bubble-other-bg: #FFFFFF;
    --cchat-bubble-other-fg: #0B2E44;
    --cchat-bubble-mine-bg: #0B2E44;
    --cchat-bubble-mine-fg: #FFFFFF;
    --cchat-meta-other: #5f6b73;
    --cchat-meta-mine: rgba(255, 255, 255, 0.75);
    --cchat-author: #7F6124;
    --cchat-day-bg: rgba(11, 46, 68, 0.08);
    --cchat-day-fg: #0B2E44;
    --cchat-composer-bg: #FFFFFF;
    --cchat-composer-border: rgba(11, 46, 68, 0.12);
    --cchat-input-bg: #F9F2E6;
    --cchat-input-fg: #0B2E44;
    --cchat-input-placeholder: #6b7280;
    --cchat-accent: #D4B06A;
    --cchat-send-bg: #0B2E44;
    --cchat-send-fg: #FFFFFF;
    --cchat-muted-fg: #4b5563;
    --cchat-danger: #b42318;
    --cchat-sheet-bg: #FFFFFF;
    --cchat-sheet-fg: #0B2E44;
    --cchat-sheet-hover: rgba(11, 46, 68, 0.06);
    --cchat-shadow: 0 1px 2px rgba(11, 46, 68, 0.12);

    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    display: flex;
    flex-direction: column;
    background: var(--cchat-bg);
    color: var(--cchat-bubble-other-fg);
    font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
    overscroll-behavior: contain;
}

.theme-dark .cchat {
    --cchat-bg: #1a1a2e;
    --cchat-header-bg: #0B2E44;
    --cchat-bubble-other-bg: #16213e;
    --cchat-bubble-other-fg: #F4E9D8;
    --cchat-bubble-mine-bg: #D4B06A;
    --cchat-bubble-mine-fg: #0B2E44;
    --cchat-meta-other: #b8c0cc;
    --cchat-meta-mine: rgba(11, 46, 68, 0.78);
    --cchat-author: #E6C48B;
    --cchat-day-bg: rgba(244, 233, 216, 0.1);
    --cchat-day-fg: #F4E9D8;
    --cchat-composer-bg: #16213e;
    --cchat-composer-border: rgba(244, 233, 216, 0.12);
    --cchat-input-bg: #1a1a2e;
    --cchat-input-fg: #F4E9D8;
    --cchat-input-placeholder: #9aa3b2;
    --cchat-send-bg: #D4B06A;
    --cchat-send-fg: #0B2E44;
    --cchat-muted-fg: #c5ccd6;
    --cchat-danger: #ff8a80;
    --cchat-sheet-bg: #16213e;
    --cchat-sheet-fg: #F4E9D8;
    --cchat-sheet-hover: rgba(244, 233, 216, 0.08);
    --cchat-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

.cchat-sr-only {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ---------- Cabecera ---------- */
.cchat-header {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 6px;
    padding: calc(8px + env(safe-area-inset-top, 0px)) 8px 8px;
    background: var(--cchat-header-bg);
    color: var(--cchat-header-fg);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.12);
}

.cchat-header-text {
    flex: 1 1 auto;
    min-width: 0;
}

.cchat-title {
    margin: 0;
    font-family: 'Lora', Georgia, serif;
    font-size: 17px;
    font-weight: 700;
    line-height: 1.25;
    color: var(--cchat-header-fg);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    outline: none;
}

.cchat-subtitle {
    margin: 2px 0 0;
    font-size: 12.5px;
    line-height: 1.3;
    color: var(--cchat-header-sub);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.cchat-muted-tag {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    vertical-align: middle;
}

.cchat-icon-btn {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 50%;
    background: transparent;
    color: inherit;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
}

.cchat-icon-btn:hover,
.cchat-icon-btn:focus-visible {
    background: rgba(255, 255, 255, 0.14);
}

.cchat button:focus-visible,
.cchat textarea:focus-visible {
    outline: 3px solid var(--cchat-accent);
    outline-offset: 2px;
}

/* ---------- Lista de mensajes ---------- */
.cchat-body {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.cchat-list {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 8px 10px 12px;
}

.cchat-messages {
    list-style: none;
    margin: 0 auto;
    padding: 0;
    max-width: 760px;
}

.cchat-day {
    display: flex;
    justify-content: center;
    margin: 14px 0 8px;
}

.cchat-day span {
    padding: 4px 12px;
    border-radius: 999px;
    background: var(--cchat-day-bg);
    color: var(--cchat-day-fg);
    font-size: 12px;
    font-weight: 600;
}

.cchat-msg {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    margin-top: 10px;
}

.cchat-msg.is-grouped {
    margin-top: 2px;
}

.cchat-msg.is-mine {
    justify-content: flex-end;
}

.cchat-avatar-slot {
    flex: 0 0 32px;
    width: 32px;
    align-self: flex-start;
    padding-top: 18px;
}

.cchat-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
    display: block;
    background: var(--cchat-day-bg);
}

.cchat-avatar-ph {
    display: flex;
    align-items: center;
    justify-content: center;
    background: #0B2E44;
    color: #D4B06A;
    font-weight: 700;
    font-size: 14px;
}

.theme-dark .cchat-avatar-ph {
    background: #D4B06A;
    color: #0B2E44;
}

.cchat-bubble-col {
    display: flex;
    flex-direction: column;
    min-width: 0;
    max-width: min(78%, 560px);
}

.cchat-msg.is-mine .cchat-bubble-col {
    align-items: flex-end;
}

.cchat-author {
    margin: 0 0 3px 4px;
    font-size: 12.5px;
    font-weight: 600;
    color: var(--cchat-author);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
}

.cchat-bubble-row {
    display: flex;
    align-items: center;
    gap: 0;
    max-width: 100%;
}

.cchat-msg.is-mine .cchat-bubble-row {
    flex-direction: row-reverse;
}

.cchat-bubble {
    min-width: 0;
    padding: 8px 12px 6px;
    border-radius: 16px;
    background: var(--cchat-bubble-other-bg);
    color: var(--cchat-bubble-other-fg);
    box-shadow: var(--cchat-shadow);
    border-bottom-left-radius: 6px;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

.cchat-msg.is-grouped.is-other .cchat-bubble {
    border-top-left-radius: 6px;
}

.cchat-msg.is-mine .cchat-bubble {
    background: var(--cchat-bubble-mine-bg);
    color: var(--cchat-bubble-mine-fg);
    border-bottom-left-radius: 16px;
    border-bottom-right-radius: 6px;
}

.cchat-msg.is-grouped.is-mine .cchat-bubble {
    border-top-right-radius: 6px;
}

.cchat-text {
    display: block;
    font-size: 15px;
    line-height: 1.42;
    white-space: pre-wrap;
    overflow-wrap: anywhere;
    word-break: break-word;
}

.cchat-text-deleted {
    font-style: italic;
    opacity: 0.75;
}

.cchat-msg.is-deleted .cchat-bubble {
    box-shadow: none;
    background: transparent;
    border: 1px dashed var(--cchat-composer-border);
    color: var(--cchat-muted-fg);
}

.cchat-meta {
    display: block;
    margin-top: 2px;
    font-size: 11px;
    line-height: 1.2;
    text-align: right;
    color: var(--cchat-meta-other);
}

.cchat-msg.is-mine .cchat-meta {
    color: var(--cchat-meta-mine);
}

.cchat-msg.is-mine.is-deleted .cchat-meta {
    color: var(--cchat-meta-other);
}

.cchat-msg.is-pending .cchat-bubble {
    opacity: 0.7;
}

/* Botón ⋮ de cada mensaje: icono pequeño, zona táctil de 44 px */
.cchat-msg-more {
    flex: 0 0 auto;
    width: 36px;
    height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    color: var(--cchat-meta-other);
    border-radius: 12px;
    cursor: pointer;
    opacity: 0.55;
    -webkit-tap-highlight-color: transparent;
}

.cchat-msg-more svg {
    width: 16px;
    height: 16px;
}

.cchat-msg-more:hover,
.cchat-msg-more:focus-visible {
    opacity: 1;
    background: var(--cchat-sheet-hover);
}

@media (hover: hover) and (pointer: fine) {
    .cchat-msg-more { opacity: 0; }
    .cchat-msg:hover .cchat-msg-more,
    .cchat-msg-more:focus-visible { opacity: 1; }
}

/* ---------- Estados ---------- */
.cchat-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 6px;
    min-height: 60%;
    padding: 32px 20px;
    color: var(--cchat-muted-fg);
}

.cchat-state p { margin: 0; }

.cchat-state-icon {
    font-size: 40px;
    line-height: 1;
    margin-bottom: 6px;
}

.cchat-state-title {
    font-family: 'Lora', Georgia, serif;
    font-size: 17px;
    font-weight: 700;
    color: var(--cchat-bubble-other-fg);
}

.cchat-state-text {
    font-size: 14px;
    max-width: 320px;
}

.cchat-spinner {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 3px solid var(--cchat-day-bg);
    border-top-color: var(--cchat-accent);
    animation: cchat-spin 0.9s linear infinite;
}

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

.cchat-retry,
.cchat-load-more-btn {
    min-height: 44px;
    padding: 10px 20px;
    border-radius: 999px;
    border: 2px solid var(--cchat-bubble-other-fg);
    background: transparent;
    color: var(--cchat-bubble-other-fg);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
}

.cchat-retry { margin-top: 12px; }

.cchat-load-more {
    display: flex;
    justify-content: center;
    padding: 6px 0 4px;
}

.cchat-load-more-btn {
    border-width: 1px;
    font-size: 13px;
}

.cchat-load-more-btn:disabled { opacity: 0.6; cursor: default; }

.cchat-new-pill {
    position: absolute;
    left: 50%;
    bottom: 12px;
    transform: translateX(-50%);
    display: inline-flex;
    align-items: center;
    gap: 6px;
    min-height: 44px;
    padding: 0 16px;
    border: none;
    border-radius: 999px;
    background: var(--cchat-send-bg);
    color: var(--cchat-send-fg);
    font-size: 13px;
    font-weight: 600;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    animation: cchat-pop var(--motion-enter, 0.28s ease);
}

.cchat-new-pill[hidden] { display: none; }

@keyframes cchat-pop {
    from { opacity: 0; transform: translate(-50%, 8px); }
    to { opacity: 1; transform: translate(-50%, 0); }
}

/* ---------- Caja de escribir ---------- */
.cchat-composer-wrap {
    flex: 0 0 auto;
    background: var(--cchat-composer-bg);
    border-top: 1px solid var(--cchat-composer-border);
    padding: 8px 10px max(8px, env(safe-area-inset-bottom, 0px));
}

.cchat-composer-wrap:empty { display: none; }

.cchat-composer {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    max-width: 760px;
    margin: 0 auto;
}

.cchat-input {
    flex: 1 1 auto;
    min-width: 0;
    min-height: 44px;
    max-height: 140px;
    padding: 11px 14px;
    border-radius: 22px;
    border: 1px solid var(--cchat-composer-border);
    background: var(--cchat-input-bg);
    color: var(--cchat-input-fg);
    font: inherit;
    font-size: 16px; /* 16 px: iOS no hace zoom al enfocar */
    line-height: 1.35;
    resize: none;
    overflow-y: hidden;
    box-sizing: border-box;
}

.cchat-input::placeholder { color: var(--cchat-input-placeholder); }

.cchat-send {
    flex: 0 0 auto;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--cchat-send-bg);
    color: var(--cchat-send-fg);
    cursor: pointer;
    transition: transform var(--motion-press, 80ms ease), opacity 0.15s ease;
}

.cchat-send:active:not(:disabled) { transform: scale(0.94); }

.cchat-send:disabled {
    opacity: 0.4;
    cursor: default;
}

.cchat-counter {
    max-width: 760px;
    margin: 4px auto 0;
    padding-right: 56px;
    text-align: right;
    font-size: 12px;
    color: var(--cchat-muted-fg);
}

.cchat-counter.is-limit {
    color: var(--cchat-danger);
    font-weight: 600;
}

.cchat-counter[hidden] { display: none; }

.cchat-composer-locked {
    max-width: 760px;
    margin: 0 auto;
    padding: 10px 12px;
    border-radius: 12px;
    background: var(--cchat-day-bg);
    color: var(--cchat-muted-fg);
    font-size: 14px;
    text-align: center;
    line-height: 1.4;
}

/* ---------- Hoja inferior (opciones) ---------- */
.cchat-sheet-backdrop {
    position: absolute;
    inset: 0;
    z-index: 5;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    background: rgba(6, 27, 41, 0);
    transition: background var(--motion-enter, 0.28s ease);
}

.cchat-sheet-backdrop.is-open {
    background: rgba(6, 27, 41, 0.45);
}

.cchat-sheet {
    width: 100%;
    max-width: 520px;
    max-height: 80%;
    overflow-y: auto;
    padding: 8px 12px max(12px, env(safe-area-inset-bottom, 0px));
    border-radius: 20px 20px 0 0;
    background: var(--cchat-sheet-bg);
    color: var(--cchat-sheet-fg);
    box-shadow: 0 -8px 24px rgba(0, 0, 0, 0.18);
    transform: translateY(100%);
    transition: transform var(--motion-enter, 0.28s cubic-bezier(0.2, 0.8, 0.2, 1));
}

.cchat-sheet-backdrop.is-open .cchat-sheet {
    transform: translateY(0);
}

.cchat-sheet-handle {
    width: 40px;
    height: 4px;
    margin: 4px auto 10px;
    border-radius: 2px;
    background: var(--cchat-composer-border);
}

.cchat-sheet-title {
    margin: 0 8px 6px;
    font-size: 13px;
    font-weight: 600;
    color: var(--cchat-muted-fg);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.cchat-sheet-heading {
    margin: 12px 8px 4px;
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--cchat-author);
}

.cchat-sheet-items {
    display: flex;
    flex-direction: column;
}

.cchat-sheet-item,
.cchat-sheet-cancel {
    display: flex;
    align-items: center;
    gap: 12px;
    width: 100%;
    min-height: 48px;
    padding: 10px 12px;
    border: none;
    border-radius: 12px;
    background: transparent;
    color: inherit;
    font: inherit;
    font-size: 15px;
    text-align: left;
    cursor: pointer;
}

.cchat-sheet-item:hover,
.cchat-sheet-item:focus-visible,
.cchat-sheet-cancel:hover,
.cchat-sheet-cancel:focus-visible {
    background: var(--cchat-sheet-hover);
}

.cchat-sheet-item-icon {
    flex: 0 0 24px;
    width: 24px;
    text-align: center;
}

.cchat-sheet-item.is-danger { color: var(--cchat-danger); }

.cchat-sheet-item.is-checked { font-weight: 700; }

.cchat-sheet-cancel {
    justify-content: center;
    margin-top: 6px;
    font-weight: 600;
    border-top: 1px solid var(--cchat-composer-border);
    border-radius: 0 0 12px 12px;
}

/* ---------- Pantallas anchas ---------- */
@media (min-width: 768px) {
    .cchat-list { padding: 12px 20px 16px; }
    .cchat-sheet {
        margin-bottom: 24px;
        border-radius: 20px;
    }
}

/* ---------- Menos movimiento ---------- */
@media (prefers-reduced-motion: reduce) {
    .cchat-sheet,
    .cchat-sheet-backdrop,
    .cchat-send,
    .cchat-new-pill {
        transition: none !important;
        animation: none !important;
    }
    .cchat-spinner { animation-duration: 2.4s; }
}

.cchat-reduced-motion .cchat-sheet,
.cchat-reduced-motion .cchat-sheet-backdrop {
    transition: none !important;
}

/* ---------- Contador de no leídos (lo pinta CommunityChat.refreshUnreadBadges) ----------
   Uso: <span class="cchat-unread-badge" data-chat-unread-for="<communityId>" hidden></span> */
.cchat-unread-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    border-radius: 999px;
    background: #D4B06A;
    color: #0B2E44;
    font-size: 12px;
    font-weight: 700;
    line-height: 1;
    font-variant-numeric: tabular-nums;
}

.cchat-unread-badge[hidden] { display: none; }

/* El título recibe el foco al abrir (lectores de pantalla): sin marco visible
   para el foco programático; el teclado sigue viendo :focus-visible. */
.cchat-title:focus:not(:focus-visible) { outline: none; }

/* Mientras el chat está abierto, el botón flotante de sugerencias tapa la caja
   de escribir: se oculta solo en esta pantalla. */
body:has(#cchat-root) #feedback-fab { display: none !important; }
