:root {
    --bg-color: #0f172a;
    --board-bg: #1e293b;
    --accent-color: #38bdf8;
    grid-template-columns: repeat(9, 1fr);
    --text-color: #f8fafc;
    --gem-size: 60px;
    --gap: 4px;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
}

body {
    margin: 0;
    padding: 0;
    background-color: #0f172a;
    color: white;
    font-family: 'Outfit', sans-serif;
    overflow: hidden;
    /* Prevent scrolling, we scale to fit */
    display: flex;
    justify-content: center;
    align-items: flex-start;
    /* Align top, scale down handles fit */
    height: 100vh;
    width: 100vw;
    touch-action: none;
    /* Prevent browser zooming/panning */
}

#game-container {
    width: 600px;
    /* Base Design Width */
    /* Height will be determined by content, we scale this box */
    padding: 20px 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    transform-origin: top center;
    /* Scale from top */
    flex-shrink: 0;
    /* Don't let flex shrink it, we scale it */
}

#controls {
    display: flex;
    gap: 16px;
}

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    padding: 0 20px;
}

h1 {
    font-weight: 900;
    font-size: 2.5rem;
    background: linear-gradient(45deg, #38bdf8, #818cf8);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 4px 12px rgba(56, 189, 248, 0.3);
}

.stats {
    display: flex;
}

.stat-box {
    display: flex;
    flex-direction: row;
    align-items: center;
    gap: 8px;
}

.label {
    font-size: 0.8rem;
    color: #94a3b8;
    font-weight: 700;
}

.value {
    font-size: 1.5rem;
    font-weight: 700;
}

.board {
    position: relative;
    width: calc(9 * var(--gem-size) + 10 * var(--gap));
    height: calc(9 * var(--gem-size) + 10 * var(--gap));
    background-color: var(--board-bg);
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

.gem {
    position: absolute;
    width: var(--gem-size);
    height: var(--gem-size);
    cursor: pointer;
    transition: top 0.4s cubic-bezier(0.8, 0, 0.2, 1.4), left 0.3s ease-in-out, transform 0.2s, opacity 0.2s;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 10;
    /* Initial position calculation handled in JS, but we need the offset from padding */
    margin-left: var(--gap);
    margin-top: var(--gap);
}

.gem svg {
    width: 80%;
    height: 80%;
    filter: drop-shadow(0 4px 4px rgba(0, 0, 0, 0.3));
    transition: transform 1.5s ease;
    /* Smooth internal scaling */
}

.gem:hover {
    transform: scale(1.1);
    z-index: 20;
}

.gem.selected {
    background-color: rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    transform: scale(1.1);
    box-shadow: 0 0 15px var(--accent-color);
}

.gem.match svg {
    animation: matchAnim 0.5s ease-in-out forwards;
}

@keyframes matchAnim {
    0% {
        transform: scale(1);
        filter: brightness(1);
    }

    20% {
        transform: scale(1.2);
        filter: brightness(2) drop-shadow(0 0 10px white);
    }

    80% {
        transform: scale(1.2);
        opacity: 1;
    }

    100% {
        transform: scale(0);
        opacity: 0;
    }
}

.gem.explode svg {
    animation: explodeAnim 0.4s ease-out forwards;
}

@keyframes explodeAnim {
    0% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.5);
        opacity: 0.8;
        filter: brightness(2);
    }

    100% {
        transform: scale(2);
        opacity: 0;
    }
}

.btn-primary {
    background: linear-gradient(45deg, #38bdf8, #818cf8);
    border: none;
    padding: 12px 32px;
    border-radius: 50px;
    color: white;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.2s;
    box-shadow: 0 4px 12px rgba(56, 189, 248, 0.4);
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(56, 189, 248, 0.6);
}

.btn-primary:active {
    transform: translateY(0);
}

.btn-primary.disabled {
    background: linear-gradient(45deg, #64748b, #94a3b8);
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
    backdrop-filter: blur(5px);
}

.modal.hidden {
    display: none;
}

.modal-content {
    background-color: var(--board-bg);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.modal-content h2 {
    font-size: 3rem;
    margin-bottom: 20px;
    color: var(--accent-color);
}

.modal-content p {
    font-size: 1.5rem;
    margin-bottom: 30px;
}

@keyframes soloHit {
    0% {
        transform: scale(1);
        filter: brightness(1);
    }

    50% {
        transform: scale(1.1);
        filter: brightness(1.3);
    }

    100% {
        transform: scale(1);
        filter: brightness(1);
    }
}

.solo-swap-anim {
    animation: soloHit 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

/* Level Progress Bar */
#level-progress-container {
    width: 100%;
    max-width: 600px;
    height: 12px;
    background-color: var(--board-bg);
    border-radius: 6px;
    overflow: hidden;
    margin-bottom: 10px;
    box-shadow: inset 0 2px 4px rgba(0, 0, 0, 0.5);
}

#level-progress-bar {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, #38bdf8, #818cf8, #c084fc);
    border-radius: 6px;
    transition: width 0.5s cubic-bezier(0.4, 0, 0.2, 1);
    box-shadow: 0 0 10px rgba(129, 140, 248, 0.5);
    position: relative;
    /* Added for badge positioning */
}

#progress-badge {
    position: absolute;
    right: -12px;
    top: 50%;
    transform: translateY(-50%);
    background: linear-gradient(45deg, #38bdf8, #818cf8);
    color: white;
    font-size: 0.75rem;
    font-weight: 700;
    padding: 2px 8px;
    border-radius: 12px;
    box-shadow: 0 2px 8px rgba(56, 189, 248, 0.4);
    min-width: 24px;
    text-align: center;
}

/* Level Cleared Modal */
.level-cleared-content {
    display: flex;
    justify-content: center;
    align-items: center;
    pointer-events: none;
}

.level-cleared-content h2 {
    font-size: 4rem;
    font-weight: 900;
    background: linear-gradient(45deg, #fbbf24, #f59e0b, #d97706);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 10px 30px rgba(245, 158, 11, 0.5);
    animation: levelClearPop 1.5s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    text-align: center;
    white-space: nowrap;
}

@keyframes levelClearPop {
    0% {
        transform: scale(0) rotate(-10deg);
        opacity: 0;
        filter: blur(10px);
    }

    50% {
        transform: scale(1.2) rotate(5deg);
        opacity: 1;
        filter: blur(0px);
    }

    70% {
        transform: scale(0.9) rotate(-3deg);
    }

    100% {
        transform: scale(1) rotate(0deg);
    }
}

/* Combo Overlay */
#combo-overlay {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 100%;
    height: 100%;
    pointer-events: none;
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 50;
}

.combo-text {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    z-index: 2000;
    pointer-events: none;
    font-size: 3.5rem;
    font-weight: 900;
    background: linear-gradient(45deg, #ff00cc, #333399);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    filter: drop-shadow(0 4px 8px rgba(0, 0, 0, 0.5));
    opacity: 0;
    white-space: nowrap;
    animation: comboPop 1s ease-out forwards;
}

@keyframes comboPop {
    0% {
        transform: scale(0.5) translateY(50px);
        opacity: 0;
    }

    20% {
        transform: scale(1.2) translateY(0);
        opacity: 1;
    }

    40% {
        transform: scale(1) translateY(0);
    }

    80% {
        transform: scale(1) translateY(-20px);
        opacity: 1;
    }

    100% {
        transform: scale(1.1) translateY(-50px);
        opacity: 0;
    }
}

/* Main Menu & Gem Buttons */
#main-menu {
    background: rgba(15, 23, 42, 0.95);
    /* Darker opacity for menu */
    z-index: 2000;
}

.menu-content {
    background: #1e293b;
    padding: 40px 60px;
    border-radius: 20px;
    border: 4px solid transparent;
    background-clip: padding-box;
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 30px;
    box-shadow: 0 0 50px rgba(56, 189, 248, 0.2), inset 0 0 20px rgba(0, 0, 0, 0.5);
}

/* Gem Frame Effect using pseudo-element for gradient border */
.menu-content::before {
    content: '';
    position: absolute;
    top: -4px;
    bottom: -4px;
    left: -4px;
    right: -4px;
    background: linear-gradient(45deg, #38bdf8, #818cf8, #c084fc, #f472b6);
    z-index: -1;
    border-radius: 24px;
}

.menu-icon {
    width: 100px;
    height: 100px;
    object-fit: contain;
    filter: drop-shadow(0 0 10px rgba(56, 189, 248, 0.5));
    margin-bottom: -15px;
    /* Pull title closer */
    border-radius: 20px;
}

.menu-title {
    margin-bottom: 10px;
    text-align: center;
    line-height: 1.1;
}

.brand-name {
    font-family: 'Times New Roman', serif;
    font-style: italic;
    font-weight: 400;
    font-size: 1em;
    /* Same size as parent/Game Name */
    background: linear-gradient(45deg, #38bdf8, #818cf8);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 4px 12px rgba(56, 189, 248, 0.3);
    letter-spacing: 1px;
}

.game-name {
    font-family: 'Outfit', sans-serif;
    font-weight: 900;
    background: linear-gradient(45deg, #38bdf8, #818cf8);
    background-clip: text;
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    text-shadow: 0 4px 12px rgba(56, 189, 248, 0.3);
}

/* Adjust Top Bar Title specifically */
#top-bar h1 {
    text-align: center;
    line-height: 1;
}

#top-bar .brand-name {
    font-size: 1em;
    /* Match size */
    display: inline;
    margin-right: 8px;
}

.menu-buttons {
    display: flex;
    flex-direction: column;
    gap: 20px;
    width: 100%;
}

.btn-gem {
    font-family: 'Outfit', sans-serif;
    font-size: 1.5rem;
    font-weight: 900;
    padding: 15px 40px;
    border: none;
    border-radius: 12px;
    color: white;
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 2px;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    position: relative;
    overflow: hidden;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2), inset 0 1px 0 rgba(255, 255, 255, 0.3);
}

.btn-gem:hover {
    transform: translateY(-2px) scale(1.05);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.3), inset 0 1px 0 rgba(255, 255, 255, 0.4);
}

.btn-gem:active {
    transform: translateY(1px);
}

/* New Game - Emerald Style */
.btn-gem-new {
    background: linear-gradient(135deg, #10b981, #059669);
    border-bottom: 4px solid #047857;
}

.btn-gem-new:hover {
    filter: brightness(1.1);
    box-shadow: 0 0 20px rgba(16, 185, 129, 0.4);
}

/* Continue - Amethyst Style */
.btn-gem-continue {
    background: linear-gradient(135deg, #8b5cf6, #7c3aed);
    border-bottom: 4px solid #6d28d9;
}

.btn-gem-continue:hover {
    filter: brightness(1.1);
    box-shadow: 0 0 20px rgba(139, 92, 246, 0.4);
}

.btn-gem.disabled {
    background: #475569;
    border-bottom: 4px solid #334155;
    color: #94a3b8;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
}

.menu-footer {
    margin-top: 10px;
    display: flex;
    align-items: center;
    gap: 15px;
    opacity: 0.7;
}

.version-text {
    color: #64748b;
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 1px;
}

.btn-text {
    background: none;
    border: none;
    color: #94a3b8;
    font-family: 'Outfit', sans-serif;
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    text-decoration: underline;
    padding: 0;
    transition: color 0.2s;
}

.btn-text:hover {
    color: #38bdf8;
}

/* Top Bar */
#top-bar {
    width: 100%;
    display: flex;
    justify-content: center;
    padding: 10px 20px 0;
    margin-bottom: 0;
}

.btn-secondary {
    background: rgba(30, 41, 59, 0.8);
    border: 1px solid #475569;
    color: #94a3b8;
    padding: 8px 16px;
    border-radius: 8px;
    font-family: 'Outfit', sans-serif;
    font-weight: 700;
    font-size: 0.9rem;
    cursor: pointer;
    transition: all 0.2s;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.btn-secondary:hover {
    background: #334155;
    color: #f8fafc;
    border-color: #64748b;
}