:root {
    --grass-dark: #6dba4a;
    --grass-light: #8dd766;
    --road-dark: #4a4f55;
    --water-dark: #2cd4cf;
    --sunset-1: #ffb347;
    --sunset-2: #ff7e5f;
    --sunset-3: #6a3d9a;
    --cream: #fff3da;
    --ink: #1d2330;
    --gold: #ffd84d;
    --bronze: #c97c3b;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }
  html, body {
    height: 100%;
    overflow: hidden;
    font-family: 'Fredoka', sans-serif;
    color: var(--ink);
    user-select: none;
    -webkit-user-select: none;
    background: #0d0d12;
  }
  body {
    display: flex;
    align-items: center;
    justify-content: center;
    background:
      radial-gradient(1200px 700px at 50% 0%, #ffb37a22, transparent 60%),
      radial-gradient(900px 600px at 50% 100%, #6a3d9a55, transparent 60%),
      linear-gradient(180deg, #1c1430 0%, #0d0d12 70%, #050507 100%);
  }
  /* subtle grain */
  body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    background-image:
      radial-gradient(rgba(255,255,255,0.04) 1px, transparent 1px);
    background-size: 3px 3px;
    mix-blend-mode: overlay;
    opacity: 0.6;
    z-index: 1000;
  }

  .stage {
    position: relative;
    width: min(100vw, 100vh * 480 / 820);
    max-width: 540px;
    aspect-ratio: 480 / 820;
    display: flex;
    flex-direction: column;
    padding: 12px;
    border-radius: 24px;
    background:
      linear-gradient(180deg, #ffd083 0%, #ff8c5a 55%, #b3568b 100%);
    box-shadow:
      0 30px 80px rgba(0,0,0,0.6),
      inset 0 0 0 4px rgba(255,255,255,0.18),
      inset 0 0 0 8px rgba(0,0,0,0.12);
  }

  /* HUD */
  .hud {
    height: 56px;
    flex: 0 0 auto;
    margin-bottom: 10px;
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    align-items: center;
    gap: 8px;
    padding: 0 6px;
    color: var(--cream);
    font-weight: 700;
  }
  .hud .pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: rgba(0,0,0,0.32);
    border-radius: 999px;
    border: 2px solid rgba(255,255,255,0.18);
    font-family: 'Bungee', sans-serif;
    font-size: 13px;
    letter-spacing: 0.05em;
    text-shadow: 0 2px 0 rgba(0,0,0,0.35);
  }
  .hud .left  { justify-self: start; display: flex; gap: 6px; }
  .hud .right { justify-self: end;   display: flex; gap: 6px; }
  .hud .center { justify-self: center; display: flex; gap: 6px; }
  .hud .lives {
    display: inline-flex;
    gap: 4px;
    padding: 6px 10px;
    background: rgba(0,0,0,0.32);
    border-radius: 999px;
    border: 2px solid rgba(255,255,255,0.18);
  }
  .heart {
    width: 16px; height: 16px;
    background: #ff5070;
    clip-path: path('M8 14 L1.5 7.5 a4 4 0 0 1 6.5 -4.5 a4 4 0 0 1 6.5 4.5 Z');
    box-shadow: inset 0 -3px 0 rgba(0,0,0,0.25);
  }
  .heart.spent { background: #4d2a30; }

  /* Canvas wrapper */
  .canvas-wrap {
    position: relative;
    flex: 1 1 auto;
    border-radius: 16px;
    overflow: hidden;
    background: #6dba4a;
    box-shadow:
      inset 0 0 0 3px rgba(0,0,0,0.25),
      inset 0 0 0 6px rgba(255,255,255,0.18),
      0 12px 30px rgba(0,0,0,0.4);
  }
  canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: -webkit-optimize-contrast;
  }

  /* Overlay screens */
  .overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 18px;
    padding: 20px;
    text-align: center;
    background:
      radial-gradient(circle at 50% 35%, rgba(255,255,255,0.05), transparent 60%),
      linear-gradient(180deg, rgba(10,8,30,0.55), rgba(10,8,30,0.85));
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    z-index: 5;
    transition: opacity 0.35s ease;
  }
  .overlay.hide { opacity: 0; pointer-events: none; }
  .overlay img.logo {
    width: 78%;
    max-width: 360px;
    filter: drop-shadow(0 8px 0 rgba(0,0,0,0.35)) drop-shadow(0 18px 30px rgba(0,0,0,0.5));
    animation: bob 2.4s ease-in-out infinite;
  }
  @keyframes bob {
    0%, 100% { transform: translateY(0) rotate(-1.5deg); }
    50%      { transform: translateY(-8px) rotate(1.5deg); }
  }
  .overlay h1 {
    font-family: 'Bungee', sans-serif;
    font-size: 42px;
    line-height: 1;
    color: var(--cream);
    text-shadow: 0 4px 0 #1d2330, 0 8px 18px rgba(0,0,0,0.5);
    letter-spacing: 0.04em;
  }
  .overlay h2 {
    font-family: 'Bungee', sans-serif;
    color: var(--gold);
    font-size: 22px;
    letter-spacing: 0.06em;
    text-shadow: 0 3px 0 #1d2330;
  }
  .overlay p {
    color: #fff5d8;
    font-weight: 500;
    max-width: 320px;
    line-height: 1.5;
    font-size: 15px;
  }
  .stats {
    display: flex;
    gap: 14px;
    flex-wrap: wrap;
    justify-content: center;
  }
  .stat {
    background: rgba(255,255,255,0.08);
    border: 2px solid rgba(255,255,255,0.18);
    border-radius: 14px;
    padding: 10px 16px;
    color: var(--cream);
    min-width: 92px;
  }
  .stat .label {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.12em;
    opacity: 0.7;
    font-weight: 600;
  }
  .stat .value {
    font-family: 'Bungee', sans-serif;
    font-size: 22px;
    color: var(--gold);
    text-shadow: 0 2px 0 rgba(0,0,0,0.4);
  }

  .btn {
    appearance: none;
    border: none;
    cursor: pointer;
    font-family: 'Bungee', sans-serif;
    font-size: 18px;
    letter-spacing: 0.06em;
    color: #2a1a0a;
    padding: 14px 28px;
    border-radius: 999px;
    background: linear-gradient(180deg, #ffe07a 0%, #ffb347 100%);
    box-shadow:
      0 6px 0 #b06a17,
      0 10px 24px rgba(0,0,0,0.4),
      inset 0 2px 0 rgba(255,255,255,0.65);
    transition: transform 0.06s, box-shadow 0.06s;
  }
  .btn:hover { transform: translateY(-1px); }
  .btn:active {
    transform: translateY(4px);
    box-shadow:
      0 2px 0 #b06a17,
      0 4px 10px rgba(0,0,0,0.4),
      inset 0 2px 0 rgba(255,255,255,0.65);
  }
  .btn.ghost {
    background: rgba(255,255,255,0.08);
    color: var(--cream);
    box-shadow:
      0 4px 0 rgba(0,0,0,0.4),
      inset 0 0 0 2px rgba(255,255,255,0.25);
    font-size: 14px;
    padding: 10px 18px;
  }

  .keyhint {
    display: flex;
    gap: 6px;
    color: #fff5d8;
    font-size: 13px;
    align-items: center;
    opacity: 0.8;
  }
  .key {
    display: inline-block;
    background: rgba(255,255,255,0.14);
    border: 2px solid rgba(255,255,255,0.3);
    border-bottom-width: 4px;
    border-radius: 6px;
    padding: 2px 8px;
    font-family: 'Bungee', sans-serif;
    font-size: 12px;
    color: var(--cream);
  }

  /* Touch controls */
  .controls {
    flex: 0 0 auto;
    margin-top: 10px;
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-rows: 1fr 1fr;
    gap: 8px;
    height: 140px;
    padding: 0 4px;
  }
  .ctrl-btn {
    appearance: none;
    border: none;
    cursor: pointer;
    font-family: 'Bungee', sans-serif;
    font-size: 22px;
    color: #2a1a0a;
    background: linear-gradient(180deg, #fff3da 0%, #ffd083 100%);
    border-radius: 18px;
    box-shadow:
      0 6px 0 #a96b2a,
      inset 0 2px 0 rgba(255,255,255,0.65);
    transition: transform 0.05s, box-shadow 0.05s;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .ctrl-btn:active {
    transform: translateY(4px);
    box-shadow:
      0 2px 0 #a96b2a,
      inset 0 2px 0 rgba(255,255,255,0.65);
  }
  .ctrl-up    { grid-column: 2; grid-row: 1; }
  .ctrl-left  { grid-column: 1; grid-row: 2; }
  .ctrl-down  { grid-column: 2; grid-row: 2; }
  .ctrl-right { grid-column: 3; grid-row: 2; }
  .ctrl-empty { visibility: hidden; }

  /* Floating score */
  .floater {
    position: absolute;
    color: var(--gold);
    font-family: 'Bungee', sans-serif;
    font-size: 22px;
    text-shadow: 0 2px 0 #1d2330, 0 0 12px rgba(255,216,77,0.6);
    pointer-events: none;
    animation: rise 0.9s ease-out forwards;
    z-index: 4;
  }
  @keyframes rise {
    0%   { opacity: 0; transform: translate(-50%, 0) scale(0.6); }
    20%  { opacity: 1; transform: translate(-50%, -10px) scale(1.1); }
    100% { opacity: 0; transform: translate(-50%, -60px) scale(1); }
  }

  .level-flash {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    pointer-events: none;
    z-index: 4;
  }
  .level-flash .text {
    font-family: 'Bungee', sans-serif;
    color: var(--cream);
    font-size: 48px;
    text-shadow:
      0 0 20px rgba(255,216,77,0.8),
      0 4px 0 #1d2330,
      0 8px 24px rgba(0,0,0,0.5);
    letter-spacing: 0.06em;
    animation: levelpop 1.4s ease-out forwards;
  }
  @keyframes levelpop {
    0%   { transform: scale(0.4) rotate(-8deg); opacity: 0; }
    20%  { transform: scale(1.15) rotate(2deg); opacity: 1; }
    70%  { transform: scale(1) rotate(0deg); opacity: 1; }
    100% { transform: scale(0.95) rotate(0deg); opacity: 0; }
  }

  /* Hide controls on landscape desktop where keys are easier — but keep them for mobile */
  @media (hover: hover) and (pointer: fine) {
    .controls { display: none; }
    .stage { aspect-ratio: 480 / 720; }
  }

  .credits {
    position: absolute;
    bottom: 8px;
    left: 0; right: 0;
    text-align: center;
    color: rgba(255,245,216,0.6);
    font-size: 11px;
    letter-spacing: 0.04em;
    z-index: 3;
  }
  .credits a { color: inherit; text-decoration: underline dotted; }

  .sound-toggle {
    position: absolute;
    top: 18px;
    right: 18px;
    z-index: 6;
    width: 38px;
    height: 38px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.3);
    background: rgba(0,0,0,0.32);
    color: var(--cream);
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
  }