/* ==================== RESET & BASE ==================== */

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

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

body {
    font-family: system-ui, -apple-system, sans-serif;
    background: #000;
    color: #fff;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    align-items: stretch;
    /* Changed from center to stretch */
    overflow: hidden;
    touch-action: manipulation;
    -webkit-overflow-scrolling: touch;
}

#title-bar {
    display: flex !important;
    flex-direction: row !important;
    justify-content: space-between !important;
    align-items: center !important;
    gap: 0.5rem !important;
    margin: 0.8vh 0.5rem !important;
    padding: 0.3rem 0.5rem !important;
    min-height: 3rem !important;
    max-height: 5rem !important;
    width: 100% !important;
    box-sizing: border-box !important;
    flex-shrink: 0 !important;
    flex-grow: 0 !important;
    flex-wrap: nowrap !important;
}

#title-bar * {
    flex-shrink: 0 !important;
    white-space: nowrap !important;
    overflow: hidden !important;
    text-overflow: ellipsis !important;
}

#title-bar h1 {
    text-align: center;
    font-size: clamp(1rem, 4vw, 1.5rem);
    margin: 0;
    color: #fff;
    line-height: 1.2;
    flex: 1 !important;
    min-width: 0 !important;
    order: 2 !important;
    flex-shrink: 1 !important;
}

#title-bar button {
    order: 1 !important;
    flex-shrink: 0 !important;
    flex-grow: 0 !important;
    white-space: nowrap !important;
    font-size: 0.75rem !important;
    padding: 0.4rem 0.7rem !important;
    min-width: 55px !important;
}

#title-bar button:last-child {
    order: 3 !important;
}

#title-bar.hidden {
    opacity: 0;
    transform: translateY(-100%);
    height: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
}

.btn-small {
    padding: 0.4rem 0.7rem !important;
    font-size: 0.75rem !important;
    border-radius: 6px;
    min-width: 60px !important;
    white-space: nowrap !important;
}


/* ==================== SAFE AREA INSETS ==================== */

@supports (padding: max(0px)) {
    #header,
    #footer,
    #game {
        width: calc(100% - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px));
        margin-left: env(safe-area-inset-left, 0px);
        margin-right: env(safe-area-inset-right, 0px);
    }
}


/* ==================== BUTTONS ==================== */

button {
    padding: 0.8rem 1.2rem;
    font-size: 1rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    background: #4a90e2;
    color: #fff;
    transition: background 0.2s, transform 0.1s;
    min-width: 80px;
    touch-action: manipulation;
    user-select: none;
    -webkit-user-select: none;
}

button:hover {
    background: #357ab8;
}

button:active {
    transform: scale(0.95);
}

button:disabled {
    background: #999;
    cursor: not-allowed;
    opacity: 0.6;
}


/* ==================== HEADER & FOOTER ==================== */

#header,
#footer {
    height: 2vh;
    min-height: 3px;
    width: 100%;
    display: flex;
    overflow: hidden;
    transition: transform 0.5s ease-in-out;
    flex-shrink: 0;
}

.segment {
    flex: 1;
    height: 100%;
    transition: transform 0.5s ease-in-out;
    min-width: 0;
}

.red {
    background: red;
}

.orange {
    background: orange;
}

.yellow {
    background: yellow;
}

.green {
    background: green;
}

.blue {
    background: blue;
}

.purple {
    background: purple;
}


/* ==================== SCORING ==================== */

#scoring {
    margin: 0.4vh 0.5rem !important;
    font-size: clamp(0.9rem, 3vw, 1.2rem) !important;
    display: grid !important;
    grid-template-columns: repeat(3, auto) !important;
    gap: 0.8rem !important;
    padding: 0.2rem 0.5rem !important;
    justify-content: center !important;
    align-items: center !important;
    flex-wrap: nowrap !important;
    flex-shrink: 0 !important;
    width: 100% !important;
    font-weight: 700;
    font-size: clamp(1.2rem, 4vw, 1.8rem);
}


/* ==================== TIMER ==================== */

#timer {
    color: #4afff6;
    transition: transform 0.15s ease;
}

#timer.warning {
    color: #ffbd07;
    animation: timerPulse 0.6s ease-in-out infinite;
}

@keyframes timerPulse {
    0%,
    100% {
        transform: scale(1);
        opacity: 1;
    }
    50% {
        transform: scale(1.15);
        opacity: 0.75;
    }
}

.score-increase,
.score-decrease {
    animation: scorePop 0.3s ease-out;
}

@keyframes scorePop {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.3);
    }
    100% {
        transform: scale(1);
    }
}


/* ==================== GAME AREA ==================== */

#game {
    position: relative;
    flex: 1 !important;
    min-height: 0;
    width: 100% !important;
    overflow: hidden;
    background-image: radial-gradient(circle at 20% 30%, rgba(74, 144, 226, 0.05) 0%, transparent 25%), radial-gradient(circle at 80% 70%, rgba(255, 165, 79, 0.05) 0%, transparent 25%), linear-gradient(180deg, #fff 0%, #f8f8ff 30%, #f0f0f5 60%, #e8e8f0 100%);
    border: 3px solid #333;
    box-shadow: inset 0 0 20px rgba(0, 0, 0, 0.1);
    touch-action: none;
    overscroll-behavior: contain;
    -webkit-user-select: none;
    user-select: none;
    flex-shrink: 1 !important;
}

#game::before {
    content: '';
    position: absolute;
    inset: 0;
    background-image: linear-gradient(rgba(0, 0, 0, 0.03) 1px, transparent 1px), linear-gradient(90deg, rgba(0, 0, 0, 0.03) 1px, transparent 1px);
    background-size: 50px 50px;
    pointer-events: none;
    z-index: 0;
}


/* ==================== PLAYER DINO ==================== */

.player-dino {
    position: absolute;
    bottom: 8px;
    left: 50%;
    transform: translateX(-50%);
    font-size: clamp(3rem, 10vw, 5rem);
    line-height: 1;
    z-index: 100;
    pointer-events: none;
    filter: drop-shadow(0 5px 8px rgba(0, 0, 0, 0.35));
    will-change: left;
}

.player-dino::after {
    content: '';
    position: absolute;
    left: 50%;
    bottom: -6px;
    transform: translateX(-50%);
    width: 46px;
    height: 9px;
    border-radius: 50%;
    background: radial-gradient(ellipse, rgba(0, 0, 0, 0.28) 0%, transparent 72%);
}


/* ==================== FALLING FOOD ==================== */

.falling-food {
    position: absolute;
    top: -60px;
    font-size: clamp(1.9rem, 6vw, 2.6rem);
    line-height: 1;
    z-index: 10;
    pointer-events: none;
    filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.28));
    will-change: top;
    animation-name: fall;
    animation-timing-function: linear;
    animation-fill-mode: forwards;
    animation-duration: var(--fall-ms, 4000ms);
}

.falling-food[data-kind="good"] {
    filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.28)) drop-shadow(0 0 6px rgba(90, 190, 90, 0.45));
}

.falling-food[data-kind="bonus"] {
    filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.28)) drop-shadow(0 0 12px rgba(255, 200, 0, 0.8));
    animation-name: fall, sparkle;
    animation-timing-function: linear, ease-in-out;
    animation-fill-mode: forwards, none;
    animation-iteration-count: 1, infinite;
    animation-direction: normal, alternate;
    animation-duration: var(--fall-ms, 4000ms), 0.45s;
}

.falling-food[data-kind="bad"] {
    filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.28)) drop-shadow(0 0 6px rgba(210, 70, 70, 0.45));
}

.falling-food[data-kind="comet"] {
    filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.28)) drop-shadow(0 0 14px rgba(255, 60, 0, 0.9));
    animation-name: fall, cometSpin;
    animation-timing-function: linear, linear;
    animation-fill-mode: forwards, none;
    animation-iteration-count: 1, infinite;
    animation-duration: var(--fall-ms, 3000ms), 0.5s;
}

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

.player-dino.stunned {
    animation: dizzy 0.25s linear infinite;
    filter: grayscale(0.7) drop-shadow(0 5px 8px rgba(0, 0, 0, 0.35));
}

@keyframes dizzy {
    0%,
    100% {
        transform: translateX(-50%) rotate(-10deg);
    }
    50% {
        transform: translateX(-50%) rotate(10deg);
    }
}

@keyframes fall {
    from {
        top: -60px;
    }
    to {
        top: calc(100% - 18px);
    }
}

@keyframes sparkle {
    from {
        transform: scale(.8) rotate(-15deg);
    }
    to {
        transform: scale(1.2) rotate(15deg);
    }
}

.falling-food.caught-good {
    transform: scale(1.6);
    opacity: 0;
    transition: transform 0.22s ease-out, opacity 0.22s ease-out;
}

.falling-food.caught-bad {
    filter: grayscale(100%) brightness(45%);
    transform: scale(0.7) rotate(25deg);
    opacity: 0;
    transition: transform 0.22s ease-in, opacity 0.22s ease-in;
}


/* ==================== SCORE FEEDBACK ==================== */

.score-increase {
    animation: scorePop 0.3s ease-out;
}

.score-decrease {
    animation: scorePop 0.3s ease-out;
}

@keyframes scorePop {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.3);
    }
    100% {
        transform: scale(1);
    }
}


/* ==================== MODAL ==================== */

.modal {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 10000;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    backdrop-filter: blur(5px);
    pointer-events: none;
}

.modal.active {
    opacity: 1;
    display: flex;
    visibility: visible;
    pointer-events: all;
}

.modal-content {
    background: linear-gradient(145deg, #fff 0%, #f0f0f5 100%);
    border-radius: 16px;
    padding: 2rem;
    max-width: 90%;
    width: 400px;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3), 0 0 0 3px #6d4aff, inset 0 1px 0 rgba(255, 255, 255, 0.8);
    text-align: center;
    position: relative;
    animation: modalSlideIn 0.4s ease-out;
}

@keyframes modalSlideIn {
    from {
        transform: translateY(-50px) scale(0.9);
        opacity: 0;
    }
    to {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

.modal-header h2 {
    margin: 0 0 1.5rem;
    font-size: clamp(1.2rem, 4vw, 1.8rem);
    color: #333;
    line-height: 1.3;
}

.modal-body p {
    margin: 0.5rem 0;
    font-size: 1rem;
    color: #555;
}

.final-score {
    font-size: clamp(3rem, 12vw, 5rem);
    font-weight: bold;
    color: #6d4aff;
    margin: 1rem 0;
    text-shadow: 2px 2px 4px rgba(109, 74, 255, 0.2);
    transition: transform 0.3s ease;
}

.modal.active .final-score {
    animation: scoreCountUp 0.6s ease-out;
}

@keyframes scoreCountUp {
    0% {
        transform: scale(0.5);
        opacity: 0;
    }
    50% {
        transform: scale(1.1);
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

.score-message {
    font-size: 1.2rem;
    font-weight: 500;
    margin-top: 0.5rem;
}

.score-message.success {
    color: #00aa00;
}

.score-message.attempt {
    color: #cc8800;
}

.score-message.fail {
    color: #d64545;
}

.modal-footer {
    display: flex;
    gap: 1rem;
    justify-content: center;
    flex-wrap: wrap;
}

.btn-primary {
    padding: 0.8rem 1.6rem;
    font-size: 1rem;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    background: linear-gradient(135deg, #6d4aff 0%, #8a6dff 100%);
    color: #fff;
    transition: transform 0.2s, box-shadow 0.2s;
    font-weight: 600;
    min-width: 120px;
}

.btn-primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(109, 74, 255, 0.4);
}

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

.btn-secondary {
    padding: 0.8rem 1.6rem;
    font-size: 1rem;
    border: 2px solid #ccc;
    border-radius: 8px;
    cursor: pointer;
    background: #fff;
    color: #555;
    transition: all 0.2s;
    font-weight: 500;
    min-width: 120px;
}

.btn-secondary:hover {
    background: #f0f0f5;
    border-color: #aaa;
}

.rainbow-bar {
    height: 6px;
    width: 100%;
    margin-top: 1.5rem;
    background: linear-gradient(90deg, red 0%, orange 16.67%, yellow 33.33%, green 50%, blue 66.67%, purple 83.33%, red 100%);
    border-radius: 3px;
}

.game-stats {
    display: flex;
    justify-content: center;
    gap: 1rem;
    flex-wrap: wrap;
    margin-top: 0.9rem;
    font-size: 0.95rem;
    color: #666;
}

.game-stats span {
    background: #f0f0f5;
    border-radius: 999px;
    padding: 0.35rem 0.8rem;
}


/* ==================== DIFFICULTY OPTION ==================== */

.difficulty-option {
    display: flex;
    align-items: center;
    padding: 1rem;
    margin: 0.5rem 0;
    background: #f8f8ff;
    border: 2px solid #e0e0e0;
    border-radius: 10px;
    transition: all 0.2s ease;
    cursor: pointer;
}

.difficulty-option:hover {
    background: #f0f0f5;
    border-color: #6d4aff;
    transform: translateY(-2px);
}

.difficulty-option input[type="radio"] {
    margin-right: 1rem;
    width: 20px;
    height: 20px;
    accent-color: #6d4aff;
    cursor: pointer;
}

.difficulty-option label {
    display: flex;
    align-items: center;
    flex-direction: column;
    gap: 0.3rem;
    flex: 1;
    cursor: pointer;
    padding: 0;
    margin: 0;
}

.difficulty-option input[type="radio"]:checked+label {
    color: #6d4aff;
    font-weight: 600;
}

.difficulty-icon {
    font-size: 1.8rem;
}

.difficulty-name {
    font-size: 1.1rem;
    font-weight: 600;
    color: #666;
}

.difficulty-desc {
    font-size: 0.85rem;
    color: #666;
}

.difficulty-option:has(input:checked) {
    border-color: #6d4aff;
    background: #f5f0ff;
    box-shadow: 0 0 0 2px rgba(109, 74, 255, 0.2);
}


/* ==================== RESPONSIVE ==================== */

@media screen and (orientation: landscape) and (max-height: 500px) {
    #scoring {
        font-size: clamp(0.8rem, 2.5vw, 1rem) !important;
        gap: 0.6rem !important;
        margin: 0.3vh 0.5rem !important;
    }
    button {
        padding: 0.4rem 0.8rem !important;
        font-size: 0.8rem !important;
        min-width: 65px !important;
    }
    #title-bar h1 {
        font-size: clamp(0.7rem, 2vw, 0.9rem) !important;
    }
    .btn-small {
        min-width: 55px !important;
        padding: 0.35rem 0.6rem !important;
    }
    .player-dino {
        font-size: clamp(2rem, 7vw, 2.8rem);
    }
    .falling-food {
        font-size: clamp(1.6rem, 5vw, 2rem);
    }
}

@media screen and (max-width: 400px) {
    #scoring {
        flex-direction: row !important;
        font-size: 0.95rem !important;
    }
    button {
        padding: 0.5rem 0.8rem !important;
        min-width: 55px !important;
        font-size: 0.85rem !important;
    }
    .btn-small {
        min-width: 50px !important;
        padding: 0.3rem 0.5rem !important;
    }
}

@media screen and (max-height: 450px) and (orientation: landscape) {
    .modal-content {
        padding: 1rem;
        max-width: 95%;
    }
    .modal-body p {
        font-size: 0.9rem;
    }
    .final-score {
        font-size: clamp(2rem, 10vw, 3rem);
    }
    .modal-header h2 {
        font-size: clamp(1rem, 3vw, 1.3rem);
    }
}

@media screen and (max-width: 480px) {
    .modal-content {
        padding: 1.3rem;
        width: 95%;
    }
    .modal-footer {
        flex-direction: column;
    }
    .btn-primary,
    .btn-secondary {
        width: 100%;
    }
}

@media (prefers-reduced-motion: reduce) {
    #header,
    #footer {
        transition: none !important;
    }
    .modal-content,
    .final-score {
        animation: none;
    }
}

@media screen and (max-width: 350px) {
    #title-bar h1 {
        font-size: 0.6rem !important;
    }
    #title-bar button {
        padding: 0.3rem 0.5rem !important;
        font-size: 0.65rem !important;
        min-width: 45px !important;
    }
}