/* ══════════════════════════════════════════════════════════════
   IcoonAI — AI TALKING TEACHER AVATAR v3.0
   Animated SVG face panel with TTS lip-sync and emotion system
   ══════════════════════════════════════════════════════════════ */

/* ── OUTER CONTAINER ──────────────────────────────────────── */
#liveAvatar {
    position: fixed !important;
    left: 0.75rem;
    bottom: 5.5rem;
    z-index: 92 !important;
    pointer-events: none !important;
    transition: opacity 0.5s ease, transform 0.5s cubic-bezier(0.34, 1.2, 0.64, 1),
                left 0.7s cubic-bezier(0.45, 0, 0.55, 1),
                bottom 0.7s cubic-bezier(0.45, 0, 0.55, 1) !important;
    will-change: transform, opacity, left, bottom !important;
}
#liveAvatar.hidden {
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
    transform: translateY(20px) !important;
}
#liveAvatar:not(.hidden) {
    visibility: visible !important;
}

/* ── FACE PANEL ───────────────────────────────────────────── */
#avFacePanel {
    position: relative;
    width: 150px;
    cursor: pointer;
    pointer-events: auto;
    user-select: none;
    animation: avFloat 4.2s ease-in-out infinite;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}
#avFacePanel:hover {
    transform: scale(1.04) translateY(-2px);
}
#avFacePanel:active {
    transform: scale(0.97);
}

/* ── SVG FACE WRAPPER ─────────────────────────────────────── */
#avFaceWrap {
    position: relative;
    width: 150px;
    height: 150px;
    border-radius: 50%;
    overflow: hidden;
    background: linear-gradient(145deg, #EEF2FF 0%, #E0E7FF 100%);
    box-shadow:
        0 0 0 3px #fff,
        0 0 0 5px var(--k12-accent, #6366f1),
        0 8px 32px rgba(99,102,241,0.35),
        0 2px 8px rgba(0,0,0,0.12);
    animation: avGlowPulse 3s ease-in-out infinite;
    transition: box-shadow 0.4s ease;
}
#liveAvatar.speaking #avFaceWrap {
    box-shadow:
        0 0 0 3px #fff,
        0 0 0 6px #22d3ee,
        0 0 28px rgba(34,211,238,0.6),
        0 8px 32px rgba(99,102,241,0.4);
    animation: avSpeakGlow 0.6s ease-in-out infinite alternate, avFloat 4.2s ease-in-out infinite;
}
#liveAvatar.celebrating #avFaceWrap {
    box-shadow:
        0 0 0 3px #fff,
        0 0 0 6px #f59e0b,
        0 0 32px rgba(245,158,11,0.55),
        0 8px 32px rgba(99,102,241,0.35);
}
#liveAvatar.thinking #avFaceWrap {
    box-shadow:
        0 0 0 3px #fff,
        0 0 0 5px #a78bfa,
        0 0 20px rgba(167,139,250,0.4),
        0 8px 28px rgba(0,0,0,0.1);
}

/* ── AUDIO WAVE BARS ──────────────────────────────────────── */
#avWaveBars {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 3px;
    height: 18px;
    margin: 5px auto 0;
    padding: 0 12px;
}
#avWaveBars span {
    display: inline-block;
    width: 4px;
    border-radius: 2px;
    background: var(--k12-accent, #6366f1);
    opacity: 0.3;
    height: 4px;
    transition: height 0.15s ease, opacity 0.15s ease;
}
#avWaveBars.active span {
    opacity: 1;
    animation: avBarBounce 0.5s ease-in-out infinite alternate;
}
#avWaveBars.active span:nth-child(1) { animation-delay: 0s;    animation-duration: 0.45s; }
#avWaveBars.active span:nth-child(2) { animation-delay: 0.07s; animation-duration: 0.5s;  }
#avWaveBars.active span:nth-child(3) { animation-delay: 0.14s; animation-duration: 0.38s; }
#avWaveBars.active span:nth-child(4) { animation-delay: 0.21s; animation-duration: 0.52s; }
#avWaveBars.active span:nth-child(5) { animation-delay: 0.1s;  animation-duration: 0.42s; }
#avWaveBars.active span:nth-child(6) { animation-delay: 0.18s; animation-duration: 0.48s; }
#avWaveBars.active span:nth-child(7) { animation-delay: 0.05s; animation-duration: 0.55s; }

/* ── NAME BAR ─────────────────────────────────────────────── */
#avNameBar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
    margin-top: 3px;
    pointer-events: auto;
}
#av3dBadge {
    background: var(--k12-accent, #6366f1);
    color: #fff;
    font-size: 0.5rem;
    font-weight: 800;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    padding: 0.18rem 0.65rem;
    border-radius: 1rem;
    white-space: nowrap;
    box-shadow: 0 2px 10px rgba(99,102,241,0.3);
}

/* ── VOICE TOGGLE BUTTON ──────────────────────────────────── */
#avVoiceToggle {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: none;
    background: var(--k12-accent, #6366f1);
    color: #fff;
    font-size: 0.55rem;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s, transform 0.2s;
    box-shadow: 0 2px 8px rgba(99,102,241,0.35);
    flex-shrink: 0;
}
#avVoiceToggle:hover { background: #4338ca; transform: scale(1.12); }
#avVoiceToggle.av-voice-off { background: #94a3b8; }
#avVoiceToggle.av-voice-off:hover { background: #64748b; }

/* ── STATUS BAR ───────────────────────────────────────────── */
#avStatusBar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    margin-top: 2px;
}
.av-live-dot2 {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #22c55e;
    animation: avDotPulse 2s ease-in-out infinite;
}
#liveAvatar.speaking .av-live-dot2 {
    background: #22d3ee;
    animation: avDotPulse 0.8s ease-in-out infinite;
}
#liveAvatar.thinking .av-live-dot2 {
    background: #a78bfa;
}
#avStatusText {
    font-size: 0.44rem;
    font-weight: 700;
    color: #64748b;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

/* ── REACTION EMOJI ───────────────────────────────────────── */
.pa-reaction {
    position: absolute;
    top: -14px;
    right: -6px;
    font-size: 1.5rem;
    z-index: 4;
    pointer-events: none;
    opacity: 0;
    transform: scale(0) translateY(10px);
    transition: all 0.35s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.pa-reaction.show {
    opacity: 1;
    transform: scale(1) translateY(0);
}
.pa-reaction.pop-out {
    opacity: 0;
    transform: scale(1.5) translateY(-22px);
    transition: all 0.4s ease-out;
}

/* ── SPEECH BUBBLE ────────────────────────────────────────── */
.av-speech {
    position: absolute !important;
    left: 162px !important;
    top: 10px !important;
    background: #fff !important;
    color: #1e293b !important;
    border-radius: 1.25rem !important;
    padding: 0.9rem 1.15rem !important;
    max-width: 330px !important;
    min-width: 180px !important;
    font-size: 0.88rem !important;
    font-weight: 600 !important;
    line-height: 1.65 !important;
    box-shadow: 0 8px 32px rgba(0,0,0,0.14), 0 2px 8px rgba(99,102,241,0.12) !important;
    pointer-events: auto !important;
    opacity: 0 !important;
    transform: scale(0.88) translateX(-8px) !important;
    transition: opacity 0.35s ease, transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) !important;
    border: 1.5px solid rgba(99,102,241,0.12) !important;
}
.av-speech.show {
    opacity: 1 !important;
    transform: scale(1) translateX(0) !important;
}
.av-speech::before {
    content: '' !important;
    position: absolute !important;
    left: -8px !important;
    top: 32px !important;
    border-top: 9px solid transparent !important;
    border-bottom: 9px solid transparent !important;
    border-right: 9px solid #fff !important;
}
.av-speech-close {
    position: absolute;
    top: 5px;
    right: 9px;
    background: none;
    border: none;
    color: #94a3b8;
    font-size: 0.68rem;
    cursor: pointer;
    transition: color 0.2s;
    padding: 2px 4px;
}
.av-speech-close:hover { color: #475569; }

.av-speech-step {
    display: inline-block;
    background: rgba(99,102,241,0.1);
    color: #6366f1;
    font-size: 0.62rem;
    font-weight: 800;
    padding: 0.1rem 0.5rem;
    border-radius: 0.5rem;
    margin-bottom: 0.35rem;
    letter-spacing: 0.04em;
}

/* ── TYPEWRITER CURSOR ────────────────────────────────────── */
.av-typewriter { display: inline; }
.av-typewriter .av-cursor {
    display: inline-block;
    width: 2px;
    height: 1em;
    background: #6366f1;
    margin-left: 2px;
    animation: avCursorBlink 0.6s step-end infinite;
    vertical-align: text-bottom;
}

/* ── "READ THIS CARD" BUTTON ──────────────────────────────── */
.av-read-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 0.7rem;
    font-weight: 700;
    color: var(--k12-accent, #6366f1);
    background: rgba(99,102,241,0.07);
    border: 1.5px solid rgba(99,102,241,0.25);
    border-radius: 2rem;
    padding: 0.3rem 0.75rem;
    cursor: pointer;
    transition: all 0.2s ease;
    margin-top: 0.5rem;
    pointer-events: auto;
}
.av-read-btn:hover {
    background: rgba(99,102,241,0.14);
    border-color: var(--k12-accent, #6366f1);
    transform: translateY(-1px);
    box-shadow: 0 3px 10px rgba(99,102,241,0.2);
}
.av-read-btn i { font-size: 0.75rem; }
.av-read-btn.reading {
    background: rgba(34,211,238,0.1);
    border-color: #22d3ee;
    color: #0891b2;
}

/* ── INTERACTION MENU ─────────────────────────────────────── */
.av-menu {
    position: absolute;
    left: 44px;
    top: -20px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s ease;
    z-index: 10;
}
.av-menu.open {
    opacity: 1;
    pointer-events: auto;
}
.av-menu-btn {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 54px;
    height: 54px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.9);
    color: #fff;
    font-size: 0.54rem;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 18px rgba(0,0,0,0.22);
    transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.3s ease, box-shadow 0.2s;
    transform: scale(0) translate(0,0);
    opacity: 0;
}
.av-menu-btn i { font-size: 1.1rem; margin-bottom: 2px; }
.av-menu-btn span { font-size: 0.42rem; letter-spacing: 0.02em; text-transform: uppercase; }
.av-menu-btn:hover { transform: scale(1.14) !important; box-shadow: 0 6px 24px rgba(0,0,0,0.3); }
.av-menu-btn:active { transform: scale(0.95) !important; }

.av-menu-doubt { background: linear-gradient(135deg, #6366f1, #4338ca); }
.av-menu-hint  { background: linear-gradient(135deg, #f59e0b, #d97706); }
.av-menu-motiv { background: linear-gradient(135deg, #ef4444, #dc2626); }

.av-menu.open .av-menu-doubt { transform: scale(1) translate(42px, -22px); opacity: 1; transition-delay: 0s; }
.av-menu.open .av-menu-hint  { transform: scale(1) translate(65px, 42px);  opacity: 1; transition-delay: 0.06s; }
.av-menu.open .av-menu-motiv { transform: scale(1) translate(42px, 106px); opacity: 1; transition-delay: 0.12s; }

/* ── LIVE COUNTER ─────────────────────────────────────────── */
.av-live-counter {
    position: absolute;
    bottom: -34px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(15,23,42,0.82);
    color: rgba(255,255,255,0.82);
    font-size: 0.46rem;
    font-weight: 700;
    padding: 0.15rem 0.55rem;
    border-radius: 0.75rem;
    white-space: nowrap;
    pointer-events: none;
    display: flex;
    align-items: center;
    gap: 0.3rem;
    backdrop-filter: blur(4px);
}
.av-live-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #22c55e;
    animation: avDotPulse 1.5s ease-in-out infinite;
}

/* ══ WORD HIGHLIGHT — karaoke TTS sync ══════════════════════ */
.av-word {
    display: inline;
    border-radius: 3px;
    transition: background 0.08s ease, color 0.08s ease, box-shadow 0.08s ease;
}
.av-word-active {
    background: rgba(99, 102, 241, 0.18) !important;
    color: #3730a3 !important;
    border-radius: 3px !important;
    box-shadow: 0 0 0 2px rgba(99,102,241,0.28) !important;
    font-weight: 700 !important;
}

/* ══ RIGHT ARM — pointing/gesturing ═════════════════════════ */
#avArmPivot {
    transform-origin: 0 0;
    /* 0.22s keeps arm responsive enough to track individual spoken words */
    transition: transform 0.22s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}
/* pointing — arm raised, finger extended toward content */
#liveAvatar.pointing #avArmPivot {
    transform: rotate(-70deg);
}
/* speaking — gentle continuous gesturing */
#liveAvatar.speaking #avArmPivot {
    animation: avArmSpeakGesture 1.5s ease-in-out infinite alternate;
}
/* waving */
#liveAvatar.waving #avArmPivot {
    animation: avArmWaveAnim 0.42s ease-in-out infinite alternate;
}
/* celebrating — arm pumps up */
#liveAvatar.celebrating #avArmPivot {
    animation: avArmCelebrateAnim 0.46s ease-in-out infinite alternate;
}
/* thinking — arm half raised */
#liveAvatar.thinking #avArmPivot {
    transform: rotate(-22deg);
}
/* Show pointing finger during point & speak */
#liveAvatar.pointing #avPointFinger,
#liveAvatar.speaking #avPointFinger {
    opacity: 1 !important;
}
/* Pulsing glow at fingertip */
#liveAvatar.pointing #avFingerGlow,
#liveAvatar.speaking #avFingerGlow {
    animation: avFingerGlowPulse 0.72s ease-in-out infinite alternate;
}

/* ══ STATE ANIMATIONS ══════════════════════════════════════ */

#liveAvatar.waving #avFacePanel {
    animation: avWave 0.48s ease 4 alternate, avFloat 4.2s ease-in-out infinite;
}
#liveAvatar.celebrating #avFacePanel {
    animation: avCelebrate 0.52s ease 3, avFloat 4.2s ease-in-out infinite;
}
#liveAvatar.pointing #avFacePanel {
    animation: avPoint 0.65s ease 2 alternate, avFloat 4.2s ease-in-out infinite;
}
#liveAvatar.thinking #avFacePanel {
    animation: avThink 2.2s ease-in-out infinite;
}
#liveAvatar.speaking #avFacePanel {
    animation: avSpeak 0.65s ease-in-out infinite alternate, avFloat 4.2s ease-in-out infinite;
}
#liveAvatar.walking #avFacePanel {
    animation: avWalkIn 0.65s cubic-bezier(0.34, 1.56, 0.64, 1), avFloat 4.2s ease-in-out infinite;
}
/* Avatar walks/runs across the screen to a new position */
#liveAvatar.moving #avFacePanel {
    animation: avWalkBounce 0.38s ease-in-out infinite !important;
}

/* ══ KEYFRAMES ══════════════════════════════════════════════ */
@keyframes avFloat {
    0%, 100% { transform: translateY(0); }
    50%       { transform: translateY(-5px); }
}
@keyframes avGlowPulse {
    0%, 100% { box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--k12-accent,#6366f1), 0 8px 32px rgba(99,102,241,0.3), 0 2px 8px rgba(0,0,0,0.1); }
    50%       { box-shadow: 0 0 0 3px #fff, 0 0 0 5px var(--k12-accent,#6366f1), 0 8px 40px rgba(99,102,241,0.5), 0 2px 10px rgba(0,0,0,0.14); }
}
@keyframes avSpeakGlow {
    from { box-shadow: 0 0 0 3px #fff, 0 0 0 5px #22d3ee, 0 0 22px rgba(34,211,238,0.5), 0 8px 28px rgba(99,102,241,0.35); }
    to   { box-shadow: 0 0 0 3px #fff, 0 0 0 7px #22d3ee, 0 0 38px rgba(34,211,238,0.72), 0 8px 36px rgba(99,102,241,0.45); }
}
@keyframes avBarBounce {
    from { height: 4px; }
    to   { height: 16px; }
}
@keyframes avDotPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(34,197,94,0.5); }
    50%       { box-shadow: 0 0 0 5px rgba(34,197,94,0); }
}
@keyframes avWave {
    from { transform: rotate(-10deg) translateY(-3px); }
    to   { transform: rotate(10deg) translateY(-3px); }
}
@keyframes avCelebrate {
    0%, 100% { transform: translateY(0) scale(1); }
    50%       { transform: translateY(-12px) scale(1.08); }
}
@keyframes avPoint {
    from { transform: translateX(0) rotate(0deg); }
    to   { transform: translateX(8px) rotate(3deg); }
}
@keyframes avThink {
    0%, 100% { transform: rotate(0deg) translateY(0); }
    40%       { transform: rotate(6deg) translateY(-3px); }
    70%       { transform: rotate(-3deg) translateY(-1px); }
}
@keyframes avSpeak {
    from { transform: scale(1); }
    to   { transform: scale(1.03) translateY(-1px); }
}
@keyframes avWalkIn {
    from { transform: translateY(50px) scale(0.6); opacity: 0; }
    to   { transform: translateY(0) scale(1); opacity: 1; }
}
@keyframes avWalkBounce {
    0%, 50%, 100% { transform: translateY(0) rotate(0deg); }
    25%            { transform: translateY(-9px) rotate(-5deg); }
    75%            { transform: translateY(-9px) rotate(5deg); }
}
@keyframes avCursorBlink {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}
@keyframes avArmSpeakGesture {
    from { transform: rotate(-28deg); }
    to   { transform: rotate(-50deg); }
}
@keyframes avArmWaveAnim {
    from { transform: rotate(-44deg); }
    to   { transform: rotate(-78deg); }
}
@keyframes avArmCelebrateAnim {
    from { transform: rotate(-85deg); }
    to   { transform: rotate(-112deg); }
}
@keyframes avFingerGlowPulse {
    from { opacity: 0.18; }
    to   { opacity: 0.62; }
}

/* ══ SEGMENT THEMING ════════════════════════════════════════ */
[data-segment="primary"] #av3dBadge { background: #d97706; }
[data-segment="primary"] .av-speech { font-size: 0.95rem !important; }
[data-segment="primary"] .av-speech-step { background: rgba(245,158,11,0.12); color: #b45309; }
[data-segment="primary"] #avFaceWrap {
    box-shadow: 0 0 0 3px #fff, 0 0 0 5px #f59e0b, 0 8px 32px rgba(245,158,11,0.3), 0 2px 8px rgba(0,0,0,0.1);
}
[data-segment="secondary"] #av3dBadge { background: #15803d; }
[data-segment="secondary"] .av-speech-step { background: rgba(34,197,94,0.1); color: #15803d; }
[data-segment="secondary"] #avFaceWrap {
    box-shadow: 0 0 0 3px #fff, 0 0 0 5px #22c55e, 0 8px 32px rgba(34,197,94,0.28), 0 2px 8px rgba(0,0,0,0.1);
}
[data-segment="senior"] #av3dBadge { background: #5b21b6; }
[data-segment="senior"] .av-speech-step { background: rgba(124,58,237,0.1); color: #5b21b6; }
[data-segment="senior"] #avFaceWrap {
    box-shadow: 0 0 0 3px #fff, 0 0 0 5px #7c3aed, 0 8px 32px rgba(124,58,237,0.32), 0 2px 8px rgba(0,0,0,0.1);
}

/* ══ RESPONSIVE ═════════════════════════════════════════════ */
@media (max-width: 991px) {
    #liveAvatar { bottom: 5rem !important; left: 0.5rem; }
    #avFacePanel { width: 126px; }
    #avFaceWrap  { width: 126px; height: 126px; }
    .av-speech { max-width: 260px !important; font-size: 0.83rem !important; left: 138px !important; }
    .av-menu-btn { width: 48px; height: 48px; }
    .av-menu.open .av-menu-doubt { transform: scale(1) translate(36px, -18px); }
    .av-menu.open .av-menu-hint  { transform: scale(1) translate(56px, 36px); }
    .av-menu.open .av-menu-motiv { transform: scale(1) translate(36px, 90px); }
}
@media (max-width: 575px) {
    #liveAvatar { bottom: 4.5rem !important; left: 0.3rem; }
    #avFacePanel { width: 100px; }
    #avFaceWrap  { width: 100px; height: 100px; }
    .pa-reaction { font-size: 1.2rem; top: -10px; right: -4px; }
    .av-speech { max-width: 210px !important; font-size: 0.78rem !important; left: 110px !important; top: 4px !important; padding: 0.7rem 0.9rem !important; }
    .av-menu-btn { width: 44px; height: 44px; }
    .av-menu.open .av-menu-doubt { transform: scale(1) translate(28px, -14px); }
    .av-menu.open .av-menu-hint  { transform: scale(1) translate(44px, 28px); }
    .av-menu.open .av-menu-motiv { transform: scale(1) translate(28px, 70px); }
    #avVoiceToggle { width: 18px; height: 18px; font-size: 0.45rem; }
}

/* ══ REDUCED MOTION ══════════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
    #liveAvatar { transition: opacity 0.2s ease !important; }
    #avFacePanel, #avFaceWrap { animation: none !important; }
    .av-typewriter .av-cursor { animation: none !important; }
    #avWaveBars span { animation: none !important; }
}

/* ══ LEGACY: hide old photo elements ════════════════════════ */
.av-bubble, .av-face, .av-tag, .av-pointer,
.pa-glow, .pa-photo-ring, #photoAvatarWrap { display: none !important; }
