/* ===== Reset & Base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #f7f3ee;
    --card: #ffffff;
    --primary: #5a8fa8;
    --primary-light: #9cc5d8;
    --primary-bg: #eef5f8;
    --secondary: #d4956a;
    --secondary-light: #f0d4bd;
    --text: #2e2e2e;
    --text-mid: #555;
    --text-light: #888;
    --shadow: 0 1px 8px rgba(0,0,0,0.06);
    --shadow-md: 0 2px 16px rgba(0,0,0,0.08);
    --shadow-lg: 0 4px 24px rgba(0,0,0,0.12);
    --radius: 14px;
    --radius-sm: 8px;
}

html {
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: 'Nunito', -apple-system, BlinkMacSystemFont, sans-serif;
    background: var(--bg);
    color: var(--text);
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    -webkit-font-smoothing: antialiased;
    line-height: 1.5;
}

/* ===== Header ===== */
.header {
    background: var(--card);
    box-shadow: var(--shadow-md);
    position: sticky;
    top: 0;
    z-index: 100;
}

.header-inner {
    max-width: 1000px;
    margin: 0 auto;
    padding: 12px 20px;
    display: flex;
    align-items: center;
}

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

.logo-icon {
    font-size: 1.8rem;
    line-height: 1;
}

.logo h1 {
    font-size: 1.35rem;
    font-weight: 800;
    color: var(--primary);
    letter-spacing: -0.3px;
}

.logo-subtitle {
    font-size: 0.8rem;
    color: var(--text-light);
    font-weight: 600;
    padding-left: 10px;
    border-left: 2px solid var(--primary-light);
}

/* ===== Tabs ===== */
/* --- Desktop tabs --- */
.problem-tabs {
    max-width: 1000px;
    margin: 16px auto 0;
    padding: 0 20px;
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.tab {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 11px 14px;
    border: 2px solid transparent;
    border-radius: var(--radius-sm);
    background: var(--card);
    cursor: pointer;
    font-family: inherit;
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--text-mid);
    transition: all 0.2s ease;
    box-shadow: var(--shadow);
    -webkit-tap-highlight-color: transparent;
}

.tab:hover {
    border-color: var(--primary-light);
    color: var(--text);
}

.tab.active {
    border-color: var(--primary);
    color: var(--primary);
    background: var(--primary-bg);
    box-shadow: var(--shadow-md);
}

.tab-icon {
    font-size: 1.15rem;
}

.tab-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* --- Mobile dropdown (nascosto su desktop) --- */
.tab-mobile-select {
    display: none;
}

.tab-dropdown {
    max-width: 1000px;
    margin: 12px auto 0;
    padding: 0 12px;
    display: none;
}

.tab-dropdown select {
    width: 100%;
    padding: 12px 16px;
    padding-right: 40px;
    border: 2px solid var(--primary);
    border-radius: var(--radius-sm);
    background: var(--primary-bg);
    color: var(--primary);
    font-family: inherit;
    font-size: 1rem;
    font-weight: 700;
    cursor: pointer;
    appearance: none;
    -webkit-appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%235a8fa8' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
    box-shadow: var(--shadow-md);
}

/* ===== Main Content ===== */
.main-content {
    max-width: 1000px;
    margin: 16px auto;
    padding: 0 20px;
    flex: 1;
    width: 100%;
}

.content-grid {
    display: grid;
    grid-template-columns: 1.15fr 1fr;
    gap: 16px;
    align-items: start;
}

/* ===== Canvas ===== */
.canvas-container {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}

#animation-canvas {
    width: 100%;
    height: auto;
    border-radius: var(--radius-sm);
    background: #faf8f5;
    display: block;
}

/* Swipe hint on mobile */
.swipe-hint {
    position: absolute;
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 0.7rem;
    color: var(--text-light);
    opacity: 0.6;
    pointer-events: none;
}

/* ===== Explanation ===== */
.explanation-container {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    padding: 22px;
    min-height: 280px;
    display: flex;
    flex-direction: column;
}

.step-badge {
    display: inline-flex;
    align-items: center;
    background: var(--primary);
    color: white;
    font-weight: 700;
    font-size: 0.75rem;
    padding: 3px 12px;
    border-radius: 20px;
    align-self: flex-start;
    margin-bottom: 12px;
    letter-spacing: 0.3px;
}

.step-title {
    font-size: 1.2rem;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 10px;
    line-height: 1.3;
}

.step-text {
    font-size: 0.98rem;
    line-height: 1.75;
    color: var(--text);
    flex: 1;
}

.step-text .highlight {
    background: var(--secondary-light);
    padding: 1px 5px;
    border-radius: 4px;
    font-weight: 700;
}

.step-formula {
    margin-top: 14px;
    padding: 14px 16px;
    background: #f5f0ea;
    border-radius: var(--radius-sm);
    border-left: 3px solid var(--secondary);
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.step-formula:empty {
    display: none;
}

.step-formula .katex {
    font-size: 1.1em;
}

/* ===== Problem Statement ===== */
.problem-statement {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    margin-bottom: 16px;
    overflow: hidden;
    border-left: 3px solid var(--primary);
}

.problem-statement-toggle {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    border: none;
    background: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.88rem;
    font-weight: 700;
    color: var(--primary);
    -webkit-tap-highlight-color: transparent;
}

.toggle-icon {
    font-size: 0.65rem;
    transition: transform 0.3s ease;
    display: inline-block;
}

.toggle-icon.collapsed {
    transform: rotate(-90deg);
}

.problem-statement-body {
    padding: 0 16px 14px;
    font-size: 0.9rem;
    line-height: 1.65;
    color: var(--text);
    max-height: 500px;
    overflow: hidden;
    transition: max-height 0.4s ease, padding 0.4s ease, opacity 0.3s ease;
    opacity: 1;
}

.problem-statement-body.collapsed {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
}

.problem-statement-body .statement-parts {
    list-style: none;
    padding: 0;
    margin-top: 8px;
}

.problem-statement-body .statement-parts li {
    padding: 3px 0 3px 22px;
    position: relative;
}

.problem-statement-body .statement-parts li::before {
    content: attr(data-num);
    position: absolute;
    left: 0;
    font-weight: 700;
    color: var(--primary);
    font-size: 0.85rem;
}

/* ===== Glossario Button in Header ===== */
.header-inner {
    justify-content: space-between;
}

.btn-glossario {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 7px 14px;
    border: 2px solid var(--primary-light);
    border-radius: var(--radius-sm);
    background: white;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--primary);
    transition: all 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.btn-glossario:hover {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.btn-glossario-icon { font-size: 1.1rem; }

/* ===== Clickable Terms ===== */
.term {
    text-decoration: underline;
    text-decoration-style: dotted;
    text-decoration-color: var(--primary-light);
    text-underline-offset: 2px;
    cursor: pointer;
    color: var(--primary);
    font-weight: 600;
    transition: color 0.15s, text-decoration-color 0.15s;
    -webkit-tap-highlight-color: transparent;
}

.term:hover, .term:active {
    color: #4a7d94;
    text-decoration-color: var(--primary);
}

/* ===== Term Callout ===== */
.term-callout {
    background: #eef5f8;
    border: 2px solid var(--primary-light);
    border-radius: var(--radius);
    margin-top: 12px;
    padding: 16px;
    opacity: 0;
    transform: translateY(-8px);
    transition: opacity 0.25s ease, transform 0.25s ease;
    position: relative;
    box-shadow: var(--shadow-md);
}

.term-callout.visible {
    opacity: 1;
    transform: translateY(0);
}

.callout-header {
    display: flex;
    align-items: center;
    gap: 6px;
    margin-bottom: 10px;
    font-size: 1rem;
    color: var(--primary);
}

.callout-icon { font-size: 1.1rem; }

.callout-symbol {
    font-size: 0.85rem;
    color: var(--text-mid);
    font-weight: 400;
}

.callout-close {
    margin-left: auto;
    background: none;
    border: none;
    font-size: 1.4rem;
    color: var(--text-light);
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
    -webkit-tap-highlight-color: transparent;
}

.callout-close:hover { color: var(--text); }

.term-definition {
    font-size: 0.92rem;
    line-height: 1.65;
    color: var(--text);
    margin-bottom: 8px;
}

.term-formula {
    background: #f5f0ea;
    border-radius: 6px;
    padding: 10px 14px;
    margin: 8px 0;
    border-left: 3px solid var(--secondary);
    font-size: 0.9rem;
    overflow-x: auto;
}

.term-formula-desc {
    font-size: 0.82rem;
    color: var(--text-mid);
}

.term-extra {
    font-size: 0.88rem;
    line-height: 1.6;
    color: var(--text-mid);
    margin-top: 8px;
    padding: 10px 12px;
    background: rgba(90,143,168,0.05);
    border-radius: 6px;
}

.term-table {
    width: 100%;
    border-collapse: collapse;
    margin: 6px 0;
    font-size: 0.82rem;
}

.term-table th, .term-table td {
    padding: 4px 8px;
    border-bottom: 1px solid rgba(0,0,0,0.08);
    text-align: left;
}

.term-table th {
    font-weight: 700;
    color: var(--primary);
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.3px;
}

.term-see-also {
    margin-top: 10px;
    font-size: 0.82rem;
    color: var(--text-mid);
}

.term-link {
    color: var(--primary);
    text-decoration: none;
    font-weight: 600;
}

.term-link:hover { text-decoration: underline; }

/* ===== Glossario Modal ===== */
.glossario-modal {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 1000;
    background: rgba(0,0,0,0.4);
    justify-content: center;
    align-items: center;
    padding: 20px;
}

.glossario-modal.open {
    display: flex;
}

.glossario-panel {
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-lg);
    width: 100%;
    max-width: 600px;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.glossario-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 20px;
    border-bottom: 1px solid rgba(0,0,0,0.06);
}

.glossario-header h2 {
    font-size: 1.2rem;
    font-weight: 800;
    color: var(--primary);
}

.glossario-close {
    background: none;
    border: none;
    font-size: 1.6rem;
    color: var(--text-light);
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}

.glossario-close:hover { color: var(--text); }

.glossario-search-box {
    padding: 12px 20px;
    border-bottom: 1px solid rgba(0,0,0,0.06);
}

.glossario-search-box input {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid #e0dcd6;
    border-radius: var(--radius-sm);
    font-family: inherit;
    font-size: 0.92rem;
    outline: none;
    transition: border-color 0.15s;
}

.glossario-search-box input:focus {
    border-color: var(--primary-light);
}

.glossario-body {
    overflow-y: auto;
    flex: 1;
    padding: 8px 0;
    -webkit-overflow-scrolling: touch;
}

/* Accordion items */
.glossario-item {
    border-bottom: 1px solid rgba(0,0,0,0.04);
}

.glossario-item-header {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 20px;
    border: none;
    background: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 700;
    color: var(--text);
    text-align: left;
    -webkit-tap-highlight-color: transparent;
    transition: background 0.15s;
}

.glossario-item-header:hover { background: #f5f2ee; }

.glossario-item-symbol {
    font-size: 0.8rem;
    color: var(--text-mid);
    font-weight: 400;
    font-style: italic;
}

.glossario-chevron {
    margin-left: auto;
    font-size: 0.6rem;
    color: var(--text-light);
    transition: transform 0.2s;
}

.glossario-item.open .glossario-chevron {
    transform: rotate(180deg);
}

.glossario-item-body {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease, padding 0.3s ease;
    padding: 0 20px;
}

.glossario-item.open .glossario-item-body {
    max-height: 800px;
    padding: 0 20px 16px;
}

/* ===== Step Navigation ===== */
.step-navigation {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-top: 16px;
    padding: 12px 16px;
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow-md);
    gap: 10px;
}

.nav-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 10px 20px;
    border: 2px solid var(--primary-light);
    border-radius: var(--radius-sm);
    background: white;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.92rem;
    font-weight: 700;
    color: var(--primary);
    transition: all 0.15s ease;
    -webkit-tap-highlight-color: transparent;
    min-height: 44px;
    white-space: nowrap;
}

.nav-btn:hover:not(:disabled) {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.nav-btn:active:not(:disabled) {
    transform: scale(0.97);
}

.nav-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

.nav-btn-primary {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

.nav-btn-primary:hover:not(:disabled) {
    background: #4a7d94;
    border-color: #4a7d94;
}

.nav-arrow {
    font-size: 1.1rem;
}

/* Step Dots */
.step-dots {
    display: flex;
    gap: 7px;
    align-items: center;
    flex-wrap: wrap;
    justify-content: center;
}

.step-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #ddd;
    cursor: pointer;
    transition: all 0.2s ease;
    border: none;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

.step-dot:hover {
    background: var(--primary-light);
}

.step-dot.active {
    background: var(--primary);
    transform: scale(1.4);
}

.step-dot.completed {
    background: var(--primary-light);
}

/* ===== Home Button ===== */
.btn-home {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    border-radius: var(--radius-sm);
    color: var(--primary);
    font-family: inherit;
    font-size: 0.85rem;
    font-weight: 700;
    text-decoration: none;
    transition: background 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.btn-home:hover { background: var(--primary-bg); }
.btn-home-arrow { font-size: 1.1rem; }

/* ===== Home Page ===== */
.home-main {
    max-width: 900px;
    margin: 0 auto;
    padding: 20px 20px 40px;
}

.home-hero {
    text-align: center;
    padding: 32px 16px 24px;
}
.home-hero-title {
    font-size: 1.8rem;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 8px;
}
.home-hero-sub {
    font-size: 1rem;
    color: var(--text-mid);
    max-width: 500px;
    margin: 0 auto;
    line-height: 1.6;
}

.home-section {
    margin-bottom: 32px;
}
.home-section-title {
    font-size: 1.2rem;
    font-weight: 800;
    color: var(--text);
    margin-bottom: 4px;
}
.home-section-desc {
    font-size: 0.88rem;
    color: var(--text-light);
    margin-bottom: 14px;
}

.home-cards {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
    gap: 12px;
}

.home-card {
    display: flex;
    align-items: flex-start;
    gap: 14px;
    padding: 18px;
    background: var(--card);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    text-decoration: none;
    color: inherit;
    border: 2px solid transparent;
    transition: border-color 0.2s, box-shadow 0.2s, transform 0.15s;
    -webkit-tap-highlight-color: transparent;
}
.home-card:hover {
    border-color: var(--primary-light);
    box-shadow: var(--shadow-lg);
    transform: translateY(-2px);
}
.home-card:active { transform: translateY(0); }

.home-card-icon {
    font-size: 2rem;
    line-height: 1;
    flex-shrink: 0;
    margin-top: 2px;
}

.home-card-body h4 {
    font-size: 1rem;
    font-weight: 800;
    color: var(--primary);
    margin-bottom: 4px;
}
.home-card-body p {
    font-size: 0.85rem;
    color: var(--text-mid);
    line-height: 1.5;
}

.home-card-teoria { border-left: 3px solid var(--primary-light); }
.home-card-esercizio { border-left: 3px solid var(--secondary-light); }

/* ===== Footer ===== */
.footer {
    text-align: center;
    padding: 16px;
    color: var(--text-light);
    font-size: 0.8rem;
}

.footer-github {
    margin-top: 4px;
    font-size: 0.75rem;
}

.footer-github a {
    color: var(--primary);
    text-decoration: none;
    font-weight: 600;
}

.footer-github a:hover {
    text-decoration: underline;
}

/* ===== Animations ===== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to { opacity: 1; transform: translateY(0); }
}

.fade-in {
    animation: fadeIn 0.35s ease forwards;
}

/* ===== Responsive — Tablet (<= 900px) ===== */
@media (max-width: 900px) {
    .content-grid {
        grid-template-columns: 1fr;
    }

    .explanation-container {
        min-height: auto;
    }
}

/* ===== Responsive — Mobile (<= 600px) ===== */
@media (max-width: 600px) {
    /* Header */
    .header-inner {
        padding: 10px 14px;
    }
    .logo-icon { font-size: 1.4rem; }
    .logo h1 { font-size: 1.1rem; }

    /* Home button */
    .btn-home { padding: 4px 8px; font-size: 0.8rem; }
    .btn-home-label { display: none; }

    /* Home page */
    .home-hero { padding: 20px 12px 16px; }
    .home-hero-title { font-size: 1.4rem; }
    .home-hero-sub { font-size: 0.9rem; }
    .home-main { padding: 12px 10px 30px; }
    .home-cards { grid-template-columns: 1fr; gap: 8px; }
    .home-card { padding: 14px; gap: 10px; }
    .home-card-icon { font-size: 1.6rem; }
    .home-card-body h4 { font-size: 0.92rem; }
    .home-card-body p { font-size: 0.82rem; }
    .logo-subtitle { display: none; }

    /* Glossario button compatto su mobile */
    .btn-glossario { padding: 6px 10px; font-size: 0.8rem; }
    .btn-glossario-label { display: none; }
    .btn-glossario-icon { font-size: 1.2rem; }

    /* Modale full screen su mobile */
    .glossario-modal { padding: 0; }
    .glossario-panel { max-width: 100%; max-height: 100vh; max-height: 100dvh; border-radius: 0; }

    /* Callout */
    .term-callout { margin-top: 8px; padding: 12px; }

    /* Mobile: dropdown al posto dei tab */
    .problem-tabs { display: none; }
    .tab-dropdown { display: block; }

    /* Main */
    .main-content {
        margin: 10px auto;
        padding: 0 8px;
        /* Spazio per la nav fissa in basso */
        padding-bottom: 76px;
    }

    /* Enunciato */
    .problem-statement {
        margin-bottom: 10px;
        border-radius: 10px;
    }
    .problem-statement-toggle { padding: 10px 12px; font-size: 0.82rem; }
    .problem-statement-body { padding: 0 12px 10px; font-size: 0.84rem; line-height: 1.6; }

    /* Content grid */
    .content-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    /* Canvas: pieno, bordi minimi */
    .canvas-container {
        padding: 6px;
        border-radius: 10px;
    }

    /* Spiegazione */
    .explanation-container {
        padding: 14px;
        min-height: auto;
        border-radius: 10px;
    }
    .step-badge { font-size: 0.7rem; padding: 2px 9px; margin-bottom: 8px; }
    .step-title { font-size: 1.05rem; margin-bottom: 8px; }
    .step-text { font-size: 0.9rem; line-height: 1.65; }

    .step-formula {
        margin-top: 10px;
        padding: 10px 12px;
        border-radius: 6px;
    }
    .step-formula .katex { font-size: 0.9em; }

    /* Nav fissa in basso */
    .step-navigation {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        margin: 0;
        border-radius: 0;
        padding: 10px 12px;
        box-shadow: 0 -2px 12px rgba(0,0,0,0.1);
        z-index: 90;
        background: var(--card);
    }

    .nav-btn {
        padding: 10px 14px;
        font-size: 0.85rem;
        border-radius: 8px;
    }

    .step-dots { gap: 5px; }
    .step-dot {
        width: 12px;
        height: 12px;
        min-width: 12px;
    }

    .footer { padding: 10px; font-size: 0.75rem; padding-bottom: 80px; }
}

/* ===== Molto piccolo (<= 380px) ===== */
@media (max-width: 380px) {
    .logo h1 { font-size: 0.95rem; }
    .tab-dropdown select { font-size: 0.9rem; padding: 10px 14px; }
    .nav-btn { padding: 8px 10px; font-size: 0.8rem; gap: 4px; }
    .step-title { font-size: 0.95rem; }
    .step-text { font-size: 0.85rem; }
    .explanation-container { padding: 12px; }
}
