/* ==========================================================================
   Memory Load - Core Design System & Styles
   Science Games Lab - Fredoka Typography & Storybook Palette
   ========================================================================== */

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    box-sizing: border-box;
}

body {
    font-family: 'Fredoka', cursive, sans-serif;
    background-color: #0f0a1c;
    color: #f4f4f5;
    user-select: none;
    -webkit-user-select: none;
}

/* Page Layout */
.page-container {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: calc(var(--vh, 1vh) * 100);
    padding: 0.75rem;
    gap: 0.75rem;
    box-sizing: border-box;
}

.hud-container {
    flex-shrink: 0;
}

.game-wrapper {
    flex-grow: 1;
    min-height: 0;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}

.leaderboard-panel {
    display: none;
}

.global-footer {
    flex-shrink: 0;
}

/* HUD Styling */
.hud {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    background: rgba(24, 16, 42, 0.7);
    border: 1.5px solid rgba(167, 139, 250, 0.25);
    border-radius: 0.85rem;
    padding: 0.5rem 0.85rem;
    backdrop-filter: blur(8px);
}

.hud-element {
    background-color: rgba(255, 255, 255, 0.06);
    padding: 0.4rem 0.75rem;
    border-radius: 0.65rem;
    border: 1px solid rgba(255, 255, 255, 0.12);
    letter-spacing: 0.5px;
    white-space: nowrap;
    font-size: 0.95rem;
    font-weight: 600;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.4rem;
}

.hud-left {
    color: #facc15;
}

.hud-day {
    color: #67e8f9;
}

.hud-center {
    flex-grow: 1;
    color: #e2e8f0;
    font-size: 1.05rem;
}

.hud-right {
    color: #a78bfa;
}

.hud-shield {
    background: rgba(250, 204, 21, 0.15);
    border-color: rgba(250, 204, 21, 0.45);
    color: #fde047;
    animation: pulse-wiggle 2s infinite ease-in-out;
}

.shield-icon-box {
    width: 1.5rem !important;
    height: 1.5rem !important;
    padding: 0 !important;
    margin: 0 !important;
    background: transparent !important;
}

.hud-action-btn {
    background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
    color: white;
    cursor: pointer;
    border: 1px solid rgba(59, 130, 246, 0.6);
    box-shadow: 0 2px 8px rgba(59, 130, 246, 0.4);
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.hud-action-btn:hover {
    transform: translateY(-2px) scale(1.04);
    box-shadow: 0 4px 12px rgba(59, 130, 246, 0.6);
}

.hud-action-btn:active {
    transform: translateY(1px) scale(0.97);
}

.hud-mute-button {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.15);
    cursor: pointer;
    color: #a5f3fc;
    padding: 0.4rem 0.6rem;
    transition: transform 0.15s ease, background-color 0.2s;
}

.hud-mute-button:hover {
    background: rgba(255, 255, 255, 0.14);
    transform: scale(1.06);
}

.hud-icon {
    width: 1.25rem;
    height: 1.25rem;
}

/* Aspect-Ratio Preserving Sprite Container */
.icon-container {
    width: 3.25rem;
    height: 3.25rem;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(255, 255, 255, 0.08);
    border-radius: 0.65rem;
    padding: 0.35rem;
    box-sizing: border-box;
    transition: transform 0.2s ease, background-color 0.2s ease;
}

.icon-container img {
    max-width: 100%;
    max-height: 100%;
    width: auto;
    height: auto;
    object-fit: contain;
    display: block;
}

/* Game Area Wrapper */
.game-area-wrapper {
    position: relative;
    width: 100%;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 5 / 3;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 1rem;
    overflow: hidden;
    background: radial-gradient(circle at center, #1f1438 0%, #100a20 100%);
    border: 2px solid rgba(167, 139, 250, 0.35);
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.6), inset 0 0 40px rgba(167, 139, 250, 0.1);
}

/* Tutorial Corner Button */
.tutorial-corner-btn {
    position: absolute;
    top: 0.85rem;
    left: 0.85rem;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    background: rgba(24, 16, 42, 0.85);
    border: 1.5px solid rgba(6, 182, 212, 0.4);
    border-radius: 9999px;
    padding: 0.4rem 0.85rem;
    font-family: 'Fredoka', sans-serif;
    font-weight: 600;
    font-size: 0.82rem;
    color: #a5f3fc;
    cursor: pointer;
    backdrop-filter: blur(6px);
    transition: all 0.2s ease;
}

.tutorial-corner-btn:hover {
    background: rgba(6, 182, 212, 0.25);
    border-color: rgba(6, 182, 212, 0.8);
    transform: translateY(-2px) scale(1.03);
}

/* Memory Grid */
.memory-grid {
    position: relative;
    width: 100%;
    height: 100%;
    padding: 1rem;
    box-sizing: border-box;
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 0.75rem;
    perspective: 1000px;
}

/* Grid sizes for rounds */
.grid-4x3 {
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(3, 1fr);
}

.grid-5x3 {
    grid-template-columns: repeat(5, 1fr);
    grid-template-rows: repeat(3, 1fr);
}

.grid-6x3 {
    grid-template-columns: repeat(6, 1fr);
    grid-template-rows: repeat(3, 1fr);
}

/* Memory Cards */
.card {
    position: relative;
    width: 100%;
    height: 100%;
    cursor: pointer;
    transform-style: preserve-3d;
    transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s ease;
    user-select: none;
}

.card:hover:not(.is-matched):not(.is-flipped) {
    transform: translateY(-3px) scale(1.02);
}

.card-inner {
    position: relative;
    width: 100%;
    height: 100%;
    transform-style: preserve-3d;
    transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

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

.card.is-matched {
    opacity: 0.35;
    cursor: default;
    pointer-events: none;
    transform: scale(0.96);
    transition: opacity 0.4s ease, transform 0.4s ease;
}

.card.is-bonus .card-inner {
    box-shadow: 0 0 20px #facc15;
    border-radius: 0.75rem;
}

.card-face {
    position: absolute;
    inset: 0;
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
    border-radius: 0.75rem;
    border: 1.5px solid rgba(255, 255, 255, 0.15);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.card-front {
    background: radial-gradient(circle at center, #2e1d52 0%, #1e1338 100%);
    border-color: rgba(167, 139, 250, 0.45);
    transform: rotateY(180deg);
}

.card-front img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    padding: 0.45rem;
    box-sizing: border-box;
}

.card-back {
    background: radial-gradient(circle at center, #35215c 0%, #1e1236 100%);
    border-color: rgba(255, 255, 255, 0.2);
    font-size: 1.75rem;
    font-weight: 700;
    color: #a78bfa;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.35);
    transition: background-color 0.2s, border-color 0.2s;
}

.card-back:hover {
    background: radial-gradient(circle at center, #482c7d 0%, #291849 100%);
    border-color: rgba(167, 139, 250, 0.6);
    color: #facc15;
}

.card.day-study-highlight .card-front {
    box-shadow: 0 0 15px #67e8f9;
    border-color: #22d3ee;
}

.card.shake-card {
    animation: card-shake 0.45s cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

@keyframes card-shake {
    10%, 90% { transform: translateX(-4px) rotate(-2deg); }
    20%, 80% { transform: translateX(4px) rotate(2deg); }
    30%, 50%, 70% { transform: translateX(-4px) rotate(-1deg); }
    40%, 60% { transform: translateX(4px) rotate(1deg); }
}

/* Wiggle Keyframes */
@keyframes wiggle {
    0%, 100% { transform: rotate(0deg); }
    25% { transform: rotate(-3deg) scale(1.03); }
    75% { transform: rotate(3deg) scale(1.03); }
}

@keyframes pulse-wiggle {
    0%, 100% { transform: scale(1) rotate(0deg); }
    20% { transform: scale(1.04) rotate(-2deg); }
    40% { transform: scale(1.04) rotate(2deg); }
    60% { transform: scale(1.02) rotate(-1deg); }
    80% { transform: scale(1.02) rotate(1deg); }
}

/* Modals - In-game modals are contained inside the game area */
.modal {
    position: absolute;
    inset: 0;
    z-index: 50;
    background: rgba(10, 5, 22, 0.92);
    backdrop-filter: blur(10px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 0.75rem;
    box-sizing: border-box;
}

/* Fullscreen Dialog Modals (Briefing, Facts and Mobile Leaderboard) */
#factModal,
#howToModal,
#mainLeaderboardModal {
    position: fixed;
    inset: 0;
    z-index: 200;
    background: rgba(10, 5, 22, 0.85);
    backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    box-sizing: border-box;
}

.modal-header {
    flex-shrink: 0;
    padding-bottom: 0.75rem;
    text-align: center;
}

.modal-body {
    overflow-y: auto;
    width: 100%;
    max-height: 70vh;
    padding: 0.5rem;
    box-sizing: border-box;
}

.modal-footer {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-shrink: 0;
    padding-top: 0.75rem;
    width: 100%;
}

.modal-title {
    font-size: 2rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    color: white;
    margin: 0 0 0.35rem 0;
}

.modal-subtitle {
    font-size: 1rem;
    font-weight: 500;
    color: #facc15;
    margin: 0;
}

.game-over-title {
    font-size: 2rem;
    color: #67e8f9;
}

.failure-title {
    color: #f87171;
}

.final-score-text {
    font-size: 1.35rem;
    font-weight: 700;
    color: #f4f4f5;
    margin: 0.35rem 0;
}

.leaderboard-rank-text {
    font-size: 1.05rem;
    font-weight: 600;
    color: #facc15;
    margin-top: 0.5rem;
}

/* Low-Text Character Intro Grid & Cards */
.intro-box {
    width: 100%;
    max-width: 58rem;
    margin: 0 auto 0.5rem auto;
    padding: 0.75rem;
    background-color: rgba(24, 24, 27, 0.65);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 0.85rem;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
    box-sizing: border-box;
}

.cards-hint {
    text-align: center;
    font-family: 'Fredoka', sans-serif;
    font-size: 0.82rem;
    font-weight: 500;
    color: #94a3b8;
    letter-spacing: 0.3px;
}

.cards-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
    width: 100%;
}

@media (min-width: 540px) {
    .cards-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 720px) {
    .cards-grid { grid-template-columns: repeat(4, 1fr); }
}

@media (min-width: 860px) {
    .cards-grid { grid-template-columns: repeat(5, 1fr); }
}

@media (min-width: 1024px) {
    .cards-grid { grid-template-columns: repeat(6, 1fr); }
}

.character-card {
    position: relative;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 0.85rem;
    padding: 0.65rem 0.5rem 0.6rem 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
    cursor: pointer;
    user-select: none;
    transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s ease, border-color 0.2s ease, background 0.2s ease;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

.character-card:hover {
    animation: wiggle 0.4s ease-in-out;
    transform: translateY(-4px) scale(1.03);
    background: rgba(255, 255, 255, 0.08);
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.45);
}

.character-card:active {
    transform: scale(0.96);
}

/* Four-Sided Border Indicators */
.card-hero {
    border-color: rgba(34, 211, 238, 0.4);
}

.card-hero:hover {
    border-color: #22d3ee;
    box-shadow: 0 8px 24px rgba(34, 211, 238, 0.25);
}

.card-ally {
    border-color: rgba(56, 189, 248, 0.35);
}

.card-ally:hover {
    border-color: #38bdf8;
    box-shadow: 0 8px 24px rgba(56, 189, 248, 0.2);
}

.card-threat {
    border-color: rgba(248, 113, 113, 0.35);
}

.card-threat:hover {
    border-color: #f87171;
    box-shadow: 0 8px 24px rgba(248, 113, 113, 0.2);
}

.card-badge {
    font-family: 'Fredoka', sans-serif;
    font-size: 0.65rem;
    font-weight: 700;
    padding: 0.15rem 0.55rem;
    border-radius: 9999px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    margin-bottom: 0.35rem;
    line-height: 1.2;
}

.badge-hero {
    background: rgba(34, 211, 238, 0.2);
    color: #67e8f9;
    border: 1px solid rgba(34, 211, 238, 0.4);
}

.badge-ally {
    background: rgba(56, 189, 248, 0.2);
    color: #7dd3fc;
    border: 1px solid rgba(56, 189, 248, 0.4);
}

.badge-threat {
    background: rgba(239, 68, 68, 0.2);
    color: #fca5a5;
    border: 1px solid rgba(239, 68, 68, 0.4);
}

.card-portrait {
    width: 3.5rem;
    height: 3.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.25);
    border-radius: 0.65rem;
    padding: 0.3rem;
    box-sizing: border-box;
    margin-bottom: 0.35rem;
    transition: transform 0.2s ease;
}

.character-card:hover .card-portrait {
    transform: scale(1.08);
}

.card-name {
    font-family: 'Fredoka', sans-serif;
    font-size: 0.88rem;
    font-weight: 700;
    line-height: 1.2;
    margin-bottom: 0.2rem;
}

.card-ability {
    font-family: 'Fredoka', sans-serif;
    font-size: 0.72rem;
    color: #d4d4d8;
    line-height: 1.25;
    font-weight: 500;
}

/* Interactive Sleep Modal */
.sleep-modal-body {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
}

.sleep-animation {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 34rem;
    position: relative;
    padding: 1rem;
    box-sizing: border-box;
}

.sleep-brain-container {
    width: 6.5rem !important;
    height: 6.5rem !important;
    background: rgba(167, 139, 250, 0.15) !important;
    border-radius: 50% !important;
    box-shadow: 0 0 30px rgba(167, 139, 250, 0.3);
    margin-bottom: 0.75rem;
    animation: pulse-brain 2.5s infinite ease-in-out;
}

@keyframes pulse-brain {
    0%, 100% { transform: scale(1); box-shadow: 0 0 25px rgba(167, 139, 250, 0.25); }
    50% { transform: scale(1.08); box-shadow: 0 0 45px rgba(167, 139, 250, 0.55); }
}

.sleep-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: #facc15;
    margin: 0 0 0.25rem 0;
}

.sleep-hint {
    font-size: 0.85rem;
    color: #cbd5e1;
    margin: 0 0 0.75rem 0;
    text-align: center;
}

/* Spindles Container */
.spindles-container {
    position: relative;
    width: 100%;
    height: 10rem;
    margin-bottom: 0.75rem;
    overflow: hidden;
    border-radius: 0.85rem;
    background: rgba(0, 0, 0, 0.25);
    border: 1px dashed rgba(167, 139, 250, 0.35);
}

.spindle-target {
    position: absolute;
    width: 3.5rem;
    height: 3.5rem;
    cursor: pointer;
    transition: transform 0.15s ease;
    animation: spindle-float 3s infinite ease-in-out alternate;
}

.spindle-target:hover {
    transform: scale(1.18);
}

.spindle-target:active {
    transform: scale(0.92);
}

.spindle-target img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    filter: drop-shadow(0 0 8px rgba(250, 204, 21, 0.8));
}

@keyframes spindle-float {
    0% { transform: translateY(0) rotate(0deg); }
    50% { transform: translateY(-8px) rotate(3deg); }
    100% { transform: translateY(4px) rotate(-3deg); }
}

.sleep-bar-container {
    width: 100%;
    max-width: 22rem;
    height: 1.25rem;
    background: rgba(0, 0, 0, 0.5);
    border: 1.5px solid rgba(167, 139, 250, 0.4);
    border-radius: 9999px;
    padding: 2px;
    box-sizing: border-box;
    margin-bottom: 0.5rem;
}

.sleep-bar-fill {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, #3b82f6 0%, #a78bfa 50%, #facc15 100%);
    border-radius: 9999px;
    transition: width 0.3s ease;
}

.sleep-status-row {
    display: flex;
    justify-content: space-between;
    width: 100%;
    max-width: 22rem;
    font-size: 0.85rem;
    font-weight: 600;
    margin-bottom: 0.75rem;
}

.wake-up-btn {
    font-size: 0.95rem;
    padding: 0.5rem 1.25rem;
}

/* Buttons */
.btn-primary, .btn-secondary {
    font-family: 'Fredoka', cursive, sans-serif;
    color: white;
    font-weight: 600;
    padding: 0.65rem 1.5rem;
    border-radius: 0.65rem;
    font-size: 1.05rem;
    letter-spacing: 0.5px;
    border: none;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
    cursor: pointer;
    transition: transform 0.15s ease, box-shadow 0.15s ease, background-color 0.2s;
}

.btn-primary {
    background: linear-gradient(135deg, #22c55e 0%, #16a34a 100%);
}

.btn-primary:hover {
    background: linear-gradient(135deg, #16a34a 0%, #15803d 100%);
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 6px 18px rgba(34, 197, 94, 0.4);
}

.btn-primary:active {
    transform: translateY(1px) scale(0.98);
}

.btn-secondary {
    background: linear-gradient(135deg, #3b82f6 0%, #2563eb 100%);
}

.btn-secondary:hover {
    background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
    transform: translateY(-2px) scale(1.02);
    box-shadow: 0 6px 18px rgba(59, 130, 246, 0.4);
}

.btn-secondary:active {
    transform: translateY(1px) scale(0.98);
}

.modal-footer-buttons {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 0.75rem;
    width: 100%;
    max-width: 24rem;
    margin: 0 auto;
}

.player-name-input {
    background-color: rgba(255, 255, 255, 0.08);
    color: white;
    width: 100%;
    max-width: 24rem;
    text-align: center;
    padding: 0.6rem 1rem;
    border-radius: 0.65rem;
    border: 1.5px solid rgba(167, 139, 250, 0.4);
    margin-bottom: 0.75rem;
    font-family: 'Fredoka', sans-serif;
    font-size: 1rem;
    font-weight: 500;
    outline: none;
    transition: border-color 0.2s, background-color 0.2s;
}

.player-name-input:focus {
    border-color: #22c55e;
    background-color: rgba(255, 255, 255, 0.12);
}

.player-name-input.shake-input {
    animation: input-shake 0.4s;
}

@keyframes input-shake {
    10%, 90% { transform: translateX(-3px); }
    20%, 80% { transform: translateX(3px); }
    30%, 50%, 70% { transform: translateX(-3px); }
    40%, 60% { transform: translateX(3px); }
}

/* Science Fact Modal */
.fact-card {
    position: relative;
    width: 100%;
    max-width: 28rem;
    background: linear-gradient(135deg, rgba(30, 15, 52, 0.98) 0%, rgba(16, 8, 30, 0.98) 100%);
    border: 2px solid rgba(167, 139, 250, 0.45);
    border-radius: 1.25rem;
    padding: 1.5rem;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.7), 0 0 30px rgba(167, 139, 250, 0.25);
    text-align: center;
    box-sizing: border-box;
}

.fact-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.75rem;
}

.fact-close-btn {
    background: none;
    border: none;
    color: #94a3b8;
    font-size: 1.5rem;
    cursor: pointer;
    line-height: 1;
    padding: 0.2rem 0.5rem;
    border-radius: 0.35rem;
    transition: color 0.15s, background-color 0.15s;
}

.fact-close-btn:hover {
    color: white;
    background: rgba(255, 255, 255, 0.1);
}

.fact-portrait {
    width: 5rem !important;
    height: 5rem !important;
    margin: 0 auto 0.75rem auto !important;
    background: rgba(0, 0, 0, 0.35) !important;
    border-radius: 1rem !important;
    padding: 0.5rem !important;
}

.fact-title {
    font-size: 1.4rem;
    font-weight: 700;
    color: #facc15;
    margin: 0 0 0.5rem 0;
}

.fact-text {
    font-size: 0.95rem;
    color: #e2e8f0;
    line-height: 1.5;
    margin: 0 0 1.25rem 0;
    font-weight: 400;
}

.fact-btn {
    width: 100%;
    max-width: 14rem;
    margin: 0 auto;
}

/* How to Play Modal */
.how-to-card {
    position: relative;
    width: 100%;
    max-width: 48rem;
    max-height: 90vh;
    margin: auto;
    overflow-y: auto;
    background: linear-gradient(135deg, rgba(30, 15, 52, 0.98) 0%, rgba(16, 8, 30, 0.98) 100%);
    border: 2px solid rgba(6, 182, 212, 0.45);
    border-radius: 1.25rem;
    padding: 1.5rem 1.75rem;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.7), 0 0 30px rgba(6, 182, 212, 0.25);
    text-align: center;
    box-sizing: border-box;
}

.how-to-header-badge {
    font-family: 'Fredoka', sans-serif;
    font-size: 0.75rem;
    font-weight: 700;
    padding: 0.2rem 0.65rem;
    border-radius: 9999px;
    letter-spacing: 0.5px;
    text-transform: uppercase;
    background: rgba(6, 182, 212, 0.2);
    color: #67e8f9;
    border: 1px solid rgba(6, 182, 212, 0.4);
}

.how-to-title {
    font-size: 1.75rem;
    font-weight: 700;
    color: white;
    margin: 0.25rem 0;
}

.how-to-subtitle {
    font-size: 0.95rem;
    color: #94a3b8;
    margin: 0 0 1.25rem 0;
}

.tutorial-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.75rem;
    margin-bottom: 1.5rem;
}

@media (min-width: 640px) {
    .tutorial-grid {
        grid-template-columns: repeat(3, 1fr);
    }
}

.tutorial-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 0.85rem;
    padding: 1rem 0.75rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.tutorial-icon-box {
    width: 4rem !important;
    height: 4rem !important;
    margin-bottom: 0.65rem !important;
    background: rgba(0, 0, 0, 0.25) !important;
}

.tutorial-step-title {
    font-size: 1.05rem;
    font-weight: 700;
    color: #67e8f9;
    margin-bottom: 0.35rem;
}

.tutorial-step-desc {
    font-size: 0.82rem;
    color: #d4d4d8;
    line-height: 1.35;
}

/* Game Over Stats Grid */
.game-over-stats-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.75rem;
    width: 100%;
    max-width: 32rem;
    margin: 1rem auto;
}

.game-over-stat-card {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 0.75rem;
    padding: 0.75rem 0.5rem;
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.stat-value {
    font-size: 1.75rem;
    font-weight: 700;
    line-height: 1.1;
    margin-bottom: 0.25rem;
}

.stat-label {
    font-size: 0.75rem;
    color: #94a3b8;
    font-weight: 500;
}

/* Leaderboard Sidebar & Podium Cards */
.leaderboard-panel-title {
    font-size: 1.35rem;
    font-weight: 700;
    text-align: center;
    margin: 0 0 0.75rem 0;
    color: #facc15;
}

.leaderboard-tip {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: rgba(167, 139, 250, 0.1);
    border: 1px dashed rgba(167, 139, 250, 0.35);
    border-radius: 0.65rem;
    padding: 0.5rem 0.75rem;
    margin-bottom: 0.75rem;
    font-family: 'Fredoka', sans-serif;
    font-size: 0.78rem;
    color: #c4b5fd;
    text-align: center;
}

.leaderboard-box {
    background: rgba(15, 8, 30, 0.6);
    padding: 0.75rem;
    border-radius: 0.65rem;
    font-size: 0.85rem;
    border: 1px solid rgba(255, 255, 255, 0.08);
}

.leaderboard-podium-card {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.55rem 0.75rem;
    margin-bottom: 0.5rem;
    border-radius: 0.65rem;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    transition: transform 0.15s ease;
}

.leaderboard-podium-card:hover {
    transform: translateX(2px);
}

.podium-rank-1 {
    background: linear-gradient(90deg, rgba(250, 204, 21, 0.2) 0%, rgba(35, 12, 50, 0.7) 100%);
    border: 1.5px solid rgba(250, 204, 21, 0.6);
}

.podium-rank-2 {
    background: linear-gradient(90deg, rgba(226, 232, 240, 0.2) 0%, rgba(35, 12, 50, 0.7) 100%);
    border: 1.5px solid rgba(226, 232, 240, 0.6);
}

.podium-rank-3 {
    background: linear-gradient(90deg, rgba(245, 158, 11, 0.2) 0%, rgba(35, 12, 50, 0.7) 100%);
    border: 1.5px solid rgba(245, 158, 11, 0.6);
}

.podium-badge {
    font-size: 0.7rem;
    font-weight: 700;
    padding: 0.1rem 0.45rem;
    border-radius: 9999px;
    margin-right: 0.5rem;
}

.podium-badge.rank-1 {
    background: linear-gradient(135deg, #facc15 0%, #ca8a04 100%);
    color: #1e092b;
}

.podium-badge.rank-2 {
    background: linear-gradient(135deg, #e2e8f0 0%, #94a3b8 100%);
    color: #0f172a;
}

.podium-badge.rank-3 {
    background: linear-gradient(135deg, #fbbf24 0%, #b45309 100%);
    color: #1e092b;
}

/* Floating Score Pops */
#score-pop-container {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 101;
}

.score-pop {
    position: absolute;
    font-size: 1.35rem;
    font-weight: 700;
    pointer-events: none;
    z-index: 101;
    animation: score-pop-animation 1s ease-out forwards;
    white-space: nowrap;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
}

.score-positive {
    color: #fde047;
}

.score-negative {
    color: #f87171;
}

.score-bonus {
    color: #67e8f9;
}

.combo-text {
    font-size: 0.95rem;
    color: #a78bfa;
    margin-left: 0.35rem;
}

@keyframes score-pop-animation {
    0% { transform: translate(-50%, 0) scale(0.8); opacity: 1; }
    50% { transform: translate(-50%, -25px) scale(1.1); opacity: 1; }
    100% { transform: translate(-50%, -50px) scale(1); opacity: 0; }
}

/* Modal Specificity and Visibility Rule */
.hidden,
.modal.hidden,
#factModal.hidden,
#howToModal.hidden,
#mainLeaderboardModal.hidden,
#soundOffIcon.hidden {
    display: none !important;
}

/* Scrollbar */
.modal-body::-webkit-scrollbar {
    width: 8px;
}

.modal-body::-webkit-scrollbar-track {
    background: #1e1338;
    border-radius: 4px;
}

.modal-body::-webkit-scrollbar-thumb {
    background-color: #facc15;
    border-radius: 6px;
    border: 2px solid #1e1338;
}

/* Footer */
.footer-content {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 1rem;
    margin-bottom: 0.25rem;
}

.footer-logo {
    height: 1.85rem;
    transition: transform 0.2s ease-in-out;
}

.footer-logo.large {
    height: 2.25rem;
}

.footer-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    text-decoration: none;
    color: #a1a1aa;
    transition: color 0.2s;
}

.footer-link:hover {
    color: #f4f4f5;
}

.footer-link:hover .footer-logo {
    transform: scale(1.08);
}

/* Rotate Warning */
@media (min-width: 768px) {
    .modal-footer-buttons {
        flex-direction: row;
        justify-content: center;
    }
}

@media (min-width: 1024px) {
    body {
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .page-container {
        display: grid;
        grid-template-columns: minmax(0, 3.2fr) minmax(0, 1fr);
        grid-template-rows: auto 1fr auto;
        grid-template-areas:
            "header header"
            "main   leaderboard"
            "footer footer";
        height: 96vh;
        max-height: 920px;
        max-width: 82rem;
        gap: 1rem;
        align-content: center;
    }
    .hud-container { grid-area: header; }
    .game-wrapper {
        grid-area: main;
        min-height: 0;
        position: relative;
        display: flex;
        justify-content: center;
        align-items: center;
    }
    .leaderboard-panel {
        grid-area: leaderboard;
        display: flex !important;
        flex-direction: column;
        background: rgba(24, 16, 42, 0.75);
        border-radius: 1rem;
        padding: 1rem;
        border: 1.5px solid rgba(167, 139, 250, 0.25);
        backdrop-filter: blur(8px);
        min-height: 0;
        box-sizing: border-box;
    }
    .leaderboard-box {
        overflow-y: auto;
        flex-grow: 1;
        min-height: 0;
    }
    .global-footer { grid-area: footer; }
    #leaderboardBtn,
    #leaderboardGameOverBtn {
        display: none !important;
    }

    .modal-footer-buttons {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
    }

    #start-game-btn {
        margin: 0 auto;
        display: inline-block;
    }
}

.hud-mute-button {
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    transition: transform 0.15s ease, background-color 0.2s, border-color 0.2s;
}

.hud-mute-button:hover {
    transform: scale(1.06);
    background: rgba(39, 39, 42, 0.95);
    border-color: #a1a1aa;
}

.hud-mute-button:active {
    transform: scale(0.95);
}

.hud-mute-button svg {
    width: 1.25rem;
    height: 1.25rem;
    display: block;
}

#rotate-warning {
    display: none;
    position: fixed;
    inset: 0;
    background-color: #18181b;
    color: #f4f4f5;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    z-index: 200;
    padding: 1.5rem;
    font-family: 'Fredoka', sans-serif;
}

.rotate-icon {
    width: 4.5rem;
    height: 4.5rem;
    margin-bottom: 1.25rem;
    color: #38bdf8;
    animation: rotate-pulse 2s infinite ease-in-out;
}

@keyframes rotate-pulse {
    0%, 100% { transform: rotate(0deg) scale(1); }
    50% { transform: rotate(90deg) scale(1.08); }
}

@media screen and (max-width: 1023px) and (orientation: portrait) {
    .page-container, footer {
        display: none !important;
    }
    #rotate-warning {
        display: flex !important;
    }
}

@media screen and (max-height: 540px) and (orientation: landscape) {
    footer {
        display: none !important;
    }
    .page-container {
        padding: 0.25rem 0.5rem;
        height: 100%;
    }
    .game-wrapper {
        max-width: 100%;
        height: 100%;
    }
    .leaderboard-panel {
        display: none !important;
    }
    .hud {
        display: flex !important;
        flex-direction: row !important;
        justify-content: space-between !important;
        align-items: center !important;
        margin-bottom: 0.25rem !important;
        gap: 0.35rem !important;
    }
    .hud-element {
        font-size: 0.78rem;
        padding: 0.2rem 0.5rem;
        border-radius: 0.5rem;
    }
    .hud-label {
        display: none !important;
    }
    .integrity-container {
        min-width: 70px;
        max-width: 180px;
        flex-grow: 1;
    }
    .integrity-bar {
        height: 0.95rem;
        border-radius: 0.35rem;
    }
    .hud-mute-button {
        display: flex !important;
        padding: 0.2rem 0.4rem;
    }
    .canvas-container,
    #game-container {
        min-height: 0;
        flex: 1 1 auto;
    }
    /* Stretch the board to the wrapper's height instead of relying on
       max-height: 100%. WebKit/WKWebView treats a flexed parent's height as
       indefinite for percentage resolution, which lets aspect-ratio push the
       board past the visible area and crops the bottom row. */
    .game-area-wrapper {
        min-height: 0;
        flex: 0 0 auto;
        align-self: stretch;
        width: auto;
        height: auto;
    }
    canvas {
        max-height: 100%;
        max-width: 100%;
    }
    .modal {
        padding: 0.35rem 0.6rem;
    }
    .modal-header {
        padding-bottom: 0.15rem;
    }
    .modal-title {
        font-size: 1.3rem;
        line-height: 1.1;
    }
    .modal-subtitle {
        font-size: 0.7rem;
        margin: 0;
    }
    .tutorial-corner-btn {
        top: 0.35rem;
        left: 0.35rem;
        right: auto !important;
        padding: 0.2rem 0.5rem;
        font-size: 0.7rem;
    }
    .intro-box {
        padding: 0.3rem 0.4rem;
        margin-bottom: 0.25rem;
    }
    .cards-hint {
        font-size: 0.65rem;
        margin-bottom: 0.15rem;
    }
    .cards-grid {
        grid-template-columns: repeat(5, 1fr) !important;
        gap: 0.25rem;
    }
    .character-card {
        padding: 0.25rem 0.2rem;
        border-radius: 0.5rem;
    }
    .card-badge {
        font-size: 0.5rem;
        padding: 0.05rem 0.25rem;
        margin-bottom: 0.15rem;
    }
    .card-portrait, .icon-container {
        width: 2rem;
        height: 2rem;
        padding: 0.15rem;
        margin-bottom: 0.15rem;
    }
    .card-name {
        font-size: 0.65rem;
        margin-bottom: 0.05rem;
    }
    .card-ability {
        font-size: 0.56rem;
        line-height: 1.1;
    }
    .modal-footer {
        padding-top: 0.25rem;
    }
    .player-name-input {
        padding: 0.3rem 0.6rem;
        font-size: 0.85rem;
        max-width: 16rem;
        border-radius: 0.5rem;
    }
    .modal-footer-buttons {
        margin-top: 0.25rem;
        gap: 0.5rem;
    }
    .btn-primary, .btn-secondary {
        padding: 0.35rem 1rem;
        font-size: 0.88rem;
        border-radius: 0.5rem;
    }
    .game-over-title {
        font-size: 1.3rem;
    }
    .game-over-fact-card {
        padding: 0.35rem 0.6rem;
        max-width: 22rem;
        margin: 0.2rem auto;
    }
    .game-over-fact-portrait {
        width: 2.75rem;
        height: 2.75rem;
        margin-bottom: 0.2rem;
    }
    .game-over-fact-title {
        font-size: 0.95rem;
        margin-bottom: 0.15rem;
    }
    .game-over-fact-text {
        font-size: 0.72rem;
        line-height: 1.25;
    }
    #mainLeaderboardModal .modal-body,
    .how-to-card,
    .fact-card {
        max-height: 85vh;
        overflow-y: auto;
    }
    .tutorial-cards {
        grid-template-columns: repeat(3, 1fr) !important;
        gap: 0.35rem;
    }
    .tutorial-card {
        padding: 0.4rem;
    }
    .tutorial-step-img {
        width: 2rem;
        height: 2rem;
    }
}

.science-corner-btn {
    position: absolute;
    top: 1rem;
    left: auto;
    right: 1rem;
    z-index: 20;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-family: 'Fredoka', sans-serif;
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.5px;
    color: #f4f4f5;
    background: rgba(5, 150, 105, 0.35);
    border: 1.5px solid #34d399;
    border-radius: 9999px;
    padding: 0.35rem 1rem;
    cursor: pointer;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
    transition: all 0.2s;
}
.science-corner-btn:hover {
    background: #059669;
    color: #ffffff;
    box-shadow: 0 0 15px rgba(52, 211, 153, 0.6);
    transform: translateY(-1px);
}
#scienceInfoModal {
    position: fixed;
    inset: 0;
    z-index: 110;
    background: rgba(10, 10, 15, 0.85);
    backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1.5rem 1rem;
    box-sizing: border-box;
}
#scienceInfoModal.hidden {
    display: none !important;
}
.science-info-card {
    position: relative;
    background: #18181b;
    border: 2px solid #34d399;
    border-radius: 1.25rem;
    padding: 1.5rem;
    max-width: 48rem;
    width: 100%;
    max-height: 90vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    box-shadow: 0 20px 45px rgba(0, 0, 0, 0.65);
    margin: auto;
}
.science-info-body {
    text-align: left;
    color: #d4d4d8;
    font-family: 'Fredoka', sans-serif;
    font-size: 0.85rem;
    line-height: 1.55;
    width: 100%;
}
.science-info-body h3 {
    color: #6ee7b7;
    font-size: 0.95rem;
    font-weight: 700;
    margin: 1rem 0 0.4rem 0;
}
.science-info-body p { margin: 0.5rem 0; }
.science-info-body ul {
    margin: 0.3rem 0 0.6rem 1.1rem;
    list-style: disc;
}
.science-info-body li { margin: 0.2rem 0; }
.science-info-body a {
    color: #38bdf8;
    text-decoration: underline;
}
.science-info-body a:hover { color: #7dd3fc; }
.science-grade { font-size: 0.8rem; color: #a1a1aa; }
@media (max-width: 1024px) {
    .science-corner-btn {
        top: 0.35rem;
        right: 0.35rem;
        left: auto !important;
        padding: 0.2rem 0.5rem;
        font-size: 0.7rem;
    }
}

.footer-legal {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.9rem;
    margin-top: 0.4rem;
    font-family: 'Fredoka', sans-serif;
    font-size: 0.7rem;
}
.footer-legal a, .footer-legal-btn {
    color: #71717a;
    text-decoration: none;
    background: none;
    border: none;
    padding: 0;
    font: inherit;
    cursor: pointer;
}
.footer-legal a:hover, .footer-legal-btn:hover { color: #34d399; }
