:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1f242d;
  --text: #e6e8ee;
  --muted: #9aa3b2;
  --accent: #5cc8ff;
  --grid-bg: #11141a;
  --cell-bg: #1a1f28;
  --cell-bg-2: #1d222c;
  --cell-border: #232936;
  --food: #ff6b6b;
  --food-glow: rgba(255, 107, 107, 0.45);
  --hint: rgba(92, 200, 255, 0.18);
  --miss: rgba(255, 107, 107, 0.35);
  --player: #5cc8ff;
  --player-head: #b9e6ff;
  --opponent: #e040fb;
  --opponent-head: #f5b8ff;
  --bot-1: #ffb454;
  --bot-2: #b07cff;
  --bot-3: #6dd6a3;
  --bot-4: #ff7ab6;
  --bot-5: #f7d160;
  --cell-size: 32px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  min-height: 100vh;
}

header {
  padding: 16px 24px 0;
}
header h1 {
  margin: 0;
  font-size: 22px;
  letter-spacing: 0.5px;
}
.tagline {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

main {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 24px;
  padding: 16px 24px 32px;
  align-items: start;
}

#hud {
  display: flex;
  flex-direction: column;
  gap: 12px;
  position: sticky;
  top: 16px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--cell-border);
  border-radius: 10px;
  padding: 12px 14px;
}
.panel h2 {
  margin: 0 0 8px;
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--muted);
}
.panel ul, .panel ol {
  margin: 0;
  padding-left: 18px;
  font-size: 13px;
  line-height: 1.5;
}
.panel label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  margin-bottom: 8px;
  gap: 8px;
}
.panel input[type="number"], .panel select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  padding: 4px 6px;
  font-size: 13px;
  width: 90px;
}
.panel button {
  width: 100%;
  margin-top: 6px;
  background: var(--accent);
  color: #07131c;
  border: 0;
  border-radius: 6px;
  padding: 8px;
  font-weight: 600;
  cursor: pointer;
}
.panel button:hover { filter: brightness(1.1); }
kbd {
  background: var(--panel-2);
  border: 1px solid var(--cell-border);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.legend.food {
  display: inline-block;
  background: var(--food);
  color: #2a0707;
  border-radius: 4px;
  padding: 0 4px;
  font-weight: 700;
}

#scoreboard {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.score-row {
  display: grid;
  grid-template-columns: 14px 1fr auto auto;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
}
.score-row .swatch {
  width: 12px; height: 12px; border-radius: 3px;
}
.score-row .name { font-weight: 600; }
.score-row .meta { color: var(--muted); font-variant-numeric: tabular-nums; }
.score-row.dead { opacity: 0.5; text-decoration: line-through; }
.score-row.self {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* Difficulty preset selector */
.diff-label {
  display: block;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 6px;
}
.diff-btns {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}
.diff-btn {
  padding: 7px 4px;
  border: 1px solid var(--cell-border);
  border-radius: 6px;
  background: var(--panel-2);
  color: var(--muted);
  cursor: pointer;
  transition: background 120ms, color 120ms, border-color 120ms;
  text-align: center;
  line-height: 1;
}
.diff-btn:hover { border-color: #3a4050; color: var(--text); }
.diff-name { display: block; font-size: 12px; font-weight: 700; }
.diff-sub  { display: block; font-size: 10px; font-weight: 500; opacity: 0.75; margin-top: 3px; }
.diff-btn[data-diff="easy"].active   { background: rgba(109,214,163,0.15); border-color: #6dd6a3; color: #6dd6a3; }
.diff-btn[data-diff="medium"].active { background: rgba(255,180,84,0.15);  border-color: #ffb454; color: #ffb454; }
.diff-btn[data-diff="hard"].active   { background: rgba(255,107,107,0.15); border-color: #ff6b6b; color: #ff6b6b; }

.mode-toggle {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}
.mode-toggle label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 13px;
  cursor: pointer;
}
.hi-score {
  margin-top: 4px;
  font-size: 13px;
  color: var(--muted);
}
.hi-score b { color: var(--text); font-variant-numeric: tabular-nums; }
.notice {
  margin: 8px 0 0;
  padding: 6px 8px;
  background: var(--panel-2);
  border-left: 3px solid var(--food);
  font-size: 12px;
  color: var(--muted);
  border-radius: 4px;
}
.cell.snake[data-self="1"] {
  outline: 1px solid #ffffff80;
  outline-offset: -2px;
}
#status.ok { color: var(--accent); }

#leaderboard {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.lb-empty {
  color: var(--muted);
  font-style: italic;
  font-size: 12px;
  padding: 4px 0;
}
.lb-row {
  display: grid;
  grid-template-columns: 18px 1fr auto auto auto auto;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  background: var(--panel-2);
  border: 1px solid var(--cell-border);
  border-radius: 4px;
}
.lb-rank {
  color: var(--muted);
  text-align: right;
}
.lb-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.lb-meta {
  color: var(--muted);
}
.lb-score {
  color: var(--accent);
  font-weight: 700;
}
.lb-tag {
  font-size: 10px;
  font-weight: 700;
  padding: 1px 4px;
  border-radius: 3px;
  letter-spacing: 0.5px;
}
.lb-tag-multi {
  background: rgba(255,107,107,0.25);
  color: #ffb4b4;
}
.lb-tag-single {
  background: rgba(92,200,255,0.20);
  color: #a8def5;
}
.lb-ago {
  font-size: 11px;
}

#board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  position: relative; /* anchor for room overlay positioning */
}

/* Per-move countdown timer bar */
#timer-wrap {
  width: var(--grid-px-width, 400px);
  height: 5px;
  background: var(--panel-2);
  border-radius: 3px;
  overflow: hidden;
  margin-top: -6px; /* close the gap between grid and bar */
  transition: opacity 200ms;
}
#timer-wrap.no-timer {
  opacity: 0;
  pointer-events: none;
}
#timer-bar {
  height: 100%;
  width: 100%;
  background: var(--accent);
  border-radius: 3px;
}

#grid {
  display: grid;
  gap: 2px;
  padding: 8px;
  background: var(--grid-bg);
  border: 1px solid var(--cell-border);
  border-radius: 10px;
  user-select: none;
}

#fx-canvas {
  position: absolute;
  pointer-events: none;
  z-index: 5;
  border-radius: 10px;
}

/* Floating "YOU" tag above the local player's head (multiplayer onboarding) */
#you-tag {
  position: absolute;
  z-index: 10;
  background: var(--player);
  color: #07131c;
  font-weight: 800;
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 6px;
  transform: translate(-50%, 0);
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 14px rgba(92, 200, 255, 0.55);
  transition: opacity 600ms ease-out;
  animation: you-tag-bob 1.6s ease-in-out infinite;
  opacity: 1;
}
#you-tag::after {
  content: "";
  position: absolute;
  bottom: -5px;
  left: 50%;
  transform: translateX(-50%);
  width: 0; height: 0;
  border-left: 5px solid transparent;
  border-right: 5px solid transparent;
  border-top: 5px solid var(--player);
}
#you-tag.fading { opacity: 0; }
@keyframes you-tag-bob {
  0%, 100% { translate: 0 0; }
  50%      { translate: 0 -3px; }
}

/* RTL tweaks (Arabic) — keep grid + score panels visually balanced */
html[dir="rtl"] .lobby-code-input,
html[dir="rtl"] .room-code-big {
  direction: ltr; /* room codes are always Latin */
  unicode-bidi: isolate;
}
html[dir="rtl"] kbd { direction: ltr; unicode-bidi: isolate; }

.cell {
  width: var(--cell-size);
  height: var(--cell-size);
  background: var(--cell-bg);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: calc(var(--cell-size) * 0.55);
  font-weight: 600;
  color: #c8cfdb;
  position: relative;
  transition: background-color 80ms linear, transform 80ms linear;
}
.cell.alt { background: var(--cell-bg-2); }

.cell.hint {
  background: var(--hint);
  color: var(--player-head);
}

.cell.miss-flash {
  background: var(--miss) !important;
}

.cell.food {
  background: var(--food);
  color: #2a0707;
  box-shadow: 0 0 0 0 var(--food-glow);
  animation: pulse 1.6s infinite;
}
@keyframes pulse {
  0%   { box-shadow: 0 0 0 0 var(--food-glow); }
  70%  { box-shadow: 0 0 0 10px rgba(255,107,107,0); }
  100% { box-shadow: 0 0 0 0 rgba(255,107,107,0); }
}

.cell.snake {
  color: rgba(255,255,255,0.0);
}
.cell.snake.head {
  color: #0a0d12;
  font-weight: 800;
}
.cell.snake[data-pid="0"] { background: var(--player); }
.cell.snake.head[data-pid="0"] { background: var(--player-head); }
.cell.snake[data-pid="1"] { background: var(--bot-1); }
.cell.snake[data-pid="2"] { background: var(--bot-2); }
.cell.snake[data-pid="3"] { background: var(--bot-3); }
.cell.snake[data-pid="4"] { background: var(--bot-4); }
.cell.snake[data-pid="5"] { background: var(--bot-5); }

#status {
  min-height: 24px;
  font-size: 14px;
  color: var(--muted);
}
#status.gameover {
  color: var(--food);
  font-weight: 600;
}

/* === Juice ================================================================ */

/* Screen shake on death */
@keyframes shake {
  0%,100% { transform: translate(0, 0) rotate(0deg); }
  12%  { transform: translate(-6px, -3px) rotate(-0.5deg); }
  25%  { transform: translate(5px, 3px) rotate(0.4deg); }
  37%  { transform: translate(-4px, 4px) rotate(-0.35deg); }
  50%  { transform: translate(4px, -3px) rotate(0.3deg); }
  62%  { transform: translate(-3px, 2px) rotate(-0.2deg); }
  75%  { transform: translate(3px, -2px) rotate(0.15deg); }
  87%  { transform: translate(-1px, 1px) rotate(-0.1deg); }
}
.board-shake { animation: shake 0.45s ease-out; }

/* Food eat pop */
@keyframes eat-pop {
  0%   { transform: scale(1); }
  35%  { transform: scale(1.42); box-shadow: 0 0 0 7px rgba(185,230,255,0.45); }
  70%  { transform: scale(0.93); }
  100% { transform: scale(1); box-shadow: none; }
}
.cell.eat-pop {
  animation: eat-pop 0.28s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Snake head glow after eating */
@keyframes head-glow-anim {
  0%   { box-shadow: 0 0 0 4px rgba(185,230,255,0.9), 0 0 18px rgba(185,230,255,0.55); }
  100% { box-shadow: 0 0 0 0 rgba(185,230,255,0), 0 0 0 rgba(185,230,255,0); }
}
.cell.head-glow { animation: head-glow-anim 0.55s ease-out; }

/* Combo streak display */
#streak-display {
  min-height: 26px;
  font-size: 15px;
  font-weight: 700;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  letter-spacing: 0.5px;
  color: transparent;
  user-select: none;
}
#streak-display.active { color: #ffb454; }
#streak-display.hot {
  color: #ff6b6b;
  text-shadow: 0 0 10px rgba(255,107,107,0.7);
}
@keyframes streak-pop {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.25); }
  100% { transform: scale(1); }
}
#streak-display.bump { animation: streak-pop 0.18s ease-out; }

/* Mute indicator */
#mute-indicator {
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}
#mute-indicator:hover { color: var(--text); }
#mute-indicator.muted { color: #ff6b6b; opacity: 0.8; }

/* Stats panel */
#stats-panel {
  display: flex;
  width: 100%;
  max-width: 760px;
  background: var(--panel);
  border: 1px solid var(--cell-border);
  border-radius: 10px;
  overflow: hidden;
}

.stats-col {
  flex: 1;
  padding: 14px 18px;
}

.stats-col + .stats-col {
  border-left: 1px solid var(--cell-border);
}

.stats-heading {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 10px;
}

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 5px 0;
  border-bottom: 1px solid rgba(35, 41, 54, 0.6);
}

.stat-row:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

.stat-row:first-of-type {
  padding-top: 0;
}

.stat-label {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.stat-value {
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text);
}

.stat-accent {
  color: var(--accent);
}

/* === Room snake colors ===================================================== */

.cell.snake[data-pid="room-self"]      { background: var(--player); }
.cell.snake.head[data-pid="room-self"] { background: var(--player-head); }
.cell.snake[data-pid="room-opp"]       { background: var(--opponent); }
.cell.snake.head[data-pid="room-opp"]  { background: var(--opponent-head); }

/* === Room lobby overlay ==================================================== */

/* display:flex in these rules overrides [hidden]'s default display:none */
#room-lobby[hidden], #room-result[hidden] { display: none; }

#room-lobby {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 14, 21, 0.96);
  z-index: 20;
  border-radius: 10px;
}

.lobby-box {
  background: var(--panel);
  border: 1px solid var(--cell-border);
  border-radius: 12px;
  padding: 28px 28px 24px;
  min-width: 270px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.lobby-title {
  margin: 0;
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
}

.lobby-btn {
  display: block;
  width: 100%;
  background: var(--accent);
  color: #07131c;
  border: 0;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: filter 120ms;
}
.lobby-btn:hover { filter: brightness(1.12); }

.lobby-btn-cancel {
  background: var(--panel-2);
  color: var(--muted);
  border: 1px solid var(--cell-border);
  margin-top: 4px;
}
.lobby-btn-cancel:hover { color: var(--text); filter: none; border-color: #3a4050; }

.lobby-btn-primary {
  background: linear-gradient(135deg, #5cc8ff 0%, #b07cff 100%);
  font-size: 16px;
  padding: 12px 16px;
  box-shadow: 0 6px 18px rgba(92,200,255,0.30);
}

.lobby-search-spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--panel-2);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 4px auto 8px;
  animation: lobby-spin 0.9s linear infinite;
}
@keyframes lobby-spin { to { transform: rotate(360deg); } }

#lobby-searching {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
}
#lobby-searching[hidden] { display: none; }

.lobby-or {
  font-size: 12px;
  color: var(--muted);
  margin: 2px 0;
}

.lobby-join-row {
  display: flex;
  gap: 8px;
  align-items: stretch;
}

.lobby-code-input {
  flex: 1;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--cell-border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 18px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-weight: 700;
  letter-spacing: 0.15em;
  text-align: center;
  text-transform: uppercase;
}
.lobby-code-input::placeholder { letter-spacing: 0.1em; font-size: 13px; font-weight: 400; }
.lobby-code-input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.lobby-join-row .lobby-btn {
  flex: 0 0 auto;
  width: auto;
  padding: 8px 18px;
}

.lobby-err {
  color: var(--food);
  font-size: 12px;
  margin: 0;
  padding: 6px 8px;
  background: rgba(255,107,107,0.12);
  border-radius: 6px;
  border: 1px solid rgba(255,107,107,0.25);
}

.lobby-share-label {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.room-code-big {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 42px;
  font-weight: 800;
  letter-spacing: 0.22em;
  color: var(--accent);
  padding: 6px 0;
  user-select: all;
}

.lobby-wait-msg {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
  animation: lobby-pulse 1.8s ease-in-out infinite;
}
@keyframes lobby-pulse {
  0%,100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

/* === Room result overlay (YOU WIN / YOU LOSE) ============================== */

#room-result {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(11, 14, 21, 0.93);
  z-index: 20;
  border-radius: 10px;
}

.result-box {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 36px 40px;
}

.result-title {
  font-size: 52px;
  font-weight: 900;
  letter-spacing: 0.04em;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  line-height: 1;
}
.result-win  { color: #6dd6a3; text-shadow: 0 0 36px rgba(109,214,163,0.55); }
.result-lose { color: var(--food); text-shadow: 0 0 36px rgba(255,107,107,0.55); }

.result-detail {
  font-size: 14px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-height: 20px;
}
