  * { margin: 0; padding: 0; box-sizing: border-box; }
  :root {
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
  }
  html, body {
    width: 100%; height: 100%;
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: touch;
    position: fixed;
  }
  body {
    background: linear-gradient(180deg, #87ceeb 0%, #b6e2a1 60%, #8bc34a 100%);
    font-family: 'Comic Sans MS', 'Trebuchet MS', sans-serif;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    touch-action: none;
  }
  canvas { display: block; touch-action: none; }

  /* On-screen touch buttons (visible on touch devices) */
  .touch-controls { display: none; }
  body.touch-mode .touch-controls { display: block; }
  .touch-btn {
    position: fixed;
    width: 78px; height: 78px;
    border-radius: 50%;
    background: rgba(0,0,0,0.45);
    border: 3px solid rgba(255,255,255,0.7);
    color: white;
    font-size: 30px;
    display: flex; align-items: center; justify-content: center;
    z-index: 8;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    -webkit-user-select: none; user-select: none;
    touch-action: manipulation;
    transition: transform 0.05s ease, background 0.1s;
  }
  .touch-btn:active, .touch-btn.pressed {
    transform: scale(0.92);
    background: rgba(255, 222, 100, 0.45);
  }
  #btnUp    { left: calc(20px + var(--safe-left)); bottom: calc(120px + var(--safe-bottom)); }
  #btnDown  { left: calc(20px + var(--safe-left)); bottom: calc(20px  + var(--safe-bottom)); }
  #btnFire  { right: calc(20px + var(--safe-right));   bottom: calc(20px + var(--safe-bottom)); width: 90px; height: 90px; font-size: 34px; }
  #btnSkate { right: calc(115px + var(--safe-right));  bottom: calc(20px + var(--safe-bottom)); background: rgba(120, 60, 180, 0.6); }
  #btnSlime { right: calc(20px + var(--safe-right));   bottom: calc(125px + var(--safe-bottom)); background: rgba(80, 140, 60, 0.7); }
  .touch-btn.disabled { opacity: 0.4; }
  #btnFire .badge {
    position: absolute; top: -4px; right: -4px;
    background: #ffd60a; color: #2d4a1e;
    border-radius: 12px; padding: 2px 6px;
    font-size: 12px; font-weight: bold;
    border: 2px solid white;
    min-width: 22px; text-align: center;
  }

  body.no-slime #btnSlime { display: none; }

  /* Portrait warning */
  #portraitHint {
    position: fixed; inset: 0;
    background: rgba(15, 32, 14, 0.95);
    color: white;
    display: none;
    align-items: center; justify-content: center;
    flex-direction: column;
    z-index: 50;
    text-align: center;
    padding: 20px;
  }
  body.touch-mode.portrait #portraitHint { display: flex; }
  #portraitHint h2 { font-size: 28px; margin-bottom: 12px; color: #ffd60a; }
  #portraitHint p { font-size: 16px; max-width: 320px; line-height: 1.5; }
  .rotate-icon { font-size: 64px; animation: rotate 2.5s ease-in-out infinite; margin: 16px 0; }
  @keyframes rotate {
    0%, 50% { transform: rotate(0); }
    60%, 100% { transform: rotate(-90deg); }
  }

  #ui {
    color: white;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
    font-size: 16px;
    pointer-events: none;
    z-index: 5;
  }
  #ui div { margin-bottom: 4px; }
  #time-display {
    position: fixed;
    top: calc(12px + var(--safe-top));
    left: calc(12px + var(--safe-left));
    color: white;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
    font-size: 18px;
    z-index: 5;
    pointer-events: none;
  }
  #hud-p1 {
    position: fixed;
    top: calc(50px + var(--safe-top));
    left: calc(12px + var(--safe-left));
  }
  #hud-p2 {
    position: fixed;
    top: calc(150px + var(--safe-top));
    left: calc(12px + var(--safe-left));
  }
  body.split-mode #hud-p2 {
    top: calc(50% + 12px);
  }

  #controls {
    position: fixed;
    bottom: 12px;
    left: 12px;
    color: white;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.8);
    font-size: 14px;
    pointer-events: none;
    z-index: 5;
  }

  #musicBtn {
    position: fixed;
    bottom: calc(12px + var(--safe-bottom));
    right: calc(12px + var(--safe-right));
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.8);
    background: rgba(0,0,0,0.45);
    color: white;
    font-size: 20px;
    cursor: pointer;
    z-index: 6;
    display: flex; align-items: center; justify-content: center;
  }
  #musicBtn:hover { background: rgba(0,0,0,0.65); }
  #homeBtn {
    position: fixed;
    bottom: calc(64px + var(--safe-bottom));
    right: calc(12px + var(--safe-right));
    width: 44px; height: 44px;
    border-radius: 50%;
    border: 2px solid rgba(255,255,255,0.8);
    background: rgba(0,0,0,0.45);
    color: white;
    font-size: 20px;
    cursor: pointer;
    z-index: 6;
    display: flex; align-items: center; justify-content: center;
  }
  #homeBtn:hover { background: rgba(0,0,0,0.65); }
  body.touch-mode #homeBtn { width: 38px; height: 38px; font-size: 16px; bottom: calc(265px + var(--safe-bottom)); }

  #minimap {
    position: fixed;
    top: calc(12px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    width: 320px;
    height: 60px;
    background: rgba(0,0,0,0.4);
    border: 2px solid rgba(255,255,255,0.6);
    border-radius: 10px;
    z-index: 5;
  }

  .overlay {
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(15, 32, 14, 0.85);
    display: flex; align-items: center; justify-content: flex-start;
    flex-direction: column;
    color: white;
    z-index: 10;
    text-align: center;
    padding: calc(20px + var(--safe-top)) calc(20px + var(--safe-right))
             calc(20px + var(--safe-bottom)) calc(20px + var(--safe-left));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .overlay > * { flex-shrink: 0; }
  .overlay h1 {
    font-size: clamp(28px, 6.5vmin, 64px);
    margin-bottom: 10px;
    text-shadow: 4px 4px 0 #2d4a1e, 8px 8px 20px rgba(0,0,0,0.6);
    color: #ffd60a;
    line-height: 1.1;
  }
  /* Hide gameplay touch buttons while a menu overlay is on screen */
  body.menu-open .touch-controls { display: none !important; }
  .overlay p {
    font-size: clamp(13px, 2.2vmin, 18px);
    max-width: 720px;
    line-height: 1.4;
    margin-bottom: 10px;
  }
  .overlay button {
    padding: 12px 28px;
    font-size: clamp(15px, 2.6vmin, 22px);
    cursor: pointer;
    border: none;
    border-radius: 12px;
    background: linear-gradient(180deg, #ff8fab, #c9184a);
    color: white;
    font-weight: bold;
    box-shadow: 0 6px 0 #6b0f25, 0 8px 20px rgba(0,0,0,0.5);
    font-family: inherit;
    transition: transform 0.1s;
  }
  .overlay button:hover { transform: translateY(-2px); }
  .overlay button:active { transform: translateY(4px); box-shadow: 0 2px 0 #6b0f25; }

  .legend {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px 24px;
    background: rgba(0,0,0,0.3);
    padding: 16px 24px;
    border-radius: 12px;
    margin-bottom: 20px;
    font-size: 16px;
    text-align: left;
  }

  /* Level select grid */
  .level-pager {
    display: flex;
    align-items: stretch;
    gap: 10px;
    width: 100%;
    max-width: 900px;
    margin: 6px auto 8px;
  }
  .level-arrow {
    flex: 0 0 auto;
    width: 48px;
    border: 2px solid rgba(255,255,255,0.4) !important;
    background: rgba(0,0,0,0.35) !important;
    color: white !important;
    font-size: 24px !important;
    border-radius: 12px !important;
    padding: 0 !important;
    box-shadow: none !important;
    cursor: pointer;
  }
  .level-arrow:disabled {
    opacity: 0.3;
    cursor: not-allowed;
  }
  .level-dots {
    display: flex;
    gap: 6px;
    margin: 0 0 10px;
  }
  .level-dots .dot {
    width: 8px; height: 8px;
    border-radius: 50%;
    background: rgba(255,255,255,0.35);
  }
  .level-dots .dot.active { background: #ffd60a; }
  .level-grid {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 12px;
    flex: 1 1 auto;
    margin: 0;
    padding: 0;
  }
  .level-card {
    background: linear-gradient(160deg, #2d4a1e 0%, #1a2e1a 100%);
    border: 3px solid #4f8a4a;
    border-radius: 14px;
    padding: 10px 8px;
    text-align: center;
    cursor: pointer;
    transition: transform 0.1s, box-shadow 0.1s;
    color: white;
    position: relative;
    min-height: 120px;
    display: flex; flex-direction: column; justify-content: space-between;
  }
  .level-card .lvl-icon { font-size: clamp(24px, 4.5vmin, 38px) !important; }
  .level-card .lvl-name { font-size: clamp(13px, 2.1vmin, 16px) !important; }
  .level-card .lvl-ability { font-size: clamp(10px, 1.6vmin, 12px) !important; }
  .level-card:not(.locked):hover, .level-card:not(.locked):active {
    transform: translateY(-3px);
    box-shadow: 0 6px 16px rgba(0,0,0,0.5);
    border-color: #ffd60a;
  }
  .level-card.locked {
    cursor: not-allowed;
    opacity: 0.55;
    border-color: #555;
    background: linear-gradient(160deg, #2a2a2a 0%, #1a1a1a 100%);
  }
  .level-card .lvl-num {
    font-size: 12px;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #ffd60a;
    opacity: 0.85;
  }
  .level-card .lvl-icon { font-size: 38px; margin: 4px 0; }
  .level-card .lvl-name { font-size: 16px; font-weight: bold; }
  .level-card .lvl-ability {
    font-size: 12px;
    opacity: 0.85;
    line-height: 1.3;
    margin-top: 4px;
  }
  .level-card.completed::after {
    content: '✓'; position: absolute;
    top: 6px; right: 8px;
    color: #80ed99;
    font-size: 22px;
    font-weight: bold;
  }
  .level-card.locked::after {
    content: '🔒'; position: absolute;
    top: 6px; right: 8px;
    font-size: 18px;
  }

  .hidden { display: none !important; }

  /* Homepage mode cards */
  .mode-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(140px, 200px));
    gap: 14px;
    margin: 10px auto 12px;
    justify-content: center;
  }
  .mode-card {
    padding: 14px 12px !important;
    border-radius: 18px !important;
    background: linear-gradient(160deg, #2d4a1e 0%, #1a2e1a 100%) !important;
    border: 3px solid #4f8a4a !important;
    color: white !important;
    box-shadow: 0 6px 14px rgba(0,0,0,0.5) !important;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.1s, box-shadow 0.1s;
  }
  .mode-card:hover { border-color: #ffd60a !important; transform: translateY(-3px); }
  .mode-icon { font-size: clamp(32px, 7vmin, 56px); margin-bottom: 4px; line-height: 1; }
  .mode-name { font-size: clamp(16px, 2.6vmin, 20px); font-weight: bold; margin-bottom: 2px; }
  .mode-desc { font-size: clamp(11px, 1.7vmin, 13px); opacity: 0.85; line-height: 1.25; }
  .profile-btn {
    padding: 10px 22px !important;
    font-size: 16px !important;
    background: rgba(255,255,255,0.12) !important;
    border: 2px solid rgba(255,255,255,0.4) !important;
    color: white !important;
    box-shadow: none !important;
  }

  /* Profile form */
  .profile-form {
    display: flex; flex-direction: column;
    gap: 6px;
    background: rgba(0,0,0,0.3);
    padding: 18px 20px;
    border-radius: 14px;
    width: min(360px, 90%);
    text-align: left;
  }
  .profile-form label {
    font-size: 13px;
    color: #ffd60a;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-top: 6px;
  }
  .profile-form input, .profile-form select {
    padding: 10px 12px;
    border-radius: 8px;
    border: 2px solid rgba(255,255,255,0.3);
    background: rgba(0,0,0,0.4);
    color: white;
    font-family: inherit;
    font-size: 16px;
  }
  .profile-form .reset-btn {
    margin-top: 14px;
    padding: 10px 14px !important;
    background: rgba(200,30,30,0.4) !important;
    border: 2px solid rgba(255,80,80,0.6) !important;
    color: white !important;
    font-size: 14px !important;
    box-shadow: none !important;
  }
  /* Language selector — flag button, top-right. Hidden while a race is running. */
  #langSelect {
    position: fixed;
    top: calc(10px + var(--safe-top));
    right: calc(12px + var(--safe-right));
    z-index: 50;
  }
  body.in-game #langSelect { display: none; }
  #langBtn {
    width: 46px;
    height: 36px;
    border-radius: 10px;
    border: 2px solid rgba(255,255,255,0.5);
    background: rgba(0,0,0,0.45);
    font-size: 20px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
  }
  #langBtn:hover { background: rgba(0,0,0,0.65); }
  #langMenu {
    list-style: none;
    margin: 6px 0 0;
    padding: 6px;
    position: absolute;
    right: 0;
    min-width: 160px;
    background: rgba(13,19,32,0.97);
    border: 1px solid rgba(255,255,255,0.18);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0,0,0,0.45);
  }
  #langMenu li {
    padding: 9px 12px;
    border-radius: 7px;
    cursor: pointer;
    color: #eafff2;
    white-space: nowrap;
    font-size: 15px;
  }
  #langMenu li:hover, #langMenu li.sel { background: rgba(255,255,255,0.12); }

  /* Compact HUD on small screens / mobile */
  body.touch-mode #ui { font-size: 13px; }
  body.touch-mode #hud-p1 { top: 50px; }
  body.touch-mode #time-display { font-size: 15px; top: calc(8px + var(--safe-top)); left: calc(10px + var(--safe-left)); }
  body.touch-mode #musicBtn { width: 38px; height: 38px; font-size: 16px; bottom: calc(220px + var(--safe-bottom)); right: calc(20px + var(--safe-right)); }
  body.touch-mode #minimap {
    top: calc(8px + var(--safe-top));
    right: calc(10px + var(--safe-right));
    width: 220px; height: 44px;
  }
  body.touch-mode #controls { display: none; }
  /* Smaller level grid on small phones */
  @media (max-width: 520px) {
    .level-grid { grid-template-columns: repeat(2, 1fr); }
    .overlay h1 { font-size: 36px; }
    .overlay p { font-size: 14px; }
    .legend { font-size: 13px; padding: 10px 14px; }
  }

  #placement {
    font-size: clamp(16px, 3.2vmin, 28px);
    margin: 12px 0;
    line-height: 1.5;
  }
  #placement .you { color: #ffd60a; font-weight: bold; }
  #result {
    font-size: clamp(28px, 6vmin, 48px);
    margin-bottom: 10px;
    line-height: 1.15;
    max-width: 100%;
    word-wrap: break-word;
  }
  .win { color: #80ed99; }
  .lose { color: #ff6b6b; }
