/* 2.48: giao diện chung cho mọi chỗ dùng AI — nhìn là biết AI: dải màu tím → hồng → xanh ngọc, biểu tượng lấp lánh,
   ánh sáng chạy khi đang soạn, sóng âm khi đang nghe, ô vừa được AI điền sáng lên một nhịp. */
:root {
  --td-ai-1: #7c3aed;
  --td-ai-2: #d946ef;
  --td-ai-3: #ec4899;
  --td-ai-4: #22d3ee;
  --td-ai-grad: linear-gradient(110deg, var(--td-ai-1), var(--td-ai-2) 45%, var(--td-ai-3) 70%, var(--td-ai-4));
  --td-ai-ink: #4c1d95;
}

/* ---------- Nút AI và nút micro ---------- */
.td-ai-btn,
.td-voice-btn {
  position: relative;
  display: inline-flex;
  width: max-content;
  max-width: 100%;
  align-self: flex-start;
  justify-self: start;
  align-items: center;
  gap: 8px;
  margin: 8px 0 10px;
  padding: 9px 16px 9px 12px;
  border: 1.5px solid transparent;
  border-radius: 999px;
  background: linear-gradient(#fff, #fff) padding-box, var(--td-ai-grad) border-box;
  color: var(--td-ai-ink);
  font: 600 15px/21px sans-serif; font-family: inherit;
  letter-spacing: .01em;
  cursor: pointer;
  overflow: hidden;
  isolation: isolate;
  box-shadow: 0 1px 2px rgba(76, 29, 149, .08), 0 4px 14px -6px rgba(217, 70, 239, .45);
  transition: transform .15s ease, box-shadow .2s ease;
}
.td-ai-btn:hover,
.td-voice-btn:hover { transform: translateY(-1px); box-shadow: 0 2px 4px rgba(76, 29, 149, .1), 0 8px 24px -8px rgba(217, 70, 239, .6); }
.td-ai-btn:focus-visible,
.td-voice-btn:focus-visible { outline: 2px solid var(--td-ai-2); outline-offset: 2px; }
.td-ai-btn .td-ai-ic,
.td-voice-btn svg { flex: none; width: 18px; height: 18px; }
.td-ai-btn .td-ai-ic path { fill: url(#td-ai-g); }
.td-voice-btn svg { stroke: url(#td-ai-g); }
.td-ai-btn > span,
.td-voice-btn > span {
  background: var(--td-ai-grad);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

/* Đang soạn: dải sáng chạy qua nút. */
.td-ai-btn[disabled],
.td-voice-btn.is-busy { cursor: wait; }
.td-ai-btn[disabled]::after,
.td-voice-btn.is-busy::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: linear-gradient(100deg, transparent 20%, rgba(217, 70, 239, .18) 45%, rgba(34, 211, 238, .18) 55%, transparent 80%);
  transform: translateX(-100%);
  animation: td-ai-sweep 1.2s ease-in-out infinite;
}
@keyframes td-ai-sweep { to { transform: translateX(100%); } }

/* Đang nghe: nút đổ màu, có sóng âm. */
.td-voice-btn.is-on {
  background: var(--td-ai-grad) border-box;
  color: #fff;
  box-shadow: 0 0 0 0 rgba(217, 70, 239, .5);
  animation: td-ai-ring 1.4s ease-out infinite;
}
.td-voice-btn.is-on > span { background: none; color: #fff; }
.td-voice-btn.is-on svg { stroke: #fff; }
.td-voice-btn.is-on::before {
  content: "";
  width: 22px;
  height: 14px;
  margin-right: -2px;
  background:
    linear-gradient(#fff, #fff) 0 50% / 3px 40% no-repeat,
    linear-gradient(#fff, #fff) 6px 50% / 3px 90% no-repeat,
    linear-gradient(#fff, #fff) 12px 50% / 3px 60% no-repeat,
    linear-gradient(#fff, #fff) 18px 50% / 3px 80% no-repeat;
  animation: td-ai-wave .9s ease-in-out infinite alternate;
  order: 3;
}
@keyframes td-ai-wave {
  50% { background-size: 3px 90%, 3px 35%, 3px 100%, 3px 45%; }
  100% { background-size: 3px 70%, 3px 60%, 3px 40%, 3px 95%; }
}
@keyframes td-ai-ring { 70% { box-shadow: 0 0 0 10px rgba(217, 70, 239, 0); } 100% { box-shadow: 0 0 0 0 rgba(217, 70, 239, 0); } }

/* Micro nhỏ trong ô ghi chú: chỉ biểu tượng. */
.td-cg-note .td-voice-btn { margin: 0 6px 0 0; padding: 8px; }
.td-cg-note .td-voice-btn > span { display: none; }

/* ---------- Dòng trạng thái của AI ---------- */
.td-ai-note,
.td-voice-note {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: -2px 0 12px;
  padding: 8px 12px;
  border-radius: 12px;
  background: linear-gradient(90deg, rgba(124, 58, 237, .07), rgba(34, 211, 238, .06));
  border-left: 3px solid transparent;
  border-image: var(--td-ai-grad) 1;
  color: var(--td-ai-ink);
  font-size: 13px;
  line-height: 19px;
}
.td-ai-note::before,
.td-voice-note::before { content: "✦"; flex: none; background: var(--td-ai-grad); -webkit-background-clip: text; background-clip: text; color: transparent; font-size: 14px; }

/* Ô vừa được AI điền: sáng lên một nhịp. */
.td-ai-filled { animation: td-ai-filled 1.6s ease-out 1; }
@keyframes td-ai-filled {
  0% { box-shadow: 0 0 0 0 rgba(217, 70, 239, .55); background-color: rgba(250, 232, 255, .9); }
  100% { box-shadow: 0 0 0 8px rgba(217, 70, 239, 0); background-color: inherit; }
}

/* ---------- Hộp tin nhắn AI soạn (Khách hàng) ---------- */
.td-ai-box {
  width: 100%;
  margin: 10px 0 0;
  padding: 12px;
  border: 1.5px solid transparent;
  border-radius: 16px;
  background: linear-gradient(#fdfaff, #fdfaff) padding-box, var(--td-ai-grad) border-box;
}
.td-ai-box textarea { width: 100%; min-height: 96px; padding: 10px; border: 1px solid #ede9fe; border-radius: 12px; background: #fff; font: inherit; }
.td-ai-box .td-ai-row { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }

/* ---------- Ô "Hỏi Tôi đẹp": bảng tối kiểu kính ---------- */
.td-ai-ask {
  position: relative;
  margin: 20px 0 8px;
  padding: 20px;
  border-radius: 22px;
  color: #ede9fe;
  background:
    radial-gradient(120% 90% at 0% 0%, rgba(124, 58, 237, .55), transparent 55%),
    radial-gradient(90% 80% at 100% 100%, rgba(236, 72, 153, .38), transparent 60%),
    radial-gradient(60% 60% at 90% 0%, rgba(34, 211, 238, .25), transparent 60%),
    #120a24;
  box-shadow: 0 18px 40px -18px rgba(76, 29, 149, .7), inset 0 0 0 1px rgba(255, 255, 255, .08);
  overflow: hidden;
  isolation: isolate;
}
.td-ai-ask::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background-image: linear-gradient(rgba(255, 255, 255, .05) 1px, transparent 1px), linear-gradient(90deg, rgba(255, 255, 255, .05) 1px, transparent 1px);
  background-size: 22px 22px;
  mask-image: linear-gradient(180deg, #000, transparent 85%);
  -webkit-mask-image: linear-gradient(180deg, #000, transparent 85%);
}
.td-ai-ask h2 { display: flex; align-items: center; gap: 10px; margin: 0 0 6px; color: #fff; font-size: 20px; line-height: 28px; }
.td-ai-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--td-ai-grad);
  color: #fff;
  font: 700 11px/18px sans-serif; font-family: inherit;
  letter-spacing: .08em;
}
.td-ai-ask .td-muted { color: rgba(237, 233, 254, .72); }
.td-ai-ask form { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; margin-top: 12px; }
.td-ai-ask textarea {
  flex: 1 1 260px;
  min-height: 52px;
  padding: 13px 14px;
  border: 1px solid rgba(255, 255, 255, .16);
  border-radius: 16px;
  background: rgba(255, 255, 255, .07);
  color: #fff;
  font: inherit;
  resize: vertical;
  backdrop-filter: blur(6px);
}
.td-ai-ask textarea::placeholder { color: rgba(237, 233, 254, .5); }
.td-ai-ask textarea:focus { outline: none; border-color: transparent; box-shadow: 0 0 0 2px var(--td-ai-2), 0 0 24px -4px rgba(217, 70, 239, .7); }
.td-ai-ask button[type="submit"] {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 52px;
  padding: 0 22px;
  border: 0;
  border-radius: 16px;
  background: var(--td-ai-grad);
  background-size: 160% 100%;
  color: #fff;
  font: 700 16px/1 sans-serif; font-family: inherit;
  cursor: pointer;
  transition: background-position .4s ease, transform .15s ease;
}
.td-ai-ask button[type="submit"]:hover { background-position: 100% 0; transform: translateY(-1px); }
.td-ai-ask button[disabled] { opacity: .7; cursor: wait; }
.td-ai-ask .td-voice-btn { margin: 0; min-height: 52px; background: linear-gradient(rgba(30, 16, 56, .92), rgba(30, 16, 56, .92)) padding-box, var(--td-ai-grad) border-box; color: #fff; box-shadow: none; }
.td-ai-ask .td-voice-btn > span { background: none; color: #fff; }
.td-ai-ask .td-voice-btn svg { stroke: #fff; }
.td-ai-ask .td-voice-note { color: #ede9fe; background: rgba(255, 255, 255, .06); flex-basis: 100%; }

/* Câu trả lời */
[data-ai-out]:not(:empty) {
  margin-top: 14px;
  padding: 14px 16px;
  border-radius: 16px;
  background: rgba(255, 255, 255, .06);
  border: 1px solid rgba(255, 255, 255, .1);
  color: #f5f3ff;
  font-size: 15px;
  line-height: 24px;
}
[data-ai-out] p { margin: 0 0 8px; }
[data-ai-out] p:last-of-type { margin-bottom: 0; }
[data-ai-out] strong { color: #fff; }
[data-ai-out] a {
  display: inline-block;
  margin: 0 1px;
  padding: 0 6px;
  border-radius: 6px;
  background: rgba(217, 70, 239, .25);
  color: #fff;
  font-size: 12px;
  line-height: 18px;
  text-decoration: none;
  vertical-align: 1px;
}
[data-ai-out] a:hover { background: var(--td-ai-2); }
[data-ai-out] .td-ai-think { display: flex; align-items: center; gap: 10px; margin: 0; color: rgba(237, 233, 254, .8); }
[data-ai-out] .td-ai-think i { display: inline-flex; gap: 4px; }
[data-ai-out] .td-ai-think i b { width: 6px; height: 6px; border-radius: 50%; background: var(--td-ai-grad); animation: td-ai-dot 1s ease-in-out infinite; }
[data-ai-out] .td-ai-think i b:nth-child(2) { animation-delay: .15s; }
[data-ai-out] .td-ai-think i b:nth-child(3) { animation-delay: .3s; }
@keyframes td-ai-dot { 50% { transform: translateY(-4px); opacity: .5; } }
.td-ai-ask .td-voice-say { margin-top: 12px; min-height: 44px; padding: 10px 20px; font-size: 16px; line-height: 22px; }
.td-ai-ask .td-voice-say svg { width: 20px; height: 20px; }
.td-ai-foot { margin: 10px 0 0; font-size: 12px; }

/* ---------- Nút nổi "Nói để tìm" (trang chủ, trang Tìm) ---------- */
/* 2.51.2: ghim giữa đáy màn hình; có thanh menu đáy (điện thoại) thì nằm ngay trên thanh đó. Ghi chú / ô gõ hiện phía trên nút. */
.td-voice-find { position: fixed; right: calc(16px + env(safe-area-inset-right)); bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 55; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; width: max-content; max-width: calc(100vw - 32px); transition: bottom .22s ease; }
.td-voice-find.on-dock { bottom: calc(92px + env(safe-area-inset-bottom)); }
body.td-chrome-off .td-voice-find.on-dock { bottom: calc(16px + env(safe-area-inset-bottom)); }
.td-voice-find .td-voice-btn {
  margin: 0;
  align-self: flex-end;
  padding: 10px 18px 10px 14px;
  font-size: 15px;
  line-height: 24px;
  background: linear-gradient(#120a24, #120a24) padding-box, var(--td-ai-grad) border-box;
  color: #fff;
  box-shadow: 0 10px 30px -8px rgba(124, 58, 237, .7), 0 0 0 1px rgba(255, 255, 255, .04);
}
.td-voice-find .td-voice-btn > span { background: none; color: #fff; }
.td-voice-find .td-voice-btn svg { stroke: #f0abfc; }
.td-voice-find .td-voice-btn.is-on svg { stroke: #fff; }
.td-voice-find .td-voice-btn svg { width: 19px; height: 19px; }
.td-voice-find .td-voice-note { order: -1; max-width: min(320px, calc(100vw - 32px)); font-size: 14px; margin: 0; background: #120a24; color: #ede9fe; box-shadow: 0 8px 24px -8px rgba(0, 0, 0, .4); }

@media (max-width: 480px) {
  .td-ai-ask { padding: 16px; border-radius: 18px; }
  .td-ai-ask button[type="submit"] { flex: 1; justify-content: center; }
}
@media (prefers-reduced-motion: reduce) {
  .td-ai-btn, .td-voice-btn, .td-ai-btn::after, .td-voice-btn::after, .td-voice-btn::before, .td-ai-filled, [data-ai-out] .td-ai-think i b { animation: none !important; transition: none !important; }
}

/* ---------- Ô gõ dự phòng khi trình duyệt không nhận giọng (app trên iPhone, micro bị chặn) ---------- */
.td-voice-type {
  width: 100%;
  max-width: 420px;
  margin: 0 0 12px;
  padding: 10px;
  border: 1.5px solid transparent;
  border-radius: 16px;
  background: linear-gradient(#fdfaff, #fdfaff) padding-box, var(--td-ai-grad) border-box;
}
.td-voice-type p { margin: 0 0 8px; color: var(--td-ai-ink); font-size: 13px; line-height: 19px; }
.td-voice-type div { display: flex; gap: 8px; }
.td-voice-type input { flex: 1; min-width: 0; padding: 10px 12px; border: 1px solid #ede9fe; border-radius: 12px; font: inherit; }
.td-voice-type button { padding: 0 16px; border: 0; border-radius: 12px; background: var(--td-ai-grad); color: #fff; font: 700 14px/1 sans-serif; font-family: inherit; cursor: pointer; }
.td-ai-ask .td-voice-type { max-width: none; flex-basis: 100%; background: linear-gradient(rgba(30, 16, 56, .92), rgba(30, 16, 56, .92)) padding-box, var(--td-ai-grad) border-box; }
.td-ai-ask .td-voice-type p { color: #ede9fe; }
.td-ai-ask .td-voice-type input { background: rgba(255, 255, 255, .08); border-color: rgba(255, 255, 255, .16); color: #fff; }
.td-voice-find .td-voice-type { order: -1; width: min(340px, calc(100vw - 32px)); margin: 0; background: linear-gradient(#120a24, #120a24) padding-box, var(--td-ai-grad) border-box; box-shadow: 0 12px 30px -10px rgba(0, 0, 0, .5); }
.td-voice-find .td-voice-type p { color: #ede9fe; }

/* 2.48.2: nút đóng ô gõ; hàng nút của "Hỏi Tôi đẹp" (Nói câu hỏi — Hỏi AI) nằm dưới ô nhập. */
.td-voice-type { position: relative; }
.td-voice-type p { padding-right: 26px; }
.td-voice-x { position: absolute; top: 6px; right: 6px; width: 28px; height: 28px; padding: 0; border: 0; border-radius: 50%; background: rgba(124, 58, 237, .1); color: var(--td-ai-ink); font: 400 20px/28px sans-serif; font-family: inherit; cursor: pointer; }
.td-voice-type .td-voice-x { width: 28px; padding: 0; background: rgba(124, 58, 237, .1); color: var(--td-ai-ink); }
.td-ai-ask .td-voice-type .td-voice-x, .td-voice-find .td-voice-type .td-voice-x { background: rgba(255, 255, 255, .12); color: #fff; }
.td-ai-ask form { align-items: center; }
.td-ai-ask textarea { flex: 1 1 100%; order: 0; }
/* 2.50.2: Hỏi AI một hàng, Nói câu hỏi một hàng bên dưới — cả hai rộng hết khung. */
.td-ai-ask button[type="submit"] { order: 1; flex: 1 1 100%; width: 100%; margin-left: 0; min-height: 48px; justify-content: center; }
.td-ai-ask form .td-voice-btn { order: 2; flex: 1 1 100%; width: 100%; min-height: 48px; justify-content: center; font-size: 16px; line-height: 22px; }
.td-ai-ask form .td-voice-btn svg { width: 20px; height: 20px; }
.td-ai-ask form .td-voice-note, .td-ai-ask form .td-voice-type { order: 3; }
.td-voice-type > div { width: 100%; }
.td-ai-ask .td-voice-type { width: 100%; }
.td-voice-diag { display: block; margin-top: 8px; font: 11px/16px ui-monospace, Consolas, monospace; color: rgba(76, 29, 149, .7); word-break: break-word; }
.td-ai-ask .td-voice-diag, .td-voice-find .td-voice-diag { color: rgba(237, 233, 254, .6); }

/* 2.52: micro nhỏ trong ô tìm (bảng lọc của ô tìm tiệm, ô tìm bài viết). Chỉ có khi bật AI và giọng nói. */
.td-voice-in { position: relative; }
.td-voice-btn.td-voice-ico,
.td-hub-search .td-voice-btn.td-voice-ico {
  position: absolute; top: 50%; right: 44px; z-index: 2;
  display: grid; place-items: center;
  width: 36px; height: 36px; min-height: 0; margin: 0; padding: 0;
  border: 1.5px solid transparent; border-radius: 50%;
  background: linear-gradient(#fff, #fff) padding-box, var(--td-ai-grad) border-box;
  color: #7c3aed; transform: translateY(-50%); box-shadow: none;
}
.td-voice-btn.td-voice-ico:hover { transform: translateY(-50%); }
.td-voice-btn.td-voice-ico > span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.td-voice-btn.td-voice-ico svg { width: 18px; height: 18px; stroke: currentColor; }
.td-voice-btn.td-voice-ico.is-on { background: var(--td-ai-grad); color: #fff; }
.td-voice-btn.td-voice-ico.is-on::before { display: none; }
.td-hub-search .td-voice-btn.td-voice-ico { right: 52px; }
.td-pop-qwrap.td-voice-in .td-pop-q { padding-right: 84px; }
.td-hub-search.td-voice-in input[name="q"] { padding-right: 100px; }
/* Câu ví dụ, chữ vừa nghe, ô gõ dự phòng: một dòng riêng ngay dưới ô tìm, đẩy phần dưới xuống (không đè). */
.td-voice-in + .td-voice-note,
.td-voice-in + .td-voice-type { width: auto; max-width: none; margin: 8px 0 0; }
/* 2.67: có menu đáy (đăng nhập, màn ≤ 1023px) thì nút Nói để tìm nằm trên dock ngay từ đầu — không nhảy vị trí khi JS gắn .on-dock. */
@media (max-width: 1023px) { body.td-app .td-voice-find { bottom: calc(92px + env(safe-area-inset-bottom)); } }
