/* ============================================================
   ナンバーボックス - Number Box  カラフル文具×パズルのスタイル
   ============================================================ */

:root {
  --paper: #eef3f8;
  --panel: #ffffff;
  --ink: #243447;
  --ink-soft: #64748b;
  --line-pale: #d7e0ea;
  --accent: #3a7bd5;
  --accent-deep: #2b5fa8;
  --pop: #ff8b3d;
  --good: #17a06a;
  --bad: #e5484d;
  --gold: #e8960c;
  --radius: 14px;
  --sans: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", sans-serif;
}

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

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

body {
  font-family: var(--sans);
  color: var(--ink);
  background:
    radial-gradient(ellipse at 15% 8%, rgba(58, 123, 213, .10), transparent 50%),
    radial-gradient(ellipse at 88% 92%, rgba(255, 139, 61, .10), transparent 50%),
    radial-gradient(ellipse at 75% 20%, rgba(23, 160, 106, .06), transparent 45%),
    var(--paper);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#app {
  height: 100%;
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  display: flex;
  flex-direction: column;
  max-width: 640px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  /* 上のセーフエリアは共通ヘッダー（--sgh-top）が取っているので二重に空けない */
  padding-top: max(0px, env(safe-area-inset-top, 0px) - var(--sgh-top, 0px));
}

/* 広告があるときは --ad-space が下のセーフエリアを含むので二重に空けない */
html.has-ad-banner #app { padding-bottom: 0; }

/* ---------- ヘッダー ---------- */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px 6px;
}

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

#logo {
  font-size: 21px;
  font-weight: 800;
  letter-spacing: .04em;
  line-height: 1;
  white-space: nowrap;
}

#logo::before {
  content: "";
  display: inline-block;
  width: 15px;
  height: 15px;
  margin-right: 7px;
  border-radius: 4px;
  background:
    conic-gradient(from 0deg, #ffb03a 0 25%, #3a7bd5 0 50%, #17a06a 0 75%, #e5484d 0 100%);
  box-shadow: 0 0 0 2px var(--ink) inset;
}

#stage-badge {
  font-size: 14px;
  font-weight: 700;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
  box-shadow: 0 2px 0 var(--accent-deep);
}

#stage-badge:active { transform: translateY(1px); box-shadow: none; }

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

.icon-btn {
  width: 40px;
  height: 40px;
  font-size: 19px;
  line-height: 1;
  background: rgba(255, 255, 255, .75);
  border: 1px solid rgba(36, 52, 71, .14);
  border-radius: 12px;
  cursor: pointer;
  color: var(--ink);
}

.icon-btn:active { transform: scale(.94); }

/* ---------- ステータスバー ---------- */
#status-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 18px 4px;
}

#status-level {
  font-size: 15px;
  font-weight: 800;
  white-space: nowrap;
}

#status-count {
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  color: var(--ink-soft);
  white-space: nowrap;
  min-width: 48px;
  text-align: right;
}

#progress-track {
  flex: 1;
  height: 8px;
  border-radius: 999px;
  background: rgba(36, 52, 71, .12);
  overflow: hidden;
}

#progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent), #6db3f2);
  transition: width .18s ease;
}

/* ---------- 盤面 ---------- */
#board-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
}

/* サイズはJSがインラインで指定する。絶対配置にして
   canvas が親のレイアウト幅を固定してしまわないようにする */
#board {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: block;
  touch-action: none;
  outline: none;
  border-radius: var(--radius);
  box-shadow: 0 8px 26px rgba(36, 52, 71, .14);
}

#board:focus-visible {
  box-shadow: 0 8px 26px rgba(36, 52, 71, .14), 0 0 0 3px rgba(58, 123, 213, .5);
}

/* ---------- ツールバー ---------- */
#toolbar {
  display: flex;
  gap: 10px;
  padding: 6px 14px 10px;
}

.tool-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  font-size: 15px;
  font-weight: 700;
  font-family: var(--sans);
  color: var(--ink);
  background: rgba(255, 255, 255, .8);
  border: 1px solid rgba(36, 52, 71, .16);
  border-radius: 12px;
  padding: 11px 6px;
  white-space: nowrap;
  cursor: pointer;
  box-shadow: 0 2px 0 rgba(36, 52, 71, .10);
}

.tool-btn:active { transform: translateY(1px); box-shadow: none; }

.tool-btn:disabled {
  opacity: .4;
  pointer-events: none;
}

.tool-icon { font-size: 16px; }

/* ---------- 汎用ボタン ---------- */
.btn {
  font-family: var(--sans);
  font-size: 16px;
  font-weight: 700;
  border: none;
  border-radius: 12px;
  padding: 13px 18px;
  cursor: pointer;
  width: 100%;
}

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

.btn-primary {
  color: #fff;
  background: linear-gradient(180deg, #4f8de0, var(--accent-deep));
  box-shadow: 0 3px 0 #1e4275;
}

.btn-sub {
  color: var(--ink);
  background: rgba(36, 52, 71, .08);
}

.btn-danger-sub {
  color: var(--bad);
  background: rgba(229, 72, 77, .10);
  font-size: 14px;
  padding: 10px 14px;
}

/* ---------- 結果オーバーレイ ---------- */
#result-overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  /* 共通ヘッダーと下部広告に隠れない範囲の中央に置く */
  padding: calc(var(--sgh-top, 0px) + 10px) 10px calc(var(--ad-space, 0px) + 10px);
  background: rgba(36, 52, 71, .45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  animation: fade-in .25s ease;
}

#result-box {
  position: relative;
  width: min(86vw, 340px);
  background: var(--panel);
  border-radius: 18px;
  padding: 30px 24px 24px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .35);
  animation: pop-in .35s cubic-bezier(.2, 1.6, .4, 1);
  max-height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
}

#result-stamp {
  width: 84px;
  height: 84px;
  margin: 0 auto 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 44px;
  border: 4px solid var(--gold);
  border-radius: 20px;
  transform: rotate(-8deg);
  animation: stamp-in .5s cubic-bezier(.2, 1.8, .4, 1) .15s backwards;
  background: rgba(232, 150, 12, .08);
}

#result-title {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: .02em;
}

#result-sub {
  font-size: 14px;
  color: var(--ink-soft);
  margin: 6px 0 18px;
}

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

/* 紙吹雪（CSSのみ） */
.confetti {
  position: absolute;
  top: -12px;
  width: 9px;
  height: 9px;
  border-radius: 2px;
  opacity: 0;
  animation: confetti-fall 2.4s ease-in infinite;
  pointer-events: none;
}

.confetti:nth-child(1) { left: 10%; background: #ffb03a; animation-delay: 0s; }
.confetti:nth-child(2) { left: 24%; background: #3a7bd5; animation-delay: .35s; }
.confetti:nth-child(3) { left: 38%; background: #17a06a; animation-delay: .7s; }
.confetti:nth-child(4) { left: 52%; background: #e5484d; animation-delay: .15s; }
.confetti:nth-child(5) { left: 66%; background: #a284dd; animation-delay: .55s; }
.confetti:nth-child(6) { left: 80%; background: #ffb03a; animation-delay: .9s; }
.confetti:nth-child(7) { left: 92%; background: #3a7bd5; animation-delay: .25s; }

@keyframes confetti-fall {
  0% { opacity: 0; transform: translateY(0) rotate(0deg); }
  10% { opacity: 1; }
  100% { opacity: .9; transform: translateY(240px) rotate(340deg); }
}

/* ---------- モーダル ---------- */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(36, 52, 71, .45);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  padding: 18px;
  /* 共通ヘッダーと下部広告に隠れない範囲の中央に置く */
  padding-top: calc(var(--sgh-top, 0px) + 18px);
  padding-bottom: calc(var(--ad-space, 0px) + 18px);
  animation: fade-in .2s ease;
}

.modal-box {
  width: min(92vw, 420px);
  max-height: min(84dvh, 640px);
  max-height: min(84dvh, 640px, 100%);
  overflow-y: auto;
  background: var(--panel);
  border-radius: 18px;
  padding: 22px 20px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, .35);
  animation: pop-in .3s cubic-bezier(.2, 1.5, .4, 1);
}

.modal-title {
  font-size: 19px;
  font-weight: 800;
  letter-spacing: .02em;
  text-align: center;
  margin-bottom: 16px;
}

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

.menu-caption {
  font-size: 12px;
  font-weight: 700;
  color: var(--ink-soft);
  letter-spacing: .04em;
  margin-bottom: 8px;
  border-left: 3px solid var(--accent);
  padding-left: 8px;
}

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

.howto {
  list-style: none;
  font-size: 13.5px;
  line-height: 1.75;
  color: #3a4757;
}

.howto li {
  padding-left: 16px;
  position: relative;
  margin-bottom: 5px;
}

.howto li::before {
  content: "•";
  position: absolute;
  left: 0;
  color: var(--accent);
}

/* トグルスイッチ */
.toggle {
  width: 48px;
  height: 28px;
  border-radius: 999px;
  border: none;
  background: rgba(36, 52, 71, .22);
  position: relative;
  cursor: pointer;
  transition: background .2s;
  flex-shrink: 0;
}

.toggle .knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: left .2s;
}

.toggle.on { background: var(--accent); }
.toggle.on .knob { left: 23px; }

/* ステージ選択 */
#level-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.lv-btn {
  aspect-ratio: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  font-family: var(--sans);
  font-size: 16px;
  font-weight: 700;
  color: var(--ink);
  background: rgba(255, 255, 255, .85);
  border: 1px solid rgba(36, 52, 71, .16);
  border-radius: 10px;
  cursor: pointer;
}

.lv-btn:active { transform: scale(.94); }

.lv-btn .lv-mark {
  font-size: 10px;
  line-height: 1;
  color: var(--accent);
  min-height: 10px;
}

.lv-btn.cleared {
  background: rgba(23, 160, 106, .10);
  border-color: rgba(23, 160, 106, .4);
}

.lv-btn.cleared .lv-mark { color: var(--good); }

.lv-btn.current { outline: 2px solid var(--accent); outline-offset: 1px; }

.lv-btn.locked {
  color: rgba(36, 52, 71, .3);
  background: rgba(36, 52, 71, .05);
  cursor: default;
  pointer-events: none;
}

/* ギミック紹介モーダル */
#modal-intro .modal-box { text-align: center; }

#intro-icon {
  width: 76px;
  height: 76px;
  margin: 2px auto 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 40px;
  background: rgba(58, 123, 213, .08);
  border: 2px solid rgba(58, 123, 213, .25);
  border-radius: 20px;
}

#intro-icon.intro-badge-black {
  background: #28324a;
  color: #e9eef5;
  border-color: #28324a;
}

#intro-body {
  font-size: 14.5px;
  line-height: 1.9;
  color: #3a4757;
  margin-bottom: 18px;
}

/* ---------- トースト ---------- */
#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(86px + env(safe-area-inset-bottom) + var(--ad-space, 0px));
  transform: translateX(-50%);
  z-index: 60;
  background: rgba(36, 52, 71, .93);
  color: #f2f6fa;
  font-size: 14px;
  font-weight: 700;
  padding: 10px 20px;
  border-radius: 999px;
  white-space: nowrap;
  pointer-events: none;
  animation: toast-in .25s ease;
}

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

#footer a { color: var(--ink-soft); }

/* ---------- ユーティリティ ---------- */
.hidden { display: none !important; }

@keyframes fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes pop-in {
  from { opacity: 0; transform: scale(.85); }
  to { opacity: 1; transform: scale(1); }
}

@keyframes stamp-in {
  from { opacity: 0; transform: rotate(-8deg) scale(2.2); }
  to { opacity: 1; transform: rotate(-8deg) scale(1); }
}

@keyframes toast-in {
  from { opacity: 0; transform: translate(-50%, 8px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* 幅の狭いスマホではヘッダーを詰める */
@media (max-width: 430px) {
  #logo { font-size: 17px; }
  #logo::before { width: 12px; height: 12px; margin-right: 5px; }
  #stage-badge { font-size: 12.5px; padding: 5px 10px; }
  .icon-btn { width: 36px; height: 36px; font-size: 17px; }
  #topbar { padding: 8px 10px 4px; }
}

/* 縦に短い画面（スマホ横向き等）では上下の余白を詰めて盤面の高さを確保する */
@media (max-height: 480px) {
  #topbar { padding-top: 4px; padding-bottom: 2px; }
  #status-bar { padding-top: 2px; padding-bottom: 2px; }
  #toolbar { padding-top: 4px; padding-bottom: 4px; }
  .tool-btn { padding-top: 7px; padding-bottom: 7px; }
  #footer { display: flex; flex-wrap: wrap; justify-content: center; column-gap: 1em; padding: 2px 0 4px; }
  /* 盤面が最小サイズでも入りきらないとき、ボタンが盤面に覆われないようにする */
  #topbar, #status-bar, #toolbar, #footer { position: relative; z-index: 1; }
}

/* 横長画面（PC等）ではやや広く */
@media (min-width: 900px) and (min-height: 700px) {
  #logo { font-size: 25px; }
  .tool-btn { font-size: 16px; }
}

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