:root {
  --bg-sidebar: #111b21;
  --bg-sidebar-hover: #202c33;
  --bg-header: #202c33;
  --bg-chat: #0b141a;
  --bg-bubble-in: #202c33;
  --bg-bubble-out: #005c4b;
  --bg-composer: #202c33;
  --bg-input: #2a3942;
  --text-primary: #e9edef;
  --text-secondary: #8696a0;
  --text-muted: #667781;
  --divider: #222e35;
  --accent: #00a884;
  --danger: #f15c6d;
  --pinned: #ffc857;
  --bubble-radius: 7.5px;
  --sidebar-width: 320px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: #0a0f13; color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial,
    "Apple Color Emoji", "Segoe UI Emoji", sans-serif;
  font-size: 14.5px; overflow: hidden;
}
button { font-family: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
input, textarea { font-family: inherit; color: inherit; }

.app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  height: 100vh; height: 100dvh;
  background: var(--bg-chat);
}

/* ----- Gate ----- */
.gate {
  position: fixed; inset: 0; background: rgba(0,0,0,0.85);
  display: flex; align-items: center; justify-content: center; z-index: 100;
}
.gate.hidden { display: none; }
.gate-card {
  background: var(--bg-header); border-radius: 12px; padding: 28px 30px;
  width: 380px; max-width: 92vw; text-align: center;
  box-shadow: 0 16px 48px rgba(0,0,0,0.6);
}
.gate-card h1 { margin: 0 0 8px; font-size: 22px; }
.gate-sub { color: var(--text-secondary); margin: 0 0 18px; font-size: 14px; line-height: 1.4; }
.gate-card form { display: flex; gap: 8px; }
.gate-card input {
  flex: 1; background: var(--bg-input); border: 1px solid transparent;
  border-radius: 8px; padding: 10px 12px; font-size: 14px;
}
.gate-card input:focus { outline: 0; border-color: var(--accent); }
.gate-card form button {
  background: var(--accent); color: #001d18;
  border-radius: 8px; padding: 10px 18px; font-weight: 600; font-size: 14px;
}
.gate-google { display: flex; justify-content: center; margin-top: 8px; }
.google-btn {
  display: inline-flex; align-items: center; gap: 10px;
  background: #fff; color: #3c4043; border-radius: 8px;
  padding: 10px 18px; font-weight: 500; font-size: 14px;
  box-shadow: 0 1px 2px rgba(0,0,0,.2); transition: box-shadow 120ms;
}
.google-btn:hover { box-shadow: 0 2px 6px rgba(0,0,0,.3); }
.google-btn svg { display: block; }
.gate-fine { color: var(--text-muted); font-size: 12px; margin-top: 14px; font-style: italic; }
.gate-error { color: var(--danger); font-size: 13px; margin-top: 10px; }

/* ----- Sidebar ----- */
.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--divider);
  display: flex; flex-direction: column;
  min-width: 0;
}
.sidebar-header {
  padding: 10px 16px; background: var(--bg-header);
  display: flex; align-items: center; min-height: 60px;
  border-bottom: 1px solid var(--divider);
}
.me { display: flex; align-items: center; gap: 12px; min-width: 0; }
.me-name { font-weight: 600; color: var(--text-primary); font-size: 15px; }
.me-status {
  font-size: 12.5px; color: var(--text-secondary);
  display: flex; align-items: center; gap: 6px;
}
.conn-dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: var(--accent); transition: background 200ms;
}
.conn-dot.ok { background: var(--accent); box-shadow: 0 0 4px rgba(0, 168, 132, 0.6); }
.conn-dot.down { background: var(--danger); animation: pulse 1.2s infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.4; } }

.contacts { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.contact {
  display: grid; grid-template-columns: 48px 1fr;
  align-items: center; gap: 12px;
  padding: 10px 16px; cursor: pointer;
  border-bottom: 1px solid var(--divider);
  transition: background 100ms;
}
.avatar-wrap { position: relative; width: 48px; height: 48px; flex: 0 0 auto; }
.avatar-wrap .conn-dot {
  position: absolute;
  bottom: 0; right: 0;
  width: 12px; height: 12px;
  border-radius: 50%;
  border: 2px solid var(--bg-sidebar);
}
.contact:hover { background: var(--bg-sidebar-hover); }
.contact.active { background: var(--bg-sidebar-hover); }
.contact-meta { min-width: 0; }
.contact-partner {
  color: var(--text-secondary); font-size: 11.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: 0.3px;
}
.contact-name {
  font-weight: 600; color: var(--text-primary); font-size: 15.5px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 1px;
}
.pin { color: var(--pinned); font-size: 11px; margin-left: 4px; }

.avatar {
  width: 48px; height: 48px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 600; color: #fff; font-size: 18px; flex: 0 0 auto;
}
.sidebar-header .avatar { width: 40px; height: 40px; font-size: 16px; }

/* ----- Chat ----- */
.chat {
  display: grid; grid-template-rows: auto 1fr auto;
  min-height: 0; background: var(--bg-chat);
}
.chat-header {
  background: var(--bg-header); border-bottom: 1px solid var(--divider);
  padding: 10px 16px; display: flex; align-items: center; gap: 12px; min-height: 60px;
}
.chat-header .avatar { width: 40px; height: 40px; font-size: 16px; }
/* Header del chat: refleja el orden de la sidebar — partner_name pequeño arriba,
   rol grande abajo. */
.chat-meta { display: flex; flex-direction: column-reverse; min-width: 0; }
.chat-name { font-weight: 600; font-size: 16px; }
.chat-sub { color: var(--text-secondary); font-size: 12px; letter-spacing: 0.3px; }
.back-btn {
  display: none; font-size: 22px;
  width: 32px; height: 32px; border-radius: 50%;
  align-items: center; justify-content: center; color: var(--text-secondary);
}

.messages {
  background: var(--bg-chat);
  padding: 18px 14% 12px;
  overflow-y: auto; min-height: 0;
  display: flex; flex-direction: column;
}

.bubble {
  max-width: 70%; margin: 4px 0; padding: 8px 11px 6px;
  border-radius: var(--bubble-radius);
  display: inline-flex; flex-direction: column; word-wrap: break-word;
}
.bubble.in { background: var(--bg-bubble-in); align-self: flex-start; border-top-left-radius: 0; }
.bubble.out { background: var(--bg-bubble-out); align-self: flex-end; border-top-right-radius: 0; }
.bubble .content { white-space: pre-wrap; line-height: 1.45; }
.bubble .content strong { font-weight: 700; }
.bubble .content em { font-style: italic; color: var(--text-primary); }
.bubble .content del { opacity: 0.6; }
.bubble .time {
  font-size: 11px; color: var(--text-muted); align-self: flex-end; margin-top: 2px;
}
.typing { display: inline-flex; gap: 3px; padding: 6px 0; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--text-secondary);
  animation: typing 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing { 0%, 60%, 100% { transform: translateY(0); opacity: 0.4; } 30% { transform: translateY(-3px); opacity: 1; } }

.composer {
  background: var(--bg-composer); border-top: 1px solid var(--divider);
  padding: 10px 16px; display: flex; align-items: flex-end; gap: 10px;
}
.composer-input {
  flex: 1; background: var(--bg-input); border: 0; border-radius: 8px;
  padding: 9px 12px; font-size: 14.5px; resize: none; max-height: 160px; line-height: 1.4;
}
.composer-input:focus { outline: 0; }
.send-btn {
  background: var(--accent); color: #001d18; border-radius: 50%;
  width: 40px; height: 40px; display: inline-flex; align-items: center; justify-content: center;
}
.send-btn:disabled { opacity: 0.5; cursor: not-allowed; }

.empty {
  color: var(--text-muted); text-align: center;
  padding: 60px 20px; flex: 1;
  display: flex; align-items: center; justify-content: center;
}
.empty .big { font-size: 56px; margin-bottom: 12px; }

/* ----- Mobile ----- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { display: flex; }
  .chat { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat { display: grid; }
  .back-btn { display: inline-flex; }
  .messages { padding: 14px 12px 8px; }
}
