/*
 * Ticket Compare AI concierge — mobile-first stylesheet.
 *
 * Layout strategy:
 *   - Phones (<600px): icon-only launcher, full-screen panel slides up
 *     from the bottom like a native sheet, drag-handle for swipe-down close,
 *     visualViewport-aware sizing so the keyboard doesn't cover the input.
 *   - Tablet portrait (600–900px): same sheet treatment, max-width 480px,
 *     centred horizontally.
 *   - Tablet landscape / desktop (>=900px): floating widget anchored
 *     bottom-right, 400×640.
 *
 * Tokens are scoped to .tc-chat-* so we cannot accidentally bleed into the
 * theme. Colors use CSS custom properties so the theme can override per brand.
 */

:root {
  --tc-chat-primary: #102f3f;
  --tc-chat-primary-dark: #0a2230;
  --tc-chat-accent: #fc904d;
  --tc-chat-accent-dark: #e87d36;
  --tc-chat-bg: #ffffff;
  --tc-chat-surface: #f4f6f8;
  --tc-chat-surface-2: #eaf0f4;
  --tc-chat-border: #dfe5ea;
  --tc-chat-text: #102f3f;
  --tc-chat-muted: #4f6373;
  --tc-chat-user-bg: var(--tc-chat-primary);
  --tc-chat-user-fg: #ffffff;
  --tc-chat-shadow: 0 24px 60px rgba(16, 47, 63, 0.28);
  --tc-chat-radius-lg: 22px;
  --tc-chat-radius-md: 14px;
  --tc-chat-z: 99990;
  --tc-chat-tap: 44px;
  --tc-vv-height: 100dvh;
}

#tc-chat-root[hidden] { display: none !important; }

#tc-chat-root {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: var(--tc-chat-z);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--tc-chat-text);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ----------------------------------------------------------
 * Launcher — circular icon-only button (mobile-first default)
 * ---------------------------------------------------------- */
.tc-chat-launcher {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 60px;
  height: 60px;
  padding: 0;
  background: var(--tc-chat-primary);
  color: #fff;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  box-shadow: 0 12px 28px rgba(16, 47, 63, 0.32), 0 4px 10px rgba(16, 47, 63, 0.18);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
              background 0.18s ease,
              box-shadow 0.18s ease;
  -webkit-tap-highlight-color: transparent;
}
.tc-chat-launcher:hover {
  background: var(--tc-chat-primary-dark);
  transform: translateY(-2px);
  box-shadow: 0 16px 36px rgba(16, 47, 63, 0.42);
}
.tc-chat-launcher:active { transform: scale(0.94); }
.tc-chat-launcher:focus-visible {
  outline: 3px solid var(--tc-chat-accent);
  outline-offset: 3px;
}
.tc-chat-launcher__icon-open,
.tc-chat-launcher__icon-close {
  position: absolute;
  transition: opacity 0.18s ease, transform 0.22s cubic-bezier(0.4, 0, 0.2, 1);
}
.tc-chat-launcher__icon-close {
  opacity: 0;
  transform: rotate(-45deg) scale(0.6);
}
[data-tc-chat-open="true"] .tc-chat-launcher__icon-open {
  opacity: 0;
  transform: rotate(45deg) scale(0.6);
}
[data-tc-chat-open="true"] .tc-chat-launcher__icon-close {
  opacity: 1;
  transform: none;
}
.tc-chat-launcher__sr {
  position: absolute;
  width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0;
}
/* Subtle pulse to draw attention on first paint. JS removes after open. */
.tc-chat-launcher__badge {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 12px;
  height: 12px;
  background: var(--tc-chat-accent);
  border: 2px solid #fff;
  border-radius: 50%;
  box-shadow: 0 0 0 0 rgba(252, 144, 77, 0.6);
  animation: tc-pulse 2.2s ease-out infinite;
}
[data-tc-chat-open="true"] .tc-chat-launcher__badge { display: none; }
@keyframes tc-pulse {
  0%   { box-shadow: 0 0 0 0 rgba(252, 144, 77, 0.55); }
  70%  { box-shadow: 0 0 0 10px rgba(252, 144, 77, 0); }
  100% { box-shadow: 0 0 0 0 rgba(252, 144, 77, 0); }
}

/* On hide-when-open we use the same button to close, no separate close needed */
[data-tc-chat-open="true"] .tc-chat-launcher {
  background: var(--tc-chat-primary-dark);
}

/* ----------------------------------------------------------
 * Panel — bottom-sheet on mobile, floating widget on desktop
 * Mobile is the DEFAULT; desktop is an enhancement.
 * ---------------------------------------------------------- */
.tc-chat-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* Use visualViewport height when JS sets it, otherwise dvh fallback */
  height: var(--tc-vv-height, 100dvh);
  max-height: 100dvh;
  background: var(--tc-chat-bg);
  border-top-left-radius: var(--tc-chat-radius-lg);
  border-top-right-radius: var(--tc-chat-radius-lg);
  box-shadow: var(--tc-chat-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transform: translateY(100%);
  opacity: 1;
  pointer-events: none;
  transition: transform 0.32s cubic-bezier(0.32, 0.72, 0, 1);
  will-change: transform;
}
[data-tc-chat-open="true"] .tc-chat-panel {
  transform: translateY(0);
  pointer-events: auto;
}

/* Drag-handle (mobile only) — visual + a real touch target */
.tc-chat-panel__handle {
  position: relative;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  cursor: grab;
  touch-action: none;
}
.tc-chat-panel__handle::before {
  content: "";
  width: 40px;
  height: 4px;
  background: #d2d8de;
  border-radius: 4px;
}

/* Header */
.tc-chat-panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 12px 14px;
  background: #fff;
  flex: 0 0 auto;
  cursor: grab;
  touch-action: none;
}
.tc-chat-panel__title {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.tc-chat-avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--tc-chat-primary), var(--tc-chat-primary-dark));
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  box-shadow: 0 4px 10px rgba(16, 47, 63, 0.22);
}
.tc-chat-panel__title-text { min-width: 0; }
.tc-chat-panel__title-text strong {
  display: block;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.2;
  color: var(--tc-chat-text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.tc-chat-panel__title-text small {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--tc-chat-muted);
  margin-top: 3px;
}
.tc-chat-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #25c26a;
  box-shadow: 0 0 0 3px rgba(37, 194, 106, 0.18);
  flex: 0 0 auto;
}
.tc-chat-panel__close {
  width: var(--tc-chat-tap);
  height: var(--tc-chat-tap);
  background: var(--tc-chat-surface);
  border: 0;
  border-radius: 50%;
  color: var(--tc-chat-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 auto;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, transform 0.15s ease;
}
.tc-chat-panel__close:hover { background: var(--tc-chat-surface-2); }
.tc-chat-panel__close:active { transform: scale(0.92); }
.tc-chat-panel__close:focus-visible {
  outline: 2px solid var(--tc-chat-primary);
  outline-offset: 2px;
}

/* Body — message list */
.tc-chat-panel__body {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
  background: linear-gradient(180deg, #ffffff 0%, #fbfcfd 100%);
}

/* Message bubbles */
.tc-msg {
  max-width: 84%;
  padding: 11px 15px;
  border-radius: 20px;
  font-size: 15px;
  line-height: 1.5;
  word-wrap: break-word;
  animation: tc-msg-in 0.22s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}
@keyframes tc-msg-in {
  from { opacity: 0; transform: translateY(8px) scale(0.96); }
  to   { opacity: 1; transform: none; }
}
.tc-msg--user {
  align-self: flex-end;
  background: var(--tc-chat-user-bg);
  color: var(--tc-chat-user-fg);
  border-bottom-right-radius: 6px;
  box-shadow: 0 2px 8px rgba(16, 47, 63, 0.14);
}
.tc-msg--assistant {
  align-self: flex-start;
  background: var(--tc-chat-surface);
  color: var(--tc-chat-text);
  border-bottom-left-radius: 6px;
}
.tc-msg--error {
  align-self: flex-start;
  background: #fff1f1;
  color: #b21f24;
  border: 1px solid #fbd0d3;
}
.tc-msg__caret::after {
  content: "▍";
  margin-left: 1px;
  animation: tc-blink 1s steps(2) infinite;
  color: var(--tc-chat-muted);
}
@keyframes tc-blink { 50% { opacity: 0; } }

.tc-typing {
  align-self: flex-start;
  background: var(--tc-chat-surface);
  border-radius: 18px;
  padding: 12px 16px;
  display: inline-flex;
  gap: 5px;
}
.tc-typing span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--tc-chat-muted);
  animation: tc-bounce 1.2s infinite ease-in-out;
}
.tc-typing span:nth-child(2) { animation-delay: 0.15s; }
.tc-typing span:nth-child(3) { animation-delay: 0.3s; }
@keyframes tc-bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40%           { transform: scale(1);   opacity: 1; }
}

/* Cards */
.tc-cards {
  align-self: stretch;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 10px;
  margin: 4px 0;
  width: 100%;
  min-width: 0;
}
.tc-card {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  padding: 14px;
  border: 1px solid var(--tc-chat-border);
  border-radius: var(--tc-chat-radius-md);
  background: #fff;
  text-decoration: none;
  color: inherit;
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  transition: border-color 0.15s ease, transform 0.12s ease, box-shadow 0.18s ease;
}
/* Entrance animation for result cards.
 *
 * Cards are held back until the reply text finishes streaming, then dealt
 * in with a per-card stagger (set inline by chatbot.js). `both` retains the
 * from-state during the stagger delay, so a card is invisible until its
 * turn — without it every card would flash at full opacity first.
 *
 * Deliberately a different curve to `tc-msg-in`: messages get a springy
 * overshoot, cards settle without bounce so a list of them stays calm.
 */
.tc-card--enter {
  animation: tc-card-in 0.28s cubic-bezier(0.22, 0.61, 0.36, 1) both;
}
@keyframes tc-card-in {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: none; }
}

.tc-card:hover {
  border-color: var(--tc-chat-accent);
  box-shadow: 0 8px 22px rgba(252, 144, 77, 0.18);
}
.tc-card:active { transform: scale(0.985); }
.tc-card__main {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
}
.tc-card__event {
  font-weight: 700;
  font-size: 15px;
  color: var(--tc-chat-text);
  line-height: 1.3;
  /* Allow up to 2 lines on mobile so long event names aren't lost */
  display: -webkit-box;
  -webkit-line-clamp: 2;
          line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tc-card__meta {
  font-size: 13px;
  color: var(--tc-chat-muted);
  margin-top: 5px;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
          line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.tc-card__price {
  flex: 0 0 auto;
  font-weight: 800;
  font-size: 16px;
  color: var(--tc-chat-accent-dark);
  text-align: right;
  min-width: 60px;
  line-height: 1.1;
  padding-top: 2px;
}
.tc-card__price small {
  display: block;
  font-weight: 700;
  font-size: 10px;
  color: var(--tc-chat-muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  margin-bottom: 3px;
}
.tc-card__price--soldout {
  color: var(--tc-chat-muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding-top: 4px;
}

/* Suggestions */
.tc-chat-panel__suggestions {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  overflow-x: auto;
  scrollbar-width: none;
  border-top: 1px solid var(--tc-chat-border);
  background: #fff;
  flex: 0 0 auto;
  /* Mask edges to hint at horizontal scroll */
  -webkit-mask-image: linear-gradient(to right, transparent 0, #000 16px, #000 calc(100% - 16px), transparent 100%);
          mask-image: linear-gradient(to right, transparent 0, #000 16px, #000 calc(100% - 16px), transparent 100%);
}
.tc-chat-panel__suggestions::-webkit-scrollbar { display: none; }
.tc-chip {
  flex: 0 0 auto;
  min-height: 36px;
  max-width: 320px;
  padding: 8px 14px;
  background: #fff;
  border: 1px solid var(--tc-chat-border);
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  color: var(--tc-chat-text);
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.12s ease;
}
.tc-chip:hover {
  background: var(--tc-chat-primary);
  border-color: var(--tc-chat-primary);
  color: #fff;
}
.tc-chip:active { transform: scale(0.96); }
.tc-chat-panel__suggestions[hidden] { display: none; }

/* Form */
.tc-chat-panel__form {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--tc-chat-border);
  background: #fff;
  align-items: center;
  flex: 0 0 auto;
}
.tc-chat-panel__suggestions:not([hidden]) + .tc-chat-panel__form {
  border-top: 0;
}
/* Wrap holds the input pill and the optional mic button so they look like
   one combined control. Mic sits inside the pill on the left; input takes
   the remaining width. */
.tc-chat-input-wrap {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  align-items: center;
  height: 48px;
  padding: 0 6px 0 6px;
  border: 1px solid var(--tc-chat-border);
  border-radius: 999px;
  background: var(--tc-chat-surface);
  transition: border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}
.tc-chat-input-wrap:focus-within {
  border-color: var(--tc-chat-primary);
  background: #fff;
  box-shadow: 0 0 0 3px rgba(16, 47, 63, 0.10);
}
.tc-chat-input {
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
  padding: 0 14px;
  border: 0;
  background: transparent;
  /* 16px+ prevents iOS from auto-zooming on focus */
  font-size: 16px;
  color: var(--tc-chat-text);
  outline: none;
  -webkit-appearance: none;
  appearance: none;
}
.tc-chat-input::placeholder { color: var(--tc-chat-muted); }

/* Mic button sits inside the input pill on the left. Hidden via the
   [hidden] attribute when SpeechRecognition isn't supported (Firefox etc.)
   so the layout collapses cleanly. */
.tc-chat-mic {
  position: relative;
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 0;
  border-radius: 50%;
  color: var(--tc-chat-muted);
  cursor: pointer;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
  transition: color 0.15s ease, background 0.15s ease, transform 0.15s ease;
}
.tc-chat-mic:hover { color: var(--tc-chat-primary); background: var(--tc-chat-surface-2); }
.tc-chat-mic:active { transform: scale(0.92); }
.tc-chat-mic:focus-visible {
  outline: 2px solid var(--tc-chat-primary);
  outline-offset: 2px;
}
.tc-chat-mic[hidden] { display: none; }

.tc-chat-mic__icon-listening { display: none; }
.tc-chat-mic--listening {
  color: #fff;
  background: var(--tc-chat-accent);
  animation: tc-mic-pulse 1.2s ease-in-out infinite;
}
.tc-chat-mic--listening:hover { background: var(--tc-chat-accent-dark); color: #fff; }
.tc-chat-mic--listening .tc-chat-mic__icon-idle { display: none; }
.tc-chat-mic--listening .tc-chat-mic__icon-listening { display: block; }

@keyframes tc-mic-pulse {
  0%   { box-shadow: 0 0 0 0 rgba(252, 144, 77, 0.55); }
  70%  { box-shadow: 0 0 0 12px rgba(252, 144, 77, 0); }
  100% { box-shadow: 0 0 0 0 rgba(252, 144, 77, 0); }
}
.tc-chat-send {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--tc-chat-primary);
  color: #fff;
  border: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: 0 0 auto;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.15s ease, transform 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 4px 12px rgba(16, 47, 63, 0.22);
}
.tc-chat-send:hover { background: var(--tc-chat-primary-dark); }
.tc-chat-send:active { transform: scale(0.92); }
.tc-chat-send:disabled {
  background: #b8c3d6;
  cursor: not-allowed;
  box-shadow: none;
  transform: none;
}
.tc-chat-send svg { transform: translate(-1px, 1px); }

/* ----------------------------------------------------------
 * Tablet portrait (600–900px) — bottom sheet but centred
 * ---------------------------------------------------------- */
@media (min-width: 600px) {
  .tc-chat-panel {
    left: 50%;
    right: auto;
    transform: translate(-50%, 100%);
    width: min(480px, calc(100vw - 48px));
    height: min(700px, calc(100dvh - 32px));
    border-radius: var(--tc-chat-radius-lg);
    bottom: 24px;
  }
  [data-tc-chat-open="true"] .tc-chat-panel {
    transform: translate(-50%, 0);
  }
  .tc-chat-panel__handle { display: none; }
  .tc-chat-panel__head { cursor: default; padding: 16px 18px; }
}

/* ----------------------------------------------------------
 * Desktop / tablet landscape (>=900px) — floating widget
 * ---------------------------------------------------------- */
@media (min-width: 900px) {
  #tc-chat-root { right: 24px; bottom: 24px; }
  .tc-chat-panel {
    position: absolute;
    left: auto;
    right: 0;
    bottom: 0;
    width: 400px;
    height: 640px;
    max-height: calc(100dvh - 48px);
    transform: translateY(12px) scale(0.98);
    opacity: 0;
    transition: transform 0.24s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.18s ease;
  }
  [data-tc-chat-open="true"] .tc-chat-panel {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
}

/* ----------------------------------------------------------
 * Reduced motion — respect user preference
 * ---------------------------------------------------------- */
@media (prefers-reduced-motion: reduce) {
  .tc-chat-launcher,
  .tc-chat-panel,
  .tc-msg,
  .tc-card,
  .tc-chip,
  .tc-chat-send {
    transition: none !important;
    animation: none !important;
  }
  .tc-chat-launcher__badge { animation: none !important; }
  .tc-chat-mic--listening { animation: none !important; }
  /* The entrance animation uses `both`, which holds the from-state
   * (opacity: 0) during its stagger delay. Cancelling the animation alone
   * would leave cards permanently invisible, so restore the end state
   * explicitly. Cards still appear after the text — only the motion goes. */
  .tc-card--enter {
    animation: none !important;
    opacity: 1 !important;
    transform: none !important;
  }
}
