/* AI 客服對話框（/_byoc/ai-chat.js）：零壹官網與學會後台共用；主色跟著網站（官網 --brick、後台 --accent），吉祥物「小壹」固定用零壹電光藍 */
#byoc-ai {
    --ai-accent: var(--brick, var(--accent, #2f5bff));
    --ai-ink: #141a26;
    --ai-muted: #5b6476;
    --ai-line: #dce2ec;
    --ai-bg: #ffffff;
    --ai-soft: #f3f6fc;
    --ai-bot-bg: #eaf0ff;
    --ai-ok: #16a34a;
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 900;
    font-size: 15px;
    font-weight: 400;
    line-height: 1.6;
    text-align: left;
    color: var(--ai-ink);
}
#byoc-ai[hidden] { display: none; }
#byoc-ai.ai-no-chat { display: none; }   /* 只開填寫建議、沒有 AI 助理：右下角不顯示（填寫建議在頁面欄位旁，不在這個框裡） */
#byoc-ai button { font: inherit; }
#byoc-ai [hidden] { display: none !important; }

/* ===== 吉祥物「小壹」 ===== */
.ai-bot { display: inline-block; width: 32px; height: 32px; flex: none; }
.ai-bot svg { display: block; width: 100%; height: 100%; overflow: visible; }
.ai-bot .b-all, .ai-bot .b-look, .ai-bot .b-eyes, .ai-bot .b-tip, .ai-bot .b-glass, .ai-bot .b-mouth { transform-box: fill-box; transform-origin: center; }
.ai-bot .b-all { transform-origin: 50% 100%; transition: transform .3s; }
.ai-bot .b-look { transition: transform .3s; }
.ai-bot .b-happy, .ai-bot .b-sad, .ai-bot .b-glass { opacity: 0; transition: opacity .2s; }
.ai-bot .b-eyes, .ai-bot .b-mouth { transition: opacity .2s; }
.ai-bot[data-mood] .b-eyes { animation: ai-blink 4.8s infinite; }
.ai-bot[data-mood="thinking"] .b-look { transform: translate(2px, -2.5px); }
.ai-bot[data-mood="thinking"] .b-all { animation: ai-tilt 2.2s ease-in-out infinite; }
.ai-bot[data-mood="thinking"] .b-tip,
.ai-bot[data-mood="searching"] .b-tip,
.ai-bot[data-mood="talking"] .b-tip { animation: ai-glow 1s ease-in-out infinite; }
.ai-bot[data-mood="searching"] .b-glass { opacity: 1; animation: ai-scan 1.6s ease-in-out infinite; }
.ai-bot[data-mood="searching"] .b-look { animation: ai-look 1.6s ease-in-out infinite; }
.ai-bot[data-mood="talking"] .b-mouth { animation: ai-talk .32s ease-in-out infinite alternate; }
.ai-bot[data-mood="happy"] .b-eyes { opacity: 0; }
.ai-bot[data-mood="happy"] .b-happy { opacity: 1; }
.ai-bot[data-mood="happy"] .b-all { animation: ai-hop .6s ease-out 2; }
.ai-bot[data-mood="sad"] .b-mouth { opacity: 0; }
.ai-bot[data-mood="sad"] .b-sad { opacity: 1; }
.ai-bot[data-mood="sad"] .b-look { transform: translateY(1.5px); }
@keyframes ai-blink { 0%, 93%, 100% { transform: scaleY(1); } 96% { transform: scaleY(.1); } }
@keyframes ai-tilt { 0%, 100% { transform: rotate(-5deg); } 50% { transform: rotate(5deg); } }
@keyframes ai-glow { 0%, 100% { fill: #ffd34d; transform: scale(1); } 50% { fill: #ffef9e; transform: scale(1.3); } }
@keyframes ai-scan { 0%, 100% { transform: translate(0, 0) rotate(0); } 50% { transform: translate(-22px, -6px) rotate(-12deg); } }
@keyframes ai-look { 0%, 100% { transform: translateX(1.5px); } 50% { transform: translateX(-3px); } }
@keyframes ai-talk { from { transform: scaleY(1); } to { transform: scaleY(1.9) translateY(.5px); } }
@keyframes ai-hop { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-4px) scale(1.04); } }
@keyframes ai-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-3px); } }

/* ===== 右下角按鈕與招呼泡泡 ===== */
.ai-fab {
    position: relative; display: inline-flex; align-items: center; gap: 10px;
    border: 0; border-radius: 999px; padding: 6px 18px 6px 6px;
    background: var(--ai-bg); color: var(--ai-ink); font-weight: 700; cursor: pointer;
    box-shadow: 0 10px 28px rgba(31, 67, 214, .25), 0 0 0 1px rgba(47, 91, 255, .12);
    transition: transform .2s, box-shadow .2s;
}
.ai-fab:hover { transform: translateY(-2px); box-shadow: 0 14px 32px rgba(31, 67, 214, .32), 0 0 0 2px rgba(47, 91, 255, .25); }
.ai-fab-bot { width: 46px; height: 46px; padding: 5px; border-radius: 50%; background: var(--ai-bot-bg); box-sizing: border-box; animation: ai-bob 2.8s ease-in-out infinite; }
.ai-fab:hover .ai-fab-bot .b-all { transform: rotate(-8deg); }
.ai-unread { position: absolute; top: 4px; left: 42px; width: 12px; height: 12px; border-radius: 50%; background: #ef4444; border: 2px solid #fff; }
.ai-fab:focus-visible, #byoc-ai .ai-panel button:focus-visible, .ai-teaser button:focus-visible { outline: 3px solid rgba(47, 91, 255, .4); outline-offset: 2px; }
#byoc-ai.is-open .ai-fab, #byoc-ai.is-open .ai-teaser { display: none; }
.ai-teaser {
    position: absolute; right: 0; bottom: calc(100% + 12px); width: max-content; max-width: 250px;
    display: flex; align-items: flex-start; gap: 4px; padding: 10px 8px 10px 14px;
    background: var(--ai-bg); border-radius: 16px 16px 4px 16px;
    box-shadow: 0 10px 28px rgba(11, 19, 36, .18); animation: ai-pop .35s cubic-bezier(.2, 1.4, .4, 1) both;
}
.ai-teaser::after { content: ""; position: absolute; right: 26px; bottom: -6px; width: 12px; height: 12px; background: var(--ai-bg); transform: rotate(45deg); }
.ai-teaser-text { border: 0; background: none; padding: 0; color: var(--ai-ink); font-size: 14px; text-align: left; cursor: pointer; line-height: 1.5; }
.ai-teaser-x { border: 0; background: none; color: var(--ai-muted); font-size: 12px; padding: 0 4px; cursor: pointer; }

/* ===== 對話視窗 ===== */
.ai-panel {
    width: min(390px, calc(100vw - 32px)); height: min(600px, calc(100vh - 110px));
    display: flex; flex-direction: column; overflow: hidden;
    background: var(--ai-bg); color: var(--ai-ink);
    border-radius: 22px;
    box-shadow: 0 24px 60px rgba(11, 19, 36, .28), 0 0 0 1px rgba(11, 19, 36, .06);
    transform-origin: 100% 100%; animation: ai-open .28s cubic-bezier(.2, 1.2, .4, 1) both;
}
.ai-head {
    display: flex; align-items: center; gap: 10px; padding: 12px 12px 12px 14px;
    background: var(--ai-accent); background-image: linear-gradient(135deg, rgba(255, 255, 255, .22), rgba(255, 255, 255, 0) 60%); color: #fff;
}
.ai-head-avatar { position: relative; flex: none; width: 44px; height: 44px; border-radius: 50%; background: #fff; display: grid; place-items: center; box-shadow: 0 4px 12px rgba(0, 0, 0, .15); }
.ai-head-bot { width: 36px; height: 36px; }
.ai-online { position: absolute; right: 0; bottom: 1px; width: 11px; height: 11px; border-radius: 50%; background: #22c55e; border: 2px solid #fff; }
.ai-head-text { flex: 1; display: flex; flex-direction: column; min-width: 0; line-height: 1.35; }
.ai-head-text strong { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ai-status { font-size: 12.5px; opacity: .9; }
.ai-head.is-busy .ai-status { animation: ai-fade 1.4s ease-in-out infinite; }
.ai-head-btn { border: 0; background: rgba(255, 255, 255, .18); color: #fff; border-radius: 999px; padding: 4px 11px; font-size: 13px; cursor: pointer; transition: background .15s; }
.ai-head-btn:hover { background: rgba(255, 255, 255, .3); }
.ai-head-btn:disabled { opacity: .5; cursor: default; }
.ai-close { padding: 4px 9px; }

.ai-log {
    flex: 1; overflow-y: auto; padding: 16px 14px; display: flex; flex-direction: column; gap: 14px;
    background: var(--ai-soft); background-image: radial-gradient(rgba(47, 91, 255, .07) 1px, transparent 1px); background-size: 18px 18px;
    scroll-behavior: smooth;
}
.ai-row { display: flex; gap: 8px; align-items: flex-end; animation: ai-in .3s ease-out both; }
.ai-static .ai-row, .ai-static .ai-suggest { animation: none; }
.ai-row-user { justify-content: flex-end; }
.ai-avatar { flex: none; width: 32px; height: 32px; border-radius: 50%; background: var(--ai-bg); display: grid; place-items: center; box-shadow: 0 2px 6px rgba(11, 19, 36, .1); align-self: flex-start; }
.ai-avatar .ai-bot { width: 26px; height: 26px; }
.ai-avatar .ai-bot:not([data-mood]) .b-eyes { animation: none; }
.ai-col { display: flex; flex-direction: column; min-width: 0; max-width: 84%; }
.ai-row-user .ai-col { align-items: flex-end; }
.ai-msg { font-weight: 400; padding: 10px 13px; border-radius: 18px; white-space: normal; overflow-wrap: anywhere; }
.ai-assistant { background: var(--ai-bg); border-top-left-radius: 6px; box-shadow: 0 1px 3px rgba(11, 19, 36, .08); }
.ai-user { background: var(--ai-accent); color: #fff; border-bottom-right-radius: 6px; box-shadow: 0 2px 8px rgba(47, 91, 255, .2); }
.ai-user a { color: #fff; }
.ai-assistant a { color: var(--ai-accent); }
.ai-error { background: #fff6f6; box-shadow: 0 0 0 1px #f5cccc; }
.ai-foot { display: flex; gap: 10px; align-items: center; margin-top: 3px; padding: 0 4px; font-size: 11.5px; color: var(--ai-muted); }
.ai-foot:empty { display: none; }
.ai-mini { border: 0; background: none; padding: 0; color: var(--ai-muted); font-size: 11.5px !important; cursor: pointer; }
.ai-mini:hover { color: var(--ai-accent); text-decoration: underline; }
.ai-fb { display: inline-flex; gap: 2px; }
.ai-fb-btn { border: 1px solid transparent; background: none; border-radius: 999px; padding: 0 5px; font-size: 13px !important; line-height: 1.5; cursor: pointer; filter: grayscale(1); opacity: .6; transition: opacity .15s, filter .15s, background .15s; }
.ai-fb-btn:hover { opacity: 1; filter: none; }
.ai-fb-btn.is-on { opacity: 1; filter: none; background: var(--ai-bot-bg); border-color: rgba(47, 91, 255, .25); }
.ai-fb-note { margin: 4px 4px 0; font-size: 12px; color: var(--ai-muted); animation: ai-in .25s ease-out both; }
.ai-trace {font-size: 11.5px; color: var(--ai-ok); margin: 0 0 3px 4px; }
.ai-trace::before { content: "✓ "; font-weight: 700; }
.ai-src { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 9px; padding-top: 8px; border-top: 1px dashed var(--ai-line); }
.ai-src-label { font-size: 12px; color: var(--ai-muted); }
.ai-src-label::before { content: "📄 "; }
a.ai-src-chip { font-size: 12px; line-height: 1.4; padding: 3px 9px; border-radius: 999px; background: var(--ai-bot-bg); color: #1f43d6; text-decoration: none; transition: background .15s; }
a.ai-src-chip:hover { background: #d8e3ff; }
.ai-typing-text { white-space: pre-wrap; }
.ai-caret { display: inline-block; width: 2px; height: 1.05em; margin-left: 2px; vertical-align: -2px; background: var(--ai-accent); animation: ai-fade .7s steps(2) infinite; }

/* 查詢中：三個步驟 */
.ai-think-card { min-width: 210px; }
.ai-think-title { font-weight: 700; font-size: 14px; display: flex; align-items: center; gap: 6px; }
.ai-dots { display: inline-flex; gap: 3px; }
.ai-dots i { width: 5px; height: 5px; border-radius: 50%; background: var(--ai-accent); animation: ai-dot 1.1s ease-in-out infinite; }
.ai-dots i:nth-child(2) { animation-delay: .15s; }
.ai-dots i:nth-child(3) { animation-delay: .3s; }
.ai-steps { list-style: none; margin: 6px 0 0; padding: 0; display: grid; gap: 4px; }
.ai-step { display: flex; align-items: center; gap: 8px; font-size: 13.5px; color: #9aa3b5; transition: color .2s; }
.ai-step i { position: relative; flex: none; width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--ai-line); box-sizing: border-box; transition: background .2s, border-color .2s; }
.ai-step.is-active { color: var(--ai-ink); }
.ai-step.is-active i { border-color: var(--ai-accent); border-right-color: transparent; animation: ai-spin .8s linear infinite; }
.ai-step.is-active span {
    background: linear-gradient(90deg, var(--ai-ink) 0%, var(--ai-ink) 35%, #9aa3b5 50%, var(--ai-ink) 65%, var(--ai-ink) 100%);
    background-size: 220% 100%; -webkit-background-clip: text; background-clip: text; color: transparent; animation: ai-shine 1.6s linear infinite;
}
.ai-step.is-done { color: var(--ai-muted); }
.ai-step.is-done i { background: var(--ai-ok); border-color: var(--ai-ok); animation: ai-pop .3s ease-out; }
.ai-step.is-done i::after { content: ""; position: absolute; left: 3.5px; top: 1px; width: 4px; height: 7px; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg); }

/* 建議問題 */
.ai-suggest { display: flex; flex-wrap: wrap; gap: 7px; padding-left: 40px; animation: ai-in .3s .1s ease-out both; }
.ai-suggest-label { width: 100%; font-size: 12px; color: var(--ai-muted); }
.ai-chip { border: 1px solid rgba(47, 91, 255, .25); background: var(--ai-bg); color: #1f43d6; border-radius: 999px; padding: 6px 12px; font-size: 13px !important; cursor: pointer; transition: background .15s, transform .15s; }
.ai-chip:hover { background: var(--ai-bot-bg); transform: translateY(-1px); }

/* 輸入區 */
.ai-form { display: flex; align-items: flex-end; gap: 8px; padding: 10px 12px; border-top: 1px solid var(--ai-line); background: var(--ai-bg); }
.ai-input { position: relative; flex: 1; display: flex; background: var(--ai-soft); border: 1px solid var(--ai-line); border-radius: 20px; transition: border-color .15s, box-shadow .15s; }
.ai-input:focus-within { border-color: var(--ai-accent); box-shadow: 0 0 0 3px rgba(47, 91, 255, .12); background: var(--ai-bg); }
.ai-input textarea { flex: 1; resize: none; border: 0; outline: 0; background: transparent; padding: 9px 14px; font: inherit; font-size: 14px; line-height: 1.5; color: var(--ai-ink); max-height: 110px; min-height: 0; box-shadow: none; }
.ai-count { position: absolute; right: 12px; top: -18px; font-size: 11px; color: var(--ai-muted); }
.ai-send { flex: none; width: 40px; height: 40px; border: 0; border-radius: 50%; display: grid; place-items: center; background: var(--ai-accent); color: #fff; cursor: pointer; transition: transform .15s, opacity .15s; }
.ai-send svg { width: 19px; height: 19px; margin-left: -2px; }
.ai-send:not(:disabled):hover { transform: scale(1.07); }
.ai-send:disabled { opacity: .35; cursor: default; }
.ai-note { margin: 0; padding: 0 14px 10px; font-size: 11.5px; line-height: 1.5; color: var(--ai-muted); background: var(--ai-bg); }

@keyframes ai-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
@keyframes ai-open { from { opacity: 0; transform: scale(.88) translateY(10px); } to { opacity: 1; transform: none; } }
@keyframes ai-pop { 0% { transform: scale(.6); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }
@keyframes ai-dot { 0%, 60%, 100% { transform: translateY(0); opacity: .45; } 30% { transform: translateY(-4px); opacity: 1; } }
@keyframes ai-spin { to { transform: rotate(360deg); } }
@keyframes ai-shine { from { background-position: 100% 0; } to { background-position: -120% 0; } }
@keyframes ai-fade { 50% { opacity: .45; } }

/* ===== AI 填寫建議（欄位旁的「✨ 填寫建議」，在對話框以外的頁面內容裡） ===== */
.ai-sg-btn { display: inline-flex; align-items: center; gap: 6px; margin: 6px 0 2px; padding: 4px 12px 4px 5px; border: 1px solid rgba(47, 91, 255, .3); border-radius: 999px;
    background: #eef3ff; color: #1f43d6; font: inherit; font-size: 13.5px; font-weight: 600; cursor: pointer; transition: background .15s, transform .15s; }
.ai-sg-btn:hover { background: #e0e8ff; transform: translateY(-1px); }
.ai-sg-btn:disabled { opacity: .6; cursor: wait; transform: none; }
.ai-sg-bot { width: 22px; height: 22px; }
.ai-sg-panel { margin: 8px 0 12px; padding: 12px 14px; border: 1px solid #d6e0ff; border-radius: 14px; background: #f7f9ff; color: #141a26; font-size: 14px; line-height: 1.65; animation: ai-in .25s ease-out both; }
.ai-sg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ai-sg-x { border: 0; background: none; color: #5b6476; cursor: pointer; font-size: 13px; }
.ai-sg-wait { display: flex; align-items: center; gap: 8px; color: #5b6476; }
.ai-sg-wait .ai-bot { width: 30px; height: 30px; }
.ai-sg-meta, .ai-sg-tip, .ai-sg-note { margin: 6px 0; font-size: 12.5px; color: #5b6476; }
.ai-sg-tip { color: #1f43d6; }
.ai-sg-err { margin: 6px 0; color: #991b1b; }
.ai-sg-card { margin: 10px 0; padding: 10px 12px; border-radius: 12px; background: #fff; box-shadow: 0 1px 3px rgba(11, 19, 36, .08); }
.ai-sg-title { font-weight: 700; font-size: 13px; color: #1f43d6; margin-bottom: 4px; }
.ai-sg-text { white-space: pre-line; overflow-wrap: anywhere; }
.ai-sg-acts { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.ai-sg-act { border: 1px solid #d6e0ff; background: #fff; color: #1f43d6; border-radius: 999px; padding: 3px 12px; font: inherit; font-size: 13px; cursor: pointer; }
.ai-sg-act:hover { background: #eef3ff; }
.ai-sg-primary { background: #2f5bff; border-color: #2f5bff; color: #fff; }
.ai-sg-primary:hover { background: #1f43d6; }
.ai-sg-link { border: 0; background: none; padding: 0; color: #1f43d6; text-decoration: underline; font: inherit; cursor: pointer; }
textarea.ai-sg-filled { box-shadow: 0 0 0 3px rgba(47, 91, 255, .35); transition: box-shadow .3s; }

@media (max-width: 560px) {
    #byoc-ai { right: 12px; bottom: 12px; }
    #byoc-ai.is-open { left: 12px; }
    .ai-panel { width: 100%; height: calc(100vh - 90px); border-radius: 18px; }
    .ai-fab-text { display: none; }
    .ai-fab { padding: 6px; }
    .ai-unread { left: auto; right: 4px; }
    .ai-teaser { max-width: calc(100vw - 40px); }
}
@media (prefers-reduced-motion: reduce) {
    #byoc-ai *, #byoc-ai *::before, #byoc-ai *::after { animation: none !important; transition: none !important; }
    .ai-step.is-active span { color: var(--ai-ink); background: none; }
}
@media print { #byoc-ai { display: none !important; } }
