:root {
  --bg: #071018;
  --bg-deep: #04090e;
  --panel: #102333;
  --line: rgba(167, 215, 255, 0.22);
  --text: #f7fbff;
  --muted: #a9c2d6;
  --dim: #708ea5;
  --gold: #f5c76b;
  --cyan: #65d8ff;
  --red: #ff6577;
  --floor-a: #d9edf2;
  --floor-b: #c6e1e8;
  --wall: #172a3a;
  --wall-edge: #29475d;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.42);
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text);
  font-family: "Hiragino Sans", "Yu Gothic", system-ui, sans-serif;
  background:
    radial-gradient(circle at 18% 5%, rgba(101, 216, 255, 0.16), transparent 29%),
    radial-gradient(circle at 84% 0%, rgba(245, 199, 107, 0.14), transparent 30%),
    linear-gradient(180deg, var(--bg), var(--bg-deep));
  -webkit-tap-highlight-color: transparent;
  overflow-x: hidden;
}

button {
  font: inherit;
}

.app {
  width: min(100%, 860px);
  margin: 0 auto;
  padding: max(10px, env(safe-area-inset-top)) 14px max(14px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 8px;
}

.home-link {
  padding: 9px 13px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.045);
  font-size: 13px;
  text-decoration: none;
}

.brand {
  color: var(--dim);
  font-size: 11px;
  letter-spacing: 0.12em;
}

.game-card {
  padding: clamp(13px, 2.5vw, 20px);
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 28px;
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.075), rgba(255, 255, 255, 0.02));
  box-shadow: var(--shadow);
}

.game-heading {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 18px;
  margin-bottom: 10px;
}

.eyebrow {
  margin: 0 0 6px;
  color: #ffdf9a;
  font-size: 11px;
  letter-spacing: 0.19em;
}

h1 {
  margin: 0;
  font-size: clamp(30px, 7vw, 52px);
  line-height: 1;
}

h1 span {
  color: var(--cyan);
  text-shadow: 0 0 24px rgba(101, 216, 255, 0.3);
}

.lead {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: clamp(13px, 2.8vw, 16px);
}

.status-panel {
  min-width: 82px;
  padding: 10px 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.045);
  text-align: center;
}

.status-label {
  display: block;
  color: var(--dim);
  font-size: 10px;
  letter-spacing: 0.14em;
}

.stage-name {
  display: block;
  max-width: 88px;
  margin-bottom: 5px;
  overflow: hidden;
  color: var(--cyan);
  font-size: 11px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status-panel strong {
  color: #ffdf9a;
  font-size: 27px;
}

.game-settings {
  display: grid;
  grid-template-columns: 1.15fr 1fr 0.78fr;
  gap: 7px;
  width: min(100%, 760px);
  margin: 0 auto 9px;
  padding: 7px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(2, 7, 12, 0.3);
}

.setting-row {
  display: grid;
  grid-template-columns: 42px 1fr 1fr;
  gap: 4px;
  align-items: center;
  min-width: 0;
}

.turn-order-label {
  padding: 0 3px;
  color: var(--dim);
  font-size: 11px;
  letter-spacing: 0.12em;
}

.order-button,
.role-button {
  min-width: 0;
  min-height: 36px;
  padding: 6px 7px;
  color: var(--muted);
  border: 1px solid transparent;
  border-radius: 11px;
  background: transparent;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}

.order-button.active,
.role-button.active {
  color: #071018;
  border-color: rgba(255, 255, 255, 0.2);
  background: linear-gradient(135deg, #dff8ff, var(--cyan));
  box-shadow: 0 5px 16px rgba(101, 216, 255, 0.16);
}

.order-button[data-order="opponent-first"].active,
.role-button[data-role="chaser"].active {
  color: white;
  background: linear-gradient(135deg, #ff8793, #cc2943);
  box-shadow: 0 5px 16px rgba(204, 41, 67, 0.2);
}

.trap-count-setting {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 5px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.trap-count-setting select {
  min-width: 72px;
  padding: 6px 8px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #142b3d;
  font: inherit;
}

.trap-count-setting select:disabled {
  opacity: 0.42;
}

.board-shell {
  position: relative;
  width: min(100%, 520px, calc(100dvh - 390px));
  margin: 0 auto;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 19px;
  background: rgba(2, 7, 12, 0.55);
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  width: 100%;
  aspect-ratio: 1;
  gap: 2px;
  overflow: hidden;
  border-radius: 12px;
  background: #9ab9c2;
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  background: var(--floor-a);
}

.cell:nth-child(odd) {
  background: var(--floor-b);
}

.cell.wall {
  background:
    linear-gradient(135deg, transparent 0 45%, rgba(255, 255, 255, 0.04) 46% 52%, transparent 53%),
    var(--wall);
  box-shadow: inset 0 0 0 2px var(--wall-edge);
}

.entity {
  position: absolute;
  z-index: 2;
  width: 68%;
  height: 68%;
  transition: transform 180ms ease;
}

.player {
  border: 3px solid #dff8ff;
  border-radius: 50%;
  background: linear-gradient(145deg, #72ddff, #167fc5);
  box-shadow: 0 4px 10px rgba(8, 88, 145, 0.4);
}

.player::after {
  position: absolute;
  top: 27%;
  left: 27%;
  width: 16%;
  height: 16%;
  border-radius: 50%;
  background: white;
  box-shadow: 20px 0 0 white;
  content: "";
}

.enemy {
  border: 3px solid #ffd8dc;
  border-radius: 50%;
  background: linear-gradient(145deg, #ff7d8b, #c6213c);
  box-shadow: 0 4px 11px rgba(133, 8, 29, 0.45);
}

.enemy.stunned {
  filter: grayscale(0.65) brightness(1.15);
}

.enemy.stunned::before,
.enemy.stunned::after {
  background: #b9c9d3;
}

.enemy::before,
.enemy::after {
  position: absolute;
  top: -20%;
  width: 30%;
  height: 35%;
  background: #ff9b72;
  clip-path: polygon(50% 0, 100% 100%, 0 82%);
  content: "";
}

.enemy::before {
  left: 7%;
  transform: rotate(-17deg);
}

.enemy::after {
  right: 7%;
  transform: rotate(17deg);
}

.goal {
  position: absolute;
  z-index: 1;
  width: 64%;
  height: 72%;
}

.goal::before {
  position: absolute;
  left: 22%;
  width: 8%;
  height: 100%;
  border-radius: 4px;
  background: #805c20;
  content: "";
}

.goal::after {
  position: absolute;
  top: 4%;
  left: 29%;
  width: 60%;
  height: 47%;
  background: linear-gradient(135deg, #fff29d, var(--gold));
  clip-path: polygon(0 0, 100% 18%, 74% 55%, 100% 90%, 0 73%);
  filter: drop-shadow(0 2px 3px rgba(94, 64, 0, 0.35));
  content: "";
}

.enemy-moving .enemy {
  animation: enemy-pulse 260ms ease;
}

@keyframes enemy-pulse {
  50% { transform: scale(1.15); }
}

.result-overlay {
  position: absolute;
  z-index: 10;
  inset: 8px;
  display: grid;
  place-items: center;
  padding: 16px;
  border-radius: 12px;
  background: rgba(3, 10, 16, 0.82);
  backdrop-filter: blur(5px);
}

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

.result-dialog {
  width: min(100%, 360px);
  padding: 24px 20px;
  border: 1px solid rgba(245, 199, 107, 0.45);
  border-radius: 22px;
  background: linear-gradient(180deg, #173147, #0b1925);
  text-align: center;
  box-shadow: var(--shadow);
}

.result-kicker {
  margin: 0 0 5px;
  color: var(--gold);
  font-size: 11px;
  letter-spacing: 0.18em;
}

.result-dialog h2 {
  margin: 0;
  font-size: clamp(28px, 8vw, 42px);
}

.result-dialog p:not(.result-kicker) {
  margin: 10px 0 18px;
  color: var(--muted);
}

.actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: min(100%, 600px);
  margin: 8px auto;
}

.control-dock {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 22px;
  width: min(100%, 560px);
  margin: 0 auto;
}

.trap-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: min(100%, 245px);
  min-height: 58px;
  margin: 0;
  padding: 8px 16px;
  color: #fff4d4;
  border: 1px solid rgba(245, 199, 107, 0.45);
  border-radius: 16px;
  background: linear-gradient(145deg, rgba(245, 199, 107, 0.17), rgba(245, 199, 107, 0.06));
  cursor: pointer;
}

.trap-button[hidden] {
  display: none;
}

.item-guide {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 16px;
  margin: 5px auto 0;
  color: var(--dim);
  font-size: 10px;
}

.item-guide[hidden] {
  display: none;
}

.item-guide span {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.guide-swatch {
  width: 10px;
  height: 10px;
  transform: rotate(45deg);
}

.bind-swatch { background: #9d67ef; }
.knockback-swatch { background: #35c981; }

.trap-button:disabled {
  cursor: default;
  opacity: 0.42;
}

.trap-button strong,
.trap-button small {
  display: block;
}

.trap-button small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 10px;
}

.trap-icon,
.trap-marker {
  position: relative;
  width: 30px;
  height: 30px;
  border: 3px solid var(--gold);
  transform: rotate(45deg);
}

.trap-icon::before,
.trap-icon::after,
.trap-marker::before,
.trap-marker::after {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 36px;
  height: 3px;
  background: var(--gold);
  content: "";
}

.item {
  position: absolute;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 48%;
  height: 48%;
  border: 2px solid white;
  transform: rotate(45deg);
  box-shadow: 0 0 12px currentColor;
}

.item::before {
  width: 40%;
  height: 40%;
  border-radius: 50%;
  background: white;
  content: "";
}

.item-bind {
  color: #ba8cff;
  background: #6d3db7;
}

.item-knockback {
  color: #77f0aa;
  background: #1f9c62;
  clip-path: polygon(50% 0, 63% 32%, 100% 18%, 78% 50%, 100% 82%, 63% 68%, 50% 100%, 37% 68%, 0 82%, 22% 50%, 0 18%, 37% 32%);
}

.trap-icon::before,
.trap-marker::before {
  transform: translate(-50%, -50%);
}

.trap-icon::after,
.trap-marker::after {
  transform: translate(-50%, -50%) rotate(90deg);
}

.trap-marker {
  position: absolute;
  z-index: 1;
  width: 42%;
  height: 42%;
  border-width: 2px;
  opacity: 0.9;
}

.trap-marker::before,
.trap-marker::after {
  width: 130%;
  height: 2px;
}

.action-buttons {
  display: flex;
  flex: 0 0 auto;
  gap: 8px;
}

.primary-button,
.secondary-button {
  min-height: 44px;
  padding: 10px 17px;
  border-radius: 999px;
  font-weight: 800;
  cursor: pointer;
}

.course-button {
  color: #ffdf9a;
  border-color: rgba(245, 199, 107, 0.34);
}

.primary-button {
  border: 0;
  color: #071018;
  background: linear-gradient(135deg, #ffdf9a, var(--gold));
}

.secondary-button {
  flex: 0 0 auto;
  color: var(--text);
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.055);
}

.game-message {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  text-align: right;
}

.controls {
  display: grid;
  grid-template-areas:
    ". up ."
    "left down right";
  grid-template-columns: repeat(3, 64px);
  gap: 8px;
  justify-content: center;
  margin: 0;
}

.move-button {
  display: grid;
  place-items: center;
  width: 64px;
  height: 58px;
  padding: 0;
  color: var(--cyan);
  border: 1px solid rgba(101, 216, 255, 0.3);
  border-radius: 16px;
  background: rgba(101, 216, 255, 0.08);
  cursor: pointer;
  touch-action: manipulation;
}

.move-button:active {
  transform: scale(0.96);
}

.move-button:disabled {
  cursor: default;
  opacity: 0.45;
}

.up { grid-area: up; }
.left { grid-area: left; }
.down { grid-area: down; }
.right { grid-area: right; }

.arrow {
  width: 17px;
  height: 17px;
  border-top: 4px solid currentColor;
  border-left: 4px solid currentColor;
}

.arrow-up { transform: rotate(45deg) translate(3px, 3px); }
.arrow-left { transform: rotate(-45deg) translate(3px, 3px); }
.arrow-down { transform: rotate(225deg) translate(3px, 3px); }
.arrow-right { transform: rotate(135deg) translate(3px, 3px); }

.instructions {
  margin: 5px 0 0;
  color: var(--dim);
  font-size: 12px;
  text-align: center;
}

footer {
  margin-top: 8px;
  color: rgba(200, 152, 80, 0.65);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-align: center;
}

@media (max-width: 540px) {
  .brand { display: none; }
  .game-heading { align-items: center; gap: 8px; }
  .game-heading > div:first-child { min-width: 0; }
  h1 { font-size: clamp(29px, 10vw, 42px); }
  .lead { max-width: 240px; }
  .status-panel { flex: 0 0 70px; min-width: 70px; padding-right: 8px; padding-left: 8px; }
  .game-settings {
    display: flex;
    flex-direction: column;
    width: min(100%, 560px);
    min-width: 0;
  }
  .setting-row { width: 100%; grid-template-columns: 54px minmax(0, 1fr) minmax(0, 1fr); gap: 5px; }
  .order-button,
  .role-button { padding-right: 4px; padding-left: 4px; font-size: 12px; }
  .trap-count-setting { width: 100%; min-width: 0; justify-content: space-between; padding: 5px 6px 2px 59px; }
  .board-shell { width: min(100%, 560px); }
  .actions {
    align-items: stretch;
    flex-direction: column;
  }
  .action-buttons { width: 100%; min-width: 0; }
  .action-buttons button { flex: 1 1 0; min-width: 0; padding-right: 10px; padding-left: 10px; }
  .game-message { font-size: 12px; }
  .control-dock {
    flex-direction: column;
    gap: 10px;
  }
  .trap-button { width: min(100%, 280px); }
}

@media (max-width: 370px) {
  .app { padding-right: 9px; padding-left: 9px; }
  .game-card { padding: 13px; border-radius: 22px; }
  .controls { grid-template-columns: repeat(3, 58px); }
  .move-button { width: 58px; height: 54px; }
  .order-button,
  .role-button { padding-right: 5px; padding-left: 5px; font-size: 12px; }
  .trap-count-setting { padding-left: 7px; }
}

@media (prefers-reduced-motion: reduce) {
  .entity { transition: none; }
  .enemy-moving .enemy { animation: none; }
}
