:root {
  color-scheme: dark;
  --bg: #0a0c0b;
  --surface: #101310;
  --surface-raised: #171a17;
  --line: #282d28;
  --line-strong: #3a413a;
  --text: #f3f5ef;
  --muted: #8b938b;
  --green: #b7f34b;
  --green-soft: #86b83a;
  --coral: #ff665d;
  --danger: #ff877f;
  --font-sans: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  --font-mono: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--bg);
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100svh;
  margin: 0;
  overflow-x: hidden;
  background:
    linear-gradient(rgba(255, 255, 255, 0.018) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.018) 1px, transparent 1px),
    var(--bg);
  background-size: 42px 42px;
  color: var(--text);
  font-family: var(--font-sans);
  letter-spacing: 0;
}

button,
input {
  font: inherit;
  letter-spacing: 0;
}

button {
  color: inherit;
}

button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--green);
  outline-offset: 3px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-shell {
  width: min(1220px, calc(100% - 48px));
  min-height: 100vh;
  min-height: 100svh;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.site-header,
.site-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 72px;
  border-color: var(--line);
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
}

.site-header {
  border-bottom: 1px solid var(--line);
}

.site-footer {
  margin-top: auto;
  border-top: 1px solid var(--line);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
}

.brand-mark {
  width: 13px;
  height: 13px;
  border: 3px solid var(--green);
  border-right-color: transparent;
}

.brand-index {
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 10px;
  font-weight: 400;
}

.status-line {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--green);
  box-shadow: 0 0 10px rgba(183, 243, 75, 0.6);
}

.status-line.is-offline .status-dot {
  background: var(--coral);
  box-shadow: none;
}

.game-layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 330px;
  gap: clamp(34px, 5vw, 78px);
  align-items: start;
  padding: clamp(28px, 5vh, 60px) 0;
}

.play-area {
  min-width: 0;
}

.score-strip {
  min-height: 68px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 16px;
}

.score-group {
  display: flex;
  align-items: stretch;
}

.metric {
  min-width: 92px;
  padding: 0 24px;
  border-left: 1px solid var(--line);
}

.metric:first-child {
  padding-left: 0;
  border-left: 0;
}

.metric-label {
  display: block;
  margin-bottom: 7px;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 10px;
}

.metric-value {
  display: block;
  color: var(--text);
  font-family: var(--font-mono);
  font-size: 23px;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  line-height: 1;
}

.metric-primary .metric-value {
  color: var(--green);
}

.game-actions {
  display: flex;
  gap: 8px;
}

.icon-button {
  width: 42px;
  height: 42px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 auto;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 16px;
  line-height: 1;
  transition: background-color 150ms ease, border-color 150ms ease, color 150ms ease;
}

.icon-button:hover:not(:disabled) {
  border-color: var(--green-soft);
  background: rgba(183, 243, 75, 0.08);
  color: var(--green);
}

.icon-button:disabled {
  cursor: not-allowed;
  opacity: 0.35;
}

.icon-button.compact {
  width: 36px;
  height: 36px;
  font-size: 15px;
}

.icon-button.is-loading span {
  animation: spin 700ms linear infinite;
}

.music-button {
  position: relative;
}

.music-button.is-muted {
  color: var(--muted);
}

.music-button.is-muted::after {
  content: "";
  position: absolute;
  width: 19px;
  height: 1px;
  background: currentColor;
  transform: rotate(-45deg);
  pointer-events: none;
}

.board-shell {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
  background: var(--surface);
  box-shadow: 0 26px 80px rgba(0, 0, 0, 0.28);
  isolation: isolate;
}

.board-shell::before,
.board-shell::after {
  content: "";
  position: absolute;
  z-index: 4;
  width: 16px;
  height: 16px;
  pointer-events: none;
}

.board-shell::before {
  top: 8px;
  left: 8px;
  border-top: 1px solid rgba(183, 243, 75, 0.75);
  border-left: 1px solid rgba(183, 243, 75, 0.75);
}

.board-shell::after {
  right: 8px;
  bottom: 8px;
  border-right: 1px solid rgba(183, 243, 75, 0.75);
  border-bottom: 1px solid rgba(183, 243, 75, 0.75);
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.board-overlay {
  position: absolute;
  inset: 0;
  z-index: 3;
  display: grid;
  place-items: center;
  padding: 28px;
  background: rgba(10, 12, 11, 0.72);
  backdrop-filter: blur(5px);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 180ms ease, visibility 180ms ease;
}

.board-overlay.is-visible {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

.primary-button,
.secondary-button {
  min-height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  border-radius: 4px;
  padding: 0 22px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: transform 140ms ease, background-color 140ms ease, border-color 140ms ease;
}

.primary-button {
  border: 1px solid var(--green);
  background: var(--green);
  color: #12170c;
}

.primary-button:hover:not(:disabled) {
  background: #c6ff5c;
  transform: translateY(-1px);
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.55;
}

.secondary-button {
  border: 1px solid var(--line-strong);
  background: transparent;
  color: var(--text);
}

.secondary-button:hover {
  border-color: var(--muted);
  background: rgba(255, 255, 255, 0.04);
}

.play-icon {
  font-size: 11px;
}

.pause-copy {
  display: grid;
  gap: 8px;
  text-align: center;
}

.pause-copy strong {
  font-size: 24px;
}

.overlay-kicker,
.section-index {
  color: var(--green);
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
}

.score-form {
  width: min(380px, 100%);
  padding: 26px;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: rgba(16, 19, 16, 0.96);
}

.final-score {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 0 20px;
}

.final-score span {
  color: var(--muted);
  font-size: 12px;
}

.final-score strong {
  color: var(--green);
  font-family: var(--font-mono);
  font-size: 40px;
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.nickname-display {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 0 13px;
  border: 1px solid var(--line-strong);
  border-radius: 4px;
  background: var(--bg);
}

.nickname-display span {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 11px;
}

.nickname-display strong {
  min-width: 0;
  overflow: hidden;
  color: var(--green);
  font-size: 16px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.form-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 9px;
  margin-top: 12px;
}

.form-message {
  min-height: 16px;
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}

.form-message.is-error {
  color: var(--danger);
}

.leaderboard {
  min-width: 0;
  padding-top: 9px;
}

.leaderboard-header {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line-strong);
}

.leaderboard h2 {
  margin: 8px 0 0;
  font-size: 25px;
  line-height: 1;
}

.leaderboard-columns,
.leaderboard-entry {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 60px;
  align-items: center;
  gap: 8px;
}

.leaderboard-columns {
  padding: 15px 4px 9px;
  color: #687068;
  font-family: var(--font-mono);
  font-size: 9px;
}

.leaderboard-columns span:last-child,
.entry-score {
  text-align: right;
}

.leaderboard-list {
  min-height: 420px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.leaderboard-entry {
  min-height: 44px;
  padding: 0 4px;
  border-bottom: 1px solid var(--line);
  font-size: 12px;
}

.leaderboard-entry.is-current {
  background: rgba(183, 243, 75, 0.06);
}

.entry-rank,
.entry-score {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

.entry-rank {
  color: var(--muted);
}

.leaderboard-entry:nth-child(-n + 3) .entry-rank {
  color: var(--green);
}

.entry-name {
  overflow: hidden;
  color: #ced3cb;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.entry-score {
  color: var(--text);
  font-weight: 700;
}

.leaderboard-state {
  display: grid;
  min-height: 180px;
  place-items: center;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

.legend {
  display: flex;
  gap: 20px;
  margin-top: 22px;
  color: var(--muted);
  font-family: var(--font-mono);
  font-size: 9px;
}

.legend > span {
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.legend-swatch {
  width: 8px;
  height: 8px;
  display: inline-block;
}

.snake-swatch {
  background: var(--green);
}

.food-swatch {
  border-radius: 50%;
  background: var(--coral);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@media (max-width: 900px) {
  .app-shell {
    width: min(680px, calc(100% - 32px));
  }

  .game-layout {
    grid-template-columns: 1fr;
    gap: 54px;
  }

  .leaderboard {
    padding-top: 0;
  }

  .leaderboard-list {
    min-height: 0;
  }
}

@media (max-width: 540px) {
  .app-shell {
    width: 100%;
    padding-right: max(14px, env(safe-area-inset-right));
    padding-left: max(14px, env(safe-area-inset-left));
  }

  .site-header {
    min-height: 58px;
  }

  .site-footer {
    min-height: 54px;
  }

  .game-layout {
    gap: 42px;
    padding: 18px 0 34px;
  }

  .score-strip {
    min-height: 54px;
    margin-bottom: 10px;
  }

  .metric {
    min-width: 67px;
    padding: 0 12px;
  }

  .metric-label {
    margin-bottom: 6px;
    font-size: 9px;
  }

  .metric-value {
    font-size: 19px;
  }

  .metric-speed {
    display: none;
  }

  .icon-button {
    width: 40px;
    height: 40px;
  }

  .board-overlay {
    padding: 18px;
  }

  .score-form {
    padding: 20px;
  }

  .final-score {
    padding: 12px 0 16px;
  }

  .final-score strong {
    font-size: 34px;
  }

  .primary-button,
  .secondary-button {
    min-height: 44px;
    padding: 0 14px;
    font-size: 13px;
  }
}

@media (max-width: 365px) {
  .brand-index,
  .status-line span:last-child {
    display: none;
  }

  .metric {
    min-width: 61px;
    padding-right: 10px;
    padding-left: 10px;
  }

  .form-actions {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
