:root {
  color-scheme: dark;
  --paper: #f8f7ee;
  --ink: #1d140b;
  --night: #101217;
  --panel: rgba(31, 24, 18, 0.78);
  --panel-strong: rgba(28, 22, 18, 0.95);
  --line: rgba(248, 247, 238, 0.17);
  --mint: #9ecf8d;
  --sky: #76a7c8;
  --amber: #f0c05a;
  --coral: #bf6f45;
  --berry: #b9865b;
  --shadow: rgba(0, 0, 0, 0.4);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  min-width: 320px;
  overflow: hidden;
  background: var(--night);
  color: var(--paper);
}

button {
  border: 0;
  font: inherit;
}

.game-shell {
  position: relative;
  width: 100vw;
  height: 100vh;
  min-height: 500px;
  overflow: hidden;
  background: #101217;
}

#gameCanvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hud {
  position: fixed;
  z-index: 5;
  top: 16px;
  left: 16px;
  right: 16px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  pointer-events: none;
}

.hud-group,
.hud-actions {
  display: flex;
  gap: 8px;
  pointer-events: auto;
}

.stat,
.hud-actions,
.active-codes,
.level-banner,
.toast,
.result-grid > div {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: 0 12px 34px var(--shadow);
  backdrop-filter: blur(16px);
}

.stat {
  display: grid;
  min-width: 84px;
  height: 58px;
  align-content: center;
  padding: 8px 12px;
}

.stat-label {
  display: block;
  margin-bottom: 2px;
  color: rgba(248, 247, 238, 0.62);
  font-size: 0.68rem;
  font-weight: 800;
  line-height: 1.1;
  letter-spacing: 0;
  white-space: nowrap;
  text-transform: uppercase;
}

.stat strong,
.result-grid strong {
  color: var(--paper);
  font-size: 1.04rem;
  line-height: 1.1;
  letter-spacing: 0;
}

.hud-actions {
  height: 58px;
  padding: 8px;
}

.active-codes {
  position: absolute;
  top: 68px;
  right: 0;
  display: grid;
  gap: 6px;
  width: min(260px, calc(100vw - 32px));
  padding: 8px;
  pointer-events: auto;
}

.active-codes-title {
  padding: 0 3px 2px;
  color: rgba(248, 247, 238, 0.68);
  font-size: 0.62rem;
  font-weight: 900;
  line-height: 1;
  letter-spacing: 0;
  text-transform: uppercase;
}

.active-code {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
  min-height: 30px;
  padding: 6px 8px;
  border: 1px solid rgba(240, 192, 90, 0.32);
  border-radius: 8px;
  background: rgba(240, 192, 90, 0.11);
  color: rgba(248, 247, 238, 0.9);
  font-size: 0.72rem;
  font-weight: 850;
  line-height: 1.12;
}

.active-code strong {
  color: var(--amber);
  font-size: 0.78rem;
  letter-spacing: 0;
  text-transform: uppercase;
}

.active-code span {
  color: rgba(248, 247, 238, 0.72);
  min-width: 0;
  overflow-wrap: anywhere;
  text-align: left;
}

.active-code-off {
  min-height: 24px;
  padding: 0 8px;
  border: 1px solid rgba(248, 247, 238, 0.2);
  border-radius: 6px;
  background: rgba(248, 247, 238, 0.08);
  color: rgba(248, 247, 238, 0.86);
  cursor: pointer;
  font-size: 0.62rem;
  font-weight: 850;
  line-height: 1;
  letter-spacing: 0;
}

.active-code-off:hover {
  border-color: rgba(191, 111, 69, 0.58);
  background: rgba(191, 111, 69, 0.18);
}

.icon-button,
.primary-button,
.secondary-button,
.action-button,
.active-code-off {
  cursor: pointer;
  border-radius: 8px;
  transition:
    transform 140ms ease,
    background-color 140ms ease,
    border-color 140ms ease,
    box-shadow 140ms ease;
}

.icon-button {
  width: 42px;
  height: 42px;
  border: 1px solid rgba(248, 247, 238, 0.18);
  background: rgba(248, 247, 238, 0.09);
  color: var(--paper);
  font-size: 0.75rem;
  font-weight: 850;
  letter-spacing: 0;
}

.icon-button:hover,
.primary-button:hover,
.secondary-button:hover,
.action-button:hover {
  transform: translateY(-1px);
}

.icon-button:focus-visible,
.primary-button:focus-visible,
.secondary-button:focus-visible,
.action-button:focus-visible,
.active-code-off:focus-visible {
  outline: 3px solid rgba(118, 167, 200, 0.46);
  outline-offset: 2px;
}

.icon-button.is-active {
  border-color: rgba(240, 192, 90, 0.66);
  background: rgba(240, 192, 90, 0.18);
}

.level-banner {
  position: fixed;
  z-index: 4;
  top: 88px;
  left: 50%;
  max-width: min(420px, calc(100vw - 32px));
  padding: 9px 16px;
  transform: translateX(-50%);
  color: rgba(248, 247, 238, 0.92);
  font-size: 0.86rem;
  font-weight: 850;
  line-height: 1.15;
  letter-spacing: 0;
  text-align: center;
  white-space: nowrap;
}

.toast {
  position: fixed;
  z-index: 7;
  left: 50%;
  bottom: 28px;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px 16px;
  transform: translateX(-50%);
  color: var(--paper);
  font-size: 0.94rem;
  font-weight: 800;
  line-height: 1.25;
  text-align: center;
}

.panel {
  position: fixed;
  z-index: 8;
  left: 50%;
  top: 50%;
  width: min(430px, calc(100vw - 32px));
  padding: 28px;
  transform: translate(-50%, -50%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel-strong);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.52);
  text-align: center;
  backdrop-filter: blur(20px);
}

.shop-panel {
  width: min(660px, calc(100vw - 28px));
  max-height: min(78vh, 720px);
  overflow: auto;
}

.locations-panel {
  width: min(720px, calc(100vw - 28px));
  max-height: min(78vh, 720px);
  overflow: auto;
}

.panel-hidden {
  display: none;
}

.kicker {
  margin: 0 0 10px;
  color: var(--mint);
  font-size: 0.78rem;
  font-weight: 850;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  color: var(--paper);
  letter-spacing: 0;
}

h1 {
  font-size: 2.35rem;
  line-height: 1.02;
}

h2 {
  font-size: 1.72rem;
  line-height: 1.08;
}

.flavor {
  max-width: 30ch;
  margin: 14px auto 24px;
  color: rgba(248, 247, 238, 0.72);
  font-size: 1rem;
  line-height: 1.45;
}

.primary-button,
.secondary-button {
  min-width: 148px;
  min-height: 48px;
  padding: 0 22px;
  font-weight: 850;
}

.primary-button {
  background: linear-gradient(135deg, var(--mint), var(--sky));
  color: var(--ink);
  box-shadow: 0 12px 26px rgba(158, 207, 141, 0.22);
}

.secondary-button {
  border: 1px solid rgba(248, 247, 238, 0.18);
  background: rgba(248, 247, 238, 0.09);
  color: var(--paper);
}

.primary-button:disabled,
.secondary-button:disabled {
  cursor: not-allowed;
  opacity: 0.48;
  transform: none;
}

.primary-button:active,
.icon-button:active,
.secondary-button:active,
.action-button:active,
.active-code-off:active {
  transform: translateY(1px);
}

.panel-actions {
  display: grid;
  gap: 10px;
  justify-items: center;
  margin-top: 20px;
}

.locations-grid {
  display: grid;
  gap: 9px;
  margin-top: 18px;
}

.location-row {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid rgba(248, 247, 238, 0.13);
  border-radius: 8px;
  background: rgba(248, 247, 238, 0.06);
  text-align: left;
}

.location-row.is-locked {
  opacity: 0.52;
}

.location-badge {
  display: grid;
  width: 46px;
  height: 46px;
  place-items: center;
  border: 1px solid rgba(240, 192, 90, 0.42);
  border-radius: 8px;
  background: rgba(240, 192, 90, 0.12);
  color: var(--paper);
  font-size: 1.05rem;
  font-weight: 900;
}

.location-row h3 {
  margin: 0;
  color: var(--paper);
  font-size: 1rem;
  line-height: 1.15;
}

.location-row p {
  margin: 5px 0 0;
  color: rgba(248, 247, 238, 0.66);
  font-size: 0.82rem;
  font-weight: 700;
  line-height: 1.3;
}

.shop-balance {
  margin: 12px 0 18px;
  color: rgba(248, 247, 238, 0.74);
  font-weight: 800;
}

.shop-grid {
  display: grid;
  gap: 8px;
}

.character-row {
  display: grid;
  grid-template-columns: 46px minmax(0, 1fr) auto;
  gap: 12px;
  align-items: center;
  padding: 12px 0;
  border-top: 1px solid rgba(248, 247, 238, 0.13);
  text-align: left;
}

.character-row:first-child {
  border-top: 0;
}

.character-badge {
  display: grid;
  width: 46px;
  height: 46px;
  place-items: center;
  border: 1px solid rgba(248, 247, 238, 0.18);
  border-radius: 50%;
  background: rgba(248, 247, 238, 0.08);
  color: var(--paper);
  font-size: 1.25rem;
  font-weight: 900;
}

.character-row h3 {
  margin: 0;
  color: var(--paper);
  font-size: 1rem;
  line-height: 1.15;
}

.character-row p {
  margin: 5px 0 0;
  color: rgba(248, 247, 238, 0.66);
  font-size: 0.84rem;
  font-weight: 650;
  line-height: 1.3;
}

.buy-button {
  min-width: 116px;
  min-height: 42px;
  padding: 0 14px;
  border: 1px solid rgba(248, 247, 238, 0.18);
  border-radius: 8px;
  background: rgba(248, 247, 238, 0.1);
  color: var(--paper);
  cursor: pointer;
  font-weight: 850;
}

.buy-button.is-selected {
  border-color: rgba(158, 207, 141, 0.65);
  background: rgba(158, 207, 141, 0.18);
  color: var(--mint);
}

.buy-button:disabled {
  cursor: not-allowed;
  opacity: 0.46;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin: 20px 0 0;
}

.result-grid > div {
  padding: 14px;
}

.touch-controls {
  position: fixed;
  z-index: 6;
  left: 18px;
  right: 18px;
  bottom: 18px;
  display: none;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  pointer-events: none;
}

.joystick {
  position: relative;
  width: 116px;
  height: 116px;
  border: 1px solid rgba(248, 247, 238, 0.2);
  border-radius: 50%;
  background:
    radial-gradient(circle at 50% 50%, rgba(118, 167, 200, 0.2), rgba(31, 24, 18, 0.62) 68%),
    rgba(248, 247, 238, 0.08);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.36);
  pointer-events: auto;
  touch-action: none;
}

.joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 48px;
  height: 48px;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: linear-gradient(135deg, var(--paper), var(--sky));
  box-shadow: 0 8px 22px rgba(118, 167, 200, 0.35);
}

.action-button {
  width: 88px;
  height: 88px;
  border: 1px solid rgba(248, 247, 238, 0.22);
  border-radius: 50%;
  background:
    radial-gradient(circle at 34% 28%, #ffffff 0 9%, transparent 10%),
    linear-gradient(135deg, var(--amber), var(--coral));
  color: #2a100b;
  font-size: 2rem;
  font-weight: 900;
  box-shadow: 0 16px 34px rgba(191, 111, 69, 0.3);
  pointer-events: auto;
  touch-action: manipulation;
}

@media (hover: none), (pointer: coarse), (max-width: 780px) {
  .touch-controls {
    display: flex;
  }
}

@media (max-width: 720px) {
  .game-shell {
    min-height: 560px;
  }

  .hud {
    top: 10px;
    left: 10px;
    right: 10px;
    gap: 8px;
  }

  .hud-group {
    display: grid;
    grid-template-columns: repeat(3, minmax(68px, 1fr));
    flex: 1;
  }

  .stat {
    min-width: 0;
    height: 52px;
    padding: 7px 8px;
  }

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

  .stat strong,
  .result-grid strong {
    font-size: 0.92rem;
  }

  .hud-actions {
    height: 52px;
    padding: 6px;
  }

  .active-codes {
    top: 62px;
    width: min(250px, calc(100vw - 20px));
    padding: 7px;
  }

  .active-code {
    min-height: 28px;
    padding: 5px 7px;
    font-size: 0.66rem;
  }

  .active-code-off {
    min-height: 23px;
    padding: 0 7px;
    font-size: 0.58rem;
  }

  .icon-button {
    width: 36px;
    height: 38px;
    font-size: 0.68rem;
  }

  .level-banner {
    top: 126px;
    max-width: calc(100vw - 24px);
    padding: 8px 12px;
    font-size: 0.78rem;
  }

  .toast {
    bottom: 144px;
    padding: 10px 12px;
    font-size: 0.86rem;
  }

  .panel {
    padding: 24px 20px;
  }

  .shop-panel,
  .locations-panel {
    max-height: min(82vh, 720px);
  }

  .character-row,
  .location-row {
    grid-template-columns: 40px minmax(0, 1fr);
  }

  .character-badge,
  .location-badge {
    width: 40px;
    height: 40px;
    font-size: 1rem;
  }

  .buy-button {
    grid-column: 2;
    justify-self: start;
    min-height: 38px;
  }

  h1 {
    font-size: 1.92rem;
  }

  h2 {
    font-size: 1.48rem;
  }
}

@media (max-width: 430px) {
  .hud-group {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .hud-actions {
    flex-direction: column;
    height: auto;
  }

  .active-codes {
    top: 156px;
    width: min(220px, calc(100vw - 20px));
  }

  .level-banner {
    display: none;
  }

  .joystick {
    width: 104px;
    height: 104px;
  }

  .action-button {
    width: 80px;
    height: 80px;
  }
}
