@import url('https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700&display=swap');

:root {
    --color-green: #10b981;
    --color-yellow: #f59e0b;
    --color-red: #ef4444;
    --bg-white: #fafafa;
    
    /* Modern UI colors */
    --primary: #4f46e5;
    --primary-hover: #4338ca;
    --text-main: #1f2937;
    --text-muted: #6b7280;
    --border-color: #e5e7eb;
}

/* Reset e Box Sizing globale per evitare overflow nativi */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    font-family: 'Plus Jakarta Sans', sans-serif;
    margin: 0;
    background: radial-gradient(circle at top right, #f3f4f6 0%, #e5e7eb 100%);
    display: flex;
    justify-content: center;
    height: 100vh;
    height: 100dvh; /* Sfrutta la dimensione dinamica reale escludendo le barre del browser */
    align-items: center;
    color: var(--text-main);
    -webkit-font-smoothing: antialiased;
}

/* Layout principale: su desktop mantiene il box, su mobile diventa full screen */
.app-container {
    width: 100%;
    max-width: 850px;
    height: 90vh;
    background: #ffffff;
    border-radius: 20px;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.06), 0 1px 3px rgba(0, 0, 0, 0.01);
    display: flex;
    flex-direction: column;
    border: 1px solid rgba(255, 255, 255, 0.8);
}

header {
    padding: 20px 24px;
    background: #0f172a;
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    z-index: 20;
}

header h1 {
    font-size: 1.25rem;
    font-weight: 700;
    margin: 0;
    letter-spacing: -0.025em;
    background: linear-gradient(to right, #ffffff, #cbd5e1);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

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

.controls label {
    font-size: 0.875rem;
    color: #94a3b8;
    font-weight: 500;
}

#context-limit {
    background: #1e293b;
    color: white;
    border: 1px solid #334155;
    padding: 6px 12px;
    border-radius: 8px;
    font-family: inherit;
    font-size: 0.875rem;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s;
}

#context-limit:focus {
    border-color: #6366f1;
}

#chat-container {
    flex-grow: 1;
    overflow-y: auto;
    padding: 30px 24px;
    background-color: #f8fafc;
    display: flex;
    flex-direction: column;
    scroll-behavior: smooth;
    gap: 8px;
}

/* Messaggi standard */
.message {
    padding: 14px 20px;
    border-radius: 16px;
    position: relative;
    max-width: 75%;
    line-height: 1.6;
    font-size: 0.95rem;
    background: white;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.03);
    border: 1px solid rgba(0, 0, 0, 0.05);
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    word-break: break-word;
    margin-bottom: 20px;
}

.message.user {
    align-self: flex-end;
    background-color: #4f46e5;
    color: white;
    border-color: transparent;
    border-bottom-right-radius: 4px;
    box-shadow: 0 6px 16px rgba(79, 70, 229, 0.1);
}

.message.assistant {
    align-self: flex-start;
    background-color: #ffffff;
    color: var(--text-main);
    border-bottom-left-radius: 4px;
}

/* Markdown Rendering */
.message.assistant p {
    margin-top: 0;
    margin-bottom: 10px;
}
.message.assistant p:last-child {
    margin-bottom: 0;
}
.message.assistant pre {
    background: #f1f5f9;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    border: 1px solid #e2e8f0;
    margin: 8px 0;
}
.message.assistant code {
    background: #f1f5f9;
    padding: 2px 6px;
    border-radius: 4px;
    font-family: 'Courier New', Courier, monospace;
    font-size: 0.9em;
    border: 1px solid #e2e8f0;
    color: #e11d48;
}

/* Messaggi fuori dalla finestra (Dimenticati) */
.dropped-header {
    text-align: center;
    color: #94a3b8;
    font-size: 0.75rem;
    font-weight: 700;
    text-transform: uppercase;
    margin: 20px 0 15px 0;
    letter-spacing: 0.05em;
    background: #f1f5f9;
    padding: 8px 16px;
    border-radius: 20px;
    align-self: center;
    border: 1px solid #e2e8f0;
}

.message.dropped {
    opacity: 0.45;
    filter: grayscale(100%);
    border: 1px dashed #d1d5db;
    background-color: rgba(243, 244, 246, 0.6);
    box-shadow: none;
    align-self: center;
    max-width: 85%;
}

.message.dropped .token-badge {
    background: #9ca3af;
    color: white;
    box-shadow: none;
}

/* IL BOX DELLA FINESTRA DI CONTESTO */
.context-window-box {
    border: 3px solid #cbd5e1;
    border-radius: 20px;
    padding: 0px 20px 12px 20px;
    margin-top: 10px;
    position: relative;
    display: flex;
    flex-direction: column;
    background-color: rgba(255, 255, 255, 0.7);
    backdrop-filter: blur(8px);
    transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.context-window-box.green {
    border-color: var(--color-green);
    background-color: rgba(16, 185, 129, 0.02);
    box-shadow: 0 10px 25px rgba(16, 185, 129, 0.05);
}
.context-window-box.yellow {
    border-color: var(--color-yellow);
    background-color: rgba(245, 158, 11, 0.02);
    box-shadow: 0 10px 25px rgba(245, 158, 11, 0.05);
}
.context-window-box.red {
    border-color: var(--color-red);
    background-color: rgba(239, 68, 68, 0.02);
    box-shadow: 0 10px 25px rgba(239, 68, 68, 0.05);
}

/* RISOLUZIONE MOBILE: L'header diventa STICKY (Fisso in alto durante lo scroll) */
.context-window-header {
    position: sticky;
    top: -30px; /* Si ancora perfettamente al top del chat container padding */
    background: #ffffff; /* Copre i messaggi che scorrono sotto */
    z-index: 10;
    margin-left: -20px;
    margin-right: -20px;
    margin-bottom: 25px; /* <-- NUOVA RIGA: Crea lo spazio vitale per i badge */
    padding: 18px 20px;
    border-bottom: 2px dashed #cbd5e1;
    border-top-left-radius: 16px;
    border-top-right-radius: 16px;
    text-align: center;
    font-weight: 700;
    font-size: 0.8rem;
    box-shadow: 0 4px 6px rgba(0,0,0,0.02);
    display: flex;
    flex-direction: column;
    gap: 8px;
    transition: all 0.4s ease;
}

.context-window-box.green .context-window-header {
    border-bottom-color: var(--color-green);
    color: var(--color-green);
}
.context-window-box.yellow .context-window-header {
    border-bottom-color: var(--color-yellow);
    color: #d97706;
}
.context-window-box.red .context-window-header {
    border-bottom-color: var(--color-red);
    color: var(--color-red);
}

.header-text {
    letter-spacing: 0.075em;
    text-transform: uppercase;
    font-size: 0.8rem;
}

/* La barra di progresso interna al box */
.progress-container {
    width: 90%;
    margin: 0 auto;
    height: 8px;
    background-color: #e2e8f0;
    border-radius: 4px;
    overflow: hidden;
    box-shadow: inset 0 1px 2px rgba(0,0,0,0.05);
}
.progress-bar {
    height: 100%;
    transition: width 0.4s ease, background-color 0.4s ease;
}
.progress-bar.green { background-color: var(--color-green); }
.progress-bar.yellow { background-color: var(--color-yellow); }
.progress-bar.red { background-color: var(--color-red); }

/* Badge dei Token */
.token-badge {
    position: absolute;
    top: -12px;
    right: -5px; /* Leggermente più rientrato per non sbattere sui bordi del telefono */
    background: #f59e0b;
    color: white;
    font-size: 11px;
    padding: 3px 10px;
    border-radius: 20px;
    font-weight: 700;
    box-shadow: 0 4px 6px rgba(245, 158, 11, 0.2);
    letter-spacing: -0.01em;
    z-index: 5; /* Assicura che il badge stia sempre sopra gli altri bordi */
}
.message.user .token-badge {
    left: -5px;
    right: auto;
    background: #3b82f6;
    box-shadow: 0 4px 6px rgba(59, 130, 246, 0.2);
}

/* Input area inferiore */
.input-area {
    display: flex;
    padding: 20px 24px;
    background: #ffffff;
    border-top: 1px solid var(--border-color);
    align-items: center;
    gap: 12px;
    box-shadow: 0 -10px 20px rgba(0, 0, 0, 0.02);
    z-index: 20;
}
#user-input {
    flex-grow: 1;
    resize: none;
    padding: 14px 16px;
    border: 1px solid var(--border-color);
    border-radius: 12px;
    height: 48px;
    font-family: inherit;
    font-size: 16px; /* 16px previene lo zoom forzato su iOS */
    outline: none;
    transition: all 0.2s;
    background-color: #f9fafb;
}
#user-input:focus {
    border-color: var(--primary);
    background-color: #ffffff;
    box-shadow: 0 0 0 4px rgba(79, 70, 229, 0.1);
}
#send-btn {
    padding: 0 24px;
    height: 48px;
    background: #10b981;
    color: white;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 600;
    font-size: 0.95rem;
    transition: all 0.2s;
    box-shadow: 0 4px 12px rgba(16, 185, 129, 0.15);
}
#send-btn:hover {
    background: #059669;
    box-shadow: 0 6px 16px rgba(16, 185, 129, 0.25);
    transform: translateY(-1px);
}
#send-btn:active {
    transform: translateY(0);
}
#send-btn:disabled {
    background: #9ca3af;
    cursor: not-allowed;
    box-shadow: none;
    transform: none;
}

/* Custom scrollbar for chat container */
#chat-container::-webkit-scrollbar {
    width: 6px;
}
#chat-container::-webkit-scrollbar-track {
    background: transparent;
}
#chat-container::-webkit-scrollbar-thumb {
    background-color: rgba(0, 0, 0, 0.1);
    border-radius: 3px;
}
#chat-container::-webkit-scrollbar-thumb:hover {
    background-color: rgba(0, 0, 0, 0.2);
}

/* ==========================================
   MEDIA QUERY SPECIFICA PER SMARTPHONE
   ========================================== */
@media (max-width: 768px) {
    body {
        background: white;
    }

    .app-container {
        height: 100vh;
        height: 100dvh; /* Sfrutta la dimensione dinamica reale escludendo le barre del browser */
        border-radius: 0;
        box-shadow: none;
        border: none;
    }

    header {
        padding: 12px 16px;
    }

    header h1 {
        font-size: 1.1rem;
    }

    .controls label {
        font-size: 0.8rem;
    }

    #chat-container {
        padding: 15px 12px;
    }

    .message {
        max-width: 85%; /* Più largo su mobile per ottimizzare lo spazio di lettura */
        padding: 10px 14px;
        font-size: 0.9rem;
        margin-bottom: 12px;
        border-radius: 12px;
    }

    .context-window-box {
        padding: 0 12px 8px 12px;
        border-radius: 16px;
    }

    .context-window-header {
        font-size: 0.75rem;
        padding: 12px 10px;
        top: -15px; /* Si ancora alla nuova altezza del padding */
        margin-left: -12px;
        margin-right: -12px;
        border-top-left-radius: 12px;
        border-top-right-radius: 12px;
    }

    .progress-container {
        width: 100%;
    }

    .input-area {
        padding: 10px 12px;
    }

    #user-input {
        height: 42px;
        padding: 8px 12px;
        border-radius: 8px;
    }

    #send-btn {
        height: 42px;
        padding: 0 16px;
        border-radius: 8px;
        font-size: 0.9rem;
    }
}
