/**
 * Simracing.Fan — Chat Component (sf-chat)
 *
 * UX Discord-style:
 *   - lista messaggi con grouping per autore (≤5 min gap)
 *   - form di composizione in basso, textarea auto-resize, Enter-to-send
 *   - allegato immagine opzionale (input[type=file] in label)
 *   - tag opzionale (es. "Complaint")
 *   - dark mode (body.buddyx-dark-theme)
 *   - mobile responsive
 *
 * Usato da:
 *   - group-channels.php → chat dei canali nei gruppi BP
 *   - event-sections.php → chat dell'evento (incl. complaints area)
 */

/* ==========================================================================
   Tokens
   ========================================================================== */

.sf-chat-messages,
.sf-chat-form,
.sf-chat-empty,
.sf-chat-form-login {
    --sfc-bg-card:        #ffffff;
    --sfc-bg-page:        #f7f8fa;
    --sfc-bg-hover:       rgba(0, 0, 0, 0.04);
    --sfc-text-primary:   #1c1e21;
    --sfc-text-secondary: #65676b;
    --sfc-text-muted:     #8a8d91;
    --sfc-border:         #e4e6eb;
    --sfc-link:           #1877f2;
    --sfc-accent:         #003DA5;
    --sfc-accent-hover:   #002d7a;
    --sfc-radius:         8px;
    --sfc-radius-sm:      6px;
}

body.buddyx-dark-theme .sf-chat-messages,
body.buddyx-dark-theme .sf-chat-form,
body.buddyx-dark-theme .sf-chat-empty,
body.buddyx-dark-theme .sf-chat-form-login {
    --sfc-bg-card:        #232323;
    --sfc-bg-page:        #1a1a1a;
    --sfc-bg-hover:       rgba(255, 255, 255, 0.06);
    --sfc-text-primary:   #e8e8e8;
    --sfc-text-secondary: #b3b3b3;
    --sfc-text-muted:     #8a8a8a;
    --sfc-border:         #2e2e2e;
    --sfc-link:           #5b9fe6;
}

/* ==========================================================================
   Messages list
   ========================================================================== */

.sf-chat-messages {
    list-style: none;
    margin: 0;
    padding: 8px 0;
    display: flex;
    flex-direction: column;
    color: var(--sfc-text-primary);
}

/* Specificità aumentata (.sf-chat-messages > .sf-chat-message) per vincere
   su reset globali di Youzify: `.youzify li, .youzify ul { margin:0; padding:0 }` */
.sf-chat-messages .sf-chat-message {
    display: flex;
    gap: 12px;
    padding: 4px 12px;
    margin-top: 20px;
    border-radius: var(--sfc-radius-sm);
    transition: background-color 0.12s ease;
    position: relative;
}

.sf-chat-messages .sf-chat-message:first-child {
    margin-top: 0;
}

.sf-chat-messages .sf-chat-message:hover {
    background: var(--sfc-bg-hover);
}

/* Grouped: stesso autore + ≤5 min → no header, margin-top piccolo */
.sf-chat-messages .sf-chat-message--grouped {
    margin-top: 2px;
}

.sf-chat-message-avatar {
    flex-shrink: 0;
    width: 40px;
}

.sf-chat-message-avatar img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: block;
    object-fit: cover;
}

.sf-chat-message-avatar--spacer {
    /* Mantiene l'allineamento orizzontale ma non occupa contenuto */
    width: 40px;
    height: 0;
    position: relative;
}

/* Show timestamp on hover when grouped (al posto dell'avatar mancante) */
.sf-chat-message-avatar--spacer .sf-chat-message-time-mini {
    display: none;
}

.sf-chat-message-body {
    flex: 1 1 auto;
    min-width: 0;
}

.sf-chat-message-head {
    display: flex;
    align-items: baseline;
    gap: 8px;
    margin-bottom: 2px;
    flex-wrap: wrap;
}

.sf-chat-message-author {
    font-weight: 700;
    color: var(--sfc-text-primary);
    font-size: 14px;
    line-height: 1.2;
}

.sf-chat-message-time {
    font-size: 11px;
    color: var(--sfc-text-muted);
    line-height: 1.2;
}

.sf-chat-message-tag {
    display: inline-block;
    padding: 1px 6px;
    border-radius: 4px;
    font-size: 10px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    background: var(--sfc-bg-hover);
    color: var(--sfc-text-muted);
    line-height: 1.4;
}

.sf-chat-message-tag--complaint {
    background: rgba(220, 38, 38, 0.15);
    color: var(--sfc-accent);
}

.sf-chat-message-content {
    color: var(--sfc-text-primary);
    font-size: 14px;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: anywhere;
}

.sf-chat-message-content p { margin: 0 0 6px; }
.sf-chat-message-content p:last-child { margin-bottom: 0; }
.sf-chat-message-content a { color: var(--sfc-link); }

.sf-chat-message-attachment {
    margin-top: 6px;
}

.sf-chat-message-attachment img {
    max-width: 100%;
    height: auto;
    max-height: 360px;
    border-radius: var(--sfc-radius-sm);
    display: block;
    border: 1px solid var(--sfc-border);
}

/* ==========================================================================
   Empty / login state
   ========================================================================== */

.sf-chat-empty {
    padding: 30px 20px;
    text-align: center;
    color: var(--sfc-text-muted);
}

.sf-chat-empty-cta {
    margin-top: 6px;
    font-size: 13px;
    color: var(--sfc-text-secondary);
}

.sf-chat-form-login {
    padding: 12px 16px;
    background: var(--sfc-bg-page);
    border: 1px solid var(--sfc-border);
    border-radius: var(--sfc-radius);
    color: var(--sfc-text-secondary);
    font-size: 13px;
    text-align: center;
}

.sf-chat-form-login a { color: var(--sfc-link); }

/* ==========================================================================
   Form composer (in basso)
   ========================================================================== */

.sf-chat-form {
    display: flex;
    align-items: flex-end;
    gap: 10px;
    padding: 10px 12px;
    background: var(--sfc-bg-card);
    border: 1px solid var(--sfc-border);
    border-radius: var(--sfc-radius);
    margin-top: 12px;
}

.sf-chat-form-avatar {
    flex-shrink: 0;
}

.sf-chat-form-avatar img {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: block;
    object-fit: cover;
}

.sf-chat-form-input-wrap {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    align-items: flex-end;
    gap: 6px;
    background: var(--sfc-bg-page);
    border: 1px solid var(--sfc-border);
    border-radius: var(--sfc-radius-sm);
    padding: 6px 10px;
}

.sf-chat-form-textarea {
    flex: 1 1 auto;
    border: 0;
    background: transparent;
    color: var(--sfc-text-primary);
    font-family: inherit;
    font-size: 14px;
    line-height: 1.4;
    resize: none;
    padding: 6px 4px;
    margin: 0;
    width: 100%;
    max-height: 120px;
    overflow-y: auto;
    box-shadow: none;
    outline: none;
}

.sf-chat-form-textarea:focus {
    outline: none;
    box-shadow: none;
}

.sf-chat-form-attach {
    display: inline-flex;
    align-items: center;
    cursor: pointer;
    color: var(--sfc-text-muted);
    padding: 6px;
    border-radius: 50%;
    flex-shrink: 0;
    transition: background-color 0.12s ease, color 0.12s ease;
}

.sf-chat-form-attach:hover {
    background: var(--sfc-bg-hover);
    color: var(--sfc-text-primary);
}

.sf-chat-form-attach input[type="file"] {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    pointer-events: none;
}

.sf-chat-form-attach-icon {
    font-size: 16px;
    line-height: 1;
}

.sf-chat-form-attach.has-file {
    color: var(--sfc-link);
    background: rgba(91, 159, 230, 0.12);
}

/* Specificità alta per vincere su Youzify: body #youzify button[type=submit]
   ha specificità 1,1,2 con !important. Servono selettori con #youzify (ID). */
body #youzify .sf-chat-form .sf-chat-form-submit,
body #youzify .sf-chat-form button.sf-chat-form-submit,
.sf-chat-form .sf-chat-form-submit,
.sf-chat-form button.sf-chat-form-submit {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background-color: var(--sfc-accent) !important;
    color: #ffffff !important;
    border: none !important;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.12s ease, transform 0.08s ease;
    padding: 0;
}

body #youzify .sf-chat-form .sf-chat-form-submit:hover,
body #youzify .sf-chat-form .sf-chat-form-submit:focus,
.sf-chat-form .sf-chat-form-submit:hover,
.sf-chat-form .sf-chat-form-submit:focus {
    background-color: var(--sfc-accent-hover) !important;
}

.sf-chat-form-submit:active {
    transform: scale(0.95);
}

.sf-chat-form-submit:disabled {
    background: var(--sfc-text-muted);
    cursor: not-allowed;
}

.screen-reader-text {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ==========================================================================
   Mobile (≤ 767px)
   ========================================================================== */

@media (max-width: 767px) {
    .sf-chat-message {
        gap: 10px;
        padding: 4px 8px;
    }
    .sf-chat-message-avatar { width: 36px; }
    .sf-chat-message-avatar--spacer { width: 36px; }
    .sf-chat-message-avatar img { width: 36px; height: 36px; }

    .sf-chat-form { padding: 8px; gap: 8px; }
    .sf-chat-form-avatar { display: none; }
    .sf-chat-form-input-wrap { padding: 4px 8px; }
    .sf-chat-form-submit { width: 36px; height: 36px; }
}

/* ==========================================================================
   Reduced motion
   ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    .sf-chat-message,
    .sf-chat-form-attach,
    .sf-chat-form-submit {
        transition: none;
    }
    .sf-chat-form-submit:active { transform: none; }
}
