@import url("https://fonts.googleapis.com/css2?family=IBM+Plex+Mono:wght@400;500&display=swap");

:root {
    --color-bg: #f2f2f0;
    --color-surface: #e7e7e4;
    --color-text: #0d0d0d;
    --color-muted: #595955;
    --color-accent: #0d0d0d;
    --color-on-accent: #f2f2f0;
    --color-accent-hover: #30302d;
    --color-border: #c2c2bc;
    --color-grid: #d1d1cb;
    --font: "IBM Plex Mono", monospace;
    --text-h1: 28px;
    --text-small: 14px;
    --text-ui: 14px;
    --space-2: 8px;
    --space-3: 16px;
    --space-4: 24px;
    --space-5: 32px;
    --edge: 20px;
    --ease-ui: cubic-bezier(.16, 1, .3, 1);
    --board: min(70vw, 480px);
}

@media (min-width: 768px) {
    :root {
        --text-h1: 48px;
    }
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    min-height: 100vh;
    background-color: var(--color-bg);
    background-image: radial-gradient(
        var(--color-grid) 1.2px,
        transparent 1.6px
    );
    background-size: 24px 24px;
    background-attachment: fixed;

    color: var(--color-text);
    font: 400 var(--text-ui)/1.4 var(--font);

    -webkit-font-smoothing: antialiased;
}

a {
    color: inherit;
    text-decoration: none;
}

:focus-visible {
    outline: 2px solid var(--color-text);
    outline-offset: 4px;
}

::selection {
    background: var(--color-text);
    color: var(--color-bg);
}

nav,
main {
    position: relative;
    z-index: 1;
}

nav {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: var(--space-5);
    padding: var(--edge);
}

nav .brand {
    font-weight: 500;
}

nav .links {
    display: flex;
    align-items: center;
    gap: var(--space-4);
}

nav .links a {
    text-transform: uppercase;
    transition: opacity .2s ease;
}

nav .links a:hover {
    text-decoration: underline;
    text-underline-offset: 4px;
}

nav .links a.active::before {
    content: "";
    display: inline-block;
    width: 8px;
    height: 8px;
    margin-right: 8px;
    background: var(--color-text);
}

nav .actions {
    justify-self: end;
    display: flex;
    gap: var(--space-2);
}

.btn {
    display: inline-flex;
    justify-content: center;
    align-items: center;

    height: 36px;
    padding: 0 var(--space-3);

    border: 1px solid var(--color-text);
    white-space: nowrap;
    text-transform: uppercase;

    transition:
        background-color .25s var(--ease-ui),
        color .25s var(--ease-ui);
}

.btn-fill {
    background: var(--color-accent);
    color: var(--color-on-accent);
}

.btn-fill:hover {
    background: var(--color-accent-hover);
}

.btn-line {
    color: var(--color-text);
}

.btn-line:hover {
    background: var(--color-surface);
}

main {
    max-width: 720px;
    margin: 0 auto;
    padding: 48px var(--edge);
}

main.wide {
    max-width: 1200px;
    width: 100%;
}

h1 {
    margin-bottom: var(--space-3);
    font-size: var(--text-h1);
    line-height: 1.1;
    letter-spacing: -.01em;
    font-weight: 500;
    text-wrap: balance;
}

p {
    color: var(--color-muted);
    font-size: var(--text-small);
    line-height: 1.4;
}


/* Game layout */

.game-layout {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: var(--space-4);

    margin-top: var(--space-4);
}

#move-history {
    flex: 0 0 220px;
    min-width: 0;

    padding: var(--space-3);

    border: 1px solid var(--color-text);
    background: var(--color-bg);
}

#move-history h2 {
    margin-bottom: var(--space-2);

    font-size: var(--text-small);
    font-weight: 500;
    text-transform: uppercase;
}

#last-move {
    margin-bottom: var(--space-4);

    padding: 10px;
    border: 1px solid var(--color-border);

    font-size: 20px;
}

#move-history ol {
    list-style: none;
    max-height: 350px;
    overflow-y: auto;
}

#move-history li {
    display: grid;
    grid-template-columns: 32px 1fr 1fr;
    gap: 5px;

    padding: 5px 0;
    border-bottom: 1px solid var(--color-border);
}

#move-history .num {
    color: var(--color-muted);
}


/* Shared chessboard styling */

.board-container {
    width: var(--board);
    flex: 0 1 var(--board);
    min-width: 0;
}

:is(#chessboard, #puzzle-board) {
    width: 100%;
    aspect-ratio: 1 / 1;

    margin: 0;
    border: 1px solid var(--color-text);
    border-collapse: collapse;
    table-layout: fixed;

    user-select: none;
    -webkit-user-select: none;
}

:is(#chessboard, #puzzle-board) td {
    width: 12.5%;
    height: 12.5%;
    padding: 0;

    text-align: center;
    vertical-align: middle;

    background: var(--color-border);

    user-select: none;
    -webkit-user-select: none;
}

/* Light squares */

:is(#chessboard, #puzzle-board) tr:nth-child(odd) td:nth-child(odd),
:is(#chessboard, #puzzle-board) tr:nth-child(even) td:nth-child(even) {
    background: var(--color-surface);
}

/* Chess pieces */

:is(#chessboard, #puzzle-board) img {
    display: block;

    width: 78%;
    height: 78%;
    margin: auto;

    object-fit: contain;

    cursor: grab;

    user-select: none;
    -webkit-user-select: none;
}

:is(#chessboard, #puzzle-board) img:active {
    cursor: grabbing;
}

/* Square hover */

:is(#chessboard, #puzzle-board) td:hover {
    outline: 2px solid var(--color-text);
    outline-offset: -2px;
}

/* Drag and drop */

:is(#chessboard, #puzzle-board) td.drag-over {
    outline: 3px solid var(--color-text);
    outline-offset: -3px;
}


/* Evaluation bar */

.evaluation-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;

    flex: 0 0 24px;

    height: var(--board);
}

.evaluation-label {
    min-width: 55px;

    font-size: 14px;
    font-weight: 500;
    text-align: center;
}

.evaluation-bar {
    position: relative;

    width: 18px;
    flex: 1;

    overflow: hidden;

    background: #222;
    border: 1px solid #555;
    border-radius: 5px;
}

#evaluation-fill {
    position: absolute;
    bottom: 0;

    width: 100%;
    height: 50%;

    background: #f0f0f0;

    transition: height .4s ease;
}


/* Move history scrollbar */

#move-history ol::-webkit-scrollbar {
    width: 5px;
}

#move-history ol::-webkit-scrollbar-thumb {
    background: var(--color-border);
}

#move-history ol::-webkit-scrollbar-track {
    background: transparent;
}


/* Responsive layout */

@media (max-width: 950px) {
    .game-layout {
        flex-wrap: wrap;
        justify-content: center;
    }

    #move-history {
        order: 2;
        flex: 1 1 100%;
        max-width: 480px;
    }

    .board-container {
        order: 1;
    }

    .evaluation-container {
        order: 1;
    }
}

@media (max-width: 767px) {
    nav {
        grid-template-columns: 1fr auto;
        gap: var(--space-3);
    }

    nav .links {
        grid-column: 1 / -1;
        grid-row: 2;
        flex-wrap: wrap;
        gap: var(--space-3);
    }

    main {
        padding: 32px var(--edge);
    }

    main.wide {
        padding-top: 24px;
    }

    :root {
        --board: min(78vw, 420px);
    }

    .game-layout {
        gap: var(--space-3);
    }

    #move-history {
        max-width: 100%;
    }
}

@media (max-width: 400px) {
    nav .links {
        font-size: 11px;
    }

    .evaluation-container {
        flex-basis: 20px;
    }

    .evaluation-bar {
        width: 14px;
    }
}


/* Match History */

.match-history {
    margin-top: var(--space-4);
    border: 1px solid var(--color-text);
    background: var(--color-bg);
    overflow-x: auto;
}

.history-table {
    width: 100%;
    border-collapse: collapse;
    text-align: left;
}

.history-table th {
    background: var(--color-surface);
    font-weight: 500;
    text-transform: uppercase;
    font-size: var(--text-small);
}

.history-table th,
.history-table td {
    padding: var(--space-3);
    border-bottom: 1px solid var(--color-border);
}

.history-table tbody tr:last-child td {
    border-bottom: none;
}

.history-table tbody tr:hover {
    background: var(--color-surface);
}

.history-table th:last-child,
.history-table td:last-child {
    text-align: right;
}

.history-table .btn {
    min-width: 90px;
    font-size: var(--text-small);
}

@media (max-width: 600px) {
    .history-table th,
    .history-table td {
        padding: var(--space-2);
        font-size: 12px;
    }
}


/* Save Game */

.save-game-container {
    margin-top: 24px;
    padding-top: 16px;
    border-top: 1px solid #ccc;
}

.save-game-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;

    width: 100%;
    padding: 12px 16px;

    background: #202020;
    color: #fff;
    border: 1px solid #202020;
    border-radius: 4px;

    font-family: inherit;
    font-size: 13px;
    font-weight: 500;
    text-transform: uppercase;
    letter-spacing: 1px;

    cursor: pointer;
    transition: all 0.15s ease;
}

.save-game-btn:hover {
    background: #404040;
    border-color: #404040;
    transform: translateY(-1px);
}

.save-game-btn:active {
    transform: translateY(1px);
}

.save-game-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.save-game-btn:focus-visible {
    outline: 2px solid #777;
    outline-offset: 3px;
}

#save-status {
    margin-top: 10px;
    font-size: 11px;
    text-align: center;
    color: #777;
    min-height: 16px;
}


/* Match History Actions */

.history-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
}

.history-actions form {
    margin: 0;
}

.delete-game-btn {
    padding: 8px 12px;
    background: transparent;
    color: #a33;
    border: 1px solid #a33;
    font-family: inherit;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.delete-game-btn:hover {
    background: #a33;
    color: white;
}


/* Rename Game */

.rename-game-btn {
    padding: 8px 12px;
    background: transparent;
    color: #202020;
    border: 1px solid #202020;
    font-family: inherit;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s ease;
}

.rename-game-btn:hover {
    background: #202020;
    color: white;
}


/* Game timestamps */

.game-date {
    white-space: nowrap;
    font-size: 12px;
}

.game-time {
    display: block;
    margin-top: 4px;
    font-size: 11px;
    opacity: 0.6;
}


/* Match History Filters */

.history-filters {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 24px;
    margin-bottom: 16px;
}

.history-filters input,
.history-filters select {
    padding: 10px 12px;
    background: transparent;
    color: inherit;
    border: 1px solid #aaa;
    border-radius: 0;
    font-family: inherit;
    font-size: 12px;
}

.history-filters input {
    flex: 1;
    min-width: 200px;
}

.history-filters input:focus,
.history-filters select:focus {
    outline: 1px solid currentColor;
}

.filter-btn {
    padding: 10px 18px;
    background: #202020;
    color: white;
    border: 1px solid #202020;
    font-family: inherit;
    font-size: 12px;
    cursor: pointer;
    transition: background 0.15s ease;
}

.filter-btn:hover {
    background: #404040;
}

.reset-filters {
    padding: 10px;
    font-size: 12px;
    color: inherit;
    text-decoration: underline;
}

.history-results {
    font-size: 12px;
    opacity: 0.65;
    margin-bottom: 12px;
}

.game-id {
    display: block;
    font-size: 11px;
    opacity: 0.6;
    margin-top: 4px;
}

@media (max-width: 600px) {
    .history-filters input {
        width: 100%;
        flex-basis: 100%;
    }

    .history-filters select {
        flex: 1;
    }
}


/* Player profiles */

.profile-panel {
    margin: 24px 0;
    padding: 24px;
    border: 1px solid var(--color-border);
    background: var(--color-bg);
}

.profile-hero {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 20px;
}

.profile-hero > div:nth-child(2) {
    flex: 1;
    min-width: 120px;
    overflow-wrap: anywhere;
}

.profile-avatar {
    display: grid;
    place-items: center;
    width: 76px;
    height: 76px;
    background: var(--color-accent);
    color: var(--color-on-accent);
    font-size: 52px;
}

.profile-panel small {
    color: var(--color-muted);
}

.profile-stats {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 10px;
    margin: 24px 0;
}

.profile-stats > div {
    border: 1px solid var(--color-border);
    padding: 12px;
}

.profile-stats strong {
    display: block;
    font-size: 28px;
    margin-top: 8px;
}

.profile-history-link {
    text-decoration: underline;
}

.profile-player {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--color-border);
    overflow-wrap: anywhere;
}

.profile-create {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin: 16px 0;
}

.profile-create input {
    flex: 1;
    min-width: 140px;
    padding: 10px;
    font: inherit;
    color: inherit;
    background: transparent;
    border: 1px solid var(--color-border);
}

@media (max-width: 600px) {
    .profile-stats {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .profile-panel {
        padding: 16px;
    }
}


/* Puzzle page */

.puzzle-sidebar button {
    width: 100%;
    margin-bottom: var(--space-2);
    font: inherit;
    cursor: pointer;
    border-radius: 0;
}

.puzzle-sidebar .btn-line {
    background: transparent;
}

.puzzle-sidebar .btn-line:hover {
    background: var(--color-surface);
}

.puzzle-sidebar button:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.puzzle-sidebar input {
    width: 100%;
    padding: 10px;
    margin-bottom: var(--space-2);

    font: inherit;
    color: var(--color-text);
    background: var(--color-bg);

    border: 1px solid var(--color-border);
    border-radius: 0;
}

.puzzle-sidebar hr {
    border: 0;
    border-top: 1px solid var(--color-border);
    margin: var(--space-3) 0;
}

.puzzle-detail {
    display: flex;
    justify-content: space-between;
    gap: 8px;

    padding: 8px 0;
    border-bottom: 1px solid var(--color-border);
}

.puzzle-detail span {
    color: var(--color-muted);
}

#puzzle-themes {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.puzzle-theme {
    padding: 4px 8px;
    border: 1px solid var(--color-border);
    font-size: 12px;
}

#puzzle-status {
    margin-top: var(--space-3);
    padding: 12px;

    border: 1px solid var(--color-border);
    background: var(--color-bg);

    text-align: center;
}

#puzzle-board td.selected-square {
    outline: 3px solid var(--color-text);
    outline-offset: -3px;
}

.puzzle-success {
    color: #267344;
}

.puzzle-error {
    color: #b42318;
}

.puzzle-credit {
    margin-top: var(--space-2);
    font-size: 12px;
}

.puzzle-credit a {
    text-decoration: underline;
}