/* Page frame. UX-16: the page fills the viewport height; the word list takes the spare height and scrolls inside its area.
   UX-18: page content uses the window width up to 1280px. */
body {
    font-family: system-ui, -apple-system, Arial, sans-serif;
    margin: 0;
    padding: 0.5rem;
    box-sizing: border-box;
    height: 100vh;
    height: 100dvh;
    color: #1d1d1f;
}

main {
    max-width: 1280px;
    height: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

main > h1 {
    margin: 0;
    font-size: 1.1rem;
}

.entry-row {
    display: flex;
    gap: 0.5rem;
}

input {
    flex: 1;
    padding: 0.5rem;
    font: inherit;
}

button {
    padding: 0.5rem 1rem;
    font: inherit;
}

/* Portrait (one column): grid, candidates, settings, word entry, word list, Start, message. */
#board {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
}

#mode-controls {
    display: flex;
    gap: 0.5rem;
}

#grid-label {
    margin: 0;
    font-size: 1.1rem;
}

#controls {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

#settings-form {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.125rem 0.75rem;
    font-size: 0.85rem;
}

#settings-form input[type="number"] {
    flex: none;
    width: 3.5rem;
    padding: 0.25rem;
}

#word-form,
#start-button,
#message {
    flex: none;
}

#message {
    min-height: 1.25rem;
    font-size: 0.9rem;
}

#word-list {
    flex: 1 1 auto;
    min-height: 3rem;
    overflow-y: auto;
    margin: 0;
    padding-left: 1.25rem;
}

/* UX-02: drag handle. Drawn with CSS so the item's text stays equal to the word. */
#word-list li {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.25rem 0;
}

#word-list li.dragging {
    opacity: 0.5;
}

.drag-handle {
    cursor: grab;
    color: #6e6e73;
    user-select: none;
    padding: 0 0.25rem;
}

.drag-handle::before {
    content: "\2630";
}

/* UX-14: a grid is a rectangle of square cells, letters centred. Rows are display: contents, so the cells share the grid's columns. */
.grid {
    display: grid;
    width: 100%;
}

.grid-row {
    display: contents;
}

.grid-cell {
    aspect-ratio: 1 / 1;
    min-width: 0;
    border: 1px solid #1d1d1f;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
}

/* UX-15: crossword black cells (blank cells in crossword mode). */
.grid-cell.black-cell {
    background: #1d1d1f;
}

/* UX-16 / UX-18: the main grid is as large as its area allows, and its height is capped as well as its width.
   --cols and --rows give the grid's shape (defaults: 10 x 10). A grid of cells of width w is w * rows / cols tall.
   No fixed width cap: the grid is the largest square its area allows (UX-18), so the height caps below are the only limits.
   Portrait: the viewport height less the space the other controls need (about 31rem, estimate, not measured).
   Landscape: the viewport height less the heading and controls above it. */
#grid-section.grid {
    --cols: 10;
    --rows: 10;
    width: min(100%, calc((100dvh - 31rem) * var(--cols) / var(--rows)));
    margin: 0 auto;
}

/* UX-13 / UX-17: candidate thumbnails are mini grids of one fixed size; the focused one is outlined.
   Portrait: a row that scrolls sideways (height stays fixed). */
#candidate-list {
    --thumb: 4.5rem;
    list-style: none;
    margin: 0;
    padding: 0.25rem 0;
    display: flex;
    gap: 0.5rem;
    overflow-x: auto;
    flex: none;
}

#candidate-list li {
    flex: none;
}

/* vertical-align: top stops the inline button sitting on the text baseline, which left a gap under each thumbnail. */
#candidate-list button {
    vertical-align: top;
    padding: 2px;
    border: 2px solid transparent;
    background: none;
    cursor: pointer;
}

#candidate-list button[aria-current="true"] {
    border-color: #0071e3;
}

/* Each thumbnail fits a --thumb square box, so every thumbnail is no taller than --thumb. */
#candidate-list .grid {
    width: min(var(--thumb), calc(var(--thumb) * var(--cols, 10) / var(--rows, 10)));
}

#candidate-list .grid-cell {
    font-size: 0.5rem;
}

#candidate-details {
    font-size: 0.85rem;
}

/* UX-07 landscape: the grid on the left with the candidate ribbon beside it; the controls on the right.
   UX-17: the ribbon is a vertical column that scrolls inside its height. */
body.landscape-layout main {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(16rem, 22rem);
    grid-template-rows: auto minmax(0, 1fr);
    column-gap: 2rem;
}

body.landscape-layout main > h1 {
    grid-column: 1 / -1;
}

body.landscape-layout #board {
    align-self: start;
    display: grid;
    grid-template-columns: minmax(0, 1fr) 7rem;
    grid-template-areas:
        "mode mode"
        "label label"
        "grid cands"
        "details details";
    gap: 0.5rem 0.75rem;
    align-items: start;
}

body.landscape-layout #mode-controls { grid-area: mode; }
body.landscape-layout #grid-label { grid-area: label; }
body.landscape-layout #grid-section { grid-area: grid; }
body.landscape-layout #candidate-details { grid-area: details; }

body.landscape-layout #grid-section.grid {
    width: min(100%, calc((100dvh - 15rem) * var(--cols) / var(--rows)));
    margin: 0;
}

body.landscape-layout #candidate-list {
    --thumb: 6rem;
    grid-area: cands;
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
    max-height: calc(100dvh - 15rem);
    padding: 0;
}

body.landscape-layout #controls {
    min-height: 0;
}

/* UX-06: print output shows only the grid. The UI adds final_grid_only to body while a grid is on screen. */
@media print {
    body.final_grid_only {
        height: auto;
        padding: 0;
    }

    body.final_grid_only > *:not(main) {
        display: none;
    }

    body.final_grid_only main,
    body.final_grid_only #board {
        display: block;
        height: auto;
    }

    body.final_grid_only main > *:not(#board) {
        display: none;
    }

    body.final_grid_only #board > *:not(#grid-section) {
        display: none;
    }

    body.final_grid_only #grid-section {
        margin: 0 auto;
        width: fit-content;
        max-width: none;
    }

    body.final_grid_only #grid-section .grid-cell {
        width: min(2.5rem, calc((100vw - 2rem) / 20));
    }
}
