:root {
    --card-overlap: -50px;
}

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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    height: 100vh;
    margin: 0;
    padding: 10px;
    color: #fff;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    overflow: auto;
}

h1 {
    text-align: center;
    font-size: 1.5em;
    margin: 5px 0 10px 0;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
    flex-shrink: 0;
}

.panel {
    background: #f7f7fa;
    backdrop-filter: blur(6px);
    border-radius: 14px;
    padding: 32px 28px;
    margin: 28px auto;
    max-width: 500px;
    max-height: 85vh;
    overflow-y: auto;
    box-shadow: 0 4px 18px rgba(60,60,80,0.10);
    color: #222;
    color: #2a3c5a;
    text-align: center;
    font-size: 1.25em;
    margin-bottom: 16px;
    text-shadow: none;
}

#deal-end, #game-end {
    position: absolute;
    top: 20%;
    left: 50%;
    transform: translate(-50%, 0);
    z-index: 1000;
    margin: 0;
    max-width: 90%;
    max-height: 60vh;
    overflow-y: auto;
}

input[type="text"] {
    width: 100%;
    padding: 14px;
    margin: 18px 0;
    border: 1.5px solid #b0b8c9;
    border-radius: 8px;
    font-size: 16px;
    background: #fff;
    color: #222;
}

.game-settings {
    margin: 15px 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 15px;
}

.game-settings label {
    font-size: 16px;
    font-weight: 500;
    flex-shrink: 0;
}

select {
    padding: 10px 15px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    background: white;
    color: #333;
    cursor: pointer;
    font-weight: 500;
    min-width: 200px;
    transition: background 0.3s, transform 0.2s;
}

select:hover {
    background: #f0f0f0;
    transform: translateY(-2px);
}

select:focus {
    outline: 2px solid #4CAF50;
    outline-offset: 2px;
}

/* Range slider styling for MCTS max_time */
input[type="range"] {
    flex: 1;
    height: 6px;
    border-radius: 3px;
    background: #ddd;
    outline: none;
    appearance: none;
    -webkit-appearance: none;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
    transition: background 0.3s, transform 0.2s;
}

input[type="range"]::-webkit-slider-thumb:hover {
    background: #388e3c;
    transform: scale(1.2);
}

input[type="range"]::-moz-range-thumb {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: #4CAF50;
    cursor: pointer;
    border: none;
    transition: background 0.3s, transform 0.2s;
}

input[type="range"]::-moz-range-thumb:hover {
    background: #388e3c;
    transform: scale(1.2);
}

button {
    width: 100%;
    padding: 16px;
    background: linear-gradient(90deg, #4caf50 0%, #388e3c 100%);
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    transition: background 0.3s, box-shadow 0.2s;
    box-shadow: 0 2px 8px #388e3c22;
}

button:hover {
    background: linear-gradient(90deg, #388e3c 0%, #4caf50 100%);
    box-shadow: 0 4px 16px #388e3c22;
}

.rules {
    margin-top: 20px;
    text-align: left;
}

.rules ul {
    margin-left: 20px;
    margin-top: 10px;
}

.rules li {
    margin: 8px 0;
}

#game-area {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-height: 0;
}

.scores {
    background: rgba(0, 0, 0, 0.3);
    padding: 8px;
    border-radius: 10px;
    margin-bottom: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.95em;
    flex-shrink: 0;
    /* allow wrapping for smaller viewports (restore previous behavior) */
    flex-wrap: wrap;
    gap: 10px;
    position: relative;
}

/* Compact settings gear button in the scores bar */
.settings-button {
    background: transparent;
    border: 1px solid rgba(255,255,255,0.15);
    color: #fff;
    font-size: 1.05rem;
    padding: 6px;
    width: auto !important;
    min-width: 36px;
    height: 36px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s, transform 0.08s;
    position: absolute;
    right: 8px;
}
.settings-button:hover {
    background: rgba(255,255,255,0.06);
    transform: translateY(-1px);
}

.settings-button { margin-left: 8px; }

/* Settings overlay/backdrop */
#settings-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 1500;
}

.in-game-settings {
    position: fixed !important;
    left: 50% !important;
    top: 50% !important;
    transform: translate(-50%, -50%) !important;
    width: 340px !important;
    max-width: calc(100% - 32px) !important;
    padding: 18px 16px !important;
    border-radius: 12px !important;
    box-shadow: 0 12px 28px rgba(0,0,0,0.45) !important;
    z-index: 1600 !important;
    background: linear-gradient(180deg, rgba(255,255,255,0.98), #fff) !important;
    color: #1f2b3a !important;
    transition: transform 160ms ease, opacity 160ms ease;
    opacity: 1;
}

.in-game-settings h3 {
    margin-top: 6px;
    margin-bottom: 8px;
    font-size: 1.05rem;
}

.settings-close {
    position: absolute;
    right: 10px;
    top: 8px;
    background: #fff;
    border: 1px solid rgba(0,0,0,0.06);
    width: 36px;
    height: 36px;
    border-radius: 8px;
    font-size: 1rem;
    cursor: pointer;
    color: #333;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    z-index: 1700;
}

@media (max-width: 420px) {
    .in-game-settings { width: 92% !important; padding: 12px !important; }
}

@media (max-width: 768px) {
    .settings-button { font-size: 0.95rem; padding: 4px 6px; }
}

#scores-display {
    display: flex;
    gap: 15px;
    flex-wrap: wrap;
    align-items: center;
}

.card-color-selector {
    display: flex;
    align-items: center;
    gap: 8px;
}

.card-color-selector label {
    font-size: 0.9em;
    white-space: nowrap;
}

.card-color-selector select {
    padding: 4px 8px;
    font-size: 0.85em;
    min-width: auto;
    border-radius: 5px;
}

.score-item {
    text-align: center;
}

.score-item.eliminated {
    text-decoration: line-through;
    opacity: 0.5;
}

.game-board {
    background: rgba(255, 255, 255, 0.1);
    backdrop-filter: blur(10px);
    border-radius: 15px;
    padding: 10px;
    flex: 1;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
    position: relative;
}

.player-area {
    margin: 3px 0;
    text-align: center;
    flex-shrink: 0;
    min-height: 120px;
}

.player-area h3 {
    font-size: 1em;
    margin: 3px 0;
}

.player-header {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 15px;
    margin-bottom: 10px;
    position: relative;
}

.player-header h3 {
    margin: 0;
    color: #ffd700;
}

.speech-bubble {
    position: relative;
    /* use flex so content centers and multiple lines don't change vertical alignment */
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 12px;
    padding: 8px 15px;
    max-width: 300px;
    font-size: 0.95em;
    font-weight: bold;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    opacity: 0;
    transform: scale(0.8);
    transition: opacity 0.3s ease, transform 0.3s ease;
    /* Reserve enough vertical space for up to two lines to avoid layout jumps */
    min-height: 48px;
    line-height: 1.15;
    visibility: hidden;
    margin-left: 8px;
    margin-right: 8px;
    /* Allow text wrapping without overflow */
    white-space: normal;
    word-break: break-word;
    overflow-wrap: anywhere;
}

.speech-bubble.show {
    opacity: 1;
    transform: scale(1);
    visibility: visible;
}

.speech-bubble.fade-out {
    opacity: 0;
    transform: scale(0.9);
    transition: opacity 0.5s ease, transform 0.5s ease;
}
.speech-bubble::before {
    content: '';
    position: absolute;
    width: 0;
    height: 0;
    border: 8px solid transparent;
}

#player-area .speech-bubble::before {
    border-right-color: rgba(255, 255, 255, 0.95);
    left: -15px;
    top: 50%;
    transform: translateY(-50%);
}

#opponent-area .speech-bubble::before {
    border-right-color: rgba(255, 255, 255, 0.95);
    left: -15px;
    top: 50%;
    transform: translateY(-50%);
}

.cards-hidden {
    display: flex;
    justify-content: center;
    gap: 5px;
    margin: 15px 0;
    height: 85px;
}

.card-back {
    width: 60px;
    height: 85px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

.cards-hand {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin: 5px 0;
    flex-wrap: wrap;
    height: 110px;
}

.card {
    width: 70px;
    height: 100px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 1.5em;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    box-shadow: 0 4px 8px rgba(0,0,0,0.3);
    position: relative;
    flex-shrink: 0;
    will-change: transform;
    background-size: contain;
    background-repeat: no-repeat;
    background-position: center;
}

.card:hover {
    transform: translateY(-10px);
    box-shadow: 0 8px 16px rgba(0,0,0,0.4);
}

.card.playing {
    animation: cardPlaying 0.5s ease-out forwards;
}

.card.shake {
    animation: cardShake 0.5s ease-in-out;
}

@keyframes cardPlaying {
    0% {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
    50% {
        transform: translateY(-30px) scale(1.1);
        opacity: 0.8;
    }
    100% {
        transform: translateY(-100px) scale(1);
        opacity: 0;
    }
}

@keyframes cardShake {
    0%, 100% {
        transform: translateX(0);
    }
    10%, 30%, 50%, 70%, 90% {
        transform: translateX(-5px);
    }
    20%, 40%, 60%, 80% {
        transform: translateX(5px);
    }
}

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

.card.disabled:hover {
    transform: none;
}

.card.red {
    color: #d32f2f;
}

.card.black {
    color: #000;
}

.card-suit {
    font-size: 1.2em;
}

.table {
    min-height: 100px;
    background: rgba(0, 100, 0, 0.3);
    border-radius: 10px;
    padding: 8px;
    margin: 8px 0;
    text-align: center;
    flex-shrink: 0;
}

.played-card-area {
    min-height: 100px;
    max-height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 5px 0;
    position: relative;
    flex-wrap: nowrap;
    overflow-x: hidden;
    overflow-y: hidden;
    will-change: transform;
}

.played-card {
    box-shadow: 0 6px 12px rgba(0,0,0,0.4);
    position: relative;
    flex-shrink: 0;
    transition: transform 0.2s ease, z-index 0s;
    will-change: transform;
}

.played-card:hover {
    transform: translateY(-20px);
    z-index: 1000 !important;
    cursor: pointer;
}

/* Animaatio uudelle kortille - lisätään JavaScriptillä */
.played-card.slide-in {
    animation: cardSlideIn 0.5s ease-out;
}

@keyframes cardSlideIn {
    from {
        transform: translateY(100px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

/* Animaatio vastustajan korteille */
#opponent-played .played-card.slide-in {
    animation: cardSlideDown 0.5s ease-out;
}

@keyframes cardSlideDown {
    from {
        transform: translateY(-100px);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

.message {
    margin: 5px 0;
    padding: 8px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    font-size: 0.95em;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#turn-indicator {
    font-size: 1em;
    margin-bottom: 5px;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

/* Pienemmät kortit desktop-resoluutioille joissa korkeus on pieni */
@media (max-height: 900px) {
    .player-area {
        min-height: 100px;
    }

    .card {
        width: 60px;
        height: 85px;
        font-size: 1.3em;
    }

    .card-back {
        width: 50px;
        height: 70px;
    }

    .cards-hand {
        gap: 6px;
        height: 90px;
    }

    .played-card-area {
        min-height: 85px;
        max-height: 85px;
    }

    .table {
        min-height: 80px;
        padding: 6px;
        margin: 6px 0;
    }

    .message {
        min-height: 30px;
        height: 40px;
        padding: 6px;
        font-size: 0.9em;
    }

    .player-area {
        margin: 2px 0;
    }

    .scores {
        font-size: 0.9em;
        padding: 6px;
    }
}

@media (max-width: 768px) {
    :root {
        --card-overlap: -30px;
    }

    body {
        padding: 2px;
        height: 100dvh;
        min-height: 100dvh;
    }

    .container {
        padding: 0;
    }

    h1 {
        font-size: 0.95em;
        margin: 2px 0 3px 0;
    }

    #game-area {
        flex: 1;
        min-height: 0;
    }

    .panel {
        padding: 12px;
        margin: 5px auto;
    }

    input[type="text"], select {
        padding: 10px;
        font-size: 14px;
    }

    .game-settings {
        flex-direction: column;
        align-items: stretch;
        gap: 10px;
    }

    .game-settings label {
        text-align: left;
    }

    select {
        min-width: auto;
        width: 100%;
    }

    button {
        padding: 12px;
        font-size: 16px;
    }

    .scores {
        font-size: 0.75em;
        padding: 4px;
        margin-bottom: 4px;
        flex-wrap: wrap;
        gap: 5px;
    }

    #scores-display {
        gap: 8px;
    }

    .card-color-selector {
        width: 100%;
        justify-content: center;
        gap: 5px;
    }

    .card-color-selector label {
        font-size: 0.8em;
    }

    .card-color-selector select {
        font-size: 0.75em;
        padding: 2px 4px;
    }

    .score-item {
        margin: 2px;
    }

    .game-board {
        padding: 4px;
        /* Poistetaan justify-content: space-between mobiilissa */
        justify-content: flex-start;
    }

    #opponent-area {
        flex-shrink: 0;
    }

    .player-area {
        flex-shrink: 0;
        min-height: 80px;
        margin: 2px 0;
    }

    .player-area h3 {
        font-size: 0.85em;
        margin: 1px 0;
    }

    .card {
        width: 45px;
        height: 63px;
        font-size: 1.1em;
    }

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

    .cards-hand {
        gap: 2px;
        height: 65px;
        margin: 2px 0;
    }

    .card-back {
        width: 45px;
        height: 63px;
    }

    .cards-hidden {
        gap: 2px;
        margin: 4px 0;
        height: 63px;
    }

    .played-card-area {
        min-height: 65px;
        max-height: 65px;
        margin: 2px 0;
    }

    .table {
        padding: 4px;
        margin: 3px 0;
        min-height: 50px;
    }

    .message {
        font-size: 0.75em;
        height: 28px;
        padding: 4px;
        margin: 2px 0;
    }

    #turn-indicator {
        font-size: 0.8em;
        margin-bottom: 2px;
    }

    .rules {
        margin-top: 15px;
    }

    .rules ul {
        margin-left: 15px;
        font-size: 0.9em;
    }

    .rules li {
        margin: 5px 0;
    }
}
/* Game History Menu */
.history-menu {
    position: fixed;
    top: 0;
    left: -350px;
    width: 350px;
    height: 100vh;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    box-shadow: 2px 0 10px rgba(0, 0, 0, 0.3);
    z-index: 1000;
    transition: left 0.3s ease;
    overflow-y: auto;
    padding: 20px;
    color: #222;
}

.history-menu.open {
    left: 0;
}

.history-menu h2 {
    color: #1e3c72;
    margin-bottom: 20px;
    font-size: 1.5em;
    border-bottom: 2px solid #2a5298;
    padding-bottom: 10px;
}

.history-close-btn {
    position: absolute;
    top: 15px;
    right: 15px;
    background: none;
    border: none;
    font-size: 24px;
    cursor: pointer;
    color: #666;
    padding: 5px 10px;
    transition: color 0.2s;
}

.history-close-btn:hover {
    color: #222;
}

.history-toggle-btn {
    position: fixed;
    top: 20px;
    left: 20px;
    width: 50px;
    height: 50px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.9);
    border: 2px solid #2a5298;
    font-size: 24px;
    cursor: pointer;
    z-index: 999;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
    transition: all 0.3s ease;
}

.history-toggle-btn:hover {
    background: #fff;
    transform: scale(1.1);
}

.history-content {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.history-loading {
    text-align: center;
    color: #666;
    padding: 20px;
}

.history-empty {
    text-align: center;
    color: #999;
    padding: 40px 20px;
    font-style: italic;
}

.history-entry {
    background: #f9f9f9;
    border-radius: 8px;
    padding: 12px;
    border-left: 4px solid #2a5298;
    transition: transform 0.2s, box-shadow 0.2s;
}

.history-entry:hover {
    transform: translateX(5px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.history-entry.player-won {
    border-left-color: #4caf50;
}

.history-entry.ai-won {
    border-left-color: #f44336;
}

.history-match {
    font-weight: bold;
    font-size: 1.1em;
    margin-bottom: 6px;
    color: #1e3c72;
}

.history-winner {
    font-size: 0.95em;
    margin-bottom: 4px;
}

.history-winner.player-won {
    color: #4caf50;
}

.history-winner.ai-won {
    color: #f44336;
}

.history-scores {
    font-size: 0.85em;
    color: #666;
    margin-bottom: 4px;
}

.history-time {
    font-size: 0.75em;
    color: #999;
    text-align: right;
}

@media (max-width: 600px) {
    .history-menu {
        width: 280px;
        left: -280px;
        padding: 15px;
    }

    .history-toggle-btn {
        width: 40px;
        height: 40px;
        font-size: 20px;
        top: 15px;
        left: 15px;
    }
}