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

body, html {
    width: 100%;
    height: 100%;
    overflow: hidden; /* Prevent scrolling, game takes full screen */
    font-family: 'Sarabun', 'Itim', sans-serif;
    background-color: #000; /* Fallback */
    background-image: url('pic/water_bg.png');
    background-size: cover;
    background-position: center;
    color: #333;
    touch-action: none; /* Prevent browser touch behaviors like zoom/swipe */
}

/* Typography */
h1, h2, h3, .game-title, .highlight-text {
    font-family: 'Itim', cursive;
}

/* =========================================
   Background Layers
   ========================================= */
.ar-background-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 1; /* Lowest layer */
}

video#webcam {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    transform: scaleX(-1); /* Mirror camera */
    opacity: 0.35; /* Semi-transparent so water background is clearly visible */
}

.fallback-background {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    /* We will use a drawn cartoon river or a solid color gradient if image fails */
    background: linear-gradient(180deg, #4facfe 0%, #00f2fe 100%);
    z-index: 2; 
    display: none; /* Hidden by default if webcam works */
}

.camera-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(255, 255, 255, 0.1);
    z-index: 3;
    pointer-events: none;
}

/* River Layer (Theme) */
.river-container {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 4; /* Above webcam, behind UI */
    pointer-events: none;
    transition: opacity 1s ease-in-out;
}

.river-dirty {
    position: absolute;
    width: 100%;
    height: 100%;
    background-color: rgba(101, 67, 33, 0.6); /* Muddy brown overlay */
    transition: opacity 0.5s ease;
    opacity: 1; /* Starts dirty */
}

.river-clean {
    position: absolute;
    width: 100%;
    height: 100%;
    background: radial-gradient(circle, transparent 20%, #ffffff22 20%, #ffffff22 80%, transparent 80%, transparent);
    background-size: 20px 20px;
    opacity: 0; /* Starts hidden */
    transition: opacity 0.5s ease;
    animation: sparkle 3s infinite linear;
}

@keyframes sparkle {
    0% { background-position: 0 0; }
    100% { background-position: 40px 40px; }
}

/* =========================================
   UI Wrapper & Screens
   ========================================= */
.ui-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    z-index: 10; /* Highest layer for interactions */
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none; /* Let clicks pass through if not on a specific element */
}

/* Screens */
.ui-card {
    background: rgba(255, 255, 255, 0.9);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    padding: 30px;
    width: 90%;
    max-width: 600px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    text-align: center;
    pointer-events: auto; /* Re-enable pointer events for the card */
    display: none; /* Hidden by default */
    flex-direction: column;
    gap: 20px;
    animation: slideUp 0.5s ease-out;
}

.ui-card.active {
    display: flex;
}

.ui-hud {
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    pointer-events: none; /* Crucial: Let touches hit the game area if needed */
}

.ui-hud.active {
    display: flex;
}

.hidden {
    display: none !important;
}

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

/* =========================================
   Common UI Elements
   ========================================= */
.game-title {
    color: #2c3e50;
    font-size: 2.5rem;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.1);
    margin-bottom: 10px;
}

.game-subtitle {
    color: #555;
    font-size: 1.1rem;
    margin-bottom: 20px;
}

.form-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
    gap: 10px;
}

.form-group label {
    font-weight: bold;
    color: #333;
}

input[type="text"] {
    width: 100%;
    padding: 15px;
    border: 2px solid #3498db;
    border-radius: 10px;
    font-size: 1.2rem;
    font-family: 'Sarabun', sans-serif;
    outline: none;
    transition: border-color 0.3s;
}

input[type="text"]:focus {
    border-color: #2980b9;
}

.btn {
    padding: 15px 25px;
    border: none;
    border-radius: 10px;
    font-size: 1.2rem;
    font-family: 'Itim', cursive;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    width: 100%;
    font-weight: bold;
}

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

.btn:hover {
    box-shadow: 0 5px 15px rgba(0,0,0,0.2);
}

.btn-primary {
    background: linear-gradient(135deg, #2ecc71, #27ae60);
    color: white;
}

.btn-secondary {
    background: linear-gradient(135deg, #3498db, #2980b9);
    color: white;
}

.btn-success {
    background: linear-gradient(135deg, #f1c40f, #f39c12);
    color: white;
}

.btn-pulse {
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0% { box-shadow: 0 0 0 0 rgba(241, 196, 15, 0.7); }
    70% { box-shadow: 0 0 0 15px rgba(241, 196, 15, 0); }
    100% { box-shadow: 0 0 0 0 rgba(241, 196, 15, 0); }
}

.instructions-card {
    background: #f8f9fa;
    padding: 15px;
    border-radius: 10px;
    text-align: left;
    font-size: 0.95rem;
    border-left: 5px solid #e74c3c;
}

.instructions-card ul {
    list-style-type: none;
    margin-top: 10px;
}

.instructions-card li {
    margin-bottom: 5px;
}

.loading-msg {
    color: #e67e22;
    font-weight: bold;
    margin-top: 10px;
    animation: blink 1s infinite;
}

@keyframes blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* =========================================
   HUD (Heads Up Display) for Game Screen
   ========================================= */
.hud-header {
    display: flex;
    justify-content: space-between;
    padding: 15px;
    pointer-events: none; /* Let touches fall through */
}

.hud-left, .hud-right {
    display: flex;
    gap: 15px;
    align-items: flex-start;
}

.hud-item {
    background: rgba(255, 255, 255, 0.85);
    padding: 10px 15px;
    border-radius: 12px;
    box-shadow: 0 4px 6px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
    font-weight: bold;
}

.hud-label {
    font-size: 0.8rem;
    color: #7f8c8d;
    text-transform: uppercase;
}

.hud-value {
    font-size: 1.5rem;
    color: #2c3e50;
    font-family: 'Itim', cursive;
}

.highlight-text {
    color: #27ae60;
}

.name-text {
    font-size: 1.2rem;
    color: #8e44ad;
}

.lives-container {
    display: flex;
    gap: 5px;
    margin-top: 5px;
}

.life-icon {
    width: 30px;
    height: 30px;
    object-fit: contain;
    transition: all 0.3s ease;
}

.life-icon.lost {
    opacity: 0.2;
    filter: grayscale(100%);
    transform: scale(0.8);
}

/* =========================================
   Game Mechanics Container
   ========================================= */
.falling-area {
    flex: 1;
    position: relative;
    width: 100%;
    overflow: hidden;
    pointer-events: none; /* Important: do not block dragging */
}

/* Falling Item Sprite */
.falling-item {
    position: absolute;
    width: 60px;
    height: 60px;
    object-fit: contain;
    will-change: transform; /* Optimize animation */
}

/* Fish Item */
.fish-item {
    position: absolute;
    width: 60px;
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 45px; /* Use emoji */
    will-change: transform;
}

.player-bag-area {
    position: absolute;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 120px;
    height: 120px;
    pointer-events: auto; /* Enable touch dragging */
    touch-action: none; /* Required for dragging */
}

.trash-bag-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 10px 10px rgba(0,0,0,0.5));
    transition: transform 0.1s;
}

.trash-bag-img.catching {
    transform: scale(1.1) translateY(-10px);
}

.mediapipe-canvas {
    position: absolute;
    top: 10px;
    left: 50%;
    transform: translateX(-50%);
    width: 160px; /* Small debug view */
    height: 120px;
    border-radius: 10px;
    opacity: 0.5;
    z-index: 100;
    pointer-events: none;
    border: 2px solid white;
}

/* =========================================
   Leaderboard Screen
   ========================================= */
.game-over-title {
    color: #e74c3c;
}

.final-score-text {
    font-size: 1.2rem;
}

.final-score-val {
    font-size: 4rem;
    color: #27ae60;
    font-family: 'Itim', cursive;
    margin-bottom: 20px;
}

.high-score-card {
    background: #fff3cd;
    border: 2px solid #ffeeba;
    padding: 15px;
    border-radius: 10px;
    margin-bottom: 20px;
    color: #856404;
}

.star-animation {
    font-size: 2rem;
    animation: bounce 1s infinite alternate;
}

@keyframes bounce {
    from { transform: translateY(0); }
    to { transform: translateY(-10px); }
}

.leaderboard-table-container {
    max-height: 250px;
    overflow-y: auto;
    border-radius: 10px;
    border: 1px solid #ddd;
    margin-bottom: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th, td {
    padding: 12px;
    text-align: center;
    border-bottom: 1px solid #ddd;
}

th {
    background-color: #34495e;
    color: white;
    position: sticky;
    top: 0;
}

tr:nth-child(even) {
    background-color: #f2f2f2;
}

tr:hover {
    background-color: #e8f4f8;
}

.actions-group {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

/* =========================================
   Notifications
   ========================================= */
.game-notice {
    position: absolute;
    font-size: 2rem;
    font-weight: bold;
    font-family: 'Itim', cursive;
    pointer-events: none;
    animation: floatUp 1s forwards;
    z-index: 20;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.5);
}

.notice-good { color: #2ecc71; }
.notice-bad { color: #e74c3c; }

@keyframes floatUp {
    0% { opacity: 1; transform: translateY(0) scale(1); }
    100% { opacity: 0; transform: translateY(-50px) scale(1.5); }
}
