/* =========================================
   1. Theme Variables (색상 및 상수 변수)
   ========================================= */
:root {
    /* [기본: 다크 테마] */
    --bg-body: #343541;
    --bg-header: #202123;
    --bg-ai-msg: #444654;
    --bg-user-msg: #343541;
    --bg-input-area: #343541;
    --bg-input-wrapper: #40414f;
    --bg-hover: #40414f;
    --bg-intro: #343541;
    
    --bg-ref-group: rgba(0,0,0,0.2);
    --bg-ref-title: rgba(255,255,255,0.03);
    --bg-ref-title-hover: rgba(255,255,255,0.08);
    --bg-ref-content: rgba(0,0,0,0.1);

    --text-primary: #ffffff;
    --text-secondary: #ececf1;
    --text-placeholder: #8e8ea0;
    --text-code: #ececf1;
    --text-ref-item: #d1d5db;

    --border-color: #565869;
    --border-light: #4d4d4f;
    --border-msg: #2d2f3a;

    --primary-color: #10a37f; /* SAGA Green */
    --primary-hover: #0d8a6a;
    --shadow-input: 0 0 15px rgba(0,0,0,0.1);
    --logo-filter: invert(1);
}

body.light-theme {
    --bg-body: #ffffff;
    --bg-header: #ffffff;
    --bg-ai-msg: #f7f7f8;
    --bg-user-msg: #ffffff;
    --bg-input-area: #ffffff;
    --bg-input-wrapper: #ffffff;
    --bg-hover: #ececf1;
    --bg-intro: #ffffff;
    
    --bg-ref-group: #f0f0f0;
    --bg-ref-title: rgba(0,0,0,0.03);
    --bg-ref-title-hover: rgba(0,0,0,0.06);
    --bg-ref-content: rgba(0,0,0,0.02);

    --text-primary: #343541;
    --text-secondary: #6e6e80;
    --text-placeholder: #8e8ea0;
    --text-code: #343541;
    --text-ref-item: #40414f;

    --border-color: #d9d9e3;
    --border-light: #e5e5e5;
    --border-msg: #e5e5e5;

    --shadow-input: 0 0 10px rgba(0,0,0,0.08);
    --logo-filter: none;
}

/* 관련 기술기준 문서 제목 줄임 처리 */
.ref-title-text {
    flex: 1;
    overflow: hidden;         /* 영역을 벗어나는 텍스트 숨김 */
    text-overflow: ellipsis;  /* 말줄임표(...) 표시 🚀 */
    white-space: nowrap;      /* 텍스트가 줄바꿈되지 않도록 설정 */
    margin-right: 15px;       /* 우측 버튼과의 간격 확보 */
    font-weight: bold;
    color: var(--primary-color);
}

/* =========================================
   2. Reset & Base Styles
   ========================================= */
* { box-sizing: border-box; }

html, body {
    margin: 0; padding: 0;
    width: 100%; height: 100%;
    overflow-x: hidden;
}

body { 
    font-family: 'Noto Sans KR', sans-serif; 
    background-color: var(--bg-body); 
    display: flex; flex-direction: column; 
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.3s, color 0.3s;
}

.main-content {
    flex: 1; display: flex; flex-direction: column;
    overflow: hidden; position: relative;
}

/* =========================================
   3. Header & Navigation
   ========================================= */
.header {
    background-color: var(--bg-header);
    padding: 10px 20px;
    display: flex; align-items: center; justify-content: space-between; 
    border-bottom: 1px solid var(--border-light);
    flex-shrink: 0; position: relative; min-height: 60px;
}

.logo-area { display: flex; align-items: center; z-index: 101; cursor: pointer; }
.logo-area img { height: 50px; width: auto; filter: var(--logo-filter); }
.logo-area:hover { opacity: 0.8; }

.logo-text {
    position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
    display: flex; flex-direction: column; align-items: center;
    cursor: pointer; transition: opacity 0.2s;
}

.logo-text .main-title { font-size: 1.2em; font-weight: 800; color: var(--text-primary); letter-spacing: 1px; }
.logo-text .sub-title { font-size: 0.75em; color: var(--text-placeholder); font-weight: 400; }

.btn-group-right { display: flex; align-items: center; gap: 8px; }

.btn-new-chat, .btn-theme-toggle, .btn-admin-right {
    background-color: transparent; border: 1px solid var(--border-color);
    color: var(--text-secondary); border-radius: 6px; cursor: pointer;
    transition: all 0.2s ease; display: flex; align-items: center; justify-content: center;
}

.btn-new-chat { padding: 6px 12px; font-size: 0.85em; font-weight: 500; gap: 6px; }
.btn-theme-toggle, .btn-admin-right { width: 32px; height: 32px; padding: 0; }

.btn-new-chat:hover, .btn-theme-toggle:hover, .btn-admin-right:hover {
    background-color: var(--bg-hover); color: var(--text-primary); border-color: var(--primary-color);
}

/* =========================================
   4. Intro Screen (초기 화면)
   ========================================= */
#intro-screen {
    display: none; flex: 1; flex-direction: column;
    align-items: center; justify-content: center; width: 100%;
}

body.intro-mode .header, body.intro-mode #chat-container { display: none; }
body.intro-mode #intro-screen { display: flex; }

.intro-logo {
    width: 50vw; max-width: 400px; height: auto; max-height: 35vh;
    object-fit: contain; filter: var(--logo-filter); margin-bottom: 20px;
}

.intro-text-box {
    text-align: center; display: flex; flex-direction: column; gap: 8px;
    animation: fadeInMove 1s ease-out forwards;
}

@keyframes fadeInMove {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 상태 메시지 전체 페이드인 효과 */
@keyframes fadeInStatus {
    from { opacity: 0; transform: translateY(5px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 이 클래스가 붙으면 애니메이션이 작동함 */
.status-fade {
    display: inline-block;
    animation: fadeInStatus 0.5s ease-out forwards;
    color: var(--primary-color); /* 진행 중임을 알리기 위해 색상을 강조해도 좋습니다 */
    font-weight: 500;
}

/* 글자 하나하나가 나타날 때 효과 */
@keyframes characterFadeIn {
    from { opacity: 0; transform: translateY(2px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 타이핑 중인 글자에 붙을 클래스 */
.typing-char {
    display: inline-block;
    opacity: 0;
    animation: characterFadeIn 0.3s ease-out forwards;
    color: var(--primary-color);
    font-weight: 500;
    /* 🚀 추가: 공백 및 줄바꿈을 있는 그대로 표시 */
    white-space: pre-wrap; 
}

/* 부드럽게 깜빡거리는(Pulse) 무한 반복 애니메이션 */
@keyframes pulseLoop {
    0% { opacity: 0.4; }
    50% { opacity: 1; text-shadow: 0 0 6px rgba(150, 150, 150, 0.4); } /* 살짝 빛나는 효과 */
    100% { opacity: 0.3; }
}

.status-loop {
    display: inline-block;
    color: var(--text-placeholder);
    font-weight: 500;
    /* 1.5초 주기로 무한(infinite) 반복 */
    animation: pulseLoop 1.5s infinite ease-in-out; 
}

.intro-title { font-size: clamp(1.2rem, 2.5vw, 1.8rem); font-weight: 700; }
.intro-desc { font-size: 0.9rem; color: var(--text-placeholder); }

.intro-model-wrapper {
    margin-top: 25px; display: flex; justify-content: center; align-items: center; gap: 10px;
}

/* =========================================
   5. Chat Area (메시지 레이아웃)
   ========================================= */
#chat-container { flex: 1; overflow-y: auto; overflow-x: hidden; width: 100%; padding-bottom: 20px; }

.message-row { display: flex; justify-content: center; padding: 25px 20px; border-bottom: 1px solid var(--border-msg); width: 100%; }
.message-row.ai { background-color: var(--bg-ai-msg); }
.message-row.user { background-color: var(--bg-user-msg); justify-content: center; }

.message-content { display: flex; gap: 15px; max-width: 800px; width: 100%; align-items: flex-start; min-width: 0; }
.message-row.user .message-content { flex-direction: row-reverse; }

.sender-name { font-weight: 700; font-size: 15px; margin-top: 8px; min-width: 45px; flex-shrink: 0; }
.message-row.ai .sender-name { color: var(--primary-color); text-align: right; }

.message-row .text { flex: 1; min-width: 0; line-height: 1.6; font-size: 16px; word-break: break-word; }
.message-row.user .text { 
    background-color: #5436DA; color: white; padding: 10px 16px; 
    border-radius: 18px; border-top-right-radius: 4px; flex: 0 1 auto; max-width: 80%;
}

/* =========================================
   6. Components (Tables, References, Feedback)
   ========================================= */
/* Markdown Table */
.table-scroll { overflow-x: auto; margin: 15px 0; border: 1px solid var(--border-color); border-radius: 8px; }
.md-table { width: 100%; border-collapse: collapse; min-width: 600px; font-size: 0.9em; }
.md-table th { background: var(--primary-color); color: #fff; padding: 12px; text-align: left; }
.md-table td { padding: 12px; border-bottom: 1px solid var(--border-color); }
.md-table tbody tr:nth-child(even) { background: rgba(0,0,0,0.03); }

/* References (참고문헌 박스) */
.reference-box { margin-top: 20px; padding-top: 15px; border-top: 1px solid var(--border-color); }
.ref-group { margin-bottom: 8px; background: var(--bg-ref-group); border-radius: 6px; overflow: hidden; }
.ref-title { 
    display: flex; justify-content: space-between; align-items: center;
    padding: 12px 15px; cursor: pointer; background: var(--bg-ref-title);
    font-weight: bold; font-size: 0.95em; color: var(--primary-color);
}
.ref-header-right { display: flex; align-items: center; gap: 12px; }

/* 🚀 [추가] PDF 전체 다운로드 버튼 */
.btn-header-download {
    background-color: var(--bg-body); 
    color: var(--text-secondary); 
    border: 1px solid var(--border-color); 
    border-radius: 4px; 
    padding: 4px 10px; font-size: 0.75em; 
    cursor: pointer; text-decoration: none; 
    transition: all 0.2s; display: inline-flex; align-items: center; gap: 4px;
}
.btn-header-download:hover { 
    background-color: var(--primary-color); color: white; border-color: var(--primary-color); 
}

.ref-content { display: none; padding: 10px; background: var(--bg-ref-content); }
.ref-content.show { display: block; }

/* 🚀 [추가] PDF 조항 바로가기 버튼 */
.btn-view-pdf { 
    display: inline-block; 
    margin-top: 8px; 
    color: #ffffff !important; /* 라이트 모드에서도 글자는 흰색 유지 */
    background-color: var(--primary-color); 
    text-decoration: none; 
    font-size: 0.8em; 
    padding: 4px 10px; 
    border-radius: 4px; 
    cursor: pointer; /* 클릭 가능 커서 */
    transition: background 0.2s ease;
    border: none;
}
.btn-view-pdf:hover { 
    background-color: var(--primary-hover); 
    transform: translateY(-1px);
}

.ref-item { font-size: 0.85em; padding-left: 10px; border-left: 2px solid var(--border-color); margin-top: 8px; color: var(--text-ref-item); }

/* Feedback (도움됨/별로임 버튼) */
.feedback-area { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; border-top: 1px solid var(--border-msg); padding-top: 8px; }
.btn-feedback { 
    background: transparent; border: 1px solid var(--border-color); color: var(--text-placeholder);
    padding: 4px 12px; border-radius: 20px; font-size: 0.8rem; cursor: pointer; transition: 0.2s;
}
.btn-feedback:hover, .btn-feedback.active { background: var(--primary-color); color: #fff; border-color: var(--primary-color); }


/* =========================================
   7. Input Area
   ========================================= */
.input-area { 
    width: 100%; background-color: var(--bg-input-area); 
    padding: 10px 20px 20px; display: flex; flex-direction: column; align-items: center; 
}

.search-mode-selector { display: flex; gap: 8px; margin-bottom: 12px; max-width: 800px; width: 100%; }
.mode-btn { 
    padding: 6px 14px; border-radius: 8px; border: 1px solid var(--border-color);
    background: var(--bg-header); color: var(--text-placeholder); cursor: pointer; font-size: 0.82rem;
}
.mode-btn.active { background: var(--primary-color); color: #fff; border-color: var(--primary-color); font-weight: 600; }

.input-wrapper { 
    max-width: 800px; width: 100%; display: flex; 
    background-color: var(--bg-input-wrapper); border-radius: 12px; 
    box-shadow: var(--shadow-input); border: 1px solid var(--border-color); 
}
.input-wrapper input { flex: 1; background: transparent; border: none; padding: 16px; color: var(--text-primary); font-size: 16px; outline: none; }
.input-wrapper button { background: transparent; border: none; padding: 0 15px; cursor: pointer; color: var(--text-secondary); }
.input-wrapper button:hover { color: var(--primary-color); }

.disclaimer { margin-top: 12px; font-size: 11px; color: var(--text-placeholder); opacity: 0.8; }

/* =========================================
   8. PDF Modal & Other UI
   ========================================= */
#pdf-modal { 
    display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; 
    background: rgba(0,0,0,0.85); z-index: 9999; flex-direction: column; align-items: center;
}
#pdf-container { width: 100%; height: 100%; }
.pdf-toolbar { display: flex; background: #333; color: #fff; padding: 10px; gap: 10px; align-items: center; width: 100%; }
#pdf-canvas-container { flex: 1; overflow: auto; background: #525659; width: 100%; display: flex; justify-content: center; }

.model-selector { 
    background: transparent; border: 1px solid var(--border-color); color: var(--text-secondary);
    padding: 6px 10px; border-radius: 6px; cursor: pointer; outline: none;
}

/* =========================================
   9. Mobile Responsive
   ========================================= */
@media screen and (max-width: 768px) {
    .header { padding: 10px 15px; min-height: 50px; }
    .logo-area img { height: 35px; }
    .logo-text { display: none; }
    
    .intro-logo { width: 40vw; max-width: 150px; }
    
    #chat-container { padding-bottom: 180px; }
    .message-row { padding: 20px 15px; }
    .message-row.user .text { max-width: 90%; }
    
    .input-area { position: fixed; bottom: 0; left: 0; border-top: 1px solid var(--border-light); z-index: 1000; }
    .mode-btn { flex: 1; text-align: center; }

    /* 모바일 인트로 보정 */
    body.intro-mode #intro-screen { padding-bottom: 180px; }
    body.intro-mode .input-area { position: fixed !important; bottom: 0 !important; }
    
    .md-table { min-width: 500px; }
}


/* 로딩 인디케이터 (Typing Indicator) 스타일 */
.typing-indicator span { 
    display: inline-block; 
    width: 6px; 
    height: 6px; 
    background-color: var(--text-placeholder); 
    border-radius: 50%; 
    animation: typing 1.4s infinite ease-in-out both; 
    margin-right: 4px; 
}
.typing-indicator span:nth-child(1) { animation-delay: -0.32s; }
.typing-indicator span:nth-child(2) { animation-delay: -0.16s; }

@keyframes typing { 
    0%, 80%, 100% { transform: scale(0); } 
    40% { transform: scale(1); } 
}