/* Reset & Global styles */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    user-select: none;
    -webkit-user-select: none;
    touch-action: none; /* Disable default touch behaviors for fluid dragging */
}

body {
    background-color: #1a1105;
    color: #f7ede2;
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    display: flex;
    justify-content: center;
    align-items: flex-start; /* Snugly anchors the game window to the top edge, completely closing the top gap! */
}

/* Portrait view locking container - Responsive Aspect Ratio Locking to prevent tile distortions and bottom clipping! */
#game-container {
    position: relative;
    width: 100vw;
    height: 100vh;
    max-width: 448px; /* Restrict to internal game virtual resolution width */
    max-height: 780px; /* Restrict to internal game virtual resolution height (set to 780) */
    background: radial-gradient(circle, #2e442c 0%, #172415 100%);
    box-shadow: 0 0 20px rgba(0,0,0,0.8);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    container-type: size; /* ESTABLISHES CONTAINER QUERY BOUNDS FOR VECTOR-STYLE PROPORTIONAL SCALING! */
}

/* Elite containment media queries for 100% square tiles on all devices! */
@media (min-aspect-ratio: 448/780) {
    /* Screen is wider than game aspect ratio - restrict height to screen, letterbox sides */
    #game-container {
        height: 100vh;
        width: calc(100vh * (448 / 780));
    }
}

@media (max-aspect-ratio: 448/780) {
    /* Screen is taller than game aspect ratio - restrict width to screen, letterbox top/bottom */
    #game-container {
        width: 100vw;
        height: calc(100vw * (780 / 448));
    }
}

/* Responsive Canvas scaling */
#game-canvas {
    width: 100%;
    height: 100%;
    display: block;
}

/* UI Layer overlaying the Canvas */
#ui-layer {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none; /* Let clicks pass through to canvas unless overridden */
}

/* Level Indicator Banner - Vector container-relative scaling */
.level-indicator {
    position: relative; /* Static flex flow in Option A */
    background: linear-gradient(180deg, #5c4033 0%, #3e2712 100%); /* Oak plaque */
    border: 1.5px solid #8e6f3e; /* Gold border */
    border-radius: 2.2cqw;
    padding: 0.38cqh 4cqw;
    font-size: 2.9cqw; /* ENLARGED: Much larger, highly readable level title! */
    font-weight: 800;
    color: #dfb275;
    text-shadow: 0 1.5px #000;
    box-shadow: 0 4px 6px rgba(0,0,0,0.4);
    text-transform: uppercase;
    letter-spacing: 0.2cqw;
    z-index: 50;
    pointer-events: auto;
    text-align: center;
    white-space: nowrap;
    align-self: center;
}

/* HUD Styling - Option A "Triple-Decker" vertical rows stack */
.hud-container {
    position: absolute;
    top: 2.2cqh; /* Grounded vertically */
    left: 4cqw;
    right: 4cqw;
    display: flex;
    flex-direction: column; /* Vertical rows stack! */
    gap: 1.1cqh; /* spacing between rows */
    align-items: center;
    pointer-events: auto;
}

/* ROW 2: Stats & Controls Bar (Score | Controls | Moves) */
.hud-row-stats {
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: center;
    width: 100%;
}

.hud-item {
    background: rgba(46, 32, 15, 0.85);
    border: 2px solid #8e6f3e;
    border-radius: 1.8cqw;
    padding: 0.4cqh 2.5cqw;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 20cqw; /* MUCH WIDER Score & Moves indicators! */
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
}

.hud-item .label {
    font-size: 2.0cqw; /* ENLARGED: Scales to a clean, crisp readable label */
    text-transform: uppercase;
    color: #dfb275;
    font-weight: bold;
    letter-spacing: 0.1cqw;
}

.hud-item .value {
    font-size: 3.6cqw; /* ENLARGED: Scales to a bold, highly readable metric! */
    font-weight: 800;
    color: #fff;
}

.hud-controls {
    display: flex;
    gap: 1.5cqw; /* Comfortable button spacing */
    align-items: center;
    pointer-events: auto;
}

.ui-btn {
    pointer-events: auto;
    background: #8e6f3e;
    border: 1.5px solid #dfb275;
    color: white;
    border-radius: 50%;
    width: 8.5cqw; /* ENLARGED buttons */
    height: 8.5cqw;
    font-size: 3.4cqw; /* ENLARGED button font */
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 6px rgba(0,0,0,0.3);
    transition: transform 0.1s;
}

.ui-btn:active {
    transform: scale(0.9);
}

/* ROW 3: Dedicated spacious centered Quests shelf */
.hud-row-quests {
    width: 100%;
    display: flex;
    justify-content: center;
}

/* Screen overlays and Menus */
.screen {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    background: rgba(22, 15, 7, 0.85); /* Dark translucent forest backdrop */
    z-index: 100;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    pointer-events: auto; /* CAPTURE ALL CLICKS/TOUCHES (Prevents canvas bleed-through!) */
}

.menu-content {
    background: #3e2712; /* Warm oak bark brown */
    border: 4px solid #8e6f3e; /* Cozy gold outline */
    border-radius: 24px;
    padding: 36px 24px;
    text-align: center;
    width: 90%;
    max-width: 400px;
    box-shadow: 0 10px 25px rgba(0,0,0,0.6), inset 0 0 15px rgba(0,0,0,0.5);
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 24px;
    animation: popIn 0.35s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes popIn {
    from { transform: scale(0.8); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.title-logo {
    font-size: 32px;
    font-weight: bold;
    color: #f7ede2;
    text-shadow: 0 4px #1a1105, 0 8px rgba(0,0,0,0.4);
    letter-spacing: 1px;
}

.tagline, .pause-tagline {
    font-size: 15px;
    color: #dfb275;
    font-style: italic;
    margin-top: -12px;
    margin-bottom: 8px;
}

/* Woodland-style Cozy Buttons - Upgraded for High Tactility & Big Mobile Hitboxes */
.menu-btn {
    width: 100%;
    max-width: 290px;
    padding: 16px 36px;
    font-size: 22px; /* Chunky and readable */
    font-weight: 800;
    border-radius: 35px; /* Softer pill shape */
    cursor: pointer;
    border: none;
    outline: none;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 6px 0px rgba(0,0,0,0.3), 0 8px 15px rgba(0,0,0,0.4);
    transition: transform 0.1s, box-shadow 0.1s;
    text-transform: uppercase;
    letter-spacing: 1.5px;
}

.menu-btn:active {
    transform: translateY(4px);
    box-shadow: 0 2px 0px rgba(0,0,0,0.3), 0 4px 8px rgba(0,0,0,0.4);
}

.btn-win {
    background: linear-gradient(180deg, #4caf50 0%, #2e7d32 100%); /* Lush forest green */
    border: 2px solid #81c784;
    color: #fff;
}

.btn-lose {
    background: linear-gradient(180deg, #d84a4a 0%, #b71c1c 100%); /* Berry red */
    border: 2px solid #e57373;
    color: #fff;
}

.btn-secondary {
    background: linear-gradient(180deg, #b18c5e 0%, #7d5a36 100%); /* Acorn brown */
    border: 2px solid #dfb275;
    color: #fff;
}

/* Results text and details */
#results-title {
    font-size: 26px;
    text-shadow: 0 2px rgba(0,0,0,0.5);
}

.title-win {
    color: #a5d6a7; /* Soft gold green */
}

.title-lose {
    color: #ef9a9a; /* Soft autumn red */
}

#results-score {
    font-size: 20px;
    color: #f7ede2;
    background: rgba(26, 17, 8, 0.4);
    border: 1.5px solid #8e6f3e;
    border-radius: 12px;
    padding: 10px 24px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    min-width: 150px;
}

#results-score .value {
    font-size: 24px;
    font-weight: bold;
    color: #ffeb3b;
}

/* Profile Section styling */
.profile-section {
    width: 100%;
    background: rgba(26, 17, 8, 0.4);
    border: 1.5px solid #8e6f3e;
    border-radius: 16px;
    padding: 12px;
    display: flex;
    justify-content: center;
    align-items: center;
}

#profile-active-container {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    justify-content: space-between;
}

.profile-label {
    font-size: 11px;
    text-transform: uppercase;
    color: #dfb275;
    font-weight: bold;
}

.menu-select {
    flex-grow: 1;
    background: #50381e;
    color: #fff;
    border: 2px solid #8e6f3e;
    border-radius: 8px;
    padding: 6px;
    font-weight: bold;
    outline: none;
}

.btn-text {
    background: none;
    border: none;
    color: #dfb275;
    text-decoration: underline;
    cursor: pointer;
    font-size: 13px;
    font-weight: bold;
}

.profile-input-group {
    display: flex;
    gap: 8px;
    width: 100%;
}

.menu-input {
    flex-grow: 1;
    width: 100%;
    min-width: 0; /* CRITICAL FLEXBOX BUGFIX: Allows text inputs to shrink below default size inside flex rows, preventing horizontal overflows! */
    background: #f7ede2;
    border: 2px solid #8e6f3e;
    border-radius: 8px;
    padding: 8px 12px;
    font-size: 14px;
    color: #3e2712;
    outline: none;
    font-weight: bold;
}

.btn-small {
    padding: 6px 14px; /* Tighter padding matches the .menu-input height perfectly! */
    font-size: 13px;
    font-weight: 800;
    border-radius: 8px;
    box-shadow: 0 3px 0px rgba(0,0,0,0.3);
    cursor: pointer;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.1s, box-shadow 0.1s;
    border: none;
    outline: none;
}

.btn-small:active {
    transform: translateY(2px);
    box-shadow: 0 1px 0px rgba(0,0,0,0.3);
}

/* Level Selection Box & Grid */
.level-select-box {
    width: 100%;
    background: rgba(26, 17, 8, 0.4);
    border: 1.5px solid #8e6f3e;
    border-radius: 16px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.box-title {
    font-size: 11px;
    text-transform: uppercase;
    color: #dfb275;
    font-weight: bold;
    letter-spacing: 1px;
    align-self: center;
}

.levels-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;
    max-height: 180px; /* Limits size to fit on mobile viewports */
    overflow-y: auto;
    padding-right: 4px;
    touch-action: pan-y !important; /* Forces touchscreen scroll swipe vertical gestures to be unlocked! */
    -webkit-overflow-scrolling: touch; /* Butter-smooth momentum scrolling on iOS Safari */
}

/* Scrollbar customization for cozy feel */
.levels-grid::-webkit-scrollbar {
    width: 6px;
}
.levels-grid::-webkit-scrollbar-track {
    background: rgba(46, 32, 15, 0.3);
    border-radius: 3px;
}
.levels-grid::-webkit-scrollbar-thumb {
    background: #8e6f3e;
    border-radius: 3px;
}

/* Level Circular node styling */
.level-node {
    aspect-ratio: 1;
    background: rgba(46, 32, 15, 0.85);
    border: 2px solid #8e6f3e;
    border-radius: 50%;
    color: #dfb275;
    font-size: 16px;
    font-weight: bold;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    position: relative;
    box-shadow: 0 4px 6px rgba(0,0,0,0.2);
    transition: transform 0.1s, background-color 0.1s;
    user-select: none;
}

.level-node:active {
    transform: scale(0.9);
}

.level-node.locked {
    opacity: 0.4;
    cursor: not-allowed;
    background: #1a1105;
}

.level-node.unlocked {
    color: #fff;
    border-color: #dfb275;
    background: #50381e;
}

.level-node.selected {
    background: #4caf50 !important;
    border-color: #fff !important;
    color: #fff !important;
    box-shadow: 0 0 10px rgba(76, 175, 80, 0.8);
}

.level-stars {
    font-size: 7px;
    color: #ffeb3b;
    position: absolute;
    bottom: 3px;
    letter-spacing: 0.5px;
}

/* Dynamic Goals Area inside HUD - Enlarged and spacious inside Right Column with Container Scaling! */
.hud-goals-container {
    display: flex;
    flex-wrap: wrap; /* Auto wrap goals grid! */
    gap: 1.8cqw; /* Roomy proportional spacing */
    align-items: center;
    justify-content: center;
    width: 100%;
}

.hud-goal-item {
    display: flex;
    align-items: center;
    gap: 0.9cqw;
    font-size: 3.35cqw; /* Chunky proportional text (scales to 15px at max width) */
    font-weight: 900;
    color: #fff;
    padding: 0.55cqh 2.2cqw; /* Cozy proportional padding */
    background: rgba(26, 17, 8, 0.55); /* Deeper contrast dark fill */
    border: 1.5px solid #8e6f3e; /* Standard gold board */
    border-radius: 1.8cqw;
    transition: transform 0.15s, opacity 0.2s, background-color 0.2s;
    min-width: 15.2cqw; /* Grounded uniform card footprint scales beautifully */
    justify-content: center;
    box-shadow: inset 0 0 4px rgba(0,0,0,0.3);
}

.hud-goal-item.completed {
    opacity: 0.65;
    background: rgba(46, 125, 50, 0.22); /* Lush green completing overlay */
    border-color: rgba(76, 175, 80, 0.6);
}

.hud-goal-item.completed::after {
    content: "✓";
    color: #4caf50;
    font-size: 2.9cqw;
    margin-left: 0.44cqw;
}

.menu-content-large {
    max-width: 440px;
    width: 92%;
    gap: 16px;
}

/* Pruning Thicket Suspend Layouts */
.text-glow {
    box-shadow: 0 0 25px rgba(223, 178, 117, 0.35);
}

.animate-pulse {
    animation: pulseGlow 1.8s infinite ease-in-out;
}

@keyframes pulseGlow {
    0%, 100% { transform: scale(1); opacity: 0.92; }
    50% { transform: scale(1.03); opacity: 1; text-shadow: 0 0 15px rgba(223, 178, 117, 0.6); }
}

.version-tag {
    font-size: 11px;
    color: #dfb275;
    opacity: 0.45;
    font-weight: 800;
    align-self: center;
    margin-top: 4px;
    letter-spacing: 0.5px;
    text-shadow: 0 1px #000;
}

/* Beautiful Forest Guide Instructions Modal styles */
.instruction-scroll-container {
    width: 100%;
    max-height: 400px; /* Perfect cozy scrolling limit for mobile viewports */
    overflow-y: auto;
    padding-right: 8px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    text-align: left;
    touch-action: pan-y !important; /* UNLOCKS vertical swiping scroll gestures on screens! */
    -webkit-overflow-scrolling: touch; /* Momentum scroll behavior for smooth touch scrolling */
}

/* Custom Scrollbar for Guide Container */
.instruction-scroll-container::-webkit-scrollbar {
    width: 6px;
}
.instruction-scroll-container::-webkit-scrollbar-track {
    background: rgba(46, 32, 15, 0.3);
    border-radius: 3px;
}
.instruction-scroll-container::-webkit-scrollbar-thumb {
    background: #8e6f3e;
    border-radius: 3px;
}

.guide-section {
    background: rgba(26, 17, 8, 0.45);
    border: 1.5px solid #8e6f3e;
    border-radius: 12px;
    padding: 12px;
    box-shadow: inset 0 0 10px rgba(0,0,0,0.3);
}

.guide-section h3 {
    font-size: 14px;
    text-transform: uppercase;
    color: #dfb275;
    margin-bottom: 8px;
    border-bottom: 1px solid rgba(142, 111, 62, 0.4);
    padding-bottom: 4px;
    letter-spacing: 0.5px;
    font-weight: 800;
}

.guide-section p {
    font-size: 12px;
    line-height: 1.5;
    color: #f7ede2;
}

.guide-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.guide-row {
    display: flex;
    align-items: flex-start;
    gap: 12px;
}

.guide-icon {
    font-size: 20px;
    width: 34px;
    height: 34px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(46, 32, 15, 0.95);
    border: 1.5px solid #8e6f3e;
    border-radius: 8px;
    flex-shrink: 0;
    box-shadow: 0 2px 4px rgba(0,0,0,0.3);
}

.guide-text {
    flex-grow: 1;
}

.guide-text h4 {
    font-size: 13px;
    color: #dfb275;
    font-weight: bold;
    margin-bottom: 2px;
}

.guide-text p {
    font-size: 11.5px;
    color: #f7ede2;
    line-height: 1.4;
    margin: 0;
}

.hidden {
    display: none !important;
}

/* Portrait mode warning */
#orientation-warning {
    display: none;
}

@media (orientation: landscape) and (max-height: 500px) {
    #orientation-warning {
        display: flex;
    }
}
