:root {
  color-scheme: light;
  --bar: #f6ecd9;
  --paper: #f6ecd9;
  --ink: #3d2f24;
  --muted: #9b8b7a;
  --card: #fffaf0;
  --accent: #ff7a59;
  --shadow: 0 6px 20px rgba(80, 55, 30, 0.18);
}

* {
  box-sizing: border-box;
  /* No double-tap zoom anywhere: tapping a button twice quickly would
     otherwise zoom the whole page on an iPhone. Scrolling still works. */
  touch-action: manipulation;
}

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

html {
  /* iOS tints the status bar area from the page background. */
  background: var(--bar);
}

body {
  position: fixed;
  inset: 0;
  overflow: hidden;
  background: var(--bar);
  color: var(--ink);
  font: 600 15px/1.4 ui-rounded, 'SF Pro Rounded', 'Nunito', system-ui, sans-serif;
  /* Stop the page itself bouncing, zooming or selecting like a website. */
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

button,
label {
  font: inherit;
  color: inherit;
  border: 0;
  background: none;
  padding: 0;
  cursor: pointer;
}

input[type='file'] {
  display: none;
}

/* --- The board ---------------------------------------------------------- */

#board {
  position: absolute;
  inset: 0;
  overflow: hidden;
  touch-action: none;
  cursor: grab;
  background:
    radial-gradient(circle, var(--dot, rgba(120, 90, 60, 0.16)) 1.3px, transparent 1.6px) 0 0 / 22px 22px,
    var(--paper);
}

#empty {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
  padding: 0 40px 90px;
  text-align: center;
  color: var(--muted);
  pointer-events: none;
}

#board.sliding {
  cursor: grabbing;
}

/* The paper is a point at the middle of the board; the view moves and
   scales it, and everything on it follows. */
.paper {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  transform-origin: 0 0;
}

.placed {
  position: absolute;
  left: 0;
  top: 0;
  max-width: none;
  cursor: move;
  touch-action: none;
  filter: drop-shadow(0 3px 5px rgba(60, 40, 20, 0.3));
  will-change: transform;
}

.placed.selected {
  filter: drop-shadow(0 0 2px var(--accent)) drop-shadow(0 6px 10px rgba(60, 40, 20, 0.35));
}

/* Round buttons up the right-hand side, just above the bar. */
#extras {
  position: absolute;
  bottom: calc(env(safe-area-inset-bottom) + 88px);
  right: calc(env(safe-area-inset-right) + 14px);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#extras button {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--card);
  box-shadow: var(--shadow);
  font-size: 20px;
  line-height: 1;
}

#extras button:active {
  background: var(--accent);
}

/* Words and emoji on the board, with a sticker's white edge. */
.placed.words {
  font-weight: 800;
  line-height: 1.2;
  white-space: pre;
  -webkit-text-stroke: 19px #fff;
  paint-order: stroke fill;
}

.placed.doodle {
  overflow: visible;
}

.doodle path,
.ink path {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* The drawing in progress: a sheet with no size of its own, over the paper. */
.ink {
  position: absolute;
  left: 0;
  top: 0;
  width: 1px;
  height: 1px;
  overflow: visible;
  z-index: 100000;
  pointer-events: none;
}

#board.drawing {
  cursor: crosshair;
}

#board.drawing .placed {
  cursor: crosshair;
}

#pens {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 50%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 10px;
  width: max-content;
  max-width: calc(100% - 24px);
  padding: 8px 12px;
  transform: translateX(-50%);
  background: var(--ink);
  border-radius: 22px;
  box-shadow: var(--shadow);
  color: #fff;
}

#pens .swatches button {
  border-color: rgba(255, 255, 255, 0.4);
}

#pen-sizes {
  display: flex;
  gap: 4px;
}

#pen-sizes button {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 50%;
}

#pen-sizes button.on {
  background: rgba(255, 255, 255, 0.25);
}

#pen-sizes i {
  border-radius: 50%;
  background: #fff;
}

#pens > button {
  padding: 7px 12px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
}

#pens #done-drawing {
  background: var(--accent);
}

.swatches {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.swatches button,
#own-colour {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 2px solid rgba(120, 90, 60, 0.25);
}

.swatches .on {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

/* The "any colour" swatch: a rainbow until a colour of your own is picked. */
#own-colour {
  position: relative;
  overflow: hidden;
  background: conic-gradient(#f66, #fd6, #6d8, #6cf, #a7f, #f66);
}

#own-colour input {
  display: block;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
}

#tools {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  left: 50%;
  display: flex;
  gap: 2px;
  transform: translateX(-50%);
  background: var(--ink);
  border-radius: 999px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

#tools button {
  min-width: 44px;
  padding: 10px 14px;
  color: #fff;
  font-size: 16px;
  white-space: nowrap;
}

#tools button:active {
  background: var(--accent);
}

/* --- The bar along the bottom ------------------------------------------- */

nav {
  position: absolute;
  left: 12px;
  right: 12px;
  /* Keep clear of the home indicator when installed. */
  bottom: calc(env(safe-area-inset-bottom) + 12px);
  max-width: 460px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px;
  background: var(--card);
  border-radius: 999px;
  box-shadow: var(--shadow);
}

nav > * {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 52px;
  border-radius: 999px;
  line-height: 1.1;
}

nav b {
  font-size: 19px;
}

nav span {
  font-size: 11px;
  color: var(--muted);
}

nav > *:active {
  background: rgba(120, 90, 60, 0.12);
}

nav .big {
  flex: 2.2;
  background: var(--accent);
}

nav .big span {
  color: #fff;
  font-size: 16px;
  font-weight: 800;
}

nav .big:active {
  background: #e8623f;
}

/* --- The tray of stickers ----------------------------------------------- */

#tray {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  z-index: 3;
  display: flex;
  flex-direction: column;
  max-height: 72%;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--card);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -8px 40px rgba(80, 55, 30, 0.3);
  animation: rise 0.22s ease-out;
}

@keyframes rise {
  from {
    transform: translateY(40%);
    opacity: 0;
  }
}

#tray header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 14px 8px 20px;
}

#tray h2 {
  flex: 1;
  margin: 0;
  font-size: 20px;
}

#tray header button {
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(120, 90, 60, 0.1);
}

#close-tray {
  background: var(--ink) !important;
  color: #fff;
}

#grid {
  display: grid;
  /* As many columns as fit, so the cats stay thumbnail-sized on any screen. */
  grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px;
  padding: 8px 14px;
  overflow: hidden auto;
  overscroll-behavior: contain;
}

#grid button {
  position: relative;
  min-width: 0;
  aspect-ratio: 1;
  padding: 8px;
  border-radius: 16px;
  background: var(--paper);
}

#grid img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 2px 3px rgba(60, 40, 20, 0.3));
  pointer-events: none;
}

.editing #grid button::after {
  content: '×';
  position: absolute;
  top: -4px;
  right: -4px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #d8342a;
  color: #fff;
  font-size: 19px;
  line-height: 26px;
}

#tray-hint {
  margin: 4px 0 14px;
  padding: 0 20px;
  text-align: center;
  color: var(--muted);
  font-size: 13px;
}

/* --- While a photo is being cut out ------------------------------------- */

#darkroom {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 28px;
  background: rgba(40, 30, 22, 0.92);
  color: #fff;
  text-align: center;
}

#preview {
  max-width: 78%;
  max-height: 46%;
  border: 6px solid #fff;
  border-radius: 6px;
  transform: rotate(-2deg);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.busy #preview {
  animation: develop 1.1s ease-in-out infinite alternate;
}

@keyframes develop {
  to {
    transform: rotate(2deg) scale(0.97);
  }
}

#verdict {
  margin: 0;
  max-width: 300px;
  font-size: 17px;
}

#choices {
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 100px;
}

#choices button {
  padding: 12px 22px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  color: #fff;
}

#choices #anyway {
  background: var(--accent);
}

#toast {
  position: absolute;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 92px);
  margin: 0;
  padding: 9px 16px;
  transform: translate(-50%, 12px);
  background: var(--ink);
  color: #fff;
  border-radius: 22px;
  width: max-content;
  max-width: calc(100% - 32px);
  text-align: center;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.25s, transform 0.25s;
}

#toast.on {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* --- Sheets for adding words and emoji, and for saving ------------------ */

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: 100%;
  max-width: 640px;
  max-height: 86%;
  margin: 0 auto;
  padding: 0 16px calc(env(safe-area-inset-bottom) + 16px);
  background: var(--card);
  border-radius: 24px 24px 0 0;
  box-shadow: 0 -8px 40px rgba(80, 55, 30, 0.3);
  animation: rise 0.22s ease-out;
}

.sheet header {
  display: flex;
  align-items: center;
  align-self: stretch;
  padding: 14px 0 0 4px;
}

.sheet h2 {
  flex: 1;
  margin: 0;
  font-size: 20px;
}

.sheet .close {
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(120, 90, 60, 0.1);
}

.sheet .go {
  align-self: stretch;
  padding: 14px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

.sheet .hint {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

#words {
  align-self: stretch;
  padding: 12px 16px;
  border: 2px solid rgba(120, 90, 60, 0.25);
  border-radius: 14px;
  background: #fff;
  color: var(--ink);
  /* 16px or more, or an iPhone zooms the page to the field. */
  font: inherit;
  font-size: 18px;
  user-select: text;
  -webkit-user-select: text;
}

#emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(44px, 1fr));
  gap: 4px;
  align-self: stretch;
  overflow-y: auto;
}

#emoji-grid button {
  aspect-ratio: 1;
  border-radius: 12px;
  font-size: 26px;
}

#emoji-grid button:active {
  background: rgba(120, 90, 60, 0.12);
}

#proof {
  min-height: 0;
  flex-shrink: 1;
}

#proof canvas {
  display: block;
  max-width: 100%;
  max-height: 34vh;
  border-radius: 10px;
  box-shadow: 0 2px 10px rgba(80, 55, 30, 0.25);
}
