/* ══════════════════════════════════════════════════
           Chat Variables (기존 사이트 테마 연동)
        ══════════════════════════════════════════════════ */
:root {
    --chat-bg:         #0f1117;
    --chat-surface:    #161b26;
    --chat-surface2:   #1e2535;
    --chat-border:     #2a3347;
    --chat-text:       #e2e8f0;
    --chat-text-muted: #64748b;
    --chat-accent:     #22d3ee;
    --chat-positive:   #10b981;
    --chat-negative:   #ef4444;
    --chat-mine-bg:    #164e63;
    --chat-other-bg:   #1e2535;
    --chat-system:     #334155;
    --chat-radius:     12px;
    --chat-w:          340px;
    --chat-h:          480px;
    --chat-font:       'SUIT Variable', 'Inter', sans-serif;
}

/* ══════════════════════════════════════════════════
   Chat Toggle Button
══════════════════════════════════════════════════ */
#chat-toggle-btn {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--chat-surface2);
    border: 1px solid var(--chat-border);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    transition: background 0.2s, border-color 0.2s, transform 0.15s;
    box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}
#chat-toggle-btn:hover {
    background: var(--chat-surface);
    border-color: var(--chat-accent);
    transform: translateY(-2px);
}
#chat-toggle-btn svg {
    width: 22px;
    height: 22px;
    stroke: var(--chat-accent);
    fill: none;
    stroke-width: 1.8;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* 미확인 메시지 뱃지 */
#chat-unread-badge {
    display: none;
    position: absolute;
    top: -4px;
    right: -4px;
    min-width: 20px;
    height: 20px;
    padding: 0 5px;
    border-radius: 10px;
    background: var(--chat-negative);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    font-family: var(--chat-font);
    align-items: center;
    justify-content: center;
    border: 2px solid var(--chat-bg);
}

/* ══════════════════════════════════════════════════
   Chat Window
══════════════════════════════════════════════════ */
#chat-window {
    position: fixed;
    bottom: 88px;
    right: 24px;
    width: var(--chat-w);
    height: var(--chat-h);
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    display: flex;
    flex-direction: column;
    z-index: 999;
    box-shadow: 0 8px 40px rgba(0,0,0,0.6);
    font-family: var(--chat-font);
    opacity: 0;
    transform: translateY(12px) scale(0.97);
    pointer-events: none;
    transition: opacity 0.22s ease, transform 0.22s ease;
    overflow: hidden;
}
#chat-window.chat-open {
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: all;
}

/* ── 헤더 ── */
.chat-header {
    display: flex;
    align-items: center;
    padding: 12px 14px;
    border-bottom: 1px solid var(--chat-border);
    background: var(--chat-bg);
    gap: 8px;
    flex-shrink: 0;
}
.chat-header-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--chat-positive);
    flex-shrink: 0;
}
.chat-header-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--chat-text);
    letter-spacing: 0.02em;
}
.chat-header-right {
    margin-left: auto;
    display: flex;
    align-items: center;
    gap: 6px;
}
#chat-close-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--chat-text-muted);
    padding: 4px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    transition: color 0.15s, background 0.15s;
}
#chat-close-btn:hover {
    color: var(--chat-text);
    background: var(--chat-surface2);
}
#chat-close-btn svg {
    width: 16px;
    height: 16px;
    stroke: currentColor;
    fill: none;
    stroke-width: 2;
    stroke-linecap: round;
}

/* ── 닉네임 바 ── */
.chat-nick-bar {
    display: flex;
    align-items: center;
    padding: 8px 14px;
    border-bottom: 1px solid var(--chat-border);
    background: var(--chat-surface);
    gap: 6px;
    flex-shrink: 0;
}
.chat-nick-label {
    font-size: 11px;
    color: var(--chat-text-muted);
}
#chat-nickname-display {
    font-size: 12px;
    font-weight: 600;
    color: var(--chat-accent);
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
#chat-edit-nick-btn {
    background: none;
    border: 1px solid var(--chat-border);
    border-radius: 6px;
    color: var(--chat-text-muted);
    font-size: 11px;
    font-family: var(--chat-font);
    padding: 3px 8px;
    cursor: pointer;
    transition: border-color 0.15s, color 0.15s;
    white-space: nowrap;
    flex-shrink: 0;
}
#chat-edit-nick-btn:hover {
    border-color: var(--chat-accent);
    color: var(--chat-accent);
}

/* ── 메시지 영역 ── */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 12px 12px 6px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    scroll-behavior: smooth;
}
#chat-messages::-webkit-scrollbar {
    width: 4px;
}
#chat-messages::-webkit-scrollbar-track {
    background: transparent;
}
#chat-messages::-webkit-scrollbar-thumb {
    background: var(--chat-border);
    border-radius: 2px;
}

/* ── 메시지 버블 ── */
.chat-msg-wrap {
    display: flex;
    flex-direction: column;
    max-width: 82%;
}
.chat-msg-wrap.chat-mine {
    align-self: flex-end;
    align-items: flex-end;
}
.chat-msg-wrap.chat-other {
    align-self: flex-start;
    align-items: flex-start;
}
.chat-msg-nick {
    font-size: 10px;
    color: var(--chat-text-muted);
    margin-bottom: 3px;
    padding-left: 2px;
}
.chat-msg-row {
    display: flex;
    align-items: flex-end;
    gap: 5px;
}
.chat-msg-bubble {
    padding: 7px 11px;
    border-radius: 10px;
    font-size: 13px;
    line-height: 1.45;
    color: var(--chat-text);
    word-break: break-word;
    white-space: pre-wrap;
}
.chat-mine .chat-msg-bubble {
    background: var(--chat-mine-bg);
    border-bottom-right-radius: 3px;
}
.chat-other .chat-msg-bubble {
    background: var(--chat-other-bg);
    border-bottom-left-radius: 3px;
}
.chat-msg-time {
    font-size: 10px;
    color: var(--chat-text-muted);
    white-space: nowrap;
    flex-shrink: 0;
}
.chat-msg-system {
    align-self: center;
    font-size: 11px;
    color: var(--chat-text-muted);
    background: var(--chat-system);
    padding: 3px 10px;
    border-radius: 20px;
    margin: 4px 0;
    display: flex;
    align-items: center;
    gap: 6px;
    text-align: center;
}
.chat-msg-error {
    color: var(--chat-negative);
    background: rgba(239,68,68,0.1);
}

/* ── 입력창 ── */
.chat-input-area {
    display: flex;
    padding: 10px 12px;
    border-top: 1px solid var(--chat-border);
    gap: 8px;
    background: var(--chat-bg);
    flex-shrink: 0;
}
#chat-input {
    flex: 1;
    background: var(--chat-surface2);
    border: 1px solid var(--chat-border);
    border-radius: 8px;
    color: var(--chat-text);
    font-size: 13px;
    font-family: var(--chat-font);
    padding: 8px 12px;
    outline: none;
    transition: border-color 0.15s;
    resize: none;
    min-height: 36px;
    max-height: 36px;
}
#chat-input::placeholder {
    color: var(--chat-text-muted);
}
#chat-input:focus {
    border-color: var(--chat-accent);
}
#chat-send-btn {
    width: 36px;
    height: 36px;
    border-radius: 8px;
    background: var(--chat-accent);
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: background 0.15s, transform 0.1s;
}
#chat-send-btn:hover {
    background: #06b6d4;
}
#chat-send-btn:active {
    transform: scale(0.93);
}
#chat-send-btn svg {
    width: 16px;
    height: 16px;
    stroke: #0f1117;
    fill: none;
    stroke-width: 2.2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

/* ══════════════════════════════════════════════════
   닉네임 변경 모달
══════════════════════════════════════════════════ */
#nick-modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1100;
    align-items: center;
    justify-content: center;
    background: rgba(0,0,0,0.6);
    backdrop-filter: blur(3px);
}
#nick-modal.nick-modal-open {
    display: flex;
}
.nick-modal-box {
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    padding: 24px 20px 20px;
    width: 300px;
    max-width: 90vw;
    font-family: var(--chat-font);
}
.nick-modal-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--chat-text);
    margin-bottom: 4px;
}
.nick-modal-desc {
    font-size: 11px;
    color: var(--chat-text-muted);
    margin-bottom: 14px;
}
#nick-modal-input {
    width: 100%;
    box-sizing: border-box;
    background: var(--chat-surface2);
    border: 1px solid var(--chat-border);
    border-radius: 8px;
    color: var(--chat-text);
    font-size: 13px;
    font-family: var(--chat-font);
    padding: 9px 12px;
    outline: none;
    transition: border-color 0.15s;
    margin-bottom: 14px;
}
#nick-modal-input:focus {
    border-color: var(--chat-accent);
}
.nick-modal-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
}
#nick-modal-cancel {
    background: none;
    border: 1px solid var(--chat-border);
    border-radius: 7px;
    color: var(--chat-text-muted);
    font-size: 12px;
    font-family: var(--chat-font);
    padding: 7px 14px;
    cursor: pointer;
    transition: border-color 0.15s;
}
#nick-modal-cancel:hover { border-color: var(--chat-text-muted); }
#nick-modal-confirm {
    background: var(--chat-accent);
    border: none;
    border-radius: 7px;
    color: #0f1117;
    font-size: 12px;
    font-weight: 600;
    font-family: var(--chat-font);
    padding: 7px 16px;
    cursor: pointer;
    transition: background 0.15s;
}
#nick-modal-confirm:hover { background: #06b6d4; }

/* ══════════════════════════════════════════════════
   모바일 최적화
══════════════════════════════════════════════════ */
@media (max-width: 480px) {
    #chat-window {
        right: 0;
        bottom: 0;
        width: 100vw;
        height: 60vh;
        border-radius: var(--chat-radius) var(--chat-radius) 0 0;
        border-left: none;
        border-right: none;
        border-bottom: none;
    }
    #chat-toggle-btn {
        bottom: 16px;
        right: 16px;
    }
    #chat-window.chat-open {
        /* 모바일에서 키보드 올라와도 괜찮도록 */
        bottom: 0;
    }
}