:root {
  --paper: #F3EDE1;
  --paper-2: #E8E0D0;
  --ink: #1B1A17;
  --ink-2: #5A554C;
  --red: #D6402B;
  --r0: #F7A1C4; --r1: #FFD95A; --r2: #7FD1C7; --r3: #8FB3F5;
  --r4: #FFAD6B; --r5: #B7E07A; --r6: #C9B3F2; --r7: #F58C7B;
  --gut: 16px;
  --display: "Bowlby One", "Arial Black", sans-serif;
  --body: "Bricolage Grotesque", system-ui, sans-serif;
  --mono: "Martian Mono", ui-monospace, monospace;
}

* { box-sizing: border-box; margin: 0; }
[hidden] { display: none !important; }
html, body { height: 100%; background: var(--paper); color: var(--ink); }
body {
  font-family: var(--body);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

.app {
  height: 100svh;
  max-width: 560px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto auto;
  gap: 10px;
  padding: max(12px, env(safe-area-inset-top)) var(--gut) max(12px, env(safe-area-inset-bottom));
}

.top { display: flex; align-items: baseline; justify-content: space-between; border-bottom: 3px solid var(--ink); padding-bottom: 6px; }
.mark { font-family: var(--display); font-weight: 400; font-size: clamp(28px, 8vw, 40px); letter-spacing: -0.02em; line-height: 1; }
.modes { display: flex; gap: 14px; }
.mode { font-family: var(--mono); font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; padding: 6px 0; color: var(--ink-2); }
.mode[aria-pressed="true"] { color: var(--ink); box-shadow: inset 0 -3px 0 var(--ink); }

.status { display: grid; grid-template-columns: 1fr auto auto auto; align-items: center; gap: 12px; font-family: var(--mono); font-size: 12px; }
.me { display: flex; align-items: center; gap: 8px; min-width: 0; text-align: left; }
.me img { width: 34px; height: 34px; border-radius: 50%; border: 2px solid var(--ink); object-fit: cover; background: var(--paper-2); flex: none; }
.me span { font-family: var(--body); font-weight: 800; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.label { text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-2); }
.lives { display: flex; gap: 3px; }
.lives svg { width: 16px; height: 16px; }
.clock { min-width: 4ch; text-align: right; font-variant-numeric: tabular-nums; }

.stage { position: relative; display: grid; place-items: center; min-height: 0; }
.board {
  display: grid;
  border: 3px solid var(--ink);
  touch-action: none;
  background: var(--ink);
}
.cell { position: relative; display: grid; place-items: center; border-style: solid; border-color: var(--ink); }
.cell.ex::before {
  content: ""; position: absolute; inset: 0;
  background: repeating-linear-gradient(135deg, rgba(27,26,23,.13) 0 2px, transparent 2px 7px);
}
.cell .x { width: 34%; height: 34%; opacity: .7; }
.cell .pet {
  width: 82%; height: 82%; border-radius: 50%; border: 2px solid var(--ink); object-fit: cover;
  background: var(--paper); pointer-events: none; -webkit-touch-callout: none; animation: pop .42s cubic-bezier(.3, 1.6, .5, 1) both;
}
.cell.bad { animation: shake .35s; background-color: var(--red) !important; }
.board.won .pet { animation: hop .6s cubic-bezier(.3, 1.6, .5, 1) both; animation-delay: calc(var(--i) * 70ms); }
@keyframes pop { from { transform: scale(0) rotate(-25deg); } to { transform: scale(1) rotate(0); } }
@keyframes hop { 0% { transform: translateY(0); } 40% { transform: translateY(-28%) rotate(8deg); } 100% { transform: translateY(0); } }
@keyframes shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

.skeleton { position: absolute; width: min(100%, 60svh); aspect-ratio: 1; background: var(--paper-2); border: 3px solid var(--ink-2); animation: pulse 1.1s ease-in-out infinite alternate; }
.skeleton[hidden] { display: none; }
@keyframes pulse { to { opacity: .45; } }

.actions { display: grid; grid-template-columns: 1fr 1fr 1fr 48px; border: 3px solid var(--ink); }
.act { padding: 12px 4px; font-weight: 800; font-size: 15px; border-right: 3px solid var(--ink); }
.act:last-child { border-right: 0; }
.act:active { background: var(--ink); color: var(--paper); }
.act:disabled { color: var(--ink-2); opacity: .5; }
.act.icon { display: grid; place-items: center; }
.act.icon svg { width: 20px; height: 20px; }
#hintsLeft { font-family: var(--mono); font-weight: 400; font-size: 11px; }
.rule { font-size: 13px; line-height: 1.35; color: var(--ink-2); }

/* Bottom sheet: flat paper slab with an ink rule on top. */
.sheet { position: fixed; inset: 0; background: rgba(27,26,23,.45); display: flex; align-items: flex-end; justify-content: center; z-index: 10; }
.sheet[hidden] { display: none; }
.sheet-body {
  width: 100%; max-width: 560px; max-height: 100svh;
  background: var(--paper); border-top: 6px solid var(--ink);
  padding: 22px var(--gut) max(22px, env(safe-area-inset-bottom));
  display: grid; gap: 14px; animation: rise .28s ease-out;
}
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
.sheet h2 { font-family: var(--display); font-weight: 400; font-size: clamp(26px, 7.5vw, 36px); line-height: 1.02; letter-spacing: -0.01em; }
.sheet p { font-size: 16px; line-height: 1.4; }
.sheet .stats { display: flex; gap: 22px; font-family: var(--mono); font-size: 12px; text-transform: uppercase; }
.sheet .stats b { display: block; font-family: var(--display); font-weight: 400; font-size: 28px; text-transform: none; }
.btn { padding: 15px 12px; font-weight: 800; font-size: 17px; border: 3px solid var(--ink); text-align: center; }
.btn.main { background: var(--ink); color: var(--paper); }
.btn:disabled { opacity: .55; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.winpet { width: 96px; height: 96px; border-radius: 50%; border: 3px solid var(--ink); object-fit: cover; animation: pop .5s cubic-bezier(.3, 1.6, .5, 1) both; }
.err { color: var(--red); font-weight: 600; font-size: 14px; }
.err:empty { display: none; }

/* Pet editor */
.crop { position: relative; width: min(100%, 42svh); aspect-ratio: 1; margin: 0 auto; overflow: hidden; background: var(--paper-2); touch-action: none; border: 3px solid var(--ink); }
.crop img { position: absolute; left: 50%; top: 50%; transform-origin: 0 0; max-width: none; pointer-events: none; }
.crop::after { content: ""; position: absolute; inset: 0; border-radius: 50%; box-shadow: 0 0 0 999px rgba(243,237,225,.78); outline: 3px solid var(--ink); outline-offset: -3px; pointer-events: none; }
.crop .empty { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 24px; font-size: 15px; color: var(--ink-2); z-index: 1; }
.zoom { width: 100%; accent-color: var(--ink); }
.field { display: grid; gap: 6px; font-family: var(--mono); font-size: 11px; text-transform: uppercase; letter-spacing: .05em; }
.field input { -webkit-user-select: text; user-select: text; font: 600 18px var(--body); padding: 12px; border: 3px solid var(--ink); background: #fff; color: var(--ink); text-transform: none; letter-spacing: 0; border-radius: 0; }

/* Stand-in for the rewarded ad slot. */
.adslot { aspect-ratio: 16/9; border: 3px dashed var(--ink-2); display: grid; place-items: center; font-family: var(--mono); font-size: 12px; text-transform: uppercase; color: var(--ink-2); text-align: center; padding: 16px; }

.oops { position: fixed; left: 0; right: 0; top: 0; z-index: 20; background: var(--red); color: #fff; padding: max(12px, env(safe-area-inset-top)) var(--gut) 12px; font-weight: 600; }
.oops button { text-decoration: underline; font-weight: 800; color: #fff; }

@media (max-height: 640px) { .rule { display: none; } .app { gap: 8px; } }
