/* ============================================================
   マインスイーパー - Minesweeper  style.css
   ============================================================ */

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

:root {
  --bg1: #10182b;
  --bg2: #1b2a4a;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-border: rgba(255, 255, 255, 0.12);
  --text: #eaf0ff;
  --text-dim: #8fa0c0;
  --accent: #4da3ff;
  --accent2: #ffd166;
  --danger: #ff5d5d;
  --cell: 34px;
  --radius: 14px;
}

html, body {
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", sans-serif;
  background: radial-gradient(120% 100% at 50% 0%, var(--bg2) 0%, var(--bg1) 70%);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

#app {
  min-height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 上のセーフエリアは共通ヘッダー（body の padding-top = --sgh-top）が含むので、ここでは足さない */
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- ヘッダー ---------- */
#topbar {
  width: 100%;
  max-width: 1100px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 14px 6px;
}

#logo-wrap { display: flex; align-items: center; gap: 10px; min-width: 0; }

#logo {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: 0.04em;
  background: linear-gradient(180deg, #fff, #a8c8ff);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}

#diff-badge {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent2);
  background: rgba(255, 209, 102, 0.12);
  border: 1px solid rgba(255, 209, 102, 0.35);
  border-radius: 999px;
  padding: 4px 10px;
  cursor: pointer;
  font-family: inherit;
}
#diff-badge:active { transform: scale(0.95); }

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

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  display: grid;
  place-items: center;
}
.icon-btn:active { transform: scale(0.92); }

/* ---------- ステータスバー ---------- */
#status-bar {
  width: 100%;
  max-width: 560px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 14px;
}

.status-box {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 7px 10px;
}

.status-icon { font-size: 15px; }

.status-value {
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 21px;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: #ffd166;
  text-shadow: 0 0 12px rgba(255, 209, 102, 0.4);
  min-width: 3.2ch;
  text-align: center;
}

#btn-face {
  width: 52px;
  height: 52px;
  flex-shrink: 0;
  font-size: 28px;
  border-radius: 14px;
  border: 1px solid var(--panel-border);
  background: linear-gradient(180deg, rgba(255,255,255,0.12), rgba(255,255,255,0.04));
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.35);
  transition: transform 0.1s;
}
#btn-face:active { transform: scale(0.9); }

/* ---------- 盤面 ---------- */
#board-wrap {
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 8px 10px;
  flex: 1;
}

#board-scroll {
  max-width: 100%;
  overflow: auto;
  border-radius: var(--radius);
  padding: 8px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--panel-border);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.4);
  -webkit-overflow-scrolling: touch;
}

#board {
  /* セルサイズはビューポートと盤面サイズから CSS で自動算出 */
  --cell-w: calc((min(100vw, 1100px) - 64px) / var(--cols, 9) - 3px);
  --cell-h: calc(((100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px)) - 330px) / var(--rows, 9) - 3px);
  --cell: clamp(22px, min(var(--cell-w), var(--cell-h)), 40px);
  display: grid;
  grid-template-columns: repeat(var(--cols, 9), var(--cell));
  gap: 3px;
  touch-action: pan-x pan-y;
}

.cell {
  width: var(--cell);
  height: var(--cell);
  border: none;
  border-radius: 6px;
  font-family: inherit;
  font-size: calc(var(--cell) * 0.52);
  font-weight: 800;
  display: grid;
  place-items: center;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  position: relative;
}

/* 未開封タイル */
.cell.closed {
  background: linear-gradient(160deg, #55719f 0%, #3d5580 55%, #33486d 100%);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.25), inset 0 -2px 3px rgba(0, 0, 0, 0.3);
}
@media (hover: hover) {
  .cell.closed:hover { filter: brightness(1.15); }
}
.cell.closed:active { filter: brightness(0.85); }

/* 市松模様で見やすく */
.cell.closed.alt {
  background: linear-gradient(160deg, #4e6893 0%, #384e76 55%, #2e4264 100%);
}

/* 開いたマス */
.cell.open {
  background: #1a2338;
  box-shadow: inset 0 1px 4px rgba(0, 0, 0, 0.45);
  cursor: default;
  animation: pop 0.18s ease-out;
}
.cell.open.alt { background: #182136; }

@keyframes pop {
  0% { transform: scale(0.75); opacity: 0.4; }
  100% { transform: scale(1); opacity: 1; }
}

/* 数字の色 */
.cell.n1 { color: #62b0ff; }
.cell.n2 { color: #57d98a; }
.cell.n3 { color: #ff7070; }
.cell.n4 { color: #b48cff; }
.cell.n5 { color: #ffb454; }
.cell.n6 { color: #4fd7d0; }
.cell.n7 { color: #ff8fd6; }
.cell.n8 { color: #cfd8ea; }

/* 旗・? */
.cell.flag::after {
  content: "🚩";
  font-size: calc(var(--cell) * 0.55);
  animation: plant 0.2s ease-out;
}
.cell.question::after {
  content: "?";
  color: var(--accent2);
  font-size: calc(var(--cell) * 0.6);
  font-weight: 800;
}
@keyframes plant {
  0% { transform: scale(0.3) translateY(4px); }
  60% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

/* 地雷 */
.cell.mine {
  background: #46283a;
}
.cell.mine::after {
  content: "💣";
  font-size: calc(var(--cell) * 0.55);
}
.cell.mine.boom {
  background: radial-gradient(circle, #ff8a5c 0%, #e04444 60%, #8f1f1f 100%);
  animation: boom 0.45s ease-out;
}
@keyframes boom {
  0% { transform: scale(0.4); box-shadow: 0 0 0 rgba(255, 120, 60, 0.9); }
  50% { transform: scale(1.35); box-shadow: 0 0 30px rgba(255, 120, 60, 0.9); }
  100% { transform: scale(1); box-shadow: 0 0 8px rgba(255, 120, 60, 0.5); }
}

/* 間違った旗 */
.cell.wrong {
  background: #46283a;
}
.cell.wrong::after {
  content: "❌";
  font-size: calc(var(--cell) * 0.5);
}

/* クリア時の地雷（花に変える） */
.cell.cleared-mine::after {
  content: "🌸";
  font-size: calc(var(--cell) * 0.55);
  animation: plant 0.35s ease-out;
}

/* コード対象のハイライト */
.cell.press {
  filter: brightness(1.35);
}

/* 盤面シェイク（ゲームオーバー） */
#board-scroll.shake { animation: shake 0.5s; }
@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15% { transform: translate(-6px, 2px); }
  30% { transform: translate(6px, -2px); }
  45% { transform: translate(-5px, -2px); }
  60% { transform: translate(5px, 2px); }
  75% { transform: translate(-3px, 1px); }
  90% { transform: translate(3px, -1px); }
}

/* ---------- ツールバー ---------- */
#toolbar {
  width: 100%;
  max-width: 560px;
  display: flex;
  gap: 10px;
  padding: 4px 14px 10px;
}

.tool-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 8px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.tool-btn:active { transform: scale(0.96); }
.tool-icon { font-size: 17px; }

#btn-mode[aria-pressed="true"] {
  background: rgba(255, 93, 93, 0.18);
  border-color: rgba(255, 93, 93, 0.5);
  color: #ffb3b3;
}

/* ---------- 結果オーバーレイ ---------- */
#result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 12, 24, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 50;
  animation: fadein 0.25s ease-out;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

#result-box {
  width: min(92vw, 380px);
  background: linear-gradient(180deg, #223154, #16203a);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 26px 22px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  animation: rise 0.3s ease-out;
}
@keyframes rise {
  from { transform: translateY(24px) scale(0.95); opacity: 0; }
  to { transform: none; opacity: 1; }
}

#result-title {
  font-size: 30px;
  font-weight: 900;
  letter-spacing: 0.06em;
}
#result-title.win {
  background: linear-gradient(180deg, #ffe9a8, #ffb648);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
#result-title.lose { color: var(--danger); }

#result-sub {
  margin-top: 8px;
  color: var(--accent2);
  font-weight: 800;
  font-size: 14px;
  animation: blink 1s infinite;
}
@keyframes blink { 50% { opacity: 0.4; } }

#result-time-row { margin: 14px 0 4px; }
#result-time {
  font-size: 46px;
  font-weight: 900;
  font-family: "SF Mono", "Courier New", monospace;
}
.result-unit { font-size: 16px; color: var(--text-dim); margin-left: 2px; }

#result-stats {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 14px 0 20px;
}
.rstat { display: flex; flex-direction: column; gap: 3px; }
.rstat-label { font-size: 11px; color: var(--text-dim); font-weight: 700; }
.rstat span:last-child { font-size: 16px; font-weight: 800; }

#result-buttons { display: flex; flex-direction: column; gap: 10px; }

/* ---------- ボタン共通 ---------- */
.btn {
  padding: 13px 18px;
  border-radius: 12px;
  border: none;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: linear-gradient(180deg, #5db1ff, #2f7fe0);
  color: #fff;
  box-shadow: 0 6px 18px rgba(77, 163, 255, 0.35);
}

.btn-sub {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--text);
}

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(8, 12, 24, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 60;
  animation: fadein 0.2s ease-out;
}

.modal-box {
  width: min(92vw, 400px);
  max-height: 86dvh;
  overflow-y: auto;
  background: linear-gradient(180deg, #223154, #16203a);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 22px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal-title {
  font-size: 20px;
  font-weight: 900;
  text-align: center;
  margin-bottom: 16px;
}

.menu-section { margin-bottom: 18px; }

.menu-caption {
  font-size: 12px;
  font-weight: 800;
  color: var(--text-dim);
  letter-spacing: 0.08em;
  margin-bottom: 8px;
}

.diff-buttons { display: flex; gap: 8px; }

.diff-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 10px 4px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
}
.diff-btn small { font-size: 10px; font-weight: 600; color: var(--text-dim); }
.diff-btn.active {
  border-color: var(--accent);
  background: rgba(77, 163, 255, 0.15);
  color: var(--accent);
}
.diff-btn.active small { color: var(--accent); }

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

.toggle {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.1);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
}
.toggle .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.toggle.on { background: var(--accent); border-color: var(--accent); }
.toggle.on .knob { transform: translateX(20px); }

#best-list { display: flex; flex-direction: column; }
.best-row {
  display: flex;
  justify-content: space-between;
  padding: 7px 2px;
  font-size: 14px;
  font-weight: 600;
  border-bottom: 1px dashed rgba(255, 255, 255, 0.08);
}
.best-row span:last-child { font-weight: 800; color: var(--accent2); }

.howto {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 13px;
  line-height: 1.55;
  color: #c6d2ea;
}
.howto li { padding-left: 1.1em; position: relative; }
.howto li::before { content: "•"; position: absolute; left: 0; color: var(--accent); }

#btn-close-menu { width: 100%; }

/* ---------- トースト ---------- */
#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(90px + env(safe-area-inset-bottom) + var(--ad-space, 0px));
  transform: translateX(-50%);
  background: rgba(20, 30, 55, 0.95);
  border: 1px solid var(--panel-border);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 70;
  white-space: nowrap;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
  animation: toastin 0.25s ease-out;
}
@keyframes toastin {
  from { transform: translate(-50%, 10px); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}

/* ---------- 紙吹雪 ---------- */
.confetti {
  position: fixed;
  top: -12px;
  width: 9px;
  height: 14px;
  z-index: 55;
  pointer-events: none;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to { transform: translateY(105dvh) rotate(720deg); opacity: 0.7; }
}

/* ---------- フッター ---------- */
#footer {
  padding: 8px 0 14px;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.7;
}
#footer a { color: var(--text-dim); }

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

/* ---------- レスポンシブ ---------- */
@media (min-width: 700px) {
  #logo { font-size: 22px; }
  #status-bar { padding-top: 10px; }
}

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

.sgr-overlay {
  position: fixed;
  inset: 0;
  z-index: 2147483000; /* 既存モーダル(60)・トースト(70)より上 */
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  background: rgba(8, 12, 24, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.sgr-panel {
  width: min(420px, 100%);
  max-height: min(80vh, 640px);
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(180deg, #223154, #16203a);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 20px 18px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

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

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

.sgr-tab {
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 14px;
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}

.sgr-tab-active {
  background: rgba(77, 163, 255, 0.18);
  border-color: var(--accent);
  color: var(--accent);
}

.sgr-register-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--accent2);
  text-align: center;
}

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

.sgr-name {
  flex: 1;
  min-width: 0;
  font-family: inherit;
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.35);
  color: var(--text);
}

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

.sgr-submit {
  border: none;
  border-radius: 10px;
  padding: 8px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #5db1ff, #2f7fe0);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 4px 14px rgba(77, 163, 255, 0.35);
}

.sgr-submit:hover {
  filter: brightness(1.08);
}

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

.sgr-error {
  margin: 4px 0 0;
  font-size: 12px;
  font-weight: 700;
  color: var(--danger);
}

.sgr-status {
  margin: 0;
  font-size: 13px;
  color: var(--text-dim);
  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: 8px;
  font-size: 14px;
}

.sgr-row:nth-child(even) {
  background: rgba(255, 255, 255, 0.05);
}

.sgr-row.sgr-row-me {
  background: rgba(77, 163, 255, 0.25);
  color: #fff;
}

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


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

.sgr-row-score {
  font-weight: 800;
  color: var(--accent2);
  flex-shrink: 0;
}

.sgr-row.sgr-row-me .sgr-row-score {
  color: #fff;
}

.sgr-close {
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 800;
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.sgr-close:active { transform: scale(0.97); }

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

.sgr-cta-msg {
  margin: 0 0 6px;
  font-size: 14px;
  font-weight: 800;
  color: var(--accent2);
}

.sgr-cta-btn {
  border: none;
  border-radius: 12px;
  padding: 10px 16px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  background: linear-gradient(180deg, #ffe08a, #ffb648);
  color: #4a3200;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(255, 209, 102, 0.35);
}
.sgr-cta-btn:active { transform: scale(0.97); }

.sgr-cta-link {
  border: none;
  background: none;
  padding: 6px;
  font-family: inherit;
  font-size: 13px;
  color: var(--text-dim);
  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; }
}
