/* ------------------------------------------------------------------
   AIGameHub 本地增强层：全屏沉浸模式 + 悬浮控制条
   本文件不修改上游任何 CSS，仅由 index.html 额外引入。
   ------------------------------------------------------------------ */

/* 兜底：任何子元素都不得让页面产生横向滚动（游戏是整屏，不需要横滚） */
html { overflow-x: hidden; }

/* ---------- 悬浮控制条 ---------- */
#mj-toolbar {
  position: fixed;
  top: 10px;
  right: max(8px, calc(50vw - 480px));
  z-index: 9999;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border: 1px solid rgba(255, 255, 255, .16);
  border-radius: 999px;
  background: rgba(18, 26, 22, .82);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  box-shadow: 0 6px 22px rgba(0, 0, 0, .35);
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  font-size: 13px;
  line-height: 1;
  color: #f2f6f3;
  user-select: none;
}

#mj-toolbar button,
#mj-toolbar a.mj-link {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 7px 11px;
  background: rgba(255, 255, 255, .1);
  color: inherit;
  font: inherit;
  text-decoration: none;
  white-space: nowrap;
  cursor: pointer;
  transition: background .16s ease, transform .16s ease;
}

#mj-toolbar button:hover,
#mj-toolbar a.mj-link:hover {
  background: rgba(255, 255, 255, .22);
}

#mj-toolbar button:active { transform: scale(.94); }

#mj-toolbar button.mj-on {
  background: #2f7d55;
  color: #fff;
}

#mj-toolbar button.mj-off {
  background: rgba(255, 255, 255, .06);
  color: #9aa9a0;
}

#mj-toolbar .mj-sep {
  width: 1px;
  height: 18px;
  background: rgba(255, 255, 255, .18);
  margin: 0 2px;
}

#mj-toolbar input[type="range"] {
  width: 68px;
  height: 4px;
  accent-color: #4fbf82;
  cursor: pointer;
}

#mj-volume-label {
  min-width: 30px;
  text-align: right;
  opacity: .75;
  font-variant-numeric: tabular-nums;
}

#mj-toolbar .mj-credit {
  font-size: 11px;
  opacity: .6;
  color: inherit;
  text-decoration: none;
  padding: 0 4px;
  white-space: nowrap;
}

#mj-toolbar .mj-credit:hover { opacity: 1; text-decoration: underline; }

/* ---------- 沉浸模式 ---------- */
body.mj-immersive {
  box-sizing: border-box;
  /* 可见视口尺寸由 immersive.js 写入；正常浏览器下等于 100vw/100vh */
  width: var(--mj-vw, 100vw);
  max-width: none;
  height: var(--mj-vh, 100vh);
  margin: 0;
  /* 顶部预留给悬浮控制条，避免压住对家手牌 */
  padding: 46px 0 0 0;
  overflow: hidden;
  background: #101713;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 游戏页只保留棋盘：静态长文说明全部移到独立手册 manual.html。
   这里全局隐藏（不限定沉浸 class），避免首屏加载瞬间闪出一整页文字。 */
body > br,
body > footer,
body > section {
  display: none !important;
}

body.mj-immersive > main.board {
  flex: 0 0 auto;
  border-radius: 10px;
  box-shadow: 0 18px 60px rgba(0, 0, 0, .6);
}

body.mj-immersive #mj-toolbar {
  top: 6px;
  right: 8px;
  background: rgba(12, 18, 15, .78);
  /* 手册入口要始终可辨，只做轻微淡化 */
  opacity: .9;
  transition: opacity .2s ease;
}
body.mj-immersive #mj-toolbar:hover,
body.mj-immersive #mj-toolbar:focus-within,
body.mj-immersive #mj-toolbar.mj-fresh {
  opacity: 1;
}

/* ---------- 窄屏适配 ---------- */
/* 上游 .board .sidebar 被 relative 推到牌桌右侧外面，窄屏时整条
   被裁在屏幕外、还把文档撑宽约 31px（页面「看不全」的真正原因）。 */
@media (max-width: 760px) {
  body.mj-immersive { padding-top: 62px; }
  body.mj-immersive .board .sidebar { display: none; }
}
/* 竖屏给底部大手牌托盘预留空间，牌桌上移且不缩小；
   用可见视口高度的比例，避免矮屏把牌桌挤出可视区。 */
@media (max-width: 760px) and (orientation: portrait) {
  body.mj-immersive {
    padding-bottom: min(148px, calc(var(--mj-vh, 100vh) * .22));
  }
}
/* 工具栏：横屏 844px 也要走窄屏规则，否则内容宽度 984px 左右两头溢出。
   box-sizing 必须补上，padding 才不会把宽度再撑出去。 */
@media (max-width: 900px) {
  #mj-toolbar {
    top: 6px;
    left: 6px;
    right: auto;
    width: calc(var(--mj-vw, 100vw) - 12px);
    box-sizing: border-box;
    justify-content: center;
    flex-wrap: wrap;
    border-radius: 14px;
    font-size: 11px;
    padding: 4px;
    gap: 3px;
  }
  #mj-toolbar button,
  #mj-toolbar a.mj-link { padding: 4px 6px; }
  #mj-toolbar .mj-credit,
  #mj-toolbar .mj-sep,
  #mj-toolbar #mj-volume,
  #mj-volume-label { display: none; }
}

/* ---------- 手机端：把牌看清 ---------- */
/* 轮到你出牌时把 14 张手牌放大成底部「大手牌区」托盘，点它等价于
   点桌上原牌（事件转发，上游逻辑/长辈模式原样生效）。
   竖屏两行 7 张、横屏一行 14 张，牌面保持 36:50 正常比例不变形。 */
#mj-mobile-hand {
  position: fixed;
  left: 0;
  bottom: 0;
  z-index: 9996;
  display: none;
  width: var(--mj-vw, 100vw);
  max-width: 100%;
  box-sizing: border-box; /* 不补这条，padding 会把宽度撑出屏幕、裁掉最后一张 */
  /* 固定 7 列：14 张牌必然两行，不靠「宽度够不够」碰运气 */
  grid-template-columns: repeat(7, minmax(0, 1fr));
  justify-items: center;
  align-items: end;
  align-content: end;
  gap: 3px;
  padding: 6px 6px 10px;
  background: linear-gradient(to top, rgba(0, 0, 0, .78), rgba(0, 0, 0, .34));
  touch-action: manipulation;
}

body.mj-mobile-hand-on #mj-mobile-hand { display: grid; }

/* 托盘标题：轮到你时「该你出牌」就在手牌正上方，视线不用离开牌。
   只要放大手牌区开着就统一用托盘标题，浮动指示牌一律收起——
   否则在平板竖屏 / 中屏横屏上它会压住 4 张放大牌。 */
#mj-mobile-hand-title {
  display: block;
  grid-column: 1 / -1;
  text-align: center;
  color: #eafff1;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: .08em;
  padding: 1px 0 4px;
  text-shadow: 0 2px 6px rgba(0, 0, 0, .7);
}
/* 标题已写明「该你出牌」，浮动指示牌不再压住手牌 */
body.mj-mobile-hand-on #mj-turn-marker.mj-you { display: none !important; }
/* 横屏高度紧张：标题收成一行小字，别把放大牌的行高挤掉 */
@media (orientation: landscape) and (max-height: 600px) {
  #mj-mobile-hand-title { font-size: 15px; letter-spacing: .04em; padding: 0 0 2px; }
}

/* [tile="N"] 的牌面规则对任何带 tile 属性的元素都生效，
   这里只覆盖尺寸即可复用同一套牌图，不必复制图片。
   aspect-ratio 保证是正常麻将牌比例，不再被压成细条或方块。 */
#mj-mobile-hand [tile] {
  box-sizing: border-box;
  width: 100%;
  max-width: 48px;
  height: auto;
  aspect-ratio: 36 / 50;
  min-width: 0;
  min-height: 0;
  max-height: none;
  justify-self: center;
  margin: 0;
  border: 1px solid #d8c9a0;
  border-radius: 4px;
  background-size: contain;
  background-position: center center;
  background-repeat: no-repeat;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .55);
  cursor: pointer;
}

#mj-mobile-hand [tile].highlight {
  box-shadow: 0 0 3px 3px var(--tile-highlight);
  position: relative;
  z-index: 2;
}

#mj-mobile-hand [tile].suggestion { border-top: 3px solid var(--tile-suggestion); }

#mj-mobile-hand [tile].mj-armed {
  box-shadow: 0 0 4px 5px #ffc54a, 0 0 20px rgba(255, 197, 74, .8);
  filter: brightness(1.18);
}

/* 横屏：宽度富余，一行 14 张居中摆放，比例不变、最后一张不裁 */
@media (orientation: landscape) and (max-height: 500px) and (max-width: 900px) {
  #mj-mobile-hand {
    grid-template-columns: repeat(14, minmax(0, 1fr));
    padding: 6px 10px 8px;
    gap: 3px;
    align-content: center;
    /* 独立深色托盘，视觉上不像牌桌被切掉一块 */
    background: rgba(24, 17, 11, .92);
    border-top: 2px solid rgba(255, 215, 130, .4);
  }
  #mj-mobile-hand-title { display: none !important; }
  /* 横屏托盘塞不下标题，仍用右侧空白处的浮动指示牌提示「该你出牌」，
     它不压任何牌；其它有标题的场景一律收起指示牌。 */
  body.mj-mobile-hand-on #mj-turn-marker.mj-you.mj-on { display: block !important; }
  #mj-mobile-hand [tile] { max-width: 48px; }
}

/* 大牌区出现时，桌上那排小手牌淡化，避免两排牌重复干扰 */
body.mj-mobile-hand-on .player[id="0"] game-tile { opacity: .3; }

/* 小屏弹窗：不再跟着牌桌缩到 6~8px，脱离牌桌定位到视口中央 */
@media (max-width: 900px) {
  body.mj-immersive .modal {
    position: fixed;
    top: 0;
    left: 0;
    right: auto;
    bottom: auto;
    /* 用可见视口而非布局视口，WebView 缩放时弹窗也不会溢出右侧 */
    width: var(--mj-vw, 100vw);
    height: var(--mj-vh, 100vh);
    z-index: 10000;
  }
  body.mj-immersive .modal .panel {
    font-size: 13px;
    max-width: calc(var(--mj-vw, 100vw) - 20px);
    max-height: calc(var(--mj-vh, 100vh) - 24px);
    overflow: auto;
  }
  body.mj-immersive .modal .panel h1 { font-size: 15px; }
  body.mj-immersive .modal .panel button {
    font-size: 14px;
    height: 2.2em;
    min-height: 34px;
  }
}

/* ---------- 电脑代玩角标 ---------- */
#mj-autopilot-badge {
  position: fixed;
  left: 14px;
  bottom: 14px;
  z-index: 9998;
  display: none;
  padding: 8px 14px;
  border-radius: 999px;
  background: rgba(47, 125, 85, .92);
  color: #fff;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  font-size: 13px;
  line-height: 1;
  letter-spacing: .05em;
  box-shadow: 0 6px 18px rgba(0, 0, 0, .4);
  user-select: none;
  pointer-events: none;
}

body.mj-autopilot #mj-autopilot-badge { display: block; }

/* ---------- 玩法选择（注入上游开局弹窗） ---------- */
.mj-rules-choose {
  margin-top: 14px;
  padding: 12px 14px;
  border: 1px dashed rgba(255, 255, 255, .28);
  border-radius: 12px;
  background: rgba(0, 0, 0, .18);
  text-align: center;
}

.mj-rules-choose .mj-rc-head {
  font-weight: 700;
  font-size: 15px;
  margin-bottom: 10px;
  letter-spacing: .12em;
}

.mj-rules-choose button {
  appearance: none;
  border: 1px solid rgba(255, 255, 255, .3);
  border-radius: 999px;
  margin: 0 5px 6px;
  padding: 8px 16px;
  background: rgba(255, 255, 255, .1);
  color: inherit;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  transition: background .16s ease;
}

.mj-rules-choose button:hover { background: rgba(255, 255, 255, .24); }

.mj-rules-choose button.mj-cur {
  background: #2f7d55;
  border-color: #3fa26c;
  color: #fff;
  cursor: default;
}

.mj-rules-choose .mj-rc-hint {
  font-size: 12px;
  opacity: .72;
  margin-top: 4px;
}

/* ---------- 玩法切换浮层（对局中） ---------- */
#mj-rules-overlay {
  position: fixed;
  inset: 0;
  z-index: 10000;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .55);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}

#mj-rules-overlay .mj-ro-card {
  min-width: 250px;
  padding: 22px 26px;
  border-radius: 16px;
  background: #16211b;
  border: 1px solid rgba(255, 255, 255, .18);
  color: #f2f6f3;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  text-align: center;
  box-shadow: 0 18px 48px rgba(0, 0, 0, .55);
}

#mj-rules-overlay h3 {
  margin: 0 0 14px;
  letter-spacing: .18em;
  font-size: 17px;
}

#mj-rules-overlay button {
  appearance: none;
  display: block;
  width: 100%;
  border: 1px solid rgba(255, 255, 255, .3);
  border-radius: 999px;
  margin: 0 0 9px;
  padding: 10px 18px;
  background: rgba(255, 255, 255, .1);
  color: inherit;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
  transition: background .16s ease;
}

#mj-rules-overlay button:hover { background: rgba(255, 255, 255, .24); }

#mj-rules-overlay button.mj-cur {
  background: #2f7d55;
  border-color: #3fa26c;
  color: #fff;
  cursor: default;
}

#mj-rules-overlay .mj-ro-note {
  font-size: 12px;
  opacity: .75;
  margin: 4px 0 12px;
}

#mj-rules-overlay .mj-ro-cancel {
  background: transparent;
  border-color: transparent;
  opacity: .8;
}

/* ---------- 轻量提示条 ---------- */
#mj-hint-toast {
  position: fixed;
  left: 50%;
  bottom: 64px;
  z-index: 10001;
  transform: translateX(-50%) translateY(8px);
  padding: 10px 18px;
  border-radius: 999px;
  background: rgba(18, 26, 22, .92);
  border: 1px solid rgba(255, 255, 255, .2);
  color: #f2f6f3;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  font-size: 14px;
  line-height: 1;
  letter-spacing: .04em;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .45);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease, transform .22s ease;
}

#mj-hint-toast.mj-on {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- 阜阳牌桌俚语：同桌搭话气泡 ----------
   放在牌桌上方而不是屏幕底部，避免挡住移动端放大后的手牌。 */
#mj-chatter-bubble {
  position: fixed;
  left: 50%;
  top: 16%;
  z-index: 10002;
  transform: translate(-50%, 10px) scale(.96);
  display: flex;
  flex-direction: column;
  gap: 5px;
  max-width: min(84vw, 420px);
  padding: 10px 18px 12px;
  border-radius: 16px;
  background: rgba(28, 20, 14, .93);
  border: 1px solid rgba(240, 196, 120, .38);
  color: #fff6e6;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .45);
  opacity: 0;
  pointer-events: none;
  text-align: center;
  transition: opacity .22s ease, transform .22s ease;
}
#mj-chatter-bubble.mj-on {
  opacity: 1;
  transform: translate(-50%, 0) scale(1);
}
#mj-chatter-bubble .mj-chatter-who {
  font-size: 11px;
  letter-spacing: .12em;
  color: #e8b96d;
}
#mj-chatter-bubble .mj-chatter-line {
  font-size: 16px;
  line-height: 1.45;
  font-weight: 600;
}
@media (max-width: 760px) {
  #mj-chatter-bubble { top: 14%; padding: 8px 14px 10px; }
  #mj-chatter-bubble .mj-chatter-line { font-size: 15px; }
}

/* ---------- 回合指示：贴在出牌玩家座位旁的浮动指示牌 ---------- */
#mj-turn-marker {
  position: fixed;
  z-index: 9997;
  display: none;
  transform: translate(-50%, -50%);
  padding: 11px 24px;
  border-radius: 999px;
  background: rgba(198, 40, 40, .95);
  border: 2px solid rgba(255, 205, 130, .8);
  color: #fff;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  letter-spacing: .1em;
  white-space: nowrap;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .4);
  box-shadow: 0 8px 26px rgba(0, 0, 0, .5);
  user-select: none;
  pointer-events: none;
}

#mj-turn-marker.mj-on {
  display: block;
}

#mj-turn-marker.mj-you {
  background: rgba(47, 125, 85, .96);
  border-color: rgba(150, 235, 190, .75);
}

@keyframes mj-turn-pulse {
  0%, 100% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -50%) scale(1.08); }
}

#mj-turn-marker.mj-you {
  font-size: 23px;
  padding: 13px 28px;
  animation: mj-turn-pulse 1.5s ease-in-out infinite;
}

/* ---------- 长辈模式 ---------- */
/* 出牌二次确认：举牌状态用金色描边 + 提亮，一眼能看出「点了但还没打」 */
game-tile.mj-armed {
  box-shadow: 0 0 4px 5px #ffc54a, 0 0 20px rgba(255, 197, 74, .75) !important;
  filter: brightness(1.18);
}

/* 举牌气泡提示：跟着牌走，告诉用户还要再点一下 */
#mj-elder-hint {
  position: fixed;
  z-index: 9998;
  transform: translate(-50%, -100%);
  background: #1b5e20;
  color: #fff;
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  font-size: 15px;
  font-weight: 700;
  line-height: 1.2;
  padding: 9px 16px;
  border-radius: 10px;
  white-space: nowrap;
  pointer-events: none;
  user-select: none;
  opacity: 0;
  transition: opacity .15s;
  box-shadow: 0 4px 14px rgba(0, 0, 0, .5);
}

#mj-elder-hint.mj-on { opacity: 1; }

#mj-elder-hint::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  transform: translateX(-50%);
  border: 7px solid transparent;
  border-top-color: #1b5e20;
}

/* 弹窗按钮加大加亮：碰/过/胡、结算确认等，老人看得清点得准 */
body.mj-immersive .modal .panel button {
  font-size: 130%;
  height: 2.5em;
  min-height: 48px;   /* 移动端声明框的碰/胡按钮也要够手指点 */
  letter-spacing: .12em;
}

/* 上游弃牌区的回合进度条：用户反馈鸡肋，直接隐藏 */
.discards .countdown-bar { display: none !important; }

/* 当前出牌玩家：强高亮（上游只有极淡的色块变化，基本看不出） */
body.mj-immersive .player.active {
  box-shadow: 0 0 0 3px rgba(255, 197, 61, .9), 0 0 20px rgba(255, 197, 61, .45);
  border-radius: 10px;
}

/* 「失焦暂停」已移除：即便上游残留 paused 类，也不再显示英文 GAME PAUSED 遮罩 */
body.mj-immersive .discards.paused::after { display: none !important; }

/* ---------- 移动端声明操作条：碰 / 杠 / 胡的大按钮 ----------
   上游那张可声明弃牌只有 18×27px，手指点不中，点偏即被判「过」。
   给两个大按钮，并把 5 秒声明窗口画成看得见的倒计时条。 */
#mj-claim-bar {
  position: fixed;
  left: 50%;
  bottom: 17%;
  z-index: 10003;
  display: none;
  width: min(88vw, 420px);
  box-sizing: border-box;
  transform: translateX(-50%);
  padding: 12px 14px 13px;
  border: 2px solid rgba(255, 197, 61, .8);
  border-radius: 18px;
  background: rgba(20, 32, 24, .97);
  box-shadow: 0 14px 38px rgba(0, 0, 0, .62);
  font-family: "Microsoft YaHei", "PingFang SC", Arial, sans-serif;
  text-align: center;
  color: #fff6e6;
  touch-action: manipulation;
}
#mj-claim-bar.mj-on { display: block; }
@media (min-width: 901px) {
  #mj-claim-bar { display: none !important; }
}
#mj-claim-bar .mj-claim-tip {
  font-size: 15px;
  letter-spacing: .08em;
  color: #ffd27a;
  margin-bottom: 9px;
}
#mj-claim-bar button {
  display: block;
  width: 100%;
  box-sizing: border-box;
  min-height: 56px;
  margin: 0 0 8px;
  border-radius: 12px;
  font: inherit;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: .04em;
  cursor: pointer;
  touch-action: manipulation;
}
#mj-claim-bar .mj-claim-yes {
  border: 1px solid #5cc98f;
  background: #2f7d55;
  color: #fff;
  box-shadow: 0 4px 14px rgba(47, 125, 85, .5);
}
#mj-claim-bar .mj-claim-yes:active { background: #3c9c6c; }
#mj-claim-bar .mj-claim-no {
  min-height: 46px;
  margin-bottom: 0;
  border: 1px solid rgba(255, 255, 255, .38);
  background: rgba(255, 255, 255, .1);
  color: #f2f6f3;
  font-size: 16px;
}
#mj-claim-bar .mj-claim-timer {
  height: 4px;
  margin-top: 11px;
  border-radius: 2px;
  background: rgba(255, 255, 255, .16);
  overflow: hidden;
}
#mj-claim-bar .mj-claim-timer i {
  display: block;
  width: 100%;
  height: 100%;
  background: #ffc54a;
  transform-origin: left center;
  animation: mjClaimTimer 5s linear forwards;
}
@keyframes mjClaimTimer { to { transform: scaleX(0); } }
