/* The chat panel (js/chat.js builds it; window.RitornoChat). Reads the site tokens from css/site.css:
   every colour is a token, or a color-mix() of one inside @supports (the plain token paints without it);
   every movement uses --ease-return and a --dur-* token. No shadows: glass, blur and hairlines only.
   Desktop: a glass panel rising from the pill, bottom right, never covering the page.
   Phone (≤ 760px): a bottom sheet above the on-screen keyboard (--chat-kb / --chat-vh from visualViewport),
   and a docked "Ask Ritorno" bar at the bottom while no page pill is in view. */

.chat {
  --chat-bg: var(--night);
  --chat-line: var(--glass-line);
  --chat-rule: var(--glass-line);
  --chat-field-line: var(--glass-line);
  --chat-field: var(--glass);
  --chat-well: var(--night);
  position: fixed;
  z-index: 1000;
  right: max(1rem, env(safe-area-inset-right, 0px));
  bottom: max(1rem, env(safe-area-inset-bottom, 0px));
  width: min(26rem, calc(100vw - 2rem));
  height: min(40rem, calc(100dvh - 2rem));
  display: flex;
  flex-direction: column;
  color: var(--paper);
  background: var(--chat-bg);
  border: 1px solid var(--chat-line);
  border-radius: var(--r-card);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
  backdrop-filter: blur(18px) saturate(1.2);
  font-family: var(--sans);
  font-size: 1rem;
  line-height: 1.45;
  transform-origin: 100% 100%;
  animation: chat-rise var(--dur-settle) var(--ease-return) both;
}
.chat[hidden], .chat-scrim[hidden], .chat [hidden] { display: none !important; }

@keyframes chat-rise {
  from { opacity: 0; transform: translateY(0.75rem) scale(0.97); }
  to { opacity: 1; transform: none; }
}

.chat-head {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0.75rem 0.75rem 1.125rem;
  border-bottom: 1px solid var(--chat-rule);
}
.chat-avatar { width: 2rem; height: 2rem; flex: none; }
.chat-title {
  margin: 0;
  flex: 1;
  font-size: 1.0625rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.chat-close {
  flex: none;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: var(--r-pill);
  border: 1px solid var(--chat-line);
  background: var(--glass);
  color: inherit;
  font: inherit;
  font-size: 1.375rem;
  line-height: 1;
  cursor: pointer;
}
.chat-close:hover { background: var(--glass-strong); }

/* CSS dot mark, only when the logo file is missing */
.chat-dots { display: none; }
.chat-head--dots .chat-dots {
  display: block;
  width: 2rem;
  height: 2rem;
  flex: none;
  background:
    radial-gradient(circle at 30% 30%, var(--signal-soft) 0 3px, transparent 3.5px),
    radial-gradient(circle at 70% 30%, var(--signal-soft) 0 2px, transparent 2.5px),
    radial-gradient(circle at 30% 70%, var(--signal-soft) 0 2px, transparent 2.5px),
    radial-gradient(circle at 70% 70%, var(--lamp) 0 3px, transparent 3.5px);
}

/* thinking: the mark pulses (never under reduced motion; chat.js doesn't set the class then) */
.chat.is-thinking .chat-avatar,
.chat.is-thinking .chat-dots { animation: chat-pulse var(--dur-settle) var(--ease-return) infinite alternate; }
@keyframes chat-pulse {
  from { opacity: 1; transform: scale(1); }
  to { opacity: 0.45; transform: scale(0.9); }
}

.chat-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1rem 1.125rem 0.5rem;
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  scrollbar-width: thin;
}
.chat-msg {
  margin: 0;
  max-width: 92%;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.chat-msg--bot { align-self: flex-start; color: var(--paper); }
.chat-msg--me {
  align-self: flex-end;
  padding: 0.5rem 0.875rem;
  border-radius: 1.25rem;
  background: var(--plane);
  border: 1px solid var(--line-dark);
  color: var(--paper);
}
.chat-msg a { color: var(--signal-soft); text-underline-offset: 0.2em; }

.chat-card {
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
  padding: 0.75rem;
  border: 1px solid var(--chat-line);
  border-radius: 1.25rem;
  background: var(--glass);
}
.chat-summary {
  width: 100%;
  box-sizing: border-box;
  min-height: 12rem;
  resize: vertical;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--chat-field-line);
  border-radius: 0.875rem;
  background: var(--chat-well);
  color: var(--paper);
  font: inherit;
  font-size: 0.9375rem;
  line-height: 1.45;
}
.chat-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; }
.chat-actions .chat-email { min-height: 2.75rem; }
.chat-note { margin: 0; min-height: 1.25em; font-size: 0.9375rem; color: var(--mist); }

/* suggestions wrap onto as many rows as they need: a chip is never cut at the panel's edge */
.chat-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0.25rem 1.125rem 0.625rem;
}
.chat-chip { flex: none; max-width: 100%; text-align: start; }
.chat-chips:empty { display: none; }

.chat-chip, .chat-copy, .chat-restart, .chat-send {
  min-height: 2.75rem;
  padding: 0.5rem 1rem;
  border-radius: var(--r-pill);
  border: 1px solid var(--chat-line);
  background: var(--glass);
  color: var(--paper);
  font: inherit;
  font-size: 0.9375rem;
  cursor: pointer;
  transition: background-color var(--dur-quick) var(--ease-return), border-color var(--dur-quick) var(--ease-return);
}
.chat-chip:hover, .chat-copy:hover, .chat-restart:hover { background: var(--glass-strong); }

.chat-form {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem 0.25rem 1.125rem;
  border-top: 1px solid var(--chat-rule);
}
.chat-input {
  flex: 1;
  min-width: 0;
  min-height: 2.75rem;
  max-height: 10rem;
  box-sizing: border-box;
  resize: none;
  padding: 0.625rem 0.875rem;
  border: 1px solid var(--chat-field-line);
  border-radius: 1.375rem;
  background: var(--chat-field);
  color: var(--paper);
  font: inherit;
  font-size: 1rem; /* 16px: no zoom on focus in iOS Safari */
  line-height: 1.4;
}
.chat-input::placeholder { color: var(--mist); opacity: 0.8; }
.chat-send {
  flex: none;
  min-width: 2.75rem;
  background: var(--signal);
  border-color: var(--signal);
  font-weight: 600;
}
.chat-send:hover { background: var(--indigo); border-color: var(--signal-soft); }
.chat-count {
  position: absolute;
  right: 5.5rem;
  top: -1.4rem;
  margin: 0;
  font-size: 0.8125rem;
  color: var(--mist);
}

.chat-skip {
  align-self: flex-start;
  min-height: 2.75rem;
  margin: 0 0 0.25rem 0.625rem;
  padding: 0 0.5rem;
  border: 0;
  background: none;
  color: var(--mist);
  font: inherit;
  font-size: 0.9375rem;
  text-decoration: underline;
  text-underline-offset: 0.2em;
  cursor: pointer;
}
.chat-skip:hover { color: var(--paper); }

.chat-status {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.chat :focus-visible {
  outline: 2px solid var(--lamp);
  outline-offset: 2px;
}

.chat-scrim {
  position: fixed;
  inset: 0;
  z-index: 999;
  background: var(--night);
  opacity: 0.55;
}

/* The docked bar (phones only): a solid-enough glass so it reads over paper sections too; it leaves the
   bottom-left 64px to the pause control and sits on the same baseline. Hidden = visibility, so it can't be
   focused or tapped while away. */
.pill.chat-dock {
  position: fixed;
  z-index: 40;
  left: calc(64px + env(safe-area-inset-left, 0px));
  right: max(1rem, env(safe-area-inset-right, 0px));
  bottom: max(0.875rem, env(safe-area-inset-bottom, 0px));
  display: flex;
  gap: 0.625rem;
  padding-inline: 1.125rem;
  --dock-bg: var(--night);
  --dock-hover: var(--indigo);
  color: var(--paper);
  background: var(--dock-bg);
  border-color: var(--glass-line);
  -webkit-backdrop-filter: blur(18px) saturate(1.2);
  backdrop-filter: blur(18px) saturate(1.2);
  visibility: hidden;
  opacity: 0;
  transform: translateY(0.75rem);
  transition: opacity var(--dur-settle) var(--ease-return), transform var(--dur-settle) var(--ease-return),
    visibility 0s linear var(--dur-settle);
}
.pill.chat-dock.is-docked { visibility: visible; opacity: 1; transform: none; transition-delay: 0s; }
.pill.chat-dock:hover { background: var(--dock-hover); }
.chat-dock :where(.chat-dock-text) { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-dock:focus-visible { outline: 2px solid var(--lamp); outline-offset: 2px; }
@media (min-width: 761px) {
  .pill.chat-dock { display: none; }
}

/* translucency where color-mix() exists; the plain tokens above paint where it doesn't */
@supports (color: color-mix(in srgb, red, blue)) {
  .chat {
    --chat-bg: color-mix(in srgb, var(--night) 90%, transparent);
    --chat-rule: color-mix(in srgb, var(--paper) 12%, transparent);
    --chat-field-line: color-mix(in srgb, var(--paper) 20%, transparent);
    --chat-field: color-mix(in srgb, var(--paper) 6%, transparent);
    --chat-well: color-mix(in srgb, var(--night) 55%, transparent);
  }
  .pill.chat-dock {
    --dock-bg: color-mix(in srgb, var(--night) 82%, transparent);
    --dock-hover: color-mix(in srgb, var(--indigo) 88%, transparent);
  }
}

@media (max-width: 760px) {
  .chat {
    left: 0;
    right: 0;
    bottom: var(--chat-kb, 0px);
    width: auto;
    height: auto;
    min-height: min(26rem, calc(var(--chat-vh, 100dvh) - 1rem));
    max-height: min(88dvh, calc(var(--chat-vh, 100dvh) - 0.75rem));
    border-radius: var(--r-card) var(--r-card) 0 0;
    border-bottom: 0;
    padding-bottom: env(safe-area-inset-bottom, 0px);
    transform-origin: 50% 100%;
    animation-name: chat-sheet;
  }
  @keyframes chat-sheet {
    from { transform: translateY(1.5rem); opacity: 0; }
    to { transform: none; opacity: 1; }
  }
  .chat-msg { max-width: 100%; }
  /* tighter chips so the eight topics take three rows, not four */
  .chat-chips { gap: 0.375rem; }
  .chat-chip { padding-inline: 0.8125rem; }
}

@media (prefers-reduced-motion: reduce) {
  .chat { animation: none; }
  .chat.is-thinking .chat-avatar, .chat.is-thinking .chat-dots { animation: none; }
  .chat-chip, .chat-copy, .chat-restart, .chat-send, .pill.chat-dock { transition: none; }
}
