/* ═══════════════════════════════════════════════════════════════
   TRIAKA WEB — Buddy Chat Dock (Messenger-style PRO)
   Sidebar fijo derecho + ventanas chat bottom-right
   Light/dark aware
═══════════════════════════════════════════════════════════════ */

:root {
  --bc-rail-w: 296px;
  --bc-win-w: 320px;
  --bc-win-h: 440px;
  --bc-glass: rgba(255, 255, 255, .78);
  --bc-glass2: rgba(255, 255, 255, .92);
  --bc-line: rgba(8, 14, 30, .08);
  --bc-line2: rgba(8, 14, 30, .04);
  --bc-shadow: 0 18px 50px -12px rgba(8, 14, 30, .18), 0 4px 12px -3px rgba(8, 14, 30, .06);
  --bc-cy: var(--acc, #00b4ff);
  --bc-cyD: var(--acc-2, #0099d6);
  --bc-ink: var(--text, #0d1620);
  --bc-ink2: var(--text2, #465465);
  --bc-ink3: var(--text3, #8492a6);
  --bc-online: #10b981;
  --bc-offline: #cbd5e1;
}
:root[data-theme="dark"] {
  --bc-glass: rgba(20, 28, 24, .85);
  --bc-glass2: rgba(13, 21, 18, .96);
  --bc-line: rgba(255, 255, 255, .08);
  --bc-line2: rgba(255, 255, 255, .04);
  --bc-shadow: 0 18px 50px -12px rgba(0, 0, 0, .55), 0 4px 12px -3px rgba(0, 0, 0, .35);
  --bc-offline: #475569;
}

/* ───── RIGHT RAIL ───── */
#chatRail {
  position: fixed;
  top: 92px;
  right: 20px;
  width: var(--bc-rail-w);
  max-height: calc(100vh - 132px);
  z-index: 60;
  background: var(--bc-glass);
  backdrop-filter: blur(28px) saturate(160%);
  -webkit-backdrop-filter: blur(28px) saturate(160%);
  border: 1px solid var(--bc-line);
  border-radius: 20px;
  box-shadow: var(--bc-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

#chatRail .cr-head {
  display: flex;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--bc-line);
  flex-shrink: 0;
  gap: 10px;
}
#chatRail .cr-head-icon {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--bc-cy), var(--bc-cyD));
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 14px;
  flex-shrink: 0;
  position: relative;
  box-shadow: 0 4px 12px -2px rgba(0, 180, 255, .35);
}
#chatRail .cr-head-icon .badge-dot {
  position: absolute;
  top: -3px;
  right: -3px;
  background: #ef4444;
  color: #fff;
  font: 700 9.5px Inter, sans-serif;
  min-width: 16px;
  height: 16px;
  border-radius: 999px;
  padding: 0 4px;
  display: grid;
  place-items: center;
  border: 2px solid var(--bc-glass2);
}
#chatRail .cr-head-text { flex: 1; min-width: 0; }
#chatRail .cr-head-title {
  font: 700 14px 'Space Grotesk', 'Inter', sans-serif;
  color: var(--bc-ink);
  letter-spacing: -.3px;
}
#chatRail .cr-head-sub {
  font: 500 11.5px 'Inter', sans-serif;
  color: var(--bc-ink3);
  margin-top: 1px;
}
#chatRail .cr-head-sub b.online-count {
  color: var(--bc-online);
  font-weight: 700;
}
#chatRail .cr-close-btn {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  border: 1px solid var(--bc-line);
  background: var(--bc-line2);
  color: var(--bc-ink2);
  cursor: pointer;
  font: 400 22px/1 'Inter', sans-serif;
  display: none;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background .15s, color .15s;
}
#chatRail .cr-close-btn:hover {
  background: rgba(239, 68, 68, .1);
  color: #ef4444;
  border-color: rgba(239, 68, 68, .25);
}
/* Visible solo en mobile (cuando se abre via FAB) */
@media (max-width: 1280px) {
  #chatRail .cr-close-btn { display: grid; }
}
#chatRail .cr-search {
  position: relative;
  padding: 0 12px 10px;
  flex-shrink: 0;
}
#chatRail .cr-search input {
  width: 100%;
  background: var(--bc-line2);
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 8px 12px 8px 32px;
  font: 500 12.5px 'Inter', sans-serif;
  color: var(--bc-ink);
  outline: none;
  transition: border-color .15s, background .15s;
}
#chatRail .cr-search input:focus {
  border-color: var(--bc-cy);
  background: var(--bc-glass2);
}
#chatRail .cr-search input::placeholder { color: var(--bc-ink3); }
#chatRail .cr-search svg {
  position: absolute;
  left: 22px;
  top: 50%;
  transform: translateY(-6px);
  color: var(--bc-ink3);
  pointer-events: none;
}

#chatRail .cr-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px 8px 10px;
  scrollbar-width: thin;
  scrollbar-color: var(--bc-line) transparent;
}
#chatRail .cr-list::-webkit-scrollbar { width: 4px; }
#chatRail .cr-list::-webkit-scrollbar-thumb { background: var(--bc-line); border-radius: 4px; }

#chatRail .cr-section {
  font: 700 10px 'Inter', sans-serif;
  letter-spacing: 1.4px;
  text-transform: uppercase;
  color: var(--bc-ink3);
  padding: 12px 10px 6px;
  display: flex;
  align-items: center;
  gap: 8px;
}
#chatRail .cr-section.add {
  color: var(--bc-cy);
  border-top: 1px solid var(--bc-line);
  margin-top: 6px;
  padding-top: 14px;
}
#chatRail .cr-section.add::before {
  content: '＋';
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0;
}
#chatRail .cr-section .count {
  background: var(--bc-line);
  color: var(--bc-ink2);
  padding: 1px 7px;
  border-radius: 999px;
  font-size: 9.5px;
  font-weight: 700;
  letter-spacing: .5px;
}
#chatRail .cr-section.online {
  color: var(--bc-online);
}
#chatRail .cr-section.online::before {
  content: '';
  width: 7px;
  height: 7px;
  background: var(--bc-online);
  border-radius: 50%;
  box-shadow: 0 0 8px var(--bc-online);
  animation: bc-pulse 2s infinite;
}
@keyframes bc-pulse { 50% { opacity: .5; } }

#chatRail .cr-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 8px 10px;
  border-radius: 12px;
  background: transparent;
  border: none;
  cursor: pointer;
  width: 100%;
  text-align: left;
  margin-bottom: 1px;
  transition: background .15s;
  position: relative;
  color: var(--bc-ink);
  font-family: inherit;
}
#chatRail .cr-item:hover { background: rgba(0, 180, 255, .08); }
#chatRail .cr-item.active { background: rgba(0, 180, 255, .14); }
#chatRail .cr-item.offline { opacity: .68; }
#chatRail .cr-item.offline:hover { opacity: 1; }

#chatRail .cr-av {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font: 700 12.5px 'Inter', sans-serif;
  position: relative;
  flex-shrink: 0;
  overflow: hidden;
}
#chatRail .cr-av img { width: 100%; height: 100%; object-fit: cover; }

/* Online/offline dot SIEMPRE visible */
#chatRail .cr-dot {
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 2.5px solid var(--bc-glass2);
  background: var(--bc-offline);
  z-index: 1;
}
#chatRail .cr-dot.on {
  background: var(--bc-online);
  box-shadow: 0 0 0 2px rgba(16, 185, 129, .22);
}

#chatRail .cr-info {
  flex: 1;
  min-width: 0;
}
#chatRail .cr-name {
  font: 600 13.5px 'Inter', sans-serif;
  color: var(--bc-ink);
  letter-spacing: -.15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
#chatRail .cr-time {
  font: 500 10.5px 'Inter', sans-serif;
  color: var(--bc-ink3);
  flex-shrink: 0;
}
#chatRail .cr-last {
  font: 500 12px 'Inter', sans-serif;
  color: var(--bc-ink3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 1px;
}
#chatRail .cr-item.unread .cr-name { font-weight: 700; }
#chatRail .cr-item.unread .cr-last { color: var(--bc-ink); font-weight: 600; }
#chatRail .cr-item.unread .cr-time { color: var(--bc-cy); font-weight: 700; }

#chatRail .cr-badge {
  position: absolute;
  right: 10px;
  top: 50%;
  transform: translateY(-50%);
  background: #ef4444;
  color: #fff;
  font: 700 10px 'Inter', sans-serif;
  min-width: 18px;
  height: 18px;
  border-radius: 999px;
  padding: 0 5px;
  display: grid;
  place-items: center;
  border: 2px solid var(--bc-glass2);
}
#chatRail .cr-item.unread .cr-name span:first-child { padding-right: 24px; }

/* "+ Agregar" external rider rows */
#chatRail .cr-item.external .cr-info .cr-last {
  color: var(--bc-ink3);
}
#chatRail .cr-item.external .cr-dot { display: none; }
#chatRail .cr-add-btn {
  background: linear-gradient(135deg, var(--bc-cy), var(--bc-cyD));
  color: #fff;
  border: none;
  font: 700 10.5px 'Inter', sans-serif;
  letter-spacing: .3px;
  padding: 6px 11px;
  border-radius: 999px;
  cursor: pointer;
  flex-shrink: 0;
  transition: transform .12s, opacity .15s;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  box-shadow: 0 2px 8px -2px rgba(0, 180, 255, .4);
}
#chatRail .cr-add-btn:hover:not(:disabled) { transform: scale(1.05); }
#chatRail .cr-add-btn:disabled { opacity: .55; cursor: default; transform: none; }
#chatRail .cr-add-btn.sent {
  background: var(--bc-line);
  color: var(--bc-ink2);
  box-shadow: none;
  cursor: default;
}
#chatRail .cr-add-btn.failed {
  background: #fee2e2;
  color: #dc2626;
  box-shadow: none;
}

#chatRail .cr-empty {
  padding: 28px 16px;
  text-align: center;
  color: var(--bc-ink3);
  font: 500 12.5px 'Inter', sans-serif;
}
#chatRail .cr-empty strong {
  display: block;
  color: var(--bc-ink);
  font: 700 13.5px 'Inter', sans-serif;
  margin-bottom: 4px;
}
#chatRail .cr-empty span { display: block; line-height: 1.45; }

/* ───── CHAT DOCK ───── */
#chatDock {
  position: fixed;
  bottom: 0;
  right: calc(var(--bc-rail-w) + 36px);
  z-index: 70;
  display: flex;
  flex-direction: row-reverse;
  align-items: flex-end;
  gap: 12px;
  pointer-events: none;
}

.cw {
  width: var(--bc-win-w);
  height: var(--bc-win-h);
  background: var(--bc-glass2);
  backdrop-filter: blur(28px) saturate(160%);
  -webkit-backdrop-filter: blur(28px) saturate(160%);
  border: 1px solid var(--bc-line);
  border-radius: 18px 18px 0 0;
  box-shadow: var(--bc-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  pointer-events: auto;
  animation: cw-open .22s cubic-bezier(.4, 0, .2, 1);
  transition: height .2s cubic-bezier(.4, 0, .2, 1);
}
@keyframes cw-open {
  from { transform: translateY(40px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.cw.minimized { height: 52px; }

.cw-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--bc-line);
  flex-shrink: 0;
  cursor: pointer;
  background: linear-gradient(180deg, rgba(255, 255, 255, .14), transparent);
  transition: background .15s;
}
:root[data-theme="dark"] .cw-head { background: linear-gradient(180deg, rgba(255, 255, 255, .04), transparent); }
.cw.minimized .cw-head { border-bottom: none; }
.cw-head:hover { background: rgba(0, 180, 255, .06); }

.cw-head-l {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  flex: 1;
}
.cw-av {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: #fff;
  font: 700 12px 'Inter', sans-serif;
  position: relative;
  flex-shrink: 0;
  overflow: hidden;
}
.cw-av img { width: 100%; height: 100%; object-fit: cover; }
.cw-av .cw-dot {
  position: absolute;
  bottom: -1px;
  right: -1px;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--bc-offline);
  border: 2px solid var(--bc-glass2);
}
.cw-av .cw-dot.on { background: var(--bc-online); box-shadow: 0 0 0 2px rgba(16,185,129,.2); }

.cw-meta { min-width: 0; flex: 1; }
.cw-name {
  font: 600 13.5px 'Inter', sans-serif;
  color: var(--bc-ink);
  letter-spacing: -.2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cw-status {
  font: 500 11px 'Inter', sans-serif;
  color: var(--bc-ink3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.cw-status.online { color: var(--bc-online); font-weight: 600; }
.cw-status.online::before { content: '●  '; }

.cw-acts { display: flex; gap: 2px; }
.cw-acts button {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--bc-ink2);
  cursor: pointer;
  font-size: 14px;
  display: grid;
  place-items: center;
  transition: background .15s;
}
.cw-acts button:hover { background: rgba(0, 0, 0, .06); color: var(--bc-ink); }
:root[data-theme="dark"] .cw-acts button:hover { background: rgba(255, 255, 255, .08); }
.cw-acts button.cw-min::before { content: '−'; font-weight: 700; font-size: 18px; line-height: 1; }
.cw-acts button.cw-close::before { content: '×'; font-weight: 400; font-size: 20px; line-height: 1; }

.cw-body {
  flex: 1;
  overflow-y: auto;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  scrollbar-width: thin;
  scrollbar-color: var(--bc-line) transparent;
  background: rgba(245, 248, 251, .35);
}
:root[data-theme="dark"] .cw-body { background: rgba(0, 0, 0, .15); }
.cw-body::-webkit-scrollbar { width: 4px; }
.cw-body::-webkit-scrollbar-thumb { background: var(--bc-line); border-radius: 4px; }
.cw.minimized .cw-body { display: none; }

.cw-loading,
.cw-empty {
  margin: auto;
  text-align: center;
  color: var(--bc-ink3);
  font: 500 12.5px 'Inter', sans-serif;
  padding: 24px 16px;
}
.cw-empty strong {
  display: block;
  color: var(--bc-ink);
  font: 700 14px 'Inter', sans-serif;
  margin-bottom: 4px;
}

.cm {
  display: flex;
  flex-direction: column;
  max-width: 78%;
}
.cm.own { align-self: flex-end; align-items: flex-end; }
.cm:not(.own) { align-self: flex-start; align-items: flex-start; }
.cm-bubble {
  padding: 9px 13px;
  border-radius: 16px;
  font: 500 13.5px 'Inter', sans-serif;
  line-height: 1.4;
  color: var(--bc-ink);
  background: #fff;
  border: 1px solid var(--bc-line);
  word-wrap: break-word;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 2px rgba(8, 14, 30, .04);
}
:root[data-theme="dark"] .cm-bubble {
  background: rgba(255, 255, 255, .06);
  color: var(--bc-ink);
  border-color: rgba(255, 255, 255, .08);
}
.cm.own .cm-bubble {
  background: linear-gradient(135deg, var(--bc-cy), var(--bc-cyD));
  color: #fff;
  border: none;
  border-bottom-right-radius: 5px;
}
.cm:not(.own) .cm-bubble { border-bottom-left-radius: 5px; }
.cm.failed .cm-bubble { opacity: .6; border: 1px solid #ef4444; }
.cm.sending .cm-bubble { opacity: .65; }
.cm-time {
  font: 500 10px 'Inter', sans-serif;
  color: var(--bc-ink3);
  margin-top: 3px;
  padding: 0 6px;
}
.cm.failed .cm-time::after { content: ' · falló'; color: #ef4444; }

.cm-day {
  align-self: center;
  font: 600 10px 'Inter', sans-serif;
  color: var(--bc-ink3);
  background: rgba(0, 0, 0, .06);
  padding: 3px 10px;
  border-radius: 999px;
  margin: 8px 0 4px;
  text-transform: uppercase;
  letter-spacing: .5px;
}
:root[data-theme="dark"] .cm-day { background: rgba(255, 255, 255, .06); }

.cw-input {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--bc-line);
  background: var(--bc-glass2);
  flex-shrink: 0;
}
.cw-input input {
  flex: 1;
  background: rgba(0, 0, 0, .04);
  border: 1px solid var(--bc-line);
  border-radius: 999px;
  padding: 9px 14px;
  font: 500 13.5px 'Inter', sans-serif;
  color: var(--bc-ink);
  outline: none;
  transition: border-color .15s, background .15s;
}
:root[data-theme="dark"] .cw-input input { background: rgba(255, 255, 255, .04); }
.cw-input input:focus { border-color: var(--bc-cy); background: #fff; }
:root[data-theme="dark"] .cw-input input:focus { background: rgba(255, 255, 255, .08); }
.cw-input input::placeholder { color: var(--bc-ink3); }
.cw-input button {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(135deg, var(--bc-cy), var(--bc-cyD));
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  font-size: 15px;
  transition: transform .12s;
}
.cw-input button:hover:not(:disabled) { transform: scale(1.06); }

/* Mobile FAB */
#chatFab {
  position: fixed;
  bottom: 22px;
  right: 22px;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--bc-cy), var(--bc-cyD));
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 22px;
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: 0 12px 28px -6px rgba(0, 180, 255, .5);
  z-index: 80;
}
#chatFab .badge-dot {
  position: absolute;
  top: -3px;
  right: -3px;
  background: #ef4444;
  color: #fff;
  font: 700 10px Inter, sans-serif;
  min-width: 20px;
  height: 20px;
  border-radius: 999px;
  padding: 0 5px;
  display: grid;
  place-items: center;
  border: 2px solid var(--bg, #fff);
}

/* En desktop, padding-right en .main para no tapar contenido */
@media (min-width: 1281px) {
  body.has-chat-rail .main { padding-right: calc(var(--bc-rail-w) + 40px) !important; }
}

@media (max-width: 1280px) {
  #chatRail { display: none; }
  #chatFab { display: flex; }
  #chatDock { right: 22px; bottom: 86px; }
}
@media (max-width: 720px) {
  .cw { width: calc(100vw - 32px); height: calc(100vh - 100px); border-radius: 16px; }
  #chatDock { right: 16px; left: 16px; bottom: 16px; flex-direction: column; }
}
