:root {
    --bg-color: #f4f7f6;
    --text-color: #333;

    /* Colori categorie migliorati per bambini */
    --alkali-metal: #ff6b6b;
    --alkaline-earth-metal: #fca311;
    --transition-metal: #ffe66d;
    --post-transition-metal: #a8dadc;
    --metalloid: #4ecdc4;
    --non-metal: #95d5b2;
    --halogen: #457b9d;
    --noble-gas: #e0aaff;
    --lanthanide: #fbc4ab;
    --actinide: #f08080;
    --unknown: #e9ecef;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Nunito', sans-serif;
    background-color: var(--bg-color);
    background-image: url('svg/bg-whatsapp.svg');
    background-repeat: repeat;
    background-size: 900px;
    background-attachment: fixed;
    color: var(--text-color);
    padding: 20px 0;
    margin: 0;
}

header {
    text-align: center;
    margin-bottom: 30px;
    padding: 0 20px;
}

main {
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 0 20px;
    overflow-x: auto;
    /* Permette scroll su schermi piccoli */
}

h1 {
    font-size: 3rem;
    color: #1a535c;
    margin-bottom: 10px;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

header p {
    font-size: 1.2rem;
    color: #555;
    margin-bottom: 15px;
}

.legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    max-width: 900px;
    margin: 0 auto;
}

.legend-item {
    padding: 5px 12px;
    border-radius: 20px;
    font-size: 0.9rem;
    font-weight: 700;
    box-shadow: 1px 1px 3px rgba(0, 0, 0, 0.1);
}

.periodic-table {
    display: grid;
    grid-template-columns: repeat(18, 1fr);
    grid-template-rows: repeat(10, 1fr);
    gap: 8px;
    /* Rimuovendo width 100% e usando grid e min-content forziamo il container ad abbracciare gli elementi */
    width: max-content;
    margin: 0 auto;
    padding-bottom: 40px;
}

.element {
    position: relative;
    border-radius: 8px;
    padding: 8px 4px;
    cursor: pointer;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275), box-shadow 0.2s;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    /* Forziamo la grandezza quadratica rigida per mantenere la simmetria */
    width: 65px;
    height: 65px;
    border: 2px solid transparent;
}

.element:hover {
    transform: scale(1.15);
    box-shadow: 0 8px 15px rgba(0, 0, 0, 0.2);
    z-index: 10;
    border-color: rgba(255, 255, 255, 0.7);
}

.el-number {
    font-size: 0.75rem;
    font-weight: 700;
    align-self: flex-start;
    padding-left: 2px;
}

.el-symbol {
    font-size: 1.5rem;
    font-weight: 900;
    margin: 0;
    margin-top: -6px;
    /* Spinto in alto per fare spazio al nome scritto in basso */
}

.el-name {
    position: absolute;
    bottom: 3px;
    font-size: 0.55rem;
    font-weight: 600;
    width: 90%;
    text-align: center;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: rgba(0, 0, 0, 0.7);
}

/* Modal Styles */
.modal {
    display: none;
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: white;
    border-radius: 20px;
    width: 90%;
    max-width: 450px;
    overflow: hidden;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    animation: zoomIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    position: relative;
}

@keyframes zoomIn {
    from {
        transform: scale(0.5);
        opacity: 0;
    }

    to {
        transform: scale(1);
        opacity: 1;
    }
}

.close-btn {
    position: absolute;
    top: 15px;
    right: 20px;
    color: white;
    font-size: 30px;
    font-weight: bold;
    cursor: pointer;
    text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.3);
    z-index: 10;
    /* Assicura che sia sempre sopra all'header testuale */
    padding: 10px;
    /* Aumenta l'area cliccabile */
    line-height: 1;
}

.close-btn:hover {
    color: #f1f1f1;
}

.modal-header {
    padding: 30px 20px;
    text-align: center;
    color: white;
}

.modal-number {
    font-size: 1.5rem;
    font-weight: bold;
    opacity: 0.8;
}

.modal-symbol {
    font-size: 5rem;
    font-weight: 900;
    line-height: 1;
    margin: 10px 0;
}

.modal-name {
    font-size: 2rem;
    font-weight: 700;
}

.modal-body {
    padding: 30px 20px;
    text-align: center;
}

.modal-icon {
    font-size: 5rem;
    margin-bottom: 20px;
    animation: bounce 2s infinite;
}

@keyframes bounce {

    0%,
    20%,
    50%,
    80%,
    100% {
        transform: translateY(0);
    }

    40% {
        transform: translateY(-15px);
    }

    60% {
        transform: translateY(-7px);
    }
}

.modal-item-title {
    color: #555;
    font-size: 1.2rem;
    margin-bottom: 10px;
}

.modal-item {
    font-size: 1.4rem;
    font-weight: 700;
    color: #333;
}

/* Category Colors */
.alkali-metal {
    background-color: var(--alkali-metal);
}

.alkaline-earth-metal {
    background-color: var(--alkaline-earth-metal);
}

.transition-metal {
    background-color: var(--transition-metal);
}

.post-transition-metal {
    background-color: var(--post-transition-metal);
}

.metalloid {
    background-color: var(--metalloid);
}

.non-metal {
    background-color: var(--non-metal);
}

.halogen {
    background-color: var(--halogen);
}

.noble-gas {
    background-color: var(--noble-gas);
}

.lanthanide {
    background-color: var(--lanthanide);
}

.actinide {
    background-color: var(--actinide);
}

.unknown {
    background-color: var(--unknown);
}

/* --- LAB MODE STYLES --- */
.controls {
    margin-bottom: 20px;
}

.lab-btn {
    background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
    color: white;
    border: none;
    padding: 12px 24px;
    font-size: 1.2rem;
    font-weight: 900;
    border-radius: 30px;
    cursor: pointer;
    box-shadow: 0 4px 10px rgba(37, 117, 252, 0.3);
    transition: transform 0.2s, box-shadow 0.2s;
    font-family: 'Nunito', sans-serif;
}

.lab-btn:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 15px rgba(37, 117, 252, 0.5);
}

.lab-btn.active {
    background: linear-gradient(135deg, #ff416c 0%, #ff4b2b 100%);
    box-shadow: 0 4px 10px rgba(255, 65, 108, 0.4);
}

.element.selected-for-lab {
    border: 3px solid #ff416c;
    box-shadow: 0 0 15px rgba(255, 65, 108, 0.8);
    transform: scale(1.1);
    z-index: 5;
}

.lab-tray {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background-color: white;
    box-shadow: 0 -5px 20px rgba(0, 0, 0, 0.1);
    padding: 15px 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: 50;
    transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    border-top-left-radius: 20px;
    border-top-right-radius: 20px;
}

.lab-tray.hidden {
    transform: translateY(120%);
}

.lab-tray h3 {
    color: #333;
    margin-bottom: 15px;
    font-size: 1.2rem;
}

.lab-tray h3 span {
    font-weight: normal;
    font-size: 1rem;
    color: #666;
}

.tray-slots {
    display: flex;
    gap: 15px;
    min-height: 70px;
    margin-bottom: 15px;
    flex-wrap: wrap;
    justify-content: center;
}

.tray-slot-item {
    width: 60px;
    height: 60px;
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    font-size: 1.5rem;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.2);
    animation: zoomIn 0.3s;
}

.tray-slot-item .slot-number {
    font-size: 0.6rem;
    align-self: flex-start;
    padding-left: 4px;
}

.tray-actions {
    display: flex;
    gap: 10px;
}

.combine-btn,
.clear-btn {
    border: none;
    padding: 10px 20px;
    font-size: 1rem;
    font-weight: bold;
    border-radius: 20px;
    cursor: pointer;
    font-family: 'Nunito', sans-serif;
    transition: transform 0.2s;
}

.combine-btn {
    background-color: #2ecc71;
    color: white;
    box-shadow: 0 4px 6px rgba(46, 204, 113, 0.3);
}

.combine-btn:hover:not(:disabled) {
    transform: scale(1.05);
}

.combine-btn:disabled {
    background-color: #bdc3c7;
    cursor: not-allowed;
    box-shadow: none;
}

.clear-btn {
    background-color: #e74c3c;
    color: white;
    box-shadow: 0 4px 6px rgba(231, 76, 60, 0.3);
}

.clear-btn:hover {
    transform: scale(1.05);
}

.element:active {
    transform: scale(0.95);
}

/* Modalità Lab: transizione fluida e oscuramento elementi disabilitati */
.element {
    transition: transform 0.2s, box-shadow 0.2s, opacity 0.3s, filter 0.3s;
}

.element.disabled-for-lab {
    opacity: 0.3;
    filter: grayscale(100%);
    pointer-events: none;
    cursor: default;
}

/* Compound Modal */
.compound-style {
    background: linear-gradient(135deg, #fff 0%, #f0f8ff 100%);
}

.compound-header {
    background: linear-gradient(135deg, #f6d365 0%, #fda085 100%);
    border-bottom-left-radius: 30px;
    border-bottom-right-radius: 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20px;
}

.compound-title-container {
    flex-grow: 1;
    text-align: center;
}

.nav-compound-btn {
    background: rgba(255, 255, 255, 0.4);
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    font-size: 1.2rem;
    cursor: pointer;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
    transition: transform 0.2s, background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #333;
}

.nav-compound-btn:hover {
    transform: scale(1.1);
    background: rgba(255, 255, 255, 0.7);
}

.nav-compound-btn.hidden {
    visibility: hidden;
}

.modal-indicator {
    text-align: center;
    padding-top: 15px;
    font-size: 1rem;
    color: #666;
    font-weight: bold;
}

.modal-indicator.hidden {
    display: none;
}

#compound-formula {
    font-size: 4rem;
    font-weight: 900;
    margin-bottom: 5px;
}

/* Responsive Adjustments */
@media (max-width: 1300px) {
    main {
        justify-content: flex-start;
        /* Evita il taglio a sinistra su schermi piccoli */
    }
}

@media (max-width: 1000px) {
    .element {
        width: 45px;
        height: 45px;
    }

    .el-symbol {
        font-size: 1.1rem;
    }

    .el-name {
        font-size: 0.55rem;
    }
}