/* ============================================
   Look & Find Game - Main Stylesheet
   --------------------------------------------
   Mau, chu, bo goc va do do bong deu lay tu /assets/app.css; lop `app-tim`
   tren <body> chon bang mau xanh troi. File nay chi giu phan rieng cua tro
   choi: khung choi, sprite va cac hieu ung.

   iPad doi cua nha chay iOS 12 Safari: khong `gap` trong flexbox, khong
   `aspect-ratio`, khong `dvh`, khong `:has()`. `clamp()` luon di kem mot dong
   thuong dat ngay tren no.
   ============================================ */

html, body {
    width: 100%;
    height: 100vh;
    margin: 0;
    padding: 0;
    overflow: hidden;
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-text-size-adjust: 100%;
    position: fixed;
    touch-action: manipulation;
}

/* Nen san choi va mau bao "da tim thay" khong co trong bo dung chung: san
   choi phai khac han moi chu de trang, con dau tich phai la xanh la du mau
   nhan cua app doi sang mau gi. */
body.app-tim {
    --sprite-cell-size: 204.8px;
    --scene-bg: #D4F1F4;
    --scene-dot: #B8E0E8;
    --success-color: #8FD081;
    --dark-success: #6BA850;
    --safe-top: env(safe-area-inset-top, 0);
    --safe-bottom: env(safe-area-inset-bottom, 0);
    --safe-left: env(safe-area-inset-left, 0);
    --safe-right: env(safe-area-inset-right, 0);
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: var(--font-body);
    background-color: var(--paper);
    color: var(--ink);
    height: 100%;
    width: 100%;
    overflow: hidden;
}

/* ============================================
   Game Container Layout
   ============================================ */

#game-container {
    display: flex;
    width: 100%;
    height: 100vh;
    background-color: var(--surface);
    overflow: hidden;
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
}

#sidebar {
    width: 120px;
    width: clamp(80px, 15vw, 220px);
    background: rgba(255, 255, 255, 0.4);
    backdrop-filter: blur(15px);
    -webkit-backdrop-filter: blur(15px);
    padding: 15px 10px;
    padding-left: calc(15px + var(--safe-left));
    padding-top: calc(15px + var(--safe-top));
    padding-bottom: calc(15px + var(--safe-bottom));
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    border-right: 1px solid var(--edge);
    box-shadow: var(--shadow);
    z-index: 100;
}

h1 {
    font-family: var(--font-head);
    color: var(--accent-deep);
    margin-top: 0;
    font-size: 0.9em;
    margin-bottom: 4px;
    display: none;
}

p {
    font-size: 0.8em;
    color: var(--ink-soft);
    line-height: 1.2;
    margin-bottom: 6px;
    display: none;
}

/* ============================================
   Items List & Cards
   ============================================ */

/* `gap` trong flexbox chi co tu Safari 14.1 — khoang cach giua the dung margin. */
.items-list {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow-y: auto;
    padding-right: 2px;
}

.item-card + .item-card {
    margin-top: var(--s-2);
}

.item-card {
    background: var(--surface);
    border-radius: var(--r-lg);
    padding: var(--s-2);
    box-shadow: var(--shadow);
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    position: relative;
    flex: 0 0 auto;
    border: 1px solid var(--edge);
}

.item-card:hover {
    transform: translateY(-5px) scale(1.02);
    box-shadow: var(--shadow-lift);
    border-color: var(--accent);
}

/* Hop vuong bang padding-top thay cho `aspect-ratio` (Safari 15 tro len). */
.item-preview {
    position: relative;
    width: 100%;
    height: 0;
    padding-top: 100%;
    background: var(--surface-2);
    border-radius: var(--r-md);
    margin-bottom: 0;
    overflow: hidden;
}

.item-preview img {
    position: absolute;
    top: 5px;
    right: 5px;
    bottom: 5px;
    left: 5px;
    width: auto;
    height: auto;
    max-width: calc(100% - 10px);
    max-height: calc(100% - 10px);
    margin: auto;
    object-fit: contain;
}

.item-name {
    font-family: var(--font-head);
    font-weight: 700;
    color: var(--ink);
    font-size: 0.6em;
    display: none;
    margin-top: 2px;
}

.item-card.found {
    background-color: var(--success-color);
    animation: sidebarItemFound 0.8s ease-in-out forwards;
    pointer-events: none;
}

@keyframes sidebarItemFound {
    0% { transform: scale(1); opacity: 1; }
    30% { transform: scale(1.1); background-color: var(--success-color); }
    100% { transform: scale(0); opacity: 0; }
}

.item-card.found .item-name {
    color: white;
}

.item-card.found::after {
    content: '✓';
    position: absolute;
    top: -5px;
    right: -5px;
    background-color: var(--dark-success);
    color: white;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9em;
    font-weight: bold;
}

/* ============================================
   Scene Area
   ============================================ */

#scene-area {
    position: relative;
    flex-grow: 1;
    background-color: var(--scene-bg);
    background-image: radial-gradient(var(--scene-dot) 20%, transparent 20%);
    background-size: 30px 30px;
    overflow: hidden;
}

/* ============================================
   Sprite Objects
   ============================================ */

.sprite-object {
    position: absolute;
    max-width: 120px;
    max-height: 120px;
    width: auto;
    height: auto;
    cursor: pointer;
    transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    filter: drop-shadow(2px 4px 6px rgba(0,0,0,0.2));
    image-rendering: high-quality;
    object-fit: contain;
}

.sprite-object:hover {
    transform: scale(1.1);
    z-index: 100;
}

.sprite-object.dragging {
    cursor: grabbing;
    transition: none;
}

.sprite-object.found {
    animation: popDisappear 0.4s ease-out forwards;
    pointer-events: none;
}

/* ============================================
   Animations
   ============================================ */

@keyframes popDisappear {
    0% {
        transform: scale(1.1);
    }
    50% {
        transform: scale(1.4) rotate(10deg);
        opacity: 1;
    }
    100% {
        transform: scale(0) rotate(-10deg);
        opacity: 0;
    }
}

.confetti {
    position: absolute;
    width: 10px;
    height: 10px;
    background-color: #FFD700;
    animation: confettiFall 2.5s ease-out forwards;
}

@keyframes confettiFall {
    0% {
        opacity: 1;
        transform: translate(0, 0) rotate(0deg);
    }
    100% {
        opacity: 0;
        transform: translate(var(--tx), var(--ty)) rotate(360deg);
    }
}

.celebrate-text {
    position: absolute;
    font-size: 3em;
    font-weight: bold;
    animation: celebratePopup 2.5s ease-out forwards;
    pointer-events: none;
}

@keyframes celebratePopup {
    0% {
        opacity: 1;
        transform: scale(0) rotate(0deg);
    }
    50% {
        opacity: 1;
        transform: scale(1.2) rotate(10deg);
    }
    80% {
        opacity: 1;
        transform: scale(0.8) rotate(-5deg) translateY(-150px);
    }
    100% {
        opacity: 0;
        transform: scale(0.5) rotate(-10deg) translateY(-300px);
    }
}

@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(255, 215, 0, 0.7);
    }
    50% {
        box-shadow: 0 0 0 30px rgba(255, 215, 0, 0);
    }
    100% {
        box-shadow: 0 0 0 0 rgba(255, 215, 0, 0);
    }
}

@keyframes fireworksExplosion {
    0% {
        opacity: 1;
        transform: translate(0, 0) rotate(0deg) scale(1);
    }
    50% {
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: translate(var(--tx), var(--ty)) rotate(var(--rotation)) scale(0);
    }
}

.hint-glow {
    animation: hintGlowMove 4s ease-in-out infinite;
    filter: drop-shadow(0 0 15px #FFD700) brightness(1.2) !important;
    z-index: 5000 !important;
}

@keyframes hintGlowMove {
    0%, 100% { transform: scale(1) rotate(0deg); }
    50% { transform: scale(1.3) rotate(15deg); }
}

/* ============================================
   HUD & Controls
   ============================================ */

.game-hud {
    position: fixed;
    top: 15px;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    z-index: 1000;
    pointer-events: none;
}

.hud-item + .hud-item { margin-left: var(--s-5); }

.hud-item {
    background: rgba(255, 255, 255, 0.85);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    padding: var(--s-2) var(--s-5);
    border-radius: var(--r-pill);
    box-shadow: var(--shadow);
    display: flex;
    align-items: center;
    font-family: var(--font-head);
    font-weight: 700;
    font-size: 1.2rem;
    color: var(--ink);
    border: 2px solid var(--surface);
}

.hud-item > span + span { margin-left: var(--s-3); }

.timer-icon { color: var(--accent); }
.score-icon { color: #FFD700; }

#refresh-btn {
    display: none;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 25px 60px;
    font-family: var(--font-head);
    font-size: 2.5rem;
    background: var(--accent-deep);
    color: var(--accent-ink);
    border: none;
    border-radius: var(--r-pill);
    cursor: pointer;
    box-shadow: var(--shadow-lift);
    font-weight: 900;
    z-index: 10000;
    animation: bounceIn 0.8s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    transition: all 0.3s;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.2);
}

@keyframes bounceIn {
    0% { transform: translate(-50%, -50%) scale(0); }
    70% { transform: translate(-50%, -50%) scale(1.1); }
    100% { transform: translate(-50%, -50%) scale(1); }
}

#refresh-btn:hover {
    transform: translate(-50%, -50%) scale(1.05);
    background: var(--accent);
}

.control-buttons {
    position: fixed;
    top: 15px;
    right: 15px;
    display: flex;
    z-index: 1000;
}

.control-btn + .control-btn { margin-left: var(--s-2); }

.control-btn {
    width: var(--tap);
    height: var(--tap);
    border-radius: var(--r-md);
    background: var(--surface);
    color: var(--ink);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    cursor: pointer;
    touch-action: manipulation;
    box-shadow: var(--shadow);
    transition: background var(--dur) ease, border-color var(--dur) ease, transform var(--dur) ease;
    border: 2px solid var(--edge);
}

.control-btn:hover {
    background: var(--accent-soft);
    border-color: var(--accent);
    transform: translateY(-3px);
}

@keyframes popIn {
    0% {
        opacity: 0;
        transform: translate(-50%, -50%) scale(0);
    }
    100% {
        opacity: 1;
        transform: translate(-50%, -50%) scale(1);
    }
}

/* ============================================
   Overlay
   ============================================ */

.overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    display: none;
    z-index: 9999;
}

.overlay.show {
    display: block;
}

/* ============================================
   Loader
   ============================================ */

.loader {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 9999;
}

.loader::after {
    content: "";
    display: block;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 4px solid var(--accent);
    border-color: var(--accent) transparent var(--accent) transparent;
    animation: spin 1.2s linear infinite;
}

@keyframes spin {
    0% {
        transform: rotate(0deg);
    }
    100% {
        transform: rotate(360deg);
    }
}

/* ============================================
   Responsive Design - Tablet (768px)
   ============================================ */

@media (max-width: 768px) {
    #game-container {
        flex-direction: column;
    }

    #sidebar {
        width: 100%;
        height: auto;
        max-height: 35vh;
        border-right: none;
        border-bottom: 4px solid var(--accent);
        padding: 12px;
        padding-left: calc(12px + var(--safe-left));
        padding-right: calc(12px + var(--safe-right));
        overflow-x: auto;
        overflow-y: hidden;
        min-width: auto;
        max-width: none;
    }

    h1 {
        font-size: 1.3em;
        margin-bottom: 8px;
        display: block;
    }

    p {
        font-size: 0.95em;
        margin-bottom: 10px;
        display: block;
    }

    .items-list {
        flex-direction: row;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        padding: 5px 0;
        padding-right: 8px;
    }

    /* Hang ngang thi khoang cach doi sang canh phai. */
    .item-card + .item-card {
        margin-top: 0;
        margin-left: var(--s-3);
    }

    .item-card {
        flex: 0 0 auto;
        min-width: 100px;
        padding: 6px;
        border-radius: var(--r-sm);
    }

    .item-preview {
        border: 2px solid var(--accent);
    }

    .item-name {
        font-size: 0.75em;
        display: block;
        margin-top: 3px;
    }

    #fullscreen-btn {
        padding: 8px 14px;
        font-size: 1em;
        min-width: 45px;
    }

    #reload-btn {
        padding: 8px 12px;
        font-size: 0.9em;
        right: 62px;
    }
}

/* ============================================
   Responsive Design - Mobile (600px)
   ============================================ */

@media (max-width: 600px) {
    #sidebar {
        max-height: 40vh;
        padding: 12px;
        padding-left: calc(12px + var(--safe-left));
        padding-right: calc(12px + var(--safe-right));
    }

    h1 {
        font-size: 1.1em;
        display: block;
    }

    p {
        font-size: 0.9em;
        display: block;
    }

    .item-card + .item-card {
        margin-left: var(--s-3);
    }

    .item-card {
        min-width: 90px;
        padding: 6px;
    }

    .item-name {
        font-size: 0.75em;
        display: block;
    }

    #fullscreen-btn {
        padding: 8px 12px;
        font-size: 0.9em;
    }

    #reload-btn {
        padding: 8px 10px;
        font-size: 0.85em;
        right: 55px;
    }
}

/* ============================================
   Responsive Design - Mobile (480px)
   ============================================ */

@media (max-width: 480px) {
    html, body {
        position: fixed;
        width: 100vw;
        height: 100vh;
        overflow: hidden;
    }

    #game-container {
        height: 100vh;
    }

    #sidebar {
        max-height: 45vh;
        padding: 10px;
        padding-bottom: calc(10px + var(--safe-bottom));
        padding-left: calc(10px + var(--safe-left));
        padding-right: calc(10px + var(--safe-right));
    }

    h1 {
        font-size: 1em;
        margin-bottom: 5px;
    }

    p {
        font-size: 0.85em;
        margin-bottom: 8px;
    }

    .item-card + .item-card {
        margin-left: var(--s-2);
    }

    .item-card {
        min-width: 85px;
        padding: 5px;
    }

    .item-preview {
        border: 2px solid var(--accent);
    }

    .item-name {
        font-size: 0.7em;
    }

    .sprite-object {
        max-width: 80px;
        max-height: 80px;
    }

    #refresh-btn {
        padding: 15px 40px;
        font-size: 1.5em;
        min-height: 50px;
        min-width: 200px;
    }

    #fullscreen-btn {
        padding: 8px 10px;
        font-size: 0.85em;
        top: 10px;
        right: 10px;
    }

    #reload-btn {
        padding: 8px 8px;
        font-size: 0.8em;
        right: 50px;
        top: 10px;
    }
}
