:root {
  --paper: oklch(94% 0.027 88);
  --paper-deep: oklch(88% 0.041 84);
  --ink: oklch(24% 0.038 148);
  --leaf: oklch(34% 0.08 148);
  --leaf-dark: oklch(25% 0.06 152);
  --gold: oklch(74% 0.14 79);
  --berry: oklch(55% 0.15 19);
  --sky: oklch(70% 0.12 222);
  --shadow: 0 28px 60px rgba(44, 39, 22, 0.22), 0 8px 18px rgba(44, 39, 22, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at 12% 16%, rgba(222, 177, 75, 0.28), transparent 26rem),
    radial-gradient(circle at 84% 10%, rgba(83, 144, 126, 0.24), transparent 22rem),
    linear-gradient(135deg, var(--paper), var(--paper-deep));
  color: var(--ink);
  font-family: "Trebuchet MS", "Microsoft YaHei UI", "PingFang SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overflow: hidden;
  touch-action: manipulation;
}

button {
  min-width: 40px;
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  background: var(--leaf);
  color: oklch(97% 0.02 92);
  font: 700 14px/1 "Trebuchet MS", "Microsoft YaHei UI", sans-serif;
  box-shadow: 0 7px 0 var(--leaf-dark), 0 12px 22px rgba(29, 64, 48, 0.2);
  cursor: pointer;
  transition-property: transform, box-shadow, background-color;
  transition-duration: 160ms;
  transition-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
}

button:active,
button.is-pressed {
  transform: translateY(4px) scale(0.97);
  box-shadow: 0 3px 0 var(--leaf-dark), 0 6px 14px rgba(29, 64, 48, 0.18);
}

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

.page-shell {
  min-height: 100dvh;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 10px;
  place-items: center;
}

.toolbar {
  width: min(100%, calc((100dvh - 74px) * 16 / 9));
  max-width: 1400px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.toolbar strong {
  color: var(--leaf-dark);
  font: 900 20px/1 "Trebuchet MS", "Microsoft YaHei UI", sans-serif;
}

.game-stage {
  position: relative;
  width: min(100%, calc((100dvh - 74px) * 16 / 9));
  max-width: 1400px;
  aspect-ratio: 16 / 9;
  border-radius: 12px;
  padding: clamp(8px, 1.1vw, 16px);
  background:
    linear-gradient(135deg, rgba(255, 248, 226, 0.9), rgba(234, 220, 178, 0.8)),
    repeating-linear-gradient(45deg, rgba(92, 64, 31, 0.12) 0 1px, transparent 1px 12px);
  box-shadow: var(--shadow);
}

canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  background: #18241d;
  image-rendering: auto;
  outline: 1px solid rgba(44, 38, 20, 0.24);
  outline-offset: -1px;
}

.top-actions {
  display: flex;
  gap: 10px;
}

.top-actions button {
  background: oklch(40% 0.08 37);
  box-shadow: 0 6px 0 oklch(28% 0.06 37), 0 12px 20px rgba(65, 34, 25, 0.18);
}

.touch-controls {
  position: absolute;
  inset: auto 18px 18px 18px;
  display: none;
  justify-content: space-between;
  align-items: end;
  pointer-events: none;
  z-index: 5;
}

.touch-controls button {
  pointer-events: auto;
  background: var(--gold);
  color: oklch(21% 0.04 76);
  box-shadow: 0 7px 0 oklch(49% 0.11 71), 0 12px 24px rgba(112, 80, 24, 0.22);
}

.pad {
  width: 142px;
  height: 142px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 6px;
}

.pad button:nth-child(1) {
  grid-column: 2;
}

.pad button:nth-child(2) {
  grid-column: 1;
  grid-row: 2;
}

.pad button:nth-child(3) {
  grid-column: 2;
  grid-row: 3;
}

.pad button:nth-child(4) {
  grid-column: 3;
  grid-row: 2;
}

.actions {
  display: flex;
  gap: 8px;
  flex-direction: column;
}

@media (hover: hover) {
  button:hover {
    transform: translateY(-1px);
  }
}

@media (max-width: 820px), (pointer: coarse) {
  body {
    overflow: auto;
  }

  .page-shell {
    align-items: start;
  }

  .toolbar {
    width: 100%;
  }

  .game-stage {
    width: 100%;
    margin-top: 8px;
  }

  .top-actions {
    gap: 6px;
  }

  .top-actions button {
    min-width: 44px;
    padding-inline: 8px;
  }

  .touch-controls {
    display: none;
  }

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

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