:root {
  --ink: #111d14;
  --ink-soft: #2f3c33;
  --paper: #fbf2dc;
  --paper-deep: #eadfc5;
  --field: #f8f3e4;
  --line: #cfc2a4;
  --green: #2ecb69;
  --green-dark: #0f7339;
  --green-soft: #d7f5df;
  --purple: #9c55c8;
  --purple-dark: #653286;
  --purple-soft: #ead8f2;
  --yellow: #f2c94c;
  --orange: #f06a3c;
  --blue: #2479b8;
  --red: #d84f49;
  --radius: 8px;
  --shadow-hard: 10px 10px 0 #111d14;
  --shadow-soft: 0 22px 80px rgba(24, 29, 18, 0.18);
  color: var(--ink);
  background: var(--paper);
  font-family: "Avenir Next Condensed", "Trebuchet MS", "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  overflow-x: hidden;
  background:
    radial-gradient(circle at 12% 18%, rgba(46, 203, 105, 0.22) 0 90px, transparent 91px),
    radial-gradient(circle at 88% 82%, rgba(156, 85, 200, 0.18) 0 110px, transparent 111px),
    linear-gradient(135deg, rgba(17, 29, 20, 0.1) 25%, transparent 25%) 0 0 / 18px 18px,
    var(--paper);
}

button,
summary {
  font: inherit;
}

button:focus-visible,
summary:focus-visible {
  outline: 3px solid var(--yellow);
  outline-offset: 3px;
}

.game-shell {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(320px, 380px);
  grid-template-areas:
    "arena builder"
    "arena rank";
  grid-template-rows: auto 1fr;
  gap: clamp(12px, 1.8vw, 24px);
  width: min(1680px, calc(100vw - 24px));
  min-height: calc(100vh - 24px);
  margin: 12px auto;
}

.arena-panel { grid-area: arena; }
.gene-panel { grid-area: builder; }
.rank-panel { grid-area: rank; }

@media (min-width: 1400px) {
  .game-shell {
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr) minmax(320px, 380px);
    grid-template-areas: "rank arena builder";
    grid-template-rows: auto;
  }
}

.arena-panel,
.gene-panel,
.rank-panel {
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  background: rgba(251, 242, 220, 0.94);
  box-shadow: var(--shadow-soft);
}

.rank-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: clamp(16px, 1.8vw, 22px);
  min-width: 0;
}

.arena-panel {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  gap: 12px;
  padding: clamp(14px, 1.7vw, 22px);
  min-width: 0;
}

.gene-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: clamp(16px, 1.8vw, 22px);
  min-width: 0;
}

.top-hud,
.domination-row,
.section-heading,
.control-panel {
  display: flex;
  align-items: center;
}

.top-hud,
.domination-row,
.section-heading {
  justify-content: space-between;
  gap: 14px;
}

.title-block,
.panel-intro {
  min-width: 0;
}

.eyebrow {
  margin: 0 0 6px;
  color: var(--ink-soft);
  font-size: 0.73rem;
  font-weight: 900;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2.25rem, 4vw, 4.05rem);
  line-height: 0.86;
  text-transform: uppercase;
}

.panel-intro h2 {
  font-size: clamp(2rem, 4vw, 3.2rem);
  line-height: 0.9;
  text-transform: uppercase;
}

.panel-intro p:not(.eyebrow),
.coach-text {
  color: var(--ink-soft);
  font-size: 1rem;
  font-weight: 900;
  line-height: 1.25;
}

.coach-text {
  margin-top: 10px;
  max-width: 42ch;
  color: #31533a;
}

.match-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(74px, 1fr));
  gap: 8px;
  min-width: 180px;
}

.match-stats div {
  display: grid;
  gap: 2px;
  min-width: 0;
  padding: 9px 10px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  background: #fff8e8;
}

.stat-label {
  color: var(--ink-soft);
  font-size: 0.65rem;
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.match-stats strong {
  font-size: clamp(1.1rem, 2vw, 1.45rem);
  line-height: 1;
}

.domination-board {
  display: grid;
  gap: 8px;
}

.matchup-strip {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  background: #fff8e8;
  font-size: 0.86rem;
  font-weight: 1000;
  text-transform: uppercase;
}

.matchup-strip span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.matchup-strip span:last-child {
  text-align: right;
}

.team-tag {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-width: max-content;
  font-size: 0.92rem;
  font-weight: 1000;
  text-transform: uppercase;
}

.team-tag.green { color: var(--green-dark); }
.team-tag.purple { color: var(--purple-dark); }

.status-text {
  overflow: hidden;
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-weight: 1000;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.domination-track {
  display: flex;
  overflow: hidden;
  height: 24px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--paper-deep);
}

.green-bar,
.purple-bar,
.neutral-bar {
  display: block;
  width: 0%;
  transition: width 140ms linear;
}

.green-bar {
  background: repeating-linear-gradient(90deg, var(--green), var(--green) 14px, #63df8f 14px, #63df8f 22px);
}

.purple-bar {
  background: repeating-linear-gradient(90deg, var(--purple), var(--purple) 14px, #bb84dc 14px, #bb84dc 22px);
}

.neutral-bar {
  background: var(--paper-deep);
}

.canvas-stage {
  position: relative;
  display: grid;
  justify-items: center;
  align-items: start;
  min-height: 0;
  padding: clamp(6px, 1vw, 12px);
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  background:
    linear-gradient(90deg, rgba(17, 29, 20, 0.08) 1px, transparent 1px),
    linear-gradient(0deg, rgba(17, 29, 20, 0.08) 1px, transparent 1px),
    #efe4c7;
  background-size: 24px 24px;
  box-shadow: var(--shadow-hard);
}

#arenaCanvas {
  display: block;
  width: min(100%, calc(100vh - 280px));
  max-width: 100%;
  min-width: 280px;
  aspect-ratio: 1;
  border: 2px solid var(--ink);
  border-radius: var(--radius);
  background: var(--field);
}

.result-overlay {
  position: absolute;
  inset: clamp(6px, 1vw, 12px);
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  background: rgba(17, 29, 20, 0.45);
  pointer-events: none;
  opacity: 0;
  transition: opacity 200ms ease;
}

.result-overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.result-card {
  display: grid;
  justify-items: center;
  gap: 6px;
  padding: clamp(20px, 3vw, 32px) clamp(24px, 4vw, 44px);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  background: #fff8e8;
  box-shadow: var(--shadow-hard);
  text-align: center;
  min-width: min(90%, 320px);
  animation: pop-in 280ms cubic-bezier(0.2, 1.2, 0.4, 1);
}

@keyframes pop-in {
  from { transform: translateY(20px) scale(0.92); opacity: 0; }
  to   { transform: translateY(0) scale(1);    opacity: 1; }
}

.result-badge {
  padding: 6px 14px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--paper-deep);
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 1000;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.result-badge[data-outcome="win"]  { background: var(--green); color: #fff8e8; }
.result-badge[data-outcome="loss"] { background: var(--purple); color: #fff8e8; }
.result-badge[data-outcome="draw"] { background: var(--yellow); }

.result-delta {
  font-size: clamp(3.4rem, 9vw, 5.6rem);
  line-height: 0.9;
  letter-spacing: -0.03em;
  text-transform: uppercase;
  animation: bump 350ms cubic-bezier(0.2, 1.2, 0.4, 1) 60ms both;
}

.result-delta[data-outcome="win"]  { color: var(--green-dark); }
.result-delta[data-outcome="loss"] { color: var(--purple-dark); }
.result-delta[data-outcome="draw"] { color: var(--ink); }

@keyframes bump {
  0%   { transform: scale(0.4); opacity: 0; }
  60%  { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}

.result-elo-label {
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-weight: 1000;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}

.result-detail {
  max-width: 32ch;
  color: var(--ink-soft);
  font-size: 0.95rem;
  font-weight: 900;
  line-height: 1.25;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  width: 100%;
  margin-top: 4px;
}

.panel-section,
.player-card,
.leaderboard-panel {
  display: grid;
  gap: 9px;
}

.section-heading h3 {
  font-size: 1.05rem;
  line-height: 1;
  text-transform: uppercase;
}

.pill {
  display: inline-flex;
  align-items: center;
  min-height: 26px;
  padding: 0 9px;
  border: 2px solid var(--ink);
  border-radius: 999px;
  color: var(--ink);
  background: #fff8e8;
  font-size: 0.74rem;
  font-weight: 1000;
}

.pill[data-state="ok"]    { background: var(--green); color: #fff8e8; }
.pill[data-state="under"] { background: var(--yellow); }
.pill[data-state="over"]  { background: var(--red); color: #fff8e8; }

.field-label {
  color: var(--ink-soft);
  font-size: 0.72rem;
  font-weight: 1000;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.input-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
}

input {
  width: 100%;
  min-height: 44px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  padding: 0 10px;
  color: var(--ink);
  background: #fff8e8;
  font: inherit;
  font-weight: 900;
}

input::placeholder {
  color: rgba(17, 29, 20, 0.42);
}

.input-row .secondary-action {
  min-width: 78px;
  padding: 0 12px;
  min-height: 44px;
}

.claim-banner {
  margin-top: 10px;
  padding: 10px 12px;
  border: 2px dashed var(--ink);
  border-radius: 9px;
  background: #fff1b8;
  display: grid;
  gap: 6px;
}

.claim-banner strong {
  font-size: 0.92rem;
  letter-spacing: 0.02em;
}

.claim-banner p {
  margin: 0;
  font-size: 0.78rem;
  color: var(--ink-soft);
  font-weight: 800;
}

.trait-hint {
  margin: -2px 0 4px;
  color: var(--ink-soft);
  font-size: 0.78rem;
  font-weight: 1000;
  letter-spacing: 0.02em;
}

.command-palette {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.command-button {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  min-height: 50px;
  padding: 6px 8px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  background: #fff8e8;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.command-button:hover:not(:disabled) {
  transform: translate(-2px, -2px);
  box-shadow: 4px 4px 0 var(--ink);
}

.command-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
  box-shadow: none;
}

.command-code {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 2px solid var(--ink);
  border-radius: 6px;
  color: #fff8e8;
  font-size: 0.95rem;
  font-weight: 1000;
  letter-spacing: 0.04em;
}

.command-copy {
  min-width: 0;
}

.command-copy strong {
  display: block;
  font-size: 0.85rem;
  line-height: 1;
  text-transform: uppercase;
}

.command-copy span {
  display: block;
  margin-top: 3px;
  color: var(--ink-soft);
  font-size: 0.66rem;
  font-weight: 1000;
  line-height: 1.1;
}

.genome-slots {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px;
}

.slot-button {
  display: grid;
  grid-template-columns: 22px 30px minmax(0, 1fr);
  align-items: center;
  gap: 7px;
  min-height: 38px;
  padding: 4px 7px;
  border: 2px solid var(--line);
  border-radius: 7px;
  background: #fff8e8;
  color: var(--ink);
  cursor: pointer;
  text-align: left;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.slot-button:hover:not(:disabled) {
  transform: translate(-1px, -1px);
  box-shadow: 3px 3px 0 var(--ink);
}

.slot-button.empty {
  background: rgba(255, 248, 232, 0.5);
  color: rgba(17, 29, 20, 0.42);
  cursor: default;
  grid-template-columns: 22px minmax(0, 1fr);
}

.slot-button.empty .slot-name {
  font-size: 0.7rem;
}

.slot-index {
  color: var(--ink-soft);
  font-size: 0.7rem;
  font-weight: 1000;
  text-align: center;
}

.slot-code {
  display: grid;
  place-items: center;
  width: 28px;
  height: 26px;
  border: 2px solid var(--ink);
  border-radius: 5px;
  color: #fff8e8;
  font-size: 0.74rem;
  font-weight: 1000;
}

.slot-name {
  font-size: 0.78rem;
  font-weight: 1000;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.microscope {
  display: grid;
  gap: 6px;
  padding: 8px 10px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  background: #fff8e8;
}

.microscope-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.microscope-grid {
  display: grid;
  gap: 2px;
  margin: 0 auto;
  justify-content: center;
}

.micro-cell {
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--field);
  color: var(--ink-soft);
  font-weight: 1000;
}

.micro-cell.visited {
  background: var(--green-soft);
  border-color: var(--green-dark);
  color: var(--green-dark);
}

.micro-cell.target {
  background: #f6dbb1;
  border-color: var(--orange);
  color: var(--orange);
}

.micro-cell.sensor {
  background: #d8e6f4;
  border-color: var(--blue);
  color: var(--blue);
}

.micro-cell.start {
  background: var(--yellow);
  color: var(--ink);
}

.micro-cell.end {
  outline: 2px solid var(--ink);
  outline-offset: -2px;
}

.genome-tag {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 16px;
  margin: 0 2px 2px 0;
  padding: 0 3px;
  border: 1px solid var(--ink);
  border-radius: 4px;
  color: #fff8e8;
  font-size: 0.62rem;
  font-weight: 1000;
  letter-spacing: 0.02em;
  vertical-align: middle;
}

.control-panel {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.primary-action,
.secondary-action {
  min-height: 50px;
  border: 2px solid var(--ink);
  border-radius: 7px;
  color: var(--ink);
  background: #fff8e8;
  cursor: pointer;
  font-weight: 1000;
  text-transform: uppercase;
  transition: transform 120ms ease, box-shadow 120ms ease;
}

.primary-action {
  grid-column: span 2;
  min-height: 64px;
  font-size: 1.4rem;
  letter-spacing: 0.06em;
  color: #fff8e8;
  background: var(--green);
  box-shadow: 5px 5px 0 var(--ink);
}

.primary-action:hover:not(:disabled),
.secondary-action:hover:not(:disabled) {
  transform: translate(-2px, -2px);
  box-shadow: 7px 7px 0 var(--ink);
}

.secondary-action:hover:not(:disabled) {
  box-shadow: 5px 5px 0 var(--ink);
}

.primary-action:disabled,
.secondary-action:disabled,
.stepper-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
  transform: none;
  box-shadow: none;
}

.result-actions .primary-action,
.result-actions .secondary-action {
  grid-column: auto;
  min-height: 48px;
  font-size: 1rem;
  box-shadow: 4px 4px 0 var(--ink);
}

.leaderboard-list {
  display: grid;
  gap: 6px;
}

.leaderboard-row {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  padding: 6px 8px;
  border: 2px solid var(--line);
  border-radius: 7px;
  background: #fff8e8;
  font-weight: 1000;
}

.leaderboard-row.is-you {
  border-color: var(--green);
  background: var(--green-soft);
}

.leaderboard-row span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.leaderboard-row small {
  display: block;
  color: var(--ink-soft);
  font-size: 0.68rem;
  font-weight: 1000;
}

.login-hint {
  margin: -2px 0 4px;
  color: var(--ink-soft);
  font-size: 0.74rem;
  font-weight: 800;
  letter-spacing: 0.01em;
}

.page-footer {
  display: flex;
  justify-content: center;
  gap: 18px;
  padding: 16px 12px 24px;
  font-weight: 1000;
  font-size: 0.86rem;
  letter-spacing: 0.04em;
}

.page-footer a {
  color: var(--ink);
  text-decoration: none;
  border-bottom: 2px solid var(--ink);
  padding-bottom: 1px;
}

.page-footer a:hover {
  color: var(--purple-dark);
  border-color: var(--purple-dark);
}

.pill-button {
  border: 2px solid var(--ink);
  cursor: pointer;
  background: var(--paper-deep);
  letter-spacing: 0.04em;
}

.pill-button:hover {
  background: var(--yellow);
}

.leaderboard-footer {
  margin-top: 8px;
  justify-self: start;
}

.leaderboard-overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(17, 29, 20, 0.6);
  z-index: 100;
}

.leaderboard-overlay[hidden] {
  display: none;
}

.leaderboard-card {
  width: 100%;
  max-width: 520px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  background: var(--paper);
  border: 3px solid var(--ink);
  border-radius: var(--radius);
  box-shadow: var(--shadow-hard);
  overflow: hidden;
}

.leaderboard-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 2px solid var(--ink);
  background: var(--paper-deep);
}

.leaderboard-card-header h3 {
  margin: 0;
  font-size: 1.1rem;
  letter-spacing: 0.04em;
}

.leaderboard-full-list {
  display: grid;
  gap: 6px;
  padding: 14px 18px 18px;
  overflow-y: auto;
}

@media (max-width: 1060px) {
  .game-shell {
    grid-template-columns: 1fr;
    grid-template-areas:
      "arena"
      "builder"
      "rank";
    grid-template-rows: auto;
  }

  #arenaCanvas {
    width: min(100%, 76vh);
  }
}

@media (max-width: 720px) {
  .game-shell {
    width: calc(100vw - 16px);
    margin: 8px auto;
    grid-template-areas:
      "builder"
      "arena"
      "rank";
  }

  .arena-panel,
  .gene-panel,
  .rank-panel {
    padding: 12px;
  }

  .top-hud,
  .domination-row {
    align-items: flex-start;
    flex-direction: column;
  }

  h1 {
    max-width: 10ch;
    font-size: clamp(2.15rem, 13vw, 4.2rem);
  }

  .match-stats {
    width: 100%;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
  }

  .match-stats div {
    padding: 8px 7px;
  }

  .stat-label {
    font-size: 0.58rem;
  }

  .status-text {
    width: 100%;
    text-align: left;
    white-space: normal;
  }

  .canvas-stage {
    box-shadow: none;
  }

  #arenaCanvas {
    width: min(100%, 72vh);
  }

  .control-panel {
    grid-template-columns: 1fr;
  }

  .primary-action {
    grid-column: auto;
  }

  .matchup-strip {
    grid-template-columns: 1fr;
  }

  .matchup-strip span:last-child {
    text-align: left;
  }

  .command-palette,
  .genome-slots {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .command-button {
    min-height: 46px;
  }
}

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