/* Live chat (public/zenpn/chat.js): nút nổi góc phải dưới + khung chat. Màu lấy từ --zc-*
 * nên tự theo sáng/tối. Hiện/ẩn đều bằng transition trên cùng thuộc tính nên vào và ra
 * đối xứng; visibility trễ đúng một nhịp để phần đang ẩn không nhận chuột hay focus.
 * --zlc-lift đẩy cả cụm lên khi góc dưới đã có thứ khác (nút lên đầu trang ở màn hẹp). */

.zc-livechat {
  --zlc-gap: 20px;
  --zlc-size: 56px;
  --zlc-lift: 0px;
  --zlc-dur: var(--zm-base, 250ms);
  --zlc-fast: var(--zm-fast, 150ms);
  --zlc-ease: var(--zm-ease, cubic-bezier(.2, .8, .2, 1));
  --zlc-accent: var(--zc-primary, #165dff);
  --zlc-bottom: calc(var(--zlc-gap) + env(safe-area-inset-bottom) + var(--zlc-lift));
  font-family: var(--zc-font, Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif);
}

.zc-livechat svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.zc-livechat button { font: inherit; -webkit-tap-highlight-color: transparent; }

/* ── Nút nổi ── */
.zc-livechat-launcher {
  position: fixed;
  right: var(--zlc-gap);
  bottom: var(--zlc-bottom);
  z-index: 990;
  display: grid;
  place-items: center;
  width: var(--zlc-size);
  height: var(--zlc-size);
  padding: 0;
  color: #fff;
  background: var(--zlc-accent);
  border: 0;
  border-radius: 50%;
  box-shadow: 0 10px 26px color-mix(in srgb, var(--zlc-accent) 38%, transparent), 0 2px 6px rgba(0, 0, 0, .18);
  cursor: pointer;
  opacity: 0;
  visibility: hidden;
  transform: translateY(16px) scale(.6);
  transition: opacity var(--zlc-dur) var(--zlc-ease), transform var(--zlc-dur) var(--zlc-ease),
    bottom var(--zlc-dur) var(--zlc-ease), box-shadow var(--zlc-fast) var(--zlc-ease), visibility 0s linear var(--zlc-dur);
}
.zc-livechat[data-shown] .zc-livechat-launcher {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition-delay: 0s;
}
.zc-livechat[data-shown] .zc-livechat-launcher:hover { transform: translateY(-2px); }
.zc-livechat[data-shown] .zc-livechat-launcher:active { transform: scale(.94); }
.zc-livechat-launcher:focus-visible { outline: 2px solid var(--zc-blue, #4580ff); outline-offset: 3px; }

.zc-livechat-launcher-chat,
.zc-livechat-launcher-close {
  grid-area: 1 / 1;
  width: 26px;
  height: 26px;
  transition: opacity var(--zlc-dur) var(--zlc-ease), transform var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-launcher-close { opacity: 0; transform: rotate(-90deg) scale(.6); }
.zc-livechat[data-open] .zc-livechat-launcher-chat { opacity: 0; transform: rotate(90deg) scale(.6); }
.zc-livechat[data-open] .zc-livechat-launcher-close { opacity: 1; transform: none; }

.zc-livechat-badge {
  position: absolute;
  top: -3px;
  right: -3px;
  min-width: 20px;
  height: 20px;
  padding: 0 5px;
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  line-height: 16px;
  text-align: center;
  font-variant-numeric: tabular-nums;
  background: var(--zc-danger, #e5484d);
  border: 2px solid var(--zc-bg, #1b1e28);
  border-radius: 10px;
  opacity: 0;
  transform: scale(0);
  transition: opacity var(--zlc-fast) var(--zlc-ease), transform var(--zlc-dur) cubic-bezier(.34, 1.56, .64, 1);
}
.zc-livechat-badge[data-shown] { opacity: 1; transform: none; }

/* ── Khung chat ── */
.zc-livechat-panel {
  position: fixed;
  right: var(--zlc-gap);
  bottom: calc(var(--zlc-bottom) + var(--zlc-size) + 12px);
  z-index: 999;
  display: flex;
  flex-direction: column;
  width: min(380px, calc(100vw - 2 * var(--zlc-gap)));
  height: min(600px, calc(100vh - var(--zlc-size) - 2 * var(--zlc-gap) - var(--zlc-lift) - 28px));
  height: min(600px, calc(100dvh - var(--zlc-size) - 2 * var(--zlc-gap) - var(--zlc-lift) - 28px));
  overflow: hidden;
  color: var(--zc-text);
  background: var(--zc-card);
  border: 1px solid var(--zc-line);
  border-radius: 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .28), 0 4px 14px rgba(0, 0, 0, .12);
  transform-origin: calc(100% - var(--zlc-size) / 2) 100%;
  opacity: 0;
  visibility: hidden;
  transform: translateY(12px) scale(.96);
  transition: opacity var(--zlc-dur) var(--zlc-ease), transform var(--zlc-dur) var(--zlc-ease),
    bottom var(--zlc-dur) var(--zlc-ease), visibility 0s linear var(--zlc-dur);
}
.zc-livechat-panel:focus { outline: 0; }
.zc-livechat[data-open] .zc-livechat-panel {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition-delay: 0s;
}

.zc-livechat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 12px 14px 16px;
  border-bottom: 1px solid var(--zc-line);
}
.zc-livechat-avatar {
  display: grid;
  flex-shrink: 0;
  place-items: center;
  width: 40px;
  height: 40px;
  color: var(--zlc-accent);
  background: color-mix(in srgb, var(--zlc-accent) 14%, transparent);
  border-radius: 50%;
}
.zc-livechat-avatar-icon { width: 22px; height: 22px; }
.zc-livechat-titles { flex: 1; min-width: 0; }
.zc-livechat-title {
  margin: 0;
  overflow: hidden;
  color: var(--zc-heading, var(--zc-text));
  font-size: 15px;
  font-weight: 700;
  line-height: 1.3;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.zc-livechat-status {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 2px 0 0;
  color: var(--zc-muted);
  font-size: 12.5px;
  line-height: 1.35;
}
.zc-livechat-dot {
  position: relative;
  flex-shrink: 0;
  width: 8px;
  height: 8px;
  background: var(--zc-orange, #ee9b29);
  border-radius: 50%;
  transition: background-color var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-status[data-online="true"] .zc-livechat-dot { background: var(--zc-green, #26bd68); }
.zc-livechat-status[data-online="true"] .zc-livechat-dot::after {
  position: absolute;
  inset: 0;
  content: "";
  border-radius: 50%;
  box-shadow: 0 0 0 0 var(--zc-green, #26bd68);
  animation: zc-livechat-pulse 2.4s ease-out infinite;
}
.zc-livechat-states { display: grid; min-width: 0; }
.zc-livechat-on,
.zc-livechat-off {
  grid-area: 1 / 1;
  min-width: 0;
  transition: opacity var(--zlc-dur) var(--zlc-ease), transform var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-status[data-online="true"] .zc-livechat-off,
.zc-livechat-status:not([data-online="true"]) .zc-livechat-on { opacity: 0; transform: translateY(4px); }

.zc-livechat-close {
  display: grid;
  flex-shrink: 0;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  color: var(--zc-muted);
  background: transparent;
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  transition: background-color var(--zlc-fast) var(--zlc-ease), color var(--zlc-fast) var(--zlc-ease);
}
.zc-livechat-close:hover { color: var(--zc-text); background: var(--zc-hover); }
.zc-livechat-close:focus-visible { outline: 2px solid var(--zc-blue, #4580ff); outline-offset: -2px; }
.zc-livechat-close-icon { width: 18px; height: 18px; }

.zc-livechat-body {
  position: relative;
  display: grid;
  flex: 1;
  min-height: 0;
}
.zc-livechat-log,
.zc-livechat-skeleton { grid-area: 1 / 1; min-height: 0; }

.zc-livechat-log {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  transition: opacity var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-body[data-loading] .zc-livechat-log { opacity: 0; }

.zc-livechat-skeleton {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--zlc-dur) var(--zlc-ease), visibility 0s linear var(--zlc-dur);
}
.zc-livechat-body[data-loading] .zc-livechat-skeleton { opacity: 1; visibility: visible; transition-delay: 0s; }
.zc-livechat-bone {
  width: 64%;
  height: 38px;
  background: linear-gradient(90deg, var(--zc-raised) 0, var(--zc-hover) 50%, var(--zc-raised) 100%) 0 0 / 200% 100%;
  border-radius: 16px 16px 16px 6px;
  animation: zc-livechat-shimmer 1.4s linear infinite;
}
.zc-livechat-bone:nth-child(2) { align-self: flex-end; width: 46%; border-radius: 16px 16px 6px 16px; }
.zc-livechat-bone:nth-child(3) { width: 72%; height: 56px; }

.zc-livechat-empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  padding: 24px 32px;
  color: var(--zc-muted);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transform: translateY(8px);
  transition: opacity var(--zlc-dur) var(--zlc-ease), transform var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-empty[data-shown] { opacity: 1; transform: none; }
.zc-livechat-empty-icon {
  width: 52px;
  height: 52px;
  padding: 13px;
  color: var(--zlc-accent);
  background: color-mix(in srgb, var(--zlc-accent) 12%, transparent);
  border-radius: 50%;
}
.zc-livechat-empty-text { max-width: 240px; margin: 0; font-size: 13.5px; line-height: 1.55; }

/* ── Tin nhắn ── */
.zc-livechat-msg {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  max-width: 84%;
  min-width: 0;
  align-self: flex-start;
}
.zc-livechat-msg[data-from="user"] { align-self: flex-end; align-items: flex-end; }
.zc-livechat-msg[data-from="system"] { align-self: center; align-items: center; max-width: 92%; }

.zc-livechat-text {
  margin: 0;
  padding: 9px 13px;
  color: var(--zc-text);
  font-size: 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  background: var(--zc-raised);
  border-radius: 16px 16px 16px 6px;
  transition: opacity var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-msg[data-from="user"] .zc-livechat-text { color: #fff; background: var(--zlc-accent); border-radius: 16px 16px 6px 16px; }
.zc-livechat-msg[data-from="system"] .zc-livechat-text {
  padding: 5px 12px;
  color: var(--zc-muted);
  font-size: 12.5px;
  text-align: center;
  background: color-mix(in srgb, var(--zc-muted) 12%, transparent);
  border-radius: 10px;
}
.zc-livechat-time {
  max-height: 16px;
  padding: 0 4px;
  overflow: hidden;
  color: var(--zc-muted);
  font-size: 11px;
  line-height: 16px;
  font-variant-numeric: tabular-nums;
  opacity: .85;
  transition: max-height var(--zlc-dur) var(--zlc-ease), opacity var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-msg { transition: margin-bottom var(--zlc-dur) var(--zlc-ease); }
.zc-livechat-msg[data-joined] { margin-bottom: -7px; }
.zc-livechat-msg[data-joined] .zc-livechat-time { max-height: 0; opacity: 0; }

.zc-livechat-msg[data-new] { animation: zc-livechat-in-left var(--zlc-dur) var(--zlc-ease) backwards; }
.zc-livechat-msg[data-new][data-from="user"] { animation-name: zc-livechat-in-right; }
.zc-livechat-msg[data-new][data-from="system"] { animation-name: zc-livechat-in-up; }
.zc-livechat-msg[data-pending] .zc-livechat-text { opacity: .6; }
.zc-livechat-msg[data-leaving] { animation: zc-livechat-out-right var(--zlc-dur) var(--zlc-ease) forwards; }

/* ── Ô nhập ── */
.zc-livechat-form {
  padding: 10px 12px 12px;
  border-top: 1px solid var(--zc-line);
}
.zc-livechat-error {
  max-height: 0;
  margin: 0;
  overflow: hidden;
  color: var(--zc-danger, #e5484d);
  font-size: 12.5px;
  line-height: 1.45;
  opacity: 0;
  transition: max-height var(--zlc-dur) var(--zlc-ease), opacity var(--zlc-dur) var(--zlc-ease), margin var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-error[data-shown] { max-height: 80px; margin-bottom: 8px; opacity: 1; }
.zc-livechat-form[data-wait] .zc-livechat-error { color: var(--zc-orange, #ee9b29); }

/* /block trong topic: dòng xám thay chỗ ô nhập, ô nhập và nút gửi khoá. */
.zc-livechat-blocked {
  max-height: 0;
  margin: 0;
  overflow: hidden;
  color: var(--zc-muted);
  font-size: 12.5px;
  line-height: 1.45;
  opacity: 0;
  transform: translateY(4px);
  transition: max-height var(--zlc-dur) var(--zlc-ease), opacity var(--zlc-dur) var(--zlc-ease),
    margin var(--zlc-dur) var(--zlc-ease), transform var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-blocked[data-shown] { max-height: 80px; margin-bottom: 8px; opacity: 1; transform: none; }
.zc-livechat-form[data-blocked] .zc-livechat-compose { opacity: .6; }
.zc-livechat-form[data-blocked] .zc-livechat-compose::after { opacity: 0; }
.zc-livechat-input:disabled { cursor: not-allowed; }

.zc-livechat-compose {
  position: relative;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 5px 5px 5px 12px;
  background: var(--zc-input-bg, var(--zc-raised));
  border: 1px solid var(--zc-input-border, var(--zc-line));
  border-radius: 14px;
  transition: border-color var(--zlc-fast) var(--zlc-ease), box-shadow var(--zlc-fast) var(--zlc-ease),
    opacity var(--zlc-dur) var(--zlc-ease);
}
/* Backend trả 429: vạch dưới ô nhập cạn dần đúng số giây phải chờ (--zlc-wait do chat.js đặt). */
.zc-livechat-compose::after {
  position: absolute;
  right: 14px;
  bottom: -1px;
  left: 14px;
  height: 2px;
  content: "";
  background: var(--zc-orange, #ee9b29);
  border-radius: 2px;
  opacity: 0;
  transform: scaleX(0);
  transform-origin: left center;
  transition: opacity var(--zlc-dur) var(--zlc-ease);
}
.zc-livechat-form[data-wait] .zc-livechat-compose::after {
  opacity: 1;
  animation: zc-livechat-drain var(--zlc-wait, 3s) linear forwards;
}
.zc-livechat-compose:focus-within {
  border-color: var(--zlc-accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--zlc-accent) 18%, transparent);
}
.zc-livechat-input {
  flex: 1;
  min-width: 0;
  min-height: 36px;
  max-height: 128px;
  padding: 7px 0;
  margin: 0;
  color: var(--zc-text);
  font: inherit;
  font-size: 14px;
  line-height: 1.55;
  background: transparent;
  border: 0;
  outline: 0;
  resize: none;
  field-sizing: content;
}
.zc-livechat-input::placeholder { color: var(--zc-muted); opacity: 1; }
.zc-livechat-send {
  display: grid;
  flex-shrink: 0;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  color: #fff;
  background: var(--zlc-accent);
  border: 0;
  border-radius: 10px;
  cursor: pointer;
  transition: opacity var(--zlc-fast) var(--zlc-ease), transform var(--zlc-fast) var(--zlc-ease), filter var(--zlc-fast) var(--zlc-ease);
}
.zc-livechat-send:hover:not(:disabled) { filter: brightness(1.1); }
.zc-livechat-send:active:not(:disabled) { transform: scale(.94); }
.zc-livechat-send:disabled { cursor: default; opacity: .55; }
.zc-livechat-send:focus-visible { outline: 2px solid var(--zc-blue, #4580ff); outline-offset: 2px; }
.zc-livechat-send-icon { width: 18px; height: 18px; transition: transform var(--zlc-dur) var(--zlc-ease); }
.zc-livechat-form[data-busy] .zc-livechat-send-icon { transform: translate(2px, -2px); }

/* Trang chi tiết ticket đã là khung hội thoại hỗ trợ toàn màn hình, góc phải dưới là tin
 * mới nhất và ô nhập của nó: nút nổi lui đi (khung đang mở thì vẫn giữ để đóng được). */
body:has(.zc-chat) .zc-livechat[data-shown]:not([data-open]) .zc-livechat-launcher {
  opacity: 0;
  visibility: hidden;
  transform: translateY(16px) scale(.6);
  transition-delay: 0s, 0s, 0s, 0s, var(--zlc-dur);
}

/* html/body cao cố định bằng màn hình nên padding của body không kéo dài trang. Đệm là phần tử
 * đứng sau nội dung #root (nội dung tràn khỏi #root, ::after đứng sau chỗ tràn). */
#root::after {
  content: "";
  display: block;
  height: 0;
  transition: height var(--zm-base, 250ms) var(--zm-ease, ease);
}

@media (max-width: 600px) {
  .zc-livechat { --zlc-gap: 16px; --zlc-size: 52px; }
  /* Chừa chỗ dưới trang bằng chiều cao nút chat + khe, để cuộn hết cỡ thì nội dung lọt lên trên nút. */
  body:has(.zc-livechat[data-shown]):not(:has(.zc-chat)) #root::after { height: calc(80px + env(safe-area-inset-bottom)); }
  body:has(.zc-to-top[data-visible]):has(.zc-livechat[data-shown]) #root::after { height: calc(136px + env(safe-area-inset-bottom)); }
  /* Nút lên đầu trang của trang / nằm đúng góc này ở màn hẹp: đẩy cụm chat lên trên nó. */
  body:has(.zc-to-top[data-visible]) .zc-livechat { --zlc-lift: 56px; }

  .zc-livechat-panel {
    top: max(16px, env(safe-area-inset-top));
    right: 0;
    bottom: 0;
    left: 0;
    width: auto;
    height: auto;
    padding-bottom: env(safe-area-inset-bottom);
    border-width: 1px 0 0;
    border-radius: 18px 18px 0 0;
    transform-origin: 50% 100%;
    transform: translateY(32px);
  }
  .zc-livechat[data-open][data-shown] .zc-livechat-launcher {
    opacity: 0;
    visibility: hidden;
    transform: scale(.6);
    transition-delay: 0s, 0s, 0s, 0s, var(--zlc-dur);
  }
  .zc-livechat-msg { max-width: 88%; }
}

@keyframes zc-livechat-in-left { from { opacity: 0; transform: translateX(-10px); } }
@keyframes zc-livechat-in-right { from { opacity: 0; transform: translateX(10px); } }
@keyframes zc-livechat-in-up { from { opacity: 0; transform: translateY(6px); } }
@keyframes zc-livechat-fade { from { opacity: 0; } }
@keyframes zc-livechat-out-right { to { opacity: 0; transform: translateX(10px); } }
@keyframes zc-livechat-fade-out { to { opacity: 0; } }
@keyframes zc-livechat-drain { from { transform: scaleX(1); } to { transform: scaleX(0); } }
@keyframes zc-livechat-shimmer { to { background-position: -200% 0; } }
@keyframes zc-livechat-pulse { 70% { box-shadow: 0 0 0 6px transparent; } 100% { box-shadow: 0 0 0 0 transparent; } }

/* Giảm chuyển động: bỏ mọi dịch chuyển, chỉ giữ mờ dần ngắn để vẫn thấy được thay đổi. */
@media (prefers-reduced-motion: reduce) {
  .zc-livechat { --zlc-dur: 120ms; --zlc-fast: 80ms; }
  .zc-livechat-launcher,
  .zc-livechat[data-shown] .zc-livechat-launcher:hover,
  .zc-livechat[data-shown] .zc-livechat-launcher:active,
  .zc-livechat-panel,
  .zc-livechat-launcher-chat,
  .zc-livechat-launcher-close,
  .zc-livechat-on,
  .zc-livechat-off,
  .zc-livechat-empty,
  .zc-livechat-badge,
  .zc-livechat-send-icon,
  .zc-livechat-blocked { transform: none !important; }
  .zc-livechat-msg[data-new] { animation-name: zc-livechat-fade !important; }
  .zc-livechat-msg[data-leaving] { animation-name: zc-livechat-fade-out !important; }
  .zc-livechat-bone,
  .zc-livechat-status[data-online="true"] .zc-livechat-dot::after { animation: none; }
  .zc-livechat-form[data-wait] .zc-livechat-compose::after { animation: none; transform: none; }
  #root::after { transition: none; }
}
