/* ==========================================================================
   1. VARIABLES & MOTEUR DE THÈME (Clair & Sombre)
   ========================================================================== */

/* 1.1 Thème Clair (Défaut) : Soft & Élégant */
:root {
  --brand-primary: #5c7cfa; 
  --brand-accent: #ccff00; 
  --brand-status: #25d366; 

  --bg-base: #f0f4f8; 
  --bg-gradient: linear-gradient(135deg, #e2e8f0, #f8fafc);
  --bg-surface: rgba(255, 255, 255, 0.85); 
  --bg-header: #ffffff;

  --text-main: #1e293b; 
  --text-muted: #64748b; 
  --text-inverse: #ffffff; 

  --border-color: rgba(0, 0, 0, 0.08);
  --glass-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.07);
  
  /* Variables adaptatives pour corriger le mode clair */
  --input-bg: rgba(255, 255, 255, 0.9);
  --input-text: #1e293b;
  --placeholder-color: rgba(0, 0, 0, 0.5); /* Gris visible sur fond clair */
  --bot-msg-bg: #5c7cfa; /* Bleu fort pour lire le texte blanc */
  --bot-msg-text: #ffffff;
  --btn-outline-color: #5c7cfa; /* Bordure bleue au lieu de fluo sur fond clair */

  --toggle-bg: #5c7cfa; 
  --toggle-text: #ffffff;
  --toggle-border: #5c7cfa;
}

/* 1.2 Thème Sombre (Rendu Premium) - RESTAURÉ AUX COULEURS ORIGINALES */
.dark-mode {
  --brand-primary: #5c7cfa; 
  --brand-accent: #ccff00; 

  --bg-base: #5c7cfa; 
  --bg-gradient: linear-gradient(135deg, #6a85f7, #5c7cfa, #4a6ee0); 
  --bg-surface: rgba(0, 0, 0, 0.15); 
  --bg-header: #4a6ee0;

  --text-main: #ffffff; 
  --text-muted: #e2e8f0; 
  --text-inverse: #ffffff;

  --border-color: rgba(255, 255, 255, 0.15); 
  --glass-shadow: 0 8px 32px 0 rgba(0, 0, 0, 0.2);
  
  /* Variables adaptatives pour le mode sombre */
  --input-bg: rgba(0, 0, 0, 0.2);
  --input-text: #ffffff;
  --placeholder-color: rgba(255, 255, 255, 0.6); /* Blanc transparent */
  --bot-msg-bg: rgba(255, 255, 255, 0.1); /* Transparence stylée */
  --bot-msg-text: #ffffff;
  --btn-outline-color: #ccff00; /* Retour au beau fluo */

  --toggle-bg: rgba(255, 255, 255, 0.1);
  --toggle-text: #ffffff;
  --toggle-border: rgba(255, 255, 255, 0.3);
}

/* Boutons Langue/Thème */
.toggle-btn {
    background: var(--toggle-bg);
    color: var(--toggle-text);
    border: 1px solid var(--toggle-border);
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 14px;
    cursor: pointer;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.3s ease;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
}
.toggle-btn:hover {
    transform: scale(1.05);
}

/* ==========================================================================
   2. RESET & STRUCTURE GLOBALE (Bouclier Anti-Défilement & Edge-to-Edge)
   ========================================================================== */
html, body {
    position: fixed; 
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100vw;
    height: var(--app-height, 100vh); 
    background: var(--bg-gradient) !important; 
    color: var(--text-main);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden; 
    margin: 0; padding: 0;
    overscroll-behavior-y: none;
}

#app-screen {
    display: flex;
    flex-direction: column;
    height: 100%; 
    width: 100%;
}

.screen.hidden, .view.hidden { display: none !important; }

/* ==========================================================================
   3. ÉCRAN DE CONNEXION (Mise à jour dimensionnelle)
   ========================================================================== */
#login-screen {
    background: var(--bg-gradient) !important; 
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100vw; 
    height: var(--app-height, 100vh); 
    
    display: flex;
    flex-direction: column;
    padding: 20px; 
    box-sizing: border-box;

    overflow-y: auto;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.login-container {
    position: relative; 
    width: 100%;
    max-width: 450px;
    margin: auto; 
    background: var(--bg-surface) !important;  
    backdrop-filter: blur(16px) !important; 
    -webkit-backdrop-filter: blur(16px) !important;
    border: 1px solid var(--border-color) !important;
    border-radius: 16px;
    box-shadow: var(--glass-shadow);
    padding: 35px 25px;
    box-sizing: border-box;
    z-index: 10;
}

.login-container::before {
    content: '';
    position: absolute;
    top: -1px; right: -1px; 
    width: 45px; height: 45px; 
    border-top: 3px solid var(--brand-accent);
    border-right: 3px solid var(--brand-accent);
    border-top-right-radius: 16px; 
    pointer-events: none;
}

.login-container::after {
    content: '';
    position: absolute;
    bottom: -1px; left: -1px; 
    width: 45px; height: 45px; 
    border-bottom: 3px solid var(--brand-accent);
    border-left: 3px solid var(--brand-accent);
    border-bottom-left-radius: 16px; 
    pointer-events: none;
}

.login-container h2 { color: var(--text-main) !important; }

.form-wrapper {
    display: flex;
    flex-direction: column;
    gap: 16px;
    width: 100%;
}

.input-group {
    display: flex;
    flex-direction: column; 
    text-align: left;
}

.login-container label { 
    margin-bottom: 8px;
    font-size: 14px;
    color: var(--text-muted) !important; 
}

.login-container input {
    background: var(--input-bg) !important;
    border: 1px solid var(--border-color) !important;
    color: var(--input-text) !important; 
    padding: 12px;
    border-radius: 8px;
    width: 100%;
    box-sizing: border-box;
    font-size: 16px;
    outline: none;
    transition: border-color 0.3s;
}

/* Placeholders dynamiques */
.login-container input::placeholder, 
#search-input::placeholder,
#message-input::placeholder { 
    color: var(--placeholder-color) !important; 
}

.login-container input:focus { border-color: var(--brand-primary) !important; }

.custom-select { position: relative; width: 100%; }
.select-selected {
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    color: var(--input-text);
    padding: 12px;
    border-radius: 8px;
    font-size: 16px;
    cursor: pointer;
    user-select: none;
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.select-selected::after { content: '▼'; font-size: 10px; color: var(--text-muted); }
.select-items {
    position: absolute; top: 100%; left: 0; right: 0;
    z-index: 9999; margin-top: 5px;
    background: var(--brand-primary);
    border: 1px solid var(--border-color);
    border-radius: 8px; overflow: hidden;
    box-shadow: 0 15px 30px rgba(0,0,0,0.4);
}
.select-hide { display: none; }
.select-items div {
    color: #ffffff; 
    padding: 12px 15px; cursor: pointer;
    font-size: 15px; border-bottom: 1px solid rgba(255,255,255,0.1);
    transition: background 0.2s;
}
.select-items div:hover { background: rgba(0,0,0,0.2); }

/* Bouton Submit adaptatif (Bleu en clair, Fluo en sombre) */
#submit-onboarding {
    width: 100%; padding: 15px; margin-top: 10px;
    background: transparent !important; 
    color: var(--btn-outline-color) !important;
    border: 2px solid var(--btn-outline-color) !important; 
    border-radius: 10px;
    font-size: 18px; font-weight: bold; cursor: pointer; transition: all 0.3s ease;
}
#submit-onboarding:hover {
    background: var(--btn-outline-color) !important; 
    color: #0f172a !important; 
    box-shadow: 0 0 15px rgba(204, 255, 0, 0.4);
}
/* En mode clair, au survol, le texte doit être blanc pour contraster avec le fond bleu */
:root:not(.dark-mode) #submit-onboarding:hover {
    color: #ffffff !important;
    box-shadow: 0 0 15px rgba(92, 124, 250, 0.4);
}

/* ==========================================================================
   4. EN-TÊTE (Header)
   ========================================================================== */
.app-header {
    background: var(--bg-header); color: var(--text-main);
    padding: 15px; padding-top: calc(env(safe-area-inset-top) + 15px); 
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1); z-index: 1000;
    display: flex; justify-content: space-between; align-items: center;
}
.dark-mode .app-header { color: var(--text-inverse); }

.bot-info { display: flex; align-items: center; gap: 10px; }
.status-dot {
    width: 10px; height: 10px; background-color: var(--brand-status);
    border-radius: 50%; box-shadow: 0 0 8px var(--brand-status);
}

/* ==========================================================================
   5. CONTENU CENTRAL (Chat & Coffre)
   ========================================================================== */
#main-content {
    flex-grow: 1; 
    display: flex; 
    flex-direction: column;
    overflow: hidden; 
    padding-bottom: calc(75px + env(safe-area-inset-bottom)); 
    background: var(--bg-base);
}

#view-chat { 
    display: flex; 
    flex-direction: column; 
    flex: 1; 
    height: 100%;
    min-height: 0; 
}

.chat-history { 
    flex: 1; 
    padding: 15px; 
    display: flex; 
    flex-direction: column; 
    gap: 12px; 
    overflow-y: auto; 
    -webkit-overflow-scrolling: touch; 
}

.message {
    padding: 12px 16px; border-radius: 18px; max-width: 85%;
    font-size: 15px; line-height: 1.4; word-wrap: break-word;
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
}

.message.client { 
    background: var(--bg-surface); color: var(--text-main);
    align-self: flex-end; border-bottom-right-radius: 4px;
    border: 1px solid var(--border-color);
}

/* Bulles Bot lisibles dans les deux modes */
.message.bot {
    background: var(--bot-msg-bg); 
    color: var(--bot-msg-text);
    align-self: flex-start; border-bottom-left-radius: 4px;
}

/* ==========================================================================
   5.5 BOUCLIER ANTI-DÉBORDEMENT MÉDIAS
   ========================================================================== */
.media-container { 
    background: rgba(0,0,0,0.05); 
    padding: 8px; 
    border-radius: 10px; 
    margin-top: 5px; 
}
.dark-mode .media-container {
    background: rgba(255,255,255,0.05);
}
.chat-image-preview { 
    max-height: 220px; 
    width: 100%; 
    object-fit: cover; 
    border-radius: 8px; 
}
.chat-pdf-link { 
    display: block; 
    overflow: hidden; 
    text-overflow: ellipsis; 
    white-space: nowrap; 
    max-width: 100%; 
    background: var(--bg-surface); 
    padding: 8px 12px; 
    border-radius: 6px; 
    margin-top: 5px;
    border: 1px solid var(--border-color);
    color: var(--text-main);
}

.chat-input-area {
    padding: 10px 15px; 
    background: var(--bg-surface); backdrop-filter: blur(10px);
    border-top: 1px solid var(--border-color); display: flex; gap: 10px;
    flex-shrink: 0; 
    align-items: center; 
}

#view-vault { 
    padding: 20px; 
    overflow-y: auto; height: 100%; box-sizing: border-box;
}

/* ==========================================================================
   6. BARRE DE NAVIGATION INFÉRIEURE
   ========================================================================== */
.bottom-nav {
    position: fixed; bottom: 0; left: 0; width: 100%;
    background: var(--bg-surface); backdrop-filter: blur(15px); -webkit-backdrop-filter: blur(15px);
    border-top: 1px solid var(--border-color); display: flex; justify-content: space-around;
    padding-bottom: calc(env(safe-area-inset-bottom) + 15px); padding-top: 15px; z-index: 1000;
}
.nav-item {
    flex: 1; background: none; border: none; padding: 8px 0; font-size: 12px;
    color: var(--text-muted); display: flex; flex-direction: column; align-items: center;
    gap: 4px; cursor: pointer; transition: color 0.2s; user-select: none;
}
.nav-item.active { 
    color: var(--btn-outline-color); 
    font-weight: bold; 
}

/* ==========================================================================
   7. RECHERCHE IA
   ========================================================================== */
.search-icon-left {
    font-size: 20px;
    opacity: 0.7;
    margin-right: 10px;
    flex-shrink: 0;
    color: var(--text-main);
}

#search-input {
    flex: 1; 
    background: transparent;
    border: none;
    color: var(--input-text) !important;
    outline: none;
    font-size: 16px;
    min-width: 0; 
    padding: 10px 10px 10px 0; 
}

#search-btn {
    background: var(--brand-accent, #ccff00);
    color: #0A1118;
    border: none;
    border-radius: 30px; 
    padding: 12px 20px;
    font-weight: bold;
    font-size: 14px;
    cursor: pointer;
    white-space: nowrap; 
    flex-shrink: 0; 
    transition: transform 0.1s;
}

#search-btn:active {
    transform: scale(0.95);
}

#search-results-area::-webkit-scrollbar { width: 6px; }
#search-results-area::-webkit-scrollbar-track { background: transparent; }
#search-results-area::-webkit-scrollbar-thumb { background: rgba(0, 0, 0, 0.2); border-radius: 10px; }
.dark-mode #search-results-area::-webkit-scrollbar-thumb { background: rgba(255, 255, 255, 0.2); }

/* ==========================================================================
   8. TOGGLE PARTICULIER / ORGANISATION
   ========================================================================== */
.onboarding-type-toggle {
    display: flex;
    position: relative;
    background: var(--input-bg);
    border: 1px solid var(--border-color);
    border-radius: 30px;
    padding: 5px;
    margin-bottom: 20px;
    overflow: hidden;
}

.toggle-option {
    flex: 1;
    text-align: center;
    padding: 10px 0;
    font-size: 14px;
    font-weight: bold;
    color: var(--text-muted);
    cursor: pointer;
    position: relative;
    z-index: 2;
    transition: color 0.3s ease;
    user-select: none;
}

.toggle-option.active {
    color: #0A1118 !important; 
}

.toggle-slider {
    position: absolute;
    top: 5px;
    bottom: 5px;
    left: 5px;
    width: calc(50% - 5px);
    background: var(--brand-accent, #ccff00);
    border-radius: 25px;
    transition: transform 0.4s cubic-bezier(0.25, 1, 0.5, 1);
    z-index: 1;
    box-shadow: 0 4px 15px rgba(204, 255, 0, 0.3);
}

.onboarding-type-toggle[data-active="organisation"] .toggle-slider {
    transform: translateX(100%);
}

/* ==========================================================================
   9. ANIMATIONS & SPECIFIQUES
   ========================================================================== */
.auth-step {
    display: none;
    opacity: 0;
    transform: translateY(15px);
}

.auth-step.active {
    display: block;
    animation: slideUpFade 0.5s cubic-bezier(0.25, 1, 0.5, 1) forwards;
}

@keyframes slideUpFade {
    0% { opacity: 0; transform: translateY(15px); }
    100% { opacity: 1; transform: translateY(0); }
}

/* Flèche du select visible en mode clair et sombre */
#country-code {
    appearance: none;
    -webkit-appearance: none;
    background-image: url('data:image/svg+xml;utf8,<svg fill="%23888888" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/><path d="M0 0h24v24H0z" fill="none"/></svg>');
    background-repeat: no-repeat;
    background-position: right 8px center;
    padding-right: 30px !important;
    color: var(--input-text) !important;
}
.dark-mode #country-code {
    background-image: url('data:image/svg+xml;utf8,<svg fill="%23ffffff" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg"><path d="M7 10l5 5 5-5z"/><path d="M0 0h24v24H0z" fill="none"/></svg>');
}

input[id*="pin"] {
    letter-spacing: 8px !important;
    font-weight: bold;
    font-size: 24px !important;
    font-family: monospace;
}

input[id*="pin"]::placeholder {
    letter-spacing: 5px !important;
    font-size: 20px !important;
}

#generated-recovery-key {
    user-select: all; 
    -webkit-user-select: all;
    text-shadow: 0 0 10px rgba(0, 255, 163, 0.3);
}

/* ==============================================================================
   FIX : ANTI-DÉBORDEMENT DE LA MODALE CLASSIQUE
   ============================================================================== */
#custom-alert-box {
    max-width: 90%; 
    width: 320px;
    padding: 20px;
    box-sizing: border-box; 
    word-wrap: break-word; 
    text-align: center;
}


/* ==========================================================================
   BOUTON RETOUR (Login / Inscription)
   ========================================================================== */
.btn-retour {
    background: rgba(255, 255, 255, 0.1); 
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: var(--text-main);
    padding: 8px 18px;
    border-radius: 30px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    transition: all 0.3s cubic-bezier(0.25, 1, 0.5, 1);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
}

:root:not(.dark-mode) .btn-retour {
    background: rgba(0, 0, 0, 0.05);
    border: 1px solid rgba(0, 0, 0, 0.1);
}

.btn-retour:hover {
    background: var(--brand-accent);
    color: #0f172a;
    border-color: var(--brand-accent);
    transform: translateX(-5px); 
    box-shadow: 0 4px 15px rgba(204, 255, 0, 0.4);
}

/* ==========================================================================
   BOUTON DÉCONNEXION (Profil)
   ========================================================================== */
.logout-btn {
    background: rgba(255, 77, 77, 0.1);
    color: #ff4d4d;
    border: 1px solid rgba(255, 77, 77, 0.3);
    padding: 12px 20px;
    border-radius: 12px;
    font-size: 16px;
    font-weight: bold;
    width: 100%;
    cursor: pointer;
    transition: all 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
}

.logout-btn:hover {
    background: #ff4d4d;
    color: #ffffff;
    box-shadow: 0 4px 15px rgba(255, 77, 77, 0.4);
}