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

:root {
    --nonmetal: #4fc3f7;
    --noble-gas: #ff7043;
    --alkali-metal: #ef5350;
    --alkaline-earth-metal: #ffa726;
    --transition-metal: #ab47bc;
    --post-transition-metal: #78909c;
    --metalloid: #ffee58;
    --metal: #66bb6a;
    --halogen: #26a69a;
    --lanthanide: #ec407a;
    --actinide: #7e57c2;

    --cell-size: 62px;
    --cell-gap: 4px;
}

body {
    font-family: Arial, Helvetica, sans-serif;
    background-color: #f7f7f7;
    margin: 0;
    padding: clamp(12px, 3vw, 24px);
}

/* Faint drifting color blobs behind the content. Fixed + negative z-index so
   they sit under everything and never intercept clicks or scroll with the page. */
.bg-decor {
    position: fixed;
    inset: 0;
    overflow: hidden;
    z-index: -1;
    pointer-events: none;
}

.bg-decor .dot {
    position: absolute;
    border-radius: 50%;
    filter: blur(1px);
    animation: drift linear infinite;
}

@keyframes drift {
    0%   { transform: translate(0, 0); }
    25%  { transform: translate(18px, -26px); }
    50%  { transform: translate(-12px, -46px); }
    75%  { transform: translate(-26px, -16px); }
    100% { transform: translate(0, 0); }
}

.bg-decor .dot:nth-child(1) { width: 100px; height: 100px; left: 6%;  top: 12%; animation-duration: 28s; background: radial-gradient(circle, rgba(79, 195, 247, 0.30), rgba(79, 195, 247, 0) 70%); }
.bg-decor .dot:nth-child(2) { width: 55px;  height: 55px;  left: 24%; top: 70%; animation-duration: 20s; animation-delay: -4s; background: radial-gradient(circle, rgba(236, 64, 122, 0.28), rgba(236, 64, 122, 0) 70%); }
.bg-decor .dot:nth-child(3) { width: 140px; height: 140px; left: 46%; top: 6%;  animation-duration: 34s; animation-delay: -10s; background: radial-gradient(circle, rgba(171, 71, 188, 0.26), rgba(171, 71, 188, 0) 70%); }
.bg-decor .dot:nth-child(4) { width: 75px;  height: 75px;  left: 66%; top: 58%; animation-duration: 23s; animation-delay: -7s; background: radial-gradient(circle, rgba(102, 187, 106, 0.28), rgba(102, 187, 106, 0) 70%); }
.bg-decor .dot:nth-child(5) { width: 45px;  height: 45px;  left: 84%; top: 20%; animation-duration: 18s; animation-delay: -12s; background: radial-gradient(circle, rgba(79, 195, 247, 0.29), rgba(79, 195, 247, 0) 70%); }
.bg-decor .dot:nth-child(6) { width: 120px; height: 120px; left: 90%; top: 78%; animation-duration: 30s; animation-delay: -3s;  background: radial-gradient(circle, rgba(255, 167, 38, 0.27), rgba(255, 167, 38, 0) 70%); }
.bg-decor .dot:nth-child(7) { width: 65px;  height: 65px;  left: 4%;  top: 84%; animation-duration: 25s; animation-delay: -15s; background: radial-gradient(circle, rgba(38, 166, 154, 0.28), rgba(38, 166, 154, 0) 70%); }
.bg-decor .dot:nth-child(8) { width: 38px;  height: 38px;  left: 56%; top: 90%; animation-duration: 17s; animation-delay: -6s;  background: radial-gradient(circle, rgba(171, 71, 188, 0.28), rgba(171, 71, 188, 0) 70%); }

@media (prefers-reduced-motion: reduce) {
    .bg-decor .dot {
        animation: none;
    }
}

h1 {
    text-align: center;
    font-size: clamp(20px, 4.5vw, 32px);
    margin: 0 0 8px;
}

/* Lets the grid scroll horizontally on narrow viewports instead of
   overflowing the page or getting squished unreadably. */
.table-scroll {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 8px;
}

.table-container {
    display: grid;
    grid-template-columns: repeat(18, var(--cell-size));
    grid-auto-rows: var(--cell-size);
    gap: var(--cell-gap);
    justify-content: center;
    margin: 20px auto 0;
    width: max-content;
    min-width: 100%;
}

.element {
    border: 1px solid rgba(0, 0, 0, 0.15);
    border-radius: 5px;
    background-color: #cfd8dc;
    text-align: center;
    padding: 2px;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.element:hover,
.element:focus-visible {
    transform: scale(1.15);
    box-shadow: 0 8px 15px rgba(0, 0, 0, 0.25);
    z-index: 5;
    outline: none;
}

.element .symbol {
    font-size: clamp(13px, 1.6vw, 20px);
    font-weight: bold;
    line-height: 1.1;
}

.element .number {
    font-size: clamp(9px, 1vw, 13px);
    color: rgba(0, 0, 0, 0.6);
    line-height: 1.1;
}

.element .name {
    font-size: clamp(7px, 0.85vw, 11px);
    color: rgba(0, 0, 0, 0.55);
    line-height: 1.1;
    white-space: nowrap;
}

/* Category colors, shared by the grid cells and the legend chips
   via the same data-category attribute. */
[data-category="Nonmetal"] { background-color: var(--nonmetal); }
[data-category="Noble Gas"] { background-color: var(--noble-gas); }
[data-category="Alkali Metal"] { background-color: var(--alkali-metal); }
[data-category="Alkaline Earth Metal"] { background-color: var(--alkaline-earth-metal); }
[data-category="Transition Metal"] { background-color: var(--transition-metal); }
[data-category="Post-transition Metal"] { background-color: var(--post-transition-metal); }
[data-category="Metalloid"] { background-color: var(--metalloid); }
[data-category="Metal"] { background-color: var(--metal); }
[data-category="Halogen"] { background-color: var(--halogen); }
[data-category="Lanthanide"] { background-color: var(--lanthanide); }
[data-category="Actinide"] { background-color: var(--actinide); }

.legend {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
    max-width: 1100px;
    margin: 18px auto 0;
    padding: 0 8px;
}

.legend-chip {
    border: none;
    border-radius: 20px;
    padding: 6px 14px;
    font-size: clamp(11px, 1.4vw, 14px);
    font-weight: 600;
    color: #1a1a1a;
    cursor: pointer;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}

.legend-chip:hover,
.legend-chip:focus-visible {
    outline: 2px solid #333;
    outline-offset: 1px;
}

#overlay {
    position: fixed;
    inset: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: none;
    z-index: 999;
}

#modal,
#modal2 {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: white;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
    padding: 20px;
    display: none;
    border-radius: 8px;
    z-index: 1000;
    max-height: 90vh;
    overflow-y: auto;
}

#modal { width: min(550px, 92vw); }
#modal2 { width: min(340px, 90vw); }

#modal h2,
#modal2 h2 {
    margin: 0 0 10px;
    padding-right: 60px;
}

#modal p,
#modal2 p {
    margin: 5px 0;
}

.viewer-box {
    width: 100%;
    max-width: min(460px, 55vh);
    aspect-ratio: 1 / 1;
    margin: 0 auto;
}

.viewer-box model-viewer {
    width: 100%;
    height: 100%;
}

#close-modal,
#close-modal2 {
    background-color: #f44336;
    color: white;
    padding: 6px 12px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    float: right;
}

#close-modal:hover,
#close-modal2:hover {
    background-color: #d32f2f;
}

.highlight-noble-gas {
    outline: 4px solid #2e7d32;
    outline-offset: -2px;
    z-index: 6;
}

/* Tablet: shrink cells, drop the element name to keep symbols legible. */
@media (max-width: 900px) {
    :root {
        --cell-size: 46px;
        --cell-gap: 3px;
    }
    .element .name {
        display: none;
    }
}

/* Phone: shrink further, drop the atomic number too. */
@media (max-width: 520px) {
    :root {
        --cell-size: 34px;
        --cell-gap: 2px;
    }
    .element .number {
        display: none;
    }
}
