:root {
  color-scheme: dark;
  --ink: #f7ecd9;
  --muted: #c6b49e;
  --dark: #1c1713;
  --amber: #ffc255;
  --cyan: #8cdee8;
  font-family: system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html,
body {
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--dark);
  color: var(--ink);
}

button {
  color: inherit;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 750;
  line-height: 1.1;
  cursor: pointer;
  touch-action: manipulation;
}

button:focus-visible { outline: 2px solid #fff0bd; outline-offset: 3px; }
button:disabled { cursor: default; }

.game {
  --hud-top: max(0.7rem, calc(env(safe-area-inset-top) + 0.28rem));
  --hud-bottom: max(0.7rem, calc(env(safe-area-inset-bottom) + 0.36rem));
  --hud-left: max(clamp(0.55rem, 1.55vw, 1.35rem), calc(env(safe-area-inset-left) + 0.4rem));
  --hud-right: max(clamp(0.55rem, 1.55vw, 1.35rem), calc(env(safe-area-inset-right) + 0.4rem));
  --cue-top: calc(var(--hud-top) + 56px);
  position: relative;
  isolation: isolate;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: #3d5731;
}

.game:fullscreen { width: 100%; height: 100%; }
.game:-webkit-full-screen { width: 100%; height: 100%; }

.game::before,
.game::after {
  content: "";
  position: absolute;
  z-index: 1;
  inset-inline: 0;
  pointer-events: none;
}

.game::before {
  inset-block-start: 0;
  height: calc(var(--cue-top) + 1.8rem);
  background: linear-gradient(#101811c2 0%, #10181155 46%, transparent);
}

.game::after {
  inset-block-end: 0;
  height: calc(var(--hud-bottom) + 7.5rem);
  background: linear-gradient(transparent, #11150db8);
}

.game-header,
.scoreboard,
.controls {
  position: absolute;
  z-index: 2;
  pointer-events: none;
}

.game-header {
  top: var(--hud-top);
  left: var(--hud-left);
  right: var(--hud-right);
  display: flex;
  align-items: center;
  gap: 0.55rem;
}
.game-header img { width: 2.35rem; height: 2.35rem; object-fit: contain; }
.game-header h1 {
  margin: 0;
  font-size: clamp(1rem, 2.2vw, 1.65rem);
  font-weight: 950;
  line-height: 1;
  letter-spacing: -0.045em;
  text-shadow: 0 1px 5px #0a0e09;
  text-transform: uppercase;
  white-space: nowrap;
}

.pause,
.fullscreen {
  min-height: 44px;
  border: 1px solid #baa48391;
  border-radius: 0.4rem;
  background: #211a14e8;
  color: var(--ink);
  pointer-events: auto;
}
.pause { margin-left: auto; padding: 0 0.78rem; }
.pause:disabled { opacity: 0.55; }
.fullscreen { display: grid; place-content: center; min-width: 44px; padding: 0; }
.fullscreen-mark {
  width: 17px;
  height: 17px;
  background:
    linear-gradient(currentColor 0 0) left top / 7px 2px,
    linear-gradient(currentColor 0 0) left top / 2px 7px,
    linear-gradient(currentColor 0 0) right top / 7px 2px,
    linear-gradient(currentColor 0 0) right top / 2px 7px,
    linear-gradient(currentColor 0 0) left bottom / 7px 2px,
    linear-gradient(currentColor 0 0) left bottom / 2px 7px,
    linear-gradient(currentColor 0 0) right bottom / 7px 2px,
    linear-gradient(currentColor 0 0) right bottom / 2px 7px;
  background-repeat: no-repeat;
}
.fullscreen[data-active] .fullscreen-mark { transform: scale(0.78); color: var(--amber); }

.scoreboard {
  top: var(--hud-top);
  left: 50%;
  transform: translateX(-50%);
  width: min(24rem, calc(100% - 20rem));
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  gap: 0.3rem;
  align-items: stretch;
  font-size: 0.79rem;
  font-weight: 800;
  text-transform: uppercase;
}
.team, .match-format { display: flex; align-items: center; justify-content: center; }
.team {
  gap: 0.46rem;
  min-height: 2.3rem;
  border: 1px solid currentColor;
  border-radius: 0.4rem;
}
.team-you { color: var(--amber); background: #332515ee; }
.team-cpu { color: var(--cyan); background: #203139ee; }
.team output { font-size: 1.18rem; line-height: 1; font-variant-numeric: tabular-nums; }
.match-format {
  flex-direction: column;
  gap: 0.08rem;
  padding-inline: 0.52rem;
  border-radius: 0.3rem;
  background: #221b15e0;
  color: var(--ink);
  white-space: nowrap;
}
#match-clock { font-size: 0.94rem; line-height: 1; font-variant-numeric: tabular-nums; }
#match-clock[data-ending] { color: var(--amber); }

.arena {
  position: absolute;
  inset: 0;
  z-index: 0;
  overflow: hidden;
}
.arena canvas { display: block; width: 100%; height: 100%; touch-action: none; }
.game[data-phase="playing"] .arena canvas { cursor: grab; }
.game .arena canvas[data-looking] { cursor: grabbing; }
.center-view {
  position: absolute;
  top: var(--cue-top);
  right: var(--hud-right);
  min-height: 44px;
  padding: 0.42rem 0.57rem;
  border: 1px solid #b08055;
  border-radius: 0.38rem;
  color: var(--ink);
  background: #271b12ed;
  font-size: 0.75rem;
}
.ball-direction,
.cell-direction {
  position: absolute;
  top: var(--cue-top);
  padding: 0.22rem 0.58rem;
  border: 1px solid;
  border-radius: 99rem;
  font-size: 0.85rem;
  font-weight: 750;
  white-space: nowrap;
  pointer-events: none;
}
.ball-direction {
  left: 50%;
  transform: translateX(-50%);
  border-color: #b08055;
  color: var(--ink);
  background: #271b12ed;
}
.cell-direction {
  left: var(--hud-left);
  border-color: #a6d575;
  color: #dfffac;
  background: #182812ed;
}

.kickoff {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 0.8rem;
  overflow: auto;
  background: #20150d47;
}
.game[data-phase="resume"] .kickoff { align-items: safe center; padding-block: max(3.5rem, var(--cue-top)) 0.8rem; }
.game[data-phase="resume"] .kickoff-card { width: min(100%, 30rem); }
.kickoff-card {
  text-align: center;
  width: min(100%, 18rem);
  padding: 1.15rem 0.9rem;
  border: 1px solid #b27e4e;
  border-radius: 0.54rem;
  background: #23190ff0;
  box-shadow: 0 7px 24px #150d08b8;
}
.kickoff h2 { font-size: clamp(1.25rem, 5vw, 1.8rem); margin: 0 0 0.33rem; line-height: 1.1; }
.kickoff p { margin: 0 0 0.85rem; color: var(--muted); font-size: 1rem; line-height: 1.4; }
.play {
  padding: 0.72rem 1.35rem;
  min-height: 44px;
  border: 1px solid #ffe1a3;
  border-radius: 0.32rem;
  background: var(--amber);
  color: #23180b;
  font-size: 1rem;
}
.play:hover { background: #ffd682; }
.kickoff-cue {
  position: absolute;
  inset: calc(var(--cue-top) + clamp(0.72rem, 4vh, 2.35rem)) 0 auto;
  width: min(17.5rem, 76%);
  margin: 0 auto;
  padding: 0.58rem 0.65rem 0.66rem;
  border-block: 2px solid currentColor;
  color: var(--amber);
  background: linear-gradient(100deg, #23170bed, #171a16f0 58%, #23170bed);
  box-shadow: 0 0.7rem 2rem #1009058c;
  text-align: center;
  text-transform: uppercase;
  pointer-events: none;
}
.kickoff-note, .kickoff-instruction {
  margin: 0;
  color: #f5e9d2;
  font-weight: 800;
  font-size: clamp(0.62rem, 2.2vw, 0.78rem);
  letter-spacing: 0.13em;
}
.kickoff-instruction:empty { display: none; }
.kickoff-count {
  margin: 0.05em 0 0.07em;
  font-size: clamp(2.9rem, 11vw, 5.6rem);
  font-weight: 950;
  font-variant-numeric: tabular-nums;
  line-height: 0.96;
  text-shadow: 0 3px 0 #2c1e10, 0 0 1rem #e9a14266;
}
.kickoff-cue[data-stage="earthwork"] { width: min(14rem, 70%); color: #b5e6a0; }
.kickoff-cue[data-stage="earthwork"] .kickoff-count { font-size: clamp(1.6rem, 6vw, 2.6rem); }
.kickoff-cue[data-stage="go"] { color: #c4f4ac; background: #192917e8; }
.kickoff-cue[data-stage="go"] .kickoff-count { text-shadow: 0 3px 0 #213b1f, 0 0 1rem #92d86a7a; }
.goal-banner {
  position: absolute;
  inset: calc(var(--cue-top) + 0.65rem) 0 auto;
  width: min(35rem, 66%);
  margin-inline: auto;
  pointer-events: none;
  margin-block: 0;
  padding: 0.55rem 0.25rem;
  text-align: center;
  font-size: clamp(1.45rem, 6vw, 2rem);
  font-weight: 950;
  text-transform: uppercase;
  background: #24170be8;
  color: var(--amber);
  border-block: 2px solid currentColor;
  box-shadow: 0 0 1.4rem color-mix(in srgb, currentColor 28%, transparent);
  animation: goal-reveal 440ms cubic-bezier(0.18, 0.85, 0.29, 1) both;
}
.goal-banner[data-team="cpu"] { color: var(--cyan); }
.goal-replay {
  position: absolute;
  top: calc(var(--cue-top) + 0.5rem);
  left: 50%;
  width: max-content;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.7rem;
  padding: 0.32rem 0.4rem 0.32rem 0.68rem;
  border: 1px solid #d4a752;
  border-radius: 0.28rem;
  background: #21190ff2;
  color: var(--amber);
  font-size: 0.76rem;
  font-weight: 900;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}
#skip-replay { min-height: 40px; padding-inline: 0.5rem; border: 1px solid #80633e; border-radius: 0.23rem; background: #392b1d; font-size: 0.73rem; }
.game[data-phase="replay"] .controls { display: none; }

@keyframes goal-reveal {
  from { opacity: 0; transform: scale(0.9) translateY(0.35rem); }
  54% { opacity: 1; transform: scale(1.045) translateY(0); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  .goal-banner { animation: none; }
}

.controls {
  right: var(--hud-right);
  bottom: var(--hud-bottom);
  left: var(--hud-left);
  display: grid;
  align-items: center;
  grid-template-columns: auto minmax(2.2rem, 10rem) 1fr;
  gap: clamp(0.37rem, 1.2vw, 0.78rem);
}
.boost-readout {
  min-width: 0;
  padding: 0.4rem 0.55rem;
  border: 1px solid #aab18b65;
  border-radius: 0.4rem;
  background: #211a14dc;
  color: var(--ink);
  font-size: 0.8125rem;
  font-weight: 750;
}
.boost-readout div { display: flex; justify-content: space-between; gap: 0.25rem; }
.boost-readout output { color: #b6eca6; font-variant-numeric: tabular-nums; }
.boost-readout meter { width: 100%; height: 0.85rem; display: block; }
.boost-readout meter::-webkit-meter-bar { background: #191c14; border: 1px solid #687051; border-radius: 0.25rem; }
.boost-readout meter::-webkit-meter-optimum-value { background: #9ed87e; border-radius: 0.2rem; }
.boost-readout meter::-moz-meter-bar { background: #9ed87e; }
.control-buttons { display: flex; justify-self: end; gap: clamp(0.2rem, 1vw, 0.4rem); }
.earth-controls {
  position: absolute;
  inset: auto 0 calc(100% + 0.34rem);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.35rem;
}
.earth-load { display: flex; align-items: center; gap: 0.32rem; }
#soil-status {
  padding: 0.26rem 0.45rem;
  border: 1px solid #b1835078;
  border-radius: 0.35rem;
  background: #241c16e8;
  color: #ffe1a9;
  font-size: 0.75rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.game:not([data-input="touch"]) .controls { grid-template-columns: minmax(6rem, 9rem); }
.game:not([data-input="touch"]) .controls :is(.stick, .control-buttons, #scoop, #dump),
.controls kbd { display: none; }
.gear-button {
  display: grid;
  place-content: center;
  min-width: 3rem;
  min-height: 49px;
  padding-inline: 0.48rem;
  border-radius: 0.37rem;
  border: 1px solid #b18350;
  background: #3c2d1e;
  color: var(--ink);
  pointer-events: auto;
  touch-action: none;
  user-select: none;
}
#boost { background: #2f3d23; border-color: #788759; }
.earth-controls .gear-button { min-height: 44px; grid-auto-flow: column; gap: 0.32rem; align-items: center; }
#switch-rig { background: #3a2b1c; border-color: #c8914d; }
#switch-rig::before { content: '↔'; color: var(--amber); }
#push[aria-pressed="true"] { background: #e2a45f; color: #27190f; border-color: #ffe0ac; }
#dump { background: #ffd082; color: #291708; border-color: #ffe4ae; padding-inline: 0.65rem; }
.earth-controls .gear-button[hidden] { display: none; }
#dump[data-dumping] { opacity: 0.8; }
.earth-controls .gear-button kbd { margin: 0; }
#hop { background: #2b343b; border-color: #788e9a; }
#jump { background: #263947; border-color: #7998ab; }
.gear-button[data-held] { background: var(--amber); color: #23180c; }
#boost[data-held] { background: #addd80; color: #1d2a13; }
#hop[data-held], #jump[data-held] { background: #b6e7f4; color: #202b3c; }
.gear-button:disabled, .gear-button[data-empty] { opacity: 0.55; }
.gear-button kbd { margin-top: 0.15rem; font: 600 0.7rem/1.15 system-ui, sans-serif; opacity: 0.8; }

.stick {
  position: relative;
  flex: 0 0 56px;
  width: 56px;
  height: 56px;
  border: 1px solid #a3734d;
  border-radius: 50%;
  background: radial-gradient(circle, #62462d 15%, #302317 75%);
  box-shadow: inset 0 0 0 3px #211711;
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  cursor: grab;
}
.stick[data-active] { border-color: var(--amber); cursor: grabbing; }
.stick span {
  position: absolute;
  top: 50%; left: 50%;
  width: 26px; height: 26px;
  border-radius: 50%;
  border: 1px solid #ffdb91;
  background: radial-gradient(circle at 41% 32%, #ffc96d, #aa6226);
  transform: translate(-50%, -50%) translate(var(--stick-x, 0px), var(--stick-y, 0px));
  pointer-events: none;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

@media (max-width: 700px) {
  .game { --cue-top: calc(var(--hud-top) + 87px); }
  .game-header img { width: 2rem; height: 2rem; }
  .scoreboard {
    top: calc(var(--hud-top) + 48px);
    width: min(21rem, calc(100% - var(--hud-left) - var(--hud-right)));
    font-size: 0.73rem;
  }
  .team { min-height: 2rem; }
  .cell-direction { top: calc(var(--cue-top) + 1.9rem); }
}

@media (max-width: 420px) {
  .controls { grid-template-columns: auto minmax(2.2rem, 1fr) auto; gap: 0.32rem; }
  .controls .stick { width: clamp(44px, 13vw, 56px); height: clamp(44px, 13vw, 56px); }
  .gear-button { min-width: 44px; min-height: 44px; padding-inline: 0.28rem; font-size: 0.77rem; }
  .boost-readout { padding: 0.28rem 0.24rem; font-size: 0.72rem; }
  .boost-readout div { display: grid; justify-content: center; gap: 0; text-align: center; line-height: 1.15; }
  #switch-rig { white-space: nowrap; }
}

@media (max-width: 350px) {
  .earth-load:has(#dump:not([hidden])) #soil-status { position: absolute; right: 0; bottom: calc(100% + 0.25rem); }
}

@media (max-width: 300px) {
  .game-header img { width: 1.55rem; height: 1.55rem; }
  .game-header { gap: 0.36rem; }
  .pause { padding-inline: 0.48rem; }
  .control-buttons { display: grid; grid-template-columns: repeat(2, 1fr); }
}

@media (max-height: 470px) and (min-width: 581px) {
  .game-header img { width: 1.8rem; height: 1.8rem; }
  .game-header h1 { font-size: 1.15rem; }
  .controls .stick { width: 50px; height: 50px; }
  .gear-button { min-height: 44px; }
}
