.play-workspace {
    position: relative;
    display: none;
    flex: 1 1 0;
    min-height: 0;
}

.play-workspace:has(#pixelcanvas:not([hidden])) {
    background: var(--puzzle-paper);
    display: flex;
    flex-direction: column;
}

/* スクロール領域の外に余白を置き、固定ヒントと入力の座標系を揃える。 */
#pixelcanvas {
    margin: 0.75rem 1rem;
    position: relative;
    min-width: 0;
    flex: 1 0 0;
    min-height: 0;
    overflow: auto;
}

header:has(#play-panel:not([hidden])),
#fix-phone-under-menu,
#pixelcanvas {
    background: var(--puzzle-paper);
    color: var(--puzzle-ink);
}

header:has(#play-panel:not([hidden])) {
    border-bottom: 1px solid var(--puzzle-rule);
}

/* プレイ面の操作欄ではスクロールを許し、ブラウザの拡大だけを抑える。 */
header:has(#play-panel:not([hidden])),
.play-workspace,
#pixelcanvas,
#board-side-controls,
#color-palette,
#pause-message {
    touch-action: pan-x pan-y;
}

#canvas {
    /* 指の本数による塗り・移動の使い分けは盤面側で処理する。 */
    touch-action: none;
    display: block;
}

.fixed-clues {
    position: sticky;
    top: 0;
    left: 0;
    width: 0;
    height: 0;
    z-index: 1;
    pointer-events: none;
}

#fixed-clues {
    position: absolute;
    top: 0;
    left: 0;
}

#hint-line-marker {
    position: absolute;
    box-sizing: border-box;
    border: 2px solid #ffca28;
    box-shadow: inset 0 0 0 1px #24374a;
    pointer-events: none;
}

#hint-line-marker.is-logical {
    border-color: #6c29a6;
    box-shadow: inset 0 0 0 1px white, 0 0 0 1px white;
}

.hint-color {
    display: inline-block;
    border: 1px solid currentColor;
    border-radius: 0.25rem;
    padding: 0 0.3em;
    font-weight: 700;
}

#hint-message,
#conflict-message,
#board-result {
    margin: 0.4rem 0 0;
    font-size: 0.875rem;
}

#hint-message:empty,
#conflict-message:empty,
#board-result:empty,
.quality-result:empty {
    display: none;
}

#play-panel.is-viewing-tile .play-only,
body:has(#play-panel.is-viewing-tile) #board-side-controls .play-only {
    display: none;
}

#play-panel.is-viewing-tile #board-result {
    margin-block: 0.65rem;
    font-size: 1rem;
}

#board-result.is-complete {
    padding: 0.375rem 0.65rem;
    border: 1px solid var(--ui-accent);
    background: var(--ui-accent-tint);
    color: var(--puzzle-accent-active);
    font-weight: 700;
}

#board-clear-result,
#mosaic-clear-result {
    margin-block-start: 0.65rem;
}

#canvas.is-painting {
    cursor: crosshair;
}

.play-instructions {
    margin: 0.5rem 0;
}

.play-clock,
.play-clock-controls {
    display: flex;
    gap: 0.5rem;
}

.play-clock {
    align-items: flex-start;
}

.play-clock-controls {
    flex: 1 1 auto;
    min-inline-size: 0;
    flex-wrap: wrap;
    align-items: center;
}

/* 時計と一時停止を一つの操作欄に収める。 */
.play-timer {
    display: inline-flex;
    align-items: stretch;
    border-radius: 0.35rem;
    background: var(--puzzle-accent-active);
    color: var(--puzzle-paper);
}

.play-timer-readout {
    display: grid;
    align-content: center;
    justify-items: center;
    gap: 0.2rem;
    min-height: 44px;
    padding-inline: 0.5rem;
    line-height: 1;
}

.play-timer-caption {
    font-size: 0.6875rem;
}

.play-timer-value {
    text-align: center;
    font-family: ui-monospace, "Menlo", monospace;
    font-size: 1.125rem;
    font-weight: 600;
    white-space: nowrap;
}

#play-time {
    min-inline-size: 8ch;
}

.play-clock #pause-play {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.35rem;
    min-inline-size: 6.25rem;
    border-color: var(--puzzle-accent-active);
    border-radius: 0 0.35rem 0.35rem 0;
    background: var(--puzzle-paper);
    color: var(--puzzle-accent-active);
}

.play-clock #pause-play:hover:not(:disabled) {
    background: var(--ui-accent-tint);
}

.play-timer:has(#pause-play[data-paused]) {
    background: var(--ui-accent-tint);
    color: var(--puzzle-accent-active);
    box-shadow: inset 0 0 0 1px var(--puzzle-accent-active);
}

.play-clock #pause-play[data-paused] {
    background: var(--puzzle-accent);
    color: #fff;
}

.play-clock #pause-play[data-paused]:hover {
    background: var(--puzzle-accent-active);
}

.play-clock #pause-play:disabled {
    background: var(--ui-subtle);
    color: #676461;
}

.pause-icon {
    display: inline-grid;
    place-items: center;
    inline-size: 0.85rem;
}

.pause-icon::before {
    content: "";
    inline-size: 0.25rem;
    block-size: 0.7rem;
    border-inline: 2px solid currentColor;
}

#pause-play[data-paused] .pause-icon::before {
    inline-size: 0;
    block-size: 0;
    border: 0.33rem solid transparent;
    border-inline-start: 0.52rem solid currentColor;
    border-inline-end: 0;
}

@media (max-width: 360px) {
    .play-clock,
    .play-clock-controls {
        column-gap: 0.375rem;
    }

    .play-timer-readout {
        padding-inline: 0.375rem;
    }

    #play-time {
        min-inline-size: 5ch;
        font-size: 1rem;
    }

    .play-clock #pause-play {
        min-inline-size: 5.5rem;
        padding-inline: 0.4rem;
    }
}

#pause-message {
    box-sizing: border-box;
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    overflow: auto;
    padding: 1.5rem 1rem;
    border-top: 1px solid var(--puzzle-rule);
    background: var(--puzzle-paper);
    color: var(--puzzle-ink);
    text-align: center;
}

#pause-message:not([hidden]) {
    display: flex;
}

.pause-copy {
    max-width: 30rem;
    margin: auto;
}

#pause-message h2 {
    margin: 0 0 0.75rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--puzzle-rule);
    font-size: 1.5rem;
}

#pause-message p {
    margin: 0;
    line-height: 1.65;
}

.board-tools,
.toolbar-group:not([hidden]),
.board-menu:not([hidden]) {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.375rem;
}

#board-side-controls {
    flex: 0 0 auto;
    min-width: 0;
}

#board-view-tools {
    position: relative;
    padding: 0.25rem max(0.5rem, env(safe-area-inset-right)) 0.375rem max(0.5rem, env(safe-area-inset-left));
}

.board-tools {
    gap: 0.25rem;
}

.board-tools .toolbar-group:not([hidden]) {
    gap: 0.25rem;
}

#board-view-tools .board-tools button {
    padding-inline: 0.4rem;
}

.board-zoom button {
    min-width: 44px;
}

.board-minimap {
    position: absolute;
    z-index: 1;
    inset-block-start: 0.75rem;
    inset-inline-start: max(1rem, env(safe-area-inset-left));
    padding: 0.25rem;
    border: 1px solid var(--puzzle-major-rule);
    border-radius: 0.25rem;
    background: var(--puzzle-paper);
}

.board-minimap-image {
    position: relative;
    border: 1px solid var(--puzzle-rule);
}

#board-minimap-canvas {
    display: block;
    cursor: pointer;
    image-rendering: pixelated;
}

.board-minimap:focus-visible {
    outline: 2px solid var(--puzzle-accent);
    outline-offset: 2px;
}

#board-minimap-frame {
    position: absolute;
    pointer-events: none;
    box-sizing: border-box;
    outline: 1px solid #12293e;
    outline-offset: -1px;
    box-shadow: inset 0 0 0 2px #fff;
}

#toggle-board-minimap[aria-expanded="true"] {
    background: var(--ui-hover);
    border-color: var(--ui-rule-strong);
}

@media (max-width: 30rem) {
    #board-view-tools #toggle-board-minimap,
    #board-view-tools #hint-tools-toggle {
        padding-inline: 0.25rem;
    }
}

.puzzle-share input {
    box-sizing: border-box;
    width: 100%;
    font: inherit;
}

.board-menu:not([hidden]) {
    position: absolute;
    z-index: 2;
    inset-inline: 0;
    bottom: calc(100% + 0.25rem);
    max-inline-size: 24rem;
    padding: 0.5rem;
    border: 1px solid var(--puzzle-major-rule);
    border-radius: 0.5rem;
    background: var(--ui-subtle);
    max-block-size: min(14rem, 50dvh);
    overflow: auto;
}

#hint-tools:not([hidden]) {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.625rem;
    inset-inline: max(0.5rem, env(safe-area-inset-left)) max(0.5rem, env(safe-area-inset-right));
}

.play-clock button,
.paint-tools button,
.board-tools button,
.board-menu button,
#open-settings,
.dialog-heading button,
.action-link {
    box-sizing: border-box;
    min-height: 44px;
    padding: 0.45rem 0.65rem;
    border: 1px solid var(--ui-rule);
    border-radius: var(--ui-corner);
    background: #fff;
    color: var(--ui-ink);
    font: inherit;
    font-size: 0.875rem;
    cursor: pointer;
}

.action-link:not([hidden]) {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-decoration: none;
}

.play-clock button:hover:not(:disabled),
.paint-tools button:hover:not(:disabled),
.board-tools button:hover:not(:disabled),
.board-menu button:hover:not(:disabled),
#open-settings:hover,
.dialog-heading button:hover,
.action-link:hover:not(:disabled),
.board-menu-button[aria-expanded="true"] {
    background: var(--ui-hover);
    border-color: var(--ui-rule-strong);
}

.action-link.action-primary:not(:disabled) {
    border-color: var(--ui-accent);
    background: var(--ui-accent);
    color: #fff;
}

.action-link.action-primary:hover:not(:disabled),
.action-link.action-primary:active:not(:disabled) {
    background: var(--ui-accent-active);
}

#palette-move[aria-pressed="true"],
#palette-blank[aria-pressed="true"],
#palette-slash[aria-pressed="true"],
#pick-painted-color[aria-pressed="true"] {
    border-color: var(--puzzle-accent);
    background: var(--puzzle-accent);
    color: #fff;
}

.display-toggle-box {
    display: grid;
    place-items: center;
    box-sizing: border-box;
    flex: none;
    width: 0.875rem;
    height: 0.875rem;
    border: 1px solid currentColor;
}

.display-toggle:not([hidden]) {
    justify-content: flex-start;
    gap: 0.5rem;
}

.display-toggle[aria-pressed="true"] .display-toggle-box::after {
    content: "";
    width: 0.375rem;
    height: 0.1875rem;
    border-inline-start: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: translateY(-1px) rotate(-45deg);
}

.play-clock button:focus-visible,
.paint-tools button:focus-visible,
.board-tools button:focus-visible,
.board-menu button:focus-visible,
#open-settings:focus-visible,
.dialog-heading button:focus-visible,
.action-link:focus-visible {
    outline: 2px solid #175caa;
    outline-offset: 2px;
}

.play-clock button:disabled,
.paint-tools button:disabled,
.board-tools button:disabled,
.board-menu button:disabled,
.action-link:disabled {
    color: #676461;
    border-color: #d7d4d0;
    cursor: default;
}

.site-heading {
    max-width: 59rem;
    margin-inline: auto;
}

.site-heading,
.dialog-heading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
}

#open-settings,
#open-play-menu,
.dialog-heading button {
    flex: none;
}

.mosaic-tile-navigation:has(#return-mosaic:not([hidden])) {
    display: flex;
    flex-wrap: wrap;
    max-inline-size: 100%;
    align-items: center;
    gap: 0.5rem;
}

.mosaic-tile-navigation:not(:has(#return-mosaic:not([hidden]))) {
    display: none;
}

.play-clock #open-play-menu {
    margin-inline-start: auto;
}

@media (width <= 360px) {
    .mosaic-tile-navigation {
        gap: 0.375rem;
    }

    #return-mosaic {
        padding-inline: 0.4rem;
    }

    #mosaic-tile-progress {
        font-size: 0.875rem;
    }
}

@media (width >= 48rem) {
    /* Reserve the previous right-side space so the menu stays in place. */
    .play-clock:has(#return-mosaic:not([hidden]))::after {
        content: attr(data-tile-label);
        padding-inline-start: calc(6 * 0.875rem + 1.3rem + 2px + 0.5rem);
        visibility: hidden;
    }
}

header:has(#play-panel:not([hidden])) .site-heading h1 {
    /* 画面上の高さは使わず、見出しは読み上げや見出し一覧に残す。 */
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
}

body:has(#play-panel:not([hidden]), #mosaic-panel:not([hidden])) #open-settings,
body:not(:has(#play-panel:not([hidden]), #mosaic-panel:not([hidden]))) #open-play-menu {
    display: none;
}

#hint-tools button {
    max-inline-size: 100%;
}

#answer-tool {
    padding-block-start: 0.625rem;
    border-block-start: 1px solid var(--puzzle-rule);
}

#answer-tool button {
    inline-size: 100%;
    text-align: start;
}

.penalty {
    display: inline-block;
    box-sizing: border-box;
    max-inline-size: 100%;
    padding: 0.08rem 0.3rem;
    border-radius: 0.25rem;
    background: #f2e8d6;
    color: #593b13;
    font-size: 0.75rem;
    font-weight: 650;
}

.penalty > span {
    white-space: nowrap;
}

#zoom-out,
#zoom-in {
    min-width: 44px;
    font-size: 1.125rem;
}

#fix-phone-under-menu {
    container: paint-toolbar / inline-size;
    flex: 0 0 auto;
    min-width: 0;
    border-top: 1px solid var(--puzzle-rule);
    padding: 0.375rem max(0.5rem, env(safe-area-inset-right))
        max(0.375rem, env(safe-area-inset-bottom)) max(0.5rem, env(safe-area-inset-left));
}

#fix-phone-under-menu:not([hidden]) {
    display: flex;
    flex-direction: column;
    gap: 0.375rem;
}

#palette-panel {
    flex: 1 1 auto;
    min-width: 0;
    display: flex;
    flex-direction: column;
}

.palette-label,
#palette-scroll-note {
    margin: 0 0 0.125rem;
    font-size: 0.75rem;
}

.palette-label {
    font-weight: 600;
}

#palette-scroll-note {
    display: none;
}

#palette-strip {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    align-items: center;
}

#palette-strip[data-scrollable] {
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 0.25rem;
}

#color-palette {
    display: flex;
    flex-wrap: nowrap;
    gap: 0.375rem;
    padding: 0.25rem;
    overflow-x: auto;
    overscroll-behavior-x: contain;
}

.palette-step:not([hidden]) {
    min-width: 44px;
    font-size: 1.3rem;
    padding-inline: 0.25rem;
    line-height: 1;
}

.palette-step[aria-disabled="true"] {
    opacity: 0.5;
    cursor: default;
}

#fix-phone-under-menu .paint-tools {
    gap: 0.25rem;
}

.history-controls {
    display: flex;
    gap: inherit;
    white-space: nowrap;
}

.history-icon {
    display: none;
}

#fix-phone-under-menu .paint-tools button {
    min-width: 44px;
    padding-inline: 0.3rem;
    font-size: 0.8125rem;
}

@media (min-width: 44rem) and (orientation: landscape) {
    .play-workspace:has(#pixelcanvas:not([hidden])) {
        display: grid;
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-rows: minmax(0, 1fr) auto;
    }

    #board-side-controls:not([hidden]) {
        display: contents;
    }

    #board-view-tools {
        grid-column: 2;
        grid-row: 1;
        width: 11.75rem;
        padding: 0.75rem 0.5rem;
        border-inline-start: 1px solid var(--puzzle-rule);
    }

    #fix-phone-under-menu:not([hidden]) {
        grid-column: 1 / -1;
        grid-row: 2;
        flex-direction: row;
        align-items: center;
    }

    #hint-tools:not([hidden]) {
        inset: 0 calc(100% + 0.25rem) auto auto;
        width: min(22rem, 50vw);
    }

    #fix-phone-under-menu .paint-tools {
        flex: 0 0 auto;
    }
}

@media (min-width: 64rem) and (min-height: 37rem) and (hover: hover) and (pointer: fine) {
    .play-workspace:has(#pixelcanvas:not([hidden])) {
        display: flex;
        flex-direction: row;
    }

    #board-side-controls:not([hidden]) {
        display: flex;
        flex: 0 0 16rem;
        flex-direction: column;
        min-height: 0;
        border-inline-start: 1px solid var(--puzzle-rule);
    }

    #board-view-tools {
        flex: none;
        width: auto;
        overflow: visible;
        border-inline-start: 0;
        padding: 0.75rem 0.75rem 0.375rem;
    }

    #fix-phone-under-menu:not([hidden]) {
        flex: 1 1 auto;
        min-height: 0;
        flex-direction: column;
        align-items: stretch;
        border-top: 0;
        padding: 0.375rem 0.75rem 0.75rem;
    }

    #palette-panel {
        flex: 0 1 auto;
        min-height: 0;
    }

    #palette-strip {
        display: flex;
        align-items: stretch;
        min-height: 0;
    }

    #color-palette {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(auto-fill, 44px);
        align-content: start;
        overflow: auto;
        overscroll-behavior: contain;
    }

    #palette-scroll-note:not([hidden]) {
        display: block;
        margin-top: 0.25rem;
    }
}

.palette-color {
    display: grid;
    place-items: center;
    box-sizing: border-box;
    flex: 0 0 44px;
    width: 44px;
    min-width: 44px;
    height: 44px;
    padding: 4px;
    border: 2px solid #777;
    border-radius: 6px;
    background: white;
    cursor: pointer;
}

.palette-color[aria-pressed="true"] {
    border-color: #111;
    box-shadow: 0 0 0 2px var(--puzzle-accent);
}

.palette-swatch {
    display: grid;
    place-items: center;
    font-weight: 700;
    width: 100%;
    height: 100%;
    border: 1px solid #aaa;
    border-radius: 2px;
    box-sizing: border-box;
}

.palette-color:focus-visible {
    outline: 2px solid #175caa;
    outline-offset: 2px;
}

#keyboard-cell-marker {
    position: absolute;
    box-sizing: border-box;
    border: 2px solid #0072ce;
    box-shadow: inset 0 0 0 1px white;
    pointer-events: none;
}

#canvas:not(:focus-visible) + #keyboard-cell-marker {
    visibility: hidden;
}

#color-palette:not(.show-symbols) .palette-symbol {
    display: none;
}

#progress-warning {
    margin: 0.25rem 1rem;
    font-size: 0.875rem;
}

#progress-warning:empty {
    display: none;
}

#canvas.is-moving { cursor: grab; }
#canvas.is-moving:active { cursor: grabbing; }
#palette-move { display: inline-grid; place-items: center; }
@media (max-width: 63.99rem) {
    #fix-phone-under-menu .history-controls { margin-inline-start: auto; }
}

#paint-feedback-marker {
    position: absolute;
    box-sizing: border-box;
    border: 2px solid white;
    box-shadow: inset 0 0 0 1px var(--ui-ink), 0 0 0 1px var(--ui-ink);
    pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
    #paint-feedback-marker { display: none; }
}

/* Keep the labels in the desktop side panel, where a second row is intentional. */
@media (width < 64rem) or (height < 37rem) or (hover: none) or (pointer: coarse) {
    /* Three 44px targets, three text buttons, and five gaps. */
    @container paint-toolbar (width < calc(138px + 12.8rem)) {
        .history-label { display: none; }
        .history-icon { display: block; }
        .history-controls button { display: grid; place-items: center; }
    }
}
