:root {
    --bg-app: #f5f5f7;
    --bg-surface: #ffffff;
    --text-main: #1d1d1f;
    --text-secondary: #86868b;

    --marathon-green: #00d64f;
    --marathon-orange: #ff5f00;

    --header-height: 120px;
    --footer-height: 80px;
    --side-width: 160px;

    --font-ui: 'Inter', sans-serif;
    --font-heading: 'Orbitron', sans-serif;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
}

body,
html {
    height: 100%;
    width: 100%;
    overflow: hidden;
    background: var(--bg-app);
    font-family: var(--font-ui);
    color: var(--text-main);
}

.app-layout {
    display: grid;
    grid-template-areas: "header header header" "left main right" "footer footer footer";
    grid-template-rows: auto 1fr auto;
    grid-template-columns: var(--side-width) 1fr var(--side-width);
    height: 100%;
    width: 100%;
}

@media (max-width: 1000px) {
    .app-layout {
        grid-template-columns: 0 1fr 0;
    }

    .layout-left,
    .layout-right {
        display: none;
    }
}

/* Header */
.layout-header {
    grid-area: header;
    background: var(--bg-surface);
    border-bottom: 1px solid #d2d2d7;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px;
    z-index: 10;
}

.hud {
    width: 100%;
    max-width: 800px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 10px;
}

/* Controls (Left) */
.hud-controls {
    display: flex;
    gap: 10px;
    flex: 1;
}

.hud-btn {
    background: #f0f0f0;
    border: none;
    border-radius: 8px;
    width: 40px;
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    position: relative;
    font-size: 1.2rem;
    transition: all 0.2s;
}

.hud-btn:hover {
    background: #e0e0e0;
    transform: translateY(-2px);
}

#hint-count {
    position: absolute;
    bottom: -5px;
    right: -5px;
    background: var(--marathon-orange);
    color: white;
    font-size: 0.6rem;
    font-weight: bold;
    width: 16px;
    height: 16px;
    border-radius: 50%;
    display: flex;
    justify-content: center;
    align-items: center;
}

#hint-btn[disabled] {
    opacity: 0.5;
    cursor: not-allowed;
    filter: grayscale(1);
}

/* Center */
.hud-center {
    display: flex;
    flex-direction: column;
    align-items: center;
    flex: 1;
}

.game-logo {
    font-family: var(--font-heading);
    font-size: 1rem;
    color: var(--marathon-green);
    letter-spacing: 2px;
}

.hud-timer {
    font-family: var(--font-heading);
    font-size: 1.5rem;
    background: #eee;
    padding: 2px 10px;
    border-radius: 4px;
    margin-top: 4px;
}

/* Stats (Right) */
.hud-stats {
    display: flex;
    gap: 15px;
    flex: 1;
    justify-content: flex-end;
}

.stat-group {
    text-align: center;
}

.stat-group .label {
    font-size: 0.6rem;
    color: var(--text-secondary);
    font-weight: 600;
}

.stat-group .value {
    font-size: 1.2rem;
    font-family: var(--font-heading);
}

/* Main */
.layout-main {
    grid-area: main;
    background: var(--bg-app);
    display: flex;
    justify-content: center;
    align-items: center;
    position: relative;
    padding: 20px;
}

#grid-area {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.game-grid {
    display: grid;
    gap: 12px;
    width: fit-content;
    height: fit-content;
}

/* Cards & Skins */
.card {
    width: 11vmin;
    height: 11vmin;
    max-width: 90px;
    max-height: 90px;
    background: var(--bg-surface);
    border-radius: 12px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.08);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 4.5vmin;
    cursor: pointer;
    transition: transform 0.4s, box-shadow 0.3s;
    position: relative;
    transform-style: preserve-3d;
}

.card.flipped {
    transform: rotateY(180deg);
    background: #fff;
    box-shadow: 0 8px 20px rgba(0, 0, 0, 0.12);
}

.card.matched {
    transform: rotateY(180deg);
    box-shadow: 0 0 0 2px var(--marathon-green);
    background: #f0fff4;
    cursor: default;
}

.card.hint {
    animation: hintPulse 1s infinite;
    border: 2px solid orange;
    z-index: 10;
}

@keyframes hintPulse {
    0% {
        box-shadow: 0 0 0 0 rgba(255, 165, 0, 0.7);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(255, 165, 0, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(255, 165, 0, 0);
    }
}

.card-content {
    display: block;
    transform: rotateY(180deg);
    backface-visibility: hidden;
}

.card::after {
    content: '';
    position: absolute;
    width: 100%;
    height: 100%;
    background: #eef1f5;
    border-radius: 12px;
    backface-visibility: hidden;
    border: 2px solid #fff;
}

/* --- Animated Skins --- */

/* Standard */
.card.skin-default::after {
    content: '';
    background: linear-gradient(135deg, #eef1f5, #e6e9ef);
    border: 2px solid #fff;
}

/* Bronze: Brushed Metal Texture */
.card.skin-bronze::after {
    content: '';
    background:
        repeating-linear-gradient(45deg, rgba(0, 0, 0, 0.1) 0px, rgba(0, 0, 0, 0.1) 2px, transparent 2px, transparent 4px),
        linear-gradient(135deg, #cd7f32, #b08d55);
    border-color: #a0522d;
    box-shadow: inset 0 0 15px rgba(71, 35, 6, 0.6);
}

.card.skin-bronze {
    color: #5c3a21;
}

/* Silver: Brushed Chrome */
.card.skin-silver::after {
    content: '';
    background:
        repeating-linear-gradient(110deg, rgba(255, 255, 255, 0.4) 0px, rgba(255, 255, 255, 0.4) 1px, transparent 1px, transparent 3px),
        linear-gradient(135deg, #e0e0e0, #f8f8f8, #c0c0c0);
    border-color: #a9a9a9;
    box-shadow: inset 0 0 5px rgba(255, 255, 255, 0.8), 0 0 10px rgba(0, 0, 0, 0.1);
}

.card.skin-silver {
    color: #555;
}

/* Gold: Luxury Shine Pattern */
.card.skin-gold::after {
    content: '';
    background:
        radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.4) 0%, transparent 10%),
        repeating-radial-gradient(circle at 50% 50%, rgba(184, 134, 11, 0.1) 0px, rgba(184, 134, 11, 0.1) 2px, transparent 2px, transparent 10px),
        linear-gradient(135deg, #ffd700, #b8860b);
    border-color: #ffd700;
    box-shadow: inset 0 0 15px rgba(255, 215, 0, 0.6);
    animation: goldShimmer 4s infinite linear;
}

@keyframes goldShimmer {
    0% {
        filter: brightness(1);
    }

    50% {
        filter: brightness(1.2);
    }

    100% {
        filter: brightness(1);
    }
}

/* Marathon: Tech Grid */
.card.skin-marathon::after {
    content: '';
    background: #1a1a1a;
    border: 2px solid var(--marathon-green);
    background-image:
        linear-gradient(var(--marathon-green) 1px, transparent 1px),
        linear-gradient(90deg, var(--marathon-green) 1px, transparent 1px);
    background-size: 15px 15px;
    background-position: center center;
    opacity: 0.9;
}

/* Titanium: Shimmer Metal */
.card.skin-titanium::after {
    content: '';
    background: linear-gradient(to bottom, #37474f, #263238);
    border-color: #546e7a;
    box-shadow: inset 0 0 20px rgba(255, 255, 255, 0.1);
    position: relative;
    overflow: hidden;
}

.card.skin-titanium::before {
    content: '';
    position: absolute;
    top: -50%;
    left: -50%;
    width: 200%;
    height: 200%;
    background: linear-gradient(to bottom right, transparent, transparent, rgba(255, 255, 255, 0.3), transparent, transparent);
    transform: rotate(45deg);
    animation: titaniumShine 3s infinite;
    pointer-events: none;
}

@keyframes titaniumShine {
    0% {
        transform: translateX(-100%) rotate(45deg);
    }

    100% {
        transform: translateX(100%) rotate(45deg);
    }
}

.card.skin-titanium {
    color: #cfd8dc;
}

/* Prism: Holographic */
.card.skin-prism::after {
    content: '';
    background: linear-gradient(135deg, #ff0000, #ff7f00, #ffff00, #00ff00, #0000ff, #4b0082, #9400d3);
    opacity: 0.8;
    border: 2px solid #fff;
    animation: prismFlow 5s linear infinite;
}

@keyframes prismFlow {
    0% {
        filter: hue-rotate(0deg);
    }

    100% {
        filter: hue-rotate(360deg);
    }
}

/* Diamond: Faceted */
.card.skin-diamond::after {
    content: '';
    background: conic-gradient(from 45deg, #e0f7fa 0%, #b2ebf2 25%, #e0f7fa 50%, #b2ebf2 75%, #e0f7fa 100%);
    border: 2px solid #80deea;
    box-shadow: inset 0 0 10px #00bcd4;
}

/* Laser: Neon Beams */
.card.skin-laser::after {
    content: '';
    background: #000;
    border: 2px solid #f0f;
    background-image: repeating-linear-gradient(90deg, transparent 0, transparent 4px, #f0f 5px);
    box-shadow: 0 0 10px #f0f;
}

/* Cotton Candy: Soft Gradients */
.card.skin-cotton-candy::after {
    content: '';
    background: radial-gradient(circle, #ffc1e3, #c1e3ff);
    border: 2px solid #fff;
}

/* Bubble Gum: Pop Pink */
.card.skin-bubble-gum::after {
    content: '';
    background: radial-gradient(circle at 30% 30%, #ff69b4, #ff1493);
    border: 2px solid #ff69b4;
    box-shadow: inset 5px 5px 10px rgba(255, 255, 255, 0.4);
}

/* Earth: Planet Texture */
.card.skin-earth::after {
    content: '';
    background: #1a237e;
    border: 2px solid #4caf50;
    background-image: radial-gradient(circle at 50% 50%, #4caf50 0%, transparent 20%), radial-gradient(circle at 20% 80%, #4caf50 0%, transparent 15%);
    box-shadow: inset 0 0 20px #000;
}

/* Knight: Chainmail */
.card.skin-knight::after {
    content: '';
    background: #555;
    border: 2px solid #777;
    background-image: radial-gradient(#888 15%, transparent 16%), radial-gradient(#888 15%, transparent 16%);
    background-size: 8px 8px;
    background-position: 0 0, 4px 4px;
}

/* Dragon: Scales */
.card.skin-dragon::after {
    content: '';
    background: #800000;
    border: 2px solid #daa520;
    background-image: linear-gradient(45deg, #600 25%, transparent 25%, transparent 75%, #600 75%, #600), linear-gradient(45deg, #600 25%, transparent 25%, transparent 75%, #600 75%, #600);
    background-size: 10px 10px;
    background-position: 0 0, 5px 5px;
    box-shadow: inset 0 0 15px #000;
}

/* Robot: Metal Panels */
.card.skin-robot::after {
    content: '';
    background: #607d8b;
    border: 2px solid #37474f;
    box-shadow: inset 2px 2px 0 rgba(255, 255, 255, 0.2), inset -2px -2px 0 rgba(0, 0, 0, 0.2);
    background-image: linear-gradient(#37474f 1px, transparent 1px), linear-gradient(90deg, #37474f 1px, transparent 1px);
    background-size: 20px 20px;
}

/* AI Robot: Glowing Circuitry */
.card.skin-ai-robot::after {
    content: '';
    background: #000;
    border: 2px solid #00e5ff;
    background-image: radial-gradient(circle at 50% 50%, #00e5ff 2px, transparent 3px);
    background-size: 20px 20px;
    box-shadow: 0 0 15px #00e5ff;
    animation: aiPulse 2s infinite alternate;
}

@keyframes aiPulse {
    from {
        box-shadow: 0 0 10px #00e5ff;
    }

    to {
        box-shadow: 0 0 25px #00e5ff;
    }
}

.card.skin-ai-robot {
    color: #00e5ff;
}

/* Footer & Ads */
.layout-footer {
    grid-area: footer;
    background: var(--bg-surface);
    border-top: 1px solid #d2d2d7;
    display: flex;
    justify-content: center;
    align-items: center;
}

.ad-slot {
    background: #fcfcfc;
    color: #ddd;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 0.6rem;
    border: 1px dashed #eee;
    font-family: var(--font-heading);
}

.ad-horizontal.small {
    width: 320px;
    height: 50px;
}

.ad-vertical {
    width: 160px;
    height: 600px;
}

/* Overlay & Modal */
.overlay-screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(245, 245, 247, 0.95);
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.primary-btn {
    background: #111;
    color: #fff;
    border: none;
    padding: 16px 32px;
    border-radius: 99px;
    font-weight: 700;
    cursor: pointer;
    font-family: var(--font-heading);
}

.secondary-btn {
    background: transparent;
    color: #111;
    border: 2px solid #111;
    padding: 12px 24px;
    border-radius: 99px;
    font-weight: 600;
    cursor: pointer;
    margin-top: 15px;
    font-family: var(--font-heading);
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 2000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal.hidden {
    display: none;
}

.modal-glass {
    background: rgba(255, 255, 255, 0.95);
    padding: 30px;
    border-radius: 20px;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.2);
}

.modal-header {
    display: flex;
    justify-content: space-between;
    margin-bottom: 20px;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
}

/* Shop Items */
.shop-content {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 10px;
    margin: 20px 0;
    max-height: 40vh;
    overflow-y: auto;
}

.skin-item {
    padding: 10px;
    border-radius: 8px;
    background: #f9f9f9;
    text-align: center;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s;
}

.skin-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.skin-item.owned {
    background: #e0e0e0;
}

.skin-item.equipped {
    border-color: var(--marathon-green);
    background: #e0fbe0;
}

.skin-preview-container {
    height: 60px;
    display: flex;
    justify-content: center;
    align-items: center;
    margin-bottom: 5px;
}

.skin-name {
    font-weight: bold;
    font-size: 0.8rem;
    margin-bottom: 3px;
}

.skin-cost {
    font-size: 0.8rem;
    color: #111;
    font-weight: 900;
}

.hidden {
    display: none !important;
}

.toast {
    position: fixed;
    bottom: 140px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.8);
    color: white;
    padding: 15px 30px;
    border-radius: 99px;
    z-index: 5000;
    transition: opacity 0.3s;
    pointer-events: none;
}


/* Item Shop Styles */
.shop-content {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
    gap: 15px;
    padding: 20px;
    max-height: 400px;
    overflow-y: auto;
}

.shop-item {
    background: #f9f9f9;
    border: 1px solid #e5e5e5;
    border-radius: 12px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    transition: all 0.2s;
    text-align: center;
}

.shop-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.05);
}

.shop-item.unlocked {
    background: #fff;
    border-color: #007aff;
}

.shop-item.equipped {
    background: #f0f7ff;
    border: 2px solid #007aff;
    box-shadow: 0 0 0 2px rgba(0, 122, 255, 0.2);
}

.shop-icon {
    font-size: 3rem;
    margin-bottom: 5px;
    height: 60px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.shop-name {
    font-size: 0.8rem;
    font-weight: 600;
    margin-bottom: 8px;
    color: #333;
}

.shop-action-btn {
    width: 100%;
    padding: 6px;
    border: none;
    border-radius: 6px;
    font-size: 0.75rem;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
}

.buy-btn {
    background: #333;
    color: #fff;
}

.buy-btn:hover {
    background: #000;
}

.shop-item.unlocked .shop-action-btn {
    background: #e5e5e5;
    color: #333;
}

.shop-item.unlocked .shop-action-btn:hover {
    background: #d5d5d5;
}

.shop-item.equipped .shop-action-btn {
    background: #007aff;
    color: #fff;
    cursor: default;
}

/* Helper to flip icons */
.flip-icon {
    transform: scaleX(-1);
    display: inline-block;
}