:root {
  --bg: #f6f1e6;
  --card: #fffdf8;
  --ink: #3b3730;
  --ink-soft: #857c6d;
  --line: #e6ddcc;
  --primary: #e8453c;
  --primary-dark: #c8362e;
  --red: #e8453c;
  --yellow: #f4b400;
  --blue: #3b78e7;
  --green: #34a853;
  --radius: 16px;
  --shadow: 0 6px 20px rgba(90, 74, 45, .12);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html { height: 100%; }
html, body {
  margin: 0;
  height: 100dvh;   /* 手机上跟着地址栏收放，避免底部被切掉 */
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB",
               "Microsoft YaHei", system-ui, sans-serif;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.screen { display: none; flex: 1; min-height: 0; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- 通用控件 ---------- */
.btn {
  border: none;
  border-radius: 12px;
  background: #efe7d8;
  color: var(--ink);
  font-size: 15px;
  font-weight: 600;
  padding: 11px 18px;
  cursor: pointer;
  font-family: inherit;
  transition: transform .08s, filter .15s;
}
.btn:active { transform: scale(.96); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--primary); color: #fff; box-shadow: 0 3px 0 var(--primary-dark); }
.btn.primary:active { box-shadow: 0 1px 0 var(--primary-dark); }
.btn.big { width: 100%; padding: 14px; font-size: 17px; }
.btn.ghost { background: transparent; color: var(--ink-soft); }
.btn.tiny { padding: 6px 12px; font-size: 13px; }
.btn.round { width: 48px; height: 48px; padding: 0; border-radius: 50%; font-size: 20px; background: var(--card); box-shadow: var(--shadow); }

input[type=text], input:not([type]) {
  font-family: inherit;
}
.field { display: block; margin-bottom: 18px; }
.field > span { display: block; font-size: 13px; color: var(--ink-soft); margin-bottom: 8px; font-weight: 600; }
.field input, .join-row input {
  width: 100%;
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 16px;
  background: #fff;
  color: var(--ink);
  outline: none;
  font-family: inherit;
}
.field input:focus, .join-row input:focus { border-color: var(--primary); }

/* ---------- 首页 ---------- */
#screen-home { align-items: center; justify-content: center; padding: 20px; overflow-y: auto; }
.home-card {
  width: 100%; max-width: 400px;
  background: var(--card);
  border-radius: 22px;
  padding: 26px 22px;
  box-shadow: var(--shadow);
}
.logo { margin: 0 0 6px; font-size: 26px; text-align: center; }
.sub { margin: 0 0 22px; text-align: center; color: var(--ink-soft); font-size: 14px; }

.avatar-grid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 6px; }
.avatar-grid button {
  aspect-ratio: 1; border: 2px solid transparent; border-radius: 10px;
  background: #f4eee1; font-size: 20px; cursor: pointer; padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.avatar-grid button.on { border-color: var(--primary); background: #fff; transform: scale(1.08); }

.join-row { display: flex; gap: 8px; margin: 14px 0 10px; }
.join-row input { flex: 1; text-align: center; letter-spacing: 6px; font-size: 20px; font-weight: 700; }

/* ---------- 大厅 ---------- */
#screen-lobby { align-items: center; justify-content: center; padding: 20px; overflow-y: auto; }
.lobby-card { width: 100%; max-width: 420px; background: var(--card); border-radius: 22px; padding: 22px; box-shadow: var(--shadow); }
.room-code { display: flex; align-items: center; gap: 12px; margin-bottom: 18px; }
.room-code .label { font-size: 13px; color: var(--ink-soft); }
.room-code strong { font-size: 34px; letter-spacing: 6px; color: var(--primary); flex: 1; }

.seat-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 18px; }
.seat {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 14px; border-radius: 14px; background: #f7f2e7;
  border-left: 6px solid var(--line);
}
.seat .ava { font-size: 26px; }
.seat .nm { flex: 1; font-weight: 700; }
.seat .tag { font-size: 12px; padding: 3px 9px; border-radius: 20px; background: #e6ddcc; color: var(--ink-soft); font-weight: 600; }
.seat .tag.ready { background: #d6f0dc; color: #1e7a38; }
.seat .tag.host { background: #ffe9b8; color: #96660a; }
.seat.empty { opacity: .5; border-left-color: transparent; }
.seat.off { opacity: .55; }

.color-picker { margin-bottom: 18px; }
.color-picker .label { font-size: 13px; color: var(--ink-soft); font-weight: 600; display: block; margin-bottom: 8px; }
.color-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.color-grid button {
  height: 46px; border: 3px solid transparent; border-radius: 12px;
  cursor: pointer; font-size: 14px; font-weight: 700; color: #fff; padding: 0;
}
.color-grid button.on { border-color: var(--ink); transform: scale(1.05); }
.color-grid button:disabled { opacity: .3; cursor: default; }

.lobby-actions { display: flex; flex-direction: column; gap: 10px; }
.tip { font-size: 12px; color: var(--ink-soft); line-height: 1.7; margin: 16px 0 0; }

/* ---------- 对局 ---------- */
/* 左右不留边，让棋盘铺满屏幕宽度（竖屏时宽度就是棋盘的天花板） */
#screen-game { padding: 6px 0 8px; gap: 6px; }
#screen-game > *:not(.board-wrap) { padding-left: 8px; padding-right: 8px; }

.players {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: 5px; flex: none;
  /* +16px 抵消左右 padding，让卡片外沿和棋盘严格对齐 */
  width: 100%; max-width: calc(var(--board-size, 640px) + 16px); margin-inline: auto;
}
.players:empty { display: none; }
.pcard {
  background: var(--card); border-radius: 12px; padding: 5px 4px;
  text-align: center; box-shadow: 0 2px 6px rgba(90,74,45,.08);
  border-bottom: 4px solid transparent; position: relative; overflow: hidden;
}
.pcard .ava { font-size: 22px; line-height: 1.1; }
.pcard .nm { font-size: 11px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pcard .meta { font-size: 10px; color: var(--ink-soft); }
.pcard.turn { transform: translateY(-2px); box-shadow: 0 6px 14px rgba(90,74,45,.2); }
.pcard.turn::after {
  content: ''; position: absolute; inset: 0; border-radius: 12px;
  border: 2px solid currentColor; animation: blink 1.1s infinite;
}
.pcard.off { opacity: .5; }
.pcard.done { background: #f0e7d3; }
@keyframes blink { 0%,100% { opacity: 1 } 50% { opacity: .25 } }

.board-wrap {
  flex: 1; min-width: 0; min-height: 0;
  display: flex; align-items: center; justify-content: center;
  position: relative;
}
/* 实际像素尺寸由 render.js 按容器较小边算出，这里只管样式 */
#board {
  display: block;
  border-radius: 18px;
  box-shadow: var(--shadow);
  touch-action: manipulation;
}

.controls { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: none; }
.status { font-size: 14px; font-weight: 600; color: var(--ink-soft); min-height: 19px; text-align: center; }
.control-row { display: flex; align-items: center; gap: 20px; }

/* 骰子：象牙白骨骰的样子，1 点和 4 点是红的 */
.dice {
  width: 74px; height: 74px; border-radius: 22%;
  border: 2px solid #d9ccb1;
  background: linear-gradient(158deg, #fffdf6 0%, #f7f0dd 55%, #efe5cd 100%);
  box-shadow:
    inset 0 2px 5px rgba(255, 255, 255, .95),
    inset 0 -4px 8px rgba(120, 100, 70, .16),
    0 5px 0 #cdbfa1,
    0 9px 20px rgba(90, 74, 45, .3);
  cursor: pointer; display: flex; align-items: center; justify-content: center;
  padding: 0; transition: transform .1s, box-shadow .1s;
}
.dice:active:not(:disabled) {
  transform: translateY(4px);
  box-shadow:
    inset 0 2px 5px rgba(255, 255, 255, .95),
    inset 0 -4px 8px rgba(120, 100, 70, .16),
    0 1px 0 #cdbfa1,
    0 3px 8px rgba(90, 74, 45, .25);
}
.dice:disabled { opacity: .45; }
.dice.rolling { animation: tumble .34s infinite ease-in-out; }
@keyframes tumble {
  0%   { transform: rotate(-11deg) scale(.94) }
  50%  { transform: rotate(10deg) scale(1.04) }
  100% { transform: rotate(-11deg) scale(.94) }
}

#dice-face { display: grid; grid-template-columns: repeat(3, 10px); grid-template-rows: repeat(3, 10px); gap: 5px; }
#dice-face i {
  width: 10px; height: 10px; border-radius: 50%; display: block;
  background: radial-gradient(circle at 34% 28%, #6e6455 0%, #3f3527 60%, #241d13 100%);
  box-shadow: inset 0 -1px 2px rgba(0, 0, 0, .45), 0 1px 1px rgba(255, 255, 255, .6);
}
#dice-face i.red {
  background: radial-gradient(circle at 34% 28%, #ef7d74 0%, #d5342b 55%, #9d2018 100%);
}
/* 一点独大，跟真骰子一样 */
#dice-face.one i { transform: scale(1.8); }

/* ---------- 聊天 ---------- */
.chat-bar { display: none; flex-direction: column; gap: 8px; padding-top: 6px; }
.chat-bar.open { display: flex; }
.quick { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.quick button { background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 6px 12px; font-size: 13px; cursor: pointer; font-family: inherit; color: var(--ink); }
.chat-input { display: flex; gap: 8px; }
.chat-input input { flex: 1; border: 2px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: 15px; outline: none; font-family: inherit; }

.flyer { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.bubble {
  position: absolute; left: 50%; bottom: 12%;
  background: rgba(255,255,255,.96); border-radius: 18px; padding: 8px 14px;
  font-size: 15px; font-weight: 600; box-shadow: var(--shadow); white-space: nowrap;
  animation: float-up 2.6s ease-out forwards;
}
@keyframes float-up {
  0% { opacity: 0; transform: translate(-50%, 20px) scale(.8) }
  15% { opacity: 1; transform: translate(-50%, 0) scale(1) }
  75% { opacity: 1; transform: translate(-50%, -50px) }
  100% { opacity: 0; transform: translate(-50%, -80px) }
}

/* ---------- 弹层 ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(50,42,30,.5);
  display: none; align-items: center; justify-content: center; padding: 20px; z-index: 50;
  backdrop-filter: blur(3px);
}
.modal.open { display: flex; }
.modal-card {
  background: var(--card); border-radius: 22px; padding: 24px;
  width: 100%; max-width: 400px; max-height: 86vh; overflow-y: auto;
  box-shadow: 0 20px 50px rgba(0,0,0,.25);
  animation: pop .22s ease-out;
}
.modal-card.wide { max-width: 620px; }
@keyframes pop { from { transform: scale(.9); opacity: 0 } }
.modal-card h2 { margin: 0 0 16px; font-size: 20px; text-align: center; }
.modal-actions { display: flex; gap: 10px; margin-top: 18px; }
.modal-actions.column { flex-direction: column; }
.modal-actions .btn { flex: 1; }

.rank-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.rank-list li {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-radius: 14px; background: #f7f2e7;
  border-left: 6px solid var(--line);
}
.rank-list .place { font-size: 20px; width: 30px; text-align: center; }
.rank-list .ava { font-size: 24px; }
.rank-list .nm { flex: 1; font-weight: 700; }

.invite-text {
  width: 100%; border: 2px solid var(--line); border-radius: 12px;
  padding: 11px 13px; font-size: 14px; font-family: inherit; line-height: 1.7;
  background: #fff; color: var(--ink); resize: none; outline: none;
  -webkit-user-select: text; user-select: text;
}
.invite-text:focus { border-color: var(--primary); }

.rules { margin: 0; padding-left: 20px; line-height: 1.9; font-size: 14px; color: var(--ink); }
.rules b { color: var(--primary); }

.switch-row { display: flex; align-items: center; justify-content: space-between; padding: 12px 0; font-weight: 600; }
.switch-row input { width: 46px; height: 26px; }

.stats-body table { width: 100%; border-collapse: collapse; font-size: 13px; }
.stats-body th, .stats-body td { padding: 8px 6px; text-align: center; border-bottom: 1px solid var(--line); }
.stats-body th { color: var(--ink-soft); font-size: 12px; font-weight: 600; }
.stats-body td.name { text-align: left; font-weight: 700; white-space: nowrap; }
.stats-body .crown { color: var(--yellow); }
.stats-body .empty { text-align: center; color: var(--ink-soft); padding: 30px 0; }
.stats-history { margin-top: 18px; font-size: 12px; color: var(--ink-soft); line-height: 1.9; }

/* ---------- 提示条 ---------- */
.toast {
  position: fixed; top: calc(env(safe-area-inset-top) + 14px); left: 50%;
  transform: translate(-50%, -140%);
  background: var(--ink); color: #fff; padding: 11px 20px; border-radius: 24px;
  font-size: 14px; font-weight: 600; z-index: 100; transition: transform .28s;
  max-width: 86vw; text-align: center; box-shadow: 0 8px 24px rgba(0,0,0,.25);
}
.toast.show { transform: translate(-50%, 0); }

@media (max-height: 700px) {
  .dice { width: 62px; height: 62px; }
  .btn.round { width: 42px; height: 42px; font-size: 18px; }
  .pcard .ava { font-size: 19px; }
}

/* 高屏手机：棋盘受宽度限制没法再大，把富余的竖向空间给控件，别让页面显得空 */
@media (min-height: 760px) {
  .dice { width: 88px; height: 88px; }
  #dice-face { grid-template-columns: repeat(3, 12px); grid-template-rows: repeat(3, 12px); gap: 6px; }
  #dice-face i { width: 12px; height: 12px; }
  .btn.round { width: 54px; height: 54px; font-size: 22px; }
  .control-row { gap: 26px; }
  .controls { gap: 10px; }
  .status { font-size: 15px; }
  .pcard { padding: 9px 4px; }
  .pcard .ava { font-size: 27px; }
  .pcard .nm { font-size: 12px; }
  .pcard .meta { font-size: 11px; }
}

/* 手机横屏：玩家条挪到左边、操作区挪到右边，把中间整块留给棋盘 */
@media (orientation: landscape) and (max-height: 560px) {
  /* 红黄一列在棋盘左边，绿蓝一列在右边，中间整块留给棋盘 */
  #screen-game { flex-direction: row; align-items: stretch; padding: 6px; gap: 6px; }
  #screen-game > *:not(.board-wrap) { padding-left: 0; padding-right: 0; }
  .players {
    grid-template-columns: 1fr; grid-auto-rows: min-content;
    align-content: center; width: clamp(72px, 14vw, 100px);
    max-width: none; margin-inline: 0;
  }
  .pcard {
    display: grid; grid-template-columns: auto 1fr; column-gap: 6px;
    align-items: center; padding: 5px 7px; text-align: left;
  }
  .pcard .ava { grid-row: 1 / 3; font-size: 19px; }
  .pcard .nm { font-size: 11px; }
  .pcard .meta { font-size: 10px; }
  .controls { width: clamp(84px, 17vw, 120px); justify-content: center; gap: 10px; }
  .control-row { flex-direction: column-reverse; gap: 10px; }
  .status { font-size: 11px; line-height: 1.4; }
  .dice { width: 58px; height: 58px; }
  .chat-bar { position: absolute; left: 6px; right: 6px; bottom: 6px; z-index: 20;
              background: var(--card); border-radius: 14px; padding: 8px; box-shadow: var(--shadow); }
}
