/* ===== スパイダレット ===== */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  --felt-1: #1d5f8f;
  --felt-2: #0b3253;
  --bar-bg: #0e1d2b;
  --gold: #e8c56a;
  --gold-dim: #b99a4e;
  --ink: #1e2430;
  --red: #c1272d;
  --card-face: #fdfdf6;
  --cw: 60px;
  --ch: 87px;
}

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

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Sans", sans-serif;
  background: #082944;
  color: #fff;
  -webkit-tap-highlight-color: transparent;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

#app {
  display: flex;
  flex-direction: column;
  height: calc(100vh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
}

/* ===== ヘッダー ===== */
#topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  /* 上のセーフエリアは共通ヘッダー（--sgh-top）が取るので、ここでは足さない */
  padding: 6px 10px;
  background: linear-gradient(#16293c, #0e1d2b);
  border-bottom: 1px solid rgba(232, 197, 106, .25);
  box-shadow: 0 2px 8px rgba(0, 0, 0, .35);
  z-index: 30;
}

#logo-wrap {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  min-width: 0;
}

#logo {
  font-size: 16px;
  font-weight: 800;
  letter-spacing: .04em;
  color: var(--gold);
  text-shadow: 0 1px 2px rgba(0, 0, 0, .5);
  white-space: nowrap;
}

#diff-badge {
  font-size: 9px;
  font-weight: 700;
  color: #cfe0e8;
  background: rgba(255, 255, 255, .12);
  border-radius: 8px;
  padding: 1px 7px;
  letter-spacing: .06em;
}

#stats-bar {
  flex: 1 1 auto;
  display: flex;
  justify-content: center;
  gap: 14px;
  min-width: 0;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-width: 44px;
}

.stat-label {
  font-size: 9px;
  color: #9fb4bf;
  letter-spacing: .1em;
}

.stat-value {
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  color: #fff;
}

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

.icon-btn {
  width: 36px;
  height: 36px;
  border: 1px solid rgba(255, 255, 255, .18);
  border-radius: 10px;
  background: rgba(255, 255, 255, .07);
  color: #fff;
  font-size: 16px;
  cursor: pointer;
  transition: background .15s;
}

.icon-btn:active { background: rgba(255, 255, 255, .2); }

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

#board {
  position: absolute;
  inset: 0;
  /* 重ね合わせコンテキストを作り、カードの z-index（組札 45〜58・移動中 500 など）を盤面の中だけで比べる。
     無いとモーダル（50）より前にカードが浮いて見える */
  z-index: 0;
  overflow: hidden;
  touch-action: none;
  background:
    radial-gradient(ellipse 120% 90% at 50% 0%, rgba(255, 255, 255, .10), transparent 55%),
    radial-gradient(ellipse 140% 120% at 50% 100%, rgba(0, 0, 0, .35), transparent 60%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, .015) 0 2px, transparent 2px 6px),
    linear-gradient(var(--felt-1), var(--felt-2));
}

/* スロット（枠） */
.slot {
  position: absolute;
  width: var(--cw);
  height: var(--ch);
  border: 1.5px dashed rgba(255, 255, 255, .22);
  border-radius: calc(var(--cw) * .1);
  background: rgba(0, 0, 0, .08);
}

.slot.foundation-slot { border-color: rgba(232, 197, 106, .35); }
.slot.stock-slot { border-color: rgba(255, 255, 255, .3); }

#stock-count {
  position: absolute;
  font-size: 11px;
  font-weight: 700;
  color: #eaf2f6;
  text-shadow: 0 1px 2px rgba(0, 0, 0, .6);
  text-align: center;
  white-space: nowrap; /* カードが小さい画面（横向き等）で「残り3回」が2行に折れて山札に重ならないように */
  pointer-events: none;
  z-index: 25;
}

/* ===== カード ===== */
.card {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  will-change: transform;
  transition: transform .25s ease;
  cursor: pointer;
  perspective: 600px;
}

.card.no-anim { transition: none; }
.card.dragging { transition: none; z-index: 1000 !important; }
.card.moving { z-index: 500 !important; }

.card .in {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform .3s ease;
}

.card.down .in { transform: rotateY(180deg); }
.card.down { cursor: default; }

.card .front,
.card .back {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  border-radius: calc(var(--cw) * .1);
}

.card .front {
  background: linear-gradient(160deg, #ffffff, var(--card-face) 60%, #f1f0e4);
  border: 1px solid #c9c9b8;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
  color: var(--ink);
  overflow: hidden;
}

.card.red .front { color: var(--red); }

.card .back {
  transform: rotateY(180deg);
  background:
    radial-gradient(ellipse at 50% 40%, rgba(255, 255, 255, .16), transparent 65%),
    repeating-linear-gradient(45deg, rgba(255, 255, 255, .09) 0 3px, transparent 3px 7px),
    repeating-linear-gradient(-45deg, rgba(0, 0, 0, .10) 0 3px, transparent 3px 7px),
    linear-gradient(160deg, #a8433c, #6e2028);
  border: 1px solid #571a20;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .4);
}

.card .back::after {
  content: "";
  position: absolute;
  inset: 9%;
  border: 1.5px solid rgba(255, 255, 255, .35);
  border-radius: calc(var(--cw) * .06);
}

.cnr {
  position: absolute;
  top: 3%;
  left: 7%;
  line-height: 1.02;
  text-align: center;
  font-weight: 800;
}

.cnr b { display: block; font-size: calc(var(--cw) * .32); letter-spacing: -0.02em; }
.cnr i { display: block; font-style: normal; font-size: calc(var(--cw) * .28); }

/* 4スートは重なったカードでもスートを見分けられるよう、ランクとスートを横に並べる
   （縦並びだとスートが次のカードの下に隠れ、♠と♣・♥と♦の区別がつかない） */
#board[data-diff="4"] .cnr { display: flex; align-items: baseline; gap: calc(var(--cw) * .02); }

.pip {
  position: absolute;
  right: 5%;
  bottom: 2%;
  font-size: calc(var(--cw) * .62);
  line-height: 1;
  opacity: .92;
}

/* 選択・ヒント */
.card.sel .front {
  box-shadow: 0 0 0 2px #7ec8ff, 0 2px 8px rgba(0, 0, 0, .5);
}

.card.hint-glow .front,
.card.hint-glow .back {
  box-shadow: 0 0 0 2px var(--gold), 0 0 14px 3px rgba(232, 197, 106, .8);
  animation: hintPulse .6s ease-in-out 2;
}

.slot.hint-glow {
  border-color: var(--gold);
  box-shadow: 0 0 14px 3px rgba(232, 197, 106, .8);
  animation: hintPulse .6s ease-in-out 2;
}

@keyframes hintPulse {
  50% { filter: brightness(1.35); }
}

.card.shake { animation: shake .3s ease; }

@keyframes shake {
  25% { margin-left: -4px; }
  75% { margin-left: 4px; }
}

/* 完成演出 */
.card.celebrate .front { animation: celebrate .5s ease; }

@keyframes celebrate {
  40% { filter: brightness(1.6) saturate(1.4); }
}

/* ===== ツールバー ===== */
#toolbar {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  gap: 10px;
  padding: 7px 10px;
  background: linear-gradient(#0e1d2b, #16293c);
  border-top: 1px solid rgba(232, 197, 106, .25);
  z-index: 30;
}

.tool-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 64px;
  padding: 5px 10px;
  border: 1px solid rgba(255, 255, 255, .16);
  border-radius: 12px;
  background: rgba(255, 255, 255, .07);
  color: #e6eef2;
  font-size: 11px;
  font-weight: 700;
  cursor: pointer;
  transition: background .15s, transform .1s;
}

.tool-btn:active { background: rgba(255, 255, 255, .2); transform: scale(.96); }
.tool-btn:disabled { opacity: .4; cursor: default; }

.tool-icon { font-size: 17px; line-height: 1; }

/* ===== フッター ===== */
#footer {
  flex: 0 0 auto;
  padding: 5px 8px calc(5px + env(safe-area-inset-bottom));
  text-align: center;
  font-size: 10px;
  line-height: 1.6;
  color: #8699a7;
  background: #0a1620;
}

#footer a { color: #a8bfcb; }

/* 広告があるときは --ad-space が下のセーフエリアを含むので、フッターでは足さない */
html.has-ad-banner #footer { padding-bottom: 5px; }

/* ===== トースト ===== */
#toast {
  position: fixed;
  left: 50%;
  /* フッター固定バナー広告に隠れないよう持ち上げる（広告なしのときは 0px） */
  bottom: calc(120px + var(--ad-space, 0px));
  transform: translateX(-50%);
  background: rgba(10, 16, 20, .92);
  border: 1px solid rgba(232, 197, 106, .5);
  color: #fff;
  font-size: 13px;
  font-weight: 700;
  padding: 10px 18px;
  border-radius: 12px;
  z-index: 60;
  white-space: nowrap;
  transition: opacity .3s;
  pointer-events: none;
}

/* 汎用 .hidden の display:none を上書きし、opacity でフェードアウトさせる */
#toast.hidden { display: block; opacity: 0; }

/* ===== モーダル ===== */
.modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, .6);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 50;
  padding: 16px;
  transition: opacity .2s;
}

.modal.hidden { display: none; }

/* 勝利モーダルの表示中に共通ヘッダーから開いたメニューが、勝利モーダルの裏に隠れないようにする */
#modal-menu,
#modal-confirm { z-index: 51; }

.modal-box {
  width: 100%;
  max-width: 400px;
  max-height: calc((100vh - var(--sgh-top, 0px) - var(--ad-space, 0px)) - 48px);
  max-height: calc((100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px)) - 48px);
  overflow-y: auto;
  background: linear-gradient(#1c3145, #12212f);
  border: 1px solid rgba(232, 197, 106, .4);
  border-radius: 18px;
  padding: 20px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .6);
}

.modal-box-small { max-width: 320px; }

.modal-title {
  font-size: 18px;
  font-weight: 800;
  color: var(--gold);
  text-align: center;
  margin-bottom: 14px;
}

.menu-section {
  margin-bottom: 16px;
  padding-bottom: 4px;
}

.menu-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.menu-label { font-size: 13px; font-weight: 700; color: #d9e2e8; }

.menu-note {
  font-size: 10px;
  color: #8fa0aa;
  margin: -4px 0 10px;
}

.seg {
  display: flex;
  background: rgba(0, 0, 0, .3);
  border-radius: 10px;
  padding: 3px;
  gap: 3px;
}

.seg-btn {
  border: none;
  background: transparent;
  color: #a9bcc4;
  font-size: 12px;
  font-weight: 700;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.seg-btn.active {
  background: var(--gold);
  color: #1c2530;
}

.menu-subtitle {
  font-size: 13px;
  font-weight: 800;
  color: var(--gold);
  margin-bottom: 8px;
}

.stats-diff-label { font-size: 11px; color: #a9bcc4; font-weight: 700; margin-left: 4px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.sg-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  background: rgba(0, 0, 0, .25);
  border-radius: 10px;
  padding: 8px 4px;
}

.sg-value { font-size: 16px; font-weight: 800; color: #fff; font-variant-numeric: tabular-nums; }
.sg-label { font-size: 9px; color: #8fa0aa; margin-top: 2px; }

.modal-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.btn {
  border: none;
  border-radius: 12px;
  padding: 12px;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .1s, filter .15s;
}

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

.btn-primary {
  background: linear-gradient(#f3d98b, #d8ab4a);
  color: #33260c;
  box-shadow: 0 3px 10px rgba(216, 171, 74, .35);
}

.btn-sub {
  background: rgba(255, 255, 255, .1);
  color: #d9e2e8;
  border: 1px solid rgba(255, 255, 255, .18);
}

.rules-hint {
  margin-top: 16px;
  padding-top: 12px;
  border-top: 1px solid rgba(255, 255, 255, .12);
}

.rules-hint p {
  font-size: 12px;
  line-height: 1.7;
  color: #b9c8d2;
}

.confirm-text {
  text-align: center;
  font-size: 13px;
  line-height: 1.7;
  color: #d9e2e8;
  margin-bottom: 14px;
}

/* 勝利モーダル */
.modal-box-win { max-width: 340px; text-align: center; }

.win-title {
  font-size: 26px;
  font-weight: 800;
  color: var(--gold);
  margin-bottom: 8px;
  animation: winPop .5s ease;
}

@keyframes winPop {
  0% { transform: scale(.5); opacity: 0; }
  70% { transform: scale(1.1); }
  100% { transform: scale(1); }
}

.win-record {
  font-size: 13px;
  font-weight: 800;
  color: #ffe9a8;
  margin-bottom: 10px;
  animation: hintPulse .8s ease-in-out infinite;
}

.win-stats {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin: 12px 0 16px;
}

#confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 55;
}

.hidden { display: none; }

/* ===== レスポンシブ ===== */
@media (max-width: 430px) {
  #logo { font-size: 13px; }
  #diff-badge { font-size: 8px; }
  #stats-bar { gap: 8px; }
  .stat { min-width: 36px; }
  .stat-value { font-size: 13px; }
}

@media (min-width: 700px) {
  #logo { font-size: 20px; }
  #diff-badge { font-size: 10px; }
  .stat-label { font-size: 10px; }
  .stat-value { font-size: 17px; }
  #stats-bar { gap: 26px; }
  .tool-btn { min-width: 84px; font-size: 12px; }
  #footer { font-size: 11px; }
}

/* 小さいカードでは下部の大ピップを隠して視認性を確保 */
.card.tiny .pip { font-size: calc(var(--cw) * .5); opacity: .8; }

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