:root {
    --bg-color: #f0f2f5;
    --board-bg: #ffffff;
    --text-color: #2c3e50;
    --primary-color: #3498db;
    --primary-hover: #2980b9;
    --selection-color: #e8f6ff;
    --highlight-color: #d6eaf8;
    --error-color: #e74c3c;
    --error-bg: #fadbd8;
    --border-color: #bdc3c7;
    --border-thick: #34495e;
    --font-family: 'Poppins', sans-serif;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 6px rgba(0,0,0,0.1);
}

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

body {
    font-family: var(--font-family);
    background-color: var(--bg-color);
    color: var(--text-color);
    display: flex;
    justify-content: center;
    align-items: flex-start; /* Aligns items to top */
    min-height: 100vh;
    min-height: 100dvh;
}

/* Layout Container for Game and Chat */
.main-layout {
    display: flex;
    gap: 20px;
    justify-content: center;
    align-items: flex-start;
    padding: 20px;
    width: 100%;
    max-width: 1000px;
    height: 100dvh; /* Constrain height for desktop to avoid double scrollbars */
}

/* Game Container */
.app-container {
    flex: 1; /* Take available space */
    max-width: 500px;
    padding: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    height: 100%;
    justify-content: center; /* Center game vertically */
}

/* Chat Container */
.chat-container {
    width: 350px;
    height: calc(100% - 40px); /* Fill height minus padding */
    max-height: 800px;
    background: white;
    border-radius: 12px;
    box-shadow: var(--shadow-md);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    border: 1px solid var(--border-color);
    margin-top: auto;
    margin-bottom: auto; /* Vertically center chat */
}

/* Chat UI Elements */
.chat-header {
    padding: 15px;
    background-color: var(--primary-color);
    color: white;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.online-status {
    font-size: 0.8rem;
    color: #2ecc71;
    background: rgba(255,255,255,0.2);
    padding: 2px 8px;
    border-radius: 10px;
}

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

.message {
    padding: 8px 12px;
    border-radius: 8px;
    background-color: white;
    font-size: 0.9rem;
    max-width: 85%;
    box-shadow: var(--shadow-sm);
    line-height: 1.4;
    word-break: break-word;
}

.message.system {
    align-self: center;
    background-color: rgba(0,0,0,0.05);
    color: #7f8c8d;
    font-size: 0.8rem;
    box-shadow: none;
}

.message.mine {
    align-self: flex-end;
    background-color: var(--primary-color);
    color: white;
}

.message.others {
    align-self: flex-start;
    background-color: white;
    color: var(--text-color);
}

.chat-input-area {
    padding: 10px;
    background-color: white;
    border-top: 1px solid var(--border-color);
    display: flex;
    gap: 8px;
}

#chat-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border-color);
    border-radius: 20px;
    font-family: var(--font-family);
    font-size: 0.9rem;
    outline: none;
}

#chat-input:focus {
    border-color: var(--primary-color);
}

/* Game Header & Controls */
header {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 5px;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.game-stats {
    display: flex;
    gap: 15px;
    font-weight: 600;
    color: var(--text-color);
    font-size: 0.95rem;
}

#mistakes {
    color: var(--error-color);
}

h1 {
    font-size: 1.8rem;
    font-weight: 700;
    color: var(--text-color);
}

.controls {
    display: flex;
    gap: 10px;
    width: 100%;
    justify-content: flex-end;
}

select {
    padding: 8px 12px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
    background-color: white;
    font-family: var(--font-family);
    font-size: 0.9rem;
    cursor: pointer;
}

.btn {
    padding: 8px 16px;
    border: none;
    border-radius: 8px;
    font-family: var(--font-family);
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.1s;
}

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

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

.btn:active {
    transform: scale(0.98);
}

main {
    width: 100%;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

/* Game Board */
.game-board {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    width: 100%;
    aspect-ratio: 1 / 1;
    background-color: var(--board-bg);
    border: 2px solid var(--border-thick);
    border-radius: 4px;
    box-shadow: var(--shadow-md);
    overflow: hidden;
}

.cell {
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.2rem;
    font-weight: 500;
    border: 1px solid var(--border-color);
    cursor: pointer;
    user-select: none;
    transition: background-color 0.1s;
    position: relative;
}

/* Thicker borders for 3x3 grids */
.cell:nth-child(3n) {
    border-right: 2px solid var(--border-thick);
}
.cell:nth-child(9n) {
    border-right: none;
}
.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 2px solid var(--border-thick);
}
.cell:nth-child(9n) {
    border-right: none;
}
.cell:nth-child(n+73) {
    border-bottom: none;
}

/* Cell States */
.cell.selected {
    background-color: var(--primary-color);
    color: white;
}

.cell.highlighted {
    background-color: var(--highlight-color);
}

.cell.same-number {
    background-color: var(--selection-color);
    font-weight: 700;
}

.cell.fixed {
    color: #2c3e50;
    font-weight: 700;
}

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

.cell.error {
    background-color: var(--error-bg);
    color: var(--error-color);
}

.cell.selected.error {
    background-color: var(--error-color);
    color: white;
}

/* Numpad */
.numpad {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    width: 100%;
    margin-top: 0;
}

.num-btn {
    padding: 12px 0;
    font-size: 1.2rem;
    font-weight: 600;
    background-color: white;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-color);
    cursor: pointer;
    transition: all 0.2s;
    box-shadow: var(--shadow-sm);
}

.num-btn:hover {
    background-color: var(--highlight-color);
    transform: translateY(-2px);
}

.num-btn:active {
    transform: translateY(0);
}

.action-btn {
    grid-column: span 1;
    font-size: 0.9rem;
    color: var(--error-color);
    border-color: var(--error-bg);
}

footer {
    text-align: center;
    color: #7f8c8d;
    font-size: 0.9rem;
}

/* Auth Controls in Header */
.auth-controls {
    display: flex;
    gap: 8px;
    align-items: center;
}

.btn.small {
    padding: 6px 12px;
    font-size: 0.85rem;
}

#user-display {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--primary-color);
}

/* Modals */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    backdrop-filter: blur(4px);
    transition: opacity 0.3s;
}

.modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 25px;
    border-radius: 16px;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
    width: 90%;
    max-width: 400px;
    z-index: 1001;
    display: flex;
    flex-direction: column;
    gap: 20px;
    animation: modalPop 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes modalPop {
    from { opacity: 0; transform: translate(-50%, -40%) scale(0.95); }
    to { opacity: 1; transform: translate(-50%, -50%) scale(1); }
}

.hidden {
    display: none !important;
}

.close-btn {
    position: absolute;
    top: 15px;
    right: 15px;
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #7f8c8d;
}

/* Auth Form */
#auth-form {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

#auth-form input {
    padding: 12px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    font-family: var(--font-family);
    font-size: 1rem;
}

#auth-form input:focus {
    outline: none;
    border-color: var(--primary-color);
}

.full-width {
    width: 100%;
    padding: 12px;
}

.auth-switch {
    text-align: center;
    font-size: 0.9rem;
    color: #7f8c8d;
}

.text-btn {
    background: none;
    border: none;
    color: var(--primary-color);
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
}

/* Ranking Modal */
.ranking-tabs {
    display: flex;
    gap: 10px;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 10px;
}

.tab-btn {
    flex: 1;
    padding: 8px;
    background: none;
    border: none;
    cursor: pointer;
    font-family: var(--font-family);
    color: #7f8c8d;
    font-weight: 600;
    border-radius: 8px;
    transition: all 0.2s;
}

.tab-btn.active {
    background-color: var(--selection-color);
    color: var(--primary-color);
}

.ranking-list {
    max-height: 300px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.ranking-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px;
    background-color: #f8f9fa;
    border-radius: 8px;
}

.ranking-rank {
    font-weight: 700;
    width: 30px;
    color: var(--primary-color);
}

.ranking-user {
    flex: 1;
    font-weight: 500;
}

.ranking-time {
    font-family: monospace;
    font-weight: 600;
}

/* Game Blur & Overlay */
.blur-content {
    position: relative;
}

.blur-content.blocked .game-board,
.blur-content.blocked .numpad {
    filter: blur(5px);
    pointer-events: none;
}

.game-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: white;
    padding: 30px;
    border-radius: 16px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.15);
    z-index: 100;
    text-align: center;
    display: none;
    width: 80%;
    max-width: 300px;
}

.blur-content.blocked .game-overlay {
    display: flex;
    flex-direction: column;
    gap: 15px;
}

/* Responsive adjustments */
@media (max-width: 850px) {
/* ... existing ... */
    .main-layout {
        flex-direction: column;
        align-items: center;
        height: auto;
        padding: 10px;
    }

    .app-container {
        width: 100%;
        max-width: min(500px, calc(100dvh - 100px));
        height: auto;
    }

    .chat-container {
        width: 100%;
        max-width: 500px;
        height: 300px; /* Fixed height for chat on mobile */
        margin-top: 20px;
    }
    
    .cell {
        font-size: 1rem;
    }
    .numpad {
        gap: 5px;
    }
    .num-btn {
        padding: 10px 0;
        font-size: 1rem;
    }
}