:root {
  color-scheme: dark;
  --paper: #10191f;
  --ink: #efe6cf;
  --muted: #a9aa9d;
  --ochre: #d4a55b;
  --moss: #9bb46a;
  --line: rgba(239, 230, 207, .18);
  --panel: rgba(12, 20, 25, .84);
  font-family: "Kaiti SC", "STKaiti", "PingFang SC", system-ui, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #080d10;
  color: var(--ink);
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

button { font: inherit; color: inherit; }
button:focus-visible { outline: 2px solid #f1c77d; outline-offset: 3px; }

#app {
  position: relative;
  width: min(100vw, 520px);
  height: 100dvh;
  min-height: 560px;
  margin: 0 auto;
  overflow: hidden;
  isolation: isolate;
  background:
    linear-gradient(rgba(7, 15, 20, .08), rgba(7, 15, 20, .32)),
    url("../assets/bg-handdrawn.png") center / cover no-repeat,
    var(--paper);
  box-shadow: 0 0 70px rgba(0, 0, 0, .58);
}

#app::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
  background: linear-gradient(180deg, rgba(2, 8, 12, .24), transparent 20%, transparent 68%, rgba(3, 8, 10, .48));
}

#stage {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  height: 100%;
  cursor: grab;
  touch-action: none;
}

#stage:active { cursor: grabbing; }

.brand {
  position: absolute;
  z-index: 4;
  top: max(18px, env(safe-area-inset-top));
  left: 20px;
  pointer-events: none;
  filter: drop-shadow(0 2px 6px rgba(0, 0, 0, .5));
}

.brand p {
  margin: 0 0 2px;
  color: var(--ochre);
  font-size: 10px;
  letter-spacing: .32em;
}

.brand h1 {
  margin: 0;
  font-size: clamp(24px, 7vw, 34px);
  font-weight: 800;
  letter-spacing: .06em;
  transform: rotate(-1.5deg);
}

.official-home {
  position: absolute;
  z-index: 8;
  top: calc(max(18px, env(safe-area-inset-top)) + 70px);
  left: 20px;
  padding: 6px 10px;
  border: 1px solid rgba(239, 230, 207, .18);
  border-radius: 11px 14px 10px 13px;
  background: rgba(9, 17, 22, .55);
  color: rgba(239, 230, 207, .78);
  font-size: 10px;
  letter-spacing: .06em;
  line-height: 1;
  text-decoration: none;
  backdrop-filter: blur(8px);
}

.official-home:hover,
.official-home:focus-visible {
  border-color: rgba(212, 165, 91, .65);
  color: #ffe8b7;
}

.official-home:active { transform: scale(.96) rotate(-1deg); }

.top-actions {
  position: absolute;
  z-index: 6;
  top: max(18px, env(safe-area-inset-top));
  right: 14px;
  display: flex;
  gap: 5px;
}

.top-actions button {
  display: grid;
  gap: 1px;
  place-items: center;
  min-width: 44px;
  height: 48px;
  padding: 4px 7px;
  border: 1px solid var(--line);
  border-radius: 15px 13px 16px 12px;
  background: rgba(11, 21, 27, .66);
  backdrop-filter: blur(8px);
  font-size: 10px;
}

.top-actions button span { font: 700 17px system-ui; color: var(--ochre); }
.top-actions button:active { transform: scale(.94) rotate(-1deg); }

.play-status {
  position: absolute;
  z-index: 4;
  top: calc(max(18px, env(safe-area-inset-top)) + 68px);
  right: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border: 1px solid rgba(239, 230, 207, .12);
  border-radius: 999px;
  background: rgba(9, 17, 22, .44);
  color: var(--muted);
  font-size: 11px;
  white-space: nowrap;
  pointer-events: none;
}

.play-status #animalName { color: var(--ink); }
.play-status i { width: 3px; height: 3px; border-radius: 50%; background: var(--ochre); }

.hint {
  position: absolute;
  z-index: 4;
  left: 50%;
  bottom: max(223px, calc(207px + env(safe-area-inset-bottom)));
  display: flex;
  align-items: center;
  gap: 7px;
  transform: translateX(-50%) rotate(-1deg);
  padding: 8px 13px;
  border: 1px dashed rgba(239, 230, 207, .28);
  border-radius: 13px 17px 12px 18px;
  background: rgba(10, 18, 23, .58);
  color: rgba(239, 230, 207, .8);
  font-size: 12px;
  white-space: nowrap;
  pointer-events: none;
  transition: opacity .35s, transform .35s;
}

.hint.gone { opacity: 0; transform: translate(-50%, 7px); }
.hint-loop { color: var(--ochre); font: 22px/1 system-ui; }

.control-dock {
  position: absolute;
  z-index: 5;
  right: 10px;
  bottom: max(10px, env(safe-area-inset-bottom));
  left: 10px;
  padding: 12px 12px 10px;
  border: 1px solid rgba(239, 230, 207, .16);
  border-radius: 24px 20px 28px 18px;
  background: var(--panel);
  box-shadow: 0 16px 45px rgba(0, 0, 0, .4);
  backdrop-filter: blur(16px) saturate(.85);
}

.control-dock::before {
  content: "";
  position: absolute;
  inset: 5px;
  pointer-events: none;
  border: 1px solid rgba(212, 165, 91, .08);
  border-radius: 20px 17px 23px 15px;
}

.speed-line {
  position: relative;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 10px;
  height: 34px;
}

.speed-line > div:first-child { display: flex; align-items: baseline; gap: 4px; min-width: 82px; }
.speed-line b { color: var(--ochre); font: 800 24px/1 ui-rounded, system-ui; }
.speed-line small, .speed-line > span { color: var(--muted); font-size: 9px; }
.speed-line > span { min-width: 29px; text-align: right; }

.intensity {
  height: 7px;
  overflow: hidden;
  border: 1px solid rgba(239, 230, 207, .13);
  border-radius: 99px;
  background: rgba(255, 255, 255, .05);
}

.intensity i {
  display: block;
  width: 0%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--moss), var(--ochre), #c45c4f);
  transition: width .1s linear;
}

.mode-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
  margin: 6px 0 9px;
}

.mode-btn {
  position: relative;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 5px;
  min-height: 40px;
  border: 1px solid rgba(239, 230, 207, .13);
  border-radius: 13px 11px 15px 10px;
  background: rgba(255, 255, 255, .035);
}

.mode-btn span { font-size: 13px; font-weight: 700; }
.mode-btn small { color: var(--muted); font-size: 9px; }
.mode-btn.active { border-color: rgba(212, 165, 91, .58); background: rgba(212, 165, 91, .14); color: #ffe8b7; }
.mode-btn:disabled { opacity: .38; }

.animal-rail-wrap { position: relative; padding-right: 54px; }
.animal-rail { display: grid; grid-template-columns: repeat(6, minmax(40px, 1fr)); gap: 5px; }

.animal-choice {
  position: relative;
  height: 47px;
  padding: 0;
  overflow: hidden;
  border: 1px solid rgba(239, 230, 207, .12);
  border-radius: 14px 10px 13px 11px;
  background: rgba(0, 0, 0, .15);
}

.animal-choice.active { border-color: var(--ochre); background: rgba(212, 165, 91, .13); transform: translateY(-2px) rotate(-1deg); }
.animal-choice:not(.discovered)::after { content: "?"; position: absolute; right: 3px; top: 2px; width: 13px; height: 13px; border-radius: 50%; background: rgba(8, 13, 16, .82); color: var(--muted); font: 10px/13px system-ui; }

.sprite-thumb {
  position: absolute;
  inset: 0;
  background-image: url("../assets/animals-handdrawn.png");
  background-repeat: no-repeat;
  background-size: 300% 200%;
  background-position: var(--x) var(--y);
  filter: saturate(.88) drop-shadow(0 2px 3px rgba(0, 0, 0, .45));
}

.collection-count {
  position: absolute;
  top: 50%;
  right: 0;
  width: 49px;
  transform: translateY(-50%);
  color: var(--muted);
  text-align: center;
  font-size: 8px;
  line-height: 1.25;
}
.collection-count b { display: block; color: var(--ochre); font: 800 17px/1 system-ui; }

.toast, .unlock {
  position: absolute;
  z-index: 20;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  padding: 9px 14px;
  border: 1px solid rgba(212, 165, 91, .38);
  border-radius: 15px 12px 16px 10px;
  background: rgba(8, 15, 19, .93);
  box-shadow: 0 10px 35px rgba(0, 0, 0, .35);
  color: #f3ddb0;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transition: opacity .24s, transform .24s;
}
.toast { bottom: max(226px, calc(210px + env(safe-area-inset-bottom))); }
.toast.show, .unlock:not([hidden]) { opacity: 1; transform: translateX(-50%); }
.unlock { top: 48%; pointer-events: auto; }

.hidden { display: none !important; }

.overlay {
  position: absolute;
  z-index: 30;
  inset: 0;
  display: grid;
  place-items: end center;
  padding: 18px 12px max(14px, env(safe-area-inset-bottom));
  background: rgba(3, 8, 11, .62);
  backdrop-filter: blur(8px);
}

.overlay-card {
  position: relative;
  width: 100%;
  max-height: min(82dvh, 700px);
  overflow: auto;
  padding: 24px 18px 18px;
  border: 1px solid rgba(239, 230, 207, .2);
  border-radius: 28px 22px 30px 18px;
  background: #121e24;
  box-shadow: 0 24px 70px rgba(0, 0, 0, .52);
  text-align: center;
  touch-action: pan-y;
}

.overlay-card h2 { margin: 0 40px 4px; color: #f4e5c2; font-size: 23px; }
.overlay-card > p { margin: 5px 0 15px; color: var(--muted); font-size: 12px; }
.overlay-close { position: absolute; top: 13px; right: 13px; width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 50%; background: transparent; }

.challenge-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin: 14px 0; text-align: left; }
.challenge-option { min-height: 76px; padding: 10px; border: 1px solid var(--line); border-radius: 15px 11px 16px 12px; background: rgba(255,255,255,.025); }
.challenge-option.active { border-color: var(--ochre); background: rgba(212,165,91,.12); }
.challenge-option b, .challenge-option small { display: block; }
.challenge-option b { margin-bottom: 4px; font-size: 13px; }
.challenge-option small { color: var(--muted); font-size: 10px; line-height: 1.35; }

.primary-btn, .secondary-btn { min-height: 44px; padding: 0 18px; border-radius: 15px 12px 16px 11px; font-weight: 700; }
.primary-btn { border: 1px solid #e8c27d; background: #c7954e; color: #172027; }
.secondary-btn { border: 1px solid var(--line); background: transparent; color: var(--ink); }
.button-row { display: flex; justify-content: center; gap: 8px; margin-top: 13px; }

.challenge-hud {
  position: absolute;
  z-index: 14;
  top: max(96px, calc(env(safe-area-inset-top) + 82px));
  right: 12px;
  left: 12px;
  display: grid;
  grid-template-columns: auto 1fr 1fr;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 17px 13px 18px 12px;
  background: rgba(8, 16, 21, .82);
  backdrop-filter: blur(10px);
}
.challenge-hud button { border: 0; background: none; color: var(--muted); font-size: 10px; }
.challenge-hud > div { display: flex; align-items: baseline; justify-content: center; gap: 3px; }
.challenge-hud b { color: var(--ochre); font: 800 23px system-ui; }
.challenge-hud small { color: var(--muted); font-size: 9px; }
.challenge-progress { position: absolute; right: 10px; bottom: -6px; left: 10px; height: 3px; overflow: hidden; border-radius: 10px; background: rgba(255,255,255,.08); }
.challenge-progress i { display: block; width: 0; height: 100%; background: var(--ochre); }

.collection-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin-top: 15px; }
.collection-card { display: grid; grid-template-columns: 62px 1fr; align-items: center; min-height: 76px; padding: 6px; border: 1px solid var(--line); border-radius: 16px 12px 14px 11px; text-align: left; }
.collection-card .sprite-box { position: relative; width: 58px; height: 58px; overflow: hidden; }
.collection-card:not(.discovered) .sprite-box { filter: grayscale(1) brightness(.38); }
.collection-card b { display: block; font-size: 12px; }
.collection-card small { display: block; margin-top: 4px; color: var(--muted); font-size: 9px; line-height: 1.35; }
.big-result { margin: 12px 0 0; color: var(--ochre); font: 900 62px/1 system-ui; }
.result-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; margin: 14px 0; }
.result-stats div { padding: 9px 4px; border: 1px solid var(--line); border-radius: 12px; color: var(--muted); font-size: 9px; }
.result-stats b { display: block; margin-bottom: 3px; color: var(--ink); font: 800 18px system-ui; }
.poster-img { display: block; width: min(100%, 300px); margin: 12px auto; border-radius: 12px; box-shadow: 0 8px 25px rgba(0,0,0,.4); }
.no-js { padding: 24px; }

@media (min-width: 700px) {
  body { padding: 16px 0; }
  #app { height: calc(100dvh - 32px); min-height: 650px; border: 1px solid rgba(239,230,207,.15); border-radius: 34px; }
}

@media (max-height: 680px) {
  .brand p { display: none; }
  .brand h1 { font-size: 23px; }
  .control-dock { padding-top: 9px; }
  .hint { bottom: max(210px, calc(194px + env(safe-area-inset-bottom))); }
  .mode-row { margin: 4px 0 7px; }
}

@media (max-width: 350px) {
  .top-actions { gap: 3px; right: 10px; }
  .top-actions button { min-width: 40px; padding-inline: 4px; }
  .animal-rail-wrap { padding-right: 0; }
  .collection-count { display: none; }
  .animal-choice { height: 44px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: .01ms !important; }
}
