* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  font-family: 'Khmer OS Battambang', 'Battambang', 'Khmer OS', sans-serif;
}
html, body {
  overflow: hidden;
  background: #000;
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  touch-action: manipulation;
}
canvas { display: block; touch-action: none; }

#mobile-controls,
#mobile-controls *,
#mob-pause,
#audio-controls button {
  touch-action: none;
}

/* ============================================================
   HUD
   ============================================================ */
#hud {
  position: fixed;
  top: 0; left: 0; right: 0;
  padding: 15px 20px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 10;
  gap: 10px;
  flex-wrap: wrap;
}
.hud-panel {
  background: rgba(0, 20, 30, 0.7);
  border: 1px solid rgba(0, 255, 200, 0.3);
  border-radius: 8px;
  padding: 10px 15px;
  backdrop-filter: blur(10px);
  min-width: 130px;
  text-shadow: 0 0 10px rgba(0,255,200,0.5);
}
.hud-label { font-size: 11px; color: #0fc; opacity: 0.75; }
.hud-value { font-size: 20px; font-weight: bold; color: #0fc; line-height: 1.3; }
#energy-bar-wrap {
  width: 180px; height: 12px;
  background: rgba(0,0,0,0.5);
  border: 1px solid rgba(0,255,200,0.4);
  border-radius: 6px; overflow: hidden; margin-top: 5px;
}
#energy-bar {
  height: 100%; width: 100%;
  background: linear-gradient(90deg, #f0f, #0ff);
  transition: width 0.15s;
}
.hud-stars { border-color: rgba(255, 200, 0, 0.4) !important; }
.hud-stars .hud-label { color: #fa0 !important; }
.hud-stars .hud-value { color: #fa0 !important; font-size: 18px; white-space: nowrap; }

#objective {
  position: fixed;
  top: 95px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 20, 30, 0.85);
  border: 1px solid rgba(0, 255, 200, 0.4);
  border-radius: 8px;
  padding: 8px 18px;
  font-size: 13px;
  color: #0fc;
  z-index: 10;
  text-align: center;
  backdrop-filter: blur(8px);
  pointer-events: none;
  max-width: min(90vw, 640px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
#objective .target { color: #fff; font-weight: bold; }

#abilities {
  position: fixed; bottom: 20px; left: 50%;
  transform: translateX(-50%);
  display: flex; gap: 12px; z-index: 10; pointer-events: none;
}
.ability {
  width: 78px; height: 78px;
  background: rgba(0, 20, 30, 0.75);
  border: 2px solid rgba(0, 255, 200, 0.4);
  border-radius: 10px;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  font-size: 10px; text-align: center;
  position: relative; overflow: hidden;
  backdrop-filter: blur(6px);
}
.ability .key {
  position: absolute; top: 3px; right: 5px;
  font-size: 10px; color: #0fc; opacity: 0.85;
}
.ability .icon { font-size: 22px; }
.ability .name { font-size: 10px; opacity: 0.9; line-height: 1; margin-top: 2px; }
.ability.active { background: rgba(0,255,200,0.3); }

/* ============================================================
   MOBILE CONTROLS
   ============================================================ */
#mobile-controls {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  z-index: 25;
  touch-action: none;
  pointer-events: auto;
}
#mobile-controls.hidden { display: none; }

.mobile-btn {
  width: 68px;
  height: 68px;
  border-radius: 50%;
  background: rgba(0, 20, 30, 0.85);
  border: 2px solid rgba(0, 255, 200, 0.6);
  color: #0fc;
  font-size: 26px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(8px);
  transition: transform 0.08s, background 0.08s, border-color 0.08s;
  padding: 0;
  font-family: inherit;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  touch-action: none;
}
.mobile-btn .mob-icon {
  pointer-events: none;
  line-height: 1;
}
.mobile-btn:active,
.mobile-btn.active {
  transform: scale(0.9);
  background: rgba(0, 255, 200, 0.35);
  border-color: #0fc;
  box-shadow: 0 0 20px rgba(0,255,200,0.7);
}
.mobile-btn:disabled {
  opacity: 0.3;
  filter: grayscale(0.7);
}

#mob-pause {
  position: fixed;
  top: 100px;
  right: 20px;
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: rgba(0, 20, 30, 0.85);
  border: 2px solid rgba(0, 255, 200, 0.5);
  color: #0fc;
  font-size: 22px;
  z-index: 25;
  backdrop-filter: blur(8px);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  padding: 0;
  font-family: inherit;
  touch-action: none;
}
#mob-pause:active { transform: scale(0.9); }
#mob-pause.hidden { display: none; }

.float-text {
  position: fixed; font-size: 22px; font-weight: bold;
  pointer-events: none; z-index: 30;
  text-shadow: 0 0 12px currentColor, 0 2px 6px rgba(0,0,0,0.9);
  animation: floatUp 1.1s ease-out forwards;
  white-space: nowrap;
}
@keyframes floatUp {
  0% { opacity: 0; transform: translate(-50%, 0) scale(0.6); }
  15% { opacity: 1; transform: translate(-50%, -12px) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%, -70px) scale(1); }
}

#message {
  position: fixed; top: 28%; left: 50%;
  transform: translate(-50%, -50%);
  font-size: 38px; font-weight: bold; text-align: center;
  color: #0fc; text-shadow: 0 0 30px rgba(0,255,200,0.8);
  z-index: 20; pointer-events: none;
  opacity: 0; transition: opacity 0.3s; white-space: nowrap;
}
#message.show { opacity: 1; }
#message .sub {
  font-size: 15px; color: #fff;
  margin-top: 8px; opacity: 0.85;
  white-space: normal; line-height: 1.5;
}

#overlay {
  position: fixed; inset: 0;
  background: radial-gradient(circle at center, rgba(0,30,40,0.96), rgba(0,0,10,0.99));
  display: flex; flex-direction: column;
  align-items: center; justify-content: flex-start;
  z-index: 100; text-align: center;
  padding: 30px 20px; overflow-y: auto;
}
#overlay.hidden { display: none; }
#overlay::-webkit-scrollbar { width: 8px; }
#overlay::-webkit-scrollbar-track { background: rgba(0,255,200,0.05); }
#overlay::-webkit-scrollbar-thumb { background: rgba(0,255,200,0.35); border-radius: 4px; }
#overlay h1 {
  font-size: 44px; color: #0fc;
  text-shadow: 0 0 40px rgba(0,255,200,0.8);
  margin-bottom: 8px; line-height: 1.3; flex-shrink: 0;
}
#overlay .tag {
  color: #f0f; font-size: 13px; margin-bottom: 18px; flex-shrink: 0;
}
#overlay p {
  max-width: 680px; line-height: 1.9;
  margin-bottom: 10px; color: #bcd; font-size: 14px;
}
#overlay .controls {
  display: grid; grid-template-columns: repeat(2, auto);
  gap: 8px 30px; margin: 20px 0; font-size: 14px; align-items: center;
}
#overlay .controls span:nth-child(odd) { color: #0fc; font-weight: bold; text-align: right; }
#overlay .controls span:nth-child(even) { text-align: left; color: #bcd; }
#overlay button {
  padding: 14px 40px; font-size: 16px; font-weight: bold;
  background: linear-gradient(135deg, #0fc, #f0f);
  border: none; border-radius: 8px; color: #001;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 0 30px rgba(0,255,200,0.4);
  margin: 6px 5px;
  font-family: 'Khmer OS Battambang', 'Battambang', sans-serif;
  flex-shrink: 0;
}
#overlay button:hover {
  transform: scale(1.05);
  box-shadow: 0 0 50px rgba(0,255,200,0.7);
}
#overlay button.secondary {
  background: transparent; border: 2px solid #0fc;
  color: #0fc; box-shadow: none;
}
#overlay button.secondary:hover {
  background: rgba(0,255,200,0.1);
  box-shadow: 0 0 20px rgba(0,255,200,0.4);
}

#pause-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 10, 20, 0.85);
  backdrop-filter: blur(10px);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  z-index: 200; text-align: center; padding: 20px;
}
#pause-overlay.hidden { display: none; }
#pause-overlay h1 {
  font-size: 56px; color: #0fc;
  text-shadow: 0 0 50px rgba(0,255,200,0.9);
  margin-bottom: 10px;
}
#pause-overlay .tag {
  color: #f0f; font-size: 14px; margin-bottom: 30px;
}
#pause-overlay button {
  padding: 14px 44px; font-size: 16px; font-weight: bold;
  background: linear-gradient(135deg, #0fc, #f0f);
  border: none; border-radius: 8px; color: #001;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
  box-shadow: 0 0 30px rgba(0,255,200,0.4);
  margin: 6px 5px;
  font-family: 'Khmer OS Battambang', 'Battambang', sans-serif;
}
#pause-overlay button:hover {
  transform: scale(1.05);
  box-shadow: 0 0 50px rgba(0,255,200,0.7);
}
#pause-overlay button.secondary {
  background: transparent; border: 2px solid #0fc;
  color: #0fc; box-shadow: none;
}

#hint {
  position: fixed;
  bottom: 130px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 20, 30, 0.95);
  border: 2px solid #fa0;
  border-radius: 12px;
  padding: 12px 24px;
  font-size: 15px;
  color: #fa0;
  z-index: 60;
  backdrop-filter: blur(10px);
  box-shadow: 0 0 40px rgba(255,170,0,0.4);
  animation: hintPop 0.4s ease-out;
  pointer-events: none;
  max-width: 90vw;
  text-align: center;
}
#hint.hidden { display: none; }
#hint .hint-icon { font-size: 20px; margin-right: 6px; }
@keyframes hintPop {
  0% { opacity: 0; transform: translate(-50%, 20px) scale(0.8); }
  100% { opacity: 1; transform: translate(-50%, 0) scale(1); }
}

#rotate-hint {
  position: fixed; inset: 0;
  background: rgba(0, 10, 20, 0.98);
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  z-index: 500; color: #0fc;
  font-size: 20px; gap: 20px;
  text-align: center; padding: 30px;
  pointer-events: none;
}
#rotate-hint.hidden { display: none; }
#rotate-hint .rotate-icon {
  font-size: 60px;
  animation: rotateAnim 2s infinite ease-in-out;
}
@keyframes rotateAnim {
  0%, 100% { transform: rotate(0deg); }
  50% { transform: rotate(90deg); }
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(190px, 1fr));
  gap: 12px; max-width: 950px; width: 100%; margin: 20px 0;
}
.level-card {
  background: rgba(0, 30, 45, 0.8);
  border: 2px solid rgba(0, 255, 200, 0.4);
  border-radius: 10px; padding: 12px;
  cursor: pointer; transition: all 0.15s;
  text-align: left; position: relative; overflow: hidden;
}
.level-card:hover:not(.locked) {
  transform: translateY(-3px);
  border-color: #0fc;
  box-shadow: 0 0 25px rgba(0,255,200,0.5);
}
.level-card.locked { opacity: 0.4; cursor: not-allowed; filter: grayscale(0.7); }
.level-card.locked-hard {
  border-color: #ff2200;
  background: linear-gradient(135deg, rgba(60,0,0,0.6), rgba(20,0,30,0.85));
  opacity: 1; filter: none;
  animation: lightning-pulse 2.5s infinite;
}
.level-card.locked-hard.locked { opacity: 0.5; animation: none; }
@keyframes lightning-pulse {
  0%, 88%, 100% { box-shadow: 0 0 15px rgba(255,40,0,0.4); }
  91%, 94% { box-shadow: 0 0 45px rgba(255,255,255,0.95); }
}
.level-num { font-size: 22px; font-weight: bold; color: #0fc; line-height: 1; }
.level-card.locked-hard .level-num { color: #ff5533; }
.level-name { font-size: 14px; font-weight: bold; margin: 6px 0 4px; }
.level-desc { font-size: 11px; color: #8ab; line-height: 1.5; }
.level-stars { font-size: 15px; margin-top: 6px; color: #fa0; letter-spacing: 2px; }
.level-stars .empty { color: rgba(255,170,0,0.25); }

#legend {
  position: fixed; bottom: 20px; right: 20px;
  background: rgba(0, 20, 30, 0.7);
  border: 1px solid rgba(0, 255, 200, 0.3);
  border-radius: 8px; padding: 10px 14px;
  font-size: 12px; backdrop-filter: blur(8px);
  z-index: 10; line-height: 1.7;
}
#legend .row { display: flex; align-items: center; gap: 8px; }
#legend .dot {
  width: 10px; height: 10px; border-radius: 50%;
  box-shadow: 0 0 8px currentColor; flex-shrink: 0;
}
#combo {
  position: fixed; top: 100px; left: 50%;
  transform: translateX(-50%);
  font-size: 26px; color: #f0f; font-weight: bold;
  text-shadow: 0 0 20px #f0f;
  opacity: 0; transition: opacity 0.3s, transform 0.3s;
  z-index: 10; pointer-events: none;
}
#combo.show { opacity: 1; transform: translateX(-50%) scale(1.2); }
#audio-controls {
  position: fixed; bottom: 20px; left: 20px;
  display: flex; gap: 8px; z-index: 15;
}
#audio-controls button {
  width: 44px; height: 44px; border-radius: 50%;
  background: rgba(0, 20, 30, 0.8);
  border: 2px solid rgba(0, 255, 200, 0.4);
  color: #0fc; font-size: 18px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: all 0.15s;
  backdrop-filter: blur(8px); padding: 0; font-family: inherit;
}
#audio-controls button:hover { background: rgba(0, 255, 200, 0.15); transform: scale(1.1); }
#audio-controls button.muted { opacity: 0.45; border-color: rgba(255, 100, 100, 0.4); color: #f66; }

.about-box {
  max-width: 820px; width: 100%;
  max-height: 65vh; overflow-y: auto;
  background: rgba(0, 25, 35, 0.85);
  border: 1px solid rgba(0, 255, 200, 0.4);
  border-radius: 14px; padding: 20px 24px;
  text-align: left; margin: 15px 0;
  line-height: 1.75; font-size: 12.5px; color: #cde;
  box-shadow: 0 0 40px rgba(0,255,200,0.15);
}
.about-box::-webkit-scrollbar { width: 8px; }
.about-box::-webkit-scrollbar-track { background: rgba(0,255,200,0.05); border-radius: 4px; }
.about-box::-webkit-scrollbar-thumb { background: rgba(0,255,200,0.35); border-radius: 4px; }
.about-box h2 { color: #0fc; font-size: 16px; margin-bottom: 8px; margin-top: 18px; line-height: 1.5; }
.about-box h2:first-child { margin-top: 0; }
.about-box p { margin-bottom: 8px; line-height: 1.75; font-size: 12.5px; }
.about-box .lesson {
  background: rgba(255,170,0,0.08);
  border-left: 3px solid #fa0;
  padding: 8px 12px; border-radius: 6px;
  margin-bottom: 10px; font-size: 12px; line-height: 1.75;
}
.about-box .lesson b { color: #fa0; }

.star-row {
  display: flex; gap: 20px; justify-content: center;
  align-items: center; margin: 12px 0 20px;
  font-size: 46px; height: 60px; line-height: 1;
  flex-shrink: 0; overflow: visible;
}
.star-row .s {
  color: rgba(255,170,0,0.2);
  transition: color 0.4s, text-shadow 0.4s, transform 0.4s;
  display: inline-block; line-height: 1; transform: scale(1);
}
.star-row .s.earned {
  color: #fa0;
  text-shadow: 0 0 30px rgba(255,170,0,0.9);
  transform: scale(1.15);
}
.star-criteria {
  display: flex; flex-direction: column;
  gap: 6px; margin: 0 auto 20px;
  max-width: 380px; width: 100%;
  text-align: left;
  background: rgba(0, 20, 30, 0.5);
  border: 1px solid rgba(0, 255, 200, 0.2);
  border-radius: 8px;
  padding: 12px 18px;
  font-size: 13px;
}
.star-criteria .criterion {
  display: flex; align-items: center; gap: 8px;
  color: #8ab;
}
.star-criteria .criterion.met { color: #0fc; }
.star-criteria .criterion .check {
  width: 18px; text-align: center;
  font-weight: bold;
  flex-shrink: 0;
}
.star-criteria .criterion.met .check { color: #0fc; }
.star-criteria .criterion:not(.met) .check { color: #f66; }
.stats {
  display: grid; grid-template-columns: repeat(2, auto);
  gap: 8px 30px; margin: 12px 0 20px;
  font-size: 14px; justify-content: center;
  align-items: center; flex-shrink: 0;
}
.stats .k { color: #0fc; text-align: right; white-space: nowrap; }
.stats .v { color: #fff; text-align: left; font-weight: bold; white-space: nowrap; }

.difficulty-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  max-width: 720px; width: 100%;
  margin: 20px 0;
}
.diff-card {
  background: rgba(0, 30, 45, 0.8);
  border: 2px solid rgba(0, 255, 200, 0.4);
  border-radius: 12px;
  padding: 18px 14px;
  cursor: pointer;
  transition: all 0.15s;
  text-align: center;
}
.diff-card:hover {
  transform: translateY(-4px);
  box-shadow: 0 0 30px rgba(0,255,200,0.6);
}
.diff-card.selected {
  border-color: #fa0;
  box-shadow: 0 0 30px rgba(255,170,0,0.6);
  background: rgba(80, 40, 0, 0.4);
}
.diff-card .diff-icon { font-size: 32px; margin-bottom: 8px; }
.diff-card .diff-name { font-size: 16px; font-weight: bold; margin-bottom: 6px; }
.diff-card .diff-desc { font-size: 11px; color: #8ab; line-height: 1.5; }
.diff-card.easy .diff-name { color: #0f8; }
.diff-card.normal .diff-name { color: #0fc; }
.diff-card.hard .diff-name { color: #f66; }

@media (max-width: 720px) {
  .hud-panel { min-width: 90px; padding: 8px 11px; }
  .hud-value { font-size: 16px; }
  .hud-stars .hud-value { font-size: 15px; }
  #energy-bar-wrap { width: 110px; }
  #objective { top: 88px; font-size: 12px; }
  #overlay h1 { font-size: 32px; margin-bottom: 4px; }
  #overlay .tag { font-size: 12px; margin-bottom: 12px; }
  .star-row { font-size: 38px; height: 48px; margin: 8px 0 14px; }
  .stats { font-size: 13px; margin: 8px 0 14px; gap: 6px 20px; }
  #overlay button { padding: 11px 28px; font-size: 14px; }
  .difficulty-grid { grid-template-columns: 1fr; }
  #pause-overlay h1 { font-size: 40px; }
}

@media (hover: none) and (pointer: coarse) {
  #abilities { display: none; }
  #legend { display: none; }
  .hud-stars { display: none; }

  #mobile-controls:not(.hidden) { display: grid; }
  #mob-pause:not(.hidden) { display: flex; }

  #hud {
    padding: 8px 10px;
    gap: 4px;
    padding-top: max(8px, env(safe-area-inset-top));
  }
  .hud-panel {
    min-width: 60px;
    padding: 5px 8px;
    border-radius: 6px;
  }
  .hud-label { font-size: 8px; }
  .hud-value { font-size: 13px; }
  #energy-bar-wrap {
    width: 70px;
    height: 8px;
  }
  #energy-text { font-size: 10px !important; }

  #objective {
    top: 62px;
    font-size: 11px;
    padding: 5px 10px;
    max-width: 92vw;
  }

  #combo { font-size: 20px; top: 75px; }

  #audio-controls {
    bottom: 15px;
    left: 15px;
    gap: 6px;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #audio-controls button {
    width: 40px;
    height: 40px;
    font-size: 16px;
  }

  #mobile-controls {
    bottom: 20px;
    right: 20px;
    padding-bottom: env(safe-area-inset-bottom);
    padding-right: env(safe-area-inset-right);
  }

  #mob-pause {
    top: 62px;
    right: 12px;
    width: 44px;
    height: 44px;
    font-size: 18px;
  }

  #overlay { padding: 20px 14px; }
  #overlay h1 { font-size: 26px; }
  #overlay .tag { font-size: 11px; }
  #overlay p { font-size: 12px; }
  #overlay .controls { font-size: 12px; gap: 6px 20px; }
  #overlay button { padding: 12px 26px; font-size: 14px; }

  #message { font-size: 26px; }
  #message .sub { font-size: 12px; }

  #pause-overlay h1 { font-size: 36px; }
  #pause-overlay button { padding: 12px 32px; font-size: 14px; }

  #hint {
    bottom: 110px;
    font-size: 13px;
    padding: 10px 18px;
    max-width: 88vw;
  }
}

@media (max-width: 480px) {
  .mobile-btn { width: 60px; height: 60px; font-size: 22px; }
  #mobile-controls { gap: 10px; bottom: 15px; right: 15px; }

  #hud { padding: 6px 8px; }
  .hud-panel { min-width: 55px; padding: 4px 7px; }
  .hud-value { font-size: 12px; }
  .hud-label { font-size: 7px; }
  #energy-bar-wrap { width: 60px; height: 7px; }

  #objective { top: 55px; font-size: 10px; }

  #mob-pause { top: 55px; width: 40px; height: 40px; font-size: 16px; }

  .level-grid { grid-template-columns: 1fr; }
  .level-card { padding: 10px; }
  .level-num { font-size: 18px; }
  .level-name { font-size: 12px; }
  .level-desc { font-size: 10px; }
}

@media (orientation: landscape) and (max-height: 500px) {
  #hud { padding: 6px 10px; }
  .hud-panel { padding: 4px 8px; min-width: 55px; }
  .hud-label { font-size: 8px; }
  .hud-value { font-size: 12px; }
  #energy-bar-wrap { width: 70px; height: 7px; }
  #energy-text { font-size: 10px !important; }

  #objective { top: 50px; font-size: 10px; padding: 4px 10px; }

  #mobile-controls {
    bottom: 15px;
    right: 15px;
    gap: 10px;
  }
  .mobile-btn { width: 56px; height: 56px; font-size: 20px; }

  #mob-pause { top: 50px; width: 38px; height: 38px; font-size: 16px; }

  #audio-controls { bottom: 12px; left: 12px; }
  #audio-controls button { width: 36px; height: 36px; font-size: 14px; }

  #combo { top: 55px; font-size: 18px; }
}