:root {
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --ink: #211c19;
  --muted: #77695c;
  --paper: #fff8e8;
  --panel: rgba(255, 252, 242, 0.92);
  --line: rgba(79, 52, 38, 0.16);
  --red: #e9493f;
  --green: #3b9461;
  --gold: #d89427;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  min-height: 100%;
  margin: 0;
  overflow: hidden;
  background:
    radial-gradient(circle at 20% 8%, rgba(255, 214, 126, 0.36), transparent 28rem),
    radial-gradient(circle at 80% 12%, rgba(99, 184, 120, 0.22), transparent 24rem),
    linear-gradient(180deg, #fcf5de 0%, #f1d99d 100%);
  color: var(--ink);
  font-family: Inter, ui-rounded, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

button {
  border: 0;
  border-radius: 14px;
  background: var(--red);
  color: white;
  font: inherit;
  font-weight: 800;
  min-height: 48px;
  padding: 0 12px;
  box-shadow: 0 8px 0 #a82c2a, 0 14px 28px rgba(82, 43, 34, 0.18);
  transform: translateY(0);
}

input {
  font: inherit;
}

button:active {
  transform: translateY(4px);
  box-shadow: 0 4px 0 #a82c2a, 0 8px 18px rgba(82, 43, 34, 0.16);
}

button:disabled {
  opacity: 0.48;
  filter: grayscale(0.35);
}

.app-shell {
  width: min(100vw, 520px);
  height: 100dvh;
  margin: 0 auto;
  padding: calc(10px + var(--safe-top)) calc(10px + var(--safe-right)) calc(10px + var(--safe-bottom)) calc(10px + var(--safe-left));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
}

.hud {
  display: grid;
  gap: 8px;
  z-index: 5;
}

.hud-top {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.hud-top div {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  padding: 7px 8px;
  text-align: center;
  box-shadow: 0 8px 24px rgba(88, 60, 37, 0.08);
}

.label {
  display: block;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.1;
}

.hud-top strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(17px, 5vw, 23px);
  line-height: 1;
}

.score-bump {
  animation: score-bump 320ms ease-out;
}

@keyframes score-bump {
  0% {
    transform: scale(1);
    color: var(--ink);
  }

  42% {
    transform: scale(1.18);
    color: #2f8d50;
  }

  100% {
    transform: scale(1);
    color: var(--ink);
  }
}

.playfield {
  position: relative;
  min-height: 0;
  border: 1px solid rgba(72, 48, 35, 0.18);
  border-radius: 18px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.82), rgba(255, 236, 176, 0.72)),
    repeating-linear-gradient(135deg, rgba(122, 92, 57, 0.05) 0 10px, transparent 10px 20px);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.5), 0 22px 46px rgba(102, 70, 40, 0.16);
}

.playfield.danger-shake {
  animation: danger-shake 420ms cubic-bezier(0.28, 0.84, 0.42, 1);
}

@keyframes danger-shake {
  0%,
  100% {
    transform: translate3d(0, 0, 0);
  }

  15% {
    transform: translate3d(-5px, 2px, 0);
  }

  32% {
    transform: translate3d(5px, -2px, 0);
  }

  49% {
    transform: translate3d(-4px, -1px, 0);
  }

  66% {
    transform: translate3d(3px, 1px, 0);
  }

  82% {
    transform: translate3d(-2px, 0, 0);
  }
}

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

.hud-bottom {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.hud-bottom button {
  min-width: 0;
  font-size: clamp(13px, 3.7vw, 16px);
}

.menu-panel {
  position: absolute;
  inset: 50% auto auto 50%;
  width: min(90%, 390px);
  transform: translate(-50%, -50%);
  border: 1px solid rgba(67, 45, 31, 0.14);
  border-radius: 24px;
  background:
    linear-gradient(180deg, rgba(255, 253, 244, 0.96), rgba(255, 241, 202, 0.94)),
    radial-gradient(circle at 50% 18%, rgba(255, 210, 91, 0.28), transparent 11rem);
  padding: 18px;
  text-align: center;
  box-shadow: 0 22px 48px rgba(74, 50, 30, 0.22);
  backdrop-filter: blur(18px);
  z-index: 10;
}

.menu-topline,
.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.menu-topline span {
  border-radius: 999px;
  background: rgba(59, 148, 97, 0.12);
  color: #2f774e;
  font-size: 13px;
  font-weight: 900;
  padding: 7px 11px;
}

.icon-button {
  min-height: 34px;
  border-radius: 999px;
  background: #fff6dc;
  color: #3b3430;
  font-size: 13px;
  padding: 0 13px;
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.12), 0 4px 0 rgba(120, 86, 48, 0.18);
}

.icon-button.dark {
  background: #3b3430;
  color: #fff8e8;
  box-shadow: 0 4px 0 #1d1917;
}

.mini-button {
  min-height: 34px;
  border-radius: 999px;
  background: #3b3430;
  color: #fff8e8;
  font-size: 13px;
  padding: 0 13px;
  box-shadow: 0 4px 0 #1d1917, 0 8px 18px rgba(82, 43, 34, 0.12);
}

.menu-hero {
  height: 150px;
  display: grid;
  place-items: center;
  margin-top: 4px;
}

.fruit-mascot {
  position: relative;
  width: 138px;
  height: 126px;
  border-radius: 46% 54% 50% 50%;
  background:
    radial-gradient(circle at 36% 30%, rgba(255, 255, 255, 0.76), transparent 1.8rem),
    linear-gradient(145deg, #ff7a6e 0%, #ef443e 56%, #c52c32 100%);
  box-shadow: inset -14px -18px 0 rgba(92, 24, 24, 0.16), 0 18px 30px rgba(118, 57, 34, 0.2);
}

.fruit-face::before,
.fruit-face::after {
  content: "";
  position: absolute;
  top: 58px;
  width: 9px;
  height: 13px;
  border-radius: 999px;
  background: #3b2420;
  z-index: 2;
}

.fruit-face::before {
  left: 48px;
}

.fruit-face::after {
  right: 48px;
}

.fruit-leaf {
  position: absolute;
  top: -21px;
  right: 32px;
  width: 48px;
  height: 30px;
  border-radius: 100% 0 100% 0;
  background: linear-gradient(135deg, #68c772, #2d8a4e);
  transform: rotate(-20deg);
}

.fruit-worm-peek {
  position: absolute;
  right: 9px;
  bottom: 18px;
  width: 34px;
  height: 34px;
  border: 5px solid #1f7436;
  border-radius: 999px;
  background: #5fc46b;
  box-shadow: 0 0 0 5px rgba(255, 255, 255, 0.72);
}

.player-card {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 8px;
  margin: 14px 0;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.64);
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.1);
  padding: 10px 12px;
  text-align: left;
}

.player-card span,
.player-card em {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
  font-weight: 800;
}

.player-card strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 17px;
}

.menu-actions {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 10px;
}

.dialog-panel {
  position: absolute;
  inset: auto auto 22px 50%;
  width: min(88%, 380px);
  transform: translateX(-50%);
  border: 1px solid rgba(67, 45, 31, 0.14);
  border-radius: 18px;
  background: rgba(255, 252, 242, 0.78);
  padding: 14px;
  text-align: center;
  box-shadow: 0 18px 40px rgba(74, 50, 30, 0.18);
  backdrop-filter: blur(18px);
  z-index: 10;
}

.danger-panel {
  border-color: rgba(221, 53, 48, 0.28);
  background: rgba(255, 247, 238, 0.88);
  box-shadow: 0 20px 44px rgba(142, 42, 30, 0.22);
}

.danger-panel h2 {
  color: #d73530;
}

.danger-copy {
  margin-top: -4px !important;
  color: #4b3b34 !important;
  font-weight: 900;
}

.sheet-panel {
  position: absolute;
  inset: auto 14px 14px 14px;
  border: 1px solid rgba(67, 45, 31, 0.14);
  border-radius: 22px;
  background: rgba(255, 252, 242, 0.94);
  padding: 16px;
  box-shadow: 0 22px 48px rgba(74, 50, 30, 0.22);
  backdrop-filter: blur(18px);
  z-index: 16;
}

.modal-scrim {
  position: absolute;
  inset: 0;
  background: rgba(39, 29, 22, 0.2);
  backdrop-filter: blur(3px);
  z-index: 17;
}

.name-panel {
  inset: 50% auto auto 50%;
  width: min(88%, 360px);
  transform: translate(-50%, -50%);
  z-index: 18;
}

.name-field {
  display: grid;
  gap: 7px;
  margin: 10px 0 6px;
  text-align: left;
  font-weight: 900;
}

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

.name-field input {
  width: 100%;
  border: 0;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.84);
  color: var(--ink);
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.16);
  min-height: 50px;
  padding: 0 13px;
  font-size: 19px;
  font-weight: 900;
  outline: none;
}

.name-field input:focus {
  box-shadow: inset 0 0 0 2px rgba(59, 148, 97, 0.46), 0 0 0 4px rgba(59, 148, 97, 0.12);
}

.name-error {
  min-height: 19px;
  margin: 4px 0 10px !important;
  color: #cf342f !important;
  font-size: 13px !important;
  font-weight: 900;
}

.name-actions {
  grid-template-columns: 1.15fr 0.85fr;
}

.sheet-panel h2 {
  margin: 0;
  font-size: 28px;
}

.settings-list {
  display: grid;
  gap: 10px;
  margin: 16px 0 12px;
}

.settings-list label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 50px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.64);
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.1);
  padding: 0 14px;
  font-weight: 900;
}

.settings-list input {
  width: 24px;
  height: 24px;
  accent-color: var(--green);
}

.settings-list .range-setting {
  display: grid;
  grid-template-columns: auto minmax(96px, 1fr) 44px;
  gap: 10px;
}

.range-setting input {
  width: 100%;
  height: 24px;
}

.range-setting em {
  color: var(--muted);
  font-style: normal;
  text-align: right;
}

.secondary-button {
  width: 100%;
  margin-top: 10px;
  background: #fff6dc;
  color: #3b3430;
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.12), 0 5px 0 rgba(120, 86, 48, 0.2);
}

.placeholder-copy {
  color: var(--muted);
  font-weight: 700;
  line-height: 1.45;
}

.leaderboard-panel {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  max-height: min(78dvh, 620px);
}

.leaderboard-summary {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 14px 0 10px;
}

.leaderboard-summary div {
  min-width: 0;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.68);
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.1);
  padding: 10px 12px;
}

.leaderboard-summary span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

.leaderboard-summary strong {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-top: 2px;
  font-size: 20px;
}

.leaderboard-toolbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.leaderboard-toolbar p {
  min-width: 0;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-list {
  min-height: 210px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: grid;
  align-content: start;
  gap: 8px;
  padding-right: 2px;
}

.leaderboard-row,
.leaderboard-empty {
  display: grid;
  align-items: center;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.64);
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.1);
}

.leaderboard-row {
  grid-template-columns: 34px minmax(0, 1fr) auto;
  gap: 9px;
  min-height: 62px;
  padding: 0 12px;
}

.leaderboard-row.is-me {
  background: rgba(255, 246, 205, 0.92);
  box-shadow: inset 0 0 0 2px rgba(216, 148, 39, 0.26);
}

.leaderboard-rank {
  display: grid;
  place-items: center;
  width: 31px;
  height: 31px;
  border-radius: 999px;
  background: var(--gold);
  color: white;
  font-weight: 900;
}

.leaderboard-player {
  min-width: 0;
}

.leaderboard-player strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 15px;
}

.leaderboard-player span,
.leaderboard-score span {
  display: block;
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.leaderboard-score {
  text-align: right;
  white-space: nowrap;
}

.leaderboard-score strong {
  display: block;
  font-size: 18px;
}

.leaderboard-empty {
  min-height: 132px;
  place-items: center;
  color: var(--muted);
  font-weight: 900;
  text-align: center;
  padding: 16px;
}

.leaderboard-result {
  margin: 8px 0 10px !important;
  color: #2f774e !important;
  font-weight: 900;
}

.end-actions {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

.end-actions button {
  min-width: 0;
  font-size: clamp(13px, 3.5vw, 16px);
}

.end-actions button:nth-child(2) {
  background: var(--gold);
  box-shadow: 0 8px 0 #99631b, 0 14px 28px rgba(82, 43, 34, 0.18);
}

.menu-panel h1,
.dialog-panel h2 {
  margin: 0;
  font-size: clamp(26px, 7vw, 38px);
  letter-spacing: 0;
}

.main-menu h1 {
  font-size: clamp(36px, 10vw, 48px);
  line-height: 0.96;
}

.menu-panel p,
.dialog-panel p {
  margin: 8px 0 12px;
  color: var(--muted);
  font-size: 15px;
}

.menu-actions button {
  background: var(--green);
  box-shadow: 0 8px 0 #23683d, 0 14px 28px rgba(82, 43, 34, 0.18);
}

.menu-actions button:last-child {
  background: var(--gold);
  box-shadow: 0 8px 0 #99631b, 0 14px 28px rgba(82, 43, 34, 0.18);
}

.dialog-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.dialog-actions button:last-child {
  background: #3b3430;
  box-shadow: 0 8px 0 #1d1917, 0 14px 28px rgba(82, 43, 34, 0.18);
}

.card-count-pop {
  animation: card-count-pop 420ms ease-out;
}

@keyframes card-count-pop {
  0% {
    transform: scale(1);
  }

  40% {
    transform: scale(1.1) rotate(-2deg);
    filter: brightness(1.08);
  }

  100% {
    transform: scale(1);
  }
}

.end-stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin: 12px 0;
}

.end-stats div {
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.62);
  box-shadow: inset 0 0 0 1px rgba(92, 64, 42, 0.1);
  padding: 8px;
}

.end-stats span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.end-stats strong {
  display: block;
  margin-top: 2px;
  font-size: 20px;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 18px;
  max-width: calc(100% - 32px);
  transform: translateX(-50%) translateY(12px);
  border-radius: 999px;
  background: rgba(32, 24, 20, 0.84);
  color: white;
  font-size: 14px;
  font-weight: 700;
  padding: 10px 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 180ms ease, transform 180ms ease;
  z-index: 20;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

@media (max-height: 680px) {
  .app-shell {
    gap: 7px;
    padding-top: calc(7px + var(--safe-top));
    padding-bottom: calc(7px + var(--safe-bottom));
  }

  .hud-top div {
    padding: 5px 6px;
  }

  .menu-hero {
    height: 112px;
  }

  .fruit-mascot {
    width: 108px;
    height: 98px;
  }

  button {
    min-height: 42px;
    border-radius: 12px;
  }
}
