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

:root {
  --accent: #7CFC00;
  --accent-2: #36d66b;
  --panel: rgba(13, 18, 38, 0.75);
  --panel-border: rgba(255, 255, 255, 0.12);
  --board: 600px;
}

html, body {
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "Segoe UI", system-ui, sans-serif;
  background:
    radial-gradient(circle at 20% 20%, #14402a 0%, transparent 45%),
    radial-gradient(circle at 80% 30%, #1b2a6b 0%, transparent 50%),
    linear-gradient(160deg, #0a1410 0%, #0d162e 55%, #05060f 100%);
  color: #f4f6ff;
  padding: 18px;
  touch-action: manipulation;
}

.game-wrapper {
  display: grid;
  grid-template-columns: minmax(0, var(--board)) 260px;
  grid-template-rows: auto auto auto;
  gap: 14px 18px;
  width: 100%;
  max-width: 900px;
}

/* ===================== HUD ===================== */
.hud {
  grid-column: 1 / 2;
  display: flex;
  align-items: center;
  gap: 14px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 12px 16px;
  backdrop-filter: blur(6px);
}
.hud-item { display: flex; flex-direction: column; gap: 2px; }
.hud-label {
  font-size: 11px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: #9aa6d4;
}
.hud-value {
  font-size: 24px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
}
#difficulty, #wallMode {
  background: #1a2348;
  color: #fff;
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 14px;
  cursor: pointer;
}
.sound-btn {
  background: #1a2348;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  font-size: 20px;
  width: 44px;
  height: 44px;
  cursor: pointer;
  transition: transform 0.1s ease, background 0.2s;
}
.sound-btn:hover { background: #26316a; transform: scale(1.05); }
.sound-btn.off { opacity: 0.55; font-size: 14px; }
#musicToggle { margin-left: auto; }
.hud-home { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

/* ===================== TABULEIRO ===================== */
/* O frame é o item responsivo do grid; o board é o sistema de coordenadas
   fixo de 600px, escalado por completo via transform (JS) para caber. */
.board-frame {
  grid-column: 1 / 2;
  position: relative;
  width: 100%;
  height: var(--board);
  touch-action: none;
}
.board {
  position: absolute;
  left: 0; top: 0;
  transform-origin: top left;
  width: var(--board);
  height: var(--board);
  border-radius: 18px;
  overflow: hidden;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
  border: 1px solid var(--panel-border);
  background: linear-gradient(160deg, #0e2018 0%, #0c1730 100%);
  user-select: none;
  touch-action: none;
}
.grid-bg { position: absolute; inset: 0; width: 100%; height: 100%; }
.stars { position: absolute; inset: 0; pointer-events: none; }
.star { position: absolute; width: 2px; height: 2px; background: #cfe0ff; border-radius: 50%; }

.layer { position: absolute; inset: 0; pointer-events: none; }
.fx-layer { z-index: 6; }

/* Segmentos da cobra */
.seg {
  position: absolute;
  left: 0; top: 0;
  border-radius: 28%;
  background: linear-gradient(150deg, #8dff4a, #36b84e);
  box-shadow: inset -3px -3px 6px rgba(0, 60, 0, 0.4);
  will-change: transform;
}
.seg.head {
  border-radius: 42%;
  background: linear-gradient(150deg, #b6ff6a, #4cd964);
  z-index: 3;
}
.seg .eye {
  position: absolute;
  width: 22%;
  height: 22%;
  background: #fff;
  border-radius: 50%;
  top: 18%;
}
.seg .eye::after {
  content: "";
  position: absolute;
  width: 55%; height: 55%;
  background: #111;
  border-radius: 50%;
  top: 22%; left: 22%;
}
.seg .eye.left { left: 18%; }
.seg .eye.right { right: 18%; }

/* Maçã */
.apple {
  position: absolute;
  left: 0; top: 0;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ff8a8a, #e23434 70%, #a51d1d);
  box-shadow: inset -3px -3px 6px rgba(120, 0, 0, 0.5), 0 0 12px rgba(255, 60, 60, 0.5);
  z-index: 2;
}
.apple::before { /* folha */
  content: "";
  position: absolute;
  top: -14%; left: 52%;
  width: 36%; height: 34%;
  background: #4cd964;
  border-radius: 0 60% 0 60%;
  transform: rotate(15deg);
}

/* Efeitos */
.particle { position: absolute; border-radius: 50%; will-change: transform, opacity; }
.float-text {
  position: absolute;
  font-weight: 800;
  font-size: 24px;
  color: #fff36b;
  text-shadow: 0 0 6px rgba(0,0,0,0.7);
}

/* ===================== OVERLAYS ===================== */
.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  padding: 24px;
  background: rgba(5, 8, 22, 0.72);
  backdrop-filter: blur(4px);
  z-index: 10;
}
.overlay.hidden { display: none; }
#startOverlay, #pauseOverlay { pointer-events: none; }

.overlay h1 {
  font-size: 44px;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 4px 24px rgba(124, 252, 0, 0.2);
}
.overlay p { font-size: 17px; line-height: 1.5; color: #d7ddf5; }
.overlay .hint { font-size: 14px; color: #9aa6d4; }
.record { color: #ffd84d; font-weight: 700; }

.name-row { display: flex; gap: 8px; margin-top: 6px; }
#playerName {
  padding: 10px 14px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: #11183a;
  color: #fff;
  font-size: 15px;
  outline: none;
}
#playerName:focus { border-color: var(--accent); }

.overlay button {
  cursor: pointer;
  border: none;
  border-radius: 10px;
  padding: 10px 18px;
  font-size: 15px;
  font-weight: 700;
  color: #06210a;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  transition: transform 0.1s ease, filter 0.2s;
}
.overlay button:hover { transform: translateY(-2px); filter: brightness(1.08); }
.play-again { margin-top: 6px; }

/* ===================== D-PAD ===================== */
.dpad {
  grid-column: 1 / 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.dpad-mid { display: flex; gap: 6px; }
.dbtn {
  width: 58px;
  height: 50px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: #1a2348;
  color: #cfe0ff;
  font-size: 20px;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.dbtn:hover { background: #26316a; }
.dbtn:active { transform: scale(0.92); background: var(--accent-2); color: #06210a; }
.dbtn.pause { font-size: 16px; }

/* ===================== RANKING ===================== */
.ranking {
  grid-column: 2 / 3;
  grid-row: 1 / 4;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 16px;
  backdrop-filter: blur(6px);
  display: flex;
  flex-direction: column;
}
.ranking h2 { font-size: 18px; margin-bottom: 12px; }
#rankList { list-style: none; counter-reset: rank; flex: 1; overflow-y: auto; }
#rankList li {
  counter-increment: rank;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 9px;
  margin-bottom: 6px;
  background: rgba(255, 255, 255, 0.04);
  font-size: 14px;
}
#rankList li::before {
  content: counter(rank);
  font-weight: 700;
  color: var(--accent);
  min-width: 18px;
}
#rankList li .rk-name { flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#rankList li .rk-score { font-variant-numeric: tabular-nums; color: #ffce47; font-weight: 700; }
#rankList li:nth-child(1)::before { color: #ffd84d; }
#rankList li:nth-child(2)::before { color: #cfd6e6; }
#rankList li:nth-child(3)::before { color: #e09b5a; }
#rankList li.empty { justify-content: center; color: #6f7aa6; font-style: italic; }
#rankList li.empty::before { content: ""; min-width: 0; }

.back-link {
  margin-top: 10px;
  display: block;
  text-align: center;
  background: transparent;
  border: 1px solid var(--panel-border);
  color: #9aa6d4;
  border-radius: 9px;
  padding: 10px;
  cursor: pointer;
  font-size: 13px;
  text-decoration: none;
  transition: background 0.2s, color 0.2s;
}
.back-link:hover { background: rgba(255, 255, 255, 0.06); color: #fff; }

/* Seletores de dificuldade/paredes na TELA INICIAL (não na HUD) */
.start-controls {
  display: flex;
  gap: 16px;
  justify-content: center;
  flex-wrap: wrap;
  margin: 4px 0 2px;
}
.start-controls label {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  letter-spacing: 0.5px;
  color: #9aa6d4;
}
.start-controls select { font-size: 15px; padding: 8px 10px; }

/* Ranking aparece SOMENTE antes de iniciar (estado "ready") */
body[data-state="playing"] .ranking,
body[data-state="paused"] .ranking,
body[data-state="over"] .ranking { display: none !important; }

/* Botões do game over mais fáceis de tocar */
.play-again { width: 100%; padding: 12px; font-size: 16px; }
#saveScore { white-space: nowrap; }
#playerName { text-transform: uppercase; }
.ovl-home { display: inline-block; margin-top: 8px; color: #9aa6d4; font-size: 14px; text-decoration: none; }
.ovl-home:hover { color: #fff; }
.rank-total { font-size: 11px; color: #6f7aa6; text-align: center; margin-top: 6px; }

/* ===================== JOYSTICK VIRTUAL (mobile) ===================== */
/* Posicionado por JS no ponto onde o dedo toca; base e knob são centralizados
   nesse ponto via offsets negativos. Só aparece quando .active (no toque). */
.joystick {
  position: fixed;
  width: 0;
  height: 0;
  z-index: 50;
  pointer-events: none;
  display: none;
}
.joystick.active { display: block; }
.joy-base {
  position: absolute;
  left: -52px; top: -52px;
  width: 104px; height: 104px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.06);
  border: 2px solid rgba(124, 252, 0, 0.45);
  box-shadow: 0 0 18px rgba(124, 252, 0, 0.18);
}
.joy-knob {
  position: absolute;
  left: -26px; top: -26px;
  width: 52px; height: 52px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #b6ff6a, #4cd964 70%, #36b84e);
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.5);
  will-change: transform;
}

@media (max-width: 900px) and (min-width: 821px) {
  .game-wrapper { grid-template-columns: 1fr; }
  .hud, .board-frame, .dpad { grid-column: 1; }
  .ranking { grid-column: 1; grid-row: auto; }
  .hud { flex-wrap: wrap; }
}

/* ===================== MOBILE (reformulado) ===================== */
@media (max-width: 820px) {
  body { padding: 0; align-items: stretch; justify-content: flex-start; }

  .game-wrapper {
    display: flex;
    flex-direction: column;
    gap: 4px;
    width: 100%;
    max-width: none;
    height: 100dvh;
    padding: calc(env(safe-area-inset-top, 0px) + 4px) 4px 4px;
  }

  /* HUD enxuta: barra fina translúcida só com o essencial */
  .hud {
    flex: 0 0 auto;
    gap: 8px;
    padding: 6px 10px;
    border-radius: 10px;
    flex-wrap: nowrap;
    overflow-x: auto;
  }
  .hud-label { display: none; }
  .hud-value { font-size: 18px; }
  .hud-item:nth-child(2) { display: none; }      /* esconde "Recorde" */
  #difficulty, #wallMode { font-size: 12px; padding: 4px 6px; }
  .sound-btn { width: 38px; height: 38px; font-size: 16px; }

  /* Tabuleiro ocupa todo o espaço restante */
  .board-frame {
    flex: 1 1 auto;
    width: 100%;
    height: auto;
    min-height: 0;
  }

  /* D-pad compacto e flutuante, sobreposto na base (não rouba espaço) */
  .dpad {
    position: fixed;
    left: 50%;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);
    transform: translateX(-50%);
    z-index: 35;
    gap: 4px;
    opacity: 0.92;
  }
  .dbtn {
    width: 54px;
    height: 42px;
    font-size: 18px;
    background: rgba(26, 35, 72, 0.55);
    backdrop-filter: blur(4px);
  }

  /* Ranking: selo discreto no canto superior direito, só os pontos (top 3) */
  .ranking {
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 54px);
    right: 8px;
    z-index: 40;
    width: auto;
    min-width: 96px;
    max-width: 44vw;
    padding: 5px 8px;
    border-radius: 10px;
    background: rgba(5, 8, 22, 0.6);
    backdrop-filter: blur(4px);
  }
  .ranking h2 { font-size: 10px; letter-spacing: 0.5px; margin-bottom: 3px; opacity: 0.8; }
  #rankList { overflow: visible; }
  #rankList li {
    padding: 1px 3px;
    margin-bottom: 1px;
    font-size: 11px;
    background: transparent;
    border-radius: 0;
  }
  #rankList li::before { min-width: 12px; }
  .back-link { display: none; }
  .rank-total { font-size: 10px; }

  /* Game over: campos e botões grandes (fáceis de tocar) */
  .overlay { padding: 16px; }
  .overlay h1 { font-size: 30px; }
  .name-row { width: 100%; gap: 10px; }
  #playerName { flex: 1; min-width: 0; font-size: 18px; padding: 14px; }
  #saveScore { font-size: 17px; padding: 14px 16px; }
  .play-again { font-size: 18px; padding: 16px; }
}
