* {
  box-sizing: border-box;
}

/* No double-tap zoom anywhere */
html {
  touch-action: manipulation;
}

body {
  margin: 0;
  overscroll-behavior: none;
  background: #f3efe2;
  color: #3d4a3a;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

/* Title, board and buttons sit together, with the buttons right under the board. Any
   spare height on a tall phone goes a third above and two thirds below (the ::before and
   ::after), so the buttons stay low enough for a thumb without a gap above the title. */
main,
#game {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

main {
  max-width: 600px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: calc(10px + env(safe-area-inset-top)) calc(16px + env(safe-area-inset-right))
    calc(12px + env(safe-area-inset-bottom)) calc(16px + env(safe-area-inset-left));
}

main::before,
main::after {
  content: "";
  flex: 1;
  margin: -5px 0;
}

main::after {
  flex: 2;
}

#game {
  width: 100%;
}

#no-puzzle {
  margin-top: 40px;
  text-align: center;
  font-size: 1.1rem;
  line-height: 1.6;
}

header {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
}

header div {
  display: flex;
  gap: 6px;
}

header div:last-child {
  justify-content: flex-end;
}

button.icon {
  width: 38px;
  height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 1.2rem;
  font-weight: 700;
}

button.icon svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

h1 {
  margin: 4px 0 0;
  font-size: 1.6rem;
  letter-spacing: 0.02em;
  text-align: center;
}

#day-line {
  margin: -8px 0 0;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
}

#day-label {
  color: #5f6a5a;
}

#streak {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  color: #b04c18;
}

#streak svg {
  width: 16px;
  height: 16px;
  fill: #e0782f;
}

.hud {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  font-size: 1rem;
}

.hud b {
  display: inline-block;
  font-size: 1.4rem;
  margin-left: 4px;
}

/* One little bush per hedge; placed ones grey out */
/* The bushes shrink a little on big-budget days so the row always fits */
#tray {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  gap: 2px;
}

#tray svg {
  flex: 0 1 18px;
  min-width: 0;
  aspect-ratio: 1;
  fill: #467f37;
  transition: fill 0.2s, opacity 0.2s;
}

#tray svg.used {
  fill: #b9b5a6;
  opacity: 0.6;
}

.steps {
  flex: none;
  white-space: nowrap;
}

#step-delta {
  display: inline-block;
  width: 2.4em;
  margin-left: 4px;
  font-weight: 700;
  opacity: 0;
}

#step-delta.up {
  color: #2b7534;
}

#step-delta.down {
  color: #a8463f;
}

/* The board is as big as fits in this box, and centred in it. It's a little wider than the
   rest of the page so the board gets nearly the full width of a phone. The 250px leaves room
   for the title, hedges and buttons; in landscape on a phone the page scrolls instead. */
#stage {
  position: relative;
  width: calc(100% + 16px);
  margin: 0 -8px;
  height: min(100vw - 16px, 100vh - 250px, 560px);
  height: min(100vw - 16px, 100dvh - 250px, 560px);
  min-height: 240px;
  display: grid;
  place-items: center;
}

/* Short messages over the top of the board */
#toast {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  padding: 6px 14px;
  max-width: 90%;
  border-radius: 999px;
  background: rgba(45, 55, 40, 0.88);
  color: #fff;
  font-size: 0.9rem;
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}

#toast.shown {
  opacity: 1;
}

#controls {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 8px;
}

#controls .big {
  flex: 1 1 auto;
  padding-left: 16px;
  padding-right: 16px;
}

/* On a computer everything is centred and the big button doesn't need to stretch */
@media (min-width: 700px) and (min-height: 600px) {
  main::after {
    flex: 1;
  }

  #controls .big {
    flex: none;
  }
}

#stars {
  font-size: 2.2rem;
  letter-spacing: 6px;
  line-height: 1;
  margin-bottom: 8px;
}

#stars span {
  display: inline-block;
  color: #ddd5bf;
}

#stars .on {
  color: #b87f0c;
  animation: pop 0.4s backwards;
}

@keyframes pop {
  from { transform: scale(0); }
  70% { transform: scale(1.3); }
  to { transform: scale(1); }
}

dialog .row {
  flex-wrap: wrap;
  align-items: center;
}

/* main.js sizes the board to fit the stage */
#board {
  display: block;
  aspect-ratio: 1;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(60, 70, 50, 0.2);
  /* Taps only: no scrolling, zooming or rubber-band bounce when a finger lands on the board */
  touch-action: none;
  cursor: pointer;
}

button,
.button {
  display: inline-block;
  text-align: center;
  line-height: 1.2;
  font: inherit;
  color: inherit;
  text-decoration: none;
  background: #e6dfca;
  border: none;
  border-radius: 999px;
  padding: 10px 20px;
  cursor: pointer;
  touch-action: manipulation;
}

.big {
  background: #b85c26;
  color: #fff;
  font-size: 1.15rem;
  font-weight: 600;
  padding: 14px 32px;
}

button:disabled {
  opacity: 0.5;
  cursor: default;
}

dialog {
  width: min(360px, calc(100% - 32px));
  border: none;
  border-radius: 16px;
  padding: 24px;
  background: #fbf8ef;
  color: inherit;
  text-align: center;
}

dialog::backdrop {
  background: rgba(40, 50, 35, 0.4);
}

dialog h2 {
  margin: 0 0 16px;
  font-size: 1.3rem;
}

dialog h3 {
  margin: 16px 0 8px;
  font-size: 1rem;
}

dialog .row {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin-top: 16px;
}

#next-puzzle {
  margin-top: 10px;
}

.sizes {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 6px;
}

.sizes .button {
  padding: 8px 12px;
}

#archive-list {
  max-height: 34vh;
  overflow-y: auto;
  margin: 0;
  padding: 0;
  list-style: none;
  text-align: left;
}

#archive-list a {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  color: inherit;
  text-decoration: none;
  border-bottom: 1px solid #e6dfca;
}

#archive-list .played {
  color: #5f6a5a;
  white-space: nowrap;
}

.scores {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin-bottom: 20px;
}

.scores b {
  display: block;
  font-size: 2.2rem;
}

#help {
  text-align: left;
}

#help h2,
#help form {
  text-align: center;
}

#help p {
  margin: 0 0 12px;
  line-height: 1.4;
}

.example {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 16px;
  margin-bottom: 12px;
  text-align: center;
  font-size: 0.85rem;
}

.example canvas {
  display: block;
  width: 84px;
  aspect-ratio: 1;
  margin-bottom: 4px;
  border-radius: 6px;
}

.example .arrow {
  font-size: 1.4rem;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  margin-bottom: 20px;
  font-size: 0.9rem;
}

.stats b {
  display: block;
  font-size: 2rem;
}

.toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 20px;
  font-size: 1.05rem;
}

.toggle input {
  width: 22px;
  height: 22px;
  accent-color: #b85c26;
}

textarea {
  display: block;
  width: 100%;
  margin-top: 12px;
  font: inherit;
  font-size: 0.8rem;
  line-height: 1.2;
}

.small {
  font-size: 0.85rem;
  color: #5f6a5a;
}

/* A clear ring round whatever has keyboard focus */
:focus-visible {
  outline: 3px solid #2b5f9e;
  outline-offset: 3px;
}

/* For screen readers only */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Two buttons each side of the title only just fit on a phone */
@media (max-width: 400px) {
  h1 {
    font-size: 1.35rem;
  }

  button.icon {
    width: 34px;
    height: 34px;
  }
}

@media (max-width: 360px) {
  h1 {
    font-size: 1.05rem;
  }

  header,
  header div {
    gap: 4px;
  }

  button.icon {
    width: 30px;
    height: 30px;
  }

  button.icon svg {
    width: 18px;
    height: 18px;
  }
}

/* Dialogs fade and slide in */
dialog[open] {
  animation: dialog-in 0.22s ease-out;
}

dialog[open]::backdrop {
  animation: fade-in 0.22s ease-out;
}

@keyframes dialog-in {
  from {
    opacity: 0;
    transform: translateY(18px);
  }
}

@keyframes fade-in {
  from {
    opacity: 0;
  }
}

/* A small picture of the finished maze on the results */
#result-board {
  display: block;
  width: 150px;
  aspect-ratio: 1;
  margin: 0 auto 14px;
  border-radius: 8px;
}

/* Keep the results card on screen on short phones */
@media (max-height: 700px) {
  dialog {
    padding: 16px;
  }

  #stars {
    font-size: 1.6rem;
    margin-bottom: 4px;
  }

  dialog h2 {
    margin-bottom: 8px;
  }

  #result-board {
    width: 96px;
    margin-bottom: 8px;
  }

  .scores {
    margin-bottom: 10px;
  }

  .scores b {
    font-size: 1.6rem;
  }

  .big {
    padding-top: 10px;
    padding-bottom: 10px;
  }

  dialog .row {
    margin-top: 8px;
  }

  button,
  .button {
    padding-top: 8px;
    padding-bottom: 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after,
  dialog[open]::backdrop {
    animation: none !important;
    transition: none !important;
  }
}
