/* Direct messages (الرسائل) — shared by the teacher and student chat pages.
 *
 * Everything is keyed off the base.css tokens, never literal colors, so a
 * teacher's dashboard palette (dashboard-theme.js) recolors this page too.
 *
 * Layout is a two-pane grid on desktop. On narrow screens both panes occupy the
 * same cell and `body.thread-open` swaps which one is visible — a real
 * back-and-forth rather than a squeezed sidebar, which is how a phone user
 * actually reads a chat. */

.messages-app {
    display: grid;
    grid-template-columns: minmax(260px, 340px) 1fr;
    gap: 1rem;
    /* Fill the viewport under the header so only the message list scrolls. */
    height: calc(100vh - 140px);
    min-height: 420px;
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 1rem 1rem;
}

/* `display:grid`/`display:flex` below beat the UA's `[hidden]{display:none}`,
 * so every container the JS toggles needs an explicit rule. */
.messages-app[hidden],
.messaging-disabled[hidden],
.thread-empty[hidden],
.composer[hidden],
.conv-empty[hidden],
.thread-actions[hidden],
.attachment-preview[hidden],
.load-older[hidden] { display: none; }

.messages-pane {
    background: var(--card-background);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

/* ---------- conversation list ---------- */

.conv-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .5rem;
    padding: .85rem 1rem;
    border-bottom: 1px solid var(--border-color);
}

.conv-header h2 {
    font-size: 1rem;
    margin: 0;
    color: var(--text-primary);
}

.conv-list {
    overflow-y: auto;
    flex: 1;
    min-height: 0;
}

.conv-item {
    display: flex;
    align-items: center;
    gap: .7rem;
    width: 100%;
    padding: .75rem 1rem;
    background: none;
    border: none;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    text-align: start;
    font: inherit;
    color: var(--text-primary);
    transition: var(--transition);
}

.conv-item:hover { background: var(--hover-bg); }
.conv-item.active { background: var(--unread-bg); }

.conv-avatar {
    width: 44px;
    height: 44px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.msg-avatar-fallback {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: var(--btn-bg);
    color: var(--btn-text);
    font-weight: 700;
    font-size: .85rem;
}

.conv-body {
    display: flex;
    flex-direction: column;
    gap: .2rem;
    flex: 1;
    min-width: 0;
}

.conv-top {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: .5rem;
}

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

.conv-time {
    font-size: .72rem;
    color: var(--text-muted);
    flex-shrink: 0;
}

.conv-preview {
    font-size: .82rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.conv-unread {
    background: var(--error-color);
    color: #fff;
    font-size: .72rem;
    font-weight: 700;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
    flex-shrink: 0;
}

/* ---------- thread ---------- */

.thread-pane {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-height: 0;
}

.thread-pane[hidden] { display: none; }

.thread-header {
    display: flex;
    align-items: center;
    gap: .7rem;
    padding: .75rem 1rem;
    border-bottom: 1px solid var(--border-color);
}

.thread-avatar,
.thread-avatar img {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
    flex-shrink: 0;
}

.thread-avatar { display: inline-flex; }
.thread-avatar .msg-avatar-fallback { width: 42px; height: 42px; border-radius: 50%; }

.thread-meta { flex: 1; min-width: 0; }
.thread-meta h2 { margin: 0; font-size: 1rem; color: var(--text-primary); }
.thread-meta p { margin: 0; font-size: .8rem; color: var(--text-secondary); }

.thread-actions { display: flex; gap: .4rem; }

.thread-action {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-secondary);
    padding: .35rem .6rem;
    font: inherit;
    font-size: .8rem;
    cursor: pointer;
    transition: var(--transition);
}

.thread-action:hover { background: var(--hover-bg); color: var(--text-primary); }
.thread-action.danger:hover { border-color: var(--danger-color); color: var(--danger-color); }

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

.messages-list {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: .6rem;
    background: var(--bg-secondary);
}

.msg-row { display: flex; }
.msg-row.mine { justify-content: flex-end; }
.msg-row.theirs { justify-content: flex-start; }

.msg-bubble {
    max-width: min(78%, 520px);
    background: var(--card-background);
    border: 1px solid var(--border-color);
    border-radius: 14px;
    padding: .5rem .7rem;
    display: flex;
    flex-direction: column;
    gap: .3rem;
    word-break: break-word;
}

.msg-row.mine .msg-bubble {
    background: var(--btn-bg);
    border-color: transparent;
    color: var(--btn-text);
}

.msg-row.mine .msg-author-name,
.msg-row.mine .msg-time { color: inherit; opacity: .85; }

.msg-author {
    display: flex;
    align-items: center;
    gap: .35rem;
    font-size: .75rem;
}

.msg-author-name { font-weight: 700; color: var(--accent-color); }

/* The whole point of the badge: a reply written by an assistant must never look
 * like the teacher wrote it. */
.msg-badge {
    background: var(--warning-color);
    color: #4a3a00;
    font-size: .65rem;
    font-weight: 700;
    padding: .05rem .35rem;
    border-radius: 6px;
}

.msg-text { margin: 0; font-size: .92rem; white-space: pre-wrap; }
.msg-time { font-size: .68rem; color: var(--text-muted); align-self: flex-end; }

.msg-image img {
    max-width: 100%;
    max-height: 260px;
    border-radius: 10px;
    display: block;
}

.msg-loading,
.msg-empty-thread {
    text-align: center;
    color: var(--text-muted);
    padding: 1.5rem 1rem;
    font-size: .9rem;
}

.load-older {
    align-self: center;
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: .3rem .9rem;
    font: inherit;
    font-size: .8rem;
    color: var(--text-secondary);
    cursor: pointer;
}

/* ---------- composer ---------- */

.composer {
    border-top: 1px solid var(--border-color);
    padding: .6rem .75rem;
    display: flex;
    align-items: flex-end;
    gap: .5rem;
    background: var(--card-background);
}

.composer textarea {
    flex: 1;
    resize: none;
    min-height: 42px;
    max-height: 140px;
    padding: .6rem .75rem;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    background: var(--input-bg);
    color: var(--text-primary);
    font: inherit;
    font-size: .92rem;
    line-height: 1.4;
}

.composer textarea:focus { outline: none; border-color: var(--btn-bg); }

.composer-btn {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    flex-shrink: 0;
    font-size: 1rem;
    transition: var(--transition);
}

.composer-btn.attach { background: var(--hover-bg); color: var(--text-secondary); }
.composer-btn.send { background: var(--btn-bg); color: var(--btn-text); }
.composer-btn:disabled { opacity: .5; cursor: default; }

.composer-notice {
    padding: .7rem 1rem;
    text-align: center;
    font-size: .85rem;
    color: var(--text-muted);
    border-top: 1px solid var(--border-color);
}

.attachment-preview {
    position: relative;
    align-self: center;
    flex-shrink: 0;
}

.attachment-preview img {
    width: 46px;
    height: 46px;
    object-fit: cover;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.attachment-remove {
    position: absolute;
    inset-block-start: -6px;
    inset-inline-end: -6px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    border: none;
    background: var(--danger-color);
    color: #fff;
    font-size: .7rem;
    cursor: pointer;
    line-height: 1;
}

/* ---------- empty / disabled states ---------- */

.thread-empty,
.messaging-disabled,
.conv-empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .5rem;
    flex: 1;
    padding: 2rem 1rem;
    text-align: center;
    color: var(--text-secondary);
}

.thread-empty i,
.messaging-disabled i,
.conv-empty i { font-size: 2.5rem; color: var(--btn-bg); opacity: .5; }

.messaging-disabled {
    max-width: 480px;
    margin: 3rem auto;
    background: var(--card-background);
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    padding: 2.5rem 1.5rem;
}

/* ---------- new-chat modal ---------- */

.msg-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, .5);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
}

.msg-modal-overlay[hidden] { display: none; }

.msg-modal {
    background: var(--card-background);
    border-radius: var(--border-radius);
    width: min(460px, 100%);
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.msg-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: .9rem 1rem;
    border-bottom: 1px solid var(--border-color);
}

.msg-modal-header h2 { margin: 0; font-size: 1rem; color: var(--text-primary); }

.msg-modal-close {
    background: none;
    border: none;
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    color: var(--text-secondary);
}

.msg-modal input[type="search"] {
    margin: .75rem 1rem;
    padding: .55rem .75rem;
    border: 1px solid var(--border-color);
    border-radius: 10px;
    background: var(--input-bg);
    color: var(--text-primary);
    font: inherit;
}

.contacts-list { overflow-y: auto; flex: 1; min-height: 0; }

.contact-item {
    display: flex;
    align-items: center;
    gap: .7rem;
    width: 100%;
    padding: .65rem 1rem;
    background: none;
    border: none;
    border-bottom: 1px solid var(--border-color);
    cursor: pointer;
    text-align: start;
    font: inherit;
    color: var(--text-primary);
}

.contact-item:hover { background: var(--hover-bg); }
.contact-item .conv-avatar { width: 38px; height: 38px; }

/* ---------- messaging settings modal ---------- */

.msg-settings-modal { padding: 0 1rem 1rem; }

.msg-settings-toggle {
    display: flex;
    align-items: center;
    gap: .6rem;
    margin-top: 1rem;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
}

.msg-settings-toggle input { width: 1.1rem; height: 1.1rem; }

.msg-settings-hint {
    margin: .6rem 0 0;
    font-size: .85rem;
    color: var(--text-secondary);
    line-height: 1.6;
}

/* ---------- mobile ---------- */

@media (max-width: 820px) {
    .messages-app {
        grid-template-columns: 1fr;
        height: calc(100vh - 120px);
        padding: 0 .5rem .5rem;
    }

    /* Both panes share the single cell; only one is shown at a time. */
    .messages-app > .messages-pane { grid-area: 1 / 1; }
    body.thread-open .conversations-pane { display: none; }
    body:not(.thread-open) .thread-pane-wrapper { display: none; }

    .back-to-list {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 34px;
        height: 34px;
        border-radius: 50%;
        border: 1px solid var(--border-color);
        background: none;
        color: var(--text-secondary);
        cursor: pointer;
        flex-shrink: 0;
    }

    .msg-bubble { max-width: 88%; }
}
