/* 忖度リバーシ */
:root {
  --bg: #10161c;
  --bg-2: #182129;
  --felt: #1f7a4d;
  --felt-dark: #17603c;
  --frame: #6b4a2c;
  --frame-dark: #4a3119;
  --line: rgba(0, 0, 0, .45);
  --text: #eef3f2;
  --muted: #9fb0ac;
  --accent: #ffd166;
  --danger: #ff6b6b;   /* 忖度無しレベルの強調色 */

  /* セーフエリア（ノッチ・ホームバー・パンチホール）。PC ブラウザでは 0 */
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);

  /* 画面下に重なるネイティブ広告バナー（AdMob）のための予約領域。
     バナーは WebView の上に描かれるネイティブビューなので DOM では場所を取らない。
     バナー表示時に JS 側から
       document.documentElement.style.setProperty('--ad-h', h + 'px')
     と書き換えれば、下パディングと盤面サイズの両方が自動で追従する */
  --ad-h: 0px;

  /* 実際に中身を置ける高さ */
  --avail-h: calc(100vh - var(--safe-t) - var(--safe-b) - var(--ad-h));

  /* 盤面以外（ヘッダー・メッセージ・フッター・余白）が使う高さ。この値を引いた
     残りを盤面に割り当てる。375x812 での実測は ゲーム 216px / タイトル 577px（英語モードは 611px） */
  --game-chrome: 230px;
  --title-chrome: 580px;

  --board-size: clamp(200px, min(92vw, calc(var(--avail-h) - var(--game-chrome))), 520px);
  --title-board-size: clamp(150px, min(74vw, calc(var(--avail-h) - var(--title-chrome))), 320px);
}

/* 英語モードはロゴに「Sontaku Reversi」の 1 行が増え、説明文も 2 行になる */
html[lang="en"] { --title-chrome: 614px; }

/* dvh が使える環境ではアドレスバーの伸縮に追従させる */
@supports (height: 100dvh) {
  :root {
    --avail-h: calc(100dvh - var(--safe-t) - var(--safe-b) - var(--ad-h));
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
}

html {
  background: var(--bg);
  /* ダブルタップズームだけを無効にする。ピンチズームは残す（拡大したい人のため）。
     touch-action は祖先の指定と合成されるので、ここで指定すれば全体に効く */
  touch-action: manipulation;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", "Meiryo", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  display: flex;
  justify-content: center;
  align-items: center;
  /* 中身が画面より高いとき、中央寄せだと上が切れて読めなくなるので端に寄せる */
  align-items: safe center;
  min-height: 100vh;
  padding: var(--safe-t) var(--safe-r) calc(var(--safe-b) + var(--ad-h)) var(--safe-l);
  overflow-x: hidden;

  /* モバイルでの誤操作を抑止する */
  -webkit-touch-callout: none;   /* 長押しのコンテキストメニュー */
  -webkit-user-select: none;     /* テキスト選択 */
  user-select: none;
  -webkit-tap-highlight-color: transparent;  /* タップ時の青いハイライト */
  overscroll-behavior: none;     /* 引っ張りすぎたときのバウンス／リロード */
}
@supports (height: 100dvh) {
  body { min-height: 100dvh; }
}

/* 背景のグラデーション。background-attachment: fixed は WebView で描画が崩れる
   ことがあるので、固定配置のレイヤーとして敷く */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(1200px 700px at 50% -10%, #22303a 0%, transparent 60%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
}

#app { width: 100%; }

.screen {
  display: none;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 18px 12px 24px;
}
.screen.is-active { display: flex; }

/* ===== 盤面 ===== */
.board-frame {
  padding: clamp(8px, 2.2vw, 14px);
  background: linear-gradient(160deg, var(--frame) 0%, var(--frame-dark) 100%);
  border-radius: 10px;
  box-shadow:
    0 2px 0 rgba(255, 255, 255, .12) inset,
    0 18px 40px rgba(0, 0, 0, .55);
}

.board {
  width: var(--board-size);
  height: var(--board-size);
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: 3px;
  overflow: hidden;
}

.cell {
  position: relative;
  background: linear-gradient(150deg, var(--felt) 0%, var(--felt-dark) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6%;
}

/* 星（4隅の目印） */
.cell.star::before {
  content: "";
  position: absolute;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .55);
  right: -4px;
  bottom: -4px;
  z-index: 2;
}

/* 石 */
.disc {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform .45s cubic-bezier(.4, .1, .3, 1);
  opacity: 0;
  pointer-events: none;
}
.cell.has-disc .disc { opacity: 1; }
.cell.white .disc { transform: rotateY(180deg); }

.disc .face {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  backface-visibility: hidden;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .5);
}
.disc .front {
  background: radial-gradient(circle at 32% 28%, #5a5f66 0%, #23262b 45%, #0c0d10 100%);
}
.disc .back {
  transform: rotateY(180deg);
  background: radial-gradient(circle at 32% 28%, #ffffff 0%, #eceff1 45%, #b8bfc4 100%);
}

.cell.placed .disc { animation: pop .32s ease-out; }
@keyframes pop {
  0% { transform: scale(.2); }
  60% { transform: scale(1.12); }
  100% { transform: scale(1); }
}
.cell.white.placed .disc { animation: pop-w .32s ease-out; }
@keyframes pop-w {
  0% { transform: rotateY(180deg) scale(.2); }
  60% { transform: rotateY(180deg) scale(1.12); }
  100% { transform: rotateY(180deg) scale(1); }
}

/* 直前の手 */
.cell.last::after {
  content: "";
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  border: 2px solid var(--accent);
  opacity: .85;
  z-index: 3;
  pointer-events: none;
}

/* 着手可能マス */
.cell.playable { cursor: pointer; }
.cell.playable .hint {
  position: absolute;
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: rgba(255, 255, 255, .3);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, .18);
  animation: breathe 1.8s ease-in-out infinite;
}
@keyframes breathe {
  0%, 100% { transform: scale(.85); opacity: .55; }
  50% { transform: scale(1.05); opacity: .95; }
}
.cell .hint { display: none; }
.cell.playable .hint { display: block; }

/* hover 効果はマウスのある環境だけに限る。
   タッチだと最後にタップしたマスに hover が貼り付いたままになる */
@media (hover: hover) and (pointer: fine) {
  .cell.playable:hover { background: linear-gradient(150deg, #2a8d5b 0%, #1c6b43 100%); }
}

/* ===== タイトル画面 ===== */
#title-screen { gap: 18px; }

/* 言語の切り替えボタンはロゴの上に置く。ロゴ画像に重ならないよう、
   ボタンの下端 + 4px からロゴを始める */
.title-head {
  text-align: center;
  padding-top: calc(var(--lang-top) + var(--lang-h) + 4px - var(--screen-pad-t));
}

/* 言語の切り替えボタン。タイトル画面は縦がぎりぎりなので（増えた高さは
   そのまま盤面が縮む）、見た目は小さくし、タップ領域だけ ::before で 44px に広げる。
   右端は操作部（.title-controls）の右端に合わせる */
#title-screen {
  position: relative;
  --lang-top: 4px;
  --lang-h: 26px;
  --screen-pad-t: 18px;   /* .screen の上パディングと揃える */
}
.lang-btn {
  position: absolute;
  top: var(--lang-top);
  right: calc((100% - min(88vw, 340px)) / 2);
  z-index: 1;
  height: var(--lang-h);
  padding: 0 10px;
  font-size: 11px;
  font-family: inherit;
  color: var(--muted);
  background: rgba(16, 22, 28, .6);
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 8px;
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.lang-btn::before {
  content: "";
  position: absolute;
  inset: -9px -4px;
}
@media (hover: hover) and (pointer: fine) {
  .lang-btn:hover {
    color: var(--text);
    border-color: rgba(255, 255, 255, .4);
  }
}

/* ポータルへ戻るボタン（Web 版のみ）。言語ボタンと左右対称の位置・高さに置き、
   矢印だけを見せる。タップ領域は ::before で 44px に広げる */
.back-btn {
  position: absolute;
  top: var(--lang-top);
  left: calc((100% - min(88vw, 340px)) / 2);
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  width: var(--lang-h);
  height: var(--lang-h);
  color: var(--muted);
  border-radius: 8px;
  transition: color .15s;
}
.back-btn::before {
  content: "";
  position: absolute;
  inset: -9px;
}
.back-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
@media (hover: hover) and (pointer: fine) {
  .back-btn:hover { color: var(--text); }
}

.title-board .board {
  width: var(--title-board-size);
  height: var(--title-board-size);
}

/* 盤（装飾）は画面の高さに応じて縮むので、操作部の幅は盤に連動させず独立させる */
.title-controls {
  width: min(88vw, 340px);
  text-align: center;
}

.game-title {
  margin: 0;
  line-height: 0;
}

/* ロゴ画像（img/title-logo.jpg）。元画像 img/reversi.jpg の余白を切り詰めただけのもの */
.title-logo {
  display: block;
  width: min(80vw, 420px);
  height: auto;
  margin: 0 auto;
}

/* 英語表記「Sontaku Reversi」。英語モードのときだけ出す */
.logo-en {
  display: none;
  margin-top: 2px;
  font-family: "Helvetica Neue", "Segoe UI", Arial, sans-serif;
  font-size: 18px;
  font-weight: 800;
  line-height: 1.2;
  letter-spacing: .06em;
  color: #e0b25a;
}
html[lang="en"] .logo-en { display: block; }
.tagline {
  margin: 12px 0 0;
  font-size: 13px;
  color: var(--muted);
}

.choice { margin-bottom: 16px; }
.choice-note {
  margin: 8px 0 0;
  font-size: 11px;
  line-height: 1.6;
  color: var(--muted);
  min-height: 2.2em;
}
/* 戦績（選択中の忖度レベルのもの） */
.record-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.record {
  font-size: 12px;
  color: var(--text);
  font-variant-numeric: tabular-nums;
  letter-spacing: .05em;
}
.text-btn {
  min-height: 32px;
  padding: 4px 8px;
  font-size: 11px;
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: 0;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}
@media (hover: hover) and (pointer: fine) {
  .text-btn:hover { color: var(--text); }
}
.result-record {
  margin: -8px 0 16px;
  color: var(--muted);
}

.choice-label {
  display: block;
  font-size: 12px;
  color: var(--muted);
  letter-spacing: .1em;
  margin-bottom: 8px;
}
.choice-buttons {
  display: flex;
  gap: 8px;
  justify-content: center;
}
.side-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 11px 8px;
  min-height: 44px;          /* タップ領域の確保 */
  font-size: 13px;
  font-family: inherit;
  color: var(--text);
  background: rgba(255, 255, 255, .06);
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s, border-color .15s, transform .1s;
}
@media (hover: hover) and (pointer: fine) {
  .side-btn:hover { background: rgba(255, 255, 255, .12); }
}
.side-btn:active { transform: translateY(1px); }
.side-btn.is-selected {
  background: rgba(255, 209, 102, .16);
  border-color: var(--accent);
  color: #fff;
}
/* 忖度無しだけは警告色にして、他の 2 つと性格が違うことを見せる */
.side-btn[data-level="none"].is-selected {
  background: rgba(255, 107, 107, .18);
  border-color: var(--danger);
}
/* 3 択になる忖度レベルは 1 つあたりが狭いので、文字を詰める */
#level-choice .side-btn {
  padding-left: 4px;
  padding-right: 4px;
  letter-spacing: 0;
}

.mini-disc {
  width: 15px;
  height: 15px;
  border-radius: 50%;
  display: inline-block;
  flex: none;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .5);
}
.mini-disc.black { background: radial-gradient(circle at 32% 28%, #5a5f66, #16181c 70%); }
.mini-disc.white { background: radial-gradient(circle at 32% 28%, #fff, #c3c9cd 70%); }

.primary-btn {
  width: 100%;
  padding: 14px;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: .18em;
  font-family: inherit;
  color: #22160a;
  background: linear-gradient(180deg, #ffe08a 0%, var(--accent) 60%, #e9b83f 100%);
  border: none;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: 0 6px 0 #a9832a, 0 12px 24px rgba(0, 0, 0, .4);
  transition: transform .08s, box-shadow .08s;
}
@media (hover: hover) and (pointer: fine) {
  .primary-btn:hover { filter: brightness(1.05); }
}
.primary-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #a9832a, 0 6px 14px rgba(0, 0, 0, .4);
}
.primary-btn.small {
  width: auto;
  padding: 11px 22px;
  min-height: 44px;
  font-size: 14px;
}

.hint-text {
  margin: 12px 0 0;
  font-size: 11px;
  color: var(--muted);
}

/* ===== ゲーム画面 ===== */
.game-header {
  width: var(--board-size);
  max-width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.score {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(255, 255, 255, .05);
  border: 1px solid transparent;
  transition: border-color .2s, background .2s;
  min-width: 108px;
}
.score.is-turn {
  border-color: var(--accent);
  background: rgba(255, 209, 102, .12);
}
.score-num {
  font-size: 21px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  min-width: 2ch;
  text-align: center;
}
.score-tag { font-size: 11px; color: var(--muted); }

.turn-box { text-align: center; flex: 1; }
.turn-label {
  font-size: 13px;
  letter-spacing: .08em;
  color: var(--text);
  white-space: nowrap;
}
.thinking {
  display: none;
  justify-content: center;
  gap: 4px;
  margin-top: 6px;
  height: 6px;
}
.thinking.is-on { display: flex; }
.thinking span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
  animation: blink 1.1s infinite ease-in-out;
}
.thinking span:nth-child(2) { animation-delay: .18s; }
.thinking span:nth-child(3) { animation-delay: .36s; }
@keyframes blink {
  0%, 80%, 100% { opacity: .2; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-3px); }
}

.board-wrap { position: relative; }

.message {
  min-height: 22px;
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  letter-spacing: .05em;
}
.message.is-alert { color: var(--accent); }

.game-footer {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}
.level-badge {
  padding: 8px 12px;
  font-size: 11px;
  letter-spacing: .05em;
  color: var(--muted);
  background: rgba(255, 255, 255, .05);
  border-radius: 8px;
  white-space: nowrap;
}
.level-badge.is-max {
  color: var(--accent);
  background: rgba(255, 209, 102, .12);
}
.level-badge.is-none {
  color: var(--danger);
  background: rgba(255, 107, 107, .12);
}
.ghost-btn {
  padding: 9px 16px;
  min-height: 44px;          /* タップ領域の確保 */
  font-size: 12px;
  font-family: inherit;
  color: var(--muted);
  background: transparent;
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 8px;
  cursor: pointer;
  transition: color .15s, border-color .15s, background .15s;
}
@media (hover: hover) and (pointer: fine) {
  .ghost-btn:hover {
    color: var(--text);
    border-color: rgba(255, 255, 255, .4);
    background: rgba(255, 255, 255, .05);
  }
}
.ghost-btn.small { padding: 11px 18px; font-size: 13px; }

/* ===== 結果オーバーレイ ===== */
.overlay {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(8, 12, 15, .72);
  border-radius: 10px;
  z-index: 10;
}
.overlay.is-on { display: flex; animation: fade .35s ease-out; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.overlay-card {
  width: 84%;
  padding: 24px 18px;
  text-align: center;
  background: rgba(16, 22, 28, .95);
  border: 1px solid rgba(255, 255, 255, .14);
  border-radius: 14px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .6);
}
.result-title {
  font-size: clamp(22px, 6vw, 30px);
  font-weight: 700;
  letter-spacing: .1em;
  margin-bottom: 8px;
}
.result-title.win { color: var(--accent); }
.result-title.lose { color: #8fb7d6; }
.result-title.draw { color: #cfd6d4; }
.result-score {
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  letter-spacing: .1em;
  color: var(--text);
}
.result-comment {
  margin: 12px 0 18px;
  font-size: 12px;
  line-height: 1.7;
  color: var(--muted);
}
.overlay-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

@media (max-width: 380px) {
  .score { min-width: 92px; padding: 7px 9px; }
  .score-num { font-size: 18px; }
}

/* 縦に余裕のない端末（小型 Android など）。固定で積み上がる部分を詰めて
   盤面に回せる高さを稼ぐ。360x640 での実測は ゲーム 192px / タイトル 504px（英語モードは 539px）。
   ゲーム画面はこれで必ず 1 画面に収まる。タイトル画面は操作部（タップ領域
   44px の確保が優先）が詰められないので、収まらない場合はスクロールさせる */
@media (max-height: 720px) {
  :root {
    --game-chrome: 208px;
    --title-chrome: 506px;
  }
  html[lang="en"] { --title-chrome: 541px; }
  #title-screen { --screen-pad-t: 10px; }
  .screen { padding: 10px 12px 14px; }
  #title-screen { gap: 12px; }
  .title-logo { width: min(62vw, 320px); }
  .tagline { margin-top: 8px; font-size: 12px; }
  .choice { margin-bottom: 10px; }
  .hint-text { margin-top: 8px; }
}
