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

/* hidden 속성은 항상 이겨야 한다. .lightbox-overlay/.attachment-preview처럼 같은 요소에
   display:flex를 거는 규칙이 있으면 동일 우선순위에서 저 규칙이 [hidden]보다 나중에 읽혀
   hidden 속성이 무시되는 문제가 있었음 (라이트박스가 새로고침해도 항상 열려 보이던 원인). */
[hidden] {
  display: none !important;
}

:root {
  --bg: #ffffff;
  --header-bg: #ffffff;
  --header-border: #efefef;
  --text-primary: #262626;
  --text-secondary: #8e8e8e;
  --bubble-in-bg: #f0f0f0;
  --bubble-in-text: #262626;
  --bubble-out-text: #ffffff;
  --bubble-gradient: linear-gradient(135deg, #4776e6 0%, #8e54e9 100%);
  --input-bg: #f0f0f0;
  --input-text: #262626;
  --input-placeholder: #8e8e8e;
  --status-dot: #3fd07f;
  --send-btn-color: #4776e6;
  --send-btn-disabled: #b8d3ff;
  --avatar-gradient: linear-gradient(135deg, #4776e6 0%, #8e54e9 55%, #e95cb0 100%);
  --scrollbar-thumb: rgba(0, 0, 0, 0.15);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --header-bg: #000000;
    --header-border: #262626;
    --text-primary: #f5f5f5;
    --text-secondary: #a8a8a8;
    --bubble-in-bg: #262626;
    --bubble-in-text: #f5f5f5;
    --bubble-out-text: #ffffff;
    --bubble-gradient: linear-gradient(135deg, #4776e6 0%, #8e54e9 100%);
    --input-bg: #1c1c1c;
    --input-text: #f5f5f5;
    --input-placeholder: #737373;
    --status-dot: #3fd07f;
    --send-btn-color: #6d93ef;
    --send-btn-disabled: #37415a;
    --avatar-gradient: linear-gradient(135deg, #4776e6 0%, #8e54e9 55%, #e95cb0 100%);
    --scrollbar-thumb: rgba(255, 255, 255, 0.15);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  }
}

/* 수동 토글이 시스템 설정보다 우선 */
:root[data-theme="light"] {
  --bg: #ffffff;
  --header-bg: #ffffff;
  --header-border: #efefef;
  --text-primary: #262626;
  --text-secondary: #8e8e8e;
  --bubble-in-bg: #f0f0f0;
  --bubble-in-text: #262626;
  --input-bg: #f0f0f0;
  --input-text: #262626;
  --input-placeholder: #8e8e8e;
  --send-btn-color: #4776e6;
  --send-btn-disabled: #b8d3ff;
  --scrollbar-thumb: rgba(0, 0, 0, 0.15);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}

:root[data-theme="dark"] {
  --bg: #000000;
  --header-bg: #000000;
  --header-border: #262626;
  --text-primary: #f5f5f5;
  --text-secondary: #a8a8a8;
  --bubble-in-bg: #262626;
  --bubble-in-text: #f5f5f5;
  --input-bg: #1c1c1c;
  --input-text: #f5f5f5;
  --input-placeholder: #737373;
  --send-btn-color: #6d93ef;
  --send-btn-disabled: #37415a;
  --scrollbar-thumb: rgba(255, 255, 255, 0.15);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Apple SD Gothic Neo",
    "Noto Sans KR", "Malgun Gothic", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text-primary);
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100dvh;
  background: var(--bg);
  position: relative;
}

/* ---------- 헤더 ---------- */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--header-border);
  background: var(--header-bg);
  flex-shrink: 0;
  z-index: 10;
}

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

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--avatar-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: #fff;
  font-weight: 600;
  font-size: 16px;
  overflow: hidden;
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(1.6);
  transform-origin: 50% 3%;
}

.header-info {
  min-width: 0;
}

.header-name {
  font-weight: 600;
  font-size: 15px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-status {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--status-dot);
  flex-shrink: 0;
}

.theme-toggle {
  background: transparent;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  padding: 8px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
  flex-shrink: 0;
}

.theme-toggle:hover {
  background: var(--bubble-in-bg);
}

/* ---------- 토큰 사용 현황 위젯 (근사치, 자동충전/환불 등 미반영) ---------- */
.usage-widget {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 2px;
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.3;
  cursor: help;
  margin-right: 6px;
  flex-shrink: 0;
}

.usage-line {
  white-space: nowrap;
}

.usage-line.usage-low {
  color: #e5484d;
  font-weight: 700;
}

/* ---------- 메시지 영역 ---------- */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scroll-behavior: smooth;
}

.chat-messages::-webkit-scrollbar {
  width: 6px;
}

.chat-messages::-webkit-scrollbar-thumb {
  background: var(--scrollbar-thumb);
  border-radius: 3px;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 8px;
  padding: 24px;
  color: var(--text-secondary);
}

.empty-avatar {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--avatar-gradient);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 8px;
  overflow: hidden;
}

.empty-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(1.6);
  transform-origin: 50% 3%;
}

.empty-title {
  font-size: 17px;
  font-weight: 600;
  color: var(--text-primary);
}

.empty-subtitle {
  font-size: 13px;
  max-width: 260px;
  line-height: 1.5;
}

.msg-row {
  display: flex;
  width: 100%;
  animation: fadeIn 0.18s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-row.assistant {
  justify-content: flex-start;
}

.bubble {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: 20px;
  font-size: 14.5px;
  line-height: 1.45;
  word-wrap: break-word;
  white-space: pre-wrap;
  box-shadow: var(--shadow);
}

.msg-row.user .bubble {
  background: var(--bubble-gradient);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 6px;
}

.msg-row.assistant .bubble {
  background: var(--bubble-in-bg);
  color: var(--bubble-in-text);
  border-bottom-left-radius: 6px;
}

.bubble.error {
  background: #fdecea;
  color: #b3261e;
}

/* 도구 사용 표시 배지 (일반 말풍선과 구분되는 작은 알림 형태) */
.tool-indicator {
  display: inline-block;
  max-width: 75%;
  padding: 6px 12px;
  border-radius: 14px;
  font-size: 12.5px;
  color: var(--text-secondary);
  background: transparent;
  border: 1px dashed var(--header-border);
}

/* 명령 실행 승인/거부 카드 */
.confirm-card {
  max-width: 85%;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.confirm-header {
  font-weight: 600;
  font-size: 13.5px;
}

.confirm-reason {
  font-size: 12.5px;
  color: var(--text-secondary);
}

.confirm-command {
  background: rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  padding: 8px 10px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 12.5px;
  white-space: pre-wrap;
  word-break: break-all;
  color: var(--bubble-in-text);
}

:root[data-theme="dark"] .confirm-command {
  background: rgba(255, 255, 255, 0.08);
}

@media (prefers-color-scheme: dark) {
  .confirm-command {
    background: rgba(255, 255, 255, 0.08);
  }
}

/* 부서 지침 수정용 diff 뷰 */
.diff-mode-badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 8px;
  font-size: 11.5px;
  font-weight: 600;
  vertical-align: middle;
}

.diff-mode-append {
  background: rgba(63, 208, 127, 0.18);
  color: #2f9e5c;
}

.diff-mode-replace {
  background: rgba(232, 93, 93, 0.18);
  color: #c94b4b;
}

.diff-view {
  max-height: 260px;
  overflow-y: auto;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.06);
  padding: 6px 0;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, monospace;
  font-size: 12px;
}

:root[data-theme="dark"] .diff-view {
  background: rgba(255, 255, 255, 0.08);
}

@media (prefers-color-scheme: dark) {
  .diff-view {
    background: rgba(255, 255, 255, 0.08);
  }
}

.diff-line {
  padding: 1px 10px;
  white-space: pre-wrap;
  word-break: break-all;
  line-height: 1.5;
  color: var(--bubble-in-text);
}

.diff-line.diff-add {
  background: rgba(63, 208, 127, 0.18);
  color: #2f9e5c;
}

.diff-line.diff-remove {
  background: rgba(232, 93, 93, 0.16);
  color: #c94b4b;
  text-decoration: line-through;
  text-decoration-thickness: 1.5px;
}

/* 이메일 답장 승인 카드 */
.email-draft-meta {
  font-size: 12.5px;
  color: var(--text-secondary);
}

.email-draft-section {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.email-draft-label {
  font-size: 11.5px;
  font-weight: 600;
  color: var(--text-secondary);
}

.email-draft-text {
  background: rgba(0, 0, 0, 0.06);
  border-radius: 10px;
  padding: 8px 10px;
  font-size: 13px;
  white-space: pre-wrap;
  word-break: break-word;
  color: var(--bubble-in-text);
}

:root[data-theme="dark"] .email-draft-text {
  background: rgba(255, 255, 255, 0.08);
}

@media (prefers-color-scheme: dark) {
  .email-draft-text {
    background: rgba(255, 255, 255, 0.08);
  }
}

.confirm-actions {
  display: flex;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

.confirm-btn {
  flex: 1;
  border: none;
  border-radius: 12px;
  padding: 8px 0;
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.15s ease, transform 0.1s ease;
}

.confirm-btn:active {
  transform: scale(0.97);
}

.confirm-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.confirm-btn.approve {
  background: var(--bubble-gradient);
  color: #fff;
}

.confirm-btn.reject {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--header-border);
}

/* 타이핑 인디케이터 */
.typing-indicator {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 12px 16px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-secondary);
  opacity: 0.6;
  animation: typingBounce 1.2s infinite ease-in-out;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typingBounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.5;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* ---------- 입력 영역 ---------- */
.chat-input-area {
  flex-shrink: 0;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--header-border);
  background: var(--bg);
}

.chat-form {
  display: flex;
  align-items: center;
  gap: 8px;
}

.attach-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease;
}

.attach-btn:hover {
  background: var(--bubble-in-bg);
  color: var(--text-primary);
}

.attach-btn:active {
  transform: scale(0.92);
}

/* ---------- 첨부 미리보기 스트립 ---------- */
.attachment-preview {
  display: flex;
  gap: 8px;
  padding: 0 2px 10px;
  overflow-x: auto;
}

.attachment-thumb {
  position: relative;
  width: 56px;
  height: 56px;
  flex-shrink: 0;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.attachment-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.attachment-remove {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 13px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
}

.attachment-error {
  padding: 0 2px 8px;
  font-size: 12px;
  color: #c94b4b;
}

/* ---------- 말풍선 안 이미지 ---------- */
.bubble-images {
  display: grid;
  gap: 4px;
  margin-bottom: 4px;
}

.bubble-images.single {
  grid-template-columns: 1fr;
  max-width: 220px;
}

.bubble-images.grid {
  grid-template-columns: repeat(2, 1fr);
  max-width: 220px;
}

.bubble-images img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 14px;
  cursor: pointer;
  display: block;
}

.bubble-images.single img {
  aspect-ratio: auto;
  max-height: 280px;
}

.bubble-text {
  white-space: pre-wrap;
}

/* ---------- 라이트박스 ---------- */
.lightbox-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.lightbox-img {
  max-width: 90vw;
  max-height: 90vh;
  object-fit: contain;
  border-radius: 8px;
}

/* ---------- 프로필 소개 카드 (헤더 아바타 클릭 시) ---------- */
.header-left .avatar {
  cursor: pointer;
}

/* 라이트박스 클래스(.lightbox-overlay)만으로는 배경이 완전히 안 가려지는 경우가 있어
   ID 선택자로 더 확실하게 전체 화면을 덮는다 (이미지 라이트박스 쪽은 건드리지 않음). */
#profile-modal-overlay {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.6);
  z-index: 1000;
}

.profile-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  max-width: min(90vw, 340px);
  padding: 28px 24px;
  border-radius: 20px;
  background: var(--bg);
  text-align: center;
}

.profile-card-img-wrap {
  width: 96px;
  height: 96px;
  border-radius: 50%;
  overflow: hidden;
  flex-shrink: 0;
}

.profile-card-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transform: scale(1.6);
  transform-origin: 50% 3%;
}

.profile-card-name {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
}

.profile-card-tagline {
  font-size: 13.5px;
  font-weight: 600;
  color: var(--send-btn-color);
}

.profile-card-role {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-secondary);
}

.profile-card-stats {
  display: flex;
  gap: 14px;
  font-size: 12.5px;
  font-weight: 600;
  margin-top: 4px;
}

.profile-card-stats .stat-positive {
  color: #2f9e5c;
}

.profile-card-stats .stat-negative {
  color: #c94b4b;
}

.profile-card-recent {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-secondary);
  text-align: left;
  width: 100%;
}

.lightbox-close {
  position: absolute;
  top: 16px;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.chat-input {
  flex: 1;
  border: none;
  outline: none;
  background: var(--input-bg);
  color: var(--input-text);
  border-radius: 22px;
  padding: 11px 18px;
  font-size: 14.5px;
  font-family: inherit;
}

.chat-input::placeholder {
  color: var(--input-placeholder);
}

.send-btn {
  border: none;
  background: transparent;
  color: var(--send-btn-disabled);
  cursor: not-allowed;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: color 0.15s ease, transform 0.1s ease;
}

.send-btn:not(:disabled) {
  color: var(--send-btn-color);
  cursor: pointer;
}

.send-btn:not(:disabled):active {
  transform: scale(0.9);
}

/* ---------- 반응형 ---------- */
@media (max-width: 600px) {
  .app {
    max-width: 100%;
  }

  .chat-header {
    padding: 10px 14px;
  }

  .bubble {
    max-width: 82%;
  }
}

