body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background-color: #f0f2f5;
    color: #333;
    margin: 0;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

h1 {
    color: #007bff;
    margin-bottom: 20px; /* Adjusted margin */
    font-size: 2.5em;
    text-align: center;
}

/* New: Legend Styling */
.periodic-table-legend {
    width: 100%;
    max-width: 1300px; /* Match periodic table container max-width */
    margin-bottom: 25px;
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    padding: 15px;
    border-radius: 8px;
    display: flex;
    flex-wrap: wrap; /* Allow items to wrap to the next line */
    align-items: center;
    gap: 15px; /* Space between legend items */
}

.periodic-table-legend h2 {
    color: #007bff;
    margin: 0 10px 0 0;
    font-size: 1.2em;
    white-space: nowrap; /* Prevent wrapping for "Legend:" */
}

.legend-item {
    display: flex;
    align-items: center;
    font-size: 0.9em;
    color: #555;
    white-space: nowrap;
}

.legend-color-box {
    width: 20px;
    height: 20px;
    border: 1px solid #aaa;
    margin-right: 8px;
    border-radius: 3px;
    flex-shrink: 0; /* Prevent box from shrinking */
}


.periodic-table-container {
    display: flex; /* Use flexbox to arrange grid and info panel side-by-side */
    gap: 30px;
    max-width: 1400px; /* Limit overall width */
    width: 100%;
}

.periodic-table-grid {
    display: grid;
    grid-template-columns: repeat(18, 1fr);
    grid-template-rows: repeat(9, auto);
    gap: 3px;
    flex-grow: 1;
}

.element-box {
    background-color: #fff;
    border: 1px solid #ccc;
    padding: 5px;
    text-align: center;
    font-size: 0.7em;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    align-items: center;
    cursor: pointer;
    transition: all 0.2s ease-in-out;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05);
    border-radius: 4px;
    height: 70px;
    width: 100%;
    box-sizing: border-box;
}

.element-box:hover {
    transform: translateY(-3px);
    box-shadow: 0 4px 8px rgba(0,0,0,0.15);
    border-color: #007bff;
}

.element-box.active {
    transform: scale(1.05);
    box-shadow: 0 6px 12px rgba(0,0,0,0.2);
    border-color: #0056b3;
    z-index: 10;
}

.atomic-number {
    font-size: 0.8em;
    align-self: flex-start;
    margin-bottom: 2px;
    color: #666;
}

.symbol {
    font-size: 1.8em;
    font-weight: bold;
    line-height: 1;
    color: #0056b3;
}

.element-name {
    font-size: 0.7em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    width: 100%;
    padding-top: 2px;
    color: #444;
}

/* Specific styling for groups using background colors */
.alkali-metal { background-color: #ffb3b3; } /* Light Red */
.alkaline-earth-metal { background-color: #ffdb99; } /* Light Orange */
.transition-metal { background-color: #c2e0c2; } /* Light Green */
.post-transition-metal { background-color: #a0d9b4; } /* Medium Green */
.metalloid { background-color: #aed9e0; } /* Light Cyan */
.nonmetal { background-color: #f7d794; } /* Pale Yellow */
.halogen { background-color: #b3cde0; } /* Light Blue */
.noble-gas { background-color: #d9b3e0; } /* Light Purple */
.lanthanide { background-color: #ff99cc; } /* Pink */
.actinide { background-color: #cc99ff; } /* Lavender */
.unknown { background-color: #e0e0e0; } /* Grey */

/* Positioning for Lanthanides and Actinides (f-block) */
.placeholder {
    grid-column: span 1;
    visibility: hidden;
}


.element-info-panel {
    background-color: #fff;
    border: 1px solid #ccc;
    box-shadow: 0 2px 5px rgba(0,0,0,0.1);
    padding: 20px;
    width: 300px;
    flex-shrink: 0;
    border-radius: 8px;
    box-sizing: border-box;
}

.element-info-panel h2 {
    color: #007bff;
    margin-top: 0;
    border-bottom: 2px solid #eee;
    padding-bottom: 10px;
}

#element-details-content p {
    margin: 8px 0;
    line-height: 1.5;
}

#element-details-content strong {
    color: #555;
}

#element-details-content .detail-value {
    color: #0056b3;
    font-weight: bold;
}

/* Responsive adjustments */
@media (max-width: 1024px) {
    .periodic-table-grid {
        gap: 2px;
    }
    .element-box {
        height: 60px;
        font-size: 0.65em;
    }
    .symbol {
        font-size: 1.5em;
    }
    .periodic-table-legend {
        padding: 10px;
        gap: 10px;
    }
    .periodic-table-legend h2 {
        font-size: 1.1em;
        margin-right: 5px;
    }
    .legend-color-box {
        width: 18px;
        height: 18px;
    }
    .legend-item {
        font-size: 0.8em;
    }
}

@media (max-width: 768px) {
    body {
        padding: 10px;
    }
    h1 {
        font-size: 2em;
        margin-bottom: 20px;
    }
    .periodic-table-container {
        flex-direction: column;
        gap: 20px;
    }
    .periodic-table-grid {
        grid-template-columns: repeat(auto-fit, minmax(40px, 1fr));
        gap: 1px;
    }
    .element-box {
        height: 50px;
        font-size: 0.6em;
    }
    .symbol {
        font-size: 1.2em;
    }
    .element-info-panel {
        width: 100%;
        max-width: none;
    }
    .periodic-table-legend {
        flex-direction: column; /* Stack legend title and items vertically */
        align-items: flex-start;
        padding: 15px;
        gap: 8px;
    }
    .periodic-table-legend h2 {
        margin: 0;
        margin-bottom: 5px; /* Add some space below the title */
    }
    .legend-item {
        width: 100%; /* Make items take full width to stack */
    }
}

@media (max-width: 480px) {
    h1 {
        font-size: 1.5em;
    }
    .element-box {
        height: 40px;
        font-size: 0.5em;
        padding: 3px;
    }
    .symbol {
        font-size: 1em;
    }
    .atomic-number {
        font-size: 0.7em;
    }
    .element-name {
        font-size: 0.6em;
    }
    .legend-color-box {
        width: 16px;
        height: 16px;
        margin-right: 5px;
    }
    .legend-item {
        font-size: 0.75em;
    }
}