/* ============================================================
   ブロック脱出パズル - Block Escape  style.css
   ============================================================ */

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

:root {
  --bg1: #0b1226;
  --bg2: #1b2a52;
  --panel: rgba(255, 255, 255, 0.07);
  --panel-border: rgba(255, 255, 255, 0.15);
  --text: #eaf0ff;
  --text-dim: #93a1c9;
  --accent: #4fc3ff;
  --accent2: #ffd166;
  --danger: #ff6b6b;
  --radius: 14px;
}

html, body { height: 100%; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif;
  background: radial-gradient(130% 95% at 50% 0%, var(--bg2) 0%, var(--bg1) 72%);
  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: 18px;
  font-weight: 800;
  letter-spacing: 0.02em;
  background: linear-gradient(180deg, #fff, #a6cdff);
  -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: 700;
  color: var(--accent2);
  background: rgba(255, 209, 102, 0.12);
  border: 1px solid rgba(255, 209, 102, 0.35);
  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;
  align-items: stretch;
  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.06em;
}

.status-value {
  font-family: "SF Mono", "Courier New", monospace;
  font-size: 19px;
  font-weight: 700;
  color: #d5e6ff;
  min-width: 1.4ch;
  text-align: center;
  line-height: 1;
}
.status-value.over { color: var(--accent2); }
.status-value.bump { animation: bump 0.25s ease-out; }
@keyframes bump {
  0% { transform: scale(1); }
  45% { transform: scale(1.28); }
  100% { transform: scale(1); }
}

/* ---------- 盤面 ---------- */
#board-wrap {
  position: relative;
  width: 100%;
  max-width: 640px;
  flex: 1;
  min-height: 0;
  padding: 4px 8px;
}

#board {
  position: absolute;
  inset: 4px 8px;
  width: calc(100% - 16px);
  height: calc(100% - 8px);
  touch-action: none;
  cursor: pointer;
  outline: none;
}
#board:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 8px; }
#board.no-focus-ring:focus-visible { outline: none; }

/* ---------- 操作バー ---------- */
#control-bar {
  width: 100%;
  max-width: 460px;
  display: flex;
  gap: 8px;
  padding: 4px 14px 2px;
}

.ctrl-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 6px;
  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;
}
.ctrl-btn:active { transform: scale(0.96); }
.ctrl-btn:disabled { opacity: 0.38; cursor: default; }
.ctrl-btn:disabled:active { transform: none; }
.ctrl-ico { font-size: 15px; line-height: 1; }

/* ---------- 結果オーバーレイ ---------- */
#result-overlay {
  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: 50;
  animation: fadein 0.25s ease-out;
}
@keyframes fadein { from { opacity: 0; } to { opacity: 1; } }

#result-box {
  width: min(92vw, 360px);
  background: linear-gradient(180deg, #24356b, #131c3d);
  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.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: 38px;
  line-height: 1;
  color: rgba(255, 255, 255, 0.16);
  transform: scale(0.7);
}
.rstar.on {
  color: #ffcf4d;
  text-shadow: 0 0 18px rgba(255, 200, 60, 0.65);
  animation: starpop 0.45s cubic-bezier(0.2, 1.5, 0.4, 1) forwards;
}
@keyframes starpop {
  0% { transform: scale(0.2) rotate(-40deg); opacity: 0; }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}
.rstar.on.d1 { animation-delay: 0.12s; }
.rstar.on.d2 { animation-delay: 0.24s; }

#result-title {
  font-size: 26px;
  font-weight: 900;
  letter-spacing: 0.05em;
  background: linear-gradient(180deg, #ffeeb5, #ffb648);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#result-sub {
  margin-top: 8px;
  color: #cfe0ff;
  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;
}

#result-buttons {
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin-top: 20px;
}

/* ---------- ボタン共通 ---------- */
.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, #4fc3ff, #2b7fe0);
  color: #fff;
  box-shadow: 0 6px 18px rgba(70, 165, 255, 0.35);
}

.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;
  animation: fadein 0.2s ease-out;
}

.modal-box {
  width: min(92vw, 400px);
  max-height: 86vh;   /* dvh 未対応ブラウザ向けフォールバック */
  max-height: 86dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  background: linear-gradient(180deg, #24356b, #131c3d);
  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(--accent); border-color: var(--accent); }
.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(--accent2); }

#btn-levelselect, #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.6;
  color: #cdd8f5;
}
.howto li { padding-left: 1.1em; position: relative; }
.howto li::before { content: "•"; position: absolute; left: 0; color: var(--accent); }

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

/* ---------- レベル選択 ---------- */
#level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(58px, 1fr));
  gap: 7px;
  max-height: 46vh;
  max-height: 46dvh;
  overflow-y: auto;
  padding: 2px 2px 12px;
  margin-bottom: 14px;
}

.lv-cell {
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.06);
  border-radius: 10px;
  color: var(--text);
  font-family: inherit;
  padding: 7px 2px 5px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.lv-cell:active { transform: scale(0.94); }
.lv-cell.current { border-color: var(--accent); background: rgba(79, 195, 255, 0.16); }
.lv-cell.next { border-style: dashed; border-color: rgba(255, 209, 102, 0.5); }
.lv-num { font-size: 14px; font-weight: 800; line-height: 1; }
.lv-stars { font-size: 9px; line-height: 1; color: #ffcf4d; letter-spacing: 1px; min-height: 9px; }
.lv-stars.none { color: rgba(255, 255, 255, 0.2); }

/* ---------- トースト ---------- */
#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(90px + env(safe-area-inset-bottom) + var(--ad-space, 0px));
  transform: translateX(-50%);
  background: rgba(20, 28, 58, 0.96);
  border: 1px solid var(--panel-border);
  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;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  animation: toastin 0.25s ease-out;
}
html.has-ad-banner #toast { bottom: calc(90px + var(--ad-space, 0px)); }
@keyframes toastin {
  from { transform: translate(-50%, 10px); 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 10px;
  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: 8px; }
  .ctrl-btn { font-size: 14px; }
}
@media (max-width: 360px) {
  #logo { font-size: 15px; }
  .ctrl-label { display: none; }
}
/* 横向きのスマホなど高さの低い画面：縦に積むと盤面の高さが残らない（740x360 では 0px）ので、
   盤面を左、見出し・ステータス・操作バー・フッターを右の列に並べる */
@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 minmax(0, 1fr) auto;
    grid-template-areas:
      "board top"
      "board status"
      "board ctrl"
      "board ."
      "board footer";
    align-items: stretch;
    justify-items: center;
  }
  #board-wrap { grid-area: board; }
  #topbar { grid-area: top; padding: 6px 8px 2px; }
  #logo { font-size: 18px; }
  #status-bar { grid-area: status; padding: 4px 8px; }
  #control-bar { grid-area: ctrl; padding: 4px 8px; }
  #footer { grid-area: footer; padding: 2px 0 4px; }
}
@media (prefers-reduced-motion: reduce) {
  .rstar.on, .confetti, .status-value.bump { 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; }
}
