* {
  box-sizing: border-box;
}

:root {
  color-scheme: dark;
  --bg: #101115;
  --panel: #1a1d23;
  --ink: #f3f0e8;
  --muted: #a9b0ba;
  --line: #323741;
  --wall: #e3e0d8;
  --floor: #597d72;
  --sky: #5b7896;
  --accent: #db6b4d;
  --accent-2: #e0b44c;
}

html,
body {
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
}

body {
  display: grid;
  place-items: center;
  min-height: 100dvh;
  padding: 14px;
}

body[data-mode="play"] canvas {
  cursor: grab;
}

main {
  width: min(1100px, 100%);
  display: grid;
  gap: 12px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
  min-height: 42px;
}

h1 {
  margin: 0;
  font-size: clamp(1.1rem, 2vw, 1.55rem);
  font-weight: 750;
  letter-spacing: 0;
}

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

.key-status {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  min-height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
  color: #6f7681;
  font-size: 1rem;
  font-weight: 800;
  user-select: none;
  -webkit-user-select: none;
}

.key-status[data-held="true"] {
  border-color: var(--accent-2);
  background: var(--accent-2);
  color: #111;
}

body:not([data-editor="true"]) .editor-only {
  display: none;
}

button,
.nav-link {
  appearance: none;
  border: 1px solid #343a43;
  background: #181b20;
  color: #e9edf2;
  border-radius: 8px;
  min-height: 36px;
  padding: 0 12px;
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

.nav-link {
  display: inline-flex;
  align-items: center;
  text-decoration: none;
}

button:hover,
.nav-link:hover {
  border-color: #747c88;
}

button[aria-pressed="true"],
button.primary,
.nav-link[aria-current="page"] {
  background: #5ed0a4;
  border-color: #5ed0a4;
  color: #111;
  font-weight: 700;
}

.stage {
  position: relative;
  min-height: 0;
  border: 1px solid var(--line);
  background: #0b0c0f;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 20px 80px rgba(0, 0, 0, 0.35);
}

canvas {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  min-height: 420px;
  max-height: calc(100vh - 120px);
  touch-action: none;
  cursor: crosshair;
}

.hud {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  pointer-events: none;
}

.hud[data-visible="false"] {
  display: none;
}

.pill {
  border: 1px solid rgba(255, 255, 255, 0.16);
  background: rgba(10, 12, 16, 0.72);
  backdrop-filter: blur(10px);
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.35;
}

.pill strong {
  color: var(--ink);
  font-weight: 700;
}

.fallback {
  display: none;
  margin: 0;
  padding: 18px;
  color: var(--ink);
  background: #191d24;
}

.dialog {
  position: absolute;
  left: 50%;
  bottom: 18px;
  width: min(520px, calc(100% - 28px));
  transform: translateX(-50%);
  display: none;
  gap: 10px;
  padding: 14px;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  background: rgba(14, 16, 20, 0.92);
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.38);
  z-index: 3;
}

.dialog[data-open="true"] {
  display: grid;
}

.dialog-title {
  margin: 0;
  color: var(--ink);
  font-weight: 800;
  font-size: 1rem;
}

.dialog-text,
.dialog-response {
  margin: 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.35;
}

.dialog-response[data-tone="error"] {
  color: #f27878;
}

.choice-list {
  display: grid;
  gap: 8px;
}

.choice-btn {
  width: 100%;
  text-align: left;
}

.touch-controls {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: 12px;
  display: none;
  align-items: end;
  justify-content: space-between;
  gap: 18px;
  pointer-events: none;
}

.touch-cluster {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  grid-template-rows: repeat(2, 48px);
  gap: 8px;
  pointer-events: auto;
}

.touch-cluster.turn {
  grid-template-columns: repeat(2, 52px);
  grid-template-rows: 52px;
}

.touch-cluster.action {
  grid-template-columns: 58px;
  grid-template-rows: 58px;
}

.touch-btn {
  min-height: 48px;
  min-width: 48px;
  padding: 0;
  border-color: rgba(255, 255, 255, 0.2);
  background: rgba(10, 12, 16, 0.72);
  color: var(--ink);
  font-size: 1.2rem;
  font-weight: 800;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.touch-btn:active,
.touch-btn.is-held {
  background: var(--accent-2);
  border-color: var(--accent-2);
  color: #111;
}

.touch-btn[data-key="w"] {
  grid-column: 2;
  grid-row: 1;
}

.touch-btn[data-key="a"] {
  grid-column: 1;
  grid-row: 2;
}

.touch-btn[data-key="s"] {
  grid-column: 2;
  grid-row: 2;
}

.touch-btn[data-key="d"] {
  grid-column: 3;
  grid-row: 2;
}

@media (max-width: 720px) {
  body {
    place-items: start center;
    padding: 10px;
  }

  .topbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .controls {
    justify-content: flex-start;
  }

  canvas {
    aspect-ratio: 3 / 4;
    min-height: 520px;
  }

  .hud {
    display: none;
  }

  body[data-mode="play"] .touch-controls {
    display: flex;
  }
}

@media (pointer: coarse) {
  .hud {
    display: none;
  }

  body[data-mode="play"] .touch-controls {
    display: flex;
  }
}

@media (max-width: 430px) {
  .controls {
    gap: 6px;
  }

  button {
    min-height: 34px;
    padding: 0 10px;
    font-size: 0.84rem;
  }

  .touch-controls {
    left: 10px;
    right: 10px;
    bottom: 10px;
    gap: 10px;
  }

  .touch-cluster {
    grid-template-columns: repeat(3, 42px);
    grid-template-rows: repeat(2, 42px);
    gap: 7px;
  }

  .touch-cluster.turn {
    grid-template-columns: repeat(2, 46px);
    grid-template-rows: 46px;
  }

  .touch-cluster.action {
    grid-template-columns: 50px;
    grid-template-rows: 50px;
  }

  .touch-btn {
    min-height: 42px;
    min-width: 42px;
    font-size: 1.05rem;
  }
}
