/*
 * style.css - styling of the input page.
 *
 * The visual grammar is borrowed from mat-en-web: gradient background,
 * white cards with rounded corners and drop shadow, coloured buttons.
 *
 * It ONLY applies to this page. The exported diagrams keep their sober
 * template, grey squares and A4, because they are meant to be printed and
 * the brown of the screen would come out as a dirty grey on a black and
 * white printer.
 *
 * The colours of the board itself are not defined here: the brown
 * #b58863 and the cream #f0d9b5 are the native values of
 * chessboard-1.0.0.min.css, there is nothing to override.
 */

:root {
    --primary:    #2c3e50;
    --info:       #3498db;
    --success:    #27ae60;
    --danger:     #e74c3c;
    --warning:    #f39c12;
    --border:     #bdc3c7;
    --text:       #2c3e50;
    --text-muted: #7f8c8d;
}

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

/*
 * Vertical margins of 12 px: together with the board width below, they
 * let the page fit without a scrollbar on a 1600x900 screen, including
 * with Windows scaling at 125%, where the usable window drops to about
 * 1280x600 CSS px. Measured with the default font size of the browsers
 * (16 px), the page fits from 598 px of usable height (671 px below
 * 1200 px of width, see the end of this file); a larger font size raises
 * these thresholds, see the board width below. That leaves 2 px to spare
 * at 600 px: a taller header or footer brings the scrollbar back. Below
 * these thresholds the form, not the board, sets the height of the page:
 * shrinking the board no longer helps.
 */
body {
    min-height: 100vh;
    padding: 12px 20px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: var(--text);
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                 "Helvetica Neue", Arial, sans-serif;
    font-size: 15px;
    line-height: 1.5;
}

/* --------------------------------------------------------------- */
/* Header                                                           */
/* --------------------------------------------------------------- */

/*
 * A line height of 1.2 instead of the 1.5 of the body: the title and the
 * subtitle hold on one line each, and the header gives back about 13 px
 * of height to the board (see its width below).
 */
.page-header {
    max-width: 1180px;
    margin: 0 auto 12px;
    color: #fff;
    text-align: center;
    line-height: 1.2;
}

.page-header h1 {
    font-size: 1.8rem;
    font-weight: 700;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
}

.page-header p {
    opacity: 0.9;
    font-size: 0.95rem;
}

/* --------------------------------------------------------------- */
/* Alert banner                                                     */
/* --------------------------------------------------------------- */

.alert {
    max-width: 1180px;
    margin: 0 auto 20px;
    padding: 12px 18px;
    border-radius: 8px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    color: #fff;
    font-weight: 600;
    text-align: center;
}

.alert-warning { background: var(--warning); }

/* --------------------------------------------------------------- */
/* General layout                                                   */
/* --------------------------------------------------------------- */

/*
 * align-items: stretch gives both cards the same height: their top and
 * bottom edges line up, whatever the size of the board.
 */
.container {
    display: flex;
    flex-wrap: wrap;
    gap: 20px;
    align-items: stretch;
    justify-content: center;
    max-width: 1180px;
    margin: 0 auto;
}

.card {
    padding: 16px 20px;
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 340px;
    max-width: 100%;
}

/* --------------------------------------------------------------- */
/* Board and outer coordinates                                      */
/* --------------------------------------------------------------- */

/*
 * The frame is the positioning reference of the coordinates: app.js
 * places them relative to its rectangle.
 */
.board-frame {
    position: relative;
    padding-left: 1.7rem;
}

/*
 * The width of the board follows the height of the window, which avoids
 * a scrollbar on a short screen.
 *
 * The formula comes from the vertical budget of the page, measured in a
 * browser: header, margins, button bar, palette gap and footer take
 * about 231 px with the default font size, and the board block is 1.25
 * times as high as it is wide (ten squares for eight: the board and its
 * two palettes). The page therefore fits as long as that budget + 1.25 x
 * width stays within 100vh, that is a width of 80vh minus a constant.
 *
 * Part of the budget is set in rem (the header, the gap above the bottom
 * palette), so it grows with the font size chosen in the browser: the
 * constant is therefore made of pixels and rem. Measured pixel by pixel in
 * Chromium and Firefox, with chessboard.js rounding the square size down,
 * the smallest constant that never shows a scrollbar between the lowest
 * possible height and 1000 px is 189 px for a font size of 16 px, 205 px
 * for 20 px and 221 px for 24 px: 125 px + 4rem. 10 px of margin are
 * added, for font and browser differences. The width is capped at 460 px
 * at the top and 280 px at the bottom.
 *
 * app.js calls board.resize() again on resize, so the square size follows.
 */
#board {
    width: clamp(280px, calc(80vh - 135px - 4rem), 460px);
    max-width: 100%;
    /*
     * Touch screens: chessboard.js drags the pieces with the finger itself,
     * on the board and from the two palettes, which sit inside #board.
     * Without this, the browser turns the gesture into a page scroll and the
     * piece never moves.
     */
    touch-action: none;
}

/*
 * chessboard.js sticks the bottom palette against the board. The two are
 * moved apart to free the band where the file letters sit, otherwise the
 * palette pieces cover them.
 *
 * The selector targets a substring of the class name: chessboard.js
 * appends a suffix of its own to it, better not to hard-code it here.
 */
#board [class*="spare-pieces-bottom"] {
    margin-top: 1.7rem;
}

.coords {
    position: absolute;
    display: flex;
    color: var(--text);
    font-size: 0.95rem;
    font-weight: 600;
}

.coords span {
    display: flex;
    flex: 1 1 0;
    align-items: center;
    justify-content: center;
}

.coords-ranks {
    left: 0;
    width: 1.7rem;
    flex-direction: column;
}

.coords-files {
    height: 1.6rem;
    flex-direction: row;
}

.card-board { flex: 0 0 auto; }

.toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    margin-top: 12px;
}

/*
 * How to use the board, in a card to the right of the form. It does not
 * stretch over the whole height of the row: align-self lines it up with
 * the top of the other two cards, level with the board. Its title reuses
 * the style of the form headings (.block h2).
 */
.card-help {
    flex: 0 0 220px;
    align-self: flex-start;
    font-size: 0.88rem;
    line-height: 1.5;
}

.card-help h2 {
    margin-bottom: 8px;
    color: var(--primary);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

/*
 * The card is collapsible: the summary line carries the heading and the
 * disclosure triangle, and the whole line is clickable. The heading stays
 * inline so that the triangle sits beside it.
 */
.card-help summary {
    cursor: pointer;
}

.card-help summary h2 {
    display: inline;
}

.card-help details[open] > summary {
    margin-bottom: 8px;
}

.card-help p + p {
    margin-top: 8px;
}

/* --------------------------------------------------------------- */
/* Form                                                             */
/* --------------------------------------------------------------- */

.block h2 {
    margin-bottom: 8px;
    color: var(--primary);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
}

.row {
    display: flex;
    gap: 8px;
}

.row input[type="text"],
.row textarea { flex: 1 1 auto; min-width: 0; }

/* The button stays at the top when the paste area grows. */
.row { align-items: flex-start; }

input[type="text"],
textarea {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: #fff;
    color: inherit;
    font: inherit;
}

input[type="text"]:focus,
textarea:focus {
    border-color: var(--info);
    outline: none;
}

textarea {
    resize: vertical;
    font-size: 0.9rem;
}

#fen-paste {
    font-family: "DejaVu Sans Mono", Consolas, monospace;
    font-size: 0.8rem;
}

.counter {
    margin-top: 4px;
    color: var(--text-muted);
    font-size: 0.78rem;
    text-align: right;
}

.counter-full { color: var(--danger); font-weight: 600; }

.message {
    min-height: 1.2em;
    margin-top: 6px;
    font-size: 0.82rem;
}

.message.success { color: var(--success); }
.message.error   { color: var(--danger); }

/*
 * Navigation through a PGN game. The hidden attribute must win over
 * display: flex, hence the explicit rule.
 */
.nav {
    display: flex;
    gap: 6px;
    align-items: center;
    margin-top: 6px;
}

.nav[hidden] { display: none; }

.nav .btn {
    padding: 5px 11px;
    font-size: 1rem;
    line-height: 1;
}

.nav .btn:disabled {
    opacity: 0.4;
    cursor: default;
}

.move {
    margin-left: 6px;
    font-family: "DejaVu Sans Mono", Consolas, monospace;
    font-size: 0.82rem;
}

.choice {
    display: flex;
    gap: 18px;
}

.choice label {
    display: flex;
    gap: 6px;
    align-items: center;
    cursor: pointer;
}

/* --------------------------------------------------------------- */
/* Buttons                                                          */
/* --------------------------------------------------------------- */

.btn {
    padding: 9px 16px;
    border: none;
    border-radius: 6px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
    color: #fff;
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: box-shadow 0.15s, filter 0.15s;
}

.btn:hover {
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.15);
    filter: brightness(1.08);
}

.btn-success { background: var(--success); }
.btn-info    { background: var(--info); }
.btn-danger  { background: var(--danger); }

.btn-neutral {
    border: 1px solid var(--border);
    background: #fff;
    color: var(--text);
}

/*
 * The generate button is pushed to the bottom of the panel, level with
 * the board's button bar, since both cards have the same height.
 */
.panel .btn-wide { margin-top: auto; }

.btn-wide {
    width: 100%;
    padding: 12px;
    font-size: 1rem;
}

/* --------------------------------------------------------------- */
/* Footer                                                           */
/* --------------------------------------------------------------- */

/*
 * White on the gradient, like the header. The links take the colour of
 * the text: the default blue of the browser reads badly on the violet.
 * Line height of 1.2, like the header: the line counts in the height of
 * the page (see the board width).
 */
.page-footer {
    color: #fff;
    text-align: center;
    line-height: 1.2;
}

.page-footer a {
    color: inherit;
}

/* --------------------------------------------------------------- */
/* Small screens                                                    */
/* --------------------------------------------------------------- */

/*
 * Below 1200 px the three cards no longer fit side by side: the how-to
 * moves above, full width. It comes last in the HTML (on the right on a
 * large screen), order brings it back to the top here so that it is still
 * read before the board.
 *
 * Closed, the card still adds 73 px to the height of the page with the
 * default font size, hence a board width of its own, with two bounds,
 * measured as above for font sizes of 16, 20 and 24 px:
 *
 *  - height: the smallest constant that never shows a scrollbar is 248,
 *    268 and 288 px, that is 168 px + 5rem; 10 px of margin are added;
 *  - width: besides the board, the two cards and the margins take 468,
 *    474 and 481 px, that is about 441 px + 1.7rem (the frame of the
 *    coordinates); beyond that, the form drops below the board and the
 *    page can no longer fit. 10 px of margin are added.
 *
 * With the default font size, the page thus fits without a scrollbar from
 * 671 px of height, in any window at least 786 px wide: below that, the
 * button bar under the board (345 px in the measuring browser, depending
 * on the font) already makes the board card too wide for the form to stay
 * beside it. Opening the card is the visitor's choice, and may bring the
 * scrollbar back.
 */
@media (max-width: 1200px) {
    .card-help {
        flex-basis: 100%;
        order: -1;
    }

    #board {
        width: clamp(280px, min(80vh - 178px - 5rem, 100vw - 451px - 1.7rem), 460px);
    }
}

@media (max-width: 720px) {
    body { padding: 12px; }

    #board { width: 100%; }

    .panel { width: 100%; }

    .board-frame { padding-left: 1.3rem; }

    .card-board { width: 100%; }

    /* On a narrow screen, height is no longer the limiting factor. */
    #board { width: 100%; }

    .coords-ranks { width: 1.3rem; }
    .coords-files { height: 1.3rem; }
    .coords       { font-size: 0.8rem; }
}
