/* Overall Container */
#rtp-chat-container {
    display: flex;
    flex-direction: column; /* Start with column for mobile-first */
    font-family: sans-serif;
    height: auto;
    /* min-height: 85vh; */
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    /* ▼▼▼ 修正: スマホ表示ではみ出しを許可 ▼▼▼ */
    overflow: visible;
    /* ▲▲▲ 修正ここまで ▲▲▲ */
    max-width: none;
    width: 97vw;
    position: relative;
    left: 50%;
    transform: translateX(-50%);
}

/* New wrapper for main chat area */
#chat-main-wrapper {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    height: auto;
    /* ▼▼▼ 修正: スマホ表示ではみ出しを許可 ▼▼▼ */
    overflow: visible;
    /* ▲▲▲ 修正ここまで ▲▲▲ */
}

/* Left Sidebar */
#chat-history-sidebar {
    width: 100%;
    background-color: #f5f5f5;
    border-bottom: 1px solid #e0e0e0;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    height: 250px;
    transition: width 0.3s ease, padding 0.3s ease, height 0.3s ease;
}

#sidebar-header, #chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid #e0e0e0;
    flex-shrink: 0;
    background-color: #CAE0CD;
}

#chat-header {
    padding: 10px 20px;
    transition: padding-left 0.3s ease;
}

#sidebar-title { margin: 0; font-size: 16px; font-weight: bold; }

#minimize-sidebar-btn {
    background: none; border: none; cursor: pointer; padding: 5px; line-height: 0;
    display: none; /* Hidden on mobile by default */
}

.new-chat-btn {
    margin: 10px;
    border: 1px solid #ccc;
    border-radius: 5px;
    cursor: pointer;
    background: #fff;
    flex-shrink: 0;
    height: 42px;
    padding: 0 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
}

#history-list { flex-grow: 1; overflow-y: auto; padding: 0 10px; }

#history-list p { color: #666; }

/* Right Main Area */
#chat-main-area {
    flex-grow: 1; display: flex; flex-direction: column;
    /* background: #CAE0CD; */
    /* ▼▼▼ 修正: スマホ表示ではみ出しを許可 ▼▼▼ */
    overflow: visible;
    /* ▲▲▲ 修正ここまで ▲▲▲ */
}

#chat-messages {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
    /* ▼▼▼ 修正: スマホでコンテナが伸びるようにする ▼▼▼ */
    flex-grow: 1;
    /* ▲▲▲ 修正ここまで ▲▲▲ */
    background-color: #fff;
}

/* Chat Bubbles */
.message-bubble {
    padding: 10px 15px; border-radius: 18px; max-width: 85%;
    line-height: 1.5; position: relative; word-wrap: break-word;
}

.message-bubble.user { background-color: #dcf8c6; align-self: flex-end; border-bottom-right-radius: 4px; }
.message-bubble.ai {
    background-color: transparent;
    align-self: stretch;
    max-width: 100%;
    padding: 0;
    border-radius: 0;
}
.message-bubble p { margin: 0; }
.message-bubble.error { background-color: #ffebee; color: #c62828; }

/* Input Footer */
#chat-footer {
    padding: 10px 15px; border-top: 1px solid #e0e0e0; flex-shrink: 0;
    background-color: #CAE0CD;
    position: sticky;
    bottom: 0;
}

#chat-input-wrapper {
    border: 1px solid #ccc;
    border-radius: 8px;
    padding: 5px 10px;
    background-color: #fff;
}

#chat-input {
    width: 100%; border: none; outline: none; resize: none;
    font-size: 16px; background: transparent;
    min-height: 64px;
    line-height: 1.4;
}

/* 添付・送信ボタンエリアのスタイル */
#chat-submit-area {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-top: 8px;
}

#attach-file-btn, #send-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 1px solid transparent;
    border-radius: 5px;
    cursor: pointer;
    font-size: 14px;
    height: 42px;
    padding: 0 15px;
    box-sizing: border-box;
}

#attach-file-btn {
    background-color: #f0f0f0;
    color: #333;
}

#send-btn {
    background-color: #11351C;
    color: white;
}

#send-btn:disabled {
    background-color: #a0a0a0;
}

/* --- History List Styles --- */
.history-item { padding: 10px; cursor: pointer; border-bottom: 1px solid #e0e0e0; transition: background-color 0.2s; }
.history-item:hover { background-color: #e9e9e9; }
.history-item.active { background-color: #d1e7fd; font-weight: bold; }
.history-item p { margin: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #333; }

/* --- Right-click Menu Styles --- */
.custom-context-menu { position: absolute; z-index: 1000; background-color: #fff; border: 1px solid #ccc; box-shadow: 0 2px 5px rgba(0,0,0,0.2); border-radius: 5px; padding: 5px 0; }
.custom-context-menu ul { list-style: none; padding: 0; margin: 0; }
.custom-context-menu ul li { padding: 8px 15px; cursor: pointer; }
.custom-context-menu ul li:hover { background-color: #f5f5f5; }

/* --- Chat Action Button Styles --- */
#chat-actions-container {
    display: flex;
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 10px;
    padding-top: 10px;
    margin-top: 5px;
    border-top: 1px solid #e0e0e0;
}

#copy-chat-btn, .footer-btn, #scroll-to-top-btn {
    font-size: 12px;
    border: 1px solid #ccc;
    background-color: #f5f5f5;
    cursor: pointer;
    border-radius: 4px;
    transition: background-color 0.2s;
    height: 42px;
    padding: 0 10px;
    margin: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
}

#copy-chat-btn:hover, .footer-btn:hover, #scroll-to-top-btn:hover {
    background-color: #e0e0e0;
}

#copy-chat-btn.copied { background-color: #4CAF50; color: white; border-color: #4CAF50; }

/* --- File Upload Related Styles --- */
#file-preview-container { padding: 10px 0; font-size: 14px; }
.file-preview-item { display: inline-flex; align-items: center; background-color: #e9e9e9; border-radius: 15px; padding: 5px 10px; gap: 8px; }
.file-preview-item .remove-file { cursor: pointer; font-weight: bold; color: #888; }

/* --- Message Editing Feature Styles (No changes) --- */
.message-bubble .message-actions{position:absolute;top:8px;right:8px;display:flex;gap:5px;opacity:0;transition:opacity .2s}
.message-bubble:hover .message-actions{opacity:1}
.message-actions button{width:26px;height:26px;border:none;background:rgba(0,0,0,.08);color:#555;cursor:pointer;border-radius:50%;display:flex;align-items:center;justify-content:center;transition:background-color .2s}
.message-actions button:hover{background:rgba(0,0,0,.15)}
.message-bubble .edit-message-btn{position:absolute;top:-10px;left:-15px;width:26px;height:26px;border:1px solid #ddd;background:#fff;color:#555;cursor:pointer;border-radius:50%;display:flex;align-items:center;justify-content:center;transition:all .2s;box-shadow:0 1px 3px rgba(0,0,0,.1);opacity:0;z-index:10;pointer-events:none}
.message-bubble.user.can-edit:hover .edit-message-btn{opacity:1;pointer-events:auto}
.message-bubble .edit-message-btn:hover{background:#f0f0f0;transform:scale(1.1)}
.message-bubble .edit-message-btn svg{width:14px;height:14px}
.message-bubble .edit-controls{margin-top:10px;display:flex;gap:10px}
.message-bubble .edit-controls button{font-size:12px;padding:4px 8px;border:1px solid #ccc;border-radius:4px;cursor:pointer}
.message-bubble .edit-controls button.save{background-color:#28a745;color:white;border-color:#28a745}
.message-bubble textarea.edit-input{width:100%;border:1px solid #ccc;border-radius:4px;padding:5px;font-family:inherit;font-size:inherit;resize:vertical}

/* --- Desktop Layout (768px and wider) --- */
@media (min-width: 768px) {
    #rtp-chat-container {
        flex-direction: row;
        height: 75vh;
    }

    #chat-main-wrapper { height: 100%; }

    /* ▼▼▼ 修正: 親要素のはみ出しを禁止し、中のスクロールを有効にする ▼▼▼ */
    #chat-main-area {
        overflow: hidden;
    }
    /* ▲▲▲ 修正ここまで ▲▲▲ */

    #chat-messages {
        flex-grow: 1;
        overflow-y: auto;
    }

    #chat-history-sidebar {
        width: 260px;
        height: 100%;
        border-right: 1px solid #e0e0e0;
        border-bottom: none;
        transition: width 0.3s ease;
    }

    #chat-footer {
        position: relative;
    }

    #minimize-sidebar-btn {
        display: block;
        position: absolute;
        top: 10px;
        left: 232px;
        z-index: 100;
        transition: left 0.3s ease, transform 0.3s ease;
    }

    #rtp-chat-container.sidebar-minimized #chat-history-sidebar {
        width: 0;
        padding: 0;
        border: none;
        overflow: hidden;
    }

    #rtp-chat-container.sidebar-minimized #minimize-sidebar-btn {
        left: 10px;
        transform: rotate(180deg);
    }

    #rtp-chat-container.sidebar-minimized #sidebar-header #sidebar-title,
    #rtp-chat-container.sidebar-minimized .new-chat-btn,
    #rtp-chat-container.sidebar-minimized #history-list {
        display: none;
    }
}
