@import url('https://fonts.googleapis.com/css2?family=Libre+Baskerville:ital,wght@0,400;0,700;1,400&family=Playfair+Display:ital,wght@0,600;0,800;1,600&family=Inter:wght@300;400;600&display=swap');

/* =========================================
   1. IMPOSTAZIONI DI BASE & VARIABILI
   ========================================= */

:root {
    /* --- COLORI BASE (LIGHT - Stile Pergamena) --- */
    --bg-body: #fdfbf7;      /* Bianco sporco/pergamena chiarissima */
    --bg-card: #ffffff;
    --bg-sidebar: #1e1b18;   /* Inchiostro scuro/marrone caldissimo */
    --input-bg: #2a2522;

    /* --- TESTO (LIGHT) --- */
    --text-main: #2c241b;    /* Grigio/Marrone molto scuro, non nero puro */
    --text-title: #110e0a;
    --text-sidebar: #e8e3d9;
    --text-muted: #6b5c4f;

    /* --- UI ELEMENTS (L'Oro della tua icona) --- */
    --border-color: #e6dfd3;
    --accent-color: #b58d3d; /* Oro antico/Bronzo */
    --hero-gradient: linear-gradient(135deg, #1e1b18 0%, #3a2e24 100%); /* Sfondo testata elegante */

    /* --- BOX TEMATICI (LIGHT - Colori accademici desaturati) --- */
    --box-def-bg: #f4f6f3;
    --box-def-border: #ccd6c8;
    --box-law-bg: #fdf5f5;
    --box-law-border: #e6c8c8;
    --box-note-bg: #f7f5f0;
    --box-note-border: #dcd6cb;
    --box-thm-bg: #fdf8ef;
    --box-thm-border: #ebdaba;

    /* --- BOTTONI CARD (LIGHT) --- */
    --btn-read-bg: #f4efe6;
    --btn-read-text: #8c6a26;
    --btn-down-bg: #fdf5f5;
    --btn-down-text: #a83232;
}

/* APPLICA VARIABILI DARK QUANDO HTML HA L'ATTRIBUTO */
[data-theme='dark'] {
    /* --- COLORI BASE (DARK - Stile Lavagna/Biblioteca Notturna) --- */
    --bg-body: #141210;
    --bg-card: #1c1a17;
    --bg-sidebar: #0a0908;
    --input-bg: #24211e;

    /* --- TESTO (DARK) --- */
    --text-main: #d4cdc4;
    --text-title: #f5f0e6;
    --text-sidebar: #cfc8bd;
    --text-muted: #8c8276;

    /* --- UI ELEMENTS (DARK) --- */
    --border-color: #36302a;
    --accent-color: #d4af37; /* Oro più brillante per risaltare sul nero */
    --hero-gradient: linear-gradient(135deg, #0f0e0c 0%, #211c18 100%);

    /* --- BOX TEMATICI (DARK) --- */
    --box-def-bg: #162419;
    --box-def-border: #294a30;
    --box-law-bg: #2e1818;
    --box-law-border: #803030;
    --box-note-bg: #26231f;
    --box-note-border: #4d463e;
    --box-thm-bg: #2e2413;
    --box-thm-border: #997326;

    /* --- BOTTONI CARD (DARK) --- */
    --btn-read-bg: #2b2416;
    --btn-read-text: #e6c875;
    --btn-down-bg: #381515;
    --btn-down-text: #e07474;
}

* { box-sizing: border-box; }

/* FIX DEFINITIVO SFONDO */
html {
    width: 100%;
    height: 100%;
    /* L'HTML ora prende il colore di sfondo! */
    background-color: var(--bg-body);
    transition: background-color 0.3s ease;
}

body {
    margin: 0 !important;
    padding: 0 !important;
    width: 100%;
    min-height: 100vh;
    font-family: 'Inter', sans-serif;
    display: flex;

    /* Il body è trasparente o eredita, così non copre l'html */
    background-color: transparent;
    color: var(--text-main);
    transition: color 0.3s ease;
}

/* =========================================
   2. TIPOGRAFIA
   ========================================= */

/* Font SERIF (Libre Baskerville) per la lettura */
.content-wrapper p,
.content-wrapper li,
.definition, .law, .note, .thm {
    font-family: 'Libre Baskerville', serif;
    font-size: 1.15rem;
    line-height: 1.8;
    color: var(--text-main);

    /* GIUSTIFICAZIONE TESTO */
    text-align: justify;
    hyphens: auto;
    text-justify: inter-word;
}

/* Font Playfair Display per UI e Titoli (Austero ed elegante) */
h1, h2, h3, h4, .box-title {
    font-family: 'Playfair Display', serif;
}

/* Inter rimane per UI secondarie (Sidebar, bottoni, footer) */
.nav-btn, .sidebar, footer {
    font-family: 'Inter', sans-serif;
}

h1 { font-size: 2.5rem; margin-bottom: 10px; color: var(--text-title); letter-spacing: -1px; }
p { margin-bottom: 1.5em; }

/* =========================================
   3. SIDEBAR (MENU LATERALE)
   ========================================= */
.sidebar {
    width: 280px;
    min-width: 280px;
    height: 100vh;
    background-color: var(--bg-sidebar);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    padding: 20px;
    overflow-y: auto;
    overflow-x: hidden;
    transition: all 0.3s ease-in-out;
    border-right: 1px solid var(--border-color);
    z-index: 100;
}

.sidebar h3 {
    margin-top: 0;
    color: var(--text-sidebar);
    font-size: 1.4rem;
    margin-bottom: 20px;
    white-space: nowrap;
}

.sidebar.closed {
    width: 0; min-width: 0; padding: 0; opacity: 0; border: none;
}

/* LISTA CAPITOLI */
.capitoli-list { list-style: none; padding: 0; margin: 0; }
.capitoli-list li { margin-bottom: 4px; }

.capitoli-list a {
    text-decoration: none;
    color: var(--text-sidebar);
    display: block;
    padding: 10px 12px;
    border-radius: 6px;
    transition: all 0.2s;
    font-size: 0.95rem;
    white-space: normal;
    word-wrap: break-word;
    line-height: 1.4;
}

.capitoli-list a:hover {
    background-color: #2b2b36;
    color: #fff;
    transform: translateX(5px);
}

.capitoli-list a.active {
    background-color: var(--accent-color);
    color: white !important;
    font-weight: 600;
    box-shadow: 0 4px 10px rgba(0,0,0,0.2);
}

/* Search Box Sidebar */
.search-box input {
    width: 100%;
    padding: 10px;
    background: var(--input-bg);
    border: 1px solid #444;
    border-radius: 6px;
    color: white; /* Sempre bianco nella sidebar scura */
    margin-bottom: 20px;
}
.search-box input:focus { outline: 2px solid var(--accent-color); border-color: transparent; }

/* =========================================
   4. AREA CONTENUTO PRINCIPALE
   ========================================= */
.main-content {
    flex-grow: 1;
    height: 100vh;
    overflow-y: auto;
    padding: 0;
    background-color: var(--bg-body);
    position: relative;
    display: flex;
    flex-direction: column;
}

.content-wrapper {
    padding: 40px 60px;
    max-width: 1000px;
    margin: 0 auto;
    width: 100%;
    flex: 1;
}

/* =========================================
   5. BOTTONE MENU (TOGGLE)
   ========================================= */
.toggle-btn {
    position: sticky;
    top: 10px; left: 10px;
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1.8rem;
    color: var(--text-title);
    padding: 10px;
    z-index: 10;
    margin-left: 10px; margin-top: 10px;
    width: fit-content;
}
.toggle-btn:hover { color: var(--accent-color); }

/* =========================================
   6. TEXTBOOK BOXES (Definizioni, Leggi, Teoremi)
   ========================================= */
.definition, .law, .note, .thm {
    padding: 20px 25px;
    margin: 30px 0;
    border-radius: 4px;
    border-left-width: 6px;
    border-left-style: solid;
    page-break-inside: avoid;
    color: var(--text-main);
}

.definition {
    background-color: var(--box-def-bg);
    border-color: var(--box-def-border);
    border-left-color: #55705a; /* Verde desaturato e accademico */
}

.law {
    background-color: var(--box-law-bg);
    border-color: var(--box-law-border);
    border-left-color: #9e3d3d; /* Rosso scuro stile ceralacca */
}

.note {
    background-color: var(--box-note-bg);
    border-color: var(--box-note-border);
    border-left-color: #8c8276; /* Grigio caldo */
}

.thm {
    background-color: var(--box-thm-bg);
    border-color: var(--box-thm-border);
    border-left-color: var(--accent-color); /* Oro antico */
}

.box-title {
    display: block; width: 100%; font-weight: 700;
    text-transform: uppercase; font-size: 0.85rem; letter-spacing: 1px;
    margin-bottom: 12px; padding-bottom: 8px;
    border-bottom: 1px solid rgba(128,128,128, 0.2);
    color: var(--text-title);
}

/* =========================================
   7. ELEMENTI GRAFICI E NAVIGAZIONE
   ========================================= */
figure {
    display: block;
    margin: 40px auto;
    text-align: center;
    width: 100%;
}

img {
    max-width: 100%; height: auto;
    display: inline-block; margin: 0 auto;
    border-radius: 8px;
    box-shadow: 0 8px 20px rgba(0,0,0,0.1);
}

figcaption {
    display: block; width: 100%; text-align: center;
    margin-top: 15px;
    font-family: 'Inter', sans-serif;
    font-size: 0.95rem;
    color: var(--text-muted);
    font-style: italic;
    line-height: 1.4;
    padding: 0 20px;
}

.btn-pdf {
    display: inline-flex; align-items: center;
    background-color: #9e3d3d; /* Cambiato per abbinarsi al rosso ceralacca */
    color: white;
    padding: 10px 20px; text-decoration: none;
    border-radius: 50px; font-weight: 600; font-size: 0.9rem;
    box-shadow: 0 4px 10px rgba(158, 61, 61, 0.3);
}
.btn-pdf:hover { transform: translateY(-2px); box-shadow: 0 6px 15px rgba(158, 61, 61, 0.4); }

.navigazione-footer {
    margin-top: 80px; padding-top: 30px;
    border-top: 1px solid var(--border-color);
    display: flex; justify-content: space-between;
}
.nav-btn {
    text-decoration: none;
    color: var(--text-main);
    padding: 12px 25px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--bg-card);
    font-weight: 500;
}
.nav-btn:hover { border-color: var(--accent-color); color: var(--accent-color); }

/* =========================================
   8. FOOTER
   ========================================= */
.site-footer {
    width: 100%;
    background-color: var(--bg-card);
    color: var(--text-muted);
    padding: 40px 20px; text-align: center;
    border-top: 1px solid var(--border-color);
    margin-top: auto;
    font-family: 'Inter', sans-serif; font-size: 0.9rem;
}
.footer-inner { max-width: 800px; margin: 0 auto; }
.footer-links { margin: 15px 0; }
.footer-links a {
    color: var(--text-main);
    text-decoration: none; font-weight: 600; margin: 0 5px;
    transition: color 0.2s;
}
.footer-links a:hover { color: var(--accent-color); text-decoration: underline; }
.footer-links .separator { color: var(--border-color); margin: 0 5px; }
.disclaimer { font-size: 0.8rem; color: var(--text-muted); margin-top: 15px; }

/* =========================================
   9. HOME PAGE SPECIFICS
   ========================================= */
.hero-header {
    text-align: center; margin-bottom: 50px;
    padding-bottom: 30px; border-bottom: 1px solid var(--border-color);
}
/* Stile specifico per Home Mode Header (con gradiente e pattern vintage) */
.hero-header.home-mode {
    padding: 80px 20px;

    /* Disegna una finissima griglia color oro antico sovrapposta al tuo gradiente */
    background-image:
        linear-gradient(rgba(181, 141, 61, 0.07) 1px, transparent 1px),
        linear-gradient(90deg, rgba(181, 141, 61, 0.07) 1px, transparent 1px),
        var(--hero-gradient);

    /* Dimensione dei quadretti (30x30 pixel) */
    background-size: 30px 30px, 30px 30px, 100% 100%;
    background-position: center center;

    color: white;
    border-radius: 0 0 40px 40px;
    margin-bottom: 60px;

    /* Aggiunge una leggera ombra sotto la testata per farla "sollevare" dalla pagina */
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
    position: relative;
}
/* Testo Hero sempre bianco su gradiente */
.hero-header.home-mode h1 {
    color: white;
    font-size: 3.5rem; font-weight: 800;
    margin-bottom: 15px;
    -webkit-text-fill-color: initial; /* Reset in caso di override */
}
.hero-header.home-mode p { color: rgba(255,255,255,0.9); font-size: 1.1rem; }

/* Stats Hero */
.hero-stats {
    display: flex; justify-content: center; gap: 40px; margin-top: 30px;
}
.stat-item { text-align: center; }
.stat-value { display: block; font-weight: 800; font-size: 1.2rem; color: white; }
.stat-label { font-size: 0.8rem; opacity: 0.8; text-transform: uppercase; color: white; }

/* Grid Cards */
.grid-capitoli { display: grid; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); gap: 25px; }

.card-capitolo {
    background: var(--bg-card);
    border-radius: 6px; /* Ridotto per sembrare più formale e "libresco" */
    padding: 25px;
    border: 1px solid var(--border-color);
    box-shadow: 0 4px 15px rgba(0,0,0,0.03);
    display: flex; flex-direction: column; justify-content: space-between; height: 100%;
    color: var(--text-main);
    transition: all 0.3s cubic-bezier(.25,.8,.25,1);
}
.card-capitolo:hover {
    transform: translateY(-8px); border-color: var(--accent-color);
    box-shadow: 0 14px 28px rgba(0,0,0,0.1), 0 10px 10px rgba(0,0,0,0.05);
}
.card-actions { display: flex; gap: 10px; margin-top: 20px; }

.btn-card {
    flex: 1; padding: 10px; border-radius: 4px;
    text-align: center; text-decoration: none;
    font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 6px;
}
.btn-read { background-color: var(--btn-read-bg); color: var(--btn-read-text); }
.btn-download { background-color: var(--btn-down-bg); color: var(--btn-down-text); }

/* Big Search Bar Home */
.big-search-container { max-width: 600px; margin: 30px auto; position: relative; }
.big-search-container input {
    width: 100%; padding: 15px 20px 15px 50px; font-size: 1.1rem;
    border: 2px solid var(--border-color);
    border-radius: 50px; outline: none;
    background-color: var(--bg-card);
    color: var(--text-main);
}
.big-search-container input:focus {
    border-color: var(--accent-color);
    box-shadow: 0 4px 20px rgba(181, 141, 61, 0.2); /* Ombra oro antico */
}
.search-icon {
    position: absolute; left: 20px; top: 50%;
    transform: translateY(-50%); color: var(--text-muted); font-size: 1.3rem;
}

/* =========================================
   10. MOBILE RESPONSIVE
   ========================================= */
@media (max-width: 768px) {
    body { flex-direction: column; height: auto; overflow: auto; }
    .sidebar {
        width: 100%; height: auto; padding: 15px;
        border-right: none; border-bottom: 1px solid var(--border-color);
        position: relative; min-width: 0;
    }
    .toggle-btn { display: none; }
    .main-content { overflow: visible; height: auto; }
    .content-wrapper { padding: 20px; }
    .hero-header h1 { font-size: 2.5rem; }
    .hero-stats { gap: 20px; }
}

/* =========================================
   11. MATHJAX FIXES
   ========================================= */
mjx-container { overflow: visible !important; }
mjx-container[display="true"] { overflow-x: auto !important; overflow-y: hidden !important; max-width: 100%; }

/* =========================================
   FIX HOME PAGE E DARK MODE
   ========================================= */
body.home-body {
    display: block !important;
    overflow-y: auto !important;
    height: auto !important;
}
body.home-body .home-wrapper {
    margin: 0 auto;
    max-width: 100%;
    width: 100%;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
body.home-body .site-footer {
    width: 100%; margin-top: auto;
}

/* Gestione margini interni per non far toccare i bordi allo schermo */
.hero-header.home-mode,
.grid-capitoli,
.section-title {
    max-width: 1600px;
    margin-left: auto;
    margin-right: auto;
    width: 95%;
}

/* Adattamento automatico della griglia */
.grid-capitoli {
    grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
    gap: 30px;
    margin-bottom: 60px;
}

/* BOTTONE DARK MODE (STILE VETRO) */
.dark-mode-toggle {
    position: absolute;
    top: 30px;
    right: 30px;
    z-index: 1000;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.3);
    color: white;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    font-size: 1.4rem;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.dark-mode-toggle:hover {
    background: rgba(255, 255, 255, 0.3);
    transform: scale(1.1) rotate(15deg);
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.2);
}

.dark-mode-toggle:active{
    transform: scale(0.95);
}

/* =========================================
   12. TITOLI DELLE SEZIONI (HOME)
   ========================================= */
.section-title {
    margin-top: 20px;
    margin-bottom: 30px;
}

.section-title h2 {
    font-size: 2.2rem;
    font-weight: 800;
    color: var(--text-title);
    margin-bottom: 8px;
    letter-spacing: -0.5px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.section-line {
    height: 4px;
    width: 60px;
    background-color: var(--accent-color);
    border-radius: 4px;
}

/* =========================================
   13. BARRA DI PROGRESSO LETTURA
   ========================================= */
.reading-progress {
    position: fixed;
    top: 0;
    left: 0;
    width: 0%;
    height: 4px;
    background-color: var(--accent-color);
    z-index: 9999; /* La tiene sopra a qualsiasi altra cosa */
    box-shadow: 0 1px 8px rgba(181, 141, 61, 0.5); /* Leggero bagliore dorato */
    transition: width 0.1s ease-out; /* Rende il riempimento fluido ma scattante */
}