:root {
    color-scheme: light;
    /* a marked-up worksheet: red-pen circles and blue-pen crosses on paper */
    --ground: #f3f0e8;
    --card: #fffdf7;
    --ink: #262f38;
    --ink-soft: #6f7b86;
    --muted-text: #8d9199;
    --grid-line: #e2dccd;
    --wall: #2b343d;
    --maru: #d9573f;
    --peke: #2f5fae;
    --done: #1f9d7a;
    --illegal: #c9342c;
    --illegal-soft: rgba(201, 52, 44, 0.12);
    --given-tint: rgba(38, 47, 56, 0.05);
    --hover: rgba(38, 47, 56, 0.04);
    --control: #ffffff;
    --control-border: #d9d3c6;
    --control-hover: #f6f3ec;
    --card-shadow: 0 1px 2px rgba(38, 47, 56, 0.06), 0 12px 32px rgba(38, 47, 56, 0.08);
    --modal-backdrop: rgba(38, 47, 56, 0.45);
    --glow: rgba(31, 157, 122, 0.35);
    --hint: #d9a22a;
}

[data-theme="dark"] {
    color-scheme: dark;
    /* the same pens on slate */
    --ground: #12161a;
    --card: #1a2026;
    --ink: #e3e8ed;
    --ink-soft: #8c98a4;
    --muted-text: #717e8a;
    --grid-line: #2e3840;
    --wall: #0a0d10;
    --maru: #ff7b5e;
    --peke: #6f9fff;
    --done: #3cc59c;
    --illegal: #ef6358;
    --illegal-soft: rgba(239, 99, 88, 0.16);
    --given-tint: rgba(227, 232, 237, 0.06);
    --hover: rgba(227, 232, 237, 0.05);
    --control: #202830;
    --control-border: #323d47;
    --control-hover: #28323b;
    --card-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 12px 32px rgba(0, 0, 0, 0.35);
    --modal-backdrop: rgba(0, 0, 0, 0.6);
    --glow: rgba(60, 197, 156, 0.4);
    --hint: #f0c04f;
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
}

/* The board owns the centre of the viewport: the rows above and below it
   share the spare height equally, and the header and controls sit against
   its edges within them. */
body {
    margin: 0;
    min-height: 100%;
    background: var(--ground);
    color: var(--ink);
    font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    display: grid;
    grid-template-rows: auto 1fr auto 1fr;
    justify-items: center;
    row-gap: 20px;
    padding: 16px 16px 24px;
    overscroll-behavior: none;
}

/* Masthead: the title sits on the board's axis whatever the toolbar takes,
   so both side columns share the leftover width. */

.masthead {
    justify-self: stretch;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: start;
    column-gap: 12px;
}

.masthead-title {
    position: relative;
    grid-column: 2;
    text-align: center;
}

h1 {
    margin: 0;
    font-size: 30px;
    font-weight: 800;
    letter-spacing: -0.02em;
}

.tagline {
    margin: 4px 0 0;
    font-size: 14px;
    color: var(--muted-text);
}

/* The title is the switcher's button. Its hover pill is a pseudo-element
   drawn past the text, so it can have padding without moving the title. */

.title-button {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 10px;
    margin: 0;
    padding: 0;
    border: 0;
    border-radius: 0;
    background: none;
    color: inherit;
    font: inherit;
    letter-spacing: inherit;
    cursor: pointer;
}

.title-button:hover {
    background: none;
}

.title-button::before {
    content: "";
    position: absolute;
    inset: -6px -16px;
    z-index: -1;
    border-radius: 999px;
    background: var(--hover);
    opacity: 0;
    transition: opacity 120ms ease;
}

.title-button:hover::before,
.title-button[aria-expanded="true"]::before {
    opacity: 1;
}

.chevron {
    width: 10px;
    height: 10px;
    margin-top: -5px;
    border-right: 3px solid var(--muted-text);
    border-bottom: 3px solid var(--muted-text);
    border-radius: 1px;
    transform: rotate(45deg);
    transition: transform 150ms ease, border-color 150ms ease;
}

.title-button:hover .chevron,
.title-button[aria-expanded="true"] .chevron {
    border-color: var(--ink);
}

.title-button[aria-expanded="true"] .chevron {
    margin-top: 5px;
    transform: rotate(225deg);
}

.family-menu {
    position: absolute;
    top: calc(100% + 10px);
    left: 50%;
    transform: translateX(-50%);
    z-index: 5;
    min-width: 170px;
    margin: 0;
    padding: 6px;
    list-style: none;
    text-align: left;
    background: var(--card);
    border: 1px solid var(--control-border);
    border-radius: 14px;
    box-shadow: var(--card-shadow);
}

.family-entry {
    display: block;
    padding: 8px 14px;
    border-radius: 9px;
    font-size: 15px;
    color: var(--ink);
    text-decoration: none;
}

a.family-entry:hover {
    background: var(--hover);
}

.family-entry.current {
    font-weight: 700;
}

.family-entry.current::after {
    content: "✓";
    float: right;
    color: var(--muted-text);
}

/* Legend and caption lead into the board */

.board-lead {
    align-self: end;
    text-align: center;
}

.caption {
    margin: 10px 0 0;
    min-height: 18px;
    font-size: 13px;
    color: var(--muted-text);
    font-variant-numeric: tabular-nums;
}

.caption.done {
    color: var(--done);
    font-weight: 600;
}

.caption.hint {
    color: var(--hint);
}

.legend {
    display: flex;
    justify-content: center;
    gap: 16px;
    list-style: none;
    margin: 0;
    padding: 0;
    font-size: 13px;
    color: var(--ink-soft);
}

.legend li {
    display: flex;
    align-items: center;
    gap: 4px;
    line-height: 1;
}

/* The legend draws the same two glyphs as the board, at a fixed size. */
.legend-mark {
    --cell: 18px;
    --stroke: 2px;
    position: relative;
    width: var(--cell);
    height: var(--cell);
    /* Centres the glyph on the x-height of the lowercase label rather than on the full line box */
    transform: translateY(1px);
}

/* Toolbar and controls */

.toolbar {
    grid-column: 3;
    justify-self: end;
    display: flex;
    gap: 6px;
}

.icon-button {
    width: 34px;
    height: 34px;
    padding: 0;
    border-radius: 50%;
    border: 1px solid var(--control-border);
    background: var(--control);
    color: var(--ink-soft);
    font: inherit;
    font-size: 16px;
    cursor: pointer;
}

.icon-button:hover {
    background: var(--control-hover);
    color: var(--ink);
}

.controls {
    align-self: start;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

.generator {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    justify-content: center;
    gap: 12px;
    margin-bottom: 14px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 11px;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: var(--muted-text);
}

.field input,
.field select {
    letter-spacing: normal;
    text-transform: none;
    height: 36px;
}

.stats {
    margin: 0;
    min-height: 16px;
    text-align: center;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    color: var(--muted-text);
}

button.busy {
    animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
    50% {
        opacity: 0.35;
    }
}

select, button, input {
    font: inherit;
    font-size: 14px;
    color: var(--ink);
    background: var(--control);
    border: 1px solid var(--control-border);
    border-radius: 999px;
    padding: 7px 14px;
    cursor: pointer;
}

button:hover, select:hover {
    background: var(--control-hover);
}

/* Hint and Solution are toggles: pressed inverts the pill */
button[aria-pressed="true"],
button[aria-pressed="true"]:hover {
    color: var(--card);
    background: var(--ink);
    border-color: var(--ink);
}

input[type="number"] {
    width: 72px;
    padding-right: 6px;
}

/* Board */

.board {
    --cell: clamp(28px, calc((100vw - 64px) / var(--size)), 50px);
    --stroke: clamp(2.5px, calc(var(--cell) * 0.085), 4.5px);
    display: grid;
    grid-template-columns: repeat(var(--size), var(--cell));
    grid-template-rows: repeat(var(--size), var(--cell));
    --frame-radius: 18px;
    padding: var(--frame-radius);
    background: var(--card);
    border-radius: var(--frame-radius);
    box-shadow: var(--card-shadow);
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.board.solved {
    animation: glow 1.4s ease;
}

@keyframes glow {
    50% {
        box-shadow: var(--card-shadow), 0 0 0 4px var(--glow);
    }
}

.cell {
    width: var(--cell);
    height: var(--cell);
    position: relative;
    border-right: 1px solid var(--grid-line);
    border-bottom: 1px solid var(--grid-line);
    transition: background 150ms ease;
}

.cell[data-column="0"] {
    border-left: 1px solid var(--grid-line);
}

.cell[data-row="0"] {
    border-top: 1px solid var(--grid-line);
}

.cell.open {
    cursor: pointer;
}

.cell.given {
    background: var(--given-tint);
    cursor: default;
}

.cell.wall {
    background: var(--wall);
    border-color: var(--wall);
}

.board.placeholder .cell {
    opacity: 0.6;
}

@media (hover: hover) {
    .board:not(.solved):not(.revealed) .cell.open:not(.given):hover {
        background: var(--hover);
    }
}

/* Solved or revealed, the board no longer takes input */

.board.solved .cell.open,
.board.revealed .cell.open {
    cursor: default;
}

/* Revealed answer: the marks the player did not place are faded */

.board.revealed .cell.open:not(.given) .glyph {
    opacity: 0.45;
}

/* Glyphs: ::before is the circle's ring or the cross's first bar, ::after
   the cross's second bar. The legend reuses them at a fixed size. */

.glyph {
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    transition: transform 0.08s ease;
}

.cell.press .glyph {
    transform: scale(0.86);
}

.cell.circle .glyph::before,
.legend-mark.circle::before {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    width: calc(var(--cell) * 0.48);
    height: calc(var(--cell) * 0.48);
    transform: translate(-50%, -50%);
    border: var(--stroke) solid var(--maru);
    border-radius: 50%;
    transition: border-color 150ms ease;
}

.cell.cross .glyph::before,
.cell.cross .glyph::after,
.legend-mark.cross::before,
.legend-mark.cross::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    width: calc(var(--cell) * 0.66);
    height: var(--stroke);
    margin-left: calc(var(--cell) * -0.33);
    margin-top: calc(var(--stroke) / -2);
    background: var(--peke);
    border-radius: 999px;
    transform: rotate(45deg);
    transition: background 150ms ease;
}

.cell.cross .glyph::after,
.legend-mark.cross::after {
    transform: rotate(-45deg);
}

/* A hint rings the cell the solver points at */

.cell.hint {
    box-shadow: inset 0 0 0 3px var(--hint);
}

/* Three alike: the cells and their marks turn red */

.cell.illegal {
    background: var(--illegal-soft);
}

.cell.illegal.circle .glyph::before {
    border-color: var(--illegal);
}

.cell.illegal.cross .glyph::before,
.cell.illegal.cross .glyph::after {
    background: var(--illegal);
}

/* Solved: both pens settle into one finished colour and the grid fades */

.board.solved .cell.circle .glyph::before {
    border-color: var(--done);
}

.board.solved .cell.cross .glyph::before,
.board.solved .cell.cross .glyph::after {
    background: var(--done);
}

.board.solved .cell.given {
    background: transparent;
}

/* Modals */

.modal {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 24px;
    background: var(--modal-backdrop);
    z-index: 10;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.2s ease, visibility 0s linear 0.2s;
}

.modal.open {
    opacity: 1;
    visibility: visible;
    transition: opacity 0.2s ease;
}

.modal-card {
    max-width: 380px;
    width: 100%;
    max-height: 100%;
    overflow-y: auto;
    padding: 24px 26px 22px;
    border-radius: 18px;
    background: var(--card);
    box-shadow: var(--card-shadow);
    font-size: 14px;
    line-height: 1.5;
    transform: scale(0.97);
    transition: transform 0.2s ease;
}

.modal.open .modal-card {
    transform: scale(1);
}

@media (prefers-reduced-motion: reduce) {
    .modal,
    .modal-card,
    .chevron,
    .board.solved {
        transition: none;
        animation: none;
    }
}

.modal-card h2 {
    margin: 0 0 10px;
    font-size: 20px;
}

.modal-card p {
    margin: 0 0 10px;
}

.modal-card ul {
    margin: 0 0 12px;
    padding-left: 20px;
}

.modal-subhead {
    font-weight: 600;
    margin-top: 14px;
}

.modal-close {
    margin-top: 6px;
    width: 100%;
}

/* Phones: the title moves to the left edge, iOS large-title style, and
   takes whatever the toolbar leaves. The widest phones are 430px, so the
   breakpoint sits well clear of them. */
@media (max-width: 600px) {
    body {
        padding-bottom: 60px;
    }

    .masthead {
        grid-template-columns: 1fr auto;
    }

    .masthead-title {
        grid-column: 1;
        justify-self: start;
        text-align: left;
    }

    .toolbar {
        grid-column: 2;
    }

    .family-menu {
        left: 0;
        transform: none;
    }

    .legend {
        gap: 12px;
    }
}
