/* Variables */
:root {
    --system-font-stack: "Segoe UI", -apple-system, BlinkMacSystemFont, system-ui, Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
    --emphasis-animation: linear(0 0%, 0 1.8%, 0.01 3.6%, 0.03 6.35%, 0.07 9.1%, 0.13 11.4%, 0.19 13.4%, 0.27 15%, 0.34 16.1%, 0.54 18.35%, 0.66 20.6%, 0.72 22.4%, 0.77 24.6%, 0.81 27.3%, 0.85 30.4%, 0.88 35.1%, 0.92 40.6%, 0.94 47.2%, 0.96 55%, 0.98 64%, 0.99 74.4%, 1 86.4%, 1 100%);

    --dark-gray: rgb(40, 40, 40);

    --color: var(--dark-gray);
    --background-color: whitesmoke;
    --contrast-background-color: white;
    --active-color: rgb(42, 100, 227);
    --active-background-color: rgba(0, 0, 0, 0.05);
    --danger-color: rgb(255, 107, 107);
    --border-color: gainsboro;
    --shadow-color: rgba(0, 0, 0, 0.2);

    &.dark {
        --color: whitesmoke;
        --background-color: var(--dark-gray);
        --contrast-background-color: black;
        --active-color: rgb(150, 200, 255);
        --active-background-color: rgba(255, 255, 255, 0.2);
        --border-color: rgb(112, 112, 112);
        --shadow-color: rgba(0, 0, 0, 0.8);
    }
    &:not(.light):not(.dark) {
        @media (prefers-color-scheme: dark) {
            --color: whitesmoke;
            --background-color: var(--dark-gray);
            --contrast-background-color: black;
            --active-color: rgb(150, 200, 255);
            --active-background-color: rgba(255, 255, 255, 0.2);
            --border-color: rgb(112, 112, 112);
            --shadow-color: rgba(0, 0, 0, 0.8);
        }
    }

    --font-family: var(--system-font-stack);
    --padding: 8px;
    --margin: 16px;
    --border: 2px solid var(--border-color);
    --box-radius: 8px;
    --button-radius: 32px;
    --focus-outline: 2px solid var(--active-color);

    --overflow-menu-display: block;
}

/* Fonts */
@font-face {
    font-family: "Noto Sans";
    src: url("media-support/fonts/noto-sans/noto-sans.ttf");
    font-weight: regular;
}
@font-face {
    font-family: "Noto Sans";
    src: url("media-support/fonts/noto-sans/noto-sans-italic.ttf");
    font-weight: regular;
    font-style: italic;
}
@font-face {
    font-family: "Noto Serif";
    src: url("media-support/fonts/noto-serif/noto-serif.ttf");
    font-weight: regular;
}
@font-face {
    font-family: "Noto Serif";
    src: url("media-support/fonts/noto-serif/noto-serif-italic.ttf");
    font-weight: regular;
    font-style: italic;
}

/* Body */
body {
    font-family: var(--font-family);
    font-size: 16px;
    line-height: 1.5em;
    margin: var(--margin);
    color: var(--color);
    background-color: var(--background-color);
}

/* Headers */
h2 {
    padding: var(--margin);
    margin: 0;
    border-bottom: var(--border);
}

/* Anchors */
a {
    color: var(--active-color);
    border-radius: var(--button-radius);
    outline-offset: 2px;
    &:hover, &:focus-visible {
        text-decoration: none;
    }
    &:focus-visible {
        outline: var(--focus-outline);
    }
}

/* Buttons */
button, .button-link {
    font-family: var(--font-family);
    font-size: 16px;
    display: flex;
    align-items: center;
    padding: var(--padding);
    margin: var(--padding);
    border: none;
    border-radius: var(--button-radius);
    fill: var(--color);
    color: var(--color);
    background-color: transparent;
    &:hover, &:focus-visible {
        fill: var(--active-color);
        color: var(--active-color);
        background-color: var(--active-background-color);
    }
    &:focus-visible {
        outline: var(--focus-outline);
    }
    & > svg {
        margin-right: var(--padding);
    }
}
.dangerous-button {
    margin-right: auto;
    &:hover, &:focus-visible {
        fill: var(--danger-color);
        color: var(--danger-color);
    }
    &:focus-visible {
        outline-color: var(--danger-color);
    }
}
.button-link {
    text-decoration: none;
    outline-offset: 0;
}
.active-button {
    fill: var(--active-color);
    color: var(--active-color);
    background-color: var(--active-background-color);
}

/* Text inputs */
input[type="text"], input[type="email"] {
    min-height: 1.5em;
}
input[type="text"], input[type="email"], textarea {
    font-family: var(--font-family);
    font-size: 16px;
    padding: var(--padding);
    border: var(--border);
    border-radius: var(--box-radius);
    color: var(--color);
    background-color: var(--background-color);
    &::placeholder {
        font-style: italic;
        color: darkgray;
    }
    &:focus {
        border-color: var(--active-color);
        outline: none;
    }
}
textarea {
    min-width: 30vw;
    resize: none;
}

/* Stylised radio inputs */
.radio-input-area {
    display: flex;
    gap: var(--margin);
}
.radio-input {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.radio-input > input {
    display: none;
    & + div {
        overflow: hidden;
        position: relative;
        width: calc(sqrt(3) * 40px);
        height: 40px;
        margin: 2px;
        margin-bottom: 4px;
        background-color: var(--background-color);
        border-radius: var(--button-radius);
        outline: var(--border);
        outline-offset: 2px;
    }
    &:checked + div {
        outline-color: var(--active-color);
    }
    &:checked ~ span {
        font-weight: bold;
    }
}
.radio-input > div:focus-visible {
    outline: var(--focus-outline);
}

/* Stylised toggle switches */
.toggle-switch {
    position: relative;
    width: 48px;
    height: 24px;
    &:focus-visible {
        border-radius: var(--button-radius);
        outline: var(--focus-outline);
        outline-offset: 1px;
    }
}
.toggle-switch > input {
    display: none;
    width: 0;
    height: 0;
    & + div {
        position: absolute;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background-color: var(--border-color);
        transition: background-color 0.3s ease;
        border-radius: var(--button-radius);
    }
    & + div::before {
        content: "";
        position: absolute;
        height: 12px;
        width: 12px;
        left: 6px;
        bottom: 6px;
        background-color: var(--contrast-background-color);
        transition: transform 0.3s var(--emphasis-animation);
        border-radius: var(--button-radius);
    }
    &:checked + div {
        background-color: var(--board-color);
    }
    &:checked + div::before {
        transform: translateX(1.5em) scale(150%);
    }
}

/* Game area */
#game-area {
    display: grid;
    grid-template: repeat(3, max-content) / repeat(3, max-content);
    gap: var(--padding);
}

/* Players */
.white-player, .black-player {
    grid-column: 2 / 3;
    display: flex;
    gap: var(--padding);
    margin: 0 var(--padding);
}
.white-player {
    grid-row: 3 / 4;
}
.black-player {
    grid-row: 1 / 2;
}
.flipped {
    & ~ .white-player {
        grid-row: 1 / 2;
    }
    & ~ .black-player {
        grid-row: 3 / 4;
    }
}
.avatar {
    width: 48px;
    height: 48px;
    border: var(--border);
    border-radius: var(--box-radius);
    &.white {
        background-color: var(--board-color);
    }
    &.black {
        background-color: var(--light-square-color);
    }
}
.player-rating {
    color: rgb(112, 112, 112);
}
.player-rating::before {
    content: "(";
}
.player-rating::after {
    content: ")";
}

/* Moves-display and other options */
#board-info {
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    grid-row: 2 / 3;
    grid-column: 3 / 4;
    min-width: 25em;
    height: calc(8 * var(--board-square-width));
    border: var(--border);
    border-radius: var(--box-radius);
    background-color: var(--contrast-background-color);
}
#move-grid {
    flex: 1;
    overflow-y: auto;
}
#move-grid > div {
    display: flex;
    align-items: center;
    &:nth-child(2n) {
        background-color: var(--background-color);
    }
}
#move-grid > div > .info {
    padding: var(--padding);
    flex: 1;
}
#move-grid > div > div {
    padding: var(--padding);
    flex: 0 0 10%;
}
#move-grid > div > button {
    flex: 1 0 calc(45% - 2*var(--padding));
    margin-left: 0;
    &:not(.move-notation) {
        &:hover, &:focus-visible {
            fill: var(--color);
            color: var(--color);
            background-color: transparent;
        }
        &:focus-visible {
            outline: none;
        }
    }
}
#move-grid > div > .move-notation > span {
    width: 16%;
    aspect-ratio: 1;
}

/* Turn indicator */
#to-move {
    display: flex;
    align-items: center;
    padding: var(--padding);
    background-color: var(--background-color);
    border-top: var(--border);
}
#to-move > div {
    overflow: hidden;
    position: relative;
    width: 32px;
    height: 32px;
    margin-right: var(--margin);
    background-color: white;
    border: var(--border);
    border-radius: var(--box-radius);
}
#to-move > div > div {
    position: absolute;
    left: 50%;
    width: 100%;
    height: 100%;
}

/* Button-containers */
.options {
    display: flex;
    justify-content: flex-end;
    padding: 0;
    border-top: var(--border);
    background-color: var(--background-color);
}

/* Overflow menus */
.overflow-container {
    display: none;
    position: absolute;
    right: 4px;
    bottom: calc(100% + 8px);
    overflow: hidden;
    background-color: var(--background-color);
    border: var(--border);
    border-radius: var(--box-radius);
    z-index: 10;
    will-change: transform;
}
.overflow-container > button {
    width: 100%;
    margin: 0;
    border-radius: 0;
    outline-offset: -2px;
    pointer-events: none;
    &:first-child {
        border-top-left-radius: var(--box-radius);
        border-top-right-radius: var(--box-radius);
    }
    &:last-child {
        border-bottom-left-radius: var(--box-radius);
        border-bottom-right-radius: var(--box-radius);
    }
}
.more-button > * {
    pointer-events: none;
}

/* Drop-down menus */
.drop-down-menu-container {
    position: relative;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
.drop-down-arrow {
    position: absolute;
    top: 50%;
    right: var(--padding);
    fill: var(--color);
    background-color: var(--background-color);
    transform: translateY(-50%) rotate(-180deg);
    pointer-events: none;
    will-change: transform;
}
.drop-down-menu-selection {
    padding: var(--padding);
    background-color: var(--background-color);
    border: var(--border);
    border-radius: var(--box-radius);
    cursor: default;
}
.drop-down-menu-selection:focus {
    border: var(--focus-outline);
    outline: none;
}
.drop-down-menu {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    overflow: hidden;
    width: 100%;
    margin-top: 2px;
    background-color: var(--background-color);
    border: var(--border);
    border-radius: var(--box-radius);
    box-shadow: 8px 16px 16px var(--shadow-color);
    z-index: 10;
}
.drop-down-menu > button {
    width: 100%;
    margin: 0;
    border-radius: 0;
    outline-offset: -2px;
    pointer-events: none;
    &:first-child {
        border-top-left-radius: var(--box-radius);
        border-top-right-radius: var(--box-radius);
    }
    &:last-child {
        border-bottom-left-radius: var(--box-radius);
        border-bottom-right-radius: var(--box-radius);
    }
}

/* Full-screen boxes */
.full-screen-box-container {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.6);
    z-index: 11;
}
.full-screen-box-container > div {
    position: absolute;
    top: 50%;
    left: 50%;
    overflow: auto;
    max-height: 90%;
    background-color: var(--background-color);
    border: var(--border);
    border-radius: var(--box-radius);
    transform: translate(-50%, -50%);
    & > div:first-child {
        position: sticky;
        top: 0;
        background-color: var(--background-color);
        z-index: 1;
    }
}
.full-screen-box-container > div > .options:last-child {
    position: sticky;
    bottom: 0;
}
.input-grid {
    display: grid;
    gap: var(--margin);
    padding: 16px;
    background-color: var(--contrast-background-color);
}
.input-grid > span {
    display: flex;
    align-items: center;
}


/* Settings-views (general, advanced, etc.) */
.settings-view {
    grid-template-columns: repeat(2, auto);
}

/* Loading indicator */
.loading-indicator {
    display: flex;
    gap: var(--margin);
    width: max-content;
    transform: translate(-50%, -50%);
    & > div {
        width: 16px;
        height: 16px;
        background-color: var(--active-color);
        border-radius: 50%;
        animation: pulse 1.6s infinite ease-in-out;
        &:nth-child(2) {
            animation-delay: 0.2s;
        }
        &:nth-child(3) {
            animation-delay: 0.4s;
        }
    }
}

/* Responsive web design */
@media (prefers-reduced-motion: reduce) {
    .btn {
        transition: none;
    }
}
@media (max-width: 1280px) {
    #game-area {
        grid-template-rows: repeat(4, max-content);
    }
    #board-info {
        grid-row: 4 / 5;
        grid-column: 2 / 3;
        width: calc(8 * var(--board-square-width));
        margin: 0;
    }
}
@media (max-width: 800px) {
    button, .button-link {
        & > svg {
            margin-right: 0;
        }
    }
    .button-desc {
        display: none;
    }
    .more-button > svg {
        transform: rotate(90deg);
    }
    :root {
        --board-square-width: round(down, min(
        calc((100vh - 2*2px) / 8),
        calc((100vw - 8px) / 8)), 1px);
        --options-display: flex;
    }
    body {
        margin: 0;
    }
    #game-area {
        grid-template: repeat(5, max-content) / repeat(1, max-content);
    }
    .square-18 {border-radius: 0 0 0 0;}
    .square-88 {border-radius: 0 0 0 0;}
    .square-81 {border-radius: 0 0 0 0;}
    .square-11 {border-radius: 0 0 0 0;}
    .flipped {
        .square-18 {border-radius: 0 0 0 0;}
        .square-88 {border-radius: 0 0 0 0;}
        .square-81 {border-radius: 0 0 0 0;}
        .square-11 {border-radius: 0 0 0 0;}
    }
    #eval-bar {
        grid-row: 1 / 2;
        grid-column: 1 / 2;
        width: auto;
        height: 16px;
        border: none;
        border-bottom: var(--border);
        border-radius: 0;
        &::before {
            transform: translateX(calc(50% * clamp(-1, 1 * var(--evaluation) / 5, 1) + 50%));
        }
        &::after {
            top: 50%;
            left: 4px;
            transform: translateY(-100%);
        }
    }
    .flipped ~ #eval-bar {
        border-top: none;
        border-bottom: var(--border);
        &::after {
            transform: rotate(180deg);
        }
    }
    .white-player {
        grid-row: 4 / 5;
        grid-column: 1 / 2;
        margin-right: 0;
    }
    .chess-board {
        grid-row: 3 / 4;
        grid-column: 1 / 2;
        border-left: none;
        border-right: none;
        border-radius: 0;
        margin: 0;
    }
    .black-player {
        grid-row: 2 / 3;
        grid-column: 1 / 2;
        margin-right: 0;
    }
    .flipped {
        & ~ .white-player {
            grid-row: 2 / 3;
        }
        & ~ .black-player {
            grid-row: 4 / 5;
        }
    }
    .avatar {
        width: 32px;
        height: 32px;
        border: none;
    }
    #board-info {
        grid-row: 5 / 6;
        grid-column: 1 / 2;
        border-left: none;
        border-right: none;
        border-radius: 0;
    }
}

/* Animations */
@keyframes pulse {
    from, to {
        opacity: 1;
        transform: scale(1);
    }
    50% {
        opacity: 0.5;
        transform: scale(0.7);
    }
}
@keyframes send {
    from {
        opacity: 0;
        transform: translateX(32px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}
@keyframes receive {
    from {
        opacity: 0;
        transform: translateX(-32px);
    }
    to {
        opacity: 1;
        transform: translateX(0);
    }
}
