:root {
    --bg-color: #ffffff;
    --header-bg: #ffffff;
    --header-border: #f0f0f0;
    --text-color: #050505;
    --sub-text-color: #888888;
    --chat-bg: #ffffff;
    --bot-msg-bg: #f0f2f5;
    --bot-msg-text: #050505;
    --input-area-bg: #ffffff;
    --input-bg: #f0f2f5;
    --input-text: #050505;
    --primary-color: #0097a7;
    --send-btn-text: #ffffff;
    --icon-hover: #f0f2f5;
    --sidebar-bg: #f7f7f8;
    --overlay-bg: rgba(0, 0, 0, 0.5);
    --sidebar-item-active: #e0e0e0;
    --modal-bg: #ffffff;
}

body.dark-mode {
    --bg-color: #18191a;
    --header-bg: #242526;
    --header-border: #3e4042;
    --text-color: #e4e6eb;
    --sub-text-color: #b0b3b8;
    --chat-bg: #18191a;
    --bot-msg-bg: #3a3b3c;
    --bot-msg-text: #e4e6eb;
    --input-area-bg: #242526;
    --input-bg: #3a3b3c;
    --input-text: #e4e6eb;
    --icon-hover: #3a3b3c;
    --sidebar-bg: #202123;
    --overlay-bg: rgba(0, 0, 0, 0.7);
    --sidebar-item-active: #343541;
    --modal-bg: #242526;
}

* { box-sizing: border-box; margin: 0; padding: 0; outline: none; }

body {
    font-family: 'Segoe UI', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    height: 100dvh; 
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background-color 0.3s, color 0.3s;
}

/* --- Header --- */
header {
    background: var(--header-bg);
    padding: 15px;
    text-align: center;
    border-bottom: 1px solid var(--header-border);
    flex-shrink: 0;
    box-shadow: 0 2px 5px rgba(0,0,0,0.02);
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

header h1 { color: var(--primary-color); font-size: 1.3rem; font-weight: 700; margin: 0; }
header p { font-size: 0.85rem; color: var(--sub-text-color); margin: 0; }

.icon-btn {
    background: transparent; border: none; cursor: pointer; color: var(--sub-text-color);
    padding: 8px; border-radius: 50%; transition: 0.3s; display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background-color: var(--icon-hover); color: var(--primary-color); }

#menu-btn { position: absolute; left: 15px; top: 50%; transform: translateY(-50%); }
#theme-toggle { position: absolute; right: 15px; top: 50%; transform: translateY(-50%); }

/* --- Sidebar --- */
.sidebar {
    position: fixed; top: 0; left: -280px; width: 280px; height: 100%;
    background-color: var(--sidebar-bg); box-shadow: 2px 0 10px rgba(0,0,0,0.1);
    z-index: 1000; transition: left 0.3s ease; display: flex; flex-direction: column; padding: 15px;
}
.sidebar.open { left: 0; }

.sidebar-top { display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }

.sidebar-action-btn {
    display: flex; align-items: center; gap: 10px; padding: 12px 15px;
    border-radius: 8px; font-size: 14px; font-weight: 500; cursor: pointer;
    transition: 0.2s; border: 1px solid var(--header-border); color: var(--text-color);
}
.new-chat-btn { background-color: var(--primary-color); color: white; border: none; }
.clear-chat-btn { background-color: transparent; }

.history-label { font-size: 12px; color: var(--sub-text-color); margin: 10px 5px 5px; text-transform: uppercase; font-weight: 600; }
.history-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; }

.history-item {
    display: flex; align-items: center; gap: 10px; padding: 10px 12px;
    border-radius: 8px; font-size: 14px; color: var(--text-color); cursor: pointer;
    transition: 0.2s; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; user-select: none;
}
.history-item:hover { background-color: var(--icon-hover); }
.history-item.active { background-color: var(--sidebar-item-active); font-weight: 500; }
.history-item svg { flex-shrink: 0; color: var(--sub-text-color); }

.pin-indicator { margin-left: auto; color: var(--primary-color); transform: rotate(45deg); }

.sidebar-footer {
    margin-top: 10px; border-top: 1px solid var(--header-border); padding-top: 10px;
    font-size: 12px; display: flex; gap: 8px; justify-content: center; align-items: center;
}
.sidebar-footer a { color: var(--sub-text-color); text-decoration: none; }
.footer-dot { color: var(--sub-text-color); font-size: 14px; line-height: 1; }

.overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background-color: var(--overlay-bg); z-index: 999; display: none; opacity: 0; transition: opacity 0.3s;
}
.overlay.active { display: block; opacity: 1; }

/* --- Context Menu --- */
.context-modal {
    position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%);
    background-color: var(--modal-bg); padding: 20px; border-radius: 12px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.2); z-index: 2000; width: 300px;
    display: none; flex-direction: column; gap: 10px;
}
.context-modal.active { display: flex; }
.context-title { font-size: 16px; font-weight: 600; margin-bottom: 5px; color: var(--text-color); text-align: center; }
.context-btn {
    padding: 12px; border: none; border-radius: 8px; font-size: 14px; cursor: pointer;
    display: flex; align-items: center; justify-content: center; gap: 8px;
}
.btn-pin { background-color: var(--icon-hover); color: var(--primary-color); }
.btn-rename { background-color: var(--icon-hover); color: var(--text-color); }
.btn-delete { background-color: #ffebee; color: #c62828; }
.btn-cancel { background-color: transparent; color: var(--sub-text-color); border: 1px solid var(--header-border); }

/* --- Chat Area --- */
#chat-container {
    flex: 1; overflow-y: auto; padding: 20px 15px; display: flex; flex-direction: column;
    gap: 15px; background-color: var(--chat-bg); transition: background-color 0.3s;
}

.message { display: flex; gap: 10px; }
.bot { align-self: flex-start; max-width: 90%; }
.user { align-self: flex-end; flex-direction: row-reverse; max-width: 70%; }

.avatar {
    width: 32px; height: 32px; border-radius: 50%; padding: 2px;
    border: 1px solid #eee; object-fit: cover; flex-shrink: 0; background: #fff;
}

.msg-bubble { display: flex; flex-direction: column; max-width: 100%; }

.content {
    padding: 12px 18px; border-radius: 20px; font-size: 15px;
    line-height: 1.5; word-wrap: break-word;
}
.bot .content { background: var(--bot-msg-bg); color: var(--bot-msg-text); border-top-left-radius: 5px; }
.user .content { background: var(--primary-color); color: var(--send-btn-text); border-top-right-radius: 5px; }
.content a { color: var(--primary-color); }
body.dark-mode .content a { color: #4dd0e1; }

/* --- Action Bar (Aligned Right) --- */
.action-bar {
    display: flex; gap: 10px; margin-top: 5px; opacity: 0.7;
    transition: opacity 0.3s; justify-content: flex-end; padding-right: 5px;
}
.action-bar:hover { opacity: 1; }

.action-icon {
    background: transparent; border: none; cursor: pointer; color: var(--sub-text-color);
    padding: 4px; border-radius: 4px; display: flex; align-items: center; justify-content: center;
    transition: 0.2s; width: 28px; height: 28px;
}
.action-icon:hover { background-color: var(--icon-hover); color: var(--primary-color); }
.action-icon.active { color: var(--primary-color); }

/* Spinner */
.loader-spinner {
    width: 16px; height: 16px; border: 2px solid var(--sub-text-color);
    border-bottom-color: transparent; border-radius: 50%;
    display: inline-block; animation: rotation 1s linear infinite;
}
@keyframes rotation { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }

/* Pulse */
.action-icon.playing { color: #e74c3c; animation: pulse-red 1.5s infinite; }
@keyframes pulse-red {
    0% { transform: scale(1); } 50% { transform: scale(1.1); } 100% { transform: scale(1); }
}

.typing { font-style: italic; color: var(--sub-text-color); font-size: 14px; }
.typing::after {
    content: ''; animation: typing-dots 1.5s infinite steps(4, end);
    display: inline-block; width: 15px; text-align: left;
}
@keyframes typing-dots { 0%, 20% { content: ''; } 40% { content: '.'; } 60% { content: '..'; } 80%, 100% { content: '...'; } }

/* --- Input Area --- */
#input-area {
    background: var(--input-area-bg); padding: 15px; display: flex; flex-direction: column;
    gap: 8px; border-top: 1px solid var(--header-border); flex-shrink: 0;
    padding-bottom: max(10px, env(safe-area-inset-bottom)); transition: background-color 0.3s;
}
.input-row { display: flex; align-items: flex-end; gap: 10px; width: 100%; }
textarea {
    flex: 1; background: var(--input-bg); color: var(--input-text); border: none;
    border-radius: 24px; padding: 12px 20px; font-size: 16px; resize: none;
    max-height: 120px; height: 48px; font-family: inherit;
}
#send-btn {
    background: var(--primary-color); color: var(--send-btn-text); border: none;
    width: 48px; height: 48px; border-radius: 50%; cursor: pointer;
    display: flex; align-items: center; justify-content: center; flex-shrink: 0; transition: 0.2s;
}
#send-btn:disabled { background: var(--input-bg); color: var(--sub-text-color); cursor: default; }
.footer-text { text-align: center; font-size: 11px; color: var(--sub-text-color); margin-top: 2px; }
.footer-text a { color: var(--sub-text-color); text-decoration: underline; font-weight: 500; }
.error-text { color: #e74c3c; font-size: 0.9rem; }
