/* ============================================
   RUMMY ONLINE - LAYOUT SYSTEM
   Mobile-first responsive grid & flexbox layouts
   ============================================ */

/* ==========================================
   CSS RESET & BASE
   ========================================== */

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

html {
    font-size: var(--font-size-base);
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    /* Prevent horizontal scroll */
    overflow-x: hidden;
}

body {
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    font-weight: var(--font-weight-normal);
    line-height: var(--line-height-normal);
    color: var(--text-primary);
    background-color: var(--bg-primary);
    min-height: 100vh;
    min-height: 100dvh;
    /* Dynamic viewport height for mobile */
    /* Lock horizontal scroll */
    overflow-x: hidden;
    position: relative;
    /* Smooth theme transitions */
    transition: background-color var(--transition-normal), color var(--transition-normal);
}

/* Ensure images don't overflow */
img,
svg {
    max-width: 100%;
    height: auto;
    display: block;
}

/* Remove default button styles */
button {
    font-family: inherit;
    font-size: inherit;
    cursor: pointer;
    border: none;
    background: none;
}

/* Remove list styles */
ul,
ol {
    list-style: none;
}

/* Links */
a {
    color: inherit;
    text-decoration: none;
}

/* Ensure hidden attribute works with flex/grid */
[hidden] {
    display: none !important;
}

/* ==========================================
   MAIN LAYOUT STRUCTURE
   ========================================== */

.app {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    min-height: 100dvh;
}

/* Header */
.header {
    position: sticky;
    top: 0;
    z-index: var(--z-header);
    background-color: var(--bg-primary);
    transition: background-color var(--transition-normal);
}

/* Main Game Area */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    width: 100%;
    max-width: 100%;
    overflow-x: hidden;
}

/* Game Layout Wrapper - contains sidebars and game container */
.game-wrapper {
    display: flex;
    justify-content: center;
    width: 100%;
}

/* Game Container */
.game-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: var(--space-md);
    max-width: var(--max-width-content);
    margin: 0 auto;
    width: 100%;
}

/* ==========================================
   HEADER LAYOUT
   ========================================== */

.header__content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--header-height-mobile);
    padding: 0 var(--space-md);
}

.header__logo {
    font-family: var(--font-family-display);
    font-size: var(--font-size-xl);
    font-weight: var(--font-weight-black);
    letter-spacing: var(--letter-spacing-tight);
    color: var(--text-primary);
    text-transform: uppercase;
}

.header__actions {
    display: flex;
    align-items: center;
    gap: var(--space-md);
}

/* ==========================================
   AD SLOTS LAYOUT
   ========================================== */

/* Mobile Header Ad - Sticky */
.ad-slot--header {
    width: 100%;
    min-height: var(--ad-header-height);
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--ad-bg);
    position: sticky;
    top: 0;
    z-index: var(--z-ad);
}

.ad-slot__label {
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-bold);
    color: var(--ad-text);
    text-transform: uppercase;
    letter-spacing: var(--letter-spacing-wider);
}

/* Sidebar Ads - Desktop Only */
.ad-slot--sidebar {
    display: none;
}

/* Below Content Ad */
.ad-slot--content {
    width: 100%;
    min-height: 280px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: var(--bg-secondary);
    border-radius: var(--radius-lg);
    margin: var(--space-xl) 0;
}

/* ==========================================
   GAME TABLE LAYOUT
   ========================================== */

.game-table {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    padding: var(--space-lg) clamp(var(--space-sm), 3vw, var(--space-md));
    position: relative;
    width: 100%;
    max-width: 100%;
}

/* Player Positions Grid */
.players-area {
    display: grid;
    grid-template-areas:
        ". top ."
        "left center right"
        ". bottom .";
    grid-template-columns: minmax(80px, 1fr) minmax(auto, 300px) minmax(80px, 1fr);
    grid-template-rows: auto 200px auto;
    width: 100%;
    max-width: 700px;
    gap: var(--space-md);
    align-items: center;
    justify-items: center;
    margin: 0 auto;
}

/* Player position areas */
.player--top {
    grid-area: top;
}

.player--left {
    grid-area: left;
    justify-self: center;
}

.player--right {
    grid-area: right;
    justify-self: center;
}

.player--bottom {
    grid-area: bottom;
    /* Hide during gameplay since hand-container is fixed at bottom */
    display: none;
}

.center-area {
    grid-area: center;
    width: 100%;
    max-width: 300px;
}

/* Center Area (Deck, Discard, Controls) */
.center-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: var(--space-lg);
    padding: clamp(var(--space-sm), 3vw, var(--space-lg));
}

/* Card Piles Layout */
.piles-area {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-lg);
}

/* Game Controls */
.game-controls {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-md);
}

/* Player Count Selector */
.player-selector {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

/* Opponent Hand (face-down cards) */
.opponent-hand {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 60px;
    overflow: visible;
}

.opponent-hand .card {
    width: 40px;
    height: 56px;
    margin-left: -20px;
}

.opponent-hand .card:first-child {
    margin-left: 0;
}

/* ==========================================
   PLAYER HAND LAYOUT
   ========================================== */

.hand-container {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    padding: var(--space-md);
    padding-bottom: calc(var(--space-md) + env(safe-area-inset-bottom, 0px));
    background: linear-gradient(to top, var(--bg-primary) 80%, transparent);
    z-index: var(--z-ui);
}

.hand {
    display: flex;
    justify-content: center;
    align-items: flex-end;
    min-height: calc(var(--card-height) + 40px);
    position: relative;
}

.hand__cards {
    display: flex;
    justify-content: center;
    position: relative;
}

/* Turn Indicator */
.turn-indicator {
    position: absolute;
    top: -40px;
    left: 50%;
    transform: translateX(-50%);
    padding: var(--space-xs) var(--space-md);
    background-color: var(--accent-tertiary);
    color: var(--text-inverse);
    border-radius: var(--radius-md);
    font-size: var(--font-size-sm);
    font-weight: var(--font-weight-bold);
    text-transform: uppercase;
    letter-spacing: var(--letter-spacing-wide);
    white-space: nowrap;
}

[data-theme="dark"] .turn-indicator {
    background-color: var(--bg-card);
    color: var(--text-primary);
}

/* ==========================================
   MELDS AREA
   ========================================== */

.melds-area {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: var(--space-md);
    padding: var(--space-md);
    min-height: 80px;
}

.meld {
    display: flex;
    gap: calc(var(--space-xs) * -1);
    border-radius: var(--radius-md);
    padding: 4px;
    transition: box-shadow var(--transition-normal), transform var(--transition-fast);
}

/* Valid lay-off target highlighting */
.meld--valid-target {
    cursor: pointer;
    box-shadow:
        0 0 8px 2px rgba(197, 230, 0, 0.5),
        0 0 20px 4px rgba(197, 230, 0, 0.3);
    animation: meld-pulse 1.5s ease-in-out infinite;
}

.meld--valid-target:hover {
    transform: scale(1.03);
    box-shadow:
        0 0 12px 3px rgba(255, 64, 129, 0.6),
        0 0 30px 6px rgba(255, 64, 129, 0.4);
}

@keyframes meld-pulse {

    0%,
    100% {
        box-shadow:
            0 0 8px 2px rgba(197, 230, 0, 0.4),
            0 0 16px 4px rgba(197, 230, 0, 0.2);
    }

    50% {
        box-shadow:
            0 0 12px 4px rgba(197, 230, 0, 0.6),
            0 0 24px 6px rgba(197, 230, 0, 0.35);
    }
}

/* ==========================================
   RULES SECTION LAYOUT
   ========================================== */

.rules-section {
    padding: var(--space-2xl) var(--space-md);
    background-color: var(--bg-secondary);
    margin-top: 60px;
}

.rules-section__content {
    max-width: 800px;
    margin: 0 auto;
}

.rules-section h2 {
    font-size: var(--font-size-xl);
    font-weight: var(--font-weight-bold);
    margin-bottom: var(--space-lg);
    color: var(--text-primary);
}

.rules-section h3 {
    font-size: var(--font-size-md);
    font-weight: var(--font-weight-semibold);
    margin-top: var(--space-lg);
    margin-bottom: var(--space-sm);
    color: var(--text-primary);
}

.rules-section p {
    margin-bottom: var(--space-md);
    color: var(--text-secondary);
    line-height: var(--line-height-relaxed);
}

.rules-section ul {
    margin-bottom: var(--space-md);
    padding-left: var(--space-lg);
}

.rules-section li {
    list-style: disc;
    margin-bottom: var(--space-sm);
    color: var(--text-secondary);
}

/* ==========================================
   FOOTER LAYOUT
   ========================================== */

.footer {
    padding: var(--space-xl) var(--space-md);
    background-color: var(--bg-tertiary);
    text-align: center;
}

.footer__content {
    max-width: var(--max-width-content);
    margin: 0 auto;
}

.footer__links {
    display: flex;
    justify-content: center;
    gap: var(--space-lg);
    margin-bottom: var(--space-md);
}

.footer__link {
    color: var(--text-secondary);
    font-size: var(--font-size-sm);
    transition: color var(--transition-fast);
}

.footer__link:hover {
    color: var(--accent-primary);
}

.footer__copyright {
    font-size: var(--font-size-xs);
    color: var(--text-muted);
}

/* ==========================================
   RESPONSIVE BREAKPOINTS
   ========================================== */

/* Tablet (768px+) */
@media (min-width: 768px) {
    .header__content {
        height: var(--header-height);
        padding: 0 var(--space-xl);
    }

    .header__logo {
        font-size: var(--font-size-2xl);
    }

    .game-container {
        padding: var(--space-xl);
    }

    .game-table {
        padding: var(--space-xl);
    }

    .players-area {
        max-width: 700px;
        gap: var(--space-lg);
    }

    .hand-container {
        padding: var(--space-lg);
        padding-bottom: calc(var(--space-lg) + env(safe-area-inset-bottom, 0px));
    }

    /* Larger cards on tablet */
    :root {
        --card-width: 80px;
        --card-height: 114px;
    }
}

/* Desktop (1024px+) */
@media (min-width: 1024px) {

    /* Hide header ad on desktop */
    .ad-slot--header {
        display: none;
    }

    /* Show sidebar ads */
    .game-wrapper {
        display: grid;
        grid-template-columns: 300px 1fr 300px;
        gap: var(--space-lg);
        max-width: 1400px;
        margin: 0 auto;
        padding: var(--space-lg);
    }

    .ad-slot--sidebar {
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: flex-start;
        padding-top: var(--space-xl);
        min-height: 600px;
    }

    .ad-slot--sidebar-inner {
        position: sticky;
        top: 120px;
        width: 300px;
        min-height: 250px;
        background-color: var(--bg-secondary);
        border-radius: var(--radius-lg);
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .game-container {
        max-width: none;
    }

    .players-area {
        max-width: 800px;
    }

    /* Larger cards on desktop */
    :root {
        --card-width: 90px;
        --card-height: 128px;
        --avatar-size: 80px;
    }
}

/* Large Desktop (1280px+) */
@media (min-width: 1280px) {
    .players-area {
        max-width: 900px;
    }

    .game-table {
        min-height: 600px;
    }
}

/* ==========================================
   SAFE AREA INSETS (iOS)
   ========================================== */

@supports (padding: max(0px)) {
    .hand-container {
        padding-bottom: max(var(--space-md), env(safe-area-inset-bottom));
    }

    .footer {
        padding-bottom: max(var(--space-xl), env(safe-area-inset-bottom));
    }
}

/* ==========================================
   LANDSCAPE MOBILE
   ========================================== */

@media (max-height: 500px) and (orientation: landscape) {
    .game-table {
        min-height: auto;
        padding: var(--space-sm);
    }

    .players-area {
        gap: var(--space-sm);
    }

    :root {
        --card-height: 80px;
        --card-width: 56px;
        --avatar-size: 48px;
    }

    .hand-container {
        padding: var(--space-sm);
    }
}

/* ==========================================
   SMALL MOBILE (< 400px) - Centering Fix
   ========================================== */

@media (max-width: 400px) {
    .players-area {
        /* Remove minimum column widths to allow proper centering */
        grid-template-columns: 0fr minmax(auto, 300px) 0fr;
        padding: 0 var(--space-xs);
    }

    .game-container {
        padding: var(--space-xs);
    }

    /* .game-table and .center-area use clamp() - no override needed */
}