/* ============================================
   CSS Variables
   ============================================ */
:root {
    --color-bg: #121213;
    --color-surface: #1a1a1b;
    --color-primary: #538d4e;
    --color-correct: #538d4e;
    --color-present: #b59f3b;
    --color-absent: #3a3a3c;
    --color-border: #3a3a3c;
    --color-filled: #565758;
    --color-empty: #3a3a3c;
    --color-text: #ffffff;
    --color-text-muted: #818384;
    --color-key: #818384;
    --color-key-text: #ffffff;
    --color-error: #e94560;
    --color-success: #538d4e;

    --transition-speed: 0.2s;
}

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

html, body {
    height: 100%;
    height: 100dvh;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--color-bg);
    color: var(--color-text);
    overflow: hidden;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

/* ============================================
   Screens
   ============================================ */
.screen {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
}

.screen.active {
    display: flex;
    flex-direction: column;
}

.screen-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 20px;
    max-width: 400px;
    margin: 0 auto;
    width: 100%;
}

/* ============================================
   Typography
   ============================================ */
.title {
    font-size: 42px;
    font-weight: 700;
    margin-bottom: 8px;
    text-align: center;
}

.subtitle {
    font-size: 16px;
    color: var(--color-text-muted);
    margin-bottom: 32px;
    text-align: center;
}

h2 {
    font-size: 24px;
    margin-bottom: 24px;
    text-align: center;
}

.label {
    font-size: 16px;
    color: var(--color-text-muted);
    margin-bottom: 12px;
}

/* ============================================
   Inputs
   ============================================ */
.input-group {
    width: 100%;
    margin-bottom: 24px;
}

.input-group label {
    display: block;
    font-size: 14px;
    color: var(--color-text-muted);
    margin-bottom: 8px;
}

.input-group input {
    width: 100%;
    padding: 14px 16px;
    font-size: 18px;
    border: 2px solid var(--color-border);
    border-radius: 8px;
    background: var(--color-surface);
    color: var(--color-text);
    outline: none;
    transition: border-color var(--transition-speed);
}

.input-group input:focus {
    border-color: var(--color-primary);
}

.input-group input::placeholder {
    color: var(--color-text-muted);
}

/* ============================================
   Buttons
   ============================================ */
.btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: 16px 24px;
    font-size: 18px;
    font-weight: 600;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.1s, opacity 0.2s;
    margin-bottom: 12px;
}

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

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

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

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

.btn-link {
    background: none;
    border: none;
    color: var(--color-text-muted);
    font-size: 14px;
    cursor: pointer;
    padding: 12px;
    margin-top: 8px;
}

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

/* ============================================
   Room Code Display
   ============================================ */
.code-display {
    background: var(--color-surface);
    border: 3px solid var(--color-primary);
    border-radius: 12px;
    padding: 24px 48px;
    margin-bottom: 24px;
}

.code-display span {
    font-size: 48px;
    font-weight: 700;
    letter-spacing: 8px;
    font-family: monospace;
}

/* ============================================
   Waiting Indicator
   ============================================ */
.waiting-indicator {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    margin-top: 24px;
    color: var(--color-text-muted);
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--color-border);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ============================================
   Game Header
   ============================================ */
.game-header {
    display: flex;
    justify-content: space-around;
    padding: 12px 16px;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
}

.player-column {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.player-label {
    font-size: 14px;
    font-weight: 600;
    color: var(--color-text);
    text-transform: uppercase;
}

/* ============================================
   Boards Container
   ============================================ */
.boards-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: flex-start;
    gap: 16px;
    padding: 16px;
    overflow-y: auto;
}

.board-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.my-board-wrapper {
    flex: 2;
}

.rival-board-wrapper {
    flex: 1;
    max-width: 120px;
}

/* ============================================
   My Board (main wordle grid)
   ============================================ */
.board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: 5px;
    width: 100%;
    max-width: 280px;
}

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

.cell {
    width: 52px;
    height: 52px;
    border: 2px solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    font-weight: 700;
    text-transform: uppercase;
    background: transparent;
    transition: border-color 0.1s, background-color 0.3s, transform 0.1s;
}

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

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

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

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

.cell.reveal {
    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); }
}

.row.shake {
    animation: shake 0.5s;
}

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

/* ============================================
   Rival Board (mini grid, colors only)
   ============================================ */
.rival-board {
    display: grid;
    grid-template-rows: repeat(6, 1fr);
    gap: 3px;
    width: 100%;
    max-width: 100px;
}

.rival-row {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 3px;
}

.rival-cell {
    width: 16px;
    height: 16px;
    border-radius: 2px;
    background: var(--color-empty);
    transition: background-color 0.3s;
}

.rival-cell.correct {
    background: var(--color-correct);
}

.rival-cell.present {
    background: var(--color-present);
}

.rival-cell.absent {
    background: var(--color-absent);
}

.rival-status {
    margin-top: 8px;
    font-size: 12px;
    color: var(--color-text-muted);
    text-align: center;
    min-height: 16px;
}

.rival-status.won {
    color: var(--color-success);
}

.rival-status.lost {
    color: var(--color-error);
}

/* ============================================
   Current Input Row
   ============================================ */
.current-input {
    display: none;
    justify-content: center;
    gap: 5px;
    padding: 8px 16px;
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
}

.current-input.active {
    display: flex;
}

.input-cell {
    width: 48px;
    height: 48px;
    border: 2px solid var(--color-border);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    font-weight: 700;
    text-transform: uppercase;
    background: var(--color-surface);
    transition: border-color 0.1s;
}

.input-cell.filled {
    border-color: var(--color-filled);
}

.input-cell.active {
    border-color: var(--color-primary);
}

/* ============================================
   Keyboard
   ============================================ */
.keyboard {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 8px;
    padding-bottom: max(8px, env(safe-area-inset-bottom));
    background: var(--color-surface);
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
}

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

.key {
    min-width: 32px;
    height: 48px;
    border: none;
    border-radius: 4px;
    background: var(--color-key);
    color: var(--color-key-text);
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    transition: background-color 0.1s, transform 0.05s;
    flex: 1;
    max-width: 44px;
}

.key:active {
    transform: scale(0.95);
}

.key.wide {
    min-width: 56px;
    max-width: 65px;
    font-size: 11px;
}

.key.correct {
    background: var(--color-correct);
}

.key.present {
    background: var(--color-present);
}

.key.absent {
    background: var(--color-absent);
}

/* ============================================
   Result Screen
   ============================================ */
.result-emoji {
    font-size: 72px;
    margin-bottom: 16px;
}

.result-title {
    font-size: 32px;
    margin-bottom: 24px;
}

.result-title.win {
    color: var(--color-success);
}

.result-title.lose {
    color: var(--color-error);
}

.result-title.tie {
    color: var(--color-present);
}

.result-word {
    text-align: center;
    margin-bottom: 24px;
}

.result-word p {
    font-size: 14px;
    color: var(--color-text-muted);
    margin-bottom: 8px;
}

.result-word span {
    font-size: 32px;
    font-weight: 700;
    letter-spacing: 4px;
    color: var(--color-primary);
}

.result-stats {
    display: flex;
    gap: 32px;
    margin-bottom: 32px;
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
}

.stat span:first-child {
    font-size: 14px;
    color: var(--color-text-muted);
}

.stat span:last-child {
    font-size: 18px;
    font-weight: 600;
}

/* ============================================
   Error Screen
   ============================================ */
.error-emoji {
    font-size: 64px;
    color: var(--color-error);
    margin-bottom: 16px;
}

#error-message {
    color: var(--color-text-muted);
    margin-bottom: 24px;
    text-align: center;
}

/* ============================================
   Toast
   ============================================ */
.toast {
    position: fixed;
    top: 60px;
    left: 50%;
    transform: translateX(-50%) translateY(-100px);
    background: var(--color-text);
    color: var(--color-bg);
    padding: 12px 24px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    z-index: 1000;
    opacity: 0;
    transition: transform 0.3s, opacity 0.3s;
    pointer-events: none;
}

.toast.show {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
}

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

/* ============================================
   Modal
   ============================================ */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    z-index: 100;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--color-surface);
    border-radius: 12px;
    padding: 24px;
    max-width: 400px;
    width: 100%;
    max-height: 80vh;
    overflow-y: auto;
    position: relative;
}

.modal-close {
    position: absolute;
    top: 12px;
    right: 12px;
    background: none;
    border: none;
    color: var(--color-text-muted);
    font-size: 24px;
    cursor: pointer;
    padding: 4px 8px;
}

.modal-content h2 {
    margin-bottom: 20px;
}

.help-section {
    margin-bottom: 20px;
}

.help-section h3 {
    font-size: 16px;
    margin-bottom: 12px;
    color: var(--color-text-muted);
}

.help-section p {
    font-size: 14px;
    line-height: 1.5;
    margin-bottom: 8px;
}

.help-example {
    display: flex;
    align-items: center;
    gap: 12px;
    margin-bottom: 8px;
}

.example-cell {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    font-weight: 700;
    border-radius: 4px;
    color: white;
}

.example-cell.correct {
    background: var(--color-correct);
}

.example-cell.present {
    background: var(--color-present);
}

.example-cell.absent {
    background: var(--color-absent);
}

.help-example span {
    font-size: 13px;
    color: var(--color-text-muted);
}

/* ============================================
   Responsive - Tablet+
   ============================================ */
@media (min-width: 600px) {
    .boards-container {
        gap: 32px;
    }

    .cell {
        width: 58px;
        height: 58px;
        font-size: 32px;
    }

    .rival-cell {
        width: 20px;
        height: 20px;
    }

    .key {
        min-width: 40px;
        height: 52px;
        font-size: 16px;
    }

    .key.wide {
        min-width: 70px;
        font-size: 12px;
    }
}

/* ============================================
   Responsive - Small phones
   ============================================ */
@media (max-width: 350px) {
    .cell {
        width: 44px;
        height: 44px;
        font-size: 24px;
    }

    .key {
        min-width: 26px;
        height: 44px;
        font-size: 12px;
    }

    .key.wide {
        min-width: 48px;
        font-size: 10px;
    }

    .code-display span {
        font-size: 36px;
        letter-spacing: 4px;
    }
}
