* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body, #app {
    height: 100%;
    width: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

/* ── 테마 변수 ── */
.theme-light {
    --bg-main: #ffffff;
    --bg-side: #f7f7f8;
    --bg-msg-user: #19c37d;
    --bg-msg-ai: #f4f4f5;
    --text: #1a1a1a;
    --text-muted: #6e6e80;
    --text-msg-user: #ffffff;
    --text-msg-ai: #1a1a1a;
    --border: #e5e5e5;
    --input-bg: #ffffff;
    --input-shadow: 0 2px 16px rgba(0,0,0,0.08);
    --header-bg: rgba(255,255,255,0.9);
    --chat-item-hover: #efefef;
    --chat-item-active: #e4e4e7;
    --send-btn: #19c37d;
    --send-btn-hover: #17b371;
    --welcome-icon: #19c37d;
    --code-bg: #f6f8fa;
    --code-border: #e1e4e8;
    --blockquote-border: #d0d0d0;
    --table-border: #e0e0e0;
    --table-head-bg: #f6f8fa;
}

.theme-black {
    --bg-main: #0d0d0d;
    --bg-side: #171717;
    --bg-msg-user: #2d6a4f;
    --bg-msg-ai: #1e1e1e;
    --text: #ececf1;
    --text-muted: #8e8ea0;
    --text-msg-user: #ffffff;
    --text-msg-ai: #ececf1;
    --border: #2d2d2d;
    --input-bg: #1e1e1e;
    --input-shadow: 0 2px 16px rgba(0,0,0,0.5);
    --header-bg: rgba(13,13,13,0.9);
    --chat-item-hover: #252525;
    --chat-item-active: #2f2f2f;
    --send-btn: #19c37d;
    --send-btn-hover: #17b371;
    --welcome-icon: #19c37d;
    --code-bg: #161b22;
    --code-border: #30363d;
    --blockquote-border: #444;
    --table-border: #333;
    --table-head-bg: #1e1e1e;
}

.theme-gradient {
    --bg-main: #f0f2f5;
    --bg-side: #e4e7ed;
    --bg-msg-user: #5b5fc7;
    --bg-msg-ai: #ffffff;
    --text: #1a1a2e;
    --text-muted: #7070a0;
    --text-msg-user: #ffffff;
    --text-msg-ai: #1a1a2e;
    --border: #d0d4e0;
    --input-bg: #ffffff;
    --input-shadow: 0 2px 20px rgba(91,95,199,0.12);
    --header-bg: rgba(240,242,245,0.9);
    --chat-item-hover: #dde0ea;
    --chat-item-active: #d0d4e0;
    --send-btn: #5b5fc7;
    --send-btn-hover: #4a4eb5;
    --welcome-icon: #5b5fc7;
    --code-bg: #f0f0ff;
    --code-border: #c8c8e8;
    --blockquote-border: #aab;
    --table-border: #ccd;
    --table-head-bg: #eeeef8;
}

/* ── 레이아웃 ── */
#app {
    display: flex;
    height: 100%;
    background: var(--bg-main);
    color: var(--text);
    transition: background 0.2s, color 0.2s;
}

/* ── 사이드바 ── */
#sidebar {
    width: 260px;
    min-width: 260px;
    background: var(--bg-side);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: width 0.3s ease, min-width 0.3s ease, opacity 0.3s ease;
    overflow: hidden;
    z-index: 100;
}

#sidebar.closed {
    width: 0;
    min-width: 0;
    opacity: 0;
    border-right: none;
}

.sidebar-header {
    padding: 14px 12px;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    flex-shrink: 0;
}

.sidebar-title {
    font-weight: 700;
    font-size: 0.92rem;
    color: var(--text);
    white-space: nowrap;
}

#new-chat-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 6px 11px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    background: var(--send-btn);
    color: #fff;
    border: none;
    border-radius: 8px;
    white-space: nowrap;
    transition: background 0.15s;
    flex-shrink: 0;
}
#new-chat-btn:hover { background: var(--send-btn-hover); }

/* 채팅 목록 */
#chat-list {
    flex: 1;
    overflow-y: auto;
    padding: 6px;
}
#chat-list::-webkit-scrollbar { width: 4px; }
#chat-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.chat-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 9px 10px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.84rem;
    color: var(--text);
    transition: background 0.15s;
    margin-bottom: 2px;
    gap: 6px;
}
.chat-item:hover { background: var(--chat-item-hover); }
.chat-item.active { background: var(--chat-item-active); font-weight: 500; }

.chat-item-title {
    flex: 1;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-item-menu {
    display: none;
    align-items: center;
    justify-content: center;
    width: 22px;
    height: 22px;
    border-radius: 5px;
    background: transparent;
    border: none;
    color: var(--text-muted);
    cursor: pointer;
    font-size: 0.85rem;
    flex-shrink: 0;
    transition: background 0.1s;
}
.chat-item:hover .chat-item-menu { display: flex; }
.chat-item-menu:hover { background: var(--border); }

.chat-list-empty {
    padding: 20px 12px;
    font-size: 0.8rem;
    color: var(--text-muted);
    text-align: center;
}

/* 사이드바 하단 */
.sidebar-footer {
    padding: 12px;
    border-top: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    gap: 6px;
    flex-shrink: 0;
}
.footer-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 500;
    white-space: nowrap;
}
#theme-select {
    width: 100%;
    padding: 7px 10px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--input-bg);
    color: var(--text);
    font-size: 0.85rem;
    cursor: pointer;
    outline: none;
}

/* ── 메인 채팅 영역 ── */
#chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    min-width: 0;
    position: relative;
}

header {
    height: 54px;
    display: flex;
    align-items: center;
    padding: 0 16px;
    border-bottom: 1px solid var(--border);
    background: var(--header-bg);
    backdrop-filter: blur(8px);
    gap: 12px;
    flex-shrink: 0;
    z-index: 10;
}

#toggle-sidebar {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: transparent;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--text);
    transition: background 0.15s;
    flex-shrink: 0;
}
#toggle-sidebar:hover { background: var(--chat-item-hover); }

#current-model-display {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text);
}

/* 메시지 영역 */
#messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px 0 180px;
    scroll-behavior: smooth;
}
#messages::-webkit-scrollbar { width: 4px; }
#messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* 웰컴 */
.welcome-msg {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 80px 20px 20px;
    gap: 12px;
}
.welcome-icon { font-size: 2.5rem; color: var(--welcome-icon); line-height: 1; }
.welcome-msg h2 { font-size: 1.4rem; font-weight: 700; color: var(--text); }
.welcome-msg p { font-size: 0.9rem; color: var(--text-muted); }

/* 메시지 행 */
.message-row {
    display: flex;
    padding: 5px 16px;
    max-width: 820px;
    margin: 0 auto;
    width: 100%;
}
.message-row.user { justify-content: flex-end; }
.message-row.assistant { justify-content: flex-start; }

/* 메시지 버블 */
.message-bubble {
    max-width: 78%;
    padding: 11px 16px;
    border-radius: 18px;
    font-size: 0.93rem;
    line-height: 1.65;
    word-break: break-word;
}

.message-row.user .message-bubble {
    background: var(--bg-msg-user);
    color: var(--text-msg-user);
    border-bottom-right-radius: 4px;
    white-space: pre-wrap;
}

.message-row.assistant .message-bubble {
    background: var(--bg-msg-ai);
    color: var(--text-msg-ai);
    border-bottom-left-radius: 4px;
    border: 1px solid var(--border);
}

/* 사용자 버블 첨부 이미지 */
.bubble-img {
    display: block;
    max-width: 260px;
    max-height: 200px;
    border-radius: 10px;
    margin-bottom: 6px;
    object-fit: cover;
}

/* ── Markdown 스타일 (AI 버블 내부) ── */
.message-bubble.md h1,
.message-bubble.md h2,
.message-bubble.md h3,
.message-bubble.md h4 {
    margin: 14px 0 6px;
    font-weight: 700;
    line-height: 1.3;
}
.message-bubble.md h1 { font-size: 1.25em; }
.message-bubble.md h2 { font-size: 1.1em; }
.message-bubble.md h3 { font-size: 1em; }

.message-bubble.md p { margin: 6px 0; }

.message-bubble.md ul,
.message-bubble.md ol {
    margin: 6px 0 6px 20px;
    padding: 0;
}
.message-bubble.md li { margin: 3px 0; }

.message-bubble.md code {
    font-family: 'SFMono-Regular', Consolas, 'Courier New', monospace;
    font-size: 0.85em;
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    border-radius: 4px;
    padding: 1px 5px;
}

.message-bubble.md pre {
    margin: 10px 0;
    border-radius: 10px;
    overflow: hidden;
    border: 1px solid var(--code-border);
}

.message-bubble.md pre .code-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 6px 12px;
    background: var(--code-border);
    font-size: 0.75rem;
    color: var(--text-muted);
    font-family: monospace;
}

.message-bubble.md pre .copy-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 2px 6px;
    border-radius: 4px;
    transition: background 0.1s;
}
.message-bubble.md pre .copy-btn:hover { background: rgba(0,0,0,0.1); }

.message-bubble.md pre code {
    display: block;
    padding: 14px 16px;
    background: var(--code-bg);
    border: none;
    border-radius: 0;
    font-size: 0.83em;
    overflow-x: auto;
    line-height: 1.6;
}

.message-bubble.md blockquote {
    border-left: 3px solid var(--blockquote-border);
    padding: 4px 12px;
    margin: 8px 0;
    color: var(--text-muted);
    font-style: italic;
}

.message-bubble.md table {
    border-collapse: collapse;
    width: 100%;
    margin: 10px 0;
    font-size: 0.88em;
}
.message-bubble.md th,
.message-bubble.md td {
    border: 1px solid var(--table-border);
    padding: 6px 12px;
    text-align: left;
}
.message-bubble.md th { background: var(--table-head-bg); font-weight: 600; }

.message-bubble.md a { color: var(--send-btn); text-decoration: underline; }
.message-bubble.md hr { border: none; border-top: 1px solid var(--border); margin: 12px 0; }
.message-bubble.md strong { font-weight: 700; }
.message-bubble.md em { font-style: italic; }

/* KaTeX 블록 수식 */
.message-bubble.md .katex-display {
    overflow-x: auto;
    padding: 6px 0;
}

/* 스트리밍 커서 */
.message-bubble.streaming::after {
    content: '▌';
    display: inline-block;
    color: var(--send-btn);
    animation: blink 0.7s step-end infinite;
    margin-left: 1px;
}
@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0; }
}

/* 로딩 애니메이션 */
.typing-indicator {
    display: flex;
    gap: 5px;
    align-items: center;
    padding: 4px 2px;
}
.typing-indicator span {
    width: 7px;
    height: 7px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typing 1.2s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
    0%, 80%, 100% { transform: scale(0.7); opacity: 0.4; }
    40% { transform: scale(1); opacity: 1; }
}

/* ── 입력창 ── */
.input-area {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 10px 16px 18px;
    background: linear-gradient(to top, var(--bg-main) 70%, transparent);
}

/* 이미지 미리보기 */
#image-preview-area {
    max-width: 820px;
    margin: 0 auto 8px;
}
#image-preview-list {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}
.preview-thumb {
    position: relative;
    width: 64px;
    height: 64px;
    border-radius: 8px;
    overflow: hidden;
    border: 1px solid var(--border);
    flex-shrink: 0;
}
.preview-thumb img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}
.preview-thumb .remove-img {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: rgba(0,0,0,0.55);
    color: #fff;
    border: none;
    font-size: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.input-box {
    max-width: 820px;
    margin: 0 auto;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 14px;
    box-shadow: var(--input-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: box-shadow 0.2s, border-color 0.2s;
}
.input-box:focus-within {
    box-shadow: var(--input-shadow), 0 0 0 2px var(--send-btn);
    border-color: var(--send-btn);
}

#user-input {
    width: 100%;
    border: none;
    background: transparent;
    color: var(--text);
    resize: none;
    outline: none;
    font-size: 0.95rem;
    line-height: 1.6;
    padding: 13px 16px 4px;
    max-height: 200px;
    overflow-y: auto;
    font-family: inherit;
}
#user-input::placeholder { color: var(--text-muted); }

.input-controls {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 5px 8px 7px;
    gap: 6px;
}

.input-left {
    display: flex;
    align-items: center;
    gap: 4px;
    min-width: 0;
}

#attach-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 7px;
    cursor: pointer;
    color: var(--text-muted);
    transition: background 0.15s, color 0.15s;
    flex-shrink: 0;
}
#attach-btn:hover { background: var(--chat-item-hover); color: var(--text); }

#model-select {
    padding: 5px 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: transparent;
    color: var(--text-muted);
    font-size: 0.76rem;
    cursor: pointer;
    outline: none;
    max-width: 190px;
    min-width: 0;
}

#send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    background: var(--send-btn);
    color: #fff;
    border: none;
    border-radius: 9px;
    cursor: pointer;
    transition: background 0.15s, transform 0.1s;
    flex-shrink: 0;
}
#send-btn:hover { background: var(--send-btn-hover); transform: scale(1.05); }
#send-btn:active { transform: scale(0.97); }
#send-btn:disabled { background: var(--border); cursor: not-allowed; transform: none; }

.input-hint {
    text-align: center;
    font-size: 0.7rem;
    color: var(--text-muted);
    margin-top: 5px;
    max-width: 820px;
    margin-left: auto;
    margin-right: auto;
}

/* ── 컨텍스트 메뉴 ── */
#ctx-menu {
    position: fixed;
    z-index: 9999;
    background: var(--input-bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    padding: 5px;
    display: none;
    flex-direction: column;
    min-width: 140px;
}
#ctx-menu.visible { display: flex; }
#ctx-menu button {
    background: transparent;
    border: none;
    padding: 8px 12px;
    font-size: 0.85rem;
    color: var(--text);
    cursor: pointer;
    border-radius: 7px;
    text-align: left;
    transition: background 0.1s;
}
#ctx-menu button:hover { background: var(--chat-item-hover); }
#ctx-delete { color: #e53e3e !important; }

/* highlight.js 다크모드 */
.theme-black #hljs-theme { display: none; }

/* ── 모바일 ── */
@media (max-width: 640px) {
    #sidebar {
        position: fixed;
        height: 100%;
        box-shadow: 2px 0 12px rgba(0,0,0,0.12);
    }
    .message-bubble { max-width: 90%; }
    .input-area { padding: 8px 10px 14px; }
}
