/* --- Global Styles & Variables --- */
:root {
    --font-family: 'Poppins', sans-serif;
    
    /* Light Theme */
    --primary-color: #0a67c2;
    --secondary-color: #004182;
    --accent-color: #4ade80;
    --background-page: #f0f2f5;
    --background-panel: #ffffff;
    --text-primary: #111827;
    --text-secondary: #4b5563;
    --border-color: #d1d5db;
    --correct-answer: #16a34a;
    --wrong-answer: #dc2626;
    --shadow: 0 10px 30px -15px rgba(0, 0, 0, 0.2);
}

body.dark-mode {
    /* Dark Theme */
    --primary-color: #2563eb;
    --secondary-color: #3b82f6;
    --accent-color: #4ade80;
    --background-page: #111827;
    --background-panel: #1f2937;
    --text-primary: #f9fafb;
    --text-secondary: #9ca3af;
    --border-color: #4b5563;
    --correct-answer: #22c55e;
    --wrong-answer: #f87171;
    --shadow: 0 10px 30px -15px rgba(0, 0, 0, 0.5);
}

*, *::before, *::after { 
    box-sizing: border-box; 
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

body {
    font-family: var(--font-family);
    background-color: var(--background-page);
    color: var(--text-primary);
    margin: 0;
    padding: 2rem;
    display: grid;
    place-items: center;
    min-height: 100vh;
    overflow-y: auto; /* Permette lo scroll su schermi piccoli */
    transition: background-color 0.3s, color 0.3s;
    /* Subtle background pattern */
    background-image: radial-gradient(var(--text-secondary) 0.5px, transparent 0.5px), radial-gradient(var(--text-secondary) 0.5px, var(--background-page) 0.5px);
    background-size: 20px 20px;
    background-position: 0 0, 10px 10px;
}

.app-wrapper {
    width: 100%;
    max-width: 900px;
    height: 90vh;
    max-height: 95vh;
    background-color: var(--background-panel);
    border-radius: 1.5rem;
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: background-color 0.3s;
}

/* --- Header --- */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem 1.5rem;
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
    transition: border-color 0.3s;
}

.logo a {
    text-decoration: none;
    color: var(--text-primary);
    font-size: 1.25rem;
    font-weight: 700;
}

.logo strong {
    color: var(--primary-color);
    font-weight: 700;
}

/* --- Dark Mode Toggle --- */
.theme-switch { position: relative; display: inline-block; width: 50px; height: 26px; }
.theme-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: 26px; }
.slider:before { position: absolute; content: ""; height: 18px; width: 18px; left: 4px; bottom: 4px; background-color: white; transition: .4s; border-radius: 50%; background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="%23fcc419" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>'); background-repeat: no-repeat; background-position: center; }
input:checked + .slider { background-color: var(--primary-color); }
input:checked + .slider:before { transform: translateX(24px); background-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="%23fcc419" stroke="none"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>'); }

/* --- App Main Content --- */
.app { flex-grow: 1; height: 100%; overflow: hidden; position: relative; padding: 0 1rem; }
.view-container { position: relative; height: 100%; }
.view { position: absolute; width: 100%; height: 100%; top: 0; left: 0; padding: 1.5rem; display: flex; flex-direction: column; justify-content: center; text-align: center; opacity: 0; visibility: hidden; transition: opacity 0.4s ease-in-out, visibility 0.4s; }
.view.active { opacity: 1; visibility: visible; z-index: 1; }

/* --- DISCLAIMER MODAL --- */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    display: grid;
    place-items: center;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s, visibility 0.3s;
}
.modal-overlay.active {
    opacity: 1;
    visibility: visible;
}
.modal-content {
    background: var(--background-panel);
    padding: 2rem;
    border-radius: 1rem;
    max-width: 500px;
    width: 90%;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}
.modal-content h2 { color: var(--text-primary); }
.modal-content p { color: var(--text-secondary); line-height: 1.6; }

/* --- BOTTONI PRESTIGE --- */
.primary-btn, .secondary-btn, .difficulty-btn, .topic-btn, .answer-btn, .share-btn {
    padding: 0.8rem 1.5rem; 
    font-size: 1rem; 
    font-weight: 600; 
    border-radius: 0.5rem;
    cursor: pointer; 
    border: 1px solid transparent; 
    text-decoration: none; 
    display: inline-block;
    transition: all 0.2s ease-out; 
    user-select: none;
}
.primary-btn:active, .secondary-btn:active, .difficulty-btn:active, .topic-btn:active, .answer-btn:active {
    transform: translateY(2px);
    box-shadow: none;
}
.primary-btn, .share-btn, .difficulty-btn.special {
    background-color: var(--primary-color);
    background-image: linear-gradient(to top, rgba(0,0,0,0.1), rgba(255,255,255,0.1));
    color: white;
    box-shadow: 0 4px 15px -5px var(--primary-color);
}
.primary-btn:hover, .share-btn:hover, .difficulty-btn.special:hover {
    background-color: var(--secondary-color);
    box-shadow: 0 6px 20px -5px var(--primary-color);
}
.secondary-btn {
    background-color: var(--background-panel);
    border: 1px solid var(--border-color);
    color: var(--text-secondary);
    box-shadow: 0 4px 15px -5px rgba(0,0,0,0.1);
}
.secondary-btn:hover { 
    border-color: var(--primary-color); 
    color: var(--primary-color); 
}
#next-btn { 
    margin-top: auto; 
}
#next-btn:disabled { 
    background: var(--border-color); 
    background-image: none;
    color: var(--text-secondary); 
    box-shadow: none; 
    cursor: not-allowed; 
}

/* Specifiche Stili per Elementi */
h1 { color: var(--text-primary); font-weight: 700; margin: 0; animation: fadeIn 0.5s both; }
.subtitle { color: var(--text-secondary); font-size: 1.1rem; margin: 0.5rem 0 2.5rem 0; animation: fadeIn 0.5s 0.1s both; }
.back-btn { background: none; border: none; font-size: 2rem; position: absolute; top: 1rem; left: 1.5rem; cursor: pointer; color: var(--text-secondary); transition: color 0.2s; padding: 0; }
.back-btn:hover { color: var(--primary-color); }
#selected-topic-name { color: var(--primary-color); }

/* Viste */
.topic-selection, #difficulty-selection { animation: fadeIn 0.5s 0.2s both; }
.topic-selection { display: grid; grid-template-columns: 1fr; gap: 1rem; }
@media(min-width: 600px) { .topic-selection { grid-template-columns: 1fr 1fr; } }
#difficulty-selection { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.75rem; }

.topic-btn, .difficulty-btn { transition: all 0.3s ease; }
.topic-btn.active, .difficulty-btn.active {
    transform: translateY(-5px);
    box-shadow: 0 8px 25px -8px var(--primary-color);
    border-color: var(--primary-color);
}

.topic-btn { text-align: left; background-color: var(--background-page); }
.topic-btn:hover { transform: translateY(-5px); box-shadow: var(--shadow); border-color: var(--primary-color); }
.topic-btn h3 { color: var(--primary-color); margin: 0 0 0.25rem 0; }
.topic-btn p { color: var(--text-secondary); margin: 0; }

.difficulty-btn { border: 1px solid var(--primary-color); color: var(--primary-color); background: transparent; box-shadow: none; }
.difficulty-btn:hover { color: white; background: var(--primary-color); }

.answer-btn { text-align: left; background-color: var(--background-page); border: 1px solid var(--border-color); width: 100%; box-shadow: none; color: var(--text-primary); }
.answer-btn:hover:not(:disabled) { border-color: var(--primary-color); background-color: var(--background-panel); }
.answer-btn.correct, .answer-btn.correct:hover { background-color: var(--correct-answer); color: white; border-color: var(--correct-answer); }
.answer-btn.wrong, .answer-btn.wrong:hover { background-color: var(--wrong-answer); color: white; border-color: var(--wrong-answer); }
.answer-btn:disabled { cursor: not-allowed; }

#answers-container { display: grid; gap: 1rem; width: 100%; margin-top: 1.5rem; margin-bottom: 1.5rem; animation: fadeIn 0.4s 0.1s both; }
@media(min-width: 768px) { #answers-container { grid-template-columns: 1fr 1fr; } }

/* Quiz & Results */
.quiz-header { margin-bottom: 2rem; animation: fadeIn 0.4s both; }
#progress-bar-container { width: 100%; height: 8px; background-color: var(--border-color); border-radius: 4px; margin-bottom: 0.5rem; overflow: hidden; }
#progress-bar { height: 100%; width: 0%; background: var(--primary-color); border-radius: 4px; transition: width 0.4s ease; }
#question-counter, #result-message { color: var(--text-secondary); }
#question-text, #result-title { margin: 0 auto 1.5rem auto; max-width: 95%; }

#result-view { justify-content: center; }
#score-visual { margin-bottom: 1.5rem; position: relative; width: 160px; height: 160px; margin-left: auto; margin-right: auto; }
.score-ring { transform: rotate(-90deg); }
.score-ring__background { stroke: var(--border-color); fill: transparent; }
.score-ring__progress { stroke: var(--accent-color); fill: transparent; stroke-linecap: round; transition: stroke-dashoffset 1s ease-out; }
#score-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 2rem; font-weight: 700; color: var(--text-primary); }
.result-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 1rem; margin-top: 2rem; }

#confetti-canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1; }

/* --- Review View --- */
#review-view { justify-content: flex-start; text-align: left; }
.review-header { display: flex; align-items: center; width: 100%; margin-bottom: 1.5rem; }
.review-header h1 { margin-left: 1rem; }
.back-to-results-btn { background: none; border: 1px solid var(--border-color); color: var(--text-secondary); padding: 0.5rem 1rem; border-radius: 0.5rem; cursor: pointer; transition: all 0.2s; }
.back-to-results-btn:hover { border-color: var(--primary-color); color: var(--primary-color); }
#review-container { width: 100%; overflow-y: auto; padding-right: 1rem; }
.review-item { margin-bottom: 2rem; border-bottom: 1px solid var(--border-color); padding-bottom: 1.5rem; }
.review-item:last-child { border-bottom: none; }
.review-question { font-weight: 600; margin-bottom: 1rem; }
.review-options button { width: 100%; text-align: left; margin-bottom: 0.5rem; padding: 0.75rem; border-radius: 0.5rem; border: 1px solid var(--border-color); background-color: var(--background-page); color: var(--text-primary); }
.review-options button.correct { background-color: var(--correct-answer); color: white; border-color: var(--correct-answer); }
.review-options button.wrong { background-color: var(--wrong-answer); color: white; border-color: var(--wrong-answer); }
.review-explanation { margin-top: 1rem; padding: 1rem; background-color: var(--background-page); border-radius: 0.5rem; border-left: 4px solid var(--primary-color); color: var(--text-secondary); font-size: 0.9rem; }

/* --- Instant Explanation & Loader --- */
#instant-explanation { 
    margin-top: 1.5rem; 
    padding: 1rem; 
    background-color: var(--background-page); 
    border-radius: 0.5rem; 
    border-left: 4px solid var(--primary-color);
    color: var(--text-secondary); 
    font-size: 0.95rem;
    height: 0;
    opacity: 0;
    overflow: hidden;
    transition: all 0.4s ease-out;
}
#instant-explanation.visible {
    height: auto;
    opacity: 1;
    padding: 1rem;
}

.loader {
    width: 48px;
    height: 48px;
    border: 5px solid var(--primary-color);
    border-bottom-color: transparent;
    border-radius: 50%;
    display: none; /* Hidden by default */
    box-sizing: border-box;
    animation: rotation 1s linear infinite;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

@keyframes rotation {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

.home-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-secondary);
    transition: color 0.2s;
    padding: 0;
    margin-right: 1rem;
}

.home-btn:hover {
    color: var(--primary-color);
}