/* WhatsApp-like chat UI — familiar mobile messaging layout */

:root {
  --wa-header: #008069;
  --wa-header-deep: #006e5b;
  --wa-online: #c5e4dc;
  --wa-bg: #efeae2;
  --wa-panel: #f0f2f5;
  --wa-ink: #111b21;
  --wa-muted: #667781;
  --wa-bubble-in: #ffffff;
  --wa-bubble-out: #d9fdd3;
  --wa-bubble-out-ink: #111b21;
  --wa-system-bg: #ffffff;
  --wa-send: #00a884;
  --wa-send-deep: #008f6f;
  --wa-line: #e9edef;
  --wa-chip-border: #00a884;
  --wa-chip-bg: #ffffff;
  --wa-radius-bubble: 7.5px;
  --wa-font: 'Segoe UI', 'Helvetica Neue', Helvetica, Arial, sans-serif;
  --chat-safe-top: env(safe-area-inset-top, 0px);
  --chat-safe-bottom: env(safe-area-inset-bottom, 0px);
  --chat-safe-left: env(safe-area-inset-left, 0px);
  --chat-safe-right: env(safe-area-inset-right, 0px);
  --chat-ease: cubic-bezier(0.22, 1, 0.36, 1);

  /* Kept for dashboard appearance.js compatibility (mapped to WA look) */
  --chat-bg-1: var(--wa-header);
  --chat-bg-2: var(--wa-header-deep);
  --chat-surface: var(--wa-bg);
  --chat-ink: var(--wa-ink);
  --chat-muted: var(--wa-muted);
  --chat-accent: var(--wa-send);
  --chat-accent-deep: var(--wa-send-deep);
  --chat-user: var(--wa-bubble-out);
  --chat-bot: var(--wa-bubble-in);
  --chat-header: #ffffff;
  --chat-line: var(--wa-line);
  --chat-font-display: var(--wa-font);
  --chat-font-body: var(--wa-font);
  --chat-radius: 0;
}

*, *::before, *::after { box-sizing: border-box; }

html {
  height: 100%;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  font-family: var(--wa-font);
  color: var(--wa-ink);
  background: #d1d7db;
}

body.chat-page {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

.chat-shell {
  width: 100%;
  margin: 0 auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  max-height: 100dvh;
  background: #fff;
  padding: 0;
}

/* —— WhatsApp header —— */
.chat-header {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-shrink: 0;
  padding:
    calc(0.45rem + var(--chat-safe-top))
    max(0.55rem, var(--chat-safe-right))
    0.45rem
    max(0.4rem, var(--chat-safe-left));
  background: var(--wa-header);
  color: #fff;
  min-height: 56px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12);
  z-index: 2;
}

.chat-header-back {
  display: none;
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: #fff;
  border-radius: 50%;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.chat-logo {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: rgba(255, 255, 255, 0.18);
  border: none;
  flex-shrink: 0;
}

.chat-logo.placeholder {
  display: grid;
  place-items: center;
  font-size: 1.05rem;
  font-weight: 500;
  color: #fff;
}

.chat-logo.is-hidden { display: none !important; }

.chat-brand {
  min-width: 0;
  flex: 1;
}

.chat-brand h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 500;
  letter-spacing: 0;
  line-height: 1.25;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-brand p {
  margin: 0.05rem 0 0;
  color: var(--wa-online);
  font-size: 0.78rem;
  line-height: 1.25;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
  -webkit-line-clamp: unset;
}

.chat-header-actions {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  flex-shrink: 0;
  margin-left: auto;
}

.chat-links {
  display: flex;
  align-items: center;
  gap: 0.1rem;
}

.chat-links a {
  color: #fff;
  text-decoration: none;
  font-size: 0;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: none;
  background: transparent;
  opacity: 0.95;
  background-repeat: no-repeat;
  background-position: center;
  background-size: 20px 20px;
}

.chat-links a[href*="/menu"] {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23fff'%3E%3Cpath d='M3 6h18v2H3V6zm0 5h18v2H3v-2zm0 5h18v2H3v-2z'/%3E%3C/svg%3E");
}

.chat-links a[href*="/prenotazioni"] {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23fff'%3E%3Cpath d='M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V10h14v10zm0-12H5V6h14v2z'/%3E%3C/svg%3E");
}

.chat-links a:active {
  background-color: rgba(255, 255, 255, 0.12);
}

/* —— Chat panel —— */
.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--wa-bg);
  color: var(--wa-ink);
  border-radius: 0;
  overflow: hidden;
  min-height: 0;
  box-shadow: none;
  position: relative;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 0.55rem 0.7rem 0.35rem;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  min-height: 0;
  position: relative;
  z-index: 1;
  background-color: var(--wa-bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='260' height='260' viewBox='0 0 260 260'%3E%3Cg fill='none' stroke='%23d4cdc4' stroke-width='1.1' opacity='0.45'%3E%3Cpath d='M40 50c8-12 22-10 28 2M70 90c-10 4-8 18 3 20M120 40c6 10-2 20-14 16M180 70c12 2 16 14 6 22M210 120c-8 10-22 6-24-6M50 150c14 0 18 14 6 22M100 180c-4-12 8-22 20-14M160 200c10 8 2 22-12 18M200 40c-14 6-10 22 4 20M30 210c8-8 22-4 20 10M140 100c12-6 22 6 14 16'/%3E%3Ccircle cx='90' cy='60' r='3'/%3E%3Ccircle cx='200' cy='180' r='3'/%3E%3Ccircle cx='55' cy='190' r='2.5'/%3E%3Cpath d='M150 55l8 4-4 8-8-4zM75 130l6 7-7 5-5-7zM220 90l5 8-8 4-4-8z'/%3E%3C/g%3E%3C/svg%3E");
  background-size: 260px 260px;
}

.msg {
  position: relative;
  max-width: min(85%, 32rem);
  padding: 0.4rem 0.55rem 0.35rem 0.6rem;
  border-radius: var(--wa-radius-bubble);
  font-size: 0.9375rem;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 1px 0.5px rgba(11, 20, 26, 0.13);
  animation: msg-in 0.18s var(--chat-ease) both;
}

.msg .msg-text {
  display: block;
  padding-right: 0.15rem;
  padding-bottom: 0.1rem;
}

.msg .msg-text.msg-rich {
  white-space: normal;
}

.msg .msg-rich .msg-p {
  margin: 0 0 0.45rem;
}

.msg .msg-rich .msg-p:last-child {
  margin-bottom: 0;
}

.msg .msg-rich .msg-heading {
  margin: 0 0 0.4rem;
  font-weight: 700;
  font-size: 0.98em;
  line-height: 1.35;
}

.msg .msg-rich .msg-list {
  margin: 0.15rem 0 0.5rem;
  padding: 0 0 0 1.1rem;
}

.msg .msg-rich .msg-list:last-child {
  margin-bottom: 0;
}

.msg .msg-rich .msg-list li {
  margin: 0 0 0.28rem;
  padding-left: 0.1rem;
  line-height: 1.4;
}

.msg .msg-rich .msg-list li:last-child {
  margin-bottom: 0;
}

.msg .msg-rich .msg-li-detail {
  display: inline-block;
  margin-top: 0.12rem;
  color: color-mix(in srgb, currentColor 72%, transparent);
  font-size: 0.92em;
  line-height: 1.35;
}

.msg .msg-rich strong {
  font-weight: 700;
}

.msg .msg-rich em {
  font-style: italic;
}

.msg .msg-rich code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
  background: rgba(0, 0, 0, 0.06);
  padding: 0.05em 0.3em;
  border-radius: 4px;
}

.msg .msg-rich a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
  font-weight: 600;
}

.msg .msg-rich .msg-figure {
  margin: 0.45rem 0 0.35rem;
  padding: 0;
  max-width: 100%;
}

.msg .msg-rich .msg-figure:first-child {
  margin-top: 0.15rem;
}

.msg .msg-rich .msg-img-btn {
  appearance: none;
  border: 0;
  padding: 0;
  margin: 0;
  display: block;
  width: 100%;
  position: relative;
  cursor: zoom-in;
  background: transparent;
  border-radius: 10px;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

.msg .msg-rich .msg-img {
  display: block;
  width: 100%;
  max-width: 100%;
  max-height: 220px;
  object-fit: cover;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.04);
  pointer-events: none;
}

.msg .msg-rich .msg-img-zoom {
  position: absolute;
  right: 0.4rem;
  bottom: 0.4rem;
  width: 1.55rem;
  height: 1.55rem;
  border-radius: 999px;
  display: grid;
  place-items: center;
  font-size: 0.72rem;
  line-height: 1;
  color: #fff;
  background: rgba(11, 20, 26, 0.55);
  pointer-events: none;
}

.msg .msg-rich li .msg-figure {
  margin-top: 0.4rem;
}

.msg .msg-rich li .msg-img {
  max-height: 160px;
}

/* —— Lightbox foto —— */
body.lightbox-open {
  overflow: hidden;
}

.chat-lightbox[hidden] {
  display: none !important;
}

.chat-lightbox {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(0.75rem + env(safe-area-inset-top, 0px))
    calc(0.75rem + env(safe-area-inset-right, 0px))
    calc(0.75rem + env(safe-area-inset-bottom, 0px))
    calc(0.75rem + env(safe-area-inset-left, 0px));
}

.chat-lightbox-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.88);
}

.chat-lightbox-dialog {
  position: relative;
  z-index: 1;
  width: min(100%, 42rem);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.55rem;
}

.chat-lightbox-img {
  display: block;
  max-width: 100%;
  max-height: min(78dvh, 780px);
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 12px;
  background: #111;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  user-select: none;
  -webkit-user-drag: none;
}

.chat-lightbox-close {
  position: absolute;
  top: -0.15rem;
  right: 0;
  z-index: 2;
  appearance: none;
  border: 0;
  width: 2.5rem;
  height: 2.5rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 1.6rem;
  line-height: 1;
  cursor: pointer;
}

.chat-lightbox-nav {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  appearance: none;
  border: 0;
  width: 2.6rem;
  height: 2.6rem;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
  z-index: 2;
}

.chat-lightbox-nav.prev { left: 0; }
.chat-lightbox-nav.next { right: 0; }

.chat-lightbox-caption,
.chat-lightbox-counter {
  margin: 0;
  color: rgba(255, 255, 255, 0.85);
  font-size: 0.85rem;
  text-align: center;
  max-width: 100%;
}

.chat-lightbox-counter {
  font-size: 0.75rem;
  color: rgba(255, 255, 255, 0.65);
}

@media (min-width: 640px) {
  .chat-lightbox-nav.prev { left: -0.25rem; }
  .chat-lightbox-nav.next { right: -0.25rem; }
}

.msg .msg-rich .msg-table-wrap {
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0.4rem 0 0.5rem;
  border-radius: 8px;
}

.msg .msg-rich .msg-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.84rem;
  min-width: 12rem;
}

.msg .msg-rich .msg-table th,
.msg .msg-rich .msg-table td {
  padding: 0.35rem 0.45rem;
  text-align: left;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
  vertical-align: top;
}

.msg .msg-rich .msg-table th {
  font-weight: 700;
  white-space: nowrap;
  background: rgba(0, 0, 0, 0.04);
}

.msg .msg-rich .msg-table tr:last-child td {
  border-bottom: none;
}

.msg .msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.2rem;
  float: right;
  margin: 0.15rem 0 0 0.65rem;
  font-size: 0.6875rem;
  line-height: 1;
  color: var(--wa-muted);
  user-select: none;
}

.msg.bot {
  align-self: flex-start;
  background: var(--wa-bubble-in);
  color: var(--wa-ink);
  border-top-left-radius: 0;
  margin-right: auto;
}

.msg.bot::before {
  content: '';
  position: absolute;
  left: -7px;
  top: 0;
  width: 8px;
  height: 13px;
  background: var(--wa-bubble-in);
  clip-path: polygon(100% 0, 0 0, 100% 100%);
}

.msg.user {
  align-self: flex-end;
  background: var(--wa-bubble-out);
  color: var(--wa-bubble-out-ink);
  border-top-right-radius: 0;
  margin-left: auto;
}

.msg.user::before {
  content: '';
  position: absolute;
  right: -7px;
  top: 0;
  width: 8px;
  height: 13px;
  background: var(--wa-bubble-out);
  clip-path: polygon(0 0, 100% 0, 0 100%);
}

.msg.user .msg-meta {
  color: #667781;
}

.msg.system {
  align-self: center;
  max-width: 92%;
  background: var(--wa-system-bg);
  color: var(--wa-muted);
  font-size: 0.75rem;
  text-align: center;
  padding: 0.35rem 0.7rem;
  border-radius: 8px;
  box-shadow: 0 1px 0.5px rgba(11, 20, 26, 0.1);
  float: none;
}

.msg.system::before { display: none; }

.msg.system .msg-meta { display: none; }

.msg.typing {
  min-width: 3.5rem;
  padding: 0.7rem 0.9rem 0.65rem;
}

.msg.typing .msg-text {
  display: flex;
  align-items: center;
  gap: 0.28rem;
  min-height: 0.9rem;
}

.msg.typing .typing-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #99a3a9;
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.msg.typing .typing-dot:nth-child(2) { animation-delay: 0.15s; }
.msg.typing .typing-dot:nth-child(3) { animation-delay: 0.3s; }

.msg.typing .msg-meta { display: none; }

/* Suggestions as WhatsApp-style quick replies */
.chat-suggestions {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.4rem;
  padding: 0.35rem 0.65rem 0.5rem;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  flex-shrink: 0;
  position: relative;
  z-index: 1;
  background: transparent;
}

.chat-suggestions:empty { display: none; }
.chat-suggestions::-webkit-scrollbar { display: none; }

.chat-suggestions button {
  appearance: none;
  flex: 0 0 auto;
  border: 1px solid var(--wa-chip-border);
  background: var(--wa-chip-bg);
  color: var(--wa-send);
  font: inherit;
  font-size: 0.82rem;
  font-weight: 500;
  padding: 0.45rem 0.85rem;
  min-height: 36px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
  max-width: 78vw;
  overflow: hidden;
  text-overflow: ellipsis;
  box-shadow: 0 1px 2px rgba(11, 20, 26, 0.08);
}

.chat-suggestions button:active {
  background: #e7f8f3;
}

/* Composer — WhatsApp footer */
.chat-composer {
  display: flex;
  align-items: flex-end;
  gap: 0.45rem;
  padding:
    0.4rem 0.5rem
    calc(0.4rem + var(--chat-safe-bottom));
  border-top: none;
  background: var(--wa-panel);
  flex-shrink: 0;
  position: relative;
  z-index: 2;
}

.chat-composer textarea {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  border: none;
  border-radius: 24px;
  padding: 0.65rem 0.95rem;
  font: inherit;
  font-size: 16px;
  line-height: 1.35;
  color: var(--wa-ink);
  background: #fff;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.06);
}

.chat-composer textarea:focus {
  border: none;
  outline: none;
}

.chat-composer textarea::placeholder {
  color: #8696a0;
}

.chat-composer button.send {
  appearance: none;
  border: none;
  background: var(--wa-send);
  color: #fff;
  font: inherit;
  font-weight: 500;
  font-size: 0;
  width: 48px;
  height: 48px;
  min-height: 48px;
  min-width: 48px;
  padding: 0;
  border-radius: 50%;
  cursor: pointer;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  box-shadow: 0 1px 3px rgba(11, 20, 26, 0.2);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23fff'%3E%3Cpath d='M2.01 21L23 12 2.01 3 2 10l15 2-15 2z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: 55% 50%;
  background-size: 22px 22px;
}

.chat-composer button.send:active:not(:disabled) {
  background-color: var(--wa-send-deep);
}

.chat-composer button.send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.chat-footer {
  display: none;
}

.chat-error-state {
  flex: 1;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 1.5rem 1rem;
  color: var(--wa-ink);
  background: var(--wa-bg);
}

.chat-error-state h2 {
  font-size: 1.15rem;
  font-weight: 500;
  margin: 0 0 0.45rem;
}

.chat-error-state p {
  color: var(--wa-muted);
  margin: 0;
  font-size: 0.9rem;
  line-height: 1.4;
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes typing-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* —— Desktop: WhatsApp Web style centered column —— */
@media (min-width: 640px) {
  body.chat-page {
    overflow: auto;
    background:
      linear-gradient(180deg, #00a884 0 127px, transparent 127px),
      #d1d7db;
  }

  html, body {
    background:
      linear-gradient(180deg, #00a884 0 127px, transparent 127px),
      #d1d7db;
  }

  .chat-shell {
    width: min(720px, 100%);
    max-height: calc(100dvh - 2rem);
    min-height: min(92dvh, 820px);
    margin: 1rem auto;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(11, 20, 26, 0.16), 0 0 0 1px rgba(11, 20, 26, 0.04);
  }

  .chat-header {
    padding: 0.55rem 1rem;
  }

  .chat-logo {
    width: 42px;
    height: 42px;
  }

  .chat-brand h1 {
    font-size: 1.05rem;
    white-space: nowrap;
  }

  .chat-brand p {
    font-size: 0.8rem;
  }

  .chat-links a:hover {
    background: rgba(255, 255, 255, 0.12);
  }

  .chat-panel {
    min-height: 0;
  }

  .chat-messages {
    padding: 0.75rem 3.5% 0.5rem;
    gap: 0.25rem;
  }

  .msg {
    max-width: 65%;
    font-size: 0.925rem;
  }

  .chat-suggestions {
    flex-wrap: wrap;
    overflow: visible;
    padding: 0.4rem 3.5% 0.65rem;
  }

  .chat-suggestions button {
    max-width: none;
    white-space: normal;
  }

  .chat-suggestions button:hover {
    background: #e7f8f3;
  }

  .chat-composer {
    padding: 0.55rem 0.75rem 0.65rem;
  }

  .chat-composer button.send:hover:not(:disabled) {
    background-color: var(--wa-send-deep);
  }

  .chat-footer {
    display: none;
  }
}
