/* ===== CSS VARIABLES ===== */
:root { 
    --primary: #4f46e5; 
    --secondary: #e11d48; 
    --bg: #f8fafc; 
    --surface: #ffffff; 
    --text: #1e293b; 
    --border: #e2e8f0; 
}

/* Global box-sizing reset for consistent sizing */
*, *::before, *::after {
    box-sizing: border-box;
}

/* Prevent horizontal overflow */
html {
    overflow-x: hidden;
}

body.dark-mode { 
    --primary: #818cf8; 
    --secondary: #fb7185; 
    --bg: #1e293b; 
    --surface: #334155; 
    --text: #f1f5f9; 
    --border: #475569; 
}

/* ===== BASE STYLES ===== */
body { 
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
    background-color: var(--bg); 
    color: var(--text); 
    display: flex; 
    flex-direction: column; 
    align-items: center; 
    justify-content: center; 
    min-height: 100vh; 
    margin: 0; 
    padding: 0;
    box-sizing: border-box;
    overflow-x: auto;
    transition: background-color 0.3s, color 0.3s; 
}

.container { 
    width: 100%; 
    max-width: 400px; 
    min-width: 300px;
    padding: 20px; 
    text-align: center;
    margin: 0 auto;
    box-sizing: border-box;
}

.card { 
    background: var(--surface); 
    padding: 2rem; 
    border-radius: 1rem; 
    box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1); 
    transition: background-color 0.3s;
    box-sizing: border-box; 
    width: 100%;
}

/* ===== TYPOGRAPHY ===== */
h1 { 
    margin-bottom: 0.5rem;
    font-size: clamp(1.8rem, 4vw, 2.5rem);
}

h2 { 
    font-size: clamp(1.4rem, 3.5vw, 2rem);
}

.subtitle { 
    color: #64748b; 
    margin-bottom: 2rem;
    font-size: clamp(0.9rem, 2.5vw, 1rem);
}

/* ===== BUTTONS ===== */
button { 
    width: 100%; 
    padding: 1rem; 
    margin-bottom: 1rem; 
    border: none; 
    border-radius: 0.75rem; 
    font-size: 1rem; 
    font-weight: 600; 
    cursor: pointer; 
    transition: transform 0.1s, background-color 0.3s, color 0.3s; 
}

button:active { 
    transform: scale(0.98); 
}

button:disabled { 
    opacity: 0.5; 
    cursor: not-allowed; 
}

.btn-primary { 
    background: var(--primary); 
    color: white; 
}

.btn-secondary { 
    background: transparent; 
    border: 2px solid var(--border); 
    color: var(--text); 
}

.btn-danger { 
    background: #334155; 
    color: white; 
    width: auto; 
    padding: 0.75rem 1.5rem; 
    margin-top: 1rem; 
}

/* ===== INPUTS ===== */
input { 
    width: 100%; 
    padding: 1rem; 
    font-size: 1.5rem; 
    text-align: center; 
    border: 2px solid var(--border); 
    border-radius: 0.75rem; 
    margin-bottom: 1rem; 
    font-family: monospace; 
    box-sizing: border-box; 
    background-color: var(--surface); 
    color: var(--text); 
    transition: background-color 0.3s, color 0.3s, border-color 0.3s; 
}

/* ===== GAME BOARD ===== */
.board { 
    display: grid; 
    grid-template-columns: repeat(3, 1fr); 
    gap: 0.75rem; 
    background: var(--border); 
    padding: 0.75rem; 
    border-radius: 1rem; 
    margin: 1.5rem auto;
    width: 100%; 
    max-width: 280px;
    grid-auto-rows: 1fr;
    box-sizing: border-box;
}

.cell { 
    background: var(--surface); 
    aspect-ratio: 1; 
    border: none; 
    border-radius: 0.5rem; 
    font-size: 3rem; 
    font-weight: bold; 
    cursor: pointer; 
    display: flex; 
    align-items: center; 
    justify-content: center; 
    transition: background-color 0.3s;
    width: 100%; 
    height: auto; 
    aspect-ratio: 1 / 1;
    box-sizing: border-box;
    min-width: 0;
    min-height: 60px;
}

.cell.X { 
    color: var(--primary); 
}

.cell.O { 
    color: var(--secondary); 
}

/* ===== STATUS & BADGES ===== */
.status-badge { 
    display: inline-block; 
    padding: 0.5rem 1.5rem; 
    background: #e0e7ff; 
    color: var(--primary); 
    border-radius: 999px; 
    font-weight: 600; 
    margin: 0 auto 1rem auto;
    text-align: center;
    max-width: fit-content;
}

body.dark-mode .status-badge { 
    background: #4338ca; 
    color: #e0e7ff; 
}

/* ===== UTILITY CLASSES ===== */
.hidden { 
    display: none; 
}

.error { 
    color: var(--secondary); 
    margin-bottom: 1rem; 
    font-size: 0.9rem; 
}

.code-display { 
    font-family: monospace; 
    font-size: 2rem; 
    font-weight: bold; 
    margin: 1rem 0; 
    letter-spacing: 2px; 
}

/* ===== DEBUG LOG ===== */
#debug-log { 
    display: none; 
}

.log-entry { 
    margin-bottom: 4px; 
    border-bottom: 1px solid #333; 
    padding-bottom: 2px; 
}

.log-error { 
    color: #ff4444; 
} 

.log-warn { 
    color: #ffbb33; 
}

/* ===== CONFIG MODAL ===== */
#config-modal { 
    position: fixed; 
    top: 0; 
    left: 0; 
    width: 100%; 
    height: 100%; 
    background: rgba(0,0,0,0.9); 
    display: none; 
    flex-direction: column; 
    align-items: center; 
    justify-content: center; 
    z-index: 1000; 
    color: white; 
    text-align: center; 
    padding: 20px; 
}

#config-modal h2 { 
    color: #ff4444; 
}

#config-modal p { 
    max-width: 500px; 
    line-height: 1.5; 
    margin-bottom: 20px; 
}

.code-block { 
    background: #333; 
    padding: 10px; 
    border-radius: 5px; 
    font-family: monospace; 
    text-align: left; 
    width: 100%; 
    max-width: 500px; 
    overflow-x: auto; 
}

/* ===== ROOM LIST ===== */
#room-list-container {
    margin-top: 2rem;
}

#room-list {
    max-height: 150px;
    overflow-y: auto;
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    padding: 0.5rem;
}

.room-item { 
    display: flex; 
    justify-content: space-between; 
    align-items: center; 
    padding: 0.75rem; 
    border-bottom: 1px solid var(--border); 
    cursor: pointer; 
}

.room-item:hover { 
    background-color: var(--bg); 
}

.room-item-name { 
    font-weight: 600; 
}

.room-item-players { 
    font-size: 0.9rem; 
    color: #64748b; 
}

/* ===== GAME UI ELEMENTS ===== */
.player-indicators {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 0.9rem;
    color: #64748b;
    margin: 1rem auto;
    max-width: 280px;
    text-align: center;
}

.player-scores {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 1.2rem;
    font-weight: bold;
    margin: 0.5rem auto;
    max-width: 280px;
    text-align: center;
}

.game-controls {
    display: flex;
    justify-content: center;
    gap: 1rem;
    align-items: center;
    margin: 1rem auto;
    max-width: 300px;
    box-sizing: border-box;
}

#rematch-status {
    margin-top: 1rem;
    color: #64748b;
    font-size: 0.9rem;
    text-align: center;
    max-width: 280px;
    margin-left: auto;
    margin-right: auto;
}

.room-info {
    margin: 1rem auto 0;
    font-size: 0.75rem;
    color: #64748b;
    text-align: center;
    max-width: 280px;
}

/* ===== THEME TOGGLE ===== */
.theme-toggle-container {
    position: fixed;
    top: clamp(0.5rem, 2vw, 1rem);
    right: clamp(0.5rem, 2vw, 1rem);
    z-index: 1001;
}

#theme-toggle {
    width: auto;
    padding: clamp(0.4rem, 1vw, 0.6rem);
    border: none;
    background: var(--surface);
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    cursor: pointer;
    font-size: clamp(1.2rem, 3vw, 1.6rem);
    transition: all 0.3s ease;
}

#theme-toggle:hover {
    transform: scale(1.1);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

/* ===== RESPONSIVE DESIGN ===== */

/* Large screens and desktops (1200px+) */
@media (min-width: 1200px) {
    .container { max-width: 480px; padding: 24px; }
    .card { padding: 2.5rem; }
    .board { gap: 1rem; padding: 1rem; max-width: 360px; margin: 2rem auto; }
    .cell { font-size: 3.5rem; }
    button { padding: 1.2rem; font-size: 1.1rem; }
    .code-display { font-size: 2.5rem; }
    #room-list { max-height: 200px; }
}

/* Laptops and medium screens (992px - 1199px) */
@media (max-width: 1199px) and (min-width: 992px) {
    .container { max-width: 450px; padding: 22px; }
    .card { padding: 2.2rem; }
    .board { gap: 0.9rem; padding: 0.9rem; }
    .cell { font-size: 3.2rem; }
    button { padding: 1.1rem; font-size: 1.05rem; }
}

/* Tablets and small laptops (769px - 991px) */
@media (max-width: 991px) and (min-width: 769px) {
    .container { max-width: 420px; padding: 18px; }
    .card { padding: 1.8rem; border-radius: 1rem; }
    .board { gap: 0.8rem; padding: 0.8rem; }
    .cell { font-size: clamp(2.5rem, 5vw, 3rem); }
    button { padding: 1rem; font-size: 1rem; }
    .code-display { font-size: 2.2rem; }
    #room-list { max-height: 180px; }
}

/* Tablets portrait and small screens (577px - 768px) */
@media (max-width: 768px) and (min-width: 577px) {
    .container { max-width: 400px; padding: 16px; }
    .card { padding: 1.5rem; border-radius: 0.9rem; }
    .board { gap: 0.7rem; padding: 0.7rem; }
    .cell { font-size: clamp(2.2rem, 6vw, 2.8rem); border-radius: 0.6rem; }
    button { padding: 0.9rem; font-size: 0.95rem; }
    .code-display { font-size: 1.8rem; }
    #room-list { max-height: 160px; }
    .room-item { padding: 0.6rem; }
    .room-item-name { font-size: 0.95rem; }
    .room-item-players { font-size: 0.85rem; }
}

/* Large phones landscape and small tablets (481px - 576px) */
@media (max-width: 576px) and (min-width: 481px) {
    .container { padding: 14px; max-width: 95%; min-width: 300px; margin: 0 auto; box-sizing: border-box; }
    .card { padding: 1.3rem; border-radius: 0.8rem; box-sizing: border-box; }
    .board { gap: 0.6rem; padding: 0.6rem; margin: 1.5rem auto; max-width: 280px; box-sizing: border-box; }
    .cell { font-size: clamp(1.8rem, 7vw, 2.4rem); border-radius: 0.5rem; box-sizing: border-box; min-width: 0; }
    button { padding: 0.8rem; font-size: 0.9rem; }
    .code-display { font-size: 1.6rem; letter-spacing: 1px; }
    .status-badge { padding: 0.4rem 1.2rem; font-size: 0.9rem; }
    #room-list { max-height: 140px; }
    .room-item { padding: 0.55rem; }
    input { padding: 0.9rem; font-size: 1.3rem; }
}

/* Phones portrait (320px - 480px) */
@media (max-width: 480px) {
    body { padding: 0; }
    .container { 
        padding: clamp(8px, 3vw, 12px); 
        max-width: calc(100vw - 16px);
        min-width: 300px;
        margin: 0 auto;
        min-height: 100vh;
        display: flex;
        flex-direction: column;
        justify-content: center;
        box-sizing: border-box;
    }
    .card { 
        padding: clamp(0.8rem, 4vw, 1.2rem); 
        border-radius: 0.7rem;
        margin-bottom: 1rem;
        box-sizing: border-box;
    }
    .board { 
        gap: clamp(0.3rem, 2vw, 0.5rem); 
        padding: clamp(0.3rem, 2vw, 0.5rem);
        margin: 1rem auto;
        max-width: min(280px, calc(100vw - 40px));
        width: 100%;
        box-sizing: border-box;
    }
    .cell { 
        font-size: clamp(1.5rem, 8vw, 2.2rem); 
        border-radius: 0.4rem;
        min-height: clamp(60px, 15vw, 80px);
        max-width: calc((100vw - 64px) / 3);
        box-sizing: border-box;
    }
    button { 
        padding: clamp(0.7rem, 3vw, 0.9rem); 
        font-size: clamp(0.85rem, 3.5vw, 0.95rem);
        margin-bottom: 0.8rem;
        min-height: 44px; /* Accessibility: minimum touch target */
    }
    .code-display { 
        font-size: clamp(1rem, 5vw, 1.4rem); 
        letter-spacing: 0.5px;
        word-break: break-all;
    }
    .status-badge { 
        padding: clamp(0.3rem, 2vw, 0.4rem) clamp(0.8rem, 4vw, 1rem); 
        font-size: clamp(0.8rem, 3vw, 0.9rem);
    }
    #room-list { 
        max-height: 120px; 
        font-size: 0.9rem;
    }
    .room-item { 
        padding: clamp(0.4rem, 2vw, 0.6rem);
        flex-direction: column;
        align-items: flex-start;
        gap: 0.2rem;
    }
    .room-item-name { font-size: clamp(0.85rem, 3vw, 0.9rem); }
    .room-item-players { font-size: clamp(0.75rem, 2.5vw, 0.8rem); }
    input { 
        padding: clamp(0.8rem, 3vw, 1rem); 
        font-size: clamp(1.1rem, 4vw, 1.4rem);
    }
    
    /* Game-specific responsive elements */
    .player-indicators {
        display: flex;
        justify-content: space-between;
        font-size: clamp(0.75rem, 3vw, 0.9rem);
        margin-top: 0.8rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }
    
    .player-scores {
        display: flex;
        justify-content: space-between;
        font-size: clamp(1rem, 4vw, 1.2rem);
        font-weight: bold;
        margin-top: 0.5rem;
    }
    
    .game-controls {
        flex-direction: column;
        gap: 0.8rem;
    }
    
    .game-controls button {
        width: 100%;
    }
    
    #rematch-status {
        margin-top: 0.5rem !important;
        font-size: clamp(0.8rem, 3vw, 0.9rem);
        text-align: center;
    }
    
    /* Config modal responsive */
    #config-modal {
        padding: clamp(12px, 4vw, 20px);
    }
    
    #config-modal p {
        max-width: 100%;
        font-size: clamp(0.85rem, 3.5vw, 1rem);
        line-height: 1.4;
    }
    
    #config-modal h2 {
        font-size: clamp(1.2rem, 5vw, 1.8rem);
    }
}

/* Ultra-small screens (below 320px) */
@media (max-width: 319px) {
    .container { padding: 6px; min-width: 300px; margin: 0 auto; box-sizing: border-box; }
    .card { padding: 0.7rem; box-sizing: border-box; }
    .board { gap: 0.25rem; padding: 0.25rem; margin: 1rem auto; max-width: 260px; box-sizing: border-box; }
    .cell { font-size: 1.2rem; min-height: 50px; box-sizing: border-box; min-width: 0; }
    button { padding: 0.6rem; font-size: 0.8rem; }
    .code-display { font-size: 0.9rem; }
    h1 { font-size: 1.4rem; }
    h2 { font-size: 1.1rem; }
}

/* Landscape orientation optimizations */
@media (max-height: 500px) and (orientation: landscape) {
    body { padding: 0; }
    .container {
        padding: 8px;
        justify-content: flex-start;
        padding-top: 2rem;
    }
    .card { padding: 1rem; margin-bottom: 0.5rem; }
    .board { margin: 0.8rem 0; }
    h1, h2 { margin-bottom: 0.3rem; }
    .status-badge { margin-bottom: 0.5rem; }
    .theme-toggle-container {
        top: 0.5rem;
        right: 0.5rem;
    }
}

/* High DPI / Retina display optimizations */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .cell {
        border: 0.5px solid var(--border);
    }
    
    button {
        border: 0.5px solid transparent;
    }
}

/* Reduce motion for users who prefer it */
@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    #theme-toggle:hover {
        transform: none;
    }
    
    button:active {
        transform: none;
    }
}