/* ============================================================
   合成大西瓜 · 样式表
   ============================================================ */

:root {
  --ink:        #4a3325;
  --ink-soft:   #8a7566;
  --cream:      #fff7ec;
  --brand:      #ff6b6b;
  --brand-dark: #e14b4b;
  --card:       rgba(255, 255, 255, .74);
  --shadow:     0 18px 50px rgba(126, 84, 44, .18);

  /* 手机浏览器地址栏会吃掉 vh，dvh 更准；不支持的浏览器自动退回 vh */
  --vh:         100vh;
  --safe-t:     env(safe-area-inset-top, 0px);
  --safe-b:     env(safe-area-inset-bottom, 0px);
  --safe-l:     env(safe-area-inset-left, 0px);
  --safe-r:     env(safe-area-inset-right, 0px);
}

@supports (height: 100dvh) {
  :root { --vh: 100dvh; }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  overscroll-behavior: none;   /* 禁掉下拉刷新 / 橡皮筋 */
}

body {
  margin: 0;
  padding: 16px;
  color: var(--ink);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
               system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background:
    radial-gradient(1200px 700px at 8% -12%,  #ffe9bd 0%, transparent 60%),
    radial-gradient(900px 600px at 104% -6%,  #d5f0ff 0%, transparent 55%),
    radial-gradient(1000px 780px at 50% 118%, #ffdbe6 0%, transparent 62%),
    #fff9f1;
  display: grid;
  place-items: center;
  overflow: hidden;
  touch-action: manipulation;          /* 干掉双击缩放 */
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

/* ---------------- 布局 ---------------- */

.app {
  display: flex;
  align-items: stretch;
  gap: 20px;
  max-height: 100%;
}

/* ---------------- 游戏区 ---------------- */

.stage {
  position: relative;
  height: min(86vh, 700px);
  aspect-ratio: 420 / 700;
  flex: 0 0 auto;
  border-radius: 24px;
  background: #fff3df;
  overflow: hidden;
  cursor: pointer;
  user-select: none;
  touch-action: none;
  box-shadow:
    0 26px 64px rgba(122, 80, 40, .30),
    0 0 0 1px rgba(255, 255, 255, .7) inset,
    0 0 0 6px rgba(255, 255, 255, .55);
}

.stage:active { cursor: grabbing; }

#game {
  display: block;
  width: 100%;
  height: 100%;
}

/* ---------------- 结束遮罩 ---------------- */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(70, 42, 24, .5);
  backdrop-filter: blur(7px);
  -webkit-backdrop-filter: blur(7px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .28s ease;
}

.overlay.show {
  opacity: 1;
  pointer-events: auto;
}

.dialog {
  width: 100%;
  padding: 26px 22px;
  border-radius: 22px;
  text-align: center;
  background: rgba(255, 252, 246, .96);
  box-shadow: 0 20px 46px rgba(60, 32, 12, .32);
  transform: translateY(14px) scale(.96);
  transition: transform .32s cubic-bezier(.2, .9, .3, 1.4);
}

.overlay.show .dialog { transform: none; }

.dialog-emoji {
  font-size: 46px;
  line-height: 1;
  margin-bottom: 6px;
}

.dialog h2 {
  margin: 0 0 12px;
  font-size: 24px;
  letter-spacing: .06em;
}

.dialog-score {
  margin: 0 0 4px;
  font-size: 15px;
  color: var(--ink-soft);
}

.dialog-score strong {
  font-size: 30px;
  color: var(--brand);
  font-variant-numeric: tabular-nums;
}

.dialog-best {
  margin: 0 0 18px;
  font-size: 13px;
  color: var(--ink-soft);
}

.dialog-hint {
  margin: 12px 0 0;
  font-size: 12px;
  color: var(--ink-soft);
}

/* ---------------- 侧边面板 ---------------- */

.panel {
  width: min(312px, 86vw);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 20px;
  border-radius: 24px;
  border: 1px solid rgba(255, 255, 255, .8);
  background: var(--card);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: var(--shadow);
}

.panel-head h1 {
  margin: 0;
  font-size: 26px;
  letter-spacing: .04em;
  background: linear-gradient(100deg, #ff7a59, #ff4d6d 55%, #b13bff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.subtitle {
  margin: 6px 0 0;
  font-size: 12.5px;
  color: var(--ink-soft);
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.stat {
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(255, 255, 255, .82);
  box-shadow: 0 4px 14px rgba(140, 96, 56, .10);
}

.stat-label {
  display: block;
  font-size: 11px;
  letter-spacing: .12em;
  color: var(--ink-soft);
}

.stat-value {
  display: block;
  margin-top: 2px;
  font-size: 26px;
  line-height: 1.1;
  font-variant-numeric: tabular-nums;
}

.stat-best { color: #e79a1f; }

.stat-value.bump { animation: bump .32s ease; }

@keyframes bump {
  0%   { transform: scale(1); }
  45%  { transform: scale(1.22); color: var(--brand); }
  100% { transform: scale(1); }
}

/* 下一个 / 合成表 */

.box-label {
  display: block;
  margin-bottom: 6px;
  font-size: 11px;
  letter-spacing: .12em;
  color: var(--ink-soft);
}

.next-box,
.chain-box {
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(255, 255, 255, .82);
  box-shadow: 0 4px 14px rgba(140, 96, 56, .10);
}

#next {
  display: block;
  width: 76px;
  height: 76px;
  margin: 2px auto 0;
}

#chain {
  display: block;
  width: 100%;
  height: auto;
}

/* 按钮 */

.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.btn {
  padding: 11px 12px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--ink);
  border: 1px solid rgba(255, 255, 255, .9);
  border-radius: 14px;
  background: rgba(255, 255, 255, .9);
  box-shadow: 0 6px 16px rgba(140, 96, 56, .14);
  cursor: pointer;
  transition: transform .12s ease, box-shadow .12s ease, background .2s ease;
}

.btn .ico { margin-right: 4px; }

.btn:hover  { transform: translateY(-1px); box-shadow: 0 10px 22px rgba(140, 96, 56, .2); }
.btn:active { transform: translateY(1px) scale(.985); }

.btn:disabled { opacity: .55; cursor: default; transform: none; }

.btn-wide { grid-column: 1 / -1; }

.btn-primary {
  color: #fff;
  border-color: transparent;
  background: linear-gradient(135deg, #ff8a5b, var(--brand) 55%, var(--brand-dark));
}

/* ---------------- 结束弹窗里的结算区 ---------------- */

.submit-box {
  margin: 0 0 14px;
  padding: 12px;
  border-radius: 16px;
  background: rgba(255, 243, 224, .85);
  box-shadow: inset 0 0 0 1px rgba(214, 160, 96, .22);
}

.name-input {
  display: block;
  width: 100%;
  margin-bottom: 8px;
  padding: 11px 12px;
  font: inherit;
  font-size: 15px;
  color: var(--ink);
  text-align: center;
  border: 1px solid rgba(214, 160, 96, .45);
  border-radius: 12px;
  background: rgba(255, 255, 255, .95);
  outline: none;
}

.name-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(255, 107, 107, .18);
}

.submit-box .btn { width: 100%; }

.submit-msg {
  margin: 0;
  min-height: 17px;
  font-size: 12.5px;
  line-height: 1.35;
  text-align: center;
  color: var(--ink-soft);
}

.submit-msg.is-good { color: #1f9d55; font-weight: 600; }
.submit-msg.is-bad  { color: #d63b3b; font-weight: 600; }

.my-name {
  margin: 7px 0 0;
  font-size: 12px;
  text-align: center;
  color: var(--ink-soft);
}

.my-name strong { color: var(--ink); }

.link-btn {
  margin-left: 4px;
  padding: 0;
  font: inherit;
  font-size: 12px;
  color: var(--brand);
  text-decoration: underline;
  border: 0;
  background: none;
  cursor: pointer;
}

#submitBtn { margin-top: 9px; }

/* 排行榜弹窗里的昵称行 */
.nick-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(255, 243, 224, .9);
  box-shadow: inset 0 0 0 1px rgba(214, 160, 96, .22);
}

.nick-label {
  flex: 0 0 auto;
  font-size: 12.5px;
  color: var(--ink-soft);
}

.nick-input {
  flex: 1 1 auto;
  min-width: 0;
  padding: 8px 10px;
  font: inherit;
  font-size: 15px;      /* ≥16px 才不会被 iOS 自动放大，这里配合 viewport 也够用 */
  color: var(--ink);
  text-align: center;
  border: 1px solid rgba(214, 160, 96, .45);
  border-radius: 10px;
  background: #fff;
  outline: none;
}

.nick-input:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(255, 107, 107, .18);
}

.dialog-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* ---------------- 排行榜弹窗 ---------------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(60, 34, 16, .5);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .22s ease;
}

.modal.show { opacity: 1; pointer-events: auto; }

.modal-card {
  display: flex;
  flex-direction: column;
  width: min(380px, 100%);
  max-height: min(560px, calc(var(--vh) - 40px));
  padding: 16px;
  border-radius: 22px;
  background: rgba(255, 252, 246, .98);
  box-shadow: 0 24px 60px rgba(60, 32, 12, .35);
  transform: translateY(12px) scale(.97);
  transition: transform .26s cubic-bezier(.2, .9, .3, 1.3);
}

.modal.show .modal-card { transform: none; }

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.modal-head h2 {
  margin: 0;
  font-size: 19px;
  letter-spacing: .04em;
}

.icon-btn {
  width: 34px;
  height: 34px;
  font: inherit;
  font-size: 15px;
  color: var(--ink-soft);
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, .05);
  cursor: pointer;
}

.icon-btn:hover { background: rgba(0, 0, 0, .1); }

.board-list {
  flex: 1 1 auto;
  min-height: 120px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 2px;
}

.board-empty {
  margin: 28px 8px;
  font-size: 13px;
  line-height: 1.7;
  text-align: center;
  color: var(--ink-soft);
}

.board-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  margin-bottom: 6px;
  border-radius: 12px;
  background: rgba(255, 255, 255, .9);
  box-shadow: 0 2px 6px rgba(140, 96, 56, .09);
}

.board-rank {
  flex: 0 0 30px;
  font-size: 14px;
  font-weight: 700;
  text-align: center;
  color: var(--ink-soft);
}

.board-name {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.board-score {
  flex: 0 0 auto;
  font-size: 16px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: var(--brand);
}

.board-row.r1 { background: linear-gradient(100deg, #fff6d8, #ffe9a8); }
.board-row.r2 { background: linear-gradient(100deg, #f7f7fa, #e8e9ef); }
.board-row.r3 { background: linear-gradient(100deg, #fdf0e2, #f6ddc4); }
.board-row.r1 .board-rank,
.board-row.r2 .board-rank,
.board-row.r3 .board-rank { font-size: 18px; }

.board-row.is-mine {
  outline: 2px solid var(--brand);
  outline-offset: -2px;
}

.modal-foot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 10px;
}

.board-note {
  font-size: 11px;
  color: var(--ink-soft);
}

.modal-foot .btn { flex: 0 0 auto; padding: 8px 16px; }

/* ---------------- 参数提示 ---------------- */

.tips {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.7;
  text-align: center;
  color: var(--ink-soft);
}

/* ============================================================
   响应式：手机 / 竖屏窄屏
   思路：面板拆成「顶部信息条」——把 .panel 提到棋盘上方并横向压扁，
   只留分数 + 按钮；棋盘按剩余高度和 96vw 取小值，永远不出现滚动条。
   ============================================================ */

@media (max-width: 860px) {
  :root { --topbar: 68px; }   /* 顶部信息条 + 间隙的大致占高 */

  body {
    padding: calc(8px + var(--safe-t)) calc(8px + var(--safe-r))
             calc(8px + var(--safe-b)) calc(8px + var(--safe-l));
  }

  .app {
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;   /* 信息条钉在顶上 */
    gap: 8px;
    width: 100%;
    height: 100%;
    max-height: none;
  }

  /* 面板是 DOM 里靠后的，用 order 提到棋盘上面 */
  .panel { order: -1; }

  /* 棋盘：高度取「剩余可用高度」与「96vw 按比例换算的高度」中较小的一个，
     宽度由 aspect-ratio 反推，保证不拉伸、不溢出；
     margin:auto 让它在剩下的空间里垂直居中 */
  .stage {
    margin: auto 0;
    height: min(calc(var(--vh) - var(--topbar) - 28px - var(--safe-t) - var(--safe-b)),
                calc(96vw * 700 / 420));
    width: auto;
    max-height: none;
    border-radius: 20px;
  }

  /* ---- 顶部信息条 ---- */
  .panel {
    flex-direction: row;
    align-items: stretch;
    gap: 8px;
    width: 100%;
    max-width: calc(96vw * 700 / 420);
    padding: 8px 10px;
    border-radius: 18px;
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
  }

  .panel-head,
  .next-box,
  .chain-box,
  .tips { display: none; }

  .stats {
    flex: 1 1 auto;
    display: flex;
    gap: 8px;
    min-width: 0;
  }

  .stat {
    flex: 1 1 0;
    min-width: 0;
    padding: 6px 10px;
    border-radius: 12px;
    background: rgba(255, 255, 255, .9);
    box-shadow: 0 2px 8px rgba(140, 96, 56, .12);
  }

  .stat-label { font-size: 10px; letter-spacing: .08em; }

  .stat-value {
    margin-top: 0;
    font-size: 20px;
    line-height: 1.15;
  }

  .actions {
    flex: 0 0 auto;
    display: flex;
    gap: 6px;
  }

  /* 触摸目标够大，别让人点不中 */
  .btn {
    min-height: 46px;
    padding: 0 12px;
    font-size: 14px;
    border-radius: 13px;
    white-space: nowrap;
  }

  /* 手机上「排行榜 / 音效」只留图标，把横向空间让给分数 */
  #boardBtn .lbl,
  #soundBtn .lbl { display: none; }

  #boardBtn, #soundBtn {
    min-width: 46px;
    padding: 0 10px;
    font-size: 17px;
  }

  #boardBtn .ico, #soundBtn .ico { margin-right: 0; }

  /* ---- 结束弹窗 ---- */
  .overlay { padding: 14px; }

  .dialog {
    padding: 20px 16px;
    border-radius: 20px;
    max-height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .dialog-emoji { font-size: 34px; }

  .dialog h2 { font-size: 21px; margin-bottom: 8px; }

  .dialog-score strong { font-size: 26px; }

  .submit-box { padding: 10px; }

  .name-input,
  .nick-input { font-size: 16px; }   /* ≥16px iOS 才不会自动放大 */

  .nick-row { padding: 7px 10px; }

  .modal-card { padding: 14px; }

  .dialog .btn { width: 100%; min-height: 48px; }

  .dialog-hint { font-size: 11px; }
}

/* 手机横屏 / 矮屏：回到左右布局，棋盘吃满高度 */
@media (max-width: 860px) and (orientation: landscape) {
  .app {
    flex-direction: row;
    align-items: center;
    justify-content: center;
    gap: 10px;
  }

  .panel {
    order: 0;
    flex-direction: column;
    width: min(230px, 30vw);
    max-width: none;
    padding: 10px;
  }

  .stats { flex-direction: column; flex: 0 0 auto; }

  .actions { flex-direction: column; }

  .stage {
    height: calc(var(--vh) - 16px - var(--safe-t) - var(--safe-b));
    width: auto;
  }

  .stat-value { font-size: 18px; }
}

/* 小屏手机（iPhone SE 等 375 以下）：再压一档，保证一行放得下 */
@media (max-width: 380px) {
  .panel { padding: 6px 8px; gap: 6px; }

  .stats { gap: 6px; }

  .stat { padding: 5px 8px; }

  .stat-value { font-size: 17px; }

  .stat-label { font-size: 9px; }

  .btn { min-height: 44px; padding: 0 10px; font-size: 13px; }

  #boardBtn, #soundBtn { min-width: 42px; font-size: 16px; }
}

/* 顶部信息条在横屏下是竖排的，恢复成图标 + 文字 */
@media (max-width: 860px) and (orientation: landscape) {
  #boardBtn, #soundBtn { min-width: 0; padding: 0 10px; font-size: 13px; }
  #boardBtn .lbl, #soundBtn .lbl { display: inline; }
  #boardBtn .ico, #soundBtn .ico { margin-right: 4px; }
}

/* 很矮的横屏（手机横过来且高度不足 430px）：进一步压缩 */
@media (max-width: 860px) and (orientation: landscape) and (max-height: 430px) {
  .stat-label { display: none; }
  .stat { padding: 4px 8px; }
  .stat-value { font-size: 16px; }
  .btn { min-height: 40px; padding: 0 10px; font-size: 13px; }
}

/* 桌面端矮窗口 */
@media (max-height: 620px) and (min-width: 861px) {
  .stage { height: 92vh; }
  .chain-box { display: none; }
}
