/* ==========================================================
   CHOMP SNAKE — Mobile-First CSS
   Color Palette:
     Background  : #E0E0CE
     Primary     : #2C3E50
     Accent      : #3D0A13
   ========================================================== */

*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  overflow: hidden;
  touch-action: none;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  background-color: #E0E0CE;
  font-family: 'Courier New', Courier, monospace;
  color: #2C3E50;
}

/* ── App Container ──────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 100vw;
  overflow: hidden;
  position: relative;
}

/* ── Top Bar ────────────────────────────────────────────── */
#top-bar {
  display: flex;
  justify-content: space-around;
  align-items: center;
  padding: 6px 8px;
  background: #E0E0CE;
  border-bottom: 2px solid #2C3E50;
  flex-shrink: 0;
  min-height: 44px;
  padding-top: env(safe-area-inset-top, 6px);
}

.top-stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}

.top-stat.rank-stat {
  flex-direction: row;
  gap: 4px;
}

.top-label {
  font-size: 0.5rem;
  letter-spacing: 0.15em;
  opacity: 0.5;
  text-transform: uppercase;
}

.top-value {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #2C3E50;
  transition: color 0.2s;
}

#stat-combo.is-active { color: #3D0A13; }

#stat-rank-emoji {
  font-size: 1.1rem;
  line-height: 1;
  display: inline-block;
}

#stat-rank-label {
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #2C3E50;
  transition: color 0.45s ease;
}

/* ── Bars Row ───────────────────────────────────────────── */
#bars-row {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
}

#combo-bar-wrap {
  width: 100%;
  height: 3px;
  background-color: rgba(44,62,80,0.12);
  overflow: hidden;
}

#combo-bar {
  height: 100%;
  width: 0%;
  background-color: #2C3E50;
  transition: background-color 0.3s ease;
}

#combo-bar.danger { background-color: #3D0A13; }

#rank-tier-bar-wrap {
  width: 100%;
  height: 2px;
  background: rgba(44,62,80,0.10);
  overflow: hidden;
}

#rank-tier-fill {
  height: 100%;
  width: 0%;
  background: #2C3E50;
  transition: width 0.55s ease, background-color 0.45s ease;
}

/* ── HP Row ─────────────────────────────────────────────── */
.top-hp-row {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  padding: 2px 8px;
  flex-shrink: 0;
  background: #E0E0CE;
}

.top-hp-row .stat-label {
  font-size: 0.6rem;
  letter-spacing: 0.18em;
  opacity: 0.55;
}

.hp-hearts, #hp-hearts {
  font-size: 0.85rem;
  letter-spacing: 0.05em;
}

/* ── NEW BEST badge ─────────────────────────────────────── */
#new-best-badge {
  font-size: 0.45rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: #3D0A13;
  display: none;
}

@keyframes bestFlash {
  0%,100% { opacity: 1; }
  50%      { opacity: 0; }
}

#new-best-badge.visible {
  display: inline;
  animation: bestFlash 0.5s ease-in-out 4;
}

/* ── Canvas Wrapper ─────────────────────────────────────── */
#canvas-wrapper {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  min-height: 0;
  padding: 2px;
  background: #E0E0CE;
}

#gameCanvas {
  display: block;
  background-color: #E0E0CE;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
  border: 2px solid #2C3E50;
  box-shadow: 2px 2px 0px rgba(44,62,80,0.25);
}

/* ── Virtual D-Pad ──────────────────────────────────────── */
#dpad-container {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 4px 0;
  flex-shrink: 0;
  border-top: 1px solid rgba(44,62,80,0.18);
}

#dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.dpad-mid-row {
  display: flex;
  align-items: center;
  gap: 2px;
}

.dpad-btn {
  width: clamp(44px, 12vw, 64px);
  height: clamp(44px, 12vw, 64px);
  font-size: clamp(1rem, 3vw, 1.5rem);
  background: rgba(44,62,80,0.06);
  border: 2px solid rgba(44,62,80,0.25);
  color: #2C3E50;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.05s, transform 0.05s, border-color 0.05s;
}

.dpad-btn:active {
  background: rgba(44,62,80,0.22);
  border-color: rgba(44,62,80,0.55);
  transform: scale(0.9);
}

.dpad-center {
  width: clamp(44px, 12vw, 64px);
  height: clamp(44px, 12vw, 64px);
  border-radius: 10px;
  background: rgba(44,62,80,0.03);
  border: 1px dashed rgba(44,62,80,0.1);
}

/* ── Bottom Controls ────────────────────────────────────── */
#bottom-controls {
  display: flex;
  justify-content: space-around;
  padding: 6px 10px;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
  border-top: 2px solid #2C3E50;
  background: #E0E0CE;
  flex-shrink: 0;
  gap: 6px;
}

#bottom-controls button {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 8px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: #2C3E50;
  background: transparent;
  border: 1.5px solid #2C3E50;
  border-radius: 4px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.08s, color 0.08s;
}

#bottom-controls button:active {
  background: rgba(44,62,80,0.15);
}

#btn-pause {
  background-color: #2C3E50 !important;
  color: #E0E0CE !important;
}

#btn-pause:active {
  background-color: #3d5166 !important;
}

#btn-pause.is-paused {
  background-color: transparent !important;
  color: #2C3E50 !important;
}

#btn-mute.is-muted { opacity: 0.35; text-decoration: line-through; }

/* ── Side Menu ──────────────────────────────────────────── */
#menu-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 998;
  transition: opacity 0.25s ease;
}

#menu-overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

#side-menu {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(280px, 80vw);
  background: #E0E0CE;
  border-left: 2px solid #2C3E50;
  padding: 20px 16px;
  z-index: 999;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  transition: transform 0.3s ease;
}

#side-menu.hidden {
  transform: translateX(100%);
}

#menu-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 36px;
  height: 36px;
  font-size: 1.2rem;
  background: transparent;
  border: 1.5px solid #2C3E50;
  border-radius: 4px;
  color: #2C3E50;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

.game-title {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  line-height: 1.25;
  text-align: center;
  color: #2C3E50;
  margin-bottom: 4px;
  margin-top: 4px;
}

.divider {
  width: 100%;
  height: 1px;
  background-color: #2C3E50;
  opacity: 0.35;
  margin: 14px 0;
}

/* ── Stats Block ────────────────────────────────────────── */
.stats-block {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.stat-label {
  font-size: 0.6rem;
  letter-spacing: 0.18em;
  opacity: 0.55;
  text-transform: uppercase;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #2C3E50;
  transition: color 0.2s;
}

/* ── Section Label ──────────────────────────────────────── */
.section-label {
  font-size: 0.55rem;
  letter-spacing: 0.22em;
  opacity: 0.45;
  text-align: center;
  margin-bottom: 10px;
}

/* ── Mode Block ─────────────────────────────────────────── */
.mode-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.mode-btn {
  position: relative;
  display: flex;
  align-items: center;
  gap: 7px;
  width: 100%;
  padding: 10px 12px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #2C3E50;
  background-color: transparent;
  border: 1px solid #2C3E50;
  cursor: pointer;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.08s ease, color 0.08s ease;
}

.mode-btn.active {
  background-color: #2C3E50;
  color: #E0E0CE;
}

.mode-icon {
  font-size: 0.5rem;
  opacity: 0.5;
  flex-shrink: 0;
}

.mode-btn.active .mode-icon { opacity: 1; }

.mode-sub {
  margin-left: auto;
  font-size: 0.5rem;
  font-weight: 400;
  opacity: 0.5;
  letter-spacing: 0;
}

.mode-btn.active .mode-sub {
  opacity: 0.75;
  color: #E0E0CE;
}

/* ── Skin Block ─────────────────────────────────────────── */
.skin-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.skin-btn-row {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.skin-btn {
  flex: 1 1 auto;
  min-width: 40px;
  padding: 7px 3px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.58rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #2C3E50;
  background-color: transparent;
  border: 1px solid rgba(44,62,80,0.35);
  cursor: pointer;
  text-align: center;
  -webkit-tap-highlight-color: transparent;
  transition: background-color 0.08s, color 0.08s, border-color 0.08s;
}

.skin-btn.active {
  background-color: #2C3E50;
  color: #E0E0CE;
  border-color: #2C3E50;
}

/* ── Control Block ──────────────────────────────────────── */
.ctrl-block {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.speed-btn-row {
  display: flex;
  gap: 4px;
  margin-top: 6px;
}

.speed-btn {
  flex: 1;
  padding: 8px 2px;
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: #2C3E50;
  background: transparent;
  border: 1px solid rgba(44,62,80,0.35);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background 0.08s, color 0.08s, border-color 0.08s;
}

.speed-btn.active {
  background: #2C3E50;
  color: #E0E0CE;
  border-color: #2C3E50;
}

.speed-btn[data-speed="easy"]::after   { content: ' \00B7'; }
.speed-btn[data-speed="medium"]::after { content: ' \00B7\00B7'; }
.speed-btn[data-speed="hard"]::after   { content: ' \00B7\00B7\00B7'; }

/* Poison */
.poison-row {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 6px;
}

.ctrl-label {
  font-size: 0.6rem;
  letter-spacing: 0.04em;
  opacity: 0.7;
  flex-shrink: 0;
}

.poison-btn {
  font-family: 'Courier New', Courier, monospace;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  padding: 6px 10px;
  color: #2C3E50;
  background: transparent;
  border: 1px solid rgba(44,62,80,0.35);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: all 0.1s ease;
  margin-left: auto;
  flex-shrink: 0;
}

.poison-btn.is-on {
  background: #C0392B;
  color: #E0E0CE;
  border-color: #C0392B;
}

.poison-hint {
  font-size: 0.5rem;
  letter-spacing: 0.03em;
  opacity: 0.45;
  margin-top: 4px;
  line-height: 1.4;
}

/* ── Controls Hint ──────────────────────────────────────── */
.controls-block {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.hint {
  font-size: 0.58rem;
  letter-spacing: 0.08em;
  opacity: 0.45;
  display: flex;
  align-items: center;
  gap: 5px;
}

/* ── Combo Animations ───────────────────────────────────── */
@keyframes comboJump {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.55); color: #3D0A13; }
  100% { transform: scale(1); }
}

#stat-combo.jump {
  animation: comboJump 0.18s ease-out forwards;
}

/* ── Rank-up Animations ─────────────────────────────────── */
@keyframes rankUp2 {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.6); }
  100% { transform: scale(1); }
}
#stat-rank-emoji.rank-up-t2 { animation: rankUp2 0.28s ease-out forwards; }

@keyframes rankUp3 {
  0%   { transform: scale(1)   rotate(0deg); }
  35%  { transform: scale(1.9) rotate(-14deg); }
  65%  { transform: scale(1.3) rotate(8deg); }
  100% { transform: scale(1)   rotate(0deg); }
}
#stat-rank-emoji.rank-up-t3 { animation: rankUp3 0.44s ease-out forwards; }

@keyframes rankUp4 {
  0%   { transform: scale(1)   rotate(0deg); }
  22%  { transform: scale(2.3) rotate(-28deg); }
  48%  { transform: scale(1.6) rotate(18deg); }
  75%  { transform: scale(1.2) rotate(-6deg); }
  100% { transform: scale(1)   rotate(0deg); }
}
#stat-rank-emoji.rank-up-t4 { animation: rankUp4 0.60s cubic-bezier(0.17,0.67,0.83,0.67) forwards; }

@keyframes rankUp5 {
  0%   { transform: scale(1)   rotate(0deg); }
  18%  { transform: scale(3.0) rotate(-45deg); }
  36%  { transform: scale(2.1) rotate(28deg); }
  54%  { transform: scale(2.4) rotate(-16deg); }
  72%  { transform: scale(1.5) rotate(6deg); }
  88%  { transform: scale(1.1) rotate(-2deg); }
  100% { transform: scale(1)   rotate(0deg); }
}
#stat-rank-emoji.rank-up-t5 { animation: rankUp5 0.85s cubic-bezier(0.17,0.67,0.83,0.67) forwards; }

@keyframes rankUp6 {
  0%   { transform: scale(1)   rotate(0deg); }
  10%  { transform: scale(3.6) rotate(-72deg); }
  22%  { transform: scale(2.6) rotate(54deg); }
  34%  { transform: scale(3.1) rotate(-36deg); }
  46%  { transform: scale(2.2) rotate(24deg); }
  58%  { transform: scale(2.6) rotate(-14deg); }
  70%  { transform: scale(1.8) rotate(8deg); }
  82%  { transform: scale(1.3) rotate(-3deg); }
  92%  { transform: scale(1.1) rotate(1deg); }
  100% { transform: scale(1)   rotate(0deg); }
}
#stat-rank-emoji.rank-up-t6 {
  animation: rankUp6 1.25s cubic-bezier(0.17,0.67,0.83,0.67) forwards;
  filter: drop-shadow(0 0 6px gold) drop-shadow(0 0 12px gold);
}

@keyframes rankLabelGlow {
  0%, 100% { text-shadow: none; }
  50%       { text-shadow: 0 0 10px currentColor, 0 0 20px currentColor; }
}
#stat-rank-label.rank-label-glow {
  animation: rankLabelGlow 0.55s ease-in-out 3;
}

/* ── Landscape Warning ──────────────────────────────────── */
#landscape-warning {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 10000;
  background: #E0E0CE;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: #2C3E50;
  font-family: 'Courier New', Courier, monospace;
  text-align: center;
  padding: 20px;
}

#landscape-warning .rotate-icon {
  font-size: 3rem;
  animation: rotateHint 1.5s ease-in-out infinite;
}

#landscape-warning .rotate-text {
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.12em;
}

#landscape-warning .rotate-sub {
  font-size: 0.6rem;
  opacity: 0.5;
}

@keyframes rotateHint {
  0%, 100% { transform: rotate(0deg); }
  50%      { transform: rotate(90deg); }
}

@media (orientation: landscape) and (max-height: 500px) {
  #landscape-warning { display: flex; }
  #app { display: none !important; }
}
