/* =====================================================================
 *  線上客服對話框（前台）
 *
 *  色票一律吃佈景的 token（--brand 等），後面的備援值是給「這個外掛被裝在
 *  別的佈景上」的情況用的 —— 不是裝飾，少了它在預設佈景上會變成黑白一片。
 *
 *  深色模式不另外寫一組：佈景已經在 :root / [data-theme] 三層規則裡
 *  把 token 換掉了（見 themes/rent858/style.css 的註解），這裡只要用 token
 *  就會跟著變。唯一要自己處理的是對話泡泡的底色，因為那不是佈景的 token。
 * ================================================================== */

.r858-chat {
    position: fixed;
    right: 18px;
    bottom: 22px;
    z-index: 9000;              /* 比「回到頂端」(900) 高，比 WP 管理列低 */
    font-family: var(--font, -apple-system, "Segoe UI", "Microsoft JhengHei", sans-serif);
    font-size: 15px;
    line-height: 1.55;
}

/* 佈景的「回到頂端」也在右下角。有對話框時把它往上推，兩顆不要疊在一起。
   （body class 由 Rent858_Chat::body_class() 加上） */
body.r858-has-chat .r858-totop { bottom: 84px; }

/* ---------- 呼叫按鈕 ---------- */

.r858-chat__fab {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 0 18px 0 14px;
    height: 52px;
    border: 0;
    border-radius: 26px;
    background: var(--brand, #F15A23);
    color: #fff;
    font: inherit;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 6px 18px rgba(0, 0, 0, .22);
    position: relative;
}

.r858-chat__fab:hover,
.r858-chat__fab:focus-visible { filter: brightness(1.07); }
.r858-chat__fab:focus-visible { outline: 2px solid var(--ink, #222); outline-offset: 2px; }
.r858-chat__fab-icon { display: flex; }

.r858-chat__badge {
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 22px;
    height: 22px;
    padding: 0 6px;
    border-radius: 11px;
    background: #d6002d;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    line-height: 22px;
    text-align: center;
    box-shadow: 0 0 0 2px #fff;
}

.r858-chat[data-state="open"] .r858-chat__fab { display: none; }

/* ---------- 面板 ---------- */

.r858-chat__panel {
    display: flex;
    flex-direction: column;
    width: 370px;
    height: min(560px, calc(100vh - 120px));
    border: 1px solid var(--line, #ddd);
    border-radius: 12px;
    background: var(--surface, #fff);
    color: var(--ink, #222);
    box-shadow: 0 16px 48px rgba(0, 0, 0, .26);
    overflow: hidden;
}

.r858-chat__panel[hidden] { display: none; }

.r858-chat__head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 8px 10px 14px;
    background: var(--brand, #F15A23);
    color: #fff;
}

.r858-chat__who { display: flex; flex-direction: column; min-width: 0; }
.r858-chat__who strong { font-size: 15px; }

.r858-chat__status {
    font-size: 12px;
    opacity: .95;
    display: flex;
    align-items: center;
    gap: 5px;
}

.r858-chat__status::before {
    content: "";
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: rgba(255, 255, 255, .55);
}

.r858-chat__status[data-status="online"]::before  { background: #4ade80; }
.r858-chat__status[data-status="offline"]::before { background: #fcd34d; }

.r858-chat__close {
    flex: 0 0 auto;
    width: 34px;
    height: 34px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: #fff;
    cursor: pointer;
}

.r858-chat__close:hover { background: rgba(255, 255, 255, .18); }

/* ---------- 訊息串 ---------- */

.r858-chat__log {
    flex: 1 1 auto;
    min-height: 0;              /* overflow-y 已經讓自動最小高度歸零，寫明是怕日後有人拿掉它 */
    overflow-y: auto;
    overscroll-behavior: contain;   /* 滑到底不要帶著整頁一起捲 */
    padding: 12px 12px 4px;
    background: var(--page-bg, #eee);
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.r858-chat__log:focus-visible { outline: 2px solid var(--brand, #F15A23); outline-offset: -2px; }

.r858-chat__day {
    align-self: center;
    margin: 10px 0 6px;
    padding: 2px 10px;
    border-radius: 10px;
    background: rgba(0, 0, 0, .07);
    color: var(--muted, #999);
    font-size: 11px;
}

.r858-chat__msg {
    max-width: 82%;
    margin-top: 8px;
    padding: 8px 12px;
    border-radius: 14px;
    white-space: pre-wrap;      /* 客人打的換行要留著 */
    overflow-wrap: anywhere;    /* 貼一串超長網址不要把面板撐寬 */
}

.r858-chat__msg--visitor {
    align-self: flex-end;
    background: var(--brand, #F15A23);
    color: #fff;
    border-bottom-right-radius: 4px;
}

.r858-chat__msg--staff {
    align-self: flex-start;
    background: var(--surface, #fff);
    border: 1px solid var(--line-soft, #eaeaea);
    border-bottom-left-radius: 4px;
}

.r858-chat__msg--system {
    align-self: center;
    max-width: 94%;
    background: transparent;
    border: 1px dashed var(--line, #ddd);
    color: var(--ink-soft, #333);
    font-size: 13.5px;
    text-align: left;
}

.r858-chat__msg--pending { opacity: .55; }

/* 訊息裡的連結（由 chat.js 的 writeBody 產生）。
   三種泡泡底色不同，顏色要分開給，不然橘底上的藍字看不見。 */
.r858-chat__msg a:not(.r858-chat__card) {
    color: inherit;
    text-decoration: underline;
    text-underline-offset: 2px;
    word-break: break-all;      /* 長網址不要把面板撐寬 */
}

.r858-chat__msg--staff a:not(.r858-chat__card),
.r858-chat__msg--system a:not(.r858-chat__card)  { color: var(--price, #3E7CB4); }
.r858-chat__msg--visitor a:not(.r858-chat__card) { color: #fff; }

.r858-chat__msg > img {
    display: block;
    max-width: 100%;
    max-height: 260px;          /* 直式截圖不要吃掉整個面板，點一下可放大 */
    width: auto;
    height: auto;
    border-radius: 8px;
    cursor: zoom-in;
}

.r858-chat__meta {
    align-self: flex-end;
    margin: 2px 2px 0;
    color: var(--muted, #999);
    font-size: 11px;
}

.r858-chat__meta--staff { align-self: flex-start; }

.r858-chat__empty {
    margin: auto 0;
    padding: 18px 10px;
    white-space: pre-line;
    text-align: center;
    color: var(--ink-soft, #333);
    font-size: 14px;
}

/* ---------- 商品卡片 ---------- */

/* 客服貼自家商品連結時，伺服器會回 m.card，這裡把它畫成卡片。
   卡片是白底的，疊在橘色的客人泡泡上也看得清楚。 */
.r858-chat__card {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 6px;
    padding: 8px;
    max-width: 100%;
    border: 1px solid var(--line, #ddd);
    border-radius: 10px;
    background: var(--surface, #fff);
    color: var(--ink, #222);
    text-decoration: none;
    white-space: normal;        /* 泡泡是 pre-wrap，卡片裡不要照著斷行 */
}

.r858-chat__card:hover { border-color: var(--brand, #F15A23); }

.r858-chat__card img {
    flex: 0 0 auto;
    width: 56px;
    height: 56px;
    object-fit: cover;          /* 直式與橫式商品圖都切成同樣大小 */
    border-radius: 6px;
    background: var(--page-bg, #eee);
}

.r858-chat__card-name {
    min-width: 0;
    font-size: 13.5px;
    font-weight: 600;
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;      /* 品名最多兩行，長的切掉 */
    -webkit-box-orient: vertical;
    overflow: hidden;
}

/* 整則訊息就是那條網址時，泡泡自己退場，只留卡片 */
.r858-chat__msg--cardonly {
    padding: 0;
    background: transparent;
    border: 0;
}

.r858-chat__msg--cardonly .r858-chat__card { margin-top: 0; }

/* ---------- 對方正在輸入 ---------- */

.r858-chat__typing {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 4px 14px 6px;
    background: var(--page-bg, #eee);
    color: var(--muted, #999);
    font-size: 12px;
}

.r858-chat__typing[hidden] { display: none; }

.r858-chat__typing span {
    width: 5px;
    height: 5px;
    border-radius: 50%;
    background: currentColor;
}

@media (prefers-reduced-motion: no-preference) {
    .r858-chat__typing span { animation: r858-chat-dot 1.1s infinite ease-in-out; }
    .r858-chat__typing span:nth-child(2) { animation-delay: .15s; }
    .r858-chat__typing span:nth-child(3) { animation-delay: .3s; }
}

@keyframes r858-chat-dot {
    0%, 60%, 100% { opacity: .3; }
    30%           { opacity: 1; }
}

/* ---------- 快速問題 ---------- */

.r858-chat__quick {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 8px 12px;
    background: var(--page-bg, #eee);
    border-top: 1px solid var(--line-soft, #eaeaea);
}

.r858-chat__quick[hidden] { display: none; }

.r858-chat__quick button {
    padding: 5px 12px;
    border: 1px solid var(--brand, #F15A23);
    border-radius: 14px;
    background: var(--surface, #fff);
    color: var(--brand, #F15A23);
    font: inherit;
    font-size: 13px;
    cursor: pointer;
}

.r858-chat__quick button:hover { background: var(--brand, #F15A23); color: #fff; }

/* ---------- 聯絡資料 ---------- */

.r858-chat__contact {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    padding: 10px 12px;
    background: var(--surface, #fff);
    border-top: 1px solid var(--line-soft, #eaeaea);
    font-size: 12px;
}

.r858-chat__contact[hidden] { display: none; }
.r858-chat__contact label { display: flex; flex-direction: column; gap: 3px; color: var(--muted, #999); }

.r858-chat__contact input {
    padding: 6px 8px;
    border: 1px solid var(--line, #ddd);
    border-radius: 6px;
    background: var(--surface, #fff);
    color: var(--ink, #222);
    font: inherit;
    font-size: 14px;
    min-width: 0;
}

/* ---------- 輸入列 ---------- */

.r858-chat__composer {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 8px;
    background: var(--surface, #fff);
    border-top: 1px solid var(--line, #ddd);
}

.r858-chat__upload,
.r858-chat__send {
    flex: 0 0 auto;
    width: 38px;
    height: 38px;
    display: grid;
    place-items: center;
    border: 0;
    border-radius: 50%;
    background: transparent;
    color: var(--muted, #999);
    cursor: pointer;
}

.r858-chat__upload:hover { color: var(--brand, #F15A23); }

.r858-chat__send {
    background: var(--brand, #F15A23);
    color: #fff;
}

.r858-chat__send:disabled { opacity: .45; cursor: default; }

.r858-chat__input {
    flex: 1 1 auto;
    min-width: 0;
    max-height: 120px;
    padding: 9px 10px;
    border: 1px solid var(--line, #ddd);
    border-radius: 10px;
    background: var(--surface, #fff);
    color: var(--ink, #222);
    font: inherit;
    resize: none;
    overflow-y: auto;
}

.r858-chat__input:focus { outline: 0; border-color: var(--brand, #F15A23); }

.r858-chat__foot {
    padding: 0 12px 8px;
    background: var(--surface, #fff);
    color: var(--muted, #999);
    font-size: 11.5px;
    text-align: center;
}

.r858-chat__foot:empty { display: none; }
.r858-chat__foot a { color: var(--price, #3E7CB4); }
.r858-chat__foot .r858-chat__err { color: #d6002d; }

/* ---------- 放大看圖 ---------- */

.r858-chat__lightbox {
    position: fixed;
    inset: 0;
    z-index: 9100;
    display: grid;
    place-items: center;
    /* place-items: center 的格子預設是 auto，會長到圖片的原始大小，
       於是 img 的 max-height: 100% 是以「2532px」為基準 —— 等於沒有限制。
       minmax(0, 1fr) 把格子綁回這個 fixed 區塊的大小，限制才會生效。 */
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 1fr);
    padding: 20px;
    background: rgba(0, 0, 0, .82);
    border: 0;
    cursor: zoom-out;
}

.r858-chat__lightbox img {
    max-width: 100%;
    max-height: 100%;
    border-radius: 6px;
}

/* ---------- 手機 ---------- */

@media (max-width: 480px) {

    .r858-chat { right: 12px; bottom: 12px; left: 12px; }

    .r858-chat__fab { float: right; }

    /* 全幅的下拉面板。用 dvh：iOS Safari 的網址列會吃掉 vh，
       底部的輸入列會被推到看不見的地方。 */
    .r858-chat__panel {
        width: auto;
        height: min(78dvh, calc(100dvh - 90px));
        border-radius: 14px;
    }

    .r858-chat__msg { max-width: 86%; }
    .r858-chat__contact { grid-template-columns: 1fr; }
}

/* 客服已讀的標記 */
.r858-chat__read { color: var(--brand, #F15A23); font-weight: 600; }
