/* Matassa: the BOARD and nothing else.
 *
 * Header, message channel, action bar, panel, statistics, archive, modal and the
 * mobile containers all come from games-common.css. What is here is the 6x8 grid
 * of letters, the thread that joins the cells of a word while it is being traced,
 * and the list of words already found.
 *
 * The grid is sized off --tools-board-max, the custom property the frame writes
 * on .tools-game-board with the height actually left between the board and the
 * dock. A 6x8 of round cells is 1.33 times its own width, so without that cap
 * the last two rows end up UNDER the docked action bar: not merely ugly, because
 * a cell under the dock cannot be touched at all, and the word that crosses it
 * cannot be traced.
 */

.tools-game-zone.is-matassa {
    --m-cell: clamp(34px, 11vw, 58px);
    --m-gap: clamp(3px, 0.9vw, 7px);
}

/* The frame centres the board contents in a ROW, which is right for a game that
 * puts one thing on the table. This one puts three, stacked: the theme line, the
 * grid, and the words found so far. Without this the theme ends up beside the
 * grid, vertically centred, reading as a caption of the left margin. */
.tools-game-zone.is-matassa .tools-game-board {
    flex-direction: column;
    align-items: center;
}

/* No override of the channel height here. The frame already reserves 24px for
 * it, and the 1.35em that used to sit in this block was SMALLER than that: the
 * channel grew by a few pixels the moment it had something to say, and the whole
 * board stepped down while the player was aiming at it. */

/* Sideways the frame drops the reserved height to nothing, to win back vertical
 * room for boards that can afford it. A 6x8 cannot: the twenty pixels the
 * channel takes when it finally says something are exactly the bottom row of the
 * grid, and a row outside the viewport cannot be traced. The space is reserved
 * again here, and the message is kept to one line so it cannot claim more. */
.tools-game.is-landscape .tools-game-zone.is-matassa .tools-game-msg {
    min-height: 24px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* ===== Theme line ===== */

.tools-matassa-theme {
    margin: 0 0 var(--s-3, 12px);
    text-align: center;
    font-size: 15px;
    font-weight: 700;
    color: var(--c-ink);
    letter-spacing: 0.01em;
}

.tools-matassa-theme::before {
    content: attr(data-label) " ";
    font-weight: 600;
    color: var(--c-ink-mute);
    letter-spacing: 0.04em;
    text-transform: uppercase;
    font-size: 11.5px;
    margin-right: 7px;
}

/* ===== The grid ===== */

.tools-matassa-grid {
    display: grid;
    grid-template-columns: repeat(6, var(--m-cell));
    gap: var(--m-gap);
    justify-content: center;
    margin: 0 auto;
    position: relative;
    touch-action: none; /* a drag across the letters must not scroll the page */
}

.tools-matassa-cell {
    width: var(--m-cell);
    height: var(--m-cell);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    border: 0;
    padding: 0;
    background: #eef2f7;
    color: var(--c-ink);
    font-family: var(--font-sans);
    font-size: calc(var(--m-cell) * 0.46);
    font-weight: 700;
    line-height: 1;
    text-transform: uppercase;
    cursor: pointer;
    transition: transform 120ms ease, background-color 140ms ease, color 140ms ease;
    -webkit-user-select: none;
    user-select: none;
}

.tools-matassa-cell:focus-visible {
    outline: 2px solid var(--c-blue);
    outline-offset: 2px;
}

/* Being traced right now. */
.tools-matassa-cell.is-active {
    background: var(--c-ink);
    color: #fff;
    transform: scale(1.04);
}

/* Part of a theme word already found. */
.tools-matassa-cell.is-theme {
    background: var(--game-correct);
    color: #fff;
}

/* Part of the word that spans the board. */
.tools-matassa-cell.is-span {
    background: var(--game-span);
    color: #fff;
}

/* Lit by a hint: the letters of a word still to find, order not revealed. */
.tools-matassa-cell.is-hinted {
    background: #fff;
    color: var(--c-ink);
    box-shadow: inset 0 0 0 2px var(--game-present);
    animation: tools-matassa-pulse 1.6s ease-in-out infinite;
}

/* Where the thread starts, and where it ends. Ten scattered circles are a second
 * puzzle on top of the first: knowing the two ends turns the hint back into a
 * hint. Which of these show is a preference, because how much help is the right
 * amount of help is not something the game gets to decide for everyone. */
.tools-matassa-cell.is-hint-start,
.tools-matassa-cell.is-hint-end {
    box-shadow: inset 0 0 0 3px var(--game-span);
    animation: none;
}

.tools-matassa-cell.is-hint-start {
    background: color-mix(in srgb, var(--game-span) 16%, #fff);
}

.tools-matassa-cell.is-hint-end {
    background: color-mix(in srgb, var(--game-span) 8%, #fff);
    box-shadow: inset 0 0 0 3px var(--game-span), inset 0 0 0 6px #fff;
}

.tools-matassa-cell.is-shake {
    animation: tools-matassa-shake 420ms ease;
}

@keyframes tools-matassa-pulse {
    0%, 100% { box-shadow: inset 0 0 0 2px var(--game-present); }
    50%      { box-shadow: inset 0 0 0 4px var(--game-present); }
}

@keyframes tools-matassa-shake {
    0%, 100% { transform: translateX(0); }
    20%      { transform: translateX(-4px); }
    40%      { transform: translateX(4px); }
    60%      { transform: translateX(-3px); }
    80%      { transform: translateX(3px); }
}

@media (prefers-reduced-motion: reduce) {
    .tools-matassa-cell,
    .tools-matassa-cell.is-hinted,
    .tools-matassa-cell.is-shake {
        animation: none;
        transition: none;
    }
}

/* The thread: an SVG under the letters that joins the cells of the word being
 * traced. Drawn under the grid rather than between cells, because a line between
 * two circles has to cross the gap and the gap belongs to no cell. */
.tools-matassa-thread {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 0;
}

.tools-matassa-grid > .tools-matassa-cell {
    position: relative;
    z-index: 1;
}

.tools-matassa-thread line {
    stroke: var(--c-ink);
    stroke-width: 7;
    stroke-linecap: round;
    opacity: 0.32;
}

/* ===== Words found ===== */

.tools-matassa-found {
    list-style: none;
    margin: var(--s-4, 16px) 0 0;
    padding: 0;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    justify-content: center;
}

.tools-matassa-found li {
    font-size: 13px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    padding: 4px 10px;
    border-radius: 999px;
    background: color-mix(in srgb, var(--game-correct) 14%, #fff);
    color: color-mix(in srgb, var(--game-correct) 72%, #1f2937);
}

.tools-matassa-found li.is-span {
    background: color-mix(in srgb, var(--game-span) 16%, #fff);
    color: color-mix(in srgb, var(--game-span) 70%, #1f2937);
}

/* ===== Phone =====
 * The variable lives on .tools-game-board, so the cell size is computed on the
 * grid itself: custom properties inherit downwards, and setting it on the zone
 * (an ancestor of the board) would read a value that is not there yet.
 *
 * The 96px taken off the budget is the theme line above the grid plus the strip
 * of found words below it: both sit inside the same board box, and the strip
 * grows as the round goes on.
 */

.tools-game.is-phone .tools-game-zone.is-matassa .tools-matassa-grid {
    --m-cell: min(13.2vw, calc((var(--tools-board-max, 60vh) - 7 * var(--m-gap) - 96px) / 8));
}

.tools-game.is-phone .tools-game-zone.is-matassa .tools-matassa-theme {
    font-size: 13.5px;
    margin-bottom: 6px;
}

/* One scrollable line instead of a block that pushes the grid down a row at a
 * time: on a phone the board must not move while the player is tracing on it. */
.tools-game.is-phone .tools-game-zone.is-matassa .tools-matassa-found {
    margin-top: 8px;
    max-height: 30px;
    flex-wrap: nowrap;
    overflow-x: auto;
    justify-content: flex-start;
    scrollbar-width: none;
}

/* Landscape on a phone is 390px tall: eight rows of legible cells do not fit,
 * and shrinking them to fit makes the letters unreadable. The grid is TRANSPOSED
 * instead -- eight columns by six rows, filled column by column -- which needs
 * two rows less of height.
 *
 * Transposition is an isometry: two cells adjacent in 6x8 are adjacent in 8x6,
 * diagonals included, so every word is traced exactly as before and the game
 * logic does not know this happened. */
.tools-game.is-landscape .tools-game-zone.is-matassa .tools-matassa-grid {
    /* Six rows have to fit between the header and the bottom of the small
     * viewport, because a word whose path leaves the screen cannot be traced at
     * all: a drag is one gesture and cannot scroll halfway through. 132px is the
     * chrome above the board in this layout, measured, not guessed. */
    --m-gap: 3px;
    --m-cell: min(40px, calc((100svh - 152px) / 6));
    grid-template-columns: repeat(8, var(--m-cell));
    grid-template-rows: repeat(6, var(--m-cell));
    grid-auto-flow: column;
}
