:root {
    /* Base settings */
    --transition: 0.3s;
    --radius: 0.3rem;

    /* Color system */
    --color-white: hsl(0, 0%, 100%);
    --color-black: hsl(0, 0%, 20%);
    --color-gray-light: hsl(0, 0%, 96.08%);
    --color-gray: hsl(0, 0%, 80%);
    --color-gray-dark: hsl(0, 0%, 60%);
    --color-blue: hsl(210, 100%, 85.1%);
    --color-blue-light: hsl(211.2, 100%, 95.1%);
    --color-red: hsl(3, 100%, 69%);

    /* Cell properties */
    --cell-size: min(3rem, 9vw);
    --cell-font-size: calc(var(--cell-size) * 0.43);
    --cell-note-font-size: calc(var(--cell-size) * 0.22);
    --cell-bg: var(--color-white);
    --cell-bg-prefilled: hsl(0, 0%, 97%);
    --cell-bg-selected: var(--color-blue);
    --cell-bg-highlight: var(--color-blue-light);
    --cell-bg-incorrect: var(--color-red);
    --cell-border: 1px solid var(--color-gray-dark);
    --cell-border-thick: 0.1rem solid var(--color-black);

    /* Button properties */
    --button-bg: var(--color-white);
    --button-border: 1px solid var(--color-gray);
    --keyboard-btn-size: min(3rem, 10vw);
    --keyboard-font-size: calc(var(--keyboard-btn-size) * 0.4);
}

/* Responsive adjustments */
@media (max-width: 600px) {
    :root {
        --radius: 0.25rem;
    }
}

@media (max-width: 400px) {
    :root {
        --cell-border-thick: 0.08rem solid var(--color-black);
        --cell-border: 0.05rem solid var(--color-gray-dark);
    }
}

main {
    background-color: var(--color-gray-light);
    font-family: Arial, sans-serif;
    width: 100%;
    min-height: 100svh;
    padding: 1rem;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: min(1rem, 2vh);
    overflow-x: hidden;
}

/* Button styles */
button {
    padding: var(--radius);
    border: var(--button-border);
    border-radius: var(--radius);
    background-color: var(--button-bg);
    font-size: min(1rem, 4vw);
    cursor: pointer;
    transition:
        background-color var(--transition),
        transform var(--transition);
}

button:hover {
    background-color: hsl(0, 0%, 95%);
    transform: scale(1.05);
}

@media (hover: none) {
    button:hover {
        transform: none;
    }
}

/* Board container */
#board {
    border: var(--cell-border-thick);
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    aspect-ratio: 1/1;
    width: min(90vw, min(90vh - 150px, 500px));
    margin: 0 auto;
    background-color: var(--color-white);
}

#board .cell {
    border: var(--cell-border);
    position: relative;
    aspect-ratio: 1/1;
    background-color: var(--cell-bg);
    cursor: pointer;
    user-select: none;
    transition: background-color var(--transition);
    touch-action: manipulation;
    display: flex;
    justify-content: center;
    align-items: center;
    box-sizing: border-box;
}

/* Selected cell */
#board .cell[aria-selected="true"] {
    background-color: var(--cell-bg-selected);
    font-weight: 600;
}

#board .cell[aria-selected="true"]:hover {
    background-color: hsl(210, 100%, 80%);
}

/* Grid lines */
/* Every 3rd column (except the right edge) */
#board .cell:nth-child(3n):not(:nth-child(9n)) {
    border-right: var(--cell-border-thick);
}

/* Every 3rd row (except the bottom edge) */
#board .cell:nth-child(n + 19):nth-child(-n + 27),
#board .cell:nth-child(n + 46):nth-child(-n + 54) {
    border-bottom: var(--cell-border-thick);
}

#board .cell:nth-child(9n) {
    border-right: none;
}

#board .cell:nth-child(n + 73):nth-child(-n + 81) {
    border-bottom: none;
}

#board .cell:nth-child(-n + 9) {
    border-top: none;
}

#board .cell:nth-child(9n + 1) {
    border-left: none;
}

/* Highlighted cells */
#board
    .cell.highlight-row:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell),
#board
    .cell.highlight-col:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell),
#board
    .cell.highlight-num:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell),
#board
    .cell.highlight-subgrid:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell) {
    background-color: var(--cell-bg-highlight);
}

#board
    .cell.highlight-row:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell):hover,
#board
    .cell.highlight-col:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell):hover,
#board
    .cell.highlight-num:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell):hover,
#board
    .cell.highlight-subgrid:not(
        [aria-selected="true"],
        [data-cell-prefilled="1"]
    ):not(.incorrect-cell):hover {
    background-color: hsl(211.2, 100%, 90%);
}

#board .cell.highlight-num:not([aria-selected="true"]) {
    font-weight: 600;
    filter: invert(100%);
}

/* Cell states */
#board .cell.empty-cell {
    color: transparent;
}

#board .cell.empty-cell:hover {
    background-color: hsl(0, 0%, 90%);
    cursor: pointer;
    transition: background-color var(--transition);
}

#board .cell.empty-cell.highlight-row:hover,
#board .cell.empty-cell.highlight-col:hover,
#board .cell.empty-cell.highlight-subgrid:hover {
    background-color: hsl(211.2, 100%, 88%);
}

#board .cell.incorrect-cell {
    background-color: var(--cell-bg-incorrect);
}

#board .cell.incorrect-cell:hover {
    background-color: hsl(3, 100%, 64%);
}

#board .cell[data-cell-prefilled="1"] {
    cursor: initial;
    background-color: var(--cell-bg-prefilled);
}

#board .cell[data-cell-prefilled="1"][aria-selected="true"] {
    background-color: var(--cell-bg-highlight);
}

#board .cell span {
    font-size: var(--cell-font-size);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 1;
}

/* Inner grid */
#board .inner-grid {
    display: grid;
    grid-template: repeat(3, 1fr) / repeat(3, 1fr);
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    box-sizing: border-box;
    padding: 2px;
}

#board .inner-grid div {
    font-size: calc(var(--cell-size) * 0.22);
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    padding: 0;
    margin: 0;
    min-width: 0;
    min-height: 0;
    overflow: visible;
}

#board .cell .inner-grid div[aria-selected="true"] {
    font-weight: 900;
}

#board .cell .inner-grid div {
    opacity: 0;
}

#board .cell .inner-grid .note-visible {
    opacity: 1;
    color: var(--color-black);
    font-weight: normal;
}

/* Inner grid */
#board .inner-grid {
    display: grid;
    grid-template: repeat(3, 1fr) / repeat(3, 1fr);
    width: 100%;
}

#board .inner-grid div {
    font-size: var(--cell-note-font-size);
    display: grid;
    place-items: center;
}

#board .inner-grid div[aria-selected="true"] {
    font-weight: 600;
}

/* Keyboard */
#keyboard {
    display: flex;
    justify-content: center;
    flex-wrap: wrap;
    gap: min(0.5rem, 1.5vw);
    width: min(100%, 500px);
    margin: 0.5rem auto 0;
    padding: 0.5rem;
}

#keyboard button {
    width: var(--keyboard-btn-size);
    height: var(--keyboard-btn-size);
    font-size: var(--keyboard-font-size);
    user-select: none;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Prevent zooming on double-tap on mobile */
@media (max-width: 768px) {
    html {
        touch-action: manipulation;
    }

    /* Add breathing room for mobile */
    main {
        padding: 0.5rem;
    }
}

/* Landscape orientation adjustments */
@media (max-width: 768px) and (orientation: landscape) {
    main {
        flex-direction: row;
        justify-content: center;
        align-items: center;
        gap: 1rem;
        padding: 0.5rem;
    }

    #board {
        width: min(60vw, 80vh);
        height: auto;
        flex-shrink: 0;
    }

    #keyboard {
        width: min(35vw, 300px);
        display: grid;
        grid-template-columns: repeat(3, 1fr);
        grid-auto-rows: 1fr;
        gap: 0.5rem;
        height: auto;
        max-height: 80vh;
        margin: 0;
    }
}
