:root {
  --teal: #1c4b4b;
  --teal-dark: #123434;
  --teal-soft: #eef4f2;
  --orange: #f0a020;
  --orange-dark: #d9891a;
  --orange-soft: #fdf1de;
  --bg: #f5f6f5;
  --card: #ffffff;
  --border: #dfe3e0;
  --text: #1f2b2b;
  --text-muted: #6b7776;
  --danger: #b23a3a;
  --online: #2f9e6e;
  --busy: #d33d3d;
  --away: #e0a63c;
  --unavailable: #8a8f8f;
}
* { box-sizing: border-box; }
body { margin: 0; font-family: "Segoe UI", Arial, sans-serif; background: var(--bg); color: var(--text); }

/* ---------- LOGIN ---------- */
.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, var(--teal) 0%, var(--teal-dark) 100%);
}
.login-box { background: #fff; border-radius: 12px; padding: 32px 30px; width: 320px; max-width: 88vw; box-shadow: 0 12px 40px rgba(0,0,0,.25); }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.login-brand .brand-name { color: var(--teal); font-size: 17px; }
.login-sub { font-size: 12.5px; color: var(--text-muted); margin: 0 0 20px; }
#loginForm { display: flex; flex-direction: column; gap: 10px; }
.login-error { color: var(--danger); font-size: 12.5px; min-height: 16px; margin: 10px 0 0; }
.login-hint { font-size: 11.5px; color: var(--text-muted); margin: 14px 0 0; line-height: 1.5; }

/* ---------- HEADER ---------- */
header.app-header { display: flex; align-items: center; gap: 14px; padding: 12px 20px; background: var(--teal); color: #fff; }
.brand-mark { width: 30px; height: 30px; border-radius: 7px; background: var(--orange); color: var(--teal-dark); display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 14px; flex-shrink: 0; }
.brand-logo { height: 26px; width: auto; display: block; object-fit: contain; flex-shrink: 0; }
.login-brand .brand-logo { height: 34px; }
.brand-name { font-weight: 600; font-size: 15px; letter-spacing: .01em; }
header.app-header .badge { margin-left: auto; font-size: 12px; background: var(--orange); color: var(--teal-dark); padding: 4px 10px; border-radius: 12px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.badge-dot { width: 6px; height: 6px; border-radius: 50%; background: var(--teal-dark); }
.header-user-wrap { position: relative; }
.header-user { display: flex; align-items: center; gap: 8px; font-size: 13px; background: rgba(255,255,255,.08); padding: 5px 10px 5px 6px; border-radius: 20px; cursor: pointer; }
.avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--orange); color: var(--teal-dark); display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; flex-shrink: 0; }
.avatar-photo, .chat-avatar-photo, .msg-avatar-photo, .member-avatar-photo, .info-avatar-photo {
  object-fit: cover; border-radius: 50%; flex-shrink: 0;
}
.header-user { cursor: pointer; }
#myAvatar.avatar-photo, #headerUser .avatar { cursor: pointer; }
.admin-header-btn { background: rgba(255,255,255,.08); }
.admin-header-btn:hover { background: rgba(255,255,255,.18); }
.user-menu {
  position: absolute; top: 42px; right: 0; background: #fff; border: 1px solid var(--border);
  border-radius: 8px; box-shadow: 0 8px 24px rgba(0,0,0,.18); min-width: 180px;
  display: none; overflow: hidden; z-index: 40;
}
.user-menu.open { display: block; }
.user-menu-item { padding: 10px 14px; font-size: 13px; color: var(--text); cursor: pointer; }
.user-menu-item:hover { background: var(--bg); }
.user-menu-item.danger { color: var(--danger); }

/* ---------- SHELL ---------- */
.shell { display: grid; grid-template-columns: 300px 1fr 260px; height: calc(100vh - 54px); }
.back-btn { display: none; }
.chat-header .back-btn { display: none; }

@media (max-width: 980px) {
  .shell { grid-template-columns: 280px 1fr; }
  .info-panel { display: none; }
}

@media (max-width: 680px) {
  header.app-header { padding: 10px 12px; gap: 8px; }
  .brand-name { font-size: 13px; }
  .badge { font-size: 10.5px; padding: 3px 8px; }
  .badge span#connStatus { max-width: 90px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: inline-block; vertical-align: middle; }
  .admin-header-btn { padding: 5px 8px; font-size: 11px; }
  .header-user span#myName { display: none; }
  .header-user { padding: 5px; }

  .shell { grid-template-columns: 1fr; height: calc(100vh - 50px); }
  .sidebar { width: 100%; border-right: none; }
  .chat-main { display: none; }
  .shell.chat-open .sidebar { display: none; }
  .shell.chat-open .chat-main { display: flex; }

  .back-btn { display: flex; }
  .chat-header .back-btn { display: flex; }
  .chat-header-actions .ghost { display: none; } /* "Cerrar sesión" ya está en el menú de usuario */
  .chat-header { padding: 10px 12px; }
  .messages { padding: 14px; }
  .msg-row { max-width: 85%; }
  .composer { padding: 10px 12px; }
  .new-chat-btn { margin: 4px 10px 10px; }

  .emoji-popover { left: 12px; right: 12px; width: auto; }

  .modal-box { width: calc(100vw - 40px) !important; }
  .admin-box { width: calc(100vw - 24px); max-height: 92vh; }
  table.admin-table { font-size: 12px; }
}

.sidebar { background: var(--card); border-right: 1px solid var(--border); display: flex; flex-direction: column; min-width: 0; }
.sidebar-top { padding: 14px 14px 10px; }
.search-box { display: flex; align-items: center; gap: 8px; background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; }
.search-box input { border: none; background: transparent; outline: none; font-size: 13px; color: var(--text); width: 100%; font-family: inherit; }
.sidebar-tabs { display: flex; gap: 4px; padding: 0 14px 10px; }
.sidebar-tab { flex: 1; text-align: center; font-size: 12px; font-weight: 600; padding: 7px 4px; border-radius: 6px; color: var(--text-muted); cursor: pointer; border: 1px solid transparent; }
.sidebar-tab.active { background: var(--teal-soft); color: var(--teal); border-color: var(--border); }
.sidebar-section-label { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); padding: 10px 14px 4px; }
.chat-list { overflow-y: auto; flex: 1; padding-bottom: 10px; }
.chat-item { display: flex; align-items: center; gap: 10px; padding: 9px 14px; cursor: pointer; border-left: 3px solid transparent; }
.chat-item:hover { background: var(--bg); }
.chat-item.active { background: var(--teal-soft); border-left-color: var(--orange); }
.chat-avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--teal); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 12px; font-weight: 700; flex-shrink: 0; position: relative; }
.chat-avatar.channel { background: var(--orange-soft); color: var(--orange-dark); }
.presence-dot { position: absolute; bottom: -1px; right: -1px; width: 10px; height: 10px; border-radius: 50%; background: var(--online); border: 2px solid var(--card); display: inline-block; }
.presence-dot.status-busy { background: var(--busy); }
.presence-dot.status-away { background: var(--away); }
.presence-dot.status-unavailable { background: var(--unavailable); }
.member-status-label { font-size: 11.5px; color: var(--text-muted); }

.status-dot-inline { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--online); margin-right: 4px; vertical-align: middle; }
.status-dot-inline.status-busy { background: var(--busy); }
.status-dot-inline.status-away { background: var(--away); }
.status-dot-inline.status-unavailable { background: var(--unavailable); }

.status-popover {
  display: none;
  background: var(--bg);
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.status-popover.open { display: block; }
.status-option { padding: 9px 14px 9px 26px; font-size: 13px; color: var(--text); cursor: pointer; display: flex; align-items: center; }
.status-option:hover { background: #fff; }
.chat-item-body { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; gap: 6px; }
.chat-item-name { font-size: 13.5px; font-weight: 600; color: var(--text); }
.chat-item-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.chat-item-preview { font-size: 12.5px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 1px; }
.unread-badge { background: var(--orange); color: var(--teal-dark); font-size: 10.5px; font-weight: 700; min-width: 18px; height: 18px; border-radius: 9px; display: flex; align-items: center; justify-content: center; padding: 0 5px; flex-shrink: 0; margin-left: 6px; }
.new-chat-btn { margin: 4px 14px 12px; display: flex; align-items: center; justify-content: center; gap: 6px; width: calc(100% - 28px); }
.empty-list { padding: 20px 16px; font-size: 12.5px; color: var(--text-muted); text-align: center; }

.chat-main { display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.chat-header { display: flex; align-items: center; gap: 10px; padding: 12px 20px; background: var(--card); border-bottom: 1px solid var(--border); }
.chat-header-info { line-height: 1.25; min-width:0; }
.chat-header-name { font-size: 14.5px; font-weight: 600; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
.chat-header-sub { font-size: 12px; color: var(--text-muted); display: flex; align-items: center; gap: 5px; }
.chat-header-actions { margin-left: auto; display: flex; gap: 6px; }

.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; }
.day-divider { text-align: center; font-size: 11px; color: var(--text-muted); margin: 10px 0 16px; position: relative; }
.day-divider span { background: var(--bg); padding: 0 10px; position: relative; z-index: 1; }
.day-divider::before { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 1px solid var(--border); }
.msg-row { display: flex; gap: 10px; max-width: 68%; margin-bottom: 10px; }
.msg-row.out { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--teal); color: #fff; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; flex-shrink: 0; margin-top: 2px; }
.msg-row.out .msg-avatar { background: var(--orange); color: var(--teal-dark); }
.msg-body { min-width: 0; }
.msg-meta { font-size: 11.5px; color: var(--text-muted); margin-bottom: 3px; }
.msg-row.out .msg-meta { text-align: right; }
.msg-bubble { background: var(--card); border: 1px solid var(--border); padding: 9px 13px; border-radius: 12px 12px 12px 3px; font-size: 13.5px; line-height: 1.45; word-wrap: break-word; }
.msg-row.out .msg-bubble { background: var(--teal); color: #fff; border-color: var(--teal); border-radius: 12px 12px 3px 12px; }

.msg-deleted { font-style: italic; color: var(--text-muted); }
.msg-row.out .msg-deleted { color: rgba(255,255,255,.75); }
.msg-edited-tag { font-size: 10.5px; color: var(--text-muted); font-weight: 400; }

.msg-actions { display: flex; gap: 10px; margin-top: 3px; padding: 0 2px; }
.msg-row.out .msg-actions { justify-content: flex-end; }
.msg-action-btn {
  background: none; border: none; padding: 0; font-size: 11px; color: var(--text-muted);
  cursor: pointer; font-weight: 600;
}
.msg-action-btn:hover { color: var(--teal); text-decoration: underline; }
.msg-action-btn.danger:hover { color: var(--danger); }

.msg-edit-textarea {
  width: 100%; min-width: 220px; min-height: 54px; resize: vertical; box-sizing: border-box;
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px;
  font-family: inherit; font-size: 13.5px; color: var(--text); background: #fff;
}
.msg-edit-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.msg-edit-actions button { font-size: 12px; padding: 6px 12px; }
.msg-file { display: flex; align-items: center; gap: 8px; background: var(--orange-soft); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 12.5px; }
.msg-row.out .msg-file { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.25); color: #fff; }
.msg-file a { color: inherit; text-decoration: underline; }
.msg-file-icon { width: 26px; height: 26px; border-radius: 5px; background: #faece7; color: #993c1d; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.msg-image-link { display: block; }
.msg-image-preview {
  display: block; max-width: 100%; width: auto; max-height: 260px;
  border-radius: 8px; border: 1px solid var(--border); object-fit: cover; cursor: zoom-in;
}
.msg-row.out .msg-image-preview { border-color: rgba(255,255,255,.3); }
.typing-row { font-size: 12px; color: var(--text-muted); padding: 4px 20px; font-style: italic; background: var(--bg); }

.composer { display: flex; align-items: flex-end; gap: 8px; padding: 12px 16px; background: var(--card); border-top: 1px solid var(--border); position: relative; }
.composer-input { flex: 1; border: 1px solid var(--border); border-radius: 20px; padding: 10px 16px; font-size: 13.5px; font-family: inherit; color: var(--text); background: var(--bg); outline: none; }
.composer-input:focus { border-color: var(--orange); background: #fff; }
.icon-round { width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--border); background: var(--card); color: var(--text-muted); display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; font-size: 17px; }
.icon-round:hover { border-color: var(--orange); color: var(--orange-dark); }
.send-btn { width: 38px; height: 38px; border-radius: 50%; background: var(--orange); color: var(--teal-dark); border: none; display: flex; align-items: center; justify-content: center; cursor: pointer; flex-shrink: 0; }
.send-btn:hover:not(:disabled) { background: var(--orange-dark); }
.send-btn:disabled, .composer-input:disabled { opacity: .6; cursor: not-allowed; }
.privacy-note.compact { font-size: 11px; color: var(--text-muted); padding: 6px 16px 10px; background: var(--card); border-top: none; margin: 0; }

.info-panel { background: var(--card); border-left: 1px solid var(--border); padding: 18px 16px; overflow-y: auto; }
.info-panel h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); margin: 18px 0 8px; }
.info-panel h3:first-child { margin-top: 0; }
.info-topline { text-align: center; }
.info-avatar-lg { width: 64px; height: 64px; border-radius: 50%; background: var(--orange-soft); color: var(--orange-dark); display: flex; align-items: center; justify-content: center; font-size: 22px; font-weight: 700; margin: 0 auto 10px; }
.info-name { font-size: 15px; font-weight: 600; }
.info-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.member-row { display: flex; align-items: center; gap: 8px; padding: 6px 0; font-size: 13px; }
.member-row .presence-wrap { position: relative; }
.member-avatar { width: 26px; height: 26px; border-radius: 50%; background: var(--teal); color: #fff; font-size: 10px; font-weight: 700; display: flex; align-items: center; justify-content: center; }
.info-btn { width: 100%; margin-top: 6px; background: transparent; border: 1px solid var(--teal); color: var(--teal); border-radius: 6px; padding: 8px; font-size: 12.5px; font-weight: 600; cursor: pointer; }
.info-btn.danger { border-color: var(--danger); color: var(--danger); }

button { font-family: inherit; font-size: 13px; font-weight: 600; border: none; border-radius: 6px; padding: 8px 14px; cursor: pointer; transition: opacity .15s ease; }
button.primary { background: var(--orange); color: var(--teal-dark); }
button.primary:hover { background: var(--orange-dark); }
button.primary:disabled { opacity: .5; cursor: not-allowed; }
button.ghost { background: transparent; color: var(--teal); border: 1px solid var(--teal); padding: 6px 12px; font-size: 12px; }

.modal-overlay { position: fixed; inset: 0; background: rgba(20,30,30,.5); display: none; align-items: center; justify-content: center; z-index: 50; }
.modal-overlay.open { display: flex; }
.modal-box { background: #fff; border-radius: 10px; padding: 22px; width: 340px; box-shadow: 0 8px 30px rgba(0,0,0,.25); }
.modal-title-row { display:flex; align-items:center; }
.modal-title { font-size: 14px; font-weight: 600; margin: 0; }
.modal-sub { font-size: 12px; color: var(--text-muted); margin: 4px 0 14px; }
.modal-input { width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: 6px; font-size: 13.5px; margin-bottom: 10px; font-family: inherit; }
.modal-result { display: flex; align-items: center; gap: 8px; padding: 7px 8px; border-radius: 6px; font-size: 13px; cursor: pointer; }
.modal-result:hover { background: var(--bg); }
.close-x { margin-left: auto; cursor: pointer; color: var(--text-muted); font-size: 16px; background: none; padding: 2px 6px; }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: var(--teal-dark); color: #fff; font-size: 12.5px; padding: 9px 16px; border-radius: 20px; box-shadow: 0 4px 14px rgba(0,0,0,.25); opacity: 0; pointer-events: none; transition: opacity .2s ease; z-index: 60; }
.toast.show { opacity: 1; }

/* ---------- EMOJI POPOVER ---------- */
.emoji-popover {
  display: none;
  position: absolute;
  bottom: 62px;
  left: 60px;
  width: 300px;
  max-width: calc(100vw - 32px);
  height: 320px;
  max-height: 60vh;
  overflow-y: auto;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
  padding: 6px 10px 10px;
  z-index: 45;
}
.emoji-popover.open { display: block; }
.emoji-section-title {
  font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-muted); padding: 8px 4px 4px; position: sticky; top: 0; background: #fff;
}
.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(30px, 1fr));
  gap: 2px;
}
.emoji-grid button {
  background: transparent; border: none; font-size: 19px; padding: 4px 0;
  border-radius: 6px; cursor: pointer; line-height: 1.3;
}
.emoji-grid button:hover { background: var(--bg); }

/* ---------- ADMIN PANEL ---------- */
.field-label { display: block; font-size: 11.5px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .03em; margin: 10px 0 4px; }
.field-label:first-of-type { margin-top: 0; }
.checkbox-row { display: flex; align-items: center; gap: 8px; font-size: 13px; margin: 14px 0 4px; cursor: pointer; }

.admin-box {
  background: #fff; border-radius: 12px; width: 640px; max-width: 92vw; max-height: 84vh;
  display: flex; flex-direction: column; box-shadow: 0 12px 40px rgba(0,0,0,.3); overflow: hidden;
}
.admin-header { display: flex; align-items: center; padding: 18px 22px 10px; }
.admin-header .close-x { font-size: 18px; }
.admin-tabs { display: flex; gap: 4px; padding: 0 22px 14px; border-bottom: 1px solid var(--border); }
.admin-tab { padding: 8px 14px; font-size: 13px; font-weight: 600; color: var(--text-muted); cursor: pointer; border-radius: 6px 6px 0 0; }
.admin-tab.active { color: var(--teal); background: var(--teal-soft); }
.admin-panel-body { padding: 16px 22px 22px; overflow-y: auto; flex: 1; }
.admin-toolbar { display: flex; justify-content: flex-end; margin-bottom: 12px; }

table.admin-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.admin-table th {
  text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .03em;
  color: var(--text-muted); padding: 8px 10px; border-bottom: 1px solid var(--border);
}
table.admin-table td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
table.admin-table tr:last-child td { border-bottom: none; }
.admin-row-name { font-weight: 600; }
.admin-row-sub { font-size: 11.5px; color: var(--text-muted); }
.role-pill {
  font-size: 10.5px; font-weight: 700; padding: 2px 8px; border-radius: 10px;
  background: var(--orange-soft); color: var(--orange-dark); display: inline-block;
}
.row-btns { display: flex; gap: 6px; justify-content: flex-end; }
.row-btn {
  background: transparent; border: 1px solid var(--border); color: var(--text-muted);
  border-radius: 5px; padding: 4px 9px; font-size: 11.5px; cursor: pointer; font-weight: 600;
}
.row-btn:hover { border-color: var(--teal); color: var(--teal); }
.row-btn.danger:hover { border-color: var(--danger); color: var(--danger); }

.member-checklist {
  max-height: 200px; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px;
  padding: 6px 10px; margin-top: 2px;
}
.member-check-row { display: flex; align-items: center; gap: 8px; padding: 6px 2px; font-size: 13px; cursor: pointer; }
.member-check-row:not(:last-child) { border-bottom: 1px solid var(--bg); }
