/* Home button top left - Formatting preserved, sizing scaled */
.home-btn {
    position: absolute;
    top: 2vh;
    left: 2vh;
    background: #fff;
    color: #007bff;
    border: 0.2vh solid #007bff;
    border-radius: 0.8vh;
    padding: 1vh 2vh;
    font-size: 1.8vh;
    font-family: inherit;
    text-decoration: none;
    font-weight: bold;
    box-shadow: 0 0.2vh 0.8vh rgba(0, 0, 0, 0.07);
    z-index: 1000;
    transition: background 0.2s, color 0.2s;
}

.home-btn:hover {
    background: #007bff;
    color: #fff;
}

h1 {
    font-family: 'Luckiest Guy', 'Comic Sans MS', 'Comic Sans', sans-serif;
    letter-spacing: 1px;
    font-size: 5vh;
    margin: 0;
    flex-shrink: 0;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    width: 100vw;
    height: 100vh;
    /* Use dvh (dynamic viewport height) for mobile browsers to account for toolbars */
    height: 100dvh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: #ffffff;
    position: relative;
    color: #333333;
}

.app {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: space-evenly;
    gap: 1vh;
    width: 100%;
    /* Keep app height slightly under 100 to ensure safety on all mobile browsers */
    height: 96dvh;
}

.subtitle {
    font-size: 1.8vh;
    text-align: center;
    margin: 0;
    color: #666666;
    flex-shrink: 0;
}

.board {
    display: grid;
    grid-template-columns: repeat(var(--grid-size, 5), 1fr);
    grid-template-rows: repeat(var(--grid-size, 5), 1fr);
    gap: 0.5vh;
    /* Scales based on height (vh) OR width (vw) to prevent overflow on narrow phones */
    width: min(85vw, 40vh);
    height: min(85vw, 40vh);
    margin: 0;
    background: #f0f0f0;
    padding: 0.5vh;
    border-radius: 1vh;
    flex-shrink: 0;
}

.cell {
    background: #ffffff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5vh;
    font-weight: bold;
    border-radius: 0.5vh;
    cursor: pointer;
    user-select: none;
    transition: all 0.2s;
    border: 0.2vh solid #cecece;
    min-width: 0;
    min-height: 0;
}

.cell.empty {
    background: #333;
    border: none;
    cursor: default;
}

.cell.prefilled {
    background: #cfd8dc;
    color: #495057;
    cursor: default;
}

.cell.selected {
    background: #e3f0ff;
    border: 0.3vh solid #007bff;
    transform: scale(1.05);
    z-index: 2;
}

.keypad {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 1vh;
    /* Keypad width also constrained by both viewport dimensions */
    width: min(80vw, 32vh);
    height: min(30vw, 24vh);
    margin: 0;
    flex-shrink: 0;
}

.key-btn {
    width: 100%;
    height: 100%;
    border-radius: 1vh;
    border: none;
    background: #007bff;
    color: #ffffff;
    font-size: 2.2vh;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

.controls {
    display: flex;
    gap: 1vh;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

button {
    padding: 1vh 2vh;
    border-radius: 1vh;
    border: none;
    background: #007bff;
    color: #ffffff;
    font-size: 1.8vh;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

button:hover {
    background: #0056b3;
    transform: translateY(-0.2vh);
}

.difficulty {
    display: flex;
    gap: 1vh;
    justify-content: center;
    margin: 0;
    flex-shrink: 0;
}

#message {
    font-weight: bold;
    min-height: 2vh;
    text-align: center;
    font-size: 2vh;
}

.hint {
    font-size: 1.5vh;
    color: #888888;
    text-align: center;
}