/* Gấu Dẻo — mascot đứng góc dưới bên phải mọi trang công khai.
 * Markup: core/templates/public_scripts.php · Hành vi: js/mascot.js · Ảnh: bin/mascot-build.js
 *
 * Widget là con trực tiếp của <body> (public_scripts.php nằm cuối body) — đừng bọc nó vào
 * section/main nào có z-index hay transform, không thì nó bị kẹt trong stacking context đó. */

.mk-mascot {
    position: fixed;
    right: 18px;
    bottom: 16px;
    z-index: 70; /* trên navbar sticky (50), dưới thanh nav mobile (90) và mọi modal */
    display: flex;
    align-items: flex-end;
    gap: 2px;
    pointer-events: none;
    opacity: 0;
    transform: translateY(18px);
    transition: opacity .35s ease, transform .5s cubic-bezier(.34, 1.56, .64, 1);
}
.mk-mascot[hidden] { display: none; }
.mk-mascot.is-in {
    opacity: 1;
    transform: none;
}
.mk-mascot > * { pointer-events: auto; }
.mk-mascot-figure {
    position: relative;
    flex-shrink: 0;
}

.mk-mascot-bear {
    --mk-ol: .9px;   /* bề dày viền tối quanh gấu */
    position: relative;
    display: block;
    appearance: none;
    border: 0;
    background: none;
    padding: 0;
    margin: 0;
    cursor: pointer;
    line-height: 0;
    border-radius: 18px;
    -webkit-tap-highlight-color: transparent;
}

/* Gấu SVG = ba lớp ảnh tĩnh chồng khít (bin/mascot-build.js): thân, tay vẫy, mí mắt.
   Mọi chuyển động là transform/opacity trên lớp ảnh nên card đồ hoạ làm, trình duyệt không
   phải vẽ lại SVG mỗi khung hình.

   Vị trí chân, vai, cỡ bóng tính theo % khung ảnh. Mỗi bộ đồ một khung (mũ Halloween làm
   ảnh cao hơn), nên public_scripts.php đặt các biến --mk-* dưới đây theo bộ đang mặc, lấy
   từ assets/mascot/manifest.json. Số mặc định là của gấu thường (khung 191×313).
   --mk-scale: ảnh cao hơn gấu thường bao nhiêu lần — để thân gấu vẫn đúng cỡ, phần dư là mũ. */
.mk-mascot {
    --mk-feet-x: 45.34%;
    --mk-feet-y: 88.24%;
    --mk-arm-x: 71.73%;
    --mk-arm-y: 64.31%;
    --mk-shadow-w: 61.2%;
    --mk-shadow-h: 11.2%;
    --mk-scale: 1;
}
.mk-mascot-rig {
    position: relative;
    display: block;
    transform-origin: var(--mk-feet-x) var(--mk-feet-y);   /* giữa hai bàn chân */
    animation: mkMascotBob 3s ease-in-out infinite;
    transition: translate .2s ease;
    /* Viền tối = bốn bóng đổ sắc lệch bốn phía, gộp cả ba lớp thành một khối; rồi bóng mờ. */
    filter:
        drop-shadow(var(--mk-ol) 0 0 #2a1029) drop-shadow(calc(var(--mk-ol) * -1) 0 0 #2a1029)
        drop-shadow(0 var(--mk-ol) 0 #2a1029) drop-shadow(0 calc(var(--mk-ol) * -1) 0 #2a1029)
        drop-shadow(0 6px 14px rgba(6, 3, 11, .35));
}
.mk-mascot-layer {
    display: block;
    height: calc(112px * var(--mk-scale));
    width: auto;
}
.mk-mascot-arm,
.mk-mascot-lid {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
}
.mk-mascot-arm {
    transform-origin: var(--mk-arm-x) var(--mk-arm-y);   /* vai phải */
    animation: mkMascotWave 7s ease-in-out infinite;
}
.mk-mascot-lid {
    opacity: 0;
    animation: mkMascotBlink 4.8s linear infinite;
}
/* Bóng đất: ngoài lớp có viền, co lại khi gấu nhún lên. */
.mk-mascot-bear::before {
    content: '';
    position: absolute;
    left: var(--mk-feet-x);
    top: var(--mk-feet-y);
    width: var(--mk-shadow-w);
    height: var(--mk-shadow-h);
    border-radius: 50%;
    background: rgba(42, 16, 41, .2);
    transform: translate(-50%, -50%);
    animation: mkMascotShadow 3s ease-in-out infinite;
}
.mk-mascot-bear:hover .mk-mascot-rig { translate: 0 -3px; }

/* Bấm trên điện thoại: thay cả ba lớp bằng ảnh gấu vui (tự chuyển động) một lúc. */
.mk-mascot-happy {
    position: absolute;
    left: 0;
    bottom: 0;
    display: block;
    height: 100%;
    width: auto;
}
.mk-mascot-happy[hidden] { display: none; }
.mk-mascot-bear.is-happy .mk-mascot-rig { visibility: hidden; }
.mk-mascot-bear.is-happy::before { opacity: 0; }

/* Vẫy hai nhịp rồi nghỉ; js/mascot.js khởi động lại từ đầu mỗi khi gấu nói. */
@keyframes mkMascotWave {
    0%, 20%, 40%, 100% { transform: rotate(-6deg); }
    10%, 30% { transform: rotate(14deg); }
}
@keyframes mkMascotBob {
    50% { transform: translateY(-1.28%); }
}
@keyframes mkMascotShadow {
    50% { transform: translate(-50%, -50%) scale(.92); }
}
@keyframes mkMascotBlink {
    0%, 93%, 100% { opacity: 0; }
    93.5%, 97% { opacity: 1; }
}

/* Bản 3D (máy có chuột): canvas đè khít lên ba lớp ảnh; ảnh giữ chỗ nhưng ẩn và đứng yên để
   bố cục — và khung chat — không xê dịch, cũng không tốn công chạy animation thừa. */
.mk-mascot.is-3d .mk-mascot-rig { visibility: hidden; animation: none; filter: none; }
.mk-mascot.is-3d .mk-mascot-rig > * { animation: none; }
.mk-mascot.is-3d .mk-mascot-bear::before { display: none; }

.mk-mascot-bear:focus-visible {
    outline: 2px solid #ff5c9a;
    outline-offset: 4px;
}

/* ---------- Khung chat ----------
   Luôn nền sáng chữ tối, đọc được trên mọi giao diện (tối/sáng/mùa). Hồng thương hiệu
   #ff5c9a chỉ để điểm xuyết; chữ trắng trên nền hồng dùng --mk-c-accent (đủ tương phản). */
.mk-mascot-bubble {
    --mk-c-surface: #ffffff;
    --mk-c-ink: #1f1424;
    --mk-c-muted: #7c6b76;
    --mk-c-line: #f1e5ec;
    --mk-c-bot: #f6eff3;
    --mk-c-brand: #ff5c9a;
    --mk-c-accent: #cf2a69;
    --mk-c-accent-hover: #b51f5a;
    --mk-c-tint: #fff1f7;

    position: relative;
    display: flex;
    flex-direction: column;
    width: 304px;
    margin-bottom: 56px;
    background: var(--mk-c-surface);
    color: var(--mk-c-ink);
    border: 1px solid rgba(42, 16, 41, .08);
    border-radius: 18px;
    box-shadow: 0 18px 48px rgba(10, 4, 14, .38), 0 2px 6px rgba(10, 4, 14, .16);
    font: 500 .84rem/1.5 'Montserrat', system-ui, sans-serif;
    animation: mkMascotPop .22s ease-out;
    transform-origin: 100% 100%;
}
.mk-mascot-bubble[hidden] { display: none; }
/* Đuôi chỉ về phía gấu. */
.mk-mascot-bubble::after {
    content: '';
    position: absolute;
    right: -7px;
    bottom: 22px;
    width: 13px;
    height: 13px;
    background: var(--mk-c-surface);
    border-right: 1px solid rgba(42, 16, 41, .08);
    border-bottom: 1px solid rgba(42, 16, 41, .08);
    transform: rotate(-45deg);
}

.mk-chat-head {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 11px 10px 10px 12px;
    border-bottom: 1px solid var(--mk-c-line);
}
.mk-chat-avatar {
    flex-shrink: 0;
    display: grid;
    place-items: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--mk-c-tint);
    box-shadow: inset 0 0 0 1.5px rgba(255, 92, 154, .45);
    overflow: hidden;
}
.mk-chat-avatar img {
    width: 27px;
    height: auto;
    margin-top: 4px;
}
.mk-chat-who {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    line-height: 1.25;
}
.mk-chat-who strong {
    font-size: .9rem;
    font-weight: 700;
    color: var(--mk-c-ink);
}
.mk-chat-who span {
    font-size: .72rem;
    color: var(--mk-c-muted);
}
.mk-chat-x {
    flex-shrink: 0;
    display: grid;
    place-items: center;
    width: 30px;
    height: 30px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: var(--mk-c-muted);
    cursor: pointer;
}
.mk-chat-x svg { width: 16px; height: 16px; }
.mk-chat-x:hover { background: var(--mk-c-bot); color: var(--mk-c-ink); }
.mk-chat-x:focus-visible { outline: 2px solid var(--mk-c-brand); outline-offset: 1px; }

/* Luồng tin nhắn: gấu bên trái, khách bên phải; cuộn trong khung, không cuộn cả trang. */
.mk-chat-log {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 12px 12px 10px;
    max-height: clamp(120px, calc(100vh - 360px), 300px);
    max-height: clamp(120px, calc(100dvh - 360px), 300px);
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: thin;
    scrollbar-color: #e6d3de transparent;
}
.mk-msg {
    max-width: 88%;
    padding: 8px 12px;
    overflow-wrap: anywhere;
}
.mk-msg-bot {
    align-self: flex-start;
    background: var(--mk-c-bot);
    color: var(--mk-c-ink);
    border-radius: 14px 14px 14px 4px;
}
.mk-msg-user {
    align-self: flex-end;
    background: var(--mk-c-accent);
    color: #fff;
    border-radius: 14px 14px 4px 14px;
}
/* Hai tin cùng phía liền nhau thì sát lại, như một cụm. */
.mk-msg-bot + .mk-msg-bot,
.mk-msg-user + .mk-msg-user { margin-top: -3px; }
.mk-msg-user + .mk-msg-bot,
.mk-msg-bot + .mk-msg-user { margin-top: 4px; }
.mk-msg-text { margin: 0; }
.mk-msg-bot .mk-msg-text a {
    color: var(--mk-c-accent);
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
}

/* "Đang gõ…": ba chấm nhún lần lượt trong một bóng tin của gấu. */
.mk-msg-typing {
    display: inline-flex;
    gap: 4px;
    align-items: center;
    height: 34px;
}
.mk-msg-typing i {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #c9a6b8;
    animation: mkMascotDot 1s ease-in-out infinite;
}
.mk-msg-typing i:nth-child(2) { animation-delay: .15s; }
.mk-msg-typing i:nth-child(3) { animation-delay: .3s; }
@keyframes mkMascotDot {
    0%, 60%, 100% { transform: translateY(0); opacity: .45; }
    30% { transform: translateY(-3px); opacity: 1; }
}

/* Nút hành động dưới tin của gấu (copy IP, mở trang). */
.mk-mascot-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
}
.mk-mascot-act {
    display: inline-flex;
    align-items: center;
    min-height: 30px;
    padding: 4px 12px;
    border: 1px solid #efc9da;
    border-radius: 999px;
    background: var(--mk-c-surface);
    color: var(--mk-c-accent);
    font: 700 .76rem/1 'Montserrat', system-ui, sans-serif;
    text-decoration: none;
    cursor: pointer;
    transition: background .15s ease, border-color .15s ease;
}
.mk-mascot-act:hover { background: var(--mk-c-tint); border-color: var(--mk-c-brand); color: var(--mk-c-accent-hover); }
.mk-mascot-act:focus-visible { outline: 2px solid var(--mk-c-brand); outline-offset: 2px; }

/* Câu hỏi nhanh — bấm là thành câu của khách trong luồng, gấu trả lời ngay. */
.mk-mascot-chips {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    padding: 2px 12px 10px;
}
.mk-mascot-chips[hidden],
.mk-mascot-bubble.is-typing .mk-mascot-chips { display: none; }
.mk-mascot-chip {
    display: inline-flex;
    align-items: center;
    min-height: 28px;
    padding: 3px 11px;
    border: 1px solid #ecdae4;
    border-radius: 999px;
    background: var(--mk-c-surface);
    color: #5d2a47;
    font: 600 .74rem/1 'Montserrat', system-ui, sans-serif;
    cursor: pointer;
    transition: background .15s ease, border-color .15s ease, color .15s ease;
}
.mk-mascot-chip:hover { background: var(--mk-c-tint); border-color: var(--mk-c-brand); color: var(--mk-c-accent); }
.mk-mascot-chip:focus-visible { outline: 2px solid var(--mk-c-brand); outline-offset: 2px; }

/* Ô gõ: một viên thuốc chứa cả ô chữ và nút gửi. */
.mk-mascot-ask {
    display: flex;
    align-items: center;
    gap: 6px;
    margin: 0 12px;
    padding: 4px 4px 4px 14px;
    border: 1px solid #e8d6e0;
    border-radius: 999px;
    background: #fcf8fa;
    transition: border-color .15s ease, box-shadow .15s ease;
}
.mk-mascot-ask:focus-within {
    border-color: var(--mk-c-brand);
    box-shadow: 0 0 0 3px rgba(255, 92, 154, .18);
    background: var(--mk-c-surface);
}
.mk-mascot-ask input {
    flex: 1;
    min-width: 0;
    height: 32px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--mk-c-ink);
    font: 500 .82rem/1 'Montserrat', system-ui, sans-serif;
    outline: none;
}
.mk-mascot-ask input::placeholder { color: #a7949f; }
.mk-mascot-ask button {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    display: grid;
    place-items: center;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: var(--mk-c-accent);
    color: #fff;
    cursor: pointer;
    transition: background .15s ease;
}
.mk-mascot-ask button:hover { background: var(--mk-c-accent-hover); }
.mk-mascot-ask button:disabled { background: #ead9e3; color: #fff; cursor: default; }
.mk-mascot-ask button:focus-visible { outline: 2px solid var(--mk-c-brand); outline-offset: 2px; }
.mk-mascot-ask svg { width: 16px; height: 16px; }

.mk-chat-note {
    margin: 0;
    padding: 7px 14px 10px;
    color: var(--mk-c-muted);
    font-size: .68rem;
    line-height: 1.4;
    text-align: center;
}
.mk-chat-note a { color: var(--mk-c-accent); font-weight: 600; }

/* Nút thu gọn gấu (cạnh đầu gấu). */
.mk-mascot-close {
    position: absolute;
    top: -6px;
    right: -8px;
    width: 26px;
    height: 26px;
    display: grid;
    place-items: center;
    padding: 0;
    border: 1px solid rgba(255, 255, 255, .14);
    border-radius: 50%;
    background: rgba(20, 10, 29, .88);
    color: #fef9ff;
    cursor: pointer;
    opacity: 0;
    transition: opacity .2s ease;
}
.mk-mascot-close svg { width: 12px; height: 12px; }
.mk-mascot:hover .mk-mascot-close,
.mk-mascot:focus-within .mk-mascot-close,
.mk-mascot.is-talking .mk-mascot-close { opacity: 1; }
.mk-mascot-close:focus-visible { outline: 2px solid #ff5c9a; outline-offset: 2px; }

/* Thu gọn: gấu thành nút tròn ló cái đầu ra; bấm là gấu ra lại. */
.mk-mascot-mini {
    display: none;
    position: relative;
    width: 50px;
    height: 50px;
    padding: 0;
    border: 2px solid #ff5c9a;
    border-radius: 50%;
    background: #1f0f24;
    box-shadow: 0 6px 18px rgba(6, 3, 11, .4);
    cursor: pointer;
    overflow: hidden;
    transition: transform .2s ease, box-shadow .2s ease;
    -webkit-tap-highlight-color: transparent;
}
.mk-mascot-mini img {
    position: absolute;
    left: 50%;
    top: 5px;
    width: 40px;
    height: auto;
    translate: -50% 0;
    transition: top .2s ease;
}
.mk-mascot-mini:hover { transform: translateY(-2px); box-shadow: 0 10px 22px rgba(6, 3, 11, .45); }
.mk-mascot-mini:hover img { top: 2px; }
/* Đầu đội mũ phù thuỷ cao gần gấp rưỡi đầu thường (khung 168×250 so với 168×179): thu ảnh
   lại cho cả mũ lẫn mặt nằm gọn trong vòng tròn. */
.mk-mascot[data-outfit="halloween"] .mk-mascot-mini img { width: 32px; top: 1px; }
.mk-mascot[data-outfit="halloween"] .mk-mascot-mini:hover img { top: -1px; }
.mk-mascot[data-outfit="halloween"] .mk-chat-avatar img { width: 21px; margin-top: 3px; }
.mk-mascot-mini:focus-visible { outline: 2px solid #ff5c9a; outline-offset: 3px; }
.mk-mascot.is-min .mk-mascot-mini { display: block; }
.mk-mascot.is-min .mk-mascot-figure,
.mk-mascot.is-min .mk-mascot-bubble { display: none; }

@keyframes mkMascotPop {
    from { opacity: 0; transform: scale(.92) translateY(6px); }
    to   { opacity: 1; transform: none; }
}

/* Màn cảm ứng không có hover — nút thu gọn phải luôn thấy được. */
@media (hover: none) {
    .mk-mascot-close { opacity: 1; }
}

/* ≤768px có thanh điều hướng cố định 60px ở đáy (style.css: .mobile-bottom-nav).
   Khung chat nằm trên đầu gấu, rộng gần hết màn hình; đuôi chỉ xuống gấu. */
@media (max-width: 768px) {
    .mk-mascot {
        right: 10px;
        bottom: calc(68px + env(safe-area-inset-bottom, 0px));
    }
    .mk-mascot-bear { --mk-ol: .7px; }
    .mk-mascot-layer { height: calc(76px * var(--mk-scale)); }
    .mk-mascot-bubble {
        position: absolute;
        right: 0;
        bottom: calc(100% + 12px);
        width: min(340px, calc(100vw - 20px));
        margin: 0;
        transform-origin: 85% 100%;
    }
    .mk-mascot-bubble::after {
        right: 30px;
        bottom: -7px;
        border-right: 1px solid rgba(42, 16, 41, .08);
        border-bottom: 1px solid rgba(42, 16, 41, .08);
        transform: rotate(45deg);
    }
    .mk-chat-log {
        max-height: clamp(110px, 38vh, 280px);
        max-height: clamp(110px, 38dvh, 280px);
    }
    /* 16px trở lên thì iOS không tự phóng to trang khi bấm vào ô gõ. */
    .mk-mascot-ask input { font-size: 16px; }

    /* Bàn phím ảo đang mở (js/mascot.js đo bằng visualViewport): ẩn gấu, đặt khung chat ngay
       trên bàn phím, bớt phần phụ cho luồng tin còn chỗ. */
    .mk-mascot.has-kb { bottom: calc(var(--mk-kb, 0px) + 8px); }
    .mk-mascot.has-kb .mk-mascot-figure,
    .mk-mascot.has-kb .mk-mascot-chips,
    .mk-mascot.has-kb .mk-chat-note,
    .mk-mascot.has-kb .mk-mascot-bubble::after { display: none; }
    .mk-mascot.has-kb .mk-mascot-bubble { bottom: 0; }
    .mk-mascot.has-kb .mk-chat-log { max-height: clamp(80px, calc(var(--mk-vh, 100vh) - 150px), 280px); }
}

@media (prefers-reduced-motion: reduce) {
    .mk-mascot { transition: opacity .2s ease; transform: none; }
    .mk-mascot-bubble { animation: none; }
    /* Nhún, vẫy tay, chớp mắt của gấu cố ý GIỮ: chuyển động nhỏ trong khung gấu, mà nhiều người
       tắt hiệu ứng Windows cho máy nhẹ chứ không phải vì say chuyển động. Bản 3D chỉ bỏ cú xoay
       360° (js/mascot-3d.js). */
}

@media print {
    .mk-mascot { display: none !important; }
}
