:root {
  --bg: oklch(0.96 0.012 150);
  --surface: #ffffff;
  --surface-2: oklch(0.985 0.004 150);
  --border: oklch(0.92 0.008 150);
  --border-soft: oklch(0.95 0.006 150);
  --text: oklch(0.24 0.02 240);
  --text-dim: oklch(0.56 0.015 240);
  --brand: oklch(0.62 0.15 150);
  --brand-strong: oklch(0.54 0.16 150);
  --brand-soft: oklch(0.95 0.045 150);
  --user-bubble: oklch(0.58 0.15 150);
  --user-text: #ffffff;
  --ai-bubble: oklch(0.975 0.005 240);
  --shadow-sm: 0 1px 2px oklch(0.2 0.02 240 / 0.06);
  --shadow-md: 0 4px 16px oklch(0.2 0.03 150 / 0.08);
  --shadow-lg: 0 12px 40px oklch(0.2 0.04 150 / 0.12);
  --radius-bubble: 18px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Pretendard Variable", Pretendard, -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  text-wrap: pretty;
  -webkit-font-smoothing: antialiased;
}

body {
  display: flex;
  justify-content: center;
  align-items: stretch;
  min-height: 100dvh;
}

/* ===== 채팅 컨테이너 ===== */
/* 모바일: 화면 꽉 채움. 태블릿/데스크톱: 가운데 카드형 위젯 */
.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  width: 100%;
  height: 100dvh;
  background: var(--surface);
  overflow: hidden;
}

/* ===== Header ===== */
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-soft);
  background: var(--surface);
  z-index: 10;
}
.avatar {
  flex-shrink: 0;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(160deg, var(--brand) 0%, var(--brand-strong) 100%);
  color: #fff;
  font-size: 18px;
  font-weight: 800;
  letter-spacing: -0.02em;
  box-shadow: 0 2px 8px oklch(0.55 0.15 150 / 0.3);
}
.header-text { min-width: 0; }
.reset-btn {
  margin-left: auto;
  flex-shrink: 0;
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-dim);
  font: inherit;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s, background 0.15s, transform 0.05s;
}
.reset-btn:hover {
  border-color: var(--brand);
  color: var(--brand-strong);
  background: var(--brand-soft);
}
.reset-btn:active { transform: scale(0.97); }
.chat-header h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.01em;
  text-wrap: balance;
}
.status {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 6px;
}
.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-soft);
  animation: pulse 2.4s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 3px var(--brand-soft); }
  50% { box-shadow: 0 0 0 5px oklch(0.62 0.15 150 / 0.12); }
}

/* ===== Messages ===== */
.messages {
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 22px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
  background:
    radial-gradient(120% 60% at 50% 0%, var(--surface-2) 0%, var(--surface) 60%);
}

.bubble {
  max-width: 80%;
  padding: 11px 15px;
  border-radius: var(--radius-bubble);
  font-size: 15px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
  text-wrap: pretty;
  animation: pop 0.22s cubic-bezier(0.2, 0.7, 0.3, 1);
}
.bubble strong { font-weight: 700; }

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

.bubble.user {
  background: var(--user-bubble);
  color: var(--user-text);
  align-self: flex-end;
  border-bottom-right-radius: 5px;
  box-shadow: 0 2px 8px oklch(0.55 0.15 150 / 0.18);
}

.bubble.ai {
  background: var(--ai-bubble);
  color: var(--text);
  align-self: flex-start;
  border-bottom-left-radius: 5px;
  border: 1px solid var(--border-soft);
}

.bubble.typing {
  display: inline-flex;
  gap: 4px;
  padding: 14px 16px;
  align-self: flex-start;
}
.bubble.typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: blink 1.2s infinite ease-in-out;
}
.bubble.typing span:nth-child(2) { animation-delay: 0.15s; }
.bubble.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

.bubble.error {
  background: oklch(0.96 0.03 25);
  color: oklch(0.45 0.15 25);
  align-self: flex-start;
  border-bottom-left-radius: 5px;
  font-size: 14px;
}

.bubble.notice {
  align-self: center;
  background: var(--brand-soft);
  color: var(--brand-strong);
  font-size: 13px;
  font-weight: 600;
  padding: 8px 16px;
  border-radius: 12px;
  max-width: 90%;
  text-align: center;
}

/* ===== 첫 화면 카테고리 버튼 ===== */
.chips {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-self: stretch;
  margin-top: 4px;
  animation: pop 0.28s cubic-bezier(0.2, 0.7, 0.3, 1);
}
.chip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  text-align: left;
  padding: 13px 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 14.5px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.16s, background 0.16s, transform 0.06s, box-shadow 0.16s;
}
.chip::after {
  content: "›";
  color: var(--text-dim);
  font-size: 18px;
  font-weight: 400;
  transition: transform 0.16s, color 0.16s;
}
.chip:hover {
  border-color: var(--brand);
  background: var(--brand-soft);
  box-shadow: var(--shadow-md);
}
.chip:hover::after { color: var(--brand-strong); transform: translateX(2px); }
.chip:active { transform: scale(0.99); }

/* ===== Composer ===== */
.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 12px 16px calc(14px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border-soft);
  background: var(--surface);
}

#input {
  resize: none;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 16px;
  font: inherit;
  font-size: 15px;
  line-height: 1.5;
  outline: none;
  max-height: 140px;
  background: var(--surface-2);
  color: var(--text);
  transition: border-color 0.15s, background 0.15s, box-shadow 0.15s;
}
#input:focus {
  border-color: var(--brand);
  background: var(--surface);
  box-shadow: 0 0 0 3px var(--brand-soft);
}
#input::placeholder { color: var(--text-dim); }

#send-btn {
  align-self: end;
  height: 46px;
  min-width: 46px;
  padding: 0 18px;
  border: 0;
  border-radius: 16px;
  background: var(--brand);
  color: #fff;
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s, transform 0.05s, box-shadow 0.15s;
  box-shadow: 0 2px 8px oklch(0.55 0.15 150 / 0.25);
}
#send-btn:hover { background: var(--brand-strong); }
#send-btn:active { transform: scale(0.97); }
#send-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  box-shadow: none;
}

/* ===== 태블릿 / 데스크톱: 가운데 카드형 위젯 ===== */
@media (min-width: 640px) {
  body { padding: 24px; }
  .chat {
    width: 100%;
    max-width: 460px;
    height: min(92dvh, 820px);
    border-radius: 24px;
    box-shadow: var(--shadow-lg);
    border: 1px solid var(--border-soft);
  }
}

/* 태블릿: 살짝 더 넓게 */
@media (min-width: 900px) {
  .chat { max-width: 500px; height: min(90dvh, 860px); }
  .messages { padding-left: 22px; padding-right: 22px; }
}

/* 모바일: 꽉 채우고 터치 타깃 키움 */
@media (max-width: 639px) {
  .bubble { max-width: 85%; }
  #send-btn { padding: 0 16px; }
}

/* 다크 모드 대응 (시스템 설정 따라감) */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: oklch(0.18 0.012 150);
    --surface: oklch(0.22 0.012 240);
    --surface-2: oklch(0.25 0.012 240);
    --border: oklch(0.32 0.012 240);
    --border-soft: oklch(0.29 0.01 240);
    --text: oklch(0.94 0.008 240);
    --text-dim: oklch(0.68 0.015 240);
    --ai-bubble: oklch(0.28 0.012 240);
    --brand-soft: oklch(0.32 0.05 150);
  }
}
