body {
    display: flex;
    flex-direction: row;
    align-items: stretch;
    margin: 0;
    width: 100%;
    min-height: 100vh;
    overflow-x: hidden;
    overflow-y: hidden;
    --controls-width: 24em;
    --color-highlight: #78b0ff;
    --color-tile-light: #EDBEA1;
    --color-tile-dark: #A37583;
}

/* Left panel */

.controls {
    z-index: 1;
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    width: var(--controls-width);
    flex-shrink: 0;
    background: #262123;
    color: white;
    box-shadow: -12px 0px 12px 12px rgba(0, 0, 0, 0.4);
    overflow: hidden;
}

#fen {
    box-sizing: border-box;
    padding: 4px;
    background: transparent;
    color: inherit;
    border: none;
    resize: none;
    width: 100%;
    font-size: 10pt;
    line-break: anywhere;
    flex-shrink: 1;
    flex-grow: 1;
    min-height: 12em;
    box-shadow: 0px -8px 12px 12px rgba(0, 0, 0, 0.25);
}

#fen:focus {
    outline: none;
}

#settings {
    font-family: monospace;
    font-size: 12pt;
    padding: 0.5em 1.0em;
    display: flex;
    flex-direction: column;
}

#settings section {
    background: rgba(255, 255, 255, 0.08);
    padding: 0.2em 0.3em;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 0.5em;
    margin-bottom: 0.5em;
    border-radius: 2px;
    box-shadow: 0px 2px 8px -3px rgba(0, 0, 0, 0.2);
}

section > .input-container {
    margin-top: 0.2em;
    margin-bottom: 0.2em;
}

#width, #height {
    width: 2em;
    font-family: inherit;
    font-size: inherit;
    background: rgba(0, 0, 0, 0.4);
    border: 1px solid black;
    color: #f0f0f0;
    padding: 2px 8px;
    text-align: center;
    transition: border-color 0.2s, color 0.2s;
    border-radius: 2px;
}

#width:active, #width:focus, #width:hover, #height:active, #height:focus, #height:hover {
    outline: none;
    border: 1px solid white;
    color: white;
}

#settings button {
    font-family: inherit;
    font-size: inherit;
    background: transparent;
    border: 1px solid rgba(0, 0, 0, 0.4);
    border-radius: 2px;
    color: var(--color-highlight);
    padding: 6px 8px;
    cursor: pointer;
    margin: 0.15em 0.1em;
}

#settings button:hover {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid white;
}

#settings button:active {
    border: 1px solid black;
}

#settings button:active, #settings button.selected {
    color: white;
    background: rgba(0, 0, 0, 0.25);
}

#settings select {
    font-family: inherit;
    font-size: inherit;
    background: rgba(0, 0, 0, 0.25);
    border: 1px solid rgba(0, 0, 0, 0.4);
    border-radius: 2px;
    padding: 6px 8px;
    color: var(--color-highlight);
    margin: 0.15em 0.1em;
    transition: border-color 0.2s, color 0.2s;
}

#settings select:hover {
    border: 1px solid white;
}

/* Right panel */

.render {
    flex-grow: 1;
    height: 100vh;
    --status-height: 28px;
    --n-pieces: 24;
    --pieces-height: calc((100vw - var(--controls-width)) / (var(--n-pieces) + 1) * var(--rows));
    --rows: 1;
    position: relative;
    display: flex;
    flex-direction: column;
}

#axes {
    width: 100%;
    height: calc(100vh - var(--status-height) - var(--pieces-height));
    flex-grow: 1;
    margin: 0;
    box-sizing: border-box;
    object-fit: contain;
}

#canvas {
    width: 100%;
    height: calc(100vh - var(--status-height) - var(--pieces-height));
    position: absolute;
    top: 0;
    left: 0;
}

/* Bottom-right panel */

#pieces {
    display: flex;
    flex-direction: row;
    box-shadow: 0px 6px 12px 4px rgba(0, 0, 0, 0.4);
}

.mobile-vertical {
    display: flex;
    flex-direction: row;
}

#pieces-none-container {
    background: #c3bfc9;
}

#pieces-white, #pieces-black, #pieces-none {
    height: calc(var(--pieces-height));
    display: flex;
    flex-direction: row;
    flex-wrap: nowrap;
    list-style: none;
    align-items: flex-start;
    padding: 0;
    margin: 0;
}

#black-phantom {
    visibility: hidden;
    position: absolute;
}

#pieces li {
    background: #c3bfc9;
    color: black;
    display: flex;
    align-items: center;
    justify-content: center;
    width: calc(var(--pieces-height) / var(--rows));
    height: calc(var(--pieces-height) / var(--rows));
}

#pieces li > img {
    width: calc(var(--pieces-height) / var(--rows));
    height: calc(var(--pieces-height) / var(--rows));
    background: #c3bfc9;
    border: 2px solid transparent;
    box-sizing: border-box;
    transition: background-color 0.2s, border-color 0.2s;
}

#pieces li.selected > img {
    background: #9e97a7;
    border: 2px solid #301014;
}

#pieces li > img:hover {
    cursor: pointer;
    background: #9e97a7;
}

#pieces li > img:active {
    cursor: pointer;
    border: 2px solid white;
}

.bar {
    flex-grow: 0;
    background: #262123;
    height: var(--status-height);
    box-sizing: border-box;
    display: flex;
    flex-direction: row;
    font-family: monospace;
    color: white;
}

#status {
    padding: 4px;
    flex-grow: 1;
    overflow-x: auto;
    white-space: nowrap;
}

#status.ok {
    color: #a0ffa0;
}

#status.err {
    color: #ffa0a0;
}

/* Mobile & tablet */

@media (max-width: 1300px) {
    .render {
        --n-pieces: 12;
        --rows: 2;
    }

    .mobile-vertical {
        flex-direction: column;
    }

    #pieces-white, #pieces-black, #pieces-none {
        height: calc(var(--pieces-height) / 2);
    }
    #pieces-white, #pieces-black {
        width: calc(var(--pieces-height) / var(--rows) * var(--n-pieces));
    }

    #black-phantom {
        visibility: visible;
        position: relative;
        display: block;
        flex-shrink: 0;
    }
}

@media (max-width: 720px) {
    html {
        overflow-y: scroll;
    }

    body {
        flex-direction: column-reverse;
        overflow-y: auto;
        --controls-width: calc(100vw - var(--scrollbar-width, 0px));
    }

    .render {
        --pieces-height: calc(100vw / (var(--n-pieces) + 1) * var(--rows));
        height: calc(100vh);
        --n-pieces: 6;
        --rows: 4;
    }

    #pieces-white, #pieces-black {
        flex-wrap: wrap;
    }

    #canvas, #axes {
        height: calc(100vh - var(--status-height) - var(--pieces-height));
    }

    .controls {
        width: 100vw;
        flex-direction: column-reverse;
        box-shadow: none;
    }

    #fen {
        font-size: 12pt;
        box-shadow: 0px 8px 12px 12px rgba(0, 0, 0, 0.25);
    }

    #settings section:not(.mobile-first) {
        order: 1;
    }

    #settings section.mobile-first {
        order: 0;
    }
}

@media (hover: none) {
    #pieces li > img:hover {
        background: #909090;
        border: 1px solid transparent;
    }

    #pieces li.selected > img {
        background: #606060;
        border: 1px solid white;
    }
}

@media (max-width: 720px) and (min-aspect-ratio: 0.8) {
    body {
        --rows: 4;
    }
}
