:root {
  --bg: #0b141a;
  --shell: #0a1014;
  --panel: #111b21;
  --panel-2: #202c33;
  --panel-3: #2a3942;
  --border: #2a3942;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --accent-2: #25d366;
  --in: #202c33;
  --out: #005c4b;
  --danger: #ea4335;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --font: "DM Sans", "Segoe UI", system-ui, sans-serif;
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--shell);
  color: var(--text);
}

button, input { font: inherit; }

/* Login */
.login-page {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 1.5rem;
  background:
    radial-gradient(ellipse at top, rgba(0, 168, 132, 0.22), transparent 45%),
    linear-gradient(165deg, #071016 0%, #0b141a 45%, #12262a 100%);
}

.login-card {
  width: min(100%, 400px);
  background: rgba(17, 27, 33, 0.92);
  border: 1px solid rgba(42, 57, 66, 0.9);
  border-radius: 20px;
  padding: 2rem;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
  animation: fadeUp 0.35s ease;
}

.login-brand-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.login-brand {
  color: var(--accent-2);
  font-weight: 700;
  letter-spacing: 0.01em;
}

.login-card h1 {
  margin: 0;
  font-size: 1.7rem;
  font-weight: 700;
}

.login-sub {
  margin: 0.4rem 0 1.35rem;
  color: var(--muted);
}

.login-error {
  background: rgba(234, 67, 53, 0.14);
  color: #ff8a80;
  border: 1px solid rgba(234, 67, 53, 0.35);
  border-radius: 12px;
  padding: 0.75rem 0.9rem;
  margin-bottom: 1rem;
  font-size: 0.92rem;
}

.login-card label {
  display: grid;
  gap: 0.4rem;
  margin-bottom: 0.95rem;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 500;
}

.login-card input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  border-radius: 12px;
  padding: 0.8rem 0.9rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.login-card input:focus,
.search-input:focus,
.compose input:focus {
  border-color: rgba(0, 168, 132, 0.7);
  box-shadow: 0 0 0 3px rgba(0, 168, 132, 0.15);
}

.login-card button {
  width: 100%;
  margin-top: 0.5rem;
  border: 0;
  background: linear-gradient(135deg, var(--accent), #019a74);
  color: #04120e;
  font-weight: 700;
  border-radius: 12px;
  padding: 0.85rem 1rem;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.login-card button:hover { filter: brightness(1.05); }
.login-card button:active { transform: translateY(1px); }

/* App shell */
.app {
  min-height: 100%;
  background:
    linear-gradient(#0b141a, #0b141a),
    linear-gradient(90deg, #00a884 0, #00a884 100%);
  background-size: 100% calc(100% - 140px), 100% 140px;
  background-position: bottom, top;
  background-repeat: no-repeat;
  display: grid;
  place-items: center;
  padding: 1.25rem;
}

.app-shell {
  width: min(1600px, 100%);
  height: min(96vh, 980px);
  display: grid;
  grid-template-columns: 380px 1fr;
  overflow: hidden;
  border-radius: 12px;
  box-shadow: var(--shadow);
  border: 1px solid rgba(42, 57, 66, 0.65);
  background: var(--panel);
  animation: fadeUp 0.3s ease;
}

.sidebar,
.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #25d366, #00a884);
  color: #06251a;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.02em;
  flex-shrink: 0;
}

.brand-block {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.sidebar-header,
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.85rem 1rem;
  background: var(--panel-2);
  border-bottom: 1px solid rgba(42, 57, 66, 0.75);
  min-height: 64px;
}

.sidebar-header strong,
.chat-header strong {
  display: block;
  font-weight: 600;
}

.sidebar-header span,
.chat-header span {
  color: var(--muted);
  font-size: 0.8rem;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.ai-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
  user-select: none;
  color: var(--muted);
  font-size: 0.82rem;
  font-weight: 700;
}

.ai-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.ai-switch {
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: #3a4a52;
  position: relative;
  transition: background 0.15s ease;
  border: 1px solid var(--border);
}

.ai-switch::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #dce3e7;
  transition: transform 0.15s ease;
}

.ai-toggle input:checked + .ai-switch {
  background: rgba(0, 168, 132, 0.85);
  border-color: rgba(0, 168, 132, 0.9);
}

.ai-toggle input:checked + .ai-switch::after {
  transform: translateX(16px);
  background: #fff;
}

.ai-toggle.is-on {
  color: var(--accent-2);
}

.logout {
  color: var(--muted);
  text-decoration: none;
  font-size: 0.88rem;
  padding: 0.4rem 0.7rem;
  border-radius: 999px;
  border: 1px solid transparent;
}

.logout:hover {
  color: var(--text);
  border-color: var(--border);
  background: rgba(255, 255, 255, 0.03);
}

.inbox-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.35rem;
  padding: 0.55rem 0.75rem 0;
  background: var(--panel);
}

.inbox-tab {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  border-radius: 999px;
  padding: 0.45rem 0.6rem;
  cursor: pointer;
  font-size: 0.86rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
}

.inbox-tab.active {
  background: rgba(0, 168, 132, 0.16);
  border-color: rgba(0, 168, 132, 0.45);
  color: var(--text);
}

.tab-badge {
  min-width: 1.15rem;
  height: 1.15rem;
  padding: 0 0.3rem;
  border-radius: 999px;
  background: #ea4335;
  color: #fff;
  font-size: 0.7rem;
  display: inline-grid;
  place-items: center;
}

.human-pill {
  display: inline-block;
  margin-left: 0.35rem;
  padding: 0.1rem 0.4rem;
  border-radius: 999px;
  background: rgba(234, 67, 53, 0.18);
  color: #ff8a80;
  font-size: 0.68rem;
  font-weight: 700;
  vertical-align: middle;
}

.human-done-btn {
  border: 1px solid rgba(0, 168, 132, 0.5);
  background: rgba(0, 168, 132, 0.16);
  color: var(--text);
  border-radius: 999px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
}

.human-done-btn:hover { filter: brightness(1.08); }

.stop-ai-btn {
  border: 1px solid rgba(234, 67, 53, 0.5);
  background: rgba(234, 67, 53, 0.16);
  color: #ff8a80;
  border-radius: 999px;
  padding: 0.4rem 0.8rem;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 600;
  white-space: nowrap;
}

.stop-ai-btn:hover { filter: brightness(1.08); }

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.list-toolbar {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  padding: 0 0.85rem 0.55rem;
  background: var(--panel);
}

.ghost-btn,
.danger-btn {
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 600;
  white-space: nowrap;
}

.ghost-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
}

.ghost-btn:hover {
  color: var(--text);
  background: rgba(255, 255, 255, 0.04);
}

.danger-btn {
  border: 1px solid rgba(234, 67, 53, 0.5);
  background: rgba(234, 67, 53, 0.16);
  color: #ff8a80;
}

.danger-btn:hover { filter: brightness(1.08); }
.danger-btn:disabled {
  opacity: 0.45;
  cursor: default;
  filter: none;
}

.conversation.select-mode {
  grid-template-columns: 22px 48px 1fr auto;
}

.chat-check {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
}

.search-wrap {
  padding: 0.65rem 0.85rem 0.75rem;
  background: var(--panel);
  border-bottom: 1px solid rgba(42, 57, 66, 0.45);
}

.search-input {
  width: 100%;
  border: 0;
  background: var(--panel-2);
  color: var(--text);
  border-radius: 10px;
  padding: 0.7rem 0.9rem;
  outline: none;
}

.conversation-list,
.messages {
  overflow: auto;
  flex: 1;
  overflow-anchor: none;
}

.conversation-list {
  background: var(--panel);
}

.conversation {
  width: 100%;
  display: grid;
  grid-template-columns: 48px 1fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.8rem 1rem;
  border: 0;
  border-bottom: 1px solid rgba(42, 57, 66, 0.35);
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s ease;
}

.conversation:hover { background: rgba(32, 44, 51, 0.7); }
.conversation.active { background: var(--panel-2); }

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #6b7c85, #526068);
  color: #111b21;
  font-weight: 700;
  font-size: 1rem;
}

.avatar-lg {
  width: 42px;
  height: 42px;
  font-size: 0.95rem;
}

.conversation-copy { min-width: 0; }
.conversation h3 {
  margin: 0;
  font-size: 0.98rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation p {
  margin: 0.22rem 0 0;
  color: var(--muted);
  font-size: 0.84rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-time {
  color: var(--muted);
  font-size: 0.72rem;
  white-space: nowrap;
  align-self: start;
  margin-top: 0.2rem;
}

.chat {
  background-color: #0b141a;
  background-image:
    linear-gradient(rgba(11, 20, 26, 0.88), rgba(11, 20, 26, 0.88)),
    url("data:image/svg+xml,%3Csvg width='60' height='60' viewBox='0 0 60 60' xmlns='http://www.w3.org/2000/svg'%3E%3Cg fill='none' fill-rule='evenodd'%3E%3Cg fill='%23ffffff' fill-opacity='0.03'%3E%3Cpath d='M36 34v-4h-2v4h-4v2h4v4h2v-4h4v-2h-4zm0-30V0h-2v4h-4v2h4v4h2V6h4V4h-4zM6 34v-4H4v4H0v2h4v4h2v-4h4v-2H6zM6 4V0H4v4H0v2h4v4h2V6h4V4H6z'/%3E%3C/g%3E%3C/g%3E%3C/svg%3E");
}

.chat-header-main {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  min-width: 0;
}

.back-btn {
  display: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 1.2rem;
  cursor: pointer;
  padding: 0.25rem 0.4rem;
}

.messages {
  padding: 1.25rem 1.5rem 1.6rem;
  display: flex;
  flex-direction: column;
  gap: 0.7rem;
  overflow-anchor: none;
}

.bubble {
  display: inline-block;
  width: fit-content;
  max-width: min(68%, 520px);
  padding: 0.4rem 0.75rem 0.25rem;
  border-radius: 12px;
  line-height: 1.45;
  font-size: 0.95rem;
  letter-spacing: 0.01em;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.22);
  animation: bubbleIn 0.18s ease;
  position: relative;
}

.bubble.has-audio {
  display: block;
  width: auto;
  min-width: 280px;
  max-width: min(72%, 360px);
}

.bubble-body {
  display: block;
  color: var(--text);
}

.bubble-row {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  max-width: 100%;
}

.bubble-row.in { align-self: flex-start; justify-content: flex-start; }
.bubble-row.out { align-self: flex-end; justify-content: flex-end; }

.bubble-row .msg-check {
  display: none;
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  margin-bottom: 0.35rem;
  flex-shrink: 0;
  cursor: pointer;
}

.messages.selecting .bubble-row .msg-check { display: block; }
.messages.selecting .bubble-row { cursor: pointer; }
.bubble-row.selected .bubble {
  outline: 2px solid rgba(0, 168, 132, 0.7);
  outline-offset: 2px;
}

.bubble-del {
  position: absolute;
  top: 0.35rem;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 50%;
  background: rgba(17, 27, 33, 0.72);
  color: #e9edef;
  font-size: 0.95rem;
  line-height: 1;
  cursor: pointer;
  display: grid;
  place-items: center;
  padding: 0;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s ease, background 0.15s ease, transform 0.12s ease;
  backdrop-filter: blur(4px);
  z-index: 1;
}

.bubble.in .bubble-del { right: 0.35rem; }
.bubble.out .bubble-del { left: 0.35rem; }

.bubble-row:hover .bubble-del,
.bubble-row:focus-within .bubble-del {
  opacity: 1;
  pointer-events: auto;
}

.bubble-del:hover {
  background: rgba(234, 67, 53, 0.9);
  transform: scale(1.05);
}

.messages.selecting .bubble-del { display: none !important; }

.bubble.in {
  align-self: flex-start;
  background: var(--in);
  border-top-left-radius: 4px;
}

.bubble.out {
  align-self: flex-end;
  background: linear-gradient(160deg, #026956 0%, #005c4b 55%, #004f41 100%);
  border-top-right-radius: 4px;
}

.bubble time {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 0.25rem;
  color: rgba(233, 237, 239, 0.5);
  font-size: 0.7rem;
  margin-top: 0.2rem;
  letter-spacing: 0.02em;
}

.bubble-img {
  display: block;
  max-width: min(100%, 280px);
  max-height: 320px;
  border-radius: 10px;
  margin-bottom: 0.45rem;
  object-fit: cover;
}

.audio-wrap,
.bubble-video {
  display: block;
  margin: 0.15rem 0 0.4rem;
}

.audio-wrap {
  width: 280px;
  max-width: 100%;
}

.bubble-audio {
  display: block;
  width: 100%;
  min-width: 0;
  height: 40px;
}

.bubble-video {
  width: min(100%, 260px);
  max-height: 240px;
  border-radius: 10px;
  background: #000;
}

.audio-download {
  display: inline-block;
  margin-top: 0.35rem;
  color: rgba(233, 237, 239, 0.78);
  font-size: 0.78rem;
  text-decoration: none;
}

.audio-download:hover {
  text-decoration: underline;
}

.bubble-file {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  color: #d9fdd3;
  font-weight: 600;
  text-decoration: none;
  padding: 0.35rem 0.55rem;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.18);
}

.bubble-file:hover { filter: brightness(1.08); }

.bubble-reaction {
  position: absolute;
  left: 0.4rem;
  bottom: -0.65rem;
  background: #1f2c34;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 999px;
  padding: 0.05rem 0.38rem;
  font-size: 0.92rem;
  line-height: 1.25;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.28);
}

.bubble.out .bubble-reaction {
  left: auto;
  right: 0.4rem;
}

.compose {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.65rem;
  align-items: center;
  padding: 0.7rem 0.9rem 0.9rem;
  background: var(--panel-2);
  border-top: 1px solid rgba(42, 57, 66, 0.75);
}

.compose input {
  width: 100%;
  border: 0;
  background: var(--panel);
  color: var(--text);
  border-radius: 24px;
  padding: 0.8rem 1.05rem;
  outline: none;
}

.send-btn {
  width: 46px;
  height: 46px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: #06251a;
  display: grid;
  place-items: center;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.12s ease;
}

.send-btn:hover { filter: brightness(1.08); }
.send-btn:active { transform: scale(0.96); }

.empty,
.empty-state {
  margin: auto;
  color: var(--muted);
  text-align: center;
  padding: 2rem 1.25rem;
  max-width: 360px;
}

.empty-state h2 {
  margin: 0.4rem 0 0.35rem;
  color: var(--text);
  font-size: 1.35rem;
}

.empty-state p { margin: 0; line-height: 1.45; }
.empty-icon {
  width: 64px;
  height: 64px;
  margin: 0 auto 0.5rem;
  border-radius: 20px;
  display: grid;
  place-items: center;
  background: rgba(0, 168, 132, 0.12);
  font-size: 1.6rem;
}

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

@keyframes bubbleIn {
  from { opacity: 0; transform: translateY(4px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@media (max-width: 980px) {
  .app { padding: 0; background: var(--panel); }
  .app-shell {
    width: 100%;
    height: 100vh;
    border-radius: 0;
    border: 0;
    grid-template-columns: 1fr;
  }
  .back-btn { display: inline-grid; }
  .app.show-list .sidebar { display: flex; }
  .app.show-list .chat { display: none; }
  .app:not(.show-list) .sidebar { display: none; }
  .app:not(.show-list) .chat { display: flex; }
}
