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

:root {
    --primary-color: #FF6B6B;
    --secondary-color: #4ECDC4;
    --accent-color: #FFE66D;
    --dark-bg: #1A1A2E;
    --light-bg: #16213E;
    --text-light: #E8E8E8;
    --text-dark: #0F3460;
    --success: #51CF66;
    --danger: #FF6B6B;
}

/* Light Theme */
body.light-theme {
    --dark-bg: #F0F0F0;
    --light-bg: #FFFFFF;
    --text-light: #2C2C2C;
    --text-dark: #666666;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, var(--dark-bg) 0%, var(--light-bg) 100%);
    min-height: 100vh;
    color: var(--text-light);
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    transition: all 0.3s ease;
}

.container {
    max-width: 600px;
    width: 100%;
}

header {
    text-align: center;
    margin-bottom: 30px;
}

h1 {
    font-size: 2.5rem;
    color: var(--accent-color);
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);
    margin-bottom: 15px;
}

.header-controls {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.connection-status {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    background: rgba(255, 255, 255, 0.1);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 0.9rem;
}

.icon-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    transition: all 0.3s;
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

.icon-btn.muted .sound-icon {
    opacity: 0.5;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--danger);
    animation: pulse 2s infinite;
}

.status-dot.connected {
    background: var(--success);
    animation: none;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Screen Management */
.screen {
    background: rgba(255, 255, 255, 0.05);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    padding: 40px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.screen.hidden {
    display: none;
}

/* Lobby Screen */
.lobby-content {
    text-align: center;
}

.lobby-content h2 {
    color: var(--secondary-color);
    margin-bottom: 30px;
    font-size: 2rem;
}

.lobby-options {
    display: flex;
    flex-direction: column;
    gap: 20px;
    margin-bottom: 30px;
}

.divider {
    color: var(--text-light);
    opacity: 0.5;
    font-size: 0.9rem;
}

.join-game {
    display: flex;
    gap: 10px;
}

.player-name-section {
    margin-top: 30px;
    text-align: left;
}

.player-name-section label {
    display: block;
    margin-bottom: 8px;
    color: var(--text-light);
    font-size: 0.95rem;
}

input[type="text"] {
    width: 100%;
    padding: 12px 16px;
    border: 2px solid rgba(255, 255, 255, 0.2);
    background: rgba(255, 255, 255, 0.1);
    border-radius: 10px;
    color: var(--text-light);
    font-size: 1rem;
    transition: all 0.3s;
}

input[type="text"]:focus {
    outline: none;
    border-color: var(--secondary-color);
    background: rgba(255, 255, 255, 0.15);
}

input[type="text"]::placeholder {
    color: rgba(232, 232, 232, 0.5);
}

/* Buttons */
.btn {
    padding: 14px 28px;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.btn-primary {
    background: linear-gradient(135deg, var(--primary-color), #FF8E8E);
    color: var(--text-light);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(255, 107, 107, 0.4);
}

.btn-secondary {
    background: linear-gradient(135deg, var(--secondary-color), #6EDDD5);
    color: var(--text-dark);
}

.btn-secondary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 20px rgba(78, 205, 196, 0.4);
}

.btn-small {
    padding: 8px 16px;
    font-size: 0.85rem;
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

/* Waiting Screen */
.waiting-content {
    text-align: center;
}

.waiting-content h2 {
    color: var(--secondary-color);
    margin-bottom: 30px;
}

.game-code-display {
    margin: 30px 0;
}

.game-code-display p {
    margin-bottom: 10px;
    opacity: 0.8;
}

.code-box {
    background: rgba(255, 255, 255, 0.15);
    padding: 20px;
    border-radius: 10px;
    font-size: 2rem;
    font-weight: bold;
    letter-spacing: 4px;
    color: var(--accent-color);
    margin-bottom: 15px;
}

.loading-animation {
    display: flex;
    justify-content: center;
    gap: 10px;
    margin: 30px 0;
}

.dot {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--secondary-color);
    animation: bounce 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) { animation-delay: -0.32s; }
.dot:nth-child(2) { animation-delay: -0.16s; }

@keyframes bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* Game Screen */
.game-info {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 30px;
    gap: 20px;
}

.player-info {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px;
    background: rgba(255, 255, 255, 0.05);
    border-radius: 12px;
    flex: 1;
    transition: all 0.3s;
}

.player-info.active {
    background: rgba(255, 230, 109, 0.2);
    border: 2px solid var(--accent-color);
    animation: pulse-border 2s infinite;
}

@keyframes pulse-border {
    0%, 100% { box-shadow: 0 0 0 0 rgba(255, 230, 109, 0.4); }
    50% { box-shadow: 0 0 0 10px rgba(255, 230, 109, 0); }
}

.player-avatar {
    width: 50px;
    height: 50px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2rem;
    background: rgba(255, 255, 255, 0.1);
    cursor: pointer;
    transition: transform 0.3s;
}

.player-avatar:hover {
    transform: scale(1.1) rotate(10deg);
}

.player-symbol {
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.8rem;
    font-weight: bold;
    border-radius: 10px;
}

#player1Info .player-symbol {
    background: var(--primary-color);
    color: var(--text-light);
}

#player2Info .player-symbol {
    background: var(--secondary-color);
    color: var(--text-dark);
}

.player-details {
    flex: 1;
}

.player-name {
    font-weight: 600;
    font-size: 0.95rem;
    margin-bottom: 4px;
}

.player-score {
    font-size: 1.2rem;
    font-weight: bold;
    color: var(--accent-color);
}

.game-status {
    text-align: center;
    font-size: 1.1rem;
    font-weight: 600;
    color: var(--accent-color);
    padding: 10px 20px;
    background: rgba(255, 230, 109, 0.1);
    border-radius: 10px;
}

/* Game Board */
.game-board {
    position: relative;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    max-width: 400px;
    margin: 0 auto 30px;
    aspect-ratio: 1;
}

.cell {
    background: rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
    position: relative;
}

.cell:hover:not(.taken) {
    background: rgba(255, 255, 255, 0.15);
    transform: scale(1.05);
}

.cell.taken {
    cursor: not-allowed;
}

.cell.x {
    color: var(--primary-color);
    animation: popIn 0.3s ease-out;
}

.cell.o {
    color: var(--secondary-color);
    animation: popIn 0.3s ease-out;
}

@keyframes popIn {
    0% { transform: scale(0) rotate(0deg); }
    50% { transform: scale(1.3) rotate(180deg); }
    100% { transform: scale(1) rotate(360deg); }
}

.winning-line {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.3s;
}

.winning-line.show {
    opacity: 1;
    animation: drawLine 0.5s ease-out;
}

@keyframes drawLine {
    from { stroke-dasharray: 1000; stroke-dashoffset: 1000; }
    to { stroke-dasharray: 1000; stroke-dashoffset: 0; }
}

.game-controls {
    text-align: center;
    display: flex;
    gap: 10px;
    justify-content: center;
}

/* Chat Feature */
.chat-container {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 100;
}

.chat-toggle {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary-color), #FF8E8E);
    border: none;
    font-size: 1.8rem;
    cursor: pointer;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    transition: all 0.3s;
}

.chat-toggle:hover {
    transform: scale(1.1);
}

.chat-box {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 300px;
    height: 400px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 15px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.3);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.light-theme .chat-box {
    background: rgba(255, 255, 255, 0.98);
}

.chat-header {
    background: linear-gradient(135deg, var(--secondary-color), #6EDDD5);
    color: var(--text-dark);
    padding: 15px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-weight: 600;
}

.chat-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-dark);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.chat-message {
    padding: 8px 12px;
    border-radius: 10px;
    max-width: 80%;
    word-wrap: break-word;
    animation: slideIn 0.3s ease-out;
}

@keyframes slideIn {
    from { transform: translateY(10px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

.chat-message.own {
    background: var(--secondary-color);
    color: var(--text-dark);
    align-self: flex-end;
}

.chat-message.other {
    background: rgba(0, 0, 0, 0.1);
    color: var(--text-dark);
    align-self: flex-start;
}

.chat-message .sender {
    font-size: 0.75rem;
    opacity: 0.7;
    margin-bottom: 2px;
    font-weight: 600;
}

.chat-input-container {
    display: flex;
    gap: 8px;
    padding: 15px;
    background: rgba(0, 0, 0, 0.05);
}

.chat-input-container input {
    flex: 1;
    padding: 10px;
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.9);
    color: var(--text-dark);
}

.chat-input-container button {
    padding: 10px 20px;
    background: var(--secondary-color);
    border: none;
    border-radius: 8px;
    color: var(--text-dark);
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.chat-input-container button:hover {
    transform: translateY(-2px);
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: linear-gradient(135deg, var(--dark-bg), var(--light-bg));
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    max-width: 400px;
    width: 90%;
    box-shadow: 0 10px 50px rgba(0, 0, 0, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.1);
    animation: modalSlideIn 0.3s ease-out;
}

@keyframes modalSlideIn {
    from { transform: translateY(-50px); opacity: 0; }
    to { transform: translateY(0); opacity: 1; }
}

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

.modal-content p {
    margin-bottom: 30px;
    opacity: 0.9;
}

.modal-actions {
    display: flex;
    gap: 15px;
    justify-content: center;
}

/* Game History */
.history-content {
    max-width: 600px;
    max-height: 80vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.modal-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--text-light);
    transition: transform 0.3s;
}

.modal-close:hover {
    transform: rotate(90deg);
}

.history-stats {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    gap: 15px;
    margin-bottom: 30px;
}

.stat-card {
    background: rgba(255, 255, 255, 0.05);
    padding: 20px;
    border-radius: 12px;
    text-align: center;
}

.stat-value {
    font-size: 2rem;
    font-weight: bold;
    color: var(--accent-color);
    margin-bottom: 5px;
}

.stat-label {
    font-size: 0.9rem;
    opacity: 0.8;
}

.history-list {
    max-height: 300px;
    overflow-y: auto;
    margin-bottom: 20px;
}

.history-item {
    background: rgba(255, 255, 255, 0.05);
    padding: 15px;
    border-radius: 10px;
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    text-align: left;
}

.history-result {
    font-weight: 600;
    padding: 5px 10px;
    border-radius: 5px;
}

.history-result.win {
    background: var(--success);
    color: white;
}

.history-result.loss {
    background: var(--danger);
    color: white;
}

.history-result.draw {
    background: var(--accent-color);
    color: var(--text-dark);
}

.no-history {
    text-align: center;
    opacity: 0.5;
    padding: 40px;
}

/* Board Size Selector */
.board-size-options {
    display: flex;
    flex-direction: column;
    gap: 15px;
    margin: 20px 0;
}

.board-size-btn {
    padding: 15px 25px;
    background: rgba(255, 255, 255, 0.1);
    border: 2px solid rgba(255, 255, 255, 0.2);
    border-radius: 10px;
    color: var(--text-light);
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.board-size-btn:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: translateX(5px);
}

.board-size-btn.active {
    background: var(--secondary-color);
    color: var(--text-dark);
    border-color: var(--secondary-color);
}

/* Dynamic Board Sizes */
.game-board[data-size="4"] {
    grid-template-columns: repeat(4, 1fr);
}

.game-board[data-size="5"] {
    grid-template-columns: repeat(5, 1fr);
}

.game-board[data-size="4"] .cell,
.game-board[data-size="5"] .cell {
    font-size: 2.5rem;
}

/* Responsive Design */
@media (max-width: 768px) {
    h1 { font-size: 2rem; }
    .screen { padding: 25px; }
    .game-info { flex-direction: column; }
    .player-info { width: 100%; }
    .game-board { max-width: 300px; }
    .cell { font-size: 2.5rem; }
    .modal-actions { flex-direction: column; }
    .join-game { flex-direction: column; }
    .chat-box { width: 280px; right: -10px; }
    .game-controls { flex-direction: column; }
}

@media (max-width: 480px) {
    h1 { font-size: 1.5rem; }
    .screen { padding: 20px; }
    .game-board { max-width: 250px; }
    .cell { font-size: 2rem; }
    .code-box { font-size: 1.5rem; }
    .game-board[data-size="4"] .cell,
    .game-board[data-size="5"] .cell {
        font-size: 1.8rem;
    }
}