/* ============================================================================
 * DTR · Carbs Widget · CSS
 * ----------------------------------------------------------------------------
 * FAB flotante + drawer/bottom-sheet + campanita de notificaciones.
 * Se engancha al index.html sin modificar los estilos base.
 * Paleta DTR: navy #0A1A2F/#0D1B2A, cards blancas, azul #2F7DD1/#4A8FD4.
 * ==========================================================================*/

:root {
  --carbs-blue: #2F7DD1;
  --carbs-blue-dark: #245e9d;
  --carbs-navy: #0A1A2F;
  --carbs-navy-2: #0D1B2A;
  --carbs-bubble-user: #2F7DD1;
  --carbs-bubble-bot: #F1F5F9;
  --carbs-border: #E2E8F0;
  --carbs-muted: #64748B;
  --carbs-warn: #EAB308;
  --carbs-danger: #EF4444;
  --carbs-radius: 18px;
  --carbs-radius-lg: 24px;
  --carbs-shadow: 0 12px 30px rgba(10, 26, 47, .18);
  --carbs-shadow-lg: 0 24px 60px rgba(10, 26, 47, .28);
  --carbs-z-fab: 900;
  --carbs-z-drawer: 950;
  --carbs-z-topbar-bell: 60;
}

/* ============================================================================
 * FAB (Floating Action Button) — solo visible en vista nutrición
 * ==========================================================================*/

#carbs-fab {
  position: fixed;
  right: 20px;
  bottom: calc(104px + env(safe-area-inset-bottom, 0px));
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: #fff;
  border: 3px solid var(--carbs-blue);
  box-shadow: var(--carbs-shadow);
  cursor: pointer;
  z-index: var(--carbs-z-fab);
  overflow: hidden;
  padding: 0;
  transition: transform .18s ease, box-shadow .18s ease;
  display: none; /* JS lo activa cuando aplique */
}
#carbs-fab.is-visible { display: block; animation: carbs-fab-in .35s ease; }
#carbs-fab:hover { transform: translateY(-2px) scale(1.03); box-shadow: var(--carbs-shadow-lg); }
#carbs-fab:active { transform: scale(0.97); }

#carbs-fab img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  border-radius: 50%;
}

/* Fallback si no carga la imagen: iniciales "C" */
#carbs-fab.no-img {
  background: var(--carbs-blue);
  color: white;
  font-family: 'Inter', sans-serif;
  font-weight: 800;
  font-size: 26px;
  display: flex; align-items: center; justify-content: center;
}
#carbs-fab.no-img::before { content: "C"; }

/* Badge de notificaciones sobre el FAB */
#carbs-fab .carbs-badge {
  position: absolute;
  top: -4px; right: -4px;
  min-width: 22px; height: 22px;
  padding: 0 6px;
  background: var(--carbs-danger);
  color: white;
  border-radius: 22px;
  font-family: 'Inter', sans-serif;
  font-weight: 700; font-size: 12px;
  display: none;
  align-items: center; justify-content: center;
  border: 2px solid white;
}
#carbs-fab.has-unread .carbs-badge { display: flex; }
#carbs-fab.has-unread { animation: carbs-pulse 1.8s ease-in-out infinite; }

@keyframes carbs-fab-in {
  from { transform: translateY(20px) scale(.7); opacity: 0; }
  to { transform: none; opacity: 1; }
}
@keyframes carbs-pulse {
  0%, 100% { box-shadow: var(--carbs-shadow), 0 0 0 0 rgba(239, 68, 68, .55); }
  50%      { box-shadow: var(--carbs-shadow), 0 0 0 12px rgba(239, 68, 68, 0); }
}

/* ============================================================================
 * Drawer / bottom-sheet del chat
 * ==========================================================================*/

#carbs-drawer-backdrop {
  position: fixed; inset: 0;
  background: rgba(10, 26, 47, .45);
  opacity: 0; visibility: hidden;
  transition: opacity .2s ease, visibility 0s linear .2s;
  z-index: var(--carbs-z-drawer);
}
#carbs-drawer-backdrop.open {
  opacity: 1; visibility: visible;
  transition: opacity .2s ease;
}

#carbs-drawer {
  position: fixed;
  right: 0; top: 0; bottom: 0;
  width: min(440px, 100vw);
  background: #fff;
  box-shadow: var(--carbs-shadow-lg);
  z-index: calc(var(--carbs-z-drawer) + 1);
  transform: translateX(100%);
  transition: transform .28s cubic-bezier(.2, .8, .2, 1);
  display: flex; flex-direction: column;
}
#carbs-drawer.open { transform: translateX(0); }

/* En móvil: bottom-sheet */
@media (max-width: 560px) {
  #carbs-drawer {
    left: 0; right: 0; top: auto;
    width: 100%; height: 88vh;
    border-radius: 22px 22px 0 0;
    transform: translateY(100%);
  }
  #carbs-drawer.open { transform: translateY(0); }
}

/* ── Header ── */
.carbs-drawer-head {
  display: flex; align-items: center; gap: 12px;
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--carbs-border);
  background: linear-gradient(135deg, var(--carbs-navy), var(--carbs-navy-2));
  color: white;
}
.carbs-drawer-head .avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: white center/cover no-repeat;
  border: 2px solid var(--carbs-blue);
  flex-shrink: 0;
}
.carbs-drawer-head .avatar.no-img {
  background: var(--carbs-blue);
  display: flex; align-items: center; justify-content: center;
  font-family: 'Inter', sans-serif; font-weight: 800; font-size: 16px;
}
.carbs-drawer-head .avatar.no-img::before { content: "C"; }
.carbs-drawer-head .titles { flex: 1; min-width: 0; }
.carbs-drawer-head .title  { font-family: 'Inter'; font-weight: 700; font-size: 15px; line-height: 1.2; }
.carbs-drawer-head .subtitle {
  font-family: 'Inter'; font-size: 12px; opacity: .7; margin-top: 2px;
  display: flex; align-items: center; gap: 6px;
}
.carbs-drawer-head .subtitle .dot {
  display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: #22c55e; box-shadow: 0 0 6px #22c55e;
}
.carbs-drawer-head .close-btn {
  background: none; border: none; color: white; cursor: pointer;
  padding: 6px; border-radius: 8px; opacity: .8;
  display: flex; align-items: center; justify-content: center;
}
.carbs-drawer-head .close-btn:hover { opacity: 1; background: rgba(255,255,255,.1); }

/* ── Chips contextuales ── */
.carbs-chips {
  display: flex; gap: 8px;
  padding: 12px 14px;
  overflow-x: auto;
  border-bottom: 1px solid var(--carbs-border);
  background: #fafbfc;
  scrollbar-width: thin;
}
.carbs-chips::-webkit-scrollbar { height: 4px; }
.carbs-chips::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
.carbs-chip {
  flex-shrink: 0;
  padding: 8px 14px;
  background: white;
  border: 1px solid var(--carbs-border);
  border-radius: 999px;
  font-family: 'Inter', sans-serif; font-size: 12.5px; font-weight: 500;
  color: var(--carbs-navy);
  cursor: pointer;
  transition: all .15s ease;
  white-space: nowrap;
}
.carbs-chip:hover {
  background: var(--carbs-blue);
  color: white;
  border-color: var(--carbs-blue);
  transform: translateY(-1px);
}
.carbs-chip.is-context {
  border-color: var(--carbs-blue);
  color: var(--carbs-blue);
  font-weight: 600;
}

/* ── Zona de mensajes ── */
.carbs-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px;
  display: flex; flex-direction: column; gap: 10px;
  background: #fff;
}

.carbs-msg {
  max-width: 82%;
  padding: 10px 13px;
  border-radius: 16px;
  font-family: 'Inter', sans-serif; font-size: 14px;
  line-height: 1.42;
  word-wrap: break-word;
  white-space: pre-wrap;
  animation: carbs-msg-in .18s ease;
}
.carbs-msg.user {
  align-self: flex-end;
  background: var(--carbs-bubble-user);
  color: white;
  border-bottom-right-radius: 6px;
}
.carbs-msg.bot {
  align-self: flex-start;
  background: var(--carbs-bubble-bot);
  color: var(--carbs-navy);
  border-bottom-left-radius: 6px;
}
.carbs-msg.escalar {
  background: #FEF3C7;
  color: #713F12;
  border: 1px solid #FDE68A;
}
.carbs-msg .escalar-btn {
  display: block;
  margin-top: 10px;
  padding: 8px 14px;
  background: var(--carbs-blue);
  color: white;
  border: none;
  border-radius: 999px;
  font-family: 'Inter', sans-serif; font-weight: 600; font-size: 13px;
  cursor: pointer;
  transition: background .15s;
}
.carbs-msg .escalar-btn:hover { background: var(--carbs-blue-dark); }

.carbs-msg-time {
  align-self: flex-start;
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  color: var(--carbs-muted);
  margin: 4px 0 -4px 4px;
}
.carbs-msg-time.user { align-self: flex-end; margin: 4px 4px -4px 0; }

.carbs-typing {
  align-self: flex-start;
  padding: 10px 14px;
  background: var(--carbs-bubble-bot);
  border-radius: 16px;
  border-bottom-left-radius: 6px;
  display: flex; gap: 4px;
}
.carbs-typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--carbs-muted);
  animation: carbs-dot 1.2s infinite ease-in-out;
}
.carbs-typing span:nth-child(2) { animation-delay: .15s; }
.carbs-typing span:nth-child(3) { animation-delay: .3s; }

@keyframes carbs-dot {
  0%, 60%, 100% { transform: translateY(0); opacity: .4; }
  30% { transform: translateY(-4px); opacity: 1; }
}
@keyframes carbs-msg-in {
  from { transform: translateY(4px); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.carbs-empty {
  text-align: center;
  color: var(--carbs-muted);
  padding: 32px 20px;
  font-family: 'Inter', sans-serif; font-size: 13.5px;
  line-height: 1.5;
}
.carbs-empty strong { color: var(--carbs-navy); display: block; margin-bottom: 4px; }

/* ── Input ── */
.carbs-input-area {
  border-top: 1px solid var(--carbs-border);
  padding: 12px 12px 14px;
  background: white;
}
.carbs-input-row {
  display: flex; gap: 8px; align-items: flex-end;
}
.carbs-input {
  flex: 1;
  min-height: 42px; max-height: 120px;
  padding: 11px 14px;
  border: 1px solid var(--carbs-border);
  border-radius: 22px;
  font-family: 'Inter', sans-serif; font-size: 14px;
  resize: none;
  outline: none;
  transition: border-color .15s;
}
.carbs-input:focus { border-color: var(--carbs-blue); }
.carbs-input:disabled { background: #f8fafc; color: var(--carbs-muted); }

.carbs-send-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--carbs-blue);
  color: white;
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .1s;
  flex-shrink: 0;
}
.carbs-send-btn:hover:not(:disabled) { background: var(--carbs-blue-dark); }
.carbs-send-btn:active { transform: scale(.94); }
.carbs-send-btn:disabled { background: #cbd5e1; cursor: not-allowed; }

.carbs-input-hint {
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  color: var(--carbs-muted);
  margin-top: 6px;
  padding: 0 6px;
}

/* ── Botón adjuntar foto ── */
.carbs-attach-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: #F1F5F9;
  color: var(--carbs-navy);
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, transform .1s;
  flex-shrink: 0;
}
.carbs-attach-btn:hover:not(:disabled) { background: #E2E8F0; }
.carbs-attach-btn:active { transform: scale(.94); }
.carbs-attach-btn:disabled { opacity: .5; cursor: not-allowed; }
.carbs-attach-btn svg { width: 20px; height: 20px; }
.carbs-attach-input { display: none; }

/* ── Preview de foto seleccionada (antes de enviar) ── */
.carbs-preview {
  display: none;
  margin: 0 0 8px 0;
  padding: 8px;
  background: #F8FAFC;
  border: 1px solid var(--carbs-border);
  border-radius: 12px;
  position: relative;
}
.carbs-preview.is-visible { display: block; }
.carbs-preview img {
  max-width: 100%;
  max-height: 180px;
  border-radius: 8px;
  display: block;
  margin: 0 auto;
  object-fit: cover;
}
.carbs-preview-remove {
  position: absolute;
  top: 12px; right: 12px;
  width: 26px; height: 26px;
  border-radius: 50%;
  background: rgba(10, 26, 47, .75);
  color: white;
  border: none;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 700;
  padding: 0; line-height: 1;
}
.carbs-preview-remove:hover { background: var(--carbs-danger); }
.carbs-preview-status {
  font-family: 'Inter', sans-serif;
  font-size: 11px;
  color: var(--carbs-muted);
  margin-top: 6px;
  text-align: center;
}

/* ── Imagen dentro de burbuja de mensaje ── */
.carbs-msg-image {
  display: block;
  max-width: 100%;
  max-height: 260px;
  border-radius: 12px;
  margin-bottom: 6px;
  object-fit: cover;
  cursor: pointer;
  transition: transform .15s;
}
.carbs-msg-image:hover { transform: scale(1.01); }
.carbs-msg.user .carbs-msg-image { border: 2px solid rgba(255,255,255,.2); }

/* ── Lightbox al hacer click en foto ── */
.carbs-lightbox {
  position: fixed; inset: 0;
  background: rgba(10, 26, 47, .92);
  z-index: 10000;
  display: none;
  align-items: center; justify-content: center;
  padding: 20px;
  cursor: zoom-out;
}
.carbs-lightbox.open { display: flex; }
.carbs-lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 8px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4);
}

.carbs-error {
  padding: 10px 14px; margin: 8px 14px;
  background: #FEE2E2; color: #991B1B;
  border-radius: 10px;
  font-family: 'Inter', sans-serif; font-size: 13px;
}

/* ============================================================================
 * Campanita de notificaciones (topbar)
 * ==========================================================================*/

.carbs-notif-bell {
  position: fixed;
  right: 26px;
  bottom: calc(180px + env(safe-area-inset-bottom, 0px));
  width: 42px; height: 42px;
  border-radius: 50%;
  background: #fff;
  border: 1px solid var(--carbs-border);
  box-shadow: var(--carbs-shadow);
  cursor: pointer;
  display: none; /* JS la muestra dentro de Nutrición */
  align-items: center; justify-content: center;
  color: var(--carbs-navy);
  transition: background .15s, transform .15s;
  z-index: var(--carbs-z-fab);
}
.carbs-notif-bell.is-visible { display: flex; }
.carbs-notif-bell:hover { background: #fff; transform: translateY(-1px); }

.carbs-notif-bell svg { width: 22px; height: 22px; }

.carbs-notif-bell .carbs-badge {
  position: absolute;
  top: 2px; right: 2px;
  min-width: 18px; height: 18px;
  padding: 0 5px;
  background: var(--carbs-danger);
  color: white;
  border-radius: 18px;
  font-family: 'Inter', sans-serif;
  font-weight: 700; font-size: 10.5px;
  display: none;
  align-items: center; justify-content: center;
  border: 2px solid white;
}
.carbs-notif-bell.has-unread .carbs-badge { display: flex; }

/* ── Panel desplegable de notifs ── */
.carbs-notif-panel {
  position: fixed;
  right: 16px; bottom: calc(232px + env(safe-area-inset-bottom, 0px));
  width: min(360px, calc(100vw - 24px));
  max-height: 70vh;
  background: white;
  border-radius: var(--carbs-radius);
  box-shadow: var(--carbs-shadow-lg);
  z-index: calc(var(--carbs-z-drawer) + 5);
  overflow: hidden;
  display: flex; flex-direction: column;
  opacity: 0; visibility: hidden; transform: translateY(-8px);
  transition: opacity .18s, transform .18s, visibility 0s linear .18s;
}
.carbs-notif-panel.open {
  opacity: 1; visibility: visible; transform: none;
  transition: opacity .18s, transform .18s;
}

.carbs-notif-head {
  padding: 14px 16px; border-bottom: 1px solid var(--carbs-border);
  display: flex; justify-content: space-between; align-items: center;
  font-family: 'Inter', sans-serif; font-weight: 700; font-size: 14px;
  color: var(--carbs-navy);
}
.carbs-notif-head .mark-all {
  background: none; border: none; color: var(--carbs-blue);
  cursor: pointer; font-size: 12px; font-weight: 500;
}
.carbs-notif-head .mark-all:hover { text-decoration: underline; }

.carbs-notif-list {
  flex: 1; overflow-y: auto;
}
.carbs-notif-item {
  padding: 12px 16px;
  border-bottom: 1px solid #f1f5f9;
  cursor: pointer;
  transition: background .12s;
  position: relative;
}
.carbs-notif-item:hover { background: #f8fafc; }
.carbs-notif-item.unread { background: #f0f7ff; }
.carbs-notif-item.unread::before {
  content: '';
  position: absolute; left: 6px; top: 20px;
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--carbs-blue);
}
.carbs-notif-item .notif-title {
  font-family: 'Inter', sans-serif; font-weight: 600; font-size: 13.5px;
  color: var(--carbs-navy);
  margin-bottom: 2px;
}
.carbs-notif-item .notif-body {
  font-family: 'Inter', sans-serif; font-size: 12.5px;
  color: var(--carbs-muted);
  line-height: 1.4;
}
.carbs-notif-item .notif-time {
  font-family: 'Inter', sans-serif; font-size: 11px;
  color: var(--carbs-muted);
  margin-top: 4px;
}
.carbs-notif-empty {
  padding: 40px 20px; text-align: center;
  color: var(--carbs-muted);
  font-family: 'Inter', sans-serif; font-size: 13px;
}

/* Ajuste final DTR: campanita flotante junto al FAB de Carbs */
@media (max-width: 560px) {
  #carbs-fab { right: 18px; bottom: calc(102px + env(safe-area-inset-bottom, 0px)); width:64px; height:64px; }
  #carbs-notif-bell { right: 24px; bottom: calc(174px + env(safe-area-inset-bottom, 0px)); }
  .carbs-notif-panel { right: 12px; bottom: calc(224px + env(safe-area-inset-bottom, 0px)); top: auto; }
}

/* V5: los avisos de Carbs se fusionan con el panel de Avisos de Nutrición. */
#carbs-notif-bell,#carbs-notif-panel{display:none!important;}
#carbs-fab .carbs-badge{display:none!important;}
