/* =====================================================================
   同音字找朋友 · 棋盘、字牌、模式选择
   ===================================================================== */

/* ============================ 模式选择（封面） ============================ */

/* 两个模式排成一行，省下高度给开始按钮 */
.mode-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.mode {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  min-width: 0;
  min-height: 52px;
  padding: 8px 11px;
  text-align: left;
  background: var(--paper-warm);
  border: 2.5px solid var(--paper-deep);
  border-radius: 14px;
  transition: transform var(--dur-fast) var(--ease-bounce),
              box-shadow var(--dur-fast),
              border-color var(--dur-fast),
              background var(--dur-fast);
  touch-action: manipulation;
}
.mode[aria-checked="true"] {
  background: #FFF4DD;
  border-color: var(--ink);
  box-shadow: 0 3px 0 var(--ink), 0 6px 12px rgba(90, 74, 58, 0.12);
  transform: translateY(-1px);
}
@media (hover: hover) {
  .mode:hover { border-color: var(--ink-soft); transform: translateY(-2px); }
}

.mode__name {
  font-family: var(--font-hand);
  font-size: var(--fs-sm);
  font-weight: 700;
  color: var(--ink);
  line-height: 1.2;
  white-space: nowrap;
}
.mode[aria-checked="true"] .mode__name { color: var(--pumpkin-deep); }

.mode__note {
  font-size: 11px;
  color: var(--ink-soft);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

/* 模式与难度同卡时，难度列表离模式行近一点 */
.setup__block .diff-list { margin-top: 2px; }

/* ============================ 棋盘 ============================ */

.hm-shell {
  display: flex;
  align-items: center;
  justify-content: center;
  width: min(100%, 660px);
  padding: clamp(7px, 1.6vmin, 14px);
  background: var(--paper-warm);
  border: 3px solid var(--ink);
  border-radius: clamp(18px, 3.2vmin, 28px);
  box-shadow: 0 7px 0 var(--paper-deep), 0 9px 0 var(--ink), var(--shadow-card);
  -webkit-user-select: none;
  user-select: none;
}

.hm-board {
  --cols: 4;
  --rows: 4;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  grid-template-rows: repeat(var(--rows), minmax(0, 1fr));
  gap: clamp(4px, 0.9vmin, 9px);
  width: 100%;
  aspect-ratio: calc(var(--cols) / var(--rows));
}

/* —— 字牌 —— */
.hm-tile {
  position: relative;
  display: block;
  min-width: 0;
  min-height: 0;
  padding: 0;
  border: none;
  background: none;
  border-radius: clamp(9px, 2.2vmin, 16px);
  container-type: size;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  transition: transform var(--dur-fast) var(--ease-bounce), opacity var(--dur) var(--ease-soft);
}

.hm-tile__face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0;
  border: clamp(2px, 1.6cqmin, 3px) solid var(--ink);
  border-radius: inherit;
  background: var(--tile-c, var(--orange-soft));
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.10), 0 3px 0 rgba(90, 74, 58, 0.38);
  transition: box-shadow var(--dur-fast), transform var(--dur-fast) var(--ease-bounce), background var(--dur-fast);
  overflow: hidden;
}

.hm-tile__ch {
  font-family: "Kaiti SC", Kaiti, STKaiti, "Songti SC", serif;
  font-size: min(58cqh, 58cqw, 78px);
  font-weight: 700;
  line-height: 1;
  color: var(--ink-tile);
  transform: translateY(4%);
}

/* 座位号：点进来的字按顺序标 1/2/3/4，一眼看出这桌还差几个 */
.hm-tile.is-picked::after {
  content: attr(data-seat);
  position: absolute;
  left: clamp(2px, 6cqh, 7px);
  top: clamp(2px, 6cqh, 7px);
  min-width: 1.5em;
  padding: 0 clamp(2px, 4cqw, 5px);
  border-radius: 999px;
  background: var(--pumpkin);
  color: #fff;
  font-family: var(--font-num);
  font-size: min(13cqh, 13cqw, 13px);
  font-weight: 800;
  line-height: 1.45;
  text-align: center;
  box-shadow: 0 1.5px 0 var(--ink);
  z-index: 2;
}
.hm-tile[data-seat="1"]::after { background: var(--pumpkin); }
.hm-tile[data-seat="2"]::after { background: #6FAFD8; }
.hm-tile[data-seat="3"]::after { background: #7CC398; }
.hm-tile[data-seat="4"]::after { background: var(--cherry-deep); }
.hm-tile[data-seat]::after { pointer-events: none; }

/* 已坐下的牌保持高亮但不再抖，避免误以为又点了一次 */
.hm-tile.is-picked .hm-tile__ch { transform: translateY(-6%); }

.hm-tile__py {
  position: absolute;
  left: 50%;
  bottom: clamp(2px, 8cqh, 10px);
  transform: translateX(-50%) translateY(6px);
  font-family: var(--font-num);
  font-size: min(17cqh, 17cqw, 18px);
  font-weight: 700;
  letter-spacing: 0.01em;
  color: rgba(68, 53, 42, 0.72);
  opacity: 0;
  transition: opacity var(--dur-fast), transform var(--dur-fast) var(--ease-bounce);
  pointer-events: none;
  white-space: nowrap;
}

/* 选中：抬起 + 亮出拼音 */
.hm-tile.is-picked { z-index: 3; transform: translateY(-3px) scale(1.04); }
.hm-tile.is-picked .hm-tile__face {
  background: #FFF6E2;
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.08), 0 4px 0 var(--ink), 0 8px 16px rgba(90, 74, 58, 0.22);
  outline: 3px solid var(--pumpkin);
  outline-offset: 1px;
}
.hm-tile.is-picked .hm-tile__py { opacity: 1; transform: translateX(-50%) translateY(0); }
/* 选中时字往上收，拼音落在底部，座位号和小标分居左上/右上 */
.hm-tile.is-picked .hm-tile__ch { font-size: min(50cqh, 50cqw, 78px); }
.hm-tile.is-picked .hm-tile__ch { transform: translateY(-6%); }

/* 提示：两张一起闪一圈 */
/* 提示：整桌一起呼吸。用轮廓 + 抬升 + 光晕三重叠加，
   在彩色牌面和各种屏幕上都能一眼看见（原来只有一圈细描边，几乎不可见）。 */
.hm-tile.is-hint {
  z-index: 4;
  animation: hmHintLift 1.1s var(--ease-soft) infinite;
}
.hm-tile.is-hint .hm-tile__face {
  outline: 4px solid var(--pumpkin);
  outline-offset: 1px;
  box-shadow:
    inset 0 -4px 0 rgba(0, 0, 0, 0.10),
    0 0 0 7px rgba(245, 213, 71, 0.55),
    0 4px 0 rgba(90, 74, 58, 0.38);
}
/* 已经坐下的牌：换一种颜色，和「还差哪几个」区分开 */
.hm-tile.is-hint-seated .hm-tile__face {
  outline-color: #4E9A6E;
  box-shadow:
    inset 0 -4px 0 rgba(0, 0, 0, 0.10),
    0 0 0 7px rgba(124, 195, 152, 0.55),
    0 4px 0 rgba(90, 74, 58, 0.38);
}
@keyframes hmHintLift {
  0%, 100% { transform: translateY(0) scale(1); }
  50%      { transform: translateY(-6px) scale(1.04); }
}

@media (prefers-reduced-motion: reduce) {
  .hm-tile.is-hint { animation: none; }
}

/* 选错：抖一下再复原 */
.hm-tile.is-wrong .hm-tile__face {
  background: #FBE3E8;
  animation: hmShake 0.4s var(--ease-soft);
}
@keyframes hmShake {
  0%, 100% { transform: translateX(0); }
  25%      { transform: translateX(-4px); }
  50%      { transform: translateX(4px); }
  75%      { transform: translateX(-2px); }
}

/* 配对成功：淡出并让位 */
.hm-tile.is-gone {
  pointer-events: none;
  animation: hmGone 0.42s var(--ease-soft) forwards;
  animation-delay: var(--pop-delay, 0s);
}
@keyframes hmGone {
  0%   { opacity: 1; transform: scale(1.06); }
  55%  { opacity: 1; transform: scale(1.12); }
  100% { opacity: 0; transform: scale(0.72); }
}
/* 消掉的格子留一枚浅浅的虚线空位，像被收拾干净的桌面，
   而不是一个突兀的洞；也方便小朋友记住这里已经找过了 */
.hm-tile.is-gone .hm-tile__face {
  box-shadow: none;
  border-color: transparent;
  background: transparent;
}
.hm-tile.is-gone .hm-tile__face::before {
  content: "";
  position: absolute;
  inset: 12%;
  border: 2px dashed rgba(90, 74, 58, 0.16);
  border-radius: inherit;
  background: rgba(90, 74, 58, 0.03);
}
.hm-tile.is-gone .hm-tile__ch,
.hm-tile.is-gone .hm-tile__py { opacity: 0; }

/* 全部消完：棋盘轻轻跳一下 */
.hm-board.is-cleared .hm-tile { animation: hmCheer 0.5s var(--ease-bounce); }
.hm-board.is-bump { animation: hmBump 0.36s var(--ease-soft); }
@keyframes hmBump {
  0%, 100% { transform: translateY(0); }
  40%      { transform: translateY(3px); }
}
@keyframes hmCheer {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-8px); }
}

/* 键盘可达性：只在键盘聚焦时显示描边 */
.hm-tile:focus-visible .hm-tile__face {
  outline: 3px dashed var(--pumpkin);
  outline-offset: 2px;
}

@media (hover: hover) {
  .hm-tile:not(.is-gone):hover .hm-tile__face { transform: translateY(-2px); }
}

/* —— 配对规则条：常驻在棋盘下方，随模式改写 —— */
.hm-rule {
  margin: 0;
  padding: 5px 10px;
  max-width: 100%;
  border: 2px dashed rgba(90, 74, 58, 0.22);
  border-radius: 10px;
  background: rgba(255, 253, 247, 0.72);
  font-family: var(--font-hand);
  font-size: var(--fs-xs);
  line-height: 1.5;
  color: var(--ink-soft);
  text-align: center;
}
.hm-rule b { color: var(--pumpkin-deep); }

/* —— 反馈文字 —— */
.hm-feedback {
  min-height: 1.5em;
  margin: 0;
  font-family: var(--font-hand);
  font-size: var(--fs-sm);
  line-height: 1.45;
  text-align: center;
  color: var(--ink-soft);
  transition: color var(--dur-fast);
}
.hm-feedback.is-ok { color: #4E9A6E; }
.hm-feedback.is-no { color: #C4576C; }
.hm-feedback.is-hint { color: var(--pumpkin-deep); font-weight: 700; }

/* ============================ 封面：窄屏压缩 ============================ */
/* 模式一行 + 难度三行 + 开始按钮，比模板游戏多一块，
   所以窄屏要把插画、标题、间距各让一点，保证「开始游戏」在首屏。 */

@media (max-width: 760px) {
  .screen--start .setup { gap: 9px; }
  .screen--start .setup__block { gap: 8px; padding: 11px; }
  .screen--start .mode { min-height: 46px; padding: 6px 10px; }
  .screen--start .diff-list { gap: 7px; }
  .screen--start .diff { min-height: 52px; padding: 6px 10px; }
  .screen--start .btn--lg { min-height: 58px; }
  .screen--start .fineprint { display: none; }
  /* 模式说明在窄屏并入名字行，省一行高度 */
  .screen--start .mode-toggle { gap: 7px; }
}

@media (max-width: 760px) and (max-height: 780px) {
  .screen--start .hero__card { width: min(150px, 34vw); padding: 10px; }
  .screen--start .start-grid { gap: 12px; }
}

/* 桌面矮屏：右侧数据条与操作栏贴顶，不额外下移 */
@media (min-width: 860px) and (max-height: 660px) {
  .side-card__head { display: none; }
}

@media (max-width: 400px) {
  .screen--start .hero__card { width: min(130px, 32vw); padding: 8px; border-radius: 22px; }
  .screen--start .mode { min-height: 42px; padding: 5px 8px; }
  .screen--start .mode__note { display: none; }
  .screen--start .diff { min-height: 46px; padding: 5px 8px; gap: 8px; }
  .screen--start .diff__mini { width: 30px; height: 30px; }
  .screen--start .setup__block { padding: 9px; gap: 6px; }
}

/* 小屏手机（320×568 / 360×640）：再收一档，保证开始按钮和底部留白 */
@media (max-width: 400px) and (max-height: 700px) {
  .screen--start { padding-top: 8px; padding-bottom: calc(10px + env(safe-area-inset-bottom)); }
  .start-grid { gap: 9px; }
  .screen--start .hero__card { width: min(112px, 30vw); padding: 7px; border-radius: 18px; }
  .screen--start .app-title { font-size: clamp(28px, 9vw, 36px); }
  .screen--start .subtitle { margin-top: 0; font-size: var(--fs-sm); }
  .screen--start .setup { gap: 7px; }
  .screen--start .setup__block { padding: 8px; gap: 5px; }
  .screen--start .diff-list { gap: 5px; }
  .screen--start .diff { min-height: 44px; padding: 4px 8px; }
  .screen--start .btn--lg { min-height: 52px; font-size: var(--fs-md); }
}

/* 矮横屏（含手机横屏、568×320）：插画与标题并排，设置占右侧 */
@media (max-height: 480px) and (orientation: landscape) {
  .screen--start { padding-top: 6px; padding-bottom: 4px; }
  .start-grid { gap: 14px; grid-template-columns: minmax(0, 0.8fr) minmax(320px, 1fr); align-items: center; }
  .screen--start .hero__card { width: min(130px, 22vw); padding: 8px; border-radius: 20px; }
  .screen--start .app-title { font-size: clamp(24px, 4.4vh, 32px); }
  .screen--start .subtitle { display: none; }
  .screen--start .setup { gap: 5px; }
  .screen--start .setup__block { padding: 7px; gap: 5px; }
  .screen--start .mode { min-height: 34px; padding: 3px 8px; }
  .screen--start .mode__note { display: none; }
  .screen--start .diff-list { gap: 4px; }
  .screen--start .diff { min-height: 34px; padding: 3px 8px; }
  .screen--start .diff__mini { width: 24px; height: 24px; }
  .screen--start .diff__meta { display: none; }
  .screen--start .btn--lg { min-height: 38px; padding: 5px 14px; font-size: var(--fs-sm); }
  .screen--start .eyebrow,
  .screen--start .fineprint { display: none; }
  .screen--start .setup__label-note { display: none; }
}

/* ============================ 游戏页：矮屏压缩 ============================ */

/* 桌面 / 平板横屏但高度不够（1024×600、800×600 一类）：
   棋盘要留够尺寸，先从数据卡、操作栏、说明文字身上省高度。 */
@media (max-height: 780px) {
  .stats { margin-top: 0; }
  .stage__caption { display: none; }
  .hint--play { display: none; }
  .side-card { gap: 9px; padding: 11px; }
  .side-actions .btn { min-height: 44px; font-size: var(--fs-sm); }
  .hm-feedback { font-size: var(--fs-xs); min-height: 1.2em; }
}

@media (max-height: 660px) {
  .screen--play { padding-top: 4px; padding-bottom: 10px; }
  .stat { padding: 3px 5px 4px; border-width: 2.5px; }
  .stat__label { font-size: 11px; }
  .stat__value { font-size: 19px; }
  .side-card { gap: 7px; padding: 8px 9px; border-width: 2.5px; }
  .side-card__head { display: none; }
  .seg { padding: 3px; }
  .seg__btn { min-height: 38px; font-size: var(--fs-xs); }
  .side-actions .btn { min-height: 38px; padding: 5px 8px; font-size: var(--fs-xs); border-width: 2px; }
  .hm-feedback { display: none; }
  .stage { gap: 6px; }
  /* 规则条不隐藏：它是玩法本身，只压缩留白与字号 */
  .hm-rule { padding: 3px 8px; font-size: 11px; line-height: 1.4; border-width: 1.5px; }
}

/* 宽而矮（800×600 一类）：改成左右两列，操作栏搬到右侧，
   棋盘能把整个高度吃满，不再被上下两块挤成一小块。 */
@media (min-width: 700px) and (max-width: 859px) and (max-height: 700px) {
  .play-grid {
    flex: 1;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr) 232px;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "stage stats"
      "stage side";
    gap: 6px 14px;
  }
  .stage { gap: 6px; }
  .stats { gap: 6px; }
  .side { gap: 6px; }
  .side-card { padding: 8px 9px; gap: 7px; }
  .seg__btn { min-height: 36px; }
  .side-actions { grid-template-columns: 1fr; gap: 6px; }
  .side-actions .btn { min-height: 38px; }
}

/* 单列 + 矮屏：操作栏压成一行两键，给棋盘腾出空间 */
@media (max-width: 859px) and (max-height: 700px) {
  .screen--play { padding: 4px 12px max(8px, env(safe-area-inset-bottom)); }
  .play-grid { gap: 8px; }
  .stats { gap: 6px; }
  .stat { padding: 4px 5px 5px; }
  .stat__value { font-size: 20px; }
  .side-card { padding: 8px 9px; gap: 7px; }
  .side-card__head { display: none; }
  .seg { padding: 3px; }
  .seg__btn { min-height: 38px; font-size: var(--fs-xs); }
  .side-actions { gap: 6px; }
  .side-actions .btn { min-height: 40px; padding: 5px 8px; font-size: var(--fs-sm); }
}

/* 庆祝弹窗里跨行的「最佳记录」占满整行 */
.celeb__stat--wide {
  flex: 1 1 100%;
  margin-top: 10px;
  min-width: 0 !important;
}

/* 入场动画里的 translateY 会把内容顶出视口，
   游戏页这类满屏页面改成只淡入、不位移，避免出现 8px 的纵向滚动。 */
.screen--play.view-enter { animation: hmFadeIn var(--dur) var(--ease-soft) both; }
@keyframes hmFadeIn {
  0%   { opacity: 0; }
  100% { opacity: 1; }
}

/* ============================ 响应式 ============================ */

@media (max-width: 760px) {
  .hm-shell { box-shadow: 0 5px 0 var(--paper-deep), 0 7px 0 var(--ink), var(--shadow-soft); }
  .hm-tile__ch { font-size: min(56cqh, 56cqw, 60px); }
  .hm-feedback { font-size: var(--fs-sm); }
}

@media (max-width: 400px) {
  .mode { min-height: 48px; padding: 6px 9px; }
  .mode__name { font-size: 15px; }
  .mode__note { font-size: 10px; }
}

/* 矮横屏：封面收一收，保证开始按钮在首屏 */
@media (max-height: 620px) and (orientation: landscape) {
  .mode { min-height: 44px; padding: 5px 9px; }
  .mode__note { display: none; }
}

/* 顶栏品牌名：5 个字比模板更长，窄屏缩一档避免「同音字找…」截断 */
@media (max-width: 640px) {
  .brand__name { font-size: 16px; letter-spacing: 0; }
}
@media (max-width: 430px) {
  .brand { gap: 6px; }
  .brand__name { font-size: 15px; }
  .brand__logo { width: 36px; height: 36px; font-size: 13px; }
}
@media (max-width: 380px) {
  .brand__name { font-size: 14px; }
}

/* 极矮横屏（568×320 一类）：规则条必须保留，用最小字号压成两行。
   放在文件最后，确保覆盖上面所有 max-height 规则。 */
@media (max-height: 380px) {
  .hm-rule {
    margin-top: 2px;
    padding: 2px 6px;
    font-size: 10px;
    line-height: 1.3;
    border-width: 1.5px;
    border-radius: 8px;
    text-align: left;
  }
  /* 棋盘已到最小尺寸：规则条压在棋盘下沿之外，靠 flex 底部对齐，
     并用 padding 给棋盘让出位置，避免遮住最下面一排字 */
  .stage { position: relative; padding-bottom: 0; }
  .hm-rule {
    position: absolute;
    left: 0;
    right: 0;
    bottom: 0;
    max-height: 3.2em;
    overflow: hidden;
  }
  /* 棋盘底边让位：宽度收一点，让规则条贴在它下方 */
  .hm-shell { margin-bottom: 26px; }
}
