/*
 * avatar.css
 * AIロールプレイ トレーニング画面のアバター/チャットUI。
 *
 * 【位置ずれ対策の方針（非常に重要）】
 *  - アバターの表示領域 (.avatar-image-wrapper) は固定サイズにする。
 *  - 画像 (.avatar-image) は width/height 100% + object-fit:cover で統一する。
 *  - 画像切り替え時にレイアウトが動かないよう、img は position:absolute で重ねる。
 *  - transform による拡大縮小やフェード(transition)は使わない。src 切り替えのみ。
 *
 * 【将来拡張メモ】
 *  - 顔位置補正済み画像へ差し替える場合も、この wrapper の固定サイズ思想は維持する。
 *  - Live2D / Talking Head Avatar に差し替える際は .avatar-image-wrapper の中身を
 *    canvas / video に置き換えれば、周辺レイアウトはそのまま流用できる。
 */

/* ===== ページ全体（淡いグラデーション背景） ===== */
.training-page {
    background: linear-gradient(160deg, #f5f7fb 0%, #eef1f8 100%);
    border-radius: 20px;
    padding: 1.25rem;
}

/* ===== 2カラムレイアウト ===== */
.training-layout {
    display: flex;
    flex-wrap: wrap;
    gap: 1.25rem;
    align-items: stretch;
}

.training-layout > .avatar-panel {
    flex: 0 0 380px;
    max-width: 100%;
}

.training-layout > .chat-panel {
    flex: 1 1 420px;
    min-width: 0; /* flex子要素のはみ出し防止 */
    display: flex;
}

/* ===== アバターパネル ===== */
.avatar-panel {
    display: flex;
}

.avatar-card {
    background: #ffffff;
    border-radius: 24px;
    box-shadow: 0 10px 30px rgba(33, 43, 79, 0.08);
    padding: 1.25rem;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.avatar-card .avatar-name {
    font-weight: 600;
    color: #1f2a44;
    margin: 0;
}

.avatar-card .avatar-role {
    color: #6b7591;
    font-size: 0.85rem;
}

/* 画像の表示領域：固定サイズ（ここが位置ずれ対策の要） */
.avatar-image-wrapper {
    position: relative;
    width: 320px;
    height: 320px;
    max-width: 100%;
    aspect-ratio: 1 / 1;
    overflow: hidden;
    border-radius: 24px;
    background: #f0f2f7;
}

/* 画像：wrapper いっぱいに敷き、object-fit を統一。絶対配置で重ねる */
.avatar-image {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
    user-select: none;
    pointer-events: none;
    /* クロスフェード用。下のレイヤーは不透明のまま維持し、上のレイヤーだけを
       0→1 でフェードインさせる（JS 側で制御）。これにより中間で背景が透けず、
       白いちらつきが起きない。位置は inset:0 固定なのでずれは発生しない。
       背景色を wrapper と同色にして、万一の隙間でも白く飛ばないようにする。 */
    background-color: #f0f2f7;
    opacity: 1;
    transition: opacity 90ms linear;
    will-change: opacity;
    backface-visibility: hidden;
    transform: translateZ(0);
}

/* クロスフェードの「奥」レイヤー（前フレーム）。JS が生成・管理する。 */
.avatar-image.avatar-image-back {
    opacity: 0;
}

/* モーション低減設定のユーザーにはフェードを無効化する。 */
@media (prefers-reduced-motion: reduce) {
    .avatar-image {
        transition: none;
    }
}

/* ステータス表示（待機中 / 考え中... / 話しています） */
.avatar-status {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.35rem 0.9rem;
    border-radius: 999px;
    font-size: 0.85rem;
    font-weight: 500;
    color: #4a557a;
    background: #eef1f8;
    min-height: 32px;
}

.avatar-status .status-dot {
    width: 9px;
    height: 9px;
    border-radius: 50%;
    background: #adb5c9;
    flex: 0 0 auto;
}

.avatar-status.is-thinking {
    color: #8a6d00;
    background: #fff4d6;
}

.avatar-status.is-thinking .status-dot {
    background: #f0ad4e;
    animation: avatar-pulse 1s ease-in-out infinite;
}

.avatar-status.is-speaking {
    color: #0a5b8a;
    background: #dceefb;
}

.avatar-status.is-speaking .status-dot {
    background: #2f93d6;
    animation: avatar-pulse 0.8s ease-in-out infinite;
}

@keyframes avatar-pulse {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.7); }
}

/* ===== チャットパネル ===== */
.chat-panel {
    width: 100%;
}

.chat-card {
    background: #ffffff;
    border-radius: 24px;
    box-shadow: 0 10px 30px rgba(33, 43, 79, 0.08);
    width: 100%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.chat-card-header {
    padding: 1rem 1.25rem;
    border-bottom: 1px solid #eef1f6;
}

.chat-card-header h1 {
    font-size: 1.1rem;
    margin: 0;
    color: #1f2a44;
}

.chat-card-header small {
    color: #8a93ad;
}

/* メッセージ表示エリア */
.chat-messages {
    flex: 1 1 auto;
    overflow-y: auto;
    padding: 1.25rem;
    background: #f8f9fc;
    min-height: 320px;
    max-height: 52vh;
}

.chat-message {
    display: flex;
    margin-bottom: 0.85rem;
}

.chat-message .chat-bubble {
    max-width: 78%;
    padding: 0.6rem 0.9rem;
    border-radius: 16px;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
    box-shadow: 0 2px 6px rgba(33, 43, 79, 0.05);
}

/* AIメッセージ：左寄せ・淡いブルー系 */
.chat-message-ai {
    justify-content: flex-start;
}

.chat-message-ai .chat-bubble {
    background: #e7f1fd;
    color: #18324b;
    border-top-left-radius: 4px;
}

/* ユーザーメッセージ：右寄せ・淡いグリーン系 */
.chat-message-user {
    justify-content: flex-end;
}

.chat-message-user .chat-bubble {
    background: #e3f4ea;
    color: #1c3b2a;
    border-top-right-radius: 4px;
}

.chat-bubble .tts-btn {
    padding: 0;
    margin-top: 0.25rem;
    font-size: 0.8rem;
    text-decoration: none;
    color: #2f93d6;
}

.chat-bubble .tts-btn:hover {
    text-decoration: underline;
}

/* ===== 入力エリア ===== */
.training-input-area {
    padding: 0.9rem 1.25rem;
    border-top: 1px solid #eef1f6;
    background: #ffffff;
}

.training-input-area .input-group .form-control {
    border-radius: 0;
}

.training-input-area .input-group > .btn:first-child {
    border-top-left-radius: 999px;
    border-bottom-left-radius: 999px;
}

.training-input-area .input-group > .btn:last-child {
    border-top-right-radius: 999px;
    border-bottom-right-radius: 999px;
}

.training-input-area .input-group > .form-control:first-child {
    border-top-left-radius: 999px;
    border-bottom-left-radius: 999px;
}

.training-input-area .mic-btn.is-recording {
    color: #fff;
    background: #e05260;
    border-color: #e05260;
}

.training-input-area .input-hint {
    margin-top: 0.4rem;
    font-size: 0.78rem;
    color: #9aa2b8;
}

/* ===== レスポンシブ（スマホ） ===== */
@media (max-width: 768px) {
    .training-layout > .avatar-panel,
    .training-layout > .chat-panel {
        flex: 1 1 100%;
    }

    .avatar-image-wrapper {
        width: 240px;
        height: 240px;
    }

    .chat-messages {
        max-height: 44vh;
    }

    .training-page {
        padding: 0.75rem;
    }
}

/* ===== カウントダウンタイマー ===== */
.training-timer {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
    background: #eef2ff;
    color: #4338ca;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    padding: 0.45rem 0.9rem;
    border-radius: 999px;
    box-shadow: 0 4px 14px rgba(30, 41, 89, 0.06);
    white-space: nowrap;
}

.training-timer .training-timer-icon {
    font-size: 1.05rem;
    line-height: 1;
}

.training-timer .training-timer-text {
    font-size: 1.05rem;
    letter-spacing: 0.02em;
}

.training-timer.is-warning {
    background: #fff7ed;
    color: #c2410c;
    animation: ts-timer-pulse 1s ease-in-out infinite;
}

.training-timer.is-overtime {
    background: #fef2f2;
    color: #dc2626;
}

@keyframes ts-timer-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.06); }
}
