:root {
    --one: #29c;
    --two: #2c2;
    --three: #c44;
    --four: #22c;
    --five: #800;
    --six: #080;
    --seven: #82c;
    --eight: #222;
    --mine-color: #f44;
    --win-color: #da3;
    --fail-color: color-mix(in srgb, var(--bg-color), var(--mine-color) 25%);
    --success-color: color-mix(in srgb, var(--bg-color), var(--win-color) 15%);
}

.dark {
    --one: #5cf;
    --two: #5f5;
    --three: #f77;
    --four: #55f;
    --five: #a22;
    --six: #2a2;
    --seven: #a5f;
    --eight: #eee;
}

.fail {
    --bg-color: var(--fail-color);
}

.success {
    --mine-color: var(--win-color);
    --bg-color: var(--success-color);
}

.main-container {
    max-width: 1400px;
    display: flex;
    position: relative;
    flex-direction: column;
    width: 100%;
    height: 100%;
    align-items: center;
    gap: 0;
}

#mine-count {
    display: flex;
    background-color: var(--text-color);
    color: var(--bg-color);
    width: 5em;
    justify-content: center;
    border-radius: var(--border-radius) var(--border-radius) 0 0;
    cursor: pointer;
    /* make sure it is always touching field */
    margin-bottom: -20px;
    padding-bottom: 20px;
}

#mode-toggle {
    position: absolute;
    /* the size of the container padding */
    right: 2rem;
    top: 1rem;
    appearance: none;
    width: 3em;
    height: 3em;
    background-color: var(--bg-color);
    border-radius: 1337px;
    cursor: pointer;
}

#mode-toggle:checked {
    background-color: var(--text-color);
}

#mode-toggle::after {
    content: "bomb";
    font-family: var(--font-icons);
    font-variation-settings:
        'FILL' 1,
        'wght' 400,
        'GRAD' 0,
        'opsz' 24;
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100%;
    font-size: 2em;
}

#mode-toggle:checked::after {
    content: "flag";
    color: var(--bg-color);
}

.hint {
    position: absolute;
    top: 50%;
    color: var(--bg-color);
    font-size: 2em;
    user-select: none;
    animation: breathe 5s infinite ease-in-out;
    pointer-events: none;
}

#field {
    display: flex;
    flex-direction: column;
    width: 100%;
    user-select: none;
    z-index: 0;
    border-radius: var(--border-radius);
    overflow: hidden;
}

#field.xyzzy:has(.tile.mine:hover)::after {
    position: fixed;
    top: 0;
    left: 0;
    width: 2px;
    height: 2px;
    content: "";
    background-color: var(--text-color);
}

.shaking {
    animation: shake 0.3s infinite linear;
}

.tile-row {
    display: flex;
    flex-direction: row;
}

.tile {
    --border-radius: 25%;
    position: relative;
    aspect-ratio: 1;
    background-color: var(--text-color);
    transition: background-color 0.2s, border-radius 0.2s;

    /* fix for subpixel gaps */
    flex: 0 0 calc(100% / var(--field-width) + 1px);
    margin: -0.5px;

    border-top-left-radius: var(--border-top-left);
    border-top-right-radius: var(--border-top-right);
    border-bottom-left-radius: var(--border-bottom-left);
    border-bottom-right-radius: var(--border-bottom-right);
}

.tile::before,
.tile.flipped.mine::before {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: -1;
    content: "";
    background-color: var(--bg-color);
    transition: background-color 0.2s;
}

/* the number or symbol that appears on a tile */
.tile::after {
    position: absolute;
    pointer-events: none;
    display: flex;
    justify-content: center;
    align-items: center;
    font-variation-settings:
        'FILL' 1,
        'wght' 400,
        'GRAD' 0,
        'opsz' 24;
    height: 100%;
    width: 100%;
    content: " ";
    z-index: 5;
    font-size: clamp(0.5rem, 2.75vw, 2.75rem);
}

.tile.flipped {
    background-color: var(--bg-color);
}

/* the corner roundness of a flipped tile */
.tile.flipped::before {
    background-color: var(--text-color);
}

/* treat flipped mines as if they were unflipped */
.tile.flipped.mine {
    background-color: var(--text-color);
}

/* cheat mode */
.odin .tile.mine:not(.flagged):not(.flipped)::after,
.horus .tile.mine:hover:not(.flagged):not(.flipped)::after {
    content: "bomb";
    font-family: var(--font-icons);
    color: var(--mine-color);
    animation: grow-and-shake 0.5s ease-in-out;
}

/* the actual mine icon */
.tile.flipped.mine::after {
    content: "bomb";
    font-family: var(--font-icons);
    animation: grow-and-shake 0.5s ease-in-out;
    z-index: 1;
    background-color: var(--mine-color);
    border-radius: var(--border-radius);
}

/* the actual flag icon */
.tile.flagged::after {
    content: "flag";
    color: var(--mine-color);
    font-family: var(--font-icons);
    animation: stamp 0.1s linear;
}

/* wiggle flags on win */
.success .tile.flagged::after {
    animation: grow-and-shake 0.5s ease-in-out;
}

/* simple selector to find all erroneous flags */
.odin .tile.flagged:not(.mine)::after,
.horus .tile.flagged:not(.mine):hover::after,
.fail .tile.flagged:not(.mine)::after {
    color: #999;
}

/* mine count styles */
.tile.flipped.mine-count-1::after {
    content: "1";
    color: var(--one);
}

.tile.flipped.mine-count-2::after {
    content: "2";
    color: var(--two);
}

.tile.flipped.mine-count-3::after {
    content: "3";
    color: var(--three);
}

.tile.flipped.mine-count-4::after {
    content: "4";
    color: var(--four);
}

.tile.flipped.mine-count-5::after {
    content: "5";
    color: var(--five);
}

.tile.flipped.mine-count-6::after {
    content: "6";
    color: var(--six);
}

.tile.flipped.mine-count-7::after {
    content: "7";
    color: var(--seven);
}

.tile.flipped.mine-count-8::after {
    content: "8";
    color: var(--eight);
}

/* turn the text grey on win or lose */
.success .tile.flipped::after,
.fail .tile.flipped::after {
    color: var(--text-color);
}

#message-container {
    position: absolute;
    bottom: 0.4em;
    width: 100%;
    display: flex;
    justify-content: center;
    flex-direction: column-reverse;
    align-items: center;
}

.message {
    font-size: 2em;
    text-align: center;
    user-select: none;
    background-color: var(--fg-color);
    padding: 0.5em 1em;
    border-radius: var(--border-radius);
    transition:
        opacity 0.4s,
        max-height 0.4s,
        margin 0.4s,
        padding 0.4s;
    opacity: 1;
    margin: 0.2em;
    max-height: 300px;
    overflow: hidden;
}

.message.hide {
    opacity: 0;
    max-height: 0;
    margin: 0;
    padding: 0;
}

#timer {
    position: absolute;
    font-family: "Fira Code", monospace;
    left: 30px;
    top: 20px;
    font-size: 1.5em;
    color: var(--text-color);
    background-color: var(--bg-color);
    padding: 0.2em;
    border-radius: var(--border-radius);
}

@keyframes grow-and-shake {
    0% {
        transform: scale(1);
    }

    25% {
        transform: scale(1.5) rotate(15deg);
    }

    50% {
        transform: scale(1) rotate(-15deg);
    }

    75% {
        transform: rotate(15deg);
    }

    100% {
        transform: rotate(0);
    }
}

@keyframes breathe {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }

    100% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes stamp {
    0% {
        transform: scale(2);
    }

    100% {
        transform: scale(1);
    }
}

@keyframes shake {
    0% {
        transform: translate(0px, 0px);
    }

    12% {
        transform: translate(-7px, -6px);
    }

    24% {
        transform: translate(9px, 5px);
    }

    36% {
        transform: translate(-10px, 8px);
    }

    48% {
        transform: translate(7px, -10px);
    }

    60% {
        transform: translate(-6px, 10px);
    }

    72% {
        transform: translate(10px, -4px);
    }

    84% {
        transform: translate(-9px, 8px);
    }

    100% {
        transform: translate(0px, 0px);
    }
}

@media (min-width: 701px) {

    /* border radius for rounding */
    .tile.round-top-left {
        --border-top-left: var(--border-radius);
    }

    .tile.round-top-right {
        --border-top-right: var(--border-radius);
    }

    .tile.round-bottom-left {
        --border-bottom-left: var(--border-radius);
    }

    .tile.round-bottom-right {
        --border-bottom-right: var(--border-radius);
    }
}

@media (max-width: 700px) {

    /* fake a vertical display by reorienting the field */
    #field {
        flex-direction: row-reverse;
    }

    .tile-row {
        flex-direction: column;
        width: 100%;
    }

    .tile {
        --border-top-left: 0;
        --border-top-right: 0;
        --border-bottom-left: 0;
        --border-bottom-right: 0;
    }

    .tile.round-top-left {
        --border-top-right: var(--border-radius);
    }

    .tile.round-bottom-left {
        --border-top-left: var(--border-radius);
    }

    .tile.round-top-right {
        --border-bottom-right: var(--border-radius);
    }

    .tile.round-bottom-right {
        --border-bottom-left: var(--border-radius);
    }

    .tile::after {
        font-size: clamp(0.5rem, 2.5vh, 2rem);
    }

    .main-container {
        padding-bottom: 6em;
    }

    #mode-toggle {
        top: auto;
        bottom: 1em;
        width: 5em;
        height: 5em;
        right: 50%;
        transform: translateX(50%);
    }

    #mode-toggle::after {
        font-size: 3em;
    }

    #timer {
        position: fixed;
        left: 50%;
        top: auto;
        bottom: 1em;
        transform: translateX(-50%);
        background-color: var(--fg-color);
    }
}