:root {
    --bg-color: #f0f0f0;
    /* Light gray background */
    --text-color: #333;
    /* Dark text */
    --board-border-color: #333;
    /* Dark border */
    --cell-bg-color: #ffffff;
    /* White cell background */
    --cell-border-color: #ccc;
    /* Light gray cell border */
    --original-num-color: #333;
    /* Dark original numbers */
    --original-cell-bg-color: #eee;
    /* Lighter gray for original cells */
    --user-num-color: #007bff;
    /* Blue for user numbers */
    --selected-cell-bg-color: #a2d2ff;
    /* Light blue for selected */
    --active-btn-bg-color: #e0f2f7;
    /* Light blue for active button */
    --highlighted-cell-bg-color: #e0f2f7;
    /* Light blue for highlighted */
    --error-cell-bg-color: #e0e0e0;
    /* Light gray for errors */
    --pencil-mark-color: #666;
    /* Gray for pencil marks */
    --palette-btn-bg-color: #ffffff;
    /* White for palette buttons */
    --palette-btn-border-color: #ccc;
    /* Light gray for palette button border */
    --status-info-color: #333;
    /* Dark for info status */
    --status-info-bg-color: #e0e0e0;
    /* Light gray for info status background */
    --status-success-color: #0056b3;
    /* Vibrant blue for success status */
    --status-success-bg-color: #e0f2f7;
    /* Light blue for success status background */
    --status-error-color: #cc5500;
    /* Dark orange for error status */
    --status-error-bg-color: #e0e0e0;
    /* Light gray for error status background */
    --modal-bg-color: #fefefe;
    /* White for modal background */
    --modal-border-color: #888;
    /* Gray for modal border */
    --modal-overlay-color: rgba(0, 0, 0, 0.4);
    /* Dark overlay */
    --close-button-color: #aaa;
    /* Light gray for close button */
    --new-game-btn-bg: #007bff;
    /* Blue for new game button */
    --new-game-btn-hover-bg: #0056b3;
    /* Darker blue for new game button hover */
    --difficulty-btn-bg: #007bff;
    /* Blue for difficulty button */
    --difficulty-btn-hover-bg: #0056b3;
    /* Darker blue for difficulty button hover */
    --settings-btn-bg: #6c757d;
    /* Gray for settings button */
}

.dark-mode {
    --bg-color: rgb(16, 60, 72);
    /* Selenized Dark bg */
    --text-color: rgb(173, 188, 188);
    /* Selenized Dark original/button_text */
    --board-border-color: rgb(112, 128, 128);
    /* Selenized Dark grid */
    --cell-bg-color: rgb(10, 50, 60);
    /* Selenized Dark sidebar_bg */
    --cell-border-color: rgb(80, 90, 90);
    /* Darker for inner grid lines */
    --original-num-color: rgb(173, 188, 188);
    /* Selenized Dark original */
    --original-cell-bg-color: #1F1F1F;
    /* Black for pre-filled numbers in dark mode */
    --user-num-color: rgb(86, 182, 194);
    /* Selenized Dark user */
    --selected-cell-bg-color: rgb(60, 120, 130);
    /* Muted blue-green */
    --highlighted-cell-bg-color: rgb(60, 120, 130);
    /* Muted color for highlighted cell */
    --error-cell-bg-color: rgb(255, 150, 0);
    /* Vibrant orange for error */
    --pencil-mark-color: rgb(112, 128, 128);
    /* Selenized Dark pencil */
    --palette-btn-bg-color: rgb(24, 73, 86);
    /* Selenized Dark button */
    --palette-btn-border-color: rgb(112, 128, 128);
    /* Selenized Dark grid */
    --active-btn-bg-color: rgb(60, 120, 130);
    /* Muted color for active button */
    --status-info-color: rgb(173, 188, 188);
    /* Selenized Dark original */
    --status-info-bg-color: rgb(24, 73, 86);
    /* Selenized Dark button */
    --status-success-color: rgb(100, 150, 255);
    /* Light blue for success */
    --status-success-bg-color: rgb(24, 73, 86);
    /* Selenized Dark button */
    --status-error-color: rgb(200, 100, 0);
    /* Dark orange for error */
    --status-error-bg-color: rgb(24, 73, 86);
    /* Selenized Dark button */
    --modal-bg-color: rgb(24, 73, 86);
    /* Selenized Dark dialog_bg */
    --modal-border-color: rgb(173, 188, 188);
    /* Selenized Dark dialog_border */
    --modal-overlay-color: rgba(0, 0, 0, 0.7);
    /* Darker overlay */
    --close-button-color: rgb(173, 188, 188);
    /* Selenized Dark original */
    --new-game-btn-bg: rgb(24, 73, 86);
    /* Selenized Dark button */
    --new-game-btn-hover-bg: rgb(10, 50, 60);
    /* Selenized Dark sidebar_bg */
    --difficulty-btn-bg: rgb(24, 73, 86);
    /* Selenized Dark button */
    --difficulty-btn-hover-bg: rgb(10, 50, 60);
    /* Selenized Dark sidebar_bg */
    --settings-btn-bg: #5a6268;
    /* Darker gray for settings button in dark mode */
}

.dark-mode button.palette-icon {
    color: var(--text-color);
}

.dark-mode .cell.selected .pencil-mark {
    color: rgb(50, 50, 50);
    /* Darker color for selected pencil marks in dark mode */
}

/* Base styles (Portrait Mode Default) */
body {
    font-family: sans-serif;
    display: flex;
    flex-direction: column;
    align-items: center;
    background-color: var(--bg-color);
    color: var(--text-color);
    position: relative;
    padding: 10px;
    min-height: 100vh;
    /* Ensure body takes full viewport height */
    overflow: hidden;
    /* Prevent scrolling */
}

#theme-toggle {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 1000;
    cursor: pointer;
    font-size: 2em;
    color: var(--text-color);
}

h1 {
    margin-bottom: 2px;
    margin-top: 5px;
}

#game-controls {
    margin-top: 2px;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    position: relative;
    /* Add this */
}

#new-game {
    margin: 0 auto;
}

#timer {
    font-size: 1.8em;
    font-weight: bold;
    margin-top: 2px;
    color: var(--text-color);
}

#status {
    margin-top: 2px !important;
    font-weight: bold;
    padding: 2px 15px;
    border-radius: 5px;
    line-height: 1;
    display: block;
    text-align: center;
}

/* Main layout for Portrait */
#main-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

#top-right-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
}

#game-area {
    display: flex;
    flex-direction: column;
    /* Board on top, controls below */
    align-items: center;
    width: 100%;
    flex-grow: 1;
}

#sudoku-board {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    border: 3px solid var(--board-border-color);
    margin-top: 4px;
    width: 90vw;
    max-width: 500px;
}

#controls-sidebar {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 90vw;
    max-width: 500px;
    margin: 0 auto;
}

#number-palette {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    justify-content: center;
    grid-template-rows: repeat(4, auto);
    grid-template-areas:
        "num1 num2 num3 pencil help"
        "num4 num5 num6 pen ."
        "num7 num8 num9 backspace ."
        "hint undo redo check restart";
    row-gap: 10px;
    column-gap: 10px;
    margin-top: 10px;
    width: 100%;
}

/* Common styles for cells */
.cell {
    border: 1px solid var(--cell-border-color);
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--cell-bg-color);
    position: relative;
    box-sizing: border-box;
    aspect-ratio: 1 / 1;
}

.cell.original {
    font-weight: normal;
    background-color: var(--original-cell-bg-color);
    font-size: clamp(1.8em, 4vw, 2.3em);
}

.cell.user-entered {
    font-size: clamp(1.8em, 4vw, 2.3em);
    background-color: var(--cell-bg-color);
}

@media (min-width: 800px) and (max-width: 1280px) and (orientation: portrait) {
    .cell.original {
        font-size: clamp(1.8em, 6vw, 2.3em);
    }

    .cell.user-entered {
        font-size: clamp(1.8em, 6vw, 2.3em);
    }
}

.pencil-mark {
    font-size: 0.85em;
    color: var(--pencil-mark-color);
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    box-sizing: border-box;
    line-height: 1;
}

.pencil-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    width: 100%;
    height: 100%;
}

.cell.selected {
    background-color: var(--selected-cell-bg-color);
}

.cell.highlighted {
    background-color: var(--highlighted-cell-bg-color);
}

.cell.error {
    background-color: var(--error-cell-bg-color);
    border: 2px solid #000080;
}

.cell:nth-child(3n) {
    border-right: 2px solid var(--board-border-color);
}

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

#sudoku-board>.cell:nth-child(n+19):nth-child(-n+27),
#sudoku-board>.cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 2px solid var(--board-border-color);
}

.palette-number,
button.palette-icon {
    width: 100%;
    aspect-ratio: 1.5 / 1;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    font-size: 2.0em;
    border: 1px solid var(--palette-btn-border-color);
    background-color: var(--palette-btn-bg-color);
}

button.palette-icon {
    font-family: 'Material Symbols Outlined';
    font-size: 2.6em;
    color: var(--text-color);
}

.palette-number:disabled,
button.palette-icon:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.palette-number.selected,
.palette-icon.selected {
    background-color: var(--selected-cell-bg-color);
}

button.palette-icon.active-icon {
    background-color: var(--active-btn-bg-color) !important;
}

.eraser {
    font-size: 0.8em;
}

#action-controls {
    margin-top: 13px;
    margin-bottom: 2px !important;
    display: block;
    overflow: hidden;
}

/* Landscape Mode Styles */
@media (orientation: landscape) {
    body {
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        padding: 10px;
    }

    #main-content {
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        width: auto;
        height: 100%;
    }

    #top-right-controls {
        flex-direction: column;
        align-items: flex-start;
        margin-right: 20px;
        width: auto;
        height: 100%;
        justify-content: space-between;
    }

    #game-controls {
        justify-content: flex-start;
        width: auto;
    }

    #game-controls {
        position: static;
        /* reset position */
    }

    #new-game {
        position: static;
        /* reset position */
        transform: none;
        /* reset transform */
    }

    h1 {
        margin-top: 0;
    }

    #game-area {
        flex-direction: row;
        /* Board next to controls */
        align-items: flex-end;
        /* Align items to the center */
        width: auto;
        height: 100%;
    }

    #sudoku-board {
        max-width: 80vh;
        /* Adjust board size for landscape */
        margin-top: 0;
        margin-right: 20px;
    }

    #controls-sidebar {
        width: auto;
        height: 100%;
        max-width: 300px;
        /* Limit sidebar width */
        justify-content: flex-end;
    }

    #number-palette {
        grid-template-columns: repeat(3, 1fr);
        /* More compact for landscape sidebar */
        grid-template-rows: auto;
        grid-template-areas:
            "num1 num2 num3"
            "num4 num5 num6"
            "num7 num8 num9"
            "pencil pen backspace"
            "undo redo hint"
            "check restart help";
        row-gap: 10px;
        column-gap: 15px;
        margin-top: 0;
        max-width: none;
    }

    #status {
        margin-top: 10px !important;
    }
}

#new-game {
    padding: 12px 25px;
    font-size: 1.2em;
    cursor: pointer;
    background-color: var(--new-game-btn-bg);
    color: white;
    border: none;
    border-radius: 5px;
}

#new-game:hover {
    background-color: var(--new-game-btn-hover-bg);
}

#settings-btn {
    padding: 10px;
    font-size: 1.5em;
    background-color: var(--settings-btn-bg);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    line-height: 1;
    position: absolute;
    right: 0;
}

#save-game-btn {
    padding: 10px;
    font-size: 1.5em;
    background-color: var(--settings-btn-bg);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    line-height: 1;
    position: absolute;
    right: 55px;
}

.modal {
    position: fixed;
    z-index: 1;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: var(--modal-overlay-color);
}

.modal-content {
    background-color: var(--modal-bg-color);
    margin: 15% auto;
    padding: 20px;
    border: 1px solid var(--modal-border-color);
    width: 80%;
    max-width: 500px;
    text-align: center;
    max-height: 80vh;
    overflow-y: auto;
}

#settings-modal .setting-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
}

.toggle-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 25px;
}

.toggle-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: #ccc;
    transition: .4s;
    border-radius: 25px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 21px;
    width: 21px;
    left: 2px;
    bottom: 2px;
    background-color: white;
    transition: .4s;
    border-radius: 50%;
}

input:checked+.slider {
    background-color: var(--user-num-color);
}

input:checked+.slider:before {
    transform: translateX(25px);
}

#solved-modal .modal-content h2 {
    color: var(--status-success-color);
}

#solved-modal .modal-content button {
    margin-top: 20px;
    padding: 10px 20px;
    font-size: 1.2em;
    cursor: pointer;
    background-color: var(--new-game-btn-bg);
    color: white;
    border: none;
    border-radius: 5px;
}

#solved-modal .modal-content button:hover {
    background-color: var(--new-game-btn-hover-bg);
}

#difficulty-modal .modal-content button {
    display: block;
    width: 80%;
    margin: 10px auto;
    padding: 15px;
    font-size: 1.5em;
    cursor: pointer;
    background-color: var(--difficulty-btn-bg);
    color: white;
    border: none;
    border-radius: 8px;
}

#difficulty-modal .modal-content button:hover {
    background-color: var(--difficulty-btn-hover-bg);
}

#difficulty-modal .modal-content h2 {
    margin-bottom: 20px;
    color: var(--text-color);
}

.close-button {
    color: var(--close-button-color);
    float: right;
    font-size: 28px;
    font-weight: bold;
}

.close-button:hover,
.close-button:focus {
    color: black;
    text-decoration: none;
    cursor: pointer;
}

.keyboard-shortcuts-grid {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 5px 15px;
    text-align: left;
    margin-top: 15px;
    margin-bottom: 20px;
}

.shortcut-key-icon {
    display: flex;
    align-items: center;
    gap: 5px;
}

.shortcut-item .material-symbols-outlined {
    font-size: 1.2em;
    vertical-align: middle;
    color: var(--text-color);
}

.shortcut-key {
    font-weight: bold;
    color: var(--user-num-color);
    text-align: right;
}

.shortcut-desc {
    color: var(--text-color);
}

.sudoku-strategy {
    text-align: left;
    margin-top: 20px;
    line-height: 1.5;
}

.sudoku-strategy h4 {
    margin-top: 15px;
    margin-bottom: 5px;
    color: var(--text-color);
}

.sudoku-strategy ul {
    list-style-type: disc;
    margin-left: 20px;
    padding-left: 0;
}

.sudoku-strategy li {
    margin-bottom: 5px;
}

#loading-indicator {
    display: flex;
    justify-content: center;
    align-items: center;
    position: absolute;
    /* Position it over the board */
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: transparent;
    /* No overlay */
    z-index: 10;
    /* Ensure it's above the board */
}

.spinner {
    border: 8px solid #f3f3f3;
    /* Light grey */
    border-top: 8px solid #3498db;
    /* Blue */
    border-radius: 50%;
    width: 60px;
    height: 60px;

    animation: spin 2s linear infinite;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }
}