/* ─── 초기화 ─────────────────────────────────────────────────────────────── */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --sidebar-bg:   #171717;
    --chat-bg:      #212121;
    --user-bubble:  #2f2f2f;
    --lg-red:       #a50034;
    --lg-red-hover: #c0003d;
    --text-primary: #ececec;
    --text-muted:   #8e8ea0;
    --border:       #2f2f2f;
    --input-bg:     #2f2f2f;
    --sidebar-w:    260px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans KR', sans-serif;
    color: var(--text-primary);
    background: var(--chat-bg);
    height: 100vh;
    overflow: hidden;
}

/* ─── 로그인 페이지 ──────────────────────────────────────────────────────── */
.login-page {
    background: linear-gradient(135deg, #0a0a0a 0%, #1a1a2e 50%, #16213e 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100vh;
    overflow: auto;
}

.login-container {
    width: 100%;
    max-width: 400px;
    padding: 1.5rem;
}

.login-card {
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 16px;
    padding: 2.5rem 2rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.login-logo {
    width: 72px;
    height: 72px;
    background: var(--lg-red);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 0.25rem;
}

.lg-text {
    font-size: 1.5rem;
    font-weight: 900;
    color: #fff;
    letter-spacing: -1px;
}

.login-title {
    font-size: 1.2rem;
    font-weight: 600;
    color: var(--text-primary);
    text-align: center;
    margin-bottom: 0.25rem;
}

.login-error {
    width: 100%;
    background: rgba(165, 0, 52, 0.12);
    border: 1px solid rgba(165, 0, 52, 0.35);
    border-radius: 8px;
    padding: 0.7rem 1rem;
    color: #ff6b6b;
    font-size: 0.875rem;
    text-align: center;
}

.login-form {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.form-input {
    width: 100%;
    background: #252525;
    border: 1px solid #3a3a3a;
    border-radius: 10px;
    padding: 0.85rem 1rem;
    color: var(--text-primary);
    font-size: 1rem;
    outline: none;
    transition: border-color 0.2s;
}

.form-input:focus {
    border-color: var(--lg-red);
}

.form-input::placeholder {
    color: var(--text-muted);
}

.login-btn {
    width: 100%;
    background: var(--lg-red);
    color: #fff;
    border: none;
    border-radius: 10px;
    padding: 0.85rem;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s;
    margin-top: 0.25rem;
}

.login-btn:hover {
    background: var(--lg-red-hover);
}

/* ─── 채팅 레이아웃 ──────────────────────────────────────────────────────── */
.layout {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ─── 사이드바 ───────────────────────────────────────────────────────────── */
.sidebar {
    width: var(--sidebar-w);
    background: var(--sidebar-bg);
    display: flex;
    flex-direction: column;
    padding: 0.875rem 0.75rem;
    border-right: 1px solid var(--border);
    flex-shrink: 0;
    gap: 0.75rem;
}

.sidebar-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid var(--border);
}

.sidebar-logo-box {
    width: 38px;
    height: 38px;
    background: var(--lg-red);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 900;
    color: #fff;
    letter-spacing: -0.5px;
    flex-shrink: 0;
}

.sidebar-brand {
    font-size: 0.8rem;
    font-weight: 600;
    line-height: 1.35;
    color: var(--text-primary);
}

.sidebar-section {
    /* no extra margin needed with gap */
}

.sidebar-label {
    display: block;
    font-size: 0.72rem;
    color: var(--text-muted);
    margin-bottom: 0.35rem;
    font-weight: 500;
}

.model-select {
    width: 100%;
    background: #252525;
    border: 1px solid #3a3a3a;
    border-radius: 8px;
    padding: 0.5rem 0.625rem;
    color: var(--text-primary);
    font-size: 0.78rem;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s;
}

.model-select:focus {
    border-color: var(--lg-red);
}

.model-select optgroup {
    color: var(--text-muted);
}

.model-select option {
    background: #252525;
    color: var(--text-primary);
}

.new-chat-btn {
    display: flex;
    align-items: center;
    gap: 0.45rem;
    width: 100%;
    background: transparent;
    border: 1px solid #3a3a3a;
    border-radius: 8px;
    padding: 0.55rem 0.75rem;
    color: var(--text-primary);
    font-size: 0.84rem;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
}

.new-chat-btn:hover {
    background: #252525;
    border-color: #4a4a4a;
}

/* conv-list가 남은 공간을 차지하고 footer를 아래로 밀어냄 */

/* ─── 대화 목록 ──────────────────────────────────────────────────────────── */
.conv-list {
    flex: 1;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-height: 0;
}

.conv-list::-webkit-scrollbar { width: 3px; }
.conv-list::-webkit-scrollbar-track { background: transparent; }
.conv-list::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 2px; }

.conv-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.4rem;
    padding: 0.5rem 0.625rem;
    border-radius: 8px;
    border: 1px solid transparent;
    cursor: pointer;
    transition: background 0.15s, border-color 0.15s;
    min-width: 0;
}

.conv-item:hover {
    background: #252525;
    border-color: #3a3a3a;
}

.conv-item.active {
    background: #2a2a2a;
    border-color: #4a4a4a;
}

.conv-title {
    font-size: 0.78rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
    min-width: 0;
}

.conv-item.active .conv-title {
    color: var(--text-primary);
}

.conv-delete {
    background: transparent;
    border: none;
    color: transparent;
    font-size: 0.7rem;
    cursor: pointer;
    padding: 0 2px;
    flex-shrink: 0;
    line-height: 1;
    transition: color 0.15s;
}

.conv-item:hover .conv-delete {
    color: var(--text-muted);
}

.conv-delete:hover {
    color: #ff6b6b !important;
}

.sidebar-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-top: 0.75rem;
    border-top: 1px solid var(--border);
}

.sidebar-username {
    font-size: 0.78rem;
    color: var(--text-muted);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.logout-btn {
    font-size: 0.74rem;
    color: var(--text-muted);
    text-decoration: none;
    padding: 0.25rem 0.5rem;
    border-radius: 6px;
    transition: background 0.15s, color 0.15s;
    white-space: nowrap;
    flex-shrink: 0;
}

.logout-btn:hover {
    background: #2a2a2a;
    color: var(--text-primary);
}

/* ─── 채팅 메인 ──────────────────────────────────────────────────────────── */
.chat-main {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--chat-bg);
    overflow: hidden;
    min-width: 0;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1.75rem 1.25rem 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar { width: 5px; }
.chat-messages::-webkit-scrollbar-track { background: transparent; }
.chat-messages::-webkit-scrollbar-thumb { background: #3a3a3a; border-radius: 3px; }

/* ─── 메시지 ─────────────────────────────────────────────────────────────── */
.message {
    display: flex;
    gap: 0.75rem;
    max-width: 820px;
    width: 100%;
}

.bot-message {
    align-self: flex-start;
    margin-right: auto;
}

.user-message {
    align-self: flex-end;
    flex-direction: row-reverse;
    margin-left: auto;
}

.message-avatar {
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.65rem;
    font-weight: 800;
    flex-shrink: 0;
    margin-top: 3px;
}

.bot-avatar  { background: var(--lg-red); color: #fff; }
.user-avatar { background: #484848; color: var(--text-primary); }

.message-content {
    font-size: 0.9375rem;
    line-height: 1.65;
    max-width: calc(100% - 42px);
    white-space: pre-wrap;
    word-break: break-word;
}

.bot-message .message-content {
    background: transparent;
    padding: 0;
}

.user-message .message-content {
    background: var(--user-bubble);
    padding: 0.7rem 1rem;
    border-radius: 16px 16px 4px 16px;
}

/* ─── 참조 문서 ──────────────────────────────────────────────────────────── */
.sources-container {
    margin-top: 0.75rem;
}

.sources-toggle {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    background: transparent;
    border: 1px solid #3a3a3a;
    border-radius: 8px;
    padding: 0.35rem 0.7rem;
    color: var(--text-muted);
    font-size: 0.78rem;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
    font-family: inherit;
}

.sources-toggle:hover {
    background: #2a2a2a;
    color: var(--text-primary);
}

.sources-toggle .arrow {
    display: inline-block;
    transition: transform 0.2s;
    font-size: 0.65rem;
}

.sources-toggle.open .arrow {
    transform: rotate(180deg);
}

.sources-list {
    display: none;
    flex-direction: column;
    gap: 0.45rem;
    margin-top: 0.5rem;
    padding: 0.75rem;
    background: #1a1a1a;
    border: 1px solid var(--border);
    border-radius: 8px;
}

.sources-list.open {
    display: flex;
}

.source-item {
    display: flex;
    align-items: baseline;
    gap: 0.4rem;
    font-size: 0.78rem;
}

.source-num   { color: var(--text-muted); min-width: 1.4em; }
.source-link  { color: #69b4ff; text-decoration: none; flex: 1; line-height: 1.4; }
.source-link:hover { text-decoration: underline; }
.source-score { color: var(--text-muted); white-space: nowrap; font-size: 0.72rem; }

/* ─── 타이핑 인디케이터 ──────────────────────────────────────────────────── */
.typing-indicator {
    display: flex;
    gap: 5px;
    padding: 0.4rem 0;
    align-items: center;
}

.typing-dot {
    width: 7px;
    height: 7px;
    background: var(--text-muted);
    border-radius: 50%;
    animation: typing-bounce 1.2s infinite ease-in-out;
}

.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typing-bounce {
    0%, 60%, 100% { transform: translateY(0); opacity: 0.6; }
    30%            { transform: translateY(-7px); opacity: 1; }
}

/* ─── 입력 영역 ──────────────────────────────────────────────────────────── */
.chat-input-area {
    padding: 0.75rem 1.25rem 1.5rem;
    background: var(--chat-bg);
}

.input-container {
    max-width: 820px;
    margin: 0 auto;
    display: flex;
    align-items: flex-end;
    gap: 0.5rem;
    background: var(--input-bg);
    border: 1px solid #3a3a3a;
    border-radius: 14px;
    padding: 0.65rem 0.65rem 0.65rem 1rem;
    transition: border-color 0.2s;
}

.input-container:focus-within {
    border-color: #555;
}

.message-input {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-primary);
    font-size: 0.9375rem;
    line-height: 1.5;
    resize: none;
    outline: none;
    max-height: 160px;
    overflow-y: auto;
    font-family: inherit;
}

.message-input::placeholder { color: var(--text-muted); }

.send-btn {
    width: 34px;
    height: 34px;
    background: var(--lg-red);
    border: none;
    border-radius: 9px;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s, opacity 0.2s;
    flex-shrink: 0;
}

.send-btn:hover:not(:disabled) { background: var(--lg-red-hover); }
.send-btn:disabled { opacity: 0.35; cursor: not-allowed; }

/* ─── 에러 메시지 ────────────────────────────────────────────────────────── */
.error-message .message-content {
    color: #ff6b6b;
}

/* ─── 반응형 ─────────────────────────────────────────────────────────────── */
@media (max-width: 600px) {
    .sidebar { display: none; }
    .chat-messages { padding: 1rem 0.75rem 0.5rem; }
    .chat-input-area { padding: 0.5rem 0.75rem 1rem; }
}
