:root {
  color-scheme: dark;
  --bg: #0b1020;
  --panel: rgba(19, 29, 55, 0.9);
  --panel-2: rgba(31, 45, 78, 0.92);
  --line: rgba(149, 180, 255, 0.34);
  --text: #f5f8ff;
  --muted: #b6c4e8;
  --accent: #ffd36a;
  --good: #61e59b;
  --egg: #fff2cf;
  --egg-shade: #e7c775;
  --target: #d6a057;
  --player: #f36b32;
  --player-dark: #9f3322;
  --wall: #385f48;
  --floor: #17321f;
  --theme-glow: rgba(255, 211, 106, 0.26);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(circle at 22% 8%, var(--theme-glow), transparent 26%),
    radial-gradient(circle at 78% 18%, rgba(91, 220, 149, 0.14), transparent 26%),
    linear-gradient(180deg, #142319, var(--bg));
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overscroll-behavior: none;
}

body {
  display: grid;
  place-items: center;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  position: relative;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image: var(--stage-backdrop-image);
  background-position: center;
  background-size: cover;
  opacity: 0.16;
  filter: blur(1px) saturate(1.12);
  mix-blend-mode: screen;
  transition: opacity 420ms ease, background-image 420ms ease;
}

button {
  min-height: 42px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  background: linear-gradient(180deg, #27385f, #18243e);
  color: var(--text);
  font: inherit;
  font-weight: 900;
  cursor: pointer;
}

button:active {
  transform: translateY(1px);
}

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

button:disabled:active {
  transform: none;
}

.cover-screen {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  align-items: end;
  justify-items: start;
  padding: max(28px, env(safe-area-inset-top)) 18px max(34px, env(safe-area-inset-bottom));
  background:
    linear-gradient(180deg, rgba(3, 9, 7, 0.06) 0%, rgba(4, 10, 7, 0.22) 42%, rgba(3, 7, 5, 0.86) 100%),
    url("./assets/clear/jungle-sumomo-bg-3.jpg") center / cover no-repeat;
}

.cover-screen.hidden {
  display: none;
}

.cover-content {
  width: min(100%, 760px);
  display: grid;
  gap: 12px;
  color: #fff9dd;
  text-shadow:
    0 2px 0 rgba(20, 41, 17, 0.72),
    0 0 24px rgba(255, 218, 116, 0.38);
}

.cover-content p {
  margin: 0;
  color: #ffdd76;
  font-size: clamp(14px, 3.6vw, 24px);
  font-weight: 950;
  letter-spacing: 0;
}

.cover-content h1 {
  max-width: 720px;
  font-size: clamp(58px, 15vw, 126px);
  line-height: 0.86;
}

.cover-content span {
  color: #d9ffd6;
  font-size: clamp(18px, 4.8vw, 34px);
  font-weight: 950;
}

.cover-actions {
  width: min(100%, 240px);
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-top: 8px;
}

.cover-actions a,
.cover-actions button {
  min-height: 52px;
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 238, 163, 0.5);
  border-radius: 8px;
  background: rgba(12, 27, 17, 0.76);
  color: #fff9df;
  font: inherit;
  font-size: 16px;
  font-weight: 950;
  text-decoration: none;
  backdrop-filter: blur(6px);
}

.cover-actions button {
  background: linear-gradient(180deg, rgba(255, 209, 94, 0.96), rgba(213, 111, 28, 0.92));
  color: #1b1306;
  text-shadow: none;
}

.app {
  width: min(100%, 820px);
  height: calc(100dvh - max(8px, env(safe-area-inset-top)) - max(8px, env(safe-area-inset-bottom)));
  min-height: 0;
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
  gap: 6px;
}

.topbar,
.status,
.world,
.toolbar,
.board-wrap,
.pad {
  border: 1px solid var(--line);
  background: var(--panel);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.24);
}

.topbar {
  display: grid;
  grid-template-columns: 1fr;
  align-items: center;
  gap: 6px;
  padding: 4px;
  border-radius: 10px;
}

.brand {
  display: none;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 12px;
  font-weight: 950;
  letter-spacing: 1px;
}

h1 {
  margin: 0;
  font-size: clamp(26px, 8vw, 44px);
  line-height: 0.95;
  letter-spacing: 0;
}

.subtitle {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 1px;
}

.top-actions {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.top-actions button,
.home-link {
  min-height: 30px;
  padding: 0 8px;
  font-size: 11px;
}

.home-link {
  display: grid;
  place-items: center;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 8px;
  background: linear-gradient(180deg, #344a73, #1d2d4a);
  color: var(--text);
  font: inherit;
  font-weight: 950;
  text-decoration: none;
}

.home-link:active {
  transform: translateY(1px);
}

.top-actions button[aria-pressed="false"] {
  opacity: 0.55;
}

.mobile-only {
  display: none;
}

.status {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 5px;
  padding: 4px;
  border-radius: 10px;
}

.status div {
  min-width: 0;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 6px;
  padding: 3px 7px;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.06);
}

.status span {
  color: var(--muted);
  font-size: 10px;
  font-weight: 900;
  letter-spacing: 0;
}

.status strong {
  font-size: clamp(17px, 4vw, 22px);
  line-height: 1;
}

.world {
  display: grid;
  grid-template-columns: auto minmax(0, auto) minmax(0, 1fr);
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  border-radius: 10px;
}

.world span {
  font-size: 16px;
}

.world strong {
  color: var(--accent);
  font-size: 12px;
}

.world small {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toolbar {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 5px;
  padding: 4px;
  border-radius: 10px;
}

.toolbar button {
  min-height: 30px;
  font-size: 18px;
}

.editor {
  display: grid;
  gap: 8px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
}

.editor.hidden {
  display: none;
}

.editor-head {
  display: grid;
  gap: 3px;
}

.editor-head strong {
  color: var(--accent);
  font-size: 14px;
}

.editor-head span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.editor textarea {
  width: 100%;
  min-height: 132px;
  resize: vertical;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.28);
  color: var(--text);
  font: 14px/1.25 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  letter-spacing: 0;
  padding: 10px;
}

.editor-actions {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.editor-actions button {
  min-height: 36px;
  font-size: 11px;
}

.board-wrap {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 0;
  height: 100%;
  padding: 7px;
  border-radius: 12px;
  overflow: hidden;
}

.board {
  display: grid;
  gap: 2px;
  width: min(100%, var(--board-width, 520px));
  height: min(100%, var(--board-height, 520px));
  max-width: var(--board-width, 520px);
  max-height: var(--board-height, 520px);
  padding: 6px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
}

.app[data-theme="grass"] {
  --bg: #0b1d14;
  --panel: rgba(17, 43, 26, 0.9);
  --panel-2: rgba(37, 72, 41, 0.95);
  --line: rgba(151, 230, 141, 0.34);
  --muted: #c6e8c7;
  --accent: #ffda73;
  --floor: #214827;
  --wall: #5d7d45;
  --target: #d0a45b;
  --theme-glow: rgba(123, 232, 108, 0.18);
}

.app[data-theme="beach"] {
  --bg: #071b2a;
  --panel: rgba(8, 43, 61, 0.9);
  --panel-2: rgba(21, 81, 105, 0.95);
  --line: rgba(125, 223, 255, 0.36);
  --muted: #c7edf6;
  --accent: #ffe28a;
  --floor: #1d6a79;
  --wall: #7d7458;
  --target: #dfb36b;
  --theme-glow: rgba(86, 215, 255, 0.2);
}

.app[data-theme="cave"] {
  --bg: #0c0b12;
  --panel: rgba(30, 28, 39, 0.93);
  --panel-2: rgba(50, 45, 63, 0.96);
  --line: rgba(183, 170, 220, 0.32);
  --muted: #d3cbe8;
  --accent: #c8a4ff;
  --floor: #272536;
  --wall: #60576d;
  --target: #b98760;
  --theme-glow: rgba(188, 126, 255, 0.18);
}

.app[data-theme="ruins"] {
  --bg: #17120b;
  --panel: rgba(47, 34, 18, 0.92);
  --panel-2: rgba(77, 55, 28, 0.96);
  --line: rgba(247, 192, 98, 0.34);
  --muted: #ead4ad;
  --accent: #ffca62;
  --floor: #3f3220;
  --wall: #856f4a;
  --target: #d4a154;
  --theme-glow: rgba(255, 184, 76, 0.2);
}

.app[data-theme="dragon"] {
  --bg: #10101f;
  --panel: rgba(26, 24, 57, 0.92);
  --panel-2: rgba(47, 39, 88, 0.96);
  --line: rgba(255, 219, 112, 0.38);
  --muted: #ddd5ff;
  --accent: #ffe37b;
  --floor: #252748;
  --wall: #78629a;
  --target: #e7b651;
  --egg: #ffe88f;
  --egg-shade: #cb8b26;
  --theme-glow: rgba(255, 218, 86, 0.24);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  border-radius: 6px;
  background: var(--floor);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.035);
}

.cell::before,
.cell::after {
  pointer-events: none;
}

.cell.wall {
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.15), transparent 42%),
    linear-gradient(315deg, rgba(0, 0, 0, 0.18), transparent 46%),
    var(--wall);
  box-shadow: inset 0 -3px rgba(0, 0, 0, 0.24);
}

.cell.target::before {
  content: "";
  position: absolute;
  inset: 7%;
  border: 2px solid rgba(255, 229, 161, 0.58);
  border-radius: 8px;
  background-image:
    linear-gradient(135deg, rgba(255, 248, 221, 0.16), transparent 42%),
    url("./assets/mizugoke.png");
  background-position: center, center;
  background-repeat: no-repeat, no-repeat;
  background-size: 100% 100%, cover;
  box-shadow:
    inset 0 0 10px rgba(87, 61, 18, 0.24),
    0 0 10px rgba(255, 210, 120, 0.26);
  z-index: 1;
}

.cell.box::after,
.cell.player::after {
  position: absolute;
  inset: 12%;
  display: grid;
  place-items: center;
  border-radius: 7px;
  font-weight: 950;
  z-index: 2;
}

.cell.box::after {
  content: "";
  inset: 4%;
  border-radius: 0;
  background-image: url("./assets/egg.png");
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.24)) drop-shadow(0 0 8px rgba(255, 232, 143, 0.18));
}

.cell.box.target::after {
  filter: drop-shadow(0 3px 0 rgba(0, 0, 0, 0.24)) drop-shadow(0 0 14px rgba(255, 215, 92, 0.62));
}

.cell.player::after {
  content: "";
  inset: -13%;
  border-radius: 0;
  --sumomo-frame: url("./assets/sumomo-step-0.png");
  --sumomo-face: 1;
  --sumomo-bob: 0%;
  --sumomo-tilt: 0deg;
  background-image:
    radial-gradient(ellipse at center, rgba(255, 231, 186, 0.3), transparent 62%),
    var(--sumomo-frame);
  background-position: center, center;
  background-repeat: no-repeat, no-repeat;
  background-size: 112% 112%, contain;
  filter: drop-shadow(0 4px 0 rgba(0, 0, 0, 0.26)) drop-shadow(0 0 8px rgba(255, 211, 106, 0.22));
  transform: translateY(var(--sumomo-bob)) scaleX(var(--sumomo-face)) rotate(var(--sumomo-tilt));
  transition: transform 90ms ease;
}

.cell.player.face-left::after {
  --sumomo-face: -1;
}

.cell.player.step-1::after {
  --sumomo-frame: url("./assets/sumomo-step-1.png");
  --sumomo-bob: -3%;
  --sumomo-tilt: -2deg;
}

.cell.player.step-2::after {
  --sumomo-frame: url("./assets/sumomo-step-2.png");
  --sumomo-bob: 2%;
}

.cell.player.step-3::after {
  --sumomo-frame: url("./assets/sumomo-step-3.png");
  --sumomo-bob: -2%;
  --sumomo-tilt: 2deg;
}

.cell.player::before {
  content: none;
}

.overlay {
  position: absolute;
  inset: 10px;
  z-index: 10;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 14px;
  border-radius: 12px;
  background:
    radial-gradient(circle at 50% 22%, rgba(255, 211, 106, 0.12), transparent 32%),
    rgba(5, 11, 24, 0.97);
  box-shadow: inset 0 0 0 1px rgba(255, 211, 106, 0.22);
  text-align: center;
  backdrop-filter: blur(4px);
}

.overlay.is-final-clear {
  position: fixed;
  inset: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
    max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  align-content: end;
  min-height: 0;
  padding: min(8vh, 70px) 18px;
  border: 1px solid rgba(255, 230, 160, 0.62);
  background:
    linear-gradient(180deg, rgba(18, 11, 4, 0.02) 0%, rgba(18, 11, 4, 0.18) 46%, rgba(12, 8, 3, 0.78) 100%),
    url("./assets/clear/end-card-hatchling.jpg") center / cover no-repeat;
  box-shadow:
    inset 0 0 0 1px rgba(255, 255, 255, 0.22),
    0 24px 90px rgba(0, 0, 0, 0.58);
  overflow: hidden;
}

.overlay.is-final-clear::before,
.overlay.is-final-clear::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.overlay.is-final-clear::before {
  background:
    radial-gradient(circle at 18% 24%, rgba(255, 245, 194, 0.82) 0 2px, transparent 3px),
    radial-gradient(circle at 72% 18%, rgba(255, 255, 255, 0.8) 0 2px, transparent 3px),
    radial-gradient(circle at 86% 64%, rgba(255, 210, 130, 0.72) 0 2px, transparent 3px),
    radial-gradient(circle at 34% 82%, rgba(255, 186, 221, 0.72) 0 2px, transparent 3px);
  animation: endSparkle 1.8s ease-in-out infinite alternate;
}

.overlay.is-final-clear::after {
  background:
    linear-gradient(115deg, transparent 0 38%, rgba(255, 255, 255, 0.28) 46%, transparent 54% 100%),
    radial-gradient(circle at 50% 72%, rgba(255, 190, 88, 0.32), transparent 34%);
  animation: endShine 2.6s ease-in-out infinite;
  transform: translateX(-70%);
}

.overlay.hidden {
  display: none;
}

.overlay strong {
  color: var(--accent);
  font-size: clamp(30px, 10vw, 54px);
  line-height: 1;
}

.overlay.is-final-clear strong,
.overlay.is-final-clear span,
.overlay.is-final-clear .overlay-actions {
  position: relative;
  z-index: 1;
}

.overlay.is-final-clear strong {
  max-width: min(100%, 520px);
  color: #fff6c8;
  font-size: clamp(39px, 12vw, 76px);
  text-shadow:
    0 3px 0 rgba(116, 46, 6, 0.55),
    0 0 16px rgba(255, 214, 105, 0.82),
    0 0 34px rgba(255, 125, 89, 0.72);
  animation: clearPop 900ms cubic-bezier(0.2, 1.35, 0.25, 1) both, clearGlow 1.6s ease-in-out 900ms infinite alternate;
}

.overlay span {
  color: var(--muted);
  font-size: 15px;
  font-weight: 850;
}

.overlay.is-final-clear span {
  width: min(100%, 460px);
  padding: 9px 14px;
  border-radius: 999px;
  background: rgba(28, 15, 4, 0.58);
  color: #fff4d5;
  font-size: clamp(15px, 4vw, 19px);
  text-shadow: 0 1px 8px rgba(0, 0, 0, 0.72);
  backdrop-filter: blur(6px);
}

.overlay.is-final-clear .overlay-actions {
  width: min(100%, 420px);
}

.overlay-actions {
  display: grid;
  grid-template-columns: repeat(2, minmax(120px, 1fr));
  gap: 10px;
}

@keyframes clearPop {
  0% {
    opacity: 0;
    transform: translateY(18px) scale(0.78) rotate(-2deg);
  }
  68% {
    opacity: 1;
    transform: translateY(-4px) scale(1.07) rotate(1deg);
  }
  100% {
    opacity: 1;
    transform: translateY(0) scale(1) rotate(0deg);
  }
}

@keyframes clearGlow {
  from {
    filter: saturate(1);
  }
  to {
    filter: saturate(1.2) brightness(1.12);
  }
}

@keyframes endSparkle {
  from {
    opacity: 0.52;
    transform: scale(1);
  }
  to {
    opacity: 1;
    transform: scale(1.015);
  }
}

@keyframes endShine {
  0% {
    opacity: 0;
    transform: translateX(-76%);
  }
  34% {
    opacity: 0.75;
  }
  72%,
  100% {
    opacity: 0;
    transform: translateX(76%);
  }
}

.pad {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 32px);
  gap: 4px;
  padding: 4px;
  border-radius: 10px;
}

.pad button {
  min-height: 0;
  height: 32px;
  font-size: 18px;
}

@media (max-width: 430px) {
  body {
    padding-inline: 6px;
  }

  .app {
    gap: 5px;
  }

  .topbar {
    grid-template-columns: 1fr;
    padding: 5px;
  }

  .brand {
    display: none;
  }

  .top-actions {
    grid-template-columns: repeat(4, 1fr);
    gap: 5px;
  }

  .top-actions .mobile-only {
    display: block;
  }

  .top-actions button,
  .home-link,
  .toolbar button,
  .editor-actions button {
    min-height: 28px;
    font-size: 10px;
  }

  .toolbar button {
    font-size: 18px;
  }

  .editor-actions {
    grid-template-columns: repeat(2, 1fr);
  }

  .editor textarea {
    min-height: 104px;
    font-size: 12px;
  }

  .status {
    gap: 4px;
    padding: 4px;
  }

  .status div {
    padding: 4px 6px;
  }

  .status span {
    font-size: 9px;
  }

  .status strong {
    font-size: 17px;
  }

  .world {
    grid-template-columns: auto auto minmax(0, 1fr);
    padding: 4px 7px;
  }

  .world small {
    display: none;
  }

  .board-wrap {
    padding: 5px;
  }

  .cell {
    border-radius: 4px;
  }

  .pad button {
    height: 34px;
  }

  .pad {
    grid-template-rows: repeat(2, 34px);
  }

  .cover-screen {
    padding-inline: 14px;
  }

  .cover-content h1 {
    font-size: clamp(54px, 18vw, 82px);
  }

  .cover-actions {
    width: min(100%, 220px);
    grid-template-columns: 1fr;
  }
}
