/* ==========================================================================
   KEMAP Live Chat — shared design system
   One stylesheet for every chat surface (customer widget, customer session,
   staff conversation, admin transcript). Replaces the four duplicated inline
   <style> blocks. Bootstrap/Soft-UI friendly; no Tailwind.
   ========================================================================== */

:root {
    --ke-green-900: #013a1e;
    --ke-green-700: #015a2e;
    --ke-green-600: #017a3e;
    --ke-green-500: #0a9d52;
    --volt: #ffc400;
    --volt-deep: #e0a800;
    --paper: #f4f6f3;
    --ink: #16241c;
    --ink-soft: #5b6b62;
    --line: #e4e9e4;
    --surface: #ffffff;
    --danger: #d6455d;

    --chat-radius: 16px;
    --chat-tail: 4px;
    --chat-shadow-soft: 0 1px 2px rgba(1, 58, 30, .06), 0 6px 18px rgba(1, 58, 30, .06);
    --chat-mono: ui-monospace, "SFMono-Regular", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

/* --- Thread (message canvas) ---------------------------------------------- */
.chat-thread {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 1rem 1rem 0.5rem;
    background: var(--paper);
    display: flex;
    flex-direction: column;
    gap: 2px;
    scroll-behavior: smooth;
}

/* utility data (meter/account/ticket/time) reads as utility data */
.chat-mono { font-family: var(--chat-mono); font-variant-numeric: tabular-nums; letter-spacing: -.01em; }

/* --- Day divider ---------------------------------------------------------- */
.chat-day {
    align-self: center;
    margin: 0.75rem 0 0.5rem;
    position: relative;
}
.chat-day > span {
    font-size: 0.68rem;
    font-weight: 600;
    color: var(--ink-soft);
    background: var(--paper);
    padding: 2px 12px;
    border: 1px solid var(--line);
    border-radius: 999px;
}

/* --- Messages ------------------------------------------------------------- */
.chat-msg {
    display: flex;
    flex-direction: column;
    max-width: 82%;
    margin-top: 2px;
}
.chat-msg--own { align-self: flex-end; align-items: flex-end; }
.chat-msg--agent { align-self: flex-start; align-items: flex-start; }
.chat-msg--system { align-self: center; max-width: 92%; margin: 0.4rem 0; }

/* first of a run (sender changes) gets extra top space */
.chat-msg + .chat-msg.is-run-start { margin-top: 0.6rem; }

.chat-msg__sender {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--ke-green-700);
    margin: 0 0 3px 6px;
}

.chat-bubble {
    padding: 0.55rem 0.8rem;
    border-radius: var(--chat-radius);
    line-height: 1.45;
    font-size: 0.9rem;
    word-break: break-word;
    overflow-wrap: anywhere;
    white-space: pre-wrap;
    box-shadow: var(--chat-shadow-soft);
}
.chat-msg--own .chat-bubble {
    background: var(--ke-green-600);
    color: #fff;
    border-bottom-right-radius: var(--chat-tail);
}
.chat-msg--agent .chat-bubble {
    background: var(--surface);
    color: var(--ink);
    border: 1px solid var(--line);
    border-bottom-left-radius: var(--chat-tail);
}

.chat-msg__meta {
    display: flex;
    align-items: center;
    gap: 4px;
    justify-content: flex-end;
    margin-top: 3px;
    font-size: 0.65rem;
    line-height: 1;
}
.chat-msg__time { font-family: var(--chat-mono); opacity: .75; }
.chat-msg--own .chat-msg__meta { color: rgba(255, 255, 255, .85); }
.chat-msg--agent .chat-msg__meta { color: var(--ink-soft); }

/* read receipt ticks (Phase B) */
.chat-msg__ticks { display: inline-flex; opacity: .85; }
.chat-msg__ticks svg { width: 15px; height: 11px; display: block; }
.chat-msg--read .chat-msg__ticks { color: var(--volt); opacity: 1; }

/* optimistic states (Phase B) */
.chat-msg--pending .chat-bubble { opacity: .72; }
.chat-msg--failed .chat-bubble { background: #fff; color: var(--danger); border: 1px solid var(--danger); }
.chat-msg__retry {
    font-size: 0.66rem;
    font-weight: 600;
    color: var(--danger);
    background: none;
    border: none;
    padding: 2px 0 0;
    cursor: pointer;
}
.chat-msg__retry:hover { text-decoration: underline; }

/* --- Attachments ---------------------------------------------------------- */
.chat-attach { display: block; margin-top: 6px; max-width: 100%; }
.chat-attach--img img {
    max-width: 220px;
    max-height: 220px;
    width: auto;
    border-radius: 10px;
    display: block;
}
.chat-attach--file {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 7px 11px;
    border-radius: 10px;
    background: rgba(0, 0, 0, .06);
    color: inherit;
    text-decoration: none;
    font-size: 0.82rem;
    max-width: 240px;
}
.chat-msg--own .chat-attach--file { background: rgba(255, 255, 255, .2); color: #fff; }
.chat-attach--file:hover { text-decoration: underline; }
.chat-attach__name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- System message ------------------------------------------------------- */
.chat-sys {
    display: inline-block;
    text-align: center;
    font-size: 0.74rem;
    color: var(--ink-soft);
    background: rgba(1, 58, 30, .05);
    border-radius: 999px;
    padding: 0.3rem 0.85rem;
}

/* --- Typing indicator ----------------------------------------------------- */
.chat-typing {
    align-self: flex-start;
    display: none;
    align-items: center;
    gap: 4px;
    padding: 0.5rem 0.8rem;
    margin-top: 2px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: var(--chat-radius);
    border-bottom-left-radius: var(--chat-tail);
    box-shadow: var(--chat-shadow-soft);
}
.chat-typing.is-active { display: inline-flex; }
.chat-typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: var(--ke-green-500);
    animation: chat-typing 1.1s infinite ease-in-out;
}
.chat-typing span:nth-child(2) { animation-delay: .18s; }
.chat-typing span:nth-child(3) { animation-delay: .36s; }
@keyframes chat-typing { 0%, 60%, 100% { opacity: .3; transform: translateY(0); } 30% { opacity: 1; transform: translateY(-3px); } }

/* --- Composer ------------------------------------------------------------- */
.chat-composer {
    background: var(--surface);
    border-top: 1px solid var(--line);
    padding: 0.6rem 0.7rem;
}
.chat-composer__quick {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    padding-bottom: 0.5rem;
    scrollbar-width: thin;
}
.chat-quick-reply {
    flex: 0 0 auto;
    font-size: 0.74rem;
    font-weight: 600;
    color: var(--ke-green-700);
    background: rgba(1, 122, 62, .08);
    border: 1px solid rgba(1, 122, 62, .18);
    border-radius: 999px;
    padding: 4px 12px;
    cursor: pointer;
    white-space: nowrap;
    transition: background .15s;
}
.chat-quick-reply:hover { background: rgba(1, 122, 62, .16); }
.chat-composer__row { display: flex; align-items: flex-end; gap: 8px; }
.chat-composer__field {
    flex: 1 1 auto;
    border: 1px solid var(--line);
    border-radius: 18px;
    padding: 0.55rem 0.9rem;
    font-size: 0.9rem;
    line-height: 1.4;
    resize: none;
    max-height: 120px;
    background: var(--paper);
    color: var(--ink);
    transition: border-color .15s, box-shadow .15s;
}
.chat-composer__field:focus {
    outline: none;
    border-color: var(--ke-green-600);
    box-shadow: 0 0 0 3px rgba(1, 122, 62, .12);
    background: #fff;
}
.chat-btn-icon {
    flex: 0 0 auto;
    width: 40px; height: 40px;
    border-radius: 50%;
    border: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform .12s, background .15s, color .15s;
}
.chat-btn-send { background: var(--ke-green-600); color: #fff; }
.chat-btn-send:hover { transform: scale(1.06); background: var(--ke-green-700); }
.chat-btn-send:disabled { opacity: .55; cursor: not-allowed; transform: none; }
.chat-btn-attach { background: transparent; color: var(--ink-soft); }
.chat-btn-attach:hover { background: var(--paper); color: var(--ke-green-700); }

/* --- Chat header (unifies the green brand across surfaces) ----------------- */
.chat-header {
    background: linear-gradient(310deg, var(--ke-green-900) 0%, var(--ke-green-600) 100%) !important;
}

/* --- Presence + the "SLA pulse" signature --------------------------------- */
.chat-avatar {
    --sla: 0;                 /* 0 → 1 as the SLA window elapses */
    --sla-color: var(--ke-green-500);
    position: relative;
    width: 38px; height: 38px;
    border-radius: 50%;
    display: inline-grid;
    place-items: center;
    font-weight: 700;
    color: #fff;
    background: var(--ke-green-700);
    background-clip: padding-box;
}
.chat-avatar--sla::before {
    content: "";
    position: absolute;
    inset: -3px;
    border-radius: 50%;
    padding: 3px;
    background: conic-gradient(var(--sla-color) calc(var(--sla) * 360deg), rgba(255, 255, 255, .25) 0);
    -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
    -webkit-mask-composite: xor;
            mask-composite: exclude;
}
.chat-presence {
    display: inline-flex; align-items: center; gap: 6px;
    font-size: 0.74rem; color: rgba(255, 255, 255, .85);
}
.chat-presence__dot {
    width: 8px; height: 8px; border-radius: 50%;
    background: #cfd8d2;
}
.chat-presence.is-online .chat-presence__dot {
    background: var(--ke-green-500);
    box-shadow: 0 0 0 0 rgba(10, 157, 82, .6);
    animation: chat-spark 1.8s infinite;
}
@keyframes chat-spark { 0% { box-shadow: 0 0 0 0 rgba(10, 157, 82, .55); } 70% { box-shadow: 0 0 0 6px rgba(10, 157, 82, 0); } 100% { box-shadow: 0 0 0 0 rgba(10, 157, 82, 0); } }

/* --- Unread badge (minimized widget / nav) -------------------------------- */
.chat-unread-badge {
    position: absolute;
    top: -4px; right: -4px;
    min-width: 18px; height: 18px;
    padding: 0 5px;
    border-radius: 999px;
    background: var(--volt);
    color: var(--ke-green-900);
    font-size: 0.68rem;
    font-weight: 700;
    line-height: 18px;
    text-align: center;
    box-shadow: 0 0 0 2px #fff;
    display: none;
}
.chat-unread-badge.is-visible { display: block; }

/* --- Mini toast (self-contained; customer pages have no toastr) ----------- */
.chat-toast-host {
    position: fixed;
    left: 50%;
    bottom: 24px;
    transform: translateX(-50%);
    z-index: 11000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}
.chat-toast {
    pointer-events: auto;
    font-size: 0.82rem;
    color: #fff;
    background: var(--ink);
    border-radius: 10px;
    padding: 0.55rem 0.9rem;
    box-shadow: 0 8px 24px rgba(0, 0, 0, .22);
    opacity: 0;
    transform: translateY(8px);
    transition: opacity .2s, transform .2s;
}
.chat-toast.is-in { opacity: 1; transform: translateY(0); }
.chat-toast--error { background: var(--danger); }
.chat-toast--success { background: var(--ke-green-700); }

@media (prefers-reduced-motion: reduce) {
    .chat-thread { scroll-behavior: auto; }
    .chat-typing span, .chat-presence.is-online .chat-presence__dot { animation: none; }
    .chat-toast { transition: none; }
}
