:root {
    --stage-scale: 1;
    --inv-stage-scale: 1;
}

html, body {
    height: 100%;
    width: 100%;
    margin: 0;
    overflow: hidden; /* no sideways scroll on phones */
}

body {
    font-family: Arial, sans-serif;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: center;
    background-color: #333;
    color: #eee;
    /* Safe areas for notched phones */
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
    /* Bottom safe-area lives on #status-banner (footer) */
    padding-bottom: 0;
    box-sizing: border-box;
}

/* Compact status line at absolute bottom (under action controls; not scaled) */
#status-banner {
    position: relative;
    z-index: 20;
    flex-shrink: 0;
    width: 100%;
    box-sizing: border-box;
    text-align: center;
    padding: 8px 16px;
    padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
    font-size: 0.95em;
    font-style: italic;
    color: #ddd;
    background: rgba(20, 20, 20, 0.92);
    border-top: 1px solid #444;
    line-height: 1.35;
    min-height: 1.35em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/*
 * Viewport docks the scaled stage + controls above the bottom status banner
 * (flex-end). Spare height clears above the table (top seat). Stage shell
 * = post-scale layout box (width/height set by JS). Stage = fixed design size
 * that includes all seat overhang; JS scales it.
 */
#game-stage-viewport {
    flex: 1 1 auto;
    min-height: 0;
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    /* Dock stage + controls just above the footer status banner */
    justify-content: flex-end;
    gap: 8px;
    overflow: hidden;
    box-sizing: border-box;
}

#game-stage-shell {
    flex: 0 0 auto;
    /* visible: counter-scaled seat labels may extend past design box */
    overflow: visible;
    margin: 0;
    position: relative;
    /* JS sets width/height to design * scale — never width:100% */
}

#game-stage {
    /* DESIGN: 700 felt + left overhang (mothman@1.6×) + right overhang (mothman+info) */
    width: 1460px;
    height: 990px;
    transform-origin: top left;
    will-change: transform;
    /* Fixed design size — do not stretch to viewport before measuring */
    max-width: none;
    flex-shrink: 0;
}

#game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    /* Side pads clear mothman wings when seat-mothman; regular seats sit flush */
    width: 1460px;
    /* Top pad clears the top-seat name; pot amount lives in the status banner */
    padding: 330px 440px 16px 320px;
    box-sizing: border-box;
    margin: 0;
}

#table {
    width: 700px;
    height: 500px;
    background-image: url('images/tables/table1.png');
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    border-radius: 0px;
    border: 0px solid #555;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0 0 0px rgba(0,0,0,0.7);
    padding: 0;
    margin: 0 auto;
    /* Clear human avatar + name/chips hanging below the bottom rail */
    margin-bottom: 130px;
    flex-shrink: 0;
}

.player-area {
    position: absolute;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    text-align: center;
    font-size: 0.9em;
    line-height: 1.2;
    z-index: 2;
}

/* Seat name+chips: dark pill; counter-scaled via --inv-stage-scale so text
 * stays ~13px on screen after the stage shrinks on phones. */
.player-info {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1px;
    white-space: nowrap;
    background: rgba(12, 12, 12, 0.82);
    color: #fff;
    padding: 5px 11px;
    border-radius: 999px;
    font-size: 13px;
    font-weight: 700;
    letter-spacing: 0.01em;
    line-height: 1.2;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.85);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.55);
    border: 1px solid rgba(255, 255, 255, 0.14);
    /* Cancel stage scale(s): on-screen size ≈ design size */
    transform: scale(var(--inv-stage-scale, 1));
    transform-origin: center top;
    position: relative;
    z-index: 6;
    pointer-events: none;
}
.player-info .player-name {
    font-size: 1em;
}
.player-info .player-chips {
    font-size: 0.92em;
    font-weight: 600;
    color: #f0f0f0;
}
.player-top .player-info {
    transform-origin: center bottom;
}
.player-left .player-info {
    transform-origin: center top;
}
.player-right .player-info {
    transform-origin: center top;
}
.player-human-area .player-info {
    transform-origin: center top;
}

/* Short NPC decision ("Called", "Raised", "Folded") just above the sprite. */
.npc-action {
    position: absolute;
    left: 50%;
    top: 0;
    transform: translate(-50%, -115%) scale(var(--inv-stage-scale, 1));
    transform-origin: center bottom;
    z-index: 9;
    display: none;
    pointer-events: none;
    white-space: nowrap;
    padding: 3px 9px;
    border-radius: 999px;
    background: rgba(12, 12, 12, 0.9);
    color: #fff;
    border: 1px solid rgba(255, 255, 255, 0.22);
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.03em;
    line-height: 1.2;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.85);
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.55);
}
.npc-action:not(:empty) {
    display: block;
}
.npc-action.action-folded {
    color: #ffb4b4;
}
.npc-action.action-raised,
.npc-action.action-bet {
    color: #ffe08a;
}
.npc-action.action-allin {
    color: #ffcc66;
}
/* Top seat's name is above the sprite and counter-scales upward.
   Park the tag above that pill so the two don't stack on each other. */
.player-top .npc-action {
    top: calc(-46px * (var(--inv-stage-scale, 1) - 1));
}
/* Rotated Mothman extends above the layout box; keep the tag above the wings. */
.player-left.seat-mothman .npc-action,
.player-right.seat-mothman .npc-action {
    top: -110px;
}

.player-image {
    /* Human ("You") base size — leave near current */
    width: 152px;
    height: 152px;
    border-radius: 0%;
    border: 2px solid transparent;
    object-fit: cover;
    box-shadow: 0 0 0px rgba(0,0,0,0.6);
    box-sizing: border-box;
    transform-origin: center center;
}

/* NPC seats only — 1.6× base (152→244); mothman wide = 2× NPC size */
.player-top .player-image,
.player-left .player-image,
.player-right .player-image {
    width: 244px;
    height: 244px;
}

.npc-wide,
.player-top .player-image.npc-wide,
.player-left .player-image.npc-wide,
.player-right .player-image.npc-wide {
    /* Must beat .player-* .player-image (0,2,0) or mothman stays square and
     * seat-mothman offsets (written for W=2S) shove the body off the rail. */
    width: 488px;
    height: 244px;
    border-radius: 0px;
    object-fit: cover;
}

/* --- PLAYER PLACEMENT (around 700×500 felt) --- */

/* Left NPC seat — flush to purple rail (same stomach inset as top/bottom).
 * Wide mothman offsets live under .seat-mothman only. S=244 (1.6×). */
.player-left {
    /* S=244: center at -S/4 so body sits just outside felt like top/bottom */
    left: -183px; /* -3S/4 */
    top: calc(50% - 122px);
    transform: none;
}
.player-left .player-image {
    transform: rotate(-90deg);
    transform-origin: center center;
}
/* Mothman left: W=488 rotated −90° to face table; body center = regular seat. */
.player-left.seat-mothman {
    left: -305px; /* -S/4 - W/2 = -61 - 244 */
    top: calc(50% - 122px);
}
.player-left.seat-mothman .player-image {
    /* Seat-aware facing: head toward left edge, torso toward felt */
    transform: rotate(-90deg);
    transform-origin: center center;
}

/* Right NPC seat — default flush for regular NPCs (NOT mothman-tuned).
 * Name/chips stack under the sprite, same as the left seat. */
.player-right {
    /* S=244: center at 700+S/4 → left = 700 - S/4 */
    left: 639px;
    right: auto;
    top: calc(50% - 122px);
    transform: none;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}
.player-right .player-image {
    transform: rotate(90deg);
    transform-origin: center center;
    position: relative;
    z-index: 1;
}
.player-right .player-info {
    text-align: center;
    align-items: center;
    /* Rotated art (Mothman wings) overflows the layout box onto this pill.
       Draw the label above the sprite so the name stays readable. */
    z-index: 8;
}
/* Mothman right: rotate +90° to face table; body center = regular seat */
.player-right.seat-mothman {
    left: 517px; /* 700 + S/4 - W/2 = 761 - 244 */
    top: calc(50% - 122px);
}
.player-right.seat-mothman .player-image {
    transform: rotate(90deg);
    transform-origin: center center;
}

/* You — bottom rail */
.player-human-area {
    left: 50%;
    top: 410px;
    transform: translateX(-50%);
}

/* Human hole cards on the felt above the avatar */
.player-human-area .player-hand-display {
    position: absolute;
    left: 50%;
    transform: translateX(-50%) scale(var(--hole-card-boost, 1));
    transform-origin: center bottom;
    bottom: calc(100% + 36px);
    margin: 0;
    min-height: 0;
    z-index: 2;
}
.player-human-area .player-hand-display:not(:empty) {
    min-height: 100px;
}

/* Top NPC seat — overhang matches taller 244px NPC avatar */
.player-top {
    left: 50%;
    /* S=244: 20px lower than -255 so stomach/hands meet the purple rail.
       Not as low as the old -190 bury-into-felt position. */
    top: -235px;
    transform: translateX(-50%);
    flex-direction: column-reverse;
    gap: 6px;
}
/* Mothman on top already sits flush — keep the previous offset. */
.player-top.seat-mothman {
    top: -255px;
}

/* Top seat hole cards on the felt below the avatar */
.player-top .player-hand-display {
    position: absolute;
    left: 50%;
    transform: translateX(-50%) scale(var(--hole-card-boost, 1));
    transform-origin: center top;
    top: calc(100% + 2px);
    margin: 0;
    min-height: 0;
    z-index: 2;
}
.player-top .player-hand-display:not(:empty) {
    min-height: 100px;
}

.player-area.active-player .player-image {
    border-color: yellow;
    box-shadow: 0 0 15px rgba(255,255,0,0.8);
}

.player-area.folded .player-image {
    opacity: 0.5;
    filter: grayscale(100%);
}


/* Side-seat hole cards toward the felt so counter-scaled name pills
 * (below / beside avatars) do not cover them. */
.player-left .player-hand-display {
    position: absolute;
    left: calc(100% + 6px);
    top: 50%;
    transform: translateY(-50%) scale(var(--hole-card-boost, 1));
    transform-origin: left center;
    margin: 0;
    min-height: 0;
    z-index: 2;
}
.player-right .player-hand-display {
    position: absolute;
    right: calc(100% + 6px);
    left: auto;
    top: 50%;
    transform: translateY(-50%) scale(var(--hole-card-boost, 1));
    transform-origin: right center;
    margin: 0;
    min-height: 0;
    z-index: 2;
}

/* Community board — center of felt */
#community-cards {
    display: flex;
    gap: 12px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    min-height: 90px;
    z-index: 1;
    pointer-events: none;
}

.card {
    width: 56px;
    height: 78px;
    background-color: #fff;
    border: 2px solid #1a1a1a;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.35em;
    font-weight: 800;
    color: #111;
    box-shadow: 2px 2px 5px rgba(0,0,0,0.35);
    background-size: cover;
    letter-spacing: -0.02em;
    line-height: 1.05;
}

/* Stronger rank/suit pip contrast */
.card-pip {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0;
    line-height: 1.02;
    font-weight: 800;
    text-shadow: 0 0 0.5px currentColor;
}
.card-rank {
    font-size: 1.05em;
    font-weight: 900;
}
.card-suit {
    font-size: 1.2em;
    margin-top: -1px;
    font-weight: 900;
    -webkit-text-stroke: 0.4px currentColor;
}
.card-red {
    color: #c41818;
}
.card-black {
    color: #0d0d0d;
}

/* Hole cards — larger design size + thicker pip for readability */
.player-hand-display .card {
    width: 74px;
    height: 104px;
    font-size: 1.7em;
    border-width: 2.5px;
    border-radius: 7px;
}
.player-hand-display .card-suit {
    font-size: 1.28em;
    -webkit-text-stroke: 0.55px currentColor;
}

/* Community board — same readable size used on phones (flop through river) */
#community-cards .card {
    width: 112px;
    height: 156px;
    font-size: 52px;
    border-width: 3px;
}

/* Once the board is out, drop your hand onto the avatar and shrink it slightly. */
#table.board-out .player-human-area .player-hand-display {
    bottom: auto;
    top: 18px;
    transform: translateX(-50%) scale(0.88);
    transform-origin: center top;
    z-index: 6;
}

.card.hidden {
    background-color: #777;
    color: transparent;
}
.card.hidden .card-pip {
    visibility: hidden;
}

/* Controls docked under scaled stage — native size for touch targets */
#action-area {
    flex: 0 0 auto;
    width: 100%;
    max-width: 520px;
    background-color: transparent;
    border: none;
    padding: 4px 12px 8px;
    border-radius: 0;
    text-align: center;
    box-shadow: none;
    font-size: 0.95em;
    position: relative;
    z-index: 1;
    box-sizing: border-box;
}

#player-options {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 6px;
}

#bet-slider {
    width: 180px;
}

button {
    padding: 8px 14px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 5px;
    cursor: pointer;
    font-size: 0.95em;
    transition: background-color 0.2s;
    white-space: nowrap;
    min-height: 36px;
    touch-action: manipulation;
}

button:hover:not([disabled]) {
    background-color: #45a049;
}

button:active:not([disabled]) {
    background-color: #367c39;
}

button:disabled {
    background-color: #666;
    cursor: not-allowed;
    opacity: 0.7;
}

.hidden {
    display: none !important;
}

.player-hand-display {
    display: flex;
    gap: 6px;
    justify-content: center;
    margin-top: 5px;
    min-height: 0;
}
.player-hand-display:not(:empty) {
    min-height: 104px;
}

#bet-live-info {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px 12px;
    width: 100%;
    margin-bottom: 2px;
    font-size: 0.85em;
    color: #ddd;
}

#bet-live-info span:not(.hidden) {
    background-color: rgba(0, 0, 0, 0.45);
    padding: 3px 8px;
    border-radius: 4px;
}

#allin-label {
    color: #ffcc66;
    font-weight: bold;
}

.bet-slider-row {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 8px;
    width: 100%;
    margin-bottom: 4px;
}

#bet-slider-label {
    width: 100%;
    font-size: 0.8em;
    color: #aaa;
}

.action-buttons {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: center;
    gap: 8px;
}


/* Compact player skin picker (start screen) */
#player-select {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
    margin-bottom: 8px;
}
.player-select-label {
    font-size: 0.85em;
    color: #bbb;
    letter-spacing: 0.02em;
}
.player-select-options {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 8px;
}
.skin-btn {
    padding: 4px;
    min-height: 0;
    background: rgba(0, 0, 0, 0.35);
    border: 2px solid transparent;
    border-radius: 8px;
    line-height: 0;
}
.skin-btn img {
    width: 48px;
    height: 48px;
    object-fit: cover;
    display: block;
    border-radius: 4px;
}
.skin-btn:hover:not([disabled]) {
    background: rgba(0, 0, 0, 0.5);
    border-color: rgba(255, 255, 255, 0.25);
}
.skin-btn.selected {
    border-color: #4CAF50;
    box-shadow: 0 0 8px rgba(76, 175, 80, 0.55);
    background: rgba(76, 175, 80, 0.15);
}

/* Dealer button (D) — rotates among all four seats */
.dealer-button {
    position: absolute;
    top: -6px;
    left: -6px;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    background: #f5f5f5;
    color: #111;
    font-weight: bold;
    font-size: 0.75em;
    line-height: 22px;
    text-align: center;
    border: 2px solid #222;
    box-shadow: 0 1px 3px rgba(0,0,0,0.5);
    z-index: 5;
    pointer-events: none;
}
.player-left .dealer-button {
    top: auto;
    bottom: -4px;
    left: auto;
    right: -4px;
}
.player-right .dealer-button {
    top: -4px;
    left: -4px;
}
.player-human-area .dealer-button {
    top: -4px;
    left: calc(50% - 76px - 6px);
}
.player-top .dealer-button {
    top: auto;
    bottom: -4px;
    left: auto;
    right: -4px;
}

/* Banner + touch targets on narrow phones (controls are outside the scaled stage) */
@media (max-width: 480px) {
    #status-banner {
        font-size: 0.85em;
        padding: 6px 10px;
        padding-bottom: max(6px, env(safe-area-inset-bottom, 0px));
        white-space: normal;
    }
    button {
        min-height: 44px;
        min-width: 72px;
        padding: 10px 16px;
        font-size: 1em;
    }
    #bet-slider {
        width: min(280px, 70vw);
        height: 28px;
    }
    .action-buttons {
        gap: 10px;
    }
    /* Counter-scaled pills are large vs avatars — stack right seat + nudge so
     * labels don't cover hole cards or each other / clip hard at the edges. */
    .player-right {
        flex-direction: column;
        align-items: center;
        gap: 6px;
    }
    .player-right .player-info {
        transform-origin: center top;
        align-items: center;
        text-align: center;
    }
    .player-left .player-info {
        margin-top: 2px;
    }
    .player-top .player-info {
        margin-bottom: 2px;
    }
    .player-human-area .player-info {
        margin-top: 4px;
    }
    /* Grow design box so larger NPC (1.6×) overhang still fits after scale */
    #game-stage {
        /* Wider: mothman wing pad. Pot amount is in the status banner. */
        width: 1560px;
        height: 1030px;
    }
    /* Human mild bump; NPC 1.6× of mobile human base (160→256 / wide 512) */
    .player-image {
        width: 160px;
        height: 160px;
    }
    .player-top .player-image,
    .player-left .player-image,
    .player-right .player-image {
        width: 256px;
        height: 256px;
    }
    /* npc-wide size for phones set with seat-mothman offsets below */
    /* S=256 flush: left=-3S/4, right=700-S/4 */
    .player-left {
        left: -192px;
        top: calc(50% - 128px);
    }
    .player-right {
        left: 636px;
        top: calc(50% - 128px);
    }
    /* Mothman rotated W=512 H=256 — seat-aware ±90°; body center = regular */
    .player-left .player-image.npc-wide,
    .player-right .player-image.npc-wide,
    .player-top .player-image.npc-wide,
    .npc-wide {
        width: 512px;
        height: 256px;
    }
    .player-left.seat-mothman {
        left: -320px; /* -S/4 - W/2 = -64 - 256 */
        top: calc(50% - 128px);
    }
    .player-left.seat-mothman .player-image {
        transform: rotate(-90deg);
        transform-origin: center center;
    }
    .player-right.seat-mothman {
        left: 508px; /* 700 + S/4 - W/2 = 764 - 256 */
        top: calc(50% - 128px);
    }
    .player-right.seat-mothman .player-image {
        transform: rotate(90deg);
        transform-origin: center center;
    }
    .player-left.seat-mothman .npc-action,
    .player-right.seat-mothman .npc-action {
        top: -116px;
    }
    .player-top .npc-action {
        top: calc(-48px * (var(--inv-stage-scale, 1) - 1));
    }
    .player-top {
        /* Same 20px drop as desktop (-265 → -245); mothman stays put */
        top: -245px;
        gap: 4px;
    }
    .player-top.seat-mothman {
        top: -265px;
    }
    .player-human-area {
        top: 404px;
    }
    .player-human-area .dealer-button {
        left: calc(50% - 80px - 6px);
    }
    #table {
        margin-bottom: 120px;
    }
    #game-container {
        width: 1560px;
        /* Side pad for mothman wings; top pad for the name pill only */
        padding: 340px 480px 12px 380px;
    }
    /* NPC hole cards only — human + board share a larger readable size below */
    .player-left .player-hand-display .card,
    .player-right .player-hand-display .card,
    .player-top .player-hand-display .card {
        width: 82px;
        height: 116px;
        font-size: 1.85em;
    }
    /*
     * Human hole cards and the board share one phone size so flop/turn/river
     * read as clearly as your hand. No hole-card boost here: that scale was
     * covering the felt. Preflop the hand sits on the lower felt; once the
     * board is out it drops onto the avatar and shrinks slightly.
     */
    .player-human-area .player-hand-display {
        transform: translateX(-50%) scale(1);
        transform-origin: center bottom;
        bottom: calc(100% + 8px);
        z-index: 3;
    }
    .player-human-area .player-hand-display .card,
    #community-cards .card {
        width: 112px;
        height: 156px;
        /* px, not em: the seat and the board inherit different parent sizes */
        font-size: 52px;
        border-width: 3px;
    }
    #community-cards {
        gap: 8px;
    }
    #table.board-out .player-human-area .player-hand-display {
        bottom: auto;
        top: 22px;
        transform: translateX(-50%) scale(0.88);
        transform-origin: center top;
        z-index: 6;
    }
    .skin-btn img {
        width: 56px;
        height: 56px;
    }
}
