:root {
    --bg-0: #f6efe0;
    --bg-1: #e2efd1;
    --bg-2: #c7ddbc;
    --card: rgba(255, 253, 244, 0.88);
    --card-strong: rgba(255, 248, 225, 0.92);
    --ink: #2e2a21;
    --muted: #6b665a;
    --accent: #2a8f3f;
    --accent-deep: #1f6d31;
    --gold: #ce8a1a;
    --line: #c6b796;
    --button-text: #223021;
    --button-a: #d9f0b6;
    --button-b: #9dcc69;
    --field-rgb: 97, 186, 78;
    --field-grown-rgb: 36, 108, 41;
    --machine-rgb: 233, 73, 61;
    --shadow-soft: 0 8px 24px rgba(57, 44, 26, 0.10);
    --shadow-hard: 0 14px 28px rgba(45, 34, 18, 0.16);
}

* { box-sizing: border-box; }

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

body {
    font-family: "Cambria", "Constantia", "Georgia", serif;
    color: var(--ink);
    background:
        radial-gradient(circle at 10% 12%, rgba(255,255,255,0.65), transparent 40%),
        radial-gradient(circle at 88% 20%, rgba(var(--machine-rgb), 0.2), transparent 36%),
        linear-gradient(150deg, var(--bg-0), var(--bg-1) 52%, var(--bg-2));
}

body::before, body::after {
    content: "";
    position: fixed;
    border-radius: 50%;
    pointer-events: none;
    z-index: 0;
}

body::before {
    width: 360px; height: 360px;
    top: -120px; right: -60px;
    background: radial-gradient(circle, rgba(var(--machine-rgb), 0.26), rgba(var(--machine-rgb), 0));
    animation: drift-a 18s ease-in-out infinite;
}

body::after {
    width: 320px; height: 320px;
    left: -90px; bottom: -120px;
    background: radial-gradient(circle, rgba(var(--field-grown-rgb), 0.24), rgba(var(--field-grown-rgb), 0));
    animation: drift-b 22s ease-in-out infinite;
}

/* ── App shell ────────────────────────────────────────────── */
.app-shell {
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100vh;
    padding: 8px;
    display: grid;
    /* play row gets all leftover space; other rows hug their content */
    grid-template-rows: auto 1fr auto auto;
    grid-template-areas:
        "topbar"
        "play"
        "prestige"
        "mulch";
    gap: 8px;
    animation: rise-in 480ms ease-out;
    transition: filter 220ms ease, transform 220ms ease;
}

body.prestiging .app-shell {
    filter: blur(1.8px) saturate(1.08);
    transform: scale(0.994);
}

#prestigeTransition {
    position: fixed;
    inset: 0;
    z-index: 999;
    pointer-events: none;
    opacity: 0;
    background:
        radial-gradient(circle at 50% 50%, rgba(var(--machine-rgb), 0.35), rgba(var(--machine-rgb), 0) 50%),
        linear-gradient(140deg, rgba(var(--field-rgb), 0.25), rgba(var(--field-grown-rgb), 0.35));
    mix-blend-mode: screen;
}

#prestigeTransition.active {
    animation: prestige-flash 2000ms ease-in-out forwards;
}

.card {
    background:
        linear-gradient(180deg, rgba(255,255,255,0.36), rgba(255,255,255,0.05)),
        var(--card);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow-soft);
    backdrop-filter: blur(3px);
}

/* ── Top bar ──────────────────────────────────────────────── */
.top-bar {
    grid-area: topbar;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 14px;
}

h1, h2, p { margin: 0; }

h1 {
    font-family: "Rockwell", "Bookman Old Style", serif;
    font-size: clamp(1.1rem, 2vw, 1.6rem);
    letter-spacing: 0.5px;
    color: #28331f;
    text-shadow: 0 1px 0 rgba(255,255,255,0.5);
}

h2 {
    font-family: "Rockwell", "Bookman Old Style", serif;
    font-size: 0.95rem;
    color: #31362a;
    letter-spacing: 0.2px;
    margin-bottom: 6px;
}

#desc {
    margin-top: 2px;
    color: var(--muted);
    font-size: 0.8rem;
}

.stat-grid {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    justify-content: flex-end;
}

#money {
    font-family: "Rockwell", "Bookman Old Style", serif;
    font-size: clamp(1rem, 1.8vw, 1.4rem);
    font-weight: 700;
    color: var(--accent-deep);
    background: linear-gradient(180deg, rgba(199,237,174,0.7), rgba(176,224,151,0.4));
    border: 1px solid rgba(74,132,61,0.35);
    border-radius: 8px;
    padding: 3px 9px;
}

.stat-mps {
    font-family: "Rockwell", "Bookman Old Style", serif;
    font-size: 0.82rem;
    font-weight: 700;
    color: var(--accent);
    border-radius: 7px;
    padding: 3px 7px;
    background: rgba(var(--field-grown-rgb), 0.1);
    border: 1px solid rgba(var(--field-grown-rgb), 0.2);
}

#totalMowed, #saveStatus {
    border-radius: 7px;
    padding: 3px 7px;
    background: rgba(255,255,255,0.5);
    border: 1px solid rgba(150,139,109,0.22);
    font-size: 0.78rem;
}

#saveStatus { color: var(--muted); }

/* ── Play area ────────────────────────────────────────────── */
.play-area {
    grid-area: play;
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 8px;
    /* Both children align to the top; the controls stretches only as tall as the canvas */
    align-items: start;
    min-height: 0;
}

/* Canvas card: shrinks to exactly its content */
.canvas-card {
    padding: 8px;
    display: inline-flex;
    justify-content: center;
    align-items: center;
    background:
        linear-gradient(145deg, rgba(var(--field-grown-rgb), 0.24), rgba(var(--machine-rgb), 0.2)),
        var(--card-strong);
    border-width: 2px;
    box-shadow: var(--shadow-hard);
    width: fit-content;
    height: fit-content;
}

/* The square lawn canvas — size driven by JS via --lawn-size */
.lawn-stack {
    position: relative;
    width: var(--lawn-size, min(46vh, 500px));
    height: var(--lawn-size, min(46vh, 500px));
    border: 3px solid rgba(var(--field-grown-rgb), 0.6);
    border-radius: 10px;
    background: rgb(var(--field-rgb));
    box-shadow:
        inset 0 0 0 2px rgba(255,255,255,0.18),
        0 6px 18px rgba(var(--field-grown-rgb), 0.3);
    overflow: hidden;
    flex-shrink: 0;
}

#lawn, #lawnOverlay {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    display: block;
}

#lawnOverlay { pointer-events: none; }

/* ── Controls panel: matches canvas height exactly ───────── */
.controls {
    padding: 10px 12px;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.44), rgba(255,255,255,0.18)),
        var(--card);
    min-width: 0;
    /* Exactly as tall as the canvas card next to it */
    height: calc(var(--lawn-size, min(46vh, 500px)) + 20px); /* lawn + canvas-card padding (8*2) + border (2*2) */
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.upgrade-list {
    display: grid;
    gap: 5px;
}

.upgrade-row {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 5px;
}

.upgrade-row button { width: 100%; }

.upgrade-list span {
    color: var(--muted);
    font-size: 0.8rem;
    padding: 0 3px 4px;
    border-bottom: 1px dashed rgba(102,98,89,0.24);
}

/* ── Buttons ──────────────────────────────────────────────── */
button {
    appearance: none;
    border: 1px solid #5f7a45;
    border-radius: 8px;
    padding: 6px 8px;
    font-family: "Trebuchet MS", "Verdana", sans-serif;
    font-size: 0.8rem;
    font-weight: 700;
    color: var(--button-text);
    background:
        linear-gradient(180deg, rgba(255,255,255,0.72), rgba(255,255,255,0.05)),
        linear-gradient(170deg, var(--button-a), color-mix(in srgb, var(--button-a) 55%, var(--button-b)), var(--button-b));
    cursor: pointer;
    box-shadow:
        0 1px 0 rgba(255,255,255,0.42) inset,
        0 3px 8px rgba(45,65,28,0.13);
    transition: transform 100ms ease, filter 100ms ease, box-shadow 100ms ease;
}

button:hover {
    filter: saturate(1.05) brightness(1.03);
    transform: translateY(-1px);
    box-shadow: 0 1px 0 rgba(255,255,255,0.48) inset, 0 6px 10px rgba(40,61,22,0.18);
}

button:active { transform: translateY(1px) scale(0.997); }

button:disabled {
    cursor: not-allowed;
    opacity: 0.52;
    filter: grayscale(0.18);
    transform: none;
    box-shadow: none;
}

.max-upgrade-btn {
    min-width: 60px;
    border-color: #5a6f7a;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.62), rgba(255,255,255,0.06)),
        linear-gradient(165deg, color-mix(in srgb, var(--button-a) 55%, white), color-mix(in srgb, var(--button-b) 35%, #bfd8ef) 72%, color-mix(in srgb, var(--button-b) 55%, #b3d0ec));
    color: #1f3442;
}

/* ── Field navigation ─────────────────────────────────────── */
.field-nav {
    margin-top: auto;
    padding-top: 8px;
    display: grid;
    gap: 5px;
}

#fieldName {
    color: var(--muted);
    font-size: 0.8rem;
    border: 1px dashed rgba(102,98,89,0.28);
    border-radius: 7px;
    padding: 5px 8px;
    background: rgba(255,255,255,0.4);
}

.field-nav-buttons {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 5px;
}

#nextFieldBtn.unlock-mode {
    border-color: color-mix(in srgb, var(--button-b) 60%, #8a5f17);
    background:
        linear-gradient(180deg, rgba(255,255,255,0.62), rgba(255,255,255,0.08)),
        linear-gradient(170deg, color-mix(in srgb, var(--button-a) 58%, #ffe6a9), color-mix(in srgb, var(--button-b) 40%, #ffcb6f) 67%, color-mix(in srgb, var(--button-b) 70%, #f2b24a));
}

/* ── Prestige ─────────────────────────────────────────────── */
#prestige {
    grid-area: prestige;
    padding: 8px 14px;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 10px;
    border-color: rgba(var(--machine-rgb), 0.38);
    background:
        linear-gradient(180deg, rgba(255,255,255,0.32), rgba(255,255,255,0.04)),
        color-mix(in srgb, var(--card) 72%, rgba(var(--machine-rgb), 0.22));
}

#prestige h2 { margin: 0; white-space: nowrap; }

.prestige-stats {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    font-size: 0.82rem;
    color: var(--muted);
    flex: 1;
}

#prestigeButton {
    white-space: nowrap;
    margin-left: auto;
    border-color: #99500c;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.62), rgba(255,255,255,0.07)),
        linear-gradient(165deg, #ffd8ae, #ffb765 64%, #f79a3d);
}

/* ── Mulch Shop ───────────────────────────────────────────── */
#mulchShop {
    grid-area: mulch;
    padding: 8px 14px;
    display: grid;
    grid-template-columns: auto auto 1fr;
    align-items: center;
    gap: 8px 14px;
    border-color: rgba(132,97,36,0.32);
    background:
        linear-gradient(180deg, rgba(255,255,255,0.32), rgba(255,255,255,0.08)),
        linear-gradient(155deg, color-mix(in srgb, var(--card) 82%, #ffe6bc), color-mix(in srgb, var(--card) 86%, #ffd8a4));
}

#mulchShop h2 { margin: 0; }
#mulchShopHint { color: var(--muted); font-size: 0.8rem; display: none; }
#mulchShopMeta { color: #5f4c35; font-size: 0.82rem; font-weight: 700; }

.mulch-shop-grid {
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    gap: 8px;
}

.cosmetic-card {
    border: 1px solid rgba(132,97,36,0.28);
    border-radius: 9px;
    padding: 8px 10px;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.5), rgba(255,255,255,0.14)),
        rgba(255,244,225,0.72);
    display: grid;
    gap: 4px;
    box-shadow: 0 4px 12px rgba(71,51,22,0.1);
    min-width: 0;
}

.cosmetic-card.affordable { border-color: rgba(88,127,51,0.42); }
.cosmetic-card.locked { filter: saturate(0.9); }
.cosmetic-card.owned {
    border-color: rgba(67,128,54,0.55);
    background:
        linear-gradient(180deg, rgba(255,255,255,0.52), rgba(255,255,255,0.16)),
        rgba(225,248,211,0.76);
}

.cosmetic-top {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 3px;
}

.cosmetic-type, .cosmetic-price {
    font-size: 0.62rem;
    font-weight: 700;
    border-radius: 999px;
    padding: 1px 5px;
    letter-spacing: 0.2px;
}

.cosmetic-type { color: #4f3d26; background: rgba(169,138,93,0.2); }
.cosmetic-price { color: #513a1f; background: rgba(255,219,160,0.55); }

.cosmetic-name {
    font-family: "Rockwell", "Bookman Old Style", serif;
    font-size: 0.8rem;
    color: #4a3721;
    overflow-wrap: anywhere;
}

.cosmetic-desc {
    color: #6e5d49;
    font-size: 0.7rem;
    overflow-wrap: anywhere;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    line-clamp: 2;
    overflow: hidden;
}

.cosmetic-buy-btn {
    width: 100%;
    font-size: 0.68rem;
    padding: 5px 6px;
    line-height: 1.2;
    white-space: normal;
    overflow-wrap: anywhere;
    border-color: #8f6a2f;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.62), rgba(255,255,255,0.08)),
        linear-gradient(165deg, #ffe7b9, #ffd08c 68%, #ecb566);
    color: #3c2e1b;
}

.cosmetic-buy-btn.unlocked {
    border-color: #4d7d3f;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.62), rgba(255,255,255,0.08)),
        linear-gradient(165deg, #d9f4bf, #b5e290 68%, #8ecf68);
}

/* ── Animations ───────────────────────────────────────────── */
@keyframes rise-in {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

@keyframes drift-a {
    0%, 100% { transform: translate(0,0) scale(1); }
    50%       { transform: translate(-20px,18px) scale(1.04); }
}

@keyframes drift-b {
    0%, 100% { transform: translate(0,0) scale(1); }
    50%       { transform: translate(18px,-14px) scale(0.97); }
}

@keyframes prestige-flash {
    0%   { opacity: 0; transform: scale(1.02); filter: blur(0px); }
    22%  { opacity: 0.55; transform: scale(1); filter: blur(1px); }
    48%  { opacity: 0.92; transform: scale(1.01); filter: blur(2px); }
    72%  { opacity: 0.45; transform: scale(1.005); filter: blur(1px); }
    100% { opacity: 0; transform: scale(1); filter: blur(0px); }
}

/* ── Reduced motion ───────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    body::before, body::after, .app-shell { animation: none; }
}

/* ── Responsive ───────────────────────────────────────────── */
@media (max-width: 900px) {
    html, body { overflow: auto; }

    .app-shell {
        height: auto;
        min-height: 100vh;
        grid-template-rows: auto auto auto auto;
    }

    .play-area { grid-template-columns: 1fr; }

    .canvas-card { width: 100%; }

    .lawn-stack {
        width: min(420px, 88vw) !important;
        height: min(420px, 88vw) !important;
        margin: 0 auto;
    }

    .controls { height: auto !important; }

    #prestige { flex-wrap: wrap; }

    .mulch-shop-grid { grid-template-columns: repeat(3, 1fr); }
}

@media (max-width: 640px) {
    body::before, body::after { animation: none; }

    .top-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 6px;
    }

    .stat-grid { justify-content: flex-start; }

    .mulch-shop-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ── Wipe Data button (fixed bottom-right) ────────────────── */
#wipeBtn {
    position: fixed;
    bottom: 14px;
    right: 14px;
    z-index: 100;
    font-size: 0.75rem;
    padding: 6px 11px;
    border-color: #a04040;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.55), rgba(255,255,255,0.06)),
        linear-gradient(165deg, #ffd6d6, #f0a0a0 64%, #d97070);
    color: #5a1a1a;
    opacity: 0.6;
    transition: opacity 150ms ease, transform 100ms ease, box-shadow 100ms ease;
}

#wipeBtn:hover {
    opacity: 1;
    transform: translateY(-1px);
}

/* ── Wipe confirmation overlay ────────────────────────────── */
#wipeOverlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 500;
    background: rgba(20, 15, 8, 0.55);
    backdrop-filter: blur(3px);
    align-items: center;
    justify-content: center;
}

#wipeOverlay.active { display: flex; }

#wipeDialog {
    background:
        linear-gradient(180deg, rgba(255,255,255,0.44), rgba(255,255,255,0.12)),
        rgba(255, 248, 235, 0.97);
    border: 1px solid rgba(180, 80, 80, 0.35);
    border-radius: 16px;
    box-shadow: 0 20px 48px rgba(40, 20, 10, 0.28);
    padding: 28px 32px;
    max-width: 360px;
    width: calc(100vw - 40px);
    display: grid;
    gap: 14px;
    animation: rise-in 200ms ease-out;
}

#wipeDialog h2 { font-size: 1.15rem; color: #7a1f1f; margin: 0; }

#wipeDialog p {
    color: var(--muted);
    font-size: 0.9rem;
    line-height: 1.5;
    margin: 0;
}

.wipe-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-top: 4px;
}

#wipeCancelBtn { border-color: #5f7a45; }

#wipeConfirmBtn {
    border-color: #a04040;
    background:
        linear-gradient(180deg, rgba(255,255,255,0.55), rgba(255,255,255,0.06)),
        linear-gradient(165deg, #ffd6d6, #f0a0a0 64%, #d97070);
    color: #5a1a1a;
}