/* ============================================================
   すなバケツ - Sand Bucket  style.css
   ============================================================ */

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

:root {
  --bg1: #0a1024;
  --bg2: #22305e;
  --panel: rgba(255, 255, 255, 0.07);
  --panel-border: rgba(255, 255, 255, 0.15);
  --text: #f1f3ff;
  --text-dim: #9aa6cc;
  --sand: #ffcf6b;
  --sand2: #ff9f43;
  --accent: #5cc8ff;
  --radius: 14px;
}

html, body { height: 100%; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Sans", sans-serif;
  background:
    radial-gradient(120% 70% at 50% 0%, rgba(255, 190, 90, 0.10) 0%, transparent 60%),
    radial-gradient(130% 95% at 50% 0%, var(--bg2) 0%, var(--bg1) 74%);
  background-color: var(--bg1);
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

#app {
  height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));   /* dvh 未対応ブラウザ向けフォールバック */
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 上のセーフエリアは共通ヘッダー（--sgh-top）が取っているので、ここでは取らない */
  padding: 0 env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
/* 広告があるときは下のセーフエリアも --ad-space に入っている（二重に空けない） */
html.has-ad-banner #app { padding-bottom: 0; }

/* ---------- ヘッダー ---------- */
#topbar {
  width: 100%;
  max-width: 640px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 14px 2px;
}
#logo-wrap { display: flex; align-items: center; gap: 9px; min-width: 0; }
#logo {
  font-size: 19px;
  font-weight: 900;
  letter-spacing: 0.04em;
  background: linear-gradient(180deg, #fff3cf, var(--sand) 55%, var(--sand2));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  white-space: nowrap;
}
#level-badge {
  flex-shrink: 0;
  font-size: 12px;
  font-weight: 800;
  color: var(--sand);
  background: rgba(255, 207, 107, 0.12);
  border: 1px solid rgba(255, 207, 107, 0.38);
  border-radius: 999px;
  padding: 3px 9px;
  white-space: nowrap;
}
#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: 460px;
  display: flex;
  justify-content: center;
  gap: 8px;
  padding: 6px 14px;
}
.status-box {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 7px 4px;
}
.status-cap { font-size: 11px; font-weight: 700; color: var(--text-dim); letter-spacing: 0.04em; white-space: nowrap; }
.status-value {
  font-family: "SF Mono", ui-monospace, "Courier New", monospace;
  font-size: 19px;
  font-weight: 700;
  color: #e3ebff;
  min-width: 1.4ch;
  text-align: center;
  line-height: 1;
}
.status-value.over { color: var(--sand2); }
.status-value.near { color: #7dffa8; }

/* ---------- 説明（新しいしかけ） ---------- */
#tip {
  width: calc(100% - 28px);
  max-width: 460px;
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 2px 0 2px;
  padding: 7px 8px 7px 12px;
  border-radius: 12px;
  background: rgba(255, 207, 107, 0.12);
  border: 1px solid rgba(255, 207, 107, 0.4);
  color: #ffe9b8;
  font-size: 12.5px;
  font-weight: 700;
  line-height: 1.5;
  animation: tipin 0.3s ease-out;
}
@keyframes tipin { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.tip-ico { flex-shrink: 0; }
#tip-text { flex: 1; }
#tip-close {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: #ffe9b8;
  font-size: 16px;
  font-family: inherit;
  cursor: pointer;
}

/* ---------- 盤面 ---------- */
#board-wrap {
  width: 100%;
  max-width: 640px;
  flex: 1;
  min-height: 0;
  padding: 3px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
#board {
  display: block;
  touch-action: none;
  cursor: pointer;
  outline: none;
  border-radius: 14px;
  box-shadow: 0 14px 40px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(255, 255, 255, 0.04);
}
#board:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- 操作バー ---------- */
#control-bar {
  width: 100%;
  max-width: 460px;
  display: flex;
  gap: 7px;
  padding: 4px 14px 2px;
}
.ctrl-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 10px 4px;
  border-radius: 12px;
  border: 1px solid var(--panel-border);
  background: var(--panel);
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  white-space: nowrap;
}
.ctrl-btn:active { transform: scale(0.96); }
.ctrl-btn:disabled { opacity: 0.38; cursor: default; }
.ctrl-btn:disabled:active { transform: none; }
.ctrl-btn.busy { animation: busy 0.8s ease-in-out infinite alternate; }
@keyframes busy { from { background: var(--panel); } to { background: rgba(255, 207, 107, 0.22); } }
.ctrl-ico { font-size: 15px; line-height: 1; }

/* ---------- 結果 ---------- */
#result-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 10, 26, 0.72);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 50;
  padding-bottom: var(--ad-space, 0px);
  animation: fadein 0.25s ease-out;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }
#result-box {
  width: min(92vw, 360px);
  background: linear-gradient(180deg, #2b3a72, #141c3e);
  border: 1px solid rgba(255, 207, 107, 0.3);
  border-radius: 20px;
  padding: 24px 22px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
  animation: rise 0.3s ease-out;
}
@keyframes rise { from { transform: translateY(24px) scale(0.95); opacity: 0; } to { transform: none; opacity: 1; } }
#result-stars { display: flex; justify-content: center; gap: 8px; margin-bottom: 12px; }
.rstar { font-size: 40px; line-height: 1; color: rgba(255, 255, 255, 0.16); transform: scale(0.7); }
.rstar.on {
  color: #ffd25a;
  text-shadow: 0 0 18px rgba(255, 200, 60, 0.65);
  animation: starpop 0.45s cubic-bezier(0.2, 1.5, 0.4, 1) both;
}
@keyframes starpop { 0% { transform: scale(0.2) rotate(-40deg); opacity: 0; } 100% { transform: scale(1) rotate(0); opacity: 1; } }
#result-title {
  font-size: 26px;
  font-weight: 900;
  letter-spacing: 0.05em;
  background: linear-gradient(180deg, #fff1c2, var(--sand) 50%, var(--sand2));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
#result-sub { margin-top: 8px; color: #dbe6ff; font-weight: 800; font-size: 15px; }
#result-detail { margin-top: 6px; color: var(--text-dim); font-weight: 700; font-size: 12.5px; line-height: 1.7; min-height: 1.7em; }
#result-buttons { display: flex; flex-direction: column; gap: 9px; margin-top: 18px; }

/* ---------- ボタン ---------- */
.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, #ffd27a, #f59a2f);
  color: #3a1f00;
  box-shadow: 0 6px 18px rgba(255, 170, 60, 0.32);
}
.btn-sub { background: var(--panel); border: 1px solid var(--panel-border); color: var(--text); }
.btn-sub.danger { background: rgba(255, 107, 107, 0.16); border-color: rgba(255, 107, 107, 0.48); color: #ffb3b3; }
.btn-sub.mini { padding: 8px 14px; font-size: 13px; }

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(6, 10, 26, 0.74);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  display: grid;
  place-items: center;
  z-index: 60;
  padding-bottom: var(--ad-space, 0px);
  animation: fadein 0.2s ease-out;
}
.modal-box {
  width: min(92vw, 420px);
  max-height: 84vh;   /* dvh 未対応ブラウザ向けフォールバック */
  max-height: calc(84dvh - var(--ad-space, 0px));
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  background: linear-gradient(180deg, #2b3a72, #141c3e);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 22px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.55);
}
.modal-title { font-size: 20px; font-weight: 900; text-align: center; margin-bottom: 14px; }
.modal-note { font-size: 12px; color: var(--text-dim); text-align: center; margin-bottom: 12px; line-height: 1.6; }
.menu-section { margin-bottom: 18px; }
.menu-caption { font-size: 12px; font-weight: 800; color: var(--text-dim); letter-spacing: 0.08em; margin-bottom: 8px; }
.menu-row { display: flex; align-items: center; justify-content: space-between; padding: 8px 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;
  flex-shrink: 0;
}
.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(--sand2); border-color: var(--sand2); }
.toggle.on .knob { transform: translateX(20px); }
.record-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); }
.record-row span:last-child { font-weight: 800; color: var(--sand); }
#btn-menu-levels, #btn-reset { width: 100%; margin-top: 12px; font-size: 13px; padding: 11px 14px; }
.confirm-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; font-size: 13px; font-weight: 700; color: #ffc4c4; }
.howto { list-style: none; display: flex; flex-direction: column; gap: 8px; font-size: 13px; line-height: 1.65; color: #d4ddf7; margin-bottom: 16px; }
.howto li { padding-left: 1.1em; position: relative; }
.howto li::before { content: "•"; position: absolute; left: 0; color: var(--sand); }
.howto b { color: #fff; }
#btn-close-menu, #btn-close-levels, #btn-close-howto { width: 100%; }

/* ---------- ステージ選択 ---------- */
#level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(56px, 1fr));
  gap: 7px;
  max-height: 50vh;
  max-height: 50dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 2px 2px 12px;
  margin-bottom: 14px;
}
.lv-btn {
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  padding: 8px 2px 6px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.lv-btn:active { transform: scale(0.94); }
.lv-btn.current { border-color: var(--sand); background: rgba(255, 207, 107, 0.16); }
.lv-btn.locked { opacity: 0.4; cursor: default; }
.lv-num { font-size: 14px; font-weight: 800; line-height: 1; }
.lv-stars { font-size: 9px; line-height: 1; color: #ffd25a; letter-spacing: 1px; min-height: 9px; }

/* ---------- トースト ---------- */
#toast {
  position: fixed;
  left: 50%;
  /* 操作の対象（下の盤面）を隠さないよう、水槽の上のほうに出す */
  top: calc(var(--sgh-top, 0px) + 104px);
  transform: translateX(-50%);
  background: rgba(22, 30, 62, 0.96);
  border: 1px solid rgba(255, 207, 107, 0.35);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 999px;
  z-index: 70;
  width: max-content;
  max-width: 92vw;
  text-align: center;
  line-height: 1.5;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  pointer-events: none;
  animation: toastin 0.25s ease-out;
}
@keyframes toastin { from { transform: translate(-50%, -8px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* ---------- 紙吹雪 ---------- */
.confetti {
  position: fixed;
  top: -14px;
  width: 9px;
  height: 14px;
  z-index: 55;
  pointer-events: none;
  animation: confetti-fall linear forwards;
}
@keyframes confetti-fall {
  to {
    transform: translateY(105vh) rotate(720deg);   /* dvh 未対応ブラウザ向けフォールバック */
    transform: translateY(105dvh) rotate(720deg);
    opacity: 0.7;
  }
}

/* ---------- フッター ---------- */
#footer { padding: 4px 0 8px; 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; }
  .ctrl-btn { font-size: 14px; }
}
@media (max-width: 360px) {
  #logo { font-size: 16px; }
  .ctrl-label { display: none; }
  .status-cap { font-size: 10px; }
}
/* 横向きのスマホなど高さの低い画面: 盤面を左、見出し・ステータス・操作バーを右に並べる */
@media (orientation: landscape) and (max-height: 500px) {
  #app {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto auto auto auto minmax(0, 1fr) auto;
    grid-template-areas:
      "board top"
      "board status"
      "board tip"
      "board ctrl"
      "board ."
      "board footer";
    justify-items: center;
  }
  #board-wrap { grid-area: board; height: 100%; }
  #topbar { grid-area: top; padding: 6px 8px 2px; }
  #status-bar { grid-area: status; padding: 4px 8px; }
  #tip { grid-area: tip; }
  #control-bar { grid-area: ctrl; padding: 4px 8px; }
  #footer { grid-area: footer; padding: 2px 0 4px; }
}
@media (prefers-reduced-motion: reduce) {
  .rstar.on, .confetti, #tip, .ctrl-btn.busy { animation: none; }
  .rstar.on { transform: scale(1); opacity: 1; }
}

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