/* Chat del pedido entre el cliente y el Tati (ver chat.js). Hoja desde abajo a casi toda la pantalla.
   Colores del tema (theme.css) más los propios del chat. Solo anima transform y opacity; con "reducir movimiento",
   nada se mueve. */
:root {
  --ch-bg: #F6F6F7;
  --ch-me-a: #D6001C;
  --ch-me-b: #FF3D55;
  --ch-other: #ECEEF1;
  --ch-other-text: #2B2E33;
  --ch-pill: #FFFFFF;
}
:root[data-theme="dark"] {
  --ch-bg: #0E1215;
  --ch-other: #2A3038;
  --ch-other-text: #E8EAEC;
  --ch-pill: #1B2026;
}

/* ---------- La hoja ---------- */
.sheet.ch-sheet {
  height: calc(100dvh - 26px); max-height: none; padding: 0; overflow: hidden;
  display: flex; flex-direction: column; background: var(--ch-bg); border-radius: 26px 26px 0 0;
}
.ch-sheet > .sheet-grab { position: absolute; left: 50%; top: 8px; margin: 0 0 0 -22px; width: 44px; z-index: 3; }
.ch-head {
  display: flex; align-items: center; gap: 12px; padding: 22px 12px 12px 16px; flex: none;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.ch-who { flex: 1; min-width: 0; }
.ch-who strong { display: block; font-size: 17px; font-weight: 800; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ch-face { width: 48px; height: 48px; flex: none; border-radius: 50%; padding: 2.5px; background: var(--tatigo-red); }
.ch-face > span {
  display: grid; place-items: center; width: 100%; height: 100%; border-radius: 50%; overflow: hidden;
  border: 2px solid var(--surface); background: var(--surface-2); color: var(--text); font-weight: 800; font-size: 18px;
}
.ch-face img { width: 100%; height: 100%; object-fit: cover; display: block; }
.ch-state {
  display: inline-flex; align-items: center; gap: 6px; margin-top: 3px; padding: 3px 10px 3px 8px; border-radius: 99px;
  font-size: 12.5px; font-weight: 700; background: var(--accent-soft); color: var(--accent-text);
}
.ch-state i { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: ch-ping 1.8s infinite; }
.ch-state.is-store { background: var(--warn-soft); color: var(--warn); }
.ch-state.is-store i { background: #F2A900; animation: none; }
.ch-state.is-arrived { background: var(--ok-soft); color: var(--ok); }
.ch-state.is-arrived i { background: var(--ok-dot); }
@keyframes ch-ping { 0% { box-shadow: 0 0 0 0 rgb(214 0 28 / .5); } 80%, 100% { box-shadow: 0 0 0 7px rgb(214 0 28 / 0); } }
.ch-btn {
  width: 44px; height: 44px; flex: none; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--surface-2); color: var(--text); cursor: pointer; text-decoration: none;
}
.ch-btn .ph-i { width: 22px; height: 22px; }
.ch-btn.is-call { background: var(--ok-soft); color: var(--ok); }
.ch-btn.is-ghost { background: transparent; width: 40px; }

/* Menú (⋯) */
.ch-menu {
  position: absolute; right: 12px; top: 74px; z-index: 4; min-width: 220px; padding: 6px; border-radius: 16px;
  background: var(--surface); box-shadow: var(--shadow-md); border: 1px solid var(--line);
}
.ch-menu button {
  display: flex; align-items: center; gap: 10px; width: 100%; padding: 12px; border: 0; border-radius: 10px;
  background: none; color: var(--text); font: 600 15px var(--font); text-align: left; cursor: pointer;
}
.ch-menu button:hover, .ch-menu button:focus-visible { background: var(--surface-2); }
.ch-menu .is-danger { color: var(--danger); }

/* ---------- Mensajes ---------- */
.ch-list {
  flex: 1; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 10px 12px 8px; display: flex; flex-direction: column; gap: 2px;
}
.ch-list::before { content: ""; flex: 1; } /* los mensajes empiezan abajo, como en todos los chats */
.ch-safety {
  align-self: center; display: flex; align-items: center; gap: 6px; max-width: 300px; margin: 2px 0 10px;
  font-size: 12px; line-height: 1.35; color: var(--muted); text-align: center;
}
.ch-safety .ph-i { width: 15px; height: 15px; flex: none; }
.ch-day { align-self: center; font-size: 12px; font-weight: 700; color: var(--muted); margin: 4px 0 6px; }
.ch-sys {
  align-self: center; display: inline-flex; align-items: center; gap: 7px; margin: 8px 0; padding: 5px 12px 5px 7px;
  border-radius: 99px; background: var(--ch-pill); border: 1px solid var(--line); box-shadow: var(--shadow-sm);
  font-size: 12.5px; font-weight: 700; color: var(--text);
}
.ch-sys .e3d { width: 22px; height: 22px; }
.ch-sys time { font-weight: 500; color: var(--muted); }

.ch-b {
  position: relative; max-width: 78%; padding: 9px 13px 8px; border-radius: 20px; font-size: 15.5px; line-height: 1.35;
  overflow-wrap: anywhere; white-space: pre-wrap;
}
.ch-b.is-me { align-self: flex-end; color: #fff; background: linear-gradient(135deg, var(--ch-me-a), var(--ch-me-b)); }
.ch-b.is-other { align-self: flex-start; background: var(--ch-other); color: var(--ch-other-text); }
/* Seguidos del mismo: las esquinas de adentro se cierran y solo el último lleva la colita. */
.ch-b.is-me.g-first, .ch-b.is-me.g-mid { border-bottom-right-radius: 7px; }
.ch-b.is-me.g-mid, .ch-b.is-me.g-last { border-top-right-radius: 7px; }
.ch-b.is-other.g-first, .ch-b.is-other.g-mid { border-bottom-left-radius: 7px; }
.ch-b.is-other.g-mid, .ch-b.is-other.g-last { border-top-left-radius: 7px; }
.ch-b.g-last, .ch-b.g-solo { margin-bottom: 8px; }
.ch-b.g-last::after, .ch-b.g-solo::after { content: ""; position: absolute; bottom: 0; width: 14px; height: 18px; }
.ch-b.is-me.g-last::after, .ch-b.is-me.g-solo::after { right: -5px; background: var(--ch-me-b); clip-path: path("M0 0 C0 10 4 15 14 18 C6 18 2 17 0 15 Z"); }
.ch-b.is-other.g-last::after, .ch-b.is-other.g-solo::after { left: -5px; background: var(--ch-other); clip-path: path("M14 0 C14 10 10 15 0 18 C8 18 12 17 14 15 Z"); }
.ch-meta {
  float: right; margin: 6px -3px -3px 10px; display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; font-weight: 600; opacity: .88; white-space: nowrap;
}
.ch-b.is-other .ch-meta { color: var(--muted); opacity: 1; }
.ch-tick { display: inline-grid; place-items: center; height: 15px; padding: 0 4px; border-radius: 99px; font-size: 10.5px; font-weight: 800; letter-spacing: -2px; }
.ch-tick.is-read { background: #fff; color: var(--tatigo-red); padding-right: 6px; }
.ch-pending { display: inline-flex; align-items: center; gap: 3px; font-style: italic; }
.ch-pending .ph-i { width: 12px; height: 12px; }
.ch-b.is-pending { opacity: .82; }
.ch-b.has-photo { padding: 4px; }
.ch-b.has-photo .ch-text { padding: 6px 9px 4px; }
.ch-b.has-photo .ch-text:empty { display: none; }
.ch-pic {
  display: block; width: 220px; max-width: 100%; aspect-ratio: 4 / 3; border: 0; padding: 0; border-radius: 16px; overflow: hidden;
  background: var(--surface-2); cursor: zoom-in;
}
.ch-pic img { width: 100%; height: 100%; object-fit: cover; display: block; }
/* Foto sola: la hora va encima de la foto, en una pastilla oscura (se lee sobre cualquier foto). */
.ch-b.has-photo > .ch-meta { position: absolute; right: 12px; bottom: 12px; margin: 0; padding: 2px 7px; border-radius: 99px; background: rgb(0 0 0 / .5); color: #fff; opacity: 1; }
.ch-b.has-photo .ch-text:not(:empty) ~ .ch-meta { position: static; float: right; background: none; margin: -2px 6px 4px 8px; padding: 0; }
.ch-b.is-other.has-photo .ch-text:not(:empty) ~ .ch-meta { color: var(--muted); }

/* Entrada de cada mensaje nuevo: un pequeño rebote */
.ch-b.is-new { animation: ch-in .38s cubic-bezier(.3, 1.5, .5, 1) both; transform-origin: bottom right; }
.ch-b.is-other.is-new { transform-origin: bottom left; }
@keyframes ch-in { from { opacity: 0; transform: translateY(10px) scale(.92); } }

.ch-typing {
  align-self: flex-start; display: inline-flex; gap: 5px; padding: 14px 16px; margin: 2px 0 6px;
  border-radius: 20px; border-bottom-left-radius: 7px; background: var(--ch-other);
}
.ch-typing i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); animation: ch-dot 1.2s infinite ease-in-out; }
.ch-typing i:nth-child(2) { animation-delay: .15s; }
.ch-typing i:nth-child(3) { animation-delay: .3s; }
@keyframes ch-dot { 0%, 60%, 100% { transform: translateY(0); opacity: .45; } 30% { transform: translateY(-5px); opacity: 1; } }

/* Sin mensajes todavía */
.ch-empty { display: grid; justify-items: center; text-align: center; gap: 8px; padding: 20px 24px 8px; }
.ch-empty .ch-halo { width: 120px; height: 120px; border-radius: 50%; display: grid; place-items: center; background: radial-gradient(circle, var(--brand-soft) 0 58%, transparent 60%); }
.ch-empty .ch-halo .e3d { width: 84px; height: 84px; animation: ch-bob 2.6s ease-in-out infinite; }
@keyframes ch-bob { 50% { transform: translateY(-6px) rotate(-3deg); } }
.ch-empty strong { font-size: 18px; font-weight: 800; }
.ch-empty p { margin: 0; color: var(--muted); font-size: 14px; max-width: 260px; }

.ch-jump {
  position: absolute; right: 16px; bottom: 132px; z-index: 2; height: 36px; padding: 0 14px; border-radius: 99px; border: 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-md); font: 700 13px var(--font);
  display: inline-flex; align-items: center; gap: 6px; cursor: pointer;
}
.ch-jump .ph-i { width: 16px; height: 16px; transform: rotate(0deg); }

/* ---------- Mensajes rápidos y campo de texto ---------- */
.ch-quick { display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px 6px; flex: none; scrollbar-width: none; }
.ch-quick::-webkit-scrollbar { display: none; }
.ch-qr {
  flex: none; display: inline-flex; align-items: center; gap: 7px; height: 42px; padding: 0 14px 0 8px; border-radius: 99px;
  border: 1.5px solid var(--brand-soft-line); background: var(--surface); color: var(--text); font: 700 14px var(--font);
  box-shadow: var(--shadow-sm); cursor: pointer;
}
.ch-qr .e3d { width: 24px; height: 24px; }
.ch-qr:active { transform: scale(.96); }
.ch-compose { display: flex; align-items: flex-end; gap: 8px; padding: 6px 12px calc(12px + env(safe-area-inset-bottom)); flex: none; }
.ch-compose .ch-btn { width: 48px; height: 48px; background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); color: var(--muted); }
.ch-field {
  flex: 1; min-height: 48px; max-height: 120px; resize: none; border: 0; border-radius: 24px; padding: 13px 18px;
  background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); color: var(--text); font: 400 16px/1.35 var(--font);
}
.ch-field:focus { outline: none; box-shadow: inset 0 0 0 2px var(--brand-soft-line); }
.ch-send {
  width: 48px; height: 48px; flex: none; border-radius: 50%; border: 0; display: grid; place-items: center; color: #fff; cursor: pointer;
  background: linear-gradient(135deg, var(--ch-me-a), var(--ch-me-b)); box-shadow: 0 6px 16px var(--brand-shadow);
  animation: ch-pop .35s cubic-bezier(.3, 1.6, .5, 1);
}
.ch-send[hidden] { display: none; }
.ch-send .ph-i { width: 22px; height: 22px; }
@keyframes ch-pop { from { transform: scale(.4); opacity: 0; } }
.ch-count { position: absolute; right: 76px; bottom: calc(66px + env(safe-area-inset-bottom)); font-size: 11.5px; font-weight: 700; color: var(--muted); }
.ch-count.is-over { color: var(--danger); }

/* ---------- Reportar ---------- */
.ch-report {
  position: absolute; inset: 0; z-index: 5; display: flex; flex-direction: column; justify-content: flex-end;
  background: rgb(0 0 0 / .35);
}
.ch-report form { background: var(--surface); border-radius: 22px 22px 0 0; padding: 20px 16px calc(16px + env(safe-area-inset-bottom)); display: grid; gap: 10px; }
.ch-report h3 { margin: 0; font-size: 19px; }
.ch-report .chips { display: flex; flex-wrap: wrap; gap: 8px; }
.ch-report textarea { min-height: 72px; }

/* Aviso al cerrarse (pedido entregado o cancelado) */
.ch-toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(92px + env(safe-area-inset-bottom)); z-index: 60; max-width: 520px; margin: 0 auto;
  display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: 18px;
  background: var(--toast-bg); color: var(--toast-text); font-weight: 700; font-size: 15px; box-shadow: var(--shadow-md);
  animation: ch-toast-in .3s cubic-bezier(.2, .8, .2, 1);
}
.ch-toast .e3d { width: 34px; height: 34px; flex: none; }
.ch-toast.is-top { top: calc(76px + env(safe-area-inset-top)); bottom: auto; }
.ch-toast.is-out { opacity: 0; transform: translateY(12px); transition: opacity .3s, transform .3s; }
@keyframes ch-toast-in { from { opacity: 0; transform: translateY(16px); } }

/* Foto en grande */
.ch-viewer { position: fixed; inset: 0; z-index: 70; background: rgb(0 0 0 / .92); display: grid; place-items: center; padding: 16px; }
.ch-viewer img { max-width: 100%; max-height: 100%; border-radius: 12px; }
.ch-viewer button { position: absolute; top: calc(12px + env(safe-area-inset-top)); right: 12px; width: 44px; height: 44px; border-radius: 50%; border: 0; background: rgb(255 255 255 / .15); color: #fff; display: grid; place-items: center; }

/* ---------- Botón del chat en el pedido: redondo, con globito de no leídos que late ---------- */
.ch-open {
  position: relative; width: 52px; height: 52px; flex: none; border-radius: 50%; border: 0; display: grid; place-items: center; cursor: pointer;
  background: var(--brand-soft); color: var(--tatigo-red); box-shadow: inset 0 0 0 1.5px var(--brand-soft-line);
}
:root[data-theme="dark"] .ch-open { color: #fff; }
.ch-open .ph-i { width: 24px; height: 24px; }
.ch-badge {
  position: absolute; top: -3px; right: -3px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 99px;
  background: var(--tatigo-red); color: #fff; font: 800 12px/22px var(--font); text-align: center;
  box-shadow: 0 0 0 2.5px var(--surface); animation: ch-beat 1.6s ease-in-out infinite;
}
@keyframes ch-beat { 0%, 100% { transform: scale(1); } 12% { transform: scale(1.18); } 24% { transform: scale(1); } }
.ch-open-lbl { display: grid; justify-items: center; gap: 4px; font-size: 11.5px; font-weight: 700; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  .ch-state i, .ch-b.is-new, .ch-typing i, .ch-empty .ch-halo .e3d, .ch-send, .ch-badge, .ch-toast { animation: none; }
  .ch-toast.is-out { transition: none; }
}
/* Modo ahorro (pantalla Ruta en teléfonos lentos): sin animaciones. */
.save-mode .ch-state i, .save-mode .ch-b.is-new, .save-mode .ch-typing i, .save-mode .ch-empty .ch-halo .e3d, .save-mode .ch-badge { animation: none; }
