* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

:root {
    --card-width: 120px;
    --card-height: 168px;
    --card-radius: 10px;
    --column-gap: 5px;
    --card-aspect-ratio: 1.4;
    --primary-color: #2d5016;
    --secondary-color: #4a7c2c;
    --accent-color: #ffd700;
    --text-light: #ffffff;
    --shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: url('./images/background.png') center center / cover no-repeat fixed;
    color: var(--text-light);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    position: fixed;
    user-select: none;
    -webkit-user-select: none;
}

/* Prevent double-tap zoom on iOS */
button, .card, .mode-card {
    touch-action: manipulation;
}

/* Screen Management */
.screen {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.screen.active {
    display: flex;
    opacity: 1;
}

.screen.overlay {
    background: rgba(0, 0, 0, 0.85);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

/* Start Screen */
.start-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
    gap: 20px;
    padding: 20px;
}

.game-title {
    font-size: clamp(36px, 8vw, 64px);
    font-weight: bold;
    color: var(--accent-color);
    text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.7);
    margin-bottom: 20px;
}

/* Buttons */
.btn-primary, .btn-secondary, .btn-back, .btn-icon {
    padding: 15px 40px;
    font-size: 18px;
    font-weight: bold;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: var(--shadow);
}

.btn-primary {
    background: linear-gradient(135deg, var(--accent-color), #ffed4e);
    color: var(--primary-color);
}

.btn-secondary {
    background: linear-gradient(135deg, var(--secondary-color), var(--primary-color));
    color: var(--text-light);
}

.btn-back {
    background: rgba(255, 255, 255, 0.2);
    color: var(--text-light);
    margin-top: 20px;
}

.btn-icon {
    width: 50px;
    height: 50px;
    padding: 0;
    font-size: 24px;
    background: rgba(255, 255, 255, 0.2);
    color: var(--text-light);
}

.btn-primary:active, .btn-secondary:active, .btn-back:active, .btn-icon:active {
    transform: scale(0.95);
}

/* Mode Selection Screen */
.mode-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    height: 100%;
    padding: 20px;
    overflow-y: auto;
}

.screen-title {
    font-size: clamp(24px, 5vw, 36px);
    margin-bottom: 20px;
    color: var(--accent-color);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.7);
}

.mode-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
    gap: 15px;
    width: 100%;
    max-width: 800px;
    margin-bottom: 20px;
}

.mode-card {
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    padding: 15px;
    cursor: pointer;
    transition: transform 0.2s, background 0.2s;
    box-shadow: var(--shadow);
}

.mode-card:active {
    transform: scale(0.95);
    background: rgba(255, 255, 255, 0.25);
}

.mode-preview {
    width: 100%;
    height: 80px;
    background: rgba(0, 0, 0, 0.3);
    border-radius: 8px;
    margin-bottom: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    padding: 10px;
}

.mode-preview svg {
    width: 100%;
    height: 100%;
    opacity: 0.8;
}

.mode-name {
    font-size: 18px;
    font-weight: bold;
    margin-bottom: 5px;
}

.mode-desc {
    font-size: 14px;
    opacity: 0.8;
}

/* Game Screen */
#gameScreen {
    flex-direction: column;
}

.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(10px);
    z-index: 10;
}

.game-info {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
    justify-content: center;
    flex: 1;
    padding: 0 10px;
}

.info-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-size: 12px;
}

.info-label {
    opacity: 0.7;
    font-size: 10px;
}

.info-value {
    font-weight: bold;
    font-size: 16px;
}

.game-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 10px;
    overflow: hidden;
}

/* Tableau */
.tableau {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: var(--column-gap, 5px);
    flex: 1;
    min-height: 0;
}

.column {
    display: flex;
    flex-direction: column;
    position: relative;
    width: var(--card-width);
}

/* Bottom Area */
.bottom-area {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 20px;
    padding: 15px 0;
    padding-bottom: 30px;
    margin-top: 10px;
}

.stock-container {
    position: relative;
}

.stock, .foundation {
    width: var(--card-width);
    height: var(--card-height);
    border-radius: var(--card-radius);
    position: relative;
}

.stock {
    background: rgba(255, 255, 255, 0.2);
    border: 2px dashed rgba(255, 255, 255, 0.4);
    cursor: pointer;
    transition: transform 0.2s;
}

.stock:active {
    transform: scale(0.95);
}

.stock-count {
    position: absolute;
    top: -10px;
    right: -10px;
    background: var(--accent-color);
    color: var(--primary-color);
    width: 30px;
    height: 30px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 14px;
    box-shadow: var(--shadow);
}

.foundation {
    border: 3px solid var(--accent-color);
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.3);
}

/* Card */
.card {
    width: var(--card-width);
    height: var(--card-height);
    border-radius: var(--card-radius);
    position: relative;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: var(--shadow);
    background-size: cover;
    background-position: center;
    transform-style: preserve-3d;
    margin-top: calc(var(--card-height) * -0.61);
}

.card.face-down {
    background-image: url('./images/cards/backdesign.png');
}

.card.clickable:hover {
    transform: translateY(-5px);
    box-shadow: 0 8px 16px rgba(255, 215, 0, 0.5);
}

.card.clickable:active {
    transform: translateY(-2px);
}

.card.valid-move {
    animation: pulse 1s infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 10px rgba(255, 215, 0, 0.5); }
    50% { box-shadow: 0 0 25px rgba(255, 215, 0, 0.9); }
}

.card:first-child {
    margin-top: 0;
}

.card.moving {
    transition: all 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
    z-index: 100;
    position: fixed;
    margin-top: 0;
}

.card.dealing {
    animation: dealCard 0.4s ease-out;
}

@keyframes dealCard {
    0% {
        transform: translateX(-100vw) rotate(-30deg);
        opacity: 0;
    }
    100% {
        transform: translateX(0) rotate(0deg);
        opacity: 1;
    }
}

.card.flipping {
    animation: flipCard 0.4s ease;
}

@keyframes flipCard {
    0% { transform: rotateY(0deg); }
    50% { transform: rotateY(90deg); }
    100% { transform: rotateY(0deg); }
}

/* Result Screens */
.result-container {
    background: rgba(0, 0, 0, 0.9);
    border-radius: 20px;
    padding: 40px;
    max-width: 400px;
    width: 90%;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

.result-title {
    font-size: 32px;
    margin-bottom: 30px;
    color: var(--accent-color);
}

.result-container.lose .result-title {
    color: #ff6b6b;
}

.result-stats {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin-bottom: 20px;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    font-size: 18px;
    padding: 10px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
}

.stat-label {
    opacity: 0.8;
}

.stat-value {
    font-weight: bold;
    color: var(--accent-color);
}

.result-best {
    margin: 20px 0;
    padding: 15px;
    background: rgba(255, 215, 0, 0.2);
    border-radius: 8px;
    font-size: 16px;
    color: var(--accent-color);
}

.result-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
}

/* Statistics Screen */
.stats-container {
    background: rgba(0, 0, 0, 0.9);
    border-radius: 20px;
    padding: 30px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
}

.stats-content {
    margin: 20px 0;
}

.stats-mode {
    background: rgba(255, 255, 255, 0.1);
    border-radius: 12px;
    padding: 15px;
    margin-bottom: 15px;
}

.stats-mode-title {
    font-size: 20px;
    font-weight: bold;
    color: var(--accent-color);
    margin-bottom: 10px;
}

.stats-row {
    display: flex;
    justify-content: space-between;
    padding: 8px 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.stats-row:last-child {
    border-bottom: none;
}

.stats-buttons {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 20px;
}

/* Responsive Design */
@media (orientation: landscape) and (max-height: 600px) {
    .card {
        margin-top: calc(var(--card-height) * -0.64);
    }
    
    .card:first-child {
        margin-top: 0;
    }
    
    .game-header {
        padding: 5px;
    }
    
    .game-area {
        padding: 5px;
    }
    
    .info-item {
        font-size: 10px;
    }
    
    .bottom-area {
        padding: 8px 0;
        gap: 15px;
    }
}

@media (max-width: 480px) {
    .card {
        margin-top: calc(var(--card-height) * -0.65);
    }
    
    .card:first-child {
        margin-top: 0;
    }
    
    .mode-grid {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .game-info {
        gap: 8px;
    }
}

@media (min-width: 768px) {
    :root {
        --card-width: 85px;
        --card-height: 119px;
    }
}

/* Landscape specific adjustments */
@media (orientation: landscape) {
    .tableau {
        flex-direction: row;
    }
}

/* Mobile landscape specific adjustments */
@media (orientation: landscape) and (max-width: 900px) and (max-height: 600px) {
    .game-header {
        padding: 5px 10px;
        min-height: 50px;
    }
    
    .game-info {
        gap: 8px;
    }
    
    .info-item {
        font-size: 10px;
    }
    
    .info-label {
        font-size: 9px;
    }
    
    .game-area {
        padding: 5px;
    }
    
    .tableau {
        gap: var(--column-gap, 3px);
        padding: 5px 0;
    }
    
    .bottom-area {
        padding: 5px 0;
        gap: 10px;
        margin-top: 5px;
    }
    
    .stock-count {
        width: 24px;
        height: 24px;
        font-size: 12px;
        top: -8px;
        right: -8px;
    }
    
    .card {
        margin-top: calc(var(--card-height) * -0.65);
    }
    
    .card:first-child {
        margin-top: 0;
    }
    
    .btn-icon {
        width: 40px;
        height: 40px;
        font-size: 20px;
    }
}

/* Portrait specific adjustments */
@media (orientation: portrait) {
    .game-area {
        justify-content: flex-start;
    }
}

/* Mode-specific adjustments for layouts with many columns */
.tableau-tripeaks,
.tableau-reverse-tripeaks,
.tableau-rainbow,
.tableau-snake-eyes {
    gap: var(--column-gap, 3px);
}

@media (max-width: 480px) {
    /* Tighter spacing for complex layouts on mobile */
    .tableau-tripeaks,
    .tableau-reverse-tripeaks {
        gap: 2px;
    }
    
    .tableau-rainbow,
    .tableau-snake-eyes {
        gap: 1px;
    }
    
    /* Reduce card overlap for small screens with many columns */
    .tableau-rainbow .card,
    .tableau-snake-eyes .card,
    .tableau-tripeaks .card,
    .tableau-reverse-tripeaks .card {
        margin-top: calc(var(--card-height) * -0.7);
    }
    
    .tableau-rainbow .card:first-child,
    .tableau-snake-eyes .card:first-child,
    .tableau-tripeaks .card:first-child,
    .tableau-reverse-tripeaks .card:first-child {
        margin-top: 0;
    }
}
