/* --- VARIABLES DE DESIGN --- */
:root {
    --primary-color: #1a4f72;
    --background-color: #f4f7f9;
    --card-background: #ffffff;
    --text-color: #2c3e50;
    --text-muted: #7f8c8d;
    --safe-color: #27ae60;
    --safe-light: #e8f8f0;
    --danger-color: #c0392b;
    --danger-light: #fdedec;
    --border-radius: 16px;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.05);
    --font-family: 'Poppins', sans-serif;
}

/* --- RESET & GLOBALS --- */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html, body {
    overflow-x: hidden;
    width: 100%;
}

body {
    font-family: var(--font-family);
    background-color: var(--background-color);
    color: var(--text-color);
    padding: 10px;
    min-height: 100vh;
    display: flex;
    justify-content: center;
}

.app-container {
    width: 100%;
    max-width: 500px; /* Taille optimale pour mobile et lisibilité sur PC */
    display: flex;
    flex-direction: column;
    gap: 15px;
}

/* --- HEADER --- */
.app-header {
    text-align: center;
    padding: 15px 0 5px 0;
}

.logo-area {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}

.logo-icon {
    font-size: 2.2rem;
}

.app-header h1 {
    font-size: 1.8rem;
    font-weight: 800;
    color: var(--primary-color);
    letter-spacing: -0.5px;
}

.subtitle {
    font-size: 0.85rem;
    color: var(--text-muted);
    font-weight: 600;
}

/* --- CARD DESIGN --- */
.card {
    background-color: var(--card-background);
    border-radius: var(--border-radius);
    padding: 20px;
    box-shadow: var(--shadow);
    border: 1px solid rgba(0, 0, 0, 0.02);
}

.card-header h2 {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 15px;
    color: var(--primary-color);
}

/* --- SCANNER VIEWPORT --- */
.viewport {
    width: 100%;
    aspect-ratio: 1/1; /* Viseur carré moderne : 33% de hauteur en plus pour le confort mobile */
    background-color: #000;
    border-radius: 12px;
    overflow: hidden;
    position: relative;
    border: 3px solid var(--primary-color);
}

#reader {
    width: 100% !important;
    height: 100% !important;
}

#reader video {
    object-fit: cover !important;
    width: 100% !important;
    height: 100% !important;
}

/* On masque les éléments natifs superflus de la bibliothèque html5-qrcode */
#reader__dashboard, #reader__header_message {
    display: none !important;
}

/* --- CONTROLS & BUTTONS --- */
.controls-area {
    margin-top: 15px;
    display: flex;
    justify-content: center;
}

.btn {
    font-family: var(--font-family);
    font-weight: 700;
    font-size: 0.95rem;
    padding: 12px 24px;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    transition: all 0.2s ease;
    width: 100%;
    text-align: center;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.btn-primary {
    background-color: var(--primary-color);
    color: #ffffff;
}

.btn-primary:hover {
    background-color: #123450;
    transform: translateY(-2px);
}

.btn-secondary {
    background-color: var(--text-color);
    color: #ffffff;
}

.btn-secondary:hover {
    background-color: #1a252f;
}

/* --- INPUTS --- */
.manual-input-area {
    margin-top: 20px;
    text-align: center;
}

.divider-text {
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 12px;
    position: relative;
}

.input-group {
    display: flex;
    gap: 8px;
}

.input-group input {
    flex: 1;
    padding: 12px 16px;
    border-radius: 30px;
    border: 2px solid #e2e8f0;
    font-family: var(--font-family);
    font-size: 0.9rem;
    font-weight: 600;
    outline: none;
    transition: border-color 0.2s ease;
}

.input-group input:focus {
    border-color: var(--primary-color);
}

.input-group .btn {
    width: auto;
    white-space: nowrap;
}

/* --- RESULTS SECTION --- */
.result-card {
    transition: all 0.3s ease;
}

.hidden {
    display: none !important;
}

/* Statut de chargement */
.loading-spinner {
    text-align: center;
    padding: 20px 0;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 4px solid rgba(0, 0, 0, 0.1);
    border-top: 4px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 10px auto;
}

@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

/* --- ÉTAT PRODUIT SÛR (VERT) --- */
.status-banner {
    padding: 15px;
    border-radius: 12px;
    font-weight: 800;
    font-size: 1.1rem;
    text-align: center;
    margin-bottom: 20px;
}

.status-safe {
    background-color: var(--safe-light);
    color: var(--safe-color);
    border: 2px solid var(--safe-color);
}

/* --- ÉTAT PRODUIT DANGER (ROUGE) --- */
.status-danger {
    background-color: var(--danger-light);
    color: var(--danger-color);
    border: 2px solid var(--danger-color);
    animation: flash 1.5s infinite;
}

@keyframes flash {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.85; border-color: #ff0000; box-shadow: 0 0 15px rgba(192, 57, 43, 0.3); }
}

.danger-message {
    text-align: center;
    padding: 10px 0;
}

.danger-title {
    font-size: 1.4rem;
    font-weight: 800;
    margin-bottom: 5px;
}

.danger-subtitle {
    font-size: 0.95rem;
    font-weight: 600;
}

.danger-ingredients {
    background-color: #fff5f5;
    border: 1px solid #feb2b2;
    padding: 12px;
    border-radius: 8px;
    margin-top: 15px;
    font-size: 0.9rem;
    line-height: 1.4;
}

/* --- INFOS PRODUIT --- */
.product-info-header {
    display: flex;
    gap: 15px;
    align-items: center;
    margin-bottom: 20px;
}

.product-image {
    width: 70px;
    height: 70px;
    object-fit: contain;
    border-radius: 8px;
    background-color: #fafbfc;
    border: 1px solid #e2e8f0;
}

.product-titles h3 {
    font-size: 1.1rem;
    font-weight: 700;
}

.product-brand {
    font-size: 0.85rem;
    color: var(--text-muted);
    font-weight: 600;
}

/* --- GRILLE DE NUTRITION & CALCULS --- */
.nutrition-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 20px;
}

.nutri-box {
    background-color: var(--background-color);
    padding: 12px;
    border-radius: 12px;
    text-align: center;
}

.nutri-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 700;
    text-transform: uppercase;
}

.nutri-value {
    font-size: 1.2rem;
    font-weight: 800;
    color: var(--primary-color);
}

/* --- CALCULATEUR DE PORTIONS --- */
.portion-calculator {
    background-color: #ebf3f9;
    padding: 15px;
    border-radius: 12px;
    border: 1px dashed var(--primary-color);
    text-align: center;
    margin-bottom: 10px;
}

.portion-label {
    font-size: 0.9rem;
    font-weight: 700;
    margin-bottom: 8px;
    display: block;
}

.portion-input-group {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    max-width: 200px;
    margin: 0 auto 12px auto;
}

.portion-input-group input {
    width: 80px;
    padding: 8px;
    border-radius: 8px;
    border: 2px solid #cbd5e1;
    font-family: var(--font-family);
    font-weight: 700;
    font-size: 1rem;
    text-align: center;
    outline: none;
}

.portion-input-group input:focus {
    border-color: var(--primary-color);
}

.portion-unit {
    font-weight: 700;
    color: var(--primary-color);
}

.result-parts-box {
    padding: 10px;
    border-radius: 8px;
    background-color: #ffffff;
    display: inline-block;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    margin-top: 5px;
}

.parts-highlight {
    font-size: 1.6rem;
    font-weight: 800;
    color: #e67e22; /* Belle couleur orange pour les parts */
}

/* --- FOOTER --- */
.app-footer {
    text-align: center;
    padding: 10px 0;
    font-size: 0.8rem;
    color: var(--text-muted);
    font-weight: 600;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.app-footer .small {
    font-size: 0.7rem;
    font-weight: 400;
}

/* --- RÉSULTATS DE RECHERCHE CIQUAL --- */
.ciqual-results-list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: 300px;
    overflow-y: auto;
    padding-right: 5px;
}

.ciqual-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 15px;
    background-color: #fafbfc;
    border: 1px solid #e2e8f0;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s ease;
}

.ciqual-item:hover {
    background-color: #f1f5f9;
    border-color: var(--primary-color);
    transform: translateX(3px);
}

.ciqual-item-info {
    display: flex;
    flex-direction: column;
    gap: 3px;
    text-align: left;
    max-width: 70%;
}

.ciqual-item-name {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--primary-color);
    line-height: 1.2;
}

.ciqual-item-brand {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
}

.ciqual-badge {
    display: inline-block;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.65rem;
    font-weight: 700;
    text-transform: uppercase;
    background-color: #e2e8f0;
    color: #475569;
    width: fit-content;
}

.ciqual-item-stats {
    text-align: right;
}

.ciqual-item-parts {
    font-size: 1.05rem;
    font-weight: 800;
    color: #e67e22;
}

.ciqual-item-proteins {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
}

/* --- RESPONSIVE MOBILE OPTIMIZATIONS --- */
@media (max-width: 480px) {
    body {
        padding: 6px;
    }
    
    .app-container {
        gap: 10px;
    }
    
    .app-header {
        padding: 10px 0 2px 0;
    }
    
    .app-header h1 {
        font-size: 1.5rem;
    }
    
    .logo-icon {
        font-size: 1.8rem;
    }
    
    .card {
        padding: 12px 10px;
        border-radius: 12px;
    }
    
    /* Améliorer les boutons de la caméra */
    .btn {
        padding: 10px 18px;
        font-size: 0.9rem;
    }
    
    /* Optimiser la zone d'ingrédients réactifs sur les petits smartphones */
    #ingredients-container div {
        font-size: 0.78rem !important;
        padding-bottom: 2px !important;
    }
    
    #ingredients-container span {
        font-size: 0.78rem !important;
    }
    
    /* Formater les entrées textuelles mobiles */
    .input-group {
        flex-direction: column;
        gap: 8px;
    }
    
    .input-group input {
        font-size: 0.85rem;
        padding: 10px 14px;
        width: 100%;
    }
    
    .input-group .btn {
        width: 100%;
        border-radius: 30px;
    }
}


