:root {
  --bg: #faf8ef;
  --ink: #776e65;
  --accent: #8f7a66;
  --accent-ink: #f9f6f2;
  --box: #bbada0;
}

* { box-sizing: border-box; }

html {
  margin: 0;
  height: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--ink);
  font-family: "Baloo 2", system-ui, -apple-system, sans-serif;
  display: grid;
  place-items: center;
  overflow: hidden;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  /* safe-area gutters so nothing hides behind notch/home bar */
  padding:
    env(safe-area-inset-top, 0px)
    env(safe-area-inset-right, 0px)
    env(safe-area-inset-bottom, 0px)
    env(safe-area-inset-left, 0px);
}

.back {
  position: fixed;
  top: max(10px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  z-index: 10;
  text-decoration: none;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.7);
  border: 1px solid rgba(0, 0, 0, 0.06);
  padding: 0 14px;
  border-radius: 999px;
  font-weight: 600;
  font-size: 14px;
  backdrop-filter: blur(6px);
  /* 44px minimum touch target height */
  min-height: 44px;
  display: inline-flex;
  align-items: center;
}
.back:hover { background: #fff; }

.wrap {
  /*
   * Card-style panel: on large screens (Mac/iPad) the board + HUD sit in a
   * clearly-bounded centered card rather than floating on a bare background.
   * Width is capped at 560px (up from 520px) for better tablet/desktop proportion.
   * Board size is driven by whichever axis is tighter; on landscape phone
   * we fall back to the height-driven path via the boardwrap rule below.
   */
  width: min(94vw, 560px);
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: clamp(14px, 3vw, 28px);
  /* subtle warm card panel */
  background: rgba(187, 173, 160, 0.18);
  border-radius: 20px;
  box-shadow: 0 4px 32px rgba(0, 0, 0, 0.07);
}

/* Prevent the square board from overflowing on short viewports.
   The board is always 100% of .wrap width; cap that width so board height
   + topbar (~90px) + padding always fit in the dvh. */
@media (max-height: 700px) {
  .wrap {
    /* leave room for topbar + gap + card padding */
    width: min(94vw, calc(100dvh - 140px));
  }
}

.topbar {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.brand h1 {
  margin: 0;
  font-size: clamp(40px, 11vw, 64px);
  font-weight: 800;
  letter-spacing: -1px;
  color: #6f655c;
}
.howto {
  margin: 2px 0 0;
  font-size: 13.5px;
  font-weight: 500;
  color: #998c80;
  max-width: 240px;
}
.howto b { color: #6f655c; }

.scores {
  display: flex;
  align-items: center;
  gap: 8px;
}

.scorebox {
  background: var(--box);
  color: var(--accent-ink);
  border-radius: 8px;
  padding: 6px 14px;
  min-width: 64px;
  text-align: center;
  display: flex;
  flex-direction: column;
  line-height: 1.1;
}
.scorebox .label {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: #eee4da;
}
.scorebox span:last-child {
  font-size: 20px;
  font-weight: 800;
}

.newbtn {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 8px;
  padding: 12px 14px;
  font-family: inherit;
  font-weight: 700;
  font-size: 14px;
  cursor: pointer;
  height: 100%;
}
.newbtn:hover { filter: brightness(1.06); }
.newbtn:active { transform: translateY(1px); }

.boardwrap {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
}

#game {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  text-align: center;
  background: rgba(238, 228, 218, 0.78);
  border-radius: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  padding: 24px;
}
.overlay.show {
  opacity: 1;
  pointer-events: auto;
}
.overlay h2 {
  margin: 0;
  font-size: clamp(28px, 7vw, 44px);
  font-weight: 800;
  color: #6f655c;
}
.overlay p {
  margin: 0;
  font-weight: 600;
  color: #877d72;
}
.overlay-actions {
  display: flex;
  gap: 10px;
  margin-top: 8px;
}
.overlay button {
  background: var(--accent);
  color: var(--accent-ink);
  border: none;
  border-radius: 8px;
  padding: 12px 20px;
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  cursor: pointer;
}
.overlay button:hover { filter: brightness(1.06); }
.overlay button.ghost {
  background: transparent;
  color: #6f655c;
  border: 2px solid var(--accent);
}

@media (max-width: 420px) {
  .scores { width: 100%; justify-content: space-between; }
  .howto { max-width: none; }
}
