:root {
  color-scheme: light;
  font-family: system-ui, sans-serif;
  color: #153b31;
  background: #e8e7db;
  --paper: #fffcf3;
  --green: #164c3b;
  --line: #b8c2ad;
  --muted: #46594b;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100svh; display: grid; place-items: center; }
h1, h2, h3, p, dl, dd { margin: 0; }
button, select, input { font: inherit; }
button, select {
  min-width: 0;
  min-height: 44px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: #153b31;
  font-weight: 700;
  line-height: 1.2;
  touch-action: manipulation;
}
button { cursor: pointer; }
button:active:not(:disabled) { box-shadow: inset 0 2px 5px #14392d45; }
button:disabled, select:disabled { color: #59655a; background: #edece2; cursor: default; }
.primary { background: var(--green); color: #fff; border-color: var(--green); }
:focus-visible { outline: 3px solid #a85b24; outline-offset: 2px; }
@media (hover: hover) {
  button:hover:not(:disabled, .primary) { background: #e7eddf; }
  .primary:hover:not(:disabled) { background: #0e3b2c; }
}

.game {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: min(960px, calc(100% - 24px), calc((100svh - 24px) * 1.6));
  aspect-ratio: 8 / 5;
  padding: clamp(10px, 2.5vw, 26px);
  gap: clamp(8px, 1.8vw, 20px);
  border: 1px solid var(--line);
  border-top: 5px solid var(--green);
  border-radius: 12px;
  background: var(--paper);
  box-shadow: 0 12px 32px #153b3119;
}
.game-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
h1 { font-size: clamp(16px, 2.3vw, 24px); line-height: 1; }
h1 span { font-size: 1.75em; margin-inline-start: .18em; vertical-align: -0.06em; }
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(205px, .49fr);
  align-items: stretch;
  min-height: 0;
  gap: clamp(12px, 3vw, 36px);
}
.board-area {
  display: grid;
  place-items: center;
  container-type: size;
  min-height: 0;
  border: 1px solid #c4c7ac;
  border-radius: 8px;
  background: #ebe9d7;
}
#board {
  --tile: min(calc((100cqw - 16px) / var(--columns)), calc((100cqh - 16px) / var(--rows)));
  display: grid;
  grid-template-columns: repeat(var(--columns), var(--tile));
  grid-auto-rows: var(--tile);
  font-size: calc(var(--tile) * .49);
  line-height: 1;
  user-select: none;
}
.cell { position: relative; display: grid; place-items: center; }
.cell[data-hinted]::after {
  content: '';
  position: absolute;
  inset: 0;
  border: 2px solid #925020;
  border-radius: 3px;
  box-shadow: inset 0 0 0 1px #fff5d5;
  pointer-events: none;
}
.cell.floor { border: 1px solid #d1c7ab; background: #f8efd3; }
.cell.wall { border: 1px solid #506c5d; background: #345649; box-shadow: inset 0 -3px #203f34; }
.piece {
  display: grid;
  place-items: center;
  width: 82%;
  height: 82%;
  font-weight: 800;
}
.box, .key-box { border: 2px solid #774521; border-radius: 4px; background: #eab778; color: #673918; }
.goal, .key-goal { color: #7a4d1e; }
.placed, .key-placed { border: 2px solid #31694b; border-radius: 4px; background: #adcf9f; color: #214c32; }
.worker, .key-worker {
  width: 74%;
  height: 74%;
  border: 2px solid #142e44;
  border-radius: 50%;
  color: #fff;
  background: #245372;
}
.worker.on-goal { border: 3px double #fff1bb; }
.game[data-screen="play"] .board-area { touch-action: none; user-select: none; }
.side { display: grid; align-content: center; gap: 12px; min-width: 0; }
.intro { color: var(--muted); font-size: 1rem; }
.stage-select { display: grid; gap: 4px; font-size: .875rem; font-weight: 700; }
.stage-select select { width: 100%; }
.actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
#start:has(+ #resume[hidden]) { grid-column: 1 / -1; }
.play-info { display: grid; gap: 6px; }
h2 { font-size: clamp(16px, 2vw, 22px); line-height: 1.2; }
h3 { font-size: 1.2rem; }
.stats { display: flex; justify-content: space-between; gap: 8px; }
.stats div { display: flex; align-items: baseline; gap: 6px; }
.stats dt { color: var(--muted); font-size: .815rem; }
.stats dd { font-size: 1.2rem; font-weight: 750; font-variant-numeric: tabular-nums; }
.play-actions { display: grid; align-items: start; }
.play-actions > * { grid-area: 1 / 1; min-width: 0; }
/* Both panels size the shared row. The hidden panel stays out of display and focus navigation. */
.play-actions > [hidden] { display: grid !important; visibility: hidden; }
.movement { display: grid; gap: 10px; }
.dpad {
  display: grid;
  grid-template-columns: repeat(3, clamp(44px, 6vw, 64px));
  gap: 4px;
  justify-content: center;
}
.dpad button {
  min-height: clamp(44px, 6vw, 58px);
  background: #e8eddf;
  color: var(--green);
  font-size: 1.65rem;
  padding: 0;
}
.dpad [data-dir="up"] { grid-area: 1 / 2; }
.dpad [data-dir="left"] { grid-area: 2 / 1; }
.dpad [data-dir="down"] { grid-area: 2 / 2; }
.dpad [data-dir="right"] { grid-area: 2 / 3; }
.dpad button[data-hinted] {
  background: #f8dca4;
  outline: 2px solid #925020;
  outline-offset: -3px;
}
#hint { grid-column: 1 / -1; }
.result { display: grid; gap: 6px; padding: 9px; border: 1px solid var(--line); border-radius: 6px; background: #e9f0e2; }
#best-result { color: var(--muted); font-size: .875rem; }
#next-stage { display: block; font-size: .8em; }
.status, .storage-status { color: var(--muted); font-size: .8rem; }
.status { block-size: 4.2em; line-height: 1.4; }
.storage-status { color: #824322; }

dialog {
  position: absolute;
  inset: 0;
  width: min(460px, 96%);
  max-width: calc(100% - 12px);
  max-height: calc(100% - 12px);
  padding: clamp(12px, 3vw, 22px);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--paper);
  color: #153b31;
}
dialog::backdrop { background: #142c23a6; }
dialog > * + * { margin-top: 10px; }
dialog ul { padding-inline-start: 1.25rem; margin-bottom: 0; }
dialog li + li { margin-top: 3px; }
dialog form { display: flex; justify-content: end; gap: 6px; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: .8rem; }
.legend span { display: flex; align-items: center; gap: 4px; }
.legend b { display: inline-grid; place-items: center; width: 22px; height: 22px; line-height: 1; }
.mute { display: flex; align-items: center; gap: 8px; min-height: 44px; }
.volume { display: grid; grid-template-columns: auto minmax(0, 1fr) 4ch; gap: 9px; align-items: center; min-height: 44px; }
input { accent-color: var(--green); }

@media (orientation: portrait) and (max-width: 720px) {
  .game {
    width: min(530px, calc(100% - 12px));
    height: min(760px, calc(100svh - 12px));
    aspect-ratio: auto;
  }
  .layout { grid-template-columns: 1fr; grid-template-rows: minmax(0, 1fr) auto; gap: 8px; }
  .side { gap: 7px; }
  .stage-select { grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 8px; }
  .play-info { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; }
  .stats { justify-content: start; }
  .movement { grid-template-columns: auto minmax(0, 1fr); gap: 10px; }
  .movement .actions { grid-template-columns: 1fr; }
  .result { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .result h3, #best-result { grid-column: 1 / -1; }
  .status { block-size: 2.8em; }
}

@media (orientation: portrait) and (max-width: 539px) and (max-height: 479px) {
  .game[data-screen="play"] { padding-block: 7px; gap: 5px; }
  #play { gap: 4px; }
  .game[data-screen="play"] #board {
    --tile: min(calc((100cqw - 16px) / var(--columns)), calc((100cqh - 8px) / var(--rows)));
  }
  .dpad { align-content: center; }
  .movement .actions { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .movement .actions button { padding-inline: 2px; font-size: .8125rem; }
  .result { padding: 4px; gap: 3px; }
}

@media (orientation: landscape) and (max-height: 460px) {
  .game {
    width: min(860px, calc(100% - 10px));
    height: calc(100svh - 10px);
    aspect-ratio: auto;
    padding: 6px 10px;
    gap: 5px;
  }
  .layout { grid-template-columns: minmax(0, 1fr) minmax(185px, .7fr); gap: 10px; }
  .side { gap: 5px; }
  .movement { gap: 5px; }
  .movement .actions { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 4px; }
  .movement .actions button { padding-inline: 3px; font-size: .825rem; }
  #hint { grid-column: auto; }
  .play-info { gap: 4px; }
  .play-info h2 { font-size: 1rem; }
  .dpad { grid-template-columns: repeat(3, 44px); }
  .dpad button { min-height: 44px; }
  dialog { font-size: .875rem; }
}

/* The iframe can be shorter than the screen when a fixed ad occupies an edge. */
@media (orientation: landscape) and (max-height: 350px) {
  .layout { grid-template-rows: minmax(0, 1fr); }
  .play-info { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; }
  .result { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .result h3, #best-result { grid-column: 1 / -1; }
  .status { block-size: 2.8em; }
}

#space-notice {
  width: min(360px, calc(100% - 12px));
  max-height: calc(100svh - 4px);
  overflow-y: auto;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  font-size: .875rem;
  line-height: 1.4;
}
