/*
 * Rotrix HTML5 demo styles.
 *
 * The reset, theme variables, game screen and overlays come from the app build so the
 * board looks identical; the start and controls screens are demo-only.
 */

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

html {
    height: 100%;
    width: 100%;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

/* CSS Custom Properties for theming */
:root {
    --theme-ui-bg: #1a1a1a;
    --theme-ui-bg-secondary: #000000;
    --theme-ui-text: #ffffff;
    --theme-ui-text-secondary: rgba(255, 255, 255, 0.7);
    --theme-overlay-bg: rgba(0, 0, 0, 0.9);
    --theme-overlay-light: rgba(255, 255, 255, 0.1);
    --theme-overlay-medium: rgba(255, 255, 255, 0.15);
    --theme-overlay-heavy: rgba(255, 255, 255, 0.2);
    --theme-accent-gold: rgba(255, 215, 0, 0.2);
    --theme-accent-gold-medium: rgba(255, 215, 0, 0.3);
    --theme-accent-gold-light: rgba(255, 215, 0, 0.1);

    /* Height of the strip under the score row that holds the level-up and
       gravity-changing signs. Reserved by the header and subtracted from the
       canvas height so the board still fits. */
    --notice-height: 20px;
}

body {
    margin: 0;
    padding: 0;
    background: var(--theme-ui-bg);
    color: var(--theme-ui-text);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    width: 100vw;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile */
    overflow: hidden;
    /* Prevent layout shifts during animations */
    contain: layout;
    /* Fullscreen - no padding, content fills entire screen */
    padding: 0;
    background-color: var(--theme-ui-bg) !important;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    /* Remove flex centering - content should fill screen */
    display: block;
    /* Smooth theme transitions */
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Decorative background (game/BackgroundPieces.js). The body is a stacking
   context (position: fixed), so z-index -1 sits above its background but
   below every screen. Opacity is set from BACKGROUND_PIECES.OPACITY. */
.background-pieces {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
    pointer-events: none;
}

.game-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    max-width: 100%;
    height: 100vh;
    height: 100dvh; /* Dynamic viewport height for mobile */
    overflow: hidden;
    padding: 0;
    padding-top: max(-30px, env(safe-area-inset-top) - 30px); /* Move up by 30px (half button height) */
    padding-bottom: max(30px, env(safe-area-inset-bottom) + 30px); /* Reduced bottom padding, moved up */
    box-sizing: border-box;
    margin: 0;
    /* Ensure game container is completely hidden during splash screen */
    visibility: hidden;
    opacity: 0;
    /* Hardware acceleration for smoother animations */
    transform: translateZ(0);
}

.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    /* Set from JS to the board's rendered width, so the header and the next-piece
       preview line up with the canvas instead of spanning the whole viewport */
    max-width: var(--board-width, 100%);
    margin-bottom: 2px;
    gap: 8px;
    padding: 2px 8px calc(2px + var(--notice-height));
    flex-shrink: 0;
    position: relative;
    /* Prevent repaints during score updates */
    will-change: contents;
}

.game-info {
    display: flex;
    flex-direction: row;
    gap: 12px;
    font-size: 12px;
    font-weight: 600;
    flex: 1;
    align-items: center;
    /* Optimize text rendering */
    text-rendering: optimizeSpeed;
}

.score, .level, .lines {
    color: #FFD700;
    /* Reduce repaints for frequently changing text */
    contain: layout style;
    white-space: nowrap;
    font-size: 12px;
    line-height: 1.2;
}

.score-label, .level-label, .lines-label {
    color: var(--theme-ui-text-secondary);
    font-size: 9px;
    font-weight: 400;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-bottom: 1px;
}

/* Sits below the score row and above the board's top edge. Both signs are absolute
   inside it so they share one slot instead of stacking. */
.game-notice {
    position: absolute;
    left: 8px;
    right: 8px;
    bottom: 2px;
    height: var(--notice-height);
    pointer-events: none;
}

.level-up-message {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    height: 100%;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease-in-out;
    z-index: 10;
}

.level-up-message.show {
    opacity: 1;
}

.level-up-label {
    color: #FFD700;
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

/* Blinking sign shown in the header while the board flips.
   Shares the level-up message position so both read as the same slot. */
.gravity-change-message {
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    align-items: center;
    height: 100%;
    opacity: 0;
    pointer-events: none;
    z-index: 11;
}

.gravity-change-message.show {
    opacity: 1;
    animation: gravity-change-blink 600ms ease-in-out infinite;
}

/* Both signs share the one slot, so the flip sign wins while it is up */
.game-notice:has(.gravity-change-message.show) .level-up-message {
    opacity: 0;
}

.gravity-change-label {
    color: #FFD700;
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    white-space: nowrap;
}

@keyframes gravity-change-blink {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.2; }
}

/* The gravity sign is a long string: shrink it on narrow phones so it never
   collides with the score/lines/level block on its left. The extra .game-info
   specificity keeps these from being overridden by the header breakpoints below. */
@media (max-width: 400px) {
    .game-info .gravity-change-label {
        font-size: 11px;
        letter-spacing: 0;
    }
}

@media (max-width: 360px) {
    .game-info .gravity-change-label {
        font-size: 10px;
    }
}

.score-value, .level-value, .lines-value {
    color: #FFD700;
    font-size: 14px;
    font-weight: 700;
}

.next-piece {
    width: 50px;
    height: 50px;
    border: 2px solid #444;
    background: var(--theme-ui-bg-secondary);
    flex-shrink: 0;
    /* GPU acceleration */
    transform: translateZ(0);
}

canvas {
    border: 2px solid #444;
    background: var(--theme-ui-bg-secondary);
    display: block;
    width: 100%;
    max-width: 100%;
    height: auto;
    /* Ensure canvas fits within viewport - account for header only */
    max-height: calc(100vh - 80px - var(--notice-height));
    max-height: calc(100dvh - 80px - var(--notice-height)); /* Dynamic viewport height */
    object-fit: contain;
    /* Optimize canvas rendering */
    image-rendering: pixelated;
    /* Prevent canvas clearing flicker */
    will-change: transform;
}

/* Game canvas wrapper for overlay positioning */
.game-canvas-wrapper {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    max-width: var(--board-width, 100%);
    flex-shrink: 1;
    flex-grow: 0;
}

/* Blur canvas when paused */
#gameCanvas.paused-blur {
    filter: blur(16px) brightness(0.3);
    -webkit-filter: blur(16px) brightness(0.3);
}

/* First gravity flip of a game: the board is shaken and lit up like a storm. Both
   effects are decorative, sit under the pause/game over overlays and never take
   pointer events, so they can't interfere with play. Every number here can be tuned
   from ANIMATIONS.GRAVITY_FLIP.FIRST_FLIP_STORM in config/gameConfig.js, which
   playGravityFlipStorm() feeds in as the custom properties below. */
#gameCanvas.gravity-flip-shake {
    animation: gravity-flip-shake var(--shake-duration, 1100ms) cubic-bezier(0.36, 0.07, 0.19, 0.97) both;
}

/* The board is thrown around and settles. Every swing is a fraction of
   --shake-amplitude, so that single value scales the whole shake. */
@keyframes gravity-flip-shake {
    0%, 100% { transform: translate3d(0, 0, 0) rotate(0deg); }
    5%   { transform: translate3d(calc(var(--shake-amplitude, 16px) * -1.0), calc(var(--shake-amplitude, 16px) * 0.55), 0) rotate(calc(var(--shake-rotation, 0.9deg) * -1.0)); }
    12%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * 0.95), calc(var(--shake-amplitude, 16px) * -0.7), 0) rotate(calc(var(--shake-rotation, 0.9deg) * 0.85)); }
    20%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * -0.85), calc(var(--shake-amplitude, 16px) * 0.75), 0) rotate(calc(var(--shake-rotation, 0.9deg) * -0.7)); }
    28%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * 0.75), calc(var(--shake-amplitude, 16px) * -0.6), 0) rotate(calc(var(--shake-rotation, 0.9deg) * 0.6)); }
    37%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * -0.62), calc(var(--shake-amplitude, 16px) * 0.5), 0) rotate(calc(var(--shake-rotation, 0.9deg) * -0.5)); }
    46%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * 0.5), calc(var(--shake-amplitude, 16px) * -0.42), 0) rotate(calc(var(--shake-rotation, 0.9deg) * 0.4)); }
    56%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * -0.38), calc(var(--shake-amplitude, 16px) * 0.32), 0) rotate(calc(var(--shake-rotation, 0.9deg) * -0.3)); }
    66%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * 0.28), calc(var(--shake-amplitude, 16px) * -0.22), 0) rotate(calc(var(--shake-rotation, 0.9deg) * 0.22)); }
    76%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * -0.18), calc(var(--shake-amplitude, 16px) * 0.14), 0) rotate(calc(var(--shake-rotation, 0.9deg) * -0.14)); }
    86%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * 0.1), calc(var(--shake-amplitude, 16px) * -0.08), 0) rotate(calc(var(--shake-rotation, 0.9deg) * 0.07)); }
    93%  { transform: translate3d(calc(var(--shake-amplitude, 16px) * -0.04), calc(var(--shake-amplitude, 16px) * 0.03), 0) rotate(calc(var(--shake-rotation, 0.9deg) * 0.0)); }
}

.gravity-flash {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--flash-color, #EAF2FF);
    opacity: 0;
    pointer-events: none;
    z-index: 90;
}

.gravity-flash.strike {
    animation: gravity-flip-lightning var(--flash-duration, 1500ms) linear both;
}

/* Three bursts of lightning: a double crack, a longer roll, and a last flicker
   as the pieces land. Peak brightness comes from --flash-peak. */
@keyframes gravity-flip-lightning {
    0%        { opacity: 0; }
    2%        { opacity: var(--flash-peak, 0.95); }
    6%        { opacity: 0.05; }
    9%        { opacity: calc(var(--flash-peak, 0.95) * 0.7); }
    14%       { opacity: 0.02; }
    18%       { opacity: calc(var(--flash-peak, 0.95) * 0.45); }
    27%, 45%  { opacity: 0; }
    48%       { opacity: calc(var(--flash-peak, 0.95) * 0.9); }
    53%       { opacity: 0.04; }
    57%       { opacity: calc(var(--flash-peak, 0.95) * 0.6); }
    62%       { opacity: 0.03; }
    65%       { opacity: calc(var(--flash-peak, 0.95) * 0.35); }
    72%, 80%  { opacity: 0; }
    83%       { opacity: calc(var(--flash-peak, 0.95) * 0.5); }
    88%       { opacity: 0.02; }
    91%       { opacity: calc(var(--flash-peak, 0.95) * 0.25); }
    97%, 100% { opacity: 0; }
}

/* Players who asked for less motion still get the lightning, but no shaking */
@media (prefers-reduced-motion: reduce) {
    #gameCanvas.gravity-flip-shake {
        animation: none;
    }
}

/* Game overlay (pause / game over buttons) */
.game-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 100;
    pointer-events: none;
}

.game-overlay.hidden {
    display: none;
}

.game-overlay-title {
    color: white;
    font-size: 36px;
    font-weight: bold;
    font-family: Arial, sans-serif;
    text-align: center;
    pointer-events: none;
}

.game-overlay-buttons {
    display: flex;
    flex-direction: column;
    /* Stretch keeps both buttons the same width while the column itself
       grows to fit the longest label in the current language */
    align-items: stretch;
    gap: 14px;
    margin-top: 20px;
    pointer-events: auto;
}

/* Game over overlay needs more top margin to clear canvas-drawn score text */
#gameOverOverlay .game-overlay-buttons {
    margin-top: 95px;
}

.game-overlay-button {
    min-width: 170px;
    white-space: nowrap;
    padding: 14px 24px;
    background: rgba(40, 40, 40, 0.85);
    border: 2px solid rgba(255, 255, 255, 0.3);
    border-radius: 10px;
    color: rgba(255, 255, 255, 0.9);
    font-size: 18px;
    font-weight: 600;
    transition: all 0.15s;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.game-overlay-button:active {
    background: rgba(80, 80, 80, 0.9);
    transform: scale(0.96);
}

.game-overlay-button.primary {
    background: rgba(255, 215, 0, 0.2);
    border-color: #FFD700;
    color: #FFD700;
}

.game-overlay-button.primary:active {
    background: rgba(255, 215, 0, 0.35);
    transform: scale(0.96);
}

/* Main Menu Styles */

.main-menu {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 500px;
    height: 100vh;
    height: 100dvh;
    padding: 20px;
    box-sizing: border-box;
}

.main-menu.hidden {
    display: none;
}

.menu-title {
    font-size: 48px;
    font-weight: bold;
    color: #FFD700;
    margin-bottom: 60px;
    text-align: center;
}

.menu-button {
    width: 200px;
    padding: 16px 24px;
    margin: 12px 0;
    background: var(--theme-overlay-light);
    border: 2px solid #444;
    border-radius: 8px;
    color: var(--theme-ui-text);
    font-size: 18px;
    font-weight: 600;
    transition: all 0.2s;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
}

.menu-button:active {
    background: var(--theme-overlay-medium);
    transform: scale(0.98);
}

.menu-button.primary {
    background: var(--theme-accent-gold);
    border-color: #FFD700;
    color: #FFD700;
}

.game-container.hidden {
    display: none !important;
    visibility: hidden !important;
    opacity: 0 !important;
    pointer-events: none !important;
}

.game-container:not(.hidden) {
    visibility: visible;
    opacity: 1;
    transition: opacity 0.3s ease-in;
}

@media (max-width: 768px) {
    body {
        padding: 0;
        padding-top: env(safe-area-inset-top);
    }

    .game-container {
        max-width: 100%;
        padding: 0;
        padding-top: max(-30px, env(safe-area-inset-top) - 30px); /* Move up by 30px */
        padding-bottom: max(30px, env(safe-area-inset-bottom) + 30px); /* Reduced bottom padding */
    }
    
    .game-header {
        flex-direction: row;
        margin-bottom: 2px;
        gap: 8px;
        padding: 2px 6px calc(2px + var(--notice-height));
        align-items: center;
    }

    .game-notice {
        left: 6px;
        right: 6px;
    }

    .game-info {
        font-size: 11px;
        gap: 10px;
    }

    .score-label, .level-label, .lines-label {
        font-size: 9px;
    }

    .score-value, .level-value, .lines-value, .level-up-label, .gravity-change-label {
        font-size: 13px;
    }

    .next-piece {
        width: 45px;
        height: 45px;
    }
    
    canvas {
        max-height: calc(100dvh - 60px - var(--notice-height));
    }
    
    .game-container {
        padding-top: max(-30px, env(safe-area-inset-top) - 30px); /* Move up by 30px */
        padding-bottom: max(30px, env(safe-area-inset-bottom) + 30px); /* Reduced bottom padding */
    }
    
    canvas {
        max-height: calc(100vh - 60px - var(--notice-height));
    }
}

/* Small mobile devices */
@media (max-width: 480px) {
    .game-container {
        padding: 5px;
        padding-top: max(5px, env(safe-area-inset-top));
        padding-bottom: max(5px, env(safe-area-inset-bottom));
    }
    
    .game-info {
        font-size: 12px;
    }

    .score, .level {
        font-size: 12px;
    }

    .next-piece {
        width: 60px;
        height: 60px;
    }
    
    canvas {
        max-height: calc(100dvh - 60px - var(--notice-height));
    }
    
    .game-header {
        padding: 2px 4px calc(2px + var(--notice-height));
        margin-bottom: 2px;
    }
    
    .game-notice {
        left: 4px;
        right: 4px;
    }

    .game-info {
        gap: 8px;
        font-size: 10px;
    }
    
    .score-value, .level-value, .lines-value {
        font-size: 12px;
    }
    
    .score-label, .level-label, .lines-label {
        font-size: 8px;
    }
    
    .score-value, .level-value, .lines-value, .level-up-label, .gravity-change-label {
        font-size: 12px;
    }
    
    .next-piece {
        width: 40px;
        height: 40px;
    }
    
    .game-container {
        padding-top: max(-30px, env(safe-area-inset-top) - 30px); /* Move up by 30px */
        padding-bottom: max(30px, env(safe-area-inset-bottom) + 30px); /* Reduced bottom padding */
    }
}

/* Landscape mobile */
@media (max-width: 768px) and (orientation: landscape) {
    body {
        padding: 0;
        padding-top: env(safe-area-inset-top);
    }
    
    .game-container {
        padding: 5px;
        padding-top: max(5px, env(safe-area-inset-top));
        padding-bottom: max(5px, env(safe-area-inset-bottom));
    }

    .game-container {
        flex-direction: row;
        align-items: flex-start;
        gap: 10px;
    }

    .game-header {
        flex-direction: column;
        width: auto;
        margin-bottom: 0;
    }

    .next-piece {
        width: 60px;
        height: 60px;
    }

    canvas {
        max-width: calc(100vh - 100px);
        max-height: calc(100vh - 60px - var(--notice-height));
    }

}

/* Reduce repaints for frequently changing elements */
#scoreDisplay, #levelDisplay {
    will-change: contents;
}

/* Reduce repaints for frequently changing elements */
#scoreDisplay, #levelDisplay {
    will-change: contents;
}


/* ---------------------------------------------------------------------------
   Demo-only start screen: title on top, play button in the middle, controls below
   --------------------------------------------------------------------------- */

.main-menu {
    margin: 0 auto;
    justify-content: space-between;
    gap: 24px;
    padding-top: max(32px, env(safe-area-inset-top));
    padding-bottom: max(20px, env(safe-area-inset-bottom));
    overflow-y: auto;
}

.menu-header {
    display: flex;
    flex-direction: column;
    align-items: center;
}

.menu-title {
    margin-bottom: 12px;
}

.menu-tagline {
    color: var(--theme-ui-text-secondary);
    font-size: 15px;
    text-align: center;
    max-width: 320px;
}

.menu-center {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
}

.menu-controls {
    width: 100%;
    max-width: 460px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.controls-list {
    list-style: none;
    margin: 0;
    padding: 0;
    width: 100%;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
}

.control-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px;
    background: var(--theme-overlay-light);
    border: 1px solid #333;
    border-radius: 8px;
}

.control-keys {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

kbd {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 28px;
    height: 28px;
    padding: 0 6px;
    background: var(--theme-overlay-medium);
    border: 2px solid #555;
    border-bottom-width: 3px;
    border-radius: 6px;
    color: var(--theme-ui-text);
    font-family: inherit;
    font-size: 14px;
    font-weight: 700;
    line-height: 1;
}

kbd.wide {
    min-width: 64px;
    font-size: 12px;
}

.control-keys .separator {
    color: var(--theme-ui-text-secondary);
    font-size: 13px;
}

.control-name {
    font-size: 14px;
    font-weight: 600;
}

.controls-note {
    margin: 0;
    font-size: 12px;
    line-height: 1.5;
    color: var(--theme-ui-text-secondary);
    text-align: center;
}

@media (max-width: 480px) {
    .menu-title {
        font-size: 40px;
    }

    .controls-list {
        grid-template-columns: 1fr;
    }
}
