/* ============================================================
   地下鉄王 - METRO LINK  スタイルシート
   ダーク濃紺 × 地下鉄路線図テーマ
   ============================================================ */

:root {
  --bg: #0d1220;
  --bg-panel: #151c30;
  --bg-panel2: #1c2440;
  --line: #2b3a55;
  --text: #e8ecf5;
  --text-dim: #93a0bb;
  --you: #3b82f6;
  --you-bright: #60a5fa;
  --cpu: #ef4444;
  --cpu-bright: #f87171;
  --amber: #f0b429;
  --radius: 12px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  overscroll-behavior: none;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

#app {
  height: 100%;
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  /* 上のセーフエリアは共通ヘッダー（--sgh-top）が含むので、ここで重ねて空けない */
  padding: 0 var(--sar) 0 var(--sal);
  max-width: 640px;
  margin: 0 auto;
}

/* ---------- ヘッダー ---------- */
/* タイトル（地下鉄王 / METRO LINK）だけの帯は縦幅を圧迫するので隠す（ボタンは共通ヘッダーのメニューへ集約済み）。
   h1 と、共通ヘッダーが click() で押す元ボタンは DOM に残すため、display: none ではなく
   スクリーンリーダー向けの見えない形にする */
#topbar {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

#logo-wrap { display: flex; align-items: baseline; gap: 8px; }

#logo {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 0.06em;
  background: linear-gradient(90deg, var(--you-bright), var(--amber));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#logo-sub {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.22em;
  color: var(--text-dim);
}

#topbar-buttons { display: flex; gap: 6px; }

.icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-panel);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:active { transform: scale(0.94); }

/* ---------- ステータスバー ---------- */
#status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 4px 12px;
  flex: none;
}

.player-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 12px;
  min-width: 108px;
  justify-content: center;
  transition: box-shadow 0.25s, border-color 0.25s;
}
.player-chip.active { border-color: var(--amber); box-shadow: 0 0 8px rgba(240, 180, 41, 0.35); }

.chip-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.dot-you { background: var(--you); box-shadow: 0 0 6px var(--you); }
.dot-cpu { background: var(--cpu); box-shadow: 0 0 6px var(--cpu); }

.chip-name { font-size: 12px; font-weight: 700; }
.chip-dist { font-size: 12px; color: var(--text-dim); font-variant-numeric: tabular-nums; }

#turn-badge {
  font-size: 12px;
  font-weight: 700;
  color: var(--amber);
  text-align: center;
  min-width: 76px;
}

/* ---------- 盤面 ---------- */
#board-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  padding: 2px 4px;
}

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
}

/* 回転工事バー */
#rotate-bar {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(21, 28, 48, 0.95);
  border: 1px solid var(--amber);
  border-radius: 999px;
  padding: 7px 12px;
  z-index: 10;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.45);
  animation: toastIn 0.2s;
}
/* 選んだマスが盤の下端でバーに隠れるときは上端に出す（js の placeRotBar） */
#rotate-bar.at-top { top: 10px; bottom: auto; }
#rotate-label {
  font-size: 12px;
  font-weight: 700;
  color: var(--amber);
  white-space: nowrap;
}
#rotate-bar .btn { padding: 8px 14px; font-size: 13px; white-space: nowrap; }

/* ---------- 手札 ---------- */
#hand-bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-panel);
  border-top: 1px solid var(--line);
}

#hand {
  display: flex;
  gap: 6px;
  flex: 1;
  min-width: 0;
  justify-content: center;
}

/* 幅が足りない画面（390px 以下）では手札が縮み、「引き直す」が画面外に押し出されないようにする */
.hand-slot {
  position: relative;
  flex: 0 1 58px;
  min-width: 0;
  width: 58px;
  height: 62px;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--bg-panel2);
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.12s, border-color 0.12s, box-shadow 0.12s;
}
.hand-slot canvas { width: 100%; max-width: 52px; height: auto; aspect-ratio: 52 / 56; display: block; }
.hand-slot.selected {
  border-color: var(--amber);
  box-shadow: 0 0 10px rgba(240, 180, 41, 0.45);
  transform: translateY(-4px);
}
/* 選択中はもう一度タップで回転できることを示すバッジ */
.hand-slot.selected::after {
  content: "⟳";
  position: absolute;
  top: -8px;
  right: -6px;
  width: 19px;
  height: 19px;
  border-radius: 50%;
  background: var(--amber);
  color: #1a1205;
  font-size: 13px;
  font-weight: 800;
  line-height: 19px;
  text-align: center;
  pointer-events: none;
}
.hand-slot:disabled { opacity: 0.45; cursor: default; }

.tool-btn {
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg-panel2);
  color: var(--text);
  font-size: 11px;
  font-weight: 700;
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  flex: none;
}
.tool-btn .tool-icon { font-size: 16px; }
.tool-btn:active { transform: scale(0.94); }
.tool-btn:disabled { opacity: 0.4; }

/* ---------- 結果オーバーレイ ---------- */
#result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 11, 20, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  animation: fadeIn 0.35s;
}

#result-box {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 26px 30px;
  text-align: center;
  width: min(86vw, 340px);
  animation: popIn 0.35s cubic-bezier(0.2, 1.6, 0.4, 1);
}

#result-title { font-size: 30px; font-weight: 800; letter-spacing: 0.05em; }
#result-title.win { color: var(--you-bright); text-shadow: 0 0 18px rgba(96, 165, 250, 0.6); }
#result-title.lose { color: var(--cpu-bright); }
#result-title.draw { color: var(--text-dim); }

#result-sub { font-size: 13px; color: var(--text-dim); margin-top: 8px; }

#result-stats {
  display: flex;
  justify-content: center;
  gap: 18px;
  margin: 18px 0 4px;
}
.rstat { display: flex; flex-direction: column; gap: 2px; }
.rstat-label { font-size: 10px; color: var(--text-dim); }
.rstat span:last-child { font-size: 16px; font-weight: 700; font-variant-numeric: tabular-nums; }

#result-buttons { display: flex; gap: 10px; margin-top: 16px; justify-content: center; }

.btn {
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-weight: 700;
  padding: 11px 20px;
  cursor: pointer;
  color: #fff;
}
.btn:active { transform: scale(0.96); }
.btn-primary { background: linear-gradient(135deg, #2f6fe0, #3b82f6); box-shadow: 0 3px 10px rgba(59, 130, 246, 0.35); }
.btn-sub { background: var(--bg-panel2); border: 1px solid var(--line); color: var(--text); }

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(8, 11, 20, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 40;
  animation: fadeIn 0.25s;
}

.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  width: min(90vw, 400px);
  max-height: min(84vh, 84dvh);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.modal-title { font-size: 18px; font-weight: 800; text-align: center; }

.menu-section { display: flex; flex-direction: column; gap: 8px; }
.menu-caption { font-size: 11px; font-weight: 700; color: var(--text-dim); letter-spacing: 0.08em; }

.diff-buttons { display: flex; gap: 8px; }
.diff-btn {
  flex: 1;
  border: 2px solid var(--line);
  border-radius: 10px;
  background: var(--bg-panel2);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  padding: 9px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.diff-btn small { font-size: 9px; color: var(--text-dim); font-weight: 400; }
.diff-btn.selected { border-color: var(--amber); box-shadow: 0 0 8px rgba(240, 180, 41, 0.35); }

.menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 14px;
  padding: 2px 0;
}

.toggle {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--bg-panel2);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
}
.toggle .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--text-dim);
  transition: transform 0.2s, background 0.2s;
}
.toggle.on { background: rgba(59, 130, 246, 0.4); }
.toggle.on .knob { transform: translateX(20px); background: var(--you-bright); }

#record-list, .record-row { font-size: 13px; }
.record-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 2px;
  border-bottom: 1px dashed var(--line);
  font-variant-numeric: tabular-nums;
}
.record-row:last-child { border-bottom: none; }

.howto { padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.howto li { font-size: 12px; line-height: 1.55; color: var(--text); }
.howto b { color: var(--amber); }

/* ---------- トースト ---------- */
#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(96px + var(--sab) + var(--ad-space, 0px));
  transform: translateX(-50%);
  background: rgba(21, 28, 48, 0.95);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 50;
  pointer-events: none;
  animation: toastIn 0.25s;
  /* 1行で収まる幅なら1行のまま。狭い画面では折り返して画面外にはみ出さない */
  width: max-content;
  max-width: calc(100vw - 16px);
  text-align: center;
}

/* ---------- フッター ---------- */
#footer {
  flex: none;
  text-align: center;
  font-size: 10px;
  color: var(--text-dim);
  padding: 4px 8px calc(4px + var(--sab));
  background: var(--bg-panel);
  display: flex;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
}
#footer a { color: var(--text-dim); }

/* 320px 幅などの狭い画面では手札バーの余白を詰める */
@media (max-width: 359px) {
  #hand-bar { gap: 6px; padding: 6px; }
  #hand { gap: 4px; }
}

/* ---------- 汎用 ---------- */
.hidden { display: none !important; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes popIn { from { opacity: 0; transform: scale(0.8); } to { opacity: 1; transform: scale(1); } }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / js/ad-banner.js が挿入する）
   --ad-h    : 広告そのものの高さ。広告なしのときは 0px
   --ad-space: レイアウトが空けるべき高さ（広告 + セーフエリア）
   ========================================================================== */
:root {
  --ad-h: 0px;
  --ad-space: 0px;
}
html.has-ad-banner {
  --ad-space: calc(var(--ad-h, 0px) + env(safe-area-inset-bottom, 0px));
}

.ad-banner {
  /* height は広告そのものの高さ。セーフエリアぶんの padding は外側に足す */
  box-sizing: content-box;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* ランキング・リザルト等の全画面オーバーレイ（2147483000）よりは下に置く */
  z-index: 2147482000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  height: var(--ad-h, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
  background: rgba(14, 16, 20, 0.86);
  /* border ではなく inset shadow にして、枠線ぶん高さがずれないようにする */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.ad-banner__frame {
  display: block;
  width: 100%;
  height: var(--ad-h, 0px);
  border: 0;
  color-scheme: light;
}

@media print {
  .ad-banner { display: none; }
}
