:root {
  --paper: #f4ead6;
  --paper-2: #eadbbd;
  --ink: #3b2c28;
  --ink-soft: rgba(59, 44, 40, 0.28);
  --clay: #dd7f5a;
  --mustard: #ecbd52;
  --sage: #a3c084;
  --sky: #96bfcf;
  --rose: #eaa8a0;
  --plum: #9f82a6;
  --night: #30365a;
  --hand: 'Neucha', 'Comic Sans MS', cursive;
  --title: 'Caveat', 'Neucha', cursive;
  --wobbly: 255px 18px 225px 18px / 18px 225px 18px 255px;
  --wobbly-2: 18px 225px 20px 255px / 255px 20px 225px 18px;
}

* { box-sizing: border-box; }

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

body {
  font-family: var(--hand);
  color: var(--ink);
  background-color: var(--paper);
  /* paper fibres */
  background-image:
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 0.23  0 0 0 0 0.17  0 0 0 0 0.15  0 0 0 0.09 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>"),
    radial-gradient(ellipse at 20% 10%, rgba(236, 189, 82, 0.12), transparent 55%),
    radial-gradient(ellipse at 85% 90%, rgba(163, 192, 132, 0.16), transparent 50%);
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.defs { position: absolute; }

button { font: inherit; color: inherit; }

/* ---------- layout ---------- */

#app {
  height: 100vh;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: max(10px, env(safe-area-inset-top)) 16px max(10px, env(safe-area-inset-bottom));
  gap: 10px;
}

.hud {
  display: flex;
  align-items: stretch;
  gap: 10px;
}

#stage {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

.field {
  position: relative;
  border-radius: 22px;
}

.field canvas {
  display: block;
  border-radius: 20px;
  cursor: pointer;
}

/* hand-drawn frame around the sky */
.field::after {
  content: "";
  position: absolute;
  inset: -5px;
  border: 5px solid var(--ink);
  border-radius: 26px 20px 28px 18px / 18px 28px 20px 26px;
  filter: url(#wobble);
  pointer-events: none;
}

.field::before {
  content: "";
  position: absolute;
  inset: -2px -4px -9px 3px;
  border-radius: 24px;
  background: var(--ink-soft);
  z-index: -1;
  filter: url(#wobble);
}

/* ---------- cards / hud ---------- */

.card {
  position: relative;
  isolation: isolate;
  padding: 4px 12px 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 0;
}

.card::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #fbf3e2;
  border: 3px solid var(--ink);
  border-radius: var(--wobbly-2);
  box-shadow: 3px 4px 0 var(--ink-soft);
  filter: url(#wobble);
}

.score-card { flex: 1; }

.label {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  font-size: 15px;
  line-height: 1;
  opacity: 0.7;
}

.value {
  font-family: var(--title);
  font-weight: 700;
  font-size: 32px;
  line-height: 1;
}

.next-card { width: 84px; padding-bottom: 2px; }
.next-slot { width: 44px; height: 44px; display: flex; align-items: center; justify-content: center; }

.icon-btn {
  position: relative;
  isolation: isolate;
  width: 56px;
  border: none;
  background: none;
  padding: 0;
  cursor: pointer;
  flex: none;
}

.icon-btn::before {
  content: "";
  position: absolute;
  inset: 4px 0;
  z-index: -1;
  background: var(--mustard);
  border: 3px solid var(--ink);
  border-radius: var(--wobbly);
  box-shadow: 3px 4px 0 var(--ink-soft);
  filter: url(#wobble);
}

.icon-btn .ico { width: 36px; height: 36px; stroke-width: 4.5; }

.icon-btn:active { transform: translate(2px, 3px); }

/* ---------- chain ---------- */

.chain {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 2px;
  min-height: 34px;
  flex-wrap: nowrap;
}

.chain canvas { flex: none; }

.chain .arrow {
  width: 8px;
  height: 8px;
  border-top: 2.5px solid var(--ink);
  border-right: 2.5px solid var(--ink);
  transform: rotate(45deg);
  opacity: 0.45;
  flex: none;
  margin: 0 1px;
}

@media (max-height: 640px) { .chain { display: none; } }

/* ---------- toast ---------- */

.toast {
  position: absolute;
  top: 14%;
  left: 50%;
  transform: translate(-50%, -20px) rotate(-2deg);
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 18px 8px 10px;
  isolation: isolate;
  white-space: nowrap;
  font-size: 20px;
}

.toast::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #fbf3e2;
  border: 3px solid var(--ink);
  border-radius: var(--wobbly);
  box-shadow: 3px 4px 0 rgba(0, 0, 0, .25);
  filter: url(#wobble);
}

.toast.show { opacity: 1; transform: translate(-50%, 0) rotate(-2deg); }
.toast b { font-family: var(--title); font-size: 26px; }

/* ---------- buttons ---------- */

.btn {
  position: relative;
  isolation: isolate;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  border: none;
  background: none;
  padding: 10px 24px 12px;
  font-size: 23px;
  cursor: pointer;
  transition: transform .12s;
  --btn-bg: var(--paper-2);
}

.btn::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: var(--btn-bg);
  border: 3px solid var(--ink);
  border-radius: var(--wobbly);
  box-shadow: 4px 5px 0 var(--ink-soft);
  filter: url(#wobble);
  transition: box-shadow .12s;
}

/* crayon hatching inside buttons */
.btn::after {
  content: "";
  position: absolute;
  inset: 5px 8px;
  z-index: -1;
  border-radius: var(--wobbly);
  background: repeating-linear-gradient(-55deg, rgba(59, 44, 40, 0.07) 0 2px, transparent 2px 7px);
  pointer-events: none;
}

.btn:hover { transform: rotate(-1deg) scale(1.02); }
.btn:active { transform: translate(3px, 4px); }
.btn:active::before { box-shadow: 0 0 0 var(--ink-soft); }
.btn:focus-visible { outline: 3px dashed var(--clay); outline-offset: 4px; }

.btn[hidden] { display: none; }

.btn-main { --btn-bg: var(--mustard); font-size: 28px; font-family: var(--title); font-weight: 700; }
.btn[data-tone="sage"] { --btn-bg: var(--sage); }
.btn[data-tone="sky"] { --btn-bg: var(--sky); }
.btn[data-tone="rose"] { --btn-bg: var(--rose); }
.btn[data-tone="clay"] { --btn-bg: var(--clay); }
.btn[data-tone="paper"] { --btn-bg: #fbf3e2; }
.btn-small { font-size: 19px; padding: 7px 18px 9px; }

/* ---------- screens ---------- */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 10;
  overflow-y: auto;
}

.screen:not(.dim) { background: var(--paper); }
.screen.dim { background: rgba(48, 40, 52, 0.55); }

.screen.active { display: flex; animation: appear .25s ease-out; }

@keyframes appear {
  from { opacity: 0; transform: scale(.97); }
  to { opacity: 1; transform: none; }
}

.sheet {
  position: relative;
  isolation: isolate;
  width: 100%;
  max-width: 400px;
  margin: auto;
  padding: 26px 26px 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: stretch;
  text-align: center;
}

.sheet::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #fbf3e2;
  border: 4px solid var(--ink);
  border-radius: 30px 14px 34px 16px / 16px 34px 14px 30px;
  box-shadow: 6px 8px 0 var(--ink-soft);
  filter: url(#wobble);
}

.menu-sheet::before { background: #fbf3e2; }

h2 {
  margin: 0;
  font-family: var(--title);
  font-weight: 700;
  font-size: 40px;
  line-height: 1;
}

.title {
  margin: -6px 0 0;
  font-family: var(--title);
  font-weight: 700;
  font-size: 96px;
  line-height: .85;
  color: var(--clay);
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  text-shadow: 4px 5px 0 var(--ink-soft);
  transform: rotate(-3deg);
  filter: url(#wobble);
}

.subtitle {
  margin: -6px 0 6px;
  font-size: 24px;
  opacity: .8;
}

.menu-buttons { display: flex; flex-direction: column; gap: 14px; }

.menu-best { margin: 4px 0 0; font-size: 20px; display: flex; align-items: center; justify-content: center; gap: 6px; }
.menu-best b { font-family: var(--title); font-size: 28px; }

.menu-sky {
  position: relative;
  height: 120px;
  margin: -6px -6px 0;
  border-radius: 20px 12px 22px 10px;
  background: var(--night);
  overflow: hidden;
}

.menu-sky .star {
  position: absolute;
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: #f6ead0;
  opacity: .7;
}

.menu-sky canvas {
  position: absolute;
  animation: bob 3.2s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-3deg); }
  50% { transform: translateY(-7px) rotate(3deg); }
}

.hint { margin: 0; font-size: 17px; opacity: .75; }

.howto-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: var(--title);
  font-weight: 700;
  font-size: 40px;
}

.howto-list {
  text-align: left;
  margin: 0;
  padding-left: 24px;
  font-size: 19px;
  line-height: 1.3;
}

.howto-list li { margin-bottom: 6px; }

/* ---------- atlas ---------- */

.atlas-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px 8px;
}

.atlas-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.atlas-item .name { font-family: var(--title); font-weight: 700; font-size: 22px; line-height: 1; }
.atlas-item .about { font-size: 13px; line-height: 1.15; opacity: .75; }
.atlas-item.locked .name { opacity: .5; }

/* ---------- settings ---------- */

.setting span { display: inline-flex; align-items: center; gap: 10px; }

.setting {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 22px;
  padding: 2px 4px;
}

.toggle {
  position: relative;
  isolation: isolate;
  width: 96px;
  height: 42px;
  border: none;
  background: none;
  cursor: pointer;
  font-size: 19px;
}

.toggle::before {
  content: "";
  position: absolute;
  inset: 0;
  z-index: -1;
  background: #e5d9c3;
  border: 3px solid var(--ink);
  border-radius: var(--wobbly);
  filter: url(#wobble);
}

.toggle.on::before { background: var(--sage); }
.toggle::after { content: "off"; }
.toggle.on::after { content: "on"; }

/* ---------- game over ---------- */

.over-planet { display: flex; justify-content: center; }
.over-caption { margin: -6px 0 0; font-size: 19px; }
.over-stats { display: flex; gap: 12px; }

.new-record {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 0;
  font-family: var(--title);
  font-weight: 700;
  font-size: 32px;
  color: var(--clay);
  transform: rotate(-3deg);
  animation: bob 1.6s ease-in-out infinite;
}

.new-record[hidden] { display: none; }

/* ---------- pencil icons ---------- */

.ico {
  width: 28px;
  height: 28px;
  flex: none;
  fill: none;
  stroke: var(--ink);
  stroke-width: 4;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: url(#wobble-icon);
  overflow: visible;
}

.btn-main .ico { --ico-fill: #fbf3e2; width: 30px; height: 30px; }
.ico-small { width: 22px; height: 22px; stroke-width: 3.8; }
.ico-tiny { width: 15px; height: 15px; stroke-width: 4.4; }
.new-record .ico { width: 24px; height: 24px; --ico-fill: var(--mustard); }
.howto-row .ico { width: 30px; height: 30px; stroke-width: 4.2; }

/* ---------- splash ---------- */

#splash {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #08061a;
  cursor: pointer;
  transition: opacity .6s ease, visibility .6s;
}

#splash.out { opacity: 0; visibility: hidden; pointer-events: none; }

.splash-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.splash-logo {
  width: min(42vw, 180px);
  height: auto;
  animation: logo-in .9s cubic-bezier(.2, 1.5, .4, 1) both;
}

.splash-name {
  margin-top: 22px;
  font-family: 'Russo One', 'Nunito', sans-serif;
  font-size: clamp(34px, 10vw, 58px);
  line-height: 1;
  color: #fff;
  letter-spacing: .02em;
  animation: rise-in .7s ease-out .55s both;
}

.splash-presents {
  margin-top: 16px;
  padding-left: .55em;
  font-family: 'Nunito', sans-serif;
  font-weight: 600;
  font-size: clamp(14px, 3.6vw, 20px);
  letter-spacing: .55em;
  color: #fff;
  animation: rise-in .7s ease-out .95s both;
}

@keyframes logo-in {
  from { opacity: 0; transform: scale(.6) rotate(-8deg); }
  to { opacity: 1; transform: none; }
}

@keyframes rise-in {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .splash-logo, .splash-name, .splash-presents { animation: none; }
}
