:root {
    --bg-dark: #0f172a;
    --bg-gradient: radial-gradient(circle at 50% 50%, #1e293b 0%, #020617 100%);
    --glass-bg: rgba(30, 41, 59, 0.7);
    --glass-border: rgba(255, 255, 255, 0.1);
    --accent: #6366f1;
    --accent-hover: #4f46e5;
    --text-main: #f8fafc;
    --text-muted: #94a3b8;
    --tile-white: #f1f5f9;
    --tile-black: #020617;
}

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

body {
    font-family: 'Outfit', sans-serif;
    background: var(--bg-dark);
    color: var(--text-main);
    overflow: hidden;
    height: 100vh;
    width: 100vw;
}

.app-container {
    display: flex;
    width: 100%;
    height: 100%;
}

/* Sidebar */
.sidebar {
    width: 300px;
    background: rgba(15, 23, 42, 0.95);
    border-right: 1px solid var(--glass-border);
    display: flex;
    flex-direction: column;
    padding: 2rem;
    gap: 2rem;
    z-index: 100;
}

.logo {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: -1px;
}

.accent {
    color: var(--accent);
}

.scoreboard {
    background: rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.player-card {
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0.5rem;
    border-radius: 8px;
    position: relative;
    transition: background 0.3s;
}

.player-card.active {
    background: rgba(99, 102, 241, 0.2);
    box-shadow: 0 0 15px rgba(99, 102, 241, 0.2);
}

.avatar {
    font-size: 1.5rem;
}

.info {
    display: flex;
    flex-direction: column;
    flex: 1;
}

.label {
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
}

.score-value {
    font-size: 1.5rem;
    font-weight: 700;
}

.turn-indicator {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--accent);
    opacity: 0;
    box-shadow: 0 0 10px var(--accent);
}

.player-card.active .turn-indicator {
    opacity: 1;
}

.game-log {
    flex: 1;
    overflow-y: auto;
    font-size: 0.85rem;
    color: var(--text-muted);
    border-top: 1px solid var(--glass-border);
    padding-top: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.log-item.highlight {
    color: #4ade80;
}

/* Game Area */
.game-area {
    flex: 1;
    position: relative;
    overflow: hidden;
    background: var(--bg-gradient);
}

/* Hands - ABSOLUTE POSITIONING FOR STABILITY */
.hand-container {
    position: absolute;
    left: 0;
    width: 100%;
    z-index: 200;
    /* Boosted */
    display: flex;
    justify-content: center;
    align-items: center;
    background: linear-gradient(to bottom, rgba(15, 23, 42, 0.9), transparent);
    /* Removed pointer-events: none to ensure clicks work */
}

.top-hand {
    top: 0;
    height: 120px;
    border-bottom: 1px solid var(--glass-border);
}

.bottom-hand {
    bottom: 0;
    height: 150px;
    background: linear-gradient(to top, rgba(15, 23, 42, 0.95), rgba(15, 23, 42, 0.4));
    border-top: 1px solid var(--glass-border);
}

.hand-wrapper {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    max-width: 90%;
    padding: 10px;
    height: 100%;
    align-items: center;
    position: relative;
    z-index: 201;
    /* Higher than container */
}

/* Scrollbar styling */
.hand-wrapper::-webkit-scrollbar {
    height: 6px;
}

.hand-wrapper::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.2);
    border-radius: 3px;
}

/* Tiles */
.domino-tile {
    width: 44px;
    height: 88px;
    background: var(--tile-white);
    border-radius: 4px;
    box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.5);
    display: flex;
    flex-direction: column;
    flex-shrink: 0;
    /* NO SHRINKING */
    position: relative;
    cursor: grab;
    transition: transform 0.2s;
    z-index: 10 !important;
    /* Base z-index */
}

.domino-tile.playable {
    cursor: pointer;
    box-shadow: 0 0 20px #4ade80, 0 0 40px rgba(74, 222, 128, 0.2);
    transform: translateY(-8px);
    border: 3px solid #4ade80 !important;
    animation: playable-pulse 2s infinite ease-in-out;
}

@keyframes playable-pulse {
    0% {
        box-shadow: 0 0 15px rgba(74, 222, 128, 0.6);
        transform: translateY(-8px);
    }

    50% {
        box-shadow: 0 0 30px rgba(74, 222, 128, 1);
        transform: translateY(-12px);
    }

    100% {
        box-shadow: 0 0 15px rgba(74, 222, 128, 0.6);
        transform: translateY(-8px);
    }
}

.domino-tile.playable:hover {
    transform: translateY(-15px) scale(1.1);
    box-shadow: 0 0 40px #4ade80;
}

.domino-tile.horizontal {
    width: 88px;
    height: 44px;
    flex-direction: row;
}

.domino-tile:hover {
    transform: translateY(-5px);
}

.domino-tile.in-hand:hover {
    z-index: 100;
    transform: scale(1.1) translateY(-10px);
}

.tile-back {
    width: 30px;
    height: 60px;
    background: #334155;
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 4px;
    flex-shrink: 0;
}

.line-devider {
    background: #cbd5e1;
}

.domino-tile.vertical .line-devider {
    width: 90%;
    height: 2px;
    margin: 0 auto;
}

.domino-tile.horizontal .line-devider {
    height: 90%;
    width: 2px;
    margin: auto 0;
}

.half {
    flex: 1;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    padding: 4px;
    gap: 2px;
}

.pip {
    width: 7px;
    height: 7px;
    background: var(--tile-black);
    border-radius: 50%;
    box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.8);
}

/* Pip Placement Classes (Grid Areas 1-9) */
.p1 {
    grid-area: 1/1/2/2;
}

.p2 {
    grid-area: 1/2/2/3;
}

.p3 {
    grid-area: 1/3/2/4;
}

.p4 {
    grid-area: 2/1/3/2;
}

.p5 {
    grid-area: 2/2/3/3;
}

.p6 {
    grid-area: 2/3/3/4;
}

.p7 {
    grid-area: 3/1/4/2;
}

.p8 {
    grid-area: 3/2/4/3;
}

.p9 {
    grid-area: 3/3/4/4;
}

/* Viewport & Board */
.board-viewport {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 10;
    overflow: hidden;
    cursor: move;
    /* Hints panning */
}

/* Side Drop Targets */
.side-target {
    position: absolute;
    border: 3px dashed rgba(74, 222, 128, 0.4);
    background: rgba(74, 222, 128, 0.05);
    border-radius: 20px;
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    text-align: center;
    color: white;
    font-weight: bold;
    font-size: 0.8rem;
    pointer-events: auto;
    transition: all 0.3s;
    backdrop-filter: blur(4px);
}

.side-target:hover {
    background: rgba(74, 222, 128, 0.15);
    border-color: #4ade80;
    transform: scale(1.05);
}

.side-target.left,
.side-target.right {
    top: 50%;
    transform: translateY(-50%);
    width: 140px;
    height: 300px;
}

.side-target.left {
    left: 40px;
}

.side-target.right {
    right: 160px;
}

.side-target.top,
.side-target.bottom {
    left: 50%;
    transform: translateX(-50%);
    width: 300px;
    height: 140px;
}

.side-target.top {
    top: 120px;
}

.side-target.bottom {
    bottom: 150px;
}


.board-surface {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 0;
    height: 0;
    pointer-events: none;
    /* Allow panning from background */
}

.board-container-2d {
    position: relative;
    width: 0;
    height: 0;
    pointer-events: auto;
    /* Re-enable for tiles */
}


.spinner-glow {
    box-shadow: 0 0 15px #6366f1, 0 0 30px rgba(99, 102, 241, 0.4) !important;
    border: 2px solid #6366f1 !important;
    z-index: 20;
}

/* Base styles for tiles in hand were already defined, 
   but board tiles need position absolute (handled in JS) */


/* Boneyard (Draw Pile) */
.boneyard {
    position: absolute;
    right: 30px;
    top: 50%;
    transform: translateY(-50%);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    background: var(--glass-bg);
    padding: 20px;
    border-radius: 20px;
    border: 1px solid var(--glass-border);
    backdrop-filter: blur(10px);
    z-index: 500;
    /* Ensure it's above the board panner */
    cursor: pointer;
    transition: all 0.3s;
    pointer-events: auto !important;
    /* CRITICAL FIX */
}

.boneyard.hidden {
    display: none;
}

.boneyard:hover {
    background: rgba(255, 255, 255, 0.1);
    transform: translateY(-50%) scale(1.05);
}

.boneyard.pulse-highlight {
    animation: pulse-glow 1.5s infinite;
    border-color: #4ade80;
    box-shadow: 0 0 20px rgba(74, 222, 128, 0.4);
}

.boneyard.disabled {
    opacity: 0.5;
    pointer-events: none;
}

.boneyard-stack {
    position: relative;
    width: 40px;
    height: 70px;
}

.stack-1 {
    position: absolute;
    top: 0;
    left: 0;
}

.stack-2 {
    position: absolute;
    top: 3px;
    left: 3px;
}

.stack-3 {
    position: absolute;
    top: 6px;
    left: 6px;
    background: #475569;
}

.boneyard-info {
    margin-top: 10px;
    background: rgba(0, 0, 0, 0.6);
    padding: 4px 8px;
    border-radius: 12px;
    text-align: center;
}

.boneyard-info .label {
    display: block;
    font-size: 0.6rem;
}

.boneyard-info .count {
    font-size: 1rem;
    font-weight: bold;
}

/* Buttons */
.btn {
    padding: 0.8rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-weight: 600;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.1s;
}

.btn:hover {
    transform: translateY(-2px);
}

.btn.primary {
    background: var(--accent);
    color: white;
    box-shadow: 0 4px 10px rgba(99, 102, 241, 0.4);
}

.btn.secondary {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border: 1px solid var(--glass-border);
    padding: 0.8rem 1.2rem;
}

.secondary:hover {
    background: rgba(255, 255, 255, 0.2);
}

.action-area {
    display: flex;
    flex-direction: column;
    gap: 0.8rem;
    margin-top: auto;
}

.btn {
    width: 100%;
    padding: 1rem;
    border-radius: 12px;
    font-weight: 600;
    font-size: 0.9rem;
}

@media (max-width: 900px) {
    .action-area {
        flex-direction: row;
        width: auto;
        gap: 0.5rem;
        margin-top: 0;
    }

    .btn {
        padding: 0.5rem 0.8rem;
        font-size: 0.75rem;
        width: auto;
    }
}

.btn.warning {
    background: #f59e0b;
    color: #020617;
}

.floating-pass {
    position: absolute;
    top: -50px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 100;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.5);
}

.hidden {
    display: none !important;
}

/* Score Float Animation */
.score-float {
    position: absolute;
    top: 40%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 4rem;
    font-weight: 800;
    color: #4ade80;
    text-shadow: 0 0 20px rgba(74, 222, 128, 0.5);
    z-index: 200;
    pointer-events: none;
    animation: floatUp 1.5s forwards ease-out;
}

@keyframes floatUp {
    0% {
        opacity: 0;
        transform: translate(-50%, 0) scale(0.5);
    }

    20% {
        opacity: 1;
        transform: translate(-50%, -50px) scale(1.2);
    }

    100% {
        opacity: 0;
        transform: translate(-50%, -150px) scale(1);
    }
}

/* Modals */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal {
    background: #1e293b;
    padding: 2rem;
    border-radius: 16px;
    border: 1px solid var(--glass-border);
    text-align: center;
    min-width: 300px;
}

.modal h2 {
    font-size: 2rem;
    margin-bottom: 1rem;
    color: var(--accent);
}

.modal p {
    color: var(--text-muted);
    margin-bottom: 2rem;
}

@media (max-width: 900px) {
    .app-container {
        flex-direction: column;
    }

    .sidebar {
        width: 100%;
        height: auto;
        padding: 0.8rem;
        border-right: none;
        border-bottom: 1px solid var(--glass-border);
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
        gap: 0.5rem;
        z-index: 1000;
        /* Ensure it's above everything */
    }

    .logo {
        display: none;
    }

    .scoreboard {
        flex-direction: row;
        width: auto;
        flex: 1;
        gap: 0.5rem;
        padding: 0.3rem;
    }

    .player-card {
        padding: 0.2rem 0.4rem;
        gap: 0.4rem;
    }

    .avatar {
        font-size: 1rem;
    }

    .score-value {
        font-size: 1.1rem;
    }

    .game-log {
        display: none;
    }

    .action-area {
        width: auto;
        flex-direction: row;
        gap: 0.4rem;
        z-index: 1001;
    }

    .btn.primary {
        padding: 0.5rem 0.8rem;
        font-size: 0.75rem;
    }

    .game-area {
        height: calc(100% - 70px);
    }

    .hand-container {
        padding: 0.4rem;
        min-height: 80px;
        overflow-x: auto;
        /* Enable scrolling if hand is too wide */
        -webkit-overflow-scrolling: touch;
    }


    .hand-wrapper {
        gap: 2px;
        justify-content: center;
    }

    /* Fixed rendering issues by removing zoom and using absolute positioning calculators in JS */

    .boneyard {
        right: 10px;
        padding: 8px;
        gap: 4px;
        transform: translateY(-50%) scale(0.8);
    }


    .side-target {
        width: 40px !important;
        height: 120px !important;
        font-size: 0.5rem;
    }

    .side-target.top,
    .side-target.bottom {
        width: 120px !important;
        height: 40px !important;
    }

    .side-target.left {
        left: 10px;
    }

    .side-target.right {
        right: 80px;
    }

    .side-target.top {
        top: 100px;
    }

    .side-target.bottom {
        bottom: 120px;
    }
}

@media (max-width: 600px) {
    .sidebar {
        padding: 0.4rem;
    }

    .player-card .label {
        display: none;
    }

    .badge-cpu {
        font-size: 0.6rem;
        right: 5px;
    }
}