/* --- Ver 1.17 (Compactació, Mode/Seed i Fix d'Espai del Formulari FINAL) --- */

:root {
    /* Tema Clar (per defecte) */
    --main-bg: #f4f4f9;
    --card-bg: #ffffff;
    --text-color: #333333;
    --border-color: #dddddd;
    --prompt-bg: #ffffe0; 
    --prompt-text-color: #000000; 
    --accent-color: #28a745; /* Verd per SuperPrompt */
    --fieldset-title-size: 0.9em; /* Mida dels títols dels fieldsets */
}

/* Temes de Color */
body.theme-gray {
    --main-bg: #5c5c5c;
    --card-bg: #7a7a7a;
    --text-color: #ffffff;
    --border-color: #555555;
    --prompt-bg: #4f4f4f; 
    --prompt-text-color: #ffffff; 
}
body.theme-dark {
    --main-bg: #1c1c1c;
    --card-bg: #333333;
    --text-color: #f0f0f0;
    --border-color: #555555;
    --prompt-bg: #4f4f4f; 
    --prompt-text-color: #f0f0f0; 
}

body {
    font-family: Arial, sans-serif;
    background-color: var(--main-bg);
    color: var(--text-color);
    padding: 10px;
    margin: 0;
    transition: background-color 0.3s, color 0.3s;
    overflow-y: hidden; 
    height: 100vh;
    box-sizing: border-box;
}

#app-container {
    max-width: 1400px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
    height: 100%;
    position: relative; 
}

/* --- 2. Estils del Menú Superior (Top Bar) --- */
#top-menu {
    display: flex;
    align-items: center;
    background: var(--card-bg);
    padding: 5px 20px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    gap: 20px;
}
#top-menu h1 {
    font-size: 1.5em;
    color: var(--accent-color);
    font-weight: bold;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 5px;
}
.menu-item {
    display: flex;
    align-items: center;
    gap: 8px;
}
.menu-item label {
    font-weight: bold;
    white-space: nowrap;
    font-size: 0.9em;
    display: flex;
    align-items: center;
    gap: 5px;
}
.save-prompts-option input[type="checkbox"] {
    transform: scale(1.2); 
    margin: 0;
}
.save-prompts-option label {
    font-weight: normal; 
    font-size: 0.85em;
}

.history-disabled {
    opacity: 0.5; 
    pointer-events: none; 
    transition: opacity 0.2s ease;
}

#top-menu input, #top-menu select {
    padding: 3px;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    background-color: var(--main-bg);
    color: var(--text-color);
    font-size: 0.8em;
}

#copy-prompt-btn, #clear-history-btn {
    margin-left: auto; 
    padding: 6px 10px; 
    background-color: var(--accent-color); 
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-weight: bold;
    transition: background-color 0.2s;
    font-size: 0.75em; 
    display: flex;
    align-items: center;
    gap: 5px; 
    white-space: nowrap;
}

#copy-prompt-btn:hover, #clear-history-btn:hover {
    background-color: #1e7e34;
}

#clear-history-btn {
    margin-left: 0; 
    background-color: #dc3545; /* Red for destructive action */
}

#clear-history-btn:hover {
    background-color: #c82333;
}

.copy-icon {
    content: "📋"; 
}
.copy-icon::before {
    content: '';
    display: inline-block;
    width: 12px; 
    height: 12px;
    border: 2px solid white;
    border-radius: 2px;
    box-shadow: 
        -3px -3px 0 0 white, 
        -3px -3px 0 1px var(--accent-color); 
    transform: translate(-1px, 1px); 
}


/* --- 3. Formulari Principal (Graella 3x3) --- */
#prompt-form {
    background: var(--card-bg);
    padding: 10px;
    border-radius: 8px;
    box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
    
    display: grid;
    /* Graella 3x3 */
    grid-template-columns: repeat(3, 1fr); 
    /* Row 1, 2 són automàtiques. Row 3 (Audio/Exclusions) ocupa l'espai restant */
    grid-template-rows: auto auto 1fr; 
    gap: 10px; 
    flex-grow: 1;
    min-height: 0;
}

.chapter {
    /* CORRECCIÓ CLAU: Padding inferior a 0 forçat per al contenidor pare */
    padding: 3px 5px 0 5px !important; 
    border: 1px solid var(--border-color);
    border-radius: 6px;
    margin: 0; 
    background-color: var(--card-bg);
    display: flex;
    flex-direction: column;
}

legend {
    font-size: var(--fieldset-title-size); 
    font-weight: bold;
    color: var(--accent-color); 
    padding: 0 5px;
    white-space: nowrap; 
    display: flex; 
    align-items: center;
    gap: 5px;
}

/* POSICIONS */
.chapter-1 { grid-area: 1 / 1 / 2 / 2; } 
.chapter-2 { grid-area: 1 / 2 / 2 / 3; } 
.chapter-3 { grid-area: 1 / 3 / 2 / 4; } 

.chapter-4 { grid-area: 2 / 1 / 3 / 2; } 
.chapter-5 { grid-area: 2 / 2 / 3 / 3; } 
.chapter-6 { grid-area: 2 / 3 / 3 / 4; } 

.chapter-7 { 
    grid-area: 3 / 1 / 4 / 3; 
}

.chapter-8 { 
    grid-area: 3 / 3 / 4 / 4;
}

label {
    display: block; 
    /* COMPACTACIÓ EXTREMA: Mínim marge superior i zero inferior */
    margin-top: 1px;
    margin-bottom: 0px; 
    font-weight: 500;
    color: var(--text-color);
    font-size: 0.75em; 
}

input[type="text"], select, textarea {
    width: 100%; 
    padding: 3px; 
    border: 1px solid var(--border-color);
    border-radius: 3px;
    box-sizing: border-box; 
    /* COMPACTACIÓ EXTREMA: Marge inferior 0 forçat per a TOTS els camps de formulari */
    margin-bottom: 0 !important;
    background-color: var(--main-bg);
    color: var(--text-color);
    font-size: 0.8em;
}

textarea[required], input[required], select[required] {
    border-left: 3px solid var(--accent-color);
}


/* Ajust de mida de textarea per al bloc 1 i bloc 8 */
#subject-action, #negative-elements {
    resize: vertical; 
    font-family: Arial, sans-serif;
    font-size: 0.8em;
}
#subject-action {
    /* Ajustat per permetre l'espai dels nous camps Mode/Llavor */
    min-height: 30px; 
}


/* Contenidor per a dos selects o inputs a la mateixa línia (Blocs 4, 6, 7 i ara 1) */
.two-column-selects {
    display: flex;
    gap: 10px; 
    /* COMPACTACIÓ EXTREMA: Marge inferior 0 */
    margin-bottom: 0 !important; 
}
.select-item {
    flex: 1; 
}
.two-column-selects .select-item select,
.two-column-selects .select-item input {
    margin-bottom: 0 !important; 
}


/* Estil del bloc 7 (AUDIO) */
.audio-grid {
    display: grid;
    grid-template-columns: 1fr 1fr; 
    gap: 10px;
    flex-grow: 1; 
    /* Assegurem que el contenidor Grid estigui col·lapsat */
    margin-bottom: 0 !important;
}

.audio-grid .audio-col-2 .two-column-selects {
    margin-bottom: 0 !important; 
}

.audio-grid select,
.audio-grid input, 
.audio-grid textarea {
    padding: 3px; 
    font-size: 0.8em; 
}

#voice-dialogue {
    min-height: 40px; 
    resize: vertical;
}


/* --- 4. Quadre de Resultat del Prompt (Sense Canvis Importants) --- */
#prompt-output-section {
    background: var(--card-bg);
    padding: 10px;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
    width: 100%; 
    box-sizing: border-box;
}

#prompt-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5px; 
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 5px;
}

.prompt-title {
    font-size: var(--fieldset-title-size); 
    font-weight: bold;
    color: var(--accent-color);
    margin: 0; 
    display: flex; 
    align-items: center;
    gap: 5px;
}

#clear-form-btn {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 4px;
    color: var(--text-color);
    cursor: pointer;
    padding: 3px 8px;
    font-size: 0.8em;
    display: flex;
    align-items: center;
    gap: 5px;
    transition: background-color 0.2s;
}

#clear-form-btn:hover {
    background-color: var(--border-color);
}

.clear-icon {
    font-size: 1.2em;
    line-height: 1;
}

#prompt-output {
    min-height: 50px; 
    max-height: 70px; 
    line-height: 1.2;
    padding: 10px;
    margin-top: 5px;
    margin-bottom: 0; 
    border: 2px dashed var(--accent-color);
    border-radius: 4px;
    background-color: var(--prompt-bg);
    white-space: pre-wrap;
    font-family: Consolas, monospace;
    font-size: 0.8em; 
    color: var(--prompt-text-color); 
    overflow-y: auto;
    user-select: all; 
}

.prompt-tag {
    color: var(--accent-color);
    font-weight: bold;
}

/* --- 5. Estils de Globus d'Ajuda (Tooltips - Sense Canvis) --- */

.info-icon {
    font-size: 0.9em;
    color: var(--accent-color);
    cursor: help;
    font-weight: bold;
    user-select: none;
    line-height: 1;
    display: inline-block;
}

#tooltip-box {
    position: absolute;
    background-color: var(--text-color);
    color: var(--main-bg);
    padding: 8px 12px;
    border-radius: 4px;
    font-size: 0.8em;
    z-index: 1000;
    max-width: 300px;
    pointer-events: none; 
    opacity: 0;
    transition: opacity 0.2s, transform 0.2s;
    transform: translateY(5px);
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    text-align: left;
    white-space: normal;
}

#tooltip-box[aria-hidden="false"] {
    opacity: 1;
    transform: translateY(0);
}

/* ======================================================= */
/* === CORRECCIÓ FINAL AGRESSIVA V1.17 D'ESPAIAT DEL FORMULARI === */
/* ======================================================= */

/* 1. Neteja Extrema de l'últim element dins del Flex/Grid - ÚS DE MARGE NEGATIU */

/* Bloc 8 (Exclusions) */
.chapter-8 textarea#negative-elements {
    /* El recurs més agressiu per absorbir l'espai sobrant */
    margin-bottom: -1px !important; 
    padding-bottom: 0 !important; 
}

/* Bloc 7 (Audio) - L'últim element és el textarea #voice-dialogue */
.chapter-7 #voice-dialogue {
    /* El recurs més agressiu per absorbir l'espai sobrant */
    margin-bottom: -1px !important; 
    padding-bottom: 0 !important; 
}

/* Per si un altre element és l'últim fill, assegurem que el seu marge es col·lapsa */
.chapter > *:last-child {
    margin-bottom: -1px !important;
    padding-bottom: 0 !important; 
}


/* Media Query (per si de cas) */
@media (max-width: 800px) { 
    #prompt-form {
        grid-template-columns: 1fr;
        grid-template-rows: auto;
    }
    .chapter-1, .chapter-2, .chapter-3, .chapter-4, .chapter-5, .chapter-6, .chapter-7, .chapter-8 {
        grid-area: auto !important;
    }
    .audio-grid {
        grid-template-columns: 1fr;
    }
    #top-menu {
        flex-wrap: wrap;
        gap: 10px 20px;
    }
    #copy-prompt-btn {
        margin-left: 0;
    }
    #top-menu h1 {
        flex-basis: 100%;
        margin-bottom: 5px;
    }
    body {
        overflow-y: auto;
        height: auto;
    }
    #app-container {
        height: auto;
    }
}