:root {
  color-scheme: light;
  font-family: "Microsoft YaHei", "PingFang SC", "Noto Sans CJK SC", sans-serif;
  color: #473b33;
  background: #eee4d7;
}

* { box-sizing: border-box; }

html, body { margin: 0; min-height: 100%; }

body {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  background: radial-gradient(circle at 50% 4%, #fff9ee 0, #eee4d7 56%, #d9c9b8 100%);
}

button { font: inherit; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

.game-shell {
  width: min(100%, 820px);
  height: min(100dvh, 980px);
  min-height: 520px;
  display: flex;
  flex-direction: column;
  padding: max(10px, env(safe-area-inset-top)) 12px max(5px, env(safe-area-inset-bottom));
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 3px 7px 11px;
}

.heading { min-width: 0; }
h1 { font-size: clamp(21px, 4.6vw, 29px); line-height: 1.2; margin: 0 0 4px; letter-spacing: .03em; }
.heading p { font-size: clamp(12px, 2.8vw, 15px); line-height: 1.35; margin: 0; color: #756358; }

.top-actions { display: flex; flex: none; gap: 6px; }

.sound-toggle, .scene-toggle {
  flex: none;
  min-height: 48px;
  border: 1px solid #b7a38e;
  border-radius: 999px;
  background: rgba(255, 250, 243, .9);
  color: #584639;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
}
.sound-toggle { min-width: 70px; }
.scene-toggle { min-width: 84px; padding: 0 10px; font-size: 13px; white-space: nowrap; }
.scene-toggle span:first-child { font-size: 19px; line-height: 1; }

.sound-toggle:focus-visible, .scene-toggle:focus-visible, .cat:focus-visible, .action-button:focus-visible {
  outline: 3px solid #9a674b;
  outline-offset: 2px;
}

.sound-toggle[aria-pressed="false"] { opacity: .72; }
#sound-icon { font-size: 21px; line-height: 1; }
.sound-label { font-size: 13px; }

.room {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  border-radius: 22px;
  background: #ddcfbd url("./assets/images/runtime/room.webp") center 52% / cover no-repeat;
  box-shadow: 0 14px 34px rgba(74, 52, 31, .16);
  isolation: isolate;
}
.room[data-scene="yard"] {
  background-image: url("./assets/images/runtime/yard.webp");
  background-position: center center;
}
.room[data-scene="balcony"] {
  background-image: url("./assets/images/runtime/balcony.webp");
  background-position: center center;
}
.room[data-scene="study"] {
  background-image: url("./assets/images/runtime/study.webp");
  background-position: center center;
}
.room[data-scene="lawn"] {
  background-image: url("./assets/images/runtime/lawn.webp");
  background-position: center center;
}

.room-shade {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(180deg, transparent 55%, rgba(43, 32, 24, .08) 77%, rgba(43, 32, 24, .29) 100%);
}

.cats-layer { position: absolute; inset: 0; }

.start-panel {
  position: absolute;
  z-index: 200;
  top: 14px;
  left: 0;
  right: 0;
  display: flex;
  justify-content: center;
  pointer-events: none;
}
.start-panel[hidden] { display: none; }
.start-button {
  pointer-events: auto;
  min-height: 58px;
  padding: 7px 20px;
  border: 1px solid #a27b5b;
  border-radius: 18px;
  background: rgba(255, 249, 238, .96);
  box-shadow: 0 5px 18px rgba(55, 42, 30, .16);
  color: #584137;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
}
.start-button span { display: block; font-size: 11px; font-weight: 400; margin-top: 1px; }
.start-button:focus-visible { outline: 3px solid #9a674b; outline-offset: 2px; }

.cat {
  position: absolute;
  width: clamp(145px, 23vw, 198px);
  aspect-ratio: 1;
  padding: 0;
  background: transparent;
  border: 0;
  transform: translate(-50%, -100%) scale(var(--depth-scale, 1));
  transform-origin: center bottom;
  opacity: 1;
  cursor: pointer;
  transition: left var(--walk-duration, 3200ms) linear, top var(--walk-duration, 3200ms) linear, transform var(--walk-duration, 3200ms) linear, opacity 1.2s ease, filter .4s ease;
  filter: drop-shadow(0 9px 7px rgba(57, 42, 31, .25));
  touch-action: manipulation;
}

.cat[data-state="entering"], .cat[data-state="leaving"] { opacity: 0; }
.cat[data-selected="true"] { z-index: 100 !important; filter: drop-shadow(0 10px 9px rgba(57, 42, 31, .30)); }

.cat-facing {
  display: block;
  position: absolute;
  inset: 0;
  transition: transform .4s ease;
}
.cat[data-facing="left"] .cat-facing { transform: scaleX(-1); }

.cat-breathe {
  display: block;
  position: absolute;
  inset: 0;
  animation: breathe 3.6s ease-in-out infinite;
}

.cat[data-walking="true"] .cat-breathe { animation: walk-bob .57s ease-in-out infinite alternate; }
.cat[data-petting="true"] .cat-breathe { animation: breathe 3.6s ease-in-out infinite; }

.pose {
  position: absolute;
  inset: 0;
  background-image: var(--cat-image);
  background-repeat: no-repeat;
  background-size: 200% 100%;
  pointer-events: none;
  transition: none;
}

.pose-stand { background-position: left center; opacity: 1; }
.pose-rest { background-position: right center; opacity: 0; }
.cat[data-id="british"] .pose-rest { clip-path: inset(0 0 0 13%); }
.pose-flop {
  inset: 0 -22%;
  background-image: var(--flop-image);
  background-size: contain;
  background-position: center bottom;
  transform: translateX(var(--flop-shift, 0px));
  opacity: 0;
}
.pose-walk {
  background-image: var(--walk-image);
  background-size: 200% 200%;
  background-position: left top;
  opacity: 0;
}
.cat[data-pose="rest"] .pose-stand { opacity: 0; }
.cat[data-pose="rest"] .pose-rest { opacity: 1; }
.cat[data-pose="flop"] .pose-stand { opacity: 0; }
.cat[data-pose="flop"] .pose-flop { opacity: 1; }
.cat[data-walking="true"] .pose-stand,
.cat[data-walking="true"] .pose-rest,
.cat[data-walking="true"] .pose-flop { opacity: 0; }
.cat[data-walking="true"] .pose-walk {
  opacity: 1;
  animation: walk-frames 1.14s step-end infinite;
}

.cat-name {
  position: absolute;
  left: 50%;
  bottom: -8px;
  transform: translateX(-50%) scale(var(--label-scale, 1));
  white-space: nowrap;
  padding: 5px 12px;
  border-radius: 999px;
  color: #4d3c31;
  background: rgba(255, 250, 242, .90);
  box-shadow: 0 2px 7px rgba(57, 42, 31, .13);
  font-size: 13px;
  font-weight: 700;
}

.cat[data-selected="true"] .cat-name { opacity: 0; }

.message {
  position: absolute;
  z-index: 150;
  left: 50%;
  top: 15px;
  max-width: calc(100% - 30px);
  transform: translateX(-50%);
  padding: 8px 15px;
  border-radius: 999px;
  background: rgba(255, 251, 245, .90);
  color: #665349;
  box-shadow: 0 3px 14px rgba(60, 43, 28, .09);
  font-size: 14px;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  pointer-events: none;
}

.actions {
  position: absolute;
  z-index: 200;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  width: min(420px, calc(100% - 24px));
  padding: 10px;
  border-radius: 21px;
  background: rgba(255, 250, 244, .94);
  box-shadow: 0 8px 25px rgba(60, 43, 28, .17);
  backdrop-filter: blur(7px);
}

.actions[hidden] { display: none; }
.actions[data-position="top"] { top: 60px; bottom: auto; }
.selected-name { font-size: 13px; text-align: center; margin-bottom: 8px; color: #6e5747; font-weight: 700; }
.action-buttons { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; }

.action-button {
  min-height: 59px;
  border: 1px solid #d6bba0;
  border-radius: 15px;
  background: #fff8ed;
  color: #4e3d33;
  font-size: 19px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
}
.action-button:active { transform: scale(.97); }
.action-emoji { font-size: 23px; line-height: 1; }
.mini-yarn {
  width: 23px;
  height: 23px;
  border-radius: 50%;
  background: repeating-linear-gradient(30deg, #b78562 0 3px, #dec09a 3px 6px);
  box-shadow: inset -3px -4px 5px rgba(97, 58, 40, .45), inset 3px 3px 4px #f7dcb9;
}

.yarn-ball {
  position: absolute;
  z-index: 90;
  width: 29px;
  height: 29px;
  border-radius: 50%;
  background: repeating-linear-gradient(28deg, #b9845e 0 3px, #d8ad84 3px 6px, #ad7955 6px 8px);
  box-shadow: inset -5px -6px 7px #8a5e45, inset 4px 4px 5px #f5d0a3, 0 5px 6px rgba(62, 39, 25, .3);
  opacity: 0;
  pointer-events: none;
}

.yarn-ball.rolling { animation: roll-ball var(--ball-duration, 2600ms) ease-in-out forwards; }

.footer { padding: 9px 0 2px; text-align: center; color: #806f61; font-size: 12px; }

@keyframes breathe { 50% { transform: translateY(-2px) scale(1.008); } }
@keyframes walk-bob { to { transform: translateY(-5px); } }
@keyframes walk-frames {
  0%, 24.9% { background-position: left top; clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%, 0 68%, 0 68%, 0 50%, 0 50%, 0 30%, 0 30%); }
  25%, 49.9% { background-position: right top; clip-path: polygon(0 0, 100% 0, 100% 100%, 5% 100%, 5% 68%, 0 68%, 0 50%, 3% 50%, 3% 30%, 0 30%); }
  50%, 74.9% { background-position: left bottom; clip-path: polygon(0 0, 100% 0, 100% 100%, 0 100%, 0 68%, 0 68%, 0 50%, 0 50%, 0 30%, 0 30%); }
  75%, 100% { background-position: right bottom; clip-path: polygon(0 0, 100% 0, 100% 100%, 5% 100%, 5% 68%, 0 68%, 0 50%, 3% 50%, 3% 30%, 0 30%); }
}
@keyframes happy { 50% { transform: translateY(-5px) rotate(-1deg); } }
@keyframes roll-ball { 0% { opacity: 0; transform: translate(0, 0) rotate(0); } 12% { opacity: 1; } 75% { opacity: 1; } 100% { opacity: 0; transform: translate(var(--ball-dx, 90px), var(--ball-dy, 0px)) rotate(700deg); } }

@media (max-width: 520px) {
  .game-shell { padding-left: 0; padding-right: 0; }
  .topbar { padding-left: 15px; padding-right: 15px; }
  .room { border-radius: 0; }
  .cat { width: clamp(108px, 32vw, 148px); }
  .cat-name { font-size: 11px; padding: 3px 8px; }
  .footer { font-size: 11px; }
}

@media (max-width: 360px) {
  .topbar { gap: 6px; }
  h1 { font-size: 19px; }
  .heading p { display: none; }
  .scene-toggle { min-width: 76px; padding: 0 7px; }
  .sound-toggle { min-width: 62px; }
}

@media (max-height: 620px) {
  .game-shell { min-height: 440px; }
  .heading p { display: none; }
  .topbar { padding-bottom: 7px; }
  .footer { padding-top: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  .cat { transition: left var(--walk-duration, 3200ms) linear, top var(--walk-duration, 3200ms) linear, transform var(--walk-duration, 3200ms) linear, opacity .3s ease; }
  .pose { transition-duration: .2s; }
  .cat-breathe, .cat[data-walking="true"] .cat-breathe, .cat[data-petting="true"] .cat-breathe { animation: none; }
  .cat[data-walking="true"] .pose-walk { animation-duration: 1.52s; }
  .yarn-ball.rolling { animation-duration: .01ms; }
}

/* v0.4.0: one compact eight-pose atlas per new companion. */
.cat[data-atlas="true"] .pose { background-image: var(--cat-image); background-size: 400% 200%; }
.cat[data-atlas="true"] .pose-stand { background-position: 0% 0%; }
.cat[data-atlas="true"] .pose-rest { background-position: 33.333333% 0%; }
.cat[data-atlas="true"] .pose-flop { inset: 0; background-position: 66.666667% 0%; }
.cat[data-atlas="true"] .pose-walk { background-position: 0% 100%; }
.cat[data-atlas="true"][data-walking="true"] .pose-walk { animation-name: atlas-walk-frames; }
@keyframes atlas-walk-frames {
  0%, 24.999% { background-position: 0% 100%; }
  25%, 49.999% { background-position: 33.333333% 100%; }
  50%, 74.999% { background-position: 66.666667% 100%; }
  75%, 100% { background-position: 100% 100%; }
}
.footer { display: flex; align-items: center; justify-content: center; flex-wrap: wrap; gap: 5px 12px; }
#friends-toggle { min-height: 38px; border: 1px solid #c9b49c; border-radius: 20px; padding: 5px 14px; background: #fff8ed; color: #584639; font-size: 13px; cursor: pointer; }
.footer-note { font-size: 11px; }
.friends-dialog { width: min(670px, calc(100% - 24px)); max-height: min(85dvh, 800px); padding: 0 18px 16px; border: 1px solid #d4bfa9; border-radius: 24px; background: #fff8ed; color: #584639; box-sizing: border-box; box-shadow: 0 16px 60px #33211340; overscroll-behavior: contain; }
.friends-dialog::backdrop { background: #33271970; }
.friends-heading { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 17px 0 12px; background: #fff8ed; }
.friends-heading h2 { font-size: 23px; margin: 0 0 3px; }
.friends-heading p { font-size: 13px; margin: 0; color: #806f61; }
#friends-close { min-width: 62px; min-height: 44px; border-radius: 24px; border: 1px solid #c9b49c; background: white; color: inherit; cursor: pointer; }
.friends-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
.friend-card { display: flex; align-items: center; flex-direction: column; min-width: 0; padding: 2px 4px 12px; border: 1px solid #e0cfbb; border-radius: 18px; background: #f3e9dc; color: inherit; cursor: pointer; }
.friend-card:hover { background: #ede0cc; }
.friend-portrait { display: block; width: 100%; aspect-ratio: 1; background-repeat: no-repeat; background-size: 200% 100%; background-position: left center; }
.friend-portrait[data-atlas="true"] { background-size: 400% 200%; background-position: left top; }
.friend-card strong { font-size: 16px; }
.friend-breed { margin-top: 3px; font-size: 11px; color: #806b59; text-align: center; }
.friends-note { font-size: 12px; line-height: 1.7; color: #806f61; margin-bottom: 0; }
.friends-note a { color: #785039; }
#friends-toggle:focus-visible, #friends-close:focus-visible, .friend-card:focus-visible { outline: 3px solid #9a674b; outline-offset: 2px; }
@media (max-width: 380px) { .friends-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); } .footer { gap: 2px 8px; } }

/* Atlas masters place their ground line at 90%; align it with the scene anchor. */
.cat[data-atlas="true"] .cat-facing { top: 10%; bottom: -10%; }

/* Five persistent places; choosing a replacement is explicit. */
.resident-picker { padding: 4px 0 14px; }
.resident-slots { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.resident-slots button { min-height: 42px; padding: 5px 2px; border: 1px solid #c9b49c; border-radius: 12px; background: #fffdf8; color: #584639; font-size: 13px; cursor: pointer; }
.resident-slots button[aria-pressed="true"] { background: #dec29d; border-color: #9a674b; font-weight: bold; }
.resident-slots button:focus-visible { outline: 3px solid #9a674b; outline-offset: 2px; }
#resident-hint { margin: 8px 0 0; color: #806b59; font-size: 12px; }
.friend-card[data-resident="true"] { border-color: #b49770; }
.friend-card[data-resident="true"]::after { content: "正在陪宁宁"; margin-top: 4px; font-size: 10px; color: #6d724e; }
