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

:root {
    --primary: #4fc3f7; --primary-dark: #0288d1;
    --accent: #ff6d00; --accent-glow: #ff9100;
    --bg-dark: #0d1117; --bg-card: rgba(255,255,255,0.07);
    --text: #e0e0e0; --text-bright: #fff;
    --star-color: #ffd740; --coin-color: #7c4dff;
    --success: #00e676; --fail: #ff1744;
    --radius: 12px; --shadow: 0 4px 24px rgba(0,0,0,0.5);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body { width: 100%; height: 100%; overflow: hidden; background: var(--bg-dark); font-family: 'Segoe UI', system-ui, -apple-system, sans-serif; color: var(--text); overscroll-behavior: none; touch-action: manipulation; position: fixed; top: 0; left: 0; }
#game-container { position: relative; width: 100%; height: 100%; overflow: hidden; overscroll-behavior: none; }
#game-canvas { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: block; touch-action: none; }

/* Screens */
.screen { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; z-index: 10; }
.screen.active { display: flex; }
.screen.overlay { z-index: 30; justify-content: center; align-items: center; background: rgba(0,0,0,0.6); backdrop-filter: blur(4px); }

/* ============ ROTATE PROMPT (portrait blocker) ============ */
#rotate-screen {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: var(--bg-dark); z-index: 9999;
    display: none; justify-content: center; align-items: center; flex-direction: column; gap: 16px;
}
#rotate-screen .rotate-icon { font-size: 56px; animation: rotateHint 2s ease-in-out infinite; }
#rotate-screen .rotate-text { font-size: 16px; opacity: 0.6; text-align: center; padding: 0 32px; }
@keyframes rotateHint { 0%,100% { transform: rotate(0deg); } 50% { transform: rotate(90deg); } }
@media (orientation: portrait) and (max-width: 600px) {
    #rotate-screen { display: flex !important; }
}
@media (orientation: landscape), (min-width: 601px) {
    #rotate-screen { display: none !important; }
}

/* ============ LOADING ============ */
#loading-screen { background: var(--bg-dark); justify-content: center; align-items: center; z-index: 100; }
.loading-content { text-align: center; }
.loading-title { margin-bottom: 24px; }
.loading-bar { width: 200px; height: 4px; background: rgba(255,255,255,0.1); border-radius: 2px; margin: 0 auto 12px; overflow: hidden; }
.loading-progress { width: 0%; height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); border-radius: 2px; transition: width 0.3s; }
#loading-text { font-size: 13px; opacity: 0.5; }

/* ============ MENU ============ */
#menu-screen { justify-content: center; align-items: center; background: transparent; padding: 0 max(16px, var(--safe-left)) 0 max(16px, var(--safe-right)); }
.menu-content { text-align: center; z-index: 2; }
.game-title { font-size: 42px; font-weight: 900; letter-spacing: 3px; color: var(--text-bright); text-shadow: 0 0 40px rgba(79,195,247,0.4); line-height: 1; margin-bottom: 12px; max-width: 100%; word-break: break-word; }
.game-title span { color: var(--primary); }
.menu-stats { display: flex; justify-content: center; gap: 24px; margin-top: 16px; }
.stat { display: flex; align-items: center; gap: 6px; font-size: 16px; font-weight: 700; }
.stat-icon { font-size: 20px; }
.stat:first-child .stat-icon { color: var(--star-color); }
.stat:last-child .stat-icon { color: var(--coin-color); }

/* ============ BUTTONS ============ */
.btn { display: inline-block; padding: 12px 36px; border: none; border-radius: var(--radius); font-size: 15px; font-weight: 700; letter-spacing: 2px; cursor: pointer; transition: all 0.2s; text-transform: uppercase; }
.btn:active { transform: scale(0.95); }
.btn-primary { background: linear-gradient(135deg, var(--primary), var(--primary-dark)); color: white; box-shadow: 0 4px 20px rgba(79,195,247,0.35); }
.btn-secondary { background: var(--bg-card); color: var(--text); border: 1px solid rgba(255,255,255,0.12); margin-top: 10px; }
.btn-accent { background: linear-gradient(135deg, var(--accent), var(--accent-glow)); color: white; box-shadow: 0 4px 16px rgba(255,109,0,0.35); }
.btn-small { padding: 8px 20px; font-size: 13px; }
.btn-icon { width: 44px; height: 44px; border: none; border-radius: 50%; background: var(--bg-card); color: var(--text-bright); font-size: 20px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.2s; border: 1px solid rgba(255,255,255,0.08); }
.btn-icon:active { transform: scale(0.9); }

/* ============ MUTE ============ */
.btn-mute { font-size: 18px; flex-shrink: 0; }
.btn-mute.muted { opacity: 0.4; }
.menu-mute { position: absolute; top: max(10px, var(--safe-top)); right: max(14px, var(--safe-right)); z-index: 3; }
.hud-right { display: flex; align-items: center; gap: 6px; }

/* ============ LEVEL SELECT ============ */
#levels-screen { flex-direction: column; background: var(--bg-dark); overflow-y: auto; padding: 0 max(16px, var(--safe-left)) 16px max(16px, var(--safe-right)); overscroll-behavior: none; touch-action: pan-y; -webkit-overflow-scrolling: touch; }
.screen-header { display: flex; align-items: center; padding: max(10px, var(--safe-top)) 0 10px; gap: 12px; position: sticky; top: 0; background: var(--bg-dark); z-index: 5; }
.screen-header h2 { flex: 1; text-align: center; font-size: 16px; }
.spacer { width: 44px; }
#chapters-container { display: flex; flex-direction: column; gap: 14px; padding-bottom: max(16px, var(--safe-bottom)); }
.chapter { background: var(--bg-card); border-radius: var(--radius); padding: 10px 14px; border: 1px solid rgba(255,255,255,0.05); }
.chapter-header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.chapter-icon { width: 34px; height: 34px; border-radius: 8px; display: flex; align-items: center; justify-content: center; font-size: 16px; }
.chapter-title { font-size: 14px; font-weight: 700; }
.chapter-progress { font-size: 11px; opacity: 0.5; }
.levels-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; }
.level-btn { aspect-ratio: 1; border: none; border-radius: 8px; background: rgba(255,255,255,0.05); color: var(--text); font-size: 14px; font-weight: 700; cursor: pointer; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px; transition: all 0.2s; border: 1px solid rgba(255,255,255,0.04); }
.level-btn:active { transform: scale(0.9); }
.level-btn.completed { background: rgba(79,195,247,0.12); border-color: rgba(79,195,247,0.25); }
.level-btn.locked { opacity: 0.25; pointer-events: none; }
.level-btn.current { background: rgba(79,195,247,0.25); border-color: var(--primary); box-shadow: 0 0 10px rgba(79,195,247,0.25); }
.level-stars { font-size: 6px; color: var(--star-color); letter-spacing: 1px; }

/* ============ HUD ============ */
#game-hud { flex-direction: column; justify-content: space-between; pointer-events: none; background: transparent; }
#game-hud > * { pointer-events: auto; }
.hud-top { display: flex; align-items: center; padding: max(8px, var(--safe-top)) max(14px, var(--safe-right)) 6px max(14px, var(--safe-left)); gap: 10px; }
.level-info { flex: 1; text-align: center; min-width: 0; }
#hud-level-name { font-size: 14px; font-weight: 700; opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* Progress in HUD */
.hud-progress { display: flex; align-items: center; gap: 6px; margin-top: 3px; }
.progress-bar { flex: 1; height: 6px; background: rgba(255,255,255,0.1); border-radius: 3px; overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--primary); border-radius: 2px; transition: width 0.3s ease-out, background 0.3s; }
.progress-text { font-size: 11px; font-weight: 700; opacity: 0.6; min-width: 28px; text-align: right; }

/* Hearts / Mistakes */
.hud-mistakes { display: flex; gap: 3px; align-items: center; }
.heart { font-size: 16px; transition: all 0.3s; }
.heart.full { color: #ff1744; text-shadow: 0 0 6px rgba(255,23,68,0.4); }
.heart.empty { color: rgba(255,255,255,0.15); }

/* ============ RESULT / FAIL / COMPLETE OVERLAYS ============ */
.result-content { text-align: center; background: rgba(13,17,23,0.95); padding: 20px 24px; border-radius: 16px; border: 1px solid rgba(255,255,255,0.08); box-shadow: var(--shadow); min-width: 220px; max-width: 360px; width: 85%; }
.result-content.animate { animation: slideUp 0.35s ease-out; }
.result-content h2 { font-size: 22px; margin-bottom: 12px; }
.result-stars { display: flex; justify-content: center; gap: 8px; margin-bottom: 12px; }
.result-stars .star { font-size: 36px; color: rgba(255,255,255,0.12); transition: all 0.5s; }
.result-stars .star.earned { color: var(--star-color); text-shadow: 0 0 16px rgba(255,215,64,0.45); animation: starPop 0.5s ease-out; }
@keyframes starPop { 0% { transform: scale(0); } 60% { transform: scale(1.3); } 100% { transform: scale(1); } }
.result-info { font-size: 20px; font-weight: 700; margin-bottom: 16px; display: flex; justify-content: center; align-items: center; gap: 8px; }
.result-info .coin-icon { color: var(--coin-color); }
.result-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.btn-buy-lives { margin-top: 12px; display: none; padding: 10px 24px; font-size: 14px; }
.btn-buy-lives.visible { display: inline-flex; align-items: center; justify-content: center; gap: 4px; }
.btn-price { white-space: nowrap; }
.yan-icon { width: 18px; height: 18px; vertical-align: -3px; margin-left: 2px; }
.hud-infinite { display: flex; align-items: center; gap: 3px; color: #ff1744; font-size: 16px; font-weight: 700; text-shadow: 0 0 6px rgba(255,23,68,0.4); }
.hud-infinite .inf-symbol { font-size: 18px; }
.fail-title { color: var(--fail); }
.fail-desc { opacity: 0.5; margin-bottom: 14px; font-size: 13px; }
.complete-title { color: var(--star-color); }
.complete-desc { opacity: 0.7; margin-bottom: 16px; font-size: 14px; }

/* ============ TUTORIAL ============ */
#tutorial-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; display: none; justify-content: center; align-items: center; z-index: 50; background: rgba(0,0,0,0.5); backdrop-filter: blur(3px); padding: 0 max(16px, var(--safe-left)) 0 max(16px, var(--safe-right)); }
.tutorial-card { background: rgba(13,17,23,0.95); border: 1px solid rgba(79,195,247,0.25); border-radius: 14px; padding: 16px 20px; max-width: 340px; text-align: center; box-shadow: 0 8px 40px rgba(0,0,0,0.5); animation: slideUp 0.35s ease-out; }
.tutorial-card h3 { font-size: 17px; margin-bottom: 10px; color: var(--primary); }
.tutorial-card ul { list-style: none; text-align: left; margin-bottom: 12px; }
.tutorial-card li { padding: 4px 0; font-size: 13px; line-height: 1.35; opacity: 0.85; }
.tutorial-card li + li { border-top: 1px solid rgba(255,255,255,0.05); }

/* ============ ANIMATIONS ============ */
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }

/* ============ LANDSCAPE MOBILE — very short height ============ */
@media (max-height: 400px) {
    .game-title { font-size: 28px; margin-bottom: 8px; letter-spacing: 2px; }
    .loading-title { font-size: 28px; margin-bottom: 16px; }
    .btn { padding: 10px 28px; font-size: 14px; }
    .btn-secondary { margin-top: 8px; }
    .menu-stats { margin-top: 12px; gap: 20px; }
    .stat { font-size: 14px; }
    .stat-icon { font-size: 17px; }

    .result-content { padding: 14px 20px; }
    .result-content h2 { font-size: 18px; margin-bottom: 8px; }
    .result-stars { margin-bottom: 8px; gap: 6px; }
    .result-stars .star { font-size: 28px; }
    .result-info { font-size: 17px; margin-bottom: 10px; }
    .fail-desc { margin-bottom: 10px; font-size: 12px; }

    .tutorial-card { padding: 12px 16px; }
    .tutorial-card h3 { font-size: 15px; margin-bottom: 8px; }
    .tutorial-card li { font-size: 12px; padding: 3px 0; }

    .hud-top { padding-top: max(4px, var(--safe-top)); padding-bottom: 4px; }
    #hud-level-name { font-size: 13px; }
    .heart { font-size: 14px; }
    .btn-icon { width: 40px; height: 40px; font-size: 17px; }
    .menu-mute { top: max(6px, var(--safe-top)); right: max(10px, var(--safe-right)); }

    .screen-header { padding-top: max(6px, var(--safe-top)); padding-bottom: 6px; }
    .screen-header h2 { font-size: 14px; }
    .chapter { padding: 8px 10px; }
    .chapter-header { margin-bottom: 6px; }
    .chapter-icon { width: 28px; height: 28px; font-size: 14px; }
    .chapter-title { font-size: 13px; }
    .levels-grid { grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); gap: 5px; }
    .level-btn { font-size: 13px; border-radius: 6px; }
}

/* ============ LANDSCAPE MOBILE — extremely short (phones) ============ */
@media (max-height: 320px) {
    .game-title { font-size: 24px; margin-bottom: 6px; letter-spacing: 1px; }
    .btn { padding: 8px 24px; font-size: 13px; }
    .btn-secondary { margin-top: 6px; }
    .menu-stats { margin-top: 8px; }
    .result-content { padding: 10px 16px; border-radius: 12px; }
    .result-content h2 { font-size: 16px; margin-bottom: 6px; }
    .result-stars .star { font-size: 24px; }
    .result-info { font-size: 15px; margin-bottom: 8px; }
    .result-buttons .btn { padding: 8px 20px; font-size: 12px; }
}

/* ============ WIDE DESKTOP ============ */
@media (min-width: 900px) {
    .levels-grid { grid-template-columns: repeat(auto-fill, minmax(52px, 1fr)); }
}

/* ============ SCROLLBAR ============ */
::-webkit-scrollbar { width: 3px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(255,255,255,0.12); border-radius: 2px; }
