/* ============================================================
   箱入り勇者 - Klotski  style.css
   ============================================================ */

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

:root {
  --bg1: #171126;
  --bg2: #2c2147;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-border: rgba(255, 255, 255, 0.14);
  --text: #f3edff;
  --text-dim: #a294c4;
  --accent: #ffb84d;
  --accent2: #ffd166;
  --danger: #ff5d5d;
  --stone: #4a4258;
  --stone-dark: #332c42;
  --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;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- ヘッダー ---------- */
#topbar {
  width: 100%;
  max-width: 560px;
  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, #ffd9a0);
  -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: 4px 10px;
  cursor: pointer;
  font-family: inherit;
}
#level-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); }
.icon-btn svg { width: 20px; height: 20px; }

/* インラインSVG定義の非表示化 */
#svg-defs { position: absolute; width: 0; height: 0; overflow: hidden; }

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

.status-box {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 6px 12px;
}
.status-label { font-size: 11px; color: var(--text-dim); font-weight: 700; }
.status-value {
  font-size: 17px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent2);
  min-width: 2ch;
  text-align: center;
}
#level-name {
  flex: 1;
  text-align: center;
  font-size: 14px;
  font-weight: 800;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ---------- 盤面（城） ---------- */
#board-wrap {
  flex: 1;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 8px 14px 4px;
}

#castle {
  position: relative;
  padding: 14px 14px 0;
  border-radius: 12px 12px 0 0;
  background:
    linear-gradient(rgba(255, 255, 255, 0.05), rgba(0, 0, 0, 0.18)),
    repeating-linear-gradient(0deg, var(--stone) 0 22px, var(--stone-dark) 22px 24px),
    repeating-linear-gradient(90deg, transparent 0 34px, var(--stone-dark) 34px 36px);
  background-blend-mode: normal, normal, normal;
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.5), inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

/* 城壁の凸凹（狭間） */
#castle::before {
  content: "";
  position: absolute;
  left: 6px;
  right: 6px;
  top: -10px;
  height: 10px;
  background: repeating-linear-gradient(90deg, var(--stone) 0 18px, transparent 18px 30px);
  border-radius: 3px 3px 0 0;
}

#board {
  position: relative;
  width: var(--board-w, 320px);
  height: var(--board-h, 400px);
  background:
    repeating-linear-gradient(0deg, transparent 0 calc(var(--cell, 80px) - 1px), rgba(255, 255, 255, 0.06) calc(var(--cell, 80px) - 1px) var(--cell, 80px)),
    repeating-linear-gradient(90deg, transparent 0 calc(var(--cell, 80px) - 1px), rgba(255, 255, 255, 0.06) calc(var(--cell, 80px) - 1px) var(--cell, 80px)),
    linear-gradient(180deg, #241b38, #1c1530);
  border-radius: 6px 6px 0 0;
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.55);
}

/* ゴール位置のヒント */
#goal-hint {
  position: absolute;
  border: 2px dashed rgba(255, 209, 102, 0.35);
  border-radius: 10px;
  pointer-events: none;
}

/* 城門（出口） */
#gate {
  position: relative;
  margin: 0 auto;
  width: calc(var(--cell, 80px) * 2);
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 800;
  color: var(--accent2);
  background: linear-gradient(180deg, #1c1530, #120d20);
  border-radius: 0 0 14px 14px;
  box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.5);
}
#gate-arrow { animation: gate-blink 1.6s ease-in-out infinite; }
@keyframes gate-blink {
  0%, 100% { opacity: 0.35; transform: translateY(-2px); }
  50% { opacity: 1; transform: translateY(2px); }
}

#tagline {
  margin-top: 14px;
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
}
.tag-art { width: 18px; height: 18px; vertical-align: -5px; margin: 0 3px; }

/* ---------- 駒 ---------- */
.piece {
  position: absolute;
  left: 0;
  top: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  border-radius: 10px;
  cursor: grab;
  touch-action: none;
  transition: transform 0.1s linear;
  will-change: transform;
  background: linear-gradient(180deg, #8a6a4a, #6a4c32);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.25), inset 0 -3px 0 rgba(0, 0, 0, 0.3), 0 3px 6px rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(0, 0, 0, 0.4);
}
.piece:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: -3px;
}
.piece.dragging { transition: transform 0.05s linear; cursor: grabbing; z-index: 10; }

.piece .p-art { display: block; pointer-events: none; }
.piece .p-name {
  font-size: 11px;
  font-weight: 800;
  color: rgba(255, 255, 255, 0.92);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  white-space: nowrap;
}

.piece.size-1x1 .p-art { width: calc(var(--cell, 80px) * 0.46); height: calc(var(--cell, 80px) * 0.46); }
.piece.size-1x1 .p-name { font-size: 10px; }
.piece.size-v .p-art, .piece.size-h .p-art { width: calc(var(--cell, 80px) * 0.52); height: calc(var(--cell, 80px) * 0.52); }
.piece.size-h { flex-direction: row; gap: 6px; }

.piece.p-soldier { background: linear-gradient(180deg, #5f7d99, #435c74); }
.piece.p-royal { background: linear-gradient(180deg, #7d5fa8, #5c4380); }
.piece.p-dragon { background: linear-gradient(180deg, #4e8a63, #356245); }

.piece.hero {
  background: linear-gradient(180deg, #d98f3e, #a8611f);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.35), inset 0 -4px 0 rgba(0, 0, 0, 0.3), 0 4px 10px rgba(255, 160, 60, 0.35);
  z-index: 2;
}
.piece.hero .p-art { width: calc(var(--cell, 80px) * 0.85); height: calc(var(--cell, 80px) * 0.85); }
.piece.hero .p-name { font-size: 13px; letter-spacing: 0.2em; }

.piece.hero.escaping {
  transition: transform 0.9s cubic-bezier(0.5, 0, 0.9, 0.6);
  z-index: 20;
}

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

.tool-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 11px 8px;
  font-size: 13px;
  font-weight: 700;
  font-family: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  cursor: pointer;
}
.tool-btn:active { transform: scale(0.96); }
.tool-btn:disabled { opacity: 0.4; cursor: default; }
.tool-btn:disabled:active { transform: none; }
.tool-icon { width: 16px; height: 16px; flex-shrink: 0; }

/* ---------- オーバーレイ・モーダル共通 ---------- */
.hidden { display: none !important; }

#result-overlay, .modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 6, 20, 0.72);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  padding: 20px;
}

#result-box, .modal-box {
  width: 100%;
  max-width: 360px;
  background: linear-gradient(180deg, #342a4e, #241c3a);
  border: 1px solid var(--panel-border);
  border-radius: 18px;
  padding: 22px 20px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
  animation: pop-in 0.28s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes pop-in {
  from { transform: scale(0.85); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-box {
  text-align: left;
  max-height: min(80dvh, 640px);
  overflow-y: auto;
}

#result-emoji { line-height: 0; }
#result-emoji svg { width: 84px; height: 84px; }
#result-title {
  font-size: 24px;
  font-weight: 900;
  margin-top: 4px;
  background: linear-gradient(180deg, #fff, #ffd166);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
#result-stars { font-size: 26px; letter-spacing: 0.2em; color: var(--accent2); margin-top: 2px; }
#result-stars .star-off { color: rgba(255, 255, 255, 0.18); }
#result-sub { margin-top: 6px; font-size: 13px; font-weight: 800; color: #7dffb0; }

#result-stats {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin: 16px 0 18px;
}
.rstat {
  flex: 1;
  background: rgba(0, 0, 0, 0.3);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 8px 4px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.rstat-label { font-size: 11px; color: var(--text-dim); font-weight: 700; }
.rstat span:last-child { font-size: 17px; font-weight: 800; color: var(--accent2); font-variant-numeric: tabular-nums; }

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

.btn {
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  padding: 12px 16px;
  border-radius: 12px;
  border: none;
  cursor: pointer;
}
.btn:active { transform: scale(0.97); }
.btn-primary {
  background: linear-gradient(180deg, #ffc46b, #f09c2e);
  color: #3d2504;
  box-shadow: 0 3px 10px rgba(240, 156, 46, 0.35);
}
.btn-sub {
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--panel-border);
}
.btn-danger {
  background: rgba(255, 93, 93, 0.14);
  color: var(--danger);
  border: 1px solid rgba(255, 93, 93, 0.4);
  font-size: 12px;
  padding: 7px 14px;
}

/* ---------- モーダル内容 ---------- */
.modal-title { font-size: 18px; font-weight: 900; text-align: center; margin-bottom: 14px; }

.menu-section { margin-bottom: 16px; }
.menu-caption {
  font-size: 12px;
  font-weight: 800;
  color: var(--accent2);
  margin-bottom: 7px;
  letter-spacing: 0.06em;
}
.story {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text);
  background: rgba(0, 0, 0, 0.25);
  border-radius: 10px;
  padding: 10px 12px;
}
.howto { padding-left: 18px; display: flex; flex-direction: column; gap: 6px; }
.howto li { font-size: 13px; line-height: 1.55; color: var(--text); }

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

.toggle {
  width: 46px;
  height: 26px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: rgba(0, 0, 0, 0.4);
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
}
.toggle .knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #cfc4e8;
  transition: transform 0.2s, background 0.2s;
}
.toggle.on { background: rgba(240, 156, 46, 0.5); }
.toggle.on .knob { transform: translateX(20px); background: #ffd166; }

/* レベル選択 */
#level-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.level-cell {
  font-family: inherit;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 9px 4px 7px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  color: var(--text);
  cursor: pointer;
}
.level-cell:active { transform: scale(0.95); }
.level-cell.current { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.level-cell.locked { opacity: 0.45; cursor: default; }
.level-cell.locked:active { transform: none; }
.level-num { font-size: 15px; font-weight: 900; color: var(--accent2); }
.level-cell.locked .level-num { color: var(--text-dim); }
.lock-art { width: 15px; height: 15px; display: block; margin: 3px auto 2px; opacity: 0.8; }
.level-label { font-size: 10px; color: var(--text-dim); white-space: nowrap; max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
.level-stars { font-size: 10px; color: var(--accent2); letter-spacing: 0.1em; }
.level-stars .star-off { color: rgba(255, 255, 255, 0.18); }

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

/* ---------- フッター ---------- */
#footer {
  width: 100%;
  text-align: center;
  font-size: 11px;
  color: var(--text-dim);
  padding: 10px 14px calc(12px + env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 3px;
}
#footer a { color: var(--text-dim); text-decoration: underline; }

/* ---------- PC ---------- */
@media (min-width: 700px) {
  #logo { font-size: 22px; }
  #tagline { font-size: 13px; }
}

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