/* --- VARIABILI CSS (THEMING) --- */
:root {
    /* Light Mode (Default) */
    --primary: #4a8cff;
    --primary-dark: #3b73d1;
    --bg-app: #f0f2f5;
    --bg-sidebar: #ffffff;
    --bg-chat-user: #eef6ff;
    --bg-chat-ai: #ffffff;
    --text-main: #333333;
    --text-sec: #666666;
    --border: #e5e5e5;
    --input-bg: #ffffff;
    --input-border: #cccccc;
    --shadow: 0 2px 5px rgba(0,0,0,0.05);
    --hover-bg: #f5f5f5;
    --code-bg: #2d2d2d;
    --code-text: #f8f8f2;
}

/* Dark Mode Overrides */
[data-theme="dark"] {
    --bg-app: #18191a;
    --bg-sidebar: #242526;
    --bg-chat-user: #2d3e50; 
    --bg-chat-ai: #242526;
    --text-main: #e4e6eb;
    --text-sec: #b0b3b8;
    --border: #393a3b;
    --input-bg: #3a3b3c;
    --input-border: #555555;
    --shadow: 0 2px 5px rgba(0,0,0,0.2);
    --hover-bg: #3a3b3c;
}

* { box-sizing: border-box; }

body {
    font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    background: var(--bg-app);
    color: var(--text-main);
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    height: 100vh;
    width: 100vw;
    overflow: hidden;
    transition: background 0.3s, color 0.3s;
}

/* HEADER FISSO */
#header {
    flex-shrink: 0;
    height: 60px;
    padding: 0 20px;
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
    font-size: 18px;
    box-shadow: var(--shadow);
    z-index: 20;
    width: 100%;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 15px;
    font-size: 14px;
    font-weight: normal;
}

.user-icon {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    border: 1px solid var(--border);
}

/* LAYOUT PRINCIPALE */
#main-content {
    display: flex;
    flex-grow: 1;
    overflow: hidden;
    width: 100%;
    position: relative;
}

/* SIDEBAR SINISTRA */
#sidebar-left {
    width: 200px;
    min-width: 160px;
    max-width: 450px;
    border-right: 1px solid var(--border);
    background: var(--bg-sidebar);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    resize: horizontal;
    overflow: auto;
}

.sidebar-header {
    padding: 15px;
    border-bottom: 1px solid var(--border);
}

#chat-list {
    flex-grow: 1;
    overflow-y: auto;
    padding: 10px;
}

.chat-item {
    padding: 10px 12px;
    border-radius: 6px;
    cursor: pointer;
    margin-bottom: 4px;
    background: transparent;
    font-size: 14px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    transition: background 0.2s;
    color: var(--text-sec);
}

.chat-item:hover { background: var(--hover-bg); }
.chat-item.active { background: rgba(74, 140, 255, 0.15); color: var(--primary); font-weight: 600; }

/* CHAT CONTAINER */
#chat-container {
    display: flex;
    flex-direction: column;
    flex-grow: 1;
    min-width: 0;
}

/* HEADER CHAT INTERNO */
#chat-header-bar {
    padding: 10px 20px;
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border);
    display: none;
    align-items: center;
    flex-shrink: 0;
    height: 50px;
}

#title-view, #title-edit {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
}

#current-chat-name {
    font-weight: 600;
    font-size: 15px;
    color: var(--text-main);
}

#rename-input {
    padding: 6px 10px;
    border: 1px solid var(--primary);
    border-radius: 4px;
    font-size: 14px;
    flex-grow: 1;
    outline: none;
    background: var(--input-bg);
    color: var(--text-main);
}

#chat {
    flex-grow: 1;
    padding: 20px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* MESSAGGI */
.msg-wrapper {
    display: flex;
    align-items: flex-start;
    gap: 15px;
    width: 100%;
}

.avatar {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    flex-shrink: 0;
    background: var(--bg-sidebar);
    border: 1px solid var(--border);
    padding: 2px;
}

.bubble {
    flex-grow: 1;
    padding: 12px 18px;
    border-radius: 8px;
    line-height: 1.6;
    font-size: 15px;
    overflow-wrap: anywhere;
    position: relative;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
    max-width: 95%;
    color: var(--text-main);
}

.user-msg .bubble { background: var(--bg-chat-user); border: 1px solid rgba(0,0,0,0.05); }
.ai-msg .bubble { background: var(--bg-chat-ai); border: 1px solid var(--border); }
.bubble p { margin: 8px 0; }
.bubble p:first-child { margin-top: 0; }
.bubble p:last-child { margin-bottom: 0; }

.bubble pre {
    background: var(--code-bg);
    color: var(--code-text);
    padding: 15px;
    border-radius: 6px;
    overflow-x: auto;
    margin: 10px 0;
}

.bubble code {
    background: rgba(128,128,128,0.2);
    padding: 2px 5px;
    border-radius: 4px;
    font-family: monospace;
    font-size: 0.9em;
}

/* ANIMAZIONE TYPING */
.typing-dots {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 5px 0;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background-color: var(--text-sec);
    border-radius: 50%;
    animation: typing 1.4s infinite ease-in-out both;
    opacity: 0.6;
}

.typing-dots span:nth-child(1) { animation-delay: -0.32s; }
.typing-dots span:nth-child(2) { animation-delay: -0.16s; }

@keyframes typing {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* INPUT AREA */
#input-area {
    padding: 20px;
    background: var(--bg-sidebar);
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: center;
    flex-shrink: 0;
}

.input-wrapper {
    background: var(--input-bg);
    border: 1px solid var(--input-border);
    border-radius: 12px;
    padding: 5px 10px 5px 15px;
    display: flex;
    width: 100%;
    align-items: flex-end;
}

textarea#input {
    flex-grow: 1;
    padding: 12px 0;
    font-family: inherit;
    font-size: 16px;
    border: none;
    resize: none;
    max-height: 150px;
    outline: none;
    background: transparent;
    color: var(--text-main);
    line-height: 1.4;
}

.send-btn {
    margin-bottom: 8px;
    width: 36px;
    height: 36px;
    background: var(--primary);
    border: none;
    color: white;
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-left: 10px;
}

.send-btn:hover { background: var(--primary-dark); }
.send-btn svg { width: 18px; height: 18px; fill: white; }

/* SIDEBAR DESTRA (IMPOSTAZIONI) */
#sidebar-right {
    width: 0;
    background: var(--bg-sidebar);
    border-left: 1px solid var(--border);
    overflow: hidden;
    transition: width 0.3s ease;
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
}

#sidebar-right.open { width: 320px; }
.settings-content { padding: 20px; width: 320px; box-sizing: border-box; }

.settings-group { margin-bottom: 20px; }
.settings-group label {
    display: block;
    font-weight: 600;
    font-size: 12px;
    margin-bottom: 8px;
    color: var(--text-sec);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.settings-group input[type="text"],
.settings-group input[type="number"],
.settings-group textarea {
    width: 100%;
    padding: 10px;
    border: 1px solid var(--input-border);
    border-radius: 6px;
    font-family: inherit;
    font-size: 14px;
    background: var(--input-bg);
    color: var(--text-main);
}

.settings-group textarea { resize: vertical; min-height: 100px; }

/* Switch Toggle CSS */
.switch-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
}
.switch-label { font-size: 14px; font-weight: 500; color: var(--text-main); }
.switch { position: relative; display: inline-block; width: 46px; height: 24px; }
.switch input { opacity: 0; width: 0; height: 0; }
.slider { position: absolute; cursor: pointer; top: 0; left: 0; right: 0; bottom: 0; background-color: #ccc; transition: .4s; border-radius: 34px; }
.slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 3px; bottom: 3px; background-color: white; transition: .4s; border-radius: 50%; }
input:checked + .slider { background-color: var(--primary); }
input:checked + .slider:before { transform: translateX(22px); }

/* BUTTONS */
.btn-action {
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--input-bg);
    color: var(--text-main);
    cursor: pointer;
    font-size: 13px;
}

.btn-action:hover { background: var(--hover-bg); }
.btn-primary { background: var(--primary); color: white; border: none; width: 100%; padding: 10px; font-weight: 600; border-radius: 6px; }
.btn-primary:hover { background: var(--primary-dark); }
.icon-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 8px;
    color: var(--text-sec);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
}

.icon-btn:hover { background: var(--hover-bg); color: var(--primary); }
.icon-btn svg { width: 20px; height: 20px; }

/* MEDIA QUERIES (STILI MOBILE) */
@media (max-width: 768px) {
    #header { padding: 0 15px; font-size: 16px; }
    .header-right { gap: 5px; }
    .user-icon { width: 24px; height: 24px; }
    #sidebar-left { display: none; }
    #chat-container { width: 100%; }
    #sidebar-right { position: fixed; top: 60px; right: 0; bottom: 0; height: calc(100% - 60px); width: 0; z-index: 30; overflow-y: auto; }
    #sidebar-right.open { width: 100vw; }
    .settings-content { width: 100vw; padding: 15px; }
    #chat { padding: 10px; gap: 15px; }
    .msg-wrapper { gap: 10px; }
    .avatar { width: 32px; height: 32px; }
    .bubble { font-size: 14px; padding: 10px 14px; }
    #input-area { padding: 10px; }
    .input-wrapper { padding: 5px 8px 5px 10px; }
    textarea#input { padding: 10px 0; font-size: 15px; }
}

.mic-btn {
    margin-bottom: 8px;
    width: 36px;
    height: 36px;
    background: var(--input-border);
    border: none;
    color: var(--text-main);
    border-radius: 8px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-left: 5px;
    transition: all 0.3s ease;
}

.mic-btn.recording { background: #ff4a4a; color: white; animation: pulse-red 1.5s infinite; }

@keyframes pulse-red {
    0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255, 74, 74, 0.7); }
    70% { transform: scale(1.05); box-shadow: 0 0 0 10px rgba(255, 74, 74, 0); }
    100% { transform: scale(1); box-shadow: 0 0 0 0 rgba(255, 74, 74, 0); }
}
