/* ==========================================
   char_bot チャット画面 スタイル
   ========================================== */

*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: 'Noto Sans JP', sans-serif;
}

/* 全画面アプリルート */
#app {
    position: relative;
    width: 100%;
    max-width: 100vw;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    overflow-x: hidden;
}

/* 背景 */
#background {
    position: fixed;
    inset: 0;
    background: linear-gradient(135deg, #a8edea, #fed6e3);
    background-size: cover;
    background-position: center;
    z-index: 0;
}

/* アバターコンテナ（常に画面高の約95%。contain+max-width:100vw だと狭い画面で高さが潰れるため高さ優先） */
#avatar-container {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 100%;
    max-width: 100vw;
    height: 95vh;
    height: 95dvh;
    z-index: 10;
    pointer-events: none;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

#avatar-stack {
    position: relative;
    width: 100%;
    height: 100%;
}

.avatar-layer {
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    height: 100%;
    width: auto;
    max-width: none;
    object-fit: contain;
    object-position: bottom center;
}

#avatar-placeholder {
    width: 180px;
    height: 250px;
    background: rgba(255,255,255,0.2);
    border-radius: 12px;
    border: 2px dashed rgba(255,255,255,0.5);
}

/* UIコンテナ */
#ui-container {
    position: fixed;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: min(600px, 100%);
    max-width: min(600px, 100vw);
    z-index: 20;
    padding: 0 max(8px, env(safe-area-inset-right, 8px)) max(12px, env(safe-area-inset-bottom, 0px))
             max(8px, env(safe-area-inset-left, 8px));
    box-sizing: border-box;
}

/* チャットウィンドウ */
#chat-window {
    background: rgba(0, 0, 0, 0.72);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -4px 30px rgba(0,0,0,0.4);
    overflow: hidden;
    border: 1px solid rgba(255,255,255,0.12);
    border-bottom: none;
    display: flex;
    flex-direction: column;
    min-width: 0;
    max-width: 100%;
}

/* キャラクター名 */
#character-name {
    background: linear-gradient(135deg, rgba(102,126,234,0.6), rgba(118,75,162,0.6));
    color: white;
    font-size: 0.85rem;
    font-weight: 700;
    padding: 6px 12px 6px 16px;
    letter-spacing: 0.05em;
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

/* メッセージボックス */
#message-box {
    padding: 12px 16px 8px;
    min-height: 70px;
    max-height: 140px;
    overflow-y: auto;
    overflow-x: hidden;
    position: relative;
    min-width: 0;
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.3) transparent;
}

#message-box::-webkit-scrollbar {
    width: 4px;
}
#message-box::-webkit-scrollbar-track { background: transparent; }
#message-box::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.3); border-radius: 4px; }

#message-text {
    font-size: 1.1rem;
    line-height: 1.5;
    color: rgba(255,255,255,0.95);
    word-break: break-word;
    overflow-wrap: anywhere;
    max-width: 100%;
}
#message-text ul {
    margin: 6px 0;
    padding-left: 1.4em;
    list-style-type: disc;
}
#message-text li {
    margin: 4px 0;
}
#message-text h1, #message-text h2, #message-text h3 {
    margin: 1em 0 0.35em;
    font-weight: bold;
    line-height: 1.4;
}
#message-text h1:first-child,
#message-text h2:first-child,
#message-text h3:first-child {
    margin-top: 0;
}
#message-text h1 { font-size: 1.15rem; }
#message-text h2 { font-size: 1.05rem; }
#message-text h3 { font-size: 0.98rem; }
#message-text code {
    background: rgba(255,255,255,0.15);
    border-radius: 3px;
    padding: 0 4px;
    font-family: monospace;
    font-size: 0.88rem;
}
#message-text a {
    color: #90caf9;
    text-decoration: underline;
    word-break: break-all;
}
#message-text a:hover {
    color: #bbdefb;
}

/* タイピングカーソル */
#cursor {
    display: none;
    font-size: 0.7rem;
    color: rgba(255,255,255,0.7);
    animation: blink-cursor 0.8s infinite;
    text-align: right;
    padding-top: 4px;
}

@keyframes blink-cursor {
    0%, 100% { opacity: 1; }
    50%       { opacity: 0; }
}

/* 入力エリア */
#input-area {
    display: flex;
    align-items: flex-end;
    flex-wrap: wrap;
    gap: 8px;
    padding: 8px 12px 12px;
    border-top: 1px solid rgba(255,255,255,0.1);
    background: rgba(255,255,255,0.05);
    flex-shrink: 0;
}

/* テキストエリア */
#transcript-box {
    flex: 1;
    border: 1.5px solid rgba(255,255,255,0.2);
    border-radius: 12px;
    padding: 8px 12px;
    font-size: 0.9rem;
    font-family: inherit;
    color: white;
    background: rgba(255,255,255,0.1);
    resize: none;
    outline: none;
    line-height: 1.5;
    max-height: 100px;
    overflow-y: auto;
    transition: border-color 0.2s;
}

#transcript-box::placeholder {
    color: rgba(255,255,255,0.4);
}

#transcript-box:focus {
    border-color: rgba(255,255,255,0.5);
}

/* アイコンボタン */
.icon-button {
    flex-shrink: 0;
    width: 42px;
    height: 42px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    padding: 0;
}

/* マイクボタン */
#mic-button {
    background: rgba(255,255,255,0.12);
    color: rgba(255,255,255,0.85);
}

#mic-button:hover {
    background: rgba(255,255,255,0.25);
}

#mic-button.listening {
    background: #ff5252;
    color: white;
    animation: pulse-mic 1s infinite;
}

@keyframes pulse-mic {
    0%   { box-shadow: 0 0 0 0 rgba(255,82,82,0.5); }
    70%  { box-shadow: 0 0 0 8px rgba(255,82,82,0); }
    100% { box-shadow: 0 0 0 0 rgba(255,82,82,0); }
}

/* 送信ボタン */
#send-button {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
}

#send-button:hover {
    transform: scale(1.08);
    box-shadow: 0 4px 12px rgba(102,126,234,0.4);
}

#send-button:disabled {
    background: #ccc;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

/* ローディングインジケータ */
.thinking-dots::after {
    content: '...';
    animation: thinking 1.2s infinite;
}

@keyframes thinking {
    0%   { content: '.'; }
    33%  { content: '..'; }
    66%  { content: '...'; }
    100% { content: '.'; }
}

/* 全画面展開ボタン（スマホのみ表示） */
.expand-btn {
    display: none;
    background: transparent;
    border: none;
    color: rgba(255,255,255,0.75);
    cursor: pointer;
    padding: 4px;
    border-radius: 6px;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: color 0.2s;
}

.expand-btn:hover {
    color: white;
}

/* PCレイアウト (769px以上): 左側・高さ95% */
@media (min-width: 769px) {
    #ui-container {
        left: 20px;
        top: 2.5vh;
        bottom: auto;
        transform: none;
        width: 360px;
        height: 95vh;
        padding: 0;
    }

    #chat-window {
        height: 100%;
        border-radius: 16px;
        border: 1px solid rgba(255,255,255,0.15);
        box-shadow: 4px 8px 32px rgba(0,0,0,0.5);
    }

    #message-box {
        flex: 1;
        min-height: 0;
        max-height: none;
    }

    .expand-btn {
        display: none !important;
    }
}

/* スマホ (768px以下): 下部固定 */
@media (max-width: 768px) {
    /* translateX(-50%) + width100% の組み合わせで端末によっては 1px ずれやはみ出しが出るため左固定 */
    #ui-container {
        left: 0;
        right: 0;
        transform: none;
        width: 100%;
        max-width: 100%;
        padding: 0 max(12px, env(safe-area-inset-right, 0px)) max(8px, env(safe-area-inset-bottom, 8px)) max(12px, env(safe-area-inset-left, 0px));
        transition: top 0.3s ease, height 0.3s ease, border-radius 0.3s ease;
    }

    /* キャラは共通スタイル（100vh・contain）のまま。文字まわりだけスマホ最適化を維持 */

    #chat-window {
        border-radius: 16px 16px 0 0;
    }

    #message-box {
        min-height: 72px;
        max-height: min(28vh, 200px);
        padding: 12px 14px 8px;
    }

    #message-text {
        font-size: clamp(0.95rem, 3.8vw, 1.05rem);
        line-height: 1.55;
    }

    #character-name {
        padding: 6px 12px 6px 14px;
    }

    #input-area {
        padding: 8px 10px max(10px, env(safe-area-inset-bottom, 10px));
    }

    .expand-btn {
        display: flex;
    }
}

/* スマホ 全画面展開状態 */
@media (max-width: 768px) {
    #ui-container.expanded {
        top: 0;
        bottom: auto;
        left: 0;
        transform: none;
        width: 100%;
        height: 100svh;
        height: 100dvh;
        padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px)
                 env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
        box-sizing: border-box;
    }

    #ui-container.expanded #chat-window {
        height: 100%;
        border-radius: 0;
        border: none;
    }

    #ui-container.expanded #message-box {
        flex: 1;
        min-height: 0;
        max-height: none;
    }
}

/* 音声ON/OFFトグルボタン */
#voice-toggle {
    position: fixed;
    top: 14px;
    right: 14px;
    z-index: 100;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: 0 2px 10px rgba(0,0,0,0.18);
    transition: background 0.2s, box-shadow 0.2s;
    color: #667eea;
}

#voice-toggle:hover {
    background: rgba(255, 255, 255, 0.98);
    box-shadow: 0 4px 16px rgba(0,0,0,0.22);
}

#voice-toggle.voice-off {
    color: #aaa;
    background: rgba(240, 240, 240, 0.9);
}

/* スマホでは左上に移動（展開/縮小ボタンとの重なり防止） */
@media (max-width: 768px) {
    #voice-toggle {
        left: 14px;
        right: auto;
    }
}

/* エラートースト */
#toast {
    position: fixed;
    top: 20px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0,0,0,0.8);
    color: white;
    padding: 10px 20px;
    border-radius: 20px;
    font-size: 0.85rem;
    z-index: 9999;
    display: none;
    white-space: nowrap;
}

/* ファイル添付ボタン */
#attach-button {
    background: rgba(255,255,255,0.12);
    color: rgba(255,255,255,0.85);
}

#attach-button:hover {
    background: rgba(255,255,255,0.25);
}

/* 添付プレビューエリア */
#attach-preview {
    display: none;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    background: rgba(102,126,234,0.25);
    border-radius: 10px;
    border: 1px solid rgba(102,126,234,0.4);
    width: 100%;
    order: -1;
    flex-wrap: nowrap;
    overflow: hidden;
}

.attach-thumb {
    width: 36px;
    height: 36px;
    object-fit: cover;
    border-radius: 6px;
    flex-shrink: 0;
}

.attach-icon {
    font-size: 1.3rem;
    flex-shrink: 0;
}

.attach-name {
    flex: 1;
    font-size: 0.78rem;
    color: rgba(255,255,255,0.9);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.attach-clear {
    flex-shrink: 0;
    background: rgba(255,255,255,0.15);
    border: none;
    color: rgba(255,255,255,0.8);
    border-radius: 50%;
    width: 22px;
    height: 22px;
    font-size: 0.85rem;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.2s;
}

.attach-clear:hover {
    background: rgba(255,82,82,0.4);
    color: white;
}
