html, body {
    height: 100%;
    margin: 0;
    overflow: hidden;      /* No scrollbars; layout must fit viewport */
    touch-action: none;    /* Keep swipe gestures for the board */
}

body {
    font-family: Arial, sans-serif;
    text-align: center;
    background-color: #faf8ef;
    display: flex;
    flex-direction: column;
    align-items: center;
}

/* Header styles */
#header {
    display: flex;
    justify-content: flex-end;
    padding: 10px;
}

#user-info {
    font-size: 18px;
}

#user-info span {
    margin-left: 20px;
    cursor: pointer;
}

/* Game styles */
#game-container {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #faf8ef;
    margin: 5px auto 5px;
    width: min(70vmin, 90vw);
    aspect-ratio: 1 / 1;      /* keep board square */
    max-height: 50vh;         /* leave more room for score + ad + footer */
    box-sizing: border-box;
    border-radius: 10px;
}

#game-board {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 5px;
    width: 100%;
    height: 100%;
    background-color: #bbada0;
    padding: 10px;
    box-sizing: border-box;
    border-radius: 10px;
}

.grid-cell, .tile {
    width: 100%;
    height: 100%;
}

.grid-cell {
    background-color: #ccc0b3;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tile {
    background-color: #eee4da;
    border-radius: 5px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    font-weight: bold;
    color: #776e65;
    box-sizing: border-box;
}

.tile[data-value] {
    animation: pop 0.2s ease;
}

.tile[data-value="2"]    { background-color: #eee4da; }
.tile[data-value="4"]    { background-color: #ede0c8; }
.tile[data-value="8"]    { background-color: #f2b179; color: #f9f6f2; }
.tile[data-value="16"]   { background-color: #f59563; color: #f9f6f2; }
.tile[data-value="32"]   { background-color: #f67c5f; color: #f9f6f2; }
.tile[data-value="64"]   { background-color: #f65e3b; color: #f9f6f2; }
.tile[data-value="128"]  { background-color: #edcf72; color: #f9f6f2; }
.tile[data-value="256"]  { background-color: #edcc61; color: #f9f6f2; }
.tile[data-value="512"]  { background-color: #edc850; color: #f9f6f2; }
.tile[data-value="1024"] { background-color: #edc53f; color: #f9f6f2; }
.tile[data-value="2048"] { background-color: #edc22e; color: #f9f6f2; }

#score-container {
    margin-top: 5px;
    text-align: center;
    font-size: 18px;
    padding-bottom: 0;
}

/* Animations */
@keyframes pop {
    0% { transform: scale(0.7); }
    100% { transform: scale(1); }
}

/* Popup styles */
.popup {
    display: none;
    position: fixed;
    z-index: 1000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    overflow: auto;
    background-color: rgba(0,0,0,0.6); /* Slightly darker overlay */
}

.popup-content {
    background-color: #f9f9f9;
    margin: 15% auto;
    padding: 20px 30px;
    border: 1px solid #ddd;
    width: 350px; /* Compact look */
    border-radius: 12px;
    box-shadow: 0px 4px 10px rgba(0,0,0,0.1);
    position: relative;
}

.close-button {
    position: absolute;
    top: 10px;
    right: 15px;
    font-size: 36px;
    cursor: pointer;
    color: #333;
    padding: 5px;
    line-height: 1;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.close-button:hover {
    background-color: rgba(0, 0, 0, 0.1);
    transform: scale(1.1);
}

/* History table styles */
#history-table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 20px;
}

#history-table th, #history-table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #ddd;
    cursor: pointer;
}

#history-table th {
    background-color: #f2f2f2;
}

#history-table th:hover {
    background-color: #e0e0e0;
}

/* Form title */
.popup-content h2 {
    font-size: 24px;
    margin-bottom: 20px;
    text-align: center;
    color: #333;
}

/* Form labels */
.popup-content label {
    display: block;
    margin-bottom: 5px;
    font-weight: bold;
    color: #666;
}

/* Form input fields */
.popup-content input[type="text"],
.popup-content input[type="email"],
.popup-content input[type="password"] {
    width: 100%;
    padding: 10px;
    margin-bottom: 15px;
    border-radius: 8px;
    border: 1px solid #ddd;
    box-sizing: border-box;
}

/* Submit button */
.popup-content button {
    width: 100%;
    padding: 10px;
    background-color: #4CAF50;
    border: none;
    border-radius: 8px;
    color: white;
    font-size: 16px;
    cursor: pointer;
}

.popup-content button:hover {
    background-color: #45a049;
}

/* Toggle link */
.popup-content #toggle-form {
    text-align: center;
    margin-top: 10px;
    font-size: 14px;
}

.popup-content #toggle-form a {
    color: #007BFF;
    text-decoration: none;
}

.popup-content #toggle-form a:hover {
    text-decoration: underline;
}

/* Footer styles */
#footer {
    text-align: center;
    padding: 10px;
    background-color: #faf8ef;
    color: #333;
    font-size: 14px;
    width: 100%;
    border-top: 1px solid #ddd;
    margin-top: auto;  /* push footer to bottom in flex layout */
}

#footer p {
    margin: 0;
}

/* Tabs */
.tabs {
    display: flex;
    justify-content: space-around;
    margin-bottom: 15px;
}

.tab-button {
    flex-grow: 1;
    padding: 10px 20px;
    background-color: #ddd;
    border: none;
    cursor: pointer;
    font-weight: bold;
    border-radius: 5px;
    margin: 0 5px;
    text-align: center;
}

.tab-button.active {
    background-color: #28a745;
    color: white;
}

.tab-content {
    display: none;
    margin-top: 20px;
}

.tab-content.active {
    display: block;
}

/* Leaderboard Popup */
#leaderboard-popup {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: auto;
  background-color: rgba(0,0,0,0.6); /* Slightly darker overlay */
}

#leaderboard-popup .popup-content {
    background-color: #f9f9f9;
    width: 95%;
    max-width: 650px;
    padding: 20px 30px;
    border: 1px solid #ddd;
    border-radius: 12px;
    box-shadow: 0px 4px 10px rgba(0,0,0,0.1);
}

#leaderboard-popup table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
}

#leaderboard-popup table th {
    background-color: #f2f2f2;
    color: #333;
    font-weight: bold;
    padding: 10px;
    text-align: left;
    border-bottom: 2px solid #ddd;
}

#leaderboard-popup table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

/* Tab container styling */
#leaderboard-popup .tabs {
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: nowrap;
}

#leaderboard-popup .tab-button {
    flex: 0 0 auto;
    width: auto;
    min-width: 140px;
    padding: 12px 24px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    font-size: 15px;
    margin: 0;
    transition: all 0.2s ease;
    white-space: nowrap;
}

#leaderboard-popup .tab-button:hover {
    background-color: #45a049;
    transform: translateY(-1px);
}

#leaderboard-popup .tab-button.active {
    background-color: #2d7a32;
    box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

#leaderboard-popup .tab-content {
    margin-top: 15px;
}

#leaderboard-popup .leaderboard-content {
    overflow: hidden;
}

#leaderboard-popup table th:nth-child(1),
#leaderboard-popup table td:nth-child(1) {
    width: 25%;
}

#leaderboard-popup table th:nth-child(2),
#leaderboard-popup table td:nth-child(2) {
    width: 25%;
}

#leaderboard-popup table th:nth-child(3),
#leaderboard-popup table td:nth-child(3) {
    width: 20%;
}

#leaderboard-popup table th:nth-child(4),
#leaderboard-popup table td:nth-child(4) {
    width: 15%;
}

#leaderboard-popup table th:nth-child(5),
#leaderboard-popup table td:nth-child(5) {
    width: 15%;
}


/* Mobile leaderboard fixes */
@media (max-width: 600px) {
    #leaderboard-popup .popup-content {
        width: 95%;
        max-width: none;
        margin: 5% auto;
        padding: 8px 12px;
        max-height: 85vh;
    }

    #leaderboard-popup .tabs {
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: nowrap;
}

/* Tab container styling */
#leaderboard-popup .tabs {
    display: flex;
    flex-direction: row;
    justify-content: center;
    gap: 12px;
    margin-bottom: 20px;
    flex-wrap: nowrap;
}

    #leaderboard-popup .tab-button {
    flex: 0 0 auto;
    width: auto;
    min-width: 140px;
    padding: 12px 24px;
    background-color: #4CAF50;
    color: white;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 600;
    font-size: 15px;
    margin: 0;
    transition: all 0.2s ease;
    white-space: nowrap;
}

#leaderboard-popup .tab-button:hover {
    background-color: #45a049;
    transform: translateY(-1px);
}

    #leaderboard-popup table {
        font-size: 12px;
    }

    #leaderboard-popup table th,
    #leaderboard-popup table td {
        padding: 6px 4px;
    }

    #leaderboard-popup .close-button {
        font-size: 28px;
        width: 44px;
        height: 44px;
        display: flex;
        align-items: center;
        justify-content: center;
        z-index: 20;
    }
}
/* Mobile adjustments */
@media (max-width: 600px) {
    .popup-content {
        padding: 20px 20px;
        width: 90%;
    }

/* Profile popup styles */
.profile-field {
    margin-bottom: 20px;
}

.profile-field label {
    display: block;
    margin-bottom: 8px;
    font-weight: bold;
    color: #333;
}

.profile-field .note {
    font-size: 12px;
    color: #666;
    font-style: italic;
    margin-top: 5px;
    margin-bottom: 0;
}

#name-email-display, #joined-date-display {
    margin-bottom: 15px;
}

#name-email-value {
    font-size: 18px;
    font-weight: bold;
    color: #333;
}

#joined-date-value {
    color: #666;
}

#password-display {
    display: flex;
    align-items: center;
    gap: 10px;
}

.change-btn, .save-btn, .cancel-btn {
    padding: 6px 12px;
    font-size: 14px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
}

.change-btn {
    background-color: #007BFF;
    color: white;
}

.change-btn:hover {
    background-color: #0056b3;
}

.save-btn {
    background-color: #4CAF50;
    color: white;
}

.save-btn:hover {
    background-color: #45a049;
}

.cancel-btn {
    background-color: #6c757d;
    color: white;
}

.cancel-btn:hover {
    background-color: #5a6268;
}

#password-edit {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#password-edit input {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    box-sizing: border-box;
}

#password-edit .save-btn,
#password-edit .cancel-btn {
    width: auto;
    align-self: flex-start;
}

#email-display {
    width: 100%;
    padding: 8px;
    border: 1px solid #ddd;
    border-radius: 4px;
    background-color: #e9ecef;
    color: #6c757d;
    cursor: not-allowed;
}

.error-message {
    color: #dc3545;
    background-color: #f8d7da;
    border: 1px solid #f5c6cb;
    padding: 10px;
    border-radius: 4px;
    margin-top: 10px;
}

.success-message {
    color: #155724;
    background-color: #d4edda;
    border: 1px solid #c3e6cb;
    padding: 10px;
    border-radius: 4px;
    margin-top: 10px;
}

    .close-button {
        right: 20px;
        font-size: 28px;
        width: 36px;
        height: 36px;
    }
}


/* History Popup - match leaderboard styling */
#history-popup .popup-content {
    width: 95%;
    max-width: 650px;
    padding: 20px 30px;
    border-radius: 12px;
    box-shadow: 0px 4px 10px rgba(0,0,0,0.1);
    margin: 15% auto;
}

#history-popup table {
    width: 100%;
    border-collapse: collapse;
    margin-top: 15px;
}

#history-popup table th {
    background-color: #f2f2f2;
    color: #333;
    font-weight: bold;
    padding: 10px;
    text-align: left;
    border-bottom: 2px solid #ddd;
}

#history-popup table td {
    padding: 10px;
    text-align: left;
    border-bottom: 1px solid #ddd;
}

@media (max-width: 600px) {
    #history-popup .popup-content {
        width: 95%;
        max-width: none;
        margin: 5% auto;
        padding: 8px 12px;
        max-height: 85vh;
    }

    #history-popup table {
        font-size: 12px;
    }

    #history-popup table th,
    #history-popup table td {
        padding: 6px 4px;
    }
}
}
