/* Base styles */
body {
    background: linear-gradient(135deg, var(--bs-primary), var(--bs-success));
    background-size: 400% 400%;
    animation: gradientBG 15s ease infinite;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.container {
    max-width: 100%;
    padding: 0 15px;
}

#game-board {
    display: grid;
    gap: 2vmin;
    max-width: 90vmin;
    margin: 0 auto;
}

body[data-level="beginner"] #game-board,
body[data-level="medium"] #game-board,
body[data-level="advanced"] #game-board {
    grid-template-columns: repeat(5, 1fr);
}

.card {
    aspect-ratio: 3/4;
    background-size: cover;
    background-position: center;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.6s cubic-bezier(0.4, 0.0, 0.2, 1);
    transform-style: preserve-3d;
    position: relative;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.card .front,
.card .back {
    position: absolute;
    width: 100%;
    height: 100%;
    backface-visibility: hidden;
    border-radius: 8px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 2rem;
    color: var(--bs-white);
    transition: transform 0.6s cubic-bezier(0.4, 0.0, 0.2, 1);
}

.card .front {
    background-color: var(--bs-primary);
    transform: rotateY(180deg);
}

.card .back {
    background-image: url('../images/card-back.jpg');
    background-image: image-set(
        url('../images/card-back.webp') type('image/webp'),
        url('../images/card-back.jpg') type('image/jpeg')
    );
    background-size: cover;
    background-position: center;
}

.card.flipped {
    transform: rotateY(180deg);
}

@keyframes intensePulse {
    0% {
        transform: scale(1) rotate(0deg);
        box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.7);
    }
    50% {
        transform: scale(1.2) rotate(180deg);
        box-shadow: 0 0 30px 15px rgba(255, 255, 255, 0.9);
    }
    100% {
        transform: scale(1) rotate(360deg);
        box-shadow: 0 0 0 0 rgba(255, 255, 255, 0.7);
    }
}

.card.matched {
    animation: matchAnimation 1.5s ease-in-out, glowAnimation 2s infinite;
}

@keyframes glowAnimation {
    0%, 100% { box-shadow: 0 0 5px 2px rgba(255, 215, 0, 0.7); }
    50% { box-shadow: 0 0 20px 10px rgba(255, 215, 0, 0.9); }
}

.card.mismatch {
    animation: shakeAnimation 0.5s ease-in-out;
}

@keyframes shakeAnimation {
    0%, 100% { transform: translateX(0) rotateY(180deg); }
    20%, 60% { transform: translateX(-5px) rotateY(180deg); }
    40%, 80% { transform: translateX(5px) rotateY(180deg); }
}

@keyframes flipAnimation {
    0% { transform: rotateY(0) scale(1); }
    50% { transform: rotateY(90deg) scale(1.1); }
    100% { transform: rotateY(180deg) scale(1); }
}

@keyframes flipBackAnimation {
    0% { transform: rotateY(180deg) scale(1); }
    50% { transform: rotateY(90deg) scale(1.1); }
    100% { transform: rotateY(0) scale(1); }
}

@keyframes matchAnimation {
    0% { transform: scale(1) rotateY(180deg); }
    25% { transform: scale(1.1) rotateY(270deg); }
    50% { transform: scale(1.2) rotateY(360deg); }
    75% { transform: scale(1.1) rotateY(450deg); }
    100% { transform: scale(1) rotateY(540deg); }
}

#game-stats {
    margin-top: 4vmin;
}

#score, #moves {
    font-size: 1rem;
    margin-bottom: 2vmin;
}

#restart {
    background: linear-gradient(45deg, var(--bs-primary), var(--bs-info));
    border: none;
    color: white;
    padding: 10px 20px;
    border-radius: 5px;
    font-weight: bold;
    transition: all 0.3s ease;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    position: relative;
    overflow: hidden;
}

#restart:hover {
    background: linear-gradient(45deg, var(--bs-info), var(--bs-primary));
    transform: translateY(-2px);
    box-shadow: 0 6px 8px rgba(0, 0, 0, 0.15);
}

#restart:active {
    transform: translateY(1px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

#restart:not(:active) {
    animation: pulse 2s infinite;
}

#mute-button {
    background-color: var(--bs-secondary);
}

#mute-button:hover {
    background-color: var(--bs-secondary-dark);
}

.colorful-title {
    font-size: 3.5rem;
    font-weight: bold;
    text-shadow: 3px 3px 6px rgba(0, 0, 0, 0.5);
    margin-bottom: 6vmin;
    letter-spacing: 0.1em;
    display: flex;
    flex-direction: column;
    align-items: center;
    background: linear-gradient(45deg, #FF6B6B, #4ECDC4, #45B7D1, #FFA07A, #98D8C8);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    animation: gradientText 10s ease infinite;
}

.title-line {
    display: flex;
    justify-content: center;
}

.colorful-title span {
    display: inline-block;
    animation: float 3s ease-in-out infinite;
    margin: 0 0.1em;
}

@keyframes float {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-10px); }
}

@keyframes gradientText {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.char1, .char2, .char3, .char4, .char5, .char6, .char7, .char8, .char9 {
    display: inline-block;
    animation: float 3s ease-in-out infinite;
}

.char1 { animation-delay: 0.1s; color: #FF6B6B; }
.char2 { animation-delay: 0.2s; color: #4ECDC4; }
.char3 { animation-delay: 0.3s; color: #45B7D1; }
.char4 { animation-delay: 0.4s; color: #FFA07A; }
.char5 { animation-delay: 0.5s; color: #98D8C8; }
.char6 { animation-delay: 0.6s; color: #FF6B6B; }
.char7 { animation-delay: 0.7s; color: #4ECDC4; }
.char8 { animation-delay: 0.8s; color: #45B7D1; }
.char9 { animation-delay: 0.9s; color: #FFA07A; }

.flower {
    position: absolute;
    width: 5vmin;
    height: 5vmin;
    background: url('../images/flower.png') no-repeat center center / contain;
    background-image: image-set(
        url('../images/flower.webp') type('image/webp'),
        url('../images/flower.png') type('image/png')
    );
    pointer-events: none;
}

@keyframes flyLeftToRight {
    0% { left: -5vmin; }
    100% { left: 105vw; }
}

@keyframes flyRightToLeft {
    0% { left: 105vw; }
    100% { left: -5vmin; }
}

.flower.left-to-right {
    animation: flyLeftToRight 15s linear infinite;
}

.flower.right-to-left {
    animation: flyRightToLeft 15s linear infinite;
}

@keyframes gradientBG {
    0% { background-position: 0% 50%; }
    50% { background-position: 100% 50%; }
    100% { background-position: 0% 50%; }
}

.particle {
    position: absolute;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    pointer-events: none;
    animation: particle 1s ease-out forwards;
}

@keyframes particle {
    0% { transform: translate(0, 0) scale(1); opacity: 1; }
    100% { transform: translate(var(--tx), var(--ty)) scale(0); opacity: 0; }
}

.ripple {
    position: absolute;
    border-radius: 50%;
    background-color: rgba(255, 255, 255, 0.7);
    transform: scale(0);
    animation: ripple 0.6s linear;
    pointer-events: none;
}

@keyframes ripple {
    to {
        transform: scale(4);
        opacity: 0;
    }
}

@media (max-width: 768px) {
    body[data-level="beginner"] #game-board,
    body[data-level="medium"] #game-board,
    body[data-level="advanced"] #game-board {
        grid-template-columns: repeat(4, 1fr);
    }

    .colorful-title {
        font-size: 2.5rem;
    }
}

@media (max-width: 480px) {
    body[data-level="beginner"] #game-board,
    body[data-level="medium"] #game-board,
    body[data-level="advanced"] #game-board {
        grid-template-columns: repeat(3, 1fr);
    }

    .colorful-title {
        font-size: 2rem;
    }

    #score, #moves {
        font-size: 0.9rem;
    }

    #restart, #mute-button {
        font-size: 0.9rem;
    }
}

.btn-lg {
    font-size: 1.5rem;
    padding: 0.75rem 1.5rem;
}

.disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

.video-container {
    max-width: 800px;
    margin: 0 auto;
}

.video-wrapper {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    overflow: hidden;
    border-radius: 10px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
    transition: transform 0.3s ease;
}

.video-wrapper:hover {
    transform: scale(1.02);
}

.video-wrapper video {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
    object-position: center 70%;
}

.play-button {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 4rem;
    background: rgba(0, 0, 0, 0.5);
    border: none;
    color: white;
    padding: 1rem;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.3s ease;
    width: 100px;
    height: 100px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.play-button:hover {
    background: rgba(0, 0, 0, 0.7);
    transform: translate(-50%, -50%) scale(1.1);
}

.play-button i {
    transition: transform 0.3s ease;
}

.play-button:hover i {
    transform: scale(1.2);
}

@media (max-width: 768px) {
    .play-button {
        font-size: 3rem;
        width: 80px;
        height: 80px;
    }

    .video-wrapper {
        padding-bottom: 75%;
    }
}

@media (max-width: 480px) {
    .play-button {
        font-size: 2rem;
        width: 60px;
        height: 60px;
    }

    .video-wrapper {
        padding-bottom: 100%;
    }
}

.difficulty-btn {
    transition: all 0.3s ease;
    border-width: 2px;
    font-weight: bold;
    position: relative;
    overflow: hidden;
    color: white;
    text-shadow: 1px 1px 2px rgba(0, 0, 0, 0.5);
}

.difficulty-btn::before {
    content: '';
    position: absolute;
    top: 0;
    left: -100%;
    width: 100%;
    height: 100%;
    background: linear-gradient(
        120deg,
        transparent,
        rgba(255, 255, 255, 0.3),
        transparent
    );
    transition: all 0.6s;
}

.difficulty-btn:hover::before {
    left: 100%;
}

.difficulty-btn:hover {
    transform: translateY(-5px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.difficulty-btn:active {
    transform: translateY(-2px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.3);
}

.difficulty-btn i {
    transition: transform 0.3s ease;
}

.difficulty-btn:hover i {
    transform: scale(1.2);
}

.difficulty-btn[href*="beginner"] {
    background-color: #4CAF50;
    border-color: #45a049;
}

.difficulty-btn[href*="beginner"]:hover {
    background-color: #45a049;
}

.difficulty-btn[href*="medium"] {
    background-color: #FFC107;
    border-color: #ffa000;
    color: #333;
    text-shadow: 1px 1px 2px rgba(255, 255, 255, 0.5);
}

.difficulty-btn[href*="medium"]:hover {
    background-color: #ffa000;
}

.difficulty-btn[href*="advanced"] {
    background-color: #F44336;
    border-color: #d32f2f;
}

.difficulty-btn[href*="advanced"]:hover {
    background-color: #d32f2f;
}

/* New styles for the difficulty title */
.difficulty-title {
    border: 2px solid #ffffff;
    border-radius: 10px;
    padding: 10px 20px;
    display: inline-block;
    margin-bottom: 20px;
    color: #ffffff;
    background-color: rgba(0, 0, 0, 0.2);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

/* ===== 文字卡片渲染（主題包系統） ===== */
.card .front.card-face--text,
.card .back.card-face--text {
    font-size: clamp(1rem, 3vw, 1.6rem);
    font-weight: 700;
    text-align: center;
    line-height: 1.2;
    padding: 8px;
    color: #fff;
    text-shadow: 0 2px 6px rgba(0, 0, 0, 0.4);
    word-break: break-word;
    background-size: cover;
}

/* ===== 主題徽章（非預設主題顯示） ===== */
.theme-badge {
    text-align: center;
    margin: -10px auto 18px;
    padding: 6px 16px;
    background: rgba(255, 255, 255, 0.18);
    color: #fff;
    border-radius: 999px;
    display: inline-block;
    font-size: 0.95rem;
    font-weight: 600;
    backdrop-filter: blur(4px);
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
}
.theme-badge {
    display: block;
    width: fit-content;
}
.theme-badge__icon { margin-right: 4px; }

/* ===== 鍵盤無障礙 focus 樣式 ===== */
.card:focus-visible {
    outline: 3px solid #FFD700;
    outline-offset: 4px;
    box-shadow: 0 0 0 6px rgba(255, 215, 0, 0.35);
}
.card[role="button"] {
    cursor: pointer;
}
.btn:focus-visible,
.difficulty-btn:focus-visible,
.play-button:focus-visible {
    outline: 3px solid #FFD700;
    outline-offset: 3px;
}

/* ===== 完成遊戲彈窗 ===== */
.win-modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    animation: fadeIn 0.3s ease-out;
}
.win-modal[hidden] { display: none; }
.win-modal__card {
    background: linear-gradient(135deg, #ff8e53, #fe6b8b);
    color: #fff;
    padding: 36px 28px;
    border-radius: 18px;
    text-align: center;
    max-width: 360px;
    width: 90%;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    animation: popIn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.win-modal__card h2 {
    font-size: 2rem;
    margin: 0 0 16px;
    font-weight: 800;
}
.win-modal__stats {
    font-size: 1.1rem;
    line-height: 1.7;
    margin: 0 0 12px;
}
.win-modal__stats strong {
    font-size: 1.3em;
    color: #fff8e1;
}
.win-modal__best {
    font-size: 0.95rem;
    opacity: 0.95;
    margin: 0 0 20px;
    background: rgba(255, 255, 255, 0.18);
    padding: 8px 12px;
    border-radius: 8px;
    min-height: 1.6em;
}
.win-modal__buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
    flex-wrap: wrap;
}
@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}
@keyframes popIn {
    from { opacity: 0; transform: scale(0.7); }
    to { opacity: 1; transform: scale(1); }
}

/* ===== 計時器顯示 ===== */
#timer {
    color: #fff;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

/* ===== 個人最佳紀錄 ===== */
.difficulty-grid {
    align-items: flex-start;
}
.difficulty-cell {
    display: flex;
    flex-direction: column;
    align-items: center;
}
.best-record {
    margin-top: 4px;
    padding: 4px 12px;
    font-size: 0.85rem;
    color: rgba(255, 255, 255, 0.7);
    background: rgba(0, 0, 0, 0.25);
    border-radius: 8px;
    min-width: 140px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}
.best-record--has-record {
    color: #FFD700;
    background: rgba(0, 0, 0, 0.4);
    font-weight: 600;
}
.best-record strong {
    color: #fff;
    font-size: 1.05em;
}
#reset-records {
    opacity: 0.7;
    transition: opacity 0.2s;
}
#reset-records:hover {
    opacity: 1;
}

/* ===== 阿凱老師頁尾署名 ===== */
.site-credit {
    text-align: center;
    padding: 24px 16px 28px;
    font-size: 13px;
    color: rgba(255, 255, 255, 0.7);
    font-family: system-ui, -apple-system, "Segoe UI", "Microsoft JhengHei", sans-serif;
    margin-top: auto;
}
.site-credit__heart {
    display: inline-block;
    margin: 0 2px;
    letter-spacing: normal;
    text-transform: none;
    vertical-align: -1px;
    filter: saturate(1.15);
}
.site-credit__author {
    color: inherit;
    text-decoration: none;
    border-bottom: 1px dotted currentColor;
    padding-bottom: 1px;
    transition: color 0.2s, border-color 0.2s;
    font-weight: 600;
}
.site-credit__author:hover,
.site-credit__author:focus-visible {
    color: #FFD700;
    border-bottom-color: #FFD700;
}
@media print {
    .site-credit { display: none; }
}
