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

html, body {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);
    color: #fff;
    touch-action: manipulation;
}

.game-container {
    height: 100%;
    max-width: 500px;
    margin: 0 auto;
    padding: 10px;
    padding-top: env(safe-area-inset-top, 10px);
    padding-bottom: max(5px, env(safe-area-inset-bottom, 5px));
    display: flex;
    flex-direction: column;
}

header {
    display: flex;
    align-items: center;
    padding: 5px 0 10px;
    flex-shrink: 0;
    gap: 10px;
}

header.game-active h1 {
    display: none;
}

h1 {
    font-size: 1.2em;
    color: #e94560;
}

.stats {
    display: flex;
    gap: 10px;
    font-size: 0.9em;
}

.stat {
    background: rgba(255,255,255,0.1);
    padding: 6px 12px;
    border-radius: 6px;
}

.stat-value {
    color: #fff;
    background: #e94560;
    padding: 2px 8px;
    border-radius: 4px;
    font-weight: bold;
}

.difficulty-stat {
    text-transform: uppercase;
    font-size: 0.85em;
}

.new-btn {
    background: rgba(255,255,255,0.1);
    color: #fff;
    border: 1px solid rgba(255,255,255,0.3);
    padding: 6px 14px;
    font-size: 0.85em;
    border-radius: 6px;
    cursor: pointer;
    margin-right: auto;
}

.new-btn:hover {
    background: rgba(255,255,255,0.2);
}

.shelving-unit {
    flex: 1;
    background: linear-gradient(180deg, #8b4513 0%, #654321 100%);
    border-radius: 10px;
    padding: 4px 6px;
    margin-bottom: 10px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-height: 0;
}

.shelf {
    flex: 1;
    background: linear-gradient(180deg, #a0522d 0%, #8b4513 100%);
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px 6px;
    gap: 6px;
    box-shadow: inset 0 -4px 8px rgba(0,0,0,0.3);
}

.shelf-slot {
    flex: 1;
    max-width: 25%;
    height: 92%;
    border: 2px dashed rgba(255,255,255,0.25);
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.shelf-slot.drag-over {
    border-color: #4ecca3;
    background: rgba(78, 204, 163, 0.3);
}

.dvd {
    width: 85%;
    height: 92%;
    max-width: 70px;
    border-radius: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: grab;
    box-shadow: 2px 2px 6px rgba(0,0,0,0.4);
    overflow: hidden;
}

.dvd:active {
    cursor: grabbing;
}

.dvd.selected {
    outline: 3px solid #4ecca3;
    transform: scale(1.08);
    z-index: 100;
}

.dvd.dragging {
    opacity: 0.5;
}

.dvd.touch-drag-clone {
    box-shadow: 0 8px 20px rgba(0,0,0,0.6);
    transform: scale(1.1);
}

.dvd-spine {
    writing-mode: vertical-rl;
    text-orientation: mixed;
    transform: rotate(180deg);
    font-size: clamp(0.6em, 2vw, 0.8em);
    font-weight: bold;
    text-align: center;
    padding: 4px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-height: 90%;
    color: #fff;
    text-shadow: 1px 1px 2px rgba(0,0,0,0.9), 0 0 8px rgba(0,0,0,0.8);
    background: linear-gradient(to right, rgba(0,0,0,0.7) 0%, rgba(0,0,0,0.5) 50%, rgba(0,0,0,0.7) 100%);
    border-radius: 2px;
}

.floor-area {
    flex-shrink: 0;
    height: 22%;
    min-height: 100px;
    background: linear-gradient(180deg, #2d2d44 0%, #1a1a2e 100%);
    border-radius: 10px;
    padding: 15px 10px 10px;
    border: 2px solid #e94560;
    position: relative;
    display: flex;
    flex-direction: column;
}

.floor-header {
    position: absolute;
    top: -12px;
    left: 0;
    right: 0;
    display: flex;
    justify-content: space-between;
    padding: 0 12px;
}

.floor-label {
    background: #e94560;
    padding: 4px 12px;
    border-radius: 4px;
    font-weight: bold;
    font-size: 0.8em;
}

.floor-capacity {
    background: #0f3460;
    padding: 4px 12px;
    border-radius: 4px;
    font-weight: bold;
    font-size: 0.8em;
}

.floor-capacity.warning {
    background: #ff6b6b;
    animation: pulse 0.5s infinite;
}

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

.floor-dvds {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding-top: 5px;
    overflow: hidden;
}

.floor-dvds .dvd {
    width: 60px;
    min-width: 45px;
    height: 100%;
    flex-shrink: 1;
}

.floor-dvds .dvd .dvd-spine {
    max-height: 100%;
}

/* Overlays */
.game-over-overlay, .start-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0,0,0,0.92);
    display: flex;
    flex-direction: column;
    align-items: center;
    z-index: 1000;
    padding: 20px;
    padding-top: max(20px, env(safe-area-inset-top, 20px));
    padding-bottom: max(20px, env(safe-area-inset-bottom, 20px));
    text-align: center;
    overflow-y: auto;
}

/* Flex spacers for vertical centering that works with overflow scroll */
.game-over-overlay::before,
.game-over-overlay::after,
.start-overlay::before,
.start-overlay::after {
    content: '';
    flex: 1 0 0px;
}

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

.game-over-overlay.active {
    display: flex;
}

.game-over-overlay.win .game-over-text {
    color: #4ecca3;
    text-shadow: 0 0 20px #4ecca3, 0 0 40px #4ecca3, 0 0 60px #4ecca3;
    animation: winPulse 1s ease-in-out infinite;
}

@keyframes winPulse {
    0%, 100% {
        transform: scale(1);
        text-shadow: 0 0 20px #4ecca3, 0 0 40px #4ecca3, 0 0 60px #4ecca3;
    }
    50% {
        transform: scale(1.1);
        text-shadow: 0 0 30px #4ecca3, 0 0 60px #4ecca3, 0 0 90px #4ecca3;
    }
}

.falling-emoji {
    position: absolute;
    font-size: 24px;
    opacity: 0;
    pointer-events: none;
    will-change: transform, opacity;
}

.game-over-overlay.win .falling-emoji {
    animation: emojiFall 4s linear forwards;
}

@keyframes emojiFall {
    0% {
        opacity: 1;
        transform: translateY(0) rotate(0deg);
    }
    80% {
        opacity: 1;
    }
    100% {
        opacity: 0;
        transform: translateY(100vh) rotate(360deg);
    }
}

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

.game-over-text {
    font-size: 2.5em;
    color: #e94560;
    margin-bottom: 15px;
    text-shadow: 0 0 20px #e94560;
}

.game-over-difficulty {
    font-size: 1.1em;
    color: #4ecca3;
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 10px;
}

.final-score {
    font-size: 1.5em;
    margin-bottom: 20px;
}


.game-over-buttons {
    display: flex;
    gap: 12px;
    margin-top: 10px;
}

.restart-btn, .start-btn {
    background: #e94560;
    color: #fff;
    border: none;
    padding: 14px 35px;
    font-size: 1.2em;
    border-radius: 10px;
    cursor: pointer;
}

.share-btn {
    background: rgba(255,255,255,0.1);
    color: #fff;
    border: 2px solid rgba(255,255,255,0.3);
    padding: 14px 35px;
    font-size: 1.2em;
    border-radius: 10px;
    cursor: pointer;
    transition: all 0.2s;
}

.share-btn:hover {
    background: rgba(255,255,255,0.2);
}

.share-btn.copied {
    background: #4ecca3;
    border-color: #4ecca3;
}

.start-overlay h1 {
    font-size: 2.2em;
    margin-bottom: 25px;
    color: #e94560;
}

.start-instructions {
    max-width: 350px;
    text-align: center;
    margin-bottom: 30px;
    line-height: 1.6;
    font-size: 1em;
}

.start-instructions h3 {
    color: #e94560;
    margin-bottom: 15px;
}

.start-instructions p {
    margin-bottom: 10px;
}

.difficulty-select {
    margin-bottom: 10px;
    width: 90%;
    max-width: 400px;
}

.difficulty-select h3 {
    color: #e94560;
    margin-bottom: 15px;
}

.difficulty-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    width: 100%;
}

@media (min-width: 600px) {
    .difficulty-buttons {
        grid-template-columns: 1fr;
    }
}

.difficulty-btn {
    background: rgba(255,255,255,0.1);
    color: #fff;
    border: 2px solid rgba(255,255,255,0.2);
    padding: 14px 24px;
    font-size: 1.1em;
    border-radius: 8px;
    cursor: pointer;
    transition: all 0.2s;
    width: 100%;
}

.difficulty-btn:hover {
    background: #e94560;
    border-color: #e94560;
    transform: scale(1.05);
}

.difficulty-btn:active {
    transform: scale(0.98);
}

.credit {
    margin-top: 20px;
    font-size: 0.85em;
    color: rgba(255,255,255,0.5);
    flex-shrink: 0;
}

.credit a {
    color: rgba(255,255,255,0.7);
    text-decoration: none;
}

.credit a:hover {
    color: #e94560;
    text-decoration: underline;
}

/* Mobile start screen adjustments */
@media (max-height: 650px) {
    .start-overlay {
        padding: 15px;
        padding-top: max(15px, env(safe-area-inset-top, 15px));
    }

    .start-overlay h1 {
        font-size: 1.8em;
        margin-bottom: 15px;
    }

    .start-instructions {
        margin-bottom: 15px;
        line-height: 1.4;
        font-size: 0.9em;
    }

    .start-instructions h3 {
        margin-bottom: 10px;
    }

    .start-instructions p {
        margin-bottom: 6px;
    }

    .difficulty-select h3 {
        margin-bottom: 10px;
    }

    .difficulty-buttons {
        gap: 8px;
    }

    .difficulty-btn {
        padding: 10px 20px;
        font-size: 1em;
    }

    .credit {
        margin-top: 15px;
    }
}

@media (max-height: 550px) {
    .start-overlay h1 {
        font-size: 1.5em;
        margin-bottom: 10px;
    }

    .start-instructions {
        margin-bottom: 10px;
        font-size: 0.85em;
    }

    .start-instructions p {
        margin-bottom: 4px;
    }

    .difficulty-btn {
        padding: 8px 16px;
        font-size: 0.95em;
    }

    .credit {
        margin-top: 10px;
        font-size: 0.75em;
    }
}

#gameOverReason {
    color: #ff6b6b;
    margin-bottom: 15px;
    text-align: center;
}

/* Animations */
.dvd-falling {
    animation: fallToFloor 0.6s cubic-bezier(0.4, 0, 0.9, 0.6) forwards;
}

@keyframes fallToFloor {
    0% {
        transform: translateY(0) rotate(0deg);
        opacity: 1;
    }
    15% {
        transform: translateY(-10px) rotate(-3deg);
    }
    100% {
        transform: translateY(var(--fall-distance, 200px)) rotate(8deg);
        opacity: 0.8;
    }
}

.dvd-spawn {
    animation: spawnIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

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

.spawn-flash {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.6em;
    font-weight: bold;
    color: #39ff14;
    text-shadow: 0 0 6px #39ff14, 0 0 12px #39ff14, 0 0 8px rgba(0,0,0,0.9);
    background: radial-gradient(ellipse, rgba(0,0,0,0.6) 30%, transparent 70%);
    pointer-events: none;
    animation: flashPop 0.45s ease-out forwards;
    z-index: 10;
}

@keyframes flashPop {
    0% {
        opacity: 1;
        transform: scale(0.5);
    }
    40% {
        opacity: 1;
        transform: scale(1.2);
    }
    100% {
        opacity: 0;
        transform: scale(1.5);
    }
}

.dvd-placed {
    animation: placed 0.2s ease-out;
}

@keyframes placed {
    0% { transform: scale(1.15); }
    100% { transform: scale(1); }
}
