/* ===== Reset & Base ===== */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

html, body {
    width: 100%;
    height: 100%;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--tg-bg, #1a1a2e);
    color: var(--tg-text-color, #fff);
    touch-action: none;
}

/* ===== Theme Variables ===== */
:root {
    --grid-size: 8;
    --cell-gap: 4px;
    --board-padding: 8px;
    --gem-size: calc((min(100vw, 420px) - var(--board-padding) * 2 - var(--cell-gap) * (var(--grid-size) - 1)) / var(--grid-size));
}

/* ===== App Container ===== */
#app {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    max-width: 480px;
    margin: 0 auto;
    position: relative;
}

/* ===== Header / HUD ===== */
.hud {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px 8px;
    flex-shrink: 0;
}

.hud-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
}

.hud-label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 1px;
    opacity: 0.6;
    font-weight: 600;
}

.hud-value {
    font-size: 22px;
    font-weight: 800;
    text-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.hud-value.score { color: #ffd700; }
.hud-value.level { color: #4ecdc4; }
.hud-value.moves { color: #ff6b6b; }

/* ===== Game Board ===== */
.board-wrapper {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px 8px;
    overflow: hidden;
}

#board {
    position: relative;
    width: calc(var(--gem-size) * var(--grid-size) + var(--cell-gap) * (var(--grid-size) - 1) + var(--board-padding) * 2);
    height: calc(var(--gem-size) * var(--grid-size) + var(--cell-gap) * (var(--grid-size) - 1) + var(--board-padding) * 2);
    background: rgba(0, 0, 0, 0.25);
    border-radius: 16px;
    padding: var(--board-padding);
    box-shadow: inset 0 2px 8px rgba(0,0,0,0.4), 0 4px 16px rgba(0,0,0,0.3);
}

.gem {
    position: absolute;
    width: var(--gem-size);
    height: var(--gem-size);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: transform 0.12s ease, opacity 0.2s ease;
    will-change: transform;
}

.gem-inner {
    width: 86%;
    height: 86%;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--gem-size) * 0.5);
    position: relative;
    box-shadow: inset 0 -3px 6px rgba(0,0,0,0.25), inset 0 3px 6px rgba(255,255,255,0.3);
}

.gem-inner::after {
    content: '';
    position: absolute;
    top: 12%;
    left: 15%;
    width: 35%;
    height: 25%;
    border-radius: 50%;
    background: rgba(255,255,255,0.35);
    filter: blur(2px);
}

.gem.selected .gem-inner {
    transform: scale(0.85);
    box-shadow: 0 0 0 3px rgba(255,255,255,0.8), inset 0 -3px 6px rgba(0,0,0,0.25), inset 0 3px 6px rgba(255,255,255,0.3);
}

.gem.hint .gem-inner {
    animation: hint-pulse 0.6s ease-in-out infinite;
}

@keyframes hint-pulse {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(0.88); }
}

.gem.matching {
    animation: match-pop 0.3s ease-out forwards;
}

@keyframes match-pop {
    0% { transform: scale(1); opacity: 1; }
    50% { transform: scale(1.3); opacity: 0.8; }
    100% { transform: scale(0); opacity: 0; }
}

.gem.falling {
    transition: transform 0.28s cubic-bezier(0.34, 1.2, 0.64, 1);
}

/* Gem Colors */
.gem-0 .gem-inner { background: linear-gradient(135deg, #ff6b6b, #ee5a24); }
.gem-1 .gem-inner { background: linear-gradient(135deg, #4ecdc4, #0abde3); }
.gem-2 .gem-inner { background: linear-gradient(135deg, #feca57, #ff9f43); }
.gem-3 .gem-inner { background: linear-gradient(135deg, #a29bfe, #6c5ce7); }
.gem-4 .gem-inner { background: linear-gradient(135deg, #55efc4, #00b894); }
.gem-5 .gem-inner { background: linear-gradient(135deg, #fd79a8, #e84393); }

/* Gem Icons */
.gem-0 .gem-inner::before { content: '\f005'; } /* star */
.gem-1 .gem-inner::before { content: '\2B22'; } /* hexagon */
.gem-2 .gem-inner::before { content: '\25CF'; } /* circle */
.gem-3 .gem-inner::before { content: '\25B2'; } /* triangle */
.gem-4 .gem-inner::before { content: '\2660'; } /* spade */
.gem-5 .gem-inner::before { content: '\2666'; } /* diamond */

.gem-inner::before {
    color: rgba(255,255,255,0.9);
    font-weight: bold;
    text-shadow: 0 1px 2px rgba(0,0,0,0.3);
    z-index: 1;
}

/* ===== Combo Display ===== */
.combo-display {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    font-size: 36px;
    font-weight: 900;
    color: #ffd700;
    text-shadow: 0 0 20px rgba(255,215,0,0.8), 0 2px 4px rgba(0,0,0,0.5);
    pointer-events: none;
    opacity: 0;
    z-index: 100;
    white-space: nowrap;
}

.combo-display.show {
    animation: combo-show 0.8s ease-out;
}

@keyframes combo-show {
    0% { opacity: 0; transform: translate(-50%, -50%) scale(0.5); }
    30% { opacity: 1; transform: translate(-50%, -50%) scale(1.2); }
    70% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
    100% { opacity: 0; transform: translate(-50%, -50%) scale(1.5) translateY(-20px); }
}

/* ===== Score Popup ===== */
.score-popup {
    position: absolute;
    font-size: 16px;
    font-weight: 800;
    color: #ffd700;
    text-shadow: 0 1px 3px rgba(0,0,0,0.5);
    pointer-events: none;
    z-index: 99;
    animation: score-float 0.8s ease-out forwards;
}

@keyframes score-float {
    0% { opacity: 1; transform: translateY(0) scale(0.8); }
    100% { opacity: 0; transform: translateY(-40px) scale(1.2); }
}

/* ===== Screens ===== */
.screen {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 200;
    transition: opacity 0.3s;
}

.screen.hidden {
    opacity: 0;
    pointer-events: none;
}

#startScreen {
    background: linear-gradient(135deg, #1a1a2e, #16213e, #0f3460);
}

.game-title {
    font-size: 42px;
    font-weight: 900;
    background: linear-gradient(135deg, #ffd700, #ff6b6b, #4ecdc4);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 8px;
    text-align: center;
}

.game-subtitle {
    font-size: 14px;
    opacity: 0.6;
    margin-bottom: 32px;
}

.btn {
    padding: 14px 48px;
    font-size: 18px;
    font-weight: 700;
    border: none;
    border-radius: 30px;
    cursor: pointer;
    transition: transform 0.15s, box-shadow 0.15s;
    color: #fff;
    background: linear-gradient(135deg, #4ecdc4, #0abde3);
    box-shadow: 0 4px 15px rgba(78,205,196,0.4);
}

.btn:active {
    transform: scale(0.95);
    box-shadow: 0 2px 8px rgba(78,205,196,0.3);
}

.btn-secondary {
    background: linear-gradient(135deg, #ff6b6b, #ee5a24);
    box-shadow: 0 4px 15px rgba(255,107,107,0.4);
}

/* ===== Result Screen ===== */
#resultScreen {
    background: rgba(0,0,0,0.85);
    backdrop-filter: blur(8px);
}

.result-title {
    font-size: 32px;
    font-weight: 900;
    margin-bottom: 8px;
}

.result-score {
    font-size: 48px;
    font-weight: 900;
    color: #ffd700;
    margin-bottom: 24px;
}

.result-stats {
    display: flex;
    gap: 24px;
    margin-bottom: 32px;
}

.result-stat {
    text-align: center;
}

.result-stat-value {
    font-size: 24px;
    font-weight: 800;
}

.result-stat-label {
    font-size: 11px;
    opacity: 0.6;
    text-transform: uppercase;
}

/* ===== Progress Bar ===== */
.progress-bar {
    width: calc(100% - 32px);
    height: 6px;
    background: rgba(255,255,255,0.1);
    border-radius: 3px;
    margin: 0 16px 8px;
    overflow: hidden;
    flex-shrink: 0;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, #4ecdc4, #0abde3);
    border-radius: 3px;
    transition: width 0.3s ease;
    box-shadow: 0 0 8px rgba(78,205,196,0.5);
}

/* ===== Loading ===== */
.loading {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
}

.loading-spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(255,255,255,0.1);
    border-top-color: #4ecdc4;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ===== Banner Ad Space ===== */
#roiify-banner {
    position: fixed;
    bottom: 20px;
    left: 50%;
    transform: translateX(-50%);
    width: 320px;
    height: 50px;
    z-index: 99999;
    pointer-events: none;
}

/* ===== Responsive ===== */
@media (max-height: 700px) {
    .hud { padding: 8px 16px 4px; }
    .hud-value { font-size: 18px; }
    .game-title { font-size: 34px; }
}

@media (max-width: 360px) {
    :root { --cell-gap: 3px; --board-padding: 6px; }
    .hud-value { font-size: 18px; }
}
