#chat-root {
    position: absolute;
    left: 8px;
    right: auto;
    /* เว้นพื้นที่ฮอตบาร์ขวา (controls padding-right 10px) + ช่องว่างระหว่างแชท–สกิล */
    width: min(
        calc(100% - var(--skill-hotbar-w) - 34px),
        360px,
        72%
    );
    bottom: calc(var(--chat-stack-bottom) + var(--safe-bottom));
    pointer-events: none;
    display: flex;
    flex-direction: column;
    gap: 5px;
    z-index: 6;
    touch-action: manipulation;
}

/* จอกว้าง (คอม) — แชทไปขอบซ้ายคอลัมน์ซ้าย */
html.layout-wide #chat-root {
    left: calc(-1 * var(--game-col-w) + 8px);
    right: auto;
    width: min(calc(var(--game-col-w) - 16px), 360px);
}

#chat-root.chat-compose-open {
    touch-action: auto;
}

#chat-root > * {
    pointer-events: auto;
}

.chat-tabs {
    display: none;
    gap: 5px;
    flex-shrink: 0;
    flex-wrap: nowrap;
    align-items: center;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-x: contain;
    touch-action: pan-x;
    padding: 2px 0;
    cursor: grab;
    /* ซ่อน scrollbar — เลื่อนด้วยการลาก/ปัดนิ้ว */
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.chat-tabs::-webkit-scrollbar {
    width: 0;
    height: 0;
    display: none;
}

.chat-tabs.is-dragging {
    cursor: grabbing;
    user-select: none;
}

#chat-root.chat-compose-open .chat-tabs,
#chat-root.has-whisper-tabs .chat-tabs {
    display: flex;
}

.chat-whisper-tabs {
    display: flex;
    flex-wrap: nowrap;
    gap: 5px;
    flex-shrink: 0;
    align-items: center;
    padding-top: 5px;
}

.chat-tab--whisper {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    max-width: 9.5rem;
    padding-right: 6px;
    flex-shrink: 0;
    overflow: visible;
}

.chat-whisper-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-whisper-name-wrap {
    position: relative;
    flex: 1 1 auto;
    min-width: 0;
    overflow: visible;
}

.chat-tab--whisper.has-unread::after {
    content: '';
    position: absolute;
    top: -4px;
    left: -4px;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #ef5350;
    border: 1.5px solid #fff;
    box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.45);
    pointer-events: none;
    z-index: 1;
}

.chat-whisper-close {
    flex-shrink: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 1.35rem;
    height: 1.35rem;
    margin: 0;
    padding: 0;
    border: none;
    border-radius: 4px;
    background: transparent;
    font-size: 1rem;
    line-height: 1;
    color: rgba(255, 248, 232, 0.85);
    cursor: pointer;
    touch-action: manipulation;
}

.chat-whisper-close:hover {
    background: rgba(0, 0, 0, 0.35);
    color: #fff;
}

.chat-line.whisper .chat-name {
    color: #90caf9;
}

.chat-line.party .chat-name {
    color: #81c784;
}

.chat-tab {
    padding: 8px 12px;
    min-height: var(--touch-min);
    font-size: var(--text-sm);
    border: 2px solid var(--px-border);
    background: rgba(0, 0, 0, 0.35);
    color: #ccc;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    white-space: nowrap;
}

.chat-tab.active {
    color: var(--px-gold);
    border-color: var(--px-gold);
    background: rgba(0, 0, 0, 0.55);
}

.chat-panel {
    display: flex;
    flex-direction: column;
    max-height: var(--chat-panel-h);
    background: transparent;
    border: none;
    box-sizing: border-box;
    position: relative;
    min-height: 0;
}

#chat-root.chat-compose-open .chat-panel {
    height: var(--chat-compose-panel-h);
    max-height: min(var(--chat-compose-panel-h), 42vh);
    min-height: 200px;
    background: linear-gradient(180deg, rgba(36, 28, 22, 0.92) 0%, rgba(12, 10, 8, 0.88) 100%);
    border: 2px solid var(--px-border);
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.35);
    backdrop-filter: blur(4px);
}

[data-chat-messages] {
    /* เหลือที่ให้ปุ่ม「แตะเพื่อพิมพ์」— อย่า max-height เท่าแผงทั้งก้อน */
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 6px 10px 4px;
    display: block;
    scrollbar-width: none;
    -ms-overflow-style: none;
    -webkit-overflow-scrolling: touch;
    cursor: pointer;
}

[data-chat-messages]::-webkit-scrollbar {
    display: none;
    width: 0;
    height: 0;
}

#chat-root.chat-compose-open [data-chat-messages] {
    flex: 1 1 auto;
    /* ~4 บรรทัดที่มองเห็น */
    min-height: 5.8rem;
    max-height: none;
    padding: 8px 10px 6px;
    overflow-y: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#chat-root.chat-compose-open [data-chat-messages]::-webkit-scrollbar {
    display: none;
}

.chat-line {
    font-size: var(--text-sm);
    line-height: 1.45;
    margin-top: 3px;
    text-shadow: -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000, 1px 1px 0 #000;
    transition: opacity 0.8s;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.chat-line.faded {
    opacity: 0.45;
}

.chat-line.system .chat-name {
    color: #64b5f6;
}

.chat-name {
    color: var(--px-gold);
    font-weight: bold;
}

.chat-text {
    color: #fff;
}

.chat-compose-toggle {
    flex: 0 0 auto;
    font-size: var(--text-sm);
    padding: 8px 10px 10px;
    margin: 0 8px 4px;
    min-height: var(--touch-min);
    text-align: left;
    color: #ccc;
    background: transparent;
    border: none;
    cursor: pointer;
    font-family: inherit;
    display: flex;
    align-items: center;
}

.chat-compose-toggle:active {
    color: var(--px-gold);
}

#chat-root.chat-compose-open .chat-compose-toggle {
    display: none;
}

.chat-compose-bar {
    display: none;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 5px 6px 4px 8px;
    flex: 0 0 auto;
    border-bottom: 1px solid rgba(141, 110, 99, 0.4);
    background: rgba(0, 0, 0, 0.22);
}

#chat-root.chat-compose-open .chat-compose-bar {
    display: flex;
}

.chat-compose-label {
    font-size: 0.72rem;
    color: #ffe082;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-shadow: 0 1px 0 rgba(0, 0, 0, 0.65);
}

.chat-compose-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 24px;
    min-width: 24px;
    height: 22px;
    padding: 0;
    border: 1px solid rgba(141, 110, 99, 0.65);
    border-radius: 4px;
    background: rgba(0, 0, 0, 0.35);
    color: #ffe082;
    font-size: 0.72rem;
    font-weight: 800;
    line-height: 1;
    cursor: pointer;
    touch-action: manipulation;
    font-family: inherit;
}

.chat-compose-close:hover,
.chat-compose-close:focus-visible {
    border-color: rgba(255, 213, 79, 0.75);
    background: rgba(255, 193, 7, 0.14);
    outline: none;
}

.chat-compose {
    display: none;
    gap: 8px;
    padding: 8px 10px 10px;
    border-top: 2px solid rgba(93, 64, 55, 0.6);
    flex: 0 0 auto;
    align-items: center;
}

#chat-root.chat-compose-open .chat-compose {
    display: flex;
    flex-wrap: nowrap;
    align-items: center;
    gap: 6px;
}

.chat-compose-main {
    flex: 1;
    min-width: 0;
    display: flex;
    align-items: center;
    padding: 6px 8px;
    min-height: var(--touch-min);
    border: 2px solid var(--px-border);
    background: #1a1410;
    overflow: hidden;
}

.chat-compose-editor {
    flex: 1;
    min-width: 0;
    max-width: 100%;
    font-family: var(--px-font, 'Segoe UI', sans-serif);
    font-size: 16px;
    line-height: 1.45;
    color: #fff;
    white-space: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    outline: none;
    caret-color: #fff;
    user-select: text;
    -webkit-user-select: text;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

.chat-compose-editor::-webkit-scrollbar {
    display: none;
}

.chat-compose-editor.is-empty::before {
    content: attr(data-placeholder);
    color: #888;
    pointer-events: none;
}

.chat-compose-chip {
    display: inline;
    white-space: nowrap;
    padding: 0 1px;
    border: none;
    border-radius: 0;
    background: transparent;
    color: #90caf9;
    font-weight: bold;
    vertical-align: baseline;
    cursor: default;
}

.chat-item-ref {
    display: inline;
    margin: 0;
    padding: 0 1px;
    border: none;
    border-radius: 0;
    background: transparent;
    color: #90caf9;
    font: inherit;
    font-weight: bold;
    cursor: pointer;
    vertical-align: baseline;
    text-decoration: underline;
    text-underline-offset: 2px;
    white-space: nowrap;
}

.chat-item-ref:hover {
    color: #bbdefb;
}

.chat-item-detail-root.item-detail-root {
    position: fixed;
    inset: 0;
    z-index: 9250;
}

.chat-compose-main:focus-within {
    border-color: var(--px-gold);
}

#chat-root.chat-system-readonly .chat-compose {
    opacity: 0.55;
    pointer-events: none;
}

#chat-root.chat-system-readonly .chat-compose-label {
    color: rgba(245, 230, 211, 0.7);
}

.chat-compose button[type='submit'],
.chat-compose .chat-emoji-btn {
    flex-shrink: 0;
    font-size: var(--text-sm);
    padding: 8px 12px;
    min-height: var(--touch-min);
    min-width: var(--touch-min);
}

.chat-emoji-btn {
    font-size: clamp(18px, 5vw, 22px);
    line-height: 1;
}

.chat-emoji-btn.active {
    border-color: var(--px-gold);
    background: rgba(241, 196, 15, 0.2);
}

.chat-emoji-picker {
    --chat-emoji-cols: 8;
    --chat-emoji-cell: 44px;
    --chat-emoji-gap: 5px;
    /* แสดง 2 แถวเต็ม ที่เหลือเลื่อนลง */
    --chat-emoji-visible-rows: 2;
    position: absolute;
    left: 6px;
    right: 6px;
    bottom: 100%;
    margin-bottom: 4px;
    padding: 8px;
    background: rgba(20, 16, 12, 0.96);
    border: 2px solid var(--px-border);
    box-sizing: border-box;
    max-height: min(
        calc(
            var(--chat-emoji-visible-rows) * var(--chat-emoji-cell) +
                (var(--chat-emoji-visible-rows) - 1) * var(--chat-emoji-gap) + 16px
        ),
        38vh
    );
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
    z-index: 2;
}

/* scrollbar ธีมเกม — ดู .game-scroll ใน base.css */

/* ไม่ให้กล่องอีโมจิถูก clip ตอนลอยเหนือแผงแชท */
#chat-root.chat-compose-open .chat-panel:has(.chat-emoji-picker:not([hidden])) {
    overflow: visible;
}

.chat-emoji-grid {
    display: grid;
    grid-template-columns: repeat(var(--chat-emoji-cols), minmax(0, 1fr));
    gap: var(--chat-emoji-gap);
    width: 100%;
}

.chat-emoji-item {
    font-size: clamp(20px, 5.5vw, 24px);
    line-height: 1.1;
    padding: 4px 2px;
    width: 100%;
    height: var(--chat-emoji-cell);
    min-height: var(--chat-emoji-cell);
    box-sizing: border-box;
    border: 2px solid transparent;
    background: transparent;
    cursor: pointer;
    border-radius: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-emoji-item:active {
    border-color: var(--px-gold);
    background: rgba(255, 255, 255, 0.08);
}

/* มือถือแนวตั้ง — แชทกระซิบไม่กินพื้นที่ฮอตบาร์ */
html:not(.layout-wide) #chat-root.has-whisper-tabs {
    width: min(
        calc(100% - var(--skill-hotbar-w) - 34px),
        68%,
        260px
    );
}

html:not(.layout-wide) #chat-root.has-whisper-tabs.chat-compose-open .chat-panel {
    height: auto;
    min-height: 0;
    max-height: min(32vh, calc(100dvh - var(--controls-h) - 96px));
}

html:not(.layout-wide)
    #chat-root.has-whisper-tabs.chat-compose-open
    [data-chat-messages] {
    min-height: 4.2rem;
    max-height: min(13vh, 112px);
}

@media (max-height: 740px) {
    html:not(.layout-wide) #chat-root.has-whisper-tabs.chat-compose-open .chat-panel {
        max-height: min(28vh, calc(100dvh - var(--controls-h) - 80px));
    }
}
