/*
 * 合成表情游戏页的样式（只在 /games/face-merge/ 加载），颜色与按钮沿用 site.css。
 * 实验稿 experiments/face-merge/styles.css 的游戏部分与此对应，网站版改用站点的 .button。
 * 两侧的表情展柜（shelf.js）为网站版独有，样式在文件末尾。
 */

.game {
  /* 场地按宽度与可视高度取较小者：页头、提示符与分数栏约占 230px。 */
  --board-width: min(100%, 440px, calc((100svh - 230px) * 400 / 560));
}

.game-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: clamp(16px, 3vh, 32px) 0 40px;
}

.game-top,
.game .hud,
.game .hint,
.game .tuning-panel {
  width: var(--board-width);
}

.game .prompt {
  /* 窄屏时提示符可能比场地宽，省略号收尾，不撑开页面。 */
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hud {
  display: flex;
  flex-wrap: wrap; /* 极窄时按钮换到下一行，不与 next 重叠 */
  gap: 8px clamp(6px, 3%, 16px); /* 场地随窗口高度变窄时，分数栏跟着收紧 */
  align-items: center;
  container: hud / inline-size;
}

.hud-item {
  display: flex;
  flex-direction: column;
  min-width: 4ch;
  line-height: 1.3;
}

.hud-item span {
  color: var(--muted);
  font-size: 12px;
}

.hud-item b {
  font-size: 20px;
  font-variant-numeric: tabular-nums;
}

/* next 与 score、best 一样标签在上，省出横向空间给按钮。 */
.hud-next canvas {
  width: 28px;
  height: 28px;
}

.hud-actions {
  display: flex;
  gap: 6px;
  margin-left: auto;
}

.game .button {
  min-height: 36px;
  padding: 6px 12px;
  font: inherit;
  font-size: 14px;
  line-height: 1.6;
  background: transparent;
  cursor: pointer;
  touch-action: manipulation; /* 手机上连点不会触发双击缩放 */
  white-space: nowrap; /* “$ ”前缀与命令保持在一行 */
}

.game .button-primary {
  background: var(--accent);
}

.game .button:disabled {
  opacity: 0.4;
  cursor: default;
}

.game .button:disabled:hover {
  border-color: var(--line);
}

.board {
  position: relative;
  width: var(--board-width);
  aspect-ratio: 400 / 560;
  border: 1px solid var(--line);
  border-top: 0;
  border-radius: 0 0 10px 10px;
  overflow: hidden;
}

.board canvas {
  display: block;
  touch-action: none;
  cursor: crosshair;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

.board canvas:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.over {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
  justify-content: center;
  background: rgb(10 12 15 / 0.78);
}

.over[hidden] {
  display: none;
}

.over p {
  margin: 0;
}

.over-title {
  color: #ff6b6b;
  font-size: 22px;
  font-weight: 600;
}

.over-title-paused {
  color: var(--ink);
}

.over b {
  font-size: 20px;
}

.record {
  margin-left: 8px;
  padding: 0 6px;
  color: var(--accent);
  font-size: 12px;
  border: 1px solid currentColor;
  border-radius: 4px;
  vertical-align: 3px;
}

.record[hidden] {
  display: none;
}

.over-note {
  color: var(--muted);
  font-size: 12px;
}

.over .button {
  margin-top: 12px;
}

.game .hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
}

/* 触屏设备换一句提示 */
.hint-touch {
  display: none;
}

@media (hover: none) and (pointer: coarse) {
  .hint-pointer {
    display: none;
  }

  .hint-touch {
    display: inline;
  }
}

/* ---------- 中英切换（lang.js）：页面文字两份都写好，按 .game 的 lang 只显示一份 ---------- */

.game:not([lang="en"]) [data-l="en"],
.game[lang="en"] [data-l="zh"] {
  display: none;
}

.game-top {
  display: flex;
  gap: 12px;
  align-items: baseline;
  justify-content: space-between;
}

.lang-switch {
  display: flex;
  flex: none;
  gap: 2px;
  align-items: baseline;
  color: var(--faint);
  font-size: 12px;
}

.lang-switch button {
  padding: 2px 3px;
  color: var(--muted);
  font: inherit;
  background: none;
  border: 0;
  cursor: pointer;
}

.lang-switch button:hover {
  color: var(--ink);
}

.lang-switch button[aria-pressed="true"] {
  color: var(--accent);
}

/* 分数栏很窄时（手机，或矮窗口里场地变窄）：按钮去掉“$ ”前缀、收紧内边距，放得下一行。 */
@container hud (max-width: 380px) {
  .game .hud .button {
    padding: 6px 9px;
  }

  .game .hud .button::before {
    content: none;
  }
}

/* 更窄时数字与按钮再收一点；四位数的分数仍放得下一行，再放不下才把按钮换到下一行。 */
@container hud (max-width: 340px) {
  .hud-item b {
    font-size: 17px;
  }

  .game .hud .button {
    padding: 6px 7px;
  }
}

/* 调参面板：只在 ?tune 时显示，供调手感用。 */
.tuning-panel {
  color: var(--muted);
  font-size: 12px;
}

.tuning-panel summary {
  cursor: pointer;
}

.tuning {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.tuning label {
  display: grid;
  grid-template-columns: 7em 1fr 4.5em;
  gap: 8px;
  align-items: center;
}

.tuning output {
  color: var(--ink);
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.tuning-panel .note {
  margin: 10px 0 0;
  word-break: break-all;
}

/* ---------- 两侧的表情展柜：只在宽屏显示 ---------- */

.shelf {
  display: none;
}

@media (min-width: 960px) {
  .game {
    display: grid;
    grid-template-columns: minmax(0, 1fr) var(--board-width) minmax(0, 1fr);
    gap: clamp(16px, 3vw, 48px);
    /* 两侧展柜可以超出正文宽度，最宽铺到 1600px；留出 64px 给页边与滚动条，不出现横向滚动。 */
    margin-inline: min(0px, (100% - min(100vw - 64px, 1600px)) / 2);
    /* 窗口很高时展柜往下铺满（页头、页脚约占 160px），不只到场地底边。 */
    min-height: calc(100svh - 160px);
  }

  .shelf {
    display: block;
    position: relative;
    /* 上接提示符，下到场地底边为止（场地下面还有两行提示与 40px 留白）。 */
    margin: clamp(16px, 3vh, 32px) 0 100px;
    container-type: size;
    opacity: 0;
    transition: opacity 0.4s;
  }

  .shelf.is-ready {
    opacity: 1;
  }
}

/*
 * 展柜按 100×160 的坐标排布（shelf.js 的 LAYOUT）：--x、--y 为脸中心，--w 为单元宽度。
 * --u 为 1 个坐标单位：通常等于展柜宽度的 1%；展柜比 100×160 矮时按高度换算，整体缩小，免得互相压住。
 * 脸中心在单元框的 (47.37%, 49.40%) 处，换算成框的左上角。
 */
.mascot {
  --u: min(1cqw, 100cqh / 160);
  position: absolute;
  left: calc(var(--x) * 1cqw - var(--w) * var(--u) * 0.473684);
  top: calc(var(--y) * 100cqh / 160 - var(--w) * var(--u) * 0.431653);
  width: calc(var(--w) * var(--u));
}

/* 单元框沿用首页圆脸组件（static/css/agent.css）的尺寸：190×166，脸占 52.6% 宽，头部倾斜 -12°。 */
.mascot-figure {
  position: relative;
  aspect-ratio: 190 / 166;
  container-type: inline-size;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
}

.mascot-actor,
.mascot-head,
.mascot-gaze,
.mascot-cursor,
.mascot-accs {
  position: absolute;
  inset: 0;
}

.mascot-actor,
.mascot-head {
  transform-origin: 47.3684% 49.3976%;
}

.mascot-head {
  transform: rotate(-12deg);
}

.mascot-face {
  position: absolute;
  top: 19.2771%;
  left: 21.0526%;
  width: 52.6316%;
  aspect-ratio: 1;
  border-radius: 50%;
  overflow: hidden;
}

/* 与深色背景对比不足的脸描一圈浅色边，与场地里的画法一致。 */
.mascot-face.is-ringed {
  box-shadow: 0 0 0 1.5px rgb(255 255 255 / 0.28);
}

.mascot-face img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.mascot-cursor {
  transition: transform 0.16s ease-out;
}

.mascot-acc {
  position: absolute;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 32.5301%;
  font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
  font-size: calc(24.2105cqw * var(--size));
  font-weight: 500;
  line-height: 1.1;
  letter-spacing: -0.9684cqw;
  white-space: nowrap;
}

.mascot-label {
  /* 紧跟在脸的下缘（单元框 79.5% 处）之下，留出 squint 逗号的位置 */
  position: absolute;
  top: calc(80% + 4px);
  left: 0;
  margin: 0;
  width: 100%;
  padding-right: 5%; /* 脸的中心略偏左 */
  color: var(--faint);
  font-size: 11px;
  line-height: 20px;
  text-align: center;
  white-space: nowrap;
  transition: color 0.4s;
}

/* 本局已经合成到的等级 */
.mascot.is-reached .mascot-label {
  color: var(--muted);
}

/* 对话气泡：从脸的上方朝另一列（空着的一侧）伸出去。 */
.mascot-bubble {
  position: absolute;
  z-index: 1;
  bottom: 84%;
  width: max-content;
  max-width: 15em;
  margin: 0;
  padding: 4px 9px;
  color: var(--ink);
  font-size: 12px;
  line-height: 1.5;
  background: var(--panel);
  border: 1px solid color-mix(in srgb, var(--color) 70%, transparent);
  border-radius: 8px;
  pointer-events: none;
  opacity: 0;
  transform: translateY(4px) scale(0.92);
  transition: opacity 0.18s, transform 0.18s;
}

.mascot[data-side="start"] .mascot-bubble {
  left: 52%;
  border-bottom-left-radius: 2px;
  transform-origin: 0 100%;
}

.mascot[data-side="end"] .mascot-bubble {
  right: 58%;
  border-bottom-right-radius: 2px;
  transform-origin: 100% 100%;
}

.mascot-bubble.is-on {
  opacity: 1;
  transform: none;
}

/* ---------- 展柜的动作（减少动态效果时不播放，见 shelf.js） ---------- */

@media (prefers-reduced-motion: no-preference) {
  /* 平时轻轻起伏，各张错开节奏 */
  .mascot-actor {
    animation: mascot-bob 4.8s ease-in-out var(--bob-delay) infinite;
  }

  .mascot.is-hop .mascot-actor {
    animation: mascot-hop 0.65s cubic-bezier(0.3, 0, 0.4, 1);
  }

  /* 点头：与首页组件相同，身子一顿、眼睛往下点两下 */
  .mascot.is-nod .mascot-actor {
    animation: mascot-nod-body 2s cubic-bezier(0.65, 0, 0.35, 1);
  }

  .mascot.is-nod .mascot-gaze {
    animation: mascot-nod-gaze 2s cubic-bezier(0.65, 0, 0.35, 1);
  }

  /* 摆弄配饰：动画名由 shelf.js 按款式写进 --act */
  .mascot.is-act .mascot-acc {
    animation: var(--act) 1.2s ease-in-out calc(var(--i) * 120ms) both;
  }

  .mascot-figure:hover .mascot-face {
    filter: brightness(1.08);
  }
}

@media (prefers-reduced-motion: reduce) {
  .mascot-cursor,
  .mascot-bubble {
    transition: none;
  }
}

@keyframes mascot-bob {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-2.5%);
  }
}

@keyframes mascot-hop {
  0%,
  100% {
    transform: none;
  }
  30% {
    transform: translateY(-14%) scale(1.04, 0.97);
  }
  60% {
    transform: translateY(0) scale(1.05, 0.94);
  }
  80% {
    transform: translateY(-3%);
  }
}

@keyframes mascot-nod-body {
  0%,
  10%,
  35%,
  64%,
  100% {
    transform: translateY(0);
  }
  23% {
    transform: translateY(3.012%);
  }
  49% {
    transform: translateY(1.8072%);
  }
}

@keyframes mascot-nod-gaze {
  0%,
  10%,
  35%,
  64%,
  100% {
    transform: translateY(0);
  }
  23%,
  49% {
    transform: translateY(7%);
  }
}

/* sleepy：z 往右上飘走，再从原处冒出来 */
@keyframes acc-drift {
  0% {
    opacity: 1;
    transform: none;
  }
  55% {
    opacity: 0;
    transform: translate(30%, -40%) scale(1.3);
  }
  56% {
    opacity: 0;
    transform: scale(0.6);
  }
  100% {
    opacity: 1;
    transform: none;
  }
}

/* calm：<3 跳两下 */
@keyframes acc-beat {
  0%,
  40%,
  100% {
    transform: none;
  }
  15% {
    transform: scale(1.3);
  }
  28% {
    transform: scale(0.95);
  }
  55% {
    transform: scale(1.2);
  }
  70% {
    transform: none;
  }
}

/* squint、curious：左右摇摆 */
@keyframes acc-wiggle {
  0%,
  100% {
    transform: none;
  }
  20% {
    transform: rotate(-18deg);
  }
  45% {
    transform: rotate(14deg);
  }
  70% {
    transform: rotate(-8deg);
  }
}

/* coding：像光标一样闪 */
@keyframes acc-blink {
  0%,
  20%,
  40%,
  60%,
  80%,
  100% {
    opacity: 1;
  }
  10%,
  30%,
  50%,
  70% {
    opacity: 0.15;
  }
}

/* designing：! 跳起来 */
@keyframes acc-jump {
  0%,
  50%,
  100% {
    transform: none;
  }
  20% {
    transform: translateY(-28%) scale(1.15);
  }
  35% {
    transform: translateY(4%) scale(1, 0.9);
  }
}

/* shocked：!! 发抖 */
@keyframes acc-shake {
  0%,
  100% {
    transform: none;
  }
  10%,
  30%,
  50%,
  70% {
    transform: translateX(-10%) rotate(-6deg);
  }
  20%,
  40%,
  60%,
  80% {
    transform: translateX(10%) rotate(6deg);
  }
}

/* wink：~ 波动 */
@keyframes acc-wave {
  0%,
  100% {
    transform: none;
  }
  25% {
    transform: translate(10%, -12%) skewX(-12deg);
  }
  50% {
    transform: translate(0, 0) skewX(10deg);
  }
  75% {
    transform: translate(10%, -6%) skewX(-6deg);
  }
}

/* dizzy：* 转圈 */
@keyframes acc-spin {
  0% {
    transform: none;
  }
  100% {
    transform: rotate(720deg);
  }
}

/* sparkle：✦ 一闪 */
@keyframes acc-twinkle {
  0%,
  100% {
    transform: none;
    opacity: 1;
  }
  30% {
    transform: scale(1.5) rotate(45deg);
    opacity: 1;
  }
  55% {
    transform: scale(0.7) rotate(90deg);
    opacity: 0.5;
  }
}
