/* ============ 上海 - Shanghai ============ */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

:root {
  --felt-1: #14532d;
  --felt-2: #0b3d20;
  --bar-bg: rgba(0, 0, 0, 0.35);
  --gold: #fbbf24;
  --tile-face: #fdfaf2;
  --tile-face-2: #f3ecd8;
  --tile-edge: #d9cba8;
  --tile-side: #b89d5e;
  --tile-side-2: #8f7539;
}

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

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Sans", sans-serif;
  background: radial-gradient(ellipse at 50% 30%, var(--felt-1) 0%, var(--felt-2) 75%);
  color: #fff;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
}

/* ============ ヘッダー ============ */
#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 12px;
  padding-top: max(6px, env(safe-area-inset-top));
  background: var(--bar-bg);
  flex-shrink: 0;
}

#logo {
  font-size: 20px;
  font-weight: 800;
  color: var(--gold);
  letter-spacing: 2px;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
}

#stats-bar {
  display: flex;
  gap: 14px;
  flex: 1;
  justify-content: center;
}

.stat { display: flex; flex-direction: column; align-items: center; min-width: 44px; }
.stat-label { font-size: 10px; opacity: 0.7; }
.stat-value { font-size: 15px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-value.danger { color: #f87171; animation: danger-blink 0.8s ease-in-out infinite; }
@keyframes danger-blink { 50% { opacity: 0.35; } }

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

.icon-btn {
  width: 36px; height: 36px;
  border: none; border-radius: 8px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff; font-size: 17px;
  cursor: pointer;
}
.icon-btn:active { background: rgba(255, 255, 255, 0.25); }

/* ============ 盤面 ============ */
#board-wrap {
  flex: 1;
  position: relative;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board-scale { transform-origin: center center; }

#board { position: relative; }

/* ============ 牌 ============ */
.tile {
  position: absolute;
  cursor: pointer;
  border-radius: 7px;
  background: linear-gradient(160deg, var(--tile-face) 0%, var(--tile-face-2) 100%);
  border: 1px solid var(--tile-edge);
  box-shadow:
    3px 3px 0 var(--tile-side),
    6px 6px 0 var(--tile-side-2),
    9px 9px 10px rgba(0, 0, 0, 0.45);
  transition: filter 0.15s, transform 0.12s;
}

.tile svg { display: block; width: 100%; height: 100%; pointer-events: none; }

.tile.locked { filter: brightness(0.68) saturate(0.85); cursor: default; }

.tile.selected {
  background: linear-gradient(160deg, #fef3c7 0%, #fde68a 100%);
  border-color: var(--gold);
  outline: 3px solid var(--gold);
  outline-offset: -1px;
  transform: translateY(-4px);
  filter: none !important;
}

/* ヒント：対象の2牌を発光＋バウンドさせ、他の牌は暗く沈める */
.tile.hinted {
  background: linear-gradient(160deg, #e0f7ff 0%, #a5e6ff 100%);
  border-color: #38bdf8;
  outline: 4px solid #38bdf8;
  outline-offset: -1px;
  box-shadow:
    3px 3px 0 var(--tile-side),
    6px 6px 0 var(--tile-side-2),
    0 0 18px 6px rgba(56, 189, 248, 0.85);
  animation: hint-bounce 0.65s ease-in-out infinite;
  z-index: 999999 !important;
  filter: none !important;
}
@keyframes hint-bounce {
  0%, 100% { transform: translateY(0) scale(1); }
  50% { transform: translateY(-7px) scale(1.05); }
}
#board.hinting .tile:not(.hinted):not(.selected) {
  filter: brightness(0.4) saturate(0.6);
}
@keyframes hint-pulse {
  0%, 100% { outline: 3px solid rgba(56, 189, 248, 0); outline-offset: -1px; }
  50% { outline: 3px solid rgba(56, 189, 248, 1); outline-offset: -1px; }
}

.tile.shake { animation: shake 0.3s; }
@keyframes shake {
  0%, 100% { margin-left: 0; }
  25% { margin-left: -5px; }
  75% { margin-left: 5px; }
}

.tile.blocker { animation: blocker-flash 0.5s; }
@keyframes blocker-flash {
  0%, 100% { outline: 3px solid rgba(248, 113, 113, 0); outline-offset: -1px; }
  40% { outline: 3px solid rgba(248, 113, 113, 0.95); outline-offset: -1px; }
}

.tile.matched {
  transition: transform 0.3s ease-in, opacity 0.3s ease-in;
  transform: scale(1.35);
  opacity: 0;
  pointer-events: none;
}

/* マッチ時のスコアポップ */
.pop {
  position: absolute;
  font-size: 26px;
  font-weight: 800;
  color: var(--gold);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  animation: pop-up 0.8s ease-out forwards;
  z-index: 99999;
}
@keyframes pop-up {
  0% { opacity: 1; transform: translateY(0) scale(0.8); }
  100% { opacity: 0; transform: translateY(-46px) scale(1.25); }
}

/* ============ ツールバー ============ */
#toolbar {
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 8px 10px;
  background: var(--bar-bg);
  flex-shrink: 0;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 68px;
  padding: 6px 12px;
  border: none; border-radius: 10px;
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}
.tool-btn:active { background: rgba(255, 255, 255, 0.25); }
.tool-btn:disabled { opacity: 0.4; cursor: default; }
.tool-icon { font-size: 18px; }

/* ============ トースト ============ */
#toast {
  position: fixed;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(110px + var(--ad-space, 0px));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 10px 20px;
  border-radius: 20px;
  font-size: 14px;
  font-weight: 600;
  z-index: 1200; /* モーダル(1000)より手前。手詰まりモーダル中の通知も見えるように */
  white-space: nowrap;
  transition: opacity 0.3s;
}

/* ============ モーダル ============ */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

/* 共通ヘッダーの「遊び方」「設定・メニュー」はクリア・手詰まり画面の上からも開けるので、それより手前に出す */
#modal-menu, #modal-help { z-index: 1010; }

.modal-box {
  background: #1c2b22;
  border: 1px solid rgba(255, 255, 255, 0.15);
  border-radius: 16px;
  padding: 22px;
  width: 100%;
  max-width: 400px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}

.modal-title {
  font-size: 20px;
  text-align: center;
  margin-bottom: 16px;
  color: var(--gold);
}

.menu-section {
  margin-bottom: 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.menu-label { font-size: 13px; opacity: 0.8; margin-bottom: 8px; }

.layout-btns { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

.layout-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 10px 4px;
  border: 2px solid rgba(255, 255, 255, 0.2);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.06);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.layout-btn small { font-size: 11px; font-weight: 400; opacity: 0.7; }
.layout-btn.active { border-color: var(--gold); background: rgba(251, 191, 36, 0.12); color: var(--gold); }

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

.toggle-btn {
  min-width: 58px;
  padding: 6px 14px;
  border: none;
  border-radius: 16px;
  background: #16a34a;
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}
.toggle-btn.off { background: rgba(255, 255, 255, 0.18); }

.menu-best { font-size: 14px; text-align: center; opacity: 0.9; }

.modal-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin-top: 6px;
}
.modal-buttons.vertical { flex-direction: column; }

.modal-btn {
  flex: 1;
  padding: 12px 16px;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}
.modal-btn.primary { background: #16a34a; color: #fff; }
.modal-btn.primary:active { background: #15803d; }
.modal-btn.sub { background: rgba(255, 255, 255, 0.14); color: #fff; }
.modal-btn.sub:active { background: rgba(255, 255, 255, 0.26); }
.modal-btn:disabled { opacity: 0.4; cursor: default; }

/* 遊び方 */
.help-body { font-size: 14px; line-height: 1.75; }
.help-body p { margin-bottom: 10px; }
.help-body ul { margin: 0 0 10px 4px; list-style: none; }
.help-body li { margin-bottom: 5px; }
.help-body strong { color: var(--gold); }

/* クリア */
.win-badge {
  text-align: center;
  font-size: 17px;
  font-weight: 800;
  color: var(--gold);
  margin-bottom: 10px;
  animation: hint-pulse 1s infinite;
}
.win-stats { margin-bottom: 16px; }
.win-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 6px;
  font-size: 16px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  font-variant-numeric: tabular-nums;
}

.stuck-text { text-align: center; font-size: 14px; line-height: 1.7; margin-bottom: 14px; }

/* ============ 紙吹雪 ============ */
#confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 900;
}

/* ============ フッター ============ */
#footer {
  text-align: center;
  font-size: 11px;
  opacity: 0.65;
  padding: 4px 0 max(6px, env(safe-area-inset-bottom));
  background: var(--bar-bg);
  flex-shrink: 0;
  line-height: 1.6;
}
#footer a { color: #fff; }

.hidden { display: none !important; }

/* 小さい画面 */
@media (max-width: 420px) {
  #logo { font-size: 17px; letter-spacing: 1px; }
  #stats-bar { gap: 8px; }
  .tool-btn { min-width: 58px; padding: 5px 8px; }
}

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / 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; }
}
