:root {
    /* Background colors */
    --color-bg-game: hsl(155, 30%, 22%); /* Deep Emerald */
    --color-bg-highlight: hsla(90, 40%, 50%, 0.9);
    --color-bg-button: hsla(200, 30%, 50%, 0.9);
    --color-bg-button-dark: hsla(200, 30%, 45%, 0.9);
    --color-bg-button-light: hsla(200, 30%, 55%, 0.9);
    --color-bg-modal-overlay: hsla(0, 0%, 0%, 0.7);
    
    /* Text colors */
    --color-text: hsla(0, 0%, 90%, 0.9);
    --color-text-grey: hsla(0, 0%, 75%, 0.9);
    --color-text-dark: hsla(0, 0%, 100%, 0.9);
    
    /* Border colors */
    --color-border: hsla(200, 20%, 40%, 0.9);
    --color-slot-border: hsla(0, 0%, 100%, 0.3);
    
    /* Shadow colors */
    --color-shadow-light: hsla(0, 0%, 0%, 0.2);
    --color-shadow-medium: hsla(0, 0%, 0%, 0.3);
    --color-shadow-heavy: hsla(0, 0%, 0%, 0.5);
    
    /* Spacing & sizing */
    --spacing-xs: 0.25em;
    --spacing-sm: 0.5em;
    --spacing-md: 1em;
    --spacing-lg: 1.5em;
    --border-radius-sm: 0.3em;
    --border-radius-md: 0.4em;
    --border-radius-lg: 0.8em;
    /* Matches SVG rx=97.5 on 1000w (9.75%) -> 7.8em * 0.0975 ≈ 0.76em */
    --card-radius: 0.76em;
    
    /* Card dimensions */
    --card-width: 7.8em;
    /* Aspect ratio 1.618034 (Golden Ratio) */
    --card-height: calc(var(--card-width) * 1.618034);
    --tableau-offset: 3.2em;
}

* {
    box-sizing: border-box;
}

body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    min-width: 100vw;
    height: 100vh;
    width: 100vw;
    margin: 0;
    padding: 0;
    background-color: hsl(0, 0%, 10%); /* Dark background outside game */
    color: var(--color-text);
    /* Base font size scales with the 16:9 box fitting in the viewport */
    /* 1em = 1% of the game width */
    font-size: calc(min(100vw, 177.78vh) / 100);
    overflow: hidden;
    user-select: none;
}

#game-container {
    /* Force 16:9 aspect ratio, fitting within viewport */
    width: min(100vw, 177.78vh);
    height: min(56.25vw, 100vh);
    
    display: flex;
    flex-direction: row;
    background: radial-gradient(circle at center, hsl(155, 25%, 28%) 0%, hsl(155, 35%, 18%) 100%); /* Deep Emerald Vignette */
    padding: var(--spacing-sm);
    overflow: hidden;
    box-shadow: 0 0 2.5em hsla(0, 0%, 0%, 0.5); /* Nice shadow for the box */
    gap: var(--spacing-md);
    position: relative; /* Anchor for card layer */
}

#card-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Let clicks pass through to buttons/slots if no card */
    z-index: 1000; /* Above board, below UI (1600) */
}

/* Left Sidebar */
#left-sidebar {
    display: flex;
    flex-direction: column;
    width: 17em;
    gap: var(--spacing-lg);
    padding: var(--spacing-sm);
    flex-shrink: 0;
}

#title {
    font-size: 1.6em;
    font-weight: bold;
    text-shadow: 0 0.1em 0.3em var(--color-shadow-heavy);
    text-align: center;
    background: linear-gradient(300deg, hsl(217,77%,63%), hsl(319,44%,61%), hsl(142,45%,63%), hsl(22,70%,63%));
    background-size: 300% 300%;
    animation: gradient-shift 10s ease infinite;
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    filter: drop-shadow(0 0.1em 0.1em hsla(0, 0%, 0%, 0.5));
    text-shadow: none;
}

#stock-container {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    align-items: center;
}

#controls-area {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    margin-top: auto; /* Push to bottom if space allows, or just flow */
    position: relative;
    z-index: 1600; /* Ensure buttons are above bouncing cards */
}

#score-container {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    font-size: 1em;
    text-shadow: 1px 1px 2px hsla(0, 0%, 0%, 0.5);
    background: hsla(0, 0%, 0%, 0.2);
    padding: var(--spacing-md);
    border-radius: var(--border-radius-md);
    position: relative;
    z-index: 1600; /* Ensure stats are above bouncing cards */
}

/* Persistent stats style */
#games, #wins, #losses, #total-moves {
    color: var(--color-text-grey);
    font-weight: normal;
    font-size: 0.95em;
}

/* Current game moves style - stands out */
#moves {
    color: hsl(0, 0%, 70%);
    font-weight: bold;
    font-size: 1.2em;
    margin: 0.2em 0;
    padding: 0.2em 0;
    border-top: 1px solid hsla(0, 0%, 100%, 0.1);
    border-bottom: 1px solid hsla(0, 0%, 100%, 0.1);
}

#button-container {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
}

#game-info {
    font-size: 0.7em;
    opacity: 0.7;
    text-align: center;
    margin-top: var(--spacing-sm);
}

/* Main Board (Center) */
#main-board {
    flex: 1;
    display: flex;
    flex-direction: column;
    height: 100%;
    overflow: hidden;
    position: relative;
}

#tableau {
    display: flex;
    justify-content: center; /* Center the columns */
    gap: var(--spacing-md);
    flex: 1;
    padding-bottom: var(--spacing-lg);
    min-height: 0;
    width: 100%;
}

/* Right Sidebar */
#right-sidebar {
    display: flex;
    flex-direction: column;
    width: 11em;
    padding: var(--spacing-sm);
    flex-shrink: 0;
    justify-content: flex-start;
}

#foundations {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    align-items: center;
}

.card-slot {
    width: var(--card-width);
    height: var(--card-height);
    border: 0.12em solid var(--color-slot-border);
    border-radius: var(--card-radius);
    background-color: hsla(0, 0%, 0%, 0.1);
    position: relative;
}

.foundation {
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

/* Foundation placeholders removed as requested */
/* #foundation-0 { background-image: url('cards/h0.svg'); } */
/* #foundation-1 { background-image: url('cards/d0.svg'); } */
/* #foundation-2 { background-image: url('cards/c0.svg'); } */
/* #foundation-3 { background-image: url('cards/s0.svg'); } */

.tableau-pile {
    border: none;
    background: none;
    height: 100%;
    position: relative; /* Ensure absolute cards are relative to this pile */
}

.card {
    width: var(--card-width);
    height: var(--card-height);
    border-radius: var(--card-radius);
    background-color: transparent;
    background-size: contain;
    background-position: center;
    background-repeat: no-repeat;
    /* Stronger black outline via drop-shadow filter */
    filter: drop-shadow(0 0 2px hsl(0, 0%, 0%));
    position: absolute;
    cursor: pointer;
    transition: transform 0.3s linear, filter 0.2s ease; /* Faster movement (0.5s -> 0.3s) */
    touch-action: none; /* Prevent scrolling while dragging */
    pointer-events: auto; /* Re-enable clicks */
    will-change: transform;
}

.card.invalid-return {
    /* Red glow for error - Double shadow for intensity */
    filter: drop-shadow(0 0 8px hsl(42, 100%, 50%)) drop-shadow(0 0 8px hsl(42, 100%, 50%)) !important;
}

/* Only hover effect on cards that are interactive (pointer-events: auto) */
.card:hover {
    filter: drop-shadow(0 0 8px hsl(0, 0%, 100%));
}

.card.shake {
    animation: shake 0.4s ease-in-out;
    /* Red glow for error - Double shadow for intensity */
    filter: drop-shadow(0 0 8px hsl(42, 100%, 50%)) drop-shadow(0 0 8px hsl(42, 100%, 50%));
}

@keyframes shake {
    0%, 100% { margin-left: 0; }
    20% { margin-left: -0.4em; }
    40% { margin-left: 0.4em; }
    60% { margin-left: -0.2em; }
    80% { margin-left: 0.2em; }
}

.card.dragging {
    /* z-index is handled in JS to ensure correct stacking order within the group */
    filter: drop-shadow(0 0 8px hsl(0, 0%, 0%));
    transition: none !important; /* Instant follow */
}

.card.flipping {
    animation: flip-reveal 0.4s ease-in-out;
}

.card.flipping-vertical {
    animation: flip-vertical 0.4s ease-in-out;
}

@keyframes flip-reveal {
    0% { transform: scaleX(1); }
    50% { transform: scaleX(0); }
    100% { transform: scaleX(1); }
}

@keyframes flip-vertical {
    0% { transform: scaleY(1); }
    50% { transform: scaleY(0); }
    100% { transform: scaleY(1); }
}

@keyframes gradient-shift {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.card-back {
    background-image: url('cards/back.svg') !important;
}

/* Tableau stacking */
.tableau-pile .card {
    position: absolute;
    /* 
       Calculate dynamic offset:
       1. Available space = 100% (container height) - var(--card-height)
       2. Space per card = Available space / (count - 1)
       3. Clamp between 1.5em (min squish) and 2.2em (max spread)
    */
    --dynamic-offset: clamp(
        1.5em, 
        (100% - var(--card-height)) / max(1, var(--card-count) - 1), 
        var(--tableau-offset)
    );
    top: calc(var(--card-index) * var(--dynamic-offset));
    /* Ensure left is 0 to align with pile */
    left: 0;
}

/* Modal styles */
.modal {
    display: none;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: var(--color-bg-modal-overlay);
    justify-content: center;
    align-items: center;
}

.modal-content {
    background-color: hsl(210, 29%, 24%);
    padding: 2em;
    border-radius: var(--border-radius-lg);
    text-align: center;
    box-shadow: 0 0.5em 2em var(--color-shadow-heavy);
    color: hsl(0, 0%, 100%);
    max-width: 90%;
    width: 45em;
}

.modal-content h2 {
    font-size: 2em;
    margin-top: 0;
    margin-bottom: 0.5em;
    color: hsl(48, 89%, 50%); /* Gold color for titles */
}

.modal-content p {
    font-size: 1.2em;
    line-height: 1.5;
    margin-bottom: 1.5em;
}

button {
    outline: none;
    font-size: 1.1em;
    padding: 0.8em 1.5em;
    border-radius: 2em;
    border: none;
    background-color: hsl(184, 6%, 53%);
    color: hsl(0, 0%, 100%);
    cursor: pointer;
    transition: all 0.2s ease;
    box-shadow: 0 0.2em 0.5em var(--color-shadow-medium);
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: bold;
    text-shadow: 0 1px 2px hsla(0, 0%, 0%, 0.3);
}

#new-game-button {
    background-color: hsl(182, 45%, 33%);
    color: hsl(145, 80%, 94%);
}

#undo-button {
    background-color: hsl(25, 55%, 50%);
    color: hsl(25, 80%, 94%);
}

#auto-complete-button {
    background-color: hsl(271, 23%, 50%);
    color: hsl(45, 90%, 95%);
    border: 1px solid hsla(0, 0%, 100%, 0.2);
    animation: pulse-gold 2s infinite;
}

@keyframes pulse-gold {
    0% { box-shadow: 0 0 0 0 hsla(39, 100%, 50%, 0.4); }
    70% { box-shadow: 0 0 0 10px hsla(39, 100%, 50%, 0); }
    100% { box-shadow: 0 0 0 0 hsla(39, 100%, 50%, 0); }
}

button:hover:not(:disabled) {
    filter: brightness(1.1);
    transform: translateY(-0.1em);
    box-shadow: 0 0.3em 0.8em var(--color-shadow-heavy);
}
button:active:not(:disabled) {
    transform: translateY(0);
    box-shadow: 0 0.1em 0.2em var(--color-shadow-medium);
}
button:disabled {
    opacity: 0.5;
    cursor: default;
    background: hsl(184, 9%, 62%);
    box-shadow: none;
    transform: none;
}
button.primary {
    background: linear-gradient(135deg, hsl(212, 72%, 59%) 0%, hsl(210, 56%, 47%) 100%);
}

/* Stock placeholder styles */
.stock-placeholder {
    display: flex;
    justify-content: center;
    align-items: center;
    border-radius: var(--card-radius);
}

.stock-placeholder.can-recycle {
    cursor: pointer;
}

.stock-placeholder.can-recycle::after {
    content: '↺'; /* Unicode Counter-Clockwise Open Circle Arrow */
    font-size: 3em;
    color: hsla(0, 0%, 100%, 0.3);
    font-weight: bold;
    transition: all 0.2s ease;
}

.stock-placeholder.can-recycle:hover::after {
    color: hsla(0, 0%, 100%, 0.8);
    transform: rotate(-180deg);
    text-shadow: 0 0 10px hsla(0, 0%, 100%, 0.5);
}
