/* ===== CSS Variables ===== */
:root {
    --cardinal-red: #9D2235;
    --cardinal-dark: #7a1a29;
    --white: #ffffff;
    --gray-100: #f8f9fa;
    --gray-200: #e9ecef;
    --gray-300: #dee2e6;
    --gray-500: #6c757d;
    --gray-700: #495057;
    --gray-900: #212529;

    --correct: #538d4e;
    --present: #b59f3b;
    --absent: #3a3a3c;

    --tile-size: 58px;
    --tile-gap: 5px;
    --key-height: 58px;
}

/* ===== Reset & Base ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    overflow-x: hidden;
    width: 100%;
    max-width: 100vw;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-weight: 400;
    background-color: var(--gray-100);
    color: var(--gray-900);
    min-height: 100vh;
    min-height: -webkit-fill-available;
    display: flex;
    flex-direction: column;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

h1, h2, h3, h4, h5, h6,
.btn,
.tile,
.stat-value,
.col-rank,
.preview-rank {
    font-weight: 700;
}

/* ===== Header ===== */
.header {
    background-color: var(--white);
    padding: 16px 20px;
}

.header-content {
    max-width: 500px;
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo {
    display: flex;
    align-items: center;
    text-decoration: none;
}

.logo-img {
    width: 200px;
    height: auto;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 14px;
}

.user-name {
    color: var(--gray-700);
}

/* ===== Main Content ===== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
}

/* ===== Buttons ===== */
.btn {
    display: inline-block;
    padding: 12px 24px;
    font-size: 16px;
    font-weight: 600;
    text-decoration: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    text-align: center;
}

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

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

.btn-primary:disabled {
    background-color: var(--gray-300);
    cursor: not-allowed;
}

.btn-secondary {
    background-color: var(--gray-200);
    color: var(--gray-700);
}

.btn-secondary:hover {
    background-color: var(--gray-300);
}

.btn-link {
    background: none;
    border: none;
    color: var(--cardinal-red);
    cursor: pointer;
    text-decoration: underline;
    font-size: 14px;
}

.btn-link:hover {
    color: var(--cardinal-dark);
}

/* ===== Auth Pages ===== */
.auth-container {
    width: 100%;
    max-width: 400px;
    margin-top: 40px;
}

.auth-card {
    background: var(--white);
    border-radius: 12px;
    padding: 40px 30px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    text-align: center;
}

.auth-card h1 {
    color: var(--cardinal-red);
    margin-bottom: 8px;
}

.subtitle {
    color: var(--gray-500);
    margin-bottom: 30px;
}

.auth-form {
    text-align: left;
}

.form-group {
    margin-bottom: 20px;
}

.form-group label {
    display: block;
    font-weight: 600;
    margin-bottom: 6px;
    color: var(--gray-700);
}

.form-group input {
    width: 100%;
    padding: 12px 14px;
    font-size: 16px;
    border: 2px solid var(--gray-200);
    border-radius: 8px;
    transition: border-color 0.2s;
}

.form-group input:focus,
.form-group select:focus {
    outline: none;
    border-color: var(--cardinal-red);
}

.form-group select {
    width: 100%;
    padding: 12px 14px;
    font-size: 16px;
    border: 2px solid var(--gray-200);
    border-radius: 8px;
    transition: border-color 0.2s;
    background-color: var(--white);
    cursor: pointer;
    -webkit-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath fill='%236c757d' d='M6 8L1 3h10z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 14px center;
}

.form-row {
    display: flex;
    gap: 12px;
}

.form-row .form-group {
    flex: 1;
}

.helper-text {
    display: block;
    font-size: 12px;
    color: var(--gray-500);
    margin-top: 4px;
}

.code-input {
    font-size: 24px !important;
    letter-spacing: 8px;
    text-align: center;
    font-weight: 700;
}

.error-message {
    color: var(--cardinal-red);
    font-size: 14px;
    margin-top: 12px;
    text-align: center;
}

.auth-footer {
    margin-top: 30px;
    color: var(--gray-500);
    font-size: 14px;
}

/* ===== Game Board ===== */
.game-container {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 15px;
    padding: 0 8px;
    box-sizing: border-box;
}

.game-board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: var(--tile-gap);
    padding: 5px;
    width: 100%;
    max-width: 330px;
}

.board-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: var(--tile-gap);
}

.tile {
    width: 100%;
    aspect-ratio: 1;
    max-width: var(--tile-size);
    max-height: var(--tile-size);
    border: 2px solid var(--gray-300);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(24px, 6vw, 32px);
    font-weight: 700;
    text-transform: uppercase;
    background: var(--white);
    transition: transform 0.1s;
}

.tile.filled {
    border-color: var(--gray-500);
    animation: pop 0.1s;
}

.tile.correct {
    background-color: var(--correct);
    border-color: var(--correct);
    color: var(--white);
}

.tile.present {
    background-color: var(--present);
    border-color: var(--present);
    color: var(--white);
}

.tile.absent {
    background-color: var(--absent);
    border-color: var(--absent);
    color: var(--white);
}

.tile.flip {
    animation: flip 0.5s ease forwards;
}

@keyframes pop {
    50% { transform: scale(1.1); }
}

@keyframes flip {
    0% { transform: rotateX(0); }
    50% { transform: rotateX(90deg); }
    100% { transform: rotateX(0); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    20%, 60% { transform: translateX(-5px); }
    40%, 80% { transform: translateX(5px); }
}

.shake {
    animation: shake 0.4s;
}

/* ===== Message ===== */
.message {
    min-height: 24px;
    font-weight: 600;
    color: var(--gray-700);
}

.message.error {
    color: var(--cardinal-red);
}

/* ===== Keyboard ===== */
.keyboard {
    display: flex;
    flex-direction: column;
    gap: 5px;
    width: 100%;
    max-width: 500px;
    padding: 0 4px;
    box-sizing: border-box;
}

.keyboard-row {
    display: flex;
    justify-content: center;
    gap: 4px;
    width: 100%;
}

.keyboard button {
    height: 50px;
    flex: 1;
    max-width: 43px;
    min-width: 0;
    padding: 0;
    font-size: clamp(11px, 3vw, 14px);
    font-weight: 700;
    border: none;
    border-radius: 4px;
    background-color: var(--gray-200);
    color: var(--gray-900);
    cursor: pointer;
    transition: background-color 0.1s;
    text-transform: uppercase;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

.keyboard button:hover {
    background-color: var(--gray-300);
}

.keyboard button.key-wide {
    flex: 1.5;
    max-width: 65px;
    font-size: clamp(10px, 2.5vw, 12px);
}

.keyboard button.correct {
    background-color: var(--correct);
    color: var(--white);
}

.keyboard button.present {
    background-color: var(--present);
    color: var(--white);
}

.keyboard button.absent {
    background-color: var(--absent);
    color: var(--white);
}

/* ===== Modal ===== */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    padding: 20px;
}

.modal-content {
    background: var(--white);
    border-radius: 12px;
    padding: 30px;
    max-width: 400px;
    width: 100%;
    text-align: center;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-content h2 {
    color: var(--cardinal-red);
    margin-bottom: 10px;
}

.results-word-label {
    font-size: 14px;
    color: var(--gray-500);
    margin-bottom: 4px;
}

.results-word {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: 4px;
    color: var(--correct);
    margin-bottom: 20px;
}

.results-word.loss {
    color: var(--cardinal-red);
}

.results-stats {
    display: flex;
    justify-content: center;
    gap: 40px;
    margin-bottom: 8px;
}

.stat {
    display: flex;
    flex-direction: column;
}

.stat-primary .stat-value {
    font-size: 48px;
    color: var(--cardinal-red);
}

.stat-value {
    font-size: 32px;
    font-weight: 700;
    color: var(--gray-900);
}

.stat-label {
    font-size: 12px;
    color: var(--gray-500);
    text-transform: uppercase;
}

.results-stats-secondary {
    display: flex;
    justify-content: center;
    gap: 8px;
    color: var(--gray-500);
    font-size: 14px;
    margin-bottom: 8px;
}

.scoring-hint {
    font-size: 11px;
    color: var(--gray-500);
    margin-bottom: 20px;
}

.lower-better {
    font-weight: 400;
    font-size: 12px;
    color: var(--gray-500);
}

.results-actions {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-bottom: 24px;
}

/* ===== Leaderboard ===== */
.leaderboard-container {
    width: 100%;
    max-width: 500px;
    text-align: center;
}

.leaderboard-container h1 {
    color: var(--cardinal-red);
    margin-bottom: 8px;
}

.scoring-info {
    background: var(--gray-200);
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 13px;
    color: var(--gray-700);
    display: inline-block;
    margin-bottom: 8px;
}

.leaderboard-table {
    background: var(--white);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
    margin: 24px 0;
}

.leaderboard-header {
    display: grid;
    grid-template-columns: 50px 1fr 70px 1fr;
    padding: 12px 16px;
    background: var(--cardinal-red);
    color: var(--white);
    font-weight: 600;
    font-size: 12px;
    text-transform: uppercase;
}

.leaderboard-row {
    display: grid;
    grid-template-columns: 50px 1fr 70px 1fr;
    padding: 12px 16px;
    border-bottom: 1px solid var(--gray-200);
    align-items: center;
}

.col-score {
    font-weight: 700;
    color: var(--cardinal-red);
}

.col-details {
    font-size: 12px;
    color: var(--gray-500);
    text-align: left;
}

.leaderboard-row:last-child {
    border-bottom: none;
}

.leaderboard-row.highlight {
    background: #fff3cd;
}

.col-rank {
    font-weight: 700;
}

.col-name {
    text-align: left;
}

.rank-1 { color: #ffd700; }
.rank-2 { color: #c0c0c0; }
.rank-3 { color: #cd7f32; }

.user-rank {
    padding: 16px;
    background: var(--gray-200);
    border-radius: 8px;
    font-weight: 600;
    margin-bottom: 20px;
}

.leaderboard-preview {
    margin-top: 20px;
    text-align: left;
}

.leaderboard-preview h3 {
    font-size: 14px;
    margin-bottom: 12px;
    color: var(--gray-700);
}

.leaderboard-row-preview {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 8px 0;
    border-bottom: 1px solid var(--gray-200);
    font-size: 13px;
}

.leaderboard-row-preview:last-child {
    border-bottom: none;
}

.preview-rank {
    font-weight: 700;
    min-width: 30px;
}

.preview-name {
    flex: 1;
}

.preview-score {
    font-weight: 700;
    color: var(--cardinal-red);
}

.preview-details {
    font-size: 11px;
    color: var(--gray-500);
}

.leaderboard-actions {
    margin-top: 24px;
}

.empty-state {
    padding: 40px;
    color: var(--gray-500);
}

/* ===== Responsive ===== */

/* Tablets and small desktops */
@media (max-width: 500px) {
    .main-content {
        padding: 10px;
    }

    .game-container {
        gap: 10px;
    }
}

/* Mobile phones */
@media (max-width: 400px) {
    :root {
        --tile-size: 52px;
        --tile-gap: 4px;
    }

    .header {
        padding: 12px;
    }

    .logo-img {
        width: 150px;
    }

    .game-board {
        max-width: 280px;
    }

    .keyboard button {
        height: 45px;
    }

    .auth-card {
        padding: 25px 16px;
    }

    .modal-content {
        padding: 20px 16px;
    }
}

/* Very small phones (iPhone SE, mini) */
@media (max-width: 375px) {
    :root {
        --tile-size: 48px;
        --tile-gap: 3px;
    }

    .logo-img {
        width: 130px;
    }

    .game-board {
        max-width: 260px;
    }

    .keyboard button {
        height: 42px;
        border-radius: 3px;
    }

    .keyboard-row {
        gap: 3px;
    }

    .tile {
        font-size: 22px;
    }
}

/* ===== Filter Controls ===== */
.filter-controls {
    position: relative;
    margin: 16px 0;
}

.filter-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    background: var(--white);
    border: 2px solid var(--gray-200);
    border-radius: 8px;
    cursor: pointer;
    font-size: 14px;
    font-weight: 600;
    color: var(--gray-700);
    transition: all 0.2s;
}

.filter-btn:hover {
    border-color: var(--cardinal-red);
    color: var(--cardinal-red);
}

.filter-icon,
.chevron-icon {
    flex-shrink: 0;
}

.filter-dropdown {
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    margin-top: 8px;
    background: var(--white);
    border: 1px solid var(--gray-200);
    border-radius: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
    z-index: 50;
    max-height: 400px;
    overflow-y: auto;
}

.filter-section {
    padding: 8px 0;
    border-bottom: 1px solid var(--gray-200);
}

.filter-section:last-child {
    border-bottom: none;
}

.filter-section-title {
    padding: 8px 16px 4px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--gray-500);
    letter-spacing: 0.5px;
}

.filter-option {
    display: block;
    width: 100%;
    padding: 10px 16px;
    text-align: left;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 14px;
    color: var(--gray-700);
    transition: background 0.15s;
}

.filter-option:hover {
    background: var(--gray-100);
}

.filter-option.active {
    background: var(--gray-100);
    color: var(--cardinal-red);
    font-weight: 600;
}

.filter-sub {
    padding-left: 24px;
    font-size: 13px;
}

.filter-sub-options {
    max-height: 150px;
    overflow-y: auto;
}

/* Breadcrumb */
.filter-breadcrumb {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 16px;
    font-size: 14px;
    color: var(--gray-700);
}

.back-btn {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 6px 12px;
    background: var(--gray-100);
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    color: var(--gray-700);
    transition: background 0.15s;
}

.back-btn:hover {
    background: var(--gray-200);
}

/* View Toggle */
.view-toggle {
    display: flex;
    background: var(--gray-100);
    border-radius: 8px;
    padding: 4px;
    margin-bottom: 16px;
}

.toggle-btn {
    flex: 1;
    padding: 8px 16px;
    background: none;
    border: none;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    color: var(--gray-500);
    transition: all 0.2s;
}

.toggle-btn.active {
    background: var(--white);
    color: var(--gray-900);
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
}

.toggle-btn:hover:not(.active) {
    color: var(--gray-700);
}

/* Clickable group rows */
.leaderboard-row-clickable {
    cursor: pointer;
    transition: background 0.15s;
}

.leaderboard-row-clickable:hover {
    background: var(--gray-100);
}

/* Mobile adjustments for filters */
@media (max-width: 400px) {
    .filter-btn {
        padding: 8px 12px;
        font-size: 13px;
    }

    .filter-dropdown {
        max-height: 350px;
    }

    .toggle-btn {
        padding: 6px 12px;
        font-size: 12px;
    }
}
