:root {
    --gold: #ffd66b;
    --gold-deep: #b07a18;
    --gold-glow: #ffe9a3;
    --ink: #1a1410;
    --ink-soft: #2a2018;
    --parchment: #efe2c2;
    --leaf: #5fa45a;
    --blood: #c8332a;
  }
  * { box-sizing: border-box; margin: 0; padding: 0; }
  html, body {
    width: 100%; height: 100%;
    background: #0a0807;
    color: var(--parchment);
    font-family: 'VT323', monospace;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
    -webkit-tap-highlight-color: transparent;
    overscroll-behavior: none;
    touch-action: none;
  }
  #game-wrap {
    position: fixed;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    background:
      radial-gradient(ellipse at center, #1a1410 0%, #050403 100%);
  }
  #stage {
    position: relative;
    width: 100%;
    height: 100%;
    overflow: hidden;
  }
  canvas {
    display: block;
    width: 100%;
    height: 100%;
    image-rendering: pixelated;
    image-rendering: -moz-crisp-edges;
    image-rendering: crisp-edges;
  }

  /* ---------- Loading overlay ---------- */
  #loading {
    position: absolute; inset: 0;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    background: var(--ink);
    color: var(--gold);
    z-index: 50;
    transition: opacity .5s ease;
  }
  #loading h2 {
    font-family: 'MedievalSharp', serif;
    font-size: clamp(28px, 6vw, 56px);
    letter-spacing: 2px;
    text-shadow: 0 3px 0 var(--ink-soft), 0 0 20px rgba(255,214,107,.4);
    margin-bottom: 20px;
  }
  #loading-bar {
    width: min(60%, 320px);
    height: 18px;
    border: 3px solid var(--gold-deep);
    background: var(--ink-soft);
    box-shadow: 0 0 0 2px var(--ink), 0 0 20px rgba(255,214,107,.3);
    overflow: hidden;
  }
  #loading-fill {
    height: 100%; width: 0%;
    background: linear-gradient(90deg, var(--gold-deep), var(--gold), var(--gold-glow));
    transition: width .15s ease;
  }
  #loading-text {
    margin-top: 12px;
    font-size: 22px;
    letter-spacing: 2px;
    opacity: .7;
  }
  .hidden { opacity: 0; pointer-events: none; }

  /* ---------- HTML overlays drawn on top of canvas ---------- */
  #hud {
    position: absolute; inset: 0;
    pointer-events: none;
    z-index: 10;
  }

  /* Top-left HP + score panel */
  #stats {
    position: absolute;
    top: 14px; left: 14px;
    display: flex; align-items: center; gap: 12px;
    pointer-events: none;
  }
  .panel {
    background: rgba(26, 20, 16, 0.82);
    border: 2px solid var(--gold-deep);
    box-shadow:
      inset 0 0 0 1px var(--ink),
      0 0 0 2px var(--ink),
      0 4px 0 rgba(0,0,0,.4);
    padding: 8px 14px;
    color: var(--gold);
    font-family: 'VT323', monospace;
    letter-spacing: 1px;
  }
  #hearts { display: flex; gap: 4px; }
  .heart {
    width: 22px; height: 22px;
    transition: transform .1s ease;
  }
  .heart.lost { opacity: .25; filter: grayscale(1); }
  .heart.beat { animation: beat .4s ease; }
  @keyframes beat {
    0%,100% { transform: scale(1); }
    40% { transform: scale(1.4); filter: drop-shadow(0 0 6px #ff6b6b); }
  }

  #score {
    font-size: 24px;
    display: flex; align-items: center; gap: 8px;
  }
  #score-coin { width: 22px; height: 22px; vertical-align: middle; image-rendering: pixelated; }

  /* Top-right pause */
  #pause-btn {
    position: absolute;
    top: 14px; right: 14px;
    width: 48px; height: 48px;
    background: rgba(26, 20, 16, 0.82);
    border: 2px solid var(--gold-deep);
    color: var(--gold);
    font-size: 28px;
    cursor: pointer;
    pointer-events: auto;
    display: flex; align-items: center; justify-content: center;
    box-shadow: inset 0 0 0 1px var(--ink), 0 0 0 2px var(--ink), 0 4px 0 rgba(0,0,0,.4);
    transition: transform .1s ease;
  }
  #pause-btn:hover { background: rgba(40, 30, 22, .9); }
  #pause-btn:active { transform: translateY(2px); }

  /* Touch controls */
  #touch {
    position: absolute;
    bottom: 0; left: 0; right: 0;
    height: 180px;
    pointer-events: none;
    display: none;
    z-index: 11;
  }
  #touch.show { display: block; }
  .touch-btn {
    position: absolute;
    pointer-events: auto;
    border-radius: 50%;
    border: 3px solid var(--gold-deep);
    background: rgba(26, 20, 16, 0.55);
    color: var(--gold);
    display: flex; align-items: center; justify-content: center;
    font-family: 'MedievalSharp', serif;
    user-select: none;
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    box-shadow: 0 0 0 2px rgba(0,0,0,.6), 0 6px 0 rgba(0,0,0,.4), inset 0 2px 0 rgba(255,255,255,.08);
    transition: transform .08s ease, background .1s;
  }
  .touch-btn.pressed {
    background: rgba(255, 214, 107, 0.35);
    transform: translateY(3px);
    box-shadow: 0 0 0 2px rgba(0,0,0,.6), 0 3px 0 rgba(0,0,0,.4), inset 0 2px 0 rgba(255,255,255,.15);
  }
  #btn-left, #btn-right {
    width: 78px; height: 78px;
    bottom: 30px;
    font-size: 36px;
  }
  #btn-left  { left: 24px; }
  #btn-right { left: 122px; }

  #btn-jump, #btn-attack {
    width: 86px; height: 86px;
    bottom: 30px;
    font-size: 18px;
    letter-spacing: 1px;
  }
  #btn-attack { right: 24px; }
  #btn-jump   { right: 130px; bottom: 70px; }

  /* ---------- Title / Pause / End screens ---------- */
  .screen {
    position: absolute; inset: 0;
    display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    background:
      radial-gradient(ellipse at center, rgba(20,15,10,.55), rgba(10,8,6,.92));
    z-index: 20;
    text-align: center;
    padding: 24px;
    pointer-events: auto;
    animation: fadeIn .4s ease;
  }
  @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
  .screen.hidden { display: none; }

  .title-art {
    font-family: 'MedievalSharp', serif;
    font-size: clamp(40px, 9vw, 92px);
    line-height: 1;
    color: var(--gold);
    text-shadow:
      0 4px 0 #5b3c0c,
      0 6px 0 var(--ink),
      0 8px 16px rgba(0,0,0,.6),
      0 0 30px rgba(255,214,107,.35);
    letter-spacing: 3px;
    margin-bottom: 4px;
    transform: rotate(-1.5deg);
  }
  .title-sub {
    font-family: 'MedievalSharp', serif;
    font-size: clamp(14px, 2.4vw, 22px);
    color: var(--parchment);
    opacity: .8;
    letter-spacing: 6px;
    margin-bottom: 36px;
  }
  .title-sub::before, .title-sub::after { content: " ✦ "; opacity: .6; }

  .menu-btn {
    font-family: 'MedievalSharp', serif;
    font-size: clamp(20px, 3.5vw, 30px);
    letter-spacing: 3px;
    background: linear-gradient(180deg, var(--gold) 0%, #c98e1f 100%);
    color: var(--ink);
    border: 3px solid #5b3c0c;
    padding: 12px 38px;
    margin: 8px;
    cursor: pointer;
    box-shadow:
      inset 0 2px 0 rgba(255,255,255,.4),
      inset 0 -3px 0 rgba(0,0,0,.25),
      0 6px 0 #3d2807,
      0 8px 16px rgba(0,0,0,.5);
    transition: transform .08s ease, box-shadow .08s ease;
    min-width: 220px;
    text-shadow: 0 1px 0 rgba(255,255,255,.4);
  }
  .menu-btn:hover {
    background: linear-gradient(180deg, var(--gold-glow), var(--gold));
  }
  .menu-btn:active {
    transform: translateY(4px);
    box-shadow:
      inset 0 2px 0 rgba(255,255,255,.4),
      inset 0 -3px 0 rgba(0,0,0,.25),
      0 2px 0 #3d2807,
      0 4px 8px rgba(0,0,0,.5);
  }
  .menu-btn.secondary {
    background: linear-gradient(180deg, #6b554a, #3a2a22);
    color: var(--parchment);
    border-color: var(--ink);
    box-shadow:
      inset 0 2px 0 rgba(255,255,255,.15),
      inset 0 -3px 0 rgba(0,0,0,.4),
      0 6px 0 #1a120c,
      0 8px 16px rgba(0,0,0,.5);
  }

  .controls-card {
    margin-top: 28px;
    background: rgba(0,0,0,.45);
    border: 2px solid var(--gold-deep);
    padding: 16px 22px;
    color: var(--parchment);
    font-size: 20px;
    letter-spacing: 1px;
    max-width: 92vw;
  }
  .controls-card h4 {
    font-family: 'MedievalSharp', serif;
    color: var(--gold);
    font-size: 22px;
    margin-bottom: 10px;
    letter-spacing: 2px;
  }
  .controls-card .row { display: flex; gap: 20px; flex-wrap: wrap; justify-content: center; }
  .key {
    display: inline-block;
    padding: 2px 10px;
    background: var(--ink-soft);
    border: 2px solid var(--gold-deep);
    border-bottom-width: 3px;
    color: var(--gold);
    font-size: 16px;
    margin: 0 2px;
    border-radius: 4px;
  }

  .end-portrait {
    width: clamp(140px, 28vw, 220px);
    height: auto;
    image-rendering: pixelated;
    margin-bottom: 18px;
    filter: drop-shadow(0 8px 16px rgba(0,0,0,.7));
    animation: bob 2.4s ease-in-out infinite;
  }
  @keyframes bob { 0%,100%{transform:translateY(0)} 50%{transform:translateY(-8px)} }

  .end-title {
    font-family: 'MedievalSharp', serif;
    font-size: clamp(40px, 8vw, 72px);
    color: var(--gold);
    text-shadow: 0 4px 0 #5b3c0c, 0 6px 0 var(--ink), 0 0 30px rgba(255,214,107,.35);
    letter-spacing: 4px;
    margin-bottom: 8px;
  }
  .end-title.defeat { color: #e25c4f; text-shadow: 0 4px 0 #5a160f, 0 6px 0 var(--ink), 0 0 30px rgba(226,92,79,.35); }

  .end-stats {
    display: flex; gap: 32px;
    margin: 18px 0 28px;
    font-size: 26px;
    color: var(--parchment);
  }
  .end-stats .stat {
    display: flex; flex-direction: column; align-items: center;
    background: rgba(0,0,0,.4);
    padding: 10px 22px;
    border: 2px solid var(--gold-deep);
  }
  .end-stats .stat span:first-child {
    font-family: 'MedievalSharp', serif;
    font-size: 16px; color: var(--gold);
    letter-spacing: 2px;
  }
  .end-stats .stat span:last-child {
    font-size: 32px; color: var(--gold-glow);
  }

  /* Subtle vignette on screens */
  .screen::after {
    content: "";
    position: absolute; inset: 0;
    pointer-events: none;
    background: radial-gradient(ellipse at center, transparent 50%, rgba(0,0,0,.55) 100%);
  }

  /* small screens: tighten title */
  @media (max-width: 560px) {
    #stats { top: 8px; left: 8px; gap: 6px; }
    .panel { padding: 6px 10px; }
    #score { font-size: 20px; }
    .heart { width: 18px; height: 18px; }
    #pause-btn { width: 40px; height: 40px; font-size: 22px; top: 8px; right: 8px; }
    .controls-card { font-size: 16px; padding: 10px 14px; }
    .controls-card h4 { font-size: 18px; }
    .end-stats { gap: 14px; }
    .end-stats .stat { padding: 8px 14px; }
  }

  /* Hint that fades after a few seconds */
  #hint {
    position: absolute;
    top: 80px; left: 50%;
    transform: translateX(-50%);
    font-family: 'MedievalSharp', serif;
    font-size: clamp(16px, 2.4vw, 22px);
    color: var(--parchment);
    background: rgba(0,0,0,.65);
    padding: 8px 18px;
    border: 2px solid var(--gold-deep);
    pointer-events: none;
    letter-spacing: 1px;
    opacity: 0;
    transition: opacity .5s;
    z-index: 9;
    white-space: nowrap;
    max-width: 90vw;
    text-align: center;
  }
  #hint.show { opacity: 1; }

  /* ---------- Level select ---------- */
  .screen-scroll {
    /* a special screen that allows internal scrolling */
    justify-content: flex-start;
    padding-top: 24px;
    overflow-y: auto;
  }
  .screen-scroll .title-art { font-size: clamp(28px, 5vw, 44px); margin-bottom: 0; }
  .screen-scroll .title-sub { margin-bottom: 12px; font-size: clamp(13px, 2vw, 18px); }

  #level-grid {
    display: grid;
    grid-template-columns: repeat(10, 48px);
    gap: 6px;
    padding: 10px;
    background: rgba(0,0,0,.45);
    border: 2px solid var(--gold-deep);
    box-shadow: inset 0 0 0 1px var(--ink), 0 0 0 2px var(--ink);
    margin-bottom: 14px;
  }
  @media (max-width: 720px) {
    #level-grid {
      grid-template-columns: repeat(5, 56px);
      gap: 8px;
    }
  }
  .level-btn {
    width: 100%; aspect-ratio: 1;
    background: linear-gradient(180deg, var(--gold) 0%, #c98e1f 100%);
    color: var(--ink);
    border: 2px solid #5b3c0c;
    font-family: 'MedievalSharp', serif;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    position: relative;
    box-shadow:
      inset 0 1px 0 rgba(255,255,255,.4),
      inset 0 -2px 0 rgba(0,0,0,.25),
      0 3px 0 #3d2807;
    transition: transform .06s ease;
    padding: 0;
  }
  .level-btn:hover { background: linear-gradient(180deg, var(--gold-glow), var(--gold)); }
  .level-btn:active { transform: translateY(3px); box-shadow: inset 0 1px 0 rgba(255,255,255,.4), inset 0 -2px 0 rgba(0,0,0,.25), 0 0 0 #3d2807; }
  .level-btn.locked {
    background: linear-gradient(180deg, #5a4a40, #2a2018);
    border-color: #1a1410;
    color: rgba(239,226,194,.35);
    cursor: not-allowed;
    box-shadow: inset 0 1px 0 rgba(255,255,255,.05), 0 3px 0 #0a0807;
  }
  .level-btn.locked:hover { background: linear-gradient(180deg, #5a4a40, #2a2018); }
  .level-btn.locked .level-num::before {
    content: "🔒";
    display: block;
    font-size: 14px;
    line-height: 1;
    margin-bottom: 0;
  }
  .level-btn.locked .level-num span { display: none; }
  .level-btn.locked .level-stars { display: none; }
  .level-btn.completed {
    box-shadow:
      inset 0 1px 0 rgba(255,255,255,.4),
      inset 0 -2px 0 rgba(0,0,0,.25),
      0 3px 0 #3d2807,
      0 0 12px rgba(255,214,107,.35);
  }
  .level-num {
    font-size: 16px; line-height: 1; font-weight: 700;
    text-shadow: 0 1px 0 rgba(255,255,255,.4);
  }
  .level-stars {
    display: flex; gap: 1px; margin-top: 2px; height: 8px;
  }
  .level-stars svg { width: 8px; height: 8px; }
  .lvl-pager {
    display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; justify-content: center;
  }
  .lvl-pager button {
    font-family: 'MedievalSharp', serif;
    font-size: 16px;
    padding: 6px 14px;
    background: var(--ink-soft);
    color: var(--gold);
    border: 2px solid var(--gold-deep);
    cursor: pointer;
    letter-spacing: 1px;
  }
  .lvl-pager button.active {
    background: var(--gold);
    color: var(--ink);
  }

  /* Pinned back button on level select */
  .lvl-back-corner {
    position: absolute;
    top: 18px; left: 18px;
    min-width: 0 !important;
    padding: 8px 18px !important;
    font-size: 18px !important;
    margin: 0 !important;
    z-index: 21;
  }
  @media (max-width: 540px) {
    .screen-scroll { padding-top: 70px; }
    .lvl-back-corner { padding: 6px 12px !important; font-size: 15px !important; }
  }

  /* Level indicator on HUD */
  #level-tag {
    font-size: 22px;
    color: var(--gold);
    letter-spacing: 1px;
  }

  /* Earned stars on victory screen */
  .earned-stars {
    display: flex; gap: 8px; margin: 10px 0 18px;
  }
  .earned-stars svg {
    width: clamp(36px, 7vw, 56px);
    height: clamp(36px, 7vw, 56px);
    filter: drop-shadow(0 0 10px rgba(255,214,107,.5));
    animation: starPop .6s cubic-bezier(.5, 1.6, .4, 1) backwards;
  }
  .earned-stars svg:nth-child(1) { animation-delay: .1s; }
  .earned-stars svg:nth-child(2) { animation-delay: .35s; }
  .earned-stars svg:nth-child(3) { animation-delay: .6s; }
  .earned-stars svg.empty { filter: none; opacity: .25; animation: none; }
  @keyframes starPop {
    from { transform: scale(0) rotate(-180deg); opacity: 0; }
    to   { transform: scale(1) rotate(0); opacity: 1; }
  }