:root {
    --bg-color: #0b0f19;
    --card-bg: rgba(17, 24, 39, 0.7);
    --border-color: rgba(255, 255, 255, 0.08);
    --text-main: #f3f4f6;
    --text-muted: #9ca3af;
    --accent-p1: #6366f1; /* Indigo */
    --accent-p1-glow: rgba(99, 102, 241, 0.4);
    --accent-p2: #10b981; /* Emerald */
    --accent-p2-glow: rgba(16, 185, 129, 0.4);
    --cell-empty-bg: rgba(255, 255, 255, 0.02);
    --cell-filled-bg: rgba(255, 255, 255, 0.05);
    --cell-active-border: #8b5cf6; /* Violet */
    --danger-color: #ef4444;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

body {
    font-family: 'Outfit', 'Comfortaa', -apple-system, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
}

.app-container {
    width: 100%;
    max-width: 480px;
    height: 100vh;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    position: relative;
}

/* HEADER */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 8px;
}

.logo {
    display: flex;
    align-items: center;
    gap: 8px;
}

.logo .emoji {
    font-size: 20px;
}

.logo h1 {
    font-size: 20px;
    font-weight: 800;
    background: linear-gradient(to right, #60a5fa, #a78bfa, #f472b6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
}

.game-meta {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 4px;
    font-size: 11px;
}

#lobby-id {
    color: var(--text-muted);
    font-family: monospace;
}

.status-badge {
    padding: 2px 8px;
    border-radius: 20px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
}

.status-badge.active { background: rgba(99, 102, 241, 0.15); color: #818cf8; border-color: rgba(99, 102, 241, 0.3); }
.status-badge.lobby { background: rgba(245, 158, 11, 0.15); color: #fbbf24; border-color: rgba(245, 158, 11, 0.3); }
.status-badge.finished { background: rgba(16, 185, 129, 0.15); color: #34d399; border-color: rgba(16, 185, 129, 0.3); }

/* SCOREBOARD */
.scoreboard {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    padding: 8px 16px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.2);
    backdrop-filter: blur(10px);
}

.player-card {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
    border-radius: 12px;
    transition: all 0.3s ease;
    border: 1.5px solid transparent;
}

.player-card.active {
    background: rgba(255, 255, 255, 0.03);
    transform: scale(1.03);
}

#player-1.active {
    border-color: var(--accent-p1);
    box-shadow: 0 0 10px var(--accent-p1-glow);
}

#player-2.active {
    border-color: var(--accent-p2);
    box-shadow: 0 0 10px var(--accent-p2-glow);
}

.avatar-placeholder {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
}

.p1-bg { background: var(--accent-p1); }
.p2-bg { background: var(--accent-p2); }

.player-info {
    display: flex;
    flex-direction: column;
}

.player-name {
    font-size: 12px;
    font-weight: 600;
    max-width: 80px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: var(--text-muted);
}

.player-card.active .player-name {
    color: var(--text-main);
}

.player-score {
    font-size: 18px;
    font-weight: 800;
}

.vs-divider {
    font-size: 12px;
    font-weight: 800;
    color: var(--text-muted);
    padding: 0 10px;
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.1);
}

/* WORD PREVIEW */
.word-preview-container {
    height: 48px;
    display: flex;
    justify-content: center;
    align-items: center;
    background: rgba(255, 255, 255, 0.02);
    border: 1px dashed var(--border-color);
    border-radius: 12px;
    padding: 6px;
}

.word-preview {
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.1em;
    color: var(--text-muted);
    text-transform: uppercase;
    text-align: center;
    transition: all 0.2s ease;
}

.word-preview.active {
    color: #e9d5ff; /* Soft purple */
    font-size: 18px;
    text-shadow: 0 0 10px rgba(167, 139, 250, 0.6);
}

/* BOARD CONTAINER */
.board-wrapper {
    position: relative;
    width: 100%;
    aspect-ratio: 1 / 1;
    display: flex;
    justify-content: center;
    align-items: center;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    backdrop-filter: blur(15px);
}

.board-container {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(5, 1fr);
    gap: 8px;
    width: 100%;
    height: 100%;
    user-select: none;
    touch-action: none; /* Block native scrolling during drag */
}

.board-cell {
    background: var(--cell-empty-bg);
    border: 1.5px solid var(--border-color);
    border-radius: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: 800;
    color: var(--text-main);
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    user-select: none;
}

.board-cell.filled {
    background: var(--cell-filled-bg);
    border-color: rgba(255, 255, 255, 0.15);
}

.board-cell.just-added {
    animation: popIn 0.3s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
    border-color: var(--cell-active-border);
    box-shadow: 0 0 12px rgba(139, 92, 246, 0.4);
}

.board-cell.selected {
    border-color: var(--accent-p1);
    box-shadow: 0 0 10px var(--accent-p1-glow);
    background: rgba(99, 102, 241, 0.15) !important;
    transform: scale(1.05);
    z-index: 5;
}

.board-cell.p2-selected {
    border-color: var(--accent-p2);
    box-shadow: 0 0 10px var(--accent-p2-glow);
    background: rgba(16, 185, 129, 0.15) !important;
}

@keyframes popIn {
    0% { transform: scale(0.6); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

.trace-canvas {
    position: absolute;
    top: 12px;
    left: 12px;
    width: calc(100% - 24px);
    height: calc(100% - 24px);
    pointer-events: none; /* Pass events to board cells below */
    z-index: 10;
}

/* TABS / INFO SECTION */
.info-section {
    flex-grow: 1;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 16px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.tabs {
    display: flex;
    border-bottom: 1px solid var(--border-color);
    background: rgba(0, 0, 0, 0.15);
}

.tab-btn {
    flex: 1;
    background: transparent;
    border: none;
    color: var(--text-muted);
    padding: 10px;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    text-align: center;
}

.tab-btn.active {
    color: var(--text-main);
    background: rgba(255, 255, 255, 0.03);
    border-bottom: 2px solid var(--accent-p1);
}

.tab-content {
    display: none;
    flex-grow: 1;
    padding: 12px;
    overflow-y: auto;
    max-height: 140px;
}

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

.log-list {
    display: flex;
    flex-direction: column;
    gap: 6px;
    font-size: 12px;
}

.log-item {
    padding: 4px 8px;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.01);
    border-left: 3px solid transparent;
}

.log-item.system { border-left-color: var(--text-muted); color: var(--text-muted); }
.log-item.p1 { border-left-color: var(--accent-p1); }
.log-item.p2 { border-left-color: var(--accent-p2); }

.played-words {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
}

.played-word-chip {
    padding: 4px 10px;
    border-radius: 20px;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid var(--border-color);
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.05em;
    transition: all 0.2s ease;
}

.played-word-chip.p1 {
    background: rgba(99, 102, 241, 0.15) !important;
    border-color: rgba(99, 102, 241, 0.4) !important;
    color: var(--accent-p1) !important;
    box-shadow: 0 0 6px rgba(99, 102, 241, 0.2);
}

.played-word-chip.p2 {
    background: rgba(16, 185, 129, 0.15) !important;
    border-color: rgba(16, 185, 129, 0.4) !important;
    color: var(--accent-p2) !important;
    box-shadow: 0 0 6px rgba(16, 185, 129, 0.2);
}

.no-words {
    color: var(--text-muted);
    font-size: 12px;
    font-style: italic;
}

/* ACTIONS */
.action-footer {
    display: flex;
    gap: 12px;
}

.btn {
    flex: 1;
    padding: 12px;
    font-size: 14px;
    font-weight: 700;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 8px;
}

.btn:disabled {
    opacity: 0.35;
    cursor: not-allowed;
    pointer-events: none;
}

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

.btn-secondary {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--border-color);
    color: var(--text-main);
}

.btn-secondary:hover {
    background: rgba(255, 255, 255, 0.1);
}

.btn-danger {
    background: rgba(239, 68, 68, 0.15);
    border: 1px solid rgba(239, 68, 68, 0.3);
    color: #fca5a5;
}

.btn-danger:hover {
    background: rgba(239, 68, 68, 0.25);
}

/* KEYPAD */
.keypad-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    z-index: 100;
    justify-content: center;
    align-items: flex-end;
}

.keypad-overlay.active {
    display: flex;
}

.keypad-container {
    width: 100%;
    max-width: 480px;
    background: #0d1222;
    border-top: 1px solid var(--border-color);
    border-top-left-radius: 24px;
    border-top-right-radius: 24px;
    padding: 20px;
    box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.5);
    animation: slideUp 0.3s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

@keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
}

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

.keypad-header h3 {
    font-size: 16px;
    font-weight: 700;
}

.close-keypad {
    background: transparent;
    border: none;
    font-size: 20px;
    color: var(--text-muted);
    cursor: pointer;
}

.keypad-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 6px;
}

.keypad-key {
    aspect-ratio: 1 / 1;
    background: rgba(255, 255, 255, 0.03);
    border: 1.5px solid var(--border-color);
    border-radius: 8px;
    color: var(--text-main);
    font-size: 16px;
    font-weight: 700;
    cursor: pointer;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.15s ease;
}

.keypad-key:active {
    background: var(--accent-p1);
    border-color: var(--accent-p1);
    transform: scale(0.9);
}

/* GENERAL LOADING OVERLAY */
.loading-overlay {
    position: fixed;
    top: 0; left: 0; width: 100vw; height: 100vh;
    background: var(--bg-color);
    z-index: 200;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 20px;
    transition: opacity 0.3s ease;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255, 255, 255, 0.05);
    border-top-color: var(--accent-p1);
    border-radius: 50%;
    animation: spin 0.8s infinite linear;
}

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

/* TOAST NOTIFICATIONS */
.toast-container {
    position: fixed;
    top: 24px;
    left: 50%;
    transform: translateX(-50%);
    width: 90%;
    max-width: 360px;
    z-index: 1000;
    display: flex;
    flex-direction: column;
    gap: 8px;
    pointer-events: none;
}

.toast {
    pointer-events: auto;
    background: rgba(17, 24, 39, 0.85);
    border: 1px solid rgba(239, 68, 68, 0.2);
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(16px);
    color: #f3f4f6;
    padding: 12px 16px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 12px;
    font-size: 13px;
    font-weight: 700;
    user-select: none;
    touch-action: none; /* Block browser actions during swipe */
    position: relative;
    cursor: grab;
    transition: transform 0.15s ease-out, opacity 0.2s ease-out;
    animation: slideDown 0.35s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

.toast:active {
    cursor: grabbing;
}

.toast.info {
    border-color: rgba(99, 102, 241, 0.25);
}

.toast.swipe-right {
    transform: translate(120%, 0) rotate(8deg) !important;
    opacity: 0;
}

.toast.swipe-left {
    transform: translate(-120%, 0) rotate(-8deg) !important;
    opacity: 0;
}

@keyframes slideDown {
    from {
        transform: translate(0, -30px);
        opacity: 0;
    }
    to {
        transform: translate(0, 0);
        opacity: 1;
    }
}

.loading-overlay h2 {
    font-size: 16px;
    font-weight: 600;
    color: var(--text-muted);
}

/* Long press indicator for touch devices */
.long-press-indicator {
    position: absolute;
    width: 112px;
    height: 112px;
    border-radius: 50%;
    display: none;
    pointer-events: none;
    z-index: 9999;
    justify-content: center;
    align-items: center;
    box-shadow: 0 0 20px rgba(0, 0, 0, 0.7);
    background: conic-gradient(#6366f1 0%, rgba(255,255,255,0.15) 0%);
    transform: scale(0.8);
    transition: transform 0.1s ease-out;

    /* Transparent center mask so the cell content remains visible */
    -webkit-mask: radial-gradient(circle, transparent 45px, black 46px);
    mask: radial-gradient(circle, transparent 45px, black 46px);
}

.long-press-indicator.visible {
    display: flex;
    transform: scale(1.0);
}

.long-press-inner {
    width: 90px;
    height: 90px;
    border-radius: 50%;
    background: transparent;
    display: flex;
    justify-content: center;
    align-items: center;
}

@keyframes long-press-success {
    0% { transform: scale(1.0); opacity: 1; }
    50% { transform: scale(1.2); opacity: 0.8; }
    100% { transform: scale(1.3); opacity: 0; }
}

.long-press-indicator.success {
    animation: long-press-success 0.25s ease-out forwards;
}
