:root {
  --bg-deep: #0b141a;
  --bg-panel: #111b21;
  --bg-elevated: #202c33;
  --bg-hover: #2a3942;
  --bg-chat: #0b141a;
  --in-bubble: #202c33;
  --out-bubble: #005c4b;
  --text: #e9edef;
  --text-muted: #8696a0;
  --accent: #00a884;
  --accent-soft: rgba(0, 168, 132, 0.16);
  --border: rgba(134, 150, 160, 0.18);
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
  --font: "DM Sans", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg-deep);
  color-scheme: dark;
}

button, input { font: inherit; }
.hidden { display: none !important; }

/* Login */
.login-page {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(ellipse 80% 60% at 20% 10%, rgba(0, 168, 132, 0.18), transparent 55%),
    radial-gradient(ellipse 70% 50% at 90% 90%, rgba(32, 44, 51, 0.9), transparent 50%),
    linear-gradient(160deg, #0b141a 0%, #111b21 45%, #0a1014 100%);
  padding: 24px;
}

.login-shell { width: min(420px, 100%); }

.login-panel {
  background: rgba(17, 27, 33, 0.92);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.login-brand, .sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 28px;
}

.brand-mark {
  width: 48px;
  height: 48px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #00a884, #027a63);
  color: #fff;
  font-weight: 700;
  font-size: 1.25rem;
  letter-spacing: -0.03em;
}

.brand-mark.sm { width: 40px; height: 40px; border-radius: 12px; font-size: 1.05rem; }
.brand-mark.lg { width: 72px; height: 72px; border-radius: 20px; font-size: 1.8rem; margin: 0 auto 16px; }

.brand-name { margin: 0; font-size: 1.15rem; font-weight: 700; letter-spacing: -0.02em; }
.brand-sub { margin: 2px 0 0; color: var(--text-muted); font-size: 0.82rem; }

.login-title { margin: 0 0 6px; font-size: 1.55rem; letter-spacing: -0.03em; }
.login-lead { margin: 0 0 22px; color: var(--text-muted); font-size: 0.95rem; }

.alert {
  padding: 12px 14px;
  border-radius: 10px;
  margin-bottom: 16px;
  font-size: 0.9rem;
}

.alert-error {
  background: rgba(234, 67, 53, 0.12);
  color: #ff8a80;
  border: 1px solid rgba(234, 67, 53, 0.28);
}

.login-form { display: grid; gap: 14px; }
.field { display: grid; gap: 6px; }
.field span { font-size: 0.82rem; color: var(--text-muted); }

.field input,
.search-wrap input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  border-radius: 10px;
  padding: 12px 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus,
.search-wrap input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.btn {
  border: 0;
  border-radius: 10px;
  padding: 12px 16px;
  cursor: pointer;
  font-weight: 600;
}

.btn-primary {
  margin-top: 6px;
  background: var(--accent);
  color: #04140f;
}

.btn-primary:hover { filter: brightness(1.05); }
.btn-primary:disabled { opacity: 0.65; cursor: wait; }

.install-hint {
  margin: 18px 0 0;
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.45;
}

.install-hint strong { color: var(--text); font-weight: 600; }

/* App */
.app-page {
  height: 100vh;
  overflow: hidden;
  background: linear-gradient(180deg, #0a1014 0%, #0b141a 100%);
}

.app-shell {
  height: 100%;
  max-width: 1600px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
}

.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--bg-panel);
  min-height: 0;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 14px 10px;
  gap: 8px;
}

.sidebar-brand { margin: 0; }
.sidebar-actions { display: flex; gap: 4px; }

.icon-btn {
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.search-wrap { padding: 0 12px 12px; }

.chat-list {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.chat-list::-webkit-scrollbar,
.messages::-webkit-scrollbar {
  width: 0 !important;
  height: 0 !important;
  display: none !important;
  background: transparent;
}

.chat-item {
  width: 100%;
  display: grid;
  grid-template-columns: 48px 1fr;
  gap: 12px;
  align-items: center;
  padding: 12px 14px;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  cursor: pointer;
  border-bottom: 1px solid rgba(134, 150, 160, 0.08);
}

.chat-item:hover { background: rgba(32, 44, 51, 0.65); }
.chat-item.active { background: var(--bg-elevated); }

.avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: linear-gradient(145deg, #3b4a54, #1f2c33);
  color: #d1d7db;
  font-weight: 600;
  flex-shrink: 0;
}

.chat-item-body { min-width: 0; }

.chat-item-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.chat-item-name {
  margin: 0;
  font-size: 0.98rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-date {
  color: var(--text-muted);
  font-size: 0.75rem;
  flex-shrink: 0;
}

.chat-item-preview {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.sidebar-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.78rem;
}

.user-chip {
  background: var(--bg-elevated);
  padding: 4px 10px;
  border-radius: 999px;
  color: var(--text);
}

.thread {
  position: relative;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background:
    linear-gradient(rgba(11, 20, 26, 0.92), rgba(11, 20, 26, 0.92)),
    radial-gradient(circle at 20% 20%, rgba(0, 168, 132, 0.05), transparent 40%),
    radial-gradient(circle at 80% 60%, rgba(32, 44, 51, 0.5), transparent 45%),
    var(--bg-chat);
}

.thread-empty {
  flex: 1;
  display: grid;
  place-items: center;
  padding: 24px;
}

.empty-card { text-align: center; max-width: 380px; }
.empty-card h2 { margin: 0 0 8px; letter-spacing: -0.03em; }
.empty-card p { margin: 0; color: var(--text-muted); line-height: 1.5; }

.thread-active {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.thread-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  background: var(--bg-elevated);
  border-bottom: 1px solid var(--border);
}

.thread-meta { min-width: 0; }
.thread-meta h2 {
  margin: 0;
  font-size: 1rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.thread-meta p { margin: 2px 0 0; color: var(--text-muted); font-size: 0.8rem; }

.back-btn { display: none; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px 5% 28px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scrollbar-width: none;
  -ms-overflow-style: none;
}

.day-sep {
  align-self: center;
  margin: 10px 0;
  padding: 6px 12px;
  border-radius: 8px;
  background: var(--bg-elevated);
  color: var(--text-muted);
  font-size: 0.75rem;
}

/* Fila WhatsApp: empuja la burbuja a izquierda o derecha */
.msg {
  display: flex;
  width: 100%;
  margin: 1px 0;
}

.msg--in {
  justify-content: flex-start;
}

.msg--out {
  justify-content: flex-end;
}

.bubble {
  flex: 0 1 auto;
  width: max-content;
  max-width: 65%;
  min-width: 48px;
  padding: 6px 8px 5px 9px;
  border-radius: 7.5px;
  box-shadow: 0 1px 0.5px rgba(0, 0, 0, 0.15);
  line-height: 1.35;
  font-size: 0.925rem;
  text-align: left;
}

.msg--in .bubble {
  background: var(--in-bubble);
  border-top-left-radius: 0;
}

.msg--out .bubble {
  background: var(--out-bubble);
  border-top-right-radius: 0;
}

.bubble-text {
  display: inline;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.bubble-meta {
  display: inline-block;
  float: right;
  position: relative;
  top: 5px;
  margin: 0 0 -1px 10px;
  color: rgba(233, 237, 239, 0.65);
  font-size: 0.68rem;
  line-height: 1;
  white-space: nowrap;
}

.msg--out .bubble-meta {
  color: rgba(233, 237, 239, 0.75);
}

.list-empty, .list-error {
  padding: 28px 18px;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.list-error { color: #ff8a80; }

@media (max-width: 860px) {
  .app-shell {
    grid-template-columns: 1fr;
    border: 0;
  }

  .sidebar { border-right: 0; }
  .app-shell.show-thread .sidebar { display: none; }
  .app-shell:not(.show-thread) .thread { display: none; }
  .back-btn { display: grid; }
  .messages { padding: 14px 12px 24px; }
  .bubble { max-width: 82%; }
}
