/* =====================================================================
   GA Chat (ADR-038). Built on the portal's tokens (static/css/base.css):
   the desk and its paper, charcoal ink, the Gulf orange accent. Phone
   first (390 px); list and conversation side by side from 900 px.
   ===================================================================== */

/* --- The app frame -------------------------------------------------- */

/* hidden always wins over a component's display (the portal's .btn is inline-flex). */
.chat-body [hidden] {
    display: none !important;
}

html:has(body.chat-body--app),
body.chat-body--app {
    height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
}

body.chat-body {
    background: var(--desk);
    min-height: 0;
    -webkit-tap-highlight-color: transparent;
}

.chat-app {
    position: fixed;
    inset: 0 0 auto 0;
    /* chat.js keeps this at the visible height, above the keyboard. */
    height: var(--chat-height, 100dvh);
    display: flex;
    background: var(--desk);
    overflow: hidden;
}

.chat-list-pane,
.chat-convo-pane {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
}

.chat-list-pane {
    flex: 1;
    background: var(--paper);
}

.chat-convo-pane {
    display: none;
    position: relative;
    flex: 1;
}

.chat-app.has-conversation .chat-list-pane {
    display: none;
}

.chat-app.has-conversation .chat-convo-pane {
    display: flex;
}

.chat-convo-empty {
    display: none;
}

@media (min-width: 900px) {
    .chat-list-pane,
    .chat-app.has-conversation .chat-list-pane {
        display: flex;
        flex: 0 0 360px;
        border-right: 1px solid var(--rule-solid);
    }

    .chat-convo-pane {
        display: flex;
    }

    .chat-app:not(.has-conversation) .chat-convo-pane > :not(.chat-convo-empty) {
        display: none;
    }

    .chat-app:not(.has-conversation) .chat-convo-empty {
        display: flex;
        flex: 1;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        gap: var(--space-3);
        color: var(--text-3);
    }

    .chat-convo-empty .icon {
        width: 40px;
        height: 40px;
    }

    .chat-back {
        display: none !important;
    }
}

/* --- Bars ----------------------------------------------------------- */

.chat-bar {
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: var(--space-1);
    min-height: 56px;
    padding: env(safe-area-inset-top) var(--space-2) 0;
    background: var(--paper);
    border-bottom: 1px solid var(--rule-solid);
}

.chat-bar__title {
    flex: 1;
    margin: 0 var(--space-2);
    font-size: var(--text-lg);
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-bar__mark {
    margin: 0 var(--space-2);
}

.chat-bar__who {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 4px;
    border: 0;
    border-radius: var(--radius-md);
    background: none;
    color: inherit;
    font: inherit;
    text-align: left;
    cursor: pointer;
}

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

.chat-bar__who .chat-bar__title {
    margin: 0;
    font-size: var(--text-md);
}

.chat-bar__sub {
    font-size: var(--text-xs);
    color: var(--text-3);
}

.chat-icon-btn {
    position: relative;
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: 0;
    border-radius: var(--radius-full);
    background: none;
    color: var(--ink-700);
    cursor: pointer;
    text-decoration: none;
}

.chat-icon-btn:hover {
    background: var(--ink-100);
    color: var(--ink-950);
}

.chat-icon-btn .icon,
.chat-send .icon,
.chat-menu-item .icon,
.chat-reply-strip .icon {
    width: 22px;
    height: 22px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.chat-icon-btn--sm {
    width: 36px;
    height: 36px;
}

.chat-icon-btn--accent {
    background: var(--ink-800);
    color: #fff;
    width: 40px;
    height: 40px;
    margin-left: var(--space-1);
}

.chat-icon-btn--accent:hover {
    background: var(--ink-950);
    color: #fff;
}

.chat-back__count {
    position: absolute;
    top: 4px;
    right: -2px;
    min-width: 18px;
    padding: 0 5px;
    border-radius: var(--radius-full);
    background: var(--sun-500);
    color: var(--ink-950);
    font-size: 11px;
    font-weight: 600;
    line-height: 18px;
}

/* --- Avatars and the list of chats ------------------------------------ */

.chat-avatar {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 46px;
    height: 46px;
    border-radius: var(--radius-full);
    background: var(--ink-100);
    color: var(--ink-700);
    font-weight: 600;
    font-size: var(--text-sm);
    letter-spacing: 0.02em;
}

.chat-avatar--group {
    background: var(--sun-100);
    color: var(--sun-700);
}

.chat-avatar .icon {
    width: 24px;
    height: 24px;
    fill: none;
    stroke: currentColor;
    stroke-width: 1.6;
}

.chat-avatar--sm {
    width: 36px;
    height: 36px;
    font-size: var(--text-xs);
}

.chat-avatar--sm .icon {
    width: 20px;
    height: 20px;
}

.chat-avatar:empty {
    display: none;
}

.chat-rows {
    flex: 1;
    margin: 0;
    padding: 0;
    list-style: none;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.chat-row {
    display: grid;
    grid-template-columns: auto 1fr auto;
    align-items: center;
    gap: var(--space-3);
    padding: 10px var(--space-4);
    color: inherit;
    text-decoration: none;
    border-bottom: 1px solid var(--rule);
}

.chat-row:hover,
.chat-row.is-active {
    background: var(--paper-2);
    color: inherit;
}

.chat-row.is-active {
    box-shadow: inset 3px 0 0 var(--sun-500);
}

.chat-row__main {
    min-width: 0;
}

.chat-row__title,
.chat-row__preview {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-row__title {
    font-size: var(--text-md);
    font-weight: 600;
}

.chat-row__preview {
    color: var(--text-2);
    font-size: var(--text-sm);
}

.chat-row__side {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    font-size: var(--text-xs);
    color: var(--text-3);
}

.chat-row.has-unread .chat-row__time {
    color: var(--sun-700);
    font-weight: 600;
}

.chat-row.has-unread .chat-row__preview {
    color: var(--text);
}

.chat-pill {
    min-width: 22px;
    padding: 0 7px;
    border-radius: var(--radius-full);
    background: var(--sun-500);
    color: var(--ink-950);
    font-size: 12px;
    font-weight: 600;
    line-height: 22px;
    text-align: center;
}

.chat-empty,
.chat-status {
    margin: var(--space-6) var(--space-5);
    color: var(--text-2);
    text-align: center;
}

.chat-status {
    margin: var(--space-2);
    font-size: var(--text-xs);
}

.chat-banner {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin: var(--space-3);
    padding: var(--space-3) var(--space-4);
    border: 1px solid var(--sun-200);
    border-radius: var(--radius-lg);
    background: var(--sun-50);
    font-size: var(--text-sm);
}

.chat-banner.is-tappable {
    cursor: pointer;
}

.chat-banner__actions {
    display: flex;
    gap: var(--space-2);
}

/* --- The conversation -------------------------------------------------- */

.chat-scroll {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    overflow-anchor: none; /* chat.js keeps the place itself, the same in every browser */
    padding: var(--space-2) var(--space-3) var(--space-3);
}

.chat-older {
    margin: var(--space-2) 0;
    text-align: center;
    font-size: var(--text-xs);
    color: var(--text-3);
}

.chat-messages {
    display: flex;
    flex-direction: column;
    margin: 0;
    padding: 0;
    list-style: none;
}

.chat-day {
    align-self: center;
    margin: var(--space-4) 0 var(--space-2);
    padding: 3px 12px;
    border-radius: var(--radius-full);
    background: var(--paper);
    box-shadow: var(--shadow-xs);
    color: var(--text-2);
    font-size: var(--text-xs);
    font-weight: 500;
}

.chat-msg {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    max-width: min(82%, 560px);
    margin-top: 2px;
    touch-action: pan-y;
    transition: transform var(--dur) var(--ease);
}

.chat-msg.is-first {
    margin-top: var(--space-2);
}

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

.chat-msg.is-flash .chat-bubble {
    box-shadow: 0 0 0 3px var(--sun-200);
}

.chat-msg__who {
    margin: 0 0 2px 12px;
    font-size: var(--text-xs);
    font-weight: 600;
    color: var(--sky-700);
}

@media (pointer: coarse) {
    /* A held message opens Reply and Copy instead of selecting text. */
    .chat-bubble {
        user-select: none !important;
        -webkit-user-select: none !important;
        -webkit-touch-callout: none;
    }
}

.chat-bubble {
    position: relative;
    min-width: 64px;
    max-width: 100%;
    padding: 6px 10px 5px;
    border: 1px solid var(--rule-solid);
    border-radius: 16px;
    background: var(--paper);
    box-shadow: var(--shadow-xs);
    font-size: 15px;
    line-height: 1.4;
    overflow-wrap: anywhere;
    user-select: text;
    -webkit-user-select: text;
}

.chat-msg.is-first:not(.is-mine) .chat-bubble {
    border-top-left-radius: 5px;
}

.chat-msg.is-first.is-mine .chat-bubble {
    border-top-right-radius: 5px;
}

.chat-msg.is-mine .chat-bubble {
    background: var(--ink-800);
    border-color: var(--ink-800);
    color: var(--on-ink);
}

.chat-bubble a {
    color: inherit;
    text-decoration-color: currentColor;
}

.chat-text {
    white-space: normal;
}

.chat-time {
    float: right;
    margin: 7px 0 -3px 10px;
    font-size: 11px;
    line-height: 1;
    color: var(--text-3);
    white-space: nowrap;
}

.chat-msg.is-mine .chat-time {
    color: var(--on-ink-3);
}

.chat-msg.is-sending .chat-bubble {
    opacity: 0.72;
}

.chat-msg.is-failed .chat-bubble {
    background: var(--rose-50);
    border-color: var(--rose-200);
    color: var(--text);
}

.chat-msg.is-failed .chat-time {
    color: var(--rose-700);
}

.chat-retry {
    margin: 4px 4px 0;
    padding: 4px 10px;
    border: 1px solid var(--rose-200);
    border-radius: var(--radius-full);
    background: var(--paper);
    color: var(--rose-700);
    font: inherit;
    font-size: var(--text-xs);
    font-weight: 600;
    cursor: pointer;
}

.chat-quote {
    display: block;
    width: 100%;
    margin: 0 0 4px;
    padding: 4px 8px;
    border: 0;
    border-left: 3px solid var(--sun-500);
    border-radius: 6px;
    background: rgba(47, 50, 64, 0.06);
    color: inherit;
    font: inherit;
    font-size: var(--text-sm);
    text-align: left;
    cursor: pointer;
}

.chat-msg.is-mine .chat-quote {
    background: rgba(255, 255, 255, 0.12);
}

.chat-quote strong {
    display: block;
    font-size: var(--text-xs);
}

.chat-quote span {
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
    opacity: 0.85;
}

.chat-tiles {
    display: grid;
    grid-template-columns: repeat(var(--tiles, 1), minmax(0, 1fr));
    gap: 4px;
    margin: 0 -6px 4px;
}

.chat-tile {
    display: block;
    padding: 0;
    border: 0;
    border-radius: 12px;
    overflow: hidden;
    background: var(--well);
    cursor: zoom-in;
}

.chat-tile img {
    display: block;
    width: 100%;
    max-width: 280px;
    height: auto; /* the width and height attributes give the shape before it loads */
    max-height: 360px;
    object-fit: cover;
}

.chat-tiles[style*="--tiles: 2"] .chat-tile img {
    aspect-ratio: 1;
}

.chat-file {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0 -4px 4px;
    padding: 8px 10px;
    border-radius: 10px;
    background: rgba(47, 50, 64, 0.06);
    color: inherit;
    text-decoration: none;
}

.chat-msg.is-mine .chat-file {
    background: rgba(255, 255, 255, 0.12);
}

.chat-file:hover {
    color: inherit;
}

.chat-file__thumb {
    flex: 0 0 auto;
    width: 40px;
    height: 52px;
    border-radius: 4px;
    object-fit: cover;
    background: var(--paper);
}

.chat-file__icon {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 8px;
    background: var(--paper);
    color: var(--ink-700);
    font-size: 10px;
    font-weight: 700;
}

.chat-file__name {
    display: block;
    font-weight: 600;
    font-size: var(--text-sm);
    overflow-wrap: anywhere;
}

.chat-file__size {
    font-size: var(--text-xs);
    opacity: 0.75;
}

.chat-new-pill {
    position: absolute;
    left: 50%;
    bottom: calc(var(--composer-height, 64px) + 12px);
    transform: translateX(-50%);
    padding: 8px 16px;
    border: 0;
    border-radius: var(--radius-full);
    background: var(--ink-800);
    color: #fff;
    font: inherit;
    font-size: var(--text-sm);
    font-weight: 600;
    box-shadow: var(--shadow-md);
    cursor: pointer;
}

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

.chat-composer {
    position: relative;
    flex: 0 0 auto;
    padding: 6px 6px calc(6px + env(safe-area-inset-bottom));
    background: var(--paper);
    border-top: 1px solid var(--rule-solid);
}

.chat-composer__row {
    display: flex;
    align-items: flex-end;
    gap: 4px;
}

.chat-input {
    flex: 1;
    min-height: 42px;
    max-height: 128px; /* five lines */
    padding: 10px 14px;
    border: 1px solid var(--rule-strong);
    border-radius: 21px;
    background: var(--well);
    color: var(--text);
    font: inherit;
    font-size: 16px; /* 16 px or more: iPhones do not zoom in on focus */
    line-height: 20px;
    resize: none;
    overflow-y: auto;
}

.chat-input:focus {
    outline: none;
    border-color: var(--ink-500);
    background: var(--paper);
}

.chat-send {
    flex: 0 0 auto;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border: 0;
    border-radius: var(--radius-full);
    background: var(--ink-800);
    color: #fff;
    cursor: pointer;
}

.chat-send:disabled {
    background: var(--ink-300);
    cursor: default;
}

.chat-reply-strip {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin: 0 4px 6px;
    padding: 6px 4px 6px 10px;
    border-left: 3px solid var(--sun-500);
    border-radius: 8px;
    background: var(--well);
    color: var(--text-2);
}

.chat-reply-strip__text {
    flex: 1;
    min-width: 0;
    font-size: var(--text-sm);
}

.chat-reply-strip__text strong {
    display: block;
    color: var(--text);
    font-size: var(--text-xs);
}

.chat-reply-strip__text span {
    display: block;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.chat-picked {
    display: flex;
    gap: 6px;
    margin: 0 4px 6px;
    padding: 0;
    list-style: none;
    overflow-x: auto;
}

.chat-picked li {
    position: relative;
    flex: 0 0 auto;
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 180px;
    height: 56px;
    padding: 0 30px 0 6px;
    border: 1px solid var(--rule-solid);
    border-radius: 10px;
    background: var(--well);
    font-size: var(--text-xs);
}

.chat-picked img {
    width: 44px;
    height: 44px;
    border-radius: 6px;
    object-fit: cover;
}

.chat-picked span {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.chat-picked button {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 26px;
    height: 26px;
    border: 0;
    border-radius: var(--radius-full);
    background: none;
    color: var(--text-2);
    font-size: 18px;
    line-height: 1;
    cursor: pointer;
}

.chat-mentions {
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: calc(100% + 4px);
    max-height: 220px;
    margin: 0;
    padding: 4px;
    overflow-y: auto;
    list-style: none;
    border: 1px solid var(--rule-solid);
    border-radius: var(--radius-lg);
    background: var(--paper);
    box-shadow: var(--shadow-md);
}

.chat-mentions li {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: 8px 10px;
    border-radius: var(--radius-md);
    cursor: pointer;
}

.chat-mentions li[aria-selected="true"],
.chat-mentions li:hover {
    background: var(--ink-100);
}

/* --- Sheets, the photo viewer and the toast ----------------------------- */

.chat-sheet {
    width: 100%;
    max-width: 100%;
    max-height: 85dvh;
    margin: auto 0 0;
    padding: 0;
    border: 0;
    border-radius: 18px 18px 0 0;
    background: var(--paper);
    color: var(--text);
    box-shadow: var(--shadow-lg);
}

.chat-sheet::backdrop {
    background: rgba(21, 22, 28, 0.42);
}

@media (min-width: 900px) {
    .chat-sheet {
        width: 420px;
        margin: auto;
        border-radius: var(--radius-xl);
    }
}

.chat-sheet__body {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4) calc(var(--space-4) + env(safe-area-inset-bottom));
}

.chat-sheet__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
}

.chat-sheet__head h2 {
    font-size: var(--text-lg);
}

.chat-sheet__sub {
    margin: 0;
    color: var(--text-2);
}

.chat-sheet__menu {
    gap: 2px;
    padding-top: var(--space-2);
}

.chat-menu-item {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    min-height: 52px;
    padding: 0 var(--space-3);
    border: 0;
    border-radius: var(--radius-md);
    background: none;
    color: var(--text);
    font: inherit;
    font-size: var(--text-md);
    text-align: left;
    cursor: pointer;
}

.chat-menu-item:hover {
    background: var(--ink-100);
}

.chat-menu-item--cancel {
    justify-content: center;
    margin-top: var(--space-2);
    background: var(--well);
    font-weight: 600;
}

.chat-search {
    width: 100%;
    height: 44px;
    padding: 0 14px;
    border: 1px solid var(--rule-strong);
    border-radius: var(--radius-lg);
    background: var(--well);
    font: inherit;
    font-size: 16px;
}

.chat-people {
    margin: 0;
    padding: 0;
    list-style: none;
    overflow-y: auto;
    max-height: 55dvh;
}

.chat-people li > button,
.chat-people li > span {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    width: 100%;
    padding: 8px 4px;
    border: 0;
    background: none;
    color: inherit;
    font: inherit;
    font-size: var(--text-md);
    text-align: left;
}

.chat-people li > button {
    cursor: pointer;
    border-radius: var(--radius-md);
}

.chat-people li > button:hover {
    background: var(--ink-100);
}

.chat-setting h3 {
    margin-bottom: var(--space-2);
    font-size: var(--text-md);
}

.chat-setting p {
    margin: 0 0 var(--space-2);
    color: var(--text-2);
}

.chat-viewer {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(10, 10, 14, 0.94);
}

.chat-viewer[hidden] {
    display: none;
}

.chat-viewer img {
    max-width: 100%;
    max-height: 100%;
    object-fit: contain;
}

.chat-viewer__close {
    position: absolute;
    top: calc(env(safe-area-inset-top) + 8px);
    right: 8px;
    color: #fff;
}

.chat-viewer__close:hover {
    background: rgba(255, 255, 255, 0.14);
    color: #fff;
}

.chat-toast {
    position: fixed;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom) + 90px);
    z-index: 60;
    transform: translateX(-50%);
    max-width: calc(100% - 32px);
    padding: 10px 16px;
    border-radius: var(--radius-lg);
    background: var(--ink-900);
    color: #fff;
    font-size: var(--text-sm);
    box-shadow: var(--shadow-md);
}

/* --- Plain pages: sign-in, errors, group admin ---------------------------- */

.chat-page {
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
}

.chat-page__main {
    width: 100%;
    max-width: 640px;
    margin: 0 auto;
    padding: var(--space-4);
}

.chat-card {
    margin-bottom: var(--space-4);
    padding: var(--space-4);
    border: 1px solid var(--rule-solid);
    border-radius: var(--radius-lg);
    background: var(--paper);
    box-shadow: var(--shadow-xs);
}

.chat-card__title {
    margin-bottom: var(--space-3);
    font-size: var(--text-lg);
}

.chat-form {
    display: flex;
    flex-direction: column;
    gap: var(--space-3);
}

.chat-form input[type="text"],
.chat-form input[type="email"],
.chat-form input[type="password"] {
    width: 100%;
    height: 44px;
    font-size: 16px;
}

.chat-form--inline {
    flex-direction: row;
    align-items: center;
}

.chat-checks {
    margin: 0;
    padding: 0;
    border: 0;
}

.chat-checks legend {
    margin-bottom: var(--space-2);
    font-weight: 600;
}

.chat-check {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    min-height: 40px;
    font-size: var(--text-md);
}

.chat-check input {
    width: 20px;
    height: 20px;
}

.chat-admin-list,
.chat-admin-members {
    margin: 0;
    padding: 0;
    list-style: none;
}

.chat-admin-list a {
    display: flex;
    flex-direction: column;
    padding: var(--space-3) 0;
    border-bottom: 1px solid var(--rule);
    text-decoration: none;
}

.chat-admin-list span {
    color: var(--text-2);
    font-size: var(--text-sm);
}

.chat-admin-members li {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    min-height: 44px;
    border-bottom: 1px solid var(--rule);
}

.chat-note {
    padding: var(--space-3);
    border-radius: var(--radius-md);
    background: var(--amber-50);
    color: var(--amber-700);
}
