/* =============================================================================
 * SecureFin Chat UI — 대화 히스토리 채팅 인터페이스 + 로그인/세션
 * ============================================================================= */

/* hidden 속성이 붙은 화면은 항상 숨긴다. .auth-container/.app 등에 display:flex 가
   있어 HTML hidden 기본값(display:none)을 덮어써 로그인·회원가입·채팅 화면이 한 페이지에
   쌓여 스크롤되던 문제를 해결한다. 화면 전환(showLoginView/showChatView 등)은 hidden
   토글로만 이뤄지므로 이 한 줄로 단일 화면 전환이 보장된다. */
[hidden] { display: none !important; }

:root {
  --bg: #ffffff;
  --sidebar-bg: #f7f7f5;
  --sidebar-border: #ececea;
  --fg: #1f2023;
  --muted: #6b6f76;
  --faint: #9a9ea6;
  --border: #e8e8e6;
  --hover: #efefec;
  --active: #e6e6e2;
  --accent: #0e5a9c;
  --accent-soft: #e8f0f8;
  --ink: #3f4349;
  --ink-hover: #2b2e33;
  --user-bubble: #f2f4f7;
  --danger: #b23b3f;
  --radius: 12px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.6 system-ui, -apple-system, "Segoe UI", "Apple SD Gothic Neo", sans-serif;
  color: var(--fg);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.ic { display: inline-flex; }
.ic svg { width: 18px; height: 18px; display: block; }

.app { display: flex; height: 100vh; overflow: hidden; }

/* =============================================================================
 * Auth (Login) View
 * ============================================================================= */

.auth-container {
  align-items: center;
  display: flex;
  justify-content: center;
  height: 100vh;
  padding: 24px;
  background: var(--sidebar-bg);
}

.auth-card {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 24px #00000012;
  max-width: 380px;
  padding: 32px;
  width: 100%;
}

.auth-brand {
  margin-bottom: 24px;
  text-align: center;
}
.auth-brand h1 {
  color: var(--fg);
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: -0.01em;
  margin: 0;
}
.auth-tagline {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 8px 0 0;
}

#login-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.auth-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.auth-field label {
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 600;
}
.auth-field input {
  border: 1px solid var(--border);
  border-radius: 9px;
  font: inherit;
  font-size: 0.95rem;
  padding: 10px 12px;
  background: #fff;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.auth-field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}

.auth-error {
  background: #fdf1f1;
  border: 1px solid #f2cdce;
  border-radius: 8px;
  color: var(--danger);
  font-size: 0.85rem;
  font-weight: 600;
  margin: 0;
  padding: 9px 12px;
}

.auth-submit {
  background: var(--accent);
  border: 0;
  border-radius: 9px;
  color: #fff;
  cursor: pointer;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  padding: 11px;
  transition: background 0.15s;
}
.auth-submit:hover { background: #0b4b84; }
.auth-submit:disabled { background: #b9c3ce; cursor: not-allowed; }

.auth-note {
  color: var(--faint);
  font-size: 0.72rem;
  line-height: 1.5;
  margin: 20px 0 0;
  text-align: center;
  word-break: keep-all;
}

.auth-switch {
  color: var(--muted);
  font-size: 0.8rem;
  margin: 14px 0 0;
  text-align: center;
}

.auth-link {
  background: none;
  border: 0;
  color: var(--accent);
  cursor: pointer;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 0 2px;
  text-decoration: underline;
}
.auth-link:hover { color: #0b4b84; }

.auth-info {
  background: #eef6ff;
  border: 1px solid #cfe3fb;
  border-radius: 9px;
  color: #0b4b84;
  font-size: 0.78rem;
  line-height: 1.45;
  margin: 4px 0 0;
  padding: 8px 10px;
}

/* Logout button (in chat header) */
.logout-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--muted);
  cursor: pointer;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  padding: 6px 12px;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.logout-btn:hover {
  background: var(--hover);
  border-color: var(--faint);
  color: var(--fg);
}
.logout-btn:disabled { opacity: 0.6; cursor: not-allowed; }

/* =============================================================================
 * Sidebar
 * ============================================================================= */

.sidebar {
  width: 260px;
  flex: 0 0 auto;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--sidebar-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.2s ease;
}

.sidebar.collapsed { width: 60px; }
.sidebar.collapsed .brand,
.sidebar.collapsed .new-chat__label,
.sidebar.collapsed .side-section,
.sidebar.collapsed .conv-list,
.sidebar.collapsed .sidebar__foot { display: none; }
.sidebar.collapsed .sidebar__head { padding: 14px 8px 8px; }
.sidebar.collapsed .sidebar__top { justify-content: center; }
.sidebar.collapsed .new-chat { justify-content: center; padding: 9px 0; }

.sidebar__head {
  padding: 14px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.sidebar__top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.brand {
  font-weight: 800;
  font-size: 1.1rem;
  letter-spacing: -0.01em;
  padding: 4px 6px;
}

.icon-btn {
  border: 0;
  background: transparent;
  cursor: pointer;
  color: var(--muted);
  padding: 6px;
  border-radius: 8px;
  display: inline-flex;
}
.icon-btn:hover { background: var(--hover); }

.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--fg);
  border-radius: 10px;
  padding: 9px 12px;
  font: inherit;
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  box-shadow: 0 1px 2px #0000000a;
}
.new-chat:hover { background: var(--hover); }

.side-section {
  margin: 0;
  padding: 10px 12px 4px;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--faint);
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 8px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--fg);
  background: transparent;
  border: 0;
  font: inherit;
  text-align: left;
  width: 100%;
}
.conv-item:hover { background: var(--hover); }
.conv-item.active { background: var(--active); }

.conv-item__title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.875rem;
}

.conv-item__del {
  opacity: 0;
  border: 0;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
  padding: 3px;
  border-radius: 6px;
  display: inline-flex;
}
.conv-item__del svg { width: 15px; height: 15px; display: block; }
.conv-item:hover .conv-item__del { opacity: 1; }
.conv-item__del:hover { background: #00000010; color: var(--danger); }

.conv-empty {
  margin: 0;
  color: var(--faint);
  font-size: 0.85rem;
  padding: 6px 10px;
}

.sidebar__foot {
  padding: 12px 14px;
  border-top: 1px solid var(--sidebar-border);
  font-size: 0.74rem;
  color: var(--faint);
}
.sidebar__foot .tag {
  display: inline-block;
  background: #f0e3e3;
  color: var(--danger);
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  font-size: 0.66rem;
  letter-spacing: 0.03em;
  margin-bottom: 6px;
}
.sidebar__foot p { margin: 0; line-height: 1.45; }

/* =============================================================================
 * Chat Area
 * ============================================================================= */

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.chat__head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
}
.chat__head h1 {
  font-size: 0.95rem;
  font-weight: 650;
  margin: 0;
  flex: 1;
}

#mobile-menu { display: none; }

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #22c55e;
  flex-shrink: 0;
  transition: background 0.3s;
}
.status-dot.loading {
  background: #eab308;
  animation: pulse 1s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.env-badge {
  font-size: 0.65rem;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 999px;
  letter-spacing: 0.02em;
}
.env-badge.env-local { background: #dbeafe; color: #1e40af; }
.env-badge.env-aws { background: #fef3c7; color: #92400e; }

/* =============================================================================
 * Messages
 * ============================================================================= */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px 0;
  scroll-behavior: smooth;
}

.msg {
  max-width: 720px;
  margin: 0 auto 22px;
  padding: 0 24px;
  display: flex;
  gap: 14px;
}

.msg__avatar {
  width: 28px;
  height: 28px;
  flex: 0 0 28px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  font-weight: 800;
  color: #fff;
}
.msg--user .msg__avatar { background: #8a9099; }
.msg--assistant .msg__avatar { background: var(--accent); }

.msg__body {
  flex: 1;
  min-width: 0;
  padding-top: 2px;
}

.msg__role {
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 5px;
}

.bubble { border-radius: 12px; }
.msg--user .bubble {
  background: var(--user-bubble);
  border: 1px solid var(--border);
  padding: 11px 14px;
}
.msg--assistant .bubble { padding: 0; }

/* Reply text — preserves whitespace and line breaks */
.reply-text {
  font-size: 0.94rem;
  line-height: 1.7;
  color: var(--fg);
  white-space: pre-wrap;
  word-break: break-word;
}

.error-text {
  color: var(--danger);
  font-weight: 600;
  font-size: 0.9rem;
}

/* =============================================================================
 * Empty State
 * ============================================================================= */

.empty-state {
  max-width: 620px;
  margin: 12vh auto 0;
  text-align: center;
  padding: 0 24px;
}
.empty-state h2 {
  color: var(--fg);
  font-size: 1.7rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
}
.empty-state p {
  color: var(--muted);
  margin: 0 auto;
  max-width: 600px;
  white-space: pre-line;
  word-break: keep-all;
  line-height: 1.65;
}

.examples {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 22px;
}
.example-chip {
  border: 1px solid var(--border);
  background: #fff;
  color: var(--fg);
  border-radius: 999px;
  padding: 8px 14px;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  transition: background 0.15s;
}
.example-chip:hover { background: var(--hover); }

/* =============================================================================
 * Composer
 * ============================================================================= */

.composer {
  padding: 8px 20px 14px;
  background: linear-gradient(to top, var(--bg) 70%, #ffffff00);
}
.composer__inner {
  max-width: 720px;
  margin: 0 auto;
}

.composer__row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 8px 8px 14px;
  background: #fff;
  box-shadow: 0 2px 10px #0000000f;
}
.composer__row:focus-within {
  border-color: var(--ink);
  box-shadow: 0 2px 14px #00000014;
}

#question {
  flex: 1;
  border: 0;
  resize: none;
  font: inherit;
  font-size: 0.95rem;
  max-height: 180px;
  padding: 8px 0;
  outline: none;
  background: transparent;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
#question::placeholder { color: #c4c8cf; opacity: 1; }
#question::-webkit-scrollbar { display: none; width: 0; height: 0; }

.send {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 0.15s;
}
.send:hover { background: var(--ink-hover); }
.send:disabled { background: #b9c3ce; cursor: not-allowed; }
.send svg { width: 18px; height: 18px; }

.composer__hint {
  margin: 8px 0 0;
  font-size: 0.72rem;
  color: var(--faint);
  text-align: center;
}

/* =============================================================================
 * Typing Indicator
 * ============================================================================= */

.typing {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}
.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--faint);
  animation: blink 1.2s infinite both;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

/* =============================================================================
 * Sidebar Overlay (Mobile)
 * ============================================================================= */

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  z-index: 99;
}
.sidebar-overlay.show { display: block; }

/* =============================================================================
 * Responsive
 * ============================================================================= */

@media (max-width: 720px) {
  .sidebar {
    position: fixed;
    z-index: 100;
    height: 100%;
    width: 260px;
    box-shadow: 2px 0 12px #0002;
    transition: transform 0.2s ease;
  }
  .sidebar.collapsed {
    transform: translateX(-100%);
  }

  #mobile-menu { display: inline-flex; }

  .msg { padding: 0 16px; }
  .composer { padding: 8px 12px 10px; }
  .examples { flex-direction: column; align-items: center; }

  .auth-card { padding: 24px; }
}

@media (max-width: 480px) {
  .empty-state h2 { font-size: 1.3rem; }
  .example-chip { font-size: 0.8rem; padding: 7px 12px; }
}
