/*
 * style.css
 *
 * iPhone Air（CSS ピクセルで 420 x 912）を基準にした固定レイアウト。
 * 捨て牌が増えても他の要素がずれないよう、各段の高さを内容ではなく
 * 牌のサイズから決めている。
 *
 *   上から  操作ボタン / 局情報 / 対面2人（手牌1段・副露1段・河2段）/
 *           ログ（余白を吸収）/ 自分の河2段 / 自分の副露1段 / 自分の手牌1段 /
 *           コマンド（最下段に固定）
 */

:root {
  --felt: #1e6b46;
  --felt-dark: #17553a;
  --panel: rgba(0, 0, 0, .28);
  --line: rgba(255, 255, 255, .18);
  --tile-bg: #fbf7ec;
  --tile-edge: #cdc4ae;
  --tile-back: #2c7fbf;
  --man: #b3403a;
  --aka: #d81e28;
  --pin: #1f6fb2;
  --sou: #1f7a45;
  --honor: #2b2b2b;
  --accent: #ffcf4d;
  --danger: #e2564a;
  --text: #f3f6f4;
  --font-ui: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", Meiryo, system-ui, sans-serif;
  --font-display: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", "Noto Serif JP", "Times New Roman", serif;

  /* 盤面の幅（iPhone Air は 420px）。牌の寸法はすべてここから決まる。
     上家・下家は縦に積んで全幅を使うので、河・副露・伏せ牌はすべて自分と同じ寸法。 */
  --app-w: min(100vw, 430px);

  --hand-w: min(30px, calc((var(--app-w) - 68px) / 14));   /* 手牌 14 枚が 1 段に収まる幅 */
  --hand-h: calc(var(--hand-w) * 1.36);
  --pond-w: min(21px, calc((var(--app-w) - 58px) / 12));   /* 河は 12 列 */
  --pond-h: calc(var(--pond-w) * 1.36);
  --mini-w: var(--pond-w);   /* 相手の河・副露も自分と同じ大きさ */
  --mini-h: var(--pond-h);
  --back-w: var(--hand-w);   /* 相手の伏せ牌も手牌と同じ大きさ */
  --back-h: var(--hand-h);

  --tile-w: var(--pond-w);
  --tile-h: var(--pond-h);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 0%, #2a8a5b, var(--felt) 45%, var(--felt-dark));
  color: var(--text);
  font-family: var(--font-ui);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

#app {
  width: 100%;
  max-width: 430px;
  height: 100dvh;
  margin: 0 auto;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom, 0px));
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr) auto auto;
  gap: 5px;
  overflow: hidden;
}

/* ---- 上部の操作ボタン ---- */
.topbar {
  display: flex;
  align-items: center;
  gap: 5px;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 5px 7px;
}
.topbar .title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 14px;
  letter-spacing: .12em;
  margin-right: 2px;
  cursor: default;
  user-select: none;
}
.topbar .title.open-mode { color: var(--accent); }
.topbar .title.open-mode::after { content: '◉'; font-size: 10px; margin-left: 3px; vertical-align: super; }
.btn {
  background: rgba(255, 255, 255, .12);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 7px;
  padding: 4px 8px;
  font-size: 12px;
  font-family: inherit;
  line-height: 1.3;
  cursor: pointer;
  transition: background .12s;
  white-space: nowrap;
}
.btn:hover { background: rgba(255, 255, 255, .22); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: var(--accent); color: #2b2205; border-color: transparent; font-weight: 700; }
.btn.primary:hover { background: #ffdd7a; }
.btn.warn { background: var(--danger); border-color: transparent; font-weight: 700; }
.btn:disabled { opacity: .4; cursor: default; }

/* ---- 局情報 ---- */
.infobar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 4px 8px;
  font-size: 12px;
  height: calc(var(--pond-h) + 12px);
  overflow: hidden;
}
.infobar .round {
  font-family: var(--font-display);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .05em;
  white-space: nowrap;
}
.infobar .stat { white-space: nowrap; font-variant-numeric: tabular-nums; opacity: .9; }
.infobar .stat b { color: var(--accent); }
.infobar .dora { margin-left: auto; display: flex; gap: 2px; align-items: center; }

/* ---- 対面 2 人（上から 下家 → 上家 → 自分 の順に手番が流れる） ---- */
.board { display: grid; grid-template-columns: 1fr; gap: 5px; }
.seat {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 4px 5px;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.seat.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.seat-head {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
  height: 17px;
  overflow: hidden;
  white-space: nowrap;
}
.seat-head .nm { font-weight: 700; }
.seat-head .pt { color: var(--accent); font-variant-numeric: tabular-nums; }
.seat-head .wind { background: rgba(255, 255, 255, .16); border-radius: 3px; padding: 0 4px; }
.seat-head .wind.dealer { background: var(--accent); color: #2b2205; font-weight: 700; }
.seat-head .riichi-mark { color: var(--danger); font-weight: 700; }
.seat-head .chip { background: rgba(255, 255, 255, .12); border-radius: 999px; padding: 0 7px; }
.seat-head .seat-label { opacity: .6; }
.seat-head .tag {
  background: rgba(255, 255, 255, .18);
  border-radius: 3px;
  padding: 0 4px;
  font-size: 10px;
}

/* ルール画面のキャラクター一覧 */
.char { margin: 9px 0; }
.char-top { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.char-top .tag { background: rgba(255, 255, 255, .18); border-radius: 3px; padding: 0 5px; font-size: 11px; }
.char-top .nums { margin-left: auto; font-size: 11.5px; opacity: .75; font-variant-numeric: tabular-nums; }
.char-desc { font-size: 12px; opacity: .85; line-height: 1.6; }

/* 段の高さは中身ではなく牌の寸法で決める（増減でずれないように） */
.row-back {
  display: flex;
  gap: 2px;
  height: calc(var(--back-h) + 1px);
  align-items: flex-end;
  overflow: hidden;
}
.row-exposed {
  display: flex;
  align-items: center;
  gap: 5px;
  height: calc(var(--mini-h) + 2px);
  overflow: hidden;
}
.meld { display: flex; gap: 1px; }
.kita { display: flex; align-items: center; gap: 1px; }
.kita .label { font-size: 9px; opacity: .7; margin-right: 1px; }

.pond-box {
  height: calc(var(--mini-h) * 2 + 3px);
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: none;
}
.pond-box::-webkit-scrollbar { display: none; }
.pond {
  display: grid;
  grid-template-columns: repeat(12, var(--mini-w));
  gap: 3px 2px;
  justify-content: center;
}
.pond .riichi-tile { outline: 2px solid var(--danger); outline-offset: -2px; }
/* ツモ切りの牌は牌面を沈ませる。明るいままの牌が手出し */
.pond .tsumogiri {
  background: linear-gradient(#ddd9cc, #c9c4b3);
  border-color: #ada693;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .05);
}

/* ---- ログ（余った高さを吸収する。卓の中央に直接刷る） ---- */
.log {
  padding: 2px 8px;
  font-size: 12.5px;
  line-height: 1.7;
  overflow-y: auto;
  font-variant-numeric: tabular-nums;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  scrollbar-width: none;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .5);
}
.log::-webkit-scrollbar { display: none; }
.log div { opacity: .8; flex: none; }
.log div.hl { color: var(--accent); }

/* ---- 自分 ---- */
.self {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 5px 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.self.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.self .seat-head { font-size: 12px; height: 17px; }
.self .pond-box { height: calc(var(--pond-h) * 2 + 3px); }
.self .pond { grid-template-columns: repeat(12, var(--pond-w)); gap: 3px 2px; }
.self .row-exposed { height: calc(var(--pond-h) + 2px); }

.self-hand {
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-end;
  gap: 2px;
  height: calc(var(--hand-h) + 8px);
  padding-top: 6px;
}
.self-hand .tile { --tile-w: var(--hand-w); --tile-h: var(--hand-h); cursor: default; }
.self-hand .tile.playable { cursor: pointer; }
.self-hand .tile.playable:hover { transform: translateY(-5px); box-shadow: 0 4px 10px rgba(0, 0, 0, .35); }
.self-hand .drawn { margin-left: 7px; }
.self-hand .tile.choice { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---- コマンド（最下段に固定） ---- */
.actions {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  height: 44px;
  overflow-x: auto;
  scrollbar-width: none;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  padding: 4px 8px;
}
.actions::-webkit-scrollbar { display: none; }
.actions .btn { padding: 7px 12px; font-size: 13px; font-weight: 700; }
.actions .hint { font-size: 11.5px; opacity: .8; line-height: 1.3; white-space: nowrap; }

/* ---- 牌 ---- */
.tile {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: var(--tile-w);
  height: var(--tile-h);
  background: linear-gradient(#fffdf6, var(--tile-bg));
  border: 1px solid var(--tile-edge);
  border-bottom-width: 2px;
  border-radius: 3px;
  color: var(--honor);
  font-weight: 700;
  line-height: 1;
  user-select: none;
  flex: none;
  vertical-align: middle;
}
.tile .n { font-family: var(--font-display); font-size: calc(var(--tile-h) * .38); }
.tile .sl { font-size: calc(var(--tile-h) * .30); margin-top: 1px; }
.tile .z { font-family: var(--font-display); font-size: calc(var(--tile-h) * .48); }
.tile.m { color: var(--man); }
.tile.p { color: var(--pin); }
.tile.s { color: var(--sou); }
.tile.z-haku { color: #3a6ea8; }
.tile.z-hatsu { color: var(--sou); }
.tile.z-chun { color: var(--man); }
.tile.red { color: var(--aka); }
.tile.back {
  background: linear-gradient(150deg, #4a9ad6, var(--tile-back) 60%, #23648f);
  border-color: #1c5680;
}
/* 相手の手牌の位置に置く牌（伏せ牌と、裏技で公開したときの表牌の両方） */
.tile.hand-size { --tile-w: var(--back-w); --tile-h: var(--back-h); }
.tile.dim { opacity: .4; }
.tile.called { outline: 2px solid var(--accent); outline-offset: -2px; }

/* ---- 結果表示 ---- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .62);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 14px;
  z-index: 20;
}
.overlay[hidden] { display: none; }
.sheet {
  background: #14251d;
  border: 1px solid rgba(255, 255, 255, .22);
  border-radius: 13px;
  padding: 14px 16px;
  width: min(430px, 100%);
  max-height: 88dvh;
  overflow-y: auto;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .5);
}
.sheet h2 {
  font-family: var(--font-display);
  margin: 0 0 6px;
  font-size: 20px;
  letter-spacing: .05em;
  text-wrap: balance;
}
.sheet .sub { font-size: 12px; opacity: .85; margin-bottom: 8px; }
.sheet .agari { display: flex; gap: 2px; flex-wrap: wrap; margin-bottom: 8px; align-items: flex-end; }
.sheet .agari .tile { --tile-w: var(--hand-w); --tile-h: var(--hand-h); }
.yaku-list { display: grid; grid-template-columns: 1fr auto; gap: 1px 12px; font-size: 12.5px; margin: 8px 0; }
.yaku-list .han { text-align: right; font-variant-numeric: tabular-nums; opacity: .85; }
.sheet .score {
  font-family: var(--font-display);
  font-size: 23px;
  font-weight: 700;
  letter-spacing: .04em;
  color: var(--accent);
  margin: 6px 0;
  font-variant-numeric: tabular-nums;
}
.sheet .detail { font-size: 12.5px; line-height: 1.75; font-variant-numeric: tabular-nums; }
.sheet .divider { height: 1px; background: rgba(255, 255, 255, .18); margin: 9px 0; }
.standings { font-size: 14px; line-height: 1.9; font-variant-numeric: tabular-nums; }
.standings .me { color: var(--accent); font-weight: 700; }

/* 縦に余裕がない端末では牌を段階的に小さくして、1 画面に収める */
@media (max-height: 800px) {
  :root {
    --hand-w: min(24px, calc((var(--app-w) - 68px) / 14));
    --pond-w: min(17px, calc((var(--app-w) - 58px) / 12));
  }
}
@media (max-height: 700px) {
  :root {
    --hand-w: min(21px, calc((var(--app-w) - 68px) / 14));
    --pond-w: min(14px, calc((var(--app-w) - 58px) / 12));
  }
  .log { font-size: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
  .self-hand .tile.playable:hover { transform: none; }
}
