:root {
  --bg: #212121;
  --bg-sidebar: #171717;
  --bg-elevated: #2f2f2f;
  --bg-input: #303030;
  --bg-hover: #2a2a2a;
  --text: #ececec;
  --text-dim: #b4b4b4;
  --text-faint: #8a8a8a;
  --border: #3a3a3a;
  --accent: #ececec;
  --user-bubble: #303030;
  --sidebar-w: 260px;
  --radius: 14px;
  --font: "Segoe UI", -apple-system, BlinkMacSystemFont, "Helvetica Neue",
    "Apple SD Gothic Neo", "Malgun Gothic", "Noto Sans KR", Roboto, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button { font-family: inherit; cursor: pointer; }

.app {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--bg-sidebar);
  display: flex;
  flex-direction: column;
  border-right: 1px solid rgba(255, 255, 255, 0.04);
  transition: margin-left 0.22s ease;
}
.app.sidebar-closed .sidebar { margin-left: calc(var(--sidebar-w) * -1); }

.sidebar-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
}
.new-chat {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s ease;
}
.new-chat:hover { background: var(--bg-hover); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  transition: background 0.15s ease, color 0.15s ease;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }

.history {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px 12px;
}
.history-label {
  font-size: 12px;
  color: var(--text-faint);
  padding: 12px 10px 6px;
  font-weight: 600;
}
.history-item {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: none;
  color: var(--text-dim);
  padding: 9px 10px;
  border-radius: 9px;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.15s ease, color 0.15s ease;
}
.history-item:hover { background: var(--bg-hover); color: var(--text); }
.history-item.active { background: var(--bg-elevated); color: var(--text); }

.sidebar-foot {
  border-top: 1px solid rgba(255, 255, 255, 0.05);
  padding: 10px;
}
.user-chip {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px;
  border-radius: 10px;
}
.user-chip:hover { background: var(--bg-hover); }
.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
  flex: 0 0 auto;
}
.avatar-user { background: #c96442; color: #fff; }
.user-meta { line-height: 1.25; overflow: hidden; }
.user-name { font-size: 13px; font-weight: 600; }
.user-plan { font-size: 11px; color: var(--text-faint); }

/* ---------- Main ---------- */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 12px;
  height: 54px;
  flex: 0 0 auto;
}
.brand {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 17px;
  font-weight: 600;
  color: var(--text);
  padding: 6px 8px;
  border-radius: 9px;
}
.brand:hover { background: var(--bg-hover); cursor: default; }
.brand-caret { color: var(--text-faint); }
.newchat-top { margin-left: auto; }
.menu-only { display: none; }

/* Sidebar-open: hide the menu button on desktop */
.app:not(.sidebar-closed) #menuBtn { display: none; }

/* ---------- Welcome ---------- */
[hidden] { display: none !important; }
.welcome {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 24px;
  text-align: center;
}
.welcome-logo {
  width: 56px;
  height: 56px;
  border-radius: 15px;
  background: linear-gradient(145deg, #4a4a4a, #202020);
  display: grid;
  place-items: center;
  font-size: 30px;
  font-weight: 800;
  color: #fff;
  margin-bottom: 20px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
}
.welcome-title {
  font-size: 26px;
  font-weight: 600;
  margin: 0 0 8px;
}
.welcome-sub {
  color: var(--text-faint);
  margin: 0 0 30px;
  font-size: 15px;
}
.suggestions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 260px));
  gap: 10px;
  width: 100%;
  max-width: 540px;
}
.suggest {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 12px;
  padding: 14px 16px;
  font-size: 14px;
  text-align: left;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.suggest:hover { background: var(--bg-hover); border-color: #4a4a4a; }

/* ---------- Thread ---------- */
.thread {
  flex: 1;
  overflow-y: auto;
  padding: 20px 0 12px;
  scroll-behavior: smooth;
}
.msg-row {
  width: 100%;
  padding: 6px 16px;
  display: flex;
}
.msg-inner {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  gap: 14px;
  align-items: flex-start;
}
.msg-avatar {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
}
.msg-avatar.assistant { background: linear-gradient(145deg, #4a4a4a, #202020); color: #fff; }
.msg-avatar.user { background: #c96442; color: #fff; }

.msg-body { min-width: 0; padding-top: 3px; flex: 1; }
.msg-name { font-size: 13px; font-weight: 700; margin-bottom: 3px; }
.msg-text {
  font-size: 15.5px;
  line-height: 1.7;
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
}

/* User messages as right-aligned bubble */
.msg-row.user .msg-inner { flex-direction: row-reverse; }
.msg-row.user .msg-body { flex: 0 1 auto; max-width: 78%; }
.msg-row.user .msg-name { text-align: right; }
.msg-row.user .msg-text {
  background: var(--user-bubble);
  padding: 10px 15px;
  border-radius: 18px;
  display: inline-block;
}

.cursor {
  display: inline-block;
  width: 8px;
  height: 17px;
  background: var(--text);
  margin-left: 2px;
  vertical-align: text-bottom;
  animation: blink 1s steps(2) infinite;
  border-radius: 1px;
}
@keyframes blink { 50% { opacity: 0; } }

.typing { display: inline-flex; gap: 4px; align-items: center; height: 20px; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-faint);
  animation: bounce 1.2s infinite ease-in-out both;
}
.typing span:nth-child(2) { animation-delay: 0.16s; }
.typing span:nth-child(3) { animation-delay: 0.32s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1); opacity: 1; }
}

/* ---------- Composer ---------- */
.composer-wrap {
  flex: 0 0 auto;
  padding: 6px 16px 12px;
  background: linear-gradient(to top, var(--bg) 60%, transparent);
}
.composer {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 26px;
  padding: 8px 8px 8px 18px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
}
.composer:focus-within { border-color: #565656; }
.composer-input {
  flex: 1;
  resize: none;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 15.5px;
  line-height: 1.5;
  max-height: 200px;
  padding: 6px 0;
  font-family: inherit;
}
.composer-input::placeholder { color: var(--text-faint); }
.send-btn {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #111;
  display: grid;
  place-items: center;
  transition: opacity 0.15s ease, transform 0.1s ease;
}
.send-btn:disabled { opacity: 0.25; cursor: not-allowed; }
.send-btn:not(:disabled):hover { opacity: 0.85; }
.send-btn:not(:disabled):active { transform: scale(0.92); }

.disclaimer {
  text-align: center;
  color: var(--text-faint);
  font-size: 12px;
  margin: 8px auto 0;
  max-width: 760px;
}

/* ---------- Scrim (mobile) ---------- */
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: 20;
  opacity: 0;
  animation: fade 0.2s ease forwards;
}
@keyframes fade { to { opacity: 1; } }

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    z-index: 30;
    box-shadow: 2px 0 20px rgba(0, 0, 0, 0.4);
  }
  .app.sidebar-closed .sidebar { margin-left: calc(var(--sidebar-w) * -1); }
  /* On mobile the app defaults to closed; menu button always shows */
  .app:not(.sidebar-closed) #menuBtn { display: inline-flex; }
  .suggestions { grid-template-columns: 1fr; max-width: 420px; }
  .welcome-title { font-size: 22px; }
  .msg-row { padding: 6px 10px; }
  .composer-wrap { padding: 6px 10px 10px; }
}

@media (max-width: 400px) {
  .brand { font-size: 16px; }
  .welcome-logo { width: 48px; height: 48px; font-size: 26px; }
}

/* Scrollbars */
.history::-webkit-scrollbar,
.thread::-webkit-scrollbar { width: 8px; }
.history::-webkit-scrollbar-thumb,
.thread::-webkit-scrollbar-thumb {
  background: #444; border-radius: 4px;
}
.thread::-webkit-scrollbar-thumb:hover { background: #555; }
