/* ============================================================
   2048 - スタイルシート
   ============================================================ */

:root {
  --grid-size: 4;
  /* 画面の高さ（100dvh 非対応の古いブラウザは 100vh） */
  --view-h: 100vh;
  /* 盤面より上（見出し・スコア・説明・操作ボタン）と盤面の下の余白の高さ。
     画面の高さから共通ヘッダー・下部広告とこれを引いた値を盤面の上限にし、
     背の低い画面（ノートPC・Safari のツールバー表示中など）でも盤面が画面外にはみ出さないようにする */
  --board-reserve: 200px;
  --app-width: min(480px, calc(100vw - 32px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)));
  --board-size: min(var(--app-width),
    max(200px, calc(var(--view-h) - var(--sgh-top, 0px) - var(--ad-space, 0px) - var(--board-reserve))));
  --gap: calc(var(--board-size) * 0.026);
  --cell-size: calc((var(--board-size) - var(--gap) * (var(--grid-size) + 1)) / var(--grid-size));

  --color-bg: #faf8ef;
  --color-text: #776e65;
  --color-text-light: #f9f6f2;
  --color-board: #bbada0;
  --color-cell: rgba(238, 228, 218, 0.35);
  --color-accent: #8f7a66;
  --color-accent-hover: #9f8b77;

  --move-duration: 120ms;
  --pop-duration: 200ms;
  --appear-duration: 180ms;

  --radius: calc(var(--board-size) * 0.014);
}

@supports (height: 100dvh) {
  :root {
    --view-h: 100dvh;
  }
}

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

/* .overlay等のdisplay指定がhidden属性(UAのdisplay:none)に勝ってしまうのを防ぐ */
[hidden] {
  display: none !important;
}

html {
  height: 100%;
  /* スワイプ操作時にページ全体が引っ張られる・跳ねるのを防ぐ */
  overscroll-behavior: none;
}

body {
  min-height: 100%;
  /* 画面全体をスワイプ操作領域にするため、タッチによるネイティブスクロール・ズームを無効化 */
  touch-action: none;
  overscroll-behavior: none;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", sans-serif;
  background: var(--color-bg);
  background-image: radial-gradient(circle at 20% 10%, rgba(237, 194, 46, 0.06), transparent 40%),
                    radial-gradient(circle at 85% 90%, rgba(143, 122, 102, 0.08), transparent 45%);
  color: var(--color-text);
  display: flex;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  /* ノッチ・ホームバー等のセーフエリア対応 */
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.app {
  width: var(--app-width);
  /* 下はスマホ下部の固定広告のぶんも空けて、フッターが広告の下に隠れないようにする */
  padding: calc(24px + env(safe-area-inset-top)) 0
    calc(40px + max(env(safe-area-inset-bottom, 0px), var(--ad-space, 0px)));
}

/* ---------- ヘッダー ---------- */

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.title {
  font-size: clamp(48px, 14vw, 68px);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -2px;
  background: linear-gradient(135deg, #edc22e 0%, #f2b179 60%, #f59563 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 2px 3px rgba(119, 110, 101, 0.25));
}

.scores {
  display: flex;
  gap: 8px;
}

.score-box {
  position: relative;
  min-width: 88px;
  padding: 8px 14px;
  background: var(--color-board);
  border-radius: 8px;
  text-align: center;
  box-shadow: 0 2px 6px rgba(119, 110, 101, 0.25);
}

.score-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1px;
  color: #eee4da;
}

.score-value {
  font-size: 22px;
  font-weight: 800;
  color: #fff;
  font-variant-numeric: tabular-nums;
}

.score-addition-anchor {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.score-addition {
  position: absolute;
  left: 0;
  right: 0;
  top: 4px;
  font-size: 20px;
  font-weight: 800;
  color: rgba(119, 110, 101, 0.9);
  text-align: center;
  animation: score-float 600ms ease-out forwards;
}

@keyframes score-float {
  0%   { opacity: 1; transform: translateY(0); }
  100% { opacity: 0; transform: translateY(-42px); }
}

/* ---------- サブヘッダー ---------- */

.sub-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}

.description {
  font-size: 14px;
  line-height: 1.5;
}

.controls {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  font-family: inherit;
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 120ms ease, transform 80ms ease, opacity 120ms ease;
  user-select: none;
  -webkit-user-select: none;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: var(--color-accent);
  color: var(--color-text-light);
  box-shadow: 0 2px 6px rgba(119, 110, 101, 0.35);
}

.btn-primary:hover {
  background: var(--color-accent-hover);
}

.btn-secondary {
  background: #cdc1b4;
  color: var(--color-text);
}

.btn-secondary:hover {
  background: #d6cabc;
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
  transform: none;
}

/* ---------- ボード ---------- */

.board-wrapper {
  position: relative;
}

.board {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
  /* 背の低い画面では盤面が見出しの幅より小さくなるので中央に寄せる */
  margin: 0 auto;
  background: var(--color-board);
  border-radius: calc(var(--radius) * 2);
  touch-action: none;
  box-shadow: 0 8px 24px rgba(119, 110, 101, 0.3);
  /* 長押ししてからのスワイプでタイルの数字が選択され、コピー用のメニューが出るのを防ぐ */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  /* 盤面の大きさに合わせて結果画面の中身を詰める（下の @container） */
  container-type: inline-size;
}

.grid-background {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(var(--grid-size), var(--cell-size));
  grid-template-rows: repeat(var(--grid-size), var(--cell-size));
  gap: var(--gap);
  padding: var(--gap);
}

.grid-cell {
  background: var(--color-cell);
  border-radius: var(--radius);
}

/* ---------- タイル ---------- */

.tile-container {
  position: absolute;
  inset: 0;
  z-index: 2;
}

.tile {
  position: absolute;
  top: var(--gap);
  left: var(--gap);
  width: var(--cell-size);
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
  font-weight: 800;
  font-size: calc(var(--cell-size) * 0.42);
  font-variant-numeric: tabular-nums;
  transition: transform var(--move-duration) ease-in-out;
  will-change: transform;
  z-index: 2;
}

/* 外側の.tileはJSがinline transformで位置指定するため、
   出現・合体アニメーションは.tile-inner側のscaleのみで行う */
.tile-new .tile-inner {
  animation: inner-appear var(--appear-duration) ease var(--move-duration) backwards;
}

.tile-merged .tile-inner {
  animation: inner-pop var(--pop-duration) ease var(--move-duration) backwards;
}

.tile-inner {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius);
}

@keyframes inner-appear {
  0%   { transform: scale(0); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes inner-pop {
  0%   { transform: scale(0); }
  50%  { transform: scale(1.2); }
  100% { transform: scale(1); }
}

/* タイル色 */
.tile-2    .tile-inner { background: #eee4da; color: var(--color-text); }
.tile-4    .tile-inner { background: #ede0c8; color: var(--color-text); }
.tile-8    .tile-inner { background: #f2b179; color: var(--color-text-light); }
.tile-16   .tile-inner { background: #f59563; color: var(--color-text-light); }
.tile-32   .tile-inner { background: #f67c5f; color: var(--color-text-light); }
.tile-64   .tile-inner { background: #f65e3b; color: var(--color-text-light); }
.tile-128  .tile-inner { background: #edcf72; color: var(--color-text-light); box-shadow: 0 0 20px 6px rgba(243, 215, 116, 0.35); }
.tile-256  .tile-inner { background: #edcc61; color: var(--color-text-light); box-shadow: 0 0 22px 7px rgba(243, 215, 116, 0.4); }
.tile-512  .tile-inner { background: #edc850; color: var(--color-text-light); box-shadow: 0 0 24px 8px rgba(243, 215, 116, 0.45); }
.tile-1024 .tile-inner { background: #edc53f; color: var(--color-text-light); box-shadow: 0 0 26px 9px rgba(243, 215, 116, 0.5); }
.tile-2048 .tile-inner { background: #edc22e; color: var(--color-text-light); box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0.6); }
.tile-super .tile-inner { background: #3c3a33; color: var(--color-text-light); box-shadow: 0 0 30px 10px rgba(60, 58, 51, 0.5); }

/* 桁数によるフォントサイズ調整 */
.tile-128  .tile-inner, .tile-256 .tile-inner, .tile-512 .tile-inner { font-size: calc(var(--cell-size) * 0.36); }
.tile-1024 .tile-inner, .tile-2048 .tile-inner { font-size: calc(var(--cell-size) * 0.3); }
.tile-super .tile-inner { font-size: calc(var(--cell-size) * 0.26); }

/* ---------- オーバーレイ ---------- */

.overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: calc(var(--radius) * 2);
  background: rgba(250, 248, 239, 0.75);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: overlay-fade 600ms ease 300ms backwards;
}

.overlay.overlay-win {
  background: rgba(237, 194, 46, 0.5);
}

@keyframes overlay-fade {
  0%   { opacity: 0; }
  100% { opacity: 1; }
}

.overlay-content {
  text-align: center;
  padding: 20px;
}

.overlay-title {
  /* 盤面の幅でも抑える（画面は広いが背が低く盤面が小さいとき「ゲームオーバー」が2行に折れないように） */
  font-size: min(clamp(36px, 10vw, 56px), calc(var(--board-size) * 0.12));
  font-weight: 800;
  margin-bottom: 8px;
}

.overlay-win .overlay-title {
  color: var(--color-text-light);
  text-shadow: 0 2px 8px rgba(119, 110, 101, 0.4);
}

.overlay-message {
  font-size: 16px;
  margin-bottom: 20px;
}

.overlay-win .overlay-message {
  color: var(--color-text-light);
  text-shadow: 0 1px 4px rgba(119, 110, 101, 0.4);
}

.overlay-buttons {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.overlay-credits {
  margin-top: 28px;
  font-size: 13px;
  line-height: 1.7;
  color: rgba(119, 110, 101, 0.85);
}

.credits-title {
  font-weight: 700;
}

.credits-copyright a {
  color: inherit;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.credits-copyright a:hover {
  color: var(--color-accent);
}

/* ---------- フッター ---------- */

.footer {
  margin-top: 20px;
  font-size: 13px;
  color: rgba(119, 110, 101, 0.75);
  text-align: center;
  line-height: 1.6;
}

.footer-credits {
  margin-top: 10px;
  font-size: 12px;
}

.footer-credits a {
  color: inherit;
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.footer-credits a:hover {
  color: var(--color-accent);
}

/* ---------- レスポンシブ ---------- */

@media (max-width: 520px) {
  :root {
    --board-reserve: 180px;
  }

  .app {
    padding-top: 16px;
  }

  .header {
    margin-bottom: 8px;
  }

  .sub-header {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    margin-bottom: 12px;
  }

  .description {
    text-align: center;
    font-size: 13px;
  }

  .controls {
    justify-content: center;
  }

  .score-box {
    min-width: 72px;
    padding: 6px 10px;
  }

  .score-value {
    font-size: 18px;
  }
}

/* 幅の狭いスマホ（幅360px 前後以下）: 操作ボタン3つとスコア欄を1行に収める */
@media (max-width: 370px) {
  .controls {
    gap: 6px;
  }

  .btn {
    padding: 10px 10px;
    font-size: 13px;
  }

  .score-box {
    min-width: 64px;
    padding: 6px 8px;
  }
}

/* スマホ横向きなど背の低い横長の画面: 見出し・操作ボタン・フッターを左、盤面を右に並べる
   （縦に積むと盤面が画面の下にはみ出し、スワイプ操作のためスクロールもできない） */
@media (orientation: landscape) and (max-height: 540px) {
  :root {
    --board-size: min(480px,
      max(180px, calc(var(--view-h) - var(--sgh-top, 0px) - var(--ad-space, 0px) - 24px)),
      calc(100vw - 32px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 20px - 290px));
    --side-width: min(380px,
      calc(100vw - 32px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px) - 20px - var(--board-size)));
  }

  .app {
    width: calc(var(--side-width) + 20px + var(--board-size));
    display: grid;
    grid-template-columns: var(--side-width) var(--board-size);
    grid-template-rows: auto auto 1fr;
    column-gap: 20px;
    align-items: start;
    padding-top: 12px;
    padding-bottom: calc(12px + max(env(safe-area-inset-bottom, 0px), var(--ad-space, 0px)));
  }

  /* main の中の説明・操作ボタン（左）と盤面（右）を .app のグリッドに直接並べる */
  main {
    display: contents;
  }

  .header {
    grid-column: 1;
    grid-row: 1;
  }

  .title {
    font-size: 44px;
  }

  .sub-header {
    grid-column: 1;
    grid-row: 2;
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    margin-bottom: 0;
  }

  .description {
    text-align: center;
  }

  .controls {
    flex-wrap: wrap;
    justify-content: center;
  }

  .btn {
    padding: 10px 12px;
  }

  .board-wrapper {
    grid-column: 2;
    grid-row: 1 / 4;
  }

  .footer {
    grid-column: 1;
    grid-row: 3;
    margin-top: 12px;
  }
}

/* 盤面が小さいとき（幅の狭いスマホ・背の低い画面・横向き）は、結果画面の中身
   （見出し・ランキング登録の案内・ボタン・クレジット）を詰めて盤面からはみ出さないようにする */
@container (max-width: 355px) {
  .overlay-content {
    padding: 10px;
  }

  .overlay-title {
    font-size: 32px;
    margin-bottom: 4px;
  }

  .overlay-message {
    margin-bottom: 12px;
  }

  .overlay-credits {
    margin-top: 12px;
  }

  .overlay .sgr-cta {
    margin: 6px 0 2px;
  }
}

@container (max-width: 290px) {
  .overlay-title {
    font-size: 28px;
  }

  .overlay-message {
    font-size: 14px;
    margin-bottom: 8px;
  }

  .overlay-credits {
    margin-top: 8px;
    font-size: 12px;
    line-height: 1.5;
  }

  .overlay .sgr-cta-msg {
    font-size: 13px;
    margin-bottom: 4px;
  }

  .overlay .sgr-cta-btn,
  .overlay-buttons .btn {
    padding: 8px 14px;
  }
}

/* さらに小さい盤面（主にスマホ横向き。フッターが盤面の左に見えている）ではクレジットを省く */
@container (max-width: 250px) {
  .overlay-content {
    padding: 6px;
  }

  .overlay-title {
    font-size: min(28px, calc(var(--board-size) * 0.12));
  }

  .overlay-message {
    margin-bottom: 4px;
  }

  .overlay-credits {
    display: none;
  }

  .overlay .sgr-cta {
    margin: 4px 0 2px;
  }

  .overlay .sgr-cta-msg {
    font-size: 12px;
  }
}

/* モーション低減設定への配慮 */
@media (prefers-reduced-motion: reduce) {
  .tile {
    transition-duration: 1ms;
  }

  .tile-new .tile-inner,
  .tile-merged .tile-inner,
  .overlay,
  .score-addition {
    animation-duration: 1ms;
    animation-delay: 0ms;
  }
}

/* ---------- 全国ランキング（SGRanking 共通モジュール） ---------- */
.sgr-hidden {
  display: none !important;
}

.sgr-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(80, 70, 60, 0.55);
}

.sgr-panel {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--color-bg);
  color: var(--color-text);
  border-radius: 12px;
  padding: 20px 18px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3);
}

.sgr-title {
  margin: 0;
  font-size: 20px;
  text-align: center;
}

.sgr-tabs {
  display: flex;
  gap: 6px;
  justify-content: center;
}

.sgr-tab {
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  font-weight: bold;
  background: var(--color-cell);
  color: var(--color-text);
  cursor: pointer;
}

.sgr-tab-active {
  background: var(--color-accent);
  color: var(--color-text-light);
}

.sgr-register-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: bold;
  text-align: center;
}

.sgr-form {
  display: flex;
  gap: 8px;
}

.sgr-name {
  flex: 1;
  min-width: 0;
  font-size: 16px;
  padding: 8px 10px;
  border: 2px solid var(--color-board);
  border-radius: 8px;
  background: #fff;
  color: var(--color-text);
}

.sgr-name:focus {
  outline: none;
  border-color: var(--color-accent);
}

.sgr-submit {
  border: none;
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 15px;
  font-weight: bold;
  background: var(--color-accent);
  color: var(--color-text-light);
  cursor: pointer;
}

.sgr-submit:hover {
  background: var(--color-accent-hover);
}

.sgr-submit:disabled {
  opacity: 0.5;
  cursor: default;
}

.sgr-error {
  margin: 4px 0 0;
  font-size: 12px;
  color: #c0392b;
}

.sgr-status {
  margin: 0;
  font-size: 13px;
  text-align: center;
}

.sgr-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

.sgr-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 14px;
}

.sgr-row:nth-child(even) {
  background: var(--color-cell);
}

.sgr-row.sgr-row-me {
  background: var(--color-accent);
  color: var(--color-text-light);
}

.sgr-rank {
  width: 2em;
  text-align: right;
  font-weight: bold;
  flex-shrink: 0;
}


.sgr-row-name {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sgr-row-score {
  font-weight: bold;
  flex-shrink: 0;
}

.sgr-close {
  border: none;
  border-radius: 8px;
  padding: 10px;
  font-size: 14px;
  font-weight: bold;
  background: var(--color-cell);
  color: var(--color-text);
  cursor: pointer;
}

.sgr-cta {
  margin: 10px 0 4px;
  text-align: center;
}

.sgr-cta-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: bold;
}

.sgr-cta-btn {
  border: none;
  border-radius: 8px;
  padding: 10px 16px;
  font-size: 15px;
  font-weight: bold;
  background: #edc22e;
  color: #fff;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(237, 194, 46, 0.5);
}

.sgr-cta-link {
  border: none;
  background: none;
  padding: 6px;
  font-size: 13px;
  color: inherit;
  text-decoration: underline;
  cursor: pointer;
}

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