/* ===========================================
   えあわせ (Memory Match)
   =========================================== */

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

html,
body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: 'Zen Maru Gothic', sans-serif;
    touch-action: none;
    user-select: none;
}

body {
    background: linear-gradient(135deg, #FFF9C4 0%, #FFECB3 100%);
}

.home-btn {
    position: fixed;
    top: 12px;
    left: 12px;
    width: 48px;
    height: 48px;
    background: white;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 1.4rem;
    text-decoration: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 9999;
    border: 3px solid #FFCA28;
}

#game-container {
    width: 100%;
    height: 100%;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    position: relative;
    overflow: hidden;
}

/* Header */
.game-header {
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    z-index: 100;
    flex-shrink: 0;
}

.level-badge {
    background: #FF6F00;
    color: white;
    padding: 8px 18px;
    border-radius: 20px;
    font-weight: bold;
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 1.1rem;
    box-shadow: 0 3px 0 #E65100;
}

.score-display {
    background: white;
    padding: 8px 18px;
    border-radius: 20px;
    font-weight: bold;
    color: #5D4037;
    box-shadow: 0 3px 0 #D7CCC8;
}

/* Game Area */
.game-area {
    flex: 1;
    display: flex;
    justify-content: flex-start; /* Start from left for horizontal scroll */
    align-items: center;
    padding: 20px;
    overflow-x: auto; /* Enable horizontal scroll */
    overflow-y: hidden;
    width: 100%;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
}

.card-grid {
    display: grid;
    grid-template-rows: repeat(2, 1fr); /* Fixed 2 rows */
    grid-auto-flow: column; /* Expand columns to the right */
    gap: 15px;
    height: 80vh; /* Fill height to allow 2 rows */
    width: fit-content; /* Expand width based on columns */
    margin: 0 auto; /* Center if it fits, scroll if it doesn't */
    min-width: min-content;
    padding-bottom: 10px;
}

/* Card Styles */
.card {
    background-color: transparent;
    perspective: 1000px;
    cursor: pointer;
    aspect-ratio: 1/1;
    height: 100%; /* Fill the row height */
    width: auto;  /* Width follows aspect ratio */
    position: relative;
    -webkit-tap-highlight-color: transparent;
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    text-align: center;
    transition: transform 0.4s;
    transform-style: preserve-3d;
    box-shadow: 0 5px 15px rgba(0,0,0,0.1);
    border-radius: 12px;
}

.card.flipped .card-inner {
    transform: rotateY(180deg);
}

.card.matched .card-inner {
    transform: rotateY(180deg);
    animation: matchPulse 0.5s ease-out;
}

@keyframes matchPulse {
    0% { transform: rotateY(180deg) scale(1); }
    50% { transform: rotateY(180deg) scale(1.15); box-shadow: 0 0 20px #FFD700; }
    100% { transform: rotateY(180deg) scale(1); }
}

.card-front, .card-back {
    position: absolute;
    width: 100%;
    height: 100%;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 4px solid white;
}

.card-back {
    background: linear-gradient(135deg, #29B6F6 0%, #03A9F4 100%);
    color: white;
}

/* Pattern for back */
.card-back::after {
    content: '★';
    font-size: 2rem;
    opacity: 0.5;
}

.card-front {
    background: white;
    color: #333;
    transform: rotateY(180deg);
    font-size: clamp(2rem, 8vh, 4rem); /* Responsive font size */
}

/* Responsive Grid Adjustments handled in JS mainly, but safe defaults here */

/* Overlays */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(5px);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 2000;
}

.overlay.hidden {
    display: none !important;
}

.message-modal {
    background: white;
    border-radius: 30px;
    padding: 30px;
    text-align: center;
    animation: modalPop 0.5s cubic-bezier(0.68, -0.55, 0.265, 1.55);
    max-width: 320px;
    width: 90%;
}

@keyframes modalPop {
    0% { transform: scale(0); }
    100% { transform: scale(1); }
}

.modal-icon {
    font-size: 4rem;
    margin-bottom: 10px;
}

.message-modal h2 {
    font-size: 1.8rem;
    margin-bottom: 10px;
    color: #FF6F00;
}

.stars {
    font-size: 2rem;
    margin-bottom: 15px;
}

.btn-primary {
    background: linear-gradient(135deg, #FF6F00 0%, #E65100 100%);
    color: white;
    border: none;
    padding: 15px 40px;
    font-size: 1.2rem;
    font-weight: bold;
    border-radius: 50px;
    cursor: pointer;
    font-family: inherit;
    box-shadow: 0 4px 0 #BF360C;
    transition: transform 0.1s;
}

.btn-primary:active {
    transform: translateY(4px);
    box-shadow: none;
}

/* Landscape Adjustments */
@media screen and (max-height: 500px) {
    #game-container {
        margin-left: 80px !important;
        width: calc(100% - 80px) !important;
    }
    .game-header {
        padding: 5px 20px !important;
    }
    .level-badge {
        padding: 4px 14px;
        font-size: 0.9rem;
    }
    .score-display {
        padding: 4px 14px;
        font-size: 0.9rem;
    }
    .card-grid {
        height: 70vh;
        gap: 10px;
    }
    .message-modal {
        padding: 15px;
        border-radius: 20px;
        max-width: 400px; /* Wider but shorter */
    }
    .modal-icon {
        font-size: 2.5rem;
        margin-bottom: 5px;
    }
    .message-modal h2 {
        font-size: 1.4rem;
        margin-bottom: 5px;
    }
    .btn-primary {
        padding: 10px 30px;
        font-size: 1rem;
    }
    .stars {
        margin-bottom: 10px;
    }
}