:root {
    --bg: #fff8e7;
    --ink: #1d1b3a;
    --muted: #5b5880;
    --panel: #ffffff;
    --shadow: #1d1b3a;
    --radius: 14px;

    --alkali: #ff6b6b;
    --erdalkali: #ffa94d;
    --transition: #ffe066;
    --post: #a9e34b;
    --metalloid: #38d9a9;
    --nonmetal: #66d9e8;
    --halogen: #74a7ff;
    --noble: #b197fc;
    --lanthanide: #f783ac;
    --actinide: #e599f7;

    --font: ui-rounded, "SF Pro Rounded", "Nunito", "Segoe UI", system-ui, sans-serif;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #14122b;
        --muted: #b9b6e0;
        --panel: #231f47;
        --shadow: #05040f;
    }
}

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

body {
    margin: 0;
    font-family: var(--font);
    color: var(--ink);
    background: var(--bg);
    background-image: radial-gradient(circle at 1px 1px, rgb(127 127 160 / 0.22) 1.5px, transparent 0);
    background-size: 22px 22px;
    line-height: 1.4;
}

@media (prefers-color-scheme: dark) {
    body { color: #f4f2ff; }
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

/* Header */
.hero {
    padding: 1.5rem 1rem 0.5rem;
    text-align: center;
}

.hero h1 {
    margin: 0;
    font-size: clamp(2rem, 9vw, 3.5rem);
    letter-spacing: -0.02em;
}

.hero p {
    margin: 0.4rem auto 0;
    max-width: 36rem;
    color: var(--muted);
}

.flask {
    display: inline-block;
    animation: bubble 3s ease-in-out infinite;
}

@keyframes bubble {
    0%, 100% { transform: rotate(-8deg); }
    50% { transform: rotate(8deg) translateY(-3px); }
}

/* Toolbar: search, random button and legend chips (sticky on phones) */
.toolbar {
    position: sticky;
    top: 0;
    z-index: 5;
    padding: 0.6rem 1rem;
    background: var(--bg);
    border-bottom: 3px solid var(--ink);
}

.tools {
    display: none;
    gap: 0.5rem;
    margin-bottom: 0.5rem;
}

/* Search and random need JavaScript */
.js .tools { display: flex; }

.search { flex: 1; min-width: 0; }

.search input,
.random {
    width: 100%;
    min-height: 44px;
    padding: 0.5rem 0.9rem;
    font: inherit;
    color: inherit;
    background: var(--panel);
    border: 3px solid var(--ink);
    border-radius: 999px;
}

.random {
    width: auto;
    flex: none;
    font-weight: 700;
    color: var(--ink);
    background: var(--transition);
    box-shadow: 3px 3px 0 var(--shadow);
    cursor: pointer;
    transition: transform 0.15s;
}

.random:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--shadow); }

.legend {
    display: flex;
    gap: 0.4rem;
    margin: 0;
    padding: 0 0 0.2rem;
    list-style: none;
    overflow-x: auto;
    scrollbar-width: none;
}

.legend::-webkit-scrollbar { display: none; }

.chip {
    flex: none;
    min-height: 36px;
    padding: 0.3rem 0.8rem;
    font: inherit;
    font-size: 0.85rem;
    font-weight: 700;
    color: var(--ink);
    background: var(--c);
    border: 3px solid var(--ink);
    border-radius: 999px;
    cursor: default;
}

.js .chip { cursor: pointer; }
.chip[aria-pressed="true"] { box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--ink); }

.status {
    min-height: 1.2em;
    margin: 0.4rem 0 0;
    font-size: 0.85rem;
    color: var(--muted);
}

/* Category colours, shared by tiles and chips */
.cat-alkali { --c: var(--alkali); }
.cat-erdalkali { --c: var(--erdalkali); }
.cat-transition { --c: var(--transition); }
.cat-post { --c: var(--post); }
.cat-metalloid { --c: var(--metalloid); }
.cat-nonmetal { --c: var(--nonmetal); }
.cat-halogen { --c: var(--halogen); }
.cat-noble { --c: var(--noble); }
.cat-lanthanide { --c: var(--lanthanide); }
.cat-actinide { --c: var(--actinide); }

/* Phones: three-column card grid in order of atomic number */
main { padding: 1rem; }

.swipe-hint,
.ph { display: none; }

.table {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.6rem;
}

.el {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 92px;
    padding: 0.4rem 0.2rem;
    color: var(--ink);
    text-align: center;
    text-decoration: none;
    background: var(--c);
    border: 3px solid var(--ink);
    border-radius: var(--radius);
    box-shadow: 3px 3px 0 var(--shadow);
    transition: transform 0.18s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.18s, opacity 0.2s, filter 0.2s;
}

.el:hover,
.el:focus-visible {
    z-index: 2;
    transform: translateY(-4px) rotate(-3deg) scale(1.06);
    box-shadow: 6px 7px 0 var(--shadow);
}

.el:active { transform: translate(2px, 2px); box-shadow: 1px 1px 0 var(--shadow); }

.el:focus-visible,
.chip:focus-visible,
.random:focus-visible,
.search input:focus-visible {
    outline: 4px solid #3b82f6;
    outline-offset: 3px;
}

.num {
    position: absolute;
    top: 4px;
    left: 8px;
    font-size: 0.7rem;
    font-weight: 700;
}

.sym {
    font-size: 1.9rem;
    font-weight: 800;
    line-height: 1.1;
}

.name {
    max-width: 100%;
    font-size: 0.72rem;
    font-weight: 600;
    overflow-wrap: anywhere;
}

/* Filter and search results */
.el.dim {
    opacity: 0.18;
    filter: grayscale(0.8);
    box-shadow: none;
}

.el.pop { animation: pop 0.9s ease 2; }

@keyframes pop {
    0%, 100% { transform: scale(1) rotate(0); }
    30% { transform: scale(1.25) rotate(-6deg); z-index: 3; }
    60% { transform: scale(1.15) rotate(5deg); z-index: 3; }
}

footer {
    padding: 1rem 1rem 2rem;
    text-align: center;
    font-size: 0.85rem;
    color: var(--muted);
}

footer p { margin: 0; }

/* Tablets: classic table inside a horizontal scroller */
@media (min-width: 700px) {
    .toolbar { position: static; border-bottom: 0; background: transparent; }
    .legend { flex-wrap: wrap; justify-content: center; overflow: visible; }
    .tools { max-width: 36rem; margin: 0 auto 0.7rem; }
    .status { text-align: center; }

    .swipe-hint {
        display: block;
        margin: 0 0 0.5rem;
        text-align: center;
        font-size: 0.85rem;
        color: var(--muted);
    }

    .table-wrap {
        margin: 0 -1rem;
        padding: 0.5rem 1rem 1rem;
        overflow-x: auto;
    }

    .table {
        grid-template-columns: repeat(18, minmax(0, 1fr));
        grid-template-rows: repeat(7, auto) 1.2rem repeat(2, auto);
        gap: 4px;
        min-width: 860px;
    }

    .el {
        grid-row: var(--row);
        grid-column: var(--col);
        min-height: 0;
        aspect-ratio: 1 / 1.1;
        padding: 0.2rem 0;
        border-width: 2px;
        border-radius: 8px;
        box-shadow: 2px 2px 0 var(--shadow);
    }

    .ph {
        display: flex;
        grid-row: var(--row);
        grid-column: var(--col);
        align-items: center;
        justify-content: center;
        font-size: 0.65rem;
        font-weight: 700;
        color: var(--muted);
        border: 2px dashed var(--muted);
        border-radius: 8px;
    }

    .num { top: 2px; left: 4px; font-size: 0.6rem; }
    .sym { font-size: 1.2rem; }
    .name { font-size: 0.55rem; }
}

/* Desktops: the whole table fits, no scrolling needed */
@media (min-width: 1000px) {
    .swipe-hint { display: none; }
    .table-wrap { overflow: visible; }
    .table { min-width: 0; max-width: 1300px; margin: 0 auto; gap: 6px; }
    .el { border-width: 3px; border-radius: 12px; box-shadow: 3px 3px 0 var(--shadow); }
    .num { top: 3px; left: 6px; font-size: 0.7rem; }
    .sym { font-size: 1.6rem; }
    .name { font-size: 0.7rem; }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation: none !important;
        transition: none !important;
    }
}
