:root {
    --bg-color: #5ea1db; /* Fallback */
    --board-color: radial-gradient(ellipse at center, #6cb2ea 0%, #4a8bcc 70%, #2f629c 100%);
    --btn-green: linear-gradient(to bottom, #a3df8b, #70c45b);
    --btn-green-border: #4d8e3b;
    --btn-yellow: linear-gradient(to bottom, #ffdc70, #f8b839);
    --btn-yellow-border: #c88916;
    
    --text-primary: #f8fafc;
    --text-secondary: #cbd5e1;
    
    --card-bg: #ffffff;
    --card-red: #e03131;
    --card-black: #212529;
    
    --shadow-heavy: 0 10px 20px rgba(0,0,0,0.4);
}

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

body {
    font-family: 'Inter', 'Microsoft YaHei', sans-serif;
    background: url('../img/bg.png') center/cover no-repeat;
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

/* Shake Animation */
@keyframes shake {
    0% { transform: translate(1px, 1px) rotate(0deg); }
    20% { transform: translate(-3px, 0px) rotate(1deg); }
    40% { transform: translate(1px, -1px) rotate(1deg); }
    60% { transform: translate(-3px, 1px) rotate(0deg); }
    80% { transform: translate(-1px, -1px) rotate(1deg); }
    100% { transform: translate(1px, -2px) rotate(-1deg); }
}
.shake-screen {
    animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

/* --- Top Bar --- */
.top-bar {
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 10px;
    height: 76px;
    padding: 8px 20px 0;
    z-index: 10;
    position: relative;
    /* 4:3 Safe Area */
    width: 100%;
    max-width: calc(100vh * (4 / 3));
    margin: 0 auto;
}

.top-left-info {
    position: relative;
    z-index: 12;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    width: 170px;
    flex: 0 0 170px;
    margin-top: 1px;
}

.top-bar .hand-id {
    position: static;
    transform: none;
}

.card-count-badge {
    background: #1e40af;
    color: #fff;
    padding: 10px 15px;
    border-radius: 5px;
    font-size: 1.8rem;
    font-weight: 900;
    border: 2px solid #fff;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.5);
    background-image: repeating-linear-gradient(45deg, rgba(255,255,255,0.1) 0px, rgba(255,255,255,0.1) 5px, transparent 5px, transparent 10px);
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60px;
}
.hand-id::before { /* Un-skew text */
    content: '';
}
.hand-id {
    color: #0f172a;
    font-weight: 900;
    font-size: 0.72rem;
    display: inline-block;
    padding: 4px 7px;
    border: 1px solid rgba(255,255,255,0.72);
    border-radius: 4px;
    background: rgba(255,255,255,0.72);
    box-shadow: 0 2px 8px rgba(0,0,0,0.22);
    text-shadow: 0 1px 0 rgba(255,255,255,0.45);
    white-space: nowrap;
}

/* Card Counter (記牌器) */
.card-counter {
    background: rgba(255, 255, 255, 0.95);
    border: 3px solid #000;
    display: flex;
    flex-direction: column;
    padding: 2px;
    box-shadow: var(--shadow-heavy);
    color: #000;
    font-weight: 800;
    font-size: 0.9rem;
    position: relative;
    transform: skewX(-10deg);
}

.counter-row {
    display: flex;
}

.counter-row span {
    width: 30px;
    text-align: center;
    border-right: 1px solid #ccc;
    padding: 2px 0;
    transform: skewX(10deg); /* un-skew text */
    display: inline-block;
}
.counter-row span:last-child {
    border-right: none;
}
.counter-row.header {
    border-bottom: 2px solid #000;
    font-size: 0.9rem;
    background: #f1f5f9;
}
.counter-row.values span {
    color: #475569;
    font-size: 1.1rem;
}
.counter-row.values span.warn {
    color: #e03131;
}

/* Top Right Info inside game-ui */
.top-right-info {
    position: absolute;
    top: 15px;
    right: 15px;
    display: flex;
    flex-direction: column;
    gap: 5px;
    align-items: flex-end;
    z-index: 10;
}

.bottom-cards {
    display: flex;
    gap: 2px;
}

.ai-status {
    display: flex;
    align-items: center;
    gap: 6px;
    max-width: 170px;
    padding: 4px 8px;
    border-radius: 5px;
    background: rgba(0, 0, 0, 0.68);
    color: #fff;
    font-size: 0.68rem;
    font-weight: 800;
    line-height: 1.2;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.4);
    white-space: nowrap;
}

.ai-status-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #f59e0b;
    flex: 0 0 auto;
}

.ai-status.online .ai-status-dot {
    background: #22c55e;
}

.ai-status.fallback .ai-status-dot {
    background: #f59e0b;
}

.ai-status.offline .ai-status-dot {
    background: #ef4444;
}

.ai-status.checking .ai-status-dot {
    animation: statusPulse 1s ease-in-out infinite;
}

@keyframes statusPulse {
    0%, 100% { opacity: 0.45; }
    50% { opacity: 1; }
}

.bottom-card-slot {
    width: 35px;
    height: 52px;
    background: #b91c1c;
    border: 1px solid #fff;
    border-radius: 4px;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.5);
}

.score-info {
    background: rgba(0, 0, 0, 0.6);
    padding: 5px 10px;
    color: #fff;
    font-size: 0.9rem;
    border-radius: 5px;
    text-align: right;
    display: flex;
    flex-direction: column;
    gap: 2px;
}
.score-info div {
    display: flex;
    justify-content: space-between;
    width: 100px;
}
.score-val { color: #dc2626; }

/* --- Game Background (Table) --- */
.board-bg {
    position: absolute;
    left: 0; right: 0;
    top: 15vh; bottom: -15vh;
    background: url('../img/table.png') center/cover no-repeat;
    z-index: 1;
}

.game-ui {
    flex: 1;
    position: relative;
    z-index: 5;
    /* 4:3 Safe Area */
    width: 100%;
    max-width: calc(100vh * (4 / 3));
    margin: 0 auto;
}

/* Player Areas */
.player-area {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.player-left {
    top: 40%; left: 10px;
    transform: translateY(-50%);
    align-items: flex-start;
    flex-direction: row;
    gap: 10px;
}
.player-right {
    top: 40%; right: 10px;
    transform: translateY(-50%);
    align-items: flex-start;
    flex-direction: row-reverse;
    gap: 10px;
}
.player-self {
    bottom: 30px; left: 10px;
    display: flex;
    flex-direction: row;
    align-items: flex-end;
}

.player-left .avatar-container, .player-right .avatar-container {
    transform: none; /* remove skew for side bots */
    width: 130px;
}
.player-left .avatar-img, .player-right .avatar-img {
    height: 130px;
}
.player-self .avatar-container {
    width: 132px;
}
.player-self .avatar-img {
    height: 132px;
}

/* Avatars */
.avatar-container {
    background: #fff;
    border: 3px solid #000;
    padding: 5px;
    width: 168px; /* 1.2x bigger */
    display: flex;
    flex-direction: column;
    transform: skewX(-5deg);
    box-shadow: 5px 5px 0px rgba(0,0,0,0.5);
    position: relative;
}
.avatar-img {
    width: 100%;
    height: 144px; /* 1.2x bigger */
    object-fit: cover;
    background: #1e293b;
    border: 2px solid #000;
}
.avatar-name {
    color: #000;
    font-weight: 900;
    font-size: 1.2rem; /* 1.2x bigger */
    text-align: center;
    background: #000;
    color: #fff;
    margin-top: 4px;
    padding: 3px 0;
}
.avatar-score {
    color: #eab308;
    font-weight: 900;
    font-size: 1.3rem; /* 1.2x bigger */
    text-align: center;
    text-shadow: 1px 1px 1px #000;
}

.role-badge {
    position: absolute;
    top: -10px; left: -10px;
    background: #dc2626;
    color: #fff;
    padding: 3px 10px;
    font-weight: 900;
    font-size: 1rem;
    border: 2px solid #000;
    transform: rotate(-15deg);
    box-shadow: 2px 2px 0 #000;
    display: none;
}
.role-badge.show { display: block; }

/* Red Card Count Box */
.card-count-badge {
    position: absolute;
    background: #b91c1c;
    color: #fff;
    border: 2px solid #fff;
    border-radius: 4px;
    width: 36px;
    height: 48px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 900;
    font-size: 1.4rem;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.5);
}
.player-left .card-count-badge { top: 50%; right: -40px; transform: translateY(-50%); }
.player-right .card-count-badge { top: 50%; left: -40px; transform: translateY(-50%); }
.player-self .card-count-badge { display: none; }

/* Play Areas */
.play-area {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
}
.play-area-left { top: 30%; left: 180px; }
.play-area-right { top: 30%; right: 180px; }
.play-area-self { bottom: 280px; left: 50%; transform: translateX(-50%); }

/* Action Popups (不出) */
.action-popup {
    position: absolute;
    font-size: 2.2rem;
    font-weight: 900;
    color: #fcd34d;
    text-shadow: 2px 2px 0 #000, -1px -1px 0 #000, 1px -1px 0 #000, -1px 1px 0 #000;
    transform: skewX(-10deg);
}
.popup-left { top: 22%; left: 210px; transform: skewX(-10deg); z-index: 10; }
.popup-right { top: 22%; right: 210px; transform: skewX(-10deg); z-index: 10; }
.popup-self { bottom: 225px; left: 50%; transform: translateX(-50%) skewX(-10deg); z-index: 31; }


/* Cards */
.card {
    width: 114px;
    height: 162px;
    background-color: transparent;
    border: none;
    filter: drop-shadow(0 3px 8px rgba(0,0,0,0.6));
    user-select: none;
    position: relative;
}
.card-overlap { margin-left: -70px; }
.card-overlap:first-child { margin-left: 0; }

.card.small {
    width: 45px;
    height: 64px;
    background-color: transparent;
    border: none;
    filter: drop-shadow(1px 1px 3px rgba(0,0,0,0.5));
}

.landlord-ribbon {
    position: absolute;
    top: 0; right: 0;
    background: #dc2626;
    color: #fff;
    font-size: 0.6rem;
    padding: 1px 4px;
    border-bottom-left-radius: 4px;
    border-top-right-radius: 4px;
}

/* Hand Cards */
.hand-container {
    position: absolute;
    bottom: -15px; /* sit near the bottom but inside game-ui */
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    justify-content: center;
    height: 188px;
    padding: 0 20px;
    z-index: 20;
}
.hand-card {
    cursor: pointer;
    transition: transform 0.15s cubic-bezier(0.4, 0, 0.2, 1);
    margin-left: -74px;
    box-shadow: -3px 0 8px rgba(0,0,0,0.3);
}
.hand-card:first-child { margin-left: 0; }
.hand-card:hover { transform: translateY(-22px); }
.hand-card.selected {
    transform: translateY(-35px);
    filter: drop-shadow(0 15px 30px rgba(0,0,0,0.5)) drop-shadow(0 0 10px rgba(59, 130, 246, 1));
}

/* Controls (Slanted Buttons) */
.controls-container {
    position: absolute;
    bottom: 210px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 30;
}
.controls {
    display: flex;
    align-items: center;
    gap: 1.1rem;
}
.btn {
    padding: 0.68rem 2.1rem;
    font-size: 1.18rem;
    font-weight: 900;
    cursor: pointer;
    transform: skewX(-15deg);
    border: 3px solid #000;
    box-shadow: 3px 3px 0 #000;
    color: #000;
    text-shadow: 1px 1px 0 rgba(255,255,255,0.5);
    transition: all 0.1s;
}
.btn:active:not(:disabled) {
    transform: skewX(-15deg) translate(2px, 2px);
    box-shadow: 1px 1px 0 #000;
}
.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    filter: grayscale(1);
}
.btn-green { background: var(--btn-green); border-color: var(--btn-green-border); }
.btn-yellow { background: var(--btn-yellow); border-color: var(--btn-yellow-border); }

/* Timer */
.central-timer {
    position: absolute;
    top: calc(43% - 56px);
    left: 50%;
    transform: translate(-50%, -50%);
    width: 60px;
    height: 60px;
    background: radial-gradient(circle, #3b82f6, #1d4ed8);
    border: 4px solid #fbbf24;
    border-radius: 50%;
    color: #fff;
    font-size: 2rem;
    font-weight: 900;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 10px rgba(0,0,0,0.5), inset 0 2px 5px rgba(255,255,255,0.3);
    z-index: 20;
    text-shadow: 1px 1px 2px #000;
}
.central-timer::before, .central-timer::after {
    content: '';
    position: absolute;
    width: 15px;
    height: 10px;
    background: #fbbf24;
    top: -5px;
    border-radius: 5px;
}
.central-timer::before { left: 5px; transform: rotate(-30deg); }
.central-timer::after { right: 5px; transform: rotate(30deg); }

/* Center Messages */
.status-message {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(0,0,0,0.8);
    color: #fff;
    max-width: min(620px, 86vw);
    padding: 12px 28px;
    border-radius: 8px;
    font-size: 1.45rem;
    font-weight: 900;
    z-index: 50;
    text-align: center;
    box-shadow: 0 4px 15px rgba(0,0,0,0.5);
    border: 2px solid #fcd34d;
    animation: scaleIn 0.3s ease-out;
}

@keyframes sweep-away {
    0% {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
    100% {
        opacity: 0;
        transform: scale(0.1) translateY(-100px) rotate(180deg);
        filter: blur(4px);
    }
}

.sweep-animation {
    animation: sweep-away 0.8s forwards cubic-bezier(0.68, -0.55, 0.27, 1.55) !important;
}

/* FX Container */
.fx-container {
    position: fixed; inset: 0; z-index: 9999;
    display: flex; align-items: center; justify-content: center;
    pointer-events: none;
}
.fx-text {
    font-size: 6rem;
    font-weight: 900;
    color: #ef4444;
    text-shadow: 4px 4px 0 #fff, -2px -2px 0 #fff, 2px -2px 0 #fff, -2px 2px 0 #fff, 0 10px 20px rgba(0,0,0,0.8);
    transform: skewX(-10deg);
}

.win-coin-fx {
    position: fixed;
    inset: 0;
    z-index: 10002;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    overflow: hidden;
    animation: winCoinFade 3.6s ease-out forwards;
}

.win-coin-fx-media {
    width: min(92vw, 760px);
    max-height: 82vh;
    object-fit: contain;
    filter: drop-shadow(0 20px 32px rgba(0,0,0,0.45));
    animation: winCoinPop 3.6s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes winCoinPop {
    0% {
        opacity: 0;
        transform: scale(0.72) translateY(28px);
    }
    12% {
        opacity: 1;
        transform: scale(1.02) translateY(0);
    }
    78% {
        opacity: 1;
        transform: scale(1) translateY(-8px);
    }
    100% {
        opacity: 0;
        transform: scale(1.04) translateY(-24px);
    }
}

@keyframes winCoinFade {
    0%, 82% { opacity: 1; }
    100% { opacity: 0; }
}

.start-overlay {
    position: fixed; inset: 0; z-index: 10000;
    background: rgba(0,0,0,0.8);
    display: flex; flex-direction: column; align-items: center; justify-content: center;
}
.start-overlay h1 { font-size: 4rem; color: #fff; margin-bottom: 2rem; }

.hidden { display: none !important; }
