:root {
  --bg: #f4f6fb;
  --sidebar: #ffffff;
  --surface: #ffffff;
  --surface-soft: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-2: #334155;
  --muted: #64748b;
  --accent: #2563eb;
  --accent-grad: linear-gradient(135deg, #2563eb, #4f46e5);
  --bubble-other: #ffffff;
  --bubble-shadow: 0 2px 10px rgba(15, 23, 42, 0.05);
  --green: #10b981;
  --amber: #f59e0b;
  --red: #ef4444;
}

[data-theme="dark"] {
  --bg: #0b1120;
  --sidebar: #111827;
  --surface: #1e293b;
  --surface-soft: #1e293b;
  --border: #293548;
  --text: #f8fafc;
  --text-2: #e2e8f0;
  --muted: #94a3b8;
  --bubble-other: #1e293b;
  --bubble-shadow: 0 2px 12px rgba(0, 0, 0, 0.25);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Pretendard", "Noto Sans KR", sans-serif;
  font-size: 14.5px;
  line-height: 1.55;
}

.app {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  height: 100dvh;
}

/* Sidebar */
.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
}

.side-head {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  font-size: 17px;
  letter-spacing: -0.03em;
}

.logo-dot {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  background: var(--accent-grad);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  font-weight: 800;
}

.side-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px;
}

.side-label {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--muted);
  padding: 0 8px;
  margin: 12px 0 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.room-item {
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 10px 12px;
  border-radius: 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 4px;
  transition: background 0.15s;
}

.room-item:hover { background: var(--surface-soft); }
.room-item.active {
  background: rgba(37, 99, 235, 0.12);
  color: var(--accent);
}

.room-name { font-weight: 700; font-size: 14px; }
.room-desc { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.member-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 8px;
  border-radius: 10px;
}

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  font-size: 13px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  position: relative;
}

.avatar.sm { width: 28px; height: 28px; font-size: 11.5px; }

.online-dot {
  position: absolute;
  right: -1px;
  bottom: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--green);
  border: 2px solid var(--sidebar);
}

.online-dot.off { background: #94a3b8; }

.member-info { flex: 1; min-width: 0; }
.member-name { font-size: 13.5px; font-weight: 600; }
.member-sub { font-size: 11.5px; color: var(--muted); }

.side-foot {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

/* Main chat stage */
.stage {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  min-width: 0;
  background: var(--bg);
}

.chat-head {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 18px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}

.chat-head-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.menu-btn {
  display: none;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text);
  border-radius: 10px;
  width: 36px;
  height: 36px;
  font-size: 18px;
  cursor: pointer;
}

.chat-title { font-weight: 800; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 11.5px;
  font-weight: 600;
  padding: 3px 9px;
  border-radius: 999px;
  background: var(--surface-soft);
  color: var(--muted);
}

.pill.ok { background: rgba(16, 185, 129, 0.14); color: #059669; }
[data-theme="dark"] .pill.ok { color: #6ee7b7; }
.pill.paid { background: rgba(37, 99, 235, 0.13); color: var(--accent); }

.chat-head-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  padding: 7px 12px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 0.15s, transform 0.1s;
}

.btn:active { transform: scale(0.98); }
.btn.primary { background: var(--accent-grad); color: #fff; border: none; }
.btn.soft { background: var(--surface-soft); border-color: transparent; }
.btn.sm { padding: 6px 10px; font-size: 12px; }
.btn.full { width: 100%; }

/* Messages area */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg-row {
  display: flex;
  gap: 10px;
  max-width: 82%;
  align-items: flex-start;
}

.msg-row.me {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-col {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  padding: 0 2px;
}

.msg-row.me .msg-meta { justify-content: flex-end; }
.msg-sender { font-weight: 700; color: var(--text); }

.role-tag {
  font-size: 10.5px;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 6px;
  background: var(--surface-soft);
  color: var(--muted);
}

.bubble {
  background: var(--bubble-other);
  border: 1px solid var(--border);
  box-shadow: var(--bubble-shadow);
  border-radius: 4px 16px 16px 16px;
  padding: 11px 14px;
  color: var(--text-2);
  word-break: break-word;
}

.msg-row.me .bubble {
  background: var(--accent-grad);
  color: #ffffff;
  border: none;
  border-radius: 16px 4px 16px 16px;
}

.bubble.report {
  border-left: 4px solid #7c3aed;
}

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul { margin: 4px 0 8px; padding-left: 18px; }
.bubble pre {
  background: #0f172a;
  color: #e2e8f0;
  padding: 10px 12px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 12.5px;
  margin: 8px 0;
}
.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: rgba(148, 163, 184, 0.18);
  padding: 1px 5px;
  border-radius: 5px;
}
.bubble pre code { background: transparent; padding: 0; }

.bubble-foot {
  margin-top: 8px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
}

/* Typing bar */
.typing-bar {
  padding: 6px 20px;
  font-size: 12.5px;
  color: var(--accent);
  font-weight: 600;
  display: none;
  align-items: center;
  gap: 8px;
}

.typing-bar.show { display: flex; }

.dots { display: inline-flex; gap: 3px; }
.dots span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
  animation: bounce 1.1s infinite ease-in-out;
}
.dots span:nth-child(2) { animation-delay: 0.16s; }
.dots span:nth-child(3) { animation-delay: 0.32s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

/* Composer */
.composer-wrap {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  flex-shrink: 0;
}

.mention-strip {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 8px;
}

.mchip {
  flex: 0 0 auto;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text-2);
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  cursor: pointer;
}

.mchip:hover { border-color: var(--accent); color: var(--accent); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface-soft);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 6px 8px 6px 14px;
}

.composer textarea {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  resize: none;
  max-height: 120px;
  min-height: 24px;
  padding: 5px 0;
  outline: none;
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent-grad);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
  flex-shrink: 0;
  display: grid;
  place-items: center;
}

/* Modal & Toast */
.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: rgba(15, 23, 42, 0.55);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal.hidden { display: none; }
.modal-card {
  width: 100%;
  max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.25);
}

.modal-card h3 { margin: 0 0 8px; font-size: 17px; }
.field { margin-top: 12px; display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; font-weight: 600; color: var(--muted); }
.field input {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

.toast {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  background: #0f172a;
  color: #fff;
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  z-index: 80;
  display: none;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25);
}

.backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 35;
}

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .menu-btn { display: grid; place-items: center; }
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 285px;
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
  }
  .sidebar.open { transform: translateX(0); }
  .backdrop.show { display: block; }
  .msg-row { max-width: 92%; }
  .messages { padding: 14px 12px; }
}
