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

:root {
    /* Color Palette */
    --bg-primary: #1a1a2e;
    --bg-surface: #16213e;
    --color-primary: #0f4c75;
    --color-accent: #3282b8;
    --color-success: #4caf50;
    --color-warning: #ff9800;
    --color-error: #f44336;
    --text-primary: #ffffff;
    --text-secondary: #b0b0b0;
    
    /* Spacing */
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 16px;
    --spacing-lg: 24px;
    --spacing-xl: 32px;
    
    /* Touch Targets */
    --touch-min: 44px;
    
    /* Transitions */
    --transition-fast: 150ms ease-out;
    --transition-normal: 300ms ease-out;
    --transition-slow: 500ms ease-out;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    font-size: 14px;
    line-height: 1.5;
    overflow: hidden;
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

/* Prevent scroll bounce on iOS */
html, body {
    position: fixed;
    width: 100%;
    height: 100%;
    overscroll-behavior: none;
}

/* ==================== UNIVERSAL NAV HEADER (P0) ==================== */
.nav-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 50px;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 var(--spacing-sm);
    z-index: 1000;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.nav-title {
    font-size: 14px;
    font-weight: 700;
    letter-spacing: 0.5px;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    margin: 0;
}

.nav-actions {
    display: flex;
    gap: var(--spacing-sm);
    margin-left: auto;
}

.nav-btn {
    width: 40px;
    height: 40px;
    min-width: var(--touch-min);
    min-height: var(--touch-min);
    background: transparent;
    border: none;
    color: var(--text-primary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    transition: background var(--transition-fast);
    touch-action: manipulation;
}

.nav-btn:active {
    background: rgba(255, 255, 255, 0.1);
    transform: scale(0.95);
}

.nav-btn svg {
    pointer-events: none;
}

.nav-back {
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--transition-normal);
}

.nav-back.visible {
    opacity: 1;
    pointer-events: auto;
}

.hidden {
    display: none !important;
}

/* ==================== MAIN GAME CONTAINER ==================== */
.game-container {
    padding-top: 50px;
    height: 100vh;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    padding-bottom: var(--spacing-md);
}

/* Source Panel */
.source-panel {
    padding: var(--spacing-md);
    background: var(--bg-surface);
    border-radius: 12px;
    margin: 0 var(--spacing-md);
}

.source-title {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
    margin-bottom: var(--spacing-sm);
}

.source-items {
    display: flex;
    gap: var(--spacing-sm);
    justify-content: center;
}

.source-item {
    width: 50px;
    height: 50px;
    background: var(--bg-primary);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    cursor: grab;
    touch-action: none;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
    position: relative;
}

.source-item::before {
    content: '';
    position: absolute;
    inset: -8px;
    /* Invisible touch area expansion */
}

.source-item:active {
    cursor: grabbing;
    transform: scale(1.1);
    box-shadow: 0 0 20px rgba(50, 130, 184, 0.5);
    z-index: 100;
}

/* Game Board */
.game-board {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 var(--spacing-md);
    overflow: hidden;
}

.board-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: var(--spacing-xs);
    max-width: 350px;
    aspect-ratio: 1;
    background: var(--bg-surface);
    padding: var(--spacing-sm);
    border-radius: 12px;
}

.board-cell {
    background: var(--bg-primary);
    border-radius: 8px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 28px;
    position: relative;
    touch-action: none;
    transition: background var(--transition-fast);
}

.board-cell.empty {
    opacity: 0.5;
}

.board-cell.valid-drop {
    background: rgba(76, 175, 80, 0.3);
    border: 2px solid var(--color-success);
    animation: pulse 1s infinite;
}

.board-cell.invalid-drop {
    background: rgba(244, 67, 54, 0.3);
    border: 2px solid var(--color-error);
    animation: shake 0.3s;
}

.board-cell .item {
    cursor: grab;
    transition: transform var(--transition-fast), opacity var(--transition-fast);
    touch-action: none;
}

.board-cell .item:active {
    cursor: grabbing;
    transform: scale(1.1);
    opacity: 0.8;
    z-index: 100;
}

.board-cell .item.dragging {
    opacity: 0.8;
    pointer-events: none;
}

/* Ghost preview when dragging */
.drag-ghost {
    position: absolute;
    opacity: 0.3;
    pointer-events: none;
    z-index: 50;
}

/* Score Panel */
.score-panel {
    display: flex;
    justify-content: space-around;
    gap: var(--spacing-md);
    padding: var(--spacing-md);
    background: var(--bg-surface);
    border-radius: 12px;
    margin: 0 var(--spacing-md);
}

.stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-xs);
}

.stat-label {
    font-size: 10px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-secondary);
}

.stat-value {
    font-size: 20px;
    font-weight: 700;
    color: var(--color-accent);
}

/* ==================== MODALS (P0) ==================== */
.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2000;
    animation: fadeIn var(--transition-normal);
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.modal {
    background: var(--bg-surface);
    border-radius: 16px;
    padding: var(--spacing-xl);
    max-width: 400px;
    width: 90%;
    animation: slideUp var(--transition-normal);
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

@keyframes slideUp {
    from {
        transform: translateY(50px) scale(0.95);
        opacity: 0;
    }
    to {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

.modal-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--spacing-lg);
}

.modal-title {
    font-size: 24px;
    font-weight: 700;
    text-align: center;
    letter-spacing: 1px;
}

.modal-close {
    position: absolute;
    top: var(--spacing-md);
    right: var(--spacing-md);
    width: 40px;
    height: 40px;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 32px;
    cursor: pointer;
    line-height: 1;
}

/* Win Modal Specific */
.stars-container {
    display: flex;
    gap: var(--spacing-md);
    font-size: 48px;
}

.star {
    opacity: 0;
    transform: scale(0);
    animation: starPop 0.5s forwards;
}

.star[data-star="1"] { animation-delay: 0.1s; }
.star[data-star="2"] { animation-delay: 0.3s; }
.star[data-star="3"] { animation-delay: 0.5s; }

@keyframes starPop {
    0% {
        opacity: 0;
        transform: scale(0) rotate(-180deg);
    }
    50% {
        transform: scale(1.2) rotate(10deg);
    }
    100% {
        opacity: 1;
        transform: scale(1) rotate(0deg);
    }
}

/* Lose Modal Specific */
.lose-icon {
    font-size: 64px;
    animation: heartbreak 0.8s ease-out;
}

@keyframes heartbreak {
    0%, 100% { transform: scale(1); }
    25% { transform: scale(1.2) rotate(-5deg); }
    75% { transform: scale(0.9) rotate(5deg); }
}

/* Stats Box */
.stats-box {
    width: 100%;
    background: var(--bg-primary);
    border-radius: 12px;
    padding: var(--spacing-md);
}

.stat-row {
    display: flex;
    justify-content: space-between;
    padding: var(--spacing-sm) 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    font-size: 16px;
}

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

.stat-row span:first-child {
    color: var(--text-secondary);
}

.stat-row span:last-child {
    color: var(--text-primary);
    font-weight: 700;
}

/* Modal Actions */
.modal-actions {
    display: flex;
    gap: var(--spacing-md);
    width: 100%;
}

.btn {
    flex: 1;
    min-height: 48px;
    border: none;
    border-radius: 12px;
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    cursor: pointer;
    touch-action: manipulation;
    transition: transform var(--transition-fast), box-shadow var(--transition-fast);
}

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

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

.btn-primary:hover {
    box-shadow: 0 4px 20px rgba(50, 130, 184, 0.4);
}

.btn-secondary {
    background: transparent;
    color: var(--text-primary);
    border: 2px solid rgba(255, 255, 255, 0.3);
}

.btn-danger {
    background: var(--color-error);
    color: white;
    margin-top: var(--spacing-md);
}

.btn-share {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 14px;
    cursor: pointer;
}

.tip-box {
    width: 100%;
    background: rgba(255, 152, 0, 0.2);
    border-left: 4px solid var(--color-warning);
    padding: var(--spacing-md);
    border-radius: 8px;
    font-size: 14px;
    color: var(--text-secondary);
}

/* Info Modal */
.info-content {
    text-align: left;
    width: 100%;
    color: var(--text-secondary);
    line-height: 1.6;
}

.info-content p {
    margin-bottom: var(--spacing-md);
}

.info-content ul {
    margin-left: var(--spacing-lg);
    margin-bottom: var(--spacing-md);
}

.info-content strong {
    color: var(--text-primary);
}

/* Settings Modal */
.settings-content {
    width: 100%;
}

.setting-item {
    padding: var(--spacing-md) 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.1);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: var(--spacing-md);
}

.setting-item:last-child {
    border-bottom: none;
}

.setting-item label {
    flex: 1;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: var(--spacing-sm);
}

.setting-item input[type="range"] {
    flex: 1;
    max-width: 150px;
}

.setting-item select {
    background: var(--bg-primary);
    color: var(--text-primary);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 8px;
    padding: var(--spacing-sm);
}

/* Confetti Canvas */
.confetti-canvas {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 1999;
}

/* ==================== ANIMATIONS ==================== */
@keyframes pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.05); }
}

@keyframes shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-5px); }
    75% { transform: translateX(5px); }
}

/* ==================== RESPONSIVE BREAKPOINTS ==================== */

/* Mobile Landscape */
@media (min-width: 600px) and (orientation: landscape) {
    .game-container {
        flex-direction: row;
        padding-left: var(--spacing-md);
        padding-right: var(--spacing-md);
    }
    
    .board-grid {
        grid-template-columns: repeat(6, 1fr);
        max-width: 400px;
    }
    
    .source-panel {
        order: 1;
    }
    
    .game-board {
        order: 2;
        flex: 1;
    }
    
    .score-panel {
        order: 3;
        flex-direction: column;
        max-width: 120px;
    }
}

/* Tablet */
@media (min-width: 768px) {
    body {
        font-size: 16px;
    }
    
    .nav-header {
        height: 60px;
    }
    
    .game-container {
        padding-top: 60px;
    }
    
    .nav-title {
        font-size: 16px;
    }
    
    .board-grid {
        grid-template-columns: repeat(7, 1fr);
        max-width: 500px;
    }
    
    .source-item,
    .board-cell {
        font-size: 32px;
    }
}

/* Desktop */
@media (min-width: 1024px) {
    .game-container {
        max-width: 1200px;
        margin: 0 auto;
    }
    
    .board-grid {
        grid-template-columns: repeat(8, 1fr);
        max-width: 600px;
    }
    
    .nav-btn:hover {
        background: rgba(255, 255, 255, 0.1);
    }
    
    .source-item:hover,
    .board-cell .item:hover {
        transform: scale(1.05);
    }
}

/* Reduced Motion */
@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}

/* Dark mode (already dark by default) */
@media (prefers-color-scheme: light) {
    /* Could add light theme here if needed */
}
