*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --color-bg: #1a472a;
    --color-surface: #1e293b;
    --color-border: #475569;
    --color-text: #f1f5f9;
    --color-text-muted: #94a3b8;
    --color-primary: #38bdf8;
    --color-red: #dc2626;
    --font-sans: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;

    --card-width: 70px;
    --card-height: 100px;
    --card-radius: 6px;
    --card-gap: 8px;
    --stack-offset: 25px;
    --stack-offset-hidden: 8px;
}

html {
    font-size: 16px;
}

body {
    font-family: var(--font-sans);
    background: var(--color-bg);
    color: var(--color-text);
    line-height: 1.6;
    min-height: 100vh;
    overflow-x: hidden;
}

main {
    max-width: 100%;
    padding: 0.5rem;
}

.back-link {
    display: inline-block;
    color: #4ecca3;
    text-decoration: none;
    margin-bottom: 0.5rem;
}

.back-link:hover {
    text-decoration: underline;
}

.game-header {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
    padding: 0 0.25rem;
}

h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--color-text);
}

.stats {
    display: flex;
    gap: 1rem;
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

.stats strong {
    color: var(--color-text);
}

.buttons {
    display: flex;
    gap: 0.5rem;
}

.buttons button {
    background: rgba(255,255,255,0.15);
    color: var(--color-text);
    border: none;
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
    border-radius: 4px;
    cursor: pointer;
    transition: background 0.15s;
}

.buttons button:hover {
    background: rgba(255,255,255,0.25);
}

.buttons button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.winnable-status {
    font-size: 0.8rem;
    padding: 0.3rem 0.6rem;
    border-radius: 4px;
    font-weight: 600;
    display: inline-block;
}

.winnable-status:empty {
    display: none;
}

.winnable-status.checking {
    background: rgba(255,255,255,0.15);
    color: var(--color-text-muted);
}

.winnable-status.winnable {
    background: rgba(34, 197, 94, 0.25);
    color: #4ade80;
}

.winnable-status.not-winnable {
    background: rgba(239, 68, 68, 0.25);
    color: #f87171;
}

.winnable-status.unknown {
    background: rgba(251, 191, 36, 0.25);
    color: #fbbf24;
}

.game-container {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.top-row {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
}

.stock-waste {
    display: flex;
    gap: var(--card-gap);
}

.foundations {
    display: flex;
    gap: var(--card-gap);
}

.tableau {
    display: flex;
    justify-content: space-between;
    gap: 4px;
}

.pile {
    width: var(--card-width);
    min-height: var(--card-height);
    position: relative;
}

.stock-pile {
    background: rgba(255,255,255,0.1);
    border-radius: var(--card-radius);
    cursor: pointer;
    border: 2px solid rgba(255,255,255,0.3);
}

.stock-pile:hover {
    background: rgba(255,255,255,0.15);
}

.stock-pile.empty {
    display: flex;
    align-items: center;
    justify-content: center;
    border-style: dashed;
    border-color: rgba(255,255,255,0.4);
}

.stock-pile.empty::after {
    content: '↻';
    font-size: 1.5rem;
    color: rgba(255,255,255,0.5);
}

.stock-pile.empty:hover {
    border-color: rgba(255,255,255,0.6);
}

.stock-pile.empty:hover::after {
    color: rgba(255,255,255,0.7);
}

.stock-pile .card {
    cursor: pointer;
}

.waste-pile {
    border: 2px solid rgba(255,255,255,0.15);
    border-radius: var(--card-radius);
}

.waste-pile .card {
    position: absolute;
    top: 0;
    left: 0;
}

.foundation-pile {
    background: rgba(255,255,255,0.05);
    border-radius: var(--card-radius);
    border: 2px solid rgba(255,255,255,0.25);
}

.tableau-pile {
    border: 2px solid rgba(255,255,255,0.15);
    border-radius: var(--card-radius);
    padding-bottom: 4px;
}

.tableau-pile:empty {
    border-style: dashed;
}

.foundation-pile .card {
    position: absolute;
    top: 0;
    left: 0;
}

.tableau-pile {
    display: flex;
    flex-direction: column;
}

.card {
    width: var(--card-width);
    height: var(--card-height);
    background: white;
    border-radius: var(--card-radius);
    position: relative;
    cursor: pointer;
    user-select: none;
    -webkit-user-select: none;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
    transition: transform 0.1s, box-shadow 0.1s;
    flex-shrink: 0;
}

.card.face-down {
    background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 50%, #1e3a8a 100%);
    cursor: default;
}

.card.face-down::after {
    content: '';
    position: absolute;
    inset: 4px;
    border: 2px solid rgba(255,255,255,0.2);
    border-radius: 4px;
}

.card.selected {
    transform: translateY(-4px);
    box-shadow: 0 8px 16px rgba(0,0,0,0.4), 0 0 0 3px var(--color-primary);
}

.card.dragging {
    opacity: 0.9;
    transform: scale(1.05);
    z-index: 1000;
    pointer-events: none;
}

.card-inner {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 4px 6px;
    font-weight: 600;
}

.card-corner {
    display: flex;
    flex-direction: column;
    align-items: center;
    line-height: 1;
}

.card-corner.bottom {
    align-self: flex-end;
    transform: rotate(180deg);
}

.card-rank {
    font-size: 1rem;
}

.card-suit {
    font-size: 0.9rem;
}

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

.card.black .card-inner {
    color: #1a1a1a;
}

.tableau-pile .card {
    margin-top: calc(-1 * var(--card-height) + var(--stack-offset-hidden));
}

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

.tableau-pile .card.face-up {
    margin-top: calc(-1 * var(--card-height) + var(--stack-offset));
}

.tableau-pile .card.face-up:first-child {
    margin-top: 0;
}

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.85);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
}

.overlay.hidden {
    display: none;
}

.win-content {
    background: var(--color-surface);
    padding: 2rem;
    border-radius: 12px;
    text-align: center;
}

.win-content h2 {
    font-size: 1.5rem;
    margin-bottom: 1rem;
    color: var(--color-primary);
}

.win-content p {
    margin-bottom: 0.5rem;
    color: var(--color-text-muted);
}

.win-content button {
    margin-top: 1rem;
    background: var(--color-primary);
    color: #0f172a;
    border: none;
    padding: 0.75rem 1.5rem;
    font-size: 1rem;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
}

.win-content .share-btn {
    background: #2a9d8f;
    margin-left: 0.5rem;
}

.win-content .share-btn:hover {
    background: #248c7f;
}

.content {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 12px;
    padding: 1.5rem;
    margin-top: 1.5rem;
}

.content h2 {
    font-size: 1.15rem;
    font-weight: 600;
    margin-top: 1.25rem;
    margin-bottom: 0.5rem;
    color: var(--color-text);
}

.content h2:first-child {
    margin-top: 0;
}

.content p {
    margin-bottom: 0.75rem;
    color: var(--color-text-muted);
    font-size: 0.9rem;
}

.content ul {
    margin: 0.75rem 0;
    padding-left: 1.25rem;
    color: var(--color-text-muted);
    font-size: 0.9rem;
}

.content li {
    margin-bottom: 0.4rem;
}

.content strong {
    color: var(--color-text);
}

.content a {
    color: var(--color-primary);
    text-decoration: none;
}

.content a:hover {
    text-decoration: underline;
}

/* Mobile responsive */
@media (max-width: 540px) {
    :root {
        --card-width: 44px;
        --card-height: 62px;
        --card-radius: 4px;
        --card-gap: 4px;
        --stack-offset: 18px;
        --stack-offset-hidden: 6px;
    }

    main {
        padding: 0.25rem;
    }

    .game-header {
        margin-bottom: 0.5rem;
    }

    h1 {
        font-size: 1rem;
    }

    .stats {
        font-size: 0.75rem;
        gap: 0.75rem;
    }

    .buttons button {
        padding: 0.3rem 0.5rem;
        font-size: 0.7rem;
    }

    .winnable-status {
        font-size: 0.65rem;
        padding: 0.2rem 0.4rem;
    }

    .card-rank {
        font-size: 0.75rem;
    }

    .card-suit {
        font-size: 0.7rem;
    }

    .card-inner {
        padding: 2px 3px;
    }

    .content {
        padding: 1rem;
        margin-top: 1rem;
    }
}

@media (max-width: 380px) {
    :root {
        --card-width: 38px;
        --card-height: 54px;
        --stack-offset: 15px;
        --stack-offset-hidden: 5px;
    }

    .card-rank {
        font-size: 0.65rem;
    }

    .card-suit {
        font-size: 0.6rem;
    }
}
