/* Chat de RMA — estética tipo WhatsApp, móvil primero */
:root {
  --header: #008069;
  --on-header: #ffffff;
  --accent: #00a884;
  --on-accent: #ffffff;
  --avatar-ink: #008069;
  --bubble-user: #d9fdd3;
  --bubble-bot: #ffffff;
  --wall: #efeae2;
  --ink: #111b21;
  --meta: #667781;
  --link: #027eb5;
  --tick: #53bdeb;
  --danger: #c62828;
  --shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--wall);
  color: var(--ink);
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, textarea:focus-visible { outline: 2px solid var(--link); outline-offset: 2px; }

/* ---------- Estructura ---------- */
.app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  background-color: var(--wall);
  background-image: url("img/wallpaper.svg");
  background-size: 200px 200px;
}

@media (min-width: 640px) {
  html, body { background: #d1d7db; }
  body::before {
    content: ""; position: fixed; inset: 0 0 auto 0; height: 127px; background: var(--header); z-index: 0;
  }
  .app {
    z-index: 1;
    height: min(100dvh - 40px, 860px);
    margin: 20px auto;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 6px 18px rgba(11, 20, 26, 0.2);
  }
}

/* ---------- Cabecera ---------- */
.top {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(10px + var(--safe-top)) 14px 10px;
  background: var(--header);
  color: var(--on-header);
  flex: none;
}
.avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: #fff; color: var(--avatar-ink);
  display: grid; place-items: center;
  overflow: hidden; flex: none;
  font-size: 19px; font-weight: 600;
}
.avatar img { width: 100%; height: 100%; object-fit: contain; }
.who { min-width: 0; }
.who h1 { margin: 0; font-size: 17px; font-weight: 500; line-height: 1.2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who p { margin: 1px 0 0; font-size: 13px; opacity: 0.82; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------- Conversación ---------- */
.chat { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 10px 10px 6px; scroll-behavior: auto; }
.thread { display: flex; flex-direction: column; }

.row { display: flex; margin-top: 2px; }
.row.first { margin-top: 8px; }
.row.user { justify-content: flex-end; }

.bubble {
  position: relative;
  max-width: 84%;
  padding: 6px 8px 6px 9px;
  border-radius: 8px;
  font-size: 15px;
  line-height: 1.38;
  box-shadow: var(--shadow);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  animation: pop 0.14s ease-out;
}
.row.bot .bubble { background: var(--bubble-bot); }
.row.user .bubble { background: var(--bubble-user); }

/* Colita de la burbuja, solo en la primera de cada grupo */
.row.first .bubble::before { content: ""; position: absolute; top: 0; width: 0; height: 0; border-top: 8px solid; }
.row.bot.first .bubble { border-top-left-radius: 0; }
.row.bot.first .bubble::before { left: -8px; border-top-color: var(--bubble-bot); border-left: 8px solid transparent; }
.row.user.first .bubble { border-top-right-radius: 0; }
.row.user.first .bubble::before { right: -8px; border-top-color: var(--bubble-user); border-right: 8px solid transparent; }

.meta {
  float: right;
  display: flex; align-items: center; gap: 3px;
  margin: 7px 0 -3px 12px;
  font-size: 11px; line-height: 1; color: var(--meta);
  white-space: nowrap;
}
.meta svg { width: 16px; height: 10px; color: var(--tick); }

.chip-sys {
  align-self: center;
  margin: 12px 0 4px;
  padding: 5px 12px;
  background: #fff;
  color: #54656f;
  font-size: 12.5px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  text-align: center;
  max-width: 90%;
}

/* Archivos dentro de una burbuja */
.files { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 3px; margin: 0 0 2px; min-width: 200px; }
.files.one { grid-template-columns: 1fr; min-width: 220px; }
.files img { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: 6px; display: block; background: #e9edef; }
.files.one img { aspect-ratio: 4 / 3; }
.filechip {
  grid-column: 1 / -1;
  display: flex; align-items: center; gap: 8px;
  padding: 8px; border-radius: 6px; background: rgba(11, 20, 26, 0.06);
  font-size: 14px; min-width: 0;
}
.filechip svg { width: 22px; height: 22px; flex: none; color: var(--meta); }
.filechip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Indicador "escribiendo…" */
.bubble.typing { display: flex; gap: 4px; padding: 11px 12px; animation: none; }
.bubble.typing i { width: 7px; height: 7px; border-radius: 50%; background: #9aa6ad; animation: dot 1.1s infinite ease-in-out; }
.bubble.typing i:nth-child(2) { animation-delay: 0.15s; }
.bubble.typing i:nth-child(3) { animation-delay: 0.3s; }

@keyframes pop { from { transform: scale(0.96); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes dot { 0%, 60%, 100% { transform: translateY(0); opacity: 0.5; } 30% { transform: translateY(-4px); opacity: 1; } }

/* ---------- Zona inferior ---------- */
.dock { flex: none; display: flex; flex-direction: column; }

.composer { display: flex; align-items: flex-end; gap: 6px; padding: 6px 8px calc(6px + var(--safe-bottom)); }
.field { flex: 1; min-width: 0; background: #fff; border-radius: 22px; padding: 0 16px; display: flex; align-items: center; min-height: 44px; box-shadow: var(--shadow); }
.field input, .field textarea {
  width: 100%; border: 0; outline: 0; background: transparent; resize: none;
  font: inherit; font-size: 16px; /* 16px evita el zoom automático en iOS */
  line-height: 1.3; padding: 11px 0; max-height: 128px; color: var(--ink);
}
.field input:focus-visible, .field textarea:focus-visible { outline: 0; }
.field:focus-within { outline: 2px solid var(--link); outline-offset: 1px; }

.send {
  width: 44px; height: 44px; flex: none; border: 0; border-radius: 50%;
  background: var(--accent); color: var(--on-accent); display: grid; place-items: center;
  box-shadow: var(--shadow);
}
.send svg { width: 22px; height: 22px; fill: currentColor; }
.send:disabled { opacity: 0.5; cursor: default; }

.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 2px 10px 4px; }
.chip {
  border: 0; background: #fff; color: var(--link); font-weight: 500; font-size: 15px;
  padding: 9px 16px; border-radius: 18px; box-shadow: var(--shadow);
}

/* Respuestas rápidas (botones) */
.choices { display: flex; flex-direction: column; gap: 6px; padding: 4px 10px calc(10px + var(--safe-bottom)); }
.choice {
  border: 0; background: #fff; color: var(--link); font-weight: 500; font-size: 16px;
  padding: 13px 14px; border-radius: 20px; box-shadow: var(--shadow); text-align: center;
}
.choice.primary { background: var(--accent); color: var(--on-accent); }
.choice:active, .chip:active { filter: brightness(0.94); }
.choice:disabled, .chip:disabled { opacity: 0.55; cursor: default; }

/* Panel de adjuntos */
.attach { display: flex; flex-direction: column; gap: 8px; padding: 4px 10px calc(10px + var(--safe-bottom)); }
.attach-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.attach-row .count { margin-left: auto; font-size: 13px; color: #54656f; background: #fff; padding: 6px 10px; border-radius: 14px; box-shadow: var(--shadow); }
.pick {
  display: inline-flex; align-items: center; gap: 8px;
  border: 0; background: #fff; color: var(--link); font-weight: 500; font-size: 15px;
  padding: 10px 14px; border-radius: 20px; box-shadow: var(--shadow);
}
.pick svg { width: 20px; height: 20px; fill: currentColor; }
.pick:disabled { opacity: 0.5; cursor: default; }
.thumbs { display: flex; gap: 8px; overflow-x: auto; padding: 2px 0 4px; }
.thumb { position: relative; width: 68px; height: 68px; flex: none; border-radius: 10px; overflow: hidden; background: #fff; box-shadow: var(--shadow); display: grid; place-items: center; }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb .ico { width: 28px; height: 28px; color: var(--meta); fill: currentColor; }
.thumb .nm { position: absolute; left: 0; right: 0; bottom: 0; font-size: 10px; padding: 2px 4px; background: rgba(255, 255, 255, 0.9); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.thumb .bar { position: absolute; left: 0; right: 0; bottom: 0; height: 5px; background: rgba(0, 0, 0, 0.18); }
.thumb .bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.12s linear; }
.thumb .rm {
  position: absolute; top: 3px; right: 3px; width: 22px; height: 22px; border: 0; border-radius: 50%;
  background: rgba(17, 27, 33, 0.72); color: #fff; font-size: 15px; line-height: 1; display: grid; place-items: center; padding: 0;
}
.thumb.err { outline: 2px solid var(--danger); }
.attach-actions { display: flex; gap: 6px; }
.attach-actions .choice { flex: 1; }

.toast {
  position: absolute; left: 50%; top: calc(var(--safe-top) + 72px);
  transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  background: #111b21; color: #fff; padding: 10px 14px; border-radius: 10px;
  font-size: 14px; max-width: 88%; text-align: center; transition: opacity 0.18s, transform 0.18s; z-index: 5;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: var(--danger); }

.hidden-input { position: absolute; width: 1px; height: 1px; opacity: 0; pointer-events: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
