:root {
    --card-width: 9vw;
    --card-height: 13.5vw;
    --card-font-size: 1.8vw;
    --card-offset: 2.5vw;

    --bg-color: #2c5f2d;
    --outer-bg: #2c5f2d; /* Default same as bg */
    --text-color: #ffffff;
    --btn-bg: #1e4d1f;
    --btn-hover-bg: #153a16;
    --btn-text: white;
    --stack-border: rgba(255, 255, 255, 0.3);
    --card-bg: white;
    --card-border: #000;
    --card-text: #000;
    --card-shadow: rgba(0, 0, 0, 0.4);
    --red-card-color: #d32f2f;
    --face-down-bg: #1565c0;
    --test-border: rgba(255, 255, 255, 0.3);
    --test-pass-color: #81c784;
    --test-fail-color: #e57373;
}

.dark-mode {
    --bg-color: #1b3d1c;
    --outer-bg: #1b3d1c;
    --text-color: #e0e0e0;
    --btn-bg: #142e15;
    --btn-hover-bg: #0d1f0e;
    --btn-text: #fff;
    --stack-border: rgba(255, 255, 255, 0.2);
    --test-border: rgba(255, 255, 255, 0.2);
    --test-pass-color: #66bb6a;
    --test-fail-color: #ff5252;
}

* {
    box-sizing: border-box;
}

html,
body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
}

html {
    background-color: var(--outer-bg);
}

body {
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
        sans-serif;
    background-color: var(--bg-color);
    color: var(--text-color);
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    display: flex;
    flex-direction: column;
    padding: 0;
    max-width: 800px;
    margin: 0 auto;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: calc(10px + env(safe-area-inset-top))
        calc(15px + env(safe-area-inset-right)) 10px
        calc(15px + env(safe-area-inset-left));
    background-color: var(--bg-color);
    border-bottom: 1px solid var(--stack-border);
    z-index: 100;
}

h1 {
    margin: 0;
    font-size: 1.2rem;
    font-weight: 600;
}

.controls {
    display: flex;
    gap: 10px;
}

#new-game-btn,
#theme-toggle-btn,
#run-tests-btn {
    padding: 8px 16px;
    font-size: 0.9rem;
    font-weight: 500;
    background-color: var(--btn-bg);
    color: var(--btn-text);
    border: none;
    border-radius: 8px;
    cursor: pointer;
    touch-action: manipulation;
    transition: opacity 0.2s;
}

#theme-toggle-btn {
    background-color: var(--text-color);
    color: var(--bg-color);
}

#new-game-btn:active,
#theme-toggle-btn:active {
    opacity: 0.7;
}

#game-board {
    display: flex;
    flex-direction: column;
    padding: 10px calc(10px + env(safe-area-inset-right)) 10px
        calc(10px + env(safe-area-inset-left));
    touch-action: none;
    flex: 1;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* Hide scrollbar for Chrome, Safari and Opera */
#game-board::-webkit-scrollbar {
    display: none;
}

/* Hide scrollbar for IE, Edge and Firefox */
#game-board {
    -ms-overflow-style: none; /* IE and Edge */
    scrollbar-width: none; /* Firefox */
}

.stack {
    width: var(--card-width);
    min-height: var(--card-height);
    border: 1px solid var(--stack-border);
    border-radius: 4px;
    margin-bottom: 1vw;
    position: relative;
}

.card {
    width: var(--card-width);
    height: var(--card-height);
    border: 1px solid var(--card-border);
    border-radius: 4px;
    position: absolute;
    background-color: var(--card-bg);
    color: var(--card-text);
    box-shadow: 0.1vw 0.1vw 0.3vw var(--card-shadow);
    text-align: center;
    font-size: var(--card-font-size);
    cursor: pointer;
    touch-action: none;
}

.rank-top {
    position: absolute;
    top: 0.3vw;
    left: 0.3vw;
    font-size: 1.1em;
    font-weight: bold;
    pointer-events: none;
}

.suit {
    font-size: 3.5em;
    line-height: var(--card-height);
    pointer-events: none;
}

.rank-bottom {
    position: absolute;
    bottom: 0.3vw;
    right: 0.3vw;
    font-size: 1.1em;
    font-weight: bold;
    transform: rotate(180deg);
    pointer-events: none;
}

.red-card {
    color: var(--red-card-color);
}

.face-down {
    background-color: var(--face-down-bg);
    color: transparent;
}

.top-container {
    display: flex;
    width: 100%;
    justify-content: space-between;
    margin-bottom: 2vw;
    align-items: flex-start;
}

.tableau-container {
    display: flex;
    width: 100%;
    flex-wrap: nowrap;
    justify-content: space-between;
}

.stock-waste-container {
    display: flex;
    flex-direction: row-reverse;
    order: 2;
    justify-content: space-between;
    gap: 0vw;
}

.foundations-container {
    display: flex;
    justify-content: space-between;
    order: 1;
    gap: 0vw;
}

.tableau-pile,
.foundation-pile {
    /* Removed fixed margin-right to allow justify-content: space-between to work effectively */
}

#test-container {
    margin-top: auto;
    padding: 10px calc(10px + env(safe-area-inset-right))
        calc(10px + env(safe-area-inset-bottom))
        calc(10px + env(safe-area-inset-left));
    border-top: 1px solid var(--stack-border);
    font-size: 0.8rem;
    opacity: 0.6;
    display: none;
}

#run-tests-btn {
    padding: 4px 8px;
    font-size: 0.7rem;
}

#test-results {
    margin-top: 5px;
}

.test-pass {
    color: var(--test-pass-color);
    font-weight: bold;
}

.test-fail {
    color: var(--test-fail-color);
    font-weight: bold;
}

@media (max-width: 768px) {
    :root {
        --card-width: 8.5vw;
        --card-height: 12.75vw;
        --card-font-size: 1.8vw;
        --card-offset: 2.5vw;
    }
}

@media (max-width: 480px) {
    :root {
        --card-width: 12vw;
        --card-height: 18vw;
        --card-font-size: 2.7vw;
        --card-offset: 5vw; /* Increased for better visibility of cards behind */
    }
    header {
        padding: 8px 10px;
    }
    h1 {
        font-size: 1rem;
    }
    #new-game-btn,
    #theme-toggle-btn {
        padding: 6px 12px;
        font-size: 0.8rem;
    }
    .rank-bottom {
        display: none; /* Skip displaying lower level for mobile */
    }
    .rank-top {
        font-size: 1.8em; /* Bigger type and icon at top */
        top: 0.5vw;
        left: 0.5vw;
    }
    .suit {
        position: absolute;
        bottom: 2vw; /* Position in mid-bottom, adjusted for larger card */
        left: 50%;
        transform: translateX(-50%);
        font-size: 3.5em; /* Big symbol */
        line-height: 1;
    }
    .top-container {
        justify-content: space-between;
        gap: 1vw;
        margin-bottom: 1vw;
    }
    .tableau-container {
        justify-content: space-around;
        gap: 1vw;
    }
    .stock-waste-container {
        justify-content: space-around;
        gap: 1vw;
    }
    .foundations-container {
        justify-content: space-around;
        gap: 1vw;
    }
    body {
        padding: 0;
    }
}

@media (min-width: 800px) {
    :root {
        --card-width: 90px;
        --card-height: 135px;
        --card-font-size: 18px;
        --card-offset: 25px;
    }
    #game-board {
        padding: 10px;
    }
    .stack {
        margin-bottom: 10px;
    }
    .card {
        box-shadow: 1px 1px 3px var(--card-shadow);
    }
    .rank-top {
        top: 3px;
        left: 3px;
    }
    .rank-bottom {
        bottom: 3px;
        right: 3px;
    }
    .top-container {
        margin-bottom: 20px;
    }
    .stock-waste-container,
    .foundations-container {
        gap: 20px;
    }
    #test-container {
        margin-top: 20px;
        padding: 20px;
    }
}
