/* ============================================
   DESIGN TOKENS
   ============================================ */

/* ============================================
   BASE STYLES & RESET
   ============================================ */
*,
*::before,
*::after {
    box-sizing: border-box;
}

html {
    font-size: 16px;
    -webkit-text-size-adjust: 100%;
}

body {
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
    overflow: hidden;
    background-color: var(--color-grass-light);
    font-family: var(--font-family-base);
    line-height: 1.5;
    color: var(--color-text);
    transition: background-color 0.25s ease, color 0.25s ease;
}

body[data-font-mode="readable"] {
    font-family: var(--font-family-readable);
}

:root {
    --hud-collapsed-height: 58px;
}

/* Skip Link */
.skip-link {
    position: absolute;
    top: -40px;
    left: 0;
    background: var(--color-black);
    color: var(--color-white);
    padding: var(--space-2) var(--space-3);
    text-decoration: none;
    z-index: 1000;
    border-radius: 0 0 var(--radius-md) 0;
    font-weight: bold;
    transition: top 0.2s;
}

.skip-link:focus {
    top: 0;
    outline: 3px solid var(--color-focus-ring);
    outline-offset: 2px;
}

/* Screen Reader Only */
.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ============================================
   GAME VIEWPORT & WORLD
   ============================================ */
#viewport {
    position: absolute;
    width: 100vw;
    height: 100vh;
    overflow: hidden;
    touch-action: none;
    /* Prevent default touch actions for game */
}

#game-area {
    position: absolute;
    /* Розміри — це запасне значення для першого кадру та для тестів без сцени.
       Фактичний розмір світу задає рівень: GameScene.applyWorldSize() виставляє
       width/height з level.world (див. js/game/level-data.js). */
    width: 4200px;
    height: 4200px;
    background-color: var(--color-grass-dark);
    background-image: radial-gradient(var(--color-grass-pattern) 2px, transparent 2px);
    background-size: 40px 40px;
    cursor: crosshair;
    transform-origin: 0 0;
    will-change: transform;
}

/* ============================================
   LAYERS & CONTAINERS
   ============================================ */
#obstacles-container,
#items-container,
#npcs-container {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
}

#items-container {
    z-index: var(--z-items);
}

#npcs-container {
    z-index: var(--z-npcs);
}

/* ============================================
   MOVE TARGET INDICATOR
   ============================================ */
#move-target {
    position: absolute;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: 2.5px solid var(--color-apple, #e53935);
    background: rgba(229, 57, 53, 0.18);
    margin-left: -12px;
    margin-top: -12px;
    pointer-events: none;
    z-index: var(--z-player, 10);
    animation: move-target-pulse 0.55s ease-in-out infinite alternate;
}

@keyframes move-target-pulse {
    from { transform: scale(0.75); opacity: 0.55; }
    to   { transform: scale(1.15); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
    #move-target { animation: none; opacity: 0.8; }
}

/* ============================================
   PLAYER (Snail)
   ============================================ */
#player-wrapper {
    position: absolute;
    width: 40px;
    height: 40px;
    margin-left: -20px;
    margin-top: -20px;
    pointer-events: none;
    z-index: var(--z-player);
    will-change: transform;
}

#player-inner {
    width: 100%;
    height: 100%;
    position: relative;
    will-change: transform;
}

.snail-body {
    position: absolute;
    width: 48px;
    height: 18px;
    background-color: var(--color-snail-body);
    border-radius: 10px;
    top: 11px;
    left: -4px;
    border: 2px solid var(--color-snail-body-border);
    z-index: 1;
    transform-origin: center left;
    transition: transform 180ms ease-out;
    box-shadow: inset 0 -2px 4px var(--color-snail-body-shadow);
}

.snail-shell {
    position: absolute;
    width: 36px;
    height: 36px;
    background: radial-gradient(circle at 30% 30%, var(--color-snail-shell-light) 20%, var(--color-snail-shell-dark) 80%);
    border-radius: 50%;
    top: 2px;
    left: -6px;
    border: 2px solid var(--color-snail-shell-border);
    box-shadow: 2px 2px 4px var(--color-shadow-strong);
    z-index: 2;
    transition: transform 180ms ease-out;
}

.snail-shell::after {
    content: '';
    position: absolute;
    width: 20px;
    height: 20px;
    border: 2px solid var(--color-shell-spiral);
    border-radius: 50%;
    top: 6px;
    left: 6px;
    border-bottom-color: transparent;
    border-left-color: transparent;
    transform: rotate(45deg);
}

.eyestalk {
    position: absolute;
    width: 10px;
    height: 4px;
    background-color: var(--color-snail-body);
    right: -10px;
    z-index: 1;
    border-radius: 0 2px 2px 0;
    border: 2px solid var(--color-snail-body-border);
    border-left: none;
    transition: transform 180ms ease-out;
    transform-origin: left center;
}

.eyestalk::after {
    content: '';
    position: absolute;
    width: 6px;
    height: 6px;
    background: radial-gradient(circle at 30% 30%, var(--color-eye-mid) 30%, var(--color-eye-dark) 70%);
    border-radius: 50%;
    right: -3px;
    top: -3px;
    box-shadow: 0 1px 2px var(--color-shadow-medium);
    animation: blink 5s infinite;
}

@keyframes blink {

    0%,
    96%,
    100% {
        transform: scaleY(1);
    }

    98% {
        transform: scaleY(0.12);
    }
}

.eyestalk.left {
    top: 10px;
    transform: translateX(0) rotate(-22deg);
}

.eyestalk.right {
    bottom: 10px;
    transform: translateX(0) rotate(22deg);
}

#player-inner.is-idle .snail-body {
    transform: scaleX(1) scaleY(1);
    animation: breathe 2.8s ease-in-out infinite;
}

#player-inner.is-idle .eyestalk.left {
    transform: translateX(0) rotate(-22deg);
}

#player-inner.is-idle .eyestalk.right {
    transform: translateX(0) rotate(22deg);
}

#player-inner.is-idle .snail-shell {
    animation: shellBreathe 2.8s ease-in-out infinite;
}

#player-inner.is-moving .snail-body {
    transform: scaleX(1.1) scaleY(0.97);
}

#player-inner.is-moving .eyestalk.left {
    transform: translateX(7px) rotate(-14deg);
}

#player-inner.is-moving .eyestalk.right {
    transform: translateX(7px) rotate(14deg);
}

#player-inner.is-moving .snail-shell {
    animation: shellGlide 900ms ease-in-out infinite;
}

#player-inner.is-turning-left .snail-shell {
    transform: rotate(-5deg);
}

#player-inner.is-turning-right .snail-shell {
    transform: rotate(5deg);
}

@keyframes breathe {

    0%,
    100% {
        transform: scaleX(1) scaleY(1);
    }

    50% {
        transform: scaleX(1.02) scaleY(0.98);
    }
}

@keyframes shellBreathe {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-1px);
    }
}

@keyframes shellGlide {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-2px);
    }
}

/* ============================================
   KEYBOARD CURSOR (Accessibility)
   ============================================ */
#keyboard-cursor {
    position: absolute;
    width: 40px;
    height: 40px;
    margin-left: -20px;
    margin-top: -20px;
    z-index: var(--z-cursor);
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.2s;
}

#keyboard-cursor.active {
    opacity: 1;
}

.cursor-crosshair {
    width: 100%;
    height: 100%;
    border: 3px dashed var(--color-focus-ring);
    border-radius: 50%;
    position: relative;
    animation: cursorPulse 1s ease-in-out infinite;
}

.cursor-crosshair::before,
.cursor-crosshair::after {
    content: '';
    position: absolute;
    background: var(--color-focus-ring);
}

.cursor-crosshair::before {
    width: 4px;
    height: 16px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.cursor-crosshair::after {
    width: 16px;
    height: 4px;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

@keyframes cursorPulse {

    0%,
    100% {
        transform: scale(1);
        opacity: 1;
    }

    50% {
        transform: scale(1.1);
        opacity: 0.8;
    }
}

/* ============================================
   DIALOG / MODAL
   ============================================ */
#dialog-layer {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: var(--color-overlay);
    z-index: var(--z-modal);
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: clamp(0.5rem, 3vw, 1rem);
    backdrop-filter: blur(2px);
}

.dialog-box {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-xl);
    padding: var(--space-5);
    width: 100%;
    max-width: 560px;
    max-height: 100%;
    overflow-y: auto;
    overscroll-behavior: contain;
    box-shadow: var(--shadow-3);
    position: relative;
    display: flex;
    flex-direction: column;
}

.dialog-title {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    font-size: var(--font-size-2xl);
    color: var(--color-text);
    margin: 0 0 var(--space-3) 0;
    border-bottom: 2px solid var(--color-surface-raised);
    padding-bottom: var(--space-2);
    line-height: 1.3;
}

#dialog-layer .dialog-title--intro {
    font-size: clamp(1.45rem, 1.05rem + 1.6vw, 2.35rem);
}

.game-icon {
    display: inline-block;
    width: 1.25em;
    height: 1.25em;
    flex: none;
    vertical-align: middle;
}

.dialog-title .animal-art {
    width: 3.25rem;
    height: 3.25rem;
    flex: none;
    padding: var(--space-1);
    border-radius: var(--radius-md);
    background: var(--color-picture-backing);
}

.dialog-text {
    font-size: var(--font-size-lg);
    margin-bottom: var(--space-3);
    line-height: 1.45;
    color: var(--color-text);
}

.intro-list {
    list-style: none;
    margin: 0 0 var(--space-4) 0;
    padding: 0;
    display: grid;
    gap: var(--space-2);
}

.intro-list li {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    font-size: var(--font-size-base);
    line-height: 1.4;
    color: var(--color-text);
    padding: var(--space-2) var(--space-3);
    background: var(--color-surface-raised);
    border-radius: var(--radius-md);
    border: 1px solid var(--color-border);
}

.dialog-content {
    display: grid;
    gap: var(--space-4);
}

/* ── Virtual Joystick (touch devices) ───────────────────── */
#joystick-outer {
    position: fixed;
    width: 104px;   /* 2 × RADIUS */
    height: 104px;
    margin-left: -52px;
    margin-top: -52px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    border: 2px solid rgba(255, 255, 255, 0.45);
    backdrop-filter: blur(4px);
    z-index: var(--z-ui, 100);
    pointer-events: none;
    touch-action: none;
}

#joystick-knob {
    position: absolute;
    top: 50%;
    left: 50%;
    width: 44px;
    height: 44px;
    margin-left: -22px;
    margin-top: -22px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.65);
    border: 2px solid rgba(255, 255, 255, 0.9);
    transform: translate(-50%, -50%);
    transition: transform 0.05s linear;
}

@media (prefers-reduced-motion: reduce) {
    #joystick-knob { transition: none; }
}

/* ── SW Update Banner ────────────────────────────────────── */
#sw-update-banner {
    position: fixed;
    bottom: var(--space-4);
    left: 50%;
    transform: translateX(-50%);
    z-index: 9999;
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding: var(--space-2) var(--space-3);
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border-strong);
    border-radius: var(--radius-pill);
    box-shadow: var(--shadow-3);
    font-size: var(--font-size-sm);
    white-space: nowrap;
}

#sw-update-banner button {
    padding: var(--space-1) var(--space-2);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    background: var(--color-primary);
    color: var(--color-text-inverse);
    font: inherit;
    font-size: var(--font-size-xs);
    cursor: pointer;
}

#sw-update-banner button[aria-label] {
    background: transparent;
    color: var(--color-text-muted);
    border-color: transparent;
    padding: var(--space-1);
}

/* ── Win Screen ──────────────────────────────────────────── */
.win-stats-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: var(--space-3);
}

/* Рівні з грушами показують три картки підсумків */
.win-stats-grid--three {
    grid-template-columns: repeat(3, 1fr);
}

.win-stat-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-3) var(--space-2);
    background: var(--color-surface-raised);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    text-align: center;
}

.win-stat-card__icon {
    font-size: 2.4rem;
    line-height: 1;
}

.win-stat-card__value {
    font-size: var(--font-size-2xl, 2rem);
    font-weight: 800;
    color: var(--color-primary);
    line-height: 1;
}

.win-stat-card__label {
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
}

.dialog-status {
    min-height: 2.2em;
    margin: var(--space-3) 0 0;
    font-size: var(--font-size-sm);
    color: var(--color-text-muted);
    font-weight: 600;
}

.dialog-status--success {
    color: var(--color-success);
    font-weight: 700;
}

.dialog-status--error {
    color: var(--color-error);
}

.dialog-actions {
    display: flex;
    gap: var(--space-3);
    justify-content: center;
    margin-top: var(--space-3);
}

.dialog-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-2);
    background: var(--color-action);
    border: 2px solid transparent;
    padding: var(--space-2) var(--space-5);
    font-size: var(--font-size-lg);
    color: var(--color-white);
    border-radius: var(--radius-lg);
    cursor: pointer;
    transition: all 0.2s;
    min-width: 120px;
    min-height: 48px;
    /* WCAG 2.5.8 */
    font-weight: 600;
}

.dialog-box,
.dialog-btn {
    transition: background-color 0.25s ease, border-color 0.25s ease, color 0.25s ease, box-shadow 0.25s ease;
}

.dialog-btn:hover {
    background: var(--color-action-hover);
    transform: translateY(-2px);
    box-shadow: var(--shadow-2);
}

.dialog-btn:focus {
    outline: 3px solid var(--color-focus-ring);
    outline-offset: 2px;
    border-color: var(--color-focus-ring);
}

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

.reward-burst {
    position: fixed;
    inset: 0;
    z-index: calc(var(--z-modal) + 1);
    pointer-events: none;
    display: grid;
    place-items: center;
}

.reward-star {
    display: grid;
    place-items: center;
    width: 96px;
    height: 96px;
    border-radius: 50%;
    background: color-mix(in srgb, var(--color-warning) 24%, var(--color-surface));
    border: 4px solid var(--color-warning);
    color: var(--color-warning);
    font-size: 4rem;
    line-height: 1;
    text-shadow: 0 2px 0 var(--color-white);
    box-shadow: var(--shadow-3);
    animation: rewardStarPop 900ms ease-out both;
}

.confetti-piece {
    position: absolute;
    width: 10px;
    height: 16px;
    border-radius: 3px;
    background: var(--confetti-color);
    transform: rotate(var(--angle)) translateY(0);
    animation: confettiFly 1100ms ease-out both;
}

@keyframes rewardStarPop {
    0% {
        transform: scale(0.35) rotate(-18deg);
        opacity: 0;
    }

    45% {
        transform: scale(1.18) rotate(8deg);
        opacity: 1;
    }

    100% {
        transform: scale(1) rotate(0);
        opacity: 0;
    }
}

@keyframes confettiFly {
    0% {
        opacity: 1;
        transform: rotate(var(--angle)) translateY(0) scale(0.7);
    }

    100% {
        opacity: 0;
        transform: rotate(var(--angle)) translateY(var(--distance)) scale(1);
    }
}

/* Secondary button style if needed */
.dialog-btn--secondary {
    background: var(--color-surface-raised);
    color: var(--color-text);
    border-color: var(--color-border);
}

.dialog-btn--secondary:hover {
    background: var(--color-surface);
}

/* ============================================
   ACCESSIBILITY: HIGH CONTRAST MODE
   ============================================ */
@media (prefers-contrast: high) {
    :root {
        --color-grass-light: var(--color-contrast-grass-light);
        --color-grass-dark: var(--color-contrast-grass-dark);
        --color-rock: var(--color-contrast-rock);
        --color-bush: var(--color-contrast-bush);
        --color-apple: var(--color-contrast-apple);
    }

}

/* ============================================
   ACCESSIBILITY: REDUCED MOTION
   ============================================ */
@media (prefers-reduced-motion: reduce) {

    *,
    *::before,
    *::after {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }

    #player-inner {
        transition: transform 0.1s linear !important;
    }

    #player-inner.is-moving .snail-shell,
    #player-inner.is-idle .snail-shell,
    #player-inner.is-idle .snail-body,
    #player-inner.is-idle .eyestalk {
        animation: none !important;
    }

    .cursor-crosshair {
        animation: none;
    }
}

/* ============================================
   RESPONSIVE ADJUSTMENTS
   ============================================ */
@media (max-width: 480px) {
    .dialog-box {
        padding: var(--space-3);
        border-width: 1px;
    }

    .dialog-title {
        font-size: var(--font-size-xl);
    }

    .dialog-text {
        font-size: var(--font-size-base);
    }

}

/* Планшети: без виділення тексту, довгого дотику й щипкового масштабу. */
html,
body {
    touch-action: pan-x pan-y;
    overscroll-behavior: none;
}

body {
    -webkit-user-select: none;
    user-select: none;
    -webkit-touch-callout: none;
    -webkit-tap-highlight-color: transparent;
}
