:root {
  --neige-orange: #f27a2a;
  --neige-orange-dark: #d85f16;
  --neige-orange-soft: #fde2ce;
  --neige-cream: #fffaf2;
  --neige-cream-deep: #f8efe3;
  --neige-white: #ffffff;
  --neige-ink: #493a31;
  --neige-muted: #8d796a;
  --neige-border: #eadbc9;
  --neige-online: #42a77a;
  --neige-badge: #e94b58;
  --neige-shadow: 0 22px 60px rgb(91 65 44 / 14%), 0 7px 20px rgb(91 65 44 / 8%);
}

.neige-chat,
.neige-chat * { box-sizing: border-box; }

html.neige-chat-scroll-lock,
body.neige-chat-scroll-lock {
  overflow: hidden !important;
  overscroll-behavior: none;
}

.neige-chat button,
.neige-chat input { font: inherit; }

.neige-chat button { -webkit-tap-highlight-color: transparent; }
.neige-chat [hidden] { display: none !important; }

.neige-chat {
  position: fixed;
  z-index: 2147483000;
  right: max(22px, env(safe-area-inset-right));
  bottom: max(22px, env(safe-area-inset-bottom));
  color: var(--neige-ink);
  font-family: Arial, "Noto Sans TC", sans-serif;
}

@media (min-width: 768px) {
  .neige-chat[data-open="false"] {
    right: 33px;
    bottom: 124px;
  }
}

.neige-chat__launcher {
  position: relative;
  display: grid;
  place-items: center;
  width: 78px;
  height: 78px;
  padding: 10px;
  border: 1px solid var(--neige-border);
  border-radius: 25px;
  background: linear-gradient(145deg, var(--neige-white), var(--neige-cream));
  color: var(--neige-orange);
  box-shadow: var(--neige-shadow);
  cursor: pointer;
  transition: transform 180ms ease, box-shadow 180ms ease, border-color 180ms ease;
}

.neige-chat__launcher:hover {
  transform: translateY(-3px);
  border-color: #e8c8af;
  box-shadow: 0 26px 64px rgb(91 65 44 / 17%);
}
.neige-chat__launcher:focus-visible { outline: 4px solid rgb(242 122 42 / 20%); outline-offset: 4px; }
.neige-chat__launcher svg { width: 34px; height: 34px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.neige-chat__launcher span { margin-top: -7px; color: var(--neige-ink); font-size: 9px; font-weight: 800; letter-spacing: .13em; }
.neige-chat__launcher b {
  position: absolute;
  top: -9px;
  right: -7px;
  display: grid;
  place-items: center;
  min-width: 29px;
  height: 29px;
  padding: 0 6px;
  border: 3px solid var(--neige-cream);
  border-radius: 999px;
  background: var(--neige-badge);
  color: white;
  font-size: 12px;
  box-shadow: 0 5px 12px rgb(92 37 40 / 18%);
  z-index: 2;
}

.neige-chat[data-has-unread="true"] .neige-chat__launcher {
  animation: neige-launcher-unread 1.05s ease-in-out infinite;
  border-color: rgba(242, 122, 42, 0.46);
}

@keyframes neige-launcher-unread {
  0%, 100% {
    transform: translateY(0);
    box-shadow: var(--neige-shadow);
  }
  50% {
    transform: translateY(-3px);
    box-shadow: 0 24px 62px rgb(242 122 42 / 23%), 0 0 0 8px rgb(242 122 42 / 8%);
  }
}

.neige-chat__window {
  display: grid;
  grid-template-rows: auto minmax(270px, 1fr) auto;
  width: min(392px, calc(100vw - 28px));
  height: min(650px, calc(var(--neige-viewport-height, 100dvh) - 44px));
  overflow: hidden;
  border: 1px solid var(--neige-border);
  border-radius: 30px;
  background: var(--neige-cream);
  box-shadow: var(--neige-shadow);
  animation: neige-in 200ms ease-out both;
}

@keyframes neige-in { from { opacity: 0; transform: translateY(12px) scale(.985); } }

.neige-chat__header {
  display: grid;
  grid-template-columns: 54px 1fr auto;
  align-items: center;
  gap: 12px;
  min-height: 94px;
  padding: 16px 15px 16px 18px;
  border-bottom: 1px solid var(--neige-border);
  background: linear-gradient(145deg, #fffdf9, var(--neige-cream));
  color: var(--neige-ink);
}

.neige-chat__avatar {
  width: 54px;
  height: 54px;
  border: 3px solid var(--neige-orange-soft);
  border-radius: 18px;
  background: var(--neige-white);
  object-fit: cover;
}

.neige-chat__identity { display: grid; gap: 6px; min-width: 0; }
.neige-chat__identity strong { overflow: hidden; font-size: 17px; text-overflow: ellipsis; white-space: nowrap; }
.neige-chat__identity span { display: flex; align-items: center; gap: 7px; color: var(--neige-muted); font-size: 12px; }
.neige-chat__identity i { width: 9px; height: 9px; border-radius: 50%; background: var(--neige-online); box-shadow: 0 0 0 3px rgb(66 167 122 / 14%); }

.neige-chat__header-actions { display: flex; gap: 5px; }
.neige-chat__header-actions button {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid transparent;
  border-radius: 13px;
  background: var(--neige-cream-deep);
  color: var(--neige-muted);
  cursor: pointer;
}
.neige-chat__header-actions button:hover { border-color: var(--neige-border); background: var(--neige-white); color: var(--neige-orange-dark); }
.neige-chat__header-actions button:focus-visible { outline: 3px solid rgb(242 122 42 / 18%); outline-offset: 2px; }
.neige-chat .icon-minus { width: 17px; height: 2px; border-radius: 2px; background: currentColor; }
.neige-chat .icon-close { position: relative; width: 18px; height: 18px; }
.neige-chat .icon-close::before, .neige-chat .icon-close::after { content: ""; position: absolute; top: 8px; left: 0; width: 18px; height: 2px; border-radius: 2px; background: currentColor; }
.neige-chat .icon-close::before { transform: rotate(45deg); }
.neige-chat .icon-close::after { transform: rotate(-45deg); }

.neige-chat__messages {
  min-height: 0;
  overflow-y: auto;
  padding: 22px 18px;
  background: linear-gradient(180deg, #fbf4e9, #fdf8f1 48%, #fbf4e9);
  scrollbar-width: thin;
  scrollbar-color: #ddcdbc transparent;
}

.neige-chat__row {
  display: flex;
  align-items: flex-end;
  gap: 9px;
  margin-bottom: 15px;
  outline: none;
}
.neige-chat__row--user { justify-content: flex-end; }
.neige-chat__bubble-avatar { width: 33px; height: 33px; flex: 0 0 auto; border: 2px solid var(--neige-orange-soft); border-radius: 12px; object-fit: cover; background: var(--neige-white); }
.neige-chat__bubble-wrap { display: grid; gap: 5px; max-width: 78%; }
.neige-chat__bubble {
  max-width: 100%;
  margin: 0;
  padding: 11px 14px;
  border: 1px solid var(--neige-border);
  border-radius: 17px 17px 17px 6px;
  background: var(--neige-white);
  color: var(--neige-ink);
  font-size: 14px;
  line-height: 1.62;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  box-shadow: 0 6px 16px rgb(91 65 44 / 6%);
}
.neige-chat__row--user .neige-chat__bubble { border-color: #ee8f52; border-radius: 17px 17px 6px 17px; background: #f28a45; color: white; }
.neige-chat__row--user .neige-chat__bubble-wrap { justify-items: end; }
.neige-chat__row:focus .neige-chat__bubble { box-shadow: 0 0 0 3px rgb(242 122 42 / 13%); }

.neige-chat__image-gallery {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 5px;
  width: min(238px, 100%);
  padding: 5px;
  border: 1px solid var(--neige-border);
  border-radius: 18px;
  background: var(--neige-white);
  box-shadow: 0 6px 16px rgb(91 65 44 / 6%);
}
.neige-chat__image-gallery--single { grid-template-columns: 1fr; }
.neige-chat__message-image { display: block; width: 100%; aspect-ratio: 4 / 3; border-radius: 13px; background: var(--neige-cream-deep); object-fit: cover; }

.neige-chat__meta {
  display: flex;
  align-items: center;
  gap: 7px;
  min-height: 16px;
  padding: 0 4px;
  color: var(--neige-muted);
  font-size: 11px;
  line-height: 1.2;
}
.neige-chat__row--user .neige-chat__meta { justify-content: flex-end; }
.neige-chat__message-time {
  max-width: 0;
  overflow: hidden;
  opacity: 0;
  transform: translateY(-2px);
  white-space: nowrap;
  transition: opacity 150ms ease, max-width 180ms ease, transform 150ms ease;
}
.neige-chat__row:hover .neige-chat__message-time,
.neige-chat__row:focus .neige-chat__message-time,
.neige-chat__row:focus-within .neige-chat__message-time {
  max-width: 190px;
  opacity: 1;
  transform: translateY(0);
}
.neige-chat__row[data-status="failed"] .neige-chat__status { color: #bf3842; font-weight: 700; }
.neige-chat__retry {
  padding: 2px 7px;
  border: 0;
  border-radius: 999px;
  background: var(--neige-orange-soft);
  color: var(--neige-orange-dark);
  cursor: pointer;
  font-size: 11px;
  font-weight: 700;
}
.neige-chat__retry:focus-visible { outline: 2px solid var(--neige-orange); outline-offset: 2px; }

.neige-chat__typing-row { padding-left: 42px; }
.neige-chat__typing {
  display: flex;
  align-items: center;
  gap: 4px;
  width: fit-content;
  padding: 12px 15px;
  border: 1px solid var(--neige-border);
  border-radius: 17px 17px 17px 6px;
  background: var(--neige-white);
  box-shadow: 0 6px 16px rgb(91 65 44 / 6%);
}
.neige-chat__typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--neige-muted); animation: neige-typing 1.1s infinite ease-in-out; }
.neige-chat__typing i:nth-child(2) { animation-delay: 120ms; }
.neige-chat__typing i:nth-child(3) { animation-delay: 240ms; }
@keyframes neige-typing {
  0%, 60%, 100% { opacity: .35; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

.neige-chat__composer {
  min-height: 0;
  display: grid;
  gap: 9px;
  padding: 12px 13px 14px;
  border-top: 1px solid var(--neige-border);
  background: linear-gradient(180deg, #fffdf9, var(--neige-cream));
}
.neige-chat__composer-row {
  display: grid;
  grid-template-columns: 42px 1fr 47px;
  align-items: center;
  gap: 8px;
}
.neige-chat__message-input {
  width: 100%;
  height: 47px;
  border: 1px solid var(--neige-border);
  border-radius: 17px;
  padding: 0 15px;
  background: var(--neige-white);
  color: var(--neige-ink);
  outline: none;
}
.neige-chat__message-input::placeholder { color: #aa998d; }
.neige-chat__message-input:focus { border-color: #e5ad86; box-shadow: 0 0 0 3px rgb(242 122 42 / 11%); }
.neige-chat__attachment-button,
.neige-chat__send-button {
  display: grid;
  place-items: center;
  border: 0;
  cursor: pointer;
}
.neige-chat__attachment-button {
  width: 42px;
  height: 42px;
  border: 1px solid var(--neige-border);
  border-radius: 14px;
  background: var(--neige-cream-deep);
  color: var(--neige-orange-dark);
}
.neige-chat__attachment-button:hover { background: var(--neige-white); border-color: #e4b594; }
.neige-chat__send-button {
  width: 47px;
  height: 47px;
  border-radius: 16px;
  background: var(--neige-orange);
  color: white;
  box-shadow: 0 8px 17px rgb(216 95 22 / 18%);
}
.neige-chat__send-button:hover { background: var(--neige-orange-dark); }
.neige-chat__attachment-button:focus-visible,
.neige-chat__send-button:focus-visible { outline: 3px solid rgb(242 122 42 / 18%); outline-offset: 3px; }
.neige-chat__attachment-button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.neige-chat__send-button svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.neige-chat__attachment-previews { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 3px; }
.neige-chat__attachment-preview { position: relative; flex: 0 0 64px; width: 64px; height: 64px; margin: 0; }
.neige-chat__attachment-preview img { display: block; width: 64px; height: 64px; border: 2px solid var(--neige-white); border-radius: 16px; background: var(--neige-cream-deep); object-fit: cover; box-shadow: 0 3px 10px rgb(91 65 44 / 10%); }
.neige-chat__attachment-preview button {
  position: absolute;
  top: -5px;
  right: -5px;
  display: grid;
  place-items: center;
  width: 22px;
  height: 22px;
  padding: 0;
  border: 2px solid var(--neige-cream);
  border-radius: 50%;
  background: var(--neige-ink);
  color: white;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
}
.neige-chat__attachment-error { margin: -2px 4px 0; color: #b23a43; font-size: 11px; }

.neige-chat .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }

@media (max-width: 767px) {
  .neige-chat { right: max(14px, env(safe-area-inset-right)); bottom: max(14px, env(safe-area-inset-bottom)); }

  .neige-chat[data-open="true"] {
    top: var(--neige-viewport-top, 0px);
    right: auto;
    bottom: auto;
    left: var(--neige-viewport-left, 0px);
    width: var(--neige-viewport-width, 100vw);
    height: var(--neige-viewport-height, 100dvh);
  }

  .neige-chat__window {
    width: 100%;
    height: 100%;
    max-width: none;
    max-height: none;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .neige-chat__header {
    min-height: calc(76px + env(safe-area-inset-top));
    padding-top: calc(10px + env(safe-area-inset-top));
  }

  .neige-chat__composer {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
}

@media (prefers-reduced-motion: reduce) {
  .neige-chat__window,
  .neige-chat__launcher,
  .neige-chat[data-has-unread="true"] .neige-chat__launcher,
  .neige-chat__typing i,
  .neige-chat__message-time { animation: none; transition: none; }
}
