:root {
    --bg-color: #FFF8EC;
    --text-main: #333;
    --text-sub: #666;
    --card-bg: #FFFFFF;
}

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

body {
    font-family: 'Mali', 'Noto Sans Thai', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-main);
    touch-action: manipulation;
    overscroll-behavior: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
}

#app {
    width: 100%;
    height: 100%;
    position: relative;
}

.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: none;
    flex-direction: column;
    padding: 20px;
    overflow-y: auto;
}

.screen.active {
    display: flex;
}

.view {
    display: none;
    flex: 1;
    flex-direction: column;
}

.view.active {
    display: flex;
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-shrink: 0;
}

h1 {
    font-size: 24px;
    font-weight: 600;
}

/* Grid for Home and Category Explore */
.grid-container {
    display: grid;
    gap: 20px;
    flex: 1;
    align-content: start;
}

#home-categories {
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}

.explore-grid {
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(2, 1fr);
    height: 100%;
}

@media (max-aspect-ratio: 1/1) {
    .explore-grid {
        grid-template-columns: repeat(2, 1fr);
        grid-template-rows: repeat(3, 1fr);
    }
}

/* Cards */
.card {
    background: var(--card-bg);
    border-radius: 28px;
    padding: 15px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-shadow: 0 8px 16px rgba(0,0,0,0.08);
    cursor: pointer;
    transition: transform 0.2s ease;
    min-height: 150px;
    border: 4px solid transparent;
}

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

.card img {
    width: 100px;
    height: 100px;
    object-fit: contain;
    margin-bottom: 10px;
}

.card .title-th {
    font-size: 24px;
    font-weight: 600;
}

.card .title-en {
    font-size: 16px;
    color: var(--text-sub);
}

.fallback-circle {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    display: none;
    align-items: center;
    justify-content: center;
    font-size: 24px;
    color: white;
    font-weight: bold;
    margin-bottom: 10px;
}

/* Buttons */
button {
    font-family: inherit;
    border: none;
    background: none;
    cursor: pointer;
    outline: none;
}

#settings-btn {
    font-size: 32px;
    padding: 10px;
    min-width: 64px;
    min-height: 64px;
}

.nav-btn {
    font-size: 40px;
    width: 64px;
    height: 64px;
    background: white;
    border-radius: 50%;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    justify-content: center;
}

.mode-switch {
    display: flex;
    background: white;
    border-radius: 30px;
    padding: 5px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
}

.mode-btn {
    padding: 10px 20px;
    font-size: 20px;
    border-radius: 25px;
    color: var(--text-sub);
}

.mode-btn.active {
    background: #FFE082;
    color: #333;
    font-weight: bold;
}

.action-btn {
    background: #4CAF50;
    color: white;
    font-size: 24px;
    padding: 15px 30px;
    border-radius: 30px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    min-height: 64px;
}

/* Modals */
.modal {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 248, 236, 0.95);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.modal.active {
    display: flex;
}

#close-modal-btn {
    position: absolute;
    top: 20px;
    left: 20px;
}

.modal-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    height: 100%;
    justify-content: center;
}

.expanded-card {
    background: white;
    border-radius: 40px;
    padding: 40px;
    box-shadow: 0 12px 24px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 30px;
    max-width: 90%;
    max-height: 70%;
    transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.expanded-card.bounce {
    animation: bounce 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.expanded-card img {
    width: 250px;
    height: 250px;
    object-fit: contain;
    margin-bottom: 20px;
}

.expanded-card .fallback-circle {
    width: 250px;
    height: 250px;
    font-size: 48px;
}

.expanded-card h2 {
    font-size: 64px;
    margin-bottom: 10px;
}

.expanded-card h3 {
    font-size: 32px;
    color: var(--text-sub);
}

.jiggle {
    animation: jiggle 0.5s ease-in-out infinite alternate;
}

/* Quiz Mode */
.quiz-controls {
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
}

.quiz-container {
    display: flex;
    gap: 30px;
    justify-content: center;
    align-items: center;
    flex: 1;
}

.quiz-card {
    width: 40%;
    max-width: 300px;
    aspect-ratio: 1;
    background: white;
    border-radius: 40px;
    padding: 20px;
    box-shadow: 0 12px 24px rgba(0,0,0,0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    border: 8px solid transparent;
}

.quiz-card img {
    width: 80%;
    height: 80%;
    object-fit: contain;
}

.quiz-card .fallback-circle {
    width: 80%;
    height: 80%;
    font-size: 40px;
}

.glow {
    box-shadow: 0 0 30px #FFD54F;
    border-color: #FFD54F;
}

.shake {
    animation: shake 0.4s ease-in-out;
}

/* End Screen */
.end-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    text-align: center;
}

.end-stars {
    font-size: 64px;
    margin-bottom: 20px;
}

.end-content h2 {
    font-size: 40px;
    color: #FF9800;
}

.end-content p {
    font-size: 24px;
    margin-bottom: 30px;
}

.recent-container {
    display: flex;
    gap: 15px;
    margin-bottom: 40px;
}

.recent-card {
    background: white;
    border-radius: 20px;
    padding: 10px;
    width: 120px;
    box-shadow: 0 4px 8px rgba(0,0,0,0.1);
    display: flex;
    flex-direction: column;
    align-items: center;
}

.recent-card img {
    width: 60px;
    height: 60px;
    object-fit: contain;
}

.recent-card .fallback-circle {
    width: 60px;
    height: 60px;
    font-size: 20px;
}

.recent-card span {
    font-size: 16px;
    font-weight: 600;
    margin-top: 5px;
}

/* Hold Button */
.hold-btn {
    position: relative;
    background: #E0E0E0;
    border-radius: 40px;
    padding: 20px 40px;
    overflow: hidden;
    color: var(--text-sub);
    font-size: 18px;
}

.progress-ring {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 0%;
    background: #4CAF50;
    opacity: 0.3;
    transition: width 0.1s linear;
}

.btn-text {
    position: relative;
    z-index: 1;
}

/* Settings */
.settings-content {
    background: white;
    padding: 40px;
    border-radius: 30px;
    width: 90%;
    max-width: 500px;
    box-shadow: 0 12px 24px rgba(0,0,0,0.15);
}

.settings-content h2 {
    margin-bottom: 20px;
    font-size: 32px;
}

.settings-group {
    margin-bottom: 20px;
}

.settings-group label {
    font-size: 20px;
    display: block;
    margin-bottom: 10px;
}

.time-options {
    display: flex;
    gap: 10px;
}

.time-btn {
    flex: 1;
    padding: 15px;
    font-size: 20px;
    border-radius: 15px;
    background: #F5F5F5;
}

.time-btn.active {
    background: #2196F3;
    color: white;
}

.print-link {
    display: block;
    text-align: center;
    background: #9C27B0;
    color: white;
    padding: 15px;
    border-radius: 15px;
    text-decoration: none;
    font-size: 20px;
}

.advice {
    background: #FFF8E1;
    padding: 15px;
    border-radius: 15px;
    margin-bottom: 20px;
    font-size: 16px;
    line-height: 1.5;
}

/* Effects */
#effects-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    z-index: 200;
    overflow: hidden;
}

.particle {
    position: absolute;
    font-size: 40px;
    animation: floatUp 1.5s ease-out forwards;
}

/* Animations */
@keyframes bounce {
    0% { transform: scale(0.5); }
    50% { transform: scale(1.05); }
    100% { transform: scale(1); }
}

@keyframes jiggle {
    from { transform: rotate(-3deg); }
    to { transform: rotate(3deg); }
}

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

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

@media (prefers-reduced-motion: reduce) {
    .card { transition: none; }
    .card:active { transform: none; }
    .expanded-card { transition: none; }
    .expanded-card.bounce { animation: none; transform: scale(1); }
    .jiggle { animation: none; }
    .shake { animation: none; }
    .particle { animation-duration: 0s; opacity: 0; }
}

/* ===== Claude pass · ขยายรูปให้เต็มการ์ด (เด็กต้องเห็นรูปก่อนตัวหนังสือ) ===== */
img { mix-blend-mode: multiply; } /* กลืนพื้นขาวของภาพเข้ากับการ์ด ไม่เห็นกรอบเทา */

.screen { padding: 16px 20px; }
.view.active { min-height: 0; }

#home-categories {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-content: stretch;
    gap: 20px;
    min-height: 0;
}
#home-categories .card {
    flex: 0 0 calc((100% - 40px) / 3);
    height: calc((100% - 20px) / 2);
}
.card { min-height: 0; overflow: hidden; }
.card img {
    flex: 1 1 auto;
    width: 100%;
    height: auto;
    min-height: 0;
    margin-bottom: 6px;
}
.card .title-th { font-size: clamp(22px, 3.2vw, 34px); line-height: 1.3; }
.card .title-en { font-size: clamp(14px, 1.8vw, 20px); }
.explore-grid { min-height: 0; grid-auto-rows: 1fr; }

.quiz-card { width: 42%; max-width: 400px; }
.quiz-card img { width: 90%; height: 90%; }
.expanded-card img { width: min(300px, 40vh); height: min(300px, 40vh); }
.recent-card { width: 140px; }
.recent-card img { width: 96px; height: 96px; }

@media (max-aspect-ratio: 1/1) {
    #home-categories .card {
        flex-basis: calc((100% - 20px) / 2);
        height: calc((100% - 40px) / 3);
    }
    .quiz-container { flex-direction: column; }
    .quiz-card { width: 70%; max-width: 340px; }
}

/* ===== ซับ + ปุ่มเลื่อนภาพ ===== */
#caption {
    position: fixed;
    left: 50%;
    bottom: max(16px, env(safe-area-inset-bottom));
    transform: translate(-50%, 20px);
    max-width: min(92vw, 900px);
    padding: 12px 28px;
    border-radius: 28px;
    background: rgba(255, 255, 255, 0.96);
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
    font-size: clamp(22px, 3.2vw, 34px);
    font-weight: 600;
    line-height: 1.35;
    text-align: center;
    color: #3a3a3a;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease, transform 0.25s ease;
    z-index: 150;
}
#caption.show { opacity: 1; transform: translate(-50%, 0); }

#card-modal { touch-action: none; } /* ให้ปัดซ้ายขวาได้ ไม่โดนเบราว์เซอร์แย่ง */
#close-modal-btn { width: 80px; height: 80px; font-size: 44px; z-index: 2; }
.modal-content { padding-bottom: 90px; } /* เว้นที่ให้ซับ */
.side-btn {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 96px;
    height: 96px;
    border-radius: 50%;
    background: white;
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
    font-size: 40px;
    color: #FF9800;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 2;
}
.side-btn.prev { left: 24px; }
.side-btn.next { right: 24px; }
.side-btn:active, #hunt-btn:active { transform: translateY(-50%) scale(0.92); }
#hunt-btn:active { transform: scale(0.94); }
#hunt-btn.speaking { animation: pulse 0.9s ease-in-out infinite alternate; }
@keyframes pulse { from { transform: scale(1); } to { transform: scale(1.06); } }

@media (max-aspect-ratio: 1/1) {
    .side-btn { top: auto; bottom: 110px; transform: none; width: 80px; height: 80px; }
    .side-btn:active { transform: scale(0.92); }
}
@media (prefers-reduced-motion: reduce) {
    #hunt-btn.speaking { animation: none; }
    #caption { transition: none; }
}
/* fix: fixed+left:50% ทำให้กล่องกว้างได้แค่ครึ่งจอ → ตัดบรรทัดเร็ว */
#caption { width: max-content; }
#quiz-view, #explore-view { padding-bottom: 84px; } /* เว้นที่ให้ซับ ไม่ทับการ์ด */
.expanded-card { max-height: none; padding: 28px 40px; }
.expanded-card img { width: min(300px, 34vh); height: min(300px, 34vh); margin-bottom: 8px; }
.expanded-card h2 { font-size: clamp(44px, 7vh, 64px); margin-bottom: 0; line-height: 1.3; }
.expanded-card h3 { font-size: clamp(22px, 3.6vh, 32px); }
