:root {
  color-scheme: light;
  --blue: #0057ad;
  --blue-dark: #003b7a;
  --ink: #0d1b2f;
  --muted: #5b6b82;
  --line: #d8e1ec;
  --soft: #f4f7fb;
  --card: #ffffff;
  --green: #16813b;
  --orange: #b65c00;
  --red: #b42318;
  --shadow: 0 18px 40px rgba(15, 42, 74, 0.13);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  width: 100%;
  min-height: 100vh;
  overflow-x: hidden;
}

body {
  background: var(--soft);
  color: var(--ink);
  font-family: Inter, "Segoe UI", Arial, sans-serif;
}

button,
textarea {
  font: inherit;
}

.mobile-chat-shell,
.mobile-chat-gate {
  width: 100%;
  max-width: 520px;
  min-height: 100dvh;
  margin: 0 auto;
  overflow: hidden;
  background: linear-gradient(180deg, #ffffff 0%, #f3f7fc 100%);
}

.mobile-chat-shell {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto;
  gap: 12px;
  padding: 14px 14px calc(86px + env(safe-area-inset-bottom));
}

.chat-mobile-header {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
}

.back-link,
.icon-button,
.attach-button,
.send-button,
.mobile-chat-gate a {
  min-height: 46px;
  border: 0;
  border-radius: 8px;
  display: grid;
  place-items: center;
  cursor: pointer;
  text-decoration: none;
  font-weight: 800;
}

.back-link {
  padding: 0 12px;
  color: var(--blue);
  background: #eaf2ff;
}

.icon-button,
.send-button,
.mobile-chat-gate a {
  color: #fff;
  background: var(--blue);
}

.icon-button {
  width: 48px;
}

.chat-mobile-header h1 {
  margin: 0;
  font-size: 1.8rem;
  line-height: 1;
  letter-spacing: 0;
}

.chat-mobile-header p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 0.9rem;
}

.eyebrow {
  display: block;
  color: var(--blue);
  font-size: 0.8rem;
  font-weight: 800;
}

.status-strip,
.conversation-list,
.mobile-user-list {
  display: flex;
  gap: 8px;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  padding-bottom: 4px;
}

.conversation-panel {
  display: grid;
  gap: 10px;
  min-width: 0;
}

.conversation-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  min-width: 0;
}

.conversation-panel strong,
.new-chat-panel span {
  display: block;
  color: var(--muted);
  font-size: 0.85rem;
}

.new-chat-toggle {
  min-height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 12px;
  color: var(--blue);
  background: #eaf2ff;
  cursor: pointer;
  font-weight: 800;
  white-space: nowrap;
}

.new-chat-toggle[aria-expanded="true"] {
  color: #fff;
  border-color: var(--blue);
  background: var(--blue);
}

.new-chat-panel {
  display: grid;
  gap: 8px;
  min-width: 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.72);
}

.connection-pill,
.room-pill,
.conversation-button,
.user-button {
  white-space: nowrap;
  border-radius: 999px;
  padding: 10px 12px;
  font-size: 0.9rem;
  font-weight: 800;
}

.connection-pill {
  color: var(--orange);
  background: #fff3df;
}

.connection-pill.ok {
  color: var(--green);
  background: #eaf8ef;
}

.connection-pill.error {
  color: var(--red);
  background: #fff0ed;
}

.room-pill {
  color: var(--blue-dark);
  background: #eaf2ff;
}

.conversation-button,
.user-button {
  flex: 0 0 auto;
  max-width: min(78vw, 240px);
  border: 1px solid var(--line);
  color: var(--muted);
  background: #fff;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conversation-button.active,
.user-button.active {
  border-color: var(--blue);
  color: #fff;
  background: var(--blue);
}

.user-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.user-initials {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 999px;
  color: var(--blue);
  background: #eaf2ff;
}

.user-button.active .user-initials {
  color: var(--blue);
  background: #fff;
}

.mobile-message-list {
  min-height: 0;
  width: 100%;
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  align-content: start;
  gap: 10px;
  padding: 4px 0 12px;
}

.empty-state {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px;
  color: var(--muted);
  background: var(--card);
  text-align: center;
}

.message-row {
  display: grid;
  justify-items: start;
}

.message-row.mine {
  justify-items: end;
}

.message-bubble {
  max-width: 88%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 12px;
  background: #fff;
  box-shadow: 0 8px 18px rgba(15, 42, 74, 0.07);
  overflow-wrap: anywhere;
}

.message-row.mine .message-bubble {
  border-color: var(--blue);
  color: #fff;
  background: var(--blue);
}

.message-meta {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.76rem;
  font-weight: 800;
}

.message-meta span {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.message-row.mine .message-meta {
  color: rgba(255, 255, 255, 0.8);
}

.message-text {
  white-space: pre-wrap;
  line-height: 1.35;
}

.message-attachments {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.image-attachment img {
  display: block;
  width: min(100%, 220px);
  max-height: 180px;
  object-fit: cover;
  border-radius: 8px;
}

.file-attachment {
  display: grid;
  gap: 2px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 9px;
  color: var(--ink);
  background: #f8fbff;
  text-decoration: none;
}

.message-row.mine .file-attachment {
  border-color: rgba(255, 255, 255, 0.42);
  color: #fff;
  background: rgba(255, 255, 255, 0.16);
}

.file-attachment small {
  color: var(--muted);
}

.message-row.mine .file-attachment small {
  color: rgba(255, 255, 255, 0.78);
}

.mobile-message-form {
  position: fixed;
  left: 50%;
  bottom: 0;
  z-index: 20;
  width: min(100%, 520px);
  transform: translateX(-50%);
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(10px);
}

.message-input-row {
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 8px;
  align-items: end;
}

.attach-button {
  width: 44px;
  color: var(--blue);
  background: #eaf2ff;
  font-size: 1.45rem;
}

.send-button {
  min-width: 76px;
  padding: 0 12px;
}

.mobile-message-form textarea {
  width: 100%;
  min-height: 46px;
  max-height: 120px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  color: var(--ink);
  background: #fff;
  resize: none;
}

.mobile-attachment-preview {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-bottom: 8px;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 999px;
  padding: 7px 8px;
  color: var(--blue-dark);
  background: #eaf2ff;
  font-size: 0.82rem;
  font-weight: 800;
}

.attachment-chip button {
  border: 0;
  border-radius: 999px;
  color: #fff;
  background: var(--blue);
}

.mobile-chat-gate {
  display: grid;
  place-items: center;
  align-content: center;
  gap: 14px;
  padding: 28px;
  text-align: center;
}

.mobile-chat-gate img {
  width: 150px;
}

.mobile-chat-gate h1 {
  margin: 0;
  font-size: 2rem;
}

.mobile-chat-gate p {
  max-width: 320px;
  margin: 0;
  color: var(--muted);
  line-height: 1.45;
}

.mobile-chat-gate a {
  padding: 0 18px;
}

.mobile-toast {
  position: fixed;
  left: 50%;
  bottom: 86px;
  z-index: 30;
  width: min(calc(100% - 24px), 480px);
  transform: translateX(-50%);
  padding: 12px 14px;
  border-radius: 8px;
  color: #fff;
  background: var(--ink);
  box-shadow: var(--shadow);
}

.mobile-toast.error {
  background: var(--red);
}

@media (min-width: 640px) {
  body {
    background: #eaf0f8;
  }

  .mobile-chat-shell,
  .mobile-chat-gate {
    min-height: calc(100vh - 32px);
    margin-top: 16px;
    margin-bottom: 16px;
    border: 1px solid var(--line);
    border-radius: 18px;
    overflow: hidden;
    box-shadow: var(--shadow);
  }
}

/* Chat móvil unificado v2 */
.mobile-chat-shell {
  height: 100dvh;
  min-height: 0;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto auto;
  gap: 8px;
  padding: 10px 12px max(8px, env(safe-area-inset-bottom));
}

.symbol-icon {
  font-family: "Segoe UI Symbol", "Arial Unicode MS", sans-serif;
}

.refresh-icon::before {
  content: "";
  width: 16px;
  height: 16px;
  border: 2px solid currentColor;
  border-right-color: transparent;
  border-radius: 50%;
  transform: rotate(-20deg);
}

.refresh-icon::after {
  content: "";
  position: absolute;
  margin: -13px 0 0 15px;
  border-width: 4px 0 4px 6px;
  border-style: solid;
  border-color: transparent transparent transparent currentColor;
  transform: rotate(28deg);
}

.refresh-icon {
  position: relative;
}

.status-strip {
  padding-bottom: 0;
}

.conversation-panel {
  gap: 7px;
}

.mobile-search-field {
  display: grid;
  gap: 4px;
  min-width: 0;
}

.mobile-search-field span {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 750;
}

.mobile-search-field input {
  width: 100%;
  min-width: 0;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  color: var(--ink);
  background: #fff;
  outline-color: var(--blue);
}

.conversation-list {
  gap: 8px;
  padding-bottom: 2px;
}

.conversation-button {
  flex: 0 0 min(78vw, 300px);
  max-width: 300px;
  min-height: 66px;
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  border-radius: 8px;
  padding: 8px;
  text-align: left;
}

.conversation-avatar {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 50%;
  color: #fff;
  background: var(--blue);
  font-size: 0.72rem;
  font-weight: 850;
}

.conversation-button.active .conversation-avatar {
  color: var(--blue);
  background: #fff;
}

.conversation-copy,
.conversation-title {
  min-width: 0;
}

.conversation-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.conversation-title strong,
.conversation-copy small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-title time {
  flex: 0 0 auto;
  color: inherit;
  opacity: 0.7;
  font-size: 0.62rem;
}

.conversation-copy small {
  margin-top: 4px;
  color: inherit;
  opacity: 0.75;
  font-size: 0.68rem;
}

.conversation-count {
  min-width: 23px;
  height: 23px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  color: var(--blue);
  background: #eaf2ff;
  font-size: 0.65rem;
}

.conversation-button.active .conversation-count {
  color: var(--blue);
  background: #fff;
}

.new-chat-panel {
  max-height: 170px;
  overflow: hidden;
}

.mobile-user-list {
  overflow-x: auto;
}

.user-button {
  display: inline-grid;
  grid-template-columns: 28px minmax(0, 1fr);
  text-align: left;
}

.user-button strong,
.user-button small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-button small {
  margin-top: 2px;
  color: inherit;
  opacity: 0.72;
  font-size: 0.68rem;
}

.mobile-active-room {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  align-items: center;
  gap: 9px;
  min-width: 0;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.mobile-active-room strong,
.mobile-active-room small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.mobile-active-room small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.72rem;
}

.mobile-message-list {
  padding-bottom: 4px;
}

.mobile-message-form {
  position: relative;
  left: auto;
  bottom: auto;
  z-index: 10;
  width: 100%;
  transform: none;
  padding: 8px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(255, 255, 255, 0.98);
}

.mobile-new-messages {
  justify-self: center;
  min-height: 34px;
  padding: 0 13px;
  border: 0;
  border-radius: 999px;
  color: #fff;
  background: var(--blue);
  box-shadow: 0 7px 20px rgba(0, 87, 173, 0.24);
  font-weight: 800;
}

.empty-state.compact {
  min-height: 60px;
  padding: 10px;
}

.empty-state small {
  display: block;
  margin-top: 5px;
}

.chat-attachment-action {
  appearance: none;
  width: 100%;
  font: inherit;
  cursor: pointer;
  text-align: left;
}

.image-attachment {
  position: relative;
  max-width: 230px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  overflow: hidden;
  background: #eef4fb;
}

.image-attachment img:not([src]) {
  min-height: 92px;
}

.attachment-loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 8px;
  color: var(--muted);
  background: #eef4fb;
  font-size: 0.7rem;
}

.image-attachment.loaded .attachment-loading {
  display: none;
}

.image-attachment.failed .attachment-loading {
  color: var(--red);
}

.file-attachment {
  grid-template-columns: minmax(0, 1fr);
  font: inherit;
  cursor: pointer;
}

.mobile-toast {
  bottom: calc(76px + env(safe-area-inset-bottom));
}

@media (min-width: 640px) {
  .mobile-chat-shell,
  .mobile-chat-gate {
    height: calc(100dvh - 32px);
    min-height: 0;
  }
}
